| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/event" |
| 9 | "reasonix/internal/provider" |
| 10 | "reasonix/internal/tool" |
| 11 | ) |
| 12 | |
| 13 | func TestNormalizeModelRequestMessagesDoesNotMutateInput(t *testing.T) { |
| 14 | const createdAt = int64(99) |
| 15 | const storedContent = "task\n<execution-policy>local policy</execution-policy>" |
| 16 | msgs := []provider.Message{ |
| 17 | {Role: provider.RoleSystem, Content: "system"}, |
| 18 | {Role: provider.RoleUser, Content: storedContent, CreatedAt: createdAt}, |
| 19 | } |
| 20 | wantStored := append([]provider.Message(nil), msgs...) |
| 21 | a := New(nil, tool.NewRegistry(), NewSession("system"), Options{}, event.Discard) |
| 22 | |
| 23 | got := a.normalizeModelRequestMessages(msgs) |
| 24 | if got[1].CreatedAt != 0 || got[1].Content != "task" { |
| 25 | t.Fatalf("provider message = %+v, want stripped local metadata", got[1]) |
| 26 | } |
| 27 | if !reflect.DeepEqual(msgs, wantStored) { |
| 28 | t.Fatalf("normalization mutated its input: got=%+v want=%+v", msgs, wantStored) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestPressurePruneNeverPromotesRawToolContent(t *testing.T) { |
| 33 | const rawOnlySentinel = "RAW-ONLY-PRESSURE-SENTINEL" |
| 34 | bounded := strings.Repeat("b", toolPruneThresholdRunes+1) |
| 35 | raw := strings.Repeat("r", 100) + rawOnlySentinel + strings.Repeat("r", toolPruneThresholdRunes+1) |
| 36 | sess := &Session{Messages: []provider.Message{ |
| 37 | {Role: provider.RoleSystem, Content: "system"}, |
| 38 | {Role: provider.RoleAssistant, ToolCalls: []provider.ToolCall{{ID: "call-1", Name: "read_file", Arguments: `{}`}}}, |
| 39 | {Role: provider.RoleTool, ToolCallID: "call-1", Name: "read_file", Content: bounded, RawContent: raw}, |
| 40 | }} |
| 41 | a := New(nil, tool.NewRegistry(), sess, Options{}, event.Discard) |
| 42 | |
| 43 | a.sess.compactionRunMu.Lock() |
| 44 | advanced, err := a.pruneToolResultsToProjectionLocked(CompactionTriggerPressure) |
| 45 | a.sess.compactionRunMu.Unlock() |
| 46 | if err != nil || !advanced { |
| 47 | t.Fatalf("pressure prune advanced=%v err=%v", advanced, err) |
| 48 | } |
| 49 | visible := modelInputMessages(a.modelVisibleMessages()) |
| 50 | if strings.Contains(joinContents(visible), rawOnlySentinel) { |
| 51 | t.Fatal("pressure projection promoted local RawContent") |
| 52 | } |
| 53 | if got := visible[len(visible)-1]; !strings.Contains(got.Content, toolPruneMarker) || got.RawContent != "" { |
| 54 | t.Fatalf("provider tool result = %+v, want pruned bounded Content without RawContent", got) |
| 55 | } |
| 56 | stored := sess.Snapshot()[2] |
| 57 | if stored.Content != bounded || stored.RawContent != raw { |
| 58 | t.Fatal("pressure prune modified canonical tool content") |
| 59 | } |
| 60 | } |
| 61 |