Repository navigation
Expand file tree
/
Copy pathsession_driver_concurrency_test.go
More file actions
258 lines (238 loc) · 7.32 KB
/
Copy pathsession_driver_concurrency_test.go
File metadata and controls
258 lines (238 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package acpruntime
import (
"context"
"errors"
"sync"
"testing"
"time"
)
func TestStartTurnRejectsConcurrentTurn(t *testing.T) {
driver := newTestDriverWithActiveTurn(t, 1)
// First turn is already currentTurn; second must fail without overwriting.
handle := driver.StartTurn(context.Background(), RuntimePrompt{Text: "second"})
result := <-handle.Completion
if result.Err == nil {
t.Fatal("second StartTurn error = nil, want turn-already-running")
}
var runtimeErr *RuntimeError
if !errors.As(result.Err, &runtimeErr) {
t.Fatalf("second StartTurn error = %T, want *RuntimeError", result.Err)
}
if runtimeErr.Kind != ErrorTurnCoalesced {
t.Fatalf("RuntimeError.Kind = %q, want %q", runtimeErr.Kind, ErrorTurnCoalesced)
}
// Original turn must still be current.
driver.mu.RLock()
active := driver.currentTurn
driver.mu.RUnlock()
if active == nil || active.id != "turn-1" {
t.Fatalf("currentTurn overwritten: %#v", active)
}
}
func TestEmitTurnEventDoesNotBlockWhenBufferFull(t *testing.T) {
driver := &acpSessionDriver{
status: "running",
toolCalls: map[string]ToolCallSnapshot{},
operations: map[string]Operation{},
profile: defaultAgentProfile(),
}
active := &activeTurn{
id: "turn-1",
events: make(chan TurnEvent, 1),
}
// Fill the only buffer slot.
active.events <- TurnEvent{Type: "started", TurnID: "turn-1"}
driver.currentTurn = active
done := make(chan struct{})
go func() {
// Would deadlock if emitTurnEvent blocked on a full channel.
driver.emitTurnEvent(active, TurnEvent{Type: "text", TurnID: "turn-1", Text: "x"})
close(done)
}()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("emitTurnEvent blocked on full events channel")
}
}
func TestFinishTurnDeliversCompletionWhenEventsBufferFull(t *testing.T) {
driver := &acpSessionDriver{status: "running"}
active := &activeTurn{
id: "turn-1",
events: make(chan TurnEvent, 1),
completion: make(chan TurnResult, 1),
}
// Saturate events so a blocking send would hang finishTurn.
active.events <- TurnEvent{Type: "text", TurnID: "turn-1", Text: "buffered"}
driver.currentTurn = active
done := make(chan struct{})
go func() {
driver.finishTurn(active, TurnCompletion{TurnID: "turn-1", OutputText: "ok"}, nil)
close(done)
}()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("finishTurn blocked while events buffer was full")
}
select {
case result := <-active.completion:
if result.Err != nil {
t.Fatalf("completion err = %v", result.Err)
}
if result.Completion.OutputText != "ok" {
t.Fatalf("completion text = %q, want ok", result.Completion.OutputText)
}
case <-time.After(time.Second):
t.Fatal("completion was not delivered")
}
}
func TestCloseFinishesInFlightTurn(t *testing.T) {
// Peer writer is enough for best-effort session/cancel Notify; CloseSession
// uses Call and is bounded by the short context below.
peer := NewPeer(neverEOFReader{}, discardWriter{}, PeerOptions{})
driver := &acpSessionDriver{
connection: NewConnection(peer, Client{}),
sessionID: "session-1",
status: "running",
toolCalls: map[string]ToolCallSnapshot{},
operations: map[string]Operation{},
permissions: map[string]PermissionRequestSnapshot{},
rawConfig: map[string]any{},
profile: defaultAgentProfile(),
}
active := &activeTurn{
id: "turn-1",
events: make(chan TurnEvent, 4),
completion: make(chan TurnResult, 1),
}
driver.currentTurn = active
// Close terminalizes the in-flight turn before waiting on CloseSession RPC.
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
closeDone := make(chan error, 1)
go func() { closeDone <- driver.Close(ctx) }()
select {
case result := <-active.completion:
if result.Err == nil {
t.Fatal("completion err = nil, want session closed")
}
var runtimeErr *RuntimeError
if !errors.As(result.Err, &runtimeErr) {
t.Fatalf("completion err = %T, want *RuntimeError", result.Err)
}
if runtimeErr.Kind != ErrorSessionClosed {
t.Fatalf("RuntimeError.Kind = %q, want %q", runtimeErr.Kind, ErrorSessionClosed)
}
case <-time.After(time.Second):
t.Fatal("in-flight turn was not finished on Close")
}
select {
case <-closeDone:
case <-time.After(time.Second):
t.Fatal("Close did not return after context timeout")
}
if got := driver.Status(); got != "closed" {
t.Fatalf("Status() = %q, want closed", got)
}
}
func TestHandleSessionUpdateDoesNotBlockOnFullEvents(t *testing.T) {
driver := &acpSessionDriver{
sessionID: "session-1",
status: "running",
toolCalls: map[string]ToolCallSnapshot{},
operations: map[string]Operation{},
rawConfig: map[string]any{},
profile: defaultAgentProfile(),
}
active := &activeTurn{
id: "turn-1",
events: make(chan TurnEvent, 1),
}
active.events <- TurnEvent{Type: "started", TurnID: "turn-1"}
driver.currentTurn = active
done := make(chan struct{})
go func() {
// Flood intermediate updates; none may block the notification path.
for i := 0; i < 128; i++ {
driver.handleSessionUpdate(SessionNotification{
SessionID: "session-1",
Update: SessionUpdate{
SessionUpdate: "agent_message_chunk",
Text: "x",
},
})
}
close(done)
}()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("handleSessionUpdate blocked on full events channel")
}
// Read model still accumulates text despite event drops.
if got := active.outputText.String(); got == "" {
t.Fatal("outputText empty; read model should update even when events drop")
}
}
func TestFinishTurnIsIdempotent(t *testing.T) {
driver := &acpSessionDriver{status: "running"}
active := &activeTurn{
id: "turn-1",
events: make(chan TurnEvent, 4),
completion: make(chan TurnResult, 1),
}
driver.currentTurn = active
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
driver.finishTurn(active, TurnCompletion{TurnID: "turn-1"}, nil)
}()
go func() {
defer wg.Done()
driver.finishTurn(active, TurnCompletion{}, &RuntimeError{Kind: ErrorSessionClosed, Op: "session.close", Msg: "closed"})
}()
wg.Wait()
// Exactly one completion value; channel is closed.
result, ok := <-active.completion
if !ok {
t.Fatal("completion closed without a result")
}
if _, ok := <-active.completion; ok {
t.Fatal("completion delivered more than once")
}
// Result may be either path depending on race; just ensure we got one.
_ = result
}
func newTestDriverWithActiveTurn(t *testing.T, buffer int) *acpSessionDriver {
t.Helper()
if buffer <= 0 {
buffer = 1
}
driver := &acpSessionDriver{
sessionID: "session-1",
status: "running",
toolCalls: map[string]ToolCallSnapshot{},
operations: map[string]Operation{},
permissions: map[string]PermissionRequestSnapshot{},
rawConfig: map[string]any{},
profile: defaultAgentProfile(),
}
active := &activeTurn{
id: "turn-1",
events: make(chan TurnEvent, buffer),
completion: make(chan TurnResult, 1),
}
driver.currentTurn = active
driver.turnSeq = 1
return driver
}
// neverEOFReader blocks reads forever so Peer.Start (if launched) does not exit
// on EOF during unit tests that only need a Connection shell.
type neverEOFReader struct{}
func (neverEOFReader) Read([]byte) (int, error) {
select {}
}
type discardWriter struct{}
func (discardWriter) Write(p []byte) (int, error) { return len(p), nil }