fix(opencode): read V2 session exports directly from authenticated API

This commit is contained in:
2026-10-20 20:40:57 +01:00
parent 074b5bb087
commit 1b8d0f2a75
7 changed files with 339 additions and 37 deletions
+11 -12
View File
@@ -33,11 +33,11 @@ func TestExportSessionDecodesRolesPartsAndUnknownTypes(t *testing.T) {
`{"type":"file","uri":"file:///tmp/x"}]},` +
`{"info":{"id":"m2","role":"user"},"text":"hi"}]}`
bin := exportFixture(t, id, body)
session, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", id)
session, err := ExportSession(context.Background(), bin, t.TempDir(), "v1", id)
if err != nil {
t.Fatal(err)
}
if session.ID != id || session.Backend != "v2" || session.Truncated {
if session.ID != id || session.Backend != "v1" || session.Truncated {
t.Fatalf("unexpected session: %+v", session)
}
if len(session.Messages) != 2 {
@@ -114,7 +114,7 @@ func TestExportSessionRetriesOneInvalidReadbackWithoutCausalClaim(t *testing.T)
"fi\n" +
"cat <<'EOF'\n" + valid + "\nEOF\n"
bin := writeFixture(t, script)
session, err := ExportSession(context.Background(), bin, dir, "v2", id)
session, err := ExportSession(context.Background(), bin, dir, "v1", id)
if err != nil {
t.Fatal(err)
}
@@ -125,17 +125,17 @@ func TestExportSessionRetriesOneInvalidReadbackWithoutCausalClaim(t *testing.T)
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 {
if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v1", "ses_want"); err == nil {
t.Fatal("mismatched session export accepted")
}
if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", "not-a-session"); err == nil {
if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v1", "not-a-session"); err == nil {
t.Fatal("invalid session id accepted without running the CLI")
}
}
func TestExportSessionFailsSanitizedWhenCLIUnavailable(t *testing.T) {
bin := filepath.Join(t.TempDir(), "absent-opencode")
if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", "ses_ok"); err == nil {
if _, err := ExportSession(context.Background(), bin, t.TempDir(), "v1", "ses_ok"); err == nil {
t.Fatal("missing CLI must fail the export")
}
}
@@ -144,7 +144,7 @@ func TestExportNativeReportsBoundedOverflow(t *testing.T) {
id := "ses_ok"
body := `{"info":{"id":"ses_ok"},"messages":[{"info":{"role":"assistant"},"parts":[{"type":"text","text":"` + strings.Repeat("x", 256) + `"}]}]}`
bin := exportFixture(t, id, body)
res, err := exportNative(context.Background(), bin, t.TempDir(), "v2", id, 32)
res, err := exportNative(context.Background(), bin, t.TempDir(), "v1", id, 32)
if err != nil {
t.Fatal(err)
}
@@ -160,9 +160,8 @@ func TestExportNativeUsesBackendSubcommand(t *testing.T) {
id := "ses_ok"
body := `{"info":{"id":"ses_ok"},"messages":[{"info":{"role":"assistant"},"parts":[{"type":"text","text":"hello"}]}]}`
bin := exportFixture(t, id, body)
// Both a v1 (top-level export) and a v2 (session export) CLI accept the one
// shared invocation.
for _, backend := range []string{"v1", "v2"} {
// V1 retains its top-level CLI export. V2 is tested with HTTP below.
for _, backend := range []string{"v1"} {
session, err := ExportSession(context.Background(), bin, t.TempDir(), backend, id)
if err != nil || len(session.Messages) != 1 {
t.Fatalf("backend %s export failed: %v %+v", backend, err, session)
@@ -177,7 +176,7 @@ func TestExportSessionKeepsCompleteTurnsFromOverCapSource(t *testing.T) {
`{"info":{"role":"assistant"},"parts":[{"type":"text","text":"first"}]},` +
`{"info":{"role":"user"},"parts":[{"type":"text","text":"` + huge + `"}]}]}`
bin := exportFixture(t, id, body)
session, err := ExportSession(context.Background(), bin, t.TempDir(), "v2", id)
session, err := ExportSession(context.Background(), bin, t.TempDir(), "v1", id)
if err != nil {
t.Fatal(err)
}
@@ -232,7 +231,7 @@ func TestExportRecoveryRetriesOnlyTransientOrEmptyReadback(t *testing.T) {
} else {
script += "if [ $n = 1 ]; then " + tc.first + "; else echo '{\"info\":{\"id\":\"ses_ok\"},\"messages\":[{\"info\":{\"role\":\"assistant\"},\"parts\":[{\"type\":\"text\",\"text\":\"ready\"}]}]}'; fi\n"
}
session, err := ExportSession(context.Background(), writeFixture(t, script), dir, "v2", "ses_ok")
session, err := ExportSession(context.Background(), writeFixture(t, script), dir, "v1", "ses_ok")
if (err != nil) != tc.wantError {
t.Fatalf("unexpected result %+v %v", session, err)
}