| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "sync/atomic" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "reasonix/internal/event" |
| 11 | "reasonix/internal/eventwire" |
| 12 | "reasonix/internal/provider" |
| 13 | "reasonix/internal/turnevent" |
| 14 | ) |
| 15 | |
| 16 | func TestDisplayTurnBufferPreservesStreamingReplacementAndTools(t *testing.T) { |
| 17 | var buffer displayTurnBuffer |
| 18 | recordHistoryDisplayEvent(&buffer, event.Event{Kind: event.Reasoning, Text: "draft reason "}) |
| 19 | recordHistoryDisplayEvent(&buffer, event.Event{Kind: event.Reasoning, Text: "continued"}) |
| 20 | recordHistoryDisplayEvent(&buffer, event.Event{Kind: event.Text, Text: "draft answer"}) |
| 21 | recordHistoryDisplayEvent(&buffer, event.Event{ |
| 22 | Kind: event.Message, |
| 23 | Text: "final answer", |
| 24 | Reasoning: "final reason", |
| 25 | MemoryCitations: []provider.MemoryCitation{{ |
| 26 | ID: "memory-1", Source: "project", |
| 27 | }}, |
| 28 | }) |
| 29 | recordHistoryDisplayEvent(&buffer, event.Event{Kind: event.ToolDispatch, Tool: event.Tool{ |
| 30 | ID: "call-1", Name: "read_file", Args: `{"path":"settings.json"}`, |
| 31 | }}) |
| 32 | recordHistoryDisplayEvent(&buffer, event.Event{Kind: event.ToolResult, Tool: event.Tool{ |
| 33 | ID: "call-1", Output: "settings contents", |
| 34 | }}) |
| 35 | |
| 36 | got := buffer.materialize() |
| 37 | if len(got) != 3 { |
| 38 | t.Fatalf("messages = %d, want 3: %+v", len(got), got) |
| 39 | } |
| 40 | if got[0].Role != "assistant" || got[0].Content != "final answer" || got[0].Reasoning != "final reason" { |
| 41 | t.Fatalf("stream replacement changed: %+v", got[0]) |
| 42 | } |
| 43 | if len(got[0].MemoryCitations) != 1 || got[0].MemoryCitations[0].ID != "memory-1" { |
| 44 | t.Fatalf("memory citations changed: %+v", got[0].MemoryCitations) |
| 45 | } |
| 46 | if got[1].Role != "assistant" || len(got[1].ToolCalls) != 1 || got[1].ToolCalls[0].ID != "call-1" || got[1].ToolCalls[0].Summary == "" { |
| 47 | t.Fatalf("tool call changed: %+v", got[1]) |
| 48 | } |
| 49 | if got[2].Role != "tool" || got[2].ToolCallID != "call-1" || got[2].ToolName != "read_file" { |
| 50 | t.Fatalf("tool result changed: %+v", got[2]) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestDisplayTurnBufferMessageIdentityAndDiscardMatchRecovery(t *testing.T) { |
| 55 | events := []event.Event{ |
| 56 | {Kind: event.StreamAttempt, MessageID: "failed", AttemptID: "failed", StreamAttempt: event.StreamAttemptInfo{ID: "failed", Action: event.StreamAttemptBegin}}, |
| 57 | {Kind: event.Reasoning, MessageID: "failed", AttemptID: "failed", Text: "discard this"}, |
| 58 | {Kind: event.Message, MessageID: "failed", AttemptID: "failed", Text: "rejected full response"}, |
| 59 | {Kind: event.StreamAttempt, MessageID: "failed", AttemptID: "failed", StreamAttempt: event.StreamAttemptInfo{ID: "failed", Action: event.StreamAttemptDiscard}}, |
| 60 | {Kind: event.Reasoning, MessageID: "a", AttemptID: "a", Text: "first thought"}, |
| 61 | {Kind: event.ToolDispatch, MessageID: "a", Tool: event.Tool{ID: "call", Name: "read_file", Args: `{}`}}, |
| 62 | {Kind: event.ToolResult, Tool: event.Tool{ID: "call", Name: "read_file", Output: "done"}}, |
| 63 | {Kind: event.Reasoning, MessageID: "b", AttemptID: "b", Text: "second thought"}, |
| 64 | {Kind: event.Message, MessageID: "a", AttemptID: "a", Reasoning: "first thought", Text: "first answer"}, |
| 65 | } |
| 66 | var live, recovered displayTurnBuffer |
| 67 | for i, e := range events { |
| 68 | recordHistoryDisplayEvent(&live, e) |
| 69 | wire := eventwire.ToWire(e) |
| 70 | replay, ok := displayEventFromEnvelope(turnevent.Envelope{Kind: wire.Kind, Sequence: uint64(i + 1), Event: wire}) |
| 71 | if !ok { |
| 72 | t.Fatalf("event %s is not replayable", wire.Kind) |
| 73 | } |
| 74 | recordHistoryDisplayEvent(&recovered, replay) |
| 75 | } |
| 76 | for name, buffer := range map[string]*displayTurnBuffer{"live": &live, "recovered": &recovered} { |
| 77 | rows := buffer.materialize() |
| 78 | if len(rows) != 3 || rows[0].MessageID != "a" || rows[0].Content != "first answer" || rows[0].Reasoning != "first thought" || len(rows[0].ToolCalls) != 1 || rows[2].MessageID != "b" || rows[2].Reasoning != "second thought" { |
| 79 | t.Fatalf("%s message ownership/discard mismatch: %+v", name, rows) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestDisplayTurnBufferStreamingAllocationsStayNearLinear(t *testing.T) { |
| 85 | const ( |
| 86 | chunks = 2_000 |
| 87 | chunkSize = 32 |
| 88 | ) |
| 89 | chunk := strings.Repeat("x", chunkSize) |
| 90 | result := testing.Benchmark(func(b *testing.B) { |
| 91 | b.ReportAllocs() |
| 92 | for range b.N { |
| 93 | var buffer displayTurnBuffer |
| 94 | for range chunks { |
| 95 | recordHistoryDisplayEvent(&buffer, event.Event{Kind: event.Text, Text: chunk}) |
| 96 | } |
| 97 | messages := buffer.materialize() |
| 98 | if len(messages) != 1 || len(messages[0].Content) != chunks*chunkSize { |
| 99 | b.Fatalf("materialized display length changed") |
| 100 | } |
| 101 | } |
| 102 | }) |
| 103 | |
| 104 | // Repeated string concatenation allocated roughly one full growing prefix |
| 105 | // per chunk (>69 MiB for this 64 KiB stream). Keep a generous ceiling for |
| 106 | // platform/runtime variance while pinning the intended near-linear shape. |
| 107 | if got, max := result.AllocedBytesPerOp(), int64(chunks*chunkSize*16); got > max { |
| 108 | t.Fatalf("stream allocated %d bytes/op, want <= %d (%s)", got, max, result.String()) |
| 109 | } |
| 110 | if got := result.AllocsPerOp(); got > 100 { |
| 111 | t.Fatalf("stream allocated %d objects/op, want <= 100 (%s)", got, result.String()) |
| 112 | } |
| 113 | t.Logf("64 KiB stream: %d bytes/op, %d allocs/op", result.AllocedBytesPerOp(), result.AllocsPerOp()) |
| 114 | } |
| 115 | |
| 116 | func TestPendingDisplayWriteRetriesWithoutDroppingTurn(t *testing.T) { |
| 117 | state := &tabDisplayState{} |
| 118 | var attempts atomic.Int32 |
| 119 | var acknowledgements atomic.Int32 |
| 120 | persisted := make(chan struct{}) |
| 121 | acknowledged := make(chan struct{}) |
| 122 | write := &pendingDisplayWrite{ |
| 123 | dir: "sessions", |
| 124 | sessionPath: "sessions/session.jsonl", |
| 125 | userContent: "prompt", |
| 126 | messages: []HistoryMessage{{Role: "assistant", Content: "partial answer"}}, |
| 127 | persist: func(_, _, _ string, messages []HistoryMessage) error { |
| 128 | attempt := attempts.Add(1) |
| 129 | if len(messages) != 1 || messages[0].Content != "partial answer" { |
| 130 | return errors.New("queued turn changed") |
| 131 | } |
| 132 | if attempt < 3 { |
| 133 | return errors.New("temporary lock contention") |
| 134 | } |
| 135 | close(persisted) |
| 136 | return nil |
| 137 | }, |
| 138 | onPersisted: func() { |
| 139 | acknowledgements.Add(1) |
| 140 | close(acknowledged) |
| 141 | }, |
| 142 | } |
| 143 | persistOrEnqueueDisplayWrite(state, write) |
| 144 | select { |
| 145 | case <-persisted: |
| 146 | case <-time.After(3 * time.Second): |
| 147 | t.Fatal("pending display write was not retried") |
| 148 | } |
| 149 | select { |
| 150 | case <-acknowledged: |
| 151 | case <-time.After(time.Second): |
| 152 | t.Fatal("durable display write was not acknowledged") |
| 153 | } |
| 154 | state.mu.Lock() |
| 155 | pending := len(state.pendingWrites) |
| 156 | running := state.persistRunning |
| 157 | state.mu.Unlock() |
| 158 | if pending != 0 || running { |
| 159 | t.Fatalf("retry worker did not drain before acknowledgement: pending=%d running=%v", pending, running) |
| 160 | } |
| 161 | if got := attempts.Load(); got != 3 { |
| 162 | t.Fatalf("persist attempts = %d, want 3", got) |
| 163 | } |
| 164 | if got := acknowledgements.Load(); got != 1 { |
| 165 | t.Fatalf("projection acknowledgements = %d, want exactly one after persistence", got) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | func TestDisplayMessagesFromInterruptedProjectionKeepsPartialOutput(t *testing.T) { |
| 170 | textEvent := event.Event{Kind: event.Text, Source: event.UsageSourceExecutor, Text: "partial answer"} |
| 171 | toolEvent := event.Event{Kind: event.ToolDispatch, Source: event.UsageSourceExecutor, Tool: event.Tool{ID: "call-1", Name: "read_file", Args: `{"path":"notes.txt"}`}} |
| 172 | projection := turnevent.PendingProjection{ |
| 173 | TurnID: "turn-1", Status: event.TurnInterrupted, |
| 174 | Events: []turnevent.Envelope{ |
| 175 | {TurnID: "turn-1", Sequence: 1, Kind: "text", Source: textEvent.Source, Event: eventwire.ToWire(textEvent)}, |
| 176 | {TurnID: "turn-1", Sequence: 2, Kind: "tool_dispatch", Source: toolEvent.Source, Event: eventwire.ToWire(toolEvent)}, |
| 177 | {TurnID: "turn-1", Sequence: 3, Kind: "turn_done", Status: event.TurnInterrupted, Event: eventwire.ToWire(event.Event{Kind: event.TurnDone})}, |
| 178 | }, |
| 179 | } |
| 180 | |
| 181 | got := displayMessagesFromProjection(projection) |
| 182 | if len(got) != 3 { |
| 183 | t.Fatalf("recovered display messages = %d, want partial answer, tool card and notice: %+v", len(got), got) |
| 184 | } |
| 185 | if got[0].Role != "assistant" || got[0].Content != "partial answer" { |
| 186 | t.Fatalf("partial assistant output changed: %+v", got[0]) |
| 187 | } |
| 188 | if got[1].Role != "assistant" || len(got[1].ToolCalls) != 1 || got[1].ToolCalls[0].ID != "call-1" { |
| 189 | t.Fatalf("tool dispatch projection changed: %+v", got[1]) |
| 190 | } |
| 191 | if got[2].Role != "notice" || got[2].Code != event.NoticeCodeCancelledTurn { |
| 192 | t.Fatalf("interruption notice missing: %+v", got[2]) |
| 193 | } |
| 194 | projection.Status = event.TurnRecoveryRequired |
| 195 | recovered := displayMessagesFromProjection(projection) |
| 196 | if len(recovered) != len(got) || recovered[0].Content != "partial answer" || recovered[2].Code != event.NoticeCodeCancelledTurn { |
| 197 | t.Fatalf("recovery-required projection lost partial display: %+v", recovered) |
| 198 | } |
| 199 | } |
| 200 |