@jjh32456/longlive 2.0.0 → 2.2.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.
@@ -26,8 +26,8 @@ import crypto from 'node:crypto';
26
26
 
27
27
  import { dayString, isoStamp } from './frontmatter.js';
28
28
  import { memoryRootFromConfig, resolveInside, relativePath } from './paths.js';
29
- import { ensureDir, writeFileAtomic } from './state.js';
30
- import { memoryConsolidate } from './consolidate.js';
29
+ import { ensureDir, readState, writeFileAtomic } from './state.js';
30
+ import { memoryConsolidate, autoResolveProposals, proposalBacklog } from './consolidate.js';
31
31
  import { memoryForget } from './forget.js';
32
32
  import { proposeForget, FORGET_SUGGESTIONS_REL } from './forget-v2.js';
33
33
  import { runConclusionDistillJob } from './conclusion-distill.js';
@@ -38,6 +38,9 @@ import { probePending as probeMigrationPending } from './migrate-200.js';
38
38
 
39
39
  export const SCHEDULE_VERSION = 1;
40
40
  export const SCHEDULE_REL = path.join('.longlive', 'bridge', 'schedule.json');
41
+ // 2.2.0 lane A: small backlog sidecar so recall/status can read the proposal
42
+ // backlog without parsing the (300KB+) memory-state.json on every step.
43
+ export const BACKLOG_REL = path.join('.longlive', 'bridge', 'backlog.json');
41
44
  export const LOCK_REL = path.join('.longlive', 'bridge', 'schedule.lock');
42
45
  export const PENDING_REPORT_PREFIX = 'forget-pending-';
43
46
  export const RUN_HISTORY_LIMIT = 30;
@@ -76,8 +79,45 @@ function warnTo(logger, message) {
76
79
  }
77
80
  }
78
81
 
79
- function toClock(now) {
80
- if (typeof now === 'function') {
82
+ /**
83
+ * 2.2.0 lane A: auto-resolve knobs may arrive through an existing raw
84
+ * passthrough (`autoResolve: {enabled, maxAccept, maxAcceptNew}`); no new
85
+ * schema leaf is added, and when nothing is configured the consolidate.js
86
+ * defaults (100 mutations / 10 new cards per run) apply.
87
+ */
88
+ function autoResolveOptions(config) {
89
+ const rows = [config?.autoResolve, config?.autoMaintenance?.autoResolve];
90
+ for (const row of rows) {
91
+ if (row && typeof row === 'object' && !Array.isArray(row)) return row;
92
+ }
93
+ return {};
94
+ }
95
+
96
+ /**
97
+ * 2.2.0 lane A: the small backlog sidecar. Readers (recall/status) can stat this
98
+ * file instead of parsing the 300KB+ memory-state.json on every step.
99
+ * Atomic tmp+rename through writeFileAtomic; no backup needed (rebuildable).
100
+ */
101
+ function writeBacklogSidecar(root, { now = new Date(), day = null } = {}) {
102
+ const file = resolveInside(root, BACKLOG_REL);
103
+ const state = readState(root);
104
+ const backlog = proposalBacklog(Array.isArray(state.seenProposals) ? state.seenProposals : []);
105
+ const payload = {
106
+ day: day ?? dayString(now),
107
+ total: backlog.total,
108
+ pending: backlog.pending,
109
+ resolved: backlog.resolved,
110
+ accepted: backlog.accepted,
111
+ rejected: backlog.rejected,
112
+ lastConsolidated: state.lastConsolidated ?? null,
113
+ at: isoStamp(now),
114
+ };
115
+ ensureDir(root, path.dirname(file));
116
+ const write = writeFileAtomic(root, file, `${JSON.stringify(payload, null, 2)}\n`, { backup: false, now });
117
+ return { rel: relativePath(root, file), ...payload, sha256: write.sha256, bytes: write.bytes };
118
+ }
119
+
120
+ function toClock(now) { if (typeof now === 'function') {
81
121
  return () => {
82
122
  const value = now();
83
123
  const date = value instanceof Date ? value : new Date(value);
@@ -98,6 +138,10 @@ function emptyState() {
98
138
  lastConsolidateReport: null,
99
139
  lastConsolidateProposals: null,
100
140
  lastConsolidateError: null,
141
+ // 2.2.0 lane A:提案积压自动处理(consolidate.autoResolveProposals)的每日槽。
142
+ lastAutoResolveAt: null,
143
+ lastAutoResolveResult: null,
144
+ lastAutoResolveError: null,
101
145
  lastForgetDate: null,
102
146
  lastForgetAt: null,
103
147
  lastForgetPending: null,
@@ -141,12 +185,12 @@ function normalizeState(raw) {
141
185
  'lastConsolidateAt', 'lastConsolidateReport',
142
186
  'lastForgetAt', 'lastForgetPending', 'lastForgetSuggestionsAt',
143
187
  'lastConclusionDistillAt', 'lastArchiveStateAt', 'lastArchiveStateStatus',
144
- 'lastMigrate200At', 'lastMigrate200Status',
188
+ 'lastMigrate200At', 'lastMigrate200Status', 'lastAutoResolveAt',
145
189
  'lastRunAt', 'lastRunDay', 'lastTrigger',
146
190
  ]) {
147
191
  if (raw[key] == null || typeof raw[key] === 'string') state[key] = raw[key] ?? null;
148
192
  }
149
- for (const key of ['lastConsolidateError', 'lastForgetError', 'lastForgetSuggestionsError', 'lastConclusionDistillError', 'lastArchiveStateError', 'lastMigrate200Error']) {
193
+ for (const key of ['lastConsolidateError', 'lastForgetError', 'lastForgetSuggestionsError', 'lastConclusionDistillError', 'lastArchiveStateError', 'lastMigrate200Error', 'lastAutoResolveError']) {
150
194
  const value = raw[key];
151
195
  if (value == null) {
152
196
  state[key] = null;
@@ -176,6 +220,16 @@ function normalizeState(raw) {
176
220
  skipped: Number(raw.lastConclusionDistillResult.skipped) || 0,
177
221
  }
178
222
  : null;
223
+ state.lastAutoResolveResult = raw.lastAutoResolveResult
224
+ && typeof raw.lastAutoResolveResult === 'object'
225
+ && !Array.isArray(raw.lastAutoResolveResult)
226
+ ? {
227
+ accepted: Number(raw.lastAutoResolveResult.accepted) || 0,
228
+ rejected: Number(raw.lastAutoResolveResult.rejected) || 0,
229
+ reviewed: Number(raw.lastAutoResolveResult.reviewed) || 0,
230
+ pending: Number(raw.lastAutoResolveResult.pending) || 0,
231
+ }
232
+ : null;
179
233
  state.runs = Array.isArray(raw.runs)
180
234
  ? raw.runs.filter((entry) => entry && typeof entry === 'object').slice(-RUN_HISTORY_LIMIT)
181
235
  : [];
@@ -475,6 +529,7 @@ function runDailyMaintenanceSync({
475
529
  day,
476
530
  trigger,
477
531
  consolidate: null,
532
+ autoResolve: null,
478
533
  forget: null,
479
534
  forgetSuggestions: null,
480
535
  conclusionDistill: null,
@@ -565,6 +620,51 @@ function runDailyMaintenanceSync({
565
620
  }
566
621
  }
567
622
 
623
+ // ---- B1b: 2.2.0 lane A — 提案积压自动处理 ----------------------------
624
+ // 「全量入库,取舍交给遗忘机制」:维护跑完 consolidate 后,把提案积压自动
625
+ // 消化一部分——只对完全重复/结构异常 reject、对干净无目标段 accept,其余
626
+ // 留人工。失败只记 autoResolve 错误,不影响 B2-B7,也不改 consolidate 状态。
627
+ if (needConsolidate && summary.consolidate?.status === 'ran') {
628
+ const autoCfg = autoResolveOptions(config);
629
+ if (autoCfg.enabled !== false) {
630
+ try {
631
+ const auto = autoResolveProposals({
632
+ root,
633
+ now: at,
634
+ maxAccept: autoCfg.maxAccept,
635
+ maxAcceptNew: autoCfg.maxAcceptNew,
636
+ });
637
+ summary.autoResolve = {
638
+ status: auto.status,
639
+ accepted: auto.accepted,
640
+ rejected: auto.rejected,
641
+ reviewed: auto.reviewed,
642
+ pending: auto.pending,
643
+ mutations: auto.mutations,
644
+ lastConsolidated: auto.lastConsolidated ?? null,
645
+ pointerMoved: auto.pointerMoved === true,
646
+ stateWritten: auto.stateWritten === true,
647
+ reviewReasons: auto.reviewReasons ?? {},
648
+ };
649
+ state.lastAutoResolveAt = isoStamp(at);
650
+ state.lastAutoResolveResult = {
651
+ accepted: auto.accepted,
652
+ rejected: auto.rejected,
653
+ reviewed: auto.reviewed,
654
+ pending: auto.pending,
655
+ };
656
+ state.lastAutoResolveError = null;
657
+ } catch (error) {
658
+ summary.autoResolve = { status: 'failed', ...errorInfo(error) };
659
+ state.lastAutoResolveError = { ...errorInfo(error), at: isoStamp(at), day };
660
+ warnTo(
661
+ logger,
662
+ `[auto-maintenance] autoResolve 失败(${summary.autoResolve.code})— ${summary.autoResolve.message};提案保持原状,不影响对话`,
663
+ );
664
+ }
665
+ }
666
+ }
667
+
568
668
  // ---- B2: forget dry-run + pending list --------------------------------
569
669
  if (needForget) {
570
670
  try {
@@ -803,9 +903,30 @@ function runDailyMaintenanceSync({
803
903
  ...(summary.archiveState?.status === 'failed' ? { archiveStateError: summary.archiveState.code } : {}),
804
904
  ...(summary.migrate200?.state ? { migrate200State: summary.migrate200.state } : {}),
805
905
  ...(summary.rotation ? { rotationArchived: summary.rotation.archived ?? 0 } : {}),
906
+ autoResolve: summary.autoResolve?.status ?? 'skipped',
907
+ ...(summary.autoResolve && summary.autoResolve.status !== 'failed'
908
+ ? {
909
+ autoAccepted: summary.autoResolve.accepted ?? 0,
910
+ autoRejected: summary.autoResolve.rejected ?? 0,
911
+ autoPending: summary.autoResolve.pending ?? 0,
912
+ }
913
+ : {}),
914
+ ...(summary.autoResolve?.status === 'failed' ? { autoResolveError: summary.autoResolve.code } : {}),
806
915
  },
807
916
  ].slice(-RUN_HISTORY_LIMIT);
808
917
 
918
+ // 2.2.0 lane A: backlog sidecar (<1KB) so recall/status never parse the
919
+ // 300KB+ memory-state.json just to show "how much is pending".
920
+ try {
921
+ summary.backlog = writeBacklogSidecar(root, { now: at, day });
922
+ } catch (error) {
923
+ summary.backlog = { status: 'failed', ...errorInfo(error) };
924
+ warnTo(
925
+ logger,
926
+ `[auto-maintenance] backlog sidecar 写入失败(${summary.backlog.code})— 不影响维护结果`,
927
+ );
928
+ }
929
+
809
930
  try {
810
931
  const written = writeScheduleState(root, state, { now: at });
811
932
  summary.state = written.state;