@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
@@ -517,7 +517,7 @@ describe("Memory Recall Quality", () => {
517
517
  test("superseded memory items do not appear in recall via recency", async () => {
518
518
  const db = getDb();
519
519
  const now = 1_700_000_200_000;
520
- insertConversation(db, "conv-contra", now);
520
+ insertConversation(db, "conv-contra", now, 1);
521
521
 
522
522
  // New preference (active, supersedes the old one)
523
523
  insertMessage(
@@ -574,7 +574,7 @@ describe("Memory Recall Quality", () => {
574
574
  test("only active items are included in recall (superseded excluded)", async () => {
575
575
  const db = getDb();
576
576
  const now = 1_700_000_250_000;
577
- insertConversation(db, "conv-entity-status", now);
577
+ insertConversation(db, "conv-entity-status", now, 1);
578
578
 
579
579
  insertMessage(
580
580
  db,
@@ -0,0 +1,538 @@
1
+ import { mkdtempSync, rmSync } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { afterAll, beforeEach, describe, expect, mock, test } from "bun:test";
5
+
6
+ // ── Test directory & platform mocks ───────────────────────────────
7
+
8
+ const testDir = mkdtempSync(join(tmpdir(), "reducer-job-test-"));
9
+
10
+ mock.module("../util/platform.js", () => ({
11
+ getDataDir: () => testDir,
12
+ getRootDir: () => testDir,
13
+ isMacOS: () => process.platform === "darwin",
14
+ isLinux: () => process.platform === "linux",
15
+ isWindows: () => process.platform === "win32",
16
+ getPidPath: () => join(testDir, "test.pid"),
17
+ getDbPath: () => join(testDir, "test.db"),
18
+ getLogPath: () => join(testDir, "test.log"),
19
+ ensureDataDir: () => {},
20
+ }));
21
+
22
+ mock.module("../util/logger.js", () => ({
23
+ getLogger: () =>
24
+ new Proxy({} as Record<string, unknown>, {
25
+ get: () => () => {},
26
+ }),
27
+ }));
28
+
29
+ // ── Mock the reducer ──────────────────────────────────────────────
30
+
31
+ import type { ReducerPromptInput } from "../memory/reducer.js";
32
+ import type { ReducerResult } from "../memory/reducer-types.js";
33
+ import { EMPTY_REDUCER_RESULT } from "../memory/reducer-types.js";
34
+
35
+ let mockReducerResult: ReducerResult = EMPTY_REDUCER_RESULT;
36
+ let lastReducerInput: ReducerPromptInput | null = null;
37
+
38
+ mock.module("../memory/reducer.js", () => ({
39
+ runReducer: async (input: ReducerPromptInput) => {
40
+ lastReducerInput = input;
41
+ return mockReducerResult;
42
+ },
43
+ }));
44
+
45
+ // ── Imports (after mocks) ─────────────────────────────────────────
46
+
47
+ import { initializeDb, resetDb } from "../memory/db.js";
48
+ import { getSqlite } from "../memory/db-connection.js";
49
+ import { reduceConversationMemoryJob } from "../memory/job-handlers/reduce-conversation-memory.js";
50
+ import type { MemoryJob } from "../memory/jobs-store.js";
51
+ import { resetTestTables } from "../memory/raw-query.js";
52
+
53
+ initializeDb();
54
+
55
+ // ── Helpers ───────────────────────────────────────────────────────
56
+
57
+ const SCOPE = "test-scope";
58
+ const NOW = 1_700_000_000_000;
59
+ const HOUR = 60 * 60 * 1000;
60
+
61
+ function insertConversation(
62
+ id: string,
63
+ opts?: {
64
+ dirtyTailMessageId?: string;
65
+ reducedThroughMessageId?: string;
66
+ contextSummary?: string;
67
+ memoryScopeId?: string;
68
+ },
69
+ ): void {
70
+ const raw = getSqlite();
71
+ raw.run(
72
+ `INSERT INTO conversations (id, title, created_at, updated_at, conversation_type, source, memory_scope_id, is_auto_title,
73
+ memory_dirty_tail_since_message_id, memory_reduced_through_message_id, context_summary)
74
+ VALUES (?, 'Test', ?, ?, 'standard', 'user', ?, 1, ?, ?, ?)`,
75
+ [
76
+ id,
77
+ NOW,
78
+ NOW,
79
+ opts?.memoryScopeId ?? SCOPE,
80
+ opts?.dirtyTailMessageId ?? null,
81
+ opts?.reducedThroughMessageId ?? null,
82
+ opts?.contextSummary ?? null,
83
+ ],
84
+ );
85
+ }
86
+
87
+ function insertMessage(opts: {
88
+ id: string;
89
+ conversationId: string;
90
+ role?: string;
91
+ content?: string;
92
+ createdAt?: number;
93
+ }): void {
94
+ const raw = getSqlite();
95
+ raw.run(
96
+ `INSERT INTO messages (id, conversation_id, role, content, created_at)
97
+ VALUES (?, ?, ?, ?, ?)`,
98
+ [
99
+ opts.id,
100
+ opts.conversationId,
101
+ opts.role ?? "user",
102
+ opts.content ?? "test message",
103
+ opts.createdAt ?? NOW,
104
+ ],
105
+ );
106
+ }
107
+
108
+ function getRawConversation(conversationId: string): Record<string, unknown> {
109
+ const raw = getSqlite();
110
+ return raw
111
+ .query(`SELECT * FROM conversations WHERE id = ?`)
112
+ .get(conversationId) as Record<string, unknown>;
113
+ }
114
+
115
+ function makeJob(conversationId: string): MemoryJob {
116
+ return {
117
+ id: "job-1",
118
+ type: "reduce_conversation_memory",
119
+ payload: { conversationId },
120
+ status: "running",
121
+ attempts: 0,
122
+ deferrals: 0,
123
+ runAfter: NOW,
124
+ lastError: null,
125
+ startedAt: NOW,
126
+ createdAt: NOW,
127
+ updatedAt: NOW,
128
+ };
129
+ }
130
+
131
+ function makeReducerResult(overrides?: Partial<ReducerResult>): ReducerResult {
132
+ return {
133
+ timeContexts: [],
134
+ openLoops: [],
135
+ archiveObservations: [],
136
+ archiveEpisodes: [],
137
+ ...overrides,
138
+ };
139
+ }
140
+
141
+ // ── Teardown ──────────────────────────────────────────────────────
142
+
143
+ afterAll(() => {
144
+ resetDb();
145
+ try {
146
+ rmSync(testDir, { recursive: true });
147
+ } catch {
148
+ /* best effort */
149
+ }
150
+ });
151
+
152
+ beforeEach(() => {
153
+ resetTestTables("messages", "conversations", "time_contexts", "open_loops");
154
+ mockReducerResult = EMPTY_REDUCER_RESULT;
155
+ lastReducerInput = null;
156
+ });
157
+
158
+ // ── Tests ─────────────────────────────────────────────────────────
159
+
160
+ describe("reduceConversationMemoryJob", () => {
161
+ describe("successful reduction", () => {
162
+ test("reduces dirty conversation and advances checkpoint", async () => {
163
+ insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
164
+ insertMessage({
165
+ id: "msg-1",
166
+ conversationId: "conv-1",
167
+ role: "user",
168
+ content: "Hello there",
169
+ createdAt: NOW,
170
+ });
171
+ insertMessage({
172
+ id: "msg-2",
173
+ conversationId: "conv-1",
174
+ role: "assistant",
175
+ content: "Hi! How can I help?",
176
+ createdAt: NOW + 1000,
177
+ });
178
+
179
+ mockReducerResult = makeReducerResult({
180
+ openLoops: [
181
+ {
182
+ action: "create",
183
+ summary: "User needs help with something",
184
+ source: "conversation",
185
+ },
186
+ ],
187
+ });
188
+
189
+ await reduceConversationMemoryJob(makeJob("conv-1"));
190
+
191
+ // Checkpoint should advance to the last message
192
+ const conv = getRawConversation("conv-1");
193
+ expect(conv.memory_reduced_through_message_id).toBe("msg-2");
194
+ expect(conv.memory_last_reduced_at).toBeGreaterThan(0);
195
+ // Dirty tail should be cleared since all messages are now reduced
196
+ expect(conv.memory_dirty_tail_since_message_id).toBeNull();
197
+ });
198
+
199
+ test("passes unreduced messages to the reducer", async () => {
200
+ insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
201
+ insertMessage({
202
+ id: "msg-1",
203
+ conversationId: "conv-1",
204
+ role: "user",
205
+ content: "First message",
206
+ createdAt: NOW,
207
+ });
208
+ insertMessage({
209
+ id: "msg-2",
210
+ conversationId: "conv-1",
211
+ role: "assistant",
212
+ content: "Second message",
213
+ createdAt: NOW + 1000,
214
+ });
215
+
216
+ mockReducerResult = makeReducerResult();
217
+
218
+ await reduceConversationMemoryJob(makeJob("conv-1"));
219
+
220
+ expect(lastReducerInput).not.toBeNull();
221
+ expect(lastReducerInput!.conversationId).toBe("conv-1");
222
+ expect(lastReducerInput!.newMessages).toHaveLength(2);
223
+ expect(lastReducerInput!.newMessages[0].role).toBe("user");
224
+ expect(lastReducerInput!.newMessages[0].content).toBe("First message");
225
+ expect(lastReducerInput!.newMessages[1].role).toBe("assistant");
226
+ expect(lastReducerInput!.newMessages[1].content).toBe("Second message");
227
+ });
228
+
229
+ test("includes contextSummary as synthetic system message when present", async () => {
230
+ insertConversation("conv-1", {
231
+ dirtyTailMessageId: "msg-1",
232
+ contextSummary: "User is working on a TypeScript project",
233
+ });
234
+ insertMessage({
235
+ id: "msg-1",
236
+ conversationId: "conv-1",
237
+ role: "user",
238
+ content: "Can you help with this bug?",
239
+ createdAt: NOW,
240
+ });
241
+
242
+ mockReducerResult = makeReducerResult();
243
+
244
+ await reduceConversationMemoryJob(makeJob("conv-1"));
245
+
246
+ expect(lastReducerInput).not.toBeNull();
247
+ // contextSummary should be prepended as a system message
248
+ expect(lastReducerInput!.newMessages).toHaveLength(2);
249
+ expect(lastReducerInput!.newMessages[0].role).toBe("system");
250
+ expect(lastReducerInput!.newMessages[0].content).toContain(
251
+ "User is working on a TypeScript project",
252
+ );
253
+ // Real message follows
254
+ expect(lastReducerInput!.newMessages[1].role).toBe("user");
255
+ expect(lastReducerInput!.newMessages[1].content).toBe(
256
+ "Can you help with this bug?",
257
+ );
258
+ });
259
+
260
+ test("loads active time contexts and open loops for the reducer", async () => {
261
+ insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
262
+ insertMessage({
263
+ id: "msg-1",
264
+ conversationId: "conv-1",
265
+ role: "user",
266
+ content: "test",
267
+ createdAt: NOW,
268
+ });
269
+
270
+ // Insert pre-existing active time context.
271
+ // Use a far-future activeUntil so it is still active at Date.now().
272
+ const farFuture = Date.now() + 365 * 24 * HOUR;
273
+ const raw = getSqlite();
274
+ raw.run(
275
+ `INSERT INTO time_contexts (id, scope_id, summary, source, active_from, active_until, created_at, updated_at)
276
+ VALUES ('tc-1', ?, 'User on vacation next week', 'conversation', ?, ?, ?, ?)`,
277
+ [SCOPE, NOW, farFuture, NOW, NOW],
278
+ );
279
+
280
+ // Insert pre-existing open loop
281
+ raw.run(
282
+ `INSERT INTO open_loops (id, scope_id, summary, status, source, created_at, updated_at)
283
+ VALUES ('ol-1', ?, 'Waiting for deploy', 'open', 'conversation', ?, ?)`,
284
+ [SCOPE, NOW, NOW],
285
+ );
286
+
287
+ mockReducerResult = makeReducerResult();
288
+
289
+ await reduceConversationMemoryJob(makeJob("conv-1"));
290
+
291
+ expect(lastReducerInput).not.toBeNull();
292
+ expect(lastReducerInput!.existingTimeContexts).toHaveLength(1);
293
+ expect(lastReducerInput!.existingTimeContexts[0].id).toBe("tc-1");
294
+ expect(lastReducerInput!.existingTimeContexts[0].summary).toBe(
295
+ "User on vacation next week",
296
+ );
297
+ expect(lastReducerInput!.existingOpenLoops).toHaveLength(1);
298
+ expect(lastReducerInput!.existingOpenLoops[0].id).toBe("ol-1");
299
+ expect(lastReducerInput!.existingOpenLoops[0].summary).toBe(
300
+ "Waiting for deploy",
301
+ );
302
+ });
303
+
304
+ test("creates time contexts and open loops from reducer output", async () => {
305
+ insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
306
+ insertMessage({
307
+ id: "msg-1",
308
+ conversationId: "conv-1",
309
+ role: "user",
310
+ content: "I'm going on vacation next week",
311
+ createdAt: NOW,
312
+ });
313
+
314
+ mockReducerResult = makeReducerResult({
315
+ timeContexts: [
316
+ {
317
+ action: "create",
318
+ summary: "User on vacation next week",
319
+ source: "conversation",
320
+ activeFrom: NOW,
321
+ activeUntil: NOW + 7 * 24 * HOUR,
322
+ },
323
+ ],
324
+ openLoops: [
325
+ {
326
+ action: "create",
327
+ summary: "Set up OOO auto-reply",
328
+ source: "conversation",
329
+ },
330
+ ],
331
+ });
332
+
333
+ await reduceConversationMemoryJob(makeJob("conv-1"));
334
+
335
+ // Verify time context was created
336
+ const raw = getSqlite();
337
+ const contexts = raw
338
+ .query(`SELECT * FROM time_contexts WHERE scope_id = ?`)
339
+ .all(SCOPE) as Array<Record<string, unknown>>;
340
+ expect(contexts).toHaveLength(1);
341
+ expect(contexts[0].summary).toBe("User on vacation next week");
342
+
343
+ // Verify open loop was created
344
+ const loops = raw
345
+ .query(`SELECT * FROM open_loops WHERE scope_id = ?`)
346
+ .all(SCOPE) as Array<Record<string, unknown>>;
347
+ expect(loops).toHaveLength(1);
348
+ expect(loops[0].summary).toBe("Set up OOO auto-reply");
349
+ expect(loops[0].status).toBe("open");
350
+ });
351
+ });
352
+
353
+ describe("empty dirty tails", () => {
354
+ test("no-ops when conversation has no dirty tail marker", async () => {
355
+ insertConversation("conv-1"); // no dirtyTailMessageId
356
+ insertMessage({
357
+ id: "msg-1",
358
+ conversationId: "conv-1",
359
+ role: "user",
360
+ content: "test",
361
+ createdAt: NOW,
362
+ });
363
+
364
+ await reduceConversationMemoryJob(makeJob("conv-1"));
365
+
366
+ // Reducer should not have been called
367
+ expect(lastReducerInput).toBeNull();
368
+
369
+ // Conversation unchanged
370
+ const conv = getRawConversation("conv-1");
371
+ expect(conv.memory_reduced_through_message_id).toBeNull();
372
+ });
373
+
374
+ test("no-ops when dirty tail message no longer exists", async () => {
375
+ insertConversation("conv-1", {
376
+ dirtyTailMessageId: "deleted-msg",
377
+ });
378
+ // No messages inserted — the dirty tail message doesn't exist
379
+
380
+ await reduceConversationMemoryJob(makeJob("conv-1"));
381
+
382
+ // Reducer should not have been called
383
+ expect(lastReducerInput).toBeNull();
384
+
385
+ // Conversation unchanged
386
+ const conv = getRawConversation("conv-1");
387
+ expect(conv.memory_reduced_through_message_id).toBeNull();
388
+ });
389
+
390
+ test("no-ops when conversation does not exist", async () => {
391
+ await reduceConversationMemoryJob(makeJob("nonexistent-conv"));
392
+
393
+ expect(lastReducerInput).toBeNull();
394
+ });
395
+
396
+ test("no-ops when payload has no conversationId", async () => {
397
+ const job: MemoryJob = {
398
+ id: "job-1",
399
+ type: "reduce_conversation_memory",
400
+ payload: {},
401
+ status: "running",
402
+ attempts: 0,
403
+ deferrals: 0,
404
+ runAfter: NOW,
405
+ lastError: null,
406
+ startedAt: NOW,
407
+ createdAt: NOW,
408
+ updatedAt: NOW,
409
+ };
410
+
411
+ await reduceConversationMemoryJob(job);
412
+
413
+ expect(lastReducerInput).toBeNull();
414
+ });
415
+ });
416
+
417
+ describe("reducer failure safety", () => {
418
+ test("does not advance checkpoint when reducer returns EMPTY_REDUCER_RESULT", async () => {
419
+ insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
420
+ insertMessage({
421
+ id: "msg-1",
422
+ conversationId: "conv-1",
423
+ role: "user",
424
+ content: "test",
425
+ createdAt: NOW,
426
+ });
427
+
428
+ // The default mockReducerResult is EMPTY_REDUCER_RESULT
429
+ mockReducerResult = EMPTY_REDUCER_RESULT;
430
+
431
+ await reduceConversationMemoryJob(makeJob("conv-1"));
432
+
433
+ // Checkpoint should NOT have advanced
434
+ const conv = getRawConversation("conv-1");
435
+ expect(conv.memory_reduced_through_message_id).toBeNull();
436
+ expect(conv.memory_last_reduced_at).toBeNull();
437
+ // Dirty tail stays in place for retry
438
+ expect(conv.memory_dirty_tail_since_message_id).toBe("msg-1");
439
+ });
440
+
441
+ test("does not advance checkpoint when reducer throws", async () => {
442
+ insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
443
+ insertMessage({
444
+ id: "msg-1",
445
+ conversationId: "conv-1",
446
+ role: "user",
447
+ content: "test",
448
+ createdAt: NOW,
449
+ });
450
+
451
+ // Temporarily replace the mock to throw
452
+ mock.module("../memory/reducer.js", () => ({
453
+ runReducer: async (input: ReducerPromptInput) => {
454
+ lastReducerInput = input;
455
+ throw new Error("Provider timeout");
456
+ },
457
+ }));
458
+
459
+ try {
460
+ await reduceConversationMemoryJob(makeJob("conv-1"));
461
+ } catch {
462
+ // Error propagation is expected — the job worker handles classification
463
+ }
464
+
465
+ // Restore the normal mock for subsequent tests
466
+ mock.module("../memory/reducer.js", () => ({
467
+ runReducer: async (input: ReducerPromptInput) => {
468
+ lastReducerInput = input;
469
+ return mockReducerResult;
470
+ },
471
+ }));
472
+
473
+ // Regardless of error handling, checkpoint must not advance
474
+ const conv = getRawConversation("conv-1");
475
+ expect(conv.memory_reduced_through_message_id).toBeNull();
476
+ expect(conv.memory_dirty_tail_since_message_id).toBe("msg-1");
477
+ });
478
+
479
+ test("partial dirty tail preserved when more messages arrive during reduction", async () => {
480
+ insertConversation("conv-1", { dirtyTailMessageId: "msg-1" });
481
+ insertMessage({
482
+ id: "msg-1",
483
+ conversationId: "conv-1",
484
+ role: "user",
485
+ content: "First",
486
+ createdAt: NOW,
487
+ });
488
+ insertMessage({
489
+ id: "msg-2",
490
+ conversationId: "conv-1",
491
+ role: "assistant",
492
+ content: "Response",
493
+ createdAt: NOW + 1000,
494
+ });
495
+ // msg-3 arrives "later" — simulates a message added during/after reduction
496
+ insertMessage({
497
+ id: "msg-3",
498
+ conversationId: "conv-1",
499
+ role: "user",
500
+ content: "Follow-up",
501
+ createdAt: NOW + 5000,
502
+ });
503
+
504
+ mockReducerResult = makeReducerResult();
505
+
506
+ await reduceConversationMemoryJob(makeJob("conv-1"));
507
+
508
+ const conv = getRawConversation("conv-1");
509
+ // All three messages were loaded (they all exist at query time), so
510
+ // checkpoint advances through msg-3
511
+ expect(conv.memory_reduced_through_message_id).toBe("msg-3");
512
+ });
513
+ });
514
+
515
+ describe("scope isolation", () => {
516
+ test("uses the conversation's memoryScopeId for context lookups", async () => {
517
+ const customScope = "custom-scope";
518
+ insertConversation("conv-1", {
519
+ dirtyTailMessageId: "msg-1",
520
+ memoryScopeId: customScope,
521
+ });
522
+ insertMessage({
523
+ id: "msg-1",
524
+ conversationId: "conv-1",
525
+ role: "user",
526
+ content: "test",
527
+ createdAt: NOW,
528
+ });
529
+
530
+ mockReducerResult = makeReducerResult();
531
+
532
+ await reduceConversationMemoryJob(makeJob("conv-1"));
533
+
534
+ expect(lastReducerInput).not.toBeNull();
535
+ expect(lastReducerInput!.scopeId).toBe(customScope);
536
+ });
537
+ });
538
+ });