@jjh32456/longlive 2.2.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.
- package/CHANGELOG.md +79 -0
- package/QUICKSTART.md +7 -7
- package/README.en.md +13 -1
- package/README.md +26 -8
- package/SPEC.md +6 -5
- package/build-info.json +132 -67
- package/cordis.patch.yml +1 -0
- package/docs/ARCHITECTURE.md +6 -3
- package/docs/CONFIG.md +25 -1
- package/docs/FAQ.md +2 -2
- package/docs/FEATURES.md +104 -2
- package/docs/INDEX-FRESHNESS.md +8 -1
- package/docs/OPERATIONS.md +63 -2
- package/docs/QUICKSTART.md +2 -2
- package/docs/README.en.md +1 -1
- package/docs/TROUBLESHOOTING.md +1 -1
- package/package.json +10 -2
- package/scripts/assert-install.mjs +124 -0
- package/scripts/backlog-report.mjs +98 -0
- package/scripts/install-230.mjs +787 -0
- package/scripts/lib/eng-common.mjs +428 -0
- package/scripts/rebuild-index.mjs +148 -0
- package/scripts/restore-drill.mjs +351 -0
- package/scripts/run-autoresolve.mjs +123 -0
- package/scripts/watchdog-230.mjs +379 -0
- package/src/features.js +14 -0
- package/src/gate/action-gate.js +234 -45
- package/src/memory/backup-retention.mjs +823 -0
- package/src/memory/cards.js +73 -12
- package/src/memory/conflict-scan.mjs +356 -0
- package/src/memory/conflicts.js +202 -9
- package/src/memory/consolidate.js +100 -13
- package/src/memory/decay.js +133 -19
- package/src/memory/event-index.js +62 -4
- package/src/memory/forget-scheduler.mjs +303 -0
- package/src/memory/forget-v2.js +94 -8
- package/src/memory/index-cache.mjs +216 -0
- package/src/memory/index.js +149 -15
- package/src/memory/lock.js +53 -12
- package/src/memory/maintenance.js +172 -17
- package/src/memory/recall-feedback.mjs +159 -17
- package/src/memory/state.js +446 -38
- package/src/memory/task-state.mjs +569 -0
- package/src/recall/brain.mjs +8 -1
- package/src/recall/continuity-volatile.mjs +131 -1
- package/src/tools.js +108 -33
- package/src/ui/routes-readonly.js +63 -5
package/src/memory/lock.js
CHANGED
|
@@ -36,17 +36,50 @@ function nowMs(now) {
|
|
|
36
36
|
return value instanceof Date ? value.getTime() : Number(value);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Cross-platform synchronous sleep. `Atomics.wait` on a fresh SharedArrayBuffer
|
|
41
|
+
* works on the main thread in Node (unlike browsers) and needs no dependency.
|
|
42
|
+
*/
|
|
43
|
+
export function sleepSync(ms) {
|
|
44
|
+
const value = Number(ms);
|
|
45
|
+
if (!Number.isFinite(value) || value <= 0) return 0;
|
|
46
|
+
const cell = new Int32Array(new SharedArrayBuffer(4));
|
|
47
|
+
Atomics.wait(cell, 0, 0, Math.ceil(value));
|
|
48
|
+
return Math.ceil(value);
|
|
49
|
+
}
|
|
50
|
+
|
|
39
51
|
/**
|
|
40
52
|
* Acquire a lease. Returns { ok:true, holder, acquiredAt, expiresAt, staleTakenOver }
|
|
41
|
-
* or throws LockError
|
|
53
|
+
* or throws LockError:
|
|
54
|
+
* * `E_LOCKED` — an active lease is held elsewhere and no waiting was asked for;
|
|
55
|
+
* * `E_LOCK_TIMEOUT`— waiting was requested (`waitTimeoutMs`) and the deadline passed.
|
|
56
|
+
*
|
|
57
|
+
* 2.3.0: optional bounded wait. `waitTimeoutMs` (default 0 = old fail-fast path) and
|
|
58
|
+
* `retryMs` (default 25) make a contended lease retry instead of failing the
|
|
59
|
+
* read-modify-write immediately; the deadline uses the real wall clock so an
|
|
60
|
+
* injected `now` cannot make the wait loop spin forever.
|
|
42
61
|
*/
|
|
62
|
+
export const DEFAULT_LOCK_RETRY_MS = 25;
|
|
63
|
+
export const DEFAULT_LOCK_WAIT_TIMEOUT_MS = 0;
|
|
64
|
+
|
|
43
65
|
export function acquireLease(lockFile, options = {}) {
|
|
44
66
|
const file = path.resolve(String(lockFile));
|
|
45
67
|
const holder = typeof options.holder === 'string' && options.holder !== '' ? options.holder : `pid-${process.pid}`;
|
|
46
68
|
const ttlMs = Number.isInteger(options.ttlMs) && options.ttlMs > 0 ? options.ttlMs : DEFAULT_LOCK_TTL_MS;
|
|
47
69
|
const now = options.now;
|
|
70
|
+
const waitTimeoutMs = Number.isFinite(options.waitTimeoutMs) && options.waitTimeoutMs > 0
|
|
71
|
+
? Number(options.waitTimeoutMs)
|
|
72
|
+
: DEFAULT_LOCK_WAIT_TIMEOUT_MS;
|
|
73
|
+
const retryMs = Number.isFinite(options.retryMs) && options.retryMs >= 0
|
|
74
|
+
? Number(options.retryMs)
|
|
75
|
+
: DEFAULT_LOCK_RETRY_MS;
|
|
76
|
+
const deadline = Date.now() + waitTimeoutMs;
|
|
77
|
+
const startedAt = Date.now();
|
|
48
78
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
49
|
-
|
|
79
|
+
let staleTakeovers = 0;
|
|
80
|
+
let attempts = 0;
|
|
81
|
+
for (;;) {
|
|
82
|
+
attempts += 1;
|
|
50
83
|
const acquiredAt = nowMs(now);
|
|
51
84
|
const lease = { holder, pid: process.pid, acquiredAt, expiresAt: acquiredAt + ttlMs };
|
|
52
85
|
try {
|
|
@@ -56,31 +89,39 @@ export function acquireLease(lockFile, options = {}) {
|
|
|
56
89
|
} finally {
|
|
57
90
|
fs.closeSync(fd);
|
|
58
91
|
}
|
|
59
|
-
return { ok: true, holder, acquiredAt, expiresAt: lease.expiresAt, lockFile: file, staleTakenOver:
|
|
92
|
+
return { ok: true, holder, acquiredAt, expiresAt: lease.expiresAt, lockFile: file, staleTakenOver: staleTakeovers > 0, attempts };
|
|
60
93
|
} catch (error) {
|
|
61
94
|
if (error?.code !== 'EEXIST') throw new LockError('E_LOCK_IO', `无法创建锁文件:${error?.message ?? error}`);
|
|
62
95
|
const current = readLease(file);
|
|
63
96
|
const currentAt = Number(current?.expiresAt ?? 0);
|
|
64
97
|
if (current && current.holder === holder) {
|
|
65
|
-
return { ok: true, holder, acquiredAt: Number(current.acquiredAt) || acquiredAt, expiresAt: currentAt, lockFile: file, alreadyHeld: true };
|
|
98
|
+
return { ok: true, holder, acquiredAt: Number(current.acquiredAt) || acquiredAt, expiresAt: currentAt, lockFile: file, alreadyHeld: true, attempts };
|
|
66
99
|
}
|
|
67
100
|
if (!current || !Number.isFinite(currentAt) || currentAt < nowMs(now)) {
|
|
68
|
-
// stale / corrupt lease: take it over
|
|
101
|
+
// stale / corrupt lease: take it over, but never loop forever on a
|
|
102
|
+
// lease that keeps being re-created by a racing contender.
|
|
103
|
+
if (staleTakeovers >= 3) {
|
|
104
|
+
throw new LockError('E_LOCKED', '写锁竞争失败(stale takeover 多次被抢先)', { attempts });
|
|
105
|
+
}
|
|
106
|
+
staleTakeovers += 1;
|
|
69
107
|
try {
|
|
70
108
|
fs.rmSync(file, { force: true });
|
|
71
109
|
} catch {
|
|
72
|
-
/* raced with another taker; loop retries
|
|
110
|
+
/* raced with another taker; loop retries */
|
|
73
111
|
}
|
|
74
112
|
continue;
|
|
75
113
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
114
|
+
const detail = { holder: current.holder, pid: current.pid, expiresAt: currentAt, attempts };
|
|
115
|
+
if (Date.now() < deadline) {
|
|
116
|
+
sleepSync(retryMs);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (waitTimeoutMs > 0) {
|
|
120
|
+
throw new LockError('E_LOCK_TIMEOUT', `等待写锁超时(${Date.now() - startedAt}ms):被 ${current.holder} 持有(pid=${current.pid})`, detail);
|
|
121
|
+
}
|
|
122
|
+
throw new LockError('E_LOCKED', `写锁被 ${current.holder} 持有(pid=${current.pid})`, detail);
|
|
81
123
|
}
|
|
82
124
|
}
|
|
83
|
-
throw new LockError('E_LOCKED', '写锁竞争失败(stale takeover 两次均被抢先)');
|
|
84
125
|
}
|
|
85
126
|
|
|
86
127
|
/** Release only if the recorded holder matches (never steal someone else's lease). */
|
|
@@ -29,12 +29,16 @@ import { memoryRootFromConfig, resolveInside, relativePath } from './paths.js';
|
|
|
29
29
|
import { ensureDir, readState, writeFileAtomic } from './state.js';
|
|
30
30
|
import { memoryConsolidate, autoResolveProposals, proposalBacklog } from './consolidate.js';
|
|
31
31
|
import { memoryForget } from './forget.js';
|
|
32
|
+
// 2.3.0 W2.3 lane forget:衰减/遗忘调度报告(只读提案;自动链绝不执行立碑/降级)。
|
|
33
|
+
import { runForgetDecayScheduler } from './forget-scheduler.mjs';
|
|
32
34
|
import { proposeForget, FORGET_SUGGESTIONS_REL } from './forget-v2.js';
|
|
33
35
|
import { runConclusionDistillJob } from './conclusion-distill.js';
|
|
34
36
|
// 2.0.0 W3b(⑧):冷档状态探针——archive-state.json 的唯一写入方(fail-closed,自身不抛)。
|
|
35
37
|
import { probeAndWriteArchiveState } from '../recall/archive-state.mjs';
|
|
36
38
|
// 2.0.0 W3c(B7):冷层迁移待办探针——只写 pending.json,永不自动迁移(两次人工 approve 缺一不可)。
|
|
37
39
|
import { probePending as probeMigrationPending } from './migrate-200.js';
|
|
40
|
+
// 2.3.0 W2.2 lane conflict:每日冲突扫描槽(候选检测 + pending 对账;只读卡/只 append 账本)。
|
|
41
|
+
import { runDailyConflictScan } from './conflict-scan.mjs';
|
|
38
42
|
|
|
39
43
|
export const SCHEDULE_VERSION = 1;
|
|
40
44
|
export const SCHEDULE_REL = path.join('.longlive', 'bridge', 'schedule.json');
|
|
@@ -53,6 +57,8 @@ export const AUTO_MAINTENANCE_DEFAULTS = Object.freeze({
|
|
|
53
57
|
consolidate: true,
|
|
54
58
|
forget: true,
|
|
55
59
|
forgetSuggestions: true,
|
|
60
|
+
// 2.3.0 W2.2:每日冲突候选扫描 + 再巩固对账(conflictScan:false 可关)。
|
|
61
|
+
conflictScan: true,
|
|
56
62
|
runOnStart: true,
|
|
57
63
|
startDelayMs: DEFAULT_START_DELAY_MS,
|
|
58
64
|
});
|
|
@@ -139,6 +145,8 @@ function emptyState() {
|
|
|
139
145
|
lastConsolidateProposals: null,
|
|
140
146
|
lastConsolidateError: null,
|
|
141
147
|
// 2.2.0 lane A:提案积压自动处理(consolidate.autoResolveProposals)的每日槽。
|
|
148
|
+
// 2.3.0 lane pipeline(W1-4):独立日期槽,B1 已跑而 B1b 未跑时启动补跑。
|
|
149
|
+
lastAutoResolveDate: null,
|
|
142
150
|
lastAutoResolveAt: null,
|
|
143
151
|
lastAutoResolveResult: null,
|
|
144
152
|
lastAutoResolveError: null,
|
|
@@ -165,6 +173,11 @@ function emptyState() {
|
|
|
165
173
|
lastMigrate200At: null,
|
|
166
174
|
lastMigrate200Status: null,
|
|
167
175
|
lastMigrate200Error: null,
|
|
176
|
+
// 2.3.0 W2.2 lane conflict:每日冲突扫描槽(conflict-scan 写入方,B6)。
|
|
177
|
+
lastConflictScanDate: null,
|
|
178
|
+
lastConflictScanAt: null,
|
|
179
|
+
lastConflictScanStatus: null,
|
|
180
|
+
lastConflictScanError: null,
|
|
168
181
|
lastRunAt: null,
|
|
169
182
|
lastRunDay: null,
|
|
170
183
|
lastTrigger: null,
|
|
@@ -176,21 +189,24 @@ function normalizeState(raw) {
|
|
|
176
189
|
const state = emptyState();
|
|
177
190
|
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return state;
|
|
178
191
|
state.lastConsolidateDate = isDay(raw.lastConsolidateDate) ? raw.lastConsolidateDate : null;
|
|
192
|
+
state.lastAutoResolveDate = isDay(raw.lastAutoResolveDate) ? raw.lastAutoResolveDate : null;
|
|
179
193
|
state.lastForgetDate = isDay(raw.lastForgetDate) ? raw.lastForgetDate : null;
|
|
180
194
|
state.lastForgetSuggestionsDate = isDay(raw.lastForgetSuggestionsDate) ? raw.lastForgetSuggestionsDate : null;
|
|
181
195
|
state.lastConclusionDistillDate = isDay(raw.lastConclusionDistillDate) ? raw.lastConclusionDistillDate : null;
|
|
182
196
|
state.lastArchiveStateDate = isDay(raw.lastArchiveStateDate) ? raw.lastArchiveStateDate : null;
|
|
183
197
|
state.lastMigrate200Date = isDay(raw.lastMigrate200Date) ? raw.lastMigrate200Date : null;
|
|
198
|
+
state.lastConflictScanDate = isDay(raw.lastConflictScanDate) ? raw.lastConflictScanDate : null;
|
|
184
199
|
for (const key of [
|
|
185
200
|
'lastConsolidateAt', 'lastConsolidateReport',
|
|
186
201
|
'lastForgetAt', 'lastForgetPending', 'lastForgetSuggestionsAt',
|
|
187
202
|
'lastConclusionDistillAt', 'lastArchiveStateAt', 'lastArchiveStateStatus',
|
|
188
203
|
'lastMigrate200At', 'lastMigrate200Status', 'lastAutoResolveAt',
|
|
204
|
+
'lastConflictScanAt', 'lastConflictScanStatus',
|
|
189
205
|
'lastRunAt', 'lastRunDay', 'lastTrigger',
|
|
190
206
|
]) {
|
|
191
207
|
if (raw[key] == null || typeof raw[key] === 'string') state[key] = raw[key] ?? null;
|
|
192
208
|
}
|
|
193
|
-
for (const key of ['lastConsolidateError', 'lastForgetError', 'lastForgetSuggestionsError', 'lastConclusionDistillError', 'lastArchiveStateError', 'lastMigrate200Error', 'lastAutoResolveError']) {
|
|
209
|
+
for (const key of ['lastConsolidateError', 'lastForgetError', 'lastForgetSuggestionsError', 'lastConclusionDistillError', 'lastArchiveStateError', 'lastMigrate200Error', 'lastAutoResolveError', 'lastConflictScanError']) {
|
|
194
210
|
const value = raw[key];
|
|
195
211
|
if (value == null) {
|
|
196
212
|
state[key] = null;
|
|
@@ -498,6 +514,8 @@ function runDailyMaintenanceSync({
|
|
|
498
514
|
consolidate = memoryConsolidate,
|
|
499
515
|
forget = memoryForget,
|
|
500
516
|
forgetSuggestions = proposeForget,
|
|
517
|
+
// 2.3.0 W2.2:冲突扫描槽可注入(测试用假实现),默认走 conflict-scan.mjs 的真实实现。
|
|
518
|
+
conflictScan = runDailyConflictScan,
|
|
501
519
|
lockHeld = false,
|
|
502
520
|
} = {}) {
|
|
503
521
|
let root;
|
|
@@ -519,7 +537,12 @@ function runDailyMaintenanceSync({
|
|
|
519
537
|
const wantArchiveState = config.archiveState !== false;
|
|
520
538
|
// 2.0.0 W3c(B7):冷层迁移待办探针槽(migrate200:false 可关);只探测、不迁移。
|
|
521
539
|
const wantMigrate200 = config.migrate200 !== false;
|
|
522
|
-
|
|
540
|
+
// 2.3.0 W2.2 lane conflict:每日冲突候选扫描 + pending 对账槽(conflictScan:false 可关)。
|
|
541
|
+
const wantConflictScan = config.conflictScan !== false;
|
|
542
|
+
// 2.3.0 lane pipeline(W1-4):B1b 有独立日期槽,B1 当天跑过而 B1b 没跑时启动补跑。
|
|
543
|
+
const autoCfg = autoResolveOptions(config);
|
|
544
|
+
const wantAutoResolve = wantConsolidate && autoCfg.enabled !== false;
|
|
545
|
+
if (!wantConsolidate && !wantForget && !wantSuggestions && !wantDrafts && !wantArchiveState && !wantMigrate200 && !wantConflictScan) {
|
|
523
546
|
return { ok: true, ran: false, reason: 'disabled', day, trigger };
|
|
524
547
|
}
|
|
525
548
|
|
|
@@ -535,6 +558,7 @@ function runDailyMaintenanceSync({
|
|
|
535
558
|
conclusionDistill: null,
|
|
536
559
|
archiveState: null,
|
|
537
560
|
migrate200: null,
|
|
561
|
+
conflictScan: null,
|
|
538
562
|
rotation: null,
|
|
539
563
|
state: null,
|
|
540
564
|
corruptState: false,
|
|
@@ -556,12 +580,14 @@ function runDailyMaintenanceSync({
|
|
|
556
580
|
let state = read.state;
|
|
557
581
|
|
|
558
582
|
let needConsolidate = wantConsolidate && state.lastConsolidateDate !== day;
|
|
583
|
+
let needAutoResolve = wantAutoResolve && state.lastAutoResolveDate !== day;
|
|
559
584
|
let needForget = wantForget && state.lastForgetDate !== day;
|
|
560
585
|
let needSuggestions = wantSuggestions && state.lastForgetSuggestionsDate !== day;
|
|
561
586
|
let needDrafts = wantDrafts && state.lastConclusionDistillDate !== day;
|
|
562
587
|
let needArchiveState = wantArchiveState && state.lastArchiveStateDate !== day;
|
|
563
588
|
let needMigrate200 = wantMigrate200 && state.lastMigrate200Date !== day;
|
|
564
|
-
|
|
589
|
+
let needConflictScan = wantConflictScan && state.lastConflictScanDate !== day;
|
|
590
|
+
if (!needConsolidate && !needAutoResolve && !needForget && !needSuggestions && !needDrafts && !needArchiveState && !needMigrate200 && !needConflictScan) {
|
|
565
591
|
return { ...summary, reason: 'already-ran-today', state };
|
|
566
592
|
}
|
|
567
593
|
|
|
@@ -585,19 +611,23 @@ function runDailyMaintenanceSync({
|
|
|
585
611
|
if (latest.corrupt) summary.corruptState = true;
|
|
586
612
|
state = latest.state;
|
|
587
613
|
needConsolidate = wantConsolidate && state.lastConsolidateDate !== day;
|
|
614
|
+
needAutoResolve = wantAutoResolve && state.lastAutoResolveDate !== day;
|
|
588
615
|
needForget = wantForget && state.lastForgetDate !== day;
|
|
589
616
|
needSuggestions = wantSuggestions && state.lastForgetSuggestionsDate !== day;
|
|
590
617
|
needDrafts = wantDrafts && state.lastConclusionDistillDate !== day;
|
|
591
618
|
needArchiveState = wantArchiveState && state.lastArchiveStateDate !== day;
|
|
592
619
|
needMigrate200 = wantMigrate200 && state.lastMigrate200Date !== day;
|
|
593
|
-
|
|
620
|
+
needConflictScan = wantConflictScan && state.lastConflictScanDate !== day;
|
|
621
|
+
if (!needConsolidate && !needAutoResolve && !needForget && !needSuggestions && !needDrafts && !needArchiveState && !needMigrate200 && !needConflictScan) {
|
|
594
622
|
return { ...summary, reason: 'already-ran-today', state };
|
|
595
623
|
}
|
|
596
624
|
|
|
597
625
|
// ---- B1: consolidate (report only) ------------------------------------
|
|
598
626
|
if (needConsolidate) {
|
|
599
627
|
try {
|
|
600
|
-
|
|
628
|
+
// 2.3.0 lane pipeline:把 autoResolve 的 maxSpan/maxText 也交给提案生成侧,
|
|
629
|
+
// 让新提案在进积压前就按同一上限切单元(只切提案,不动 journal)。
|
|
630
|
+
const result = consolidate({}, { memoryRoot: root, now: at, autoResolve: { ...autoCfg } });
|
|
601
631
|
const proposals = Array.isArray(result?.proposals) ? result.proposals.length : 0;
|
|
602
632
|
summary.consolidate = {
|
|
603
633
|
status: 'ran',
|
|
@@ -624,8 +654,12 @@ function runDailyMaintenanceSync({
|
|
|
624
654
|
// 「全量入库,取舍交给遗忘机制」:维护跑完 consolidate 后,把提案积压自动
|
|
625
655
|
// 消化一部分——只对完全重复/结构异常 reject、对干净无目标段 accept,其余
|
|
626
656
|
// 留人工。失败只记 autoResolve 错误,不影响 B2-B7,也不改 consolidate 状态。
|
|
627
|
-
|
|
628
|
-
|
|
657
|
+
//
|
|
658
|
+
// 2.3.0 lane pipeline(W1-4):B1b 改为独立每日槽(lastAutoResolveDate)。
|
|
659
|
+
// 旧逻辑要求 `needConsolidate && consolidate.status==='ran'`,一旦当天 B1 先跑
|
|
660
|
+
// 成功而 B1b 失败/升级前没跑,当天就再也不会补跑。现在只要当日 B1b 槽未打勾
|
|
661
|
+
// 就补跑(先 B1 后 B1b,顺序不变);失败不推进日期,下一次启动维护重试。
|
|
662
|
+
if (needAutoResolve) {
|
|
629
663
|
if (autoCfg.enabled !== false) {
|
|
630
664
|
try {
|
|
631
665
|
const auto = autoResolveProposals({
|
|
@@ -633,6 +667,8 @@ function runDailyMaintenanceSync({
|
|
|
633
667
|
now: at,
|
|
634
668
|
maxAccept: autoCfg.maxAccept,
|
|
635
669
|
maxAcceptNew: autoCfg.maxAcceptNew,
|
|
670
|
+
maxSpan: autoCfg.maxSpan,
|
|
671
|
+
maxText: autoCfg.maxText,
|
|
636
672
|
});
|
|
637
673
|
summary.autoResolve = {
|
|
638
674
|
status: auto.status,
|
|
@@ -646,6 +682,7 @@ function runDailyMaintenanceSync({
|
|
|
646
682
|
stateWritten: auto.stateWritten === true,
|
|
647
683
|
reviewReasons: auto.reviewReasons ?? {},
|
|
648
684
|
};
|
|
685
|
+
state.lastAutoResolveDate = day;
|
|
649
686
|
state.lastAutoResolveAt = isoStamp(at);
|
|
650
687
|
state.lastAutoResolveResult = {
|
|
651
688
|
accepted: auto.accepted,
|
|
@@ -659,7 +696,7 @@ function runDailyMaintenanceSync({
|
|
|
659
696
|
state.lastAutoResolveError = { ...errorInfo(error), at: isoStamp(at), day };
|
|
660
697
|
warnTo(
|
|
661
698
|
logger,
|
|
662
|
-
`[auto-maintenance] autoResolve 失败(${summary.autoResolve.code})— ${summary.autoResolve.message}
|
|
699
|
+
`[auto-maintenance] autoResolve 失败(${summary.autoResolve.code})— ${summary.autoResolve.message};提案保持原状,不推进日期,下次补跑`,
|
|
663
700
|
);
|
|
664
701
|
}
|
|
665
702
|
}
|
|
@@ -689,6 +726,22 @@ function runDailyMaintenanceSync({
|
|
|
689
726
|
undated: Array.isArray(result.undated) ? result.undated.length : 0,
|
|
690
727
|
sha256: write.sha256,
|
|
691
728
|
};
|
|
729
|
+
// 2.3.0 W2.3 lane forget:在 B2 之后产出「衰减/遗忘待办」报告(只读;执行仍只走人工 memory_forget confirm)。
|
|
730
|
+
const forgetDecay = runForgetDecayScheduler({
|
|
731
|
+
root,
|
|
732
|
+
config,
|
|
733
|
+
now: at,
|
|
734
|
+
day,
|
|
735
|
+
forgetResult: result,
|
|
736
|
+
});
|
|
737
|
+
summary.forgetDecay = {
|
|
738
|
+
status: 'ran',
|
|
739
|
+
decayProposals: forgetDecay.decay.proposals.length,
|
|
740
|
+
forgetProposals: forgetDecay.forget.proposals.length,
|
|
741
|
+
report: forgetDecay.report.rel,
|
|
742
|
+
sha256: forgetDecay.report.sha256,
|
|
743
|
+
privacy: forgetDecay.report.privacy.ok,
|
|
744
|
+
};
|
|
692
745
|
state.lastForgetDate = day;
|
|
693
746
|
state.lastForgetAt = isoStamp(at);
|
|
694
747
|
state.lastForgetPending = summary.forget.pendingRel;
|
|
@@ -783,6 +836,14 @@ function runDailyMaintenanceSync({
|
|
|
783
836
|
if (config.journalRotate !== false) {
|
|
784
837
|
try {
|
|
785
838
|
summary.rotation = rotateJournals({ memoryRoot: root, now: at, config });
|
|
839
|
+
// 2.3.0 lane pipeline:拆分失败/无日期行不再静默 no-op —— report.ok=false 时
|
|
840
|
+
// 明确告警(原因进 report.errors,维护摘要与 run 历史都留痕)。
|
|
841
|
+
if (summary.rotation && summary.rotation.ok === false) {
|
|
842
|
+
warnTo(
|
|
843
|
+
logger,
|
|
844
|
+
`[auto-maintenance] journal 轮转有告警:${summary.rotation.errors?.map((item) => `${item.name ?? '?'}:${item.code ?? item.reason ?? '?'}`).join(', ') || 'unknown'} — 原文件未动`,
|
|
845
|
+
);
|
|
846
|
+
}
|
|
786
847
|
} catch (error) {
|
|
787
848
|
summary.rotation = { ok: false, ...errorInfo(error) };
|
|
788
849
|
warnTo(
|
|
@@ -874,8 +935,49 @@ function runDailyMaintenanceSync({
|
|
|
874
935
|
}
|
|
875
936
|
}
|
|
876
937
|
|
|
938
|
+
// ---- B6(2.3.0 W2.2 lane conflict):每日冲突扫描 + 再巩固对账 ------------------
|
|
939
|
+
// 候选检测(runConflictCandidateJob 按天幂等,落 .longlive/conflicts/candidates.jsonl 的 pending)
|
|
940
|
+
// + pending 对账归档(端点消失 / 已有 conflicts_with 边 / 端点已 superseded → archived)。
|
|
941
|
+
// 只读卡、只 append 账本,不删卡、不合并卡;失败只告警、不推进日期,下次补跑重试。
|
|
942
|
+
if (needConflictScan) {
|
|
943
|
+
try {
|
|
944
|
+
const result = conflictScan({ memoryRoot: root, config, now: at, logger });
|
|
945
|
+
if (result && result.ok === true) {
|
|
946
|
+
summary.conflictScan = {
|
|
947
|
+
status: 'ran',
|
|
948
|
+
created: result.steps?.candidates?.created ?? 0,
|
|
949
|
+
archived: result.steps?.candidates?.archived ?? 0,
|
|
950
|
+
pending: result.steps?.candidates?.pending ?? null,
|
|
951
|
+
reconcileArchived: result.steps?.reconcile?.archived ?? 0,
|
|
952
|
+
candidatesStatus: result.steps?.candidates?.status ?? null,
|
|
953
|
+
reconcileStatus: result.steps?.reconcile?.status ?? null,
|
|
954
|
+
};
|
|
955
|
+
state.lastConflictScanDate = day;
|
|
956
|
+
state.lastConflictScanAt = isoStamp(at);
|
|
957
|
+
state.lastConflictScanStatus = result.steps?.candidates?.status ?? 'ran';
|
|
958
|
+
state.lastConflictScanError = null;
|
|
959
|
+
} else {
|
|
960
|
+
const code = String(result?.code ?? result?.steps?.candidates?.code ?? result?.steps?.reconcile?.code ?? 'E_HG_CONFLICT_SCAN_FAILED');
|
|
961
|
+
const info = { code, message: `conflict-scan 未完成(${code})`, at: isoStamp(at), day };
|
|
962
|
+
summary.conflictScan = { status: 'failed', ...info, steps: result?.steps ?? null };
|
|
963
|
+
state.lastConflictScanError = info;
|
|
964
|
+
warnTo(
|
|
965
|
+
logger,
|
|
966
|
+
`[auto-maintenance] conflict-scan 未成功(${code})— 不推进日期,下次补跑重试;不影响对话`,
|
|
967
|
+
);
|
|
968
|
+
}
|
|
969
|
+
} catch (error) {
|
|
970
|
+
summary.conflictScan = { status: 'failed', ...errorInfo(error) };
|
|
971
|
+
state.lastConflictScanError = { ...errorInfo(error), at: isoStamp(at), day };
|
|
972
|
+
warnTo(
|
|
973
|
+
logger,
|
|
974
|
+
`[auto-maintenance] conflict-scan 失败(${summary.conflictScan.code})— 已跳过,不影响对话`,
|
|
975
|
+
);
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
|
|
877
979
|
// ---- state -------------------------------------------------------------
|
|
878
|
-
if (!summary.consolidate && !summary.forget && !summary.conclusionDistill && !summary.archiveState && !summary.migrate200 && !summary.rotation) {
|
|
980
|
+
if (!summary.consolidate && !summary.autoResolve && !summary.forget && !summary.conclusionDistill && !summary.archiveState && !summary.migrate200 && !summary.conflictScan && !summary.rotation) {
|
|
879
981
|
return { ...summary, reason: 'nothing-to-do', state };
|
|
880
982
|
}
|
|
881
983
|
summary.ran = true;
|
|
@@ -899,6 +1001,14 @@ function runDailyMaintenanceSync({
|
|
|
899
1001
|
...(summary.conclusionDistill?.status === 'failed' ? { conclusionDistillError: summary.conclusionDistill.code } : {}),
|
|
900
1002
|
archiveState: summary.archiveState?.status ?? 'skipped',
|
|
901
1003
|
migrate200: summary.migrate200?.status ?? 'skipped',
|
|
1004
|
+
conflictScan: summary.conflictScan?.status ?? 'skipped',
|
|
1005
|
+
...(summary.conflictScan?.status === 'ran'
|
|
1006
|
+
? {
|
|
1007
|
+
conflictCandidates: summary.conflictScan.created ?? 0,
|
|
1008
|
+
conflictReconcileArchived: summary.conflictScan.reconcileArchived ?? 0,
|
|
1009
|
+
}
|
|
1010
|
+
: {}),
|
|
1011
|
+
...(summary.conflictScan?.status === 'failed' ? { conflictScanError: summary.conflictScan.code } : {}),
|
|
902
1012
|
...(summary.archiveState?.probeStatus ? { archiveStateStatus: summary.archiveState.probeStatus } : {}),
|
|
903
1013
|
...(summary.archiveState?.status === 'failed' ? { archiveStateError: summary.archiveState.code } : {}),
|
|
904
1014
|
...(summary.migrate200?.state ? { migrate200State: summary.migrate200.state } : {}),
|
|
@@ -1112,13 +1222,32 @@ export const JOURNAL_MANIFEST_REL = '.longlive/archive/journal/manifest.jsonl';
|
|
|
1112
1222
|
export const JOURNAL_ROTATE_DEFAULTS = Object.freeze({ afterDays: 30, maxBytes: 2 * 1024 * 1024 });
|
|
1113
1223
|
const MONTH_JOURNAL_RE = /^(\d{4})-(\d{2})\.md$/;
|
|
1114
1224
|
const DAY_JOURNAL_RE = /^(\d{4}-\d{2}-\d{2})\.md$/;
|
|
1225
|
+
// 2.3.0 lane pipeline(W1-3):真实 journal 的行首不是裸日期:
|
|
1226
|
+
// `# 2026-11-21` / `## 2026-11-02 · 标题` / `- 18:30 · 轮次…`
|
|
1227
|
+
// 旧识别只认「行首恰为 YYYY-MM-DD」,所以月文件永远 groups.size===0 → 静默 no-op。
|
|
1228
|
+
const JOURNAL_DAY_HEADING_RE = /^\s{0,3}#{1,6}\s*(\d{4}-\d{2}-\d{2})(?![\d-])/;
|
|
1229
|
+
const JOURNAL_DAY_BARE_RE = /^(\d{4}-\d{2}-\d{2})(?![\d-])/;
|
|
1230
|
+
const JOURNAL_TIME_BULLET_RE = /^\s*[-*]\s*\d{1,2}:\d{2}\b/;
|
|
1231
|
+
|
|
1232
|
+
/** 从一行认出所属日期(heading 或裸日期行);认不出返回 null。 */
|
|
1233
|
+
function journalDayOfLine(line) {
|
|
1234
|
+
const heading = JOURNAL_DAY_HEADING_RE.exec(line);
|
|
1235
|
+
if (heading) return heading[1];
|
|
1236
|
+
const bare = JOURNAL_DAY_BARE_RE.exec(line);
|
|
1237
|
+
if (bare) return bare[1];
|
|
1238
|
+
return null;
|
|
1239
|
+
}
|
|
1115
1240
|
|
|
1116
1241
|
function sha256Bytes(bytes) {
|
|
1117
1242
|
return crypto.createHash('sha256').update(bytes).digest('hex');
|
|
1118
1243
|
}
|
|
1119
1244
|
|
|
1120
1245
|
function gzipTo(archiveDir, rel, bytes, now) {
|
|
1121
|
-
|
|
1246
|
+
// archiveDir may not exist yet on the first rotation (2.3.0 lane pipeline):
|
|
1247
|
+
// ensureDir(archiveDir, archiveDir) would fail realpathSync on a missing root,
|
|
1248
|
+
// so create the absolute archive directory directly; callers already proved it
|
|
1249
|
+
// lives inside the memory root.
|
|
1250
|
+
fs.mkdirSync(archiveDir, { recursive: true });
|
|
1122
1251
|
const gzRel = `${rel}.gz`;
|
|
1123
1252
|
const gzAbs = path.join(archiveDir, gzRel);
|
|
1124
1253
|
const gz = zlib.gzipSync(bytes, { level: 9 });
|
|
@@ -1134,9 +1263,12 @@ function appendRotationManifest(root, row) {
|
|
|
1134
1263
|
}
|
|
1135
1264
|
|
|
1136
1265
|
/**
|
|
1137
|
-
* 月文件 →
|
|
1138
|
-
*
|
|
1139
|
-
*
|
|
1266
|
+
* 月文件 → 日文件:按真实行首分组——`## YYYY-MM-DD · 标题` / `# YYYY-MM-DD` /
|
|
1267
|
+
* 裸日期 `YYYY-MM-DD` 开头都算当天入口;`- HH:MM · 轮次…` 这类只有时间的流水行
|
|
1268
|
+
* 跟随最近的日期段(2026-11/12 真实 journal 就是这两种行首)。
|
|
1269
|
+
* 开头无日期的行(frontmatter、`# 2026 年…` 旧标题)留给归档,不硬塞进某一天;
|
|
1270
|
+
* 如果一个日期都认不出来,返回 ok:false + 计数,由 rotateJournals/maintenance 告警,
|
|
1271
|
+
* 不再静默 no-op。
|
|
1140
1272
|
*/
|
|
1141
1273
|
function splitMonthJournal(root, name, now, dryRun) {
|
|
1142
1274
|
const journalDir = resolveInside(root, JOURNAL_DIR_REL);
|
|
@@ -1145,14 +1277,26 @@ function splitMonthJournal(root, name, now, dryRun) {
|
|
|
1145
1277
|
const lines = text.split('\n');
|
|
1146
1278
|
const groups = new Map();
|
|
1147
1279
|
let current = null;
|
|
1280
|
+
let undated = 0;
|
|
1281
|
+
let timeOnlyWithoutDay = 0;
|
|
1148
1282
|
for (const line of lines) {
|
|
1149
|
-
const hit =
|
|
1150
|
-
if (hit)
|
|
1151
|
-
|
|
1283
|
+
const hit = journalDayOfLine(line);
|
|
1284
|
+
if (hit) {
|
|
1285
|
+
current = hit;
|
|
1286
|
+
} else if (current === null) {
|
|
1287
|
+
// 头部无日期的行:归档时原样保留,不属于任何一天。
|
|
1288
|
+
if (line.trim() !== '') {
|
|
1289
|
+
undated += 1;
|
|
1290
|
+
if (JOURNAL_TIME_BULLET_RE.test(line)) timeOnlyWithoutDay += 1;
|
|
1291
|
+
}
|
|
1292
|
+
continue;
|
|
1293
|
+
}
|
|
1152
1294
|
if (!groups.has(current)) groups.set(current, []);
|
|
1153
1295
|
groups.get(current).push(line);
|
|
1154
1296
|
}
|
|
1155
|
-
if (groups.size === 0)
|
|
1297
|
+
if (groups.size === 0) {
|
|
1298
|
+
return { name, ok: false, reason: 'no-dated-lines', lines: lines.length, undated, timeOnlyWithoutDay };
|
|
1299
|
+
}
|
|
1156
1300
|
const written = [];
|
|
1157
1301
|
if (!dryRun) {
|
|
1158
1302
|
for (const [day, groupLines] of groups) {
|
|
@@ -1200,6 +1344,17 @@ export function rotateJournals({ memoryRoot, now = new Date(), config = {}, dryR
|
|
|
1200
1344
|
try {
|
|
1201
1345
|
const result = splitMonthJournal(root, name, at, dryRun);
|
|
1202
1346
|
if (result.ok) report.split.push(result);
|
|
1347
|
+
else {
|
|
1348
|
+
// 2.3.0 lane pipeline:认不出日期不是「无事发生」,必须进 errors 让上游可见。
|
|
1349
|
+
report.errors.push({
|
|
1350
|
+
name,
|
|
1351
|
+
code: 'W_MONTH_SPLIT_NO_DATED_LINES',
|
|
1352
|
+
reason: result.reason ?? 'no-dated-lines',
|
|
1353
|
+
lines: result.lines ?? null,
|
|
1354
|
+
undated: result.undated ?? null,
|
|
1355
|
+
timeOnlyWithoutDay: result.timeOnlyWithoutDay ?? null,
|
|
1356
|
+
});
|
|
1357
|
+
}
|
|
1203
1358
|
} catch (error) {
|
|
1204
1359
|
report.errors.push({ name, code: String(error?.code ?? error?.message ?? error) });
|
|
1205
1360
|
}
|