@kenz1117/dsh-engram 0.11.0 → 0.11.1

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/index.js CHANGED
@@ -105,7 +105,11 @@ function appleLocaleBase() {
105
105
  *
106
106
  * Порядок чтения: переменные окружения (если в них **настоящий** язык — их
107
107
  * ставили руками, и это более узкое намерение, чем настройка ОС), затем на macOS
108
- * — `AppleLocale`, иначе ничего.
108
+ * — `AppleLocale`, затем — локаль по умолчанию из ICU (`Intl`): на Windows ICU
109
+ * берёт её из `GetUserDefaultLocaleName`, и это единственный надёжный источник —
110
+ * `LC_ALL`/`LC_MESSAGES`/`LANG` там обычно не заданы вовсе (issue #39: без этой
111
+ * ступени китайская Windows читалась как пустая строка и падала в умолчание
112
+ * поставки, снося готовую 512-мерную базу на 384-мерную модель).
109
113
  *
110
114
  * Нужна там, где важно не «каким словарём мы располагаем», а на каком языке человек
111
115
  * на самом деле работает: панель первого старта подставляет язык локали в выбор,
@@ -116,8 +120,25 @@ function appleLocaleBase() {
116
120
  function systemLocaleBase() {
117
121
  const fromEnv = parseLocaleTag(process.env.LC_ALL ?? process.env.LC_MESSAGES ?? process.env.LANG ?? "");
118
122
  if (fromEnv !== "") return fromEnv;
119
- if (process.platform === "darwin") return appleLocaleBase();
120
- return "";
123
+ if (process.platform === "darwin") {
124
+ const apple = appleLocaleBase();
125
+ if (apple !== "") return apple;
126
+ }
127
+ return icuLocaleBase();
128
+ }
129
+ /**
130
+ * Локаль по умолчанию из ICU: Node (full-icu) резолвит её из настроек ОС —
131
+ * на Windows это региональный формат пользователя (`GetUserDefaultLocaleName`),
132
+ * на Linux/macOS — соответствующие системные источники. Отличать от «локали
133
+ * процесса» больше не нужно: здесь это как раз источник последней надежды, когда
134
+ * переменные окружения пусты, а своего чтения у платформы нет.
135
+ */
136
+ function icuLocaleBase() {
137
+ try {
138
+ return parseLocaleTag(Intl.DateTimeFormat().resolvedOptions().locale);
139
+ } catch {
140
+ return "";
141
+ }
121
142
  }
122
143
  /**
123
144
  * Язык системы. Берётся из Intl: «ru-RU» → «ru».
@@ -205,6 +226,7 @@ const zh$5 = {
205
226
  "core.warnReplayPendingFailed": "[dsh-engram] 待补做摄取重放失败(保留 pending,不影响对话):",
206
227
  "ingest.replayKeyFailed": "[dsh-engram] 重放单条 pending 失败(保留,下个会话重试):",
207
228
  "ingest.replayAbandoned": "[dsh-engram] 陈旧 pending 放弃重放(会话超 14 天仍失败,出队):",
229
+ "ingest.candidateFailed": "[dsh-engram] 单条摄取候选落库失败(跳过该条,其余候选照常):",
208
230
  "core.warnIngestPreviousFailed": "[dsh-engram] 本轮自动摄取失败(已跳过,不影响对话):",
209
231
  "core.warnHistoryBackfillFailed": "[dsh-engram] 历史回填失败:",
210
232
  "core.warnDisposeFinalFailed": "[dsh-engram] 会话结束的收尾处理异常(不影响对话):",
@@ -260,6 +282,7 @@ const en$5 = {
260
282
  "core.warnReplayPendingFailed": "[dsh-engram] Replaying a pending ingest failed (pending kept, conversation unaffected):",
261
283
  "ingest.replayKeyFailed": "[dsh-engram] Replaying one pending key failed (kept for next session):",
262
284
  "ingest.replayAbandoned": "[dsh-engram] Stale pending abandoned (session older than 14 days, still failing — dequeued):",
285
+ "ingest.candidateFailed": "[dsh-engram] one ingest candidate failed to write (skipped; remaining candidates proceed as usual):",
263
286
  "core.warnIngestPreviousFailed": "[dsh-engram] Automatic ingest of this turn failed (skipped, conversation unaffected):",
264
287
  "core.warnHistoryBackfillFailed": "[dsh-engram] History backfill failed:",
265
288
  "core.warnDisposeFinalFailed": "[dsh-engram] Session-end finalisation threw (conversation unaffected):",
@@ -521,6 +544,8 @@ const zh$2 = {
521
544
  "store.mergeTargetMissing": "write-disposition: 并入目标 {id} 不存在",
522
545
  "store.embedderDownloadFailed": "嵌入模型下载/加载失败;检索已降级为纯关键词模式,可检查网络后重试",
523
546
  "store.embedderDimensionMismatch": "嵌入向量维度不一致:库里是 {expected} 维,嵌入器给出 {found} 维。比较与写入直接拒绝,而不是逐维截断——请把模型改回去,或用新模型重建库中向量",
547
+ "store.embedderInvalidVector": "嵌入向量含非有限分量(NaN/∞),写入被拒——这样的向量落库后余弦恒不可比,整条记忆会从语义检索里静默消失",
548
+ "store.restoreBusyWriters": "检测到 {file} 仍有活动连接在写入(可能是另一个打开着的 DeepSeek 窗口),已取消恢复——请先关闭其他窗口再试,否则那些窗口的写入会丢",
524
549
  "store.embedderModelUnresolved": "未能按语言确定本地嵌入模型;检索已降级为纯关键词模式,请检查模型登记表",
525
550
  "store.embedderRemoteUnreachable": "嵌入端点不可达({baseUrl},超时 {timeoutMs} 毫秒)",
526
551
  "store.embedderRemoteHttp": "嵌入端点返回 HTTP {status}{detail}",
@@ -566,6 +591,8 @@ const en$2 = {
566
591
  "store.mergeTargetMissing": "write-disposition: merge target {id} does not exist",
567
592
  "store.embedderDownloadFailed": "embedding model download/load failed; retrieval has degraded to keyword-only mode — check the network and retry",
568
593
  "store.embedderDimensionMismatch": "embedding dimension mismatch: the store holds {expected}-dimensional vectors, the embedder returned {found}. Comparison and writes are refused instead of being silently truncated — switch the model back, or re-embed the store with the new one",
594
+ "store.embedderInvalidVector": "embedding vector contains a non-finite component (NaN/∞); the write is refused — such a vector makes cosine comparisons meaningless and the memory would silently vanish from semantic retrieval",
595
+ "store.restoreBusyWriters": "{file} still has a live connection writing (likely another open DeepSeek window); restore cancelled — close the other windows first, otherwise their writes would be lost",
569
596
  "store.embedderModelUnresolved": "could not resolve a local embedding model for this language; retrieval has degraded to keyword-only mode — check the model registry",
570
597
  "store.embedderRemoteUnreachable": "embedding endpoint unreachable ({baseUrl}, timeout {timeoutMs} ms)",
571
598
  "store.embedderRemoteHttp": "embedding endpoint returned HTTP {status}{detail}",
@@ -578,7 +605,7 @@ const en$2 = {
578
605
  //#endregion
579
606
  //#region src/i18n/catalog/tool.ts
580
607
  /**
581
- * Словарь: 21 инструмент `engram_*` — описания схем, тексты результатов и ошибки.
608
+ * Словарь: 23 инструмента `engram_*` — описания схем, тексты результатов и ошибки.
582
609
  *
583
610
  * Ключи этой области начинаются с `tool.`. Оригинальный китайский текст автора
584
611
  * сохранён в `zh`; английский набор — образец полноты, русский может быть неполным
@@ -2407,6 +2434,7 @@ const en = {
2407
2434
  corridorIndexStaying: "staying",
2408
2435
  corridorIndexRooms: "{n} rooms",
2409
2436
  skipEmptyAnswer: "empty output (empty after retry)",
2437
+ skipAllCandidatesFailed: "every candidate failed to write (kept for replay)",
2410
2438
  tabSpiral: "Memory Spiral",
2411
2439
  spiralAriaLabel: "memory spiral view",
2412
2440
  spiralCenterLabel: "Now",
@@ -2967,6 +2995,7 @@ const ru = {
2967
2995
  corridorIndexStaying: "хранится",
2968
2996
  corridorIndexRooms: "комнат: {n}",
2969
2997
  skipEmptyAnswer: "вывод дистилляции пуст (пуст и после повтора)",
2998
+ skipAllCandidatesFailed: "все кандидаты не записались (сохранено для повтора)",
2970
2999
  tabSpiral: "Спираль воспоминаний",
2971
3000
  spiralAriaLabel: "спираль воспоминаний",
2972
3001
  spiralCenterLabel: "сейчас",
@@ -3525,6 +3554,7 @@ const zh = {
3525
3554
  corridorIndexStaying: "条驻留",
3526
3555
  corridorIndexRooms: "{n} 间房",
3527
3556
  skipEmptyAnswer: "提炼输出为空(重试后仍空)",
3557
+ skipAllCandidatesFailed: "全部候选落库失败(保留待重放)",
3528
3558
  tabSpiral: "回忆螺旋",
3529
3559
  spiralAriaLabel: "回忆螺旋视图",
3530
3560
  spiralCenterLabel: "现在",
@@ -4297,7 +4327,9 @@ function parseFillAnswer(text, terms) {
4297
4327
  /** Список переводов по порядку запроса: модель отвечает и так, без левых строк. */
4298
4328
  const byOrder = (list) => {
4299
4329
  if (list.length !== terms.length || !list.every((item) => typeof item === "string")) return;
4300
- list.forEach((item, index) => accept(terms[index], item));
4330
+ list.forEach((item, index) => {
4331
+ accept(terms[index], item);
4332
+ });
4301
4333
  };
4302
4334
  /** Карта: ключ — английская строка (или обёртка с массивом переводов внутри). */
4303
4335
  const fromRecord = (record, depth) => {
@@ -5713,10 +5745,13 @@ function resolveBaseTarget(registry, language, embedder) {
5713
5745
  * Построить эмбеддер по конфигурации и реестру.
5714
5746
  * @param config - разрешённая конфигурация плагина.
5715
5747
  * @param registry - прочитанные каталог моделей и привязки.
5748
+ * @param languageOverride - язык выбора локальной модели вместо `config.language`
5749
+ * (issue #39: метка существующей базы главнее системной локали — locale читается
5750
+ * не на всех платформах, а промах уводит готовую базу на чужую размерность).
5716
5751
  * @returns готовый EngramEmbedder.
5717
5752
  * @throws EngramError EMBEDDER_MODEL_UNRESOLVED — язык не привязан ни к одной модели; EMBEDDER_DOWNLOAD_FAILED / EMBEDDER_REMOTE_FAILED — бэкенд не поднялся.
5718
5753
  */
5719
- function createEmbedder(config, registry) {
5754
+ function createEmbedder(config, registry, languageOverride) {
5720
5755
  const embedder = config.embedder;
5721
5756
  const remote = embedder.remote;
5722
5757
  if (remote !== void 0) return createRemoteEmbedder({
@@ -5727,7 +5762,7 @@ function createEmbedder(config, registry) {
5727
5762
  batchSize: remote.batchSize,
5728
5763
  timeoutMs: remote.timeoutMs
5729
5764
  });
5730
- const spec = resolveLocalModel(registry, config.language, embedder.localModel, embedder.localPooling);
5765
+ const spec = resolveLocalModel(registry, languageOverride ?? config.language, embedder.localModel, embedder.localPooling);
5731
5766
  if (spec === void 0) return Promise.reject(new EngramError("EMBEDDER_MODEL_UNRESOLVED", message("store.embedderModelUnresolved")));
5732
5767
  return createLocalEmbedder(config.modelCacheDir, config.hfEndpoint, spec);
5733
5768
  }
@@ -6110,7 +6145,7 @@ async function resolveBand(judge, neighbor, similarity, kind, content, store) {
6110
6145
  const p = (await judge.ask(`Memory A:\n${neighbor.content}\n\nMemory B:\n${content}`, [{
6111
6146
  id: "same_memory",
6112
6147
  instructions: `Are Memory A and Memory B the same fact, i.e. restatements of one identical statement (both are "${kind}" memories)? Answer true only for genuine restatements.`
6113
- }]))["same_memory"];
6148
+ }])).same_memory;
6114
6149
  recordJevObservation({
6115
6150
  at: startedAt,
6116
6151
  site: "band",
@@ -6203,7 +6238,7 @@ async function confirmContradictions(judge, incoming, candidates, store) {
6203
6238
  const p = (await judge.ask(`Memory A:\n${candidate.content}\n\nMemory B:\n${incoming}`, [{
6204
6239
  id: "contradicts",
6205
6240
  instructions: "Do Memory A and Memory B state facts that cannot both be true at the same time (a genuine contradiction)?"
6206
- }]))["contradicts"];
6241
+ }])).contradicts;
6207
6242
  recordJevObservation({
6208
6243
  at: startedAt,
6209
6244
  site: "contradiction",
@@ -6280,6 +6315,13 @@ const INGEST_MAX_TOKENS = 4e3;
6280
6315
  const INGEST_DONE_OP = "ingest-done";
6281
6316
  /** op_log pending 键 op:disposed 末轮摄取失败/超时,待下次会话重放补做。 */
6282
6317
  const INGEST_PENDING_OP = "ingest-pending";
6318
+ /**
6319
+ * op_log 跳过标记 op:该 (sessionId, turn) 被确定性的前置检查跳过(节流/无正文/无路由)。
6320
+ * 不记它,估算与重跑就永远把这些轮当待处理(issue #40:回填的「待处理」降不到零)。
6321
+ * 跳过判定由轮内容决定、逐字可复现,标记因此不掩盖内容;target_id 存原因
6322
+ * (turn key 已含 sessionId,用它携带原因供日后按规则版本清理),detail 与 done 同键。
6323
+ */
6324
+ const INGEST_SKIP_OP = "ingest-skip";
6283
6325
  /** disposed 末轮摄取的超时(fire-and-forget 观察器,进程退出可能打断,必须限时)。 */
6284
6326
  const FINAL_INGEST_TIMEOUT_MS = 5e3;
6285
6327
  /** 幂等键编码:`${sessionId}#${turn}`。 */
@@ -6581,40 +6623,58 @@ async function ingestPreviousTurn(deps) {
6581
6623
  dropped: 0,
6582
6624
  skipped: "already-ingested"
6583
6625
  };
6626
+ if (await auditStore.hasAudit("ingest-skip", doneKey)) return {
6627
+ scannedEvents: slice.length,
6628
+ candidates: 0,
6629
+ written: 0,
6630
+ merged: 0,
6631
+ deferred: 0,
6632
+ dropped: 0,
6633
+ skipped: "already-ingested"
6634
+ };
6584
6635
  if (sliceMode === "previous" || deps.throttle === true) {
6585
6636
  const throttled = throttleDecision(slice);
6586
- if (throttled !== null) return {
6637
+ if (throttled !== null) {
6638
+ await auditStore.audit(INGEST_SKIP_OP, throttled, doneKey);
6639
+ return {
6640
+ scannedEvents: slice.length,
6641
+ candidates: 0,
6642
+ written: 0,
6643
+ merged: 0,
6644
+ deferred: 0,
6645
+ dropped: 0,
6646
+ skipped: throttled
6647
+ };
6648
+ }
6649
+ }
6650
+ const scoped = omitRecallToolResults(slice);
6651
+ const { texts, minSeq } = collectTexts(scoped, limits.includeAssistant);
6652
+ const cleaned = texts.map((text) => redactSecrets(sanitizeProtocolText(text)));
6653
+ if (cleaned.every((text) => text === "")) {
6654
+ await auditStore.audit(INGEST_SKIP_OP, "no-user-content", doneKey);
6655
+ return {
6587
6656
  scannedEvents: slice.length,
6588
6657
  candidates: 0,
6589
6658
  written: 0,
6590
6659
  merged: 0,
6591
6660
  deferred: 0,
6592
6661
  dropped: 0,
6593
- skipped: throttled
6662
+ skipped: "no-user-content"
6594
6663
  };
6595
6664
  }
6596
- const scoped = omitRecallToolResults(slice);
6597
- const { texts, minSeq } = collectTexts(scoped, limits.includeAssistant);
6598
- const cleaned = texts.map((text) => redactSecrets(sanitizeProtocolText(text)));
6599
- if (cleaned.every((text) => text === "")) return {
6600
- scannedEvents: slice.length,
6601
- candidates: 0,
6602
- written: 0,
6603
- merged: 0,
6604
- deferred: 0,
6605
- dropped: 0,
6606
- skipped: "no-user-content"
6607
- };
6608
6665
  const route = deps.routeOverride ?? routeFromEvents(deps.events);
6609
- if (route === void 0) return {
6610
- scannedEvents: slice.length,
6611
- candidates: 0,
6612
- written: 0,
6613
- merged: 0,
6614
- deferred: 0,
6615
- dropped: 0,
6616
- skipped: "no-route-in-log"
6617
- };
6666
+ if (route === void 0) {
6667
+ await auditStore.audit(INGEST_SKIP_OP, "no-route-in-log", doneKey);
6668
+ return {
6669
+ scannedEvents: slice.length,
6670
+ candidates: 0,
6671
+ written: 0,
6672
+ merged: 0,
6673
+ deferred: 0,
6674
+ dropped: 0,
6675
+ skipped: "no-route-in-log"
6676
+ };
6677
+ }
6618
6678
  const recallNote = hasRecallToolCalls(scoped) ? message("llm.ingestRecallNote") : "";
6619
6679
  const userText = `${message("llm.ingestUserPrompt")}\n${JSON.stringify(cleaned)}${recallNote === "" ? "" : `\n${recallNote}`}`;
6620
6680
  deps.logRequest({
@@ -6660,6 +6720,7 @@ async function ingestPreviousTurn(deps) {
6660
6720
  let merged = 0;
6661
6721
  let deferred = 0;
6662
6722
  let dropped = 0;
6723
+ let failedDrops = 0;
6663
6724
  for (const item of parsed.slice(0, limits.maxCandidates)) {
6664
6725
  const candidate = item;
6665
6726
  if (typeof candidate.content !== "string" || candidate.content.trim() === "") {
@@ -6680,68 +6741,83 @@ async function ingestPreviousTurn(deps) {
6680
6741
  ].includes(candidate.kind) ? candidate.kind : "fact";
6681
6742
  const modelScope = candidate.scope === "user" || candidate.scope === "project" ? candidate.scope : void 0;
6682
6743
  const scope = deps.perCandidateScope === true && modelScope !== void 0 ? modelScope : deps.writeScope ?? "user";
6683
- const target = deps.resolveStore === void 0 ? store : await deps.resolveStore(scope);
6684
6744
  const importance = typeof candidate.importance === "number" && Number.isFinite(candidate.importance) ? Math.min(1, Math.max(0, candidate.importance)) : .5;
6685
- const linkEntities = async (nodeId) => {
6686
- const mentions = parseEntityMentions(candidate.entities);
6687
- if (mentions.length === 0) return;
6688
- try {
6689
- const entities = await target.resolveEntities(mentions);
6690
- await target.linkNodeEntities(nodeId, entities.map((entity) => entity.id));
6691
- const factInputs = [];
6692
- const byName = new Map(entities.map((entity) => [normalizeEntityName(entity.name), entity]));
6693
- for (const mention of parseFactMentions(candidate.facts)) {
6694
- const entity = byName.get(normalizeEntityName(mention.entity));
6695
- if (entity === void 0) continue;
6696
- factInputs.push({
6697
- entityId: entity.id,
6698
- content: mention.content,
6699
- sourceNodeId: nodeId
6700
- });
6701
- }
6702
- if (factInputs.length > 0) await target.writeFacts(factInputs);
6703
- } catch {}
6704
- };
6705
6745
  if (writtenContents.includes(content)) {
6706
6746
  dropped += 1;
6707
6747
  continue;
6708
6748
  }
6709
- const vector = embedder === void 0 ? void 0 : (await embedder.embed([content]))[0];
6710
- const decision = await decideWrite(target, kind, vector, judge === void 0 ? {} : {
6711
- judge,
6712
- content
6713
- });
6714
- if (decision.disposition === "merge") {
6715
- await applyMerge(target, decision, content);
6716
- await linkEntities(decision.into.id);
6717
- writtenContents.push(content);
6718
- merged += 1;
6719
- continue;
6720
- }
6721
- const record = await target.write({
6722
- scope,
6723
- kind,
6724
- content,
6725
- importance,
6726
- confidence: limits.confidence,
6727
- sourceSessionId: deps.sessionId,
6728
- sourceRound: round,
6729
- ...minSeq === null ? {} : { sourceSeq: minSeq },
6730
- ...vector === void 0 ? {} : { embedding: vector },
6731
- ...deps.history === true ? { initialReviewAt: null } : {}
6732
- });
6733
- if (decision.disposition === "defer") {
6734
- const neighbors = judge === void 0 ? [decision.neighbor] : await confirmContradictions(judge, content, [decision.neighbor], target) ?? [decision.neighbor];
6735
- for (const neighbor of neighbors) {
6736
- if (neighbor.id === record.id) continue;
6737
- await target.linkEdge(record.id, neighbor.id, "contradicts");
6749
+ try {
6750
+ const target = deps.resolveStore === void 0 ? store : await deps.resolveStore(scope);
6751
+ const linkEntities = async (nodeId) => {
6752
+ const mentions = parseEntityMentions(candidate.entities);
6753
+ if (mentions.length === 0) return;
6754
+ try {
6755
+ const entities = await target.resolveEntities(mentions);
6756
+ await target.linkNodeEntities(nodeId, entities.map((entity) => entity.id));
6757
+ const factInputs = [];
6758
+ const byName = new Map(entities.map((entity) => [normalizeEntityName(entity.name), entity]));
6759
+ for (const mention of parseFactMentions(candidate.facts)) {
6760
+ const entity = byName.get(normalizeEntityName(mention.entity));
6761
+ if (entity === void 0) continue;
6762
+ factInputs.push({
6763
+ entityId: entity.id,
6764
+ content: mention.content,
6765
+ sourceNodeId: nodeId
6766
+ });
6767
+ }
6768
+ if (factInputs.length > 0) await target.writeFacts(factInputs);
6769
+ } catch {}
6770
+ };
6771
+ const vector = embedder === void 0 ? void 0 : (await embedder.embed([content]))[0];
6772
+ const decision = await decideWrite(target, kind, vector, judge === void 0 ? {} : {
6773
+ judge,
6774
+ content
6775
+ });
6776
+ if (decision.disposition === "merge") {
6777
+ await applyMerge(target, decision, content);
6778
+ await linkEntities(decision.into.id);
6779
+ writtenContents.push(content);
6780
+ merged += 1;
6781
+ continue;
6738
6782
  }
6739
- deferred += 1;
6783
+ const record = await target.write({
6784
+ scope,
6785
+ kind,
6786
+ content,
6787
+ importance,
6788
+ confidence: limits.confidence,
6789
+ sourceSessionId: deps.sessionId,
6790
+ sourceRound: round,
6791
+ ...minSeq === null ? {} : { sourceSeq: minSeq },
6792
+ ...vector === void 0 ? {} : { embedding: vector },
6793
+ ...deps.history === true ? { initialReviewAt: null } : {}
6794
+ });
6795
+ if (decision.disposition === "defer") {
6796
+ const neighbors = judge === void 0 ? [decision.neighbor] : await confirmContradictions(judge, content, [decision.neighbor], target) ?? [decision.neighbor];
6797
+ for (const neighbor of neighbors) {
6798
+ if (neighbor.id === record.id) continue;
6799
+ await target.linkEdge(record.id, neighbor.id, "contradicts");
6800
+ }
6801
+ deferred += 1;
6802
+ }
6803
+ await linkEntities(record.id);
6804
+ writtenContents.push(content);
6805
+ written += 1;
6806
+ } catch (error) {
6807
+ dropped += 1;
6808
+ failedDrops += 1;
6809
+ console.warn(message("ingest.candidateFailed"), content.slice(0, 80), error);
6740
6810
  }
6741
- await linkEntities(record.id);
6742
- writtenContents.push(content);
6743
- written += 1;
6744
6811
  }
6812
+ if (failedDrops > 0 && written === 0 && merged === 0 && deferred === 0) return {
6813
+ scannedEvents: slice.length,
6814
+ candidates: parsed.length,
6815
+ written,
6816
+ merged,
6817
+ deferred,
6818
+ dropped,
6819
+ skipped: "all-candidates-failed"
6820
+ };
6745
6821
  await auditStore.audit(INGEST_DONE_OP, deps.sessionId, doneKey);
6746
6822
  return {
6747
6823
  scannedEvents: slice.length,
@@ -6899,7 +6975,7 @@ async function replayPendingIngests(deps) {
6899
6975
  ...deps.resolveJudge === void 0 ? {} : { resolveJudge: deps.resolveJudge },
6900
6976
  signal: deps.signal
6901
6977
  });
6902
- if (outcome.skipped === "unparseable-output" || outcome.skipped === "empty-answer") {
6978
+ if (outcome.skipped === "unparseable-output" || outcome.skipped === "empty-answer" || outcome.skipped === "all-candidates-failed") {
6903
6979
  kept += 1;
6904
6980
  if (stale) {
6905
6981
  await store.clearAudit(INGEST_PENDING_OP, detail);
@@ -7112,7 +7188,11 @@ async function estimateHistoryBackfill(deps, defaults, overrides = {}) {
7112
7188
  for (const candidate of candidates) {
7113
7189
  eligibleTurns += candidate.turns.length;
7114
7190
  if (auditStore === void 0) continue;
7115
- for (const turn of candidate.turns) if (await auditStore.hasAudit("ingest-done", encodeTurnKey(candidate.header.id, turn))) alreadyIngested += 1;
7191
+ for (const turn of candidate.turns) {
7192
+ const key = encodeTurnKey(candidate.header.id, turn);
7193
+ if (await auditStore.hasAudit("ingest-done", key)) alreadyIngested += 1;
7194
+ else if (await auditStore.hasAudit("ingest-skip", key)) alreadyIngested += 1;
7195
+ }
7116
7196
  }
7117
7197
  return {
7118
7198
  rules,
@@ -7280,6 +7360,10 @@ const DEFAULTS = {
7280
7360
  importanceBelow: .3,
7281
7361
  mergeThreshold: .92
7282
7362
  };
7363
+ /** 合并阶段的工作集封顶:超出交给归档/衰减处置(审计 N12——两两比对必须设界)。 */
7364
+ const CONSOLIDATION_MAX_RECORDS = 500;
7365
+ /** 批式嵌入的块大小(一次 ONNX 前向的条数)。 */
7366
+ const EMBED_BATCH = 32;
7283
7367
  /** 闭馆整理主函数:纯异步,不抛错(失败仅记日志 / 落 pending)。 */
7284
7368
  async function runConsolidation(store, embedder, opts = {}) {
7285
7369
  const start = Date.now();
@@ -7288,35 +7372,27 @@ async function runConsolidation(store, embedder, opts = {}) {
7288
7372
  ...opts
7289
7373
  };
7290
7374
  const scope = options.scope ?? "user";
7291
- const stats = await store.stats();
7292
- const records = await store.topActive(scope, Math.max(stats.active, 0));
7293
- const archivedIds = /* @__PURE__ */ new Set();
7294
7375
  let archived = 0;
7295
7376
  let merged = 0;
7296
7377
  let skipped = 0;
7297
- const now = Date.now();
7298
- const ageCutoff = options.olderThanDays * 864e5;
7299
- for (const record of records) {
7300
- if (record.importance >= options.importanceBelow) continue;
7301
- if (now - record.createdAt < ageCutoff) continue;
7302
- if (record.confidence >= .3) continue;
7303
- try {
7304
- await store.forget(record.id);
7305
- archivedIds.add(record.id);
7306
- archived += 1;
7307
- } catch {
7308
- skipped += 1;
7309
- }
7378
+ try {
7379
+ archived = await store.decay({
7380
+ importanceBelow: options.importanceBelow,
7381
+ olderThanDays: options.olderThanDays
7382
+ });
7383
+ } catch {
7384
+ skipped += 1;
7310
7385
  }
7386
+ const seedIds = options.mergeCandidateIds !== void 0 ? new Set(options.mergeCandidateIds) : null;
7387
+ const stats = await store.stats();
7388
+ const records = await store.topActive(scope, seedIds === null ? Math.min(Math.max(stats.active, 0), CONSOLIDATION_MAX_RECORDS) : Math.max(stats.active, 0));
7311
7389
  const emb = await embedder;
7312
7390
  const buckets = /* @__PURE__ */ new Map();
7313
7391
  for (const record of records) {
7314
- if (archivedIds.has(record.id)) continue;
7315
7392
  const list = buckets.get(record.kind) ?? [];
7316
7393
  list.push(record);
7317
7394
  buckets.set(record.kind, list);
7318
7395
  }
7319
- const seedIds = options.mergeCandidateIds !== void 0 ? new Set(options.mergeCandidateIds) : null;
7320
7396
  for (const list of buckets.values()) {
7321
7397
  if (list.length < 2) continue;
7322
7398
  if (seedIds !== null && !list.some((record) => seedIds.has(record.id))) continue;
@@ -7343,13 +7419,22 @@ async function runConsolidation(store, embedder, opts = {}) {
7343
7419
  continue;
7344
7420
  }
7345
7421
  const vectors = /* @__PURE__ */ new Map();
7346
- for (const record of list) {
7347
- if (seedIds !== null && !seedIds.has(record.id)) continue;
7422
+ const seedRecords = list.filter((record) => seedIds === null || seedIds.has(record.id));
7423
+ for (let offset = 0; offset < seedRecords.length; offset += EMBED_BATCH) {
7424
+ const chunk = seedRecords.slice(offset, offset + EMBED_BATCH);
7348
7425
  try {
7349
- const vec = (await emb.embed([record.content]))[0];
7350
- if (vec !== void 0) vectors.set(record.id, vec);
7426
+ const vecs = await emb.embed(chunk.map((record) => record.content));
7427
+ chunk.forEach((record, index) => {
7428
+ const vec = vecs[index];
7429
+ if (vec !== void 0) vectors.set(record.id, vec);
7430
+ });
7351
7431
  } catch {
7352
- skipped += 1;
7432
+ for (const record of chunk) try {
7433
+ const vec = (await emb.embed([record.content]))[0];
7434
+ if (vec !== void 0) vectors.set(record.id, vec);
7435
+ } catch {
7436
+ skipped += 1;
7437
+ }
7353
7438
  }
7354
7439
  }
7355
7440
  const arr = list.filter((record) => vectors.has(record.id));
@@ -7361,6 +7446,7 @@ async function runConsolidation(store, embedder, opts = {}) {
7361
7446
  for (let j = i + 1; j < list.length; j += 1) {
7362
7447
  const b = list[j];
7363
7448
  if (visited.has(b.id)) continue;
7449
+ if (b.id === a.id) continue;
7364
7450
  const bVec = vectors.get(b.id);
7365
7451
  if (bVec === void 0) continue;
7366
7452
  if (cosine(vectors.get(a.id), bVec) >= options.mergeThreshold) dupes.push(b);
@@ -8132,7 +8218,7 @@ async function testJevConnection(connection) {
8132
8218
  return {
8133
8219
  ok: true,
8134
8220
  elapsedMs: Date.now() - startedAt,
8135
- probability: answers["ping"],
8221
+ probability: answers.ping,
8136
8222
  error: void 0,
8137
8223
  status: 200,
8138
8224
  kind: void 0
@@ -8193,7 +8279,7 @@ function readPath(root, path) {
8193
8279
  }
8194
8280
  /** Модели из конфигурации провайдера: `[{id, name}]`; битые записи пропускаем. */
8195
8281
  function readModels(profile) {
8196
- const models = profile?.["models"];
8282
+ const models = profile?.models;
8197
8283
  if (!Array.isArray(models)) return [];
8198
8284
  const result = [];
8199
8285
  for (const entry of models) {
@@ -8477,11 +8563,11 @@ function collectOverride(value) {
8477
8563
  const legacy = legacyJevFields(raw);
8478
8564
  if (legacy.length > 0) noteLegacyJevFields("jev-config.json", legacy);
8479
8565
  return {
8480
- ...typeof raw["enabled"] === "boolean" ? { enabled: raw["enabled"] } : {},
8481
- ...typeof raw["provider"] === "string" && raw["provider"] !== "" ? { provider: raw["provider"] } : {},
8482
- ...typeof raw["model"] === "string" && raw["model"] !== "" ? { model: raw["model"] } : {},
8483
- ...typeof raw["endpoint"] === "string" && raw["endpoint"] !== "" ? { endpoint: raw["endpoint"] } : {},
8484
- ...typeof raw["timeoutMs"] === "number" && Number.isInteger(raw["timeoutMs"]) ? { timeoutMs: raw["timeoutMs"] } : {}
8566
+ ...typeof raw.enabled === "boolean" ? { enabled: raw.enabled } : {},
8567
+ ...typeof raw.provider === "string" && raw.provider !== "" ? { provider: raw.provider } : {},
8568
+ ...typeof raw.model === "string" && raw.model !== "" ? { model: raw.model } : {},
8569
+ ...typeof raw.endpoint === "string" && raw.endpoint !== "" ? { endpoint: raw.endpoint } : {},
8570
+ ...typeof raw.timeoutMs === "number" && Number.isInteger(raw.timeoutMs) ? { timeoutMs: raw.timeoutMs } : {}
8485
8571
  };
8486
8572
  }
8487
8573
  /** Записать перекрытие целиком (вызывающий уже проверил его через `parseJevPatch`). */
@@ -8498,25 +8584,25 @@ function saveJevOverride(dbDir, override) {
8498
8584
  */
8499
8585
  function parseJevPatch(current, patch) {
8500
8586
  const merged = { ...current };
8501
- if (patch["enabled"] !== void 0) {
8502
- if (typeof patch["enabled"] !== "boolean") return message("misc.jevEnabledMustBeBoolean");
8503
- merged.enabled = patch["enabled"];
8587
+ if (patch.enabled !== void 0) {
8588
+ if (typeof patch.enabled !== "boolean") return message("misc.jevEnabledMustBeBoolean");
8589
+ merged.enabled = patch.enabled;
8504
8590
  }
8505
- if (patch["provider"] !== void 0) {
8506
- if (typeof patch["provider"] !== "string") return message("misc.jevProviderMustBeString");
8507
- const trimmed = patch["provider"].trim();
8591
+ if (patch.provider !== void 0) {
8592
+ if (typeof patch.provider !== "string") return message("misc.jevProviderMustBeString");
8593
+ const trimmed = patch.provider.trim();
8508
8594
  if (trimmed === "") delete merged.provider;
8509
8595
  else merged.provider = trimmed;
8510
8596
  }
8511
- if (patch["model"] !== void 0) {
8512
- if (typeof patch["model"] !== "string") return message("misc.jevModelMustNotBeEmpty");
8513
- const trimmed = patch["model"].trim();
8597
+ if (patch.model !== void 0) {
8598
+ if (typeof patch.model !== "string") return message("misc.jevModelMustNotBeEmpty");
8599
+ const trimmed = patch.model.trim();
8514
8600
  if (trimmed === "") delete merged.model;
8515
8601
  else merged.model = trimmed;
8516
8602
  }
8517
- if (patch["endpoint"] !== void 0) {
8518
- if (typeof patch["endpoint"] !== "string") return message("misc.jevEndpointMustBeHttp");
8519
- const trimmed = patch["endpoint"].trim();
8603
+ if (patch.endpoint !== void 0) {
8604
+ if (typeof patch.endpoint !== "string") return message("misc.jevEndpointMustBeHttp");
8605
+ const trimmed = patch.endpoint.trim();
8520
8606
  if (trimmed === "") delete merged.endpoint;
8521
8607
  else {
8522
8608
  let scheme = "";
@@ -8529,8 +8615,8 @@ function parseJevPatch(current, patch) {
8529
8615
  merged.endpoint = trimmed;
8530
8616
  }
8531
8617
  }
8532
- if (patch["timeoutMs"] !== void 0) {
8533
- const value = patch["timeoutMs"];
8618
+ if (patch.timeoutMs !== void 0) {
8619
+ const value = patch.timeoutMs;
8534
8620
  if (typeof value !== "number" || !Number.isInteger(value) || value < 1e3 || value > 6e4) return message("misc.jevTimeoutMsMustBeInteger");
8535
8621
  merged.timeoutMs = value;
8536
8622
  }
@@ -8674,7 +8760,7 @@ function renderMemory(record, edges) {
8674
8760
  const imagery = record.imagery;
8675
8761
  const caption = imagery?.caption;
8676
8762
  const sensory = imagery?.sensoryTags ?? [];
8677
- const frontmatter = [
8763
+ return `${[
8678
8764
  "---",
8679
8765
  `id: ${record.id}`,
8680
8766
  `scope: ${record.scope}`,
@@ -8690,8 +8776,7 @@ function renderMemory(record, edges) {
8690
8776
  ...caption !== void 0 && caption !== null ? [`imageryCaption: ${yaml(caption)}`] : [],
8691
8777
  ...sensory.length > 0 ? [`imagerySensory: [${sensory.map((s) => yaml(s)).join(", ")}]`, `imageryValence: ${(imagery?.emotionalValence ?? 0).toFixed(3)}`] : [],
8692
8778
  "---"
8693
- ].join("\n");
8694
- const body = [
8779
+ ].join("\n")}\n${[
8695
8780
  `# ${record.content.split("\n")[0]?.slice(0, 80) ?? message("misc.mirrorUntitled")}`,
8696
8781
  "",
8697
8782
  record.content,
@@ -8707,8 +8792,7 @@ function renderMemory(record, edges) {
8707
8792
  });
8708
8793
  }),
8709
8794
  ""
8710
- ].join("\n");
8711
- return frontmatter + "\n" + body;
8795
+ ].join("\n")}`;
8712
8796
  }
8713
8797
  /** 房间清单 _meta.json 模板。 */
8714
8798
  function renderMeta(scope, data, rooms) {
@@ -8718,7 +8802,7 @@ function renderMeta(scope, data, rooms) {
8718
8802
  total: data.records.length,
8719
8803
  rooms
8720
8804
  };
8721
- return JSON.stringify(payload, null, 2) + "\n";
8805
+ return `${JSON.stringify(payload, null, 2)}\n`;
8722
8806
  }
8723
8807
  /** 计算房间摘要(按 kind 分组统计 active/archived/forgotten 的记忆条数)。 */
8724
8808
  function summarizeRooms(records) {
@@ -8780,7 +8864,7 @@ async function writeMirror(rootDir, data, language) {
8780
8864
  fileCount += 2;
8781
8865
  if (scope === "shared") {
8782
8866
  const manifest = buildShareManifest(data);
8783
- await writeFile(join(rootDir, "share-manifest.json"), JSON.stringify(manifest, null, 2) + "\n", { mode: 384 });
8867
+ await writeFile(join(rootDir, "share-manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`, { mode: 384 });
8784
8868
  fileCount += 1;
8785
8869
  }
8786
8870
  return {
@@ -9022,6 +9106,25 @@ async function restoreBackup(archivePath, dbDir, options = {}) {
9022
9106
  incoming.push(name);
9023
9107
  }
9024
9108
  if (incoming.length === 0) throw new Error(message("store.backupNoDatabases"));
9109
+ const { DatabaseSync } = await import("node:sqlite");
9110
+ for (const name of await readdir(dbDir)) {
9111
+ if (!name.endsWith(".db")) continue;
9112
+ const path = join(dbDir, name);
9113
+ if (!(await stat(path)).isFile()) continue;
9114
+ let probe;
9115
+ try {
9116
+ probe = new DatabaseSync(path);
9117
+ if (probe.prepare("PRAGMA wal_checkpoint(TRUNCATE)").all()[0]?.busy !== 0) throw new Error(message("store.restoreBusyWriters", { file: name }));
9118
+ } catch (error) {
9119
+ if (error instanceof Error && error.message.includes(name)) throw error;
9120
+ const reason = error instanceof Error ? error.message : String(error);
9121
+ throw new Error(`${name}: ${reason}`, { cause: error });
9122
+ } finally {
9123
+ try {
9124
+ probe?.close();
9125
+ } catch {}
9126
+ }
9127
+ }
9025
9128
  const stamp = `${(/* @__PURE__ */ new Date()).toISOString().replaceAll(":", "-").slice(0, 19)}-${randomUUID()}`;
9026
9129
  const current = [];
9027
9130
  for (const name of await readdir(dbDir)) {
@@ -9103,30 +9206,16 @@ async function untarGz(archivePath, destDir) {
9103
9206
  //#region src/telemetry/aggregate.ts
9104
9207
  /** 聚合窗口(默认 7 天,可由路由 query 覆盖)。 */
9105
9208
  const DEFAULT_WINDOW_DAYS = 7;
9106
- /** 从单库 op_log 统计各 op 的计数(窗口内)。 */
9209
+ /** 从单库 op_log 统计各 op 的计数(窗口内)——SQL GROUP BY,不物化明细行(审计 N1)。 */
9107
9210
  async function countByOp(store, sinceMs) {
9108
- const ops = await store.recentOps(1e5);
9109
- const counts = {};
9110
- for (const op of ops) {
9111
- if (op.at < sinceMs) continue;
9112
- counts[op.op] = (counts[op.op] ?? 0) + 1;
9113
- }
9114
- return counts;
9211
+ return store.countOps(sinceMs);
9115
9212
  }
9116
- /** 按天汇总 op_log 中的所有写侧活动(用于活动热力日历)。返回的 dayMs 是该日 0 点的本地时间戳。
9117
- * 包含 write/update/ingest-done/ingest-request/distill-request/consolidation/restore/forget,排除纯读侧(search/outcome-report)。 */
9213
+ /** 按天汇总 op_log 中的写侧活动(热力日历)。返回的 dayMs 是该日 0 点的本地时间戳。
9214
+ * 包含 write/update/ingest-done/ingest-request/distill-request/consolidation/restore/forget,
9215
+ * 排除纯读侧(search/outcome-report)。折算两段走:SQL 按 UTC 天分组(吃 op_log_at 索引),
9216
+ * JS 用「UTC 正午」过 setHours 映射成本地日——正午法对夏令时切换 stable(对抗审查 P2-5)。 */
9118
9217
  async function countByDay(store, sinceMs, untilMs) {
9119
- const ops = await store.recentOps(1e5);
9120
- const counts = /* @__PURE__ */ new Map();
9121
- const dayMs = (ms) => {
9122
- const d = new Date(ms);
9123
- d.setHours(0, 0, 0, 0);
9124
- return d.getTime();
9125
- };
9126
- const startDay = dayMs(sinceMs);
9127
- const endDay = dayMs(untilMs);
9128
- for (let t = startDay; t <= endDay; t += 864e5) counts.set(t, 0);
9129
- const writeOps = /* @__PURE__ */ new Set([
9218
+ const rows = await store.countOpsByDay(sinceMs, untilMs, [
9130
9219
  "write",
9131
9220
  "update",
9132
9221
  "ingest-done",
@@ -9136,11 +9225,16 @@ async function countByDay(store, sinceMs, untilMs) {
9136
9225
  "restore",
9137
9226
  "forget"
9138
9227
  ]);
9139
- for (const op of ops) {
9140
- if (op.at < sinceMs || op.at > untilMs) continue;
9141
- if (!writeOps.has(op.op)) continue;
9142
- const d = dayMs(op.at);
9143
- counts.set(d, (counts.get(d) ?? 0) + 1);
9228
+ const counts = /* @__PURE__ */ new Map();
9229
+ const dayMs = (ms) => {
9230
+ const d = new Date(ms);
9231
+ d.setHours(0, 0, 0, 0);
9232
+ return d.getTime();
9233
+ };
9234
+ for (let t = dayMs(sinceMs); t <= dayMs(untilMs); t += 864e5) counts.set(t, 0);
9235
+ for (const row of rows) {
9236
+ const local = dayMs(row.utcDay * 864e5 + 432e5);
9237
+ counts.set(local, (counts.get(local) ?? 0) + row.count);
9144
9238
  }
9145
9239
  return counts;
9146
9240
  }
@@ -9181,15 +9275,15 @@ async function aggregateTelemetry(openStore, windowDays = DEFAULT_WINDOW_DAYS, s
9181
9275
  countsAgg.ingestRequests += counts["ingest-request"] ?? 0;
9182
9276
  countsAgg.ingestDones += counts["ingest-done"] ?? 0;
9183
9277
  countsAgg.searches += counts["search-rewrite-request"] ?? 0;
9184
- countsAgg.writes += counts["write"] ?? 0;
9185
- countsAgg.updates += counts["update"] ?? 0;
9186
- countsAgg.forgets += counts["forget"] ?? 0;
9187
- countsAgg.restores += counts["restore"] ?? 0;
9278
+ countsAgg.writes += counts.write ?? 0;
9279
+ countsAgg.updates += counts.update ?? 0;
9280
+ countsAgg.forgets += counts.forget ?? 0;
9281
+ countsAgg.restores += counts.restore ?? 0;
9188
9282
  countsAgg.distillRequests += counts["distill-request"] ?? 0;
9189
9283
  countsAgg.compressRequests += counts["compress-request"] ?? 0;
9190
9284
  countsAgg.searchRewrites += counts["search-rewrite-request"] ?? 0;
9191
9285
  countsAgg.consumptions += counts["outcome-report"] ?? 0;
9192
- countsAgg.consolidations += counts["consolidation"] ?? 0;
9286
+ countsAgg.consolidations += counts.consolidation ?? 0;
9193
9287
  countsAgg.reviewAnswers += counts["review-answer"] ?? 0;
9194
9288
  scopeViews.push({
9195
9289
  scope,
@@ -9494,12 +9588,12 @@ function toBoolean(value) {
9494
9588
  }
9495
9589
  /** 从 query 或 body 收敛历史回填规则(未给或非法的字段留给配置默认值)。 */
9496
9590
  function historyRulesOf(raw) {
9497
- const days = toNumber(raw["days"]);
9498
- const maxTurnsPerSession = toNumber(raw["maxTurnsPerSession"]);
9499
- const maxTotalTurns = toNumber(raw["maxTotalTurns"]);
9500
- const includeSubagents = toBoolean(raw["includeSubagents"]);
9501
- const includeSeeded = toBoolean(raw["includeSeeded"]);
9502
- const includeNoCwd = toBoolean(raw["includeNoCwd"]);
9591
+ const days = toNumber(raw.days);
9592
+ const maxTurnsPerSession = toNumber(raw.maxTurnsPerSession);
9593
+ const maxTotalTurns = toNumber(raw.maxTotalTurns);
9594
+ const includeSubagents = toBoolean(raw.includeSubagents);
9595
+ const includeSeeded = toBoolean(raw.includeSeeded);
9596
+ const includeNoCwd = toBoolean(raw.includeNoCwd);
9503
9597
  return {
9504
9598
  ...days === void 0 ? {} : { days },
9505
9599
  ...maxTurnsPerSession === void 0 ? {} : { maxTurnsPerSession },
@@ -9538,7 +9632,7 @@ function registerEngramRoutes(ctx, deps) {
9538
9632
  };
9539
9633
  /** POST body 里的项目选择器(面板所有写操作都会带)。 */
9540
9634
  const adoptBodySelector = (body) => {
9541
- const raw = body?.["project"];
9635
+ const raw = body?.project;
9542
9636
  if (typeof raw === "string" && raw !== "") selectorRef.value = raw;
9543
9637
  };
9544
9638
  try {
@@ -9920,32 +10014,6 @@ function registerEngramRoutes(ctx, deps) {
9920
10014
  json(res, 200, view);
9921
10015
  return;
9922
10016
  }
9923
- if (req.method === "GET" && route === "review-due") {
9924
- const scope = scopeOf$1(url.searchParams.get("scope"), "user");
9925
- const limit = Math.min(50, Math.max(1, Number(url.searchParams.get("limit") ?? 20) || 20));
9926
- const now = Date.now();
9927
- const due = await (await storeFor(scope)).dueReviews(now, limit);
9928
- json(res, 200, {
9929
- scope,
9930
- count: due.length,
9931
- now,
9932
- items: due.map((record) => ({
9933
- id: record.id,
9934
- kind: record.kind,
9935
- scope: record.scope,
9936
- importance: record.importance,
9937
- confidence: record.confidence,
9938
- ...record.slot === void 0 ? {} : { slot: record.slot },
9939
- ...typeof record.imagery?.caption === "string" ? { caption: record.imagery.caption } : {},
9940
- ...record.review?.nextReviewAt === null || record.review?.nextReviewAt === void 0 ? {} : { overdueDays: Math.max(0, Math.floor((now - record.review.nextReviewAt) / 864e5)) },
9941
- ...record.review === void 0 ? {} : {
9942
- reps: record.review.reps,
9943
- intervalDays: record.review.intervalDays
9944
- }
9945
- }))
9946
- });
9947
- return;
9948
- }
9949
10017
  if (req.method === "GET" && route === "export") {
9950
10018
  const scope = scopeOf$1(url.searchParams.get("scope"), "user");
9951
10019
  const format = url.searchParams.get("format") === "json" ? "json" : "markdown";
@@ -10092,16 +10160,16 @@ function registerEngramRoutes(ctx, deps) {
10092
10160
  if (rawStatus !== null && rawStatus !== "" && rawStatus !== "all") {
10093
10161
  for (const s of rawStatus.split(",")) if (s === "active" || s === "archived" || s === "forgotten") includeStatuses.add(s);
10094
10162
  }
10163
+ const limit = Math.min(1e3, Math.max(50, Number(url.searchParams.get("limit") ?? 400) || 400));
10095
10164
  const data = await (await storeFor(scope)).exportAll();
10096
- const nodes = data.records.filter((r) => includeStatuses.has(r.status)).map((r) => ({
10165
+ const nodes = data.records.filter((r) => includeStatuses.has(r.status)).sort((a, b) => b.importance * b.confidence - a.importance * a.confidence).slice(0, limit).map((r) => ({
10097
10166
  id: r.id,
10098
10167
  scope: r.scope,
10099
10168
  kind: r.kind,
10100
10169
  status: r.status,
10101
10170
  importance: r.importance,
10102
10171
  confidence: r.confidence,
10103
- title: r.content.length > 40 ? `${r.content.slice(0, 40)}…` : r.content,
10104
- content: r.content
10172
+ title: r.content.length > 40 ? `${r.content.slice(0, 40)}…` : r.content
10105
10173
  }));
10106
10174
  const allowedIds = new Set(nodes.map((n) => n.id));
10107
10175
  json(res, 200, {
@@ -10112,7 +10180,8 @@ function registerEngramRoutes(ctx, deps) {
10112
10180
  from: e.from,
10113
10181
  to: e.to,
10114
10182
  type: e.type
10115
- }))
10183
+ })),
10184
+ total: data.records.filter((r) => includeStatuses.has(r.status)).length
10116
10185
  });
10117
10186
  return;
10118
10187
  }
@@ -10233,7 +10302,7 @@ function registerEngramRoutes(ctx, deps) {
10233
10302
  if (!guardWrite(req, res)) return;
10234
10303
  const body = await readBody(req, res, deps.language);
10235
10304
  if (body === null) return;
10236
- const set = body["set"];
10305
+ const set = body.set;
10237
10306
  if (!isDictionarySet(set)) {
10238
10307
  json(res, 400, {
10239
10308
  ok: false,
@@ -10241,18 +10310,18 @@ function registerEngramRoutes(ctx, deps) {
10241
10310
  });
10242
10311
  return;
10243
10312
  }
10244
- json(res, 200, deps.dictionary.write(set, entriesFromRequest(body["entries"]), languageOf(body["language"])));
10313
+ json(res, 200, deps.dictionary.write(set, entriesFromRequest(body.entries), languageOf(body.language)));
10245
10314
  return;
10246
10315
  }
10247
10316
  if (req.method === "POST" && route === "dictionary-create") {
10248
10317
  if (!guardWrite(req, res)) return;
10249
10318
  const body = await readBody(req, res, deps.language);
10250
10319
  if (body === null) return;
10251
- const language = languageOf(body["language"]);
10320
+ const language = languageOf(body.language);
10252
10321
  if (language === void 0) {
10253
10322
  json(res, 400, {
10254
10323
  ok: false,
10255
- error: `unknown language: ${String(body["language"])}`
10324
+ error: `unknown language: ${String(body.language)}`
10256
10325
  });
10257
10326
  return;
10258
10327
  }
@@ -10266,7 +10335,7 @@ function registerEngramRoutes(ctx, deps) {
10266
10335
  if (!guardWrite(req, res)) return;
10267
10336
  const body = await readBody(req, res, deps.language);
10268
10337
  if (body === null) return;
10269
- const set = body["set"];
10338
+ const set = body.set;
10270
10339
  if (!isDictionarySet(set)) {
10271
10340
  json(res, 400, {
10272
10341
  ok: false,
@@ -10274,7 +10343,7 @@ function registerEngramRoutes(ctx, deps) {
10274
10343
  });
10275
10344
  return;
10276
10345
  }
10277
- const terms = (Array.isArray(body["terms"]) ? body["terms"] : []).filter((term) => typeof term === "string");
10346
+ const terms = (Array.isArray(body.terms) ? body.terms : []).filter((term) => typeof term === "string");
10278
10347
  json(res, 200, await deps.dictionaryFill(set, terms));
10279
10348
  return;
10280
10349
  }
@@ -10305,7 +10374,7 @@ function registerEngramRoutes(ctx, deps) {
10305
10374
  if (!guardWrite(req, res)) return;
10306
10375
  const body = await readBody(req, res, deps.language);
10307
10376
  if (body === null) return;
10308
- const verdict = deps.palaceGuard.check(typeof body["language"] === "string" ? body["language"] : "", Date.now(), deps.palaceChangeWindowSec);
10377
+ const verdict = deps.palaceGuard.check(typeof body.language === "string" ? body.language : "", Date.now(), deps.palaceChangeWindowSec);
10309
10378
  if (!verdict.ok) {
10310
10379
  json(res, 409, {
10311
10380
  ok: false,
@@ -10314,7 +10383,7 @@ function registerEngramRoutes(ctx, deps) {
10314
10383
  return;
10315
10384
  }
10316
10385
  deps.palaceGuard.close();
10317
- const written = await deps.writePalaceLanguage(String(body["language"]));
10386
+ const written = await deps.writePalaceLanguage(String(body.language));
10318
10387
  if (!written.ok) {
10319
10388
  json(res, 200, {
10320
10389
  ...written,
@@ -10336,7 +10405,7 @@ function registerEngramRoutes(ctx, deps) {
10336
10405
  if (!guardWrite(req, res)) return;
10337
10406
  const body = await readBody(req, res, deps.language);
10338
10407
  if (body === null) return;
10339
- const wanted = body["language"];
10408
+ const wanted = body.language;
10340
10409
  if (typeof wanted !== "string" || !isLanguageCode(wanted)) {
10341
10410
  json(res, 400, {
10342
10411
  ok: false,
@@ -10533,8 +10602,8 @@ function registerEngramRoutes(ctx, deps) {
10533
10602
  return;
10534
10603
  }
10535
10604
  try {
10536
- const result = await restoreBackup(normalized, deps.dbDir);
10537
10605
  await deps.closeStores();
10606
+ const result = await restoreBackup(normalized, deps.dbDir);
10538
10607
  json(res, 200, {
10539
10608
  restored: result.restored,
10540
10609
  meta: result.archiveMeta
@@ -10904,6 +10973,35 @@ async function readBaseCensus(input) {
10904
10973
  problems
10905
10974
  };
10906
10975
  }
10976
+ /**
10977
+ * Язык, на котором стоит база, — по метке `meta.model_label`, лёгкое чтение.
10978
+ *
10979
+ * Отдельно от `readBaseCensus`: выбору модели при сборке плагина (issue #39) нужен
10980
+ * только язык существующей базы, а не полная опись векторов. Те же гарантии, что и
10981
+ * у описи: read-only соединение, отсутствие файла — «базы нет», а не ошибка,
10982
+ * любая проблема чтения — `undefined`, а не догадка.
10983
+ * @param path - путь к файлу базы.
10984
+ * @returns код языка из метки либо undefined (файла нет, метки нет, читать не удалось).
10985
+ */
10986
+ async function readStoreLanguageLabel(path) {
10987
+ if (!existsSync(path)) return void 0;
10988
+ const { DatabaseSync } = await import("node:sqlite");
10989
+ let db;
10990
+ try {
10991
+ db = new DatabaseSync(path, { readOnly: true });
10992
+ db.exec("PRAGMA busy_timeout = 2000");
10993
+ const row = db.prepare("SELECT value FROM meta WHERE key = ?").get(MODEL_LABEL_KEY);
10994
+ if (row === void 0) return void 0;
10995
+ const language = languageOfLabels([row.value]);
10996
+ return typeof language === "string" ? language : void 0;
10997
+ } catch {
10998
+ return;
10999
+ } finally {
11000
+ try {
11001
+ db?.close();
11002
+ } catch {}
11003
+ }
11004
+ }
10907
11005
  /** Имя таблицы или поля в SQL: в кавычках, кавычка внутри удваивается. */
10908
11006
  function quoted(identifier) {
10909
11007
  return `"${identifier.replace(/"/g, "\"\"")}"`;
@@ -11182,7 +11280,7 @@ var PalaceGuard = class {
11182
11280
  const window = this.window;
11183
11281
  if (window === null) return Math.max(0, Math.round(windowSec));
11184
11282
  const left = Math.round((windowSec * 1e3 - (now - window.startedAt)) / 1e3);
11185
- return left > 0 ? left : 0;
11283
+ return Math.max(left, 0);
11186
11284
  }
11187
11285
  };
11188
11286
  //#endregion
@@ -11499,11 +11597,11 @@ function migrateProjectDb(dbDir, identity, policy = "eager") {
11499
11597
  if (policy === "conservative") return "deferred";
11500
11598
  renameSync(legacy, next);
11501
11599
  try {
11502
- writeFileSync(`${legacy}.migrated-to`, JSON.stringify({
11600
+ writeFileSync(`${legacy}.migrated-to`, `${JSON.stringify({
11503
11601
  migratedTo: identity.dbName,
11504
11602
  claimedByCwd: identity.cwd,
11505
11603
  claimedAt: (/* @__PURE__ */ new Date()).toISOString()
11506
- }) + "\n", {
11604
+ })}\n`, {
11507
11605
  flag: "wx",
11508
11606
  mode: 384
11509
11607
  });
@@ -11953,6 +12051,14 @@ function rowToRecord(row) {
11953
12051
  function blobToVec(blob) {
11954
12052
  return new Float32Array(blob.buffer.slice(blob.byteOffset, blob.byteOffset + blob.byteLength));
11955
12053
  }
12054
+ /**
12055
+ * 单位区间收口:NaN/∞/越界的 importance·confidence 一律拉回 [0,1](非有限数取备用值)。
12056
+ * 工具层不做这道检查(engram_save 直收模型给的数,`typeof NaN === 'number'` 也通过),
12057
+ * 库层是最后一道闸——`importance: 42` 会永久霸榜 topActive,进 session-start 注入。
12058
+ */
12059
+ function clampUnit(value, fallback) {
12060
+ return Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : fallback;
12061
+ }
11956
12062
  function entityRowToRecord(row) {
11957
12063
  let aliases = [];
11958
12064
  try {
@@ -12055,9 +12161,6 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
12055
12161
  });
12056
12162
  const { DatabaseSync } = await import("node:sqlite");
12057
12163
  const db = new DatabaseSync(path);
12058
- db.exec("PRAGMA journal_mode = WAL;");
12059
- db.exec("PRAGMA busy_timeout = 5000;");
12060
- db.exec("PRAGMA synchronous = NORMAL;");
12061
12164
  /** 手工事务:BEGIN/COMMIT/ROLLBACK(兼容 ^22.19 引擎范围)。 */
12062
12165
  const withTransaction = (fn) => {
12063
12166
  db.exec("BEGIN");
@@ -12069,7 +12172,36 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
12069
12172
  throw error;
12070
12173
  }
12071
12174
  };
12072
- db.exec(`
12175
+ /**
12176
+ * 库中已有向量的维度(按浮点数个数计,不按字节)。
12177
+ *
12178
+ * 打开时读一次,且两张带向量的表一起读:记忆与信念彼此比较,任何一边对不上
12179
+ * 都会让语义检索失真。库里还没有向量时维度未知——由第一次写入定下来,在那之前
12180
+ * 没有可拒绝的东西。
12181
+ *
12182
+ * 维度取占多数的那一批,而不是任意一行:库里混进过别的模型的向量时(换过模型、
12183
+ * 从备份恢复过、旧模型的归档残留),任意一行会把整库的维度判给少数派,此后
12184
+ * 正常模型的写入全部被拒,而原因(少数几行异维数据)在错误里看不见。多数派
12185
+ * 保证「这库现在算谁的库」由库里的多数回答;少数派只是被晾在一边,不参与比较。
12186
+ * 表格平局时取较小的维度:写入被拒是响的,静默沿用大维度才是危险的。
12187
+ *
12188
+ * 长度不是 4 的倍数的 BLOB 跳过:那是损坏的行,按它定维会得到 153.25 这样的
12189
+ * 小数,此后每一次写入都被拒绝,而真正的原因(一行坏数据)却看不见。
12190
+ */
12191
+ const computeStoredDim = () => {
12192
+ const dims = db.prepare(`SELECT dim, COUNT(*) AS rows FROM (
12193
+ SELECT LENGTH(embedding) / ${Float32Array.BYTES_PER_ELEMENT} AS dim FROM nodes WHERE embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0
12194
+ UNION ALL
12195
+ SELECT LENGTH(embedding) / ${Float32Array.BYTES_PER_ELEMENT} AS dim FROM observations WHERE embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0
12196
+ ) GROUP BY dim ORDER BY rows DESC, dim ASC`).all();
12197
+ return dims.length === 0 ? void 0 : dims[0].dim;
12198
+ };
12199
+ let storedDim;
12200
+ try {
12201
+ db.exec("PRAGMA journal_mode = WAL;");
12202
+ db.exec("PRAGMA busy_timeout = 5000;");
12203
+ db.exec("PRAGMA synchronous = NORMAL;");
12204
+ db.exec(`
12073
12205
  CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
12074
12206
  CREATE TABLE IF NOT EXISTS nodes (
12075
12207
  id TEXT PRIMARY KEY, scope TEXT NOT NULL, kind TEXT NOT NULL, content TEXT NOT NULL,
@@ -12123,78 +12255,57 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
12123
12255
  CREATE INDEX IF NOT EXISTS facts_entity ON facts (entity_id);
12124
12256
  CREATE INDEX IF NOT EXISTS observations_status ON observations (status, proof_count, updated_at);
12125
12257
  `);
12126
- const versionRow = db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get();
12127
- if (versionRow === void 0) {
12128
- db.exec(`CREATE INDEX IF NOT EXISTS nodes_slot ON nodes (slot_room, slot_index);
12129
- CREATE INDEX IF NOT EXISTS nodes_review_due ON nodes (next_review_at);`);
12130
- db.prepare("INSERT INTO meta (key, value) VALUES ('schema_version', ?)").run(String(SCHEMA_VERSION));
12131
- } else {
12132
- let version = Number(versionRow.value);
12133
- if (!Number.isInteger(version) || version > SCHEMA_VERSION) {
12134
- db.close();
12135
- throw new EngramError("SCHEMA_INCOMPATIBLE", message("store.schemaVersionTooNew", {
12258
+ const versionRow = db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get();
12259
+ if (versionRow === void 0) {
12260
+ db.exec(`CREATE INDEX IF NOT EXISTS nodes_slot ON nodes (slot_room, slot_index);
12261
+ CREATE INDEX IF NOT EXISTS nodes_review_due ON nodes (next_review_at);`);
12262
+ db.prepare("INSERT INTO meta (key, value) VALUES ('schema_version', ?)").run(String(SCHEMA_VERSION));
12263
+ } else {
12264
+ let version = Number(versionRow.value);
12265
+ if (!Number.isInteger(version) || version > SCHEMA_VERSION) throw new EngramError("SCHEMA_INCOMPATIBLE", message("store.schemaVersionTooNew", {
12136
12266
  found: versionRow.value,
12137
12267
  supported: SCHEMA_VERSION,
12138
12268
  path
12139
12269
  }));
12140
- }
12141
- if (version < SCHEMA_VERSION) try {
12142
- await copyFile(path, `${path}.pre-migration`);
12143
- } catch (error) {
12144
- console.warn(message("store.preMigrationSnapshotFailed", {
12145
- path,
12146
- msg: error instanceof Error ? error.message : String(error)
12147
- }));
12148
- }
12149
- while (version < SCHEMA_VERSION) {
12150
- const migration = MIGRATIONS[String(version)];
12151
- if (migration === void 0) {
12152
- db.close();
12153
- throw new EngramError("SCHEMA_INCOMPATIBLE", message("store.schemaMigrationMissing", {
12270
+ if (version < SCHEMA_VERSION) try {
12271
+ await copyFile(path, `${path}.pre-migration`);
12272
+ } catch (error) {
12273
+ console.warn(message("store.preMigrationSnapshotFailed", {
12274
+ path,
12275
+ msg: error instanceof Error ? error.message : String(error)
12276
+ }));
12277
+ }
12278
+ while (version < SCHEMA_VERSION) {
12279
+ const migration = MIGRATIONS[String(version)];
12280
+ if (migration === void 0) throw new EngramError("SCHEMA_INCOMPATIBLE", message("store.schemaMigrationMissing", {
12154
12281
  found: version,
12155
12282
  supported: SCHEMA_VERSION,
12156
12283
  path
12157
12284
  }));
12158
- }
12159
- withTransaction(() => {
12160
- const safe = migration.split(";").map((stmt) => stmt.trim()).filter((stmt) => stmt !== "").filter((stmt) => {
12161
- const match = /^ALTER\s+TABLE\s+(\w+)\s+ADD\s+COLUMN\s+(\w+)/i.exec(stmt);
12162
- if (match === null) return true;
12163
- const table = match[1];
12164
- const column = match[2];
12165
- return !db.prepare(`PRAGMA table_info(${table})`).all().some((c) => c.name === column);
12285
+ withTransaction(() => {
12286
+ const safe = migration.split(";").map((stmt) => stmt.trim()).filter((stmt) => stmt !== "").filter((stmt) => {
12287
+ const match = /^ALTER\s+TABLE\s+(\w+)\s+ADD\s+COLUMN\s+(\w+)/i.exec(stmt);
12288
+ if (match === null) return true;
12289
+ const table = match[1];
12290
+ const column = match[2];
12291
+ return !db.prepare(`PRAGMA table_info(${table})`).all().some((c) => c.name === column);
12292
+ });
12293
+ for (const stmt of safe) db.exec(stmt);
12294
+ version += 1;
12295
+ db.prepare("UPDATE meta SET value = ? WHERE key = 'schema_version'").run(String(version));
12166
12296
  });
12167
- for (const stmt of safe) db.exec(stmt);
12168
- version += 1;
12169
- db.prepare("UPDATE meta SET value = ? WHERE key = 'schema_version'").run(String(version));
12170
- });
12297
+ }
12171
12298
  }
12299
+ db.exec(`CREATE INDEX IF NOT EXISTS op_log_op_detail ON op_log (op, detail);
12300
+ CREATE INDEX IF NOT EXISTS op_log_target ON op_log (target_id);
12301
+ CREATE INDEX IF NOT EXISTS op_log_at ON op_log (at);`);
12302
+ storedDim = computeStoredDim();
12303
+ } catch (error) {
12304
+ try {
12305
+ db.close();
12306
+ } catch {}
12307
+ throw error;
12172
12308
  }
12173
- /**
12174
- * 库中已有向量的维度(按浮点数个数计,不按字节)。
12175
- *
12176
- * 打开时读一次,且两张带向量的表一起读:记忆与信念彼此比较,任何一边对不上
12177
- * 都会让语义检索失真。库里还没有向量时维度未知——由第一次写入定下来,在那之前
12178
- * 没有可拒绝的东西。
12179
- *
12180
- * 维度取占多数的那一批,而不是任意一行:库里混进过别的模型的向量时(换过模型、
12181
- * 从备份恢复过、旧模型的归档残留),任意一行会把整库的维度判给少数派,此后
12182
- * 正常模型的写入全部被拒,而原因(少数几行异维数据)在错误里看不见。多数派
12183
- * 保证「这库现在算谁的库」由库里的多数回答;少数派只是被晾在一边,不参与比较。
12184
- * 表格平局时取较小的维度:写入被拒是响的,静默沿用大维度才是危险的。
12185
- *
12186
- * 长度不是 4 的倍数的 BLOB 跳过:那是损坏的行,按它定维会得到 153.25 这样的
12187
- * 小数,此后每一次写入都被拒绝,而真正的原因(一行坏数据)却看不见。
12188
- */
12189
- const computeStoredDim = () => {
12190
- const dims = db.prepare(`SELECT dim, COUNT(*) AS rows FROM (
12191
- SELECT LENGTH(embedding) / ${Float32Array.BYTES_PER_ELEMENT} AS dim FROM nodes WHERE embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0
12192
- UNION ALL
12193
- SELECT LENGTH(embedding) / ${Float32Array.BYTES_PER_ELEMENT} AS dim FROM observations WHERE embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0
12194
- ) GROUP BY dim ORDER BY rows DESC, dim ASC`).all();
12195
- return dims.length === 0 ? void 0 : dims[0].dim;
12196
- };
12197
- let storedDim = computeStoredDim();
12198
12309
  /** 维度不符导致读取降级时只提醒一次:检索是高频路径,逐次告警会淹掉日志。 */
12199
12310
  let warnedDegradedRead = false;
12200
12311
  /**
@@ -12211,6 +12322,9 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
12211
12322
  expected,
12212
12323
  found: length
12213
12324
  }));
12325
+ if (vector instanceof Float32Array) {
12326
+ for (let i = 0; i < vector.length; i += 1) if (!Number.isFinite(vector[i])) throw new EngramError("EMBEDDER_INVALID_VECTOR", message("store.embedderInvalidVector"));
12327
+ }
12214
12328
  storedDim = length;
12215
12329
  };
12216
12330
  const sqlGet = db.prepare("SELECT * FROM nodes WHERE id = ?");
@@ -12248,6 +12362,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
12248
12362
  SELECT id FROM walk WHERE depth > 0
12249
12363
  )`);
12250
12364
  const sqlRecentOps = db.prepare("SELECT at, op, target_id, detail FROM op_log ORDER BY seq DESC LIMIT ?");
12365
+ const sqlCountOps = db.prepare("SELECT op, COUNT(*) AS n FROM op_log WHERE at >= ? GROUP BY op");
12251
12366
  const sqlRevisionInsert = db.prepare("INSERT INTO nodes_revisions (node_id, content, kind, importance, superseded_at) VALUES (?, ?, ?, ?, ?)");
12252
12367
  const sqlRevisionsById = db.prepare("SELECT content, kind, importance, superseded_at FROM nodes_revisions WHERE node_id = ? ORDER BY superseded_at DESC");
12253
12368
  const sqlTopActive = db.prepare("SELECT * FROM nodes WHERE scope = ? AND status = 'active' ORDER BY importance DESC, confidence DESC LIMIT ?");
@@ -12351,7 +12466,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
12351
12466
  const sqlObservationSetStatus = db.prepare("UPDATE observations SET status = ?, updated_at = ?, last_validated_at = ? WHERE id = ?");
12352
12467
  const sqlObservationMarkStale = db.prepare("UPDATE observations SET status = 'stale' WHERE id = ?");
12353
12468
  const sqlObservationRefute = db.prepare("UPDATE observations SET status = 'refuted', updated_at = ? WHERE id = ?");
12354
- const sqlObservationActiveVectors = db.prepare("SELECT * FROM observations WHERE status != 'refuted' AND embedding IS NOT NULL");
12469
+ const sqlObservationActiveVectors = db.prepare(`SELECT * FROM observations WHERE status != 'refuted' AND embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0`);
12355
12470
  const sqlPurgeObservations = db.prepare("DELETE FROM observations");
12356
12471
  /** FTS 道:按 scope 集合检索(占位符动态生成,scope 集合由调用方去重);rooms 非空时只查指定房间。 */
12357
12472
  const ftsSearch = (match, scopes, rooms) => {
@@ -12361,11 +12476,128 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
12361
12476
  WHERE nodes_fts MATCH ? AND n.status = 'active' AND n.scope IN (${placeholders})${roomCond}
12362
12477
  ORDER BY bm25(nodes_fts) LIMIT ${RANK_POOL}`).all(match, ...scopes, ...rooms ?? []);
12363
12478
  };
12364
- /** 向量候选池:active 且带向量的条目,按 scope 集合过滤(占位符动态生成);rooms 非空时只查指定房间。 */
12479
+ /** 向量候选池:active 且带向量的条目,按 scope 集合过滤(占位符动态生成);rooms 非空时只查指定房间。
12480
+ * 长度非 4 倍数的 BLOB(损坏行)不进池:读道按 computeStoredDim 同一标准跳过,
12481
+ * 否则一个坏行让 blobToVec 抛 RangeError,整个 engram_search 报错而不是降级(审计 N10)。 */
12365
12482
  const vectorPool = (scopes, rooms) => {
12366
12483
  const placeholders = scopes.map(() => "?").join(",");
12367
12484
  const roomCond = rooms === void 0 || rooms.length === 0 ? "" : ` AND slot_room IN (${rooms.map(() => "?").join(",")})`;
12368
- return db.prepare(`SELECT * FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND scope IN (${placeholders})${roomCond}`).all(...scopes, ...rooms ?? []);
12485
+ return db.prepare(`SELECT * FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0 AND scope IN (${placeholders})${roomCond}`).all(...scopes, ...rooms ?? []);
12486
+ };
12487
+ /**
12488
+ * 向量道的进程内打包矩阵(审计 P1-2)。双道检索每次查询、写入处置(最近邻/矛盾探测)
12489
+ * 每次写入都要全池余弦:旧实现逐行 new Float32Array + 逐行调 cosine——一万条即单扫
12490
+ * 15MB 垃圾、380 万次乘加,scope=all 一次检索最多十二连扫。打包成单个 Float32Array
12491
+ * 后用紧凑点积循环一次算完:一次分配、零逐行对象、无函数调用开销。
12492
+ *
12493
+ * 失效:gen 取 op_log 的 MAX(seq)(PK 上 O(1))。所有会改变候选集的写路径都写 op_log
12494
+ * (insertRecord/update/forget/forgetWithTombstone/restore/import/decay/reinforce),
12495
+ * 例外只有 attachEmbeddings / rewriteVectors / purge 三个——它们各自显式
12496
+ * bumpVectorMatrix()。新增会改 nodes.embedding 或 status 的写路径时必须补失效,
12497
+ * 否则检索对着旧矩阵打分——这条约定与「加列走 MIGRATIONS」同级,忘不得。
12498
+ *
12499
+ * 上限:超过 VECTOR_MATRIX_MAX_ROWS 不缓存(常数内存上界),调用方回落逐行流式扫描
12500
+ * ——那个量级该上 sqlite-vec,而不是把 150MB 钉在进程里。
12501
+ */
12502
+ const VECTOR_MATRIX_MAX_ROWS = 5e4;
12503
+ const sqlMatrixGen = db.prepare("SELECT COALESCE(MAX(seq), 0) AS g FROM op_log");
12504
+ const sqlMatrixCount = db.prepare(`SELECT COUNT(*) AS n FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0`);
12505
+ const sqlMatrixPool = db.prepare(`SELECT id, scope, slot_room, embedding FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0`);
12506
+ const SCOPE_ORDER = [
12507
+ "user",
12508
+ "project",
12509
+ "shared"
12510
+ ];
12511
+ let vecMatrix;
12512
+ /** overflow 判定按 gen 缓存:超限库里每次查询都 COUNT 一次(O(索引)),不再物化全池。 */
12513
+ let vecOverflowGen = -1;
12514
+ const roomTable = /* @__PURE__ */ new Map();
12515
+ const bumpVectorMatrix = () => {
12516
+ vecMatrix = void 0;
12517
+ vecOverflowGen = -1;
12518
+ };
12519
+ const vectorMatrix = () => {
12520
+ const gen = Number(sqlMatrixGen.get().g);
12521
+ if (vecMatrix !== void 0 && vecMatrix.gen === gen) return vecMatrix;
12522
+ if (vecOverflowGen === gen) return "overflow";
12523
+ const count = Number(sqlMatrixCount.get().n);
12524
+ if (count === 0) {
12525
+ vecMatrix = void 0;
12526
+ return;
12527
+ }
12528
+ if (count > VECTOR_MATRIX_MAX_ROWS) {
12529
+ vecOverflowGen = gen;
12530
+ return "overflow";
12531
+ }
12532
+ const all = sqlMatrixPool.all();
12533
+ const dimCounts = /* @__PURE__ */ new Map();
12534
+ for (const row of all) {
12535
+ const d = row.embedding.byteLength / Float32Array.BYTES_PER_ELEMENT;
12536
+ dimCounts.set(d, (dimCounts.get(d) ?? 0) + 1);
12537
+ }
12538
+ const dim = [...dimCounts.entries()].sort((a, b) => b[1] - a[1] || a[0] - b[0])[0][0];
12539
+ const rows = dimCounts.size === 1 ? all : all.filter((row) => row.embedding.byteLength / Float32Array.BYTES_PER_ELEMENT === dim);
12540
+ const packed = new Float32Array(rows.length * dim);
12541
+ const scopeIdx = new Uint8Array(rows.length);
12542
+ const roomIdx = new Uint16Array(rows.length);
12543
+ for (let i = 0; i < rows.length; i += 1) {
12544
+ const row = rows[i];
12545
+ const blob = row.embedding;
12546
+ const floats = new Float32Array(blob.buffer, blob.byteOffset, blob.byteLength / Float32Array.BYTES_PER_ELEMENT);
12547
+ let norm = 0;
12548
+ for (let k = 0; k < floats.length; k += 1) norm += floats[k] * floats[k];
12549
+ norm = Math.sqrt(norm);
12550
+ const base = i * dim;
12551
+ if (norm > 0) for (let k = 0; k < floats.length; k += 1) packed[base + k] = floats[k] / norm;
12552
+ scopeIdx[i] = Math.max(0, SCOPE_ORDER.indexOf(row.scope));
12553
+ if (row.slot_room === null) roomIdx[i] = 65535;
12554
+ else {
12555
+ let idx = roomTable.get(row.slot_room);
12556
+ if (idx === void 0) {
12557
+ idx = roomTable.size;
12558
+ roomTable.set(row.slot_room, idx);
12559
+ }
12560
+ roomIdx[i] = idx;
12561
+ }
12562
+ }
12563
+ vecMatrix = {
12564
+ gen,
12565
+ dim,
12566
+ ids: rows.map((row) => row.id),
12567
+ packed,
12568
+ scopeIdx,
12569
+ roomIdx
12570
+ };
12571
+ return vecMatrix;
12572
+ };
12573
+ /** 归一化副本(零向量原样返回)。查询侧每次调用做一次,行侧在打包时已归一。 */
12574
+ const unit = (vector) => {
12575
+ let norm = 0;
12576
+ for (let k = 0; k < vector.length; k += 1) norm += vector[k] * vector[k];
12577
+ norm = Math.sqrt(norm);
12578
+ if (norm === 0) return vector;
12579
+ const out = new Float32Array(vector.length);
12580
+ for (let k = 0; k < vector.length; k += 1) out[k] = vector[k] / norm;
12581
+ return out;
12582
+ };
12583
+ /** 紧凑点积:矩阵第 i 行与单位化 query 的内积(行已在打包时归一化,等价余弦)。 */
12584
+ const dotAt = (matrix, i, queryUnit) => {
12585
+ const base = i * matrix.dim;
12586
+ let dot = 0;
12587
+ for (let k = 0; k < matrix.dim; k += 1) dot += queryUnit[k] * matrix.packed[base + k];
12588
+ return dot;
12589
+ };
12590
+ /** RRF 计分累加:同 id 多道命中合并(via 升级为 both)。 */
12591
+ const addScore = (scores, id, add, via) => {
12592
+ const existing = scores.get(id);
12593
+ if (existing === void 0) scores.set(id, {
12594
+ score: add,
12595
+ via
12596
+ });
12597
+ else scores.set(id, {
12598
+ score: existing.score + add,
12599
+ via: "both"
12600
+ });
12369
12601
  };
12370
12602
  const getRow = (id) => sqlGet.get(id);
12371
12603
  /**
@@ -12376,7 +12608,9 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
12376
12608
  if (embedding !== null) guardDimension(embedding);
12377
12609
  const stored = embedding === null ? null : embedding instanceof Float32Array ? vecToBlob(embedding) : embedding;
12378
12610
  const initialReview = input.initialReviewAt ?? null;
12379
- sqlInsert.run(id, input.scope, input.kind, content, importance, confidence, at, at, sourceSessionId, input.sourceRound ?? null, input.sourceSeq ?? null, stored, imageryToJson(imagery), input.slot?.room ?? null, input.slot?.index ?? null, input.imageryScore ?? null, initialReview, initialReview === null ? null : 2.5, initialReview === null ? null : 0);
12611
+ const safeImportance = clampUnit(importance, .5);
12612
+ const safeConfidence = clampUnit(confidence, .5);
12613
+ sqlInsert.run(id, input.scope, input.kind, content, safeImportance, safeConfidence, at, at, sourceSessionId, input.sourceRound ?? null, input.sourceSeq ?? null, stored, imageryToJson(imagery), input.slot?.room ?? null, input.slot?.index ?? null, input.imageryScore ?? null, initialReview, initialReview === null ? null : 2.5, initialReview === null ? null : 0);
12380
12614
  sqlFtsInsert.run(id, tokenizeForFts(content));
12381
12615
  sqlLog.run(at, op, id, JSON.stringify({
12382
12616
  kind: input.kind,
@@ -12528,22 +12762,40 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
12528
12762
  });
12529
12763
  let degraded = true;
12530
12764
  if (queryVector !== void 0) try {
12531
- vectorPool(query.scopes, query.rooms).map((row) => ({
12532
- row,
12533
- sim: cosine(queryVector, blobToVec(row.embedding))
12534
- })).filter((entry) => entry.sim >= MIN_COSINE).sort((a, b) => b.sim - a.sim).slice(0, RANK_POOL).forEach((entry, index) => {
12535
- const add = 1 / (RRF_K + index + 1);
12536
- const existing = scores.get(entry.row.id);
12537
- if (existing === void 0) scores.set(entry.row.id, {
12538
- score: add,
12539
- via: "vec"
12765
+ const matrix = vectorMatrix();
12766
+ if (matrix === void 0) degraded = false;
12767
+ else if (matrix === "overflow") {
12768
+ vectorPool(query.scopes, query.rooms).map((row) => ({
12769
+ id: row.id,
12770
+ sim: cosine(queryVector, blobToVec(row.embedding))
12771
+ })).filter((entry) => entry.sim >= MIN_COSINE).sort((a, b) => b.sim - a.sim).slice(0, RANK_POOL).forEach((entry, index) => {
12772
+ addScore(scores, entry.id, 1 / (RRF_K + index + 1), "vec");
12540
12773
  });
12541
- else scores.set(entry.row.id, {
12542
- score: existing.score + add,
12543
- via: "both"
12774
+ degraded = false;
12775
+ } else {
12776
+ if (queryVector.length !== matrix.dim) throw new EngramError("EMBEDDER_DIMENSION_MISMATCH", message("store.embedderDimensionMismatch", {
12777
+ expected: matrix.dim,
12778
+ found: queryVector.length
12779
+ }));
12780
+ const scopeMask = query.scopes.reduce((mask, scope) => mask | 1 << Math.max(0, SCOPE_ORDER.indexOf(scope)), 0);
12781
+ const roomSet = query.rooms === void 0 || query.rooms.length === 0 ? null : new Set(query.rooms.map((room) => roomTable.get(room) ?? -1));
12782
+ const queryUnit = unit(queryVector);
12783
+ const hits = [];
12784
+ for (let i = 0; i < matrix.ids.length; i += 1) {
12785
+ if ((scopeMask & 1 << matrix.scopeIdx[i]) === 0) continue;
12786
+ if (roomSet !== null && !roomSet.has(matrix.roomIdx[i])) continue;
12787
+ const sim = dotAt(matrix, i, queryUnit);
12788
+ if (sim >= MIN_COSINE) hits.push({
12789
+ index: i,
12790
+ sim
12791
+ });
12792
+ }
12793
+ hits.sort((a, b) => b.sim - a.sim);
12794
+ hits.slice(0, RANK_POOL).forEach((hit, index) => {
12795
+ addScore(scores, matrix.ids[hit.index], 1 / (RRF_K + index + 1), "vec");
12544
12796
  });
12545
- });
12546
- degraded = false;
12797
+ degraded = false;
12798
+ }
12547
12799
  } catch (error) {
12548
12800
  if (!isDimensionMismatch(error)) throw error;
12549
12801
  if (!warnedDegradedRead) {
@@ -12977,6 +13229,25 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
12977
13229
  detail: op.detail
12978
13230
  }));
12979
13231
  },
13232
+ async countOps(sinceMs) {
13233
+ const rows = sqlCountOps.all(sinceMs);
13234
+ const counts = {};
13235
+ for (const row of rows) counts[row.op] = row.n;
13236
+ return counts;
13237
+ },
13238
+ async countOpsByDay(sinceMs, untilMs, ops) {
13239
+ const placeholders = ops.map(() => "?").join(", ");
13240
+ return db.prepare(`SELECT at / 86400000 AS utcDay, COUNT(*) AS n FROM op_log
13241
+ WHERE at >= ? AND at <= ? AND op IN (${placeholders}) GROUP BY utcDay`).all(sinceMs, untilMs, ...ops).map((row) => ({
13242
+ utcDay: Number(row.utcDay),
13243
+ count: row.n
13244
+ }));
13245
+ },
13246
+ async pruneOps(olderThanMs, keepOps) {
13247
+ const placeholders = keepOps.map(() => "?").join(", ");
13248
+ const result = db.prepare(`DELETE FROM op_log WHERE at < ? AND op NOT IN (${placeholders})`).run(olderThanMs, ...keepOps);
13249
+ return Number(result.changes);
13250
+ },
12980
13251
  async stats() {
12981
13252
  const statusRows = sqlCountBy.all();
12982
13253
  const kindRows = sqlCountKind.all();
@@ -12990,9 +13261,9 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
12990
13261
  for (const row of statusRows) byStatus[row.status] = row.n;
12991
13262
  const byKind = {};
12992
13263
  for (const row of kindRows) byKind[row.kind] = row.n;
12993
- const active = byStatus["active"] ?? 0;
12994
- const archived = byStatus["archived"] ?? 0;
12995
- const forgotten = byStatus["forgotten"] ?? 0;
13264
+ const active = byStatus.active ?? 0;
13265
+ const archived = byStatus.archived ?? 0;
13266
+ const forgotten = byStatus.forgotten ?? 0;
12996
13267
  const total = active + archived + forgotten;
12997
13268
  return {
12998
13269
  total,
@@ -13074,7 +13345,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
13074
13345
  for (const c of eligible) {
13075
13346
  const r = c.record;
13076
13347
  const review = r.review;
13077
- sqlImportInsert.run(r.id, r.scope, r.kind, c.content, r.importance, r.confidence, r.status, r.createdAt, r.lastAccessedAt, r.accessCount, r.sourceSessionId ?? null, r.sourceRound ?? null, r.sourceSeq ?? null, null, r.outcome ?? null, imageryToJson(r.imagery), r.slot?.room ?? null, r.slot?.index ?? null, r.imageryScore ?? null, review?.nextReviewAt ?? null, review?.easeFactor ?? null, review?.intervalDays ?? null, review?.reps ?? null);
13348
+ sqlImportInsert.run(r.id, r.scope, r.kind, c.content, clampUnit(r.importance, .5), clampUnit(r.confidence, .5), r.status, r.createdAt, r.lastAccessedAt, r.accessCount, r.sourceSessionId ?? null, r.sourceRound ?? null, r.sourceSeq ?? null, null, r.outcome ?? null, imageryToJson(r.imagery), r.slot?.room ?? null, r.slot?.index ?? null, r.imageryScore ?? null, review?.nextReviewAt ?? null, review?.easeFactor ?? null, review?.intervalDays ?? null, review?.reps ?? null);
13078
13349
  sqlFtsInsert.run(r.id, tokenizeForFts(c.content));
13079
13350
  }
13080
13351
  for (const edge of data.edges ?? []) {
@@ -13121,12 +13392,20 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
13121
13392
  },
13122
13393
  async attachEmbeddings(entries) {
13123
13394
  let attached = 0;
13124
- withTransaction(() => {
13125
- for (const entry of entries) {
13126
- const result = sqlImportAttachEmbedding.run(vecToBlob(entry.embedding), entry.id);
13127
- attached += Number(result.changes);
13128
- }
13129
- });
13395
+ try {
13396
+ withTransaction(() => {
13397
+ for (const entry of entries) {
13398
+ guardDimension(entry.embedding);
13399
+ const result = sqlImportAttachEmbedding.run(vecToBlob(entry.embedding), entry.id);
13400
+ attached += Number(result.changes);
13401
+ }
13402
+ });
13403
+ } catch (error) {
13404
+ storedDim = computeStoredDim();
13405
+ throw error;
13406
+ }
13407
+ storedDim = computeStoredDim();
13408
+ bumpVectorMatrix();
13130
13409
  return attached;
13131
13410
  },
13132
13411
  async rewriteVectors(targets) {
@@ -13138,6 +13417,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
13138
13417
  }
13139
13418
  });
13140
13419
  storedDim = computeStoredDim();
13420
+ bumpVectorMatrix();
13141
13421
  return written;
13142
13422
  },
13143
13423
  async decay(options) {
@@ -13148,26 +13428,67 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
13148
13428
  return changed;
13149
13429
  },
13150
13430
  async findContradictions(embedding, limit = 3) {
13151
- return db.prepare("SELECT * FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND scope IN ('user','project')").all().map((row) => ({
13431
+ const matrix = vectorMatrix();
13432
+ if (matrix === void 0) return [];
13433
+ if (matrix === "overflow") return db.prepare(`SELECT * FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0 AND scope IN ('user','project')`).all().map((row) => ({
13152
13434
  row,
13153
13435
  sim: cosine(embedding, blobToVec(row.embedding))
13154
13436
  })).filter((entry) => entry.sim >= CONTRADICTION_COSINE).sort((a, b) => b.sim - a.sim).slice(0, limit).map((entry) => rowToRecord(entry.row));
13437
+ if (embedding.length !== matrix.dim) throw new EngramError("EMBEDDER_DIMENSION_MISMATCH", message("store.embedderDimensionMismatch", {
13438
+ expected: matrix.dim,
13439
+ found: embedding.length
13440
+ }));
13441
+ const queryUnit = unit(embedding);
13442
+ const hits = [];
13443
+ for (let i = 0; i < matrix.ids.length; i += 1) {
13444
+ if (matrix.scopeIdx[i] > 1) continue;
13445
+ const sim = dotAt(matrix, i, queryUnit);
13446
+ if (sim >= CONTRADICTION_COSINE) hits.push({
13447
+ index: i,
13448
+ sim
13449
+ });
13450
+ }
13451
+ hits.sort((a, b) => b.sim - a.sim);
13452
+ return hits.slice(0, limit).map((hit) => getRow(matrix.ids[hit.index])).filter((row) => row !== void 0).map((row) => rowToRecord(row));
13155
13453
  },
13156
13454
  async linkEdge(from, to, type) {
13157
13455
  sqlEdgeUpsert.run(from, to, type, Date.now());
13158
13456
  },
13159
13457
  async nearestNeighbor(embedding) {
13160
- const pool = db.prepare("SELECT * FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND scope IN ('user','project')").all();
13458
+ const matrix = vectorMatrix();
13459
+ if (matrix === void 0) return void 0;
13460
+ if (matrix === "overflow") {
13461
+ const pool = db.prepare(`SELECT * FROM nodes WHERE status = 'active' AND embedding IS NOT NULL AND LENGTH(embedding) % ${Float32Array.BYTES_PER_ELEMENT} = 0 AND scope IN ('user','project')`).all();
13462
+ let best;
13463
+ for (const row of pool) {
13464
+ const sim = cosine(embedding, blobToVec(row.embedding));
13465
+ if (best === void 0 || sim > best.sim) best = {
13466
+ row,
13467
+ sim
13468
+ };
13469
+ }
13470
+ return best === void 0 ? void 0 : {
13471
+ record: rowToRecord(best.row),
13472
+ similarity: best.sim
13473
+ };
13474
+ }
13475
+ if (embedding.length !== matrix.dim) throw new EngramError("EMBEDDER_DIMENSION_MISMATCH", message("store.embedderDimensionMismatch", {
13476
+ expected: matrix.dim,
13477
+ found: embedding.length
13478
+ }));
13479
+ const queryUnit = unit(embedding);
13161
13480
  let best;
13162
- for (const row of pool) {
13163
- const sim = cosine(embedding, blobToVec(row.embedding));
13481
+ for (let i = 0; i < matrix.ids.length; i += 1) {
13482
+ if (matrix.scopeIdx[i] > 1) continue;
13483
+ const sim = dotAt(matrix, i, queryUnit);
13164
13484
  if (best === void 0 || sim > best.sim) best = {
13165
- row,
13485
+ index: i,
13166
13486
  sim
13167
13487
  };
13168
13488
  }
13169
- return best === void 0 ? void 0 : {
13170
- record: rowToRecord(best.row),
13489
+ const row = best === void 0 ? void 0 : getRow(matrix.ids[best.index]);
13490
+ return row === void 0 || best === void 0 ? void 0 : {
13491
+ record: rowToRecord(row),
13171
13492
  similarity: best.sim
13172
13493
  };
13173
13494
  },
@@ -13463,6 +13784,7 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
13463
13784
  sqlPurgeFacts.run();
13464
13785
  sqlPurgeObservations.run();
13465
13786
  });
13787
+ bumpVectorMatrix();
13466
13788
  },
13467
13789
  async close() {
13468
13790
  db.close();
@@ -13859,7 +14181,7 @@ function assessEvidence(batch, request) {
13859
14181
  //#endregion
13860
14182
  //#region src/tools/create.ts
13861
14183
  /**
13862
- * 21 个 engram_ 工具的定义与执行器。工具 schema 保持窄参数;
14184
+ * 23 个 engram_ 工具的定义与执行器。工具 schema 保持窄参数;
13863
14185
  * scope 决定读写哪个分库;嵌入缺失时检索结果显式标记降级。
13864
14186
  * @module @kenz1117/dsh-engram/tools/create
13865
14187
  */
@@ -14091,8 +14413,8 @@ async function rewriteQueries(deps, exec, query) {
14091
14413
  }
14092
14414
  }
14093
14415
  /**
14094
- * 构造 21 个工具定义(engram_save/search/facts/assess/timeline/episode_timeline/update/forget/report/review/review_queue/
14095
- * stats/export/import/distill/examine/neighbors/audit_forgotten/tour/ingest_history/profile_edit)。
14416
+ * 构造 23 个工具定义(engram_save/search/facts/assess/timeline/episode_timeline/update/forget/report/review/review_queue/
14417
+ * stats/export/import/distill/reflect/examine/neighbors/tour/audit_forgotten/ingest_history/profile_edit/language_guide)。
14096
14418
  * @param baseDeps - 分库打开器、嵌入器、辅助 LLM、导出目录。
14097
14419
  * @returns 可直接 register 的工具定义数组(execute 已绑定会话 cwd 的项目宫殿路由)。
14098
14420
  */
@@ -14651,7 +14973,7 @@ function createEngramTools(baseDeps) {
14651
14973
  const input = args;
14652
14974
  const scopes = scopesOf(input.scope);
14653
14975
  const rooms = typeof input.room === "string" && input.room.trim() !== "" ? [resolveRoomInput(input.room) ?? input.room.trim()] : void 0;
14654
- const limit = input.limit ?? 8;
14976
+ const limit = parseCountParam(input.limit ?? void 0, "engram_search", "limit", 8, 1, 50);
14655
14977
  const asOfMs = parseTimeParam(typeof input.asOf === "string" ? input.asOf : void 0, "engram_search", "asOf");
14656
14978
  const rewrite = await rewriteQueries(deps, exec, input.query);
14657
14979
  const retrievals = await Promise.all(rewrite.queries.map(async (queryText) => {
@@ -16800,12 +17122,12 @@ function estimateTokens(text) {
16800
17122
  }
16801
17123
  //#endregion
16802
17124
  //#region package.json
16803
- var version = "0.11.0";
17125
+ var version = "0.11.1";
16804
17126
  //#endregion
16805
17127
  //#region src/index.ts
16806
17128
  /**
16807
17129
  * dsh-engram:DeepSeek Harness 跨会话长期记忆插件(host 半)。
16808
- * 注册 18 个 engram_ 工具、会话开始注入画像(curated block 优先于派生画像)、
17130
+ * 注册 23 个 engram_ 工具、会话开始注入画像(curated block 优先于派生画像)、
16809
17131
  * 自动摄取上一轮对话、蒸馏/衰减飞轮与审计能力。
16810
17132
  * @module @kenz1117/dsh-engram
16811
17133
  */
@@ -17274,7 +17596,7 @@ function persistPalaceLanguage(ctx, language) {
17274
17596
  }, "dsh-engram: palace language persistence");
17275
17597
  }
17276
17598
  /**
17277
- * 插件体:预热分库与嵌入器,注册 10 个工具、画像注入、自动摄取与衰减调度。
17599
+ * 插件体:预热分库与嵌入器,注册 23 个工具、画像注入、自动摄取与衰减调度。
17278
17600
  * @param ctx - host 上下文。
17279
17601
  * @param config - cordis.yml 传入的可选配置;非法值在加载时 loud 失败。
17280
17602
  */
@@ -17297,7 +17619,7 @@ async function apply(ctx, config = {}) {
17297
17619
  language,
17298
17620
  languageSetting
17299
17621
  };
17300
- if (determination !== void 0 && determination.decision.grounded) {
17622
+ if (determination?.decision.grounded) {
17301
17623
  const decided = determination.decision.language;
17302
17624
  ctx.effect(() => {
17303
17625
  const timer = setTimeout(() => {
@@ -17330,7 +17652,9 @@ async function apply(ctx, config = {}) {
17330
17652
  count: panelDictionary.problems.length,
17331
17653
  sample: describeDictionaryProblems(panelDictionary.problems)
17332
17654
  }));
17333
- const localModel = resolveLocalModel(registry, resolved.language, resolved.embedder.localModel, resolved.embedder.localPooling);
17655
+ const storeLanguage = resolved.embedder.remote === void 0 && (resolved.languageSetting === void 0 || resolved.languageSetting === "auto") ? await readStoreLanguageLabel(join(resolved.dbDir, "user.db")) : void 0;
17656
+ const mountLanguage = storeLanguage !== void 0 && registry.bindings.has(storeLanguage) ? storeLanguage : resolved.language;
17657
+ const localModel = resolveLocalModel(registry, mountLanguage, resolved.embedder.localModel, resolved.embedder.localPooling);
17334
17658
  /** Снимок обслуживания по прочитанным словарям: собирается заново после правки из панели. */
17335
17659
  /**
17336
17660
  * Язык дворца в снимке обслуживания. Здесь всегда null: язык диалога появляется
@@ -17408,7 +17732,7 @@ async function apply(ctx, config = {}) {
17408
17732
  * базы подписало и пересчитало под старый — и человек получил бы ровно то расхождение,
17409
17733
  * от которого условие защищает.
17410
17734
  */
17411
- let basesLanguage = resolved.language;
17735
+ let basesLanguage = mountLanguage;
17412
17736
  /**
17413
17737
  * Предохранитель смены языка: одно окно на процесс, состояние — в памяти.
17414
17738
  *
@@ -17537,11 +17861,29 @@ async function apply(ctx, config = {}) {
17537
17861
  * 面板「项目」scope、工具读写与自动摄取的 project 落点因此同一口径。
17538
17862
  */
17539
17863
  const resolveProjectStore = (cwd) => cwd === void 0 ? openStore("project") : openStoreForProjectCwd(cwd);
17540
- const embedder = createEmbedder(resolved, registry).catch((error) => {
17541
- console.warn(message("core.warnEmbedderUnavailable"), error);
17542
- });
17864
+ const EMBEDDER_AWAIT_GRACE_MS = 15e3;
17865
+ let embedderPending;
17866
+ const embedderAttempt = () => {
17867
+ if (embedderPending === void 0) embedderPending = createEmbedder(resolved, registry, mountLanguage).catch((error) => {
17868
+ console.warn(message("core.warnEmbedderUnavailable"), error);
17869
+ embedderPending = void 0;
17870
+ });
17871
+ return embedderPending;
17872
+ };
17873
+ const embedder = { then(onfulfilled, onrejected) {
17874
+ const attempt = embedderAttempt();
17875
+ return Promise.race([attempt, new Promise((resolve) => {
17876
+ const timer = setTimeout(() => {
17877
+ resolve(void 0);
17878
+ }, EMBEDDER_AWAIT_GRACE_MS);
17879
+ attempt.then(() => {
17880
+ clearTimeout(timer);
17881
+ });
17882
+ })]).then(onfulfilled, onrejected);
17883
+ } };
17884
+ embedderAttempt();
17543
17885
  ctx.effect(() => () => {
17544
- embedder.then((active) => active?.close()).catch(() => {});
17886
+ embedderPending?.then((active) => active?.close()).catch(() => {});
17545
17887
  }, "dsh-engram: close embedder");
17546
17888
  const logIngestRequest = (data) => {
17547
17889
  openStore("user").then((store) => store.audit("ingest-request", "AUX", JSON.stringify(data))).catch(() => {});
@@ -17816,7 +18158,7 @@ async function apply(ctx, config = {}) {
17816
18158
  };
17817
18159
  const dimensions = baseTargetNow().model?.dimensions;
17818
18160
  if (dimensions === void 0) return result;
17819
- const active = await embedder;
18161
+ const active = await embedderAttempt();
17820
18162
  if (active === void 0) {
17821
18163
  result.failed.push({
17822
18164
  file: "*",
@@ -18308,7 +18650,7 @@ async function apply(ctx, config = {}) {
18308
18650
  if (route === void 0) continue;
18309
18651
  const outcome = await reflectObservations({
18310
18652
  store,
18311
- embedder: await embedder,
18653
+ embedder: await embedderAttempt(),
18312
18654
  scope: target.scope,
18313
18655
  call: (params) => streamText(ctx, {
18314
18656
  ...params,
@@ -18332,6 +18674,7 @@ async function apply(ctx, config = {}) {
18332
18674
  }
18333
18675
  })();
18334
18676
  });
18677
+ const OP_LOG_RETENTION_DAYS = 366;
18335
18678
  const runDecay = async () => {
18336
18679
  const targets = [{
18337
18680
  label: "user",
@@ -18344,8 +18687,10 @@ async function apply(ctx, config = {}) {
18344
18687
  open: () => openDb(palace.dbName)
18345
18688
  });
18346
18689
  }
18690
+ const pruneBefore = Date.now() - OP_LOG_RETENTION_DAYS * 864e5;
18347
18691
  for (const target of targets) {
18348
- const archived = await (await target.open()).decay({
18692
+ const store = await target.open();
18693
+ const archived = await store.decay({
18349
18694
  importanceBelow: resolved.decayImportanceBelow,
18350
18695
  olderThanDays: resolved.decayAfterDays
18351
18696
  });
@@ -18353,6 +18698,11 @@ async function apply(ctx, config = {}) {
18353
18698
  label: target.label,
18354
18699
  count: archived
18355
18700
  }));
18701
+ try {
18702
+ await store.pruneOps(pruneBefore, LOG_KEEP_OPS);
18703
+ } catch (error) {
18704
+ console.warn(message("core.warnDecayPeriodicFailed"), error);
18705
+ }
18356
18706
  }
18357
18707
  };
18358
18708
  runDecay().catch((error) => {