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:
2026-10-20 15:16:19 +01:00
parent 422db96c95
commit db88cebd0b
2 changed files with 56 additions and 6 deletions
+32 -6
View File
@@ -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