-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_test.go
More file actions
920 lines (828 loc) · 26.9 KB
/
Copy pathagent_test.go
File metadata and controls
920 lines (828 loc) · 26.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
package agentgo
import (
"context"
"errors"
"fmt"
"reflect"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestAgent_ModelMiddlewareOption(t *testing.T) {
middlewareCalls := 0
agent := NewAgent(
WithModel(mockModel(assistantMsg("done", StopReasonStop))),
WithModelMiddlewares(func(ctx context.Context, call ModelExecution, next ModelExecuteFunc) (ModelResult, error) {
middlewareCalls++
return next(ctx, call)
}),
)
if err := agent.Prompt(context.Background(), "hello"); err != nil {
t.Fatal(err)
}
agent.WaitForIdle()
if middlewareCalls != 1 {
t.Fatalf("model middleware calls = %d, want 1", middlewareCalls)
}
}
func TestAgentInject_WhenRunning_ReturnsSteeredCurrentRun(t *testing.T) {
release := make(chan struct{})
agent := NewAgent(
WithModel(funcModel(func(ctx context.Context, req *LLMRequest) (*LLMResponse, error) {
<-release
return &LLMResponse{Message: assistantMsg("done", StopReasonStop)}, nil
})),
)
if err := agent.Prompt(context.Background(), "start"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
result, err := agent.Inject(context.Background(), UserMsg("runtime steer"))
if err != nil {
t.Fatalf("inject failed: %v", err)
}
if result.Disposition != InjectSteeredCurrentRun {
t.Fatalf("disposition = %q, want %q", result.Disposition, InjectSteeredCurrentRun)
}
close(release)
agent.WaitForIdle()
for _, msg := range agent.Messages() {
if msg.TextContent() == "runtime steer" {
return
}
}
if !agent.HasQueuedMessages() {
t.Fatal("expected injected message to be consumed or remain queued")
}
}
func TestAgentInject_WhenIdleAndAssistantTail_ReturnsResumedIdleRun(t *testing.T) {
agent := NewAgent(
WithModel(mockModel(
assistantMsg("initial", StopReasonStop),
assistantMsg("after inject", StopReasonStop),
)),
)
if err := agent.Prompt(context.Background(), "start"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
agent.WaitForIdle()
result, err := agent.Inject(context.Background(), UserMsg("runtime reminder"))
if err != nil {
t.Fatalf("inject failed: %v", err)
}
if result.Disposition != InjectResumedIdleRun {
t.Fatalf("disposition = %q, want %q", result.Disposition, InjectResumedIdleRun)
}
agent.WaitForIdle()
msgs := agent.Messages()
want := []string{"start", "initial", "runtime reminder", "after inject"}
if len(msgs) != len(want) {
t.Fatalf("expected %d messages, got %d", len(want), len(msgs))
}
for i, msg := range msgs {
if got := msg.TextContent(); got != want[i] {
t.Fatalf("message[%d]: expected %q, got %q", i, want[i], got)
}
}
}
func TestAgentInject_WhenIdleWithoutAssistantTail_ReturnsQueued(t *testing.T) {
agent := NewAgent()
if err := agent.SetMessages([]AgentMessage{UserMsg("only user")}); err != nil {
t.Fatalf("set messages failed: %v", err)
}
result, err := agent.Inject(context.Background(), UserMsg("queued"))
if err != nil {
t.Fatalf("inject failed: %v", err)
}
if result.Disposition != InjectQueued {
t.Fatalf("disposition = %q, want %q", result.Disposition, InjectQueued)
}
if !agent.HasQueuedMessages() {
t.Fatal("expected queued inject message")
}
}
func TestAgentInject_WhenNilMessage_ReturnsError(t *testing.T) {
agent := NewAgent()
if _, err := agent.Inject(context.Background(), nil); err == nil {
t.Fatal("expected nil inject message to fail")
}
}
func TestAgentInject_IsAtomicUnderConcurrentCalls(t *testing.T) {
agent := NewAgent(
WithModel(mockModel(
assistantMsg("initial", StopReasonStop),
assistantMsg("after inject 1", StopReasonStop),
assistantMsg("after inject 2", StopReasonStop),
)),
)
if err := agent.Prompt(context.Background(), "start"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
agent.WaitForIdle()
type injectOutcome struct {
result InjectResult
err error
}
outcomes := make([]injectOutcome, 2)
msgsToInject := []AgentMessage{UserMsg("inject-a"), UserMsg("inject-b")}
var wg sync.WaitGroup
for i := range msgsToInject {
i := i
wg.Add(1)
go func() {
defer wg.Done()
outcomes[i].result, outcomes[i].err = agent.Inject(context.Background(), msgsToInject[i])
}()
}
wg.Wait()
for i, outcome := range outcomes {
if outcome.err != nil {
t.Fatalf("inject[%d] failed: %v", i, outcome.err)
}
}
deadline := time.Now().Add(2 * time.Second)
for {
agent.WaitForIdle()
if !agent.HasQueuedMessages() {
break
}
if time.Now().After(deadline) {
t.Fatal("timed out waiting for queued injected messages to drain")
}
if err := agent.Continue(context.Background()); err != nil {
t.Fatalf("continue failed while draining injected messages: %v", err)
}
}
msgs := agent.Messages()
var texts []string
for _, msg := range msgs {
if text := msg.TextContent(); text != "" {
texts = append(texts, text)
}
}
for _, want := range []string{"inject-a", "inject-b"} {
found := false
for _, got := range texts {
if got == want {
found = true
break
}
}
if !found {
t.Fatalf("expected injected message %q in history, got %v", want, texts)
}
}
}
func TestAgentExecutionErrorDoesNotBecomeConversationMessage(t *testing.T) {
providerErr := errors.New("provider unavailable")
var (
afterRunState AgentState
agentEndState AgentState
sawAgentEnd bool
)
agent := NewAgent(
WithModel(funcModel(func(context.Context, *LLMRequest) (*LLMResponse, error) {
return nil, providerErr
})),
WithMaxRetries(0),
WithAfterRun(func(_ context.Context, run AfterRunContext) error {
afterRunState = run.Snapshot.State
return nil
}),
)
agent.Subscribe(func(ev Event) {
if ev.Type == EventAgentEnd && ev.State != nil {
agentEndState = *ev.State
sawAgentEnd = true
}
})
if err := agent.Prompt(context.Background(), "trigger"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
agent.WaitForIdle()
if !sawAgentEnd {
t.Fatal("listener never received EventAgentEnd with state")
}
state := agent.State()
if state.Error != providerErr.Error() {
t.Fatalf("agent error = %q, want %q", state.Error, providerErr)
}
if !reflect.DeepEqual(afterRunState.Messages, agentEndState.Messages) {
t.Fatalf("AfterRun messages = %#v, EventAgentEnd messages = %#v", afterRunState.Messages, agentEndState.Messages)
}
if !reflect.DeepEqual(agentEndState.Messages, state.Messages) {
t.Fatalf("EventAgentEnd messages = %#v, Agent messages = %#v", agentEndState.Messages, state.Messages)
}
if len(state.Messages) != 1 || state.Messages[0].GetRole() != RoleUser || state.Messages[0].TextContent() != "trigger" {
t.Fatalf("execution error changed conversation: %#v", state.Messages)
}
if !reflect.DeepEqual(agentEndState.TotalUsage, state.TotalUsage) || !reflect.DeepEqual(agentEndState.Progress, state.Progress) {
t.Fatalf("Agent did not project final loop state: end=%#v agent=%#v", agentEndState, state)
}
}
func TestAgentTerminalErrorMessageRemainsInConversation(t *testing.T) {
agent := NewAgent(
WithModel(mockModel(assistantMsg("terminal", StopReasonError))),
)
if err := agent.Prompt(context.Background(), "trigger"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
agent.WaitForIdle()
messages := agent.Messages()
if len(messages) != 2 {
t.Fatalf("messages = %#v, want committed user and terminal model messages", messages)
}
terminal, ok := messages[1].(Message)
if !ok || terminal.StopReason != StopReasonError || terminal.TextContent() != "terminal" {
t.Fatalf("terminal model message = %#v", messages[1])
}
}
// midStreamErrorModel emits a text delta (populating Agent.streamMessage)
// then injects a StreamEventError so the agent surfaces EventError mid-stream.
type midStreamErrorModel struct{}
func (m *midStreamErrorModel) Generate(context.Context, []Message, []ToolSpec, ...CallOption) (*LLMResponse, error) {
return nil, fmt.Errorf("Generate not used")
}
func (m *midStreamErrorModel) GenerateStream(_ context.Context, _ []Message, _ []ToolSpec, _ ...CallOption) (<-chan StreamEvent, error) {
ch := make(chan StreamEvent, 4)
partial := Message{Role: RoleAssistant, Content: []ContentBlock{TextBlock("")}}
ch <- StreamEvent{Type: StreamEventTextStart, ContentIndex: 0, Message: partial}
partial.Content[0].Text = "half-formed..."
ch <- StreamEvent{Type: StreamEventTextDelta, ContentIndex: 0, Delta: "half-formed...", Message: partial}
ch <- StreamEvent{Type: StreamEventError, Err: fmt.Errorf("provider stream error")}
close(ch)
return ch, nil
}
func (m *midStreamErrorModel) SupportsTools() bool { return true }
// EventError listeners must see a cleared StreamMessage when calling State().
// Without this guarantee, the listener observes a never-completing partial
// the agent has already abandoned — confusing UI rendering and any caller
// that snapshots agent state at error time.
func TestEventError_ClearsStreamMessageBeforeListeners(t *testing.T) {
agent := NewAgent(
WithModel(&midStreamErrorModel{}),
WithMaxRetries(0),
)
var (
mu sync.Mutex
errorSnapshot AgentState
sawError bool
sawMidStream bool
done = make(chan struct{})
closeDoneOnce sync.Once
)
agent.Subscribe(func(ev Event) {
switch ev.Type {
case EventMessageUpdate:
// Sanity check: streamMessage must be populated mid-stream,
// otherwise the EventError assertion below would be vacuous.
if agent.State().StreamMessage != nil {
mu.Lock()
sawMidStream = true
mu.Unlock()
}
case EventError:
mu.Lock()
errorSnapshot = agent.State()
sawError = true
mu.Unlock()
case EventAgentEnd:
closeDoneOnce.Do(func() { close(done) })
}
})
if err := agent.Prompt(context.Background(), "trigger"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for EventAgentEnd")
}
agent.WaitForIdle()
mu.Lock()
gotSawMidStream := sawMidStream
gotSawError := sawError
gotErrorSnapshot := errorSnapshot
mu.Unlock()
if !gotSawMidStream {
t.Fatal("test setup broken: streamMessage was never populated mid-stream")
}
if !gotSawError {
t.Fatal("listener never received EventError")
}
if gotErrorSnapshot.StreamMessage != nil {
t.Fatalf("StreamMessage must be cleared before EventError listeners run, got %+v", gotErrorSnapshot.StreamMessage)
}
state := agent.State()
if len(state.Messages) != 1 || state.Messages[0].TextContent() != "trigger" {
t.Fatalf("partial stream or execution error leaked into conversation: %#v", state.Messages)
}
}
// A run started from another run's EventAgentEnd listener — exactly how a
// harness auto-continues — must survive the finishing run's cleanup. The
// finishing run's consumeLoop defer once reset a.cancel, which the synchronous
// resume had already reassigned to the new run, aborting it the instant it
// began. The fix: cleanup uses the run's own captured cancel and only resets
// shared state when no newer run has taken over (a.done == myDone).
func TestResumeFromAgentEndListenerSurvivesCleanup(t *testing.T) {
started := make(chan struct{})
release := make(chan struct{})
cancelled := make(chan struct{})
var calls atomic.Int32
agent := NewAgent(WithModel(funcModel(func(ctx context.Context, _ *LLMRequest) (*LLMResponse, error) {
if calls.Add(1) == 1 {
return &LLMResponse{Message: assistantMsg("run 1", StopReasonStop)}, nil
}
// Resumed run: announce arrival, then hold until released — unless the
// finishing run's cleanup wrongly cancels our ctx.
close(started)
select {
case <-release:
return &LLMResponse{Message: assistantMsg("run 2", StopReasonStop)}, nil
case <-ctx.Done():
close(cancelled)
return nil, ctx.Err()
}
})))
var resumed atomic.Bool
agent.Subscribe(func(ev Event) {
if ev.Type == EventAgentEnd && !resumed.Swap(true) {
_, _ = agent.Inject(context.Background(), UserMsg("resume"))
}
})
if err := agent.Prompt(context.Background(), "start"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
select {
case <-started:
// Resumed run reached the model; give the finishing run's defer its
// window to (wrongly) cancel it.
select {
case <-cancelled:
t.Fatal("resumed run was cancelled by the finishing run's cleanup")
case <-time.After(200 * time.Millisecond):
}
close(release)
case <-cancelled:
t.Fatal("resumed run was cancelled before reaching the model")
case <-time.After(2 * time.Second):
t.Fatal("resumed run never started")
}
agent.WaitForIdle()
msgs := agent.Messages()
if got := msgs[len(msgs)-1].TextContent(); got != "run 2" {
t.Fatalf("resumed run did not complete: last message = %q, want %q", got, "run 2")
}
}
// holdWithTimeout runs HoldRuns on a goroutine so a deadlock fails the test
// instead of hanging the package.
func holdWithTimeout(t *testing.T, agent *Agent) (release func()) {
t.Helper()
done := make(chan struct{})
go func() {
release = agent.HoldRuns()
close(done)
}()
select {
case <-done:
return release
case <-time.After(2 * time.Second):
t.Fatal("HoldRuns did not return")
return nil
}
}
func TestHoldRunsDrainsActiveRun(t *testing.T) {
started := make(chan struct{})
sawCancel := make(chan struct{})
agent := NewAgent(WithModel(funcModel(func(ctx context.Context, _ *LLMRequest) (*LLMResponse, error) {
close(started)
<-ctx.Done()
close(sawCancel)
return nil, ctx.Err()
})))
if err := agent.Prompt(context.Background(), "start"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
<-started
release := holdWithTimeout(t, agent)
defer release()
select {
case <-sawCancel:
default:
t.Fatal("model never observed the hold's cancellation")
}
if agent.State().IsRunning {
t.Fatal("agent still running after HoldRuns returned")
}
// The cancel is silent: no abort marker message may be appended.
msgs := agent.Messages()
if len(msgs) != 1 || msgs[0].TextContent() != "start" {
var texts []string
for _, m := range msgs {
texts = append(texts, m.TextContent())
}
t.Fatalf("history polluted by hold drain: %v", texts)
}
}
func TestHoldRunsBlocksPromptContinueInject(t *testing.T) {
agent := NewAgent(WithModel(mockModel(assistantMsg("later", StopReasonStop))))
if err := agent.SetMessages([]AgentMessage{
UserMsg("earlier"),
assistantMsg("earlier reply", StopReasonStop),
}); err != nil {
t.Fatalf("set messages failed: %v", err)
}
release := holdWithTimeout(t, agent)
if err := agent.Prompt(context.Background(), "blocked"); !errors.Is(err, ErrRunsHeld) {
t.Fatalf("Prompt during hold: err = %v, want ErrRunsHeld", err)
}
agent.Steer(UserMsg("queued steer"))
if err := agent.Continue(context.Background()); !errors.Is(err, ErrRunsHeld) {
t.Fatalf("Continue during hold: err = %v, want ErrRunsHeld", err)
}
if !agent.HasQueuedMessages() {
t.Fatal("held Continue must not consume queued messages")
}
if _, err := agent.Inject(context.Background(), UserMsg("blocked inject")); !errors.Is(err, ErrRunsHeld) {
t.Fatalf("Inject during hold: err = %v, want ErrRunsHeld", err)
}
release()
if err := agent.Prompt(context.Background(), "resumes"); err != nil {
t.Fatalf("prompt after release failed: %v", err)
}
agent.WaitForIdle()
// The steer queued during the hold survives it (queues are untouched) and
// is delivered to the released run via its initial steering poll.
var steerDelivered bool
for _, m := range agent.Messages() {
if m.TextContent() == "queued steer" {
steerDelivered = true
}
}
if !steerDelivered {
t.Fatal("steer queued during hold was not delivered to the post-release run")
}
}
func TestHoldRunsCounterAndIdempotentRelease(t *testing.T) {
agent := NewAgent(WithModel(mockModel(assistantMsg("ok", StopReasonStop))))
release1 := holdWithTimeout(t, agent)
release2 := holdWithTimeout(t, agent)
release1()
release1() // idempotent: must not decrement twice
if err := agent.Prompt(context.Background(), "still held"); !errors.Is(err, ErrRunsHeld) {
t.Fatalf("one holder released, err = %v, want ErrRunsHeld", err)
}
release2()
if err := agent.Prompt(context.Background(), "released"); err != nil {
t.Fatalf("prompt after all releases failed: %v", err)
}
agent.WaitForIdle()
}
func TestHoldRunsRejectsListenerAutoContinueWithoutDeadlock(t *testing.T) {
started := make(chan struct{})
agent := NewAgent(WithModel(funcModel(func(ctx context.Context, _ *LLMRequest) (*LLMResponse, error) {
close(started)
<-ctx.Done()
return nil, ctx.Err()
})))
var injectErr error
injectDone := make(chan struct{})
agent.Subscribe(func(ev Event) {
if ev.Type == EventAgentEnd {
_, injectErr = agent.Inject(context.Background(), UserMsg("auto resume"))
close(injectDone)
}
})
if err := agent.Prompt(context.Background(), "start"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
<-started
// The hold's cancel triggers EventAgentEnd, whose listener attempts an
// auto-continue while the hold is draining. It must fail fast — a blocking
// acquire here would deadlock HoldRuns against the listener goroutine.
release := holdWithTimeout(t, agent)
defer release()
select {
case <-injectDone:
case <-time.After(2 * time.Second):
t.Fatal("EventAgentEnd listener never ran")
}
if !errors.Is(injectErr, ErrRunsHeld) {
t.Fatalf("listener auto-continue: err = %v, want ErrRunsHeld", injectErr)
}
if agent.State().IsRunning {
t.Fatal("agent running after hold drained a listener-resume attempt")
}
}
func TestInjectDuringHoldFailsFastWithoutQueueing(t *testing.T) {
agent := NewAgent()
if err := agent.SetMessages([]AgentMessage{
UserMsg("q"),
assistantMsg("a", StopReasonStop),
}); err != nil {
t.Fatalf("set messages failed: %v", err)
}
release := holdWithTimeout(t, agent)
defer release()
if _, err := agent.Inject(context.Background(), UserMsg("dropped")); !errors.Is(err, ErrRunsHeld) {
t.Fatalf("inject during hold: err = %v, want ErrRunsHeld", err)
}
if agent.HasQueuedMessages() {
t.Fatal("held inject must not leave the message queued (double delivery once resumed)")
}
}
func TestHoldRunsVsConcurrentPromptRace(t *testing.T) {
agent := NewAgent(WithModel(funcModel(func(_ context.Context, _ *LLMRequest) (*LLMResponse, error) {
return &LLMResponse{Message: assistantMsg("quick", StopReasonStop)}, nil
})))
stop := make(chan struct{})
var wg sync.WaitGroup
wg.Go(func() {
for {
select {
case <-stop:
return
default:
_ = agent.Prompt(context.Background(), "spin")
}
}
})
for range 20 {
release := holdWithTimeout(t, agent)
if agent.State().IsRunning {
release()
t.Fatal("run in flight while held")
}
release()
}
close(stop)
wg.Wait()
agent.WaitForIdle()
}
func TestHoldRunsOnIdleAgentReturnsImmediately(t *testing.T) {
// Never ran: done is nil.
fresh := NewAgent()
release := holdWithTimeout(t, fresh)
release()
// Ran to completion: done is a closed channel.
agent := NewAgent(WithModel(mockModel(
assistantMsg("done", StopReasonStop),
assistantMsg("after", StopReasonStop),
)))
if err := agent.Prompt(context.Background(), "start"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
agent.WaitForIdle()
release = holdWithTimeout(t, agent)
if err := agent.Prompt(context.Background(), "held"); !errors.Is(err, ErrRunsHeld) {
t.Fatalf("prompt during idle hold: err = %v, want ErrRunsHeld", err)
}
release()
if err := agent.Prompt(context.Background(), "released"); err != nil {
t.Fatalf("prompt after release failed: %v", err)
}
agent.WaitForIdle()
}
func TestResetDrainsActiveRunAndLeavesAgentUsable(t *testing.T) {
started := make(chan struct{})
var calls atomic.Int32
agent := NewAgent(WithModel(funcModel(func(ctx context.Context, _ *LLMRequest) (*LLMResponse, error) {
if calls.Add(1) == 1 {
close(started)
<-ctx.Done()
return nil, ctx.Err()
}
return &LLMResponse{Message: assistantMsg("fresh", StopReasonStop)}, nil
})))
if err := agent.Prompt(context.Background(), "start"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
<-started
resetDone := make(chan struct{})
go func() {
agent.Reset()
close(resetDone)
}()
select {
case <-resetDone:
case <-time.After(2 * time.Second):
t.Fatal("Reset did not return while a run was in flight")
}
if agent.State().IsRunning {
t.Fatal("agent running after Reset")
}
if got := len(agent.Messages()); got != 0 {
t.Fatalf("history not cleared: %d messages remain", got)
}
if err := agent.Prompt(context.Background(), "again"); err != nil {
t.Fatalf("prompt after reset failed: %v", err)
}
agent.WaitForIdle()
msgs := agent.Messages()
if got := msgs[len(msgs)-1].TextContent(); got != "fresh" {
t.Fatalf("post-reset run did not complete: last message = %q", got)
}
}
func TestSetMessagesWhileRunningReturnsErrAlreadyRunning(t *testing.T) {
started := make(chan struct{})
release := make(chan struct{})
agent := NewAgent(WithModel(funcModel(func(_ context.Context, _ *LLMRequest) (*LLMResponse, error) {
close(started)
<-release
return &LLMResponse{Message: assistantMsg("done", StopReasonStop)}, nil
})))
if err := agent.Prompt(context.Background(), "start"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
<-started
if err := agent.SetMessages(nil); !errors.Is(err, ErrAlreadyRunning) {
t.Fatalf("clear during run: err = %v, want ErrAlreadyRunning", err)
}
close(release)
agent.WaitForIdle()
if err := agent.SetMessages(nil); err != nil {
t.Fatalf("clear while idle failed: %v", err)
}
if got := len(agent.Messages()); got != 0 {
t.Fatalf("history not cleared: %d messages remain", got)
}
}
// TestInjectDuringHoldChurnNeverStrandsMessage races Inject against a
// hold/release churn goroutine. The invariant under test is Inject's atomic
// idle-resume: whenever an inject fails with ErrRunsHeld the message
// must NOT be left queued (the caller reroutes it — a queued copy would
// double-deliver on the next resume), and a reported resume really started.
func TestInjectDuringHoldChurnNeverStrandsMessage(t *testing.T) {
agent := NewAgent(WithModel(funcModel(func(_ context.Context, _ *LLMRequest) (*LLMResponse, error) {
return &LLMResponse{Message: assistantMsg("reply", StopReasonStop)}, nil
})))
if err := agent.SetMessages([]AgentMessage{
UserMsg("q"),
assistantMsg("a", StopReasonStop),
}); err != nil {
t.Fatalf("set messages failed: %v", err)
}
stop := make(chan struct{})
var wg sync.WaitGroup
wg.Go(func() {
for {
select {
case <-stop:
return
default:
release := agent.HoldRuns()
release()
}
}
})
for range 50 {
// Per-iteration reset so the queue is provably empty going in: a run the
// churn cancels mid-flight leaves a user tail, and the next inject then
// queues legitimately — without the reset that queued message would trip
// the ErrRunsHeld assertion below. Only inject starts runs here, so the
// agent is reliably idle after WaitForIdle.
agent.WaitForIdle()
agent.ClearAllQueues()
if err := agent.SetMessages([]AgentMessage{
UserMsg("q"),
assistantMsg("a", StopReasonStop),
}); err != nil {
t.Fatalf("reset messages failed: %v", err)
}
res, err := agent.Inject(context.Background(), UserMsg("inject"))
switch {
case errors.Is(err, ErrRunsHeld):
if agent.HasQueuedMessages() {
t.Fatal("ErrRunsHeld inject left the message queued")
}
case err != nil:
t.Fatalf("inject failed: %v", err)
case res.Disposition == InjectResumedIdleRun:
agent.WaitForIdle()
}
}
close(stop)
wg.Wait()
agent.WaitForIdle()
}
func TestFollowUpSurvivesHoldAndDeliversAfterRelease(t *testing.T) {
agent := NewAgent(WithModel(mockModel(assistantMsg("follow-up reply", StopReasonStop))))
if err := agent.SetMessages([]AgentMessage{
UserMsg("q"),
assistantMsg("a", StopReasonStop),
}); err != nil {
t.Fatalf("set messages failed: %v", err)
}
agent.FollowUp(UserMsg("queued follow-up"))
release := holdWithTimeout(t, agent)
if err := agent.Continue(context.Background()); !errors.Is(err, ErrRunsHeld) {
t.Fatalf("Continue during hold: err = %v, want ErrRunsHeld", err)
}
if !agent.HasFollowUps() {
t.Fatal("hold consumed the follow-up queue")
}
release()
if err := agent.Continue(context.Background()); err != nil {
t.Fatalf("continue after release failed: %v", err)
}
agent.WaitForIdle()
if agent.HasFollowUps() {
t.Fatal("released Continue did not consume the follow-up")
}
var delivered bool
for _, m := range agent.Messages() {
if m.TextContent() == "queued follow-up" {
delivered = true
}
}
if !delivered {
t.Fatal("follow-up never delivered to the resumed run")
}
}
func TestTwoHoldersDrainOneActiveRun(t *testing.T) {
started := make(chan struct{})
var calls atomic.Int32
agent := NewAgent(WithModel(funcModel(func(ctx context.Context, _ *LLMRequest) (*LLMResponse, error) {
if calls.Add(1) == 1 {
close(started)
<-ctx.Done()
return nil, ctx.Err()
}
return &LLMResponse{Message: assistantMsg("fresh", StopReasonStop)}, nil
})))
if err := agent.Prompt(context.Background(), "start"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
<-started
// Both holders target the same in-flight run: both must drain and return.
results := make(chan func(), 2)
for range 2 {
go func() { results <- agent.HoldRuns() }()
}
var releases []func()
for range 2 {
select {
case r := <-results:
releases = append(releases, r)
case <-time.After(2 * time.Second):
t.Fatal("concurrent HoldRuns on one active run did not both return")
}
}
if agent.State().IsRunning {
t.Fatal("run in flight while held twice")
}
releases[0]()
if err := agent.Prompt(context.Background(), "still held"); !errors.Is(err, ErrRunsHeld) {
t.Fatalf("one holder released, err = %v, want ErrRunsHeld", err)
}
releases[1]()
if err := agent.Prompt(context.Background(), "released"); err != nil {
t.Fatalf("prompt after all releases failed: %v", err)
}
agent.WaitForIdle()
}
// TestAbortWhileIdleDoesNotArmMarkerForLaterHold pins the idle-Abort guard: an
// Abort with no run in flight must be a full no-op. Before the guard it armed
// wantAbortMarker with nothing to consume it, so the next silent cancellation
// (a HoldRuns drain) emitted an abort marker it never requested.
func TestAbortWhileIdleDoesNotArmMarkerForLaterHold(t *testing.T) {
started := make(chan struct{})
agent := NewAgent(WithModel(funcModel(func(ctx context.Context, _ *LLMRequest) (*LLMResponse, error) {
close(started)
<-ctx.Done()
return nil, ctx.Err()
})))
agent.Abort() // idle: nothing to interrupt
if err := agent.Prompt(context.Background(), "start"); err != nil {
t.Fatalf("prompt failed: %v", err)
}
<-started
release := holdWithTimeout(t, agent)
defer release()
msgs := agent.Messages()
if len(msgs) != 1 || msgs[0].TextContent() != "start" {
var texts []string
for _, m := range msgs {
texts = append(texts, m.TextContent())
}
t.Fatalf("stale idle Abort polluted the hold drain: %v", texts)
}
}
func TestHasFollowUpsTracksOnlyFollowUpQueue(t *testing.T) {
agent := NewAgent()
agent.Steer(UserMsg("steer"))
if agent.HasFollowUps() {
t.Fatal("steering message must not count as a follow-up")
}
agent.FollowUp(UserMsg("follow up"))
if !agent.HasFollowUps() {
t.Fatal("queued follow-up was not reported")
}
agent.ClearFollowUpQueue()
if agent.HasFollowUps() {
t.Fatal("cleared follow-up queue was still reported")
}
if !agent.HasQueuedMessages() {
t.Fatal("clearing follow-ups must not clear steering messages")
}
}