opencode: retry one settling session export
A stage's final session write can land just after the step settles. Retry the read-only native export once after 200 ms when its envelope is invalid or names another session, so the stage records session.html instead of dropping evidence.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
@@ -120,14 +121,39 @@ func ExportSession(ctx context.Context, executable, directory, backend, id strin
|
||||
if !exported.truncated {
|
||||
messages, ok := validNativeExport(exported.envelope, id)
|
||||
if !ok {
|
||||
return ExportedSession{}, fmt.Errorf("native export returned invalid or mismatched session evidence")
|
||||
// The CLI can observe a session whose final write is still landing
|
||||
// (the stage just settled). One short, bounded retry recovers the
|
||||
// session evidence instead of dropping session.html for the stage.
|
||||
timer := time.NewTimer(200 * time.Millisecond)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
timer.Stop()
|
||||
return ExportedSession{}, ctx.Err()
|
||||
case <-timer.C:
|
||||
}
|
||||
retried, retryErr := exportNative(ctx, executable, directory, backend, id, MaxSessionExport)
|
||||
if retryErr != nil {
|
||||
if ctx.Err() != nil {
|
||||
return ExportedSession{}, ctx.Err()
|
||||
}
|
||||
return ExportedSession{}, fmt.Errorf("native session export failed: %w", retryErr)
|
||||
}
|
||||
exported = retried
|
||||
if !exported.truncated {
|
||||
messages, ok = validNativeExport(exported.envelope, id)
|
||||
if !ok {
|
||||
return ExportedSession{}, fmt.Errorf("native export returned invalid or mismatched session evidence")
|
||||
}
|
||||
}
|
||||
}
|
||||
records, err := chatMessageRecords(messages)
|
||||
if err != nil {
|
||||
return ExportedSession{}, err
|
||||
if !exported.truncated {
|
||||
records, err := chatMessageRecords(messages)
|
||||
if err != nil {
|
||||
return ExportedSession{}, err
|
||||
}
|
||||
session.Messages = exportedMessages(records)
|
||||
return session, nil
|
||||
}
|
||||
session.Messages = exportedMessages(records)
|
||||
return session, nil
|
||||
}
|
||||
// The source exceeded the cap, so the session is larger than what was read
|
||||
// and must never be presented as whole. The prefix still yields every turn
|
||||
|
||||
@@ -73,6 +73,30 @@ func TestExportSessionDecodesRolesPartsAndUnknownTypes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportSessionRetriesOneSettlingReadback(t *testing.T) {
|
||||
id := "ses_settle"
|
||||
dir := t.TempDir()
|
||||
count := filepath.Join(dir, "count")
|
||||
valid := `{"info":{"id":"ses_settle"},"messages":[{"info":{"id":"m1","role":"assistant"},"parts":[{"type":"text","text":"ok"}]}]}`
|
||||
script := "#!/bin/sh\n" +
|
||||
"[ \"$1\" = export ] && shift || { [ \"$1\" = session ] && [ \"$2\" = export ] && shift 2 || exit 8; }\n" +
|
||||
"[ \"$1\" = " + id + " ] || exit 9\n" +
|
||||
"if [ ! -f " + count + " ]; then\n" +
|
||||
" echo 1 > " + count + "\n" +
|
||||
" echo '{\"info\":{\"id\":\"wrong\"}}'\n" +
|
||||
" exit 0\n" +
|
||||
"fi\n" +
|
||||
"cat <<'EOF'\n" + valid + "\nEOF\n"
|
||||
bin := writeFixture(t, script)
|
||||
session, err := ExportSession(context.Background(), bin, dir, "v2", id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(session.Messages) != 1 || session.Messages[0].Parts[0].Text != "ok" {
|
||||
t.Fatalf("retried session = %+v", session)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportSessionRejectsMismatchedOrInvalidID(t *testing.T) {
|
||||
bin := exportFixture(t, "ses_other", `{"info":{"id":"ses_other"},"messages":[{"info":{"role":"assistant"}}]}`)
|
||||
if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", "ses_want"); err == nil {
|
||||
|
||||
Reference in New Issue
Block a user