@vellumai/assistant 0.8.10-dev.202606092238.d04fd59 → 0.8.10-dev.202606100110.1d2c8c4

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 (91) hide show
  1. package/package.json +1 -1
  2. package/src/__tests__/agent-loop-exit-reason.test.ts +2 -1
  3. package/src/__tests__/approval-cascade.test.ts +1 -0
  4. package/src/__tests__/compaction-events.test.ts +1 -0
  5. package/src/__tests__/context-window-manager-overflow-rung.test.ts +63 -1
  6. package/src/__tests__/conversation-abort-tool-results.test.ts +1 -0
  7. package/src/__tests__/conversation-agent-loop-inference-profile.test.ts +11 -0
  8. package/src/__tests__/conversation-agent-loop-overflow.test.ts +68 -27
  9. package/src/__tests__/conversation-agent-loop.test.ts +68 -27
  10. package/src/__tests__/conversation-confirmation-signals.test.ts +1 -0
  11. package/src/__tests__/conversation-fork-crud.test.ts +149 -0
  12. package/src/__tests__/conversation-lifecycle.test.ts +257 -1
  13. package/src/__tests__/conversation-process-callsite.test.ts +1 -0
  14. package/src/__tests__/conversation-provider-retry-repair.test.ts +48 -44
  15. package/src/__tests__/conversation-queue.test.ts +1 -0
  16. package/src/__tests__/conversation-runtime-assembly.test.ts +5 -7
  17. package/src/__tests__/conversation-slash-queue.test.ts +1 -0
  18. package/src/__tests__/conversation-slash-unknown.test.ts +1 -0
  19. package/src/__tests__/conversation-speed-override.test.ts +1 -0
  20. package/src/__tests__/conversation-workspace-cache-state.test.ts +1 -0
  21. package/src/__tests__/conversation-workspace-injection.test.ts +1 -0
  22. package/src/__tests__/conversation-workspace-tool-tracking.test.ts +1 -0
  23. package/src/__tests__/injector-chain.test.ts +5 -4
  24. package/src/__tests__/injector-v3-suppression.test.ts +373 -47
  25. package/src/__tests__/memory-retrieval-hook.test.ts +88 -0
  26. package/src/__tests__/strip-memory-injections.test.ts +33 -39
  27. package/src/agent/loop.ts +3 -19
  28. package/src/api/responses/memory-v3-selection-log.ts +4 -4
  29. package/src/config/schemas/__tests__/memory-v3.test.ts +61 -1
  30. package/src/config/schemas/memory-v3.ts +104 -33
  31. package/src/context/strip-injections.ts +31 -0
  32. package/src/daemon/conversation-agent-loop.ts +36 -82
  33. package/src/daemon/conversation-runtime-assembly.ts +139 -14
  34. package/src/daemon/conversation.ts +93 -30
  35. package/src/daemon/trust-context.ts +23 -0
  36. package/src/memory/conversation-crud.ts +47 -9
  37. package/src/memory/db-init.ts +2 -0
  38. package/src/memory/graph/__tests__/conversation-graph-memory-v2-routing.test.ts +12 -0
  39. package/src/memory/graph/conversation-graph-memory.ts +35 -24
  40. package/src/memory/memory-marker.ts +29 -0
  41. package/src/memory/migrations/268-add-memory-v3-selections.ts +6 -0
  42. package/src/memory/migrations/277-add-memory-v3-ever-injected.ts +29 -0
  43. package/src/memory/migrations/index.ts +1 -0
  44. package/src/memory/v2/__tests__/injected-block-slugs.test.ts +40 -1
  45. package/src/memory/v2/injected-block-slugs.ts +61 -12
  46. package/src/memory/v2/injection.ts +6 -1
  47. package/src/memory/v2/prompts/consolidation.ts +10 -0
  48. package/src/memory/v2/router.ts +2 -28
  49. package/src/plugin-api/index.ts +3 -0
  50. package/src/plugin-api/types.ts +56 -0
  51. package/src/plugins/defaults/compaction/window-manager.ts +76 -18
  52. package/src/plugins/defaults/memory-retrieval/hooks/post-compact.ts +42 -103
  53. package/src/plugins/defaults/memory-retrieval/hooks/user-prompt-submit.ts +47 -7
  54. package/src/plugins/defaults/memory-retrieval/injector-chain.ts +14 -8
  55. package/src/plugins/defaults/memory-retrieval/injectors.ts +2 -18
  56. package/src/plugins/defaults/memory-v3-shadow/__tests__/carry-integration.test.ts +1157 -0
  57. package/src/plugins/defaults/memory-v3-shadow/__tests__/injection.test.ts +669 -0
  58. package/src/plugins/defaults/memory-v3-shadow/__tests__/live-integration.test.ts +161 -140
  59. package/src/plugins/defaults/memory-v3-shadow/__tests__/maintain-job.test.ts +65 -0
  60. package/src/plugins/defaults/memory-v3-shadow/__tests__/orchestrate.test.ts +335 -316
  61. package/src/plugins/defaults/memory-v3-shadow/__tests__/pool-select.test.ts +145 -53
  62. package/src/plugins/defaults/memory-v3-shadow/__tests__/render-injection.test.ts +38 -1
  63. package/src/plugins/defaults/memory-v3-shadow/__tests__/selection-log-store.test.ts +18 -8
  64. package/src/plugins/defaults/memory-v3-shadow/__tests__/shadow-integration.test.ts +112 -71
  65. package/src/plugins/defaults/memory-v3-shadow/__tests__/shadow-plugin.test.ts +177 -51
  66. package/src/plugins/defaults/memory-v3-shadow/__tests__/types.test.ts +4 -16
  67. package/src/plugins/defaults/memory-v3-shadow/card.test.ts +173 -0
  68. package/src/plugins/defaults/memory-v3-shadow/card.ts +116 -0
  69. package/src/plugins/defaults/memory-v3-shadow/core-set.test.ts +104 -0
  70. package/src/plugins/defaults/memory-v3-shadow/core-set.ts +59 -0
  71. package/src/plugins/defaults/memory-v3-shadow/ever-injected-store.test.ts +305 -0
  72. package/src/plugins/defaults/memory-v3-shadow/ever-injected-store.ts +278 -0
  73. package/src/plugins/defaults/memory-v3-shadow/hot-set.test.ts +138 -0
  74. package/src/plugins/defaults/memory-v3-shadow/hot-set.ts +85 -0
  75. package/src/plugins/defaults/memory-v3-shadow/injector.ts +329 -24
  76. package/src/plugins/defaults/memory-v3-shadow/maintain-job.ts +54 -5
  77. package/src/plugins/defaults/memory-v3-shadow/orchestrate.ts +169 -114
  78. package/src/plugins/defaults/memory-v3-shadow/page-content.ts +47 -16
  79. package/src/plugins/defaults/memory-v3-shadow/pool-select.ts +144 -66
  80. package/src/plugins/defaults/memory-v3-shadow/prune.test.ts +758 -0
  81. package/src/plugins/defaults/memory-v3-shadow/prune.ts +471 -0
  82. package/src/plugins/defaults/memory-v3-shadow/render-injection.ts +68 -16
  83. package/src/plugins/defaults/memory-v3-shadow/selection-log-store.ts +19 -12
  84. package/src/plugins/defaults/memory-v3-shadow/shadow-plugin.ts +92 -42
  85. package/src/plugins/defaults/memory-v3-shadow/types.ts +34 -17
  86. package/src/providers/cache-control.ts +26 -0
  87. package/src/plugins/defaults/memory-v3-shadow/__tests__/fixtures/eval-turns.json +0 -36
  88. package/src/plugins/defaults/memory-v3-shadow/__tests__/fixtures/live-turns.json +0 -37
  89. package/src/plugins/defaults/memory-v3-shadow/__tests__/working-set-eviction.test.ts +0 -106
  90. package/src/plugins/defaults/memory-v3-shadow/__tests__/working-set-skeleton.test.ts +0 -44
  91. package/src/plugins/defaults/memory-v3-shadow/working-set.ts +0 -91
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vellumai/assistant",
3
- "version": "0.8.10-dev.202606092238.d04fd59",
3
+ "version": "0.8.10-dev.202606100110.1d2c8c4",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -15,6 +15,8 @@
15
15
  */
16
16
  import { afterEach, describe, expect, test } from "bun:test";
17
17
 
18
+ import type { PostCompactContext } from "@vellumai/plugin-api";
19
+
18
20
  import type {
19
21
  AgentEvent,
20
22
  CheckpointDecision,
@@ -29,7 +31,6 @@ import {
29
31
  disposeContextWindowManager,
30
32
  getContextWindowManager,
31
33
  } from "../plugins/defaults/compaction/manager-store.js";
32
- import type { PostCompactContext } from "../plugins/defaults/memory-retrieval/hooks/post-compact.js";
33
34
  import { registerPlugin } from "../plugins/registry.js";
34
35
  import type {
35
36
  Message,
@@ -183,6 +183,7 @@ mock.module("../plugins/defaults/compaction/window-manager.js", () => ({
183
183
  async maybeCompact() {
184
184
  return { compacted: false };
185
185
  }
186
+ resetOverflowRecovery() {}
186
187
  },
187
188
  createContextSummaryMessage: () => ({
188
189
  role: "user",
@@ -198,6 +198,7 @@ mock.module("../plugins/defaults/compaction/window-manager.js", () => ({
198
198
  async maybeCompact(): Promise<ContextWindowResult> {
199
199
  return mockCompactResult;
200
200
  }
201
+ resetOverflowRecovery() {}
201
202
  },
202
203
  createContextSummaryMessage: () => ({
203
204
  role: "user",
@@ -67,6 +67,10 @@ mock.module("../context/token-estimator.js", () => ({
67
67
  }
68
68
  return next;
69
69
  },
70
+ // Deterministic per-tool budget so tests can assert the manager fed the
71
+ // turn's resolved tool set (not the constructor-time snapshot) into the
72
+ // overflow estimate.
73
+ estimateToolsTokens: (tools: unknown[]): number => tools.length * 1_000,
70
74
  }));
71
75
 
72
76
  mock.module(
@@ -96,7 +100,7 @@ mock.module(
96
100
  );
97
101
 
98
102
  import { ContextWindowManager } from "../plugins/defaults/compaction/window-manager.js";
99
- import type { Message, Provider } from "../providers/types.js";
103
+ import type { Message, Provider, ToolDefinition } from "../providers/types.js";
100
104
 
101
105
  function makeProvider(): Provider {
102
106
  return {
@@ -194,6 +198,34 @@ describe("ContextWindowManager.reduceOverflowOneRung", () => {
194
198
  ]);
195
199
  });
196
200
 
201
+ test("reuses the first rung's corrected target across the turn's later rungs", async () => {
202
+ // GIVEN the provider rejected at 480k while the estimator counted 240k, and
203
+ // only one estimate is seeded so a second estimate call would throw
204
+ estimateReturns.push(240_000);
205
+ reduceSteps = [
206
+ makeStep(["emergency_compaction"]),
207
+ makeStep(["emergency_compaction", "forced_compaction"]),
208
+ ];
209
+ const manager = buildManager();
210
+
211
+ // WHEN two rungs run within the same turn with the provider's actual count
212
+ await manager.reduceOverflowOneRung(makeMessages(10), {
213
+ actualTokens: 480_000,
214
+ allowAutoCompressLatestTurn: false,
215
+ });
216
+ await manager.reduceOverflowOneRung(makeMessages(4), {
217
+ actualTokens: 480_000,
218
+ allowAutoCompressLatestTurn: false,
219
+ });
220
+
221
+ // THEN the corrected target (190k preflight / 2x error ratio) is computed
222
+ // once and reused, rather than re-derived against the shrunk prompt
223
+ expect(reduceCalls[0]?.config.targetTokens).toBe(95_000);
224
+ expect(reduceCalls[1]?.config.targetTokens).toBe(95_000);
225
+ expect(reduceCalls[0]?.config.previousEstimatedInputTokens).toBe(240_000);
226
+ expect(reduceCalls[1]?.config.previousEstimatedInputTokens).toBe(240_000);
227
+ });
228
+
197
229
  test("resetOverflowRecovery starts a fresh ladder", async () => {
198
230
  // GIVEN a manager whose ladder has already advanced one rung
199
231
  estimateReturns.push(240_000, 240_000);
@@ -235,6 +267,36 @@ describe("ContextWindowManager.reduceOverflowOneRung", () => {
235
267
  expect(reduceCalls[0]?.config.targetTokens).toBe(95_000);
236
268
  });
237
269
 
270
+ test("estimates against the turn's resolved tool set, not the constructor snapshot", async () => {
271
+ // GIVEN a manager whose live tool set (3 tools) differs from the stale
272
+ // constructor-time budget snapshot
273
+ estimateReturns.push(240_000);
274
+ reduceSteps = [makeStep(["emergency_compaction"])];
275
+ const resolvedTools = [
276
+ { name: "a" },
277
+ { name: "b" },
278
+ { name: "c" },
279
+ ] as unknown as ToolDefinition[];
280
+ const manager = new ContextWindowManager({
281
+ provider: makeProvider(),
282
+ systemPrompt: "you are a test assistant",
283
+ config: makeConfig(),
284
+ conversationId: "conv-test",
285
+ toolTokenBudget: 50,
286
+ resolveTools: () => resolvedTools,
287
+ });
288
+
289
+ // WHEN a rung runs
290
+ await manager.reduceOverflowOneRung(makeMessages(10), {
291
+ actualTokens: null,
292
+ allowAutoCompressLatestTurn: false,
293
+ });
294
+
295
+ // THEN the reducer is fed the resolved tool budget (3 tools * 1k) rather
296
+ // than the constructor snapshot (50)
297
+ expect(reduceCalls[0]?.config.toolTokenBudget).toBe(3_000);
298
+ });
299
+
238
300
  test("target is the full preflight budget when the actual count is unknown", async () => {
239
301
  // GIVEN no provider-reported actual token count
240
302
  estimateReturns.push(240_000);
@@ -177,6 +177,7 @@ mock.module("../plugins/defaults/compaction/window-manager.js", () => ({
177
177
  async maybeCompact() {
178
178
  return { compacted: false };
179
179
  }
180
+ resetOverflowRecovery() {}
180
181
  },
181
182
  createContextSummaryMessage: () => ({
182
183
  role: "user",
@@ -435,6 +435,17 @@ function makeCtx(
435
435
  updateConfig: () => {},
436
436
  shouldCompact: () => ({ needed: false, estimatedTokens: 0 }),
437
437
  maybeCompact: async () => ({ compacted: false }),
438
+ resetOverflowRecovery: () => {},
439
+ reduceOverflowOneRung: async (msgs: Message[]) => ({
440
+ messages: msgs,
441
+ tier: "forced_compaction",
442
+ state: {
443
+ appliedTiers: ["forced_compaction"],
444
+ injectionMode: "full",
445
+ exhausted: true,
446
+ },
447
+ estimatedTokens: 1000,
448
+ }),
438
449
  } as unknown as Conversation["contextWindowManager"],
439
450
  contextCompactedMessageCount: 0,
440
451
  contextCompactedAt: null,
@@ -151,39 +151,70 @@ mock.module("../context/token-estimator.js", () => ({
151
151
  let mockReducerStepFn:
152
152
  | ((msgs: Message[], cfg: unknown, state: unknown) => unknown)
153
153
  | null = null;
154
+ const makeInitialReducerState = () => ({
155
+ appliedTiers: [] as string[],
156
+ injectionMode: "full" as const,
157
+ exhausted: false,
158
+ });
159
+ const runMockReducer = async (
160
+ msgs: Message[],
161
+ cfg: unknown,
162
+ state: unknown,
163
+ ) => {
164
+ if (mockReducerStepFn) return mockReducerStepFn(msgs, cfg, state);
165
+ return {
166
+ messages: msgs,
167
+ tier: "forced_compaction",
168
+ state: {
169
+ appliedTiers: [
170
+ "forced_compaction",
171
+ "tool_result_truncation",
172
+ "media_stubbing",
173
+ "injection_downgrade",
174
+ ],
175
+ injectionMode: "full",
176
+ exhausted: true,
177
+ },
178
+ estimatedTokens: 1000,
179
+ };
180
+ };
154
181
  mock.module(
155
182
  "../plugins/defaults/compaction/context-overflow-reducer.js",
156
183
  () => ({
157
- createInitialReducerState: () => ({
158
- appliedTiers: [],
159
- injectionMode: "full" as const,
160
- exhausted: false,
161
- }),
162
- reduceContextOverflow: async (
163
- msgs: Message[],
164
- cfg: unknown,
165
- state: unknown,
166
- ) => {
167
- if (mockReducerStepFn) return mockReducerStepFn(msgs, cfg, state);
168
- return {
169
- messages: msgs,
170
- tier: "forced_compaction",
171
- state: {
172
- appliedTiers: [
173
- "forced_compaction",
174
- "tool_result_truncation",
175
- "media_stubbing",
176
- "injection_downgrade",
177
- ],
178
- injectionMode: "full",
179
- exhausted: true,
180
- },
181
- estimatedTokens: 1000,
182
- };
183
- },
184
+ createInitialReducerState: makeInitialReducerState,
185
+ reduceContextOverflow: runMockReducer,
184
186
  }),
185
187
  );
186
188
 
189
+ // Stand-in for `ContextWindowManager`'s turn-scoped overflow ladder. Threads
190
+ // reducer state across a turn's rungs and delegates each rung to the mocked
191
+ // reducer, mirroring `reduceOverflowOneRung` / `resetOverflowRecovery` so the
192
+ // convergence driver exercises the same per-rung escalation the real manager
193
+ // drives.
194
+ function makeOverflowLadderStub(): {
195
+ resetOverflowRecovery: () => void;
196
+ reduceOverflowOneRung: (
197
+ msgs: Message[],
198
+ opts: unknown,
199
+ signal?: AbortSignal,
200
+ ) => Promise<unknown>;
201
+ } {
202
+ let state: unknown;
203
+ return {
204
+ resetOverflowRecovery: () => {
205
+ state = undefined;
206
+ },
207
+ reduceOverflowOneRung: async (msgs: Message[], opts: unknown) => {
208
+ if (!state) state = makeInitialReducerState();
209
+ const step = (await runMockReducer(msgs, opts, state)) as {
210
+ state: unknown;
211
+ };
212
+ state = step.state;
213
+ return step;
214
+ },
215
+ };
216
+ }
217
+
187
218
  // Policy: default to fail_gracefully
188
219
  let mockOverflowAction: string = "fail_gracefully";
189
220
  mock.module("../daemon/context-overflow-policy.js", () => ({
@@ -599,6 +630,16 @@ function makeCtx(
599
630
 
600
631
  ...ctxOverrides,
601
632
  } as unknown as Conversation;
633
+ // The convergence driver resolves the turn-scoped overflow ladder off the
634
+ // manager; give every fake manager the ladder methods unless a test supplied
635
+ // its own.
636
+ const manager = ctx.contextWindowManager as unknown as Record<
637
+ string,
638
+ unknown
639
+ >;
640
+ if (typeof manager.reduceOverflowOneRung !== "function") {
641
+ Object.assign(manager, makeOverflowLadderStub());
642
+ }
602
643
  fakeContextWindowManagers.set(conversationId, ctx.contextWindowManager);
603
644
  return ctx;
604
645
  }
@@ -114,39 +114,70 @@ mock.module("../context/token-estimator.js", () => ({
114
114
  let mockReducerStepFn:
115
115
  | ((msgs: Message[], cfg: unknown, state: unknown) => unknown)
116
116
  | null = null;
117
+ const makeInitialReducerState = () => ({
118
+ appliedTiers: [] as string[],
119
+ injectionMode: "full" as const,
120
+ exhausted: false,
121
+ });
122
+ const runMockReducer = async (
123
+ msgs: Message[],
124
+ cfg: unknown,
125
+ state: unknown,
126
+ ) => {
127
+ if (mockReducerStepFn) return mockReducerStepFn(msgs, cfg, state);
128
+ return {
129
+ messages: msgs,
130
+ tier: "forced_compaction",
131
+ state: {
132
+ appliedTiers: [
133
+ "forced_compaction",
134
+ "tool_result_truncation",
135
+ "media_stubbing",
136
+ "injection_downgrade",
137
+ ],
138
+ injectionMode: "full",
139
+ exhausted: true,
140
+ },
141
+ estimatedTokens: 1000,
142
+ };
143
+ };
117
144
  mock.module(
118
145
  "../plugins/defaults/compaction/context-overflow-reducer.js",
119
146
  () => ({
120
- createInitialReducerState: () => ({
121
- appliedTiers: [],
122
- injectionMode: "full" as const,
123
- exhausted: false,
124
- }),
125
- reduceContextOverflow: async (
126
- msgs: Message[],
127
- cfg: unknown,
128
- state: unknown,
129
- ) => {
130
- if (mockReducerStepFn) return mockReducerStepFn(msgs, cfg, state);
131
- return {
132
- messages: msgs,
133
- tier: "forced_compaction",
134
- state: {
135
- appliedTiers: [
136
- "forced_compaction",
137
- "tool_result_truncation",
138
- "media_stubbing",
139
- "injection_downgrade",
140
- ],
141
- injectionMode: "full",
142
- exhausted: true,
143
- },
144
- estimatedTokens: 1000,
145
- };
146
- },
147
+ createInitialReducerState: makeInitialReducerState,
148
+ reduceContextOverflow: runMockReducer,
147
149
  }),
148
150
  );
149
151
 
152
+ // Stand-in for `ContextWindowManager`'s turn-scoped overflow ladder. Threads
153
+ // reducer state across a turn's rungs and delegates each rung to the mocked
154
+ // reducer, mirroring `reduceOverflowOneRung` / `resetOverflowRecovery` so the
155
+ // convergence driver exercises the same per-rung escalation the real manager
156
+ // drives.
157
+ function makeOverflowLadderStub(): {
158
+ resetOverflowRecovery: () => void;
159
+ reduceOverflowOneRung: (
160
+ msgs: Message[],
161
+ opts: unknown,
162
+ signal?: AbortSignal,
163
+ ) => Promise<unknown>;
164
+ } {
165
+ let state: unknown;
166
+ return {
167
+ resetOverflowRecovery: () => {
168
+ state = undefined;
169
+ },
170
+ reduceOverflowOneRung: async (msgs: Message[], opts: unknown) => {
171
+ if (!state) state = makeInitialReducerState();
172
+ const step = (await runMockReducer(msgs, opts, state)) as {
173
+ state: unknown;
174
+ };
175
+ state = step.state;
176
+ return step;
177
+ },
178
+ };
179
+ }
180
+
150
181
  // Policy: default to fail_gracefully
151
182
  let mockOverflowAction: string = "fail_gracefully";
152
183
  mock.module("../daemon/context-overflow-policy.js", () => ({
@@ -716,6 +747,16 @@ function makeCtx(
716
747
 
717
748
  ...ctxOverrides,
718
749
  } as unknown as Conversation;
750
+ // The convergence driver resolves the turn-scoped overflow ladder off the
751
+ // manager; give every fake manager the ladder methods unless a test supplied
752
+ // its own.
753
+ const manager = ctx.contextWindowManager as unknown as Record<
754
+ string,
755
+ unknown
756
+ >;
757
+ if (typeof manager.reduceOverflowOneRung !== "function") {
758
+ Object.assign(manager, makeOverflowLadderStub());
759
+ }
719
760
  fakeContextWindowManagers.set(conversationId, ctx.contextWindowManager);
720
761
  return ctx;
721
762
  }
@@ -174,6 +174,7 @@ mock.module("../plugins/defaults/compaction/window-manager.js", () => ({
174
174
  async maybeCompact() {
175
175
  return { compacted: false };
176
176
  }
177
+ resetOverflowRecovery() {}
177
178
  },
178
179
  createContextSummaryMessage: () => ({
179
180
  role: "user",
@@ -63,6 +63,12 @@ import {
63
63
  toolInvocations,
64
64
  } from "../memory/schema.js";
65
65
  import { hydrate as hydrateActivationState } from "../memory/v2/activation-store.js";
66
+ import {
67
+ getInjected as getV3Injected,
68
+ markPruned as markV3Pruned,
69
+ MEMORY_V3_INJECTED_BLOCK_METADATA_KEY,
70
+ recordInjected as recordV3Injected,
71
+ } from "../plugins/defaults/memory-v3-shadow/ever-injected-store.js";
66
72
 
67
73
  initializeDb();
68
74
 
@@ -77,6 +83,7 @@ function resetTables(): void {
77
83
  db.delete(llmRequestLogs).run();
78
84
  db.delete(toolInvocations).run();
79
85
  db.delete(memoryJobs).run();
86
+ db.run("DELETE FROM memory_v3_ever_injected");
80
87
  db.run("DELETE FROM message_attachments");
81
88
  db.run("DELETE FROM attachments");
82
89
  db.run("DELETE FROM messages");
@@ -1032,6 +1039,148 @@ describe("forkConversation", () => {
1032
1039
  ]);
1033
1040
  });
1034
1041
 
1042
+ test("copies the parent's memory-v3 everInjected record into a full fork", async () => {
1043
+ const source = createConversation("V3 carry thread");
1044
+ await addMessage(source.id, "user", "first turn", { skipIndexing: true });
1045
+
1046
+ recordV3Injected(
1047
+ source.id,
1048
+ [
1049
+ { slug: "topics/page-a", bytes: 120 },
1050
+ { slug: "topics/page-b", bytes: 340 },
1051
+ ],
1052
+ 1_700_000_000_000,
1053
+ );
1054
+ markV3Pruned(source.id, ["topics/page-b"], 1_700_000_001_000);
1055
+
1056
+ const fork = forkConversation({ conversationId: source.id });
1057
+
1058
+ // Full-row copy, pruned state included.
1059
+ expect(getV3Injected(fork.id)).toEqual(
1060
+ new Map([
1061
+ ["topics/page-a", { bytes: 120, prunedAt: null }],
1062
+ ["topics/page-b", { bytes: 340, prunedAt: 1_700_000_001_000 }],
1063
+ ]),
1064
+ );
1065
+ // Parent record is untouched.
1066
+ expect(getV3Injected(source.id).size).toBe(2);
1067
+ });
1068
+
1069
+ test("leaves the fork's memory-v3 record empty when the parent has none", async () => {
1070
+ const source = createConversation("V3 pristine thread");
1071
+ await addMessage(source.id, "user", "first message", {
1072
+ skipIndexing: true,
1073
+ });
1074
+
1075
+ const fork = forkConversation({ conversationId: source.id });
1076
+
1077
+ expect(getV3Injected(fork.id).size).toBe(0);
1078
+ });
1079
+
1080
+ test("truncated fork seeds the memory-v3 record from inherited v3 card blocks", async () => {
1081
+ const source = createConversation("V3 truncated seed thread");
1082
+ await addMessage(source.id, "user", "first turn", {
1083
+ metadata: {
1084
+ [MEMORY_V3_INJECTED_BLOCK_METADATA_KEY]:
1085
+ "# memory/concepts/topics/page-a.md\nCard A\n\n# memory/concepts/topics/page-b.md\nCard B",
1086
+ // A v2 block on the same message must seed only the v2 record.
1087
+ memoryInjectedBlock: "# memory/concepts/topics/page-v2.md\nSummary",
1088
+ },
1089
+ skipIndexing: true,
1090
+ });
1091
+ await addMessage(source.id, "assistant", "first reply", {
1092
+ skipIndexing: true,
1093
+ });
1094
+ const boundaryMessage = await addMessage(source.id, "user", "second turn", {
1095
+ skipIndexing: true,
1096
+ });
1097
+ await addMessage(source.id, "assistant", "second reply", {
1098
+ skipIndexing: true,
1099
+ });
1100
+ // Past the fork boundary — its card must NOT be claimed.
1101
+ await addMessage(source.id, "user", "third turn", {
1102
+ metadata: {
1103
+ [MEMORY_V3_INJECTED_BLOCK_METADATA_KEY]:
1104
+ "# memory/concepts/topics/page-c.md\nCard C",
1105
+ },
1106
+ skipIndexing: true,
1107
+ });
1108
+
1109
+ recordV3Injected(
1110
+ source.id,
1111
+ [
1112
+ { slug: "topics/page-a", bytes: 120 },
1113
+ { slug: "topics/page-b", bytes: 340 },
1114
+ { slug: "topics/page-c", bytes: 90 },
1115
+ ],
1116
+ 1_700_000_000_000,
1117
+ );
1118
+
1119
+ const fork = forkConversation({
1120
+ conversationId: source.id,
1121
+ throughMessageId: boundaryMessage.id,
1122
+ });
1123
+
1124
+ // Exactly the slugs whose card blocks live in the copied history,
1125
+ // dedup-only (`bytes = 0` — resident accounting restarts on the child).
1126
+ expect(getV3Injected(fork.id)).toEqual(
1127
+ new Map([
1128
+ ["topics/page-a", { bytes: 0, prunedAt: null }],
1129
+ ["topics/page-b", { bytes: 0, prunedAt: null }],
1130
+ ]),
1131
+ );
1132
+ // The v2 seed picked up only the v2 block, not the v3 cards.
1133
+ const childState = await hydrateActivationState(getDb(), fork.id);
1134
+ expect(childState?.everInjected.map((e) => e.slug)).toEqual([
1135
+ "topics/page-v2",
1136
+ ]);
1137
+ });
1138
+
1139
+ test("truncated fork carries the parent's pruned tombstones for inherited v3 slugs", async () => {
1140
+ // Pruning never rewrites the persisted metadata block, so the fork scan
1141
+ // sees pruned cards' sections too — the seed must tombstone them, or the
1142
+ // child's rehydration would resurrect cards the parent's live view lost.
1143
+ const source = createConversation("V3 truncated pruned thread");
1144
+ await addMessage(source.id, "user", "first turn", {
1145
+ metadata: {
1146
+ [MEMORY_V3_INJECTED_BLOCK_METADATA_KEY]:
1147
+ "# memory/concepts/topics/page-a.md\nCard A\n\n# memory/concepts/topics/page-b.md\nCard B",
1148
+ },
1149
+ skipIndexing: true,
1150
+ });
1151
+ const boundaryMessage = await addMessage(
1152
+ source.id,
1153
+ "assistant",
1154
+ "first reply",
1155
+ { skipIndexing: true },
1156
+ );
1157
+ // Past the fork boundary — forces the truncated-seed path (a fork through
1158
+ // the final message takes the wholesale-copy path instead).
1159
+ await addMessage(source.id, "user", "second turn", { skipIndexing: true });
1160
+
1161
+ recordV3Injected(
1162
+ source.id,
1163
+ [
1164
+ { slug: "topics/page-a", bytes: 120 },
1165
+ { slug: "topics/page-b", bytes: 340 },
1166
+ ],
1167
+ 1_700_000_000_000,
1168
+ );
1169
+ markV3Pruned(source.id, ["topics/page-b"], 1_700_000_005_000);
1170
+
1171
+ const fork = forkConversation({
1172
+ conversationId: source.id,
1173
+ throughMessageId: boundaryMessage.id,
1174
+ });
1175
+
1176
+ expect(getV3Injected(fork.id)).toEqual(
1177
+ new Map([
1178
+ ["topics/page-a", { bytes: 0, prunedAt: null }],
1179
+ ["topics/page-b", { bytes: 0, prunedAt: 1_700_000_005_000 }],
1180
+ ]),
1181
+ );
1182
+ });
1183
+
1035
1184
  test("defaults conversationType to standard and inherits the parent's group", async () => {
1036
1185
  const source = createConversation("Default inheritance thread");
1037
1186
  await addMessage(source.id, "user", "first message", {