| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "reflect" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/event" |
| 11 | "reasonix/internal/extension" |
| 12 | "reasonix/internal/extension/dispatch" |
| 13 | "reasonix/internal/extension/protocol" |
| 14 | "reasonix/internal/provider" |
| 15 | ) |
| 16 | |
| 17 | func TestCompactionPrepareReplaceGuidance(t *testing.T) { |
| 18 | client := &fakeDispatchClient{interceptFn: func(ev protocol.InterceptEvent, payload json.RawMessage) (protocol.InterceptResult, error) { |
| 19 | if ev == protocol.EventCompactionPrepare { |
| 20 | var in dispatch.CompactionPreparePayload |
| 21 | if err := json.Unmarshal(payload, &in); err != nil { |
| 22 | return protocol.InterceptResult{}, err |
| 23 | } |
| 24 | in.Guidance = "EXTENSION GUIDANCE" |
| 25 | return replaceWith(t, in), nil |
| 26 | } |
| 27 | return protocol.InterceptResult{Decision: protocol.DecisionContinue}, nil |
| 28 | }} |
| 29 | d := newExtDispatcher(client, true, nil, extension.PointCompactionPrepare) |
| 30 | mp, a := newCompactionAgent(t, d) |
| 31 | telemetry := "" |
| 32 | a.svc.sink = event.FuncSink(func(e event.Event) { |
| 33 | if e.Kind == event.Notice && e.Text == "compaction telemetry" { |
| 34 | telemetry = e.Detail |
| 35 | } |
| 36 | }) |
| 37 | if err := a.CompactNow(context.Background(), ""); err != nil { |
| 38 | t.Fatalf("CompactNow: %v", err) |
| 39 | } |
| 40 | for i, req := range mp.requests { |
| 41 | if instruction := req.Messages[len(req.Messages)-1].Content; !strings.Contains(instruction, "EXTENSION GUIDANCE") { |
| 42 | t.Fatalf("summarizer call %d of %d missing the replaced guidance:\n%.200q", i+1, len(mp.requests), instruction) |
| 43 | } |
| 44 | } |
| 45 | if sc := joinContents(visibleContext(a)); !strings.Contains(sc, "SUMMARY TEXT") { |
| 46 | t.Fatalf("projection missing the summary:\n%.200q", sc) |
| 47 | } |
| 48 | if n := client.notifyCountFor(protocol.EventCompactionPrepare); n != 1 { |
| 49 | t.Fatalf("compaction.prepare events = %d, want 1", n) |
| 50 | } |
| 51 | if !strings.Contains(telemetry, "summary_input="+SummaryInputCachePrefix) { |
| 52 | t.Fatalf("telemetry = %q, want cache-prefix summary input", telemetry) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestCompactionPrepareReplaceMessages(t *testing.T) { |
| 57 | client := &fakeDispatchClient{interceptFn: func(ev protocol.InterceptEvent, _ json.RawMessage) (protocol.InterceptResult, error) { |
| 58 | if ev == protocol.EventCompactionPrepare { |
| 59 | return replaceWith(t, dispatch.CompactionPreparePayload{ |
| 60 | Messages: []protocol.ProviderMessage{{Role: protocol.ProviderRoleUser, Content: "EXTENSION FOLD"}}, |
| 61 | }), nil |
| 62 | } |
| 63 | return protocol.InterceptResult{Decision: protocol.DecisionContinue}, nil |
| 64 | }} |
| 65 | d := newExtDispatcher(client, true, nil, extension.PointCompactionPrepare) |
| 66 | mp, a := newCompactionAgent(t, d) |
| 67 | telemetry := "" |
| 68 | a.svc.sink = event.FuncSink(func(e event.Event) { |
| 69 | if e.Kind == event.Notice && e.Text == "compaction telemetry" { |
| 70 | telemetry = e.Detail |
| 71 | } |
| 72 | }) |
| 73 | if err := a.CompactNow(context.Background(), ""); err != nil { |
| 74 | t.Fatalf("CompactNow: %v", err) |
| 75 | } |
| 76 | if got := joinContents(mp.requests[0].Messages); !strings.Contains(got, "EXTENSION FOLD") { |
| 77 | t.Fatalf("summarizer messages = %.200q, want the replaced fold", got) |
| 78 | } |
| 79 | if !strings.Contains(telemetry, "summary_input="+SummaryInputExtensionRewritten) { |
| 80 | t.Fatalf("telemetry = %q, want extension-rewritten summary input", telemetry) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestCompactionPrepareGuidanceOnlyPreservesUnrepresentedMessageFields(t *testing.T) { |
| 85 | client := &fakeDispatchClient{interceptFn: func(ev protocol.InterceptEvent, payload json.RawMessage) (protocol.InterceptResult, error) { |
| 86 | if ev != protocol.EventCompactionPrepare { |
| 87 | return protocol.InterceptResult{Decision: protocol.DecisionContinue}, nil |
| 88 | } |
| 89 | var in dispatch.CompactionPreparePayload |
| 90 | if err := json.Unmarshal(payload, &in); err != nil { |
| 91 | return protocol.InterceptResult{}, err |
| 92 | } |
| 93 | in.Guidance = "keep the exact provider replay state" |
| 94 | return replaceWith(t, in), nil |
| 95 | }} |
| 96 | d := newExtDispatcher(client, true, nil, extension.PointCompactionPrepare) |
| 97 | a := New(&fakeProvider{reply: "summary"}, nil, NewSession("system"), Options{Extensions: d}, event.Discard) |
| 98 | fold := []provider.Message{ |
| 99 | { |
| 100 | Role: provider.RoleAssistant, Content: "calling", ReasoningID: "reasoning-1", ReasoningStatus: "completed", |
| 101 | ResponsesItems: []json.RawMessage{json.RawMessage(`{"type":"reasoning","id":"reasoning-1"}`)}, |
| 102 | ServerSearch: []provider.ServerSearchCall{{ID: "search-1"}}, |
| 103 | ToolCalls: []provider.ToolCall{{ID: "call-1", Name: "read", Arguments: `{}`}}, |
| 104 | }, |
| 105 | {Role: provider.RoleTool, Name: "read", ToolCallID: "call-1", Content: "bounded", RawContent: "complete tool result"}, |
| 106 | } |
| 107 | prepared, reason, err := a.prepareVisibleCompression(context.Background(), CompactionTriggerManual, fold, "", SummaryInputCachePrefix) |
| 108 | if err != nil || reason != "" { |
| 109 | t.Fatalf("prepareVisibleCompression: reason=%q err=%v", reason, err) |
| 110 | } |
| 111 | if prepared.instructions != "keep the exact provider replay state" || prepared.inputMode != SummaryInputCachePrefix { |
| 112 | t.Fatalf("prepared metadata = %+v", prepared) |
| 113 | } |
| 114 | want := modelInputMessages(fold) |
| 115 | if !reflect.DeepEqual(prepared.fold, want) { |
| 116 | t.Fatalf("guidance-only replacement changed fold:\n got=%+v\nwant=%+v", prepared.fold, want) |
| 117 | } |
| 118 | } |
| 119 |