| 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/attachment" |
| 13 | "reasonix/internal/config" |
| 14 | "reasonix/internal/event" |
| 15 | "reasonix/internal/provider" |
| 16 | ) |
| 17 | |
| 18 | func replayFixtureMessages(role provider.Role) []provider.Message { |
| 19 | image := provider.Message{ID: "image", Role: role, Content: "original text", ImageInputs: []attachment.ImageInput{{Kind: attachment.KindURL, URL: "https://fixture.invalid/image.png"}}} |
| 20 | text := provider.Message{ID: "text", Role: provider.RoleUser, Content: "plain follow-up"} |
| 21 | if role == provider.RoleTool { |
| 22 | return []provider.Message{text, image} |
| 23 | } |
| 24 | return []provider.Message{image, text} |
| 25 | } |
| 26 | |
| 27 | func TestHistoricalImageCanRecoverWithoutRewritingOrNegativeCaching(t *testing.T) { |
| 28 | broken := true |
| 29 | c := newOwnedTestController(t, Options{WorkspaceRoot: t.TempDir(), ImageRouteConfig: config.Default(), VisionModel: "fixture/vision", |
| 30 | VisionProviderResolver: func(string) (provider.Provider, error) { |
| 31 | if broken { |
| 32 | return nil, errors.New("image service unavailable") |
| 33 | } |
| 34 | return visionSummaryTestProvider{}, nil |
| 35 | }, |
| 36 | }) |
| 37 | messages := replayFixtureMessages(provider.RoleUser) |
| 38 | before, _ := json.Marshal(messages) |
| 39 | first, err := c.ResolveRequestImagesForModel(t.Context(), messages, "fixture/main", false) |
| 40 | if err != nil || !strings.Contains(first[0].Content, "unavailable") { |
| 41 | t.Fatalf("degraded request: %v", err) |
| 42 | } |
| 43 | broken = false |
| 44 | recovered, err := c.ResolveRequestImagesForModel(t.Context(), messages, "fixture/main", false) |
| 45 | if err != nil || !strings.Contains(recovered[0].Content, "Revenue 42") || strings.Contains(recovered[0].Content, "unavailable") { |
| 46 | t.Fatalf("repaired image stayed unavailable: %+v %v", recovered, err) |
| 47 | } |
| 48 | after, _ := json.Marshal(messages) |
| 49 | if !bytes.Equal(before, after) { |
| 50 | t.Fatal("canonical image history mutated") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | type replayFailureProvider struct{ err error } |
| 55 | |
| 56 | func (replayFailureProvider) Name() string { return "vision-fixture" } |
| 57 | func (replayFailureProvider) ModelInfo() provider.ModelInfo { |
| 58 | return provider.ModelInfo{InputModalities: []provider.ModelModality{provider.ModalityImage}} |
| 59 | } |
| 60 | func (p replayFailureProvider) Stream(context.Context, provider.Request) (<-chan provider.Chunk, error) { |
| 61 | ch := make(chan provider.Chunk, 1) |
| 62 | if p.err != nil { |
| 63 | ch <- provider.Chunk{Type: provider.ChunkError, Err: p.err} |
| 64 | } |
| 65 | close(ch) |
| 66 | return ch, nil |
| 67 | } |
| 68 | |
| 69 | func TestImageUnderstandingFailureDoesNotPoisonTextOrToolResults(t *testing.T) { |
| 70 | for _, role := range []provider.Role{provider.RoleUser, provider.RoleTool} { |
| 71 | for _, failure := range []string{"resolution", "capability", "upstream", "timeout", "empty"} { |
| 72 | t.Run(string(role)+"/"+failure, func(t *testing.T) { |
| 73 | var notices []event.Event |
| 74 | c := newOwnedTestController(t, Options{WorkspaceRoot: t.TempDir(), ImageRouteConfig: config.Default(), VisionModel: "fixture/vision", |
| 75 | Sink: event.FuncSink(func(e event.Event) { |
| 76 | if e.Code == event.NoticeCodeImageReplayUnavailable { |
| 77 | notices = append(notices, e) |
| 78 | } |
| 79 | }), |
| 80 | VisionProviderResolver: func(string) (provider.Provider, error) { |
| 81 | switch failure { |
| 82 | case "resolution": |
| 83 | return nil, errors.New("unknown vision model fixture/vision") |
| 84 | case "capability": |
| 85 | return &reviewImageProvider{textOnly: true}, nil |
| 86 | case "upstream": |
| 87 | return replayFailureProvider{err: &provider.APIError{Status: 503, Body: `{"error":{"message":"image service unavailable"}}`}}, nil |
| 88 | case "empty": |
| 89 | return replayFailureProvider{}, nil |
| 90 | default: |
| 91 | return replayFailureProvider{err: context.DeadlineExceeded}, nil |
| 92 | } |
| 93 | }}) |
| 94 | messages := replayFixtureMessages(role) |
| 95 | before, _ := json.Marshal(messages) |
| 96 | var first []byte |
| 97 | for attempt := range 2 { |
| 98 | got, err := c.ResolveRequestImagesForModel(t.Context(), messages, "fixture/main", false) |
| 99 | if err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | i := 0 |
| 103 | if role == provider.RoleTool { |
| 104 | i = 1 |
| 105 | } |
| 106 | if len(got[i].ImageInputs) != 0 || len(got[i].Images) != 0 || !strings.Contains(got[i].Content, "original text") || !strings.Contains(got[i].Content, "not") { |
| 107 | t.Fatalf("bad fallback: %+v", got) |
| 108 | } |
| 109 | if role == provider.RoleTool && !strings.Contains(got[i].Content, "already executed") { |
| 110 | t.Fatal("lost tool execution evidence") |
| 111 | } |
| 112 | wire, _ := json.Marshal(provider.ModelMessages(got)) |
| 113 | if attempt == 0 { |
| 114 | first = wire |
| 115 | } else if !bytes.Equal(first, wire) { |
| 116 | t.Fatal("repeated failure changed provider-visible prefix") |
| 117 | } |
| 118 | } |
| 119 | after, _ := json.Marshal(messages) |
| 120 | if !bytes.Equal(before, after) { |
| 121 | t.Fatal("rewrote canonical history") |
| 122 | } |
| 123 | if len(notices) != 2 || notices[0].Detail == "" || notices[0].Diagnostic != nil { |
| 124 | t.Fatalf("missing visible diagnosis: %+v", notices) |
| 125 | } |
| 126 | current := []provider.Message{messages[0]} |
| 127 | if role == provider.RoleTool { |
| 128 | current = []provider.Message{messages[1]} |
| 129 | current[0].Role = provider.RoleUser |
| 130 | } |
| 131 | if _, err := c.ResolveRequestImagesForModel(t.Context(), current, "fixture/main", false); err == nil || provider.DiagnoseFailure(err).Kind != provider.FailureKindImageInput { |
| 132 | t.Fatalf("current user image silently omitted: %v", err) |
| 133 | } |
| 134 | }) |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func TestImageReplayPreservesCancellationAndHealthySiblings(t *testing.T) { |
| 140 | for _, cancelTurn := range []bool{false, true} { |
| 141 | ctx, cancel := context.WithCancel(t.Context()) |
| 142 | c := newOwnedTestController(t, Options{WorkspaceRoot: t.TempDir(), ImageRouteConfig: config.Default(), VisionModel: "fixture/vision", |
| 143 | VisionProviderResolver: func(string) (provider.Provider, error) { |
| 144 | if cancelTurn { |
| 145 | cancel() |
| 146 | return nil, errors.New("concurrent failure") |
| 147 | } |
| 148 | return nil, context.Canceled |
| 149 | }}) |
| 150 | _, err := c.ResolveRequestImagesForModel(ctx, replayFixtureMessages(provider.RoleUser), "fixture/main", false) |
| 151 | cancel() |
| 152 | if !errors.Is(err, context.Canceled) { |
| 153 | t.Fatalf("cancellation swallowed: %v", err) |
| 154 | } |
| 155 | } |
| 156 | ctx, cancel := context.WithDeadline(t.Context(), time.Now().Add(-time.Second)) |
| 157 | defer cancel() |
| 158 | cancelled := newOwnedTestController(t, Options{WorkspaceRoot: t.TempDir(), ImageRouteConfig: config.Default(), VisionModel: "fixture/vision"}) |
| 159 | if _, err := cancelled.ResolveRequestImagesForModel(ctx, replayFixtureMessages(provider.RoleUser), "fixture/main", false); !errors.Is(err, context.DeadlineExceeded) { |
| 160 | t.Fatalf("parent deadline lost: %v", err) |
| 161 | } |
| 162 | calls := 0 |
| 163 | c := newOwnedTestController(t, Options{WorkspaceRoot: t.TempDir(), ImageRouteConfig: config.Default(), VisionModel: "fixture/vision", |
| 164 | VisionProviderResolver: func(string) (provider.Provider, error) { |
| 165 | calls++ |
| 166 | if calls == 1 { |
| 167 | return nil, errors.New("bad earlier image") |
| 168 | } |
| 169 | return visionSummaryTestProvider{}, nil |
| 170 | }}) |
| 171 | ms := replayFixtureMessages(provider.RoleUser) |
| 172 | ms = append(ms[:1], append([]provider.Message{ms[0]}, ms[1:]...)...) |
| 173 | got, err := c.ResolveRequestImagesForModel(t.Context(), ms, "fixture/main", false) |
| 174 | if err != nil || !strings.Contains(got[1].Content, "Revenue 42") || !strings.Contains(got[0].Content, "unavailable") { |
| 175 | t.Fatalf("healthy sibling lost: %+v %v", got, err) |
| 176 | } |
| 177 | } |
| 178 |