@kenz1117/dsh-engram 0.11.2 → 0.11.3

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
@@ -5838,7 +5838,11 @@ function finishError(finish) {
5838
5838
  error.code = finish.failure.code;
5839
5839
  return error;
5840
5840
  }
5841
- case "max-tokens": return new Error(message("llm.finishMaxTokens"));
5841
+ case "max-tokens": {
5842
+ const error = new Error(message("llm.finishMaxTokens"));
5843
+ error.code = "LLM_MAX_TOKENS";
5844
+ return error;
5845
+ }
5842
5846
  case "tool-calls": return new Error(message("llm.finishUnexpectedToolCalls"));
5843
5847
  default: return new Error(message("llm.finishUnsupported", { kind: String(finish.kind) }));
5844
5848
  }
@@ -6341,6 +6345,51 @@ const MODE_LIMITS = {
6341
6345
  /** 摄取输出 token 上限。completion 预算含 reasoning_tokens:推理模型可能把预算耗在思考上、
6342
6346
  答案为空(实测 600/2000 都会出现,4000 未复现),上限给足余量而非按答案长度估。 */
6343
6347
  const INGEST_MAX_TOKENS = 4e3;
6348
+ /** 提取切块的最小文本粒度(字符):切到低于此长度仍超输出预算的块直接放弃(贡献 0 条候选)。
6349
+ 正常文本到这个量级输出预算绰绰有余;触发即说明模型输出退化(复读死循环),再切只是空烧。 */
6350
+ const EXTRACT_MIN_CHUNK = 80;
6351
+ /** max-tokens 终止错误的判别(llm/client.ts 的 finishError 打上 LLM_MAX_TOKENS code)。 */
6352
+ function isMaxTokensError(error) {
6353
+ return error?.code === "LLM_MAX_TOKENS";
6354
+ }
6355
+ /** 单块提取的解析失败(含空答案重试后仍失败)——携带原始输出,交上层按 reason 分类报告。 */
6356
+ var ExtractFailure = class extends Error {
6357
+ raw;
6358
+ constructor(raw) {
6359
+ super("extract-parse-failed");
6360
+ this.raw = raw;
6361
+ this.name = "ExtractFailure";
6362
+ }
6363
+ };
6364
+ /**
6365
+ * 提取候选,输出超预算(max-tokens)时对半切块重试(#42 附带报告:长转写/数百行日志的
6366
+ * 轮次提取输出必然超 4000 输出预算,且同样输入重跑永远失败——回填轮次被永久卡死)。
6367
+ * 切块让这类轮次自愈:多段文本对半分;单段文本按换行边界对半切;切到最小粒度仍超限的
6368
+ * 块放弃(贡献 0 条候选),其余块照常提取,整轮照常收口为 done 而非永久 pending。
6369
+ * 语义边界:只有 max-tokens 触发切块;解析失败(ExtractFailure)与瞬时错误原样上抛,
6370
+ * 轮次保持「可重试」状态,与旧行为一致。
6371
+ */
6372
+ async function extractChunked(callOnce, texts) {
6373
+ try {
6374
+ return await callOnce(texts);
6375
+ } catch (error) {
6376
+ if (!isMaxTokensError(error)) throw error;
6377
+ if (texts.length > 1) {
6378
+ const mid = Math.ceil(texts.length / 2);
6379
+ const first = await extractChunked(callOnce, texts.slice(0, mid));
6380
+ const second = await extractChunked(callOnce, texts.slice(mid));
6381
+ return [...first, ...second];
6382
+ }
6383
+ const text = texts[0] ?? "";
6384
+ if (text.length < EXTRACT_MIN_CHUNK) return [];
6385
+ const half = Math.floor(text.length / 2);
6386
+ const positions = [text.lastIndexOf("\n", half), text.indexOf("\n", half)].filter((position) => position !== -1);
6387
+ const cut = positions.length === 0 ? half : positions.reduce((best, position) => Math.abs(position - half) < Math.abs(best - half) ? position : best);
6388
+ const first = await extractChunked(callOnce, [text.slice(0, cut)]);
6389
+ const second = await extractChunked(callOnce, [text.slice(cut + 1)]);
6390
+ return [...first, ...second];
6391
+ }
6392
+ }
6344
6393
  /** op_log 幂等键 op:该 (sessionId, turn) 已完成摄取。 */
6345
6394
  const INGEST_DONE_OP = "ingest-done";
6346
6395
  /** op_log pending 键 op:disposed 末轮摄取失败/超时,待下次会话重放补做。 */
@@ -6706,43 +6755,54 @@ async function ingestPreviousTurn(deps) {
6706
6755
  };
6707
6756
  }
6708
6757
  const recallNote = hasRecallToolCalls(scoped) ? message("llm.ingestRecallNote") : "";
6709
- const userText = `${message("llm.ingestUserPrompt")}\n${JSON.stringify(cleaned)}${recallNote === "" ? "" : `\n${recallNote}`}`;
6710
- deps.logRequest({
6711
- route,
6712
- round,
6713
- userText,
6714
- maxTokens: INGEST_MAX_TOKENS,
6715
- mode: deps.mode
6716
- });
6717
- let raw = await deps.call({
6718
- route,
6719
- system: ingestSystemPrompt(),
6720
- userText,
6721
- maxTokens: INGEST_MAX_TOKENS,
6722
- purpose: "engram-ingest",
6723
- signal: deps.signal
6724
- });
6725
- let parsed = parseJsonArray(raw);
6726
- if (parsed === void 0 && raw.trim() === "") {
6727
- raw = await deps.call({
6758
+ const callExtract = async (texts) => {
6759
+ const userText = `${message("llm.ingestUserPrompt")}\n${JSON.stringify(texts)}${recallNote === "" ? "" : `\n${recallNote}`}`;
6760
+ deps.logRequest({
6761
+ route,
6762
+ round,
6763
+ userText,
6764
+ maxTokens: INGEST_MAX_TOKENS,
6765
+ mode: deps.mode
6766
+ });
6767
+ const args = {
6728
6768
  route,
6729
6769
  system: ingestSystemPrompt(),
6730
6770
  userText,
6731
6771
  maxTokens: INGEST_MAX_TOKENS,
6732
6772
  purpose: "engram-ingest",
6733
6773
  signal: deps.signal
6734
- });
6735
- parsed = parseJsonArray(raw);
6736
- }
6737
- if (parsed === void 0) return {
6738
- scannedEvents: slice.length,
6739
- candidates: 0,
6740
- written: 0,
6741
- merged: 0,
6742
- deferred: 0,
6743
- dropped: 0,
6744
- skipped: raw.trim() === "" ? "empty-answer" : "unparseable-output"
6774
+ };
6775
+ let raw = await deps.call(args);
6776
+ let parsed = parseJsonArray(raw);
6777
+ if (parsed === void 0 && raw.trim() === "") {
6778
+ deps.logRequest({
6779
+ route,
6780
+ round,
6781
+ userText,
6782
+ maxTokens: INGEST_MAX_TOKENS,
6783
+ mode: deps.mode
6784
+ });
6785
+ raw = await deps.call(args);
6786
+ parsed = parseJsonArray(raw);
6787
+ }
6788
+ if (parsed === void 0) throw new ExtractFailure(raw);
6789
+ return parsed;
6745
6790
  };
6791
+ let parsed;
6792
+ try {
6793
+ parsed = await extractChunked(callExtract, cleaned);
6794
+ } catch (error) {
6795
+ if (!(error instanceof ExtractFailure)) throw error;
6796
+ return {
6797
+ scannedEvents: slice.length,
6798
+ candidates: 0,
6799
+ written: 0,
6800
+ merged: 0,
6801
+ deferred: 0,
6802
+ dropped: 0,
6803
+ skipped: error.raw.trim() === "" ? "empty-answer" : "unparseable-output"
6804
+ };
6805
+ }
6746
6806
  const embedder = await deps.embedder;
6747
6807
  const judge = deps.resolveJudge?.();
6748
6808
  const writtenContents = [];
@@ -7432,17 +7492,18 @@ async function runConsolidation(store, embedder, opts = {}) {
7432
7492
  const key = record.content.slice(0, 60);
7433
7493
  const existing = seen.get(key);
7434
7494
  if (existing === void 0) seen.set(key, record);
7435
- else if (record.importance > existing.importance) {
7436
- await store.supersedeMany({
7495
+ else if (record.importance > existing.importance) try {
7496
+ const survivor = await store.supersedeMany({
7437
7497
  scope,
7438
7498
  kind: record.kind,
7439
7499
  content: record.content,
7440
7500
  importance: record.importance,
7441
7501
  confidence: record.confidence
7442
- }, [existing.id]).catch(() => {
7443
- skipped += 1;
7444
- });
7502
+ }, [existing.id, record.id]);
7445
7503
  merged += 1;
7504
+ seen.set(key, survivor);
7505
+ } catch {
7506
+ skipped += 1;
7446
7507
  seen.set(key, record);
7447
7508
  }
7448
7509
  }
@@ -7481,21 +7542,23 @@ async function runConsolidation(store, embedder, opts = {}) {
7481
7542
  if (bVec === void 0) continue;
7482
7543
  if (cosine(vectors.get(a.id), bVec) >= options.mergeThreshold) dupes.push(b);
7483
7544
  }
7484
- if (dupes.length === 0) continue;
7485
- const ids = dupes.map((record) => record.id);
7486
- try {
7487
- await store.supersedeMany({
7488
- scope,
7489
- kind: a.kind,
7490
- content: a.content,
7491
- importance: a.importance,
7492
- confidence: a.confidence
7493
- }, ids);
7494
- for (const id of ids) visited.add(id);
7495
- visited.add(a.id);
7496
- merged += 1;
7497
- } catch {
7498
- skipped += 1;
7545
+ if (dupes.length > 0) {
7546
+ const ids = [...dupes.map((record) => record.id), a.id];
7547
+ const embedding = vectors.get(a.id);
7548
+ try {
7549
+ await store.supersedeMany({
7550
+ scope,
7551
+ kind: a.kind,
7552
+ content: a.content,
7553
+ importance: Math.max(a.importance, ...dupes.map((record) => record.importance)),
7554
+ confidence: a.confidence,
7555
+ ...embedding === void 0 ? {} : { embedding }
7556
+ }, ids);
7557
+ for (const id of ids) visited.add(id);
7558
+ merged += 1;
7559
+ } catch {
7560
+ skipped += 1;
7561
+ }
7499
7562
  }
7500
7563
  }
7501
7564
  }
@@ -17152,7 +17215,7 @@ function estimateTokens(text) {
17152
17215
  }
17153
17216
  //#endregion
17154
17217
  //#region package.json
17155
- var version = "0.11.1";
17218
+ var version = "0.11.3";
17156
17219
  //#endregion
17157
17220
  //#region src/index.ts
17158
17221
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kenz1117/dsh-engram",
3
3
  "description": "Local-first cross-session long-term memory for DeepSeek Harness: dual-scope SQLite stores, hybrid FTS5 + local-vector retrieval with RRF, an evidence gate, provenance audit and SM-2 spaced repetition. 23 narrow tools, zero external services, zero API keys.",
4
- "version": "0.11.2",
4
+ "version": "0.11.3",
5
5
  "icon": "icon.svg",
6
6
  "logo": "icon-256.svg",
7
7
  "publishConfig": {