@jjh32456/longlive 1.8.12 → 2.0.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 (83) hide show
  1. package/CHANGELOG.md +232 -202
  2. package/QUICKSTART.md +10 -11
  3. package/README.en.md +13 -4
  4. package/README.md +42 -11
  5. package/SPEC.md +3 -3
  6. package/build-info.json +231 -121
  7. package/cordis.patch.yml +32 -1
  8. package/docs/ARCHITECTURE.md +1 -1
  9. package/docs/AUTO-RECALL.md +1 -1
  10. package/docs/CONFIG.md +41 -9
  11. package/docs/FAQ.md +7 -7
  12. package/docs/FEATURES.md +6 -6
  13. package/docs/OPERATIONS.md +1 -1
  14. package/docs/PANEL-INTEGRATION.md +1 -1
  15. package/docs/PRIVACY.md +5 -5
  16. package/docs/QUICKSTART.md +3 -3
  17. package/docs/README.en.md +4 -2
  18. package/docs/TROUBLESHOOTING.md +4 -4
  19. package/package.json +6 -3
  20. package/runbook.md +3 -3
  21. package/scripts/migrate-200.mjs +8 -0
  22. package/scripts/migrate-lifecycle.mjs +2 -2
  23. package/src/answer/claim-guard.mjs +234 -0
  24. package/src/backup.js +165 -1
  25. package/src/config.js +18 -7
  26. package/src/features.js +128 -5
  27. package/src/handoff/index.js +133 -0
  28. package/src/handoff/model.js +48 -0
  29. package/src/identity/handover.js +203 -15
  30. package/src/identity/inject-position.js +3 -1
  31. package/src/memory/a1-migration.js +72 -6
  32. package/src/memory/archive-cold-state.mjs +794 -0
  33. package/src/memory/archive-staging.mjs +305 -0
  34. package/src/memory/archive.mjs +415 -0
  35. package/src/memory/capture-host.js +71 -2
  36. package/src/memory/capture.js +45 -6
  37. package/src/memory/catchup.mjs +534 -0
  38. package/src/memory/conclusion-distill.js +51 -2
  39. package/src/memory/conclusion-drafts.js +7 -3
  40. package/src/memory/conflicts.js +915 -2
  41. package/src/memory/consolidate.js +20 -1
  42. package/src/memory/forget-v2.js +272 -2
  43. package/src/memory/forget.js +20 -1
  44. package/src/memory/frames-activation.js +3 -1
  45. package/src/memory/fts-query.js +2 -2
  46. package/src/memory/index.js +80 -7
  47. package/src/memory/land.mjs +743 -0
  48. package/src/memory/lifecycle.js +128 -0
  49. package/src/memory/maintenance.js +386 -22
  50. package/src/memory/memory-health.mjs +607 -0
  51. package/src/memory/migrate-200.js +1681 -0
  52. package/src/memory/migrate-lifecycle.js +3 -3
  53. package/src/memory/procedure-candidate.mjs +377 -0
  54. package/src/memory/procedure.js +20 -1
  55. package/src/memory/prospective-memory.mjs +362 -0
  56. package/src/memory/recall-feedback.mjs +406 -0
  57. package/src/memory/recall-v2.js +13 -1
  58. package/src/memory/recall.js +102 -1
  59. package/src/memory/reinforce.js +138 -2
  60. package/src/memory/retention.js +34 -0
  61. package/src/memory/turn-budget.mjs +290 -0
  62. package/src/memory/wake-bridge.mjs +2999 -0
  63. package/src/memory/working-frame.js +271 -8
  64. package/src/metrics/classify.mjs +3 -1
  65. package/src/recall/archive-config.mjs +130 -0
  66. package/src/recall/archive-fallback.mjs +174 -19
  67. package/src/recall/archive-search.mjs +3 -0
  68. package/src/recall/archive-state.mjs +588 -0
  69. package/src/recall/brain.mjs +501 -0
  70. package/src/recall/current-line.mjs +110 -0
  71. package/src/recall/edges.js +21 -2
  72. package/src/recall/explain.mjs +10 -0
  73. package/src/recall/gate-log.mjs +280 -0
  74. package/src/recall/gate-metrics.mjs +191 -0
  75. package/src/recall/index.js +322 -21
  76. package/src/recall/inject-order.mjs +3 -1
  77. package/src/recall/recall-core.mjs +47 -2
  78. package/src/recall/turn-budget-inject.mjs +154 -0
  79. package/src/retention/cold-gate.mjs +146 -0
  80. package/src/retention/config.js +58 -1
  81. package/src/retention/index.js +193 -35
  82. package/src/router/design-headings.json +10 -10
  83. package/src/tools.js +122 -6
@@ -216,6 +216,99 @@ export function compactSection(sections, wanted, maxChars) {
216
216
  return { text: cut.text, found: true, truncated: cut.truncated };
217
217
  }
218
218
 
219
+ /**
220
+ * 「最近三件」按条渲染:先尽量放整条,预算不够时允许放一条不完整的并标 partial。
221
+ * 返回的 items 是完整条数;itemsStarted 是实际起笔的条数(含被截断的那条)。
222
+ * @param {Array<{level:number,title:string,content:string,line:number}>} sections
223
+ * @param {string} wanted
224
+ * @param {number} maxChars
225
+ * @returns {{text:string,chars:number,items:number,totalItems:number,itemsStarted:number,
226
+ * partial:boolean,cut:boolean,truncated:boolean,budget:number,found:boolean}}
227
+ */
228
+ export function renderRecentSection(sections, wanted, maxChars) {
229
+ const budget = Number.isFinite(maxChars) ? Math.max(0, Math.floor(maxChars)) : 0;
230
+ const section = findSection(sections, wanted);
231
+ if (!section || section.content.trim() === '') {
232
+ return {
233
+ text: MISSING_TEXT,
234
+ chars: charLength(MISSING_TEXT),
235
+ items: 0,
236
+ totalItems: 0,
237
+ itemsStarted: 0,
238
+ partial: false,
239
+ cut: false,
240
+ truncated: false,
241
+ budget,
242
+ found: false,
243
+ };
244
+ }
245
+ const cleaned = cleanMarkdown(section.content);
246
+ if (cleaned === '') {
247
+ return {
248
+ text: MISSING_TEXT,
249
+ chars: charLength(MISSING_TEXT),
250
+ items: 0,
251
+ totalItems: 0,
252
+ itemsStarted: 0,
253
+ partial: false,
254
+ cut: false,
255
+ truncated: false,
256
+ budget,
257
+ found: false,
258
+ };
259
+ }
260
+ const items = cleaned.split('\n');
261
+ const totalItems = items.length;
262
+ if (charLength(cleaned) <= budget) {
263
+ return {
264
+ text: cleaned,
265
+ chars: charLength(cleaned),
266
+ items: totalItems,
267
+ totalItems,
268
+ itemsStarted: totalItems,
269
+ partial: false,
270
+ cut: false,
271
+ truncated: false,
272
+ budget,
273
+ found: true,
274
+ };
275
+ }
276
+
277
+ let out = '';
278
+ let complete = 0;
279
+ let partial = false;
280
+ for (let index = 0; index < items.length; index += 1) {
281
+ const item = items[index];
282
+ const separator = index === 0 ? '' : '\n';
283
+ const candidate = out + separator + item;
284
+ if (charLength(candidate) <= budget) {
285
+ out = candidate;
286
+ complete += 1;
287
+ continue;
288
+ }
289
+ const remaining = budget - charLength(out + separator);
290
+ if (remaining > 0) {
291
+ const piece = truncateChars(item, remaining);
292
+ out = out + separator + piece.text;
293
+ partial = true;
294
+ }
295
+ break;
296
+ }
297
+
298
+ return {
299
+ text: out,
300
+ chars: charLength(out),
301
+ items: complete,
302
+ totalItems,
303
+ itemsStarted: partial ? complete + 1 : complete,
304
+ partial,
305
+ cut: true,
306
+ truncated: true,
307
+ budget,
308
+ found: true,
309
+ };
310
+ }
311
+
219
312
  /**
220
313
  * 从文件名里取日期(`YYYY-MM-DD-...`)。取不到返回 null。
221
314
  * @param {unknown} name
@@ -394,7 +487,7 @@ export function resolveMemoryRoot(config = {}, env = process.env) {
394
487
  * @param {number} total
395
488
  * @returns {Record<string,number>}
396
489
  */
397
- function allocateBudget(parts, total) {
490
+ export function allocateBudget(parts, total) {
398
491
  const weights = parts.reduce((sum, part) => sum + part.weight, 0);
399
492
  const budgets = {};
400
493
  let used = 0;
@@ -473,6 +566,9 @@ export function buildOpenCard(input = {}) {
473
566
  const maxChars = Number.isFinite(input.maxChars) && input.maxChars > 0
474
567
  ? Math.floor(input.maxChars)
475
568
  : DEFAULT_MAX_CHARS;
569
+ // M04(G-D7 承接):handoffSummary=false 时开机卡不注入「▍交接卡」节(显式降级,
570
+ // 不静默);默认 true,开机卡形状与 A-min 完全一致。
571
+ const handoffSummaryEnabled = input.handoffSummary !== false;
476
572
  const now = input.now instanceof Date
477
573
  ? input.now
478
574
  : new Date(input.now ?? Date.now());
@@ -495,7 +591,8 @@ export function buildOpenCard(input = {}) {
495
591
  const handoverName = typeof input.handover?.name === 'string' && input.handover.name.trim() !== ''
496
592
  ? input.handover.name.trim()
497
593
  : null;
498
- if (!input.handover) warnings.push('HANDOVER_MISSING');
594
+ if (!handoffSummaryEnabled) warnings.push('HANDOVER_SUMMARY_DISABLED');
595
+ else if (!input.handover) warnings.push('HANDOVER_MISSING');
499
596
  else if (handoverText.trim() === '') warnings.push('HANDOVER_EMPTY');
500
597
 
501
598
  const ageText = stateCardAgeText(stateAgeMs);
@@ -507,29 +604,93 @@ export function buildOpenCard(input = {}) {
507
604
  handover: '▍交接卡',
508
605
  };
509
606
  const overhead = charLength(header)
510
- + Object.values(headings).reduce((sum, value) => sum + charLength(value), 0)
607
+ + Object.entries(headings)
608
+ .filter(([key]) => handoffSummaryEnabled || key !== 'handover')
609
+ .reduce((sum, [, value]) => sum + charLength(value), 0)
511
610
  + 10;
512
611
  const usable = Math.max(80, maxChars - overhead);
513
- const budgets = allocateBudget([
514
- { key: 'where', weight: 1.1 },
515
- { key: 'waiting', weight: 0.9 },
516
- { key: 'recent', weight: 1.1 },
517
- { key: 'handover', weight: 1.3 },
518
- ], usable);
612
+
613
+ // 固定预算模式(A-min):调用方显式给各节预算时不再按权重分配。
614
+ // 这是 DESIGN v0.5.1 §8 C 的口径:recent 1,161 / where 336 / waiting 274 / handover 397。
615
+ const fixed = {};
616
+ for (const [key, value] of Object.entries({
617
+ where: input.whereBudgetChars,
618
+ waiting: input.waitingBudgetChars,
619
+ recent: input.recentBudgetChars,
620
+ handover: input.handoverChars,
621
+ })) {
622
+ if (Number.isFinite(value) && value > 0) fixed[key] = Math.floor(value);
623
+ }
624
+ const budgetSource = Object.keys(fixed).length > 0 ? 'fixed' : 'dynamic';
625
+ const layout = { maxChars, overhead, usable, budgetSource };
626
+
627
+ let budgets;
628
+ if (budgetSource === 'fixed') {
629
+ if (Number.isFinite(fixed.recent)) {
630
+ // recent 是唯一新增的配置键,其余三节从剩余预算按权重分。
631
+ const rest = Math.max(72, usable - fixed.recent);
632
+ const restBudgets = allocateBudget([
633
+ { key: 'where', weight: 1.1 },
634
+ { key: 'waiting', weight: 0.9 },
635
+ { key: 'handover', weight: 1.3 },
636
+ ], rest);
637
+ budgets = {
638
+ where: fixed.where ?? restBudgets.where,
639
+ waiting: fixed.waiting ?? restBudgets.waiting,
640
+ recent: fixed.recent,
641
+ handover: fixed.handover ?? restBudgets.handover,
642
+ };
643
+ } else {
644
+ budgets = allocateBudget([
645
+ { key: 'where', weight: 1.1 },
646
+ { key: 'waiting', weight: 0.9 },
647
+ { key: 'recent', weight: 1.1 },
648
+ { key: 'handover', weight: 1.3 },
649
+ ], usable);
650
+ for (const key of ['where', 'waiting', 'recent', 'handover']) {
651
+ if (Number.isFinite(fixed[key])) budgets[key] = fixed[key];
652
+ }
653
+ }
654
+ const sum = budgets.where + budgets.waiting + budgets.recent + budgets.handover;
655
+ if (sum > usable) {
656
+ const overflow = sum - usable;
657
+ const next = budgets.handover - overflow;
658
+ if (next >= 24) budgets.handover = next;
659
+ else {
660
+ budgets.handover = 24;
661
+ warnings.push('FIXED_BUDGET_OVERFLOW');
662
+ }
663
+ }
664
+ } else {
665
+ budgets = allocateBudget([
666
+ { key: 'where', weight: 1.1 },
667
+ { key: 'waiting', weight: 0.9 },
668
+ { key: 'recent', weight: 1.1 },
669
+ { key: 'handover', weight: 1.3 },
670
+ ], usable);
671
+ }
519
672
 
520
673
  const where = compactSection(parsed.sections, STATE_SECTIONS.where, budgets.where);
521
674
  const waiting = compactSection(parsed.sections, STATE_SECTIONS.waiting, budgets.waiting);
522
- const recent = compactSection(parsed.sections, STATE_SECTIONS.recent, budgets.recent);
675
+ const recent = renderRecentSection(parsed.sections, STATE_SECTIONS.recent, budgets.recent);
676
+
677
+ if (!handoffSummaryEnabled) {
678
+ budgets.handover = 0;
679
+ }
523
680
 
524
681
  let handoverBody;
525
682
  let handoverTitle = '';
526
- if (handoverText.trim() === '') {
683
+ if (!handoffSummaryEnabled) {
684
+ // 显式关闭交接摘要:不读交接卡内容,也不产生截断警告(只有 DISABLED 一条)。
685
+ handoverBody = { text: '', truncated: false };
686
+ } else if (handoverText.trim() === '') {
527
687
  handoverBody = { text: '(没有找到交接卡)', truncated: false };
528
688
  } else {
529
689
  const summary = summarizeHandover(handoverText, budgets.handover);
530
690
  handoverTitle = summary.title.trim();
531
691
  handoverBody = { text: summary.text, truncated: summary.truncated };
532
692
  }
693
+ if (handoverBody.truncated) warnings.push('HANDOVER_SECTION_TRUNCATED');
533
694
 
534
695
  const lines = [
535
696
  header,
@@ -539,9 +700,13 @@ export function buildOpenCard(input = {}) {
539
700
  waiting.text,
540
701
  headings.recent,
541
702
  recent.text,
542
- headings.handover,
543
- handoverName && handoverTitle === '' ? `《${handoverLabel(handoverName)}》` : '',
544
- handoverBody.text,
703
+ ...(handoffSummaryEnabled
704
+ ? [
705
+ headings.handover,
706
+ handoverName && handoverTitle === '' ? `《${handoverLabel(handoverName)}》` : '',
707
+ handoverBody.text,
708
+ ]
709
+ : []),
545
710
  ].filter((line) => line !== '');
546
711
 
547
712
  const joined = lines.join('\n');
@@ -555,9 +720,28 @@ export function buildOpenCard(input = {}) {
555
720
  chars: charLength(cut.text),
556
721
  truncated: cut.truncated || sectionTruncated,
557
722
  warnings,
558
- found: { ...parsed.found, handover: handoverText.trim() !== '' },
723
+ found: { ...parsed.found, handover: handoffSummaryEnabled && handoverText.trim() !== '' },
559
724
  handoverName,
560
725
  stateAgeMinutes,
726
+ layout,
727
+ sections: {
728
+ where: { chars: charLength(where.text), cut: where.truncated, budget: budgets.where },
729
+ waiting: { chars: charLength(waiting.text), cut: waiting.truncated, budget: budgets.waiting },
730
+ recent: {
731
+ chars: recent.chars,
732
+ cut: recent.cut,
733
+ budget: budgets.recent,
734
+ items: recent.items,
735
+ totalItems: recent.totalItems,
736
+ itemsStarted: recent.itemsStarted,
737
+ partial: recent.partial,
738
+ },
739
+ handover: {
740
+ chars: handoffSummaryEnabled ? charLength(handoverBody.text) : 0,
741
+ cut: handoffSummaryEnabled ? handoverBody.truncated : false,
742
+ budget: budgets.handover,
743
+ },
744
+ },
561
745
  };
562
746
  }
563
747
 
@@ -645,6 +829,10 @@ export function collectOpenCard(options = {}) {
645
829
  statePath,
646
830
  stateMtimeMs,
647
831
  handoverPath: handoverPath ?? undefined,
832
+ whereBudgetChars: options.whereBudgetChars,
833
+ waitingBudgetChars: options.waitingBudgetChars,
834
+ recentBudgetChars: options.recentBudgetChars,
835
+ handoverChars: options.handoverChars,
648
836
  });
649
837
 
650
838
  return {
@@ -23,7 +23,9 @@
23
23
  */
24
24
 
25
25
  export const IDENTITY_TEXT_MARK = '【开机卡】';
26
- export const AUTO_TEXT_MARK = '【自动想起】';
26
+ // 2.0.0 起 auto-recall 注入块头是 `【自动想起 · recallId=…】`(零命中同头),
27
+ // 这里只保留前缀,继续兼容历史 `【自动想起】` 正文标记。
28
+ export const AUTO_TEXT_MARK = '【自动想起';
27
29
 
28
30
  /** 本插件写入的隐藏注入 kind(宿主 MessageSourceMap 合法成员)。 */
29
31
  export const INJECTED_SOURCE_KIND = 'session-reference';
@@ -40,18 +40,76 @@ function countRecovered(root) {
40
40
  return fs.existsSync(d) ? fs.readdirSync(d).filter((n) => n.endsWith('.md')).length : 0;
41
41
  }
42
42
 
43
- /** Read-only preflight for downgrade safety. */
44
- export function preflightA1Legacy(memoryRoot) { const root = String(memoryRoot ?? '');
43
+ /** M17:一次性迁移完成标记(=receipt 清单 sha;DESIGN §5 旧机制收编)。 */
44
+ export const A1_MARKER_REL = '.longlive/migrations/a1-legacy.done';
45
+ export const A1_MARKER_SCHEMA = 'a1-legacy-marker/v1';
46
+
47
+ export function a1MarkerPathOf(memoryRoot) {
48
+ return path.join(String(memoryRoot ?? ''), ...A1_MARKER_REL.split('/'));
49
+ }
50
+
51
+ /** 读迁移标记;缺失/坏文件返回 null(坏文件不当作已迁移)。 */
52
+ export function readA1Marker(memoryRoot) {
53
+ const root = String(memoryRoot ?? '');
54
+ if (root === '') return null;
55
+ const file = a1MarkerPathOf(root);
56
+ try {
57
+ const parsed = JSON.parse(fs.readFileSync(file, 'utf8'));
58
+ if (parsed?.schema !== A1_MARKER_SCHEMA) return null;
59
+ return parsed;
60
+ } catch {
61
+ return null;
62
+ }
63
+ }
64
+
65
+ function writeA1Marker(root, receipt) {
66
+ const markerPath = a1MarkerPathOf(root);
67
+ let receiptSha256 = null;
68
+ try {
69
+ if (receipt.receiptPath) receiptSha256 = sha(fs.readFileSync(receipt.receiptPath));
70
+ } catch {
71
+ receiptSha256 = null;
72
+ }
73
+ const marker = {
74
+ schema: A1_MARKER_SCHEMA,
75
+ migratedAt: receipt.generatedAt,
76
+ memoryRoot: root,
77
+ receiptPath: receipt.receiptPath ?? null,
78
+ receiptSha256,
79
+ moved: receipt.moved,
80
+ recovered: receipt.recovered,
81
+ totalLegacy: receipt.totalLegacy,
82
+ };
83
+ fs.mkdirSync(path.dirname(markerPath), { recursive: true });
84
+ fs.writeFileSync(markerPath, `${JSON.stringify(marker, null, 2)}\n`, 'utf8');
85
+ return marker;
86
+ }
87
+
88
+ /** Read-only preflight for downgrade safety. marker 命中时不再要求重复迁移。 */
89
+ export function preflightA1Legacy(memoryRoot) {
90
+ const root = String(memoryRoot ?? '');
45
91
  if (root === '') return { ok: false, code: 'E_MEMORY_ROOT', blocking: false, legacyCount: 0, autoCount: 0 };
46
92
  const legacy = listOldDirect(root).length;
47
93
  const auto = listAutoFiles(root).length;
94
+ const marker = readA1Marker(root);
95
+ if (legacy === 0 && auto === 0) {
96
+ return {
97
+ ok: true, memoryRoot: root, legacyCount: 0, autoCount: 0,
98
+ blocking: false, code: null, migrated: true,
99
+ markerPath: a1MarkerPathOf(root), marker,
100
+ message: marker === null
101
+ ? 'A1 legacy 已迁移(无待迁移卡;无 marker)'
102
+ : `A1 legacy 已迁移(marker: ${marker.migratedAt ?? 'unknown'},总计 ${marker.totalLegacy ?? 0} 张)`,
103
+ };
104
+ }
105
+ const blockingCode = 'E_A1_LEGACY_NOT_MIGRATED';
48
106
  return {
49
107
  ok: true, memoryRoot: root, legacyCount: legacy, autoCount: auto,
50
- blocking: legacy > 0 || auto > 0,
51
- code: legacy > 0 || auto > 0 ? 'E_A1_LEGACY_NOT_MIGRATED' : null,
52
- message: legacy > 0 || auto > 0
108
+ blocking: true, code: blockingCode, migrated: false,
109
+ markerPath: a1MarkerPathOf(root), marker,
110
+ message: marker === null
53
111
  ? `检测到 ${legacy} 张 1.8.0 直写卡、${auto} 张 topics/auto 卡尚未迁移;先运行 scripts/migrate-a1-legacy.mjs --apply 再降级`
54
- : 'A1 legacy 已迁移,降级安全',
112
+ : `检测到 ${legacy} 张 1.8.0 直写卡、${auto} 张 topics/auto 卡(marker 之后新增)尚未迁移;先运行 scripts/migrate-a1-legacy.mjs --apply 再降级`,
55
113
  };
56
114
  }
57
115
 
@@ -86,6 +144,11 @@ export function migrateLegacyA1({ memoryRoot, dryRun = true, now = new Date(), r
86
144
  receiptPath: null, indexRebuilt: false, totalLegacy: 0,
87
145
  };
88
146
  receipt.recovered = countRecovered(root);
147
+ const marker = readA1Marker(root);
148
+ // M17:marker 命中(上一次已 apply 完成)且没有新增待迁移卡 → 直接跳过,不重复迁移。
149
+ if (marker !== null && plan.length === 0) {
150
+ return { ok: true, ...receipt, skipped: 'already-migrated', marker, markerPath: a1MarkerPathOf(root), migrated: true };
151
+ }
89
152
  if (plan.length === 0 && receipt.recovered === 0) return { ok: true, ...receipt };
90
153
  if (!dryRun) fs.mkdirSync(path.dirname(receipt.log), { recursive: true });
91
154
  for (const item of plan) {
@@ -117,6 +180,9 @@ export function migrateLegacyA1({ memoryRoot, dryRun = true, now = new Date(), r
117
180
  if (receipt.log && fs.existsSync(receipt.log)) {
118
181
  fs.appendFileSync(receipt.log, `${JSON.stringify({ at: now.toISOString(), event: 'receipt', path: receipt.receiptPath, moved: receipt.moved, recovered: receipt.recovered, totalLegacy: receipt.totalLegacy })}\n`, 'utf8');
119
182
  }
183
+ // M17:落一次性 marker(=receipt 清单 sha),二次运行不再重复迁移。
184
+ receipt.marker = writeA1Marker(root, receipt);
185
+ receipt.markerPath = a1MarkerPathOf(root);
120
186
  }
121
187
  return { ok: true, ...receipt };
122
188
  }