| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "sync" |
| 10 | "sync/atomic" |
| 11 | "testing" |
| 12 | |
| 13 | "reasonix/internal/agent" |
| 14 | "reasonix/internal/attachment" |
| 15 | "reasonix/internal/config" |
| 16 | "reasonix/internal/event" |
| 17 | "reasonix/internal/provider" |
| 18 | "reasonix/internal/tool" |
| 19 | ) |
| 20 | |
| 21 | type imageReplayRoundProvider struct { |
| 22 | scriptedTurns |
| 23 | requests []provider.Request |
| 24 | } |
| 25 | |
| 26 | func (p *imageReplayRoundProvider) Stream(ctx context.Context, req provider.Request) (<-chan provider.Chunk, error) { |
| 27 | p.requests = append(p.requests, req) |
| 28 | return p.scriptedTurns.Stream(ctx, req) |
| 29 | } |
| 30 | |
| 31 | func TestImageReplayWarningOncePerTurnAcrossToolRounds(t *testing.T) { |
| 32 | for _, native := range []bool{false, true} { |
| 33 | t.Run(map[bool]string{false: "image-understanding", true: "native-attachment"}[native], func(t *testing.T) { |
| 34 | root := t.TempDir() |
| 35 | reg := tool.NewRegistry() |
| 36 | reg.Add(minimalFakeTool{name: "read_file", readOnly: true}) |
| 37 | p := &imageReplayRoundProvider{scriptedTurns: scriptedTurns{turns: [][]provider.Chunk{ |
| 38 | {toolCallChunk("read-1", "read_file", `{}`), {Type: provider.ChunkDone}}, textTurn("finished first turn"), |
| 39 | {toolCallChunk("read-2", "read_file", `{}`), {Type: provider.ChunkDone}}, textTurn("finished second turn"), |
| 40 | }}} |
| 41 | exec := agent.New(p, reg, agent.NewSession("stable system"), agent.Options{ModelRef: "fixture/main"}, event.Discard) |
| 42 | var notices atomic.Int32 |
| 43 | resolutions := 0 |
| 44 | c := newOwnedTestController(t, Options{WorkspaceRoot: root, SessionPath: filepath.Join(root, "session.jsonl"), Runner: exec, Executor: exec, ModelRef: "fixture/main", |
| 45 | FrozenImageInput: &native, ImageRouteConfig: config.Default(), VisionModel: "fixture/vision", |
| 46 | VisionProviderResolver: func(string) (provider.Provider, error) { |
| 47 | resolutions++ |
| 48 | return nil, errors.New("unknown vision model fixture/vision") |
| 49 | }, Sink: event.FuncSink(func(e event.Event) { |
| 50 | if e.Code == event.NoticeCodeImageReplayUnavailable { |
| 51 | notices.Add(1) |
| 52 | } |
| 53 | }), |
| 54 | }) |
| 55 | image := replayFixtureMessages(provider.RoleUser)[0] |
| 56 | if native { |
| 57 | draft, err := c.StageImage(t.Context(), "old.png", "image/png", "data:image/png;base64,"+tinyPNG) |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | digest := draft.Ref.Content.Digest |
| 62 | if err := os.Remove(filepath.Join(c.attachmentService().Store().Root(), "objects", digest[:2], digest[2:4], digest)); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | image.ImageInputs = []attachment.ImageInput{{Kind: attachment.KindAttachment, Attachment: &draft.Ref}} |
| 66 | } |
| 67 | if err := c.RecordSessionMessages(t.Context(), "historical-image", []provider.Message{image}); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | exec.Session().Add(image) |
| 71 | for turn := range 2 { |
| 72 | if err := c.RunTurn(t.Context(), "continue with text"); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if got := notices.Load(); got != int32(turn+1) { |
| 76 | t.Fatalf("turn %d: notices=%d, want %d", turn, got, turn+1) |
| 77 | } |
| 78 | persisted := 0 |
| 79 | for _, commit := range terminationCommitHistory(t, c) { |
| 80 | for _, e := range commit.Events { |
| 81 | if e.Kind == "diagnostic" && strings.Contains(string(e.Payload), event.NoticeCodeImageReplayUnavailable) { |
| 82 | persisted++ |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | if persisted != turn+1 || len(p.requests) != (turn+1)*2 { |
| 87 | t.Fatalf("turn %d: persisted=%d requests=%d", turn, persisted, len(p.requests)) |
| 88 | } |
| 89 | for round, req := range p.requests[turn*2:] { |
| 90 | assertImageReplayRound(t, req, round == 1) |
| 91 | } |
| 92 | } |
| 93 | if !native && resolutions != 4 { |
| 94 | t.Fatalf("image failure was cached: resolutions=%d, want 4", resolutions) |
| 95 | } |
| 96 | }) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func assertImageReplayRound(t *testing.T, req provider.Request, wantTool bool) { |
| 101 | t.Helper() |
| 102 | degraded, toolResult := false, false |
| 103 | for _, m := range req.Messages { |
| 104 | degraded = degraded || strings.Contains(m.Content, "original text") && strings.Contains(m.Content, "unavailable") |
| 105 | toolResult = toolResult || m.Role == provider.RoleTool |
| 106 | if len(m.ImageInputs)+len(m.Images) != 0 { |
| 107 | t.Fatal("broken image reached provider") |
| 108 | } |
| 109 | } |
| 110 | if !degraded || (wantTool && !toolResult) { |
| 111 | t.Fatalf("degraded=%v toolResult=%v, wantTool=%v", degraded, toolResult, wantTool) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestImageReplayWarningDeduplicatesRedactedCausesConcurrently(t *testing.T) { |
| 116 | var notices atomic.Int32 |
| 117 | c := &Controller{sink: event.FuncSink(func(event.Event) { notices.Add(1) })} |
| 118 | ctx := withImageReplayWarnings(t.Context()) |
| 119 | var wg sync.WaitGroup |
| 120 | for range 8 { |
| 121 | wg.Go(func() { c.emitImageReplayWarning(ctx, errors.New("image failed API_KEY=sk-fixture-secret-123456")) }) |
| 122 | } |
| 123 | wg.Wait() |
| 124 | c.emitImageReplayWarning(ctx, errors.New("image failed API_KEY=sk-fixture-other-123456")) |
| 125 | if notices.Load() != 1 { |
| 126 | t.Fatalf("equivalent redacted causes emitted %d notices", notices.Load()) |
| 127 | } |
| 128 | c.emitImageReplayWarning(ctx, errors.New("different image failure")) |
| 129 | if notices.Load() != 2 { |
| 130 | t.Fatal("distinct failure cause was hidden") |
| 131 | } |
| 132 | c.emitImageReplayWarning(withImageReplayWarnings(ctx), errors.New("different image failure")) |
| 133 | if notices.Load() != 3 { |
| 134 | t.Fatal("new turn inherited warning suppression") |
| 135 | } |
| 136 | } |
| 137 |