Enforce runtime report contracts and recoverable session exports
This commit is contained in:
@@ -2,6 +2,9 @@ package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -73,7 +76,30 @@ func TestExportSessionDecodesRolesPartsAndUnknownTypes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportSessionRetriesOneSettlingReadback(t *testing.T) {
|
||||
func TestNativeExportEnvelopeNormalizationRetainsExactIdentity(t *testing.T) {
|
||||
for _, data := range []string{
|
||||
`{"info":{"id":"ses_ok"},"messages":[]}`,
|
||||
`{"info":{"id":"ses_ok"},"messages":{"data":[]}}`,
|
||||
`{"data":{"info":{"id":"ses_ok"},"messages":[]}}`,
|
||||
} {
|
||||
messages, ok := validNativeExport([]byte(data), "ses_ok")
|
||||
if !ok || string(messages) != "[]" {
|
||||
t.Fatalf("envelope not normalized: %s %s %v", data, messages, ok)
|
||||
}
|
||||
}
|
||||
for _, data := range []string{
|
||||
`{"info":{"id":"ses_other"},"messages":[],"data":{"info":{"id":"ses_ok"},"messages":[]}}`,
|
||||
`{"info":{"id":"ses_ok"},"messages":null}`,
|
||||
`{"info":{"id":"ses_ok"},"messages":{}}`,
|
||||
`{"data":{"info":{"id":"ses_other"},"messages":[]}}`,
|
||||
} {
|
||||
if _, ok := validNativeExport([]byte(data), "ses_ok"); ok {
|
||||
t.Fatalf("invalid identity/envelope accepted: %s", data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportSessionRetriesOneInvalidReadbackWithoutCausalClaim(t *testing.T) {
|
||||
id := "ses_settle"
|
||||
dir := t.TempDir()
|
||||
count := filepath.Join(dir, "count")
|
||||
@@ -83,7 +109,7 @@ func TestExportSessionRetriesOneSettlingReadback(t *testing.T) {
|
||||
"[ \"$1\" = " + id + " ] || exit 9\n" +
|
||||
"if [ ! -f " + count + " ]; then\n" +
|
||||
" echo 1 > " + count + "\n" +
|
||||
" echo '{\"info\":{\"id\":\"wrong\"}}'\n" +
|
||||
" echo '{\"info\":{\"id\":\"ses_settle\"},\"messages\":['\n" +
|
||||
" exit 0\n" +
|
||||
"fi\n" +
|
||||
"cat <<'EOF'\n" + valid + "\nEOF\n"
|
||||
@@ -181,3 +207,42 @@ func TestDecodeExportPrefixRejectsMismatchedID(t *testing.T) {
|
||||
t.Fatal("mismatched prefix session accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExportRecoveryRetriesOnlyTransientOrEmptyReadback(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, first string
|
||||
wantAttempts int
|
||||
empty bool
|
||||
wantError bool
|
||||
}{
|
||||
{"empty-then-ready", `echo '{"info":{"id":"ses_ok"},"messages":[]}'`, 2, false, false},
|
||||
{"temporary-then-ready", `echo 'database is locked' >&2; exit 1`, 2, false, false},
|
||||
{"wrong-session", `echo '{"info":{"id":"ses_wrong"},"messages":[]}'`, 1, false, true},
|
||||
{"wrong-session-incomplete", `echo '{"info":{"id":"ses_wrong"},"messages":['`, 1, false, true},
|
||||
{"unsupported-schema", `echo '{"info":{"id":"ses_ok"},"messages":{}}'`, 1, false, true},
|
||||
{"permanent-cli-error", `echo 'session not found' >&2; exit 1`, 1, false, true},
|
||||
{"always-empty", `echo '{"info":{"id":"ses_ok"},"messages":[]}'`, 3, true, true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
count := filepath.Join(dir, "calls")
|
||||
script := "#!/bin/sh\nn=0; [ ! -f '" + count + "' ] || n=$(cat '" + count + "'); n=$((n+1)); echo $n > '" + count + "'\n"
|
||||
if tc.empty {
|
||||
script += tc.first + "\n"
|
||||
} 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")
|
||||
if (err != nil) != tc.wantError {
|
||||
t.Fatalf("unexpected result %+v %v", session, err)
|
||||
}
|
||||
calls, _ := os.ReadFile(count)
|
||||
if strings.TrimSpace(string(calls)) != fmt.Sprint(tc.wantAttempts) || session.Attempts != tc.wantAttempts {
|
||||
t.Fatalf("attempts %s %+v", calls, session)
|
||||
}
|
||||
if tc.empty && !errors.Is(err, ErrEmptySessionExport) {
|
||||
t.Fatalf("not explicitly empty: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user