| 1 | package session |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/provider" |
| 9 | ) |
| 10 | |
| 11 | // This test is also copied unchanged into the v1.39.8 and v1.39.9 source |
| 12 | // snapshots by scripts/check-provider-diagnostic-compatibility.sh. Each reader |
| 13 | // must replay the other writer's actual framed store, then append and reopen it. |
| 14 | func TestProviderDiagnosticCrossVersionFixture(t *testing.T) { |
| 15 | dir := os.Getenv("REASONIX_COMPAT_STORE") |
| 16 | if dir == "" { |
| 17 | t.Skip("run scripts/check-provider-diagnostic-compatibility.sh") |
| 18 | } |
| 19 | kind := os.Getenv("REASONIX_COMPAT_KIND") |
| 20 | mode := os.Getenv("REASONIX_COMPAT_MODE") |
| 21 | s, err := Open(dir, "provider-compat") |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | t.Cleanup(func() { _ = s.Close(t.Context()) }) |
| 26 | if mode == "write" { |
| 27 | cause := "" |
| 28 | if kind == "image_input" { |
| 29 | cause = "Image processing failed: configured image understanding model does not accept images" |
| 30 | } |
| 31 | payload, err := json.Marshal(map[string]any{"message": provider.Message{ |
| 32 | ID: "failure", Role: provider.RoleTool, LocalOnly: true, |
| 33 | InterruptedTurn: &provider.InterruptedTurnRecovery{Pending: true, TerminalStatus: "failed", Cause: cause, FailureDiagnostic: &provider.FailureDiagnostic{Kind: kind, ProviderID: "deepseek"}}, |
| 34 | }}) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | diagnostic := json.RawMessage(`{"schemaVersion":1,"requests":[]}`) |
| 39 | if kind == "request_send_timeout" { |
| 40 | diagnostic = json.RawMessage(`{"schemaVersion":2,"requests":[{"errorCode":"request_send_timeout","errorPhase":"request_write","timeoutSource":"send_guard","sendTimeoutMs":300000,"sendElapsedMs":300001}]}`) |
| 41 | } |
| 42 | _, err = s.Append(t.Context(), Batch{OperationID: "fixture", Events: []Event{ |
| 43 | {Kind: "message/complete", Payload: payload}, |
| 44 | {Kind: "diagnostic/provider", Optional: true, Payload: diagnostic}, |
| 45 | }}) |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | } else { |
| 50 | commits, err := Replay(dir, nil) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | projection, err := Project(commits) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | messages := projection.Messages |
| 59 | if len(messages) != 1 || messages[0].InterruptedTurn == nil || messages[0].InterruptedTurn.FailureDiagnostic.Kind != kind { |
| 60 | t.Fatalf("old/new reader lost failure: %+v", messages) |
| 61 | } |
| 62 | if kind == "image_input" && messages[0].InterruptedTurn.Cause != "Image processing failed: configured image understanding model does not accept images" { |
| 63 | t.Fatal("reader lost original image cause") |
| 64 | } |
| 65 | payload, _ := json.Marshal(map[string]any{"message": provider.Message{ID: "continued", Role: provider.RoleUser, Content: "continue"}}) |
| 66 | if _, err = s.Append(t.Context(), Batch{OperationID: "continue", Events: []Event{{Kind: "message/complete", Payload: payload}}}); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | } |
| 70 | if err = s.Close(t.Context()); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | commits, err := Replay(dir, nil) |
| 74 | if err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | projection, err := Project(commits) |
| 78 | want := 1 |
| 79 | if mode != "write" { |
| 80 | want = 2 |
| 81 | } |
| 82 | if err != nil || len(projection.Messages) != want { |
| 83 | t.Fatalf("reopen: messages=%d err=%v", len(projection.Messages), err) |
| 84 | } |
| 85 | } |
| 86 |