| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "reasonix/internal/agent" |
| 13 | "reasonix/internal/config" |
| 14 | "reasonix/internal/event" |
| 15 | "reasonix/internal/provider" |
| 16 | "reasonix/internal/session" |
| 17 | "reasonix/internal/tool" |
| 18 | "reasonix/internal/transcript" |
| 19 | ) |
| 20 | |
| 21 | func TestImageFailureSurvivesCleanupAndColdExport(t *testing.T) { |
| 22 | for _, historical := range []bool{false, true} { |
| 23 | t.Run(map[bool]string{true: "historical-text-continues", false: "current-image-fails"}[historical], func(t *testing.T) { |
| 24 | service, err := session.NewService("local", session.NewFilesystemPersistence(t.TempDir())) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | runtime, err := service.Create(t.Context(), session.CreateOptions{SessionID: "image-failure"}) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | p := &reviewImageProvider{requests: make(chan provider.Request, 8), textOnly: true} |
| 33 | exec := agent.New(p, tool.NewRegistry(), agent.NewSession("stable system"), agent.Options{ModelRef: "fixture/main"}, event.Discard) |
| 34 | c := newOwnedTestController(t, Options{WorkspaceRoot: t.TempDir(), Runner: exec, Executor: exec, ModelRef: "fixture/main", |
| 35 | SessionService: service, SessionRuntime: runtime, ExclusiveSession: true, |
| 36 | ImageRouteConfig: config.Default(), VisionModel: "fixture/vision", |
| 37 | VisionProviderResolver: func(string) (provider.Provider, error) { |
| 38 | return nil, errors.New("unknown vision model fixture/vision API_KEY=sk-fixture-secret-123456") |
| 39 | }, |
| 40 | }) |
| 41 | image := replayFixtureMessages(provider.RoleUser)[0] |
| 42 | ctx := t.Context() |
| 43 | if historical { |
| 44 | if err := c.RecordSessionMessages(ctx, "historical-image", []provider.Message{image}); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | exec.Session().Add(image) |
| 48 | } else { |
| 49 | ctx = agent.WithUserImageInputs(ctx, image.ImageInputs) |
| 50 | } |
| 51 | err = c.RunTurn(ctx, "plain follow-up") |
| 52 | if historical { |
| 53 | if err != nil || len(p.requests) != 1 { |
| 54 | t.Fatalf("text did not reach main provider: %v requests=%d", err, len(p.requests)) |
| 55 | } |
| 56 | req := <-p.requests |
| 57 | foundHistory := false |
| 58 | for _, m := range req.Messages { |
| 59 | foundHistory = foundHistory || strings.Contains(m.Content, "original text") && strings.Contains(m.Content, "unavailable") |
| 60 | if len(m.ImageInputs)+len(m.Images) != 0 { |
| 61 | t.Fatal("broken image reached text model") |
| 62 | } |
| 63 | } |
| 64 | if !foundHistory { |
| 65 | t.Fatal("request did not exercise historical image replay") |
| 66 | } |
| 67 | } else { |
| 68 | if err == nil || len(p.requests) != 0 { |
| 69 | t.Fatalf("current attachment was silently omitted: %v", err) |
| 70 | } |
| 71 | rows := transcript.History(c.sessionEventStore().Snapshot().Projection.Messages, transcript.HistoryOptions{}) |
| 72 | found := false |
| 73 | for _, row := range rows { |
| 74 | if row.Code == event.NoticeCodeProviderRequestFailed { |
| 75 | found = strings.Contains(row.Detail, "unknown vision model fixture/vision") && row.Diagnostic.Kind == provider.FailureKindImageInput |
| 76 | } |
| 77 | } |
| 78 | if !found { |
| 79 | t.Fatalf("cleanup lost original detail: %+v", rows) |
| 80 | } |
| 81 | } |
| 82 | ref := runtime.Ref() |
| 83 | if !historical { |
| 84 | for _, commit := range terminationCommitHistory(t, c) { |
| 85 | for _, e := range commit.Events { |
| 86 | if e.Kind == "diagnostic" && strings.Contains(string(e.Payload), event.NoticeCodeProviderRequestFailed) { |
| 87 | t.Fatal("canonical failure duplicated as display notice") |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | c.Close() |
| 93 | <-c.closeFinalized |
| 94 | if err := service.CloseAll(context.Background()); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | snapshot, err := service.Query().CaptureDiagnosticSnapshot(t.Context(), ref) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | var out bytes.Buffer |
| 102 | if err := WriteColdSessionDiagnostics(t.Context(), &out, service.Query(), snapshot, GoalDiagnosticMetadata{}, nil); err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | if !strings.Contains(out.String(), "unknown vision model fixture/vision") || strings.Contains(out.String(), "sk-fixture-secret-123456") { |
| 106 | t.Fatalf("cold export lost/redaction failed: %s", out.String()) |
| 107 | } |
| 108 | if historical && !strings.Contains(out.String(), event.NoticeCodeImageReplayUnavailable) { |
| 109 | t.Fatal("recovered image warning not persisted") |
| 110 | } |
| 111 | assertColdImageFailureDetails(t, service, ref) |
| 112 | }) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func TestDesktopImagePrepassFailureIsDurableBeforeAgentRun(t *testing.T) { |
| 117 | service, err := session.NewService("local", session.NewFilesystemPersistence(t.TempDir())) |
| 118 | if err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | t.Cleanup(func() { _ = service.CloseAll(context.Background()) }) |
| 122 | runtime, err := service.Create(t.Context(), session.CreateOptions{SessionID: "desktop-image-prepass"}) |
| 123 | if err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | p := &reviewImageProvider{requests: make(chan provider.Request, 1), textOnly: true} |
| 127 | exec := agent.New(p, tool.NewRegistry(), agent.NewSession("system"), agent.Options{ModelRef: "fixture/main"}, event.Discard) |
| 128 | done := make(chan event.Event, 1) |
| 129 | native := false |
| 130 | c := newOwnedTestController(t, Options{WorkspaceRoot: t.TempDir(), Runner: exec, Executor: exec, ModelRef: "fixture/main", FrozenImageInput: &native, |
| 131 | SessionService: service, SessionRuntime: runtime, ExclusiveSession: true, ImageRouteConfig: config.Default(), VisionModel: "fixture/vision", |
| 132 | VisionProviderResolver: func(string) (provider.Provider, error) { return nil, errors.New("unknown vision model fixture/vision") }, |
| 133 | Sink: event.FuncSink(func(e event.Event) { |
| 134 | if e.Kind == event.TurnDone { |
| 135 | done <- e |
| 136 | } |
| 137 | }), |
| 138 | }) |
| 139 | draft, err := c.StageImage(t.Context(), "image.png", "image/png", "data:image/png;base64,"+tinyPNG) |
| 140 | if err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | if _, err := c.SubmitIdentified(SubmissionRequest{Input: "inspect current image", Attachments: []SubmissionAttachment{{ClientAttachmentID: "image", DraftID: draft.ID}}}); err != nil { |
| 144 | t.Fatal(err) |
| 145 | } |
| 146 | terminal := waitTurnDoneEvent(t, done) |
| 147 | waitIdleAdmission(t, c) |
| 148 | if terminal.Err == nil || len(p.requests) != 0 { |
| 149 | t.Fatal("prepass did not stop main provider") |
| 150 | } |
| 151 | count := 0 |
| 152 | for _, commit := range terminationCommitHistory(t, c) { |
| 153 | for _, e := range commit.Events { |
| 154 | if e.Kind != "diagnostic" { |
| 155 | continue |
| 156 | } |
| 157 | var body struct { |
| 158 | Record transcript.Message `json:"displayRecord"` |
| 159 | } |
| 160 | if err := json.Unmarshal(e.Payload, &body); err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | if body.Record.Code != event.NoticeCodeProviderRequestFailed { |
| 164 | continue |
| 165 | } |
| 166 | count++ |
| 167 | if !strings.Contains(body.Record.Detail, "unknown vision model fixture/vision") || body.Record.Diagnostic.Kind != provider.FailureKindImageInput || commit.Events[len(commit.Events)-1].Kind != "turn/end" { |
| 168 | t.Fatalf("prepass detail not atomic: %+v", body.Record) |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | if count != 1 { |
| 173 | t.Fatalf("persisted prepass notices=%d", count) |
| 174 | } |
| 175 | c.Close() |
| 176 | <-c.closeFinalized |
| 177 | if err := service.CloseAll(context.Background()); err != nil { |
| 178 | t.Fatal(err) |
| 179 | } |
| 180 | assertColdImageFailureDetails(t, service, runtime.Ref()) |
| 181 | } |
| 182 | |
| 183 | func assertColdImageFailureDetails(t *testing.T, service *session.Service, ref session.SessionRef) { |
| 184 | t.Helper() |
| 185 | deadline := time.Now().Add(10 * time.Second) |
| 186 | for { |
| 187 | page, err := service.Query().ReadHistoryWindow(t.Context(), ref, session.HistoryWindowRequest{Anchor: "newest"}) |
| 188 | if err != nil { |
| 189 | t.Fatal(err) |
| 190 | } |
| 191 | if page.Status == "preparing" && time.Now().Before(deadline) { |
| 192 | time.Sleep(time.Millisecond) |
| 193 | continue |
| 194 | } |
| 195 | if page.Status != "ready" { |
| 196 | t.Fatalf("cold history status=%s", page.Status) |
| 197 | } |
| 198 | count := 0 |
| 199 | for _, m := range page.Messages { |
| 200 | body := m.Inline |
| 201 | if m.ContentRef != nil { |
| 202 | body, err = service.Query().ReadContent(t.Context(), ref, *m.ContentRef, 0, m.ContentRef.Bytes) |
| 203 | if err != nil { |
| 204 | t.Fatal(err) |
| 205 | } |
| 206 | } |
| 207 | if bytes.Contains(body, []byte("unknown vision model fixture/vision")) { |
| 208 | count++ |
| 209 | } |
| 210 | } |
| 211 | if count != 1 { |
| 212 | t.Fatalf("cold history causes=%d, want one visible failure", count) |
| 213 | } |
| 214 | return |
| 215 | } |
| 216 | } |
| 217 |