@vellumai/assistant 0.10.7-dev.202607091656.6914014 → 0.10.7-dev.202607091742.1ef1be3
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.
- package/package.json +1 -1
- package/src/__tests__/conversation-runtime-assembly.test.ts +28 -5
- package/src/__tests__/injector-chain.test.ts +12 -0
- package/src/__tests__/injector-pkb-v2-silenced.test.ts +8 -0
- package/src/__tests__/memory-config-file-parity.test.ts +187 -0
- package/src/__tests__/plugin-import-boundary-guard.test.ts +1 -0
- package/src/config/schema.ts +0 -49
- package/src/config/schemas/memory.ts +57 -1
- package/src/plugins/defaults/memory/__tests__/memory-retrospective-startup-cleanup.test.ts +3 -5
- package/src/plugins/defaults/memory/config.ts +155 -6
- package/src/plugins/defaults/memory/pkb/pkb-search.test.ts +2 -2
- package/src/plugins/defaults/memory/v2/__tests__/static-context.test.ts +9 -6
- package/src/plugins/defaults/memory/v2/static-context.ts +6 -11
- package/src/plugins/defaults/memory/v3/__tests__/carry-integration.test.ts +19 -0
- package/src/plugins/defaults/memory/v3/__tests__/injection.test.ts +17 -0
- package/src/plugins/defaults/memory/v3/prune.test.ts +10 -0
package/package.json
CHANGED
|
@@ -10,6 +10,10 @@ import { eq } from "drizzle-orm";
|
|
|
10
10
|
// injection chain assertions stay meaningful.
|
|
11
11
|
const realLoaderForAssemblyTest = await import("../config/loader.js");
|
|
12
12
|
const realGetConfigForAssemblyTest = realLoaderForAssemblyTest.getConfig;
|
|
13
|
+
const realMemoryConfigForAssemblyTest =
|
|
14
|
+
await import("../plugins/defaults/memory/config.js");
|
|
15
|
+
const realGetMemoryConfigForAssemblyTest =
|
|
16
|
+
realMemoryConfigForAssemblyTest.getMemoryConfig;
|
|
13
17
|
// Per-test overrides for `ui.userTimezone` / `ui.detectedTimezone` so the
|
|
14
18
|
// config self-resolution inside `applyRuntimeInjections` can be exercised.
|
|
15
19
|
let assemblyConfiguredUserTimezone: string | null | undefined;
|
|
@@ -33,6 +37,15 @@ mock.module("../config/loader.js", () => ({
|
|
|
33
37
|
},
|
|
34
38
|
}));
|
|
35
39
|
|
|
40
|
+
// Memory code resolves its config through the plugin's own accessor, not
|
|
41
|
+
// getConfig(); mirror the v2-disabled override there.
|
|
42
|
+
mock.module("../plugins/defaults/memory/config.js", () => ({
|
|
43
|
+
getMemoryConfig: () => {
|
|
44
|
+
const real = realGetMemoryConfigForAssemblyTest();
|
|
45
|
+
return { ...real, v2: { ...real.v2, enabled: false } };
|
|
46
|
+
},
|
|
47
|
+
}));
|
|
48
|
+
|
|
36
49
|
// `resolveTurnInboundActorContext` reads the INFO `notes` field via a local
|
|
37
50
|
// contactId join (interaction count now comes from the gateway verdict). Stub
|
|
38
51
|
// the join so the actor-context tests can stage it without standing up the
|
|
@@ -75,7 +88,9 @@ let pkbSearchResults: Array<{
|
|
|
75
88
|
let pkbSearchThrows: Error | null = null;
|
|
76
89
|
mock.module("../plugins/defaults/memory/pkb/pkb-search.js", () => ({
|
|
77
90
|
searchPkbFiles: async () => {
|
|
78
|
-
if (pkbSearchThrows)
|
|
91
|
+
if (pkbSearchThrows) {
|
|
92
|
+
throw pkbSearchThrows;
|
|
93
|
+
}
|
|
79
94
|
return pkbSearchResults;
|
|
80
95
|
},
|
|
81
96
|
}));
|
|
@@ -3194,7 +3209,9 @@ describe("Slack channel chronological rendering — multi-thread", () => {
|
|
|
3194
3209
|
const outer: Record<string, unknown> = {
|
|
3195
3210
|
...(opts.extraOuterMetadata ?? {}),
|
|
3196
3211
|
};
|
|
3197
|
-
if (opts.slackMeta)
|
|
3212
|
+
if (opts.slackMeta) {
|
|
3213
|
+
outer.slackMeta = writeSlackMetadata(opts.slackMeta);
|
|
3214
|
+
}
|
|
3198
3215
|
return {
|
|
3199
3216
|
id: opts.id,
|
|
3200
3217
|
conversationId: "conv-1",
|
|
@@ -3213,7 +3230,9 @@ describe("Slack channel chronological rendering — multi-thread", () => {
|
|
|
3213
3230
|
slackMeta?: SlackMessageMetadata;
|
|
3214
3231
|
}): MessageRow {
|
|
3215
3232
|
const outer: Record<string, unknown> = {};
|
|
3216
|
-
if (opts.slackMeta)
|
|
3233
|
+
if (opts.slackMeta) {
|
|
3234
|
+
outer.slackMeta = writeSlackMetadata(opts.slackMeta);
|
|
3235
|
+
}
|
|
3217
3236
|
return {
|
|
3218
3237
|
id: opts.id,
|
|
3219
3238
|
conversationId: "conv-1",
|
|
@@ -3261,7 +3280,9 @@ describe("Slack channel chronological rendering — multi-thread", () => {
|
|
|
3261
3280
|
return messages.map((m) => {
|
|
3262
3281
|
for (let i = m.content.length - 1; i >= 0; i--) {
|
|
3263
3282
|
const block = m.content[i];
|
|
3264
|
-
if (block.type === "text")
|
|
3283
|
+
if (block.type === "text") {
|
|
3284
|
+
return block.text;
|
|
3285
|
+
}
|
|
3265
3286
|
}
|
|
3266
3287
|
return "";
|
|
3267
3288
|
});
|
|
@@ -4811,7 +4832,9 @@ describe("assembleSlackActiveThreadFocusBlock", () => {
|
|
|
4811
4832
|
|
|
4812
4833
|
function envelope(meta: SlackMessageMetadata | null): string {
|
|
4813
4834
|
const outer: Record<string, unknown> = {};
|
|
4814
|
-
if (meta)
|
|
4835
|
+
if (meta) {
|
|
4836
|
+
outer.slackMeta = writeSlackMetadata(meta);
|
|
4837
|
+
}
|
|
4815
4838
|
return JSON.stringify(outer);
|
|
4816
4839
|
}
|
|
4817
4840
|
|
|
@@ -39,6 +39,18 @@ mock.module("../config/loader.js", () => ({
|
|
|
39
39
|
},
|
|
40
40
|
}));
|
|
41
41
|
|
|
42
|
+
// Memory code resolves its config through the plugin's own accessor, not
|
|
43
|
+
// getConfig(); mirror the v2-disabled override there.
|
|
44
|
+
const realMemoryConfigModule =
|
|
45
|
+
await import("../plugins/defaults/memory/config.js");
|
|
46
|
+
const realGetMemoryConfig = realMemoryConfigModule.getMemoryConfig;
|
|
47
|
+
mock.module("../plugins/defaults/memory/config.js", () => ({
|
|
48
|
+
getMemoryConfig: () => {
|
|
49
|
+
const real = realGetMemoryConfig();
|
|
50
|
+
return { ...real, v2: { ...real.v2, enabled: false } };
|
|
51
|
+
},
|
|
52
|
+
}));
|
|
53
|
+
|
|
42
54
|
// `applyRuntimeInjections` computes the `<turn_context>` `current_time` live
|
|
43
55
|
// via `formatTurnTimestamp`; pin it so the rendered block matches the
|
|
44
56
|
// deterministic `buildUnifiedTurnContextBlock` expectations below. The rest of
|
|
@@ -30,6 +30,14 @@ mock.module("../config/loader.js", () => ({
|
|
|
30
30
|
}),
|
|
31
31
|
}));
|
|
32
32
|
|
|
33
|
+
// The PKB silencing gate reads the plugin's own config accessor.
|
|
34
|
+
mock.module("../plugins/defaults/memory/config.js", () => ({
|
|
35
|
+
getMemoryConfig: () => ({
|
|
36
|
+
v2: { enabled: v2Active },
|
|
37
|
+
retrieval: { scratchpadInjection: { enabled: true } },
|
|
38
|
+
}),
|
|
39
|
+
}));
|
|
40
|
+
|
|
33
41
|
mock.module("../plugins/defaults/memory/pkb/pkb-search.js", () => ({
|
|
34
42
|
searchPkbFiles: async () => [],
|
|
35
43
|
}));
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locks the memory plugin's file-based config resolution (`getMemoryConfig`)
|
|
3
|
+
* to the host loader's memory slice (`getConfig().memory`): same schema
|
|
4
|
+
* defaults, same per-field invalid fallback, same deployment-context fill.
|
|
5
|
+
*
|
|
6
|
+
* The plugin reads workspace/config.json's `memory` field itself; these
|
|
7
|
+
* cases document that both resolutions agree on every file state. If the
|
|
8
|
+
* loader's semantics change, this is the tripwire.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
15
|
+
|
|
16
|
+
import { getConfig } from "../config/loader.js";
|
|
17
|
+
import { getMemoryConfig } from "../plugins/defaults/memory/config.js";
|
|
18
|
+
import { getWorkspaceConfigPath } from "../util/platform.js";
|
|
19
|
+
|
|
20
|
+
const configPath = getWorkspaceConfigPath();
|
|
21
|
+
const savedIsPlatform = process.env.IS_PLATFORM;
|
|
22
|
+
|
|
23
|
+
function writeConfig(value: unknown): void {
|
|
24
|
+
writeFileSync(configPath, JSON.stringify(value, null, 2));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function resetState(): void {
|
|
28
|
+
rmSync(configPath, { force: true });
|
|
29
|
+
delete process.env.IS_PLATFORM;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Assert the plugin resolution deep-equals the host loader's slice. */
|
|
33
|
+
function expectParity(): ReturnType<typeof getMemoryConfig> {
|
|
34
|
+
const plugin = getMemoryConfig();
|
|
35
|
+
expect(plugin).toEqual(getConfig().memory);
|
|
36
|
+
return plugin;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe("memory config file parity", () => {
|
|
40
|
+
beforeEach(resetState);
|
|
41
|
+
afterEach(() => {
|
|
42
|
+
resetState();
|
|
43
|
+
if (savedIsPlatform !== undefined) {
|
|
44
|
+
process.env.IS_PLATFORM = savedIsPlatform;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("absent config.json resolves to schema defaults, without creating the file", () => {
|
|
49
|
+
const memory = getMemoryConfig();
|
|
50
|
+
expect(existsSync(configPath)).toBe(false); // side-effect-free read
|
|
51
|
+
expect(memory.enabled).not.toBeUndefined();
|
|
52
|
+
expectParity(); // getConfig() may seed the file; values still agree
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("custom knobs are visible and identical", () => {
|
|
56
|
+
writeConfig({ memory: { enabled: false, v2: { bm25_b: 0.9 } } });
|
|
57
|
+
const memory = expectParity();
|
|
58
|
+
expect(memory.enabled).toBe(false);
|
|
59
|
+
expect(memory.v2.bm25_b).toBe(0.9);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("an invalid leaf falls back per-field; valid siblings survive", () => {
|
|
63
|
+
writeConfig({ memory: { enabled: false, v2: { bm25_b: "high" } } });
|
|
64
|
+
const memory = expectParity();
|
|
65
|
+
expect(memory.enabled).toBe(false); // sibling customization survives
|
|
66
|
+
expect(typeof memory.v2.bm25_b).toBe("number"); // invalid leaf → default
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("unknown keys strip silently", () => {
|
|
70
|
+
writeConfig({ memory: { retiredKnob: 123, enabled: false } });
|
|
71
|
+
const memory = expectParity();
|
|
72
|
+
expect(memory.enabled).toBe(false);
|
|
73
|
+
expect("retiredKnob" in memory).toBe(false);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("a non-object memory field resolves to defaults", () => {
|
|
77
|
+
writeConfig({ memory: 5 });
|
|
78
|
+
expectParity();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("unparseable config.json resolves to defaults", () => {
|
|
82
|
+
writeFileSync(configPath, "{ not json");
|
|
83
|
+
const memory = getMemoryConfig();
|
|
84
|
+
expect(memory.enabled).not.toBeUndefined();
|
|
85
|
+
// getConfig() quarantines the corrupt file (a host-owned side effect);
|
|
86
|
+
// after that both resolutions see the same absent-file state.
|
|
87
|
+
expectParity();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("platform deployment fill applies when the leaf is absent on disk", () => {
|
|
91
|
+
process.env.IS_PLATFORM = "true";
|
|
92
|
+
writeConfig({ memory: {} });
|
|
93
|
+
const memory = expectParity();
|
|
94
|
+
expect(memory.embeddings.provider).toBe("gemini");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("an explicit on-disk provider beats the platform fill", () => {
|
|
98
|
+
process.env.IS_PLATFORM = "true";
|
|
99
|
+
writeConfig({ memory: { embeddings: { provider: "openai" } } });
|
|
100
|
+
const memory = expectParity();
|
|
101
|
+
expect(memory.embeddings.provider).toBe("openai");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("cross-field invariant: segmentation overlap >= target falls back like the loader", () => {
|
|
105
|
+
writeConfig({
|
|
106
|
+
memory: {
|
|
107
|
+
enabled: false,
|
|
108
|
+
segmentation: { targetTokens: 100, overlapTokens: 200 },
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
const memory = expectParity();
|
|
112
|
+
expect(memory.enabled).toBe(false); // valid sibling survives
|
|
113
|
+
expect(memory.segmentation.overlapTokens).toBeLessThan(
|
|
114
|
+
memory.segmentation.targetTokens,
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("cross-field invariant: dynamicBudget min > max falls back like the loader", () => {
|
|
119
|
+
writeConfig({
|
|
120
|
+
memory: {
|
|
121
|
+
retrieval: {
|
|
122
|
+
dynamicBudget: { minInjectTokens: 5000, maxInjectTokens: 100 },
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
const memory = expectParity();
|
|
127
|
+
expect(memory.retrieval.dynamicBudget.minInjectTokens).toBeLessThanOrEqual(
|
|
128
|
+
memory.retrieval.dynamicBudget.maxInjectTokens,
|
|
129
|
+
);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("cross-field invariant: injection reserves >= maxNodes fall back like the loader", () => {
|
|
133
|
+
writeConfig({
|
|
134
|
+
memory: {
|
|
135
|
+
retrieval: {
|
|
136
|
+
injection: {
|
|
137
|
+
contextLoad: {
|
|
138
|
+
capabilityReserve: 10,
|
|
139
|
+
serendipitySlots: 10,
|
|
140
|
+
maxNodes: 5,
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
const memory = expectParity();
|
|
147
|
+
const ctxLoad = memory.retrieval.injection.contextLoad;
|
|
148
|
+
expect(ctxLoad.capabilityReserve + ctxLoad.serendipitySlots).toBeLessThan(
|
|
149
|
+
ctxLoad.maxNodes,
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("switching VELLUM_WORKSPACE_DIR busts the cache (path is part of the key)", () => {
|
|
154
|
+
const savedWorkspace = process.env.VELLUM_WORKSPACE_DIR;
|
|
155
|
+
try {
|
|
156
|
+
writeConfig({ memory: { enabled: false } });
|
|
157
|
+
expect(getMemoryConfig().enabled).toBe(false);
|
|
158
|
+
|
|
159
|
+
const otherWorkspace = mkdtempSync(join(tmpdir(), "vellum-parity-ws-"));
|
|
160
|
+
process.env.VELLUM_WORKSPACE_DIR = otherWorkspace;
|
|
161
|
+
// The other workspace has no config.json → schema defaults, not the
|
|
162
|
+
// previous workspace's cached slice.
|
|
163
|
+
expect(getMemoryConfig().enabled).toBe(true);
|
|
164
|
+
expectParity();
|
|
165
|
+
|
|
166
|
+
writeFileSync(
|
|
167
|
+
join(otherWorkspace, "config.json"),
|
|
168
|
+
JSON.stringify({ memory: { v2: { enabled: false } } }),
|
|
169
|
+
);
|
|
170
|
+
expect(getMemoryConfig().v2.enabled).toBe(false);
|
|
171
|
+
expectParity();
|
|
172
|
+
} finally {
|
|
173
|
+
process.env.VELLUM_WORKSPACE_DIR = savedWorkspace;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("the cache serves the same object until the file changes", () => {
|
|
178
|
+
writeConfig({ memory: { enabled: false } });
|
|
179
|
+
const first = getMemoryConfig();
|
|
180
|
+
expect(getMemoryConfig()).toBe(first);
|
|
181
|
+
writeConfig({ memory: { enabled: true } });
|
|
182
|
+
const second = getMemoryConfig();
|
|
183
|
+
expect(second).not.toBe(first);
|
|
184
|
+
expect(second.enabled).toBe(true);
|
|
185
|
+
expectParity();
|
|
186
|
+
});
|
|
187
|
+
});
|
|
@@ -161,6 +161,7 @@ const BASELINE: Record<string, readonly string[]> = {
|
|
|
161
161
|
"../../../config/memory-v3-gate.js",
|
|
162
162
|
"../../../config/schema.js",
|
|
163
163
|
"../../../config/schemas/memory-v2.js",
|
|
164
|
+
"../../../config/schemas/memory.js",
|
|
164
165
|
"../../../config/types.js",
|
|
165
166
|
"../../../contacts/guardian-delivery-reader.js",
|
|
166
167
|
"../../../context/compactor.js",
|
package/src/config/schema.ts
CHANGED
|
@@ -194,55 +194,6 @@ export const AssistantConfigSchema = z
|
|
|
194
194
|
"llm.default.contextWindow.targetBudgetRatio must be greater than llm.default.contextWindow.summaryBudgetRatio",
|
|
195
195
|
});
|
|
196
196
|
}
|
|
197
|
-
const segmentation = config.memory?.segmentation;
|
|
198
|
-
if (
|
|
199
|
-
segmentation &&
|
|
200
|
-
segmentation.overlapTokens >= segmentation.targetTokens
|
|
201
|
-
) {
|
|
202
|
-
ctx.addIssue({
|
|
203
|
-
code: z.ZodIssueCode.custom,
|
|
204
|
-
path: ["memory", "segmentation", "overlapTokens"],
|
|
205
|
-
message:
|
|
206
|
-
"memory.segmentation.overlapTokens must be less than memory.segmentation.targetTokens",
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
const dynamicBudget = config.memory?.retrieval?.dynamicBudget;
|
|
210
|
-
if (
|
|
211
|
-
dynamicBudget &&
|
|
212
|
-
dynamicBudget.minInjectTokens > dynamicBudget.maxInjectTokens
|
|
213
|
-
) {
|
|
214
|
-
ctx.addIssue({
|
|
215
|
-
code: z.ZodIssueCode.custom,
|
|
216
|
-
path: ["memory", "retrieval", "dynamicBudget"],
|
|
217
|
-
message:
|
|
218
|
-
"memory.retrieval.dynamicBudget.minInjectTokens must be <= memory.retrieval.dynamicBudget.maxInjectTokens",
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
const injection = config.memory?.retrieval?.injection;
|
|
222
|
-
const ctxLoad = injection?.contextLoad;
|
|
223
|
-
if (
|
|
224
|
-
ctxLoad &&
|
|
225
|
-
ctxLoad.capabilityReserve + ctxLoad.serendipitySlots >= ctxLoad.maxNodes
|
|
226
|
-
) {
|
|
227
|
-
ctx.addIssue({
|
|
228
|
-
code: z.ZodIssueCode.custom,
|
|
229
|
-
path: ["memory", "retrieval", "injection", "contextLoad"],
|
|
230
|
-
message:
|
|
231
|
-
"memory.retrieval.injection.contextLoad.capabilityReserve + serendipitySlots must be less than maxNodes",
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
const perTurn = injection?.perTurn;
|
|
235
|
-
if (
|
|
236
|
-
perTurn &&
|
|
237
|
-
perTurn.capabilityReserve + perTurn.serendipitySlots >= perTurn.maxNodes
|
|
238
|
-
) {
|
|
239
|
-
ctx.addIssue({
|
|
240
|
-
code: z.ZodIssueCode.custom,
|
|
241
|
-
path: ["memory", "retrieval", "injection", "perTurn"],
|
|
242
|
-
message:
|
|
243
|
-
"memory.retrieval.injection.perTurn.capabilityReserve + serendipitySlots must be less than maxNodes",
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
197
|
});
|
|
247
198
|
|
|
248
199
|
export type AssistantConfig = z.infer<typeof AssistantConfigSchema>;
|
|
@@ -66,6 +66,62 @@ export const MemoryConfigSchema = z
|
|
|
66
66
|
})
|
|
67
67
|
.describe(
|
|
68
68
|
"Long-term memory system — stores, retrieves, and manages persistent knowledge across conversations",
|
|
69
|
-
)
|
|
69
|
+
)
|
|
70
|
+
// Cross-field memory invariants live on this schema (not the assistant
|
|
71
|
+
// schema's refinement) so every parse of the memory slice enforces them —
|
|
72
|
+
// the full-config parse via AssistantConfigSchema and the memory plugin's
|
|
73
|
+
// own slice parse alike. Parent parses prefix issue paths with "memory",
|
|
74
|
+
// preserving the loader's per-field fallback paths.
|
|
75
|
+
.superRefine((config, ctx) => {
|
|
76
|
+
const segmentation = config.segmentation;
|
|
77
|
+
if (
|
|
78
|
+
segmentation &&
|
|
79
|
+
segmentation.overlapTokens >= segmentation.targetTokens
|
|
80
|
+
) {
|
|
81
|
+
ctx.addIssue({
|
|
82
|
+
code: z.ZodIssueCode.custom,
|
|
83
|
+
path: ["segmentation", "overlapTokens"],
|
|
84
|
+
message:
|
|
85
|
+
"memory.segmentation.overlapTokens must be less than memory.segmentation.targetTokens",
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
const dynamicBudget = config.retrieval?.dynamicBudget;
|
|
89
|
+
if (
|
|
90
|
+
dynamicBudget &&
|
|
91
|
+
dynamicBudget.minInjectTokens > dynamicBudget.maxInjectTokens
|
|
92
|
+
) {
|
|
93
|
+
ctx.addIssue({
|
|
94
|
+
code: z.ZodIssueCode.custom,
|
|
95
|
+
path: ["retrieval", "dynamicBudget"],
|
|
96
|
+
message:
|
|
97
|
+
"memory.retrieval.dynamicBudget.minInjectTokens must be <= memory.retrieval.dynamicBudget.maxInjectTokens",
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
const injection = config.retrieval?.injection;
|
|
101
|
+
const ctxLoad = injection?.contextLoad;
|
|
102
|
+
if (
|
|
103
|
+
ctxLoad &&
|
|
104
|
+
ctxLoad.capabilityReserve + ctxLoad.serendipitySlots >= ctxLoad.maxNodes
|
|
105
|
+
) {
|
|
106
|
+
ctx.addIssue({
|
|
107
|
+
code: z.ZodIssueCode.custom,
|
|
108
|
+
path: ["retrieval", "injection", "contextLoad"],
|
|
109
|
+
message:
|
|
110
|
+
"memory.retrieval.injection.contextLoad.capabilityReserve + serendipitySlots must be less than maxNodes",
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
const perTurn = injection?.perTurn;
|
|
114
|
+
if (
|
|
115
|
+
perTurn &&
|
|
116
|
+
perTurn.capabilityReserve + perTurn.serendipitySlots >= perTurn.maxNodes
|
|
117
|
+
) {
|
|
118
|
+
ctx.addIssue({
|
|
119
|
+
code: z.ZodIssueCode.custom,
|
|
120
|
+
path: ["retrieval", "injection", "perTurn"],
|
|
121
|
+
message:
|
|
122
|
+
"memory.retrieval.injection.perTurn.capabilityReserve + serendipitySlots must be less than maxNodes",
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
});
|
|
70
126
|
|
|
71
127
|
export type MemoryConfig = z.infer<typeof MemoryConfigSchema>;
|
|
@@ -121,11 +121,9 @@ let activeJobSourceConvIds = new Set<string>();
|
|
|
121
121
|
let injectedNowMinusOrphanAgeMs = 0;
|
|
122
122
|
let mockKeepSupersededRuns = false;
|
|
123
123
|
|
|
124
|
-
mock.module("
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
retrospective: { keepSupersededRuns: mockKeepSupersededRuns },
|
|
128
|
-
},
|
|
124
|
+
mock.module("../config.js", () => ({
|
|
125
|
+
getMemoryConfig: () => ({
|
|
126
|
+
retrospective: { keepSupersededRuns: mockKeepSupersededRuns },
|
|
129
127
|
}),
|
|
130
128
|
}));
|
|
131
129
|
|
|
@@ -1,11 +1,160 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
import type { MemoryConfig } from "../../../config/schemas/memory.js";
|
|
4
|
+
import { MemoryConfigSchema } from "../../../config/schemas/memory.js";
|
|
5
|
+
import { getLogger } from "../../../util/logger.js";
|
|
6
|
+
import { getWorkspaceConfigPath } from "../../../util/platform.js";
|
|
7
|
+
|
|
8
|
+
const log = getLogger("memory-config");
|
|
9
|
+
|
|
10
|
+
let cached: MemoryConfig | null = null;
|
|
11
|
+
let cachedSignature: string | null = null;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Cheap freshness key for the config file, mirroring the host loader's
|
|
15
|
+
* signature check (path + size + mtime + ctime). The path is part of the key
|
|
16
|
+
* so a process that switches workspaces (tests, CLI helpers) never serves one
|
|
17
|
+
* workspace's cached slice for another's. "absent" when the file is missing
|
|
18
|
+
* or unreadable.
|
|
19
|
+
*/
|
|
20
|
+
function configFileSignature(path: string): string {
|
|
21
|
+
try {
|
|
22
|
+
const s = statSync(path);
|
|
23
|
+
return `${path}:${s.size}:${s.mtimeMs}:${s.ctimeMs}`;
|
|
24
|
+
} catch {
|
|
25
|
+
return `${path}:absent`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The raw `memory` field of workspace/config.json, or `{}` when the file is
|
|
31
|
+
* missing, unparseable, or the field is not a plain object. Matches the host
|
|
32
|
+
* loader's effective outcome for those cases (it quarantines a corrupt file
|
|
33
|
+
* and proceeds with defaults); quarantine itself stays host-owned — this is
|
|
34
|
+
* a side-effect-free read.
|
|
35
|
+
*/
|
|
36
|
+
function readRawMemoryField(path: string): Record<string, unknown> {
|
|
37
|
+
let parsed: unknown;
|
|
38
|
+
try {
|
|
39
|
+
parsed = JSON.parse(readFileSync(path, "utf-8"));
|
|
40
|
+
} catch {
|
|
41
|
+
return {};
|
|
42
|
+
}
|
|
43
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
44
|
+
return {};
|
|
45
|
+
}
|
|
46
|
+
const memory = (parsed as Record<string, unknown>).memory;
|
|
47
|
+
if (memory === null || typeof memory !== "object" || Array.isArray(memory)) {
|
|
48
|
+
return {};
|
|
49
|
+
}
|
|
50
|
+
return memory as Record<string, unknown>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Parse the raw memory field through the schema, mirroring the host loader's
|
|
55
|
+
* per-field fallback: an invalid leaf is stripped so its schema default
|
|
56
|
+
* applies while valid sibling customizations survive. Results are
|
|
57
|
+
* structured-cloned because zod returns its precomputed default sub-objects
|
|
58
|
+
* by reference on every parse.
|
|
59
|
+
*
|
|
60
|
+
* No empty-ancestor pruning (unlike the host's full-config cleanup): every
|
|
61
|
+
* memory sub-schema parses `{}` to its full defaults, so an emptied object is
|
|
62
|
+
* equivalent to an absent one.
|
|
63
|
+
*/
|
|
64
|
+
function parseMemorySlice(raw: Record<string, unknown>): MemoryConfig {
|
|
65
|
+
const result = MemoryConfigSchema.safeParse(raw);
|
|
66
|
+
if (result.success) {
|
|
67
|
+
return structuredClone(result.data);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const cleaned = structuredClone(raw);
|
|
71
|
+
for (const issue of result.error.issues) {
|
|
72
|
+
const path = issue.path.join(".");
|
|
73
|
+
log.warn(
|
|
74
|
+
`Invalid memory config${path ? ` at "${path}"` : ""}: ${issue.message}. Falling back to default.`,
|
|
75
|
+
);
|
|
76
|
+
if (issue.path.length === 0) {
|
|
77
|
+
return structuredClone(MemoryConfigSchema.parse({}));
|
|
78
|
+
}
|
|
79
|
+
deleteLeaf(cleaned, issue.path as (string | number)[]);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const retry = MemoryConfigSchema.safeParse(cleaned);
|
|
83
|
+
if (retry.success) {
|
|
84
|
+
return structuredClone(retry.data);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
log.warn("Memory config validation failed after cleanup. Using defaults.");
|
|
88
|
+
return structuredClone(MemoryConfigSchema.parse({}));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Delete the leaf at `path`, tolerating already-missing ancestors. */
|
|
92
|
+
function deleteLeaf(
|
|
93
|
+
obj: Record<string, unknown>,
|
|
94
|
+
path: (string | number)[],
|
|
95
|
+
): void {
|
|
96
|
+
let node: unknown = obj;
|
|
97
|
+
for (const key of path.slice(0, -1)) {
|
|
98
|
+
if (node === null || typeof node !== "object") {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
node = (node as Record<string | number, unknown>)[key];
|
|
102
|
+
}
|
|
103
|
+
if (node === null || typeof node !== "object") {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
delete (node as Record<string | number, unknown>)[path.at(-1)!];
|
|
107
|
+
}
|
|
3
108
|
|
|
4
109
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
110
|
+
* Deployment-context fill for the memory slice, mirroring the memory entry of
|
|
111
|
+
* the host loader's `getDeploymentContextDefaults()`: platform-managed
|
|
112
|
+
* assistants (IS_PLATFORM) embed via the managed gemini backend unless the
|
|
113
|
+
* provider leaf is explicitly set on disk. Kept local so this module does not
|
|
114
|
+
* import the host loader (whose partial test mocks would otherwise break the
|
|
115
|
+
* import); the parity test compares against the real loader under
|
|
116
|
+
* IS_PLATFORM, so a divergent fill fails loudly there.
|
|
117
|
+
*/
|
|
118
|
+
function applyDeploymentContextFill(
|
|
119
|
+
memory: MemoryConfig,
|
|
120
|
+
raw: Record<string, unknown>,
|
|
121
|
+
): void {
|
|
122
|
+
if (process.env.IS_PLATFORM !== "true" && process.env.IS_PLATFORM !== "1") {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const rawEmbeddings = raw.embeddings;
|
|
126
|
+
const explicitProvider =
|
|
127
|
+
rawEmbeddings !== null &&
|
|
128
|
+
typeof rawEmbeddings === "object" &&
|
|
129
|
+
!Array.isArray(rawEmbeddings) &&
|
|
130
|
+
(rawEmbeddings as Record<string, unknown>).provider !== undefined;
|
|
131
|
+
if (!explicitProvider) {
|
|
132
|
+
memory.embeddings.provider = "gemini";
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* The `memory` slice of the workspace config, resolved by the plugin itself:
|
|
138
|
+
* reads workspace/config.json's `memory` field directly (signature-cached),
|
|
139
|
+
* applies schema defaults with the host loader's per-field invalid fallback,
|
|
140
|
+
* and layers the deployment-context fill for leaves absent on disk.
|
|
141
|
+
*
|
|
142
|
+
* Resolution agrees with the host loader's `getConfig().memory` —
|
|
143
|
+
* `memory-config-file-parity.test.ts` locks the two together. Unlike
|
|
144
|
+
* `getConfig()`, this never creates directories or seeds a config file.
|
|
8
145
|
*/
|
|
9
146
|
export function getMemoryConfig(): MemoryConfig {
|
|
10
|
-
|
|
147
|
+
const path = getWorkspaceConfigPath();
|
|
148
|
+
const signature = configFileSignature(path);
|
|
149
|
+
if (cached && cachedSignature === signature) {
|
|
150
|
+
return cached;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const raw = readRawMemoryField(path);
|
|
154
|
+
const memory = parseMemorySlice(raw);
|
|
155
|
+
applyDeploymentContextFill(memory, raw);
|
|
156
|
+
|
|
157
|
+
cached = memory;
|
|
158
|
+
cachedSignature = signature;
|
|
159
|
+
return memory;
|
|
11
160
|
}
|
|
@@ -5,8 +5,8 @@ import { makeMockLogger } from "../../../../__tests__/helpers/mock-logger.js";
|
|
|
5
5
|
// This test exercises the v1 PKB search path. `config.memory.v2.enabled`
|
|
6
6
|
// (default `true`) makes pkb-search short-circuit to keep traffic off the
|
|
7
7
|
// legacy collection — force it off so the v1 path stays under test.
|
|
8
|
-
mock.module("
|
|
9
|
-
|
|
8
|
+
mock.module("../config.js", () => ({
|
|
9
|
+
getMemoryConfig: () => ({ v2: { enabled: false } }),
|
|
10
10
|
}));
|
|
11
11
|
|
|
12
12
|
mock.module("../../../../util/logger.js", () => ({
|
|
@@ -36,12 +36,7 @@ let configMemoryEnabled = true;
|
|
|
36
36
|
|
|
37
37
|
mock.module("../../../../../config/loader.js", () => ({
|
|
38
38
|
getConfig: () => ({}),
|
|
39
|
-
loadConfig: () => ({
|
|
40
|
-
memory: {
|
|
41
|
-
enabled: configMemoryEnabled,
|
|
42
|
-
v2: { enabled: configMemoryV2Enabled },
|
|
43
|
-
},
|
|
44
|
-
}),
|
|
39
|
+
loadConfig: () => ({}),
|
|
45
40
|
loadRawConfig: () => ({}),
|
|
46
41
|
saveRawConfig: () => {},
|
|
47
42
|
invalidateConfigCache: () => {},
|
|
@@ -49,6 +44,14 @@ mock.module("../../../../../config/loader.js", () => ({
|
|
|
49
44
|
setNestedValue: () => {},
|
|
50
45
|
}));
|
|
51
46
|
|
|
47
|
+
// static-context reads its gates through the plugin's own config accessor.
|
|
48
|
+
mock.module("../../config.js", () => ({
|
|
49
|
+
getMemoryConfig: () => ({
|
|
50
|
+
enabled: configMemoryEnabled,
|
|
51
|
+
v2: { enabled: configMemoryV2Enabled },
|
|
52
|
+
}),
|
|
53
|
+
}));
|
|
54
|
+
|
|
52
55
|
const { readMemoryV2StaticContent, shouldExposePersonalMemory } =
|
|
53
56
|
await import("../static-context.js");
|
|
54
57
|
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
// matching the existing PKB auto-inject pattern.
|
|
19
19
|
|
|
20
20
|
import type { ChannelId } from "../../../../channels/types.js";
|
|
21
|
-
import { loadConfig } from "../../../../config/loader.js";
|
|
22
21
|
import { readPromptFile } from "../../../../prompts/system-prompt.js";
|
|
23
22
|
import { getWorkspacePromptPath } from "../../../../util/platform.js";
|
|
23
|
+
import { getMemoryConfig } from "../config.js";
|
|
24
24
|
|
|
25
25
|
interface MemoryV2StaticBlock {
|
|
26
26
|
heading: string;
|
|
@@ -35,9 +35,9 @@ const MEMORY_V2_STATIC_BLOCKS: readonly MemoryV2StaticBlock[] = [
|
|
|
35
35
|
];
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* Build the v2 static memory block, gated on `
|
|
39
|
-
* `
|
|
40
|
-
*
|
|
38
|
+
* Build the v2 static memory block, gated on the memory config's `enabled`
|
|
39
|
+
* and `v2.enabled`. Empty/missing files are skipped; returns `null` when
|
|
40
|
+
* either gate is off or every file is empty.
|
|
41
41
|
*
|
|
42
42
|
* `excludeBuffer` drops the `## Buffer` section. The consolidation run sets
|
|
43
43
|
* it: the agent's contract there is the `memory/buffer.md` FILE — it reads,
|
|
@@ -49,13 +49,8 @@ const MEMORY_V2_STATIC_BLOCKS: readonly MemoryV2StaticBlock[] = [
|
|
|
49
49
|
export function readMemoryV2StaticContent(
|
|
50
50
|
options: { excludeBuffer?: boolean } = {},
|
|
51
51
|
): string | null {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
config = loadConfig();
|
|
55
|
-
} catch {
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
if (config.memory.enabled === false || !config.memory.v2.enabled) {
|
|
52
|
+
const memoryConfig = getMemoryConfig();
|
|
53
|
+
if (memoryConfig.enabled === false || !memoryConfig.v2.enabled) {
|
|
59
54
|
return null;
|
|
60
55
|
}
|
|
61
56
|
|
|
@@ -89,6 +89,7 @@ const realFlags = {
|
|
|
89
89
|
const realConfigLoader = {
|
|
90
90
|
...(await import("../../../../../config/loader.js")),
|
|
91
91
|
};
|
|
92
|
+
const realMemoryConfig = { ...(await import("../../config.js")) };
|
|
92
93
|
const realDbConnection = {
|
|
93
94
|
...(await import("../../../../../persistence/db-connection.js")),
|
|
94
95
|
};
|
|
@@ -188,6 +189,24 @@ mock.module("../../../../../config/loader.js", () => ({
|
|
|
188
189
|
: realConfigLoader.getConfig(),
|
|
189
190
|
}));
|
|
190
191
|
|
|
192
|
+
// Memory code resolves its config through the plugin's own accessor, not
|
|
193
|
+
// getConfig(); stub the same conditional slice there.
|
|
194
|
+
mock.module("../../config.js", () => ({
|
|
195
|
+
getMemoryConfig: () =>
|
|
196
|
+
carryMockActive
|
|
197
|
+
? {
|
|
198
|
+
v3: {
|
|
199
|
+
live: true,
|
|
200
|
+
spotlight: {
|
|
201
|
+
n: SPOTLIGHT_N,
|
|
202
|
+
windowTurns: SPOTLIGHT_WINDOW_TURNS,
|
|
203
|
+
},
|
|
204
|
+
prune: pruneConfig ?? undefined,
|
|
205
|
+
},
|
|
206
|
+
}
|
|
207
|
+
: realMemoryConfig.getMemoryConfig(),
|
|
208
|
+
}));
|
|
209
|
+
|
|
191
210
|
mock.module("../../../../../config/assistant-feature-flags.js", () => ({
|
|
192
211
|
...realFlags,
|
|
193
212
|
isAssistantFeatureFlagEnabled: (
|
|
@@ -44,6 +44,7 @@ import {
|
|
|
44
44
|
const realConfigLoader = {
|
|
45
45
|
...(await import("../../../../../config/loader.js")),
|
|
46
46
|
};
|
|
47
|
+
const realMemoryConfig = { ...(await import("../../config.js")) };
|
|
47
48
|
const realFlags = {
|
|
48
49
|
...(await import("../../../../../config/assistant-feature-flags.js")),
|
|
49
50
|
};
|
|
@@ -142,6 +143,22 @@ mock.module("../../../../../config/loader.js", () => ({
|
|
|
142
143
|
: realConfigLoader.getConfig(),
|
|
143
144
|
}));
|
|
144
145
|
|
|
146
|
+
// Memory code resolves its config through the plugin's own accessor, not
|
|
147
|
+
// getConfig(); stub the same conditional slice there.
|
|
148
|
+
mock.module("../../config.js", () => ({
|
|
149
|
+
getMemoryConfig: () =>
|
|
150
|
+
injectionMockActive
|
|
151
|
+
? {
|
|
152
|
+
enabled: memoryEnabled,
|
|
153
|
+
v3: {
|
|
154
|
+
live: liveEnabled,
|
|
155
|
+
spotlight: spotlightConfig,
|
|
156
|
+
prune: pruneConfig ?? undefined,
|
|
157
|
+
},
|
|
158
|
+
}
|
|
159
|
+
: realMemoryConfig.getMemoryConfig(),
|
|
160
|
+
}));
|
|
161
|
+
|
|
145
162
|
// The prune valve resolves the live conversation through the daemon registry
|
|
146
163
|
// (dynamically imported). Stub it so the deferred valve never drags the heavy
|
|
147
164
|
// daemon module graph into this test process; `undefined` = "conversation not
|
|
@@ -40,6 +40,7 @@ const realDb = {
|
|
|
40
40
|
...(await import("../../../../persistence/db-connection.js")),
|
|
41
41
|
};
|
|
42
42
|
const realConfigLoader = { ...(await import("../../../../config/loader.js")) };
|
|
43
|
+
const realMemoryConfig = { ...(await import("../config.js")) };
|
|
43
44
|
|
|
44
45
|
let pruneMockActive = false;
|
|
45
46
|
let pruneConfig: {
|
|
@@ -86,6 +87,15 @@ mock.module("../../../../config/loader.js", () => ({
|
|
|
86
87
|
: realConfigLoader.getConfig(),
|
|
87
88
|
}));
|
|
88
89
|
|
|
90
|
+
// Memory code resolves its config through the plugin's own accessor, not
|
|
91
|
+
// getConfig(); stub the same conditional slice there.
|
|
92
|
+
mock.module("../config.js", () => ({
|
|
93
|
+
getMemoryConfig: () =>
|
|
94
|
+
pruneMockActive
|
|
95
|
+
? { v3: { prune: pruneConfig ?? undefined } }
|
|
96
|
+
: realMemoryConfig.getMemoryConfig(),
|
|
97
|
+
}));
|
|
98
|
+
|
|
89
99
|
const {
|
|
90
100
|
collectPersistedV3Cards,
|
|
91
101
|
filterPrunedCardSections,
|