@jjh32456/longlive 1.8.12 → 2.1.0

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.
Files changed (92) hide show
  1. package/CHANGELOG.md +258 -202
  2. package/QUICKSTART.md +10 -11
  3. package/README.en.md +13 -4
  4. package/README.md +61 -18
  5. package/SPEC.md +3 -3
  6. package/build-info.json +259 -129
  7. package/cordis.patch.yml +32 -1
  8. package/docs/ANSWER-GUARD.md +1 -1
  9. package/docs/ARCHITECTURE.md +3 -3
  10. package/docs/AUTO-RECALL-WIRING.md +5 -3
  11. package/docs/AUTO-RECALL.md +10 -4
  12. package/docs/CONFIG.md +46 -11
  13. package/docs/ERRORS.md +1 -1
  14. package/docs/FAQ.md +9 -9
  15. package/docs/FEATURES.md +19 -16
  16. package/docs/OPERATIONS.md +3 -3
  17. package/docs/PANEL-INTEGRATION.md +1 -1
  18. package/docs/PRIVACY.md +6 -6
  19. package/docs/QUICKSTART.md +5 -5
  20. package/docs/README.en.md +5 -3
  21. package/docs/TOOLS.md +1 -1
  22. package/docs/TROUBLESHOOTING.md +4 -4
  23. package/package.json +6 -3
  24. package/runbook.md +3 -3
  25. package/scripts/migrate-200.mjs +8 -0
  26. package/scripts/migrate-lifecycle.mjs +2 -2
  27. package/src/answer/claim-guard.mjs +234 -0
  28. package/src/backup.js +165 -1
  29. package/src/config.js +18 -7
  30. package/src/features.js +132 -5
  31. package/src/handoff/index.js +133 -0
  32. package/src/handoff/model.js +48 -0
  33. package/src/identity/handover.js +203 -15
  34. package/src/identity/inject-position.js +3 -1
  35. package/src/index.js +2 -2
  36. package/src/memory/a1-migration.js +72 -6
  37. package/src/memory/archive-cold-state.mjs +794 -0
  38. package/src/memory/archive-staging.mjs +305 -0
  39. package/src/memory/archive.mjs +415 -0
  40. package/src/memory/capture-host.js +71 -2
  41. package/src/memory/capture.js +45 -6
  42. package/src/memory/catchup.mjs +534 -0
  43. package/src/memory/conclusion-distill.js +51 -2
  44. package/src/memory/conclusion-drafts.js +7 -3
  45. package/src/memory/conflicts.js +915 -2
  46. package/src/memory/consolidate.js +20 -1
  47. package/src/memory/forget-v2.js +272 -2
  48. package/src/memory/forget.js +20 -1
  49. package/src/memory/frames-activation.js +3 -1
  50. package/src/memory/fts-query.js +2 -2
  51. package/src/memory/index.js +80 -7
  52. package/src/memory/land.mjs +743 -0
  53. package/src/memory/lifecycle.js +128 -0
  54. package/src/memory/maintenance.js +386 -22
  55. package/src/memory/memory-health.mjs +607 -0
  56. package/src/memory/memory-reader.mjs +1003 -0
  57. package/src/memory/migrate-200.js +1681 -0
  58. package/src/memory/migrate-lifecycle.js +3 -3
  59. package/src/memory/procedure-candidate.mjs +377 -0
  60. package/src/memory/procedure.js +20 -1
  61. package/src/memory/prospective-memory.mjs +362 -0
  62. package/src/memory/recall-feedback.mjs +406 -0
  63. package/src/memory/recall-v2.js +13 -1
  64. package/src/memory/recall.js +102 -1
  65. package/src/memory/reinforce.js +138 -2
  66. package/src/memory/retention.js +34 -0
  67. package/src/memory/turn-budget.mjs +290 -0
  68. package/src/memory/wake-bridge.mjs +2999 -0
  69. package/src/memory/working-frame.js +271 -8
  70. package/src/metrics/classify.mjs +3 -1
  71. package/src/recall/archive-config.mjs +130 -0
  72. package/src/recall/archive-fallback.mjs +174 -19
  73. package/src/recall/archive-search.mjs +3 -0
  74. package/src/recall/archive-state.mjs +588 -0
  75. package/src/recall/brain.mjs +501 -0
  76. package/src/recall/continuity-log.mjs +435 -0
  77. package/src/recall/continuity-volatile.mjs +1001 -0
  78. package/src/recall/current-line.mjs +110 -0
  79. package/src/recall/edges.js +21 -2
  80. package/src/recall/explain.mjs +10 -0
  81. package/src/recall/gate-log.mjs +280 -0
  82. package/src/recall/gate-metrics.mjs +191 -0
  83. package/src/recall/index.js +663 -29
  84. package/src/recall/inject-order.mjs +3 -1
  85. package/src/recall/link-fastpath.mjs +662 -0
  86. package/src/recall/recall-core.mjs +74 -2
  87. package/src/recall/turn-budget-inject.mjs +154 -0
  88. package/src/retention/cold-gate.mjs +146 -0
  89. package/src/retention/config.js +58 -1
  90. package/src/retention/index.js +193 -35
  91. package/src/router/design-headings.json +10 -10
  92. package/src/tools.js +122 -6
@@ -1,22 +1,43 @@
1
1
  /**
2
- * 1.8.8 M2 归档兜底:常规召回 + 链路命中合计为 0 时,去历史会话归档里找线索。
2
+ * 1.8.8 M2 归档兜底 / 2.0.0 W3b(M32)冷档 fail-closed 门。
3
3
  *
4
- * 接口约定(与 archive-search.mjs 的作者对齐,**不得抛**):
5
- * `export async function searchArchive({ memoryRoot, queryText, limit = 3 })`
6
- * → `{ items: [{ archivePath, sessionId, turn, seq, ts, excerpt, matchedTerms }], stats }`
4
+ * 接口约定:
5
+ * `searchArchive({ memoryRoot, queryText, limit = 3 })`
6
+ * → `{ items: [{ archivePath, sessionId, turn, seq, ts, excerpt, matchedTerms, sha256, mark }], stats }`
7
7
  *
8
8
  * 本模块职责:
9
- * - 动态加载 archive-search.mjs(模块不存在/加载失败 → `archiveUnavailable`,静默回落);
10
- * - 调用 searchArchive 并吞掉一切异常(fail-open:最多这一轮不注入归档);
9
+ * - 动态加载 archive-search.mjs(模块不存在/加载失败 → 归档离线,按 fail-closed 抛);
10
+ * - 按 archive-state/v1 判定冷档是否可用(见下方五状态→五码映射);
11
11
  * - 把命中渲染成【自动想起·归档】块:≤3 条、≤800 tokens、每条带 session/turn/归档路径,
12
- * 逐条过隐私扫描(命中即整条丢弃;扫描器抛错也按命中处理)。
12
+ * 逐条过隐私扫描(命中即整条丢弃;扫描器抛错也按命中处理);摘要 ≤120 字(W12)。
13
13
  *
14
- * 注意:动态 import 用 `new URL('./archive-search.mjs', import.meta.url)` 而不是字符串字面量,
15
- * 原因有两个:① 该模块由另一条线并行开发,打包时可能尚不存在,字面量 import 会被
16
- * packaging 测试判为「包内相对 import 解析不到」;② 运行时缺失必须能 fail-open。
14
+ * 2.0.0 判定序(冻结计划 v3.6.4 §4⑧ / §14 D19):
15
+ * ① 读 state(缺省 <memoryRoot>/.longlive/archive-state.json):JSON/schema/status 非法 →
16
+ * offline 归并(旧码 STATE_INVALID 只作 internal reason);
17
+ * ② configured = 显式 archiveRoot 非空 ∨ state.archiveRoot 非空;未配置 → 旧语义静默空(W10);
18
+ * ③ state.status=offline → 抛 E_HG_ARCHIVE_OFFLINE(W5);
19
+ * ④ status=wrong-volume / readonly / low-space → 只降级不抛(v1 not_run;Node 无卷 GUID API);
20
+ * ⑤ status=ok → 读 memoryRoot/.longlive/archive-digest 的 manifest,字节级复算 manifestSha,
21
+ * 校验 schema=archive-digest/v1 与 index size+sha:manifest 缺 → E_HG_ARCHIVE_OFFLINE;
22
+ * 坏/schema 不符/index 不符/state.manifestSha 过期 → E_HG_ARCHIVE_MIGRATE_MISMATCH(W6);
23
+ * ⑥ searcher 模块缺失 → E_HG_ARCHIVE_OFFLINE(reason=searcher-unavailable);
24
+ * ⑦ stats.indexReady !== true → E_HG_ARCHIVE_MIGRATE_MISMATCH;
25
+ * ⑧ 可用 → { ok:true, available:true, items ≤3, stats:{...stats, manifestSha}, manifestSha }。
26
+ * failOpen:true(或 failClosed:false)只用于诊断:不抛,返回 {ok:false, diagnostic:true, code}(W11)。
17
27
  */
18
28
 
29
+ import { createHash } from 'node:crypto';
30
+ import { readFileSync } from 'node:fs';
31
+ import { join } from 'node:path';
32
+
19
33
  import { defaultPrivacyScan, estimateTokens, sanitizeDataText } from './recall-core.mjs';
34
+ import {
35
+ ARCHIVE_STATE_CODES,
36
+ archiveStatePathFor,
37
+ evaluateArchiveGate,
38
+ readArchiveManifest,
39
+ readArchiveState,
40
+ } from './archive-state.mjs';
20
41
 
21
42
  export const ARCHIVE_SOURCE = 'archive';
22
43
  export const ARCHIVE_TEXT_MARK = '【自动想起·归档】';
@@ -27,6 +48,14 @@ export const ARCHIVE_DEFAULTS = Object.freeze({
27
48
  maxExcerptTokens: 240,
28
49
  limit: 3,
29
50
  });
51
+ /** W12:注入摘要上限 120 字;archive-search 的 EXCERPT_MAX_CHARS=400 保留作对照。 */
52
+ export const ARCHIVE_SUMMARY_MAX_CHARS = 120;
53
+
54
+ const CODE_OFFLINE = ARCHIVE_STATE_CODES[0];
55
+ const CODE_READONLY = ARCHIVE_STATE_CODES[1];
56
+ const CODE_LOW_SPACE = ARCHIVE_STATE_CODES[2];
57
+ const CODE_WRONG_VOLUME = ARCHIVE_STATE_CODES[3];
58
+ const CODE_MIGRATE = ARCHIVE_STATE_CODES[4];
30
59
 
31
60
  // 近似重复(同一片段被轻微编辑过)的大改阈值;真正的重复靠「归一化后互为子串」判。
32
61
  // 0.6 会把只共享模板前缀的不同片段误判成重复(实测:'第四条归档片段' 对
@@ -151,7 +180,10 @@ export function renderArchiveBlock(rawItems, {
151
180
  if (items.length >= capItems) break;
152
181
  const item = normalizeArchiveItem(raw);
153
182
  if (!item) { dropped.push({ reason: 'invalid' }); continue; }
154
- const excerpt = sanitizeDataText(item.excerpt, maxExcerptTokens);
183
+ // W12:注入摘要压到 ≤120 字(sanitizeDataText 的 token 上限只是第一道闸)。
184
+ const excerpt = Array.from(sanitizeDataText(item.excerpt, maxExcerptTokens))
185
+ .slice(0, ARCHIVE_SUMMARY_MAX_CHARS)
186
+ .join('');
155
187
  if (excerpt === ''
156
188
  || privacyBlocked(privacyScan, excerpt)
157
189
  || privacyBlocked(privacyScan, item.archivePath)
@@ -199,26 +231,149 @@ export function renderArchiveBlock(rawItems, {
199
231
  }
200
232
 
201
233
  /**
202
- * 调 searchArchive(动态加载 + 全 try/catch,绝不抛)。
203
- * @returns {Promise<{available:boolean, ok:boolean, items:object[], stats:object|null, error:string|null, detail?:string}>}
234
+ * 冷档兜底门:state → 五状态/五码判定 → digest 校验 → 调用 searchArchive。
235
+ *
236
+ * fail-closed(默认):不可用即抛 `error.code`(A6 五码族,v1 实际只抛
237
+ * E_HG_ARCHIVE_OFFLINE / E_HG_ARCHIVE_MIGRATE_MISMATCH 两条);
238
+ * 诊断(failOpen:true 或 failClosed:false):不抛,返回 `{ok:false, diagnostic:true, code}`。
239
+ * 未配置(无显式 archiveRoot 且 state 未写)→ 旧语义静默空,不抛(W10)。
240
+ *
241
+ * @returns {Promise<{ok:boolean, available:boolean, configured?:boolean, items:object[], stats:object|null, manifestSha:string|null, code?:string|null, diagnostic?:boolean, failOpen?:boolean, reason?:string}>}
204
242
  */
205
243
  export async function searchArchiveFallback({
206
244
  memoryRoot,
207
245
  queryText,
208
246
  limit = ARCHIVE_DEFAULTS.limit,
209
247
  loadModule,
248
+ archiveRoot = '',
249
+ archiveStatePath = '',
250
+ failOpen = false,
251
+ failClosed = true,
210
252
  } = {}) {
253
+ const root = typeof memoryRoot === 'string' ? memoryRoot.trim() : '';
254
+ const declaredRoot = typeof archiveRoot === 'string' ? archiveRoot.trim() : '';
255
+ const explicitStatePath = typeof archiveStatePath === 'string' ? archiveStatePath.trim() : '';
256
+ const statePath = explicitStatePath !== '' ? explicitStatePath : (root !== '' ? archiveStatePathFor(root) : null);
257
+ const diagnostic = failOpen === true || failClosed === false;
258
+
259
+ const fail = (code, reason, extra = {}) => {
260
+ const stats = extra.stats ?? null;
261
+ if (diagnostic) {
262
+ return {
263
+ ok: false,
264
+ available: false,
265
+ configured: extra.configured !== false,
266
+ code,
267
+ reason,
268
+ diagnostic: true,
269
+ failOpen: true,
270
+ items: [],
271
+ stats,
272
+ manifestSha: extra.manifestSha ?? null,
273
+ error: code,
274
+ detail: extra.detail ?? null,
275
+ };
276
+ }
277
+ const error = new Error(`${code}: ${reason}`);
278
+ error.code = code;
279
+ error.reason = reason;
280
+ error.manifestSha = extra.manifestSha ?? null;
281
+ error.detail = extra.detail ?? null;
282
+ throw error;
283
+ };
284
+ const degrade = (code, reason, extra = {}) => ({
285
+ ok: false,
286
+ available: false,
287
+ configured: true,
288
+ code,
289
+ reason,
290
+ degraded: true,
291
+ diagnostic: false,
292
+ failOpen: diagnostic,
293
+ items: [],
294
+ stats: null,
295
+ manifestSha: extra.manifestSha ?? null,
296
+ error: code,
297
+ detail: extra.detail ?? null,
298
+ });
299
+
300
+ const stateRead = readArchiveState(statePath);
301
+ const state = stateRead.ok === true ? stateRead.state : null;
302
+ if (stateRead.ok !== true) {
303
+ // 坏 state(JSON/schema/status)→ offline 归并;旧码 STATE_INVALID 只留在 reason 里。
304
+ return fail(CODE_OFFLINE, 'state-invalid', { detail: stateRead.reason ?? null, configured: declaredRoot !== '' });
305
+ }
306
+ const gate = evaluateArchiveGate(state, { archiveRoot: declaredRoot });
307
+ const configured = gate.configured === true || declaredRoot !== '';
308
+ if (configured !== true) {
309
+ // W10:未配置 → 旧语义静默空(不抛、不注入、不报码)。
310
+ return { ok: true, available: false, configured: false, items: [], stats: null, manifestSha: null, code: null, error: null };
311
+ }
312
+ if (gate.ok !== true) {
313
+ return fail(CODE_OFFLINE, 'state-invalid', { detail: gate.detail ?? null });
314
+ }
315
+ if (gate.code === CODE_OFFLINE) {
316
+ return fail(CODE_OFFLINE, 'state-offline', { detail: state?.code ?? null });
317
+ }
318
+ if (gate.code === CODE_WRONG_VOLUME) return degrade(CODE_WRONG_VOLUME, 'state-wrong-volume');
319
+ if (gate.code === CODE_READONLY) return degrade(CODE_READONLY, 'state-readonly');
320
+ if (gate.code === CODE_LOW_SPACE) return degrade(CODE_LOW_SPACE, 'state-low-space');
321
+
322
+ // state=ok:memoryRoot 上的 digest 才是检索锚;显式 archiveRoot 只决定「卷在哪」。
323
+ const searchRoot = root !== '' ? root : (declaredRoot !== '' ? declaredRoot : String(state?.archiveRoot ?? ''));
324
+ const digest = readArchiveManifest(searchRoot, { archiveRoot: declaredRoot !== '' ? declaredRoot : (state?.archiveRoot ?? null) });
325
+ if (digest.ok !== true) {
326
+ const reason = String(digest.reason ?? 'digest-unreadable');
327
+ const missing = reason === 'manifest-missing' || reason === 'manifest-unreadable' || reason === 'index-missing';
328
+ return fail(missing ? CODE_OFFLINE : CODE_MIGRATE, missing ? 'digest-missing' : reason, {
329
+ manifestSha: digest.manifestSha ?? null,
330
+ });
331
+ }
332
+ // W8/W9:manifestSha 必须由 manifest 文件字节独立复算(与 archive-search 的读锚同口径)。
333
+ let manifestSha = digest.manifestSha ?? null;
334
+ try {
335
+ const rawManifest = readFileSync(digest.path);
336
+ const recomputed = createHash('sha256').update(rawManifest).digest('hex');
337
+ if (manifestSha !== null && recomputed !== manifestSha) {
338
+ return fail(CODE_MIGRATE, 'manifest-sha-unstable', { manifestSha: recomputed });
339
+ }
340
+ manifestSha = recomputed;
341
+ } catch (error) {
342
+ return fail(CODE_OFFLINE, 'manifest-unreadable', { detail: String(error?.message ?? error) });
343
+ }
344
+ if (digest.indexReady !== true) {
345
+ return fail(CODE_MIGRATE, String(digest.reason ?? 'index-not-ready'), { manifestSha });
346
+ }
347
+ if (state && typeof state.manifestSha === 'string' && state.manifestSha !== manifestSha) {
348
+ // 归档 index 在 state 写入之后被重建/换卷 → 迁移不一致,不能静默按旧索引召回。
349
+ return fail(CODE_MIGRATE, 'state-manifest-mismatch', { manifestSha });
350
+ }
351
+
211
352
  const loaded = await loadArchiveSearcher({ loadModule });
212
- if (!loaded.ok) {
213
- return { available: false, ok: false, items: [], stats: null, error: loaded.code ?? 'E_ARCHIVE_UNAVAILABLE', detail: loaded.detail ?? null };
353
+ if (loaded.ok !== true) {
354
+ return fail(CODE_OFFLINE, 'searcher-unavailable', { detail: loaded.code ?? null, manifestSha });
214
355
  }
356
+ let result;
215
357
  try {
216
- const result = await loaded.searchArchive({ memoryRoot, queryText, limit });
217
- const items = Array.isArray(result?.items) ? result.items.map(normalizeArchiveItem).filter(Boolean) : [];
218
- return { available: true, ok: true, items, stats: result?.stats && typeof result.stats === 'object' ? result.stats : null, error: null };
358
+ result = await loaded.searchArchive({ memoryRoot: searchRoot, queryText, limit });
219
359
  } catch (error) {
220
- return { available: true, ok: false, items: [], stats: null, error: 'E_ARCHIVE_SEARCH', detail: String(error?.message ?? error) };
360
+ return fail(CODE_OFFLINE, 'searcher-error', { detail: String(error?.message ?? error), manifestSha });
221
361
  }
362
+ const stats = result?.stats && typeof result.stats === 'object' ? { ...result.stats, manifestSha } : null;
363
+ if (stats === null || stats.indexReady !== true) {
364
+ return fail(CODE_MIGRATE, String(stats?.reason ?? 'index-not-ready'), { stats, manifestSha });
365
+ }
366
+ const items = Array.isArray(result?.items) ? result.items.map(normalizeArchiveItem).filter(Boolean) : [];
367
+ return {
368
+ ok: true,
369
+ available: true,
370
+ configured: true,
371
+ items,
372
+ stats,
373
+ manifestSha,
374
+ code: null,
375
+ error: null,
376
+ };
222
377
  }
223
378
 
224
379
  /** 归档条目 → recall-log 的 injected[] 形状(path 用归档文件路径,来源 source:'archive')。 */
@@ -410,6 +410,9 @@ export async function searchArchive(options = {}) {
410
410
  excerpt: buildExcerpt(text, matchedTerms, excerptPhrase),
411
411
  matchedTerms,
412
412
  sha256: typeof entry.h === 'string' ? entry.h : '',
413
+ // 2.0.0 W3b(M31):命中来源标记——调用方据此区分「冷档归档」与本地卡
414
+ // (D19-E4 断言 items 里能读到「已归档」)。additive 字段,旧消费者忽略即可。
415
+ mark: '已归档',
413
416
  };
414
417
  });
415
418
  return finish(items, {