@vellumai/assistant 0.5.2 → 0.5.4

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 (144) hide show
  1. package/ARCHITECTURE.md +109 -0
  2. package/docs/architecture/memory.md +105 -0
  3. package/docs/skills.md +100 -0
  4. package/package.json +1 -1
  5. package/src/__tests__/archive-recall.test.ts +560 -0
  6. package/src/__tests__/conversation-agent-loop-overflow.test.ts +7 -0
  7. package/src/__tests__/conversation-agent-loop.test.ts +7 -0
  8. package/src/__tests__/conversation-clear-safety.test.ts +259 -0
  9. package/src/__tests__/conversation-memory-dirty-tail.test.ts +150 -0
  10. package/src/__tests__/conversation-provider-retry-repair.test.ts +7 -0
  11. package/src/__tests__/conversation-switch-memory-reduction.test.ts +474 -0
  12. package/src/__tests__/conversation-wipe.test.ts +226 -0
  13. package/src/__tests__/db-memory-archive-migration.test.ts +372 -0
  14. package/src/__tests__/db-memory-brief-state-migration.test.ts +213 -0
  15. package/src/__tests__/db-memory-reducer-checkpoints.test.ts +273 -0
  16. package/src/__tests__/db-schedule-syntax-migration.test.ts +3 -0
  17. package/src/__tests__/inline-command-runner.test.ts +311 -0
  18. package/src/__tests__/inline-skill-authoring-guard.test.ts +220 -0
  19. package/src/__tests__/inline-skill-load-permissions.test.ts +435 -0
  20. package/src/__tests__/list-messages-attachments.test.ts +96 -0
  21. package/src/__tests__/memory-brief-open-loops.test.ts +530 -0
  22. package/src/__tests__/memory-brief-time.test.ts +285 -0
  23. package/src/__tests__/memory-brief-wrapper.test.ts +311 -0
  24. package/src/__tests__/memory-chunk-archive.test.ts +400 -0
  25. package/src/__tests__/memory-chunk-dual-write.test.ts +453 -0
  26. package/src/__tests__/memory-episode-archive.test.ts +370 -0
  27. package/src/__tests__/memory-episode-dual-write.test.ts +626 -0
  28. package/src/__tests__/memory-observation-archive.test.ts +375 -0
  29. package/src/__tests__/memory-observation-dual-write.test.ts +318 -0
  30. package/src/__tests__/memory-recall-quality.test.ts +2 -2
  31. package/src/__tests__/memory-reducer-job.test.ts +538 -0
  32. package/src/__tests__/memory-reducer-scheduling.test.ts +473 -0
  33. package/src/__tests__/memory-reducer-store.test.ts +728 -0
  34. package/src/__tests__/memory-reducer-types.test.ts +707 -0
  35. package/src/__tests__/memory-reducer.test.ts +704 -0
  36. package/src/__tests__/memory-regressions.test.ts +30 -8
  37. package/src/__tests__/memory-simplified-config.test.ts +281 -0
  38. package/src/__tests__/parse-identity-fields.test.ts +129 -0
  39. package/src/__tests__/simplified-memory-e2e.test.ts +666 -0
  40. package/src/__tests__/simplified-memory-runtime.test.ts +616 -0
  41. package/src/__tests__/skill-load-inline-command.test.ts +598 -0
  42. package/src/__tests__/skill-load-inline-includes.test.ts +644 -0
  43. package/src/__tests__/skills-inline-command-expansions.test.ts +301 -0
  44. package/src/__tests__/skills-transitive-hash.test.ts +333 -0
  45. package/src/__tests__/vellum-self-knowledge-inline-command.test.ts +320 -0
  46. package/src/__tests__/workspace-migration-backfill-installation-id.test.ts +4 -4
  47. package/src/cli/commands/conversations.ts +18 -0
  48. package/src/config/bundled-skills/app-builder/SKILL.md +8 -8
  49. package/src/config/bundled-skills/schedule/TOOLS.json +8 -0
  50. package/src/config/bundled-skills/skill-management/SKILL.md +1 -1
  51. package/src/config/bundled-skills/skill-management/TOOLS.json +2 -2
  52. package/src/config/feature-flag-registry.json +16 -0
  53. package/src/config/raw-config-utils.ts +28 -0
  54. package/src/config/schema.ts +12 -0
  55. package/src/config/schemas/memory-simplified.ts +101 -0
  56. package/src/config/schemas/memory.ts +4 -0
  57. package/src/config/skills.ts +50 -4
  58. package/src/daemon/conversation-agent-loop-handlers.ts +8 -3
  59. package/src/daemon/conversation-agent-loop.ts +71 -1
  60. package/src/daemon/conversation-lifecycle.ts +11 -1
  61. package/src/daemon/conversation-memory.ts +117 -0
  62. package/src/daemon/conversation-runtime-assembly.ts +3 -1
  63. package/src/daemon/conversation-surfaces.ts +31 -8
  64. package/src/daemon/conversation.ts +40 -23
  65. package/src/daemon/handlers/config-embeddings.ts +10 -2
  66. package/src/daemon/handlers/config-model.ts +0 -9
  67. package/src/daemon/handlers/conversations.ts +11 -0
  68. package/src/daemon/handlers/identity.ts +12 -1
  69. package/src/daemon/lifecycle.ts +52 -1
  70. package/src/daemon/message-types/conversations.ts +0 -1
  71. package/src/daemon/server.ts +1 -1
  72. package/src/followups/followup-store.ts +47 -1
  73. package/src/memory/archive-recall.ts +516 -0
  74. package/src/memory/archive-store.ts +400 -0
  75. package/src/memory/brief-formatting.ts +33 -0
  76. package/src/memory/brief-open-loops.ts +266 -0
  77. package/src/memory/brief-time.ts +162 -0
  78. package/src/memory/brief.ts +75 -0
  79. package/src/memory/conversation-crud.ts +455 -101
  80. package/src/memory/conversation-key-store.ts +33 -4
  81. package/src/memory/db-init.ts +16 -0
  82. package/src/memory/indexer.ts +106 -15
  83. package/src/memory/job-handlers/backfill-simplified-memory.ts +462 -0
  84. package/src/memory/job-handlers/conversation-starters.ts +9 -3
  85. package/src/memory/job-handlers/embedding.test.ts +1 -0
  86. package/src/memory/job-handlers/embedding.ts +83 -0
  87. package/src/memory/job-handlers/reduce-conversation-memory.ts +229 -0
  88. package/src/memory/job-utils.ts +1 -1
  89. package/src/memory/jobs-store.ts +8 -0
  90. package/src/memory/jobs-worker.ts +20 -0
  91. package/src/memory/migrations/036-normalize-phone-identities.ts +49 -14
  92. package/src/memory/migrations/135-backfill-contact-interaction-stats.ts +9 -1
  93. package/src/memory/migrations/141-rename-verification-table.ts +8 -0
  94. package/src/memory/migrations/142-rename-verification-session-id-column.ts +7 -2
  95. package/src/memory/migrations/174-rename-thread-starters-table.ts +8 -0
  96. package/src/memory/migrations/185-memory-brief-state.ts +52 -0
  97. package/src/memory/migrations/186-memory-archive.ts +109 -0
  98. package/src/memory/migrations/187-memory-reducer-checkpoints.ts +19 -0
  99. package/src/memory/migrations/188-schedule-quiet-flag.ts +13 -0
  100. package/src/memory/migrations/index.ts +4 -0
  101. package/src/memory/qdrant-client.ts +23 -4
  102. package/src/memory/reducer-scheduler.ts +242 -0
  103. package/src/memory/reducer-store.ts +271 -0
  104. package/src/memory/reducer-types.ts +106 -0
  105. package/src/memory/reducer.ts +467 -0
  106. package/src/memory/schema/conversations.ts +3 -0
  107. package/src/memory/schema/index.ts +2 -0
  108. package/src/memory/schema/infrastructure.ts +1 -0
  109. package/src/memory/schema/memory-archive.ts +121 -0
  110. package/src/memory/schema/memory-brief.ts +55 -0
  111. package/src/memory/search/semantic.ts +17 -4
  112. package/src/oauth/oauth-store.ts +3 -1
  113. package/src/permissions/checker.ts +89 -6
  114. package/src/permissions/defaults.ts +14 -0
  115. package/src/runtime/auth/route-policy.ts +10 -1
  116. package/src/runtime/routes/conversation-management-routes.ts +94 -2
  117. package/src/runtime/routes/conversation-query-routes.ts +7 -0
  118. package/src/runtime/routes/conversation-routes.ts +52 -5
  119. package/src/runtime/routes/guardian-bootstrap-routes.ts +19 -7
  120. package/src/runtime/routes/identity-routes.ts +2 -35
  121. package/src/runtime/routes/llm-context-normalization.ts +14 -1
  122. package/src/runtime/routes/memory-item-routes.ts +90 -5
  123. package/src/runtime/routes/secret-routes.ts +3 -0
  124. package/src/runtime/routes/surface-action-routes.ts +68 -1
  125. package/src/schedule/schedule-store.ts +28 -0
  126. package/src/schedule/scheduler.ts +6 -2
  127. package/src/skills/inline-command-expansions.ts +204 -0
  128. package/src/skills/inline-command-render.ts +127 -0
  129. package/src/skills/inline-command-runner.ts +242 -0
  130. package/src/skills/transitive-version-hash.ts +88 -0
  131. package/src/tasks/task-store.ts +43 -1
  132. package/src/telemetry/usage-telemetry-reporter.ts +1 -1
  133. package/src/tools/filesystem/edit.ts +6 -1
  134. package/src/tools/filesystem/read.ts +6 -1
  135. package/src/tools/filesystem/write.ts +6 -1
  136. package/src/tools/memory/handlers.ts +129 -1
  137. package/src/tools/permission-checker.ts +8 -1
  138. package/src/tools/schedule/create.ts +3 -0
  139. package/src/tools/schedule/list.ts +5 -1
  140. package/src/tools/schedule/update.ts +6 -0
  141. package/src/tools/skills/load.ts +140 -6
  142. package/src/util/platform.ts +18 -0
  143. package/src/workspace/migrations/{002-backfill-installation-id.ts → 011-backfill-installation-id.ts} +1 -1
  144. package/src/workspace/migrations/registry.ts +1 -1
@@ -0,0 +1,704 @@
1
+ import { beforeEach, describe, expect, mock, test } from "bun:test";
2
+
3
+ import type { Provider, ProviderResponse } from "../providers/types.js";
4
+
5
+ // ---------------------------------------------------------------------------
6
+ // Mocks — declared before imports that depend on them
7
+ // ---------------------------------------------------------------------------
8
+
9
+ function makeLoggerStub(): Record<string, unknown> {
10
+ const stub: Record<string, unknown> = {};
11
+ for (const m of [
12
+ "info",
13
+ "warn",
14
+ "error",
15
+ "debug",
16
+ "trace",
17
+ "fatal",
18
+ "silent",
19
+ "child",
20
+ ]) {
21
+ stub[m] = m === "child" ? () => makeLoggerStub() : () => {};
22
+ }
23
+ return stub;
24
+ }
25
+
26
+ mock.module("../util/logger.js", () => ({
27
+ getLogger: () => makeLoggerStub(),
28
+ }));
29
+
30
+ // ---------------------------------------------------------------------------
31
+ // Mock provider
32
+ // ---------------------------------------------------------------------------
33
+
34
+ const mockSendMessage = mock<Provider["sendMessage"]>();
35
+ const mockProvider: Provider = {
36
+ name: "mock-reducer-provider",
37
+ sendMessage: mockSendMessage,
38
+ };
39
+
40
+ let providerAvailable = true;
41
+
42
+ mock.module("../providers/provider-send-message.js", () => ({
43
+ getConfiguredProvider: async () => (providerAvailable ? mockProvider : null),
44
+ createTimeout: (ms: number) => {
45
+ const controller = new AbortController();
46
+ const timer = setTimeout(() => controller.abort(), ms);
47
+ return {
48
+ signal: controller.signal,
49
+ cleanup: () => clearTimeout(timer),
50
+ };
51
+ },
52
+ extractText: (response: ProviderResponse) => {
53
+ const block = response.content.find(
54
+ (b): b is Extract<(typeof response.content)[number], { type: "text" }> =>
55
+ b.type === "text",
56
+ );
57
+ return block?.text?.trim() ?? "";
58
+ },
59
+ }));
60
+
61
+ // ---------------------------------------------------------------------------
62
+ // Import module under test AFTER mocks
63
+ // ---------------------------------------------------------------------------
64
+
65
+ import {
66
+ buildReducerSystemPrompt,
67
+ buildReducerUserMessage,
68
+ type ReducerPromptInput,
69
+ runReducer,
70
+ } from "../memory/reducer.js";
71
+ import { EMPTY_REDUCER_RESULT } from "../memory/reducer-types.js";
72
+
73
+ // ---------------------------------------------------------------------------
74
+ // Helpers
75
+ // ---------------------------------------------------------------------------
76
+
77
+ function makeInput(
78
+ overrides: Partial<ReducerPromptInput> = {},
79
+ ): ReducerPromptInput {
80
+ return {
81
+ conversationId: "conv-test-1",
82
+ newMessages: [
83
+ { role: "user", content: "I'm traveling to Paris next week." },
84
+ {
85
+ role: "assistant",
86
+ content: "That sounds exciting! Do you need help planning?",
87
+ },
88
+ ],
89
+ existingTimeContexts: [],
90
+ existingOpenLoops: [],
91
+ nowMs: 1700000000000,
92
+ scopeId: "scope-default",
93
+ ...overrides,
94
+ };
95
+ }
96
+
97
+ function makeProviderResponse(jsonOutput: unknown): ProviderResponse {
98
+ return {
99
+ content: [{ type: "text", text: JSON.stringify(jsonOutput) }],
100
+ model: "mock-model",
101
+ usage: { inputTokens: 100, outputTokens: 50 },
102
+ stopReason: "end_turn",
103
+ };
104
+ }
105
+
106
+ // ---------------------------------------------------------------------------
107
+ // Tests: buildReducerSystemPrompt
108
+ // ---------------------------------------------------------------------------
109
+
110
+ describe("buildReducerSystemPrompt", () => {
111
+ test("contains key structural instructions", () => {
112
+ const prompt = buildReducerSystemPrompt();
113
+ expect(prompt).toContain("timeContexts");
114
+ expect(prompt).toContain("openLoops");
115
+ expect(prompt).toContain("archiveObservations");
116
+ expect(prompt).toContain("archiveEpisodes");
117
+ expect(prompt).toContain("JSON");
118
+ });
119
+ });
120
+
121
+ // ---------------------------------------------------------------------------
122
+ // Tests: buildReducerUserMessage
123
+ // ---------------------------------------------------------------------------
124
+
125
+ describe("buildReducerUserMessage", () => {
126
+ test("includes current time, conversation ID, and scope", () => {
127
+ const input = makeInput();
128
+ const msg = buildReducerUserMessage(input);
129
+ expect(msg).toContain("conv-test-1");
130
+ expect(msg).toContain("scope-default");
131
+ expect(msg).toContain("1700000000000");
132
+ });
133
+
134
+ test("includes new messages", () => {
135
+ const input = makeInput();
136
+ const msg = buildReducerUserMessage(input);
137
+ expect(msg).toContain("[user]: I'm traveling to Paris next week.");
138
+ expect(msg).toContain("[assistant]: That sounds exciting!");
139
+ });
140
+
141
+ test("includes existing time contexts when provided", () => {
142
+ const input = makeInput({
143
+ existingTimeContexts: [
144
+ { id: "tc-1", summary: "User on vacation until Friday" },
145
+ ],
146
+ });
147
+ const msg = buildReducerUserMessage(input);
148
+ expect(msg).toContain("Active time contexts");
149
+ expect(msg).toContain("[tc-1] User on vacation until Friday");
150
+ });
151
+
152
+ test("includes existing open loops when provided", () => {
153
+ const input = makeInput({
154
+ existingOpenLoops: [
155
+ { id: "ol-1", summary: "Follow up with Bob", status: "open" },
156
+ ],
157
+ });
158
+ const msg = buildReducerUserMessage(input);
159
+ expect(msg).toContain("Active open loops");
160
+ expect(msg).toContain("[ol-1] (open) Follow up with Bob");
161
+ });
162
+
163
+ test("omits time context section when none exist", () => {
164
+ const input = makeInput({ existingTimeContexts: [] });
165
+ const msg = buildReducerUserMessage(input);
166
+ expect(msg).not.toContain("Active time contexts");
167
+ });
168
+
169
+ test("omits open loop section when none exist", () => {
170
+ const input = makeInput({ existingOpenLoops: [] });
171
+ const msg = buildReducerUserMessage(input);
172
+ expect(msg).not.toContain("Active open loops");
173
+ });
174
+ });
175
+
176
+ // ---------------------------------------------------------------------------
177
+ // Tests: runReducer — event extraction
178
+ // ---------------------------------------------------------------------------
179
+
180
+ describe("runReducer — event extraction", () => {
181
+ beforeEach(() => {
182
+ mockSendMessage.mockClear();
183
+ providerAvailable = true;
184
+ });
185
+
186
+ test("extracts archive observations from provider response", async () => {
187
+ mockSendMessage.mockResolvedValueOnce(
188
+ makeProviderResponse({
189
+ archiveObservations: [
190
+ {
191
+ content: "User is planning a trip to Paris",
192
+ role: "user",
193
+ modality: "text",
194
+ source: "vellum",
195
+ },
196
+ {
197
+ content: "User prefers window seats on flights",
198
+ role: "user",
199
+ },
200
+ ],
201
+ }),
202
+ );
203
+
204
+ const result = await runReducer(makeInput());
205
+
206
+ expect(result.archiveObservations).toHaveLength(2);
207
+ expect(result.archiveObservations[0]).toEqual({
208
+ content: "User is planning a trip to Paris",
209
+ role: "user",
210
+ modality: "text",
211
+ source: "vellum",
212
+ });
213
+ expect(result.archiveObservations[1]).toEqual({
214
+ content: "User prefers window seats on flights",
215
+ role: "user",
216
+ });
217
+ });
218
+
219
+ test("extracts archive episodes from provider response", async () => {
220
+ mockSendMessage.mockResolvedValueOnce(
221
+ makeProviderResponse({
222
+ archiveEpisodes: [
223
+ {
224
+ title: "Paris trip planning",
225
+ summary:
226
+ "User discussed upcoming trip to Paris, mentioned interest in museums.",
227
+ source: "vellum",
228
+ },
229
+ ],
230
+ }),
231
+ );
232
+
233
+ const result = await runReducer(makeInput());
234
+
235
+ expect(result.archiveEpisodes).toHaveLength(1);
236
+ expect(result.archiveEpisodes[0]).toEqual({
237
+ title: "Paris trip planning",
238
+ summary:
239
+ "User discussed upcoming trip to Paris, mentioned interest in museums.",
240
+ source: "vellum",
241
+ });
242
+ });
243
+ });
244
+
245
+ // ---------------------------------------------------------------------------
246
+ // Tests: runReducer — temporary situation extraction
247
+ // ---------------------------------------------------------------------------
248
+
249
+ describe("runReducer — temporary situation extraction", () => {
250
+ beforeEach(() => {
251
+ mockSendMessage.mockClear();
252
+ providerAvailable = true;
253
+ });
254
+
255
+ test("extracts time context create operations", async () => {
256
+ mockSendMessage.mockResolvedValueOnce(
257
+ makeProviderResponse({
258
+ timeContexts: [
259
+ {
260
+ action: "create",
261
+ summary: "User traveling to Paris next week",
262
+ source: "conversation",
263
+ activeFrom: 1700000000000,
264
+ activeUntil: 1700604800000,
265
+ },
266
+ ],
267
+ }),
268
+ );
269
+
270
+ const result = await runReducer(makeInput());
271
+
272
+ expect(result.timeContexts).toHaveLength(1);
273
+ expect(result.timeContexts[0]).toEqual({
274
+ action: "create",
275
+ summary: "User traveling to Paris next week",
276
+ source: "conversation",
277
+ activeFrom: 1700000000000,
278
+ activeUntil: 1700604800000,
279
+ });
280
+ });
281
+
282
+ test("extracts time context update operations referencing existing IDs", async () => {
283
+ mockSendMessage.mockResolvedValueOnce(
284
+ makeProviderResponse({
285
+ timeContexts: [
286
+ {
287
+ action: "update",
288
+ id: "tc-existing-1",
289
+ summary: "Trip extended to two weeks",
290
+ activeUntil: 1701209600000,
291
+ },
292
+ ],
293
+ }),
294
+ );
295
+
296
+ const result = await runReducer(
297
+ makeInput({
298
+ existingTimeContexts: [
299
+ { id: "tc-existing-1", summary: "User traveling next week" },
300
+ ],
301
+ }),
302
+ );
303
+
304
+ expect(result.timeContexts).toHaveLength(1);
305
+ expect(result.timeContexts[0]).toEqual({
306
+ action: "update",
307
+ id: "tc-existing-1",
308
+ summary: "Trip extended to two weeks",
309
+ activeUntil: 1701209600000,
310
+ });
311
+ });
312
+ });
313
+
314
+ // ---------------------------------------------------------------------------
315
+ // Tests: runReducer — open-loop creation
316
+ // ---------------------------------------------------------------------------
317
+
318
+ describe("runReducer — open-loop creation", () => {
319
+ beforeEach(() => {
320
+ mockSendMessage.mockClear();
321
+ providerAvailable = true;
322
+ });
323
+
324
+ test("extracts open loop create operations", async () => {
325
+ mockSendMessage.mockResolvedValueOnce(
326
+ makeProviderResponse({
327
+ openLoops: [
328
+ {
329
+ action: "create",
330
+ summary: "Book hotel in Paris",
331
+ source: "conversation",
332
+ dueAt: 1700172800000,
333
+ },
334
+ ],
335
+ }),
336
+ );
337
+
338
+ const result = await runReducer(makeInput());
339
+
340
+ expect(result.openLoops).toHaveLength(1);
341
+ expect(result.openLoops[0]).toEqual({
342
+ action: "create",
343
+ summary: "Book hotel in Paris",
344
+ source: "conversation",
345
+ dueAt: 1700172800000,
346
+ });
347
+ });
348
+
349
+ test("extracts open loop create without optional dueAt", async () => {
350
+ mockSendMessage.mockResolvedValueOnce(
351
+ makeProviderResponse({
352
+ openLoops: [
353
+ {
354
+ action: "create",
355
+ summary: "Research museums in Paris",
356
+ source: "conversation",
357
+ },
358
+ ],
359
+ }),
360
+ );
361
+
362
+ const result = await runReducer(makeInput());
363
+
364
+ expect(result.openLoops).toHaveLength(1);
365
+ const op = result.openLoops[0];
366
+ expect(op.action).toBe("create");
367
+ if (op.action === "create") {
368
+ expect(op.summary).toBe("Research museums in Paris");
369
+ expect(op.dueAt).toBeUndefined();
370
+ }
371
+ });
372
+ });
373
+
374
+ // ---------------------------------------------------------------------------
375
+ // Tests: runReducer — explicit resolution
376
+ // ---------------------------------------------------------------------------
377
+
378
+ describe("runReducer — explicit resolution", () => {
379
+ beforeEach(() => {
380
+ mockSendMessage.mockClear();
381
+ providerAvailable = true;
382
+ });
383
+
384
+ test("extracts time context resolve operations", async () => {
385
+ mockSendMessage.mockResolvedValueOnce(
386
+ makeProviderResponse({
387
+ timeContexts: [
388
+ {
389
+ action: "resolve",
390
+ id: "tc-old-1",
391
+ },
392
+ ],
393
+ }),
394
+ );
395
+
396
+ const result = await runReducer(
397
+ makeInput({
398
+ existingTimeContexts: [
399
+ { id: "tc-old-1", summary: "Conference this week" },
400
+ ],
401
+ }),
402
+ );
403
+
404
+ expect(result.timeContexts).toHaveLength(1);
405
+ expect(result.timeContexts[0]).toEqual({
406
+ action: "resolve",
407
+ id: "tc-old-1",
408
+ });
409
+ });
410
+
411
+ test("extracts open loop resolve with 'resolved' status", async () => {
412
+ mockSendMessage.mockResolvedValueOnce(
413
+ makeProviderResponse({
414
+ openLoops: [
415
+ {
416
+ action: "resolve",
417
+ id: "ol-done-1",
418
+ status: "resolved",
419
+ },
420
+ ],
421
+ }),
422
+ );
423
+
424
+ const result = await runReducer(
425
+ makeInput({
426
+ existingOpenLoops: [
427
+ { id: "ol-done-1", summary: "Send report to Alice", status: "open" },
428
+ ],
429
+ }),
430
+ );
431
+
432
+ expect(result.openLoops).toHaveLength(1);
433
+ expect(result.openLoops[0]).toEqual({
434
+ action: "resolve",
435
+ id: "ol-done-1",
436
+ status: "resolved",
437
+ });
438
+ });
439
+
440
+ test("extracts open loop resolve with 'expired' status", async () => {
441
+ mockSendMessage.mockResolvedValueOnce(
442
+ makeProviderResponse({
443
+ openLoops: [
444
+ {
445
+ action: "resolve",
446
+ id: "ol-expired-1",
447
+ status: "expired",
448
+ },
449
+ ],
450
+ }),
451
+ );
452
+
453
+ const result = await runReducer(
454
+ makeInput({
455
+ existingOpenLoops: [
456
+ {
457
+ id: "ol-expired-1",
458
+ summary: "RSVP deadline passed",
459
+ status: "open",
460
+ },
461
+ ],
462
+ }),
463
+ );
464
+
465
+ expect(result.openLoops).toHaveLength(1);
466
+ expect(result.openLoops[0]).toEqual({
467
+ action: "resolve",
468
+ id: "ol-expired-1",
469
+ status: "expired",
470
+ });
471
+ });
472
+ });
473
+
474
+ // ---------------------------------------------------------------------------
475
+ // Tests: runReducer — provider unavailable / error handling
476
+ // ---------------------------------------------------------------------------
477
+
478
+ describe("runReducer — error handling", () => {
479
+ beforeEach(() => {
480
+ mockSendMessage.mockClear();
481
+ providerAvailable = true;
482
+ });
483
+
484
+ test("returns EMPTY_REDUCER_RESULT when no provider is available", async () => {
485
+ providerAvailable = false;
486
+
487
+ const result = await runReducer(makeInput());
488
+
489
+ expect(result).toBe(EMPTY_REDUCER_RESULT);
490
+ expect(mockSendMessage).not.toHaveBeenCalled();
491
+ });
492
+
493
+ test("returns EMPTY_REDUCER_RESULT when provider throws", async () => {
494
+ mockSendMessage.mockRejectedValueOnce(new Error("Provider error"));
495
+
496
+ const result = await runReducer(makeInput());
497
+
498
+ expect(result).toBe(EMPTY_REDUCER_RESULT);
499
+ });
500
+
501
+ test("returns EMPTY_REDUCER_RESULT when provider returns empty text", async () => {
502
+ mockSendMessage.mockResolvedValueOnce({
503
+ content: [{ type: "text", text: "" }],
504
+ model: "mock-model",
505
+ usage: { inputTokens: 10, outputTokens: 0 },
506
+ stopReason: "end_turn",
507
+ });
508
+
509
+ const result = await runReducer(makeInput());
510
+
511
+ expect(result).toBe(EMPTY_REDUCER_RESULT);
512
+ });
513
+
514
+ test("returns EMPTY_REDUCER_RESULT when provider returns invalid JSON", async () => {
515
+ mockSendMessage.mockResolvedValueOnce({
516
+ content: [{ type: "text", text: "not valid json at all" }],
517
+ model: "mock-model",
518
+ usage: { inputTokens: 10, outputTokens: 5 },
519
+ stopReason: "end_turn",
520
+ });
521
+
522
+ const result = await runReducer(makeInput());
523
+
524
+ expect(result).toBe(EMPTY_REDUCER_RESULT);
525
+ });
526
+
527
+ test("returns EMPTY_REDUCER_RESULT when provider returns empty object", async () => {
528
+ mockSendMessage.mockResolvedValueOnce(makeProviderResponse({}));
529
+
530
+ const result = await runReducer(makeInput());
531
+
532
+ expect(result).toEqual({
533
+ timeContexts: [],
534
+ openLoops: [],
535
+ archiveObservations: [],
536
+ archiveEpisodes: [],
537
+ });
538
+ expect(result).not.toBe(EMPTY_REDUCER_RESULT);
539
+ });
540
+ });
541
+
542
+ // ---------------------------------------------------------------------------
543
+ // Tests: runReducer — provider call arguments
544
+ // ---------------------------------------------------------------------------
545
+
546
+ describe("runReducer — provider call arguments", () => {
547
+ beforeEach(() => {
548
+ mockSendMessage.mockClear();
549
+ providerAvailable = true;
550
+ mockSendMessage.mockResolvedValue(makeProviderResponse({}));
551
+ });
552
+
553
+ test("sends messages with correct structure", async () => {
554
+ await runReducer(makeInput());
555
+
556
+ expect(mockSendMessage).toHaveBeenCalledTimes(1);
557
+ const [messages] = mockSendMessage.mock.calls[0] as Parameters<
558
+ Provider["sendMessage"]
559
+ >;
560
+
561
+ // Should be a single user message
562
+ expect(messages).toHaveLength(1);
563
+ expect(messages[0].role).toBe("user");
564
+ expect(messages[0].content).toHaveLength(1);
565
+ expect(messages[0].content[0].type).toBe("text");
566
+ });
567
+
568
+ test("does not send any tool definitions", async () => {
569
+ await runReducer(makeInput());
570
+
571
+ const [, tools] = mockSendMessage.mock.calls[0] as Parameters<
572
+ Provider["sendMessage"]
573
+ >;
574
+ expect(tools).toBeUndefined();
575
+ });
576
+
577
+ test("sends a system prompt", async () => {
578
+ await runReducer(makeInput());
579
+
580
+ const [, , systemPrompt] = mockSendMessage.mock.calls[0] as Parameters<
581
+ Provider["sendMessage"]
582
+ >;
583
+ expect(systemPrompt).toBeTruthy();
584
+ expect(typeof systemPrompt).toBe("string");
585
+ });
586
+
587
+ test("uses latency-optimized model intent", async () => {
588
+ await runReducer(makeInput());
589
+
590
+ const [, , , options] = mockSendMessage.mock.calls[0] as Parameters<
591
+ Provider["sendMessage"]
592
+ >;
593
+ expect(options?.config?.modelIntent).toBe("latency-optimized");
594
+ });
595
+
596
+ test("passes abort signal to provider", async () => {
597
+ await runReducer(makeInput());
598
+
599
+ const [, , , options] = mockSendMessage.mock.calls[0] as Parameters<
600
+ Provider["sendMessage"]
601
+ >;
602
+ expect(options?.signal).toBeDefined();
603
+ expect(options?.signal).toBeInstanceOf(AbortSignal);
604
+ });
605
+ });
606
+
607
+ // ---------------------------------------------------------------------------
608
+ // Tests: runReducer — side-effect-free guarantee
609
+ // ---------------------------------------------------------------------------
610
+
611
+ describe("runReducer — side-effect-free", () => {
612
+ beforeEach(() => {
613
+ mockSendMessage.mockClear();
614
+ providerAvailable = true;
615
+ });
616
+
617
+ test("returns typed result without performing any writes", async () => {
618
+ const fullResponse = {
619
+ timeContexts: [
620
+ {
621
+ action: "create",
622
+ summary: "Paris trip",
623
+ source: "conversation",
624
+ activeFrom: 1700000000000,
625
+ activeUntil: 1700604800000,
626
+ },
627
+ ],
628
+ openLoops: [
629
+ {
630
+ action: "create",
631
+ summary: "Book hotel",
632
+ source: "conversation",
633
+ },
634
+ ],
635
+ archiveObservations: [
636
+ {
637
+ content: "User likes museums",
638
+ role: "user",
639
+ },
640
+ ],
641
+ archiveEpisodes: [
642
+ {
643
+ title: "Trip planning",
644
+ summary: "Discussed Paris trip",
645
+ },
646
+ ],
647
+ };
648
+
649
+ mockSendMessage.mockResolvedValueOnce(makeProviderResponse(fullResponse));
650
+
651
+ const result = await runReducer(makeInput());
652
+
653
+ // Verify the result is correctly typed and populated
654
+ expect(result.timeContexts).toHaveLength(1);
655
+ expect(result.openLoops).toHaveLength(1);
656
+ expect(result.archiveObservations).toHaveLength(1);
657
+ expect(result.archiveEpisodes).toHaveLength(1);
658
+
659
+ // The function only called sendMessage — no other side effects
660
+ expect(mockSendMessage).toHaveBeenCalledTimes(1);
661
+ });
662
+
663
+ test("handles mixed valid and invalid operations gracefully", async () => {
664
+ mockSendMessage.mockResolvedValueOnce(
665
+ makeProviderResponse({
666
+ timeContexts: [
667
+ // valid
668
+ {
669
+ action: "create",
670
+ summary: "Valid context",
671
+ source: "conversation",
672
+ activeFrom: 1000,
673
+ activeUntil: 2000,
674
+ },
675
+ // invalid — missing summary
676
+ {
677
+ action: "create",
678
+ source: "conversation",
679
+ activeFrom: 1000,
680
+ activeUntil: 2000,
681
+ },
682
+ ],
683
+ openLoops: [
684
+ // valid
685
+ {
686
+ action: "create",
687
+ summary: "Valid loop",
688
+ source: "conversation",
689
+ },
690
+ // invalid — missing source
691
+ { action: "create", summary: "Bad loop" },
692
+ ],
693
+ }),
694
+ );
695
+
696
+ const result = await runReducer(makeInput());
697
+
698
+ // Only valid operations should be present
699
+ expect(result.timeContexts).toHaveLength(1);
700
+ expect(result.timeContexts[0].action).toBe("create");
701
+ expect(result.openLoops).toHaveLength(1);
702
+ expect(result.openLoops[0].action).toBe("create");
703
+ });
704
+ });