Repository navigation
Expand file tree
/
Copy pathattention_test.go
More file actions
98 lines (90 loc) · 2.89 KB
/
Copy pathattention_test.go
File metadata and controls
98 lines (90 loc) · 2.89 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
package remote
import (
"context"
"sync/atomic"
"testing"
"github.com/Agent-Field/codeaf/internal/session"
)
// Attention is a conversation fact; it must precede the event that wakes its
// hidden view, and reading it must never send a request back to the engine.
type attentionAgent struct {
*fakeAgent
waiting atomic.Bool
}
func (a *attentionAgent) NeedsPerson() bool { return a.waiting.Load() }
func TestHiddenConversationAttentionArrivesBeforeItsQuestionAndSettlement(t *testing.T) {
far := &attentionAgent{fakeAgent: &fakeAgent{model: "m"}}
loop, err := Loopback(Hello{}, Options{Boot: func(Hello) (*Engine, error) { return &Engine{Agent: far}, nil }})
if err != nil {
t.Fatal(err)
}
defer loop.Close()
handle := loop.Client.Agent()
events, err := handle.Submit(context.Background(), "permission")
if err != nil {
t.Fatal(err)
}
stream := waitForStream(t, far.fakeAgent)
for _, step := range []struct {
kind session.EventKind
waiting bool
}{
{session.EventConsentRequest, true},
{session.EventToolEnd, false},
{session.EventConnectAsk, true},
{session.EventConnectDone, false},
{session.EventTurnDone, false},
} {
far.waiting.Store(step.waiting)
stream <- session.Event{Kind: step.kind, ID: 1}
if ev := <-events; ev.Kind != step.kind {
t.Fatalf("event %v, want %v", ev.Kind, step.kind)
}
before := loop.CallsMade()
if handle.NeedsPerson() != step.waiting {
t.Fatalf("attention after %v = %v", step.kind, handle.NeedsPerson())
}
if loop.CallsMade() != before {
t.Fatal("reading attention crossed the wire")
}
}
far.finish(stream)
}
// A keeper observes the turn through its own connection-local subscription.
// Its question may arrive before the ordinary Submit pump has forwarded it.
type attentionObservedAgent struct {
*observedAgent
waiting atomic.Bool
}
func (a *attentionObservedAgent) NeedsPerson() bool { return a.waiting.Load() }
func TestObserverPublishesAttentionBeforeWakingTheKeeper(t *testing.T) {
far := &attentionObservedAgent{observedAgent: &observedAgent{fakeAgent: &fakeAgent{model: "m"}}}
loop, err := Loopback(Hello{}, Options{Boot: func(Hello) (*Engine, error) { return &Engine{Agent: far}, nil }})
if err != nil {
t.Fatal(err)
}
defer loop.Close()
handle := loop.Client.Agent()
events, running, stop := handle.Attach()
defer stop()
if !running {
t.Fatal("observer missing")
}
observerEvent(t, events, "before")
far.waiting.Store(true)
far.mu.Lock()
far.readers[0] <- session.Event{Kind: session.EventConsentRequest, ID: 7, Text: "question"}
far.mu.Unlock()
observerEvent(t, events, "question")
if !handle.NeedsPerson() {
t.Fatal("keeper woke before attention arrived")
}
far.waiting.Store(false)
far.mu.Lock()
far.readers[0] <- session.Event{Kind: session.EventTurnDone, Text: "stopped"}
far.mu.Unlock()
observerEvent(t, events, "stopped")
if handle.NeedsPerson() {
t.Fatal("stopped turn still needs a person")
}
}