| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/event" |
| 9 | "reasonix/internal/provider" |
| 10 | "reasonix/internal/tool" |
| 11 | ) |
| 12 | |
| 13 | type userInputCaptureProvider struct { |
| 14 | request provider.Request |
| 15 | } |
| 16 | |
| 17 | func (p *userInputCaptureProvider) Name() string { return "capture" } |
| 18 | |
| 19 | func (p *userInputCaptureProvider) Stream(_ context.Context, req provider.Request) (<-chan provider.Chunk, error) { |
| 20 | p.request = req |
| 21 | ch := make(chan provider.Chunk, 1) |
| 22 | ch <- provider.Chunk{Type: provider.ChunkText, Text: "done"} |
| 23 | close(ch) |
| 24 | return ch, nil |
| 25 | } |
| 26 | |
| 27 | func TestRunPersistsRawUserInputSeparatelyFromProviderContext(t *testing.T) { |
| 28 | prov := &userInputCaptureProvider{} |
| 29 | sess := NewSession("system") |
| 30 | a := New(prov, tool.NewRegistry(), sess, Options{}, event.Discard) |
| 31 | |
| 32 | const raw = "fix the bug" |
| 33 | const composed = "<capability-route version=\"1\">\nuse review\n</capability-route>\n\nfix the bug" |
| 34 | ctx := WithRawUserInput(context.Background(), raw) |
| 35 | if err := a.Run(ctx, composed); err != nil { |
| 36 | t.Fatalf("Run: %v", err) |
| 37 | } |
| 38 | |
| 39 | stored := sess.Snapshot() |
| 40 | if len(stored) < 2 { |
| 41 | t.Fatalf("stored messages = %d, want system and user", len(stored)) |
| 42 | } |
| 43 | if got := stored[1].Content; got != composed { |
| 44 | t.Fatalf("stored provider content = %q, want composed %q", got, composed) |
| 45 | } |
| 46 | if got := stored[1].RawContent; got != raw { |
| 47 | t.Fatalf("stored raw content = %q, want raw %q", got, raw) |
| 48 | } |
| 49 | if stored[1].ProviderContent != "" { |
| 50 | t.Fatalf("stored transitional provider content was not cleared: %+v", stored[1]) |
| 51 | } |
| 52 | if len(prov.request.Messages) < 2 || prov.request.Messages[1].Content != composed { |
| 53 | t.Fatalf("provider request did not receive composed context: %+v", prov.request.Messages) |
| 54 | } |
| 55 | if prov.request.Messages[1].RawContent != "" || prov.request.Messages[1].ProviderContent != "" { |
| 56 | t.Fatalf("provider request leaked display metadata: %+v", prov.request.Messages[1]) |
| 57 | } |
| 58 | |
| 59 | encoded, err := json.Marshal(stored[1]) |
| 60 | if err != nil { |
| 61 | t.Fatalf("marshal stored user turn: %v", err) |
| 62 | } |
| 63 | var legacy struct { |
| 64 | Content string `json:"content"` |
| 65 | } |
| 66 | if err := json.Unmarshal(encoded, &legacy); err != nil { |
| 67 | t.Fatalf("decode with previous-release shape: %v", err) |
| 68 | } |
| 69 | if legacy.Content != composed { |
| 70 | t.Fatalf("previous-release reader sees %q, want provider-visible %q", legacy.Content, composed) |
| 71 | } |
| 72 | } |
| 73 |