@jjh32456/longlive 2.1.0 → 2.3.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 (51) hide show
  1. package/CHANGELOG.md +108 -1
  2. package/QUICKSTART.md +7 -7
  3. package/README.en.md +13 -1
  4. package/README.md +26 -8
  5. package/SPEC.md +6 -5
  6. package/build-info.json +143 -73
  7. package/cordis.patch.yml +2 -0
  8. package/docs/ARCHITECTURE.md +6 -3
  9. package/docs/CONFIG.md +25 -1
  10. package/docs/FAQ.md +2 -2
  11. package/docs/FEATURES.md +125 -2
  12. package/docs/INDEX-FRESHNESS.md +8 -1
  13. package/docs/OPERATIONS.md +63 -2
  14. package/docs/QUICKSTART.md +2 -2
  15. package/docs/README.en.md +1 -1
  16. package/docs/TROUBLESHOOTING.md +1 -1
  17. package/package.json +10 -2
  18. package/scripts/assert-install.mjs +124 -0
  19. package/scripts/backlog-report.mjs +98 -0
  20. package/scripts/install-230.mjs +787 -0
  21. package/scripts/lib/eng-common.mjs +428 -0
  22. package/scripts/rebuild-index.mjs +148 -0
  23. package/scripts/restore-drill.mjs +351 -0
  24. package/scripts/run-autoresolve.mjs +123 -0
  25. package/scripts/watchdog-230.mjs +379 -0
  26. package/src/config.js +7 -0
  27. package/src/features.js +35 -0
  28. package/src/gate/action-gate.js +545 -0
  29. package/src/memory/backup-retention.mjs +823 -0
  30. package/src/memory/cards.js +73 -12
  31. package/src/memory/conflict-scan.mjs +356 -0
  32. package/src/memory/conflicts.js +202 -9
  33. package/src/memory/consolidate.js +537 -35
  34. package/src/memory/decay.js +133 -19
  35. package/src/memory/event-index.js +62 -4
  36. package/src/memory/forget-scheduler.mjs +303 -0
  37. package/src/memory/forget-v2.js +94 -8
  38. package/src/memory/index-cache.mjs +216 -0
  39. package/src/memory/index.js +179 -16
  40. package/src/memory/lock.js +53 -12
  41. package/src/memory/maintenance.js +295 -19
  42. package/src/memory/recall-feedback.mjs +159 -17
  43. package/src/memory/recall-v2.js +5 -3
  44. package/src/memory/recall.js +10 -4
  45. package/src/memory/state.js +446 -38
  46. package/src/memory/task-state.mjs +569 -0
  47. package/src/recall/brain.mjs +55 -6
  48. package/src/recall/continuity-volatile.mjs +195 -1
  49. package/src/recall/index.js +8 -0
  50. package/src/tools.js +141 -21
  51. package/src/ui/routes-readonly.js +63 -5
@@ -159,8 +159,9 @@ export function unifiedConflicts({ memoryRoot, includeLifecycle = false } = {})
159
159
  // ∧ 该卡对尚无 conflicts_with 边
160
160
  // ∧ 结论行/frontmatter 不是完全同文(同文是重复而非冲突)
161
161
  // → 只产候选,不改卡、不合并、不写 history。上限 maxPairs(32) 对。
162
- // - 候选落盘:`.longlive/hardgate/conflict-candidates.jsonl`(append-only;
163
- // pending <= maxPending(200) 且 90 天保留;卡对去重;archived 行只移出队列不删历史)。
162
+ // - 候选落盘:`.longlive/conflicts/candidates.jsonl`(append-only;pending <= maxPending(200)
163
+ // 且 90 天保留;卡对去重;archived 行只移出队列不删历史)。2.0.0–2.2.0 旧账本
164
+ // `.longlive/hardgate/conflict-candidates.jsonl` 只读兼容合并(同一卡对:新账本行胜)。
164
165
  // - accept:appendConflictEdge 校验双卡存在 + type 白名单 + 幂等去重;向
165
166
  // `.longlive/recall/edges.jsonl` append 恰 1 条边;向 `.longlive/hardgate/auto-authz.jsonl`
166
167
  // append 恰 1 行 kind=conflict-accept 审计;返回 {ok,edge,auditPath}。
@@ -171,7 +172,12 @@ export function unifiedConflicts({ memoryRoot, includeLifecycle = false } = {})
171
172
  export const CONFLICT_CANDIDATE_SCHEMA = 'conflict-candidates/v1';
172
173
  /** 1.8.x prep 草案写过的 schema:读取时兼容,避免旧行被当坏行。 */
173
174
  const LEGACY_CONFLICT_CANDIDATE_SCHEMA = 'hardgate-conflict-candidate/v1';
174
- export const CONFLICT_CANDIDATES_REL = '.longlive/hardgate/conflict-candidates.jsonl';
175
+ /** 2.3.0 W2.2:写卡自动产出的候选与人工 accept 共用一本账,落在记忆根的 conflicts 目录。 */
176
+ export const CONFLICT_CANDIDATES_REL = '.longlive/conflicts/candidates.jsonl';
177
+ /** 2.0.0–2.2.0 旧账本:只读兼容(升级后的记忆根里仍有 pending 行),新行一律写 CONFLICT_CANDIDATES_REL。 */
178
+ export const LEGACY_CONFLICT_CANDIDATES_REL = '.longlive/hardgate/conflict-candidates.jsonl';
179
+ /** 写卡自动形成 conflicts_with 边时,给该卡对候选记的 accepted 原因码(不是用户裁决)。 */
180
+ export const CONFLICT_WRITE_REASON = 'auto-supersede';
175
181
  export const CONFLICT_LAST_RUN_REL = '.longlive/hardgate/last-run/conflict-candidates.json';
176
182
  export const CONFLICT_CANDIDATE_REASON = 'rule-v1';
177
183
  export const CONFLICT_ACCEPT_KIND = 'conflict-accept';
@@ -610,6 +616,10 @@ export function conflictCandidatesPathOf(memoryRoot) {
610
616
  return path.join(resolveMemoryRoot(memoryRoot), ...CONFLICT_CANDIDATES_REL.split('/'));
611
617
  }
612
618
 
619
+ export function legacyConflictCandidatesPathOf(memoryRoot) {
620
+ return path.join(resolveMemoryRoot(memoryRoot), ...LEGACY_CONFLICT_CANDIDATES_REL.split('/'));
621
+ }
622
+
613
623
  export function conflictLastRunPathOf(memoryRoot) {
614
624
  return path.join(resolveMemoryRoot(memoryRoot), ...CONFLICT_LAST_RUN_REL.split('/'));
615
625
  }
@@ -621,15 +631,19 @@ function isCandidateSchema(schema) {
621
631
  /**
622
632
  * 读候选账本。append-only:同一卡对以最后一行状态为准(pending/accepted/rejected/archived)。
623
633
  * pending 只算「latest 状态 = pending 且未过 retentionDays」的行;坏行计数不抛。
634
+ * 2.3.0 W2.2:本读取兼容旧账本(`.longlive/hardgate/conflict-candidates.jsonl`),按
635
+ * 「旧账本先、新账本后」合并,同一卡对以新账本行胜(升级后不会丢旧 pending,也不会双计)。
624
636
  */
625
637
  export function readConflictCandidates({ memoryRoot, config = {}, now = new Date() } = {}) {
626
638
  const root = resolveMemoryRoot(memoryRoot);
627
639
  const cfg = conflictCfgOf(config);
628
640
  const file = path.join(root, ...CONFLICT_CANDIDATES_REL.split('/'));
641
+ const legacyFile = path.join(root, ...LEGACY_CONFLICT_CANDIDATES_REL.split('/'));
629
642
  const store = readJsonlRows(file);
643
+ const legacyStore = legacyFile === file ? { rows: [], bad: 0, missing: true } : readJsonlRows(legacyFile);
630
644
  const rows = [];
631
- let bad = store.bad;
632
- for (const row of store.rows) {
645
+ let bad = store.bad + legacyStore.bad;
646
+ for (const row of [...legacyStore.rows, ...store.rows]) {
633
647
  if (isCandidateSchema(row?.schema) && typeof row.id === 'string' && typeof row.aCard === 'string' && typeof row.bCard === 'string') rows.push(row);
634
648
  else bad += 1;
635
649
  }
@@ -647,7 +661,19 @@ export function readConflictCandidates({ memoryRoot, config = {}, now = new Date
647
661
  const fresh = !Number.isFinite(atMs) || retentionMs === 0 || nowMs - atMs <= retentionMs;
648
662
  if (fresh) pending.push(row);
649
663
  }
650
- return { ok: true, file, rows, bad, missing: store.missing, byPair, pending, pendingCount: pending.length };
664
+ return {
665
+ ok: true,
666
+ file,
667
+ legacyFile,
668
+ legacyMissing: legacyStore.missing,
669
+ legacyRows: legacyStore.rows.length,
670
+ rows,
671
+ bad,
672
+ missing: store.missing,
673
+ byPair,
674
+ pending,
675
+ pendingCount: pending.length,
676
+ };
651
677
  }
652
678
 
653
679
  /**
@@ -690,7 +716,9 @@ export function appendConflictCandidates({
690
716
  aCard,
691
717
  bCard,
692
718
  sharedTerms: Array.isArray(candidate?.sharedTerms) ? candidate.sharedTerms.map(String) : [],
693
- confidence: Number.isFinite(Number(candidate?.confidence)) ? Number(candidate.confidence) : null,
719
+ confidence: candidate?.confidence === null || candidate?.confidence === undefined
720
+ ? null
721
+ : (Number.isFinite(Number(candidate.confidence)) ? Number(candidate.confidence) : null),
694
722
  evidence: candidate?.evidence && typeof candidate.evidence === 'object' ? candidate.evidence : { a: null, b: null },
695
723
  reason: state === 'pending' ? CONFLICT_CANDIDATE_REASON : String(reason ?? state),
696
724
  state,
@@ -722,6 +750,110 @@ export function appendConflictCandidates({
722
750
  }
723
751
  }
724
752
 
753
+ // ---- 写卡路径候选(2.3.0 W2.2:写入时自动检测 → 落盘 → memory_conflicts 可见) -----
754
+
755
+ /** 写卡检测能产出的 reason 码(cards.js detectConflicts 的三种因果,不是概率)。 */
756
+ const WRITE_CONFLICT_REASONS = new Set(['same-slug', 'same-title', 'same-key-opposite-conclusion']);
757
+ /** 写卡路径的卡 rel 白名单:只接受卡目录 + 无换行/反斜杠/.. 的相对路径。 */
758
+ const WRITE_CONFLICT_REL_PATTERN = /^(topics|journal|procedures)\/[^\r\n\\]+\.md$/i;
759
+
760
+ function writeConflictCardRel(value, memoryRoot) {
761
+ const rel = conflictCardRel(value, memoryRoot);
762
+ if (rel === '' || rel.includes('..') || !WRITE_CONFLICT_REL_PATTERN.test(rel)) return '';
763
+ return rel;
764
+ }
765
+
766
+ /**
767
+ * 卡片写入/合并后的自动冲突检测落盘(2.3.0 W2.2)。
768
+ * - 输入是 cards.js detectConflicts 的返回([{rel, title, reasons}])与刚写的新卡 {rel};
769
+ * - 候选只含 rel 与固定 reason 码 + 来源标签,**不落标题/正文/摘要**(隐私:候选不含个人数据);
770
+ * - 走 appendConflictCandidates → `.longlive/conflicts/candidates.jsonl`(pending 去重/上限/保留期同人工路径);
771
+ * - 只记候选,不改卡、不写边(边由 conflict-scan 在卡落盘成功后写;失败时候选留 pending 供人裁决)。
772
+ * 返回 {ok, disabled?, file, written, pending, candidates, skipped, code?};写盘失败返回 ok:false 不抛。
773
+ */
774
+ export function recordWriteConflictCandidates({
775
+ memoryRoot,
776
+ newCard = null,
777
+ conflicts = [],
778
+ config = {},
779
+ now = new Date(),
780
+ maxPairs = null,
781
+ } = {}) {
782
+ const root = resolveMemoryRoot(memoryRoot);
783
+ const cfg = conflictCfgOf(config);
784
+ const file = conflictCandidatesPathOf(root);
785
+ if (cfg.enabled === false) {
786
+ const ledger = readConflictCandidates({ memoryRoot: root, config, now });
787
+ return { ok: true, disabled: true, file: ledger.file, written: 0, pending: ledger.pendingCount, candidates: [], skipped: [] };
788
+ }
789
+ const newRel = writeConflictCardRel(newCard?.rel, root);
790
+ if (newRel === '') {
791
+ return {
792
+ ok: false,
793
+ disabled: false,
794
+ code: 'E_HG_CONFLICT_TARGET',
795
+ message: 'E_HG_CONFLICT_TARGET: newCard.rel must be a topics/ journal/ procedures/ card rel',
796
+ file,
797
+ written: 0,
798
+ pending: null,
799
+ candidates: [],
800
+ skipped: [],
801
+ };
802
+ }
803
+ const cap = Math.max(0, Number(maxPairs ?? cfg.maxPairs) || 0);
804
+ const candidates = [];
805
+ const skipped = [];
806
+ const seen = new Set();
807
+ for (const conflict of Array.isArray(conflicts) ? conflicts : []) {
808
+ if (candidates.length >= cap) break;
809
+ const oldRel = writeConflictCardRel(conflict?.rel, root);
810
+ if (oldRel === '' || oldRel === newRel) {
811
+ skipped.push({ rel: String(conflict?.rel ?? ''), reason: oldRel === newRel ? 'self' : 'unsafe-rel' });
812
+ continue;
813
+ }
814
+ const reasons = (Array.isArray(conflict?.reasons) ? conflict.reasons : [])
815
+ .map((reason) => String(reason))
816
+ .filter((reason) => WRITE_CONFLICT_REASONS.has(reason));
817
+ if (reasons.length === 0) {
818
+ skipped.push({ rel: oldRel, reason: 'unknown-reason' });
819
+ continue;
820
+ }
821
+ const pairKey = conflictPairKey(newRel, oldRel);
822
+ if (seen.has(pairKey)) continue;
823
+ seen.add(pairKey);
824
+ const ordered = newRel <= oldRel ? [newRel, oldRel] : [oldRel, newRel];
825
+ candidates.push({
826
+ id: conflictCandidateId(newRel, oldRel),
827
+ aCard: ordered[0],
828
+ bCard: ordered[1],
829
+ sharedTerms: [],
830
+ confidence: null,
831
+ evidence: {
832
+ source: 'card-write/v1',
833
+ a: { rel: newRel, role: 'new-card', reasons },
834
+ b: { rel: oldRel, role: 'old-card', reasons },
835
+ },
836
+ });
837
+ }
838
+ const appended = appendConflictCandidates({ memoryRoot: root, candidates, config, now, state: 'pending' });
839
+ const recorded = {
840
+ ok: appended.ok !== false,
841
+ disabled: false,
842
+ file: appended.file,
843
+ written: appended.written,
844
+ archived: appended.archived,
845
+ pending: appended.pending,
846
+ corrupt: appended.corrupt,
847
+ candidates,
848
+ skipped,
849
+ };
850
+ if (appended.ok === false) {
851
+ recorded.code = appended.code;
852
+ recorded.message = appended.message;
853
+ }
854
+ return recorded;
855
+ }
856
+
725
857
  export function readConflictLastRun(memoryRoot) {
726
858
  try {
727
859
  const parsed = JSON.parse(fs.readFileSync(conflictLastRunPathOf(memoryRoot), 'utf8'));
@@ -983,22 +1115,83 @@ export function memoryConflicts(args = {}) {
983
1115
  const view = unifiedConflicts({ memoryRoot: root, includeLifecycle: args?.includeLifecycle === true });
984
1116
  const detected = detectConflictCandidates({ memoryRoot: root, config });
985
1117
  const ledger = readConflictCandidates({ memoryRoot: root, config });
1118
+ // 2.3.0 W2.2:写卡路径产出的候选与边检测候选合成一个待确认视图(同卡对以账本行为准),
1119
+ // 候选字段只含 rel/固定 reason 码/来源标签,不渲染正文(隐私)。
1120
+ const merged = new Map();
1121
+ const writePending = [];
1122
+ for (const row of ledger.pending) {
1123
+ const candidate = {
1124
+ id: row.id,
1125
+ aCard: row.aCard,
1126
+ bCard: row.bCard,
1127
+ sharedTerms: Array.isArray(row.sharedTerms) ? row.sharedTerms.map(String) : [],
1128
+ confidence: row.confidence === null || row.confidence === undefined
1129
+ ? null
1130
+ : (Number.isFinite(Number(row.confidence)) ? Number(row.confidence) : null),
1131
+ evidence: row.evidence && typeof row.evidence === 'object' ? row.evidence : null,
1132
+ // 账本里两条来源并存:写卡路径行带 evidence.source='card-write/v1';
1133
+ // 每日检测行(rule-v1)没有 evidence.source,按 reason 还原为检测来源,避免误标成写卡产物。
1134
+ source: typeof row.evidence?.source === 'string'
1135
+ ? row.evidence.source
1136
+ : (row.reason === CONFLICT_WRITE_REASON ? 'card-write' : CONFLICT_CANDIDATE_REASON),
1137
+ state: 'pending',
1138
+ };
1139
+ writePending.push(candidate);
1140
+ merged.set(conflictPairKey(row.aCard, row.bCard), candidate);
1141
+ }
1142
+ for (const candidate of detected.candidates ?? []) {
1143
+ const key = conflictPairKey(candidate.aCard, candidate.bCard);
1144
+ if (!merged.has(key)) merged.set(key, candidate);
1145
+ }
1146
+ const candidates = [...merged.values()].sort((left, right) => String(left.id).localeCompare(String(right.id)));
1147
+ const autoSuperseded = [...ledger.byPair.values()]
1148
+ .filter((row) => row.state === 'accepted' && row.reason === CONFLICT_WRITE_REASON)
1149
+ .map((row) => ({ id: row.id, aCard: row.aCard, bCard: row.bCard, at: row.at ?? null, state: 'accepted', reason: row.reason }));
986
1150
  return {
987
1151
  ok: true,
988
1152
  action: 'list',
989
1153
  ...view,
990
- candidates: detected.candidates ?? [],
1154
+ candidates,
991
1155
  counts: detected.counts ?? emptyConflictCounts(),
992
1156
  pairView: detected.pairView ?? null,
993
1157
  edgeView: detected.edgeView ?? null,
994
1158
  skippedReasons: detected.skippedReasons ?? {},
995
1159
  limits: detected.limits ?? null,
1160
+ writeConflicts: {
1161
+ pending: writePending,
1162
+ pendingCount: writePending.length,
1163
+ autoSuperseded,
1164
+ autoSupersededCount: autoSuperseded.length,
1165
+ },
996
1166
  pending: { rows: ledger.pendingCount, cap: conflictCfgOf(config).maxPending, retentionDays: conflictCfgOf(config).retentionDays },
997
- ledger: { file: ledger.file, missing: ledger.missing, bad: ledger.bad, rows: ledger.rows.length },
1167
+ ledger: {
1168
+ file: ledger.file,
1169
+ legacyFile: ledger.legacyFile,
1170
+ legacyRows: ledger.legacyRows,
1171
+ missing: ledger.missing,
1172
+ bad: ledger.bad,
1173
+ rows: ledger.rows.length,
1174
+ },
998
1175
  };
999
1176
  }
1000
1177
  if (action === 'accept') {
1001
1178
  const target = resolveConflictTarget(args, root);
1179
+ const before = readConflictCandidates({ memoryRoot: root, config });
1180
+ const pairRow = before.byPair.get(conflictPairKey(target.aCard, target.bCard));
1181
+ if (pairRow && pairRow.state === 'accepted') {
1182
+ // 写卡自动形成的冲突边已经把该卡对置 accepted:重复 accept 幂等返回,不再写第二条边/审计。
1183
+ return {
1184
+ ok: true,
1185
+ action: 'accept',
1186
+ accepted: true,
1187
+ alreadyAccepted: true,
1188
+ id: pairRow.id,
1189
+ aCard: pairRow.aCard,
1190
+ bCard: pairRow.bCard,
1191
+ edge: null,
1192
+ ledger: { file: before.file, written: 0, pending: before.pendingCount, state: 'accepted' },
1193
+ };
1194
+ }
1002
1195
  const accepted = appendConflictEdge(
1003
1196
  root,
1004
1197
  {