@kenz1117/dsh-engram 0.8.16 → 0.9.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
@@ -1,10 +1,10 @@
1
1
  import { createHash, randomUUID } from "node:crypto";
2
2
  import { createWriteStream, existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
3
- import { dirname, join, resolve } from "node:path";
3
+ import { dirname, isAbsolute, join, relative, resolve } from "node:path";
4
4
  import { BlockAssembler, createUserMessage } from "@deepseek-ai/dsh-llm";
5
5
  import { homedir } from "node:os";
6
6
  import z from "@deepseek-ai/schemastery";
7
- import { mkdir, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
7
+ import { copyFile, mkdir, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
8
8
  import { createGzip, gunzip } from "node:zlib";
9
9
  import { promisify } from "node:util";
10
10
  import { AsyncLocalStorage } from "node:async_hooks";
@@ -277,6 +277,8 @@ const zh$4 = {
277
277
  "core.sampleEpisodeTag0": "钟声",
278
278
  "core.sampleEpisodeTag1": "潮气",
279
279
  "core.warnReplayPendingFailed": "[dsh-engram] 待补做摄取重放失败(保留 pending,不影响对话):",
280
+ "ingest.replayKeyFailed": "[dsh-engram] 重放单条 pending 失败(保留,下个会话重试):",
281
+ "ingest.replayAbandoned": "[dsh-engram] 陈旧 pending 放弃重放(会话超 14 天仍失败,出队):",
280
282
  "core.warnIngestPreviousFailed": "[dsh-engram] 本轮自动摄取失败(已跳过,不影响对话):",
281
283
  "core.warnHistoryBackfillFailed": "[dsh-engram] 历史回填失败:",
282
284
  "core.warnDisposeFinalFailed": "[dsh-engram] 会话结束的收尾处理异常(不影响对话):",
@@ -328,6 +330,8 @@ const en$4 = {
328
330
  "core.sampleEpisodeTag0": "bell tone",
329
331
  "core.sampleEpisodeTag1": "damp air",
330
332
  "core.warnReplayPendingFailed": "[dsh-engram] Replaying a pending ingest failed (pending kept, conversation unaffected):",
333
+ "ingest.replayKeyFailed": "[dsh-engram] Replaying one pending key failed (kept for next session):",
334
+ "ingest.replayAbandoned": "[dsh-engram] Stale pending abandoned (session older than 14 days, still failing — dequeued):",
331
335
  "core.warnIngestPreviousFailed": "[dsh-engram] Automatic ingest of this turn failed (skipped, conversation unaffected):",
332
336
  "core.warnHistoryBackfillFailed": "[dsh-engram] History backfill failed:",
333
337
  "core.warnDisposeFinalFailed": "[dsh-engram] Session-end finalisation threw (conversation unaffected):",
@@ -379,6 +383,8 @@ const ru$4 = {
379
383
  "core.sampleEpisodeTag0": "звон колокола",
380
384
  "core.sampleEpisodeTag1": "сырость",
381
385
  "core.warnReplayPendingFailed": "[dsh-engram] Не удалось повторить отложенное извлечение (pending сохранён, диалог не затронут):",
386
+ "ingest.replayKeyFailed": "[dsh-engram] Не удалось повторить один pending (сохранён, повтор в следующей сессии):",
387
+ "ingest.replayAbandoned": "[dsh-engram] Устаревший pending снят с очереди (сессия старше 14 дней, всё ещё падает):",
382
388
  "core.warnIngestPreviousFailed": "[dsh-engram] Автоматическое извлечение этого хода не удалось (пропущено, диалог не затронут):",
383
389
  "core.warnHistoryBackfillFailed": "[dsh-engram] Заполнение истории не удалось:",
384
390
  "core.warnDisposeFinalFailed": "[dsh-engram] Ошибка завершающей обработки при закрытии сессии (диалог не затронут):",
@@ -638,6 +644,7 @@ const zh$1 = {
638
644
  "store.entityNameEmpty": "实体名不能为空(清洗后的提及仍含空名)",
639
645
  "store.observationEvidenceRequired": "createObservation 要求至少 1 条证据记忆",
640
646
  "store.backupSchemaMismatch": "备份 schema 版本 {found} 与当前 {expected} 不兼容(请升级插件或使用旧版恢复)",
647
+ "store.preMigrationSnapshotFailed": "迁移前快照失败(继续升级,但本次迁移无法整文件回退):{path}:{msg}",
641
648
  "store.backupMetaMissingPluginVersion": "备份 _meta.json 缺少 pluginVersion 字段,可能已损坏",
642
649
  "store.tarNameTooLong": "tar 文件名过长({actual} / {max}):{name}",
643
650
  "store.tarFileTooLarge": "tar 文件过大:{name}({bytes} bytes)",
@@ -670,6 +677,7 @@ const en$1 = {
670
677
  "store.entityNameEmpty": "entity name must not be empty (a mention is still nameless after cleaning)",
671
678
  "store.observationEvidenceRequired": "createObservation requires at least 1 evidence memory",
672
679
  "store.backupSchemaMismatch": "backup schema version {found} is incompatible with the current {expected} (upgrade the plugin or restore with the older version)",
680
+ "store.preMigrationSnapshotFailed": "pre-migration snapshot failed (upgrading anyway; this migration cannot be rolled back file-wise): {path}: {msg}",
673
681
  "store.backupMetaMissingPluginVersion": "backup _meta.json is missing the pluginVersion field; it may be corrupted",
674
682
  "store.tarNameTooLong": "tar file name too long ({actual} / {max}): {name}",
675
683
  "store.tarFileTooLarge": "tar file too large: {name} ({bytes} bytes)",
@@ -702,6 +710,7 @@ const ru$1 = {
702
710
  "store.entityNameEmpty": "Имя сущности не может быть пустым (после очистки упоминание осталось без имени)",
703
711
  "store.observationEvidenceRequired": "createObservation требует хотя бы одну память-свидетельство",
704
712
  "store.backupSchemaMismatch": "Версия schema резервной копии {found} несовместима с текущей {expected} (обновите плагин или восстановите старой версией)",
713
+ "store.preMigrationSnapshotFailed": "Не удалось сделать снимок перед миграцией (продолжаем; откат этой миграции файлом невозможен): {path}: {msg}",
705
714
  "store.backupMetaMissingPluginVersion": "В _meta.json резервной копии нет поля pluginVersion — возможно, файл повреждён",
706
715
  "store.tarNameTooLong": "Слишком длинное имя файла в tar ({actual} / {max}): {name}",
707
716
  "store.tarFileTooLarge": "Слишком большой файл в tar: {name} ({bytes} bytes)",
@@ -3058,9 +3067,23 @@ async function ensureSessionSummary(deps) {
3058
3067
  }
3059
3068
  /**
3060
3069
  * 重放待补做的末轮摄取(下次会话首次 pre-step 调用)。已有 done 标记或键损坏的
3061
- * pending 直接出队;事件源不可得的保留到下次。单个键失败抛出,剩余键留待下次。
3070
+ * pending 直接出队;事件源不可得的保留到下次。
3071
+ * 审计 F11:单个键失败不再冲出整个循环(队头阻塞——#1 失败 #2..N 全不试)。
3072
+ * 审计 F12/F11②:LLM 已付费但输出不可解析/为空时保留 pending 下次重试(可能是暂时性
3073
+ * 截断);但会话陈旧(末事件早于 REPLAY_ABANDON_AGE_MS)即放弃——确定性失败不应
3074
+ * 每个会话白付一次辅助调用、永久卡住队列。
3062
3075
  * 每条 pending 按它自己会话的 cwd 组装写入路由(补做时同样逐条判宫殿)。
3063
3076
  */
3077
+ /** pending 重放的放弃窗口:会话末事件早于该时长视为陈旧,失败即出队。 */
3078
+ const REPLAY_ABANDON_AGE_MS = 12096e5;
3079
+ /** 会话末事件早于放弃窗口即为陈旧(事件缺 time 无法判定时视作非陈旧,保守保留)。 */
3080
+ function replayStale(session) {
3081
+ for (let i = session.events.length - 1; i >= 0; i -= 1) {
3082
+ const time = session.events[i]?.time;
3083
+ if (time !== void 0) return Date.now() - time > REPLAY_ABANDON_AGE_MS;
3084
+ }
3085
+ return false;
3086
+ }
3064
3087
  async function replayPendingIngests(deps) {
3065
3088
  const store = await deps.openStore();
3066
3089
  const pendings = await store.listAuditDetails(INGEST_PENDING_OP);
@@ -3077,24 +3100,41 @@ async function replayPendingIngests(deps) {
3077
3100
  kept += 1;
3078
3101
  continue;
3079
3102
  }
3080
- await ingestPreviousTurn({
3081
- events: session.events,
3082
- sessionId: key.sessionId,
3083
- turn: key.turn,
3084
- slice: key.turn,
3085
- openStore: deps.openStore,
3086
- openAuditStore: deps.openStore,
3087
- ...ingestWriteRouting(session.cwd, deps.resolveStore, deps.openStore),
3088
- embedder: deps.embedder,
3089
- mode: deps.mode,
3090
- routeOverride: deps.routeOverride,
3091
- call: deps.call,
3092
- logRequest: deps.logRequest,
3093
- ...deps.judge === void 0 ? {} : { judge: deps.judge },
3094
- signal: deps.signal
3095
- });
3096
- await store.clearAudit(INGEST_PENDING_OP, detail);
3097
- replayed += 1;
3103
+ const stale = replayStale(session);
3104
+ try {
3105
+ const outcome = await ingestPreviousTurn({
3106
+ events: session.events,
3107
+ sessionId: key.sessionId,
3108
+ turn: key.turn,
3109
+ slice: key.turn,
3110
+ openStore: deps.openStore,
3111
+ openAuditStore: deps.openStore,
3112
+ ...ingestWriteRouting(session.cwd, deps.resolveStore, deps.openStore),
3113
+ embedder: deps.embedder,
3114
+ mode: deps.mode,
3115
+ routeOverride: deps.routeOverride,
3116
+ call: deps.call,
3117
+ logRequest: deps.logRequest,
3118
+ ...deps.judge === void 0 ? {} : { judge: deps.judge },
3119
+ signal: deps.signal
3120
+ });
3121
+ if (outcome.skipped === "unparseable-output" || outcome.skipped === "empty-answer") {
3122
+ kept += 1;
3123
+ if (stale) {
3124
+ await store.clearAudit(INGEST_PENDING_OP, detail);
3125
+ console.warn(message("ingest.replayAbandoned"), key.sessionId, key.turn, outcome.skipped);
3126
+ }
3127
+ continue;
3128
+ }
3129
+ await store.clearAudit(INGEST_PENDING_OP, detail);
3130
+ replayed += 1;
3131
+ } catch (error) {
3132
+ kept += 1;
3133
+ if (stale) {
3134
+ await store.clearAudit(INGEST_PENDING_OP, detail);
3135
+ console.warn(message("ingest.replayAbandoned"), key.sessionId, key.turn, error instanceof Error ? error.message : error);
3136
+ } else console.warn(message("ingest.replayKeyFailed"), key.sessionId, key.turn, error);
3137
+ }
3098
3138
  }
3099
3139
  return {
3100
3140
  replayed,
@@ -5318,13 +5358,15 @@ function registerEngramRoutes(ctx, deps) {
5318
5358
  if (req.method === "GET" && route === "refurb") {
5319
5359
  const rawScope = url.searchParams.get("scope");
5320
5360
  const scope = rawScope === "project" ? "project" : rawScope === "shared" ? "shared" : "user";
5361
+ const rawLang = url.searchParams.get("lang");
5362
+ const lang = rawLang === "zh" || rawLang === "en" || rawLang === "ru" ? rawLang : deps.language;
5321
5363
  const filter = await (await storeFor(scope)).list({
5322
5364
  scope,
5323
5365
  status: "active",
5324
5366
  limit: 500,
5325
5367
  offset: 0
5326
5368
  });
5327
- const suggestions = gatherRefurbSuggestions(filter.records, deps.language, DEFAULT_REFURB_OPTIONS);
5369
+ const suggestions = gatherRefurbSuggestions(filter.records, lang, DEFAULT_REFURB_OPTIONS);
5328
5370
  const byId = new Map(filter.records.map((record) => [record.id, record]));
5329
5371
  const previewOf = (id) => {
5330
5372
  const record = byId.get(id);
@@ -5332,7 +5374,7 @@ function registerEngramRoutes(ctx, deps) {
5332
5374
  return {
5333
5375
  id: record.id,
5334
5376
  kind: record.kind,
5335
- content: record.content.length > 280 ? `${record.content.slice(0, 280)}…` : record.content,
5377
+ content: record.content,
5336
5378
  importance: record.importance
5337
5379
  };
5338
5380
  };
@@ -5352,6 +5394,10 @@ function registerEngramRoutes(ctx, deps) {
5352
5394
  });
5353
5395
  return;
5354
5396
  }
5397
+ if (req.method === "GET" && route === "version") {
5398
+ json(res, 200, { pluginVersion: deps.pluginVersion });
5399
+ return;
5400
+ }
5355
5401
  if (req.method === "GET" && route === "models") {
5356
5402
  json(res, 200, await deps.history.models());
5357
5403
  return;
@@ -5471,12 +5517,14 @@ function registerEngramRoutes(ctx, deps) {
5471
5517
  return;
5472
5518
  }
5473
5519
  const normalized = join(deps.dbDir, body.archivePath.replace(/^\/+/, ""));
5474
- if (!normalized.startsWith(deps.dbDir + "/") && normalized !== deps.dbDir) {
5520
+ const rel = relative(deps.dbDir, normalized);
5521
+ if (rel === "" || rel.startsWith("..") || isAbsolute(rel)) {
5475
5522
  json(res, 400, { error: messageIn(deps.language, "misc.archivePathOutsideDbDir") });
5476
5523
  return;
5477
5524
  }
5478
5525
  try {
5479
5526
  const result = await restoreBackup(normalized, deps.dbDir);
5527
+ await deps.closeStores();
5480
5528
  json(res, 200, {
5481
5529
  restored: result.restored,
5482
5530
  meta: result.archiveMeta
@@ -6242,6 +6290,9 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
6242
6290
  });
6243
6291
  const { DatabaseSync } = await import("node:sqlite");
6244
6292
  const db = new DatabaseSync(path);
6293
+ db.exec("PRAGMA journal_mode = WAL;");
6294
+ db.exec("PRAGMA busy_timeout = 5000;");
6295
+ db.exec("PRAGMA synchronous = NORMAL;");
6245
6296
  /** 手工事务:BEGIN/COMMIT/ROLLBACK(兼容 ^22.19 引擎范围)。 */
6246
6297
  const withTransaction = (fn) => {
6247
6298
  db.exec("BEGIN");
@@ -6322,6 +6373,14 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
6322
6373
  path
6323
6374
  }));
6324
6375
  }
6376
+ if (version < SCHEMA_VERSION) try {
6377
+ await copyFile(path, `${path}.pre-migration`);
6378
+ } catch (error) {
6379
+ console.warn(message("store.preMigrationSnapshotFailed", {
6380
+ path,
6381
+ msg: error instanceof Error ? error.message : String(error)
6382
+ }));
6383
+ }
6325
6384
  while (version < SCHEMA_VERSION) {
6326
6385
  const migration = MIGRATIONS[String(version)];
6327
6386
  if (migration === void 0) {
@@ -10539,6 +10598,9 @@ function estimateTokens(text) {
10539
10598
  return Math.ceil(wide * WIDE_TOKENS_PER_CHAR + narrow / 4);
10540
10599
  }
10541
10600
  //#endregion
10601
+ //#region package.json
10602
+ var version = "0.9.0";
10603
+ //#endregion
10542
10604
  //#region src/index.ts
10543
10605
  /**
10544
10606
  * dsh-engram:DeepSeek Harness 跨会话长期记忆插件(host 半)。
@@ -10546,10 +10608,10 @@ function estimateTokens(text) {
10546
10608
  * 自动摄取上一轮对话、蒸馏/衰减飞轮与审计能力。
10547
10609
  * @module @kenz1117/dsh-engram
10548
10610
  */
10611
+ /** 插件版本:单一来源 = package.json(审计 F9:手写常量已漂移到 0.7.6,备份 _meta.json 随之记错版本)。 */
10549
10612
  /** Cordis 插件名(loader 诊断与注入 source 使用)。 */
10550
10613
  const name = "dsh-engram";
10551
- /** 插件版本(与 package.json 同步,写进备份 _meta.json)。 */
10552
- const VERSION = "0.7.6";
10614
+ const VERSION = version;
10553
10615
  /** 必需服务:工具注册表与 LLM 流式端点(摄取/蒸馏的辅助调用)。 */
10554
10616
  const inject = ["tools", "llm"];
10555
10617
  /** 分级递减预算默认值(配置缺省时由 resolveConfig 显式落地,同值于此)。 */
@@ -10934,15 +10996,29 @@ function apply(ctx, config = {}) {
10934
10996
  }));
10935
10997
  return store;
10936
10998
  });
10999
+ created.catch(() => {
11000
+ stores.delete(dbName);
11001
+ });
10937
11002
  stores.set(dbName, created);
10938
11003
  return created;
10939
11004
  };
10940
11005
  const openStore = (scope) => openDb(scope === "user" ? "user.db" : scope === "shared" ? "shared.db" : identity.dbName);
10941
- ctx.effect(() => () => Promise.allSettled([...stores.values()].map(async (pending) => {
10942
- try {
10943
- await (await pending).close();
10944
- } catch {}
10945
- })), "dsh-engram: close stores");
11006
+ /**
11007
+ * 关闭并清空全部分库缓存。两个用途:
11008
+ * - 卸载/重启时释放连接(不关会在 Windows 上锁住 .db 文件,目录删不掉);
11009
+ * - 审计 F16:restore 备份后 rename 换掉了 .db 的 inode,缓存连接仍指向旧文件——
11010
+ * 继续写入会落进 .before-restore-* 且重启后静默丢失。restore 成功后必须调用,
11011
+ * 让下一次访问按恢复后的文件重新打开。
11012
+ */
11013
+ const closeStores = async () => {
11014
+ await Promise.allSettled([...stores.values()].map(async (pending) => {
11015
+ try {
11016
+ await (await pending).close();
11017
+ } catch {}
11018
+ }));
11019
+ stores.clear();
11020
+ };
11021
+ ctx.effect(() => () => closeStores(), "dsh-engram: close stores");
10946
11022
  /** cwd → 分库文件名(历史回填会对每个历史 cwd 解析一次,避免重复读 git 元数据)。 */
10947
11023
  const cwdDbNames = /* @__PURE__ */ new Map();
10948
11024
  /**
@@ -11202,6 +11278,7 @@ function apply(ctx, config = {}) {
11202
11278
  ctx.inject(["webServer"], (webCtx) => {
11203
11279
  registerEngramRoutes(webCtx, {
11204
11280
  openStore,
11281
+ closeStores,
11205
11282
  projects: projectsDeps,
11206
11283
  exportDir: `${resolved.dbDir}/exports`,
11207
11284
  mirrorDir: `${resolved.dbDir}/palaces`,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kenz1117/dsh-engram",
3
3
  "description": "Cross-session long-term memory for DeepSeek Harness (memory palace · AGI Architecture Exploration on dsh-market): dual-scope SQLite memory graph, hybrid retrieval, provenance audit, and a knowledge flywheel.",
4
- "version": "0.8.16",
4
+ "version": "0.9.1",
5
5
  "icon": "icon.svg",
6
6
  "logo": "icon-256.svg",
7
7
  "publishConfig": {
@@ -109,7 +109,7 @@
109
109
  "@types/react": "^18.3.12",
110
110
  "@types/react-dom": "^18.3.1",
111
111
  "lightningcss": "^1.32.0",
112
- "tsdown": "^0.22.14",
112
+ "tsdown": "^0.23.1",
113
113
  "typescript": "^5.9.3",
114
114
  "vitest": "^4.1.11"
115
115
  },