| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "path/filepath" |
| 6 | "reflect" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/event" |
| 11 | "reasonix/internal/provider" |
| 12 | ) |
| 13 | |
| 14 | func TestCoveredPrefixHashIgnoresLocalToolRawContent(t *testing.T) { |
| 15 | msgs := []provider.Message{ |
| 16 | {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "call-1", Name: "read", Arguments: `{}`}}}, |
| 17 | {Role: provider.RoleTool, ToolCallID: "call-1", Name: "read", Content: "bounded", RawContent: "full result A"}, |
| 18 | } |
| 19 | first := coveredPrefixHash(msgs, len(msgs)) |
| 20 | msgs[1].RawContent = "full result B" |
| 21 | if second := coveredPrefixHash(msgs, len(msgs)); second != first { |
| 22 | t.Fatal("local RawContent edit changed the provider-visible covered-prefix hash") |
| 23 | } |
| 24 | msgs[1].Content = "different bounded result" |
| 25 | if second := coveredPrefixHash(msgs, len(msgs)); second == first { |
| 26 | t.Fatal("provider-visible Content edit did not invalidate the covered-prefix hash") |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestLoadProjectionSidecarMigratesPromotedV3ToolHash(t *testing.T) { |
| 31 | dir := t.TempDir() |
| 32 | path := filepath.Join(dir, "session.jsonl") |
| 33 | msgs := []provider.Message{ |
| 34 | {Role: provider.RoleSystem, Content: "system"}, |
| 35 | {Role: provider.RoleUser, Content: "task"}, |
| 36 | {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "call-1", Name: "read", Arguments: `{}`}}}, |
| 37 | {Role: provider.RoleTool, ToolCallID: "call-1", Name: "read", Content: "bounded", RawContent: "full result"}, |
| 38 | } |
| 39 | legacyHash := promotedCoveredPrefixHash(msgs, len(msgs)) |
| 40 | if legacyHash == coveredPrefixHash(msgs, len(msgs)) { |
| 41 | t.Fatal("fixture does not distinguish promoted and bounded hashes") |
| 42 | } |
| 43 | if err := SaveCompactionState(path, CompactionState{ |
| 44 | SchemaVersion: compactionStateSchemaV3, |
| 45 | PromptCacheKey: promptCacheKey("ws", BranchID(path), "model"), |
| 46 | LastReceipt: &ContextMaintenanceReceipt{ |
| 47 | Status: "applied", Action: "prune", CoveredPrefixHash: legacyHash, |
| 48 | }, |
| 49 | Projection: ContextProjection{ |
| 50 | Messages: []provider.Message{{Role: provider.RoleSystem, Content: "system"}, formatSummaryMessage("summary")}, |
| 51 | CoveredCount: len(msgs), |
| 52 | CoveredPrefixHash: legacyHash, |
| 53 | }, |
| 54 | }); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | sess := &Session{Messages: msgs} |
| 58 | a := New(nil, nil, sess, Options{SessionPath: path, WorkspaceID: "ws", ModelRef: "model"}, event.Discard) |
| 59 | want := coveredPrefixHash(msgs, len(msgs)) |
| 60 | if got := a.sess.compactionState.Projection.CoveredPrefixHash; got != want { |
| 61 | t.Fatalf("loaded hash = %q, want migrated %q", got, want) |
| 62 | } |
| 63 | disk, ok, err := LoadCompactionState(path) |
| 64 | if err != nil || !ok { |
| 65 | t.Fatalf("load migrated sidecar: ok=%v err=%v", ok, err) |
| 66 | } |
| 67 | if got := disk.Projection.CoveredPrefixHash; got != want { |
| 68 | t.Fatalf("persisted hash = %q, want %q", got, want) |
| 69 | } |
| 70 | if disk.LastReceipt == nil || disk.LastReceipt.CoveredPrefixHash != want { |
| 71 | t.Fatalf("receipt hash was not normalized: %+v", disk.LastReceipt) |
| 72 | } |
| 73 | mutated := append([]provider.Message(nil), msgs...) |
| 74 | mutated[len(mutated)-1].RawContent = "changed after migration" |
| 75 | if !projectionValid(a.sess.compactionState, mutated, a.currentPromptCacheKey()) { |
| 76 | t.Fatal("local RawContent edit invalidated a bounded provider projection") |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestLoadProjectionSidecarNormalizesPromotedToolBody(t *testing.T) { |
| 81 | dir := t.TempDir() |
| 82 | path := filepath.Join(dir, "session.jsonl") |
| 83 | const fullSentinel = "FULL-PROMOTED-RESULT-MUST-NOT-REPLAY" |
| 84 | msgs := []provider.Message{ |
| 85 | {Role: provider.RoleSystem, Content: "system"}, |
| 86 | {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "call-1", Name: "read", Arguments: `{}`}}}, |
| 87 | {Role: provider.RoleTool, ToolCallID: "call-1", Name: "read", Content: "bounded", RawContent: fullSentinel}, |
| 88 | } |
| 89 | legacyHash := promotedCoveredPrefixHash(msgs, len(msgs)) |
| 90 | if err := SaveCompactionState(path, CompactionState{ |
| 91 | SchemaVersion: compactionStateSchemaV3, |
| 92 | PromptCacheKey: promptCacheKey("ws", BranchID(path), "model"), |
| 93 | LastReceipt: &ContextMaintenanceReceipt{ |
| 94 | Status: "applied", Action: "prune", CoveredPrefixHash: legacyHash, |
| 95 | }, |
| 96 | Projection: ContextProjection{ |
| 97 | Messages: []provider.Message{ |
| 98 | {Role: provider.RoleSystem, Content: "system"}, |
| 99 | {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "call-1", Name: "read", Arguments: `{}`}}}, |
| 100 | {Role: provider.RoleTool, ToolCallID: "call-1", Name: "read", Content: fullSentinel}, |
| 101 | }, |
| 102 | CoveredCount: len(msgs), |
| 103 | CoveredPrefixHash: legacyHash, |
| 104 | }, |
| 105 | }); err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | |
| 109 | a := New(nil, nil, &Session{Messages: msgs}, Options{SessionPath: path, WorkspaceID: "ws", ModelRef: "model"}, event.Discard) |
| 110 | projection := a.sess.compactionState.Projection |
| 111 | if len(projection.Messages) != 3 || projection.Messages[2].Content != "bounded" { |
| 112 | t.Fatalf("loaded projection tool body = %+v, want bounded Content", projection.Messages) |
| 113 | } |
| 114 | visible := modelInputMessages(modelVisibleFromProjection(projection, msgs)) |
| 115 | if encoded := projectionJSON(t, visible); strings.Contains(encoded, fullSentinel) { |
| 116 | t.Fatalf("provider-visible migrated projection retained full result: %s", encoded) |
| 117 | } |
| 118 | |
| 119 | disk, ok, err := LoadCompactionState(path) |
| 120 | if err != nil || !ok { |
| 121 | t.Fatalf("load normalized sidecar: ok=%v err=%v", ok, err) |
| 122 | } |
| 123 | if len(disk.Projection.Messages) != 3 || disk.Projection.Messages[2].Content != "bounded" || |
| 124 | disk.Projection.Messages[2].RawContent != "" || disk.Projection.Messages[2].ProviderContent != "" { |
| 125 | t.Fatalf("persisted projection tool body was not normalized: %+v", disk.Projection.Messages) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func TestLoadProjectionSidecarDropsUnverifiablePromotedToolBody(t *testing.T) { |
| 130 | dir := t.TempDir() |
| 131 | path := filepath.Join(dir, "session.jsonl") |
| 132 | msgs := []provider.Message{ |
| 133 | {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "call-1", Name: "read", Arguments: `{}`}}}, |
| 134 | {Role: provider.RoleTool, ToolCallID: "call-1", Name: "read", Content: "bounded", RawContent: "canonical full result"}, |
| 135 | } |
| 136 | legacyHash := promotedCoveredPrefixHash(msgs, len(msgs)) |
| 137 | if err := SaveCompactionState(path, CompactionState{ |
| 138 | PromptCacheKey: promptCacheKey("ws", BranchID(path), "model"), |
| 139 | LastReceipt: &ContextMaintenanceReceipt{Status: "applied", Action: "prune", CoveredPrefixHash: legacyHash}, |
| 140 | Projection: ContextProjection{ |
| 141 | Messages: []provider.Message{ |
| 142 | msgs[0], |
| 143 | {Role: provider.RoleTool, ToolCallID: "call-1", Name: "read", Content: "different unverified full result"}, |
| 144 | }, |
| 145 | CoveredCount: len(msgs), CoveredPrefixHash: legacyHash, |
| 146 | }, |
| 147 | }); err != nil { |
| 148 | t.Fatal(err) |
| 149 | } |
| 150 | |
| 151 | a := New(nil, nil, &Session{Messages: msgs}, Options{SessionPath: path, WorkspaceID: "ws", ModelRef: "model"}, event.Discard) |
| 152 | if len(a.sess.compactionState.Projection.Messages) != 0 { |
| 153 | t.Fatalf("unverifiable promoted projection survived load: %+v", a.sess.compactionState.Projection.Messages) |
| 154 | } |
| 155 | if a.sess.compactionState.LastReceipt == nil || a.sess.compactionState.LastReceipt.Action != "prune" { |
| 156 | t.Fatalf("maintenance receipt was lost: %+v", a.sess.compactionState.LastReceipt) |
| 157 | } |
| 158 | if got := a.Session().Snapshot(); !reflect.DeepEqual(got, msgs) { |
| 159 | t.Fatalf("canonical transcript changed: got=%+v want=%+v", got, msgs) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | func TestCopyValidContextProjectionNormalizesPromotedToolBodyOrFailsClosed(t *testing.T) { |
| 164 | msgs := []provider.Message{ |
| 165 | {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "call-1", Name: "read", Arguments: `{}`}}}, |
| 166 | {Role: provider.RoleTool, ToolCallID: "call-1", Name: "read", Content: "bounded", RawContent: "canonical full result"}, |
| 167 | } |
| 168 | legacyHash := promotedCoveredPrefixHash(msgs, len(msgs)) |
| 169 | for _, tc := range []struct { |
| 170 | name string |
| 171 | body string |
| 172 | wantCopied bool |
| 173 | }{ |
| 174 | {name: "exact", body: msgs[1].RawContent, wantCopied: true}, |
| 175 | {name: "unverifiable", body: "different full result", wantCopied: false}, |
| 176 | } { |
| 177 | t.Run(tc.name, func(t *testing.T) { |
| 178 | dir := t.TempDir() |
| 179 | original := filepath.Join(dir, "original.jsonl") |
| 180 | target := filepath.Join(dir, "target.jsonl") |
| 181 | if err := SaveCompactionState(original, CompactionState{ |
| 182 | Projection: ContextProjection{ |
| 183 | Messages: []provider.Message{ |
| 184 | msgs[0], |
| 185 | {Role: provider.RoleTool, ToolCallID: "call-1", Name: "read", Content: tc.body}, |
| 186 | }, |
| 187 | CoveredCount: len(msgs), CoveredPrefixHash: legacyHash, |
| 188 | }, |
| 189 | LastReceipt: &ContextMaintenanceReceipt{Status: "applied", CoveredPrefixHash: legacyHash}, |
| 190 | }); err != nil { |
| 191 | t.Fatal(err) |
| 192 | } |
| 193 | copied, err := copyValidContextProjection(original, target, msgs) |
| 194 | if err != nil || copied != tc.wantCopied { |
| 195 | t.Fatalf("copy result: copied=%v err=%v want=%v", copied, err, tc.wantCopied) |
| 196 | } |
| 197 | if !copied { |
| 198 | if _, ok, err := LoadCompactionState(target); err != nil || ok { |
| 199 | t.Fatalf("unverifiable body created target sidecar: ok=%v err=%v", ok, err) |
| 200 | } |
| 201 | return |
| 202 | } |
| 203 | disk, ok, err := LoadCompactionState(target) |
| 204 | if err != nil || !ok { |
| 205 | t.Fatalf("load copied sidecar: ok=%v err=%v", ok, err) |
| 206 | } |
| 207 | if got := disk.Projection.Messages[1]; got.Content != msgs[1].Content || got.RawContent != "" || got.ProviderContent != "" { |
| 208 | t.Fatalf("copied projection body = %+v, want bounded canonical Content", got) |
| 209 | } |
| 210 | }) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | func TestNormalizePromotedProjectionToolBodiesRejectsAmbiguousDuplicateCallID(t *testing.T) { |
| 215 | canonical := []provider.Message{ |
| 216 | {Role: provider.RoleTool, ToolCallID: "repeat", Name: "read", Content: "bounded-1", RawContent: "same promoted body"}, |
| 217 | {Role: provider.RoleTool, ToolCallID: "repeat", Name: "read", Content: "bounded-2", RawContent: "same promoted body"}, |
| 218 | } |
| 219 | projection := []provider.Message{{ |
| 220 | Role: provider.RoleTool, ToolCallID: "repeat", Name: "read", Content: "same promoted body", |
| 221 | }} |
| 222 | if normalized, ok := normalizePromotedProjectionToolBodies(projection, canonical, len(canonical)); ok || normalized != nil { |
| 223 | t.Fatalf("ambiguous duplicate was normalized: ok=%v messages=%+v", ok, normalized) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | func projectionJSON(t *testing.T, value any) string { |
| 228 | t.Helper() |
| 229 | b, err := json.Marshal(value) |
| 230 | if err != nil { |
| 231 | t.Fatal(err) |
| 232 | } |
| 233 | return string(b) |
| 234 | } |
| 235 | |
| 236 | func TestLoadProjectionSidecarDropsUnverifiableBodyButKeepsReceipt(t *testing.T) { |
| 237 | dir := t.TempDir() |
| 238 | path := filepath.Join(dir, "session.jsonl") |
| 239 | msgs := []provider.Message{{Role: provider.RoleSystem, Content: "system"}, {Role: provider.RoleUser, Content: "task"}} |
| 240 | if err := SaveCompactionState(path, CompactionState{ |
| 241 | SchemaVersion: compactionStateSchemaV3, |
| 242 | PromptCacheKey: promptCacheKey("ws", BranchID(path), "model"), |
| 243 | Projection: ContextProjection{ |
| 244 | Messages: []provider.Message{{Role: provider.RoleSystem, Content: "system"}, formatSummaryMessage("summary")}, |
| 245 | CoveredCount: len(msgs), CoveredPrefixHash: "unrelated-stale-hash", |
| 246 | }, |
| 247 | LastReceipt: &ContextMaintenanceReceipt{Status: "applied", Action: "summary", CoveredPrefixHash: "unrelated-stale-hash"}, |
| 248 | }); err != nil { |
| 249 | t.Fatal(err) |
| 250 | } |
| 251 | a := New(nil, nil, &Session{Messages: msgs}, Options{SessionPath: path, WorkspaceID: "ws", ModelRef: "model"}, event.Discard) |
| 252 | if len(a.sess.compactionState.Projection.Messages) != 0 { |
| 253 | t.Fatal("unverifiable projection body survived load") |
| 254 | } |
| 255 | if a.sess.compactionState.LastReceipt == nil || a.sess.compactionState.LastReceipt.Action != "summary" { |
| 256 | t.Fatalf("maintenance receipt was lost: %+v", a.sess.compactionState.LastReceipt) |
| 257 | } |
| 258 | if got := a.Session().Snapshot(); !reflect.DeepEqual(got, msgs) { |
| 259 | t.Fatalf("canonical transcript changed: got=%+v want=%+v", got, msgs) |
| 260 | } |
| 261 | } |
| 262 |