@modusensus/dsh-mneme 0.9.1 → 0.9.2

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/lib/dream.js CHANGED
@@ -10,7 +10,7 @@ import { scopeKeyOf } from "./scope.js";
10
10
  import { isInPeakWindow, nextOffPeakAt } from "./peak-hours.js";
11
11
  import { createHash, randomUUID } from "node:crypto";
12
12
  import { STR, langOf } from "./lang.js";
13
- export { validateDecisions, applyDecisions, withEffortFallback, describeStreamFailure, resolveDreamEffort, resolveRoute };
13
+ export { validateDecisions, applyDecisions, withEffortFallback, withMaxTokensFallback, parseMaxTokensCap, describeStreamFailure, resolveDreamEffort, resolveRoute };
14
14
 
15
15
  // Extract the first JSON array from LLM output, tolerating markdown fences,
16
16
  // leading/trailing prose, and common wrapper noise. Returns an array or null.
@@ -57,10 +57,24 @@ function totalChars(memories) {
57
57
  // 两处若各写一份过滤条件,重启播种读到的基线就会和运行期判定口径漂移(一个把
58
58
  // document 算进去、一个不算),阈值随之失真,且这种漂移只会在重启后才显形。
59
59
  function activeStoreSize(service) {
60
- const memories = service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document");
60
+ const memories = withoutPaused(service, service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document"));
61
61
  return { count: memories.length, chars: totalChars(memories) };
62
62
  }
63
63
 
64
+ // issue #471 政策轴:被按下的作用域不参与巩固,也就不该再撑着阈值把它自己触发起来。
65
+ // 三个读入口(本函数、generateNarratives 的 inputs、runDream 的 memories)各自的附加
66
+ // 条件不同——有的排 forgotten、有的不排——但「政策轴这一层」对三处是一样的,故收成
67
+ // 一个助手而不是各写一遍过滤,理由与 activeStoreSize 开头记着的口径漂移同源:两处
68
+ // 各写一份过滤条件,漂移只会在重启后才显形。
69
+ //
70
+ // 走 service.filterPaused 而不是自己读状态表:判定只许有一份实现(见
71
+ // src/scope-state.js 的模块注释)。service 上取不到这个函数时(老装配、测试夹具)
72
+ // 原样返回,行为与改动前一致。
73
+ function withoutPaused(service, rows) {
74
+ if (typeof service?.filterPaused !== "function") return rows;
75
+ return service.filterPaused(rows);
76
+ }
77
+
64
78
  // 基线的唯一合法形状:两个非负整数;其余(缺字段、负数、非整数、整个对象缺失)
65
79
  // 一律视为「没有基线」→ 调用方退回 {0,0}。播种值来自持久化列,可能是老库的 NULL
66
80
  // 或手工改坏的库,判定必须比调用点更严格,而不是相信数据库里存的一定合法。
@@ -399,6 +413,60 @@ async function withEffortFallback(ctx, effort, attempt, fallback, getStreamError
399
413
  }
400
414
  }
401
415
 
416
+ // Issue #478:max_tokens 超服务商硬上限的拒收判别式与上限提取的单一来源。
417
+ // 实测失败样例(dream_runs 180 连败):"max_tokens 不能超过 65536"——131072 的
418
+ // 默认值原样发出,网关 400 直接拒,推理还没开始。与 EFFORT_REJECT_RE 同款思路:
419
+ // 各处手写会漂移,导出供测试与其它调用点复用。
420
+ export const MAX_TOKENS_REJECT_RE = /max[\s_-]*tokens?/i;
421
+
422
+ /**
423
+ * 从拒收报错里提取服务商声明的 max_tokens 上限。只认「显式给数」的说法
424
+ * (不能超过 / at most / maximum value is / <= 65536 …),认不出就返回 null
425
+ * ——宁可不降级重试,也不能把报错里那个被拒的请求值(131072)误当上限。
426
+ * @returns {number|null}
427
+ */
428
+ function parseMaxTokensCap(message) {
429
+ const m = String(message ?? "").match(
430
+ /(?:不能超过|不得超过|超出上限|至多|最多(?:只能|仅)?|at\s+most|no\s+more\s+than|maximum(?:\s+value)?(?:\s+is)?|max(?:\.)?(?:\s+value)?(?:\s+is)?|less\s+than\s+or\s+equal\s+to|<=?)\s*[::]?\s*(\d{2,10})/i
431
+ );
432
+ const cap = m ? Number(m[1]) : null;
433
+ return cap && Number.isFinite(cap) && cap > 0 ? cap : null;
434
+ }
435
+
436
+ /**
437
+ * Issue #478:max_tokens 被服务商 400 拒收时,按报错里的上限自动降级重试一次。
438
+ * 与 withEffortFallback 同构:stream 路径的结果折叠成 undefined、真实原因在
439
+ * getStreamError() 里;throw 路径直接看 message。命中拒收且提取到比当前值小的
440
+ * 上限才重试(attempt 接收 tokens 值),否则原样返回/抛出。单次重试,与
441
+ * effort 的先例一致——重试后若仍被拒(同上限再报),cap >= tokens 的守卫让
442
+ * 它自然终止,不会循环。
443
+ */
444
+ async function withMaxTokensFallback(ctx, maxTokens, attempt, getStreamError) {
445
+ const retry = (tokens, reason) => {
446
+ ctx.logger?.warn?.(`dsh-mneme dream: max_tokens ${maxTokens} rejected by provider (${reason}); retrying with ${tokens}`);
447
+ return attempt(tokens);
448
+ };
449
+ try {
450
+ const result = await attempt(maxTokens);
451
+ if (result === undefined) {
452
+ const reason = String(getStreamError?.() ?? "");
453
+ if (MAX_TOKENS_REJECT_RE.test(reason)) {
454
+ const cap = parseMaxTokensCap(reason);
455
+ if (cap && cap < maxTokens) return retry(cap, reason);
456
+ }
457
+ }
458
+ return result;
459
+ } catch (error) {
460
+ // AbortError 直接放行(取消意图绝不能被降级重试吞掉,与 effort 同款守卫)。
461
+ if (error?.name === "AbortError") throw error;
462
+ const message = String(error?.message ?? error);
463
+ if (!MAX_TOKENS_REJECT_RE.test(message)) throw error;
464
+ const cap = parseMaxTokensCap(message);
465
+ if (!cap || cap >= maxTokens) throw error;
466
+ return retry(cap, message);
467
+ }
468
+ }
469
+
402
470
  /** 向量索引同步(总览与叙述条共用):fail-safe,失败只 warn 不反噬主流程。 */
403
471
  async function reEmbedMemory(semantic, memory, logger) {
404
472
  if (!semantic?.embedder || !semantic?.vectorIndex || !memory) return;
@@ -418,7 +486,7 @@ async function reEmbedMemory(semantic, memory, logger) {
418
486
  * 任何失败降级为 0 条,绝不反噬 dream 主流程。
419
487
  */
420
488
  async function generateNarratives({ ctx, service, config, route, language, effort, semantic, logger }) {
421
- const inputs = service.all().filter((m) => !m.archived && !m.forgotten && m.type !== "summary" && m.type !== "document");
489
+ const inputs = withoutPaused(service, service.all().filter((m) => !m.archived && !m.forgotten && m.type !== "summary" && m.type !== "document"));
422
490
  const clusters = clusterByTag(inputs, { minCluster: config.dreamNarrativeMinCluster ?? 3 });
423
491
  if (!clusters.length) return 0;
424
492
 
@@ -427,7 +495,7 @@ async function generateNarratives({ ctx, service, config, route, language, effor
427
495
  [`# ${tag}`, ...members.map((m) => `- id=${m.id} | title=${m.title} | ${m.content}`)].join("\n"))
428
496
  .join("\n\n");
429
497
  let streamFailure = "";
430
- const runNarratives = (withEffort) => {
498
+ const runNarratives = (withEffort, tokens) => {
431
499
  streamFailure = "";
432
500
  return runAuditedLlm(ctx, service, config, {
433
501
  triggerSource: "autoDream",
@@ -439,7 +507,7 @@ async function generateNarratives({ ctx, service, config, route, language, effor
439
507
  provider: route.provider,
440
508
  model: route.model,
441
509
  purpose: "compaction",
442
- maxTokens: config.dreamMaxTokens ?? 2048,
510
+ maxTokens: tokens,
443
511
  ...(withEffort && effort ? { reasoningEffort: effort } : {}),
444
512
  messages: [
445
513
  { role: "system", content: [{ type: "text", text: STR.prompts.narrative[language] }], source: { kind: "plugin:dsh-mneme", plugin: "dsh-mneme" } },
@@ -447,7 +515,7 @@ async function generateNarratives({ ctx, service, config, route, language, effor
447
515
  ]
448
516
  }, reportUsage, (reason) => { streamFailure = describeStreamFailure(reason); }));
449
517
  };
450
- const text = await withEffortFallback(ctx, effort, () => runNarratives(true), () => runNarratives(false), () => streamFailure);
518
+ const text = await withMaxTokensFallback(ctx, config.dreamMaxTokens ?? 2048, (tokens) => withEffortFallback(ctx, effort, () => runNarratives(true, tokens), () => runNarratives(false, tokens), () => streamFailure), () => streamFailure);
451
519
 
452
520
  const entries = extractJsonArray(text);
453
521
  if (!Array.isArray(entries)) return 0;
@@ -946,7 +1014,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
946
1014
  async function runDream(ctx, service, config) {
947
1015
  const language = langOf(config);
948
1016
  const logger = ctx.logger;
949
- let memories = service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document");
1017
+ let memories = withoutPaused(service, service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document"));
950
1018
  if (memories.length === 0) return { ok: true, applied: 0, skipped: true, summary: false };
951
1019
  // Issue #89:开跑时刻既喂给审计行(created_at),也是调度器闸门的时间基准。
952
1020
  const dreamStartedAt = Date.now();
@@ -1127,7 +1195,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
1127
1195
  const effort = await resolveDreamEffort(ctx, route, config.dreamReasoningEffort, logger);
1128
1196
  let decisions = null;
1129
1197
  let streamFailure = "";
1130
- const runConsolidation = (withEffort) => {
1198
+ const runConsolidation = (withEffort, tokens) => {
1131
1199
  streamFailure = "";
1132
1200
  return runAuditedLlm(ctx, service, config, {
1133
1201
  triggerSource: "autoDream",
@@ -1143,7 +1211,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
1143
1211
  provider: route.provider,
1144
1212
  model: route.model,
1145
1213
  purpose: "compaction",
1146
- maxTokens: config.dreamMaxTokens ?? 4096,
1214
+ maxTokens: tokens,
1147
1215
  ...(withEffort && effort ? { reasoningEffort: effort } : {}),
1148
1216
  messages: [
1149
1217
  { role: "system", content: [{ type: "text", text: consolidationPrompt }], source: { kind: "plugin:dsh-mneme", plugin: "dsh-mneme" } },
@@ -1155,7 +1223,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
1155
1223
  // Bug8: the consolidation call is audited (tokens/time/status). A throw
1156
1224
  // re-propagates to the catch below; an aborted stream returns undefined
1157
1225
  // and is treated as a failed run after the check below.
1158
- decisionText = await withEffortFallback(ctx, effort, () => runConsolidation(true), () => runConsolidation(false), () => streamFailure);
1226
+ decisionText = await withMaxTokensFallback(ctx, config.dreamMaxTokens ?? 4096, (tokens) => withEffortFallback(ctx, effort, () => runConsolidation(true, tokens), () => runConsolidation(false, tokens), () => streamFailure), () => streamFailure);
1159
1227
  } catch (error) {
1160
1228
  logger?.warn?.(`dsh-mneme dream: consolidation llm call failed: ${String(error)}`);
1161
1229
  return finish({ ok: false, error: "llm failed", summary: false });
@@ -1368,7 +1436,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
1368
1436
  // a failed run; summary:false marks a run that produced no summary.
1369
1437
  let summaryText;
1370
1438
  let summaryStreamFailure = "";
1371
- const runSummary = (withEffort) => {
1439
+ const runSummary = (withEffort, tokens) => {
1372
1440
  summaryStreamFailure = "";
1373
1441
  // Issue #258:总览阶段可用 dreamSummaryProvider/dreamSummaryModel 走独立路由
1374
1442
  //(默认空 = 沿用 dream 路由);输入侧有 dreamSummaryMaxInputs 硬上限,
@@ -1386,7 +1454,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
1386
1454
  provider: summaryRoute.provider,
1387
1455
  model: summaryRoute.model,
1388
1456
  purpose: "compaction",
1389
- maxTokens: config.dreamMaxTokens ?? 2048,
1457
+ maxTokens: tokens,
1390
1458
  ...(withEffort && effort ? { reasoningEffort: effort } : {}),
1391
1459
  messages: [
1392
1460
  { role: "system", content: [{ type: "text", text: STR.prompts.dreamSummary[language] }], source: { kind: "plugin:dsh-mneme", plugin: "dsh-mneme" } },
@@ -1404,7 +1472,11 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
1404
1472
  // 渐进恶化。倒序排序与 consolidate 窗口同款(updated_at desc, id tiebreak),
1405
1473
  // 保留最新的;0 = 关闭上限(回归全库行为,调用方自担 ctx)。
1406
1474
  // #230:document 指针行不进总览(与 dream/sleep 五个候选池的同款排除)。
1407
- const summaryAll = service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document");
1475
+ // issue #471:这一处尤其不能漏——总览落库时是**无作用域**的 summary 行
1476
+ // (saveWithDedupe 不带 agent_scope/workspace_scope),所以被按下作用域的内容
1477
+ // 一旦进了总览,注入侧按作用域过滤就再也拦不住它:内容已经洗成了全局行。
1478
+ // 别的入口漏了只是「那一条漏过去」,这里漏了是「洗一遍之后一直漏」。
1479
+ const summaryAll = withoutPaused(service, service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document"));
1408
1480
  const summaryMaxInputs = Number.isInteger(config.dreamSummaryMaxInputs) ? config.dreamSummaryMaxInputs : 0;
1409
1481
  const summaryInputs = summaryMaxInputs > 0 && summaryAll.length > summaryMaxInputs
1410
1482
  ? [...summaryAll]
@@ -1425,7 +1497,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
1425
1497
  );
1426
1498
  try {
1427
1499
  // Bug8: the summary call is audited too (operation dream_summarize).
1428
- summaryText = await withEffortFallback(ctx, effort, () => runSummary(true), () => runSummary(false), () => summaryStreamFailure);
1500
+ summaryText = await withMaxTokensFallback(ctx, config.dreamMaxTokens ?? 2048, (tokens) => withEffortFallback(ctx, effort, () => runSummary(true, tokens), () => runSummary(false, tokens), () => summaryStreamFailure), () => summaryStreamFailure);
1429
1501
  } catch (error) {
1430
1502
  logger?.warn?.(`dsh-mneme dream: summary llm call failed: ${String(error)}`);
1431
1503
  return finish({ ok: false, error: "llm failed", applied, decisions: auditDecisions, outcome, frozen: frozenCount, summary: false });
package/lib/embedding.js CHANGED
@@ -31,10 +31,32 @@ function embeddingsUrl(baseUrl) {
31
31
  * Call the embeddings API for one text. Resolves to a Float64 array, or null
32
32
  * when the provider is not configured, the call fails, or the response is
33
33
  * unusable. Never throws: failures degrade to keyword search.
34
+ *
35
+ * The plain null return cannot tell the caller WHY — "model not loaded",
36
+ * "endpoint 404", "timeout" and "input rejected" all look identical, so the
37
+ * receipt ledger used to flatten them into one
38
+ * `embedding-request-returned-no-vector`. Use `embedTextDetailed` when the
39
+ * reason matters; this wrapper keeps the old contract for existing callers.
34
40
  */
35
- export async function embedText({ baseUrl, apiKey, model }, text) {
41
+ export async function embedText(cfg, text) {
42
+ const out = await embedTextDetailed(cfg, text);
43
+ return out.vector;
44
+ }
45
+
46
+ /**
47
+ * Same call, but the failure mode is distinguishable: returns
48
+ * { vector: number[]|null, reason: null|string } where reason is one of
49
+ * config-incomplete | request-failed:<err> | http-<status> |
50
+ * response-not-json | response-no-vector | empty-text
51
+ * Never throws. `reason: null` means success.
52
+ */
53
+ export async function embedTextDetailed({ baseUrl, apiKey, model }, text) {
36
54
  const url = embeddingsUrl(baseUrl);
37
- if (!url || !apiKey || !model || !text) return null;
55
+ if (!url || !apiKey || !model || !text) {
56
+ // 空输入不是端点故障:区分开,否则「text 为空」会被误报成网络问题。
57
+ const missing = !url ? "baseUrl" : !apiKey ? "apiKey" : !model ? "model" : "text";
58
+ return { vector: null, reason: `config-incomplete:${missing}` };
59
+ }
38
60
  let res;
39
61
  try {
40
62
  res = await fetch(url, {
@@ -46,18 +68,33 @@ export async function embedText({ baseUrl, apiKey, model }, text) {
46
68
  body: JSON.stringify({ model, input: String(text).slice(0, 8000) }),
47
69
  signal: AbortSignal.timeout(DEFAULT_TIMEOUT_MS)
48
70
  });
49
- } catch {
50
- return null;
71
+ } catch (error) {
72
+ // 超时是 AbortSignal 抛的 TimeoutError;网络层错误(DNS/拒连)是 TypeError。
73
+ // 两者都走 request-failed,但带上类型,日志里能分清「慢」和「不通」。
74
+ const kind = error?.name === "TimeoutError" || error?.name === "AbortError" ? "timeout" : "network";
75
+ return { vector: null, reason: `request-failed:${kind}:${String(error?.message ?? error).slice(0, 200)}` };
76
+ }
77
+ if (!res.ok) {
78
+ // 状态码本身就是最有用的诊断:401/403 是钥匙、404 是路径、429 是限流、
79
+ // 5xx 是服务端。文本截断且不含请求体,日志安全。
80
+ let detail = "";
81
+ try {
82
+ const t = await res.text();
83
+ detail = String(t).slice(0, 200);
84
+ } catch { /* body unreadable, status alone is already informative */ }
85
+ return { vector: null, reason: `http-${res.status}${detail ? `:${detail}` : ""}` };
51
86
  }
52
- if (!res.ok) return null;
53
87
  let body;
54
88
  try {
55
89
  body = await res.json();
56
90
  } catch {
57
- return null;
91
+ return { vector: null, reason: "response-not-json" };
58
92
  }
59
93
  const vec = body?.data?.[0]?.embedding;
60
- return Array.isArray(vec) && vec.length ? Array.from(vec) : null;
94
+ if (!Array.isArray(vec) || !vec.length) {
95
+ return { vector: null, reason: "response-no-vector" };
96
+ }
97
+ return { vector: Array.from(vec), reason: null };
61
98
  }
62
99
 
63
100
  /**
@@ -145,24 +182,17 @@ export function createEmbedder({ store, settings, logger, vectorIndex }) {
145
182
  return entry;
146
183
  }
147
184
  const text = [title, content].filter(Boolean).join("\n");
148
- let vector = null;
149
- try {
150
- vector = await embedText(cfg, text);
151
- } catch (error) {
152
- const entry = { status: "failed", id, at, reason: String(error) };
153
- ledger.record(entry);
154
- logger?.warn?.(`[dsh-mneme] embed memory ${id} failed: ${String(error)}`);
155
- return entry;
156
- }
157
- if (!vector) {
158
- // embedText swallows the HTTP detail (404 / timeout / malformed body all
159
- // resolve to null). The receipt still has to say "no vector", which is
160
- // exactly the fact the write path never used to report.
161
- const entry = { status: "failed", id, at, reason: "embedding-request-returned-no-vector" };
185
+ const detailed = await embedTextDetailed(cfg, text);
186
+ if (detailed.reason) {
187
+ // 回执带上可区分的失败模式(http-404 / request-failed:timeout /
188
+ // config-incomplete:model / response-no-vector ...),不再把「模型没配」
189
+ // 「请求挂了」「响应没有向量」压成同一个词。状态页与日志按 reason 分诊。
190
+ const entry = { status: "failed", id, at, reason: detailed.reason };
162
191
  ledger.record(entry);
163
- logger?.warn?.(`[dsh-mneme] embed memory ${id} produced no vector`);
192
+ logger?.warn?.(`[dsh-mneme] embed memory ${id} failed: ${detailed.reason}`);
164
193
  return entry;
165
194
  }
195
+ const vector = detailed.vector;
166
196
  const model = modelHashOf(cfg.model);
167
197
  store.setEmbedding(id, vector, model);
168
198
  _dimension = vector.length;
@@ -0,0 +1,277 @@
1
+ // issue #471:政策轴(作用域级可逆闸门)。
2
+ //
3
+ // 与事实轴(src/scope.js 的 agent_scope / workspace_scope 标注)的分野:
4
+ // 事实轴回答「这条记忆属于谁」——持久,跟着记忆行走(strictScope 那道可见性硬墙、
5
+ // scope_changes 那份人工修正审计都长在这条轴上)。
6
+ // 政策轴回答「这个作用域现在通不通」——暂时,由人单方面按下、松开即恢复,存在运行
7
+ // 期表 scope_state 里(建表注释见 src/store.js),不改任何记忆行、不改任何配置键、
8
+ // 不重启。
9
+ //
10
+ // 为什么单独一个模块:判定要与存储能各自被检验。本模块不 import store.js——它只接
11
+ // 收一个「读状态行」的函数,于是 test 可以直接构造暂停集合、不必开库(与
12
+ // write-admission.js 把判据实现与存储解耦同款)。存储读写留在 store.js。
13
+ //
14
+ // 依赖方向是单向的:store.js import 本模块的三个 normalizeScopeState*(落库前把
15
+ // 维度/值/状态归一到可解释的枚举),本模块 import scope.js 的 scopeKeyOf;scope.js
16
+ // 不 import 任何本包模块,所以不成环。归一化与判定同处一个文件是刻意的——两者共享
17
+ // 同一套枚举常量(SCOPE_STATE_DIMENSIONS / _VALUES / _ALL),分开写会让「写入侧认的
18
+ // 值」与「判定侧认的值」有机会各自漂移,那正是本仓 gateByScope 注释里记着的坑。
19
+ //
20
+ // 为什么判定只写一份:src/service.js 的 gateByScope 注释已写明「过滤点漂移」是本仓
21
+ // 真实踩过的坑——`entity:` 与 `attr:` 两条前缀路曾在入口就 return,整条绕过硬墙。
22
+ // 政策轴照同一纪律办:判定收在本模块,service 侧只挂在既有出口上。
23
+ //
24
+ // 两级开关(与 src/write-admission.js:11 同口径):
25
+ // enabled 打开时:被按下的作用域其候选照常注入,但留一条审计行说清「本应被拦下」;
26
+ // enforce 打开时:真的不注入。
27
+ // 先看「如果按下会少掉多少」,再决定要不要真按——writeAdmission 与 preInjectGate
28
+ // 都是这么分两次上线的。两个开关都默认关时本模块的判定恒为「通」,service 侧拿到的
29
+ // 是原数组本身(不是等值副本),行为与改动前逐字段一致。
30
+ import { scopeKeyOf } from "./scope.js";
31
+
32
+ // 审计口径(llm_audit_logs 的既有列):trigger_source = 组件名,operation_type =
33
+ // 动作名,status='skipped' = 本行没有产生任何 LLM 花费(与 summarize 的间隔门、
34
+ // 尖峰门、writeAdmission 同款读法)。
35
+ export const SCOPE_STATE_TRIGGER_SOURCE = "scopeState";
36
+ export const SCOPE_STATE_OPERATION_TYPE = "scope_state_gate";
37
+
38
+ // 作用域维度。刻意只取本仓已有的两维,不照搬 dsh-domain-role 的四级作用域——它的
39
+ // project 级存在一个已知缺口(其 docs/06-limitations.md 的 B1:「项目级暂停不读会话
40
+ // 存档 ⇒ 判不出就不拦」),而本仓的 agent_scope / workspace_scope 都是确定值,不存在
41
+ // 「判不出归属」的情况。
42
+ export const SCOPE_STATE_DIMENSIONS = ["agent", "workspace"];
43
+
44
+ // 状态两值。active 是「松开」,paused 是「按下」——写成显式两值而不是「有行=按下」,
45
+ // 是为了让回执能如实报出「这一路现在是通的」,而不是让「没有行」同时承担「从未按过」
46
+ // 与「按过又松开了」两种意思。
47
+ export const SCOPE_STATE_VALUES = ["active", "paused"];
48
+
49
+ // 该维度全局(不针对某个具体标签)。注意它只命中「在这一维上有标注」的记忆:未标注
50
+ // 的行(scopeKeyOf 为 null)在该维度上不属于任何一路,全局按下也不该把它们卷进来
51
+ // ——它们对谁都可见,不是「这一路」。
52
+ export const SCOPE_STATE_ALL = "*";
53
+
54
+ /**
55
+ * 维度归一化。脏输入返回 null 由调用方拒绝(写入侧)或忽略(读取侧)。
56
+ * @param {unknown} raw
57
+ * @returns {"agent"|"workspace"|null}
58
+ */
59
+ export function normalizeScopeStateDimension(raw) {
60
+ return SCOPE_STATE_DIMENSIONS.includes(raw) ? raw : null;
61
+ }
62
+
63
+ /**
64
+ * 作用域值归一化。与 scopeKeyOf 同款口径(非空 trim 字符串),额外的只有 '*'
65
+ * 这个哨兵值。空白/非字符串落 null——不做 trim 后回退,因为「按下空字符串」不是
66
+ * 一个可解释的意图(不像 resolveWriteScope 那样有「取不到就放宽」的正当理由)。
67
+ * @param {unknown} raw
68
+ * @returns {string|null}
69
+ */
70
+ export function normalizeScopeStateValue(raw) {
71
+ if (typeof raw !== "string") return null;
72
+ const s = raw.trim();
73
+ if (!s) return null;
74
+ return s === SCOPE_STATE_ALL ? SCOPE_STATE_ALL : s;
75
+ }
76
+
77
+ /**
78
+ * 状态归一化。
79
+ * @param {unknown} raw
80
+ * @returns {"active"|"paused"|null}
81
+ */
82
+ export function normalizeScopeStateState(raw) {
83
+ return SCOPE_STATE_VALUES.includes(raw) ? raw : null;
84
+ }
85
+
86
+ // 共享的空索引。enabled 关或表为空时返回它,让调用方的 `index.any` 短路检查是
87
+ // 一次属性读取而不是一次分配。
88
+ export const EMPTY_PAUSED_INDEX = Object.freeze({
89
+ byDimension: Object.freeze({ agent: new Set(), workspace: new Set() }),
90
+ any: false
91
+ });
92
+
93
+ /**
94
+ * 把状态行编成查找索引。只收 state='paused' 的行——active 行是「松开」的留痕,
95
+ * 不参与判定(保留它们是为了回执能报「你上次是松开的」,不是为了拦东西)。
96
+ *
97
+ * 为什么只保留一个维度→值的集合而不保留行本身:判定只需要「这个值在不在集合里」,
98
+ * 行上的 note/decided_at 是给人回看用的,不是判定输入。多留一份会让索引与表之间
99
+ * 出现第二份需要同步的状态。
100
+ *
101
+ * @param {Array<{dimension?: string, scope_value?: string, state?: string}>} rows
102
+ * @returns {{byDimension: {agent: Set<string>, workspace: Set<string>}, any: boolean}}
103
+ */
104
+ export function buildPausedIndex(rows) {
105
+ const byDimension = { agent: new Set(), workspace: new Set() };
106
+ let any = false;
107
+ for (const row of rows ?? []) {
108
+ if (!row || row.state !== "paused") continue;
109
+ const dimension = normalizeScopeStateDimension(row.dimension);
110
+ if (!dimension) continue;
111
+ const value = normalizeScopeStateValue(row.scope_value);
112
+ if (!value) continue;
113
+ byDimension[dimension].add(value);
114
+ any = true;
115
+ }
116
+ return any ? { byDimension, any } : EMPTY_PAUSED_INDEX;
117
+ }
118
+
119
+ /**
120
+ * 一行记忆命中了哪些暂停作用域。返回命中明细(而不是布尔)是为了审计行能说清
121
+ * 「是被哪一路拦下的」——只留一个布尔,日后回看只知道「少了」,不知道为什么少。
122
+ *
123
+ * 比较用 scopeKeyOf(与去重键、检索加权共用同一把钥匙):未标注行在该维度上不
124
+ * 参与比较,于是「按下 workspace=X」不会波及未标注的全局记忆。
125
+ *
126
+ * @param {object} m 记忆行
127
+ * @param {{byDimension: object, any: boolean}} index
128
+ * @returns {Array<{dimension: string, scope_value: string}>}
129
+ */
130
+ export function matchPausedScopes(m, index) {
131
+ if (!index || index.any !== true) return [];
132
+ const hits = [];
133
+ for (const dimension of SCOPE_STATE_DIMENSIONS) {
134
+ const paused = index.byDimension[dimension];
135
+ if (!paused || paused.size === 0) continue;
136
+ const label = scopeKeyOf(dimension === "agent" ? m?.agent_scope : m?.workspace_scope);
137
+ if (label === null) continue;
138
+ if (paused.has(label)) hits.push({ dimension, scope_value: label });
139
+ // 全局按下排在具体值之后:两者同时命中时,报具体值比报 '*' 更有信息量
140
+ // ('*' 是「所有这一维的行」,具体值才是「就是这一路」)。
141
+ else if (paused.has(SCOPE_STATE_ALL)) hits.push({ dimension, scope_value: SCOPE_STATE_ALL });
142
+ }
143
+ return hits;
144
+ }
145
+
146
+ /**
147
+ * 命中的暂停作用域去重(审计行的 metadata.paused)。
148
+ * @param {Array<object>} rows
149
+ * @param {{byDimension: object, any: boolean}} index
150
+ * @returns {Array<{dimension: string, scope_value: string}>}
151
+ */
152
+ export function distinctPausedScopes(rows, index) {
153
+ const seen = new Map();
154
+ for (const m of rows ?? []) {
155
+ for (const hit of matchPausedScopes(m, index)) {
156
+ seen.set(`${hit.dimension}\u0000${hit.scope_value}`, hit);
157
+ }
158
+ }
159
+ return [...seen.values()];
160
+ }
161
+
162
+ /**
163
+ * 政策轴闸门。判定无状态(每次现读一次状态表),因为表恒小(行数 = 被人按过的
164
+ * 作用域个数,个位数),而缓存会引入一套失效协议——面板或另一个进程改了状态后,
165
+ * 缓存要能立刻知道,那需要 IPC 或轮询;用一次小表读换掉整类「缓存过期」缺陷。
166
+ *
167
+ * @param {object} deps
168
+ * @param {() => Array<object>} [deps.listStates] 读状态行(store.listScopeStates)
169
+ * @param {(entry: object) => void} [deps.saveAudit] 写审计行(store.saveLlmAudit)
170
+ * @param {object} [deps.config] 已解析配置(读 scopeState.enabled / scopeState.enforce)
171
+ * @param {object} [deps.logger]
172
+ */
173
+ export function createScopeStateGate({ listStates, saveAudit, config, logger } = {}) {
174
+ let warned = false;
175
+ const warn = (msg) => {
176
+ if (warned) return;
177
+ warned = true;
178
+ try {
179
+ logger?.warn?.(msg);
180
+ } catch {
181
+ // logger 自身故障不能影响检索路径——闸门坏掉时宁可放行(fail-open),
182
+ // 与 scope.js「解析失败绝不阻塞写入」同一哲学:政策轴是加在读取侧的一道
183
+ // 便利闸门,不是安全边界(安全边界是 strictScope 那道事实轴硬墙)。
184
+ }
185
+ };
186
+
187
+ const enabled = () => config?.scopeState?.enabled === true;
188
+ const enforce = () => config?.scopeState?.enforce === true;
189
+
190
+ /**
191
+ * 当前暂停索引。开关关、读失败、表为空都返回同一个空索引——三种情况下判定
192
+ * 都是「通」,调用方不需要区分。
193
+ */
194
+ function index() {
195
+ if (!enabled()) return EMPTY_PAUSED_INDEX;
196
+ try {
197
+ return buildPausedIndex(typeof listStates === "function" ? listStates() : []);
198
+ } catch (e) {
199
+ warn(`[dsh-mneme] scope-state gate: state read failed, gate is inert this call (${e?.message ?? e})`);
200
+ return EMPTY_PAUSED_INDEX;
201
+ }
202
+ }
203
+
204
+ /**
205
+ * 检索侧的过滤出口。**只在 enforce 档动手**——enabled 单开时这一侧不过滤,
206
+ * 与注入侧同一条两级语义(观察档不拦任何东西)。
207
+ *
208
+ * 开关关、观察档、没有暂停行时返回**原数组本身**(不是等值副本)——「默认路径
209
+ * 零行为变化」这条验收要靠引用相等来钉死,副本会让它退化成「内容看起来一样」。
210
+ *
211
+ * 这一侧不写审计行:审计的口径是「本应被拦下的注入候选有多少」,只有注入路径
212
+ * 才知道这件事。检索是工具与面板主动问的,把每次问答都记一行只会让审计表按
213
+ * 查询频次增长,而查到的读数并不回答「按下会少掉多少」。
214
+ */
215
+ function filter(rows) {
216
+ const idx = index();
217
+ if (!idx.any || !enforce() || !Array.isArray(rows) || rows.length === 0) return rows;
218
+ return rows.filter((m) => matchPausedScopes(m, idx).length === 0);
219
+ }
220
+
221
+ /**
222
+ * 注入面的闸门。返回过滤后的候选与「本应被拦下」的明细。
223
+ *
224
+ * enforce 关(观察档)时**不过滤**,但留一条审计行——这正是两级开关的意义:
225
+ * 先在真实流量里看「如果按下会少掉多少」,再决定要不要真按。审计写失败只 warn
226
+ * (同 saveLlmAudit 的调用惯例),注入本身照常完成。
227
+ *
228
+ * @param {Array<object>} rows 注入候选(图召回合并之后、pin 池之前)
229
+ * @returns {{rows: Array<object>, gated: Array<object>, enforced: boolean}}
230
+ */
231
+ function gateInjection(rows) {
232
+ const idx = index();
233
+ if (!idx.any || !Array.isArray(rows) || rows.length === 0) {
234
+ return { rows, gated: [], enforced: false };
235
+ }
236
+ const gated = rows.filter((m) => matchPausedScopes(m, idx).length > 0);
237
+ if (gated.length === 0) return { rows, gated: [], enforced: false };
238
+ const enforced = enforce();
239
+ record(idx, gated, enforced);
240
+ return {
241
+ rows: enforced ? rows.filter((m) => matchPausedScopes(m, idx).length === 0) : rows,
242
+ gated,
243
+ enforced
244
+ };
245
+ }
246
+
247
+ function record(idx, gated, enforced) {
248
+ if (typeof saveAudit !== "function") return;
249
+ // llmAudit 关时一行都不写:那个开关同时关掉审计行的启动期清理
250
+ // (index.js 的 deleteOldLlmAudits),在无保留期的表里按调用频次增长是不对的
251
+ // (与 write-admission.js 的同一条理由)。
252
+ if (config?.llmAudit?.enabled === false) return;
253
+ try {
254
+ saveAudit({
255
+ trigger_source: SCOPE_STATE_TRIGGER_SOURCE,
256
+ operation_type: SCOPE_STATE_OPERATION_TYPE,
257
+ // 不是模型调用,但该列 NOT NULL——用显式占位串,免得被当成某条路由的
258
+ // 花费来源统计进去(同 write-admission.js)。
259
+ model_id: "-",
260
+ status: "skipped",
261
+ related_memory_ids: gated.map((m) => m?.id).filter(Boolean),
262
+ metadata: {
263
+ gate: "scopeState",
264
+ paused: distinctPausedScopes(gated, idx),
265
+ gated: gated.length,
266
+ // enforced 由开关推出而不是另存一个入参:两者各存一份就会出现
267
+ // 「闸门动手了但行上写着 false」这种自相矛盾的记录。
268
+ enforced
269
+ }
270
+ });
271
+ } catch (e) {
272
+ warn(`[dsh-mneme] scope-state audit write failed (never blocks injection): ${e?.message ?? e}`);
273
+ }
274
+ }
275
+
276
+ return { enabled, enforce, index, filter, gateInjection };
277
+ }