Repository navigation
Expand file tree
/
Copy pathexecution_lifecycle_test.go
More file actions
425 lines (407 loc) · 15.9 KB
/
Copy pathexecution_lifecycle_test.go
File metadata and controls
425 lines (407 loc) · 15.9 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package agentgo
import (
"context"
"encoding/json"
"errors"
"slices"
"testing"
"time"
)
type lifecycleContextKey struct{}
func TestLifecycleNestedModelAndDerivedContexts(t *testing.T) {
var calls []Execution
tool := NewFuncTool("inspect", "inspect", nil, func(ctx context.Context, _ json.RawMessage) (json.RawMessage, error) {
parent, ok := ExecutionFromContext(ctx)
if !ok || parent.ID != "inspect-1" || ctx.Value(lifecycleContextKey{}) != "tool" {
t.Errorf("tool context lost: %+v", parent)
}
_, err := ExecuteModel(ctx, ModelExecution{Execution: Execution{ID: "inspect-1/model", Kind: ExecutionKindModel, TurnIndex: 1, Attempt: 1}},
func(ctx context.Context, execution ModelExecution) (ModelResult, error) {
current, _ := ExecutionFromContext(ctx)
if current != execution.Execution || execution.ParentID != parent.ID || ctx.Value(lifecycleContextKey{}) != "model" {
t.Errorf("nested model context = %+v, execution = %+v", current, execution.Execution)
}
return ModelResult{Message: assistantMsg("detail", StopReasonStop)}, nil
})
return json.RawMessage(`"detail"`), err
})
model := callOptionFuncModel(func(ctx context.Context, _ *LLMRequest, _ CallConfig) (*LLMResponse, error) {
execution, ok := ExecutionFromContext(ctx)
if !ok || execution.Kind != ExecutionKindModel || ctx.Value(lifecycleContextKey{}) != "model" {
t.Errorf("provider context lost: %+v", execution)
}
if execution.TurnIndex == 1 {
return &LLMResponse{Message: toolCallMsg(ToolCall{ID: "inspect-1", Name: "inspect", Args: json.RawMessage(`{}`)})}, nil
}
return &LLMResponse{Message: assistantMsg("done", StopReasonStop)}, nil
})
events := runTestLoop(t, []AgentMessage{UserMsg("inspect")}, AgentContext{Tools: []Tool{tool}}, LoopConfig{
Model: model,
ToolMiddlewares: []ToolMiddleware{func(ctx context.Context, execution ToolExecution, next ToolExecuteFunc) (ToolResult, error) {
current, _ := ExecutionFromContext(ctx)
if current != execution.Execution {
t.Errorf("middleware tool context = %+v", current)
}
return next(context.WithValue(ctx, lifecycleContextKey{}, "tool"), execution)
}},
ModelMiddlewares: []ModelMiddleware{func(ctx context.Context, execution ModelExecution, next ModelExecuteFunc) (ModelResult, error) {
calls = append(calls, execution.Execution)
return next(context.WithValue(ctx, lifecycleContextKey{}, "model"), execution)
}},
})
if len(calls) != 3 || calls[1].ID != "inspect-1/model" || calls[1].ParentID != "inspect-1" {
t.Fatalf("model middleware calls = %+v", calls)
}
if countEvent(events, EventModelExecStart) != 3 || countEvent(events, EventModelExecEnd) != 3 {
t.Fatal("nested call missing model lifecycle")
}
for _, ev := range events {
if ev.Timestamp.IsZero() {
t.Errorf("missing timestamp on %s", ev.Type)
}
}
end, _ := findEvent(events, EventAgentEnd)
if end.Err != nil || end.Summary.TurnCount != 2 {
t.Fatalf("end = %+v", end)
}
}
func TestLifecycleToolOutcomes(t *testing.T) {
for _, name := range []string{"invoked", "denied", "short_circuited", "middleware_error", "panic", "cancelled_gate", "skipped"} {
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ch := make(chan Event, 32)
sink := eventSink{ctx: ctx, ch: ch}
called := 0
tool := NewFuncTool("work", "work", nil, func(ctx context.Context, args json.RawMessage) (json.RawMessage, error) {
called++
if ctx.Value(lifecycleContextKey{}) != "derived" {
t.Error("derived context lost")
}
if string(args) != `{"approved":true}` {
t.Errorf("args = %s", args)
}
if name == "panic" {
panic("tool failed")
}
return json.RawMessage(`"ok"`), nil
})
cfg := LoopConfig{
ToolMiddlewares: []ToolMiddleware{func(ctx context.Context, execution ToolExecution, next ToolExecuteFunc) (ToolResult, error) {
if name == "short_circuited" {
return ToolResult{Content: json.RawMessage(`"cached"`)}, nil
}
if name == "middleware_error" {
return ToolResult{}, errors.New("middleware failed")
}
return next(context.WithValue(ctx, lifecycleContextKey{}, "derived"), execution)
}},
ToolGate: func(ctx context.Context, _ GateRequest) (*GateDecision, error) {
execution, _ := ExecutionFromContext(ctx)
if execution.ID != "work-1" || ctx.Value(lifecycleContextKey{}) != "derived" {
t.Error("gate context lost")
}
if name == "cancelled_gate" {
cancel()
}
return &GateDecision{Allowed: name != "denied", UpdatedArgs: json.RawMessage(`{"approved":true}`)}, nil
},
}
if name == "skipped" {
cancel()
}
results, _ := executeToolCalls(ctx, 1, []Tool{tool}, []ToolCall{{ID: "work-1", Name: "work", Args: json.RawMessage(`{}`)}}, cfg, map[string]int{}, sink)
close(ch)
events := collectEvents(ch)
want := ToolInvoked
switch name {
case "denied":
want = ToolRejected
case "short_circuited", "middleware_error":
want = ToolShortCircuited
case "cancelled_gate", "skipped":
want = ToolSkipped
}
end, ok := findEvent(events, EventToolExecEnd)
if !ok || end.Disposition != want || len(results) != 1 || results[0].ToolCallID != "work-1" {
t.Fatalf("end = %+v, results = %+v", end, results)
}
if countEvent(events, EventToolQueued) != 1 || countEvent(events, EventToolExecStart) != 1 || countEvent(events, EventToolExecEnd) != 1 {
t.Fatal("tool lifecycle not paired")
}
wantCalls := 0
if want == ToolInvoked {
wantCalls = 1
}
if called != wantCalls || countEvent(events, EventToolInvokeStart) != wantCalls || countEvent(events, EventToolInvokeEnd) != wantCalls {
t.Fatalf("invocation facts disagree: calls=%d events=%+v", called, events)
}
if wantCalls > 0 {
start, _ := findEvent(events, EventToolInvokeStart)
if string(start.Args) != `{"approved":true}` {
t.Errorf("invoke args = %s", start.Args)
}
}
if name == "panic" {
invoked, _ := findEvent(events, EventToolInvokeEnd)
if !results[0].IsError || end.Err == nil || invoked.Err == nil {
t.Fatal("panic missing failure outcome")
}
}
})
}
}
type lifecycleManager struct {
projectionCommitManager
failure string
}
func (m lifecycleManager) Transform(ctx context.Context, messages []AgentMessage) ([]AgentMessage, error) {
if m.failure == "panic" {
panic("projection failed")
}
if m.failure == "error" {
return nil, errors.New("projection failed")
}
return messages, nil
}
func (m lifecycleManager) RecoverOverflow(ctx context.Context, messages []AgentMessage, cause error) (ContextRecoveryResult, error) {
if m.failure == "overflow" {
return ContextRecoveryResult{}, errors.New("recovery failed")
}
return ContextRecoveryResult{View: messages}, nil
}
func TestLifecycleFailedTurnDoesNotCommitProgress(t *testing.T) {
for _, name := range []string{"before_turn", "model_error", "model_panic", "context_error", "context_panic", "overflow", "commit"} {
t.Run(name, func(t *testing.T) {
cfg := LoopConfig{Model: mockModel(assistantMsg("done", StopReasonStop))}
afterTurns := 0
cfg.AfterTurn = func(context.Context, AfterTurnContext) error { afterTurns++; return nil }
switch name {
case "before_turn":
cfg.BeforeTurn = func(context.Context, BeforeTurnContext) ([]AgentMessage, error) {
return nil, errors.New("before failed")
}
case "model_error", "model_panic", "overflow":
cfg.Model = sequentialModel(func(int, *LLMRequest) (*LLMResponse, error) {
if name == "model_panic" {
panic("provider failed")
}
if name == "overflow" {
return nil, ErrContextOverflow
}
return nil, errors.New("provider failed")
})
if name == "overflow" {
cfg.ContextManager = lifecycleManager{failure: "overflow"}
}
case "context_error":
cfg.ContextManager = lifecycleManager{failure: "error"}
case "context_panic":
cfg.ContextManager = lifecycleManager{failure: "panic"}
case "commit":
cfg.CommitMessage = func(message AgentMessage) error {
if modelMessage, include := message.ToMessage(); include && modelMessage.Role == RoleAssistant {
return errors.New("commit failed")
}
return nil
}
}
events := runTestLoop(t, []AgentMessage{UserMsg("hi")}, AgentContext{}, cfg)
turnEnd, ok := findEvent(events, EventTurnEnd)
if !ok || turnEnd.Err == nil || turnEnd.State != nil || turnEnd.TurnIndex != 1 || afterTurns != 0 {
t.Fatalf("turn end = %+v; afterTurns=%d", turnEnd, afterTurns)
}
end, _ := findEvent(events, EventAgentEnd)
if end.Err == nil || end.State.Progress.CompletedTurns != 0 || end.Timestamp.Before(turnEnd.Timestamp) {
t.Fatalf("agent end = %+v", end)
}
if countEvent(events, EventTurnStart) != 1 || countEvent(events, EventTurnEnd) != 1 {
t.Fatal("unpaired turn")
}
if countEvent(events, EventModelExecStart) != countEvent(events, EventModelExecEnd) {
t.Fatal("unpaired model attempt")
}
if countEvent(events, EventContextPrepareStart) != countEvent(events, EventContextPrepareEnd) {
t.Fatal("unpaired context preparation")
}
if name == "context_error" || name == "context_panic" || name == "overflow" {
var last Event
for _, ev := range events {
if ev.Type == EventContextPrepareEnd {
last = ev
}
}
if last.Err == nil {
t.Fatal("missing preparation failure")
}
if name == "overflow" && last.ContextOperation != ContextRecoverOverflow {
t.Errorf("operation = %s", last.ContextOperation)
}
}
})
}
}
type lifecycleRetryError struct{ delay time.Duration }
func (e lifecycleRetryError) Error() string { return "retryable" }
func (e lifecycleRetryError) Retryable() bool { return true }
func (e lifecycleRetryError) RetryAfter() time.Duration { return e.delay }
func TestLifecycleRetryWait(t *testing.T) {
for _, abort := range []bool{false, true} {
t.Run(map[bool]string{false: "retry", true: "cancel_wait"}[abort], func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
attempts := 0
model := sequentialModel(func(i int, _ *LLMRequest) (*LLMResponse, error) {
attempts++
if i == 0 {
delay := time.Millisecond
if abort {
delay = time.Minute
}
return nil, lifecycleRetryError{delay}
}
return &LLMResponse{Message: assistantMsg("done", StopReasonStop)}, nil
})
var events []Event
for ev := range AgentLoop(ctx, []AgentMessage{UserMsg("hi")}, AgentContext{}, LoopConfig{Model: model, MaxRetries: 1}) {
events = append(events, ev)
if abort && ev.Type == EventRetryWaitStart {
cancel()
}
}
start, _ := findEvent(events, EventRetryWaitStart)
end, ok := findEvent(events, EventRetryWaitEnd)
if !ok || start.Execution == nil || end.Execution == nil || *start.Execution != *end.Execution || end.Timestamp.Before(start.Timestamp) {
t.Fatalf("wait start/end = %+v / %+v", start, end)
}
wantAttempts := 2
if abort {
wantAttempts = 1
if !errors.Is(end.Err, context.Canceled) {
t.Fatalf("wait error = %v", end.Err)
}
}
if attempts != wantAttempts {
t.Fatalf("attempts=%d", attempts)
}
var got []int
for _, ev := range events {
if ev.Type == EventModelExecStart {
if ev.Execution.ID != "model-1" {
t.Error("retry changed logical ID")
}
got = append(got, ev.Execution.Attempt)
}
}
if !abort && !slices.Equal(got, []int{1, 2}) {
t.Errorf("attempt coordinates = %v", got)
}
if countEvent(events, EventTurnStart) != 1 || countEvent(events, EventTurnEnd) != 1 {
t.Fatal("retry split logical turn")
}
})
}
}
func TestLifecycleSourceTimestampAndCommittedTurn(t *testing.T) {
// Completion precedes consumption: buffered events retain their source time.
ch := make(chan Event, 1)
sink := eventSink{ctx: context.Background(), ch: ch}
sink.emit(Event{Type: EventModelExecEnd})
afterEmission := time.Now()
ev := <-ch
if ev.Timestamp.IsZero() || ev.Timestamp.After(afterEmission) {
t.Fatalf("timestamp = %v", ev.Timestamp)
}
events := runTestLoop(t, []AgentMessage{UserMsg("hi")}, AgentContext{}, LoopConfig{
Model: mockModel(assistantMsg("done", StopReasonStop)),
AfterTurn: func(context.Context, AfterTurnContext) error { return errors.New("save failed") },
})
turn, _ := findEvent(events, EventTurnEnd)
end, _ := findEvent(events, EventAgentEnd)
if countEvent(events, EventTurnEnd) != 1 || turn.State == nil || turn.Err != nil || turn.State.Progress.CompletedTurns != 1 || end.Err == nil {
t.Fatalf("turn=%+v end=%+v", turn, end)
}
}
type lifecycleParallelTool struct{ Tool }
func (lifecycleParallelTool) ConcurrencySafe(json.RawMessage) bool { return true }
func TestLifecycleConcurrentCompletionPreservesTranscriptOrder(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
release := make(chan struct{})
tool := lifecycleParallelTool{NewFuncTool("work", "work", nil, func(ctx context.Context, args json.RawMessage) (json.RawMessage, error) {
execution, _ := ExecutionFromContext(ctx)
if execution.ID == "slow" {
select {
case <-release:
case <-ctx.Done():
return nil, ctx.Err()
}
}
return json.RawMessage(`"ok"`), nil
})}
var completed []string
var results []ToolResult
for ev := range AgentLoop(ctx, []AgentMessage{UserMsg("hi")}, AgentContext{Tools: []Tool{tool}}, LoopConfig{
MaxToolConcurrency: 2,
Model: mockModel(toolCallMsg(ToolCall{ID: "slow", Name: "work", Args: json.RawMessage(`{}`)}, ToolCall{ID: "fast", Name: "work", Args: json.RawMessage(`{}`)}), assistantMsg("done", StopReasonStop)),
}) {
if ev.Type == EventToolExecEnd {
completed = append(completed, ev.ToolID)
if ev.ToolID == "fast" {
close(release)
}
}
if ev.Type == EventTurnEnd && ev.TurnIndex == 1 {
results = ev.ToolResults
}
}
if ctx.Err() != nil {
t.Fatal("parallel tool execution stalled")
}
if !slices.Equal(completed, []string{"fast", "slow"}) {
t.Fatalf("completion order = %v", completed)
}
if len(results) != 2 || results[0].ToolCallID != "slow" || results[1].ToolCallID != "fast" {
t.Fatalf("transcript result order = %+v", results)
}
}
func TestLifecycleStatefulFailedTurnAndCompletedContinuation(t *testing.T) {
agent := NewAgent(WithModel(funcModel(func(context.Context, *LLMRequest) (*LLMResponse, error) { return nil, errors.New("provider failed") })))
var events []Event
agent.Subscribe(func(ev Event) { events = append(events, ev) })
if err := agent.Prompt(context.Background(), "hi"); err != nil {
t.Fatal(err)
}
agent.WaitForIdle()
turn, ok := findEvent(events, EventTurnEnd)
if !ok || turn.State != nil || turn.Err == nil {
t.Fatalf("stateful failed turn = %+v", turn)
}
state := agent.State()
if state.Progress.CompletedTurns != 0 || state.IsRunning || len(state.Messages) != 1 {
t.Fatalf("failed turn changed state: %+v", state)
}
events = collectEvents(AgentLoopContinue(context.Background(), AgentContext{Messages: []AgentMessage{UserMsg("hi")}}, LoopConfig{
Model: mockModel(), InitialState: AgentState{Progress: RunProgress{Active: true, CompletedTurns: 1}},
}))
if countEvent(events, EventTurnStart) != 0 || countEvent(events, EventTurnEnd) != 0 || countEvent(events, EventModelExecStart) != 0 {
t.Fatal("completed continuation started phantom work")
}
}
func TestLifecycleAfterRunErrorTimestamp(t *testing.T) {
agent := NewAgent(WithModel(mockModel(assistantMsg("done", StopReasonStop))), WithAfterRun(func(context.Context, AfterRunContext) error { return errors.New("save failed") }))
var events []Event
agent.Subscribe(func(ev Event) { events = append(events, ev) })
if err := agent.Prompt(context.Background(), "hi"); err != nil {
t.Fatal(err)
}
agent.WaitForIdle()
ev, ok := findEvent(events, EventError)
if !ok || ev.Timestamp.IsZero() {
t.Fatalf("after-run error missing source time: %+v", ev)
}
end, _ := findEvent(events, EventAgentEnd)
if end.Err == nil || end.Timestamp.After(ev.Timestamp) {
t.Fatalf("loop completion should precede outer hook error: %+v", end)
}
}