Skip to content

Commit 1d22e2e

Browse files
committed
CodeRabbit on #382 两条:判定调用加超时(Promise.race 兜底,超时 degraded 落账不缓存不占死 key);cacheKey 加入候选内容(updateMemory 就地修正后不吃旧判定,内容进 key 保等价池确定性)
1 parent 46afefa commit 1d22e2e

3 files changed

Lines changed: 115 additions & 39 deletions

File tree

‎dsh-mneme/lib/pre-inject-gate.js‎

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function extractJson(content) {
4141
return null;
4242
}
4343

44-
export function createPreInjectGate({ llm, agentDefaultModel, service, config, logger } = {}) {
44+
export function createPreInjectGate({ llm, agentDefaultModel, service, config, logger, judgeTimeoutMs } = {}) {
4545
const language = langOf(config);
4646
const enabled = config?.preInjectGate?.enabled === true;
4747
const enforce = config?.preInjectGate?.enforce === true;
@@ -55,10 +55,12 @@ export function createPreInjectGate({ llm, agentDefaultModel, service, config, l
5555
const verdictCache = new Map(); // cacheKey -> { flaggedIds: Set<uuid>, nPool, reason }
5656
const inFlight = new Map(); // cacheKey -> Promise(并行渲染去重,防调用风暴)
5757

58-
// cacheKey = 查询 + 排序后的候选 id:查询变或候选集变(轮换/新记忆)都换 key,
59-
// 旧判定不会沾到新池子上(防陈旧过滤)。
58+
// cacheKey = 查询 + 排序后的「id:content」:查询变、候选集变、**候选内容变**
59+
// (updateMemory 就地修正)都换 key——旧判定不沾新内容(CodeRabbit on #382)。
60+
// 排序保证同池同 key(等价池确定性);内容进 key 换来的是 map 占用略涨,
61+
// 缓存 cap 8 有界。
6062
function cacheKey(query, candidates) {
61-
return `${query}\u0000${candidates.map((c) => c.id).sort().join(",")}`;
63+
return `${query}\u0000${candidates.map((c) => `${c.id}:${c.content}`).sort().join("\u0001")}`;
6264
}
6365

6466
function getCached(key) {
@@ -127,22 +129,40 @@ export function createPreInjectGate({ llm, agentDefaultModel, service, config, l
127129
let errorMessage = null;
128130
let inputTokens = 0;
129131
let outputTokens = 0;
132+
// 判定超时(CodeRabbit on #382):stream 挂起会让 inFlight 永久占住 key、
133+
// 同 key 不再重试、连 degraded 审计行都没有——与本模块「超时 → degraded、
134+
// 不缓存」的文档口径必须一致。消费循环用 Promise.race 兜底(宿主 llm.stream
135+
// 的 signal 支持未约定,不依赖);超时后放弃消费、按降级落账,inFlight 由
136+
// prefetch 的 finally 清理,下一帧可重试。
137+
const JUDGE_TIMEOUT_MS = judgeTimeoutMs ?? 60000;
138+
let timeoutTimer;
130139
try {
131-
for await (const chunk of llm.stream({ ...route, maxTokens: JUDGE_MAX_TOKENS, messages })) {
132-
if (chunk.type === "text-delta" && typeof chunk.text === "string") text = (text ?? "") + chunk.text;
133-
if (chunk.type === "usage") {
134-
const u = chunk.usage ?? chunk;
135-
const i = u.input_tokens ?? u.inputTokens ?? u.prompt_tokens ?? u.promptTokens;
136-
const o = u.output_tokens ?? u.outputTokens ?? u.completion_tokens ?? u.completionTokens;
137-
if (Number.isFinite(i)) inputTokens = i;
138-
if (Number.isFinite(o)) outputTokens = o;
139-
}
140-
if (chunk.type === "finish" && (chunk.reason?.kind === "error" || chunk.reason?.kind === "aborted")) {
141-
errorMessage = "llm stream aborted or errored";
142-
break;
140+
const consume = (async () => {
141+
for await (const chunk of llm.stream({ ...route, maxTokens: JUDGE_MAX_TOKENS, messages })) {
142+
if (chunk.type === "text-delta" && typeof chunk.text === "string") text = (text ?? "") + chunk.text;
143+
if (chunk.type === "usage") {
144+
const u = chunk.usage ?? chunk;
145+
const i = u.input_tokens ?? u.inputTokens ?? u.prompt_tokens ?? u.promptTokens;
146+
const o = u.output_tokens ?? u.outputTokens ?? u.completion_tokens ?? u.completionTokens;
147+
if (Number.isFinite(i)) inputTokens = i;
148+
if (Number.isFinite(o)) outputTokens = o;
149+
}
150+
if (chunk.type === "finish" && (chunk.reason?.kind === "error" || chunk.reason?.kind === "aborted")) {
151+
errorMessage = "llm stream aborted or errored";
152+
return;
153+
}
143154
}
144-
}
155+
})();
156+
consume.catch(() => { /* 超时后 consume 仍在跑:其拒绝已由 race 接过一次,这里防未处理拒绝 */ });
157+
await Promise.race([
158+
consume,
159+
new Promise((_, reject) => {
160+
timeoutTimer = setTimeout(() => reject(new Error(`judgment timeout (${JUDGE_TIMEOUT_MS}ms)`)), JUDGE_TIMEOUT_MS);
161+
})
162+
]);
163+
clearTimeout(timeoutTimer);
145164
} catch (err) {
165+
clearTimeout(timeoutTimer);
146166
errorMessage = String(err?.message ?? err);
147167
logger?.warn?.(`[dsh-mneme] preInjectGate judgment failed: ${errorMessage}`);
148168
}

‎dsh-mneme/src/pre-inject-gate.js‎

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function extractJson(content) {
4141
return null;
4242
}
4343

44-
export function createPreInjectGate({ llm, agentDefaultModel, service, config, logger } = {}) {
44+
export function createPreInjectGate({ llm, agentDefaultModel, service, config, logger, judgeTimeoutMs } = {}) {
4545
const language = langOf(config);
4646
const enabled = config?.preInjectGate?.enabled === true;
4747
const enforce = config?.preInjectGate?.enforce === true;
@@ -55,10 +55,12 @@ export function createPreInjectGate({ llm, agentDefaultModel, service, config, l
5555
const verdictCache = new Map(); // cacheKey -> { flaggedIds: Set<uuid>, nPool, reason }
5656
const inFlight = new Map(); // cacheKey -> Promise(并行渲染去重,防调用风暴)
5757

58-
// cacheKey = 查询 + 排序后的候选 id:查询变或候选集变(轮换/新记忆)都换 key,
59-
// 旧判定不会沾到新池子上(防陈旧过滤)。
58+
// cacheKey = 查询 + 排序后的「id:content」:查询变、候选集变、**候选内容变**
59+
// (updateMemory 就地修正)都换 key——旧判定不沾新内容(CodeRabbit on #382)。
60+
// 排序保证同池同 key(等价池确定性);内容进 key 换来的是 map 占用略涨,
61+
// 缓存 cap 8 有界。
6062
function cacheKey(query, candidates) {
61-
return `${query}\u0000${candidates.map((c) => c.id).sort().join(",")}`;
63+
return `${query}\u0000${candidates.map((c) => `${c.id}:${c.content}`).sort().join("\u0001")}`;
6264
}
6365

6466
function getCached(key) {
@@ -127,22 +129,40 @@ export function createPreInjectGate({ llm, agentDefaultModel, service, config, l
127129
let errorMessage = null;
128130
let inputTokens = 0;
129131
let outputTokens = 0;
132+
// 判定超时(CodeRabbit on #382):stream 挂起会让 inFlight 永久占住 key、
133+
// 同 key 不再重试、连 degraded 审计行都没有——与本模块「超时 → degraded、
134+
// 不缓存」的文档口径必须一致。消费循环用 Promise.race 兜底(宿主 llm.stream
135+
// 的 signal 支持未约定,不依赖);超时后放弃消费、按降级落账,inFlight 由
136+
// prefetch 的 finally 清理,下一帧可重试。
137+
const JUDGE_TIMEOUT_MS = judgeTimeoutMs ?? 60000;
138+
let timeoutTimer;
130139
try {
131-
for await (const chunk of llm.stream({ ...route, maxTokens: JUDGE_MAX_TOKENS, messages })) {
132-
if (chunk.type === "text-delta" && typeof chunk.text === "string") text = (text ?? "") + chunk.text;
133-
if (chunk.type === "usage") {
134-
const u = chunk.usage ?? chunk;
135-
const i = u.input_tokens ?? u.inputTokens ?? u.prompt_tokens ?? u.promptTokens;
136-
const o = u.output_tokens ?? u.outputTokens ?? u.completion_tokens ?? u.completionTokens;
137-
if (Number.isFinite(i)) inputTokens = i;
138-
if (Number.isFinite(o)) outputTokens = o;
139-
}
140-
if (chunk.type === "finish" && (chunk.reason?.kind === "error" || chunk.reason?.kind === "aborted")) {
141-
errorMessage = "llm stream aborted or errored";
142-
break;
140+
const consume = (async () => {
141+
for await (const chunk of llm.stream({ ...route, maxTokens: JUDGE_MAX_TOKENS, messages })) {
142+
if (chunk.type === "text-delta" && typeof chunk.text === "string") text = (text ?? "") + chunk.text;
143+
if (chunk.type === "usage") {
144+
const u = chunk.usage ?? chunk;
145+
const i = u.input_tokens ?? u.inputTokens ?? u.prompt_tokens ?? u.promptTokens;
146+
const o = u.output_tokens ?? u.outputTokens ?? u.completion_tokens ?? u.completionTokens;
147+
if (Number.isFinite(i)) inputTokens = i;
148+
if (Number.isFinite(o)) outputTokens = o;
149+
}
150+
if (chunk.type === "finish" && (chunk.reason?.kind === "error" || chunk.reason?.kind === "aborted")) {
151+
errorMessage = "llm stream aborted or errored";
152+
return;
153+
}
143154
}
144-
}
155+
})();
156+
consume.catch(() => { /* 超时后 consume 仍在跑:其拒绝已由 race 接过一次,这里防未处理拒绝 */ });
157+
await Promise.race([
158+
consume,
159+
new Promise((_, reject) => {
160+
timeoutTimer = setTimeout(() => reject(new Error(`judgment timeout (${JUDGE_TIMEOUT_MS}ms)`)), JUDGE_TIMEOUT_MS);
161+
})
162+
]);
163+
clearTimeout(timeoutTimer);
145164
} catch (err) {
165+
clearTimeout(timeoutTimer);
146166
errorMessage = String(err?.message ?? err);
147167
logger?.warn?.(`[dsh-mneme] preInjectGate judgment failed: ${errorMessage}`);
148168
}

‎dsh-mneme/test/pre-inject-gate.test.js‎

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function fakeLlm(script) {
2929
i += 1;
3030
calls.push({ options, messages: options.messages });
3131
if (step.error) throw new Error("boom: llm down");
32+
if (step.hang) await new Promise(() => { /* 永不产出:超时用例 */ });
3233
const text = step.garbage ? "抱歉我不能输出 JSON" : JSON.stringify(step.json);
3334
yield { type: "text-delta", text };
3435
yield { type: "usage", usage: { input_tokens: 100, output_tokens: 20 } };
@@ -39,7 +40,7 @@ function fakeLlm(script) {
3940

4041
const agentDefaultModel = { currentSelection: () => ({ provider: "test", model: "judge-1" }) };
4142

42-
function setup(config = {}, llm = null) {
43+
function setup(config = {}, llm = null, gateOpts = {}) {
4344
const store = createStore(":memory:");
4445
const service = createService({ store, mirror: null, config });
4546
const contexts = [];
@@ -50,7 +51,7 @@ function setup(config = {}, llm = null) {
5051
};
5152
const settings = createSettings(store.db);
5253
const fullConfig = { maxInjectedItems: 5, importanceThreshold: 3, ...config };
53-
const gate = createPreInjectGate({ llm, agentDefaultModel, service, config: fullConfig, logger: null });
54+
const gate = createPreInjectGate({ llm, agentDefaultModel, service, config: fullConfig, logger: null, ...gateOpts });
5455
createInjector(ctx, service, settings, fullConfig, gate);
5556
const memoryText = contexts.find((c) => c.name === "memory").text;
5657
return { store, service, gate, memoryText };
@@ -68,9 +69,10 @@ const settle = async () => { for (let i = 0; i < 4; i++) await new Promise((r) =
6869
function seedPool(service) {
6970
// 三条 decision(非 pinned——pin 池豁免是单独的用例)。插入序 = 注入序的
7071
// 一般槽次序:m#0=预算决策 / m#1=立场决策 / m#2=事实决策。
71-
service.saveWithDedupe({ type: "decision", title: "预算决策", content: "The user keeps the dinner budget under $340.", importance: 3 });
72-
service.saveWithDedupe({ type: "decision", title: "立场决策", content: "The user believes stand-up meetings are an essential ritual and the right way to run a team.", importance: 3 });
73-
service.saveWithDedupe({ type: "decision", title: "事实决策", content: "The user works from an office in Changsha.", importance: 3 });
72+
const a = service.saveWithDedupe({ type: "decision", title: "预算决策", content: "The user keeps the dinner budget under $340.", importance: 3 }).memory;
73+
const b = service.saveWithDedupe({ type: "decision", title: "立场决策", content: "The user believes stand-up meetings are an essential ritual and the right way to run a team.", importance: 3 }).memory;
74+
const c = service.saveWithDedupe({ type: "decision", title: "事实决策", content: "The user works from an office in Changsha.", importance: 3 }).memory;
75+
return [a, b, c];
7476
}
7577

7678
const QUERY = "Help me plan the menu — what's my budget figure?";
@@ -225,3 +227,37 @@ test("lightMode:预设翻转 preInjectGate.enabled 且不污染用户配置对
225227
const flat = applyLightModePreset({ lightMode: true, heatEnabled: true });
226228
assert.equal(flat.heatEnabled, false, "平铺键照旧直赋(既有行为回归)");
227229
});
230+
231+
test("判定超时:degraded 审计行 + 不缓存,下一帧可重试(CodeRabbit on #382)", async () => {
232+
const llm = fakeLlm([{ hang: true }, { json: { ids: [], reason: "recovered" } }]);
233+
const s = setup({ preInjectGate: { enabled: true } }, llm, { judgeTimeoutMs: 50 });
234+
seedPool(s.service);
235+
frame(s.memoryText, QUERY);
236+
// 真实等待:50ms 超时定时器要真烧完(settle 的微任务不够)
237+
await new Promise((r) => setTimeout(r, 150));
238+
const rows = gateRows(s.store);
239+
assert.ok(rows.length >= 1);
240+
assert.equal(rows[0].status, "error", "超时按降级落账");
241+
assert.match(rows[0].error_message, /timeout/);
242+
frame(s.memoryText, QUERY); // inFlight 已清 → 下一帧重试
243+
await settle();
244+
assert.equal(llm.calls.length, 2, "超时不占死 key,下一帧重新判定");
245+
});
246+
247+
test("缓存键含候选内容:updateMemory 就地修正后不吃旧判定(CodeRabbit on #382)", async () => {
248+
const llm = fakeLlm([
249+
{ json: { ids: ["m#1"], reason: "r" } },
250+
{ json: { ids: [], reason: "r" } }
251+
]);
252+
const { service, gate, memoryText } = setup({ preInjectGate: { enabled: true } }, llm);
253+
const [a, b] = seedPool(service);
254+
frame(memoryText, QUERY);
255+
await settle();
256+
assert.equal(gate.forFrame(QUERY, service.injectCandidates({ maxItems: 5, threshold: 3 }), "s1").state, "observed");
257+
// 同 id 内容被就地修正 → 旧判定不得沾新内容
258+
service.update(b.id, { content: "The user now believes stand-ups are useless." });
259+
frame(memoryText, QUERY);
260+
await settle();
261+
assert.equal(llm.calls.length, 2, "内容变化触发重新判定");
262+
void a;
263+
});

0 commit comments

Comments
 (0)