@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.
- package/CHANGELOG.md +232 -202
- package/QUICKSTART.md +10 -11
- package/README.en.md +13 -4
- package/README.md +42 -11
- package/SPEC.md +3 -3
- package/build-info.json +231 -121
- package/cordis.patch.yml +32 -1
- package/docs/ARCHITECTURE.md +1 -1
- package/docs/AUTO-RECALL.md +1 -1
- package/docs/CONFIG.md +41 -9
- package/docs/FAQ.md +7 -7
- package/docs/FEATURES.md +6 -6
- package/docs/OPERATIONS.md +1 -1
- package/docs/PANEL-INTEGRATION.md +1 -1
- package/docs/PRIVACY.md +5 -5
- package/docs/QUICKSTART.md +3 -3
- package/docs/README.en.md +4 -2
- package/docs/TROUBLESHOOTING.md +4 -4
- package/package.json +6 -3
- package/runbook.md +3 -3
- package/scripts/migrate-200.mjs +8 -0
- package/scripts/migrate-lifecycle.mjs +2 -2
- package/src/answer/claim-guard.mjs +234 -0
- package/src/backup.js +165 -1
- package/src/config.js +18 -7
- package/src/features.js +128 -5
- package/src/handoff/index.js +133 -0
- package/src/handoff/model.js +48 -0
- package/src/identity/handover.js +203 -15
- package/src/identity/inject-position.js +3 -1
- package/src/memory/a1-migration.js +72 -6
- package/src/memory/archive-cold-state.mjs +794 -0
- package/src/memory/archive-staging.mjs +305 -0
- package/src/memory/archive.mjs +415 -0
- package/src/memory/capture-host.js +71 -2
- package/src/memory/capture.js +45 -6
- package/src/memory/catchup.mjs +534 -0
- package/src/memory/conclusion-distill.js +51 -2
- package/src/memory/conclusion-drafts.js +7 -3
- package/src/memory/conflicts.js +915 -2
- package/src/memory/consolidate.js +20 -1
- package/src/memory/forget-v2.js +272 -2
- package/src/memory/forget.js +20 -1
- package/src/memory/frames-activation.js +3 -1
- package/src/memory/fts-query.js +2 -2
- package/src/memory/index.js +80 -7
- package/src/memory/land.mjs +743 -0
- package/src/memory/lifecycle.js +128 -0
- package/src/memory/maintenance.js +386 -22
- package/src/memory/memory-health.mjs +607 -0
- package/src/memory/migrate-200.js +1681 -0
- package/src/memory/migrate-lifecycle.js +3 -3
- package/src/memory/procedure-candidate.mjs +377 -0
- package/src/memory/procedure.js +20 -1
- package/src/memory/prospective-memory.mjs +362 -0
- package/src/memory/recall-feedback.mjs +406 -0
- package/src/memory/recall-v2.js +13 -1
- package/src/memory/recall.js +102 -1
- package/src/memory/reinforce.js +138 -2
- package/src/memory/retention.js +34 -0
- package/src/memory/turn-budget.mjs +290 -0
- package/src/memory/wake-bridge.mjs +2999 -0
- package/src/memory/working-frame.js +271 -8
- package/src/metrics/classify.mjs +3 -1
- package/src/recall/archive-config.mjs +130 -0
- package/src/recall/archive-fallback.mjs +174 -19
- package/src/recall/archive-search.mjs +3 -0
- package/src/recall/archive-state.mjs +588 -0
- package/src/recall/brain.mjs +501 -0
- package/src/recall/current-line.mjs +110 -0
- package/src/recall/edges.js +21 -2
- package/src/recall/explain.mjs +10 -0
- package/src/recall/gate-log.mjs +280 -0
- package/src/recall/gate-metrics.mjs +191 -0
- package/src/recall/index.js +322 -21
- package/src/recall/inject-order.mjs +3 -1
- package/src/recall/recall-core.mjs +47 -2
- package/src/recall/turn-budget-inject.mjs +154 -0
- package/src/retention/cold-gate.mjs +146 -0
- package/src/retention/config.js +58 -1
- package/src/retention/index.js +193 -35
- package/src/router/design-headings.json +10 -10
- package/src/tools.js +122 -6
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
// src/memory/prospective-memory.mjs — 前瞻记忆(D15):从对话/任务里抽承诺与待办,落 prospective.jsonl。
|
|
2
|
+
//
|
|
3
|
+
// 契约来源:V0.5.2-MERGE-PLAN §4/§14 D15 行 / KICKOFF-PLAN §2-D / BATCH-D-BRIEF M 规格。
|
|
4
|
+
// 触发词:「以后 / 明天 / 下次 / 答应 / 记得 / 回头」+ 任务/时间线索;
|
|
5
|
+
// 落盘:prospective.jsonl(≤500 条 / 90d;done 保留 30d;expired 只标记不删);
|
|
6
|
+
// 注入:每轮到期 ≤3 条、每条 ≤120 字;落盘前必须脱敏(机器路径/密钥);
|
|
7
|
+
// 失败路径:写 gate-log 事件 `hg.d15.fail`,不静默吞账。
|
|
8
|
+
//
|
|
9
|
+
// 设计边界:本模块只做抽取/脱敏/落盘/选到期,不直接注入;注入接线在 recall/index.js。
|
|
10
|
+
|
|
11
|
+
import crypto from 'node:crypto';
|
|
12
|
+
import fs from 'node:fs';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
|
|
15
|
+
import { appendGateLog } from '../recall/gate-log.mjs';
|
|
16
|
+
|
|
17
|
+
export const BATCH = 'D';
|
|
18
|
+
export const MODULE = 'memory/prospective-memory.mjs';
|
|
19
|
+
export const UNIMPLEMENTED_CODE = 'E_HG_PROSPECTIVE_UNIMPLEMENTED';
|
|
20
|
+
export const SKELETON = Object.freeze({
|
|
21
|
+
version: '2.0.0',
|
|
22
|
+
failClosed: true,
|
|
23
|
+
batch: BATCH,
|
|
24
|
+
implemented: true,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
/** 前瞻记忆账本相对 memoryRoot 的路径 */
|
|
28
|
+
export const PROSPECTIVE_REL = '.longlive/hardgate/prospective.jsonl';
|
|
29
|
+
/** 触发词(计划冻结集合) */
|
|
30
|
+
export const TRIGGER_WORDS = Object.freeze(['以后', '明天', '下次', '答应', '记得', '回头']);
|
|
31
|
+
/** 时间线索(触发词之外的另一半条件;计划口径:触发词+时间线索才抽取) */
|
|
32
|
+
export const TIME_WORDS = Object.freeze([
|
|
33
|
+
'明天', '以后', '下次', '回头', '周末', '之前', '今晚', '下周', '上午', '下午',
|
|
34
|
+
'晚上', '今天', '改天', '晚些', '之后', '届时', '月底', '本周',
|
|
35
|
+
]);
|
|
36
|
+
/** 账本上限(计划冻结) */
|
|
37
|
+
export const MAX_ITEMS = 500;
|
|
38
|
+
/** 保留天数(计划冻结) */
|
|
39
|
+
export const RETENTION_DAYS = 90;
|
|
40
|
+
/** done 条目保留天数(计划冻结) */
|
|
41
|
+
export const DONE_RETENTION_DAYS = 30;
|
|
42
|
+
/** 每轮注入上限(计划冻结:≤3 条;每条 ≤120 字) */
|
|
43
|
+
export const MAX_DUE_PER_ROUND = 3;
|
|
44
|
+
export const MAX_DUE_CHARS = 120;
|
|
45
|
+
/** 失败门事件 */
|
|
46
|
+
export const GATE_EVENT = 'hg.d15.fail';
|
|
47
|
+
/** 到期窗口:24h 内到期的条目才进注入 */
|
|
48
|
+
export const DUE_HORIZON_MS = 24 * 60 * 60 * 1000;
|
|
49
|
+
|
|
50
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
51
|
+
|
|
52
|
+
function failWithGate(root, code, message) {
|
|
53
|
+
try {
|
|
54
|
+
if (typeof root === 'string' && root !== '') {
|
|
55
|
+
appendGateLog(root, {
|
|
56
|
+
sessionId: null,
|
|
57
|
+
turn: 0,
|
|
58
|
+
gate: GATE_EVENT,
|
|
59
|
+
decision: 'fail',
|
|
60
|
+
reason: code,
|
|
61
|
+
evidence: [String(message ?? '').slice(0, 300)],
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
} catch {
|
|
65
|
+
/* gate-log must never mask the original failure */
|
|
66
|
+
}
|
|
67
|
+
const error = new Error(`${code}: ${message}`);
|
|
68
|
+
error.code = code;
|
|
69
|
+
error.gate = GATE_EVENT;
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function nowMsOf(now) {
|
|
74
|
+
if (now instanceof Date) return now.getTime();
|
|
75
|
+
if (typeof now === 'number' && Number.isFinite(now)) return now;
|
|
76
|
+
if (typeof now === 'string' && now.trim() !== '') {
|
|
77
|
+
const parsed = Date.parse(now);
|
|
78
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
79
|
+
}
|
|
80
|
+
return Date.now();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function isoOf(ms) {
|
|
84
|
+
return new Date(ms).toISOString();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function clip(text, max) {
|
|
88
|
+
const value = String(text ?? '').replace(/\s+/g, ' ').trim();
|
|
89
|
+
if (value.length <= max) return value;
|
|
90
|
+
return `${value.slice(0, max - 1)}…`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function sha256Hex(value) {
|
|
94
|
+
return crypto.createHash('sha256').update(String(value), 'utf8').digest('hex');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function dayString(ms) {
|
|
98
|
+
return new Date(ms).toISOString().slice(0, 10);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 机器路径 / 常见密钥形态脱敏(不做统计口径处理,只替换字面量)。 */
|
|
102
|
+
export function redactMachinePaths(text, options = {}) {
|
|
103
|
+
void options;
|
|
104
|
+
if (typeof text !== 'string' || text === '') return '';
|
|
105
|
+
let out = text;
|
|
106
|
+
// Windows 盘符路径:C:\a\b、D:/a/b
|
|
107
|
+
out = out.replace(/(?:[A-Za-z]:[\\/])[^\s"'`,。;、,;::()()\[\]【】]*/g, '[redacted]');
|
|
108
|
+
// UNC 路径:\\server\share\...
|
|
109
|
+
out = out.replace(/\\\\[^\s\\/"'`,。;、,;::()()\[\]【】]+\\[^\s"'`,。;、,;::()()\[\]【】]*/g, '[redacted]');
|
|
110
|
+
// POSIX 路径:~/、/home/、/Users/、/mnt/
|
|
111
|
+
out = out.replace(/(?:~\/|\/home\/|\/Users\/|\/mnt\/)[^\s"'`,。;、,;::()()\[\]【】]*/g, '[redacted]');
|
|
112
|
+
// 常见 token 形态
|
|
113
|
+
out = out.replace(/\b(?:sk|pk|ghp|xoxb|glpat)-[A-Za-z0-9_-]{8,}\b/g, '[redacted]');
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** 计划允许的脱敏别名。 */
|
|
118
|
+
export function redact(text, options = {}) {
|
|
119
|
+
return redactMachinePaths(text, options);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function ledgerPath(root) {
|
|
123
|
+
return path.join(root, ...PROSPECTIVE_REL.split('/'));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function readLedger(file, root) {
|
|
127
|
+
if (!fs.existsSync(file)) return [];
|
|
128
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
129
|
+
const rows = [];
|
|
130
|
+
for (const line of text.split(/\r?\n/)) {
|
|
131
|
+
if (line.trim() === '') continue;
|
|
132
|
+
let row;
|
|
133
|
+
try {
|
|
134
|
+
row = JSON.parse(line);
|
|
135
|
+
} catch {
|
|
136
|
+
failWithGate(root, 'E_HG_D15_LEDGER_CORRUPT', `prospective ledger has corrupt JSON line: ${line.slice(0, 120)}`);
|
|
137
|
+
}
|
|
138
|
+
if (row === null || typeof row !== 'object') {
|
|
139
|
+
failWithGate(root, 'E_HG_D15_LEDGER_CORRUPT', `prospective ledger row is not an object: ${line.slice(0, 120)}`);
|
|
140
|
+
}
|
|
141
|
+
rows.push(row);
|
|
142
|
+
}
|
|
143
|
+
return rows;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function writeLedger(file, rows) {
|
|
147
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
148
|
+
const tmp = `${file}.tmp-${process.pid}-${Date.now()}`;
|
|
149
|
+
const body = rows.map((row) => JSON.stringify(row)).join('\n');
|
|
150
|
+
fs.writeFileSync(tmp, body === '' ? '' : `${body}\n`, 'utf8');
|
|
151
|
+
fs.renameSync(tmp, file);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function atTime(baseMs, dayShift, hour, minute = 0) {
|
|
155
|
+
const date = new Date(baseMs);
|
|
156
|
+
date.setHours(hour, minute, 0, 0);
|
|
157
|
+
if (dayShift !== 0) date.setDate(date.getDate() + dayShift);
|
|
158
|
+
return date;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function nextWeekday(baseMs, weekday, hour) {
|
|
162
|
+
const date = new Date(baseMs);
|
|
163
|
+
date.setHours(hour, 0, 0, 0);
|
|
164
|
+
const diff = (weekday - date.getDay() + 7) % 7;
|
|
165
|
+
if (diff === 0 && date.getTime() <= baseMs) date.setDate(date.getDate() + 7);
|
|
166
|
+
else date.setDate(date.getDate() + diff);
|
|
167
|
+
return date;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** 依据时间线索算 due;没有可判定时间线索 → null(计划:下次/回头/以后 可为 null)。 */
|
|
171
|
+
function dueForLine(line, baseMs) {
|
|
172
|
+
if (/周末|之前/.test(line) && /周末|之前/.test(line)) {
|
|
173
|
+
return nextWeekday(baseMs, 6, 9).toISOString();
|
|
174
|
+
}
|
|
175
|
+
if (/下周/.test(line)) return nextWeekday(baseMs, 1, 9).toISOString();
|
|
176
|
+
if (/明天/.test(line)) return atTime(baseMs, 1, 9).toISOString();
|
|
177
|
+
if (/今晚/.test(line)) return atTime(baseMs, 0, 21).toISOString();
|
|
178
|
+
if (/之后|改天|晚些|届时/.test(line)) return atTime(baseMs, 2, 9).toISOString();
|
|
179
|
+
if (/月底/.test(line)) {
|
|
180
|
+
const date = new Date(baseMs);
|
|
181
|
+
date.setHours(9, 0, 0, 0);
|
|
182
|
+
date.setMonth(date.getMonth() + 1, 0);
|
|
183
|
+
if (date.getTime() <= baseMs) date.setMonth(date.getMonth() + 1, 0);
|
|
184
|
+
return date.toISOString();
|
|
185
|
+
}
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function normalizeItem(item, options, baseMs) {
|
|
190
|
+
if (item === null || typeof item !== 'object') return null;
|
|
191
|
+
const rawText = typeof item.text === 'string' ? item.text : '';
|
|
192
|
+
const text = clip(redactMachinePaths(rawText), MAX_DUE_CHARS);
|
|
193
|
+
if (text === '') return null;
|
|
194
|
+
const sessionId = typeof item.sessionId === 'string' ? item.sessionId : (typeof options.sessionId === 'string' ? options.sessionId : null);
|
|
195
|
+
const turn = Number.isInteger(item.turn) ? item.turn : (Number.isInteger(options.turn) ? options.turn : 0);
|
|
196
|
+
const id = typeof item.id === 'string' && item.id !== ''
|
|
197
|
+
? item.id
|
|
198
|
+
: `pm-${sha256Hex(`${sessionId ?? ''}|${turn}|${text}`).slice(0, 16)}`;
|
|
199
|
+
let due = null;
|
|
200
|
+
if (typeof item.due === 'string' && item.due !== '') {
|
|
201
|
+
const parsed = Date.parse(item.due);
|
|
202
|
+
if (Number.isFinite(parsed)) due = isoOf(parsed);
|
|
203
|
+
}
|
|
204
|
+
const triggerWord = typeof item.triggerWord === 'string' ? item.triggerWord : null;
|
|
205
|
+
const createdAt = typeof item.createdAt === 'string' && Number.isFinite(Date.parse(item.createdAt))
|
|
206
|
+
? isoOf(Date.parse(item.createdAt))
|
|
207
|
+
: isoOf(baseMs);
|
|
208
|
+
return {
|
|
209
|
+
schema: 'hardgate-prospective/v1',
|
|
210
|
+
id,
|
|
211
|
+
text,
|
|
212
|
+
due,
|
|
213
|
+
triggerWord,
|
|
214
|
+
createdAt,
|
|
215
|
+
sessionId,
|
|
216
|
+
turn,
|
|
217
|
+
status: item.status === 'done' || item.status === 'expired' ? item.status : 'open',
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** 从一段文本抽取前瞻条目(承诺/待办)。 */
|
|
222
|
+
export function extractProspective(text, options = {}) {
|
|
223
|
+
if (typeof text !== 'string' || text.trim() === '') return [];
|
|
224
|
+
const baseMs = nowMsOf(options.now);
|
|
225
|
+
const sessionId = typeof options.sessionId === 'string' ? options.sessionId : null;
|
|
226
|
+
const turn = Number.isInteger(options.turn) ? options.turn : 0;
|
|
227
|
+
const items = [];
|
|
228
|
+
for (const rawLine of text.split(/\r?\n/)) {
|
|
229
|
+
const line = rawLine.trim();
|
|
230
|
+
if (line === '') continue;
|
|
231
|
+
const triggerWord = TRIGGER_WORDS.find((word) => line.includes(word)) ?? null;
|
|
232
|
+
if (triggerWord === null) continue;
|
|
233
|
+
const hasTimeWord = TIME_WORDS.some((word) => line.includes(word));
|
|
234
|
+
if (!hasTimeWord) continue;
|
|
235
|
+
const redactedText = clip(redactMachinePaths(line), MAX_DUE_CHARS);
|
|
236
|
+
if (redactedText === '') continue;
|
|
237
|
+
const item = normalizeItem({
|
|
238
|
+
text: redactedText,
|
|
239
|
+
due: dueForLine(line, baseMs),
|
|
240
|
+
triggerWord,
|
|
241
|
+
createdAt: isoOf(baseMs),
|
|
242
|
+
sessionId,
|
|
243
|
+
turn,
|
|
244
|
+
}, options, baseMs);
|
|
245
|
+
if (item !== null) items.push(item);
|
|
246
|
+
}
|
|
247
|
+
return items;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** 计划允许的导出别名之一(与 extractProspective 等价)。 */
|
|
251
|
+
export function extractTasks(text, options = {}) {
|
|
252
|
+
return extractProspective(text, options);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** 计划允许的导出别名之一(与 extractProspective 等价)。 */
|
|
256
|
+
export function scanProspective(text, options = {}) {
|
|
257
|
+
return extractProspective(text, options);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** 追加候选到 prospective.jsonl(去重 / 条数上限 / done 保留期 / 原子写)。 */
|
|
261
|
+
export function appendProspective(input = {}) {
|
|
262
|
+
const memoryRoot = typeof input?.memoryRoot === 'string' ? input.memoryRoot : '';
|
|
263
|
+
const root = memoryRoot;
|
|
264
|
+
try {
|
|
265
|
+
if (root === '') failWithGate('', 'E_HG_D15_NO_ROOT', 'appendProspective requires memoryRoot');
|
|
266
|
+
const items = Array.isArray(input?.items) ? input.items : null;
|
|
267
|
+
if (items === null) failWithGate(root, 'E_HG_D15_BAD_ITEMS', 'appendProspective requires items array');
|
|
268
|
+
const baseMs = nowMsOf(input?.now);
|
|
269
|
+
const file = ledgerPath(root);
|
|
270
|
+
const before = readLedger(file, root);
|
|
271
|
+
const seen = new Set(before.map((row) => row.id));
|
|
272
|
+
const added = [];
|
|
273
|
+
for (const raw of items) {
|
|
274
|
+
const item = normalizeItem(raw, input ?? {}, baseMs);
|
|
275
|
+
if (item === null) continue;
|
|
276
|
+
if (seen.has(item.id)) continue;
|
|
277
|
+
seen.add(item.id);
|
|
278
|
+
added.push(item);
|
|
279
|
+
}
|
|
280
|
+
const merged = [...before, ...added];
|
|
281
|
+
const kept = trimLedger(merged, baseMs);
|
|
282
|
+
writeLedger(file, kept);
|
|
283
|
+
return { ok: true, added: added.length, total: kept.length, file, items: added };
|
|
284
|
+
} catch (error) {
|
|
285
|
+
if (error?.gate === GATE_EVENT) throw error;
|
|
286
|
+
failWithGate(root, 'E_HG_D15_APPEND_FAILED', error?.message ?? String(error));
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function trimLedger(rows, baseMs) {
|
|
292
|
+
const cutoffDone = baseMs - DONE_RETENTION_DAYS * DAY_MS;
|
|
293
|
+
const cutoffAll = baseMs - RETENTION_DAYS * DAY_MS;
|
|
294
|
+
const kept = [];
|
|
295
|
+
for (const row of rows) {
|
|
296
|
+
const created = Date.parse(String(row?.createdAt ?? ''));
|
|
297
|
+
// done 超 30d 清出;expired 只标记不删(只受 500 条上限裁剪);其余超 90d 裁最旧
|
|
298
|
+
if (row?.status === 'done' && Number.isFinite(created) && created < cutoffDone) continue;
|
|
299
|
+
if (row?.status !== 'done' && row?.status !== 'expired'
|
|
300
|
+
&& Number.isFinite(created) && created < cutoffAll) continue;
|
|
301
|
+
kept.push(row);
|
|
302
|
+
}
|
|
303
|
+
if (kept.length <= MAX_ITEMS) return kept;
|
|
304
|
+
return kept.slice(kept.length - MAX_ITEMS);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** 选出本轮到期条目(≤3 条 / 每条 ≤120 字;过期未完成标 expired 不返回)。 */
|
|
308
|
+
export function selectDueProspective(input = {}) {
|
|
309
|
+
const memoryRoot = typeof input?.memoryRoot === 'string' ? input.memoryRoot : '';
|
|
310
|
+
const root = memoryRoot;
|
|
311
|
+
try {
|
|
312
|
+
if (root === '') failWithGate('', 'E_HG_D15_NO_ROOT', 'selectDueProspective requires memoryRoot');
|
|
313
|
+
const baseMs = nowMsOf(input?.now);
|
|
314
|
+
const file = ledgerPath(root);
|
|
315
|
+
const rows = readLedger(file, root);
|
|
316
|
+
const horizon = baseMs + DUE_HORIZON_MS;
|
|
317
|
+
const out = [];
|
|
318
|
+
let changed = false;
|
|
319
|
+
for (const row of rows) {
|
|
320
|
+
if (row?.status !== 'open') continue;
|
|
321
|
+
const dueMs = typeof row.due === 'string' ? Date.parse(row.due) : Number.NaN;
|
|
322
|
+
if (!Number.isFinite(dueMs)) continue;
|
|
323
|
+
if (dueMs < baseMs) {
|
|
324
|
+
row.status = 'expired';
|
|
325
|
+
row.expiredAt = isoOf(baseMs);
|
|
326
|
+
changed = true;
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
if (dueMs > horizon) continue;
|
|
330
|
+
out.push({ ...row, text: clip(row.text, MAX_DUE_CHARS) });
|
|
331
|
+
}
|
|
332
|
+
if (changed) writeLedger(file, rows);
|
|
333
|
+
out.sort((left, right) => (Date.parse(left.due) - Date.parse(right.due))
|
|
334
|
+
|| String(left.id).localeCompare(String(right.id)));
|
|
335
|
+
return { ok: true, items: out.slice(0, MAX_DUE_PER_ROUND), total: out.length, file };
|
|
336
|
+
} catch (error) {
|
|
337
|
+
if (error?.gate === GATE_EVENT) throw error;
|
|
338
|
+
failWithGate(root, 'E_HG_D15_SELECT_FAILED', error?.message ?? String(error));
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** 便于上层判断账期(纯函数)。 */
|
|
344
|
+
export function prospectiveAgeDays(item, now = undefined) {
|
|
345
|
+
const baseMs = nowMsOf(now);
|
|
346
|
+
const created = Date.parse(String(item?.createdAt ?? ''));
|
|
347
|
+
if (!Number.isFinite(created)) return null;
|
|
348
|
+
return Math.max(0, (baseMs - created) / DAY_MS);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** 供面板/测试读取账本(只读;坏行由 readLedger 走 fail-closed)。 */
|
|
352
|
+
export function readProspective(input = {}) {
|
|
353
|
+
const root = typeof input?.memoryRoot === 'string' ? input.memoryRoot : '';
|
|
354
|
+
if (root === '') failWithGate('', 'E_HG_D15_NO_ROOT', 'readProspective requires memoryRoot');
|
|
355
|
+
const file = ledgerPath(root);
|
|
356
|
+
const rows = readLedger(file, root);
|
|
357
|
+
return { ok: true, rows, total: rows.length, file };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export function dayStringOf(ms) {
|
|
361
|
+
return dayString(nowMsOf(ms));
|
|
362
|
+
}
|