Repository navigation
Expand file tree
/
Copy pathlearning_test.go
More file actions
369 lines (354 loc) · 13 KB
/
Copy pathlearning_test.go
File metadata and controls
369 lines (354 loc) · 13 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
package resident
import (
"context"
"path/filepath"
"strings"
"testing"
"time"
"github.com/Agent-Field/codeaf/internal/store"
)
func TestLearningMomentsAnchorOneFactAndBatchPerJob(t *testing.T) {
tests := []struct {
name string
learned []Learned
want string
}{
{
name: "one fact",
learned: []Learned{{Scope: "repo:codeaf", Kind: store.FactLesson,
Body: "card receipts belong to their originating job\nwith supporting detail"}},
want: "· learned — card receipts belong to their originating job",
},
{
name: "batched",
learned: []Learned{
{Scope: "repo:codeaf", Kind: store.FactPlain, Body: "first durable thing"},
{Scope: "repo:codeaf", Kind: store.FactLesson, Body: "second durable thing"},
{Scope: "repo:codeaf", Kind: store.FactPlaybook, Body: "third durable thing"},
},
want: "· learned 3 things ▸\n · learned — first durable thing\n · learned — second durable thing\n · learned — third durable thing",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
graph := openStore(t)
settle, reconciler := learningJobFixture(t, graph, "learning", func(_ context.Context, _, _ string, _ bool) ([]Learned, error) {
return test.learned, nil
})
settle()
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
moments := learningMessages(t, graph, "learning")
if len(moments) != 1 || moments[0].NodeID != "learning-job" || moments[0].Body != test.want {
t.Fatalf("learning moments = %+v, want one anchored %q", moments, test.want)
}
})
}
}
func TestLearningMomentShowsSettledExperiment(t *testing.T) {
graph := openStore(t)
if err := graph.Splice(store.RootID, store.Subtree{Nodes: []store.NodeSpec{
{ID: "evidence-a", Brief: "evidence A", Stage: 1},
{ID: "evidence-b", Parent: "evidence-a", Brief: "evidence B", Stage: 1},
}}, store.Provenance{Origin: store.OriginUser, SessionID: "trial", Intent: "seed evidence"}); err != nil {
t.Fatal(err)
}
first, err := graph.RecordFact("evidence-a", "repo:codeaf", store.FactLesson, "buffered reads worked")
if err != nil {
t.Fatal(err)
}
second, err := graph.RecordFact("evidence-b", "repo:codeaf", store.FactLesson, "direct reads worked")
if err != nil {
t.Fatal(err)
}
pair, err := graph.RecordUnsettledFact("evidence-a", "repo:codeaf", store.UnsettledPair{Approaches: []store.UnsettledApproach{
{Approach: "buffered reads", Scope: "repo:codeaf", Evidence: []int64{first.Seq}},
{Approach: "direct reads", Scope: "repo:codeaf", Evidence: []int64{second.Seq}},
}})
if err != nil {
t.Fatal(err)
}
if err := graph.Splice(store.RootID, store.Subtree{Nodes: []store.NodeSpec{{
ID: "trial-job", Brief: "compare buffered and direct reads", Stage: 1,
}}}, store.Provenance{Origin: store.OriginUser, SessionID: "trial", Intent: "settle the read strategy", TrialOf: pair.Seq}); err != nil {
t.Fatal(err)
}
reconciler := New(graph, nil, nil).WithDistiller(func(_ context.Context, _, _ string, _ bool) ([]Learned, error) {
return []Learned{{Scope: "repo:codeaf", Kind: store.FactPlaybook,
Body: "buffered reads win for this parser", Replaces: pair.Seq}}, nil
})
if err := reconciler.SessionOpened(context.Background(), "trial", "tui", time.Hour); err != nil {
t.Fatal(err)
}
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
claim, won, err := graph.Claim("trial-job", "worker")
if err != nil || !won {
t.Fatalf("claim won=%t err=%v", won, err)
}
if err := graph.Complete(claim, "comparison complete"); err != nil {
t.Fatal(err)
}
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
moments := learningMessages(t, graph, "trial")
if len(moments) != 1 || moments[0].Body != "⚖ settled: buffered reads win for this parser · 1 trial" {
t.Fatalf("trial moments = %+v", moments)
}
}
func TestSkillPromotionPostsForgedMomentAndReflectionDigest(t *testing.T) {
t.Setenv("HOME", t.TempDir())
graph := openStore(t)
reconciler := New(graph, nil, nil)
if err := reconciler.SessionOpened(context.Background(), "skills", "tui", time.Hour); err != nil {
t.Fatal(err)
}
for index, id := range []string{"skill-job-one", "skill-job-two"} {
artifact := writeSkillArtifact(t, "repo-audit", "#!/bin/sh\nexit 0\n")
if err := graph.Splice(store.RootID, store.Subtree{Nodes: []store.NodeSpec{{
ID: id, Brief: "teach a reusable audit", Stage: 1,
}}}, store.Provenance{Origin: store.OriginUser, SessionID: "skills", Intent: "audit this repository"}); err != nil {
t.Fatal(err)
}
claim, won, err := graph.Claim(id, "worker")
if err != nil || !won {
t.Fatalf("claim %s won=%t err=%v", id, won, err)
}
if err := graph.Complete(claim, "audit complete"); err != nil {
t.Fatal(err)
}
if _, err := graph.RecordSkillCandidate(id, "repo:audit", testSkillDoc, artifact); err != nil {
t.Fatal(err)
}
if index == 0 {
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
}
}
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
messages, err := graph.Messages("skills", 0, 0)
if err != nil {
t.Fatal(err)
}
var forged, reflected *store.Message
for index := range messages {
message := &messages[index]
if strings.HasPrefix(message.Body, "⚒ forged:") {
forged = message
}
if strings.HasPrefix(message.Body, "· reflected —") {
reflected = message
}
}
if forged == nil || forged.NodeID != "skill-job-two" ||
forged.Body != "⚒ forged: repo-audit — proved twice, now available" {
t.Fatalf("forged moment = %+v", forged)
}
if reflected == nil || !strings.Contains(reflected.Body, "1 skill promoted") {
t.Fatalf("reflection digest = %+v", reflected)
}
active, err := graph.SkillFacts(store.FactActive, 10)
if err != nil || len(active) != 1 || filepath.Base(active[0].Artifact) != "repo-audit" {
t.Fatalf("active skills = %+v err=%v", active, err)
}
}
func TestLearningVisibilitySuppressesDetachedAndEmptyPaths(t *testing.T) {
t.Run("detached defers to brief", func(t *testing.T) {
graph := openStore(t)
settle, reconciler := learningJobFixture(t, graph, "detached", func(_ context.Context, _, _ string, _ bool) ([]Learned, error) {
return []Learned{{Scope: "repo:codeaf", Kind: store.FactLesson, Body: "learned while away"}}, nil
})
if err := reconciler.SessionClosed("detached", "tui"); err != nil {
t.Fatal(err)
}
settle()
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
if moments := learningMessages(t, graph, "detached"); len(moments) != 0 {
t.Fatalf("detached learning moments = %+v", moments)
}
})
t.Run("another attached thread preserves the originating anchor", func(t *testing.T) {
graph := openStore(t)
settle, reconciler := learningJobFixture(t, graph, "originating", func(_ context.Context, _, _ string, _ bool) ([]Learned, error) {
return []Learned{{Scope: "repo:codeaf", Kind: store.FactLesson, Body: "learning stays with its job"}}, nil
})
if _, err := graph.TouchSeen("tui", "new-thread", store.SeenAttached); err != nil {
t.Fatal(err)
}
settle()
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
moments := learningMessages(t, graph, "originating")
if len(moments) != 1 || moments[0].NodeID != "learning-job" {
t.Fatalf("originating learning moments = %+v", moments)
}
})
t.Run("no learning adds no message bytes", func(t *testing.T) {
graph := openStore(t)
settle, reconciler := learningJobFixture(t, graph, "empty-learning", func(_ context.Context, _, _ string, _ bool) ([]Learned, error) {
return nil, nil
})
settle()
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
messages, err := graph.Messages("empty-learning", 0, 0)
if err != nil {
t.Fatal(err)
}
if len(messages) != 1 || messages[0].Body != "job complete" {
t.Fatalf("no-learning path wrote extra bytes: %+v", messages)
}
})
}
func TestRetrospectiveDigestNonzeroOnlyAndSilentWhenEmpty(t *testing.T) {
t.Run("nonzero categories", func(t *testing.T) {
graph := openStore(t)
for index := 1; index <= reflectionMinJobs; index++ {
settleRetrospectiveJob(t, graph, index)
}
reconciler := New(graph, nil, nil).WithReflector(func(_ context.Context, _ []JobSketch) ([]Learned, error) {
return []Learned{{Scope: "repo:codeaf", Kind: store.FactLesson, Body: "the series proved one stable lesson"}}, nil
})
if err := reconciler.SessionOpened(context.Background(), "reflection", "tui", time.Hour); err != nil {
t.Fatal(err)
}
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
messages, err := graph.Messages("reflection", 0, 0)
if err != nil {
t.Fatal(err)
}
var digest string
for _, message := range messages {
if strings.HasPrefix(message.Body, "· reflected —") {
digest = message.Body
}
}
if digest == "" || !strings.Contains(digest, "1 belief learned") || strings.Contains(digest, "proposal") ||
strings.Contains(digest, "territory") || strings.Contains(digest, "skill") {
t.Fatalf("retrospective digest = %q", digest)
}
})
t.Run("empty reflection", func(t *testing.T) {
graph := openStore(t)
for index := 1; index <= reflectionMinJobs; index++ {
settleRetrospectiveJob(t, graph, index)
}
reconciler := New(graph, nil, nil).WithReflector(func(_ context.Context, _ []JobSketch) ([]Learned, error) {
return nil, nil
})
if err := reconciler.SessionOpened(context.Background(), "quiet-reflection", "tui", time.Hour); err != nil {
t.Fatal(err)
}
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
messages, err := graph.Messages("quiet-reflection", 0, 0)
if err != nil {
t.Fatal(err)
}
for _, message := range messages {
if strings.HasPrefix(message.Body, "· reflected —") {
t.Fatalf("empty retrospective posted %q", message.Body)
}
}
})
}
func TestRetractionMomentContent(t *testing.T) {
if got := letGoMoment("obsolete belief\nold detail").headline; got != "· let go — obsolete belief" {
t.Fatalf("let-go moment = %q", got)
}
}
func learningJobFixture(t *testing.T, graph *store.Store, sessionID string, distill DistillFunc) (func(), *Reconciler) {
t.Helper()
if err := graph.Splice(store.RootID, store.Subtree{Nodes: []store.NodeSpec{{
ID: "learning-job", Brief: "finish a learning job", Stage: 1,
}}}, store.Provenance{Origin: store.OriginUser, SessionID: sessionID, Intent: "learn from this job"}); err != nil {
t.Fatal(err)
}
reconciler := New(graph, nil, nil).WithDistiller(distill)
if err := reconciler.SessionOpened(context.Background(), sessionID, "tui", time.Hour); err != nil {
t.Fatal(err)
}
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
settle := func() {
claim, won, err := graph.Claim("learning-job", "worker")
if err != nil || !won {
t.Fatalf("claim won=%t err=%v", won, err)
}
if err := graph.Complete(claim, "job complete"); err != nil {
t.Fatal(err)
}
}
return settle, reconciler
}
func learningMessages(t *testing.T, graph *store.Store, sessionID string) []store.Message {
t.Helper()
messages, err := graph.Messages(sessionID, 0, 0)
if err != nil {
t.Fatal(err)
}
var moments []store.Message
for _, message := range messages {
if isLearningMomentBody(message.Body) {
moments = append(moments, message)
}
}
return moments
}
func isLearningMomentBody(body string) bool {
return strings.HasPrefix(body, "· learned") || strings.HasPrefix(body, "⚒ forged:") ||
strings.HasPrefix(body, "⚖ settled:") || strings.HasPrefix(body, "· let go —")
}
// Overnight work learns things with nobody watching. Dropping the moment
// outright — which is what wiping the map unconditionally did — meant the whole
// `codeaf wake` path taught the notebook and told the user nothing.
func TestLearningMomentsWaitForSomebodyToReadThem(t *testing.T) {
graph := openStore(t)
settle, reconciler := learningJobFixture(t, graph, "away",
func(_ context.Context, _, _ string, _ bool) ([]Learned, error) {
return []Learned{{Scope: "repo:codeaf", Kind: store.FactLesson,
Body: "overnight work still teaches something"}}, nil
})
if err := reconciler.SessionClosed("away", "tui"); err != nil {
t.Fatal(err)
}
settle()
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
if moments := learningMessages(t, graph, "away"); len(moments) != 0 {
t.Fatalf("moment posted with no surface attached: %+v", moments)
}
// Nothing new happens; the user simply comes back.
if err := reconciler.SessionOpened(context.Background(), "away", "tui", time.Hour); err != nil {
t.Fatal(err)
}
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
moments := learningMessages(t, graph, "away")
if len(moments) != 1 || !strings.Contains(moments[0].Body, "overnight work still teaches something") {
t.Fatalf("held moment never arrived: %+v", moments)
}
// And it arrives exactly once.
if err := reconciler.Tick(context.Background()); err != nil {
t.Fatal(err)
}
if moments := learningMessages(t, graph, "away"); len(moments) != 1 {
t.Fatalf("held moment arrived twice: %+v", moments)
}
}