@dxos/brain 0.0.1 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/LICENSE +105 -0
  2. package/README.md +32 -1
  3. package/dist/lib/Builtins.mjs +178 -0
  4. package/dist/lib/Builtins.mjs.map +1 -0
  5. package/dist/lib/CompilePrompt.mjs +305 -0
  6. package/dist/lib/CompilePrompt.mjs.map +1 -0
  7. package/dist/lib/Compiler.mjs +212 -0
  8. package/dist/lib/Compiler.mjs.map +1 -0
  9. package/dist/lib/Encoding.mjs +116 -0
  10. package/dist/lib/Encoding.mjs.map +1 -0
  11. package/dist/lib/GoalRules.mjs +161 -0
  12. package/dist/lib/GoalRules.mjs.map +1 -0
  13. package/dist/lib/Vocabulary.mjs +92 -0
  14. package/dist/lib/Vocabulary.mjs.map +1 -0
  15. package/dist/lib/chunk-rolldown-runtime.mjs +13 -0
  16. package/dist/lib/index.mjs +7 -0
  17. package/dist/lib/testing.mjs +831 -0
  18. package/dist/lib/testing.mjs.map +1 -0
  19. package/dist/types/src/Builtins.d.ts +57 -0
  20. package/dist/types/src/Builtins.d.ts.map +1 -0
  21. package/dist/types/src/Builtins.test.d.ts +2 -0
  22. package/dist/types/src/Builtins.test.d.ts.map +1 -0
  23. package/dist/types/src/CompilePrompt.d.ts +107 -0
  24. package/dist/types/src/CompilePrompt.d.ts.map +1 -0
  25. package/dist/types/src/CompilePrompt.test.d.ts +2 -0
  26. package/dist/types/src/CompilePrompt.test.d.ts.map +1 -0
  27. package/dist/types/src/Compiler.d.ts +76 -0
  28. package/dist/types/src/Compiler.d.ts.map +1 -0
  29. package/dist/types/src/Compiler.test.d.ts +2 -0
  30. package/dist/types/src/Compiler.test.d.ts.map +1 -0
  31. package/dist/types/src/Encoding.d.ts +42 -0
  32. package/dist/types/src/Encoding.d.ts.map +1 -0
  33. package/dist/types/src/GoalRules.d.ts +66 -0
  34. package/dist/types/src/GoalRules.d.ts.map +1 -0
  35. package/dist/types/src/GoalRules.test.d.ts +2 -0
  36. package/dist/types/src/GoalRules.test.d.ts.map +1 -0
  37. package/dist/types/src/Vocabulary.d.ts +31 -0
  38. package/dist/types/src/Vocabulary.d.ts.map +1 -0
  39. package/dist/types/src/Vocabulary.test.d.ts +2 -0
  40. package/dist/types/src/Vocabulary.test.d.ts.map +1 -0
  41. package/dist/types/src/index.d.ts +7 -0
  42. package/dist/types/src/index.d.ts.map +1 -0
  43. package/dist/types/src/testing/index.d.ts +4 -0
  44. package/dist/types/src/testing/index.d.ts.map +1 -0
  45. package/dist/types/src/testing/reference.d.ts +9 -0
  46. package/dist/types/src/testing/reference.d.ts.map +1 -0
  47. package/dist/types/src/testing/scenarios.d.ts +73 -0
  48. package/dist/types/src/testing/scenarios.d.ts.map +1 -0
  49. package/dist/types/src/testing/simulate.d.ts +31 -0
  50. package/dist/types/src/testing/simulate.d.ts.map +1 -0
  51. package/dist/types/tsconfig.tsbuildinfo +1 -0
  52. package/package.json +74 -5
  53. package/src/Builtins.test.ts +45 -0
  54. package/src/Builtins.ts +274 -0
  55. package/src/CompilePrompt.test.ts +117 -0
  56. package/src/CompilePrompt.ts +376 -0
  57. package/src/Compiler.test.ts +85 -0
  58. package/src/Compiler.ts +234 -0
  59. package/src/Encoding.ts +99 -0
  60. package/src/GoalRules.test.ts +96 -0
  61. package/src/GoalRules.ts +181 -0
  62. package/src/Vocabulary.test.ts +48 -0
  63. package/src/Vocabulary.ts +88 -0
  64. package/src/index.ts +10 -0
  65. package/src/testing/index.ts +7 -0
  66. package/src/testing/reference.ts +91 -0
  67. package/src/testing/scenarios.ts +604 -0
  68. package/src/testing/simulate.ts +127 -0
@@ -0,0 +1,831 @@
1
+ import { CompileError, formatDiagnostic } from "./Compiler.mjs";
2
+ import { make } from "./GoalRules.mjs";
3
+ import { trim } from "@dxos/util";
4
+ //#region src/testing/reference.ts
5
+ /** Hand-written reference compilations of the eight example goals, keyed by scenario number. */
6
+ var REFERENCE = {
7
+ 1: trim`
8
+ wake(dima_work) :- speaker(F, dima), not force(F, expressive).
9
+ wake(dima_work) :- fact(F, dima, _, _), not speaker(F, dima).
10
+ `,
11
+ 2: trim`
12
+ shipped(F) :- about(F, "release"), about(F, "shipped"), polarity(F, "+"), not mood(F, interrogative), factuality(F, "CT+").
13
+ candidate(F) :- fact(F, _, _, _), about(F, "release").
14
+ wake(release) :- candidate(F), not achieved(goal).
15
+ achieved(goal) :- shipped(F), fact(N, goal, notified, _), saidAt(F, T1), saidAt(N, T2), T2 >= T1.
16
+ `,
17
+ 3: trim`
18
+ dima(F) :- speaker(F, dima), force(F, commissive), about(F, "agent plugin").
19
+ wake(reply) :- dima(F), not achieved(goal).
20
+ wake(reply) :- dima(F), achieved(goal).
21
+ wake(followup) :- elapsed(goal, 2d), not achieved(goal).
22
+ achieved(goal) :- dima(F), polarity(F, "+").
23
+ `,
24
+ 4: trim`
25
+ gone(M) :- fact(_, M, archived, inbox).
26
+ gone(M) :- fact(_, M, deleted, inbox).
27
+ waiting(M) :- fact(_, M, received, inbox), not gone(M).
28
+ wake(email) :- fact(F, M, received, inbox).
29
+ holds(goal) :- not waiting(_).
30
+ `,
31
+ 5: trim`
32
+ filed(F) :- about(F, "tax return"), about(F, "filed"), polarity(F, "+"), not speaker(F, alice).
33
+ achieved(goal) :- filed(F).
34
+ wake(remind) :- due("2027-04-15T00:00:00Z", 30d), not achieved(goal).
35
+ wake(remind) :- due("2027-04-15T00:00:00Z", 7d), not achieved(goal).
36
+ wake(overdue) :- due("2027-04-15T23:59:59Z", 0d), not achieved(goal).
37
+ wake(progress) :- subgoal(goal, G), status(G, achieved).
38
+ wake(progress) :- speaker(F, rich), about(F, "tax").
39
+ `,
40
+ 6: trim`
41
+ wake(practice) :- every(1d), not achieved(goal).
42
+ `,
43
+ 7: trim`
44
+ meeting(A) :- action(A, book_meeting).
45
+ meeting(A) :- action(A, update_meeting).
46
+ blocks(A) :- meeting(A), actionArg(A, start, T), weekday(T, friday).
47
+ `,
48
+ 8: trim`
49
+ mine(F) :- source(F, "session:s42"), speaker(F, rich).
50
+ wake(turn) :- mine(F), not achieved(goal).
51
+ achieved(goal) :- mine(F), force(F, commissive), polarity(F, "+"), fact(D, goal, drafted, _).
52
+ `
53
+ };
54
+ /** Plausible but wrong compilations; replaying the scenario must reject each one. */
55
+ var WRONG = [
56
+ {
57
+ scenario: 3,
58
+ note: "achievement ignores polarity, so a refusal achieves the goal",
59
+ source: trim`
60
+ achieved(goal) :- speaker(F, dima), force(F, commissive), about(F, "agent plugin").
61
+ wake(followup) :- elapsed(goal, 2d).
62
+ wake(reply) :- speaker(F, dima).
63
+ `
64
+ },
65
+ {
66
+ scenario: 1,
67
+ note: "only Dima's own messages wake, missing third-party reports",
68
+ source: "wake(x) :- speaker(F, dima)."
69
+ },
70
+ {
71
+ scenario: 7,
72
+ note: "any action with any Friday argument is blocked",
73
+ source: "blocks(A) :- action(A, _), actionArg(A, _, T), weekday(T, friday)."
74
+ },
75
+ {
76
+ scenario: 4,
77
+ note: "holds on any archived email instead of on an empty inbox",
78
+ source: trim`
79
+ holds(goal) :- fact(_, M, archived, inbox).
80
+ wake(e) :- fact(F, _, received, inbox).
81
+ `
82
+ },
83
+ {
84
+ scenario: 6,
85
+ note: "elapsed fires once, not daily",
86
+ source: "wake(p) :- elapsed(goal, 1d)."
87
+ }
88
+ ];
89
+ //#endregion
90
+ //#region src/testing/scenarios.ts
91
+ var at = (time) => `2027-${time}:00Z`;
92
+ var fact = (id, speaker, quote, s, p, o, extra = {}) => ({
93
+ id,
94
+ speaker,
95
+ quote,
96
+ s,
97
+ p,
98
+ o,
99
+ force: "assertive",
100
+ polarity: "+",
101
+ mood: "declarative",
102
+ source: "chat:general",
103
+ factuality: "CT+",
104
+ ...extra
105
+ });
106
+ /** A record of the agent's own action, in the goal's feed. */
107
+ var agent = (id, p, o) => fact(id, "agent", `(agent action) ${p} ${o}`, "goal", p, o, { source: "goal" });
108
+ /** The eight BRAIN.md example goals, each a hand-written timeline with near misses. */
109
+ var SCENARIOS = [
110
+ {
111
+ n: 1,
112
+ goal: "Keep me informed about what Dima is working on",
113
+ owner: "rich",
114
+ createdAt: at("01-04T09:00"),
115
+ steps: [
116
+ {
117
+ id: "s1",
118
+ at: at("01-04T09:10"),
119
+ note: "Dima states current work",
120
+ facts: [fact("f1", "dima", "I'm working on the sync engine refactor today", "dima", "working-on", "sync engine refactor")],
121
+ expect: {
122
+ wake: true,
123
+ achieved: false
124
+ }
125
+ },
126
+ {
127
+ id: "s2",
128
+ at: at("01-04T09:20"),
129
+ note: "unrelated: Alice proposes lunch",
130
+ facts: [fact("f2", "alice", "Lunch at noon?", "alice", "proposes", "lunch", {
131
+ force: "directive",
132
+ mood: "interrogative",
133
+ polarity: "?"
134
+ })],
135
+ expect: {
136
+ wake: false,
137
+ achieved: false
138
+ }
139
+ },
140
+ {
141
+ id: "s3",
142
+ at: at("01-04T10:00"),
143
+ note: "near miss: Bob (not Dima) working on something",
144
+ facts: [fact("f3", "bob", "I'm working on the docs site", "bob", "working-on", "docs site")],
145
+ expect: {
146
+ wake: false,
147
+ achieved: false
148
+ }
149
+ },
150
+ {
151
+ id: "s4",
152
+ at: at("01-04T11:00"),
153
+ note: "third party reports on Dima's work",
154
+ facts: [fact("f4", "alice", "Dima finished the auth migration", "dima", "completed", "auth migration")],
155
+ expect: {
156
+ wake: true,
157
+ achieved: false
158
+ }
159
+ },
160
+ {
161
+ id: "s5",
162
+ at: at("01-04T12:00"),
163
+ note: "Dima small talk (noise; either is fine)",
164
+ facts: [fact("f5", "dima", "thanks!", "dima", "thanks", "alice", { force: "expressive" })],
165
+ expect: {
166
+ wake: null,
167
+ achieved: false
168
+ }
169
+ },
170
+ {
171
+ id: "s6",
172
+ at: at("01-04T13:00"),
173
+ note: "Dima commits to next work item",
174
+ facts: [fact("f6", "dima", "Next up I'll pick up the indexer bug", "dima", "will-work-on", "indexer bug", { force: "commissive" })],
175
+ expect: {
176
+ wake: true,
177
+ achieved: false
178
+ }
179
+ }
180
+ ]
181
+ },
182
+ {
183
+ n: 2,
184
+ goal: "Let me know when the release ships",
185
+ owner: "rich",
186
+ createdAt: at("01-04T09:00"),
187
+ steps: [
188
+ {
189
+ id: "s1",
190
+ at: at("01-04T10:00"),
191
+ note: "future plan, not shipped",
192
+ facts: [fact("f1", "dima", "The release should ship Friday", "release", "ships-on", "friday", {
193
+ force: "commissive",
194
+ factuality: "PR+"
195
+ })],
196
+ expect: { achieved: false }
197
+ },
198
+ {
199
+ id: "s2",
200
+ at: at("01-05T10:00"),
201
+ note: "negative polarity",
202
+ facts: [fact("f2", "dima", "The release didn't ship today, CI is red", "release", "shipped", "today", { polarity: "-" })],
203
+ expect: { achieved: false }
204
+ },
205
+ {
206
+ id: "s3",
207
+ at: at("01-05T11:00"),
208
+ note: "question",
209
+ facts: [fact("f3", "alice", "Has the release shipped yet?", "release", "shipped", "unknown", {
210
+ mood: "interrogative",
211
+ polarity: "?",
212
+ force: "directive",
213
+ factuality: "Uu"
214
+ })],
215
+ expect: { achieved: false }
216
+ },
217
+ {
218
+ id: "s4",
219
+ at: at("01-06T09:00"),
220
+ note: "near miss: something else shipped",
221
+ facts: [fact("f4", "bob", "I shipped the docs update", "bob", "shipped", "docs update")],
222
+ expect: {
223
+ wake: false,
224
+ achieved: false
225
+ }
226
+ },
227
+ {
228
+ id: "s5",
229
+ at: at("01-07T16:00"),
230
+ note: "release shipped",
231
+ facts: [fact("f5", "dima", "The v0.9 release shipped to npm", "release v0.9", "shipped", "npm")],
232
+ expect: {
233
+ wake: true,
234
+ achieved: null
235
+ }
236
+ },
237
+ {
238
+ id: "s6",
239
+ at: at("01-07T16:05"),
240
+ note: "agent notified owner",
241
+ facts: [agent("g1", "notified", "rich")],
242
+ expect: { achieved: true }
243
+ }
244
+ ]
245
+ },
246
+ {
247
+ n: 3,
248
+ goal: "Get Dima to help me with the agent plugin",
249
+ owner: "rich",
250
+ createdAt: at("01-04T09:00"),
251
+ context: "Follow up after 2 days without a commitment.",
252
+ steps: [
253
+ {
254
+ id: "s1",
255
+ at: at("01-04T09:01"),
256
+ note: "agent relays request",
257
+ facts: [agent("g1", "relayed", "msg-1"), agent("g2", "awaiting", "dima")],
258
+ expect: { achieved: false }
259
+ },
260
+ {
261
+ id: "s2",
262
+ at: at("01-05T10:00"),
263
+ note: "Dima refuses",
264
+ facts: [fact("f1", "dima", "I'm busy with the release this week, can't help with the agent plugin", "dima", "helps-with", "agent plugin", {
265
+ force: "commissive",
266
+ polarity: "-"
267
+ })],
268
+ expect: {
269
+ wake: true,
270
+ achieved: false
271
+ }
272
+ },
273
+ {
274
+ id: "s3",
275
+ at: at("01-05T11:00"),
276
+ note: "near miss: Dima commits to something else",
277
+ facts: [fact("f2", "dima", "I'll review the docs PR tomorrow", "dima", "reviews", "docs PR", { force: "commissive" })],
278
+ expect: {
279
+ wake: false,
280
+ achieved: false
281
+ }
282
+ },
283
+ {
284
+ id: "s4",
285
+ at: at("01-05T12:00"),
286
+ note: "near miss: Alice (not Dima) offers help",
287
+ facts: [fact("f3", "alice", "I'll help with the agent plugin", "alice", "helps-with", "agent plugin", { force: "commissive" })],
288
+ expect: { achieved: false }
289
+ },
290
+ {
291
+ id: "s5",
292
+ at: at("01-06T08:00"),
293
+ note: "tick: under 2 days",
294
+ expect: {
295
+ wake: false,
296
+ achieved: false
297
+ }
298
+ },
299
+ {
300
+ id: "s6",
301
+ at: at("01-06T09:30"),
302
+ note: "tick: 2 days after creation",
303
+ expect: { achieved: false }
304
+ },
305
+ {
306
+ id: "s7",
307
+ at: at("01-07T10:30"),
308
+ note: "tick: 2 days after refusal",
309
+ expect: { achieved: false }
310
+ },
311
+ {
312
+ id: "s8",
313
+ at: at("01-07T11:00"),
314
+ note: "Dima commits",
315
+ facts: [fact("f4", "dima", "OK, I'll start on the agent plugin", "dima", "helps-with", "agent plugin", { force: "commissive" })],
316
+ expect: {
317
+ wake: true,
318
+ achieved: true
319
+ }
320
+ },
321
+ {
322
+ id: "s9",
323
+ at: at("01-09T12:00"),
324
+ note: "tick after achieved: no follow-up",
325
+ expect: {
326
+ wake: false,
327
+ achieved: true
328
+ }
329
+ }
330
+ ],
331
+ wakeOneOf: [["s6", "s7"]]
332
+ },
333
+ {
334
+ n: 4,
335
+ goal: "Keep my inbox empty",
336
+ owner: "rich",
337
+ createdAt: at("01-04T09:00"),
338
+ steps: [
339
+ {
340
+ id: "s0",
341
+ at: at("01-04T09:00"),
342
+ note: "empty inbox",
343
+ expect: {
344
+ wake: false,
345
+ holds: true,
346
+ achieved: false
347
+ }
348
+ },
349
+ {
350
+ id: "s1",
351
+ at: at("01-04T09:30"),
352
+ note: "email arrives",
353
+ facts: [fact("f1", "carol@example.com", "Subject: Invoice for December", "email:m1", "received", "inbox", { source: "mailbox:rich" })],
354
+ expect: {
355
+ wake: true,
356
+ holds: false,
357
+ achieved: false
358
+ }
359
+ },
360
+ {
361
+ id: "s2",
362
+ at: at("01-04T09:40"),
363
+ note: "second email",
364
+ facts: [fact("f2", "news@example.com", "Subject: Weekly newsletter", "email:m2", "received", "inbox", { source: "mailbox:rich" })],
365
+ expect: {
366
+ wake: true,
367
+ holds: false
368
+ }
369
+ },
370
+ {
371
+ id: "s3",
372
+ at: at("01-04T09:45"),
373
+ note: "agent archives m1",
374
+ facts: [fact("f3", "agent", "(agent action) archived email:m1", "email:m1", "archived", "inbox", { source: "mailbox:rich" })],
375
+ expect: { holds: false }
376
+ },
377
+ {
378
+ id: "s4",
379
+ at: at("01-04T09:46"),
380
+ note: "agent archives m2",
381
+ facts: [fact("f4", "agent", "(agent action) archived email:m2", "email:m2", "archived", "inbox", { source: "mailbox:rich" })],
382
+ expect: { holds: true }
383
+ },
384
+ {
385
+ id: "s5",
386
+ at: at("01-04T10:00"),
387
+ note: "near miss: chat mentions email",
388
+ facts: [fact("f5", "alice", "Did you see my email?", "rich", "saw", "email", {
389
+ mood: "interrogative",
390
+ polarity: "?",
391
+ force: "directive"
392
+ })],
393
+ expect: {
394
+ wake: false,
395
+ holds: true,
396
+ achieved: false
397
+ }
398
+ },
399
+ {
400
+ id: "s6",
401
+ at: at("01-04T11:00"),
402
+ note: "third email",
403
+ facts: [fact("f6", "dan@example.com", "Subject: Contract draft", "email:m3", "received", "inbox", { source: "mailbox:rich" })],
404
+ expect: {
405
+ wake: true,
406
+ holds: false,
407
+ achieved: false
408
+ }
409
+ }
410
+ ]
411
+ },
412
+ {
413
+ n: 5,
414
+ goal: "Complete my taxes by April 15",
415
+ owner: "rich",
416
+ createdAt: at("02-01T09:00"),
417
+ context: "Deadline is 2027-04-15.",
418
+ steps: [
419
+ {
420
+ id: "s1",
421
+ at: at("02-01T09:05"),
422
+ note: "judgment created sub-goals",
423
+ subgoals: [
424
+ "g_w2",
425
+ "g_return",
426
+ "g_accountant"
427
+ ],
428
+ status: {
429
+ g_w2: "active",
430
+ g_return: "active",
431
+ g_accountant: "active"
432
+ },
433
+ expect: { achieved: false }
434
+ },
435
+ {
436
+ id: "s2",
437
+ at: at("03-01T09:00"),
438
+ note: "tick 45d before deadline",
439
+ expect: { achieved: false }
440
+ },
441
+ {
442
+ id: "s3",
443
+ at: at("03-10T09:00"),
444
+ note: "near miss: Alice filed HER taxes",
445
+ facts: [fact("f1", "alice", "I filed my taxes already", "alice", "filed", "taxes")],
446
+ expect: { achieved: false }
447
+ },
448
+ {
449
+ id: "s4",
450
+ at: at("03-12T09:00"),
451
+ note: "owner progress fact",
452
+ facts: [fact("f2", "rich", "I uploaded my W-2s to the tax folder", "rich", "uploaded", "W-2s")],
453
+ expect: { achieved: false }
454
+ },
455
+ {
456
+ id: "s5",
457
+ at: at("03-12T09:10"),
458
+ note: "sub-goal achieved",
459
+ status: { g_w2: "achieved" },
460
+ expect: {
461
+ wake: true,
462
+ achieved: false
463
+ }
464
+ },
465
+ {
466
+ id: "s6",
467
+ at: at("04-08T09:00"),
468
+ note: "tick 7d before",
469
+ expect: { achieved: false }
470
+ },
471
+ {
472
+ id: "s7",
473
+ at: at("04-14T09:00"),
474
+ note: "tick 1d before",
475
+ expect: { achieved: false }
476
+ },
477
+ {
478
+ id: "s8",
479
+ at: at("04-14T12:00"),
480
+ note: "remaining sub-goals achieved",
481
+ status: {
482
+ g_return: "achieved",
483
+ g_accountant: "achieved"
484
+ },
485
+ expect: { achieved: null }
486
+ },
487
+ {
488
+ id: "s9",
489
+ at: at("04-14T15:00"),
490
+ note: "accountant filed the return",
491
+ facts: [fact("f3", "accountant", "I've filed your 2026 tax return", "tax return 2026", "filed", "irs", { concerns: ["rich"] })],
492
+ expect: { achieved: true }
493
+ },
494
+ {
495
+ id: "s10",
496
+ at: at("04-16T09:00"),
497
+ note: "after deadline, already achieved: no overdue wake",
498
+ expect: {
499
+ wake: false,
500
+ achieved: true
501
+ }
502
+ }
503
+ ],
504
+ wakeOneOf: [[
505
+ "s2",
506
+ "s6",
507
+ "s7"
508
+ ]]
509
+ },
510
+ {
511
+ n: 6,
512
+ goal: "Learn French",
513
+ owner: "rich",
514
+ createdAt: at("01-04T07:00"),
515
+ context: "Practise daily.",
516
+ steps: [
517
+ {
518
+ id: "s1",
519
+ at: at("01-04T14:00"),
520
+ note: "tick same day",
521
+ expect: {
522
+ wake: false,
523
+ achieved: false
524
+ }
525
+ },
526
+ {
527
+ id: "s2",
528
+ at: at("01-05T07:30"),
529
+ note: "tick next day",
530
+ expect: {
531
+ wake: true,
532
+ achieved: false
533
+ }
534
+ },
535
+ {
536
+ id: "s3",
537
+ at: at("01-05T12:00"),
538
+ note: "near miss: French fries",
539
+ facts: [fact("f1", "rich", "Let's get French fries for lunch", "rich", "wants", "french fries", { force: "directive" })],
540
+ expect: { achieved: false }
541
+ },
542
+ {
543
+ id: "s4",
544
+ at: at("01-05T14:00"),
545
+ note: "tick same day",
546
+ expect: {
547
+ wake: false,
548
+ achieved: false
549
+ }
550
+ },
551
+ {
552
+ id: "s5",
553
+ at: at("01-05T18:00"),
554
+ note: "owner practised",
555
+ facts: [fact("f2", "rich", "I did 20 minutes of French practice on Duolingo", "rich", "practised", "french")],
556
+ expect: { achieved: false }
557
+ },
558
+ {
559
+ id: "s6",
560
+ at: at("01-06T07:30"),
561
+ note: "tick next day",
562
+ expect: {
563
+ wake: true,
564
+ achieved: false
565
+ }
566
+ }
567
+ ]
568
+ },
569
+ {
570
+ n: 7,
571
+ goal: "Never book meetings on Fridays",
572
+ owner: "rich",
573
+ createdAt: at("01-04T09:00"),
574
+ steps: [
575
+ {
576
+ id: "s1",
577
+ at: at("01-04T10:00"),
578
+ note: "Tuesday meeting",
579
+ actions: [{
580
+ id: "a1",
581
+ kind: "book_meeting",
582
+ args: {
583
+ start: "2027-01-05T10:00:00Z",
584
+ title: "Sync"
585
+ }
586
+ }],
587
+ expect: {
588
+ blocks: false,
589
+ wake: false
590
+ }
591
+ },
592
+ {
593
+ id: "s2",
594
+ at: at("01-04T10:05"),
595
+ note: "Friday meeting",
596
+ actions: [{
597
+ id: "a2",
598
+ kind: "book_meeting",
599
+ args: {
600
+ start: "2027-01-08T15:00:00Z",
601
+ title: "Retro"
602
+ }
603
+ }],
604
+ expect: {
605
+ blocks: true,
606
+ wake: false
607
+ }
608
+ },
609
+ {
610
+ id: "s3",
611
+ at: at("01-04T10:10"),
612
+ note: "near miss: Friday email",
613
+ actions: [{
614
+ id: "a3",
615
+ kind: "send_email",
616
+ args: {
617
+ sendAt: "2027-01-08T09:00:00Z",
618
+ to: "dima"
619
+ }
620
+ }],
621
+ expect: {
622
+ blocks: false,
623
+ wake: false
624
+ }
625
+ },
626
+ {
627
+ id: "s4",
628
+ at: at("01-04T10:20"),
629
+ note: "chat fact mentions Friday meeting",
630
+ facts: [fact("f1", "dima", "Let's meet Friday", "dima", "proposes", "meeting on friday", { force: "directive" })],
631
+ expect: {
632
+ blocks: false,
633
+ wake: false
634
+ }
635
+ },
636
+ {
637
+ id: "s5",
638
+ at: at("01-04T10:30"),
639
+ note: "move meeting to Friday (arguably blocked)",
640
+ actions: [{
641
+ id: "a4",
642
+ kind: "update_meeting",
643
+ args: {
644
+ start: "2027-01-15T11:00:00Z",
645
+ title: "Sync"
646
+ }
647
+ }],
648
+ expect: { blocks: null }
649
+ }
650
+ ]
651
+ },
652
+ {
653
+ n: 8,
654
+ goal: "Help me draft this PR description",
655
+ owner: "rich",
656
+ session: "session:s42",
657
+ createdAt: at("01-04T09:00"),
658
+ steps: [
659
+ {
660
+ id: "s1",
661
+ at: at("01-04T09:01"),
662
+ note: "owner asks in session",
663
+ facts: [fact("f1", "rich", "Here's the diff, draft a PR description", "agent", "drafts", "PR description", {
664
+ force: "directive",
665
+ mood: "imperative",
666
+ source: "session:s42"
667
+ })],
668
+ expect: {
669
+ wake: true,
670
+ achieved: false
671
+ }
672
+ },
673
+ {
674
+ id: "s2",
675
+ at: at("01-04T09:03"),
676
+ note: "agent produced a draft",
677
+ facts: [agent("g1", "drafted", "pr description v1")],
678
+ expect: { achieved: false }
679
+ },
680
+ {
681
+ id: "s3",
682
+ at: at("01-04T09:05"),
683
+ note: "near miss: other source mentions PR descriptions",
684
+ facts: [fact("f2", "alice", "The PR description template changed", "pr template", "changed", "today", { source: "chat:general" })],
685
+ expect: {
686
+ wake: false,
687
+ achieved: false
688
+ }
689
+ },
690
+ {
691
+ id: "s4",
692
+ at: at("01-04T09:07"),
693
+ note: "owner asks for revision",
694
+ facts: [fact("f3", "rich", "Tighten the summary section", "agent", "tightens", "summary section", {
695
+ force: "directive",
696
+ mood: "imperative",
697
+ source: "session:s42"
698
+ })],
699
+ expect: {
700
+ wake: true,
701
+ achieved: false
702
+ }
703
+ },
704
+ {
705
+ id: "s5",
706
+ at: at("01-04T09:10"),
707
+ note: "owner accepts",
708
+ facts: [fact("f4", "rich", "Looks good, posting it now", "rich", "posts", "draft", {
709
+ force: "commissive",
710
+ source: "session:s42"
711
+ })],
712
+ expect: { achieved: true }
713
+ }
714
+ ]
715
+ }
716
+ ];
717
+ //#endregion
718
+ //#region src/testing/simulate.ts
719
+ /** Converts a scripted fact to an `RDF.Fact` said (and recorded) at `saidAt`. */
720
+ var toFact = (fact, saidAt) => ({
721
+ id: fact.id,
722
+ assertion: {
723
+ subject: {
724
+ kind: "entity",
725
+ entity: fact.s
726
+ },
727
+ predicate: fact.p,
728
+ object: {
729
+ kind: "entity",
730
+ entity: fact.o
731
+ },
732
+ quote: fact.quote
733
+ },
734
+ factuality: {
735
+ value: fact.factuality,
736
+ polarity: fact.polarity
737
+ },
738
+ illocution: {
739
+ force: fact.force,
740
+ mood: fact.mood
741
+ },
742
+ attribution: {
743
+ agent: fact.speaker,
744
+ source: fact.source,
745
+ generatedAtTime: saidAt
746
+ },
747
+ recordedAt: saidAt,
748
+ extractor: {
749
+ id: "scenario",
750
+ model: "none",
751
+ version: "1"
752
+ },
753
+ sourceHash: fact.id,
754
+ pass: "scenario"
755
+ });
756
+ /**
757
+ * Replays a scenario against compiled goal rules and checks every step's expectations; a
758
+ * compile error or a runtime error is reported as a failure.
759
+ */
760
+ var simulate = (scenario, source, options = {}) => {
761
+ const concerns = /* @__PURE__ */ new Map();
762
+ for (const step of scenario.steps) for (const fact of step.facts ?? []) concerns.set(fact.id, fact.concerns ?? [fact.s]);
763
+ let rules;
764
+ try {
765
+ rules = make({
766
+ source,
767
+ createdAt: Date.parse(scenario.createdAt),
768
+ vocabulary: options.vocabulary,
769
+ concerns: (fact) => concerns.get(fact.id) ?? []
770
+ });
771
+ } catch (error) {
772
+ if (error instanceof CompileError) return {
773
+ ok: false,
774
+ steps: [],
775
+ failures: error.diagnostics.map(formatDiagnostic)
776
+ };
777
+ throw error;
778
+ }
779
+ const statuses = {};
780
+ const steps = scenario.steps.map((step) => {
781
+ for (const subgoal of step.subgoals ?? []) statuses[subgoal] = "active";
782
+ Object.assign(statuses, step.status ?? {});
783
+ const evaluation = rules.update({
784
+ at: Date.parse(step.at),
785
+ facts: (step.facts ?? []).map((fact) => toFact(fact, step.at)),
786
+ subgoals: { ...statuses }
787
+ });
788
+ const blocks = (step.actions ?? []).some((action) => rules.checkAction(action).blocked);
789
+ const observed = {
790
+ wake: evaluation.wakes.length > 0,
791
+ labels: evaluation.wakes.map(({ label }) => label),
792
+ achieved: evaluation.achieved,
793
+ holds: evaluation.holds,
794
+ blocks
795
+ };
796
+ return {
797
+ id: step.id,
798
+ ...observed,
799
+ wakes: evaluation.wakes,
800
+ failures: checkStep(step, observed)
801
+ };
802
+ });
803
+ const failures = [...steps.flatMap((step) => step.failures), ...checkGroups(scenario, steps)];
804
+ return {
805
+ ok: failures.length === 0,
806
+ steps,
807
+ failures
808
+ };
809
+ };
810
+ var EXPECTATIONS = [
811
+ "wake",
812
+ "achieved",
813
+ "holds",
814
+ "blocks"
815
+ ];
816
+ /** The expectations of `step` that `result` violates. */
817
+ var checkStep = (step, result) => EXPECTATIONS.flatMap((key) => {
818
+ const expected = step.expect[key];
819
+ if (expected === void 0 || expected === null || result[key] === expected) return [];
820
+ const labels = key === "wake" && result.labels.length > 0 ? ` [${result.labels.join(", ")}]` : "";
821
+ return [`${step.id} (${step.note}): ${key}=${result[key]} expected ${expected}${labels}`];
822
+ });
823
+ var checkGroups = (scenario, results) => {
824
+ const failures = [];
825
+ for (const group of scenario.wakeOneOf ?? []) if (!group.some((id) => results[scenario.steps.findIndex((step) => step.id === id)]?.wake)) failures.push(`no wake in any of ${group.join(", ")} (time-driven wake expected)`);
826
+ return failures;
827
+ };
828
+ //#endregion
829
+ export { REFERENCE, SCENARIOS, WRONG, simulate, toFact };
830
+
831
+ //# sourceMappingURL=testing.mjs.map