Repository navigation
Expand file tree
/
Copy pathhead_test.go
More file actions
1259 lines (1199 loc) · 48.2 KB
/
Copy pathhead_test.go
File metadata and controls
1259 lines (1199 loc) · 48.2 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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package head
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/Agent-Field/agentfield/sdk/go/ai"
"github.com/Agent-Field/codeaf/internal/resident"
"github.com/Agent-Field/codeaf/internal/store"
)
type headModalities bool
func (supported headModalities) Supports(_, direction, modality string) bool {
return bool(supported) && direction == "input" && modality == "image"
}
func TestHeadRoutesImagePartAndPreservesAttachmentOnCommand(t *testing.T) {
graph := openHeadStore(t)
path := filepath.Join(t.TempDir(), "diagram.png")
if err := os.WriteFile(path, []byte("pixels"), 0o644); err != nil {
t.Fatal(err)
}
client := &beltClient{turns: []beltTurn{
{calls: []ai.ToolCall{beltCall("s1", beltToolTask, map[string]any{
"instruction": "inspect the diagram"})}},
{text: "I’ll inspect that."},
}}
user, err := graph.PostMessage(store.Message{
SessionID: "vision", Role: store.RoleUser, Body: "inspect the diagram", Attachments: []string{path},
})
if err != nil {
t.Fatal(err)
}
head := New(client, graph).WithImageInput(headModalities(true), "vision/model")
if err := head.answer(context.Background(), user); err != nil {
t.Fatal(err)
}
seenImage := false
for _, part := range client.seen[1].Content {
seenImage = seenImage || part.Type == "image_url" && part.ImageURL != nil && strings.HasPrefix(part.ImageURL.URL, "data:image/png;base64,")
}
if !seenImage {
t.Fatalf("head messages omitted image part: %+v", client.seen)
}
commands, err := graph.PendingCommands(10)
if err != nil || len(commands) != 1 || len(commands[0].Attachments) != 1 || commands[0].Attachments[0] != path {
t.Fatalf("commands = %+v err=%v", commands, err)
}
}
type fakeClient struct {
mutex sync.Mutex
model string
responses []string
calls int
seen []ai.Message
}
func (client *fakeClient) CompleteWithMessages(_ context.Context, messages []ai.Message, _ ...ai.Option) (*ai.Response, error) {
client.mutex.Lock()
defer client.mutex.Unlock()
client.calls++
client.seen = append([]ai.Message(nil), messages...)
if len(client.responses) == 0 {
return nil, errors.New("no fake response left")
}
text := client.responses[0]
client.responses = client.responses[1:]
response := textResponse(text)
response.Model = client.model
return response, nil
}
func (client *fakeClient) Model() string { return client.model }
func (client *fakeClient) callCount() int {
client.mutex.Lock()
defer client.mutex.Unlock()
return client.calls
}
// systemPrompt is which of the head's prompts the last call carried. It is how a
// test says "not the router" now that more than one path speaks to a model.
func (client *fakeClient) systemPrompt() string {
client.mutex.Lock()
defer client.mutex.Unlock()
if len(client.seen) == 0 {
return ""
}
var text strings.Builder
for _, part := range client.seen[0].Content {
text.WriteString(part.Text)
}
return text.String()
}
// userPrompt is everything the last call said below the system message. Tests
// that pin where a block sits need both halves, because "which message carries
// it" is now part of what is being asserted.
func (client *fakeClient) userPrompt() string {
client.mutex.Lock()
defer client.mutex.Unlock()
if len(client.seen) < 2 {
return ""
}
var text strings.Builder
for _, part := range client.seen[1].Content {
text.WriteString(part.Text)
}
return text.String()
}
func TestHeadPostsReply(t *testing.T) {
graphStore := openHeadStore(t)
// One call, no tools asked for, and the words are the reply. There is no
// envelope to parse any more: what the model says IS what the person reads.
client := &fakeClient{responses: []string{"The graph is ready and waiting for work."}}
if _, err := graphStore.PostMessage(store.Message{SessionID: "chat-1", Role: store.RoleUser, Body: "what is happening?"}); err != nil {
t.Fatalf("post user message: %v", err)
}
stop := startServing(t, New(client, graphStore))
defer stop()
reply := waitForAgentReply(t, graphStore, "chat-1", 0)
if reply.Body != "The graph is ready and waiting for work." {
t.Fatalf("reply body = %q", reply.Body)
}
if reply.SessionID != "chat-1" || reply.CommandSeq != 0 {
t.Fatalf("reply metadata wrong: %+v", reply)
}
if calls := client.callCount(); calls != 1 {
t.Fatalf("provider calls = %d, want 1", calls)
}
}
func TestHeadUsesPerMessageClientAndPersistsResolvedReplyModel(t *testing.T) {
graph := openHeadStore(t)
talk := &fakeClient{model: "cheap/talk", responses: []string{"ordinary answer"}}
boost := &fakeClient{model: "anthropic/claude-opus-5-2026-08-01", responses: []string{"boosted answer"}}
var selected []string
conversationalHead := New(talk, graph).WithMessageClient(func(message store.Message) (Client, error) {
selected = append(selected, message.Model)
return boost, nil
})
ordinary, err := graph.PostMessage(store.Message{SessionID: "client-override", Role: store.RoleUser, Body: "easy"})
if err != nil {
t.Fatal(err)
}
if err := conversationalHead.answer(context.Background(), ordinary); err != nil {
t.Fatal(err)
}
boosted, err := graph.PostMessage(store.Message{
SessionID: "client-override", Role: store.RoleUser, Body: "hard", Model: "anthropic/claude-opus-5",
})
if err != nil {
t.Fatal(err)
}
if err := conversationalHead.answer(context.Background(), boosted); err != nil {
t.Fatal(err)
}
if talk.callCount() != 1 || boost.callCount() != 1 || len(selected) != 1 || selected[0] != boosted.Model {
t.Fatalf("client calls talk=%d boost=%d selected=%v", talk.callCount(), boost.callCount(), selected)
}
messages, err := graph.Messages("client-override", ordinary.Seq, 10)
if err != nil {
t.Fatal(err)
}
var replies []store.Message
for _, message := range messages {
if message.Role == store.RoleAgent {
replies = append(replies, message)
}
}
if len(replies) != 2 || replies[0].Model != "" || replies[1].Model != boost.model {
t.Fatalf("reply attribution = %+v", replies)
}
}
func TestHeadResolvesReferencedAgentQuestionWithoutRoutingNewWork(t *testing.T) {
graph := openHeadStore(t)
question, err := graph.AskQuestion(store.AgentQuestion{
SessionID: "agent-question", Text: "Which tone should I use?", Urgency: store.QuestionWhenever,
})
if err != nil {
t.Fatal(err)
}
if _, err := graph.SurfaceQuestion(question.Seq); err != nil {
t.Fatal(err)
}
user, err := graph.PostMessage(store.Message{
SessionID: "agent-question", Role: store.RoleUser, Body: "Keep it concise.",
QuestionSeq: question.Seq,
})
if err != nil {
t.Fatal(err)
}
client := &fakeClient{}
if err := New(client, graph).answer(context.Background(), user); err != nil {
t.Fatal(err)
}
if calls := client.callCount(); calls != 0 {
t.Fatalf("answer was routed as new work: provider calls=%d", calls)
}
resolved, found, err := graph.AgentQuestionBySeq(question.Seq)
if err != nil || !found || resolved.Status != store.QuestionAnswered ||
resolved.Resolution != user.Body || resolved.AnswerMessageSeq != user.Seq {
t.Fatalf("resolved question = %+v found=%t err=%v", resolved, found, err)
}
messages, err := graph.Messages("agent-question", user.Seq, 0)
if err != nil || len(messages) != 1 || messages[0].Role != store.RoleAgent ||
!strings.Contains(messages[0].Body, "use that") {
t.Fatalf("answer acknowledgement = %+v err=%v", messages, err)
}
}
func TestHeadRequestsSpliceAndLinksReply(t *testing.T) {
graphStore := openHeadStore(t)
// Commissioning work is a tool call inside the turn rather than the turn's
// terminal decision, and the receipt still has to be tied to the command the
// tool journaled — otherwise a reply claims work that has no row.
client := &beltClient{turns: []beltTurn{
{calls: []ai.ToolCall{beltCall("s1", beltToolTask, map[string]any{"instruction": "make me X"})}},
{text: "Splicing that in — I'll report when it lands."},
}}
user, err := graphStore.PostMessage(store.Message{SessionID: "chat-2", Role: store.RoleUser, Body: "make me X"})
if err != nil {
t.Fatalf("post user message: %v", err)
}
stop := startServing(t, New(client, graphStore))
defer stop()
reply := waitForAgentReply(t, graphStore, "chat-2", user.Seq)
commands, err := graphStore.PendingCommands(0)
if err != nil {
t.Fatalf("list pending commands: %v", err)
}
if len(commands) != 1 {
t.Fatalf("pending commands = %+v, want one", commands)
}
command := commands[0]
if command.Kind != store.CommandSplice || command.Instruction != "make me X" || command.SessionID != "chat-2" {
t.Fatalf("command wrong: %+v", command)
}
if reply.CommandSeq != command.Seq {
t.Fatalf("reply command seq = %d, command seq = %d", reply.CommandSeq, command.Seq)
}
if command.Seq >= reply.Seq {
t.Fatalf("command %d was not persisted before reply %d", command.Seq, reply.Seq)
}
}
func TestHeadMalformedOutputFallsBackToRawReply(t *testing.T) {
graphStore := openHeadStore(t)
client := &fakeClient{responses: []string{"I can still answer this plainly."}}
user, err := graphStore.PostMessage(store.Message{SessionID: "chat-3", Role: store.RoleUser, Body: "hello"})
if err != nil {
t.Fatalf("post user message: %v", err)
}
stop := startServing(t, New(client, graphStore))
defer stop()
reply := waitForAgentReply(t, graphStore, "chat-3", user.Seq)
if reply.Body != "I can still answer this plainly." || reply.CommandSeq != 0 {
t.Fatalf("fallback reply wrong: %+v", reply)
}
commands, err := graphStore.PendingCommands(0)
if err != nil {
t.Fatalf("list pending commands: %v", err)
}
if len(commands) != 0 {
t.Fatalf("malformed output emitted commands: %+v", commands)
}
}
func TestRenderNotebookUsesMessageScopeCues(t *testing.T) {
graphStore := openHeadStore(t)
fact, err := graphStore.RecordFact("", "file:internal/resident/notebook.go", store.FactQuirk,
"scope-only memory with unrelated vocabulary")
if err != nil {
t.Fatalf("record fact: %v", err)
}
rendered := renderNotebook(graphStore, "Please inspect internal/resident/notebook.go", "", notebookContextBytes)
if !strings.Contains(rendered, fmt.Sprintf("#%d [", fact.Seq)) ||
!strings.Contains(rendered, "scope-only memory with unrelated vocabulary") {
t.Fatalf("scope-exact notebook fact did not reach head: %q", rendered)
}
}
func TestHeadLearningQuestionCarriesSeededNotebookFact(t *testing.T) {
graph := openHeadStore(t)
fact, err := graph.RecordFact(store.RootID, "repo:codeaf", store.FactLesson,
"card receipts stay anchored to the job that produced them")
if err != nil {
t.Fatal(err)
}
client := &fakeClient{responses: []string{
"I learned that card receipts stay with their originating job.",
}}
user, err := graph.PostMessage(store.Message{
SessionID: "learning-question", Role: store.RoleUser,
Body: "what have you learned about this repo?",
})
if err != nil {
t.Fatal(err)
}
if err := New(client, graph).answer(context.Background(), user); err != nil {
t.Fatal(err)
}
if len(client.seen) != 2 {
t.Fatalf("head messages = %+v", client.seen)
}
prompt := client.seen[1].Content[0].Text
if !strings.Contains(prompt, fmt.Sprintf("#%d", fact.Seq)) || !strings.Contains(prompt, fact.Body) {
t.Fatalf("learning question omitted notebook grounding:\n%s", prompt)
}
}
// THE HEAD LOST ITS RETRACTION DOOR IN THIS WAVE. The router's `retract` field
// was the only caller of the quarantine seam, and the belt has no tool that
// reaches it — so "that's wrong, forget that" can be replied to warmly and
// change nothing, which is the exact accumulation failure the note tool's
// supersession exists to prevent from the other side.
//
// The seam itself is intact and is what this pins: quarantine still retires a
// numbered belief, still keeps the user's own message as the evidence for it,
// still hides it from head retrieval, and still survives a rebuild of the
// journal. Everything a door would need is here; the door is what is missing,
// and it is recorded as a gap rather than papered over with a passing test.
func TestHeadRetractsNumberedNotebookBelief(t *testing.T) {
graphStore := openHeadStore(t)
fact, err := graphStore.RecordFact(store.RootID, "tool:git", store.FactQuirk,
"git always destroys worktrees")
if err != nil {
t.Fatal(err)
}
user, err := graphStore.PostMessage(store.Message{
SessionID: "chat-retract", Role: store.RoleUser, Body: "that's wrong — forget that",
})
if err != nil {
t.Fatal(err)
}
// The door back. The router's `retract` field was the only caller of this
// seam and it died with the router; the forget tool is what reaches it now,
// and this test is what says the behaviour on the far side of it is
// unchanged — the belief is quarantined rather than deleted, the evidence is
// the message that retired it, and the retirement replays.
run := &beltRun{head: New(nil, graphStore), user: user}
message, failed := run.execute(beltToolForget, beltArguments(t,
map[string]any{"belief": fact.Seq}))
if failed {
t.Fatalf("the belt could not let go of a belief the person retracted: %s", message)
}
if run.commandSeq != 0 {
t.Fatalf("letting go of a belief journaled a graph command: %d", run.commandSeq)
}
quarantined, found, err := graphStore.FactBySeq(fact.Seq)
if err != nil || !found || quarantined.Status != store.FactQuarantined ||
quarantined.EvidenceSeq != user.Seq || quarantined.StatusOrigin != store.FactOriginUser {
t.Fatalf("retracted fact = %+v found=%t err=%v", quarantined, found, err)
}
if rendered := renderNotebook(graphStore, "git worktrees", "", notebookContextBytes); strings.Contains(rendered, fact.Body) {
t.Fatalf("quarantined fact reached head retrieval: %q", rendered)
}
if err := graphStore.Rebuild(); err != nil {
t.Fatal(err)
}
rebuilt, found, err := graphStore.FactBySeq(fact.Seq)
if err != nil || !found || rebuilt.Status != store.FactQuarantined || rebuilt.EvidenceSeq != user.Seq {
t.Fatalf("rebuilt retraction = %+v found=%t err=%v", rebuilt, found, err)
}
}
func TestCompilerParsesAssumptions(t *testing.T) {
client := &fakeClient{responses: []string{strings.Join([]string{
"Here is the brief:",
"```json",
`{"goal":"Build a useful prototype with reproducible evidence.","deliverable":"prototype plus test report","budget":"$0.40","assumptions":["Use the current branch","Target a technical reader"]}`,
"```",
}, "\n")}}
brief, err := NewCompiler(client).Compile(context.Background(), "build it", "root is running")
if err != nil {
t.Fatalf("compile: %v", err)
}
if len(brief.Assumptions) != 2 || brief.Assumptions[0] != "Use the current branch" {
t.Fatalf("assumptions wrong: %+v", brief.Assumptions)
}
}
// TestCompileSurvivesAMissingDeliverableAndBudget pins the removal of two
// schema-required, validation-enforced fields that nothing read. An empty one
// used to return "compile request: empty budget" with no retry, so a perfectly
// good request was rejected for a value no consumer would ever have looked at.
// The fields are gone; a provider that still emits them is simply ignored.
func TestCompileSurvivesAMissingDeliverableAndBudget(t *testing.T) {
client := &fakeClient{responses: []string{
`{"goal":"Close the quarter's books.","assumptions":["Use the Q3 ledger"]}`,
}}
brief, err := NewCompiler(client).Compile(context.Background(), "close the books", "root is running")
if err != nil {
t.Fatalf("a brief with no deliverable and no budget was rejected: %v", err)
}
if !strings.HasPrefix(brief.Goal, "Close the quarter's books.") {
t.Fatalf("goal wrong: %q", brief.Goal)
}
for _, gone := range []string{`"deliverable"`, `"budget"`} {
if strings.Contains(compilerSystemPrompt, gone) {
t.Errorf("the compiler still asks for %s, a field nothing reads", gone)
}
}
}
func TestCompilerPreservesVerbatimInstruction(t *testing.T) {
instruction := "Make me X — keep the API name.\nDo not rename `Widget`."
client := &fakeClient{responses: []string{
`{"goal":"Produce and verify the requested change.","deliverable":"a tested implementation","budget":"$0.40","assumptions":["Use repository conventions"]}`,
}}
brief, err := NewCompiler(client).Compile(context.Background(), instruction, "root is running")
if err != nil {
t.Fatalf("compile: %v", err)
}
wantSuffix := "Verbatim request:\n" + instruction
if !strings.HasSuffix(brief.Goal, wantSuffix) {
t.Fatalf("goal lost verbatim instruction:\n%s", brief.Goal)
}
if strings.Count(brief.Goal, instruction) != 1 {
t.Fatalf("instruction was not anchored exactly once:\n%s", brief.Goal)
}
}
func TestCompilerUsesOnlyExplicitUnsettledFlagForTrial(t *testing.T) {
client := &fakeClient{responses: []string{
`{"goal":"Compare both approaches cheaply, then use the observed winner.","deliverable":"tested implementation","budget":"$0.40","assumptions":[],"trial_of":0}`,
`{"goal":"Use the requested approach.","deliverable":"tested implementation","budget":"$0.40","assumptions":[],"trial_of":999}`,
}}
compiler := NewCompiler(client)
flagged := "notebook:\n- " + store.UnsettledFactFlag + "42\n approach A vs approach B"
brief, err := compiler.Compile(context.Background(), "build it", flagged)
if err != nil {
t.Fatal(err)
}
if brief.TrialOf != 42 {
t.Fatalf("flagged trial_of = %d, want 42", brief.TrialOf)
}
plain, err := compiler.Compile(context.Background(), "build it again", "ordinary prose says two options are unsettled")
if err != nil {
t.Fatal(err)
}
if plain.TrialOf != 0 {
t.Fatalf("unflagged trial_of = %d, want 0", plain.TrialOf)
}
}
func TestHeadRestartSkipsAnsweredHistory(t *testing.T) {
graphStore := openHeadStore(t)
if _, err := graphStore.PostMessage(store.Message{SessionID: "chat-4", Role: store.RoleUser, Body: "old question"}); err != nil {
t.Fatalf("post old user message: %v", err)
}
if _, err := graphStore.PostMessage(store.Message{SessionID: "chat-4", Role: store.RoleAgent, Body: "old answer"}); err != nil {
t.Fatalf("post old agent message: %v", err)
}
client := &fakeClient{responses: []string{"new answer"}}
stop := startServing(t, New(client, graphStore))
defer stop()
user, err := graphStore.PostMessage(store.Message{SessionID: "chat-4", Role: store.RoleUser, Body: "new question"})
if err != nil {
t.Fatalf("post new user message: %v", err)
}
reply := waitForAgentReply(t, graphStore, "chat-4", user.Seq)
if reply.Body != "new answer" {
t.Fatalf("new reply = %q", reply.Body)
}
messages, err := graphStore.Messages("chat-4", 0, 0)
if err != nil {
t.Fatalf("list messages: %v", err)
}
if len(messages) != 4 {
t.Fatalf("answered history was replayed: %+v", messages)
}
if calls := client.callCount(); calls != 1 {
t.Fatalf("provider calls = %d, want only the new message", calls)
}
}
func openHeadStore(t *testing.T) *store.Store {
t.Helper()
graphStore, err := store.Open(filepath.Join(t.TempDir(), "graph.db"))
if err != nil {
t.Fatalf("open store: %v", err)
}
t.Cleanup(func() { _ = graphStore.Close() })
return graphStore
}
func startServing(t *testing.T, conversationalHead *Head) func() {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
go func() { done <- conversationalHead.Serve(ctx) }()
var once sync.Once
return func() {
once.Do(func() {
cancel()
select {
case err := <-done:
if !errors.Is(err, context.Canceled) {
t.Errorf("serve returned %v, want context cancellation", err)
}
case <-time.After(2 * time.Second):
t.Error("serve did not stop after cancellation")
}
})
}
}
func waitForAgentReply(t *testing.T, graphStore *store.Store, sessionID string, afterSeq int64) store.Message {
t.Helper()
deadline := time.Now().Add(3 * time.Second)
for time.Now().Before(deadline) {
messages, err := graphStore.Messages(sessionID, afterSeq, 0)
if err != nil {
t.Fatalf("list replies: %v", err)
}
for _, message := range messages {
if message.Role == store.RoleAgent {
return message
}
}
time.Sleep(10 * time.Millisecond)
}
t.Fatalf("timed out waiting for agent reply in %s after %d", sessionID, afterSeq)
return store.Message{}
}
func textResponse(text string) *ai.Response {
return &ai.Response{Choices: []ai.Choice{{
Message: ai.Message{Role: "assistant", Content: []ai.ContentPart{{Type: "text", Text: text}}},
FinishReason: "stop",
}}}
}
// A reflex is the model's judgment, passed as an argument to spawn rather than
// encoded in a command kind on a terminal routing decision. What has to survive
// that move is the pair of properties the terminal position guaranteed: the flag
// reaches the journaled row, and the words that travel are the person's own.
func TestHeadParsesReflexAndAnchorsVerbatimIntent(t *testing.T) {
graphStore := openHeadStore(t)
const ask = "Read VERSION and tell me the value."
client := &beltClient{turns: []beltTurn{
{calls: []ai.ToolCall{beltCall("s1", beltToolTask, map[string]any{
"instruction": ask, "reflex": true})}},
{text: "Doing that now."},
}}
user, err := graphStore.PostMessage(store.Message{
SessionID: "chat-reflex", Role: store.RoleUser, Body: ask,
})
if err != nil {
t.Fatal(err)
}
if err := New(client, graphStore).answer(context.Background(), user); err != nil {
t.Fatal(err)
}
commands, err := graphStore.PendingCommands(0)
if err != nil {
t.Fatal(err)
}
if len(commands) != 1 {
t.Fatalf("pending reflex commands = %+v", commands)
}
command := commands[0]
if command.Kind != store.CommandSplice || !command.Reflex || command.Instruction != user.Body {
t.Fatalf("parsed reflex command = %+v, want verbatim %q", command, user.Body)
}
// The tool the model reads is what tells it the flag is narrow. A permission
// with no stated bound is a permission with no bound.
spawnDescription := ""
for _, definition := range beltDefinitions() {
if definition.Function.Name == beltToolTask {
spawnDescription = definition.Function.Description
}
}
if !strings.Contains(spawnDescription, "do not improve or summarize them") {
t.Errorf("the verbatim rule is no longer stated where the model reads it: %q", spawnDescription)
}
}
// The consequence gate, moved with the judgment it guards.
//
// It used to run at routing-decision validation, before persistence, because
// that was the last place a reflex could still be caught. The judgment is an
// argument to a tool now, so the gate moved to the journaling door — the last
// place it can still be true, rather than trusted from wherever the flag was
// set. Same three asks, same verdict: the reflex is dropped, the work is
// ordinary work the person sees coming, and the words are untouched.
func TestHeadPromotesConsequentialReflexBeforePersistence(t *testing.T) {
for _, ask := range []string{
"Pay the vendor five dollars.",
"Delete /etc/obsolete.conf.",
"Publish the draft release.",
} {
t.Run(ask, func(t *testing.T) {
graphStore := openHeadStore(t)
if !consequenceGated(ask) {
t.Fatalf("%q is no longer read as consequential", ask)
}
user := postUser(t, graphStore, "chat-consequence", ask)
run := &beltRun{head: New(nil, graphStore), user: user}
result, failed := run.execute(beltToolTask, beltArguments(t, map[string]any{
"instruction": ask, "reflex": true}))
if failed {
t.Fatalf("spawn refused %q outright: %s", ask, result)
}
commands := pendingCommandsOf(t, graphStore)
if len(commands) != 1 {
t.Fatalf("%q journaled %d commands: %+v", ask, len(commands), commands)
}
command := commands[0]
if command.Kind != store.CommandSplice || strings.TrimSpace(command.Target) != "" {
t.Fatalf("command for %q = %s at %q, want an ordinary splice", ask, command.Kind, command.Target)
}
if command.Reflex {
t.Fatalf("consequential ask %q remained a reflex", ask)
}
if command.Instruction != ask {
t.Fatalf("instruction = %q, want %q", command.Instruction, ask)
}
// And the receipt does not claim the fast lane it was denied.
if strings.Contains(result, "immediate action") {
t.Fatalf("the receipt promised a reflex that was refused: %q", result)
}
})
}
}
func TestHeadReceivesMeasuredReflexPrior(t *testing.T) {
graphStore := openHeadStore(t)
client := &fakeClient{responses: []string{"Nothing is running."}}
user, err := graphStore.PostMessage(store.Message{
SessionID: "chat-prior", Role: store.RoleUser, Body: "what is running?",
})
if err != nil {
t.Fatal(err)
}
if err := New(client, graphStore).
WithSelfKnowledge(func() string {
return "reflex: median 200 tokens, 2 turns; n=10; success=90.0%; promoted=10.0%; avg cost=$0.0010"
}).
answer(context.Background(), user); err != nil {
t.Fatal(err)
}
if len(client.seen) != 2 ||
!strings.Contains(client.seen[1].Content[0].Text, "Measured execution history") ||
!strings.Contains(client.seen[1].Content[0].Text, "promoted=10.0%") {
t.Fatalf("measured reflex prior did not reach head: %+v", client.seen)
}
}
// The head does not carry the competence map in its prompt at all — it is a belt
// read — and what it must not do is invent one in its absence. The enforcement
// used to be split between a router prompt that promised the block and a belt
// that held the tool; it is one prompt and one tool now, and both halves are
// pinned: the prompt never promises the block and forbids saying anything a tool
// did not show, and the read that WOULD ground the answer exists and says so.
func TestRouterNeitherCarriesNorInventsAMeasuredSelfAssessment(t *testing.T) {
graphStore := openHeadStore(t)
client := &fakeClient{responses: []string{
"I'm strongest at Go parser work, with eight clean runs.",
}}
user := postUser(t, graphStore, "competence", "what are you good at now?")
called := false
if err := New(client, graphStore).
WithCompetenceMap(func() string { called = true; return "unexpected" }).
answer(context.Background(), user); err != nil {
t.Fatal(err)
}
if called {
t.Fatal("the head still pulls the competence map behind a phrase gate")
}
if strings.Contains(client.seen[1].Content[0].Text, "Competence map (ground truth") {
t.Fatalf("the competence block is still injected: %s", client.seen[1].Content[0].Text)
}
if strings.Contains(orchestratorPrompt, "When a competence map appears") ||
strings.Contains(orchestratorPrompt, "When standing-watch status appears") {
t.Error("the head's prompt still promises blocks it is never handed")
}
if !strings.Contains(orchestratorPrompt, "Ground every claim in something a tool showed you this turn") {
t.Error("the prompt lost the rule against stating what nothing showed it")
}
// The other half: a self-assessment is not forbidden, it is grounded. The
// read that grounds it has to exist and has to say that it is the only
// evidence there is, or the rule above is a rule against answering at all.
competence := ""
for _, definition := range beltDefinitions() {
if definition.Function.Name == beltToolCompetence {
competence = definition.Function.Description
}
}
if competence == "" {
t.Fatal("there is no competence read, so the honest answer is unreachable")
}
if !strings.Contains(competence, "a self-assessment given without it is invention") {
t.Errorf("the competence read no longer names the failure it prevents: %q", competence)
}
}
func TestHeadVoicePromptPreservesEmptyBytesAndRendersPreference(t *testing.T) {
// The empty-notebook prompt is the stable prompt PLUS the register, exactly
// — the register is unconditional now, and the byte diff from the bare
// prompt is that one segment and nothing else.
t.Run("empty notebook", func(t *testing.T) {
graphStore := openHeadStore(t)
client := &fakeClient{responses: []string{"Ready."}}
user := postUser(t, graphStore, "voice-empty", "answer this plainly")
if err := New(client, graphStore).answer(context.Background(), user); err != nil {
t.Fatal(err)
}
want := orchestratorPrompt + "\n\n" + resident.VoiceRegister
if len(client.seen) != 2 || client.seen[0].Content[0].Text != want {
t.Fatalf("empty-notebook head system prompt changed: %+v", client.seen)
}
})
t.Run("standing preference", func(t *testing.T) {
graphStore := openHeadStore(t)
const preference = "keep answers short; no preamble"
if _, err := graphStore.RecordFact("", "user", store.FactPreference, preference); err != nil {
t.Fatal(err)
}
client := &fakeClient{responses: []string{"Ready."}}
user := postUser(t, graphStore, "voice-learned", "answer this plainly")
if err := New(client, graphStore).answer(context.Background(), user); err != nil {
t.Fatal(err)
}
if len(client.seen) != 2 || !strings.Contains(client.seen[0].Content[0].Text, preference) {
t.Fatalf("head system prompt omitted voice preference: %+v", client.seen)
}
})
}
// A durable preference lands through the note tool now rather than through a
// `remember` field on a routing decision. The capture is the same fact machinery
// and the same notebook; what changed is that writing it is an ACT the reply may
// only claim because a call returned, instead of a field that could be set
// beside a sentence that never mentioned it.
func TestHeadRememberPathCapturesStatedVoicePreference(t *testing.T) {
graphStore := openHeadStore(t)
const preference = "keep answers short; no preamble"
client := &beltClient{turns: []beltTurn{
{calls: []ai.ToolCall{beltCall("n1", beltToolNote, map[string]any{
"scope": "user", "kind": "preference", "body": preference})}},
{text: "Got it."},
}}
user, err := graphStore.PostMessage(store.Message{
SessionID: "voice-remember", Role: store.RoleUser,
Body: "From now on, keep answers short and skip the preamble.",
})
if err != nil {
t.Fatal(err)
}
if err := New(client, graphStore).answer(context.Background(), user); err != nil {
t.Fatal(err)
}
facts, err := graphStore.ActiveFacts("user", 10)
if err != nil {
t.Fatal(err)
}
if len(facts) != 1 || facts[0].Kind != store.FactPreference || facts[0].Body != preference {
t.Fatalf("remembered voice preference = %+v", facts)
}
// And it comes back on the next message, which is the whole point of durable.
if rendered := renderNotebook(graphStore, "how should you answer me", "", notebookContextBytes); !strings.Contains(rendered, preference) {
t.Fatalf("the notebook does not read the preference back:\n%s", rendered)
}
}
func TestHeadAffirmativeRaisesRailAndRunnerResumes(t *testing.T) {
graph := openHeadStore(t)
if err := graph.Splice(store.RootID, store.Subtree{Nodes: []store.NodeSpec{{
ID: "paused-work", Brief: "finish after approval", Stage: 1,
}}}, store.Provenance{Origin: store.OriginUser, SessionID: "rail-head", Intent: "finish it"}); err != nil {
t.Fatal(err)
}
if err := graph.RecordUsage(store.NodeUsage{NodeID: store.RootID, Cost: 1}); err != nil {
t.Fatal(err)
}
runs := 0
runner := resident.NewRunner(graph, func(context.Context, store.Node) (resident.ExecResult, error) {
runs++
return resident.ExecResult{Summary: "finished after approval"}, nil
}, "head-rail-runner", 1).WithDailyBudgetUSD(1)
if dispatched, err := runner.Tick(context.Background()); err != nil || dispatched != 0 {
t.Fatalf("paused tick dispatched=%d err=%v", dispatched, err)
}
user, err := graph.PostMessage(store.Message{SessionID: "rail-head", Role: store.RoleUser, Body: "yes"})
if err != nil {
t.Fatal(err)
}
client := &fakeClient{}
if err := New(client, graph).WithDailyBudgetUSD(1).answer(context.Background(), user); err != nil {
t.Fatal(err)
}
if client.calls != 0 {
t.Fatalf("affirmative rail reply used %d model calls, want zero", client.calls)
}
events, err := graph.Events(0, 0)
if err != nil {
t.Fatal(err)
}
raises := 0
for _, event := range events {
if event.Kind == store.EventRailRaised {
raises++
}
}
if raises != 1 {
t.Fatalf("rail raise events = %d, want one", raises)
}
if dispatched, err := runner.Tick(context.Background()); err != nil || dispatched != 1 {
t.Fatalf("resumed tick dispatched=%d err=%v", dispatched, err)
}
runner.Wait()
if runs != 1 {
t.Fatalf("resumed executor runs = %d, want one", runs)
}
node, ok, err := graph.Node("paused-work")
if err != nil || !ok || node.Status != store.Done {
t.Fatalf("resumed node = %+v ok=%t err=%v", node, ok, err)
}
messages, err := graph.Messages("rail-head", user.Seq, 0)
if err != nil {
t.Fatal(err)
}
if len(messages) != 1 || !strings.Contains(messages[0].Body, "continuing") {
t.Fatalf("rail acknowledgement = %+v", messages)
}
}
func TestHeadSnapshotIncludesDailySpendAndCeiling(t *testing.T) {
graph := openHeadStore(t)
if err := graph.RecordUsage(store.NodeUsage{NodeID: store.RootID, Cost: 2.5}); err != nil {
t.Fatal(err)
}
client := &fakeClient{responses: []string{"Nothing is running."}}
user := postUser(t, graph, "rail-status", "what is running?")
if err := New(client, graph).WithDailyBudgetUSD(20).answer(context.Background(), user); err != nil {
t.Fatal(err)
}
if len(client.seen) != 2 || !strings.Contains(client.seen[1].Content[0].Text,
"today's spend: $2.50 of $20.00 daily rail") {
t.Fatalf("head snapshot omitted daily rail: %+v", client.seen)
}
}
func TestAffirmativeRailReplyVocabulary(t *testing.T) {
for _, reply := range []string{"y", "Yes.", "continue", "go ahead", "proceed", "okay"} {
if !affirmativeRailReply(reply) {
t.Errorf("%q was not recognized as affirmative", reply)
}
}
for _, reply := range []string{"no", "not yet", "what will it cost?"} {
if affirmativeRailReply(reply) {
t.Errorf("%q was recognized as affirmative", reply)
}
}
}
// The temporal route that drafted a standing rule out of a recurring-sounding
// ask went with the v1 scheduler. Such an ask is compiled like any other — by
// the one compile call, into ordinary work that carries its sentence verbatim —
// and nothing is held back for a ratification card that no longer exists.
func TestARecurringSoundingAskCompilesAsOrdinaryWork(t *testing.T) {
const ask = "remind me every monday at 9 to water the plants"
client := &fakeClient{responses: []string{`{"goal":"Water the plants."}`}}
brief, err := NewCompiler(client).Compile(context.Background(), ask, "")
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(brief.Question) != "" || len(brief.QuestionOptions) != 0 {
t.Fatalf("a recurring-sounding ask was asked about: %+v", brief)
}
if !strings.Contains(brief.Goal, ask) {
t.Fatalf("goal = %q, want the ask carried verbatim", brief.Goal)
}
if client.calls != 1 {
t.Fatalf("the ask took %d model calls, want the one compile", client.calls)
}
}
func TestCompilerEpisodicOutputByteIdentity(t *testing.T) {
client := &fakeClient{responses: []string{
`{"goal":"Produce the requested summary.","deliverable":"summary","budget":"$0.10","assumptions":["Use the current file"]}`,
}}
brief, err := NewCompiler(client).Compile(context.Background(), "Summarize this file.", "root is running")
if err != nil {
t.Fatal(err)
}
encoded, err := json.Marshal(brief)
if err != nil {
t.Fatal(err)
}
const want = `{"goal":"Produce the requested summary.\n\nVerbatim request:\nSummarize this file.","assumptions":["Use the current file"],"scale":"task","builds_on":[],"question":"","trial_of":0}`
if string(encoded) != want {
t.Fatalf("episodic compile bytes changed:\n got %s\nwant %s", encoded, want)
}
// The name the compile pass now returns rides the same object and nothing
// else moves. A compiler that says nothing about the name adds no field at
// all, which is what keeps the shape above byte-identical.
named := &fakeClient{responses: []string{
`{"goal":"Produce the requested summary.","title":"File summary","assumptions":["Use the current file"]}`,
}}
titled, err := NewCompiler(named).Compile(context.Background(), "Summarize this file.", "root is running")
if err != nil {
t.Fatal(err)
}
encoded, err = json.Marshal(titled)
if err != nil {
t.Fatal(err)
}
const wantTitled = `{"goal":"Produce the requested summary.\n\nVerbatim request:\nSummarize this file.","assumptions":["Use the current file"],"title":"File summary","scale":"task","builds_on":[],"question":"","trial_of":0}`
if string(encoded) != wantTitled {
t.Fatalf("named compile bytes changed:\n got %s\nwant %s", encoded, wantTitled)
}
}
func TestCompilerQuestionOptionsPreserveOrder(t *testing.T) {
client := &fakeClient{responses: []string{
`{"question":"Which region?","question_options":[{"label":"Toronto","value":"ca"},{"label":"London","value":"uk"}]}`,
}}
brief, err := NewCompiler(client).Compile(context.Background(), "Publish the regional report.", "")
if err != nil {
t.Fatal(err)
}
if len(brief.QuestionOptions) != 2 || brief.QuestionOptions[0].Label != "Toronto" ||
brief.QuestionOptions[1].Value != "uk" {
t.Fatalf("question options = %+v", brief.QuestionOptions)
}
}
func TestGenericQuestionNumericSelectionContinuesCompile(t *testing.T) {
graph := openHeadStore(t)
compile := func(context.Context, string, string) (resident.Compiled, error) {
return resident.Compiled{
Question: "Which region?",
QuestionOptions: []store.QuestionOption{
{Label: "Toronto", Value: "ca"}, {Label: "London", Value: "uk"},
},
}, nil
}
reconciler := resident.New(graph, compile, nil)
original, err := graph.RequestCommand(store.Command{
SessionID: "generic-options", Kind: store.CommandSplice, Instruction: "Publish the regional report.",
})
if err != nil {
t.Fatal(err)
}
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
question := waitForAgentReply(t, graph, "generic-options", original.Seq)
if len(question.Options) != 2 {
t.Fatalf("question options = %+v", question.Options)
}
user, err := graph.PostMessage(store.Message{
SessionID: "generic-options", Role: store.RoleUser, Body: "2",
})
if err != nil {
t.Fatal(err)
}
if err := New(&fakeClient{}, graph).answer(context.Background(), user); err != nil {
t.Fatal(err)
}
pending, err := graph.PendingCommands(0)
if err != nil || len(pending) != 1 ||
!strings.Contains(pending[0].Instruction, "Answer to compiler question: London") {
t.Fatalf("continued command = %+v err=%v", pending, err)
}
}
// A document is not something the talk model can see. It must stay off the
// wire as a content part and still reach the command, because the whole point
// is that a worker reads it from the workspace with read_document.
func TestHeadKeepsDocumentAttachmentOffTheModelAndOnTheCommand(t *testing.T) {
graph := openHeadStore(t)
path := filepath.Join(t.TempDir(), "filing.pdf")