@vellumai/assistant 0.5.3 → 0.5.5

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 (111) hide show
  1. package/Dockerfile +18 -27
  2. package/docs/architecture/memory.md +105 -0
  3. package/node_modules/@vellumai/ces-contracts/src/index.ts +1 -0
  4. package/node_modules/@vellumai/ces-contracts/src/trust-rules.ts +42 -0
  5. package/package.json +1 -1
  6. package/src/__tests__/archive-recall.test.ts +560 -0
  7. package/src/__tests__/conversation-clear-safety.test.ts +259 -0
  8. package/src/__tests__/conversation-switch-memory-reduction.test.ts +474 -0
  9. package/src/__tests__/credential-security-invariants.test.ts +2 -0
  10. package/src/__tests__/db-schedule-syntax-migration.test.ts +3 -0
  11. package/src/__tests__/memory-reducer-job.test.ts +538 -0
  12. package/src/__tests__/memory-reducer-scheduling.test.ts +473 -0
  13. package/src/__tests__/memory-reducer-types.test.ts +12 -4
  14. package/src/__tests__/memory-reducer.test.ts +7 -1
  15. package/src/__tests__/memory-regressions.test.ts +24 -4
  16. package/src/__tests__/memory-simplified-config.test.ts +4 -4
  17. package/src/__tests__/openai-whisper.test.ts +93 -0
  18. package/src/__tests__/simplified-memory-e2e.test.ts +666 -0
  19. package/src/__tests__/simplified-memory-runtime.test.ts +616 -0
  20. package/src/__tests__/slack-messaging-token-resolution.test.ts +319 -0
  21. package/src/__tests__/volume-security-guard.test.ts +155 -0
  22. package/src/cli/commands/conversations.ts +18 -0
  23. package/src/config/bundled-skills/messaging/tools/shared.ts +1 -0
  24. package/src/config/bundled-skills/schedule/TOOLS.json +8 -0
  25. package/src/config/bundled-skills/transcribe/tools/transcribe-media.ts +16 -37
  26. package/src/config/env-registry.ts +9 -0
  27. package/src/config/feature-flag-registry.json +8 -0
  28. package/src/config/loader.ts +0 -1
  29. package/src/config/schemas/memory-simplified.ts +1 -1
  30. package/src/credential-execution/managed-catalog.ts +5 -15
  31. package/src/daemon/config-watcher.ts +4 -1
  32. package/src/daemon/conversation-memory.ts +117 -0
  33. package/src/daemon/conversation-runtime-assembly.ts +1 -0
  34. package/src/daemon/daemon-control.ts +7 -0
  35. package/src/daemon/handlers/conversations.ts +11 -0
  36. package/src/daemon/lifecycle.ts +51 -2
  37. package/src/daemon/providers-setup.ts +2 -1
  38. package/src/hooks/manager.ts +7 -0
  39. package/src/instrument.ts +33 -1
  40. package/src/memory/archive-recall.ts +516 -0
  41. package/src/memory/brief-time.ts +5 -4
  42. package/src/memory/conversation-crud.ts +210 -0
  43. package/src/memory/conversation-key-store.ts +33 -4
  44. package/src/memory/db-init.ts +4 -0
  45. package/src/memory/embedding-local.ts +11 -5
  46. package/src/memory/job-handlers/backfill-simplified-memory.ts +462 -0
  47. package/src/memory/job-handlers/conversation-starters.ts +24 -30
  48. package/src/memory/job-handlers/reduce-conversation-memory.ts +229 -0
  49. package/src/memory/jobs-store.ts +2 -0
  50. package/src/memory/jobs-worker.ts +8 -0
  51. package/src/memory/migrations/036-normalize-phone-identities.ts +49 -14
  52. package/src/memory/migrations/135-backfill-contact-interaction-stats.ts +9 -1
  53. package/src/memory/migrations/141-rename-verification-table.ts +8 -0
  54. package/src/memory/migrations/142-rename-verification-session-id-column.ts +7 -2
  55. package/src/memory/migrations/174-rename-thread-starters-table.ts +8 -0
  56. package/src/memory/migrations/188-schedule-quiet-flag.ts +13 -0
  57. package/src/memory/migrations/index.ts +1 -0
  58. package/src/memory/reducer-scheduler.ts +242 -0
  59. package/src/memory/reducer-types.ts +9 -2
  60. package/src/memory/reducer.ts +25 -11
  61. package/src/memory/schema/infrastructure.ts +1 -0
  62. package/src/messaging/provider.ts +9 -0
  63. package/src/messaging/providers/slack/adapter.ts +29 -2
  64. package/src/oauth/connection-resolver.test.ts +22 -18
  65. package/src/oauth/connection-resolver.ts +92 -7
  66. package/src/oauth/platform-connection.test.ts +78 -69
  67. package/src/oauth/platform-connection.ts +12 -19
  68. package/src/permissions/trust-client.ts +343 -0
  69. package/src/permissions/trust-store-interface.ts +105 -0
  70. package/src/permissions/trust-store.ts +523 -36
  71. package/src/platform/client.test.ts +148 -0
  72. package/src/platform/client.ts +71 -0
  73. package/src/providers/speech-to-text/openai-whisper.test.ts +190 -0
  74. package/src/providers/speech-to-text/openai-whisper.ts +68 -0
  75. package/src/providers/speech-to-text/resolve.ts +9 -0
  76. package/src/providers/speech-to-text/types.ts +17 -0
  77. package/src/runtime/auth/route-policy.ts +10 -1
  78. package/src/runtime/http-server.ts +2 -2
  79. package/src/runtime/routes/conversation-management-routes.ts +88 -2
  80. package/src/runtime/routes/guardian-bootstrap-routes.ts +19 -7
  81. package/src/runtime/routes/inbound-message-handler.ts +27 -3
  82. package/src/runtime/routes/inbound-stages/acl-enforcement.ts +16 -1
  83. package/src/runtime/routes/inbound-stages/transcribe-audio.test.ts +287 -0
  84. package/src/runtime/routes/inbound-stages/transcribe-audio.ts +122 -0
  85. package/src/runtime/routes/log-export-routes.ts +1 -0
  86. package/src/runtime/routes/secret-routes.ts +5 -1
  87. package/src/schedule/schedule-store.ts +7 -0
  88. package/src/schedule/scheduler.ts +6 -2
  89. package/src/security/ces-credential-client.ts +173 -0
  90. package/src/security/secure-keys.ts +65 -22
  91. package/src/signals/bash.ts +3 -0
  92. package/src/signals/cancel.ts +3 -0
  93. package/src/signals/confirm.ts +3 -0
  94. package/src/signals/conversation-undo.ts +3 -0
  95. package/src/signals/event-stream.ts +7 -0
  96. package/src/signals/shotgun.ts +3 -0
  97. package/src/signals/trust-rule.ts +3 -0
  98. package/src/telemetry/usage-telemetry-reporter.test.ts +23 -36
  99. package/src/telemetry/usage-telemetry-reporter.ts +22 -20
  100. package/src/tools/filesystem/edit.ts +6 -1
  101. package/src/tools/filesystem/read.ts +6 -1
  102. package/src/tools/filesystem/write.ts +6 -1
  103. package/src/tools/memory/handlers.ts +129 -1
  104. package/src/tools/schedule/create.ts +3 -0
  105. package/src/tools/schedule/list.ts +5 -1
  106. package/src/tools/schedule/update.ts +6 -0
  107. package/src/util/device-id.ts +70 -7
  108. package/src/util/logger.ts +35 -9
  109. package/src/util/platform.ts +29 -5
  110. package/src/workspace/migrations/migrate-to-workspace-volume.ts +113 -0
  111. package/src/workspace/migrations/registry.ts +2 -0
@@ -0,0 +1,560 @@
1
+ /**
2
+ * Tests for the archive recall module.
3
+ *
4
+ * Covers:
5
+ * - Explicit artifact recall (past-reference triggers)
6
+ * - Analogy/debugging-shaped recall
7
+ * - Strong prefetch triggers
8
+ * - Empty result omission (no `<supporting_recall>` when nothing found)
9
+ * - Keyword extraction
10
+ * - Rendering format
11
+ */
12
+ import { mkdtempSync, rmSync } from "node:fs";
13
+ import { tmpdir } from "node:os";
14
+ import { join } from "node:path";
15
+ import {
16
+ afterAll,
17
+ beforeAll,
18
+ beforeEach,
19
+ describe,
20
+ expect,
21
+ mock,
22
+ test,
23
+ } from "bun:test";
24
+
25
+ const testDir = mkdtempSync(join(tmpdir(), "archive-recall-test-"));
26
+ const dbPath = join(testDir, "test.db");
27
+
28
+ mock.module("../util/platform.js", () => ({
29
+ getDataDir: () => testDir,
30
+ isMacOS: () => process.platform === "darwin",
31
+ isLinux: () => process.platform === "linux",
32
+ isWindows: () => process.platform === "win32",
33
+ getPidPath: () => join(testDir, "test.pid"),
34
+ getDbPath: () => dbPath,
35
+ getLogPath: () => join(testDir, "test.log"),
36
+ ensureDataDir: () => {},
37
+ }));
38
+
39
+ mock.module("../util/logger.js", () => ({
40
+ getLogger: () =>
41
+ new Proxy({} as Record<string, unknown>, {
42
+ get: () => () => {},
43
+ }),
44
+ }));
45
+
46
+ import { v4 as uuid } from "uuid";
47
+
48
+ import {
49
+ buildArchiveRecall,
50
+ classifyRecallTrigger,
51
+ extractKeywords,
52
+ prefetchArchive,
53
+ type RecallBullet,
54
+ renderSupportingRecall,
55
+ } from "../memory/archive-recall.js";
56
+ import {
57
+ insertCompactionEpisode,
58
+ insertObservation,
59
+ } from "../memory/archive-store.js";
60
+ import { getDb, initializeDb, resetDb } from "../memory/db.js";
61
+ import { conversations, messages } from "../memory/schema.js";
62
+
63
+ // ── Helpers ─────────────────────────────────────────────────────────
64
+
65
+ function removeTestDbFiles(): void {
66
+ rmSync(dbPath, { force: true });
67
+ rmSync(`${dbPath}-shm`, { force: true });
68
+ rmSync(`${dbPath}-wal`, { force: true });
69
+ }
70
+
71
+ function createConversation(id: string, title: string | null = null): void {
72
+ const db = getDb();
73
+ const now = Date.now();
74
+ db.insert(conversations)
75
+ .values({
76
+ id,
77
+ title,
78
+ createdAt: now,
79
+ updatedAt: now,
80
+ })
81
+ .run();
82
+ }
83
+
84
+ function createMessage(
85
+ id: string,
86
+ conversationId: string,
87
+ role: string = "user",
88
+ content: string = "test message",
89
+ ): void {
90
+ const db = getDb();
91
+ db.insert(messages)
92
+ .values({
93
+ id,
94
+ conversationId,
95
+ role,
96
+ content,
97
+ createdAt: Date.now(),
98
+ })
99
+ .run();
100
+ }
101
+
102
+ // ── Test suite ──────────────────────────────────────────────────────
103
+
104
+ describe("Archive Recall", () => {
105
+ beforeAll(() => {
106
+ initializeDb();
107
+ });
108
+
109
+ beforeEach(() => {
110
+ resetDb();
111
+ removeTestDbFiles();
112
+ initializeDb();
113
+ });
114
+
115
+ afterAll(() => {
116
+ resetDb();
117
+ rmSync(testDir, { recursive: true, force: true });
118
+ });
119
+
120
+ // ─────────────────────────────────────────────────────────────────
121
+ // classifyRecallTrigger
122
+ // ─────────────────────────────────────────────────────────────────
123
+
124
+ describe("classifyRecallTrigger", () => {
125
+ test("detects explicit past-reference phrases", () => {
126
+ expect(
127
+ classifyRecallTrigger("Do you remember the API we discussed?", 0),
128
+ ).toBe("explicit_past_reference");
129
+ expect(classifyRecallTrigger("We talked about this last time", 0)).toBe(
130
+ "explicit_past_reference",
131
+ );
132
+ expect(
133
+ classifyRecallTrigger("As I mentioned earlier, the config is wrong", 0),
134
+ ).toBe("explicit_past_reference");
135
+ expect(
136
+ classifyRecallTrigger("I previously told you about the bug", 0),
137
+ ).toBe("explicit_past_reference");
138
+ });
139
+
140
+ test("detects analogy/debugging-shaped phrases", () => {
141
+ expect(
142
+ classifyRecallTrigger("This is similar to the issue we had", 0),
143
+ ).toBe("analogy_debug");
144
+ expect(classifyRecallTrigger("I keep getting this error", 0)).toBe(
145
+ "analogy_debug",
146
+ );
147
+ expect(classifyRecallTrigger("Same problem as yesterday", 0)).toBe(
148
+ "analogy_debug",
149
+ );
150
+ });
151
+
152
+ test("detects strong prefetch hits", () => {
153
+ expect(
154
+ classifyRecallTrigger("How should I configure the database?", 2),
155
+ ).toBe("strong_prefetch");
156
+ expect(
157
+ classifyRecallTrigger("How should I configure the database?", 5),
158
+ ).toBe("strong_prefetch");
159
+ });
160
+
161
+ test("returns none for ordinary turns", () => {
162
+ expect(classifyRecallTrigger("What is the capital of France?", 0)).toBe(
163
+ "none",
164
+ );
165
+ expect(
166
+ classifyRecallTrigger("Write a function to sort an array", 1),
167
+ ).toBe("none");
168
+ });
169
+
170
+ test("explicit past-reference takes priority over analogy", () => {
171
+ // "remember" matches past-reference, "same issue" matches analogy
172
+ expect(
173
+ classifyRecallTrigger("Do you remember the same issue we had?", 0),
174
+ ).toBe("explicit_past_reference");
175
+ });
176
+ });
177
+
178
+ // ─────────────────────────────────────────────────────────────────
179
+ // extractKeywords
180
+ // ─────────────────────────────────────────────────────────────────
181
+
182
+ describe("extractKeywords", () => {
183
+ test("extracts meaningful words >= 4 chars", () => {
184
+ const kw = extractKeywords("How do I fix the authentication error?");
185
+ expect(kw).toContain("authentication");
186
+ expect(kw).toContain("error");
187
+ // "how", "do", "I", "fix", "the" are too short or stop words
188
+ expect(kw).not.toContain("how");
189
+ expect(kw).not.toContain("the");
190
+ });
191
+
192
+ test("removes stop words", () => {
193
+ const kw = extractKeywords("I want to make this very much better");
194
+ expect(kw).not.toContain("want");
195
+ expect(kw).not.toContain("very");
196
+ expect(kw).not.toContain("much");
197
+ expect(kw).toContain("better");
198
+ });
199
+
200
+ test("deduplicates keywords", () => {
201
+ const kw = extractKeywords("error error error authentication");
202
+ expect(kw.filter((w) => w === "error")).toHaveLength(1);
203
+ });
204
+
205
+ test("returns empty for short/stop-word-only input", () => {
206
+ expect(extractKeywords("hi")).toEqual([]);
207
+ expect(extractKeywords("the a an")).toEqual([]);
208
+ });
209
+ });
210
+
211
+ // ─────────────────────────────────────────────────────────────────
212
+ // renderSupportingRecall
213
+ // ─────────────────────────────────────────────────────────────────
214
+
215
+ describe("renderSupportingRecall", () => {
216
+ test("renders bullets in <supporting_recall> tag", () => {
217
+ const bullets: RecallBullet[] = [
218
+ {
219
+ text: "User prefers REST APIs",
220
+ source: "observation",
221
+ sourceId: "obs-1",
222
+ conversationTitle: "API Discussion",
223
+ },
224
+ {
225
+ text: "Deployed to production last week",
226
+ source: "episode",
227
+ sourceId: "ep-1",
228
+ },
229
+ ];
230
+
231
+ const result = renderSupportingRecall(bullets);
232
+ expect(result).toContain("<supporting_recall>");
233
+ expect(result).toContain("</supporting_recall>");
234
+ expect(result).toContain(
235
+ "- User prefers REST APIs (from: API Discussion)",
236
+ );
237
+ expect(result).toContain("- Deployed to production last week");
238
+ // No provenance for second bullet (no conversationTitle)
239
+ expect(result).not.toContain("(from: undefined)");
240
+ expect(result).not.toContain("(from: null)");
241
+ });
242
+
243
+ test("returns empty string for empty bullets", () => {
244
+ expect(renderSupportingRecall([])).toBe("");
245
+ });
246
+ });
247
+
248
+ // ─────────────────────────────────────────────────────────────────
249
+ // Explicit artifact recall
250
+ // ─────────────────────────────────────────────────────────────────
251
+
252
+ describe("explicit artifact recall", () => {
253
+ test("recalls observations when user references past discussion", () => {
254
+ const convId = uuid();
255
+ const msgId = uuid();
256
+ createConversation(convId, "Authentication Redesign");
257
+ createMessage(msgId, convId);
258
+
259
+ insertObservation({
260
+ conversationId: convId,
261
+ messageId: msgId,
262
+ role: "user",
263
+ content:
264
+ "User wants to migrate authentication from JWT to session tokens",
265
+ scopeId: "default",
266
+ });
267
+
268
+ const result = buildArchiveRecall(
269
+ "default",
270
+ "Do you remember what we discussed about authentication?",
271
+ );
272
+
273
+ expect(result.trigger).toBe("explicit_past_reference");
274
+ expect(result.bullets.length).toBeGreaterThan(0);
275
+ expect(result.text).toContain("<supporting_recall>");
276
+ expect(result.text).toContain("authentication");
277
+ });
278
+
279
+ test("recalls episodes when user references past work", () => {
280
+ const convId = uuid();
281
+ createConversation(convId, "Database Migration Sprint");
282
+
283
+ insertCompactionEpisode({
284
+ scopeId: "default",
285
+ conversationId: convId,
286
+ title: "PostgreSQL Migration Planning",
287
+ summary:
288
+ "Discussed migrating from MySQL to PostgreSQL, decided on a phased approach starting with read replicas",
289
+ tokenEstimate: 25,
290
+ startAt: Date.now() - 86_400_000,
291
+ endAt: Date.now() - 43_200_000,
292
+ });
293
+
294
+ const result = buildArchiveRecall(
295
+ "default",
296
+ "What did we talk about regarding the PostgreSQL migration?",
297
+ );
298
+
299
+ expect(result.trigger).toBe("explicit_past_reference");
300
+ expect(result.bullets.length).toBeGreaterThan(0);
301
+ expect(result.text).toContain("<supporting_recall>");
302
+ expect(result.text).toContain("PostgreSQL");
303
+ });
304
+ });
305
+
306
+ // ─────────────────────────────────────────────────────────────────
307
+ // Analogy/debugging-shaped recall
308
+ // ─────────────────────────────────────────────────────────────────
309
+
310
+ describe("analogy-shaped recall", () => {
311
+ test("recalls when user reports a recurring issue", () => {
312
+ const convId = uuid();
313
+ const msgId = uuid();
314
+ createConversation(convId, "Debugging Session");
315
+ createMessage(msgId, convId);
316
+
317
+ insertObservation({
318
+ conversationId: convId,
319
+ messageId: msgId,
320
+ role: "user",
321
+ content:
322
+ "Connection timeout error when calling the payment gateway service",
323
+ scopeId: "default",
324
+ });
325
+
326
+ const result = buildArchiveRecall(
327
+ "default",
328
+ "I keep getting a timeout error with the payment service",
329
+ );
330
+
331
+ expect(result.trigger).toBe("analogy_debug");
332
+ expect(result.bullets.length).toBeGreaterThan(0);
333
+ expect(result.text).toContain("<supporting_recall>");
334
+ expect(result.text).toContain("timeout");
335
+ });
336
+
337
+ test("recalls similar past episodes for analogy queries", () => {
338
+ const convId = uuid();
339
+ createConversation(convId, "Infrastructure Issues");
340
+
341
+ insertCompactionEpisode({
342
+ scopeId: "default",
343
+ conversationId: convId,
344
+ title: "Redis Connection Pool Exhaustion",
345
+ summary:
346
+ "Debugged Redis connection pool exhaustion caused by missing connection.release() calls in the retry handler",
347
+ tokenEstimate: 30,
348
+ startAt: Date.now() - 172_800_000,
349
+ endAt: Date.now() - 86_400_000,
350
+ });
351
+
352
+ const result = buildArchiveRecall(
353
+ "default",
354
+ "This is similar to the Redis connection issue we had",
355
+ );
356
+
357
+ expect(result.trigger).toBe("analogy_debug");
358
+ expect(result.bullets.length).toBeGreaterThan(0);
359
+ expect(result.text).toContain("Redis");
360
+ });
361
+ });
362
+
363
+ // ─────────────────────────────────────────────────────────────────
364
+ // Empty result omission
365
+ // ─────────────────────────────────────────────────────────────────
366
+
367
+ describe("empty result omission", () => {
368
+ test("returns empty text when no archive content exists", () => {
369
+ const result = buildArchiveRecall(
370
+ "default",
371
+ "Do you remember what we discussed about quantum computing?",
372
+ );
373
+
374
+ expect(result.trigger).toBe("explicit_past_reference");
375
+ expect(result.bullets).toHaveLength(0);
376
+ expect(result.text).toBe("");
377
+ });
378
+
379
+ test("returns empty text for ordinary turns with no matches", () => {
380
+ const result = buildArchiveRecall(
381
+ "default",
382
+ "Write a hello world program in Python",
383
+ );
384
+
385
+ expect(result.trigger).toBe("none");
386
+ expect(result.bullets).toHaveLength(0);
387
+ expect(result.text).toBe("");
388
+ });
389
+
390
+ test("does not emit <supporting_recall> when trigger fires but no data matches", () => {
391
+ // Seed with unrelated data
392
+ const convId = uuid();
393
+ const msgId = uuid();
394
+ createConversation(convId, "Cooking Tips");
395
+ createMessage(msgId, convId);
396
+
397
+ insertObservation({
398
+ conversationId: convId,
399
+ messageId: msgId,
400
+ role: "user",
401
+ content: "User enjoys Italian cooking with fresh basil",
402
+ scopeId: "default",
403
+ });
404
+
405
+ // Ask about something completely unrelated
406
+ const result = buildArchiveRecall(
407
+ "default",
408
+ "Do you remember what we discussed about Kubernetes deployments?",
409
+ );
410
+
411
+ expect(result.trigger).toBe("explicit_past_reference");
412
+ expect(result.bullets).toHaveLength(0);
413
+ expect(result.text).toBe("");
414
+ });
415
+ });
416
+
417
+ // ─────────────────────────────────────────────────────────────────
418
+ // Prefetch behavior
419
+ // ─────────────────────────────────────────────────────────────────
420
+
421
+ describe("prefetch", () => {
422
+ test("returns hits from episodes and observations", () => {
423
+ const convId = uuid();
424
+ const msgId = uuid();
425
+ createConversation(convId);
426
+ createMessage(msgId, convId);
427
+
428
+ insertObservation({
429
+ conversationId: convId,
430
+ messageId: msgId,
431
+ role: "user",
432
+ content: "User prefers TypeScript over JavaScript",
433
+ scopeId: "default",
434
+ });
435
+
436
+ insertCompactionEpisode({
437
+ scopeId: "default",
438
+ conversationId: convId,
439
+ title: "TypeScript Configuration",
440
+ summary: "Set up strict TypeScript config with path aliases",
441
+ tokenEstimate: 15,
442
+ startAt: Date.now() - 3600_000,
443
+ endAt: Date.now() - 1800_000,
444
+ });
445
+
446
+ const hits = prefetchArchive("default", "TypeScript configuration setup");
447
+ expect(hits.length).toBeGreaterThan(0);
448
+ expect(hits.some((h) => h.source === "episode")).toBe(true);
449
+ expect(hits.some((h) => h.source === "observation")).toBe(true);
450
+ });
451
+
452
+ test("returns empty for no matches", () => {
453
+ const hits = prefetchArchive("default", "xyzzy nonexistent topic");
454
+ expect(hits).toHaveLength(0);
455
+ });
456
+ });
457
+
458
+ // ─────────────────────────────────────────────────────────────────
459
+ // Bullet cap and deduplication
460
+ // ─────────────────────────────────────────────────────────────────
461
+
462
+ describe("bullet cap and dedup", () => {
463
+ test("returns at most 3 bullets", () => {
464
+ const convId = uuid();
465
+ createConversation(convId);
466
+
467
+ // Insert 5 distinct observations
468
+ for (let i = 0; i < 5; i++) {
469
+ const msgId = uuid();
470
+ createMessage(msgId, convId);
471
+ insertObservation({
472
+ conversationId: convId,
473
+ messageId: msgId,
474
+ role: "user",
475
+ content: `Authentication fact number ${i}: uses OAuth2 flow variant ${i}`,
476
+ scopeId: "default",
477
+ });
478
+ }
479
+
480
+ const result = buildArchiveRecall(
481
+ "default",
482
+ "Do you remember what authentication method we use?",
483
+ );
484
+
485
+ expect(result.trigger).toBe("explicit_past_reference");
486
+ expect(result.bullets.length).toBeLessThanOrEqual(3);
487
+ });
488
+
489
+ test("deduplicates identical content from different sources", () => {
490
+ const convId = uuid();
491
+ const msgId = uuid();
492
+ createConversation(convId);
493
+ createMessage(msgId, convId);
494
+
495
+ // Insert the same content as both an observation and in an episode
496
+ const content = "User prefers dark mode for all development tools";
497
+ insertObservation({
498
+ conversationId: convId,
499
+ messageId: msgId,
500
+ role: "user",
501
+ content,
502
+ scopeId: "default",
503
+ });
504
+
505
+ insertCompactionEpisode({
506
+ scopeId: "default",
507
+ conversationId: convId,
508
+ title: "Development Preferences",
509
+ summary: content,
510
+ tokenEstimate: 10,
511
+ startAt: Date.now() - 3600_000,
512
+ endAt: Date.now() - 1800_000,
513
+ });
514
+
515
+ const result = buildArchiveRecall(
516
+ "default",
517
+ "Do you recall my preference for dark mode development tools?",
518
+ );
519
+
520
+ // Should have bullets but content should not be duplicated
521
+ if (result.bullets.length > 1) {
522
+ const texts = result.bullets.map((b) => b.text.toLowerCase());
523
+ // Each bullet text should be distinct
524
+ const uniqueTexts = new Set(texts);
525
+ expect(uniqueTexts.size).toBe(texts.length);
526
+ }
527
+ });
528
+ });
529
+
530
+ // ─────────────────────────────────────────────────────────────────
531
+ // Scope isolation
532
+ // ─────────────────────────────────────────────────────────────────
533
+
534
+ describe("scope isolation", () => {
535
+ test("only returns results from the requested scope", () => {
536
+ const convId = uuid();
537
+ const msgId = uuid();
538
+ createConversation(convId);
539
+ createMessage(msgId, convId);
540
+
541
+ insertObservation({
542
+ conversationId: convId,
543
+ messageId: msgId,
544
+ role: "user",
545
+ content: "Deployment uses Kubernetes with Helm charts",
546
+ scopeId: "other-scope",
547
+ });
548
+
549
+ const result = buildArchiveRecall(
550
+ "default",
551
+ "Do you remember our Kubernetes deployment setup?",
552
+ );
553
+
554
+ // Should trigger but find no results in "default" scope
555
+ expect(result.trigger).toBe("explicit_past_reference");
556
+ expect(result.bullets).toHaveLength(0);
557
+ expect(result.text).toBe("");
558
+ });
559
+ });
560
+ });