@dxos/compute 0.16.0 → 0.17.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 (136) hide show
  1. package/dist/lib/AiMetering.mjs +1 -0
  2. package/dist/lib/Hook.mjs +1 -0
  3. package/dist/lib/Operation.mjs +3 -3
  4. package/dist/lib/Process.mjs +2 -2
  5. package/dist/lib/Trace.mjs +1 -1
  6. package/dist/lib/chunk-AgentIdentity.mjs +1 -1
  7. package/dist/lib/chunk-AgentService.mjs.map +1 -1
  8. package/dist/lib/chunk-AiMetering.mjs +77 -0
  9. package/dist/lib/chunk-AiMetering.mjs.map +1 -0
  10. package/dist/lib/chunk-Cancellation.mjs +1 -1
  11. package/dist/lib/chunk-Hook.mjs +78 -0
  12. package/dist/lib/chunk-Hook.mjs.map +1 -0
  13. package/dist/lib/chunk-Instructions.mjs +1 -1
  14. package/dist/lib/chunk-Operation.mjs +169 -5
  15. package/dist/lib/chunk-Operation.mjs.map +1 -1
  16. package/dist/lib/chunk-OperationHandlerSet.mjs +1 -1
  17. package/dist/lib/chunk-OperationRegistry.mjs +1 -1
  18. package/dist/lib/chunk-OperationRegistry.mjs.map +1 -1
  19. package/dist/lib/chunk-Process.mjs +18 -10
  20. package/dist/lib/chunk-Process.mjs.map +1 -1
  21. package/dist/lib/chunk-Project.mjs +52 -24
  22. package/dist/lib/chunk-Project.mjs.map +1 -1
  23. package/dist/lib/chunk-Reducer.mjs +827 -0
  24. package/dist/lib/chunk-Reducer.mjs.map +1 -0
  25. package/dist/lib/chunk-Routine.mjs +1 -1
  26. package/dist/lib/chunk-Runnable.mjs +1 -1
  27. package/dist/lib/chunk-ServiceResolver.mjs +1 -1
  28. package/dist/lib/chunk-Skill.mjs +10 -47
  29. package/dist/lib/chunk-Skill.mjs.map +1 -1
  30. package/dist/lib/chunk-Spec.mjs +49 -0
  31. package/dist/lib/chunk-Spec.mjs.map +1 -0
  32. package/dist/lib/chunk-SpecReview.mjs +98 -0
  33. package/dist/lib/chunk-SpecReview.mjs.map +1 -0
  34. package/dist/lib/chunk-SqlService.mjs.map +1 -1
  35. package/dist/lib/chunk-StorageService.mjs +1 -1
  36. package/dist/lib/chunk-Template.mjs +2 -2
  37. package/dist/lib/chunk-Trace.mjs +97 -13
  38. package/dist/lib/chunk-Trace.mjs.map +1 -1
  39. package/dist/lib/chunk-Trajectory.mjs +326 -0
  40. package/dist/lib/chunk-Trajectory.mjs.map +1 -0
  41. package/dist/lib/chunk-Transcript.mjs +84 -0
  42. package/dist/lib/chunk-Transcript.mjs.map +1 -0
  43. package/dist/lib/chunk-definitions.mjs +1 -1
  44. package/dist/lib/errors.mjs +3 -1
  45. package/dist/lib/errors.mjs.map +1 -1
  46. package/dist/lib/index.mjs +14 -8
  47. package/dist/lib/testing.mjs +2 -2
  48. package/dist/lib/trajectory/Reducer.mjs +1 -0
  49. package/dist/lib/trajectory/Trajectory.mjs +1 -0
  50. package/dist/lib/trajectory/Transcript.mjs +1 -0
  51. package/dist/lib/types/Project.mjs +1 -1
  52. package/dist/lib/types/Skill.mjs +1 -1
  53. package/dist/lib/types/Spec.mjs +1 -0
  54. package/dist/lib/types/SpecReview.mjs +1 -0
  55. package/dist/types/src/AgentService.d.ts +9 -0
  56. package/dist/types/src/AgentService.d.ts.map +1 -1
  57. package/dist/types/src/AiMetering.d.ts +23 -0
  58. package/dist/types/src/AiMetering.d.ts.map +1 -0
  59. package/dist/types/src/AiMetering.test.d.ts +2 -0
  60. package/dist/types/src/AiMetering.test.d.ts.map +1 -0
  61. package/dist/types/src/Hook.d.ts +84 -0
  62. package/dist/types/src/Hook.d.ts.map +1 -0
  63. package/dist/types/src/Hook.test.d.ts +2 -0
  64. package/dist/types/src/Hook.test.d.ts.map +1 -0
  65. package/dist/types/src/Operation.d.ts +276 -2
  66. package/dist/types/src/Operation.d.ts.map +1 -1
  67. package/dist/types/src/Process.d.ts +26 -4
  68. package/dist/types/src/Process.d.ts.map +1 -1
  69. package/dist/types/src/Script.test.d.ts +2 -0
  70. package/dist/types/src/Script.test.d.ts.map +1 -0
  71. package/dist/types/src/Trace.d.ts +103 -1
  72. package/dist/types/src/Trace.d.ts.map +1 -1
  73. package/dist/types/src/errors.d.ts +30 -0
  74. package/dist/types/src/errors.d.ts.map +1 -1
  75. package/dist/types/src/index.d.ts +3 -1
  76. package/dist/types/src/index.d.ts.map +1 -1
  77. package/dist/types/src/trajectory/Reducer.d.ts +191 -0
  78. package/dist/types/src/trajectory/Reducer.d.ts.map +1 -0
  79. package/dist/types/src/trajectory/Reducer.test.d.ts +2 -0
  80. package/dist/types/src/trajectory/Reducer.test.d.ts.map +1 -0
  81. package/dist/types/src/trajectory/Trajectory.d.ts +3211 -0
  82. package/dist/types/src/trajectory/Trajectory.d.ts.map +1 -0
  83. package/dist/types/src/trajectory/Trajectory.test.d.ts +2 -0
  84. package/dist/types/src/trajectory/Trajectory.test.d.ts.map +1 -0
  85. package/dist/types/src/trajectory/Transcript.d.ts +40 -0
  86. package/dist/types/src/trajectory/Transcript.d.ts.map +1 -0
  87. package/dist/types/src/trajectory/index.d.ts +4 -0
  88. package/dist/types/src/trajectory/index.d.ts.map +1 -0
  89. package/dist/types/src/trajectory/light-threads.test.d.ts +2 -0
  90. package/dist/types/src/trajectory/light-threads.test.d.ts.map +1 -0
  91. package/dist/types/src/trajectory/samples.test.d.ts +2 -0
  92. package/dist/types/src/trajectory/samples.test.d.ts.map +1 -0
  93. package/dist/types/src/types/Project.d.ts +11 -12
  94. package/dist/types/src/types/Project.d.ts.map +1 -1
  95. package/dist/types/src/types/Skill.d.ts +6 -51
  96. package/dist/types/src/types/Skill.d.ts.map +1 -1
  97. package/dist/types/src/types/Spec.d.ts +26 -0
  98. package/dist/types/src/types/Spec.d.ts.map +1 -0
  99. package/dist/types/src/types/SpecReview.d.ts +76 -0
  100. package/dist/types/src/types/SpecReview.d.ts.map +1 -0
  101. package/dist/types/src/types/index.d.ts +2 -0
  102. package/dist/types/src/types/index.d.ts.map +1 -1
  103. package/dist/types/tsconfig.tsbuildinfo +1 -1
  104. package/package.json +59 -26
  105. package/src/AgentService.ts +9 -0
  106. package/src/AiMetering.test.ts +89 -0
  107. package/src/AiMetering.ts +93 -0
  108. package/src/Hook.test.ts +75 -0
  109. package/src/Hook.ts +154 -0
  110. package/src/Operation.test.ts +23 -0
  111. package/src/Operation.ts +334 -0
  112. package/src/Process.ts +33 -6
  113. package/src/Script.test.ts +74 -0
  114. package/src/Trace.ts +97 -4
  115. package/src/errors.ts +3 -0
  116. package/src/index.ts +3 -1
  117. package/src/trajectory/Reducer.test.ts +575 -0
  118. package/src/trajectory/Reducer.ts +1026 -0
  119. package/src/trajectory/Trajectory.test.ts +114 -0
  120. package/src/trajectory/Trajectory.ts +440 -0
  121. package/src/trajectory/Transcript.ts +113 -0
  122. package/src/trajectory/index.ts +7 -0
  123. package/src/trajectory/light-threads.test.ts +310 -0
  124. package/src/trajectory/samples.test.ts +285 -0
  125. package/src/types/Project.test.ts +2 -1
  126. package/src/types/Project.ts +43 -27
  127. package/src/types/Skill.ts +6 -61
  128. package/src/types/Spec.ts +68 -0
  129. package/src/types/SpecReview.ts +98 -0
  130. package/src/types/index.ts +2 -0
  131. package/dist/lib/McpServer.mjs +0 -1
  132. package/dist/lib/chunk-McpServer.mjs +0 -124
  133. package/dist/lib/chunk-McpServer.mjs.map +0 -1
  134. package/dist/types/src/McpServer.d.ts +0 -113
  135. package/dist/types/src/McpServer.d.ts.map +0 -1
  136. package/src/McpServer.ts +0 -142
@@ -0,0 +1,310 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import * as Schema from 'effect/Schema';
6
+ import { describe, test } from 'vitest';
7
+
8
+ import { Obj } from '@dxos/echo';
9
+
10
+ import * as Reducer from './Reducer.ts';
11
+ import * as Trajectory from './Trajectory.ts';
12
+
13
+ const user: Trajectory.Sender = { role: 'user', name: 'alice' };
14
+ const assistant: Trajectory.Sender = { role: 'assistant', name: 'composer' };
15
+
16
+ const text = (value: string) => [{ _tag: 'text' as const, text: value }];
17
+
18
+ /**
19
+ * An event written in `thread` (absent: main). An opener starts a thread of its own, as `Feed.fork` makes it the base
20
+ * of a new branch; the branch takes the opener's id so a test can name the thread by its opener.
21
+ */
22
+ const event = (
23
+ payload: Trajectory.Payload,
24
+ sender: Trajectory.Sender = assistant,
25
+ thread?: string,
26
+ ): Trajectory.Event => {
27
+ if (payload._tag === 'turnBegin' || payload._tag === 'threadOpen') {
28
+ const id = Obj.ID.random();
29
+ return Trajectory.make({ id, payload, sender, thread: Trajectory.ThreadId.make(id) });
30
+ }
31
+ return Trajectory.make({
32
+ payload,
33
+ sender,
34
+ thread: thread === undefined ? undefined : Trajectory.ThreadId.make(thread),
35
+ });
36
+ };
37
+
38
+ /** The thread an opener started: its branch, which takes the opener's id here. */
39
+ const threadId = (opener: Trajectory.Event): Trajectory.ThreadId => Trajectory.ThreadId.make(opener.id);
40
+
41
+ const message = (value: string, sender: Trajectory.Sender = assistant, thread?: string) =>
42
+ event({ _tag: 'message', blocks: text(value) }, sender, thread);
43
+
44
+ const texts = (entries: readonly Reducer.PromptEntry[]) =>
45
+ entries.flatMap((entry) => entry.blocks.map((block) => (block._tag === 'text' ? block.text : block._tag)));
46
+
47
+ /**
48
+ * Turn A on main is mid tool call when prompt B arrives and opens light thread L, which runs a turn of its own.
49
+ */
50
+ const parallel = () => {
51
+ const promptA = message('Write a poem about the sea.', user);
52
+ const turnA = event({ _tag: 'turnBegin', from: { event: promptA.id } });
53
+ const consumeA = event({ _tag: 'promptConsume', message: promptA.id }, assistant, turnA.id);
54
+ const callA = event(
55
+ {
56
+ _tag: 'message',
57
+ blocks: [{ _tag: 'toolCall', toolCallId: 'c1', name: 'search', input: '{"q":"sea"}', providerExecuted: false }],
58
+ },
59
+ assistant,
60
+ turnA.id,
61
+ );
62
+ const promptB = message('What is 17 times 23?', user);
63
+ const light = event({
64
+ _tag: 'threadOpen',
65
+ mode: 'light',
66
+ from: { event: promptB.id },
67
+ purpose: 'What is 17 times 23?',
68
+ siblings: [threadId(turnA)],
69
+ });
70
+ const turnB = event({ _tag: 'turnBegin', from: { thread: Trajectory.ThreadId.make(light.id), event: light.id } });
71
+ const consumeB = event({ _tag: 'promptConsume', message: promptB.id }, assistant, turnB.id);
72
+ const answerB = message('391.', assistant, turnB.id);
73
+ const endB = event({ _tag: 'turnEnd', finishReason: 'stop' }, assistant, turnB.id);
74
+ const mergeB = event({ _tag: 'threadMerge', mode: 'raw' }, assistant, light.id);
75
+ const resultA = event(
76
+ {
77
+ _tag: 'message',
78
+ blocks: [{ _tag: 'toolResult', toolCallId: 'c1', name: 'search', result: 'waves', providerExecuted: false }],
79
+ },
80
+ { role: 'tool' },
81
+ turnA.id,
82
+ );
83
+ const poemA = message('The sea is wide.', assistant, turnA.id);
84
+ const endA = event({ _tag: 'turnEnd', finishReason: 'stop' }, assistant, turnA.id);
85
+ return {
86
+ promptA,
87
+ turnA,
88
+ light,
89
+ turnB,
90
+ head: [promptA, turnA, consumeA, callA, promptB, light, turnB, consumeB],
91
+ tail: [answerB, endB, mergeB, resultA, poemA, endA],
92
+ };
93
+ };
94
+
95
+ describe('light threads', () => {
96
+ test('a light thread starts with main, a snapshot of its siblings, the separator and its prompt', ({ expect }) => {
97
+ const { head, light, turnB, turnA } = parallel();
98
+ const { state } = Reducer.run(Reducer.prompt, head);
99
+ const prompt = Reducer.promptFor(state, threadId(turnB));
100
+ expect(prompt.map((entry) => entry.role)).to.deep.eq(['event', 'user']);
101
+ const [snapshot, request] = texts(prompt);
102
+ expect(snapshot).to.include(`<thread id="${turnA.id}"`);
103
+ expect(snapshot).to.include('alice: Write a poem about the sea.');
104
+ expect(snapshot).to.include('[calls search({"q":"sea"})]');
105
+ expect(snapshot).to.include(`You are thread ${light.id}.`);
106
+ expect(snapshot).to.include('thread_wait');
107
+ expect(request).to.eq('What is 17 times 23?');
108
+ expect(prompt[0].ephemeral).to.eq(true);
109
+ });
110
+
111
+ test('the running turn does not see the light thread', ({ expect }) => {
112
+ const { head, turnA } = parallel();
113
+ const { state } = Reducer.run(Reducer.prompt, head);
114
+ expect(texts(Reducer.promptFor(state, threadId(turnA)))).to.deep.eq(['Write a poem about the sea.', 'toolCall']);
115
+ });
116
+
117
+ test('merging drops the snapshot and keeps the closure in merge order', ({ expect }) => {
118
+ const { head, tail, turnA } = parallel();
119
+ const { state } = Reducer.run(Reducer.prompt, [...head, ...tail]);
120
+ expect(texts(state.entries)).to.deep.eq([
121
+ 'What is 17 times 23?',
122
+ '391.',
123
+ 'Write a poem about the sea.',
124
+ 'toolCall',
125
+ 'toolResult',
126
+ 'The sea is wide.',
127
+ ]);
128
+ // Turn A merged after the light thread it never saw.
129
+ expect(Object.keys(state.stale)).to.deep.eq([turnA.id]);
130
+ expect(state.threads).to.deep.eq({});
131
+ });
132
+
133
+ test('a light thread without siblings is a plain fork', ({ expect }) => {
134
+ const prompt = message('hello', user);
135
+ const light = event({ _tag: 'threadOpen', mode: 'light', from: { event: prompt.id }, siblings: [] });
136
+ const { state } = Reducer.run(Reducer.prompt, [prompt, light]);
137
+ expect(Reducer.promptFor(state, threadId(light))).to.deep.eq([]);
138
+ });
139
+
140
+ test('a long sibling keeps its request and its latest entries', ({ expect }) => {
141
+ const prompt = message('count', user);
142
+ const turn = event({ _tag: 'turnBegin', from: { event: prompt.id } });
143
+ const steps = Array.from({ length: 30 }, (_, index) => message(`step ${index}`, assistant, turn.id));
144
+ const light = event({
145
+ _tag: 'threadOpen',
146
+ mode: 'light',
147
+ from: { event: steps[29].id },
148
+ siblings: [threadId(turn)],
149
+ });
150
+ const { state } = Reducer.run(Reducer.prompt, [
151
+ prompt,
152
+ turn,
153
+ event({ _tag: 'promptConsume', message: prompt.id }, assistant, turn.id),
154
+ ...steps,
155
+ light,
156
+ ]);
157
+ const [snapshot] = texts(Reducer.promptFor(state, threadId(light)));
158
+ expect(snapshot).to.include('alice: count');
159
+ expect(snapshot).to.include('earlier entries left out');
160
+ expect(snapshot).to.include('step 29');
161
+ expect(snapshot).not.to.include('step 3\n');
162
+ });
163
+
164
+ test('the thread view groups items into lanes and marks overlap, waits and ends', ({ expect }) => {
165
+ const { head, tail, turnA, light, turnB } = parallel();
166
+ const wait = event({ _tag: 'threadWait', on: threadId(turnA) }, assistant, turnB.id);
167
+ const running = Reducer.run(Reducer.thread, [...head, wait]).state;
168
+ expect(
169
+ running.lanes.map(({ id, kind, status, overlapped, backgrounded }) => ({
170
+ id,
171
+ kind,
172
+ status,
173
+ overlapped,
174
+ backgrounded,
175
+ })),
176
+ ).to.deep.eq([
177
+ // The newer lane of the same owner sent the first one to the background.
178
+ { id: turnA.id, kind: 'turn', status: 'running', overlapped: true, backgrounded: true },
179
+ { id: light.id, kind: 'light', status: 'waiting', overlapped: true, backgrounded: undefined },
180
+ ]);
181
+ expect(running.lanes[1].waitingOn).to.eq(turnA.id);
182
+ expect(running.items.map((item) => item.lane)).to.deep.eq([turnA.id, turnA.id, light.id]);
183
+
184
+ const done = Reducer.run(Reducer.thread, tail, Reducer.run(Reducer.thread, [...head, wait])).state;
185
+ expect(done.lanes.map((lane) => lane.status)).to.deep.eq(['done', 'done']);
186
+ expect(
187
+ done.items
188
+ .filter((item) => item.lane === light.id)
189
+ .flatMap((item) => item.blocks.map((block) => (block._tag === 'text' ? block.text : block._tag))),
190
+ ).to.deep.eq(['What is 17 times 23?', '391.']);
191
+ });
192
+
193
+ test("another owner's lane does not send a lane to the background", ({ expect }) => {
194
+ const prompt = message('plan it', user);
195
+ const turn = event({ _tag: 'turnBegin', from: { event: prompt.id } });
196
+ const other = event(
197
+ { _tag: 'threadOpen', mode: 'light', from: { event: turn.id }, siblings: [threadId(turn)] },
198
+ { role: 'assistant', name: 'claude' },
199
+ );
200
+ const { state } = Reducer.run(Reducer.thread, [prompt, turn, other]);
201
+ expect(state.lanes.map((lane) => [lane.overlapped, lane.backgrounded])).to.deep.eq([
202
+ [true, undefined],
203
+ [true, undefined],
204
+ ]);
205
+ });
206
+
207
+ test('a turn that never overlapped is a lane without overlap', ({ expect }) => {
208
+ const prompt = message('hi', user);
209
+ const turn = event({ _tag: 'turnBegin', from: { event: prompt.id } });
210
+ const { state } = Reducer.run(Reducer.thread, [
211
+ prompt,
212
+ turn,
213
+ event({ _tag: 'promptConsume', message: prompt.id }, assistant, turn.id),
214
+ message('hello', assistant, turn.id),
215
+ event({ _tag: 'turnEnd', finishReason: 'stop' }, assistant, turn.id),
216
+ ]);
217
+ expect(state.lanes).to.have.length(1);
218
+ expect(state.lanes[0].overlapped).to.eq(undefined);
219
+ });
220
+
221
+ test('checkpoints round-trip through the codecs', ({ expect }) => {
222
+ const { head } = parallel();
223
+ const prompt = Reducer.run(Reducer.prompt, head).state;
224
+ const thread = Reducer.run(Reducer.thread, head).state;
225
+ const decodedPrompt = Schema.decodeUnknownSync(Reducer.prompt.schema)(
226
+ Schema.encodeSync(Reducer.prompt.schema)(prompt),
227
+ );
228
+ const decodedThread = Schema.decodeUnknownSync(Reducer.thread.schema)(
229
+ Schema.encodeSync(Reducer.thread.schema)(thread),
230
+ );
231
+ expect(texts(Reducer.promptFor(decodedPrompt, threadId(head[6])))).to.deep.eq(
232
+ texts(Reducer.promptFor(prompt, threadId(head[6]))),
233
+ );
234
+ expect(decodedThread.lanes).to.deep.eq(thread.lanes);
235
+ });
236
+ });
237
+
238
+ /**
239
+ * Turn A on main delegates a task to subagent S, a fresh thread on main whose task is queued in it; S runs one turn
240
+ * and merges its final answer into main as a summary.
241
+ */
242
+ const delegation = () => {
243
+ const prompt = message('Compare the three libraries.', user);
244
+ const turnA = event({ _tag: 'turnBegin', from: { event: prompt.id } });
245
+ const consumeA = event({ _tag: 'promptConsume', message: prompt.id }, assistant, turnA.id);
246
+ const contextA = message('I will ask a subagent about the first one.', assistant, turnA.id);
247
+ const subagent = event(
248
+ { _tag: 'threadOpen', mode: 'fresh', purpose: 'Research effect' },
249
+ { role: 'assistant', name: 'subagent' },
250
+ );
251
+ const task = message('Research effect.', { role: 'user', name: 'task' }, subagent.id);
252
+ const turnS = event({ _tag: 'turnBegin', from: { thread: Trajectory.ThreadId.make(subagent.id), event: task.id } });
253
+ const consumeS = event({ _tag: 'promptConsume', message: task.id }, assistant, turnS.id);
254
+ const workS = message('Reading the docs.', assistant, turnS.id);
255
+ const answerS = message('Effect is a typed effect system.', assistant, turnS.id);
256
+ const endS = event({ _tag: 'turnEnd', finishReason: 'stop' }, assistant, turnS.id);
257
+ const merge = event(
258
+ {
259
+ _tag: 'threadMerge',
260
+ mode: 'summary',
261
+ summary: text('Subagent "Research effect" finished: Effect is a typed effect system.'),
262
+ },
263
+ assistant,
264
+ subagent.id,
265
+ );
266
+ return {
267
+ turnA,
268
+ turnS,
269
+ subagent,
270
+ running: [prompt, turnA, consumeA, contextA, subagent, task, turnS, consumeS, workS],
271
+ rest: [answerS, endS, merge],
272
+ };
273
+ };
274
+
275
+ describe('delegated subagents', () => {
276
+ test('a subagent sees only its task, and only its summary joins main', ({ expect }) => {
277
+ const { running, rest, turnS, subagent } = delegation();
278
+ const during = Reducer.run(Reducer.prompt, running).state;
279
+ expect(texts(Reducer.promptFor(during, threadId(turnS)))).to.deep.eq(['Research effect.', 'Reading the docs.']);
280
+
281
+ const after = Reducer.run(Reducer.prompt, rest, Reducer.run(Reducer.prompt, running)).state;
282
+ expect(texts(after.entries)).to.deep.eq(['Subagent "Research effect" finished: Effect is a typed effect system.']);
283
+ expect(after.threads[threadId(subagent)]).to.eq(undefined);
284
+ });
285
+
286
+ test('a subagent is a lane of its own that starts in the background', ({ expect }) => {
287
+ const { running, rest, turnA, subagent } = delegation();
288
+ const during = Reducer.run(Reducer.thread, running).state;
289
+ expect(
290
+ during.lanes.map(({ id, kind, status, overlapped, backgrounded }) => ({
291
+ id,
292
+ kind,
293
+ status,
294
+ overlapped,
295
+ backgrounded,
296
+ })),
297
+ ).to.deep.eq([
298
+ // Another owner's lane leaves the delegating turn in view.
299
+ { id: turnA.id, kind: 'turn', status: 'running', overlapped: true, backgrounded: undefined },
300
+ { id: subagent.id, kind: 'fresh', status: 'running', overlapped: true, backgrounded: true },
301
+ ]);
302
+ expect(during.items.filter((item) => item.lane === subagent.id).map((item) => item.status)).to.deep.eq([
303
+ 'consumed',
304
+ 'sent',
305
+ ]);
306
+
307
+ const done = Reducer.run(Reducer.thread, rest, Reducer.run(Reducer.thread, running)).state;
308
+ expect(done.lanes[1].status).to.eq('done');
309
+ });
310
+ });
@@ -0,0 +1,285 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { describe, test } from 'vitest';
6
+
7
+ import { DXN, Obj } from '@dxos/echo';
8
+ import { type ContentBlock } from '@dxos/types';
9
+
10
+ import * as Reducer from './Reducer.ts';
11
+ import * as Trajectory from './Trajectory.ts';
12
+
13
+ // Keep in sync with README.md, which shows these sequences and the outputs asserted here.
14
+
15
+ const alice: Trajectory.Sender = { role: 'user', name: 'alice' };
16
+ const agent: Trajectory.Sender = { role: 'assistant' };
17
+ const tool: Trajectory.Sender = { role: 'tool' };
18
+ const alarm: Trajectory.Sender = { role: 'event', name: 'alarm' };
19
+ const model = DXN.make('com.anthropic.model.claude-sonnet-5.default');
20
+
21
+ const text = (value: string): ContentBlock.Any[] => [{ _tag: 'text', text: value }];
22
+
23
+ /** An opener as `Feed.fork` writes it: the base of a branch of its own, which takes the opener's id here. */
24
+ const opener = (props: Omit<Trajectory.MakeProps, 'id' | 'thread'>): Trajectory.Event => {
25
+ const id = Obj.ID.random();
26
+ return Trajectory.make({ ...props, id, thread: Trajectory.ThreadId.make(id) });
27
+ };
28
+
29
+ /** The thread an opener started. */
30
+ const threadId = (event: Trajectory.Event): Trajectory.ThreadId => Trajectory.ThreadId.make(event.id);
31
+
32
+ /** Renders prompt entries as `role: text` lines, the form README.md uses. */
33
+ const lines = (entries: readonly Reducer.PromptEntry[]) =>
34
+ entries.map(
35
+ (entry) =>
36
+ `${entry.role}: ${entry.blocks.map((block) => (block._tag === 'text' ? block.text : block._tag)).join(' ')}`,
37
+ );
38
+
39
+ describe('samples', () => {
40
+ test('1. a chat turn with a tool call and a prompt typed mid-turn', ({ expect }) => {
41
+ const question = Trajectory.make({
42
+ payload: { _tag: 'message', blocks: text('What is the weather in Oslo?') },
43
+ sender: alice,
44
+ });
45
+ const turn = opener({
46
+ payload: { _tag: 'turnBegin', model, from: { event: question.id } },
47
+ sender: agent,
48
+ });
49
+ const followUp = Trajectory.make({ payload: { _tag: 'message', blocks: text('And in Bergen?') }, sender: alice });
50
+ // Every event the turn produces carries `thread: turn.id`; #5 is typed by alice on the main thread meanwhile.
51
+ const end = Trajectory.make({
52
+ payload: { _tag: 'turnEnd', finishReason: 'stop', usage: { inputTokens: 900, cacheReadTokens: 0 } },
53
+ sender: agent,
54
+ thread: threadId(turn),
55
+ });
56
+ const events = [
57
+ question,
58
+ turn,
59
+ Trajectory.make({
60
+ payload: { _tag: 'promptConsume', message: question.id },
61
+ sender: agent,
62
+ thread: threadId(turn),
63
+ }),
64
+ Trajectory.make({
65
+ payload: {
66
+ _tag: 'message',
67
+ blocks: [
68
+ {
69
+ _tag: 'toolCall',
70
+ toolCallId: 'call-1',
71
+ name: 'weather',
72
+ input: '{"city":"Oslo"}',
73
+ providerExecuted: false,
74
+ },
75
+ ],
76
+ },
77
+ sender: agent,
78
+ thread: threadId(turn),
79
+ }),
80
+ followUp,
81
+ Trajectory.make({
82
+ payload: {
83
+ _tag: 'message',
84
+ blocks: [
85
+ {
86
+ _tag: 'toolResult',
87
+ toolCallId: 'call-1',
88
+ name: 'weather',
89
+ result: '12°C, rain',
90
+ providerExecuted: false,
91
+ },
92
+ ],
93
+ },
94
+ sender: tool,
95
+ thread: threadId(turn),
96
+ }),
97
+ Trajectory.make({
98
+ payload: { _tag: 'message', blocks: text('Oslo is 12°C with rain.') },
99
+ sender: agent,
100
+ thread: threadId(turn),
101
+ }),
102
+ ];
103
+
104
+ // While the turn runs, main is untouched and the turn sees main plus its own thread.
105
+ const during = Reducer.run(Reducer.prompt, events);
106
+ expect(lines(during.state.entries)).to.deep.eq([]);
107
+ expect(lines(Reducer.promptFor(during.state, threadId(turn)))).to.deep.eq([
108
+ 'user: What is the weather in Oslo?',
109
+ 'assistant: toolCall',
110
+ 'tool: toolResult',
111
+ 'assistant: Oslo is 12°C with rain.',
112
+ ]);
113
+
114
+ const afterTurn = Reducer.run(Reducer.prompt, [end], during);
115
+ expect(lines(afterTurn.state.entries)).to.deep.eq(lines(Reducer.promptFor(during.state, threadId(turn))));
116
+
117
+ const session = Reducer.run(Reducer.session, [...events, end]).state;
118
+ expect(session.queue).to.deep.eq([followUp.id]);
119
+ expect(session.turns).to.deep.eq([]);
120
+
121
+ // The next turn starts from what main showed after #8 and picks up the queued prompt; the earlier entries are
122
+ // untouched, so the provider cache hits.
123
+ const nextTurn = opener({
124
+ payload: { _tag: 'turnBegin', model, from: { event: end.id } },
125
+ sender: agent,
126
+ });
127
+ const next = Trajectory.make({
128
+ payload: { _tag: 'promptConsume', message: followUp.id },
129
+ sender: agent,
130
+ thread: threadId(nextTurn),
131
+ });
132
+ const nextPrompt = Reducer.run(Reducer.prompt, [nextTurn, next], afterTurn);
133
+ expect(lines(Reducer.promptFor(nextPrompt.state, threadId(nextTurn)))).to.deep.eq([
134
+ ...lines(afterTurn.state.entries),
135
+ 'user: And in Bergen?',
136
+ ]);
137
+
138
+ const thread = Reducer.run(Reducer.thread, [...events, end, nextTurn, next]).state;
139
+ expect(
140
+ thread.items.map((item) => `${item.sender.role}:${item.status}:${item.thread === turn.id ? 1 : 2}`),
141
+ ).to.deep.eq(['user:consumed:1', 'assistant:sent:1', 'tool:sent:1', 'assistant:sent:1', 'user:consumed:2']);
142
+ });
143
+
144
+ test('2. an alarm, a subagent thread and a compaction', ({ expect }) => {
145
+ const request = Trajectory.make({
146
+ payload: { _tag: 'message', blocks: text('Watch CI and tell me what fails.') },
147
+ sender: alice,
148
+ });
149
+ const set = Trajectory.make({
150
+ payload: { _tag: 'alarmSet', wakeAt: 1_800_000, message: 'check CI' },
151
+ sender: agent,
152
+ });
153
+ const subagent = opener({
154
+ payload: { _tag: 'threadOpen', mode: 'fresh', purpose: 'triage failures' },
155
+ sender: agent,
156
+ });
157
+ const notice = Trajectory.make({
158
+ payload: { _tag: 'message', blocks: text('CI finished: 2 failures.') },
159
+ sender: alarm,
160
+ });
161
+ const events = [
162
+ request,
163
+ Trajectory.make({ payload: { _tag: 'promptConsume', message: request.id }, sender: agent }),
164
+ set,
165
+ subagent,
166
+ Trajectory.make({
167
+ payload: { _tag: 'message', blocks: text('Reading the failing logs.') },
168
+ sender: agent,
169
+ thread: threadId(subagent),
170
+ }),
171
+ Trajectory.make({ payload: { _tag: 'alarmFire', alarm: set.id }, sender: alarm }),
172
+ notice,
173
+ Trajectory.make({
174
+ payload: {
175
+ _tag: 'threadMerge',
176
+ mode: 'summary',
177
+ summary: text('Both failures are a flaky echo test.'),
178
+ },
179
+ sender: agent,
180
+ thread: threadId(subagent),
181
+ }),
182
+ ];
183
+
184
+ const prompt = Reducer.run(Reducer.prompt, events);
185
+ expect(lines(prompt.state.entries)).to.deep.eq([
186
+ 'user: Watch CI and tell me what fails.',
187
+ 'event: CI finished: 2 failures.',
188
+ 'event: Both failures are a flaky echo test.',
189
+ ]);
190
+
191
+ const session = Reducer.run(Reducer.session, events).state;
192
+ expect(session.alarms).to.deep.eq([]);
193
+ expect(session.threads).to.deep.eq([{ id: subagent.id, mode: 'fresh', status: 'merged' }]);
194
+
195
+ const compacted = Reducer.run(
196
+ Reducer.prompt,
197
+ [
198
+ Trajectory.make({
199
+ payload: {
200
+ _tag: 'compact',
201
+ from: request.id,
202
+ to: notice.id,
203
+ summary: text('Alice asked to watch CI; it finished with 2 failures.'),
204
+ },
205
+ sender: agent,
206
+ }),
207
+ ],
208
+ prompt,
209
+ );
210
+ expect(lines(compacted.state.entries)).to.deep.eq([
211
+ 'event: Alice asked to watch CI; it finished with 2 failures.',
212
+ 'event: Both failures are a flaky echo test.',
213
+ ]);
214
+ });
215
+
216
+ test('3. two harnesses race for one prompt while a notice syncs in', ({ expect }) => {
217
+ const laptop: Trajectory.Sender = { role: 'assistant', name: 'laptop' };
218
+ const edge: Trajectory.Sender = { role: 'assistant', name: 'edge' };
219
+ const question = Trajectory.make({
220
+ payload: { _tag: 'message', blocks: text('Summarise the design doc.') },
221
+ sender: alice,
222
+ });
223
+ // Both harnesses see #1 as their feed head and start a turn.
224
+ const laptopTurn = opener({
225
+ payload: { _tag: 'turnBegin', model, from: { event: question.id } },
226
+ sender: laptop,
227
+ });
228
+ const edgeTurn = opener({
229
+ payload: { _tag: 'turnBegin', model, from: { event: question.id } },
230
+ sender: edge,
231
+ });
232
+ const won = Trajectory.make({
233
+ payload: { _tag: 'promptConsume', message: question.id },
234
+ sender: laptop,
235
+ thread: threadId(laptopTurn),
236
+ });
237
+ const lost = Trajectory.make({
238
+ payload: { _tag: 'promptConsume', message: question.id },
239
+ sender: edge,
240
+ thread: threadId(edgeTurn),
241
+ });
242
+ const notice = Trajectory.make({ payload: { _tag: 'message', blocks: text('CI is green.') }, sender: alarm });
243
+ const events = [
244
+ question,
245
+ laptopTurn,
246
+ edgeTurn,
247
+ won,
248
+ lost,
249
+ // #5 folds after #4, so the edge harness lost the claim and ends its turn without calling the model.
250
+ Trajectory.make({
251
+ payload: { _tag: 'turnEnd', finishReason: 'other', error: 'prompt claimed by laptop' },
252
+ sender: edge,
253
+ thread: threadId(edgeTurn),
254
+ }),
255
+ notice,
256
+ Trajectory.make({
257
+ payload: { _tag: 'message', blocks: text('It replaces messages with one event log.') },
258
+ sender: laptop,
259
+ thread: threadId(laptopTurn),
260
+ }),
261
+ Trajectory.make({
262
+ payload: { _tag: 'turnEnd', finishReason: 'stop' },
263
+ sender: laptop,
264
+ thread: threadId(laptopTurn),
265
+ }),
266
+ ];
267
+
268
+ const session = Reducer.run(Reducer.session, events).state;
269
+ expect(session.rejected).to.deep.eq([lost.id]);
270
+ expect(session.turns).to.deep.eq([]);
271
+
272
+ const prompt = Reducer.run(Reducer.prompt, events).state;
273
+ expect(lines(prompt.entries)).to.deep.eq([
274
+ 'event: CI is green.',
275
+ 'user: Summarise the design doc.',
276
+ 'assistant: It replaces messages with one event log.',
277
+ ]);
278
+ // The laptop's turn ran without the notice that landed in main meanwhile; its merge is flagged, not undone.
279
+ expect(prompt.stale).to.deep.eq({ [laptopTurn.id]: [notice.id] });
280
+
281
+ const thread = Reducer.run(Reducer.thread, events).state;
282
+ // The question shows once, where the laptop consumed it.
283
+ expect(thread.items.map((item) => item.thread)).to.deep.eq([laptopTurn.id, undefined, laptopTurn.id]);
284
+ });
285
+ });
@@ -23,11 +23,12 @@ const TestObject = Type.makeObject(DXN.make('org.dxos.type.testObject', '0.1.0')
23
23
  describe('Project', () => {
24
24
  test('typename, version, and defaults', ({ expect }) => {
25
25
  expect(Type.getTypename(Project.Project)).toBe('org.dxos.type.project');
26
- expect(Type.getVersion(Project.Project)).toBe('0.6.0');
26
+ expect(Type.getVersion(Project.Project)).toBe('0.8.0');
27
27
  const project = Project.make({ name: 'test' });
28
28
  expect(Obj.instanceOf(Project.Project, project)).toBe(true);
29
29
  expect(project.artifacts).toEqual([]);
30
30
  expect(project.routines).toEqual([]);
31
+ expect(project.specs).toEqual([]);
31
32
  });
32
33
 
33
34
  test('addRoutine links a routine by ref and parents it for cascade', ({ expect }) => {