@kanadego/dsh-heartbeat 2.3.0 → 2.4.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/README.md +70 -81
- package/USAGE-GUIDE.md +357 -0
- package/dist/{chunk-43Q42HWH.js → chunk-AJ5JAQVB.js} +13 -2
- package/dist/chunk-AJ5JAQVB.js.map +1 -0
- package/dist/chunk-CRRN6UD3.js +389 -0
- package/dist/chunk-CRRN6UD3.js.map +1 -0
- package/dist/{chunk-K47VV6IQ.js → chunk-ERRPMYKB.js} +163 -153
- package/dist/chunk-ERRPMYKB.js.map +1 -0
- package/dist/chunk-L54R23O2.js +262 -0
- package/dist/chunk-L54R23O2.js.map +1 -0
- package/dist/{chunk-HSTGRQLP.js → chunk-MHRAPKES.js} +406 -66
- package/dist/chunk-MHRAPKES.js.map +1 -0
- package/dist/{chunk-ABFO4QII.js → chunk-RPWQZYNW.js} +603 -157
- package/dist/chunk-RPWQZYNW.js.map +1 -0
- package/dist/cli/index.js +160 -23
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.js +98 -22
- package/dist/index.js.map +1 -1
- package/dist/{migrate-6HH2ZARV.js → migrate-25F53XIC.js} +4 -2
- package/dist/{orchestrator-OJJM5QSV.js → orchestrator-4PJD7QYO.js} +11 -6
- package/dist/snapshot-J6LKNY57.js +27 -0
- package/dist/store-HM6ZMFKS.js +45 -0
- package/package.json +6 -3
- package/dist/chunk-43Q42HWH.js.map +0 -1
- package/dist/chunk-ABFO4QII.js.map +0 -1
- package/dist/chunk-HSTGRQLP.js.map +0 -1
- package/dist/chunk-JIRQ36F7.js +0 -87
- package/dist/chunk-JIRQ36F7.js.map +0 -1
- package/dist/chunk-K47VV6IQ.js.map +0 -1
- package/dist/chunk-KGOC6EX4.js +0 -413
- package/dist/chunk-KGOC6EX4.js.map +0 -1
- package/dist/chunk-QOLKUYQ3.js +0 -393
- package/dist/chunk-QOLKUYQ3.js.map +0 -1
- package/dist/pool-QTY2MI56.js +0 -49
- package/dist/snapshot-6DFNGXTR.js +0 -16
- /package/dist/{migrate-6HH2ZARV.js.map → migrate-25F53XIC.js.map} +0 -0
- /package/dist/{orchestrator-OJJM5QSV.js.map → orchestrator-4PJD7QYO.js.map} +0 -0
- /package/dist/{pool-QTY2MI56.js.map → snapshot-J6LKNY57.js.map} +0 -0
- /package/dist/{snapshot-6DFNGXTR.js.map → store-HM6ZMFKS.js.map} +0 -0
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from "./chunk-
|
|
7
|
-
import {
|
|
8
|
-
appendAuditLine
|
|
9
|
-
} from "./chunk-K47VV6IQ.js";
|
|
2
|
+
appendAuditLine,
|
|
3
|
+
inHhMmWindow,
|
|
4
|
+
localDayKey,
|
|
5
|
+
minutesOfDay
|
|
6
|
+
} from "./chunk-CRRN6UD3.js";
|
|
10
7
|
import {
|
|
11
8
|
loadEncryptedText,
|
|
12
9
|
loadJson,
|
|
@@ -22,59 +19,395 @@ import {
|
|
|
22
19
|
atomicWriteJsonSync
|
|
23
20
|
} from "./chunk-66IUDHCY.js";
|
|
24
21
|
|
|
25
|
-
// src/
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
function
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
22
|
+
// src/seeds/pool.ts
|
|
23
|
+
import path from "path";
|
|
24
|
+
|
|
25
|
+
// src/seeds/types.ts
|
|
26
|
+
function emptySeedDb() {
|
|
27
|
+
return { seq: 0, seeds: [] };
|
|
28
|
+
}
|
|
29
|
+
var SEED_SOURCE_DEFAULT_CONFIDENCE = {
|
|
30
|
+
hand: 1,
|
|
31
|
+
profile: 0.7,
|
|
32
|
+
chat: 0.6,
|
|
33
|
+
screen: 0.4,
|
|
34
|
+
browse: 0.4,
|
|
35
|
+
bilibili: 0.4
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// src/seeds/pool.ts
|
|
39
|
+
var DAY_MS = 864e5;
|
|
40
|
+
var NO_AUDIT = () => {
|
|
41
|
+
};
|
|
42
|
+
function emit(audit, entry) {
|
|
43
|
+
try {
|
|
44
|
+
audit(entry);
|
|
45
|
+
} catch {
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
var SEED_CATEGORY_CAPS = { topic: 16, chat: 14 };
|
|
49
|
+
var SEED_SOURCE_CAPS = { bilibili: 6 };
|
|
50
|
+
var CATEGORY_VALUES = ["topic", "chat"];
|
|
51
|
+
function normalizeCategory(v) {
|
|
52
|
+
return typeof v === "string" && CATEGORY_VALUES.includes(v) ? v : "topic";
|
|
53
|
+
}
|
|
54
|
+
var SEED_SIMILARITY_THRESHOLD = 0.85;
|
|
55
|
+
var DEDUP_ARCHIVE_WINDOW_MS = 30 * DAY_MS;
|
|
56
|
+
var DEDUP_ARCHIVE_MAX = 50;
|
|
57
|
+
var DEDUP_SIMILAR_MIN_CHARS = 12;
|
|
58
|
+
function normalizeSeedText(s) {
|
|
59
|
+
return s.normalize("NFKC").toLowerCase().replace(/[\s\p{P}\p{S}]+/gu, "");
|
|
60
|
+
}
|
|
61
|
+
function seedSimilarity(a, b) {
|
|
62
|
+
const na = normalizeSeedText(a);
|
|
63
|
+
const nb = normalizeSeedText(b);
|
|
64
|
+
if (na === nb) return 1;
|
|
65
|
+
if (na.length < 2 || nb.length < 2) return 0;
|
|
66
|
+
const gramCount = (t) => {
|
|
67
|
+
const m = /* @__PURE__ */ new Map();
|
|
68
|
+
for (let i = 0; i < t.length - 1; i++) {
|
|
69
|
+
const g = t.slice(i, i + 2);
|
|
70
|
+
m.set(g, (m.get(g) ?? 0) + 1);
|
|
71
|
+
}
|
|
72
|
+
return m;
|
|
73
|
+
};
|
|
74
|
+
const A = gramCount(na);
|
|
75
|
+
const B = gramCount(nb);
|
|
76
|
+
let inter = 0;
|
|
77
|
+
let sizeA = 0;
|
|
78
|
+
let sizeB = 0;
|
|
79
|
+
for (const c of A.values()) sizeA += c;
|
|
80
|
+
for (const c of B.values()) sizeB += c;
|
|
81
|
+
for (const [g, c] of A) inter += Math.min(c, B.get(g) ?? 0);
|
|
82
|
+
return 2 * inter / (sizeA + sizeB);
|
|
83
|
+
}
|
|
84
|
+
function recentArchived(db, now) {
|
|
85
|
+
return archivedSeeds(db).filter((s) => now - parseIso(s.retiredAt ?? "") <= DEDUP_ARCHIVE_WINDOW_MS).sort((a, b) => parseIso(b.retiredAt ?? "") - parseIso(a.retiredAt ?? "")).slice(0, DEDUP_ARCHIVE_MAX);
|
|
86
|
+
}
|
|
87
|
+
var TTL_KEYS = ["news", "fandom", "scene", "promise"];
|
|
88
|
+
function normalizeTag(tag) {
|
|
89
|
+
if (tag && TTL_KEYS.includes(tag)) return tag;
|
|
90
|
+
return "scene";
|
|
91
|
+
}
|
|
92
|
+
function parseIso(v) {
|
|
93
|
+
const t = Date.parse(v);
|
|
94
|
+
return Number.isFinite(t) ? t : 0;
|
|
95
|
+
}
|
|
96
|
+
function seedsFilePath(dataDir) {
|
|
97
|
+
return path.join(dataDir, "seeds.jsonl");
|
|
98
|
+
}
|
|
99
|
+
function loadPool(guard, file) {
|
|
100
|
+
const raw = loadEncryptedText(guard, file);
|
|
101
|
+
if (raw === null) return emptySeedDb();
|
|
102
|
+
const db = emptySeedDb();
|
|
103
|
+
for (const line of raw.split("\n")) {
|
|
104
|
+
const trimmed = line.trim();
|
|
105
|
+
if (!trimmed) continue;
|
|
106
|
+
try {
|
|
107
|
+
const obj = JSON.parse(trimmed);
|
|
108
|
+
if (typeof obj.id === "string" && obj.id.startsWith("s")) {
|
|
109
|
+
obj.category = normalizeCategory(obj.category);
|
|
110
|
+
db.seeds.push(obj);
|
|
111
|
+
const n = Number(obj.id.slice(1));
|
|
112
|
+
if (Number.isFinite(n) && n > db.seq) db.seq = n;
|
|
113
|
+
}
|
|
114
|
+
} catch {
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return db;
|
|
118
|
+
}
|
|
119
|
+
function savePool(guard, file, db) {
|
|
120
|
+
const body = db.seeds.map((s) => JSON.stringify(s)).join("\n");
|
|
121
|
+
saveEncryptedText(guard, file, body ? body + "\n" : "");
|
|
122
|
+
}
|
|
123
|
+
function update(guard, file, fn) {
|
|
124
|
+
return withFileLock(file, () => fn(loadPool(guard, file)));
|
|
125
|
+
}
|
|
126
|
+
var activeSeeds = (db) => db.seeds.filter((s) => s.status === "active");
|
|
127
|
+
var archivedSeeds = (db) => db.seeds.filter((s) => s.status === "archived");
|
|
128
|
+
function evictionScore(seed, policy, now) {
|
|
129
|
+
const ttlDays = policy.seeds.ttlDays[seed.tag] || 14;
|
|
130
|
+
const daysStale = Math.max(0, (now - parseIso(seed.lastEvidenceAt)) / DAY_MS);
|
|
131
|
+
const freshness = Math.max(0, 1 - daysStale / ttlDays);
|
|
132
|
+
const unused = seed.used === 0 ? 1 : 1 / seed.used;
|
|
133
|
+
const w = policy.seeds.scoreWeights;
|
|
134
|
+
return freshness * w.freshness + unused * w.unused + seed.confidence * w.confidence;
|
|
135
|
+
}
|
|
136
|
+
function pickEvictionVictim(db, policy, now, category, source) {
|
|
137
|
+
const actives = activeSeeds(db).filter(
|
|
138
|
+
(s) => (category === void 0 || normalizeCategory(s.category) === category) && (source === void 0 || s.source === source)
|
|
139
|
+
);
|
|
140
|
+
if (actives.length === 0) return null;
|
|
141
|
+
const unprotected = actives.filter((s) => !s.protected);
|
|
142
|
+
const candidates = unprotected.length > 0 ? unprotected : actives;
|
|
143
|
+
let worst = null;
|
|
144
|
+
let worstScore = Number.POSITIVE_INFINITY;
|
|
145
|
+
for (const s of candidates) {
|
|
146
|
+
const score = evictionScore(s, policy, now);
|
|
147
|
+
if (score < worstScore) {
|
|
148
|
+
worstScore = score;
|
|
149
|
+
worst = s;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return worst;
|
|
153
|
+
}
|
|
154
|
+
function archiveSeed(seed, reason, now) {
|
|
155
|
+
seed.status = "archived";
|
|
156
|
+
seed.retireReason = reason;
|
|
157
|
+
seed.retiredAt = new Date(now).toISOString();
|
|
158
|
+
}
|
|
159
|
+
function retireLimit(s, policy) {
|
|
160
|
+
return normalizeCategory(s.category) === "chat" ? 1 : policy.seeds.retireAfterUsed;
|
|
161
|
+
}
|
|
162
|
+
function isConsumed(s, policy, prevUsedAt, sinceEvidence) {
|
|
163
|
+
if (s.used < retireLimit(s, policy)) return false;
|
|
164
|
+
return !(prevUsedAt > 0 && sinceEvidence > prevUsedAt);
|
|
165
|
+
}
|
|
166
|
+
function addSeed(guard, file, policy, input, now = Date.now(), audit = NO_AUDIT) {
|
|
167
|
+
return withFileLock(file, () => addSeedLocked(guard, file, policy, input, now, audit));
|
|
168
|
+
}
|
|
169
|
+
function addSeedLocked(guard, file, policy, input, now, audit) {
|
|
170
|
+
const db = loadPool(guard, file);
|
|
171
|
+
const text = input.text.trim();
|
|
172
|
+
if (!text) throw new Error("seed text must not be empty");
|
|
173
|
+
const tag = normalizeTag(input.tag);
|
|
174
|
+
const source = input.source ?? "chat";
|
|
175
|
+
const category = normalizeCategory(input.category ?? (source === "chat" ? "chat" : "topic"));
|
|
176
|
+
const confidence = input.confidence ?? SEED_SOURCE_DEFAULT_CONFIDENCE[source];
|
|
177
|
+
const textNorm = normalizeSeedText(text);
|
|
178
|
+
for (const cand of [...activeSeeds(db), ...recentArchived(db, now)]) {
|
|
179
|
+
if (cand.text === text) {
|
|
180
|
+
emit(audit, { event: "seed_duplicate", id: cand.id, reason: "exact" });
|
|
181
|
+
return { kind: "duplicate", seed: cand, reason: "exact" };
|
|
182
|
+
}
|
|
183
|
+
const candNorm = normalizeSeedText(cand.text);
|
|
184
|
+
if (candNorm === textNorm) {
|
|
185
|
+
emit(audit, { event: "seed_duplicate", id: cand.id, reason: "normalized", similarity: 1 });
|
|
186
|
+
return { kind: "duplicate", seed: cand, reason: "normalized" };
|
|
187
|
+
}
|
|
188
|
+
if (textNorm.length < DEDUP_SIMILAR_MIN_CHARS || candNorm.length < DEDUP_SIMILAR_MIN_CHARS) continue;
|
|
189
|
+
const sim = seedSimilarity(cand.text, text);
|
|
190
|
+
if (sim <= SEED_SIMILARITY_THRESHOLD) continue;
|
|
191
|
+
emit(audit, { event: "seed_duplicate", id: cand.id, reason: "similar", similarity: Number(sim.toFixed(3)) });
|
|
192
|
+
return { kind: "duplicate", seed: cand, reason: "similar" };
|
|
193
|
+
}
|
|
194
|
+
const nowIso = new Date(now).toISOString();
|
|
195
|
+
const ttlDays = input.ttlDaysOverride ?? (policy.seeds.ttlDays[tag] || 14);
|
|
196
|
+
const ttlMs = ttlDays * DAY_MS;
|
|
197
|
+
const sameTopic = activeSeeds(db).filter((s) => s.topic === (input.topic ?? text.slice(0, 24)));
|
|
198
|
+
let seed;
|
|
199
|
+
if (sameTopic.length > 0 && !input.noMerge) {
|
|
200
|
+
const kept = sameTopic[0];
|
|
201
|
+
kept.text = text;
|
|
202
|
+
kept.tag = tag;
|
|
203
|
+
kept.source = source;
|
|
204
|
+
kept.category = normalizeCategory(input.category ?? kept.category ?? category);
|
|
205
|
+
kept.confidence = Math.max(kept.confidence, confidence);
|
|
206
|
+
kept.used = sameTopic.reduce((acc, s) => acc + s.used, 0);
|
|
207
|
+
kept.lastEvidenceAt = [kept.lastEvidenceAt, nowIso, ...sameTopic.map((s) => s.lastEvidenceAt)].reduce((a, b) => parseIso(b) > parseIso(a) ? b : a);
|
|
208
|
+
kept.expiresAt = new Date(Math.max(parseIso(kept.expiresAt), now + ttlMs)).toISOString();
|
|
209
|
+
kept.protected = kept.protected || source === "hand" || source === "profile" && confidence >= 0.7;
|
|
210
|
+
for (const extra of sameTopic.slice(1)) {
|
|
211
|
+
extra.status = "archived";
|
|
212
|
+
extra.retireReason = "completed";
|
|
213
|
+
extra.retiredAt = nowIso;
|
|
214
|
+
emit(audit, { event: "seed_retired", id: extra.id, reason: "completed", via: "merge", into: kept.id });
|
|
215
|
+
}
|
|
216
|
+
seed = kept;
|
|
217
|
+
if (activeSeeds(db).length > policy.seeds.maxActive) {
|
|
218
|
+
const victim = pickEvictionVictim(db, policy, now, normalizeCategory(kept.category));
|
|
219
|
+
if (victim && victim.id !== kept.id) {
|
|
220
|
+
archiveSeed(victim, "pool_cap", now);
|
|
221
|
+
emit(audit, { event: "seed_added", id: kept.id, kind: "merged", tag: kept.tag, source: kept.source, category: kept.category, confidence: kept.confidence, mergedFrom: sameTopic.length - 1 });
|
|
222
|
+
emit(audit, { event: "seed_retired", id: victim.id, reason: "pool_cap", via: "merge", into: kept.id });
|
|
223
|
+
savePool(guard, file, db);
|
|
224
|
+
return { kind: "merged", seed: kept, evicted: victim };
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
emit(audit, { event: "seed_added", id: kept.id, kind: "merged", tag: kept.tag, source: kept.source, category: kept.category, confidence: kept.confidence, mergedFrom: sameTopic.length - 1 });
|
|
228
|
+
savePool(guard, file, db);
|
|
229
|
+
return { kind: "merged", seed: kept };
|
|
230
|
+
}
|
|
231
|
+
const sourceVictims = [];
|
|
232
|
+
const sourceCap = SEED_SOURCE_CAPS[source];
|
|
233
|
+
if (sourceCap !== void 0) {
|
|
234
|
+
while (activeSeeds(db).filter((s) => s.source === source).length >= sourceCap) {
|
|
235
|
+
const victim = pickEvictionVictim(db, policy, now, void 0, source);
|
|
236
|
+
if (!victim) break;
|
|
237
|
+
archiveSeed(victim, "source_cap", now);
|
|
238
|
+
sourceVictims.push(victim);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
let evicted;
|
|
242
|
+
if (sourceVictims.length > 0) {
|
|
243
|
+
evicted = sourceVictims[sourceVictims.length - 1];
|
|
244
|
+
} else {
|
|
245
|
+
const catCount = activeSeeds(db).filter((s) => normalizeCategory(s.category) === category).length;
|
|
246
|
+
if (catCount >= SEED_CATEGORY_CAPS[category]) {
|
|
247
|
+
const victim = pickEvictionVictim(db, policy, now, category);
|
|
248
|
+
if (victim) {
|
|
249
|
+
archiveSeed(victim, "pool_cap", now);
|
|
250
|
+
evicted = victim;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (!evicted && activeSeeds(db).length >= policy.seeds.maxActive) {
|
|
254
|
+
const victim = pickEvictionVictim(db, policy, now, category);
|
|
255
|
+
if (victim) {
|
|
256
|
+
archiveSeed(victim, "pool_cap", now);
|
|
257
|
+
evicted = victim;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
db.seq += 1;
|
|
262
|
+
seed = {
|
|
263
|
+
id: `s${db.seq}`,
|
|
264
|
+
text,
|
|
265
|
+
topic: input.topic ?? text.slice(0, 24),
|
|
266
|
+
tag,
|
|
267
|
+
source,
|
|
268
|
+
category,
|
|
269
|
+
confidence,
|
|
270
|
+
protected: source === "hand" || source === "profile" && confidence >= 0.7,
|
|
271
|
+
used: 0,
|
|
272
|
+
bornAt: nowIso,
|
|
273
|
+
expiresAt: new Date(now + ttlMs).toISOString(),
|
|
274
|
+
...input.ttlDaysOverride !== void 0 ? { ttlDaysOverride: input.ttlDaysOverride } : {},
|
|
275
|
+
lastUsedAt: null,
|
|
276
|
+
lastEvidenceAt: nowIso,
|
|
277
|
+
status: "active"
|
|
278
|
+
};
|
|
279
|
+
db.seeds.push(seed);
|
|
280
|
+
emit(audit, { event: "seed_added", id: seed.id, kind: "added", tag: seed.tag, source: seed.source, category: seed.category, confidence: seed.confidence });
|
|
281
|
+
for (const victim of sourceVictims) {
|
|
282
|
+
emit(audit, { event: "seed_retired", id: victim.id, reason: "source_cap", via: "add", into: seed.id });
|
|
283
|
+
}
|
|
284
|
+
if (evicted && sourceVictims.length === 0) {
|
|
285
|
+
emit(audit, { event: "seed_retired", id: evicted.id, reason: "pool_cap", via: "add", into: seed.id });
|
|
286
|
+
}
|
|
287
|
+
savePool(guard, file, db);
|
|
288
|
+
return { kind: "added", seed, evicted };
|
|
289
|
+
}
|
|
290
|
+
function gcPool(guard, file, policy, now = Date.now(), audit = NO_AUDIT) {
|
|
291
|
+
return update(guard, file, (db) => {
|
|
292
|
+
const report = { consumed: 0, expired: 0, coldBench: 0, activeAfter: 0, archiveTrimmed: 0 };
|
|
293
|
+
for (const s of activeSeeds(db)) {
|
|
294
|
+
const ageMs = now - parseIso(s.bornAt);
|
|
295
|
+
const sinceEvidence = parseIso(s.lastEvidenceAt);
|
|
296
|
+
const sinceUsed = s.lastUsedAt ? parseIso(s.lastUsedAt) : 0;
|
|
297
|
+
if (isConsumed(s, policy, sinceUsed, sinceEvidence)) {
|
|
298
|
+
archiveSeed(s, "consumed", now);
|
|
299
|
+
report.consumed += 1;
|
|
300
|
+
emit(audit, { event: "seed_retired", id: s.id, reason: "consumed", via: "gc", used: s.used });
|
|
301
|
+
} else if (now > parseIso(s.expiresAt)) {
|
|
302
|
+
archiveSeed(s, "expired", now);
|
|
303
|
+
report.expired += 1;
|
|
304
|
+
emit(audit, { event: "seed_retired", id: s.id, reason: "expired", via: "gc", used: s.used });
|
|
305
|
+
} else if (s.used === 0 && ageMs >= policy.seeds.coldBenchDays * DAY_MS) {
|
|
306
|
+
archiveSeed(s, "cold_bench", now);
|
|
307
|
+
report.coldBench += 1;
|
|
308
|
+
emit(audit, { event: "seed_retired", id: s.id, reason: "cold_bench", via: "gc", used: s.used });
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
const archived = db.seeds.filter((s) => s.status === "archived");
|
|
312
|
+
if (archived.length > policy.seeds.archiveCap) {
|
|
313
|
+
const drop = [...archived].sort((a, b) => parseIso(a.retiredAt ?? "") - parseIso(b.retiredAt ?? "")).slice(0, archived.length - policy.seeds.archiveCap);
|
|
314
|
+
const dropped = new Set(drop.map((s) => s.id));
|
|
315
|
+
db.seeds = db.seeds.filter((s) => !dropped.has(s.id));
|
|
316
|
+
report.archiveTrimmed = drop.length;
|
|
317
|
+
emit(audit, { event: "seed_archive_trimmed", count: drop.length, ids: drop.slice(0, 50).map((s) => s.id) });
|
|
318
|
+
}
|
|
319
|
+
report.activeAfter = activeSeeds(db).length;
|
|
320
|
+
savePool(guard, file, db);
|
|
321
|
+
return report;
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
function surfaceSeed(guard, file, policy, id, now = Date.now(), audit = NO_AUDIT) {
|
|
325
|
+
return update(guard, file, (db) => {
|
|
326
|
+
const s = db.seeds.find((x) => x.id === id && x.status === "active");
|
|
327
|
+
if (!s) return null;
|
|
328
|
+
const prevUsedAt = s.lastUsedAt ? parseIso(s.lastUsedAt) : 0;
|
|
329
|
+
s.used += 1;
|
|
330
|
+
s.lastUsedAt = new Date(now).toISOString();
|
|
331
|
+
if (isConsumed(s, policy, prevUsedAt, parseIso(s.lastEvidenceAt))) {
|
|
332
|
+
archiveSeed(s, "consumed", now);
|
|
333
|
+
emit(audit, { event: "seed_retired", id: s.id, reason: "consumed", via: "surface", used: s.used });
|
|
334
|
+
}
|
|
335
|
+
savePool(guard, file, db);
|
|
336
|
+
return s;
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
function archiveSeedById(guard, file, id, reason = "completed", now = Date.now(), audit = NO_AUDIT) {
|
|
340
|
+
return update(guard, file, (db) => {
|
|
341
|
+
const s = db.seeds.find((x) => x.id === id && x.status === "active");
|
|
342
|
+
if (!s) return null;
|
|
343
|
+
archiveSeed(s, reason, now);
|
|
344
|
+
emit(audit, { event: "seed_retired", id: s.id, reason, via: "manual" });
|
|
345
|
+
savePool(guard, file, db);
|
|
346
|
+
return s;
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
function restoreSeed(guard, file, policy, id, now = Date.now(), audit = NO_AUDIT) {
|
|
350
|
+
return update(guard, file, (db) => {
|
|
351
|
+
const s = db.seeds.find((x) => x.id === id && x.status === "archived");
|
|
352
|
+
if (!s) return { ok: false, reason: "archived seed not found" };
|
|
353
|
+
const category = normalizeCategory(s.category);
|
|
354
|
+
const catCap = SEED_CATEGORY_CAPS[category];
|
|
355
|
+
const inCategory = activeSeeds(db).filter((x) => normalizeCategory(x.category) === category).length;
|
|
356
|
+
if (inCategory >= catCap) {
|
|
357
|
+
return { ok: false, reason: `category full (${category}: ${catCap}); archive something in it first` };
|
|
358
|
+
}
|
|
359
|
+
if (activeSeeds(db).length >= policy.seeds.maxActive) {
|
|
360
|
+
return { ok: false, reason: `pool full (${policy.seeds.maxActive}); archive something first` };
|
|
361
|
+
}
|
|
362
|
+
s.status = "active";
|
|
363
|
+
s.retireReason = void 0;
|
|
364
|
+
s.retiredAt = void 0;
|
|
365
|
+
s.bornAt = new Date(now).toISOString();
|
|
366
|
+
const restoreTtlDays = s.ttlDaysOverride ?? (policy.seeds.ttlDays[s.tag] || 14);
|
|
367
|
+
s.expiresAt = new Date(now + restoreTtlDays * DAY_MS).toISOString();
|
|
368
|
+
s.lastEvidenceAt = new Date(now).toISOString();
|
|
369
|
+
emit(audit, { event: "seed_restored", id: s.id });
|
|
370
|
+
savePool(guard, file, db);
|
|
371
|
+
return { ok: true, seed: s };
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
function deleteSeed(guard, file, id, audit = NO_AUDIT) {
|
|
375
|
+
return update(guard, file, (db) => {
|
|
376
|
+
const before = db.seeds.length;
|
|
377
|
+
db.seeds = db.seeds.filter((x) => x.id !== id);
|
|
378
|
+
if (db.seeds.length === before) return false;
|
|
379
|
+
emit(audit, { event: "seed_deleted", id });
|
|
380
|
+
savePool(guard, file, db);
|
|
381
|
+
return true;
|
|
382
|
+
});
|
|
50
383
|
}
|
|
51
384
|
|
|
52
385
|
// src/ledger/ledger.ts
|
|
53
|
-
import
|
|
386
|
+
import path2 from "path";
|
|
54
387
|
import { randomUUID } from "crypto";
|
|
55
|
-
var
|
|
388
|
+
var DAY_MS2 = 864e5;
|
|
56
389
|
var LEDGER_HEADER = "# \u8D26\u672C\n";
|
|
57
390
|
var LEDGER_DONE_HEADER = "# \u8D26\u672C\u5F52\u6863\uFF08\u4E0D\u662F\u8D26\u672C\uFF1A\u8FD9\u91CC\u53EA\u653E\u5DF2\u5B8C\u6210\u7684\u6761\u76EE\uFF09\n\n> \u7531\u5FC3\u8DF3\u81EA\u52A8\u7EF4\u62A4\uFF1A\u8D26\u672C\u91CC\u52FE\u6389\u7684\u6761\u76EE\u4F1A\u4ECE ledger.md \u6458\u51FA\u6765\uFF0C\u539F\u6837\u8FFD\u52A0\u5230\u8FD9\u91CC\uFF1B\u624B\u7F16\u4E0D\u4F1A\u88AB\u8986\u76D6\u3002\n";
|
|
58
391
|
var CORRECTIONS_HEADER = "# \u9519\u9898\u672C\n";
|
|
59
392
|
var CORRECTIONS_DONE_HEADER = "# \u9519\u9898\u672C\u5F52\u6863\uFF08\u4E0D\u662F\u9519\u9898\u672C\uFF1A\u8FD9\u91CC\u53EA\u653E\u5DF2\u590D\u76D8\u7684\u6761\u76EE\uFF09\n\n> \u7531\u5FC3\u8DF3\u81EA\u52A8\u7EF4\u62A4\uFF1A\u9519\u9898\u672C\u91CC\u52FE\u6389\u7684\u6761\u76EE\u4F1A\u4ECE corrections.md \u6458\u51FA\u6765\uFF0C\u539F\u6837\u8FFD\u52A0\u5230\u8FD9\u91CC\uFF1B\u624B\u7F16\u4E0D\u4F1A\u88AB\u8986\u76D6\u3002\n";
|
|
60
393
|
function ledgerFilePath(dataDir) {
|
|
61
|
-
return
|
|
394
|
+
return path2.join(dataDir, "ledger.md");
|
|
62
395
|
}
|
|
63
396
|
function ledgerDoneFilePath(dataDir) {
|
|
64
|
-
return
|
|
397
|
+
return path2.join(dataDir, "ledger-done.md");
|
|
65
398
|
}
|
|
66
399
|
function correctionsFilePath(dataDir) {
|
|
67
|
-
return
|
|
400
|
+
return path2.join(dataDir, "corrections.md");
|
|
68
401
|
}
|
|
69
402
|
function correctionsDoneFilePath(dataDir) {
|
|
70
|
-
return
|
|
403
|
+
return path2.join(dataDir, "corrections-done.md");
|
|
71
404
|
}
|
|
72
405
|
function doneFilePathFor(file) {
|
|
73
|
-
const ext =
|
|
406
|
+
const ext = path2.extname(file);
|
|
74
407
|
return `${file.slice(0, file.length - ext.length)}-done${ext}`;
|
|
75
408
|
}
|
|
76
409
|
function doneHeaderFor(file) {
|
|
77
|
-
return
|
|
410
|
+
return path2.basename(file).toLowerCase().startsWith("corrections") ? CORRECTIONS_DONE_HEADER : LEDGER_DONE_HEADER;
|
|
78
411
|
}
|
|
79
412
|
var LINE_RE = /^- \[(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2})\]\[(open|done)\]\[#([^\]\s]+)\] (.*)$/;
|
|
80
413
|
function renderEntry(e) {
|
|
@@ -220,7 +553,7 @@ function markDone(guard, file, key, now = Date.now(), doneFile = doneFilePathFor
|
|
|
220
553
|
// Hand-written ids may carry personal text; only generated 6-hex ids
|
|
221
554
|
// are audited (the archive file name locates the row regardless).
|
|
222
555
|
...HEX_ID_RE.test(target.id) ? { id: target.id } : {},
|
|
223
|
-
archive:
|
|
556
|
+
archive: path2.basename(doneFile),
|
|
224
557
|
// The matched archive row's OWN stamp locates the swallowed line; the
|
|
225
558
|
// done stamp below is this retry's fresh minute, useless for finding
|
|
226
559
|
// the row it deduped against (N16② review 低4).
|
|
@@ -239,20 +572,20 @@ function scanPending(guard, file, now = Date.now()) {
|
|
|
239
572
|
return entries.filter((e) => e.status === "open").sort((a, b) => a.date < b.date ? -1 : 1);
|
|
240
573
|
}
|
|
241
574
|
function pendingOlderThan(guard, file, days, now = Date.now()) {
|
|
242
|
-
const cutoff = localDayKey(new Date(now - days *
|
|
575
|
+
const cutoff = localDayKey(new Date(now - days * DAY_MS2));
|
|
243
576
|
return scanPending(guard, file, now).filter((e) => e.date <= cutoff);
|
|
244
577
|
}
|
|
245
578
|
|
|
246
579
|
// src/browse/preference.ts
|
|
247
|
-
import
|
|
248
|
-
var
|
|
580
|
+
import path3 from "path";
|
|
581
|
+
var DAY_MS3 = 864e5;
|
|
249
582
|
var W_MIN = 0.5;
|
|
250
583
|
var W_MAX = 2;
|
|
251
584
|
var SMOOTH_ADOPTED = 1;
|
|
252
585
|
var SMOOTH_DELIVERED = 4;
|
|
253
586
|
var DECAY_HALF_LIFE_DAYS = 30;
|
|
254
587
|
function preferenceFilePath(dataDir) {
|
|
255
|
-
return
|
|
588
|
+
return path3.join(dataDir, "preference.json");
|
|
256
589
|
}
|
|
257
590
|
function num(v) {
|
|
258
591
|
const n = Number(v);
|
|
@@ -305,8 +638,8 @@ function recordDelivery(guard, file, opts) {
|
|
|
305
638
|
return state;
|
|
306
639
|
}
|
|
307
640
|
function decayed(t, now) {
|
|
308
|
-
const dAgeDays = Math.max(0, now - t.lastDeliveredAt) /
|
|
309
|
-
const aAgeDays = Math.max(0, now - t.lastAdoptedAt) /
|
|
641
|
+
const dAgeDays = Math.max(0, now - t.lastDeliveredAt) / DAY_MS3;
|
|
642
|
+
const aAgeDays = Math.max(0, now - t.lastAdoptedAt) / DAY_MS3;
|
|
310
643
|
const dHalf = Math.pow(0.5, dAgeDays / DECAY_HALF_LIFE_DAYS);
|
|
311
644
|
const aHalf = Math.pow(0.5, aAgeDays / DECAY_HALF_LIFE_DAYS);
|
|
312
645
|
return { delivered: t.delivered * dHalf, adopted: t.adopted * aHalf };
|
|
@@ -322,12 +655,12 @@ function preferenceWeight(state, topic, now) {
|
|
|
322
655
|
|
|
323
656
|
// src/browse/browse.ts
|
|
324
657
|
import fs from "fs";
|
|
325
|
-
import
|
|
658
|
+
import path4 from "path";
|
|
326
659
|
function emptyBrowseState() {
|
|
327
660
|
return { wander: { focusHistory: {}, focusCount: {}, last_wander_at: 0, refillCount: {}, sessionCount: {} } };
|
|
328
661
|
}
|
|
329
662
|
function browseStatePath(paths) {
|
|
330
|
-
return
|
|
663
|
+
return path4.join(paths.dataDir, "browse.json");
|
|
331
664
|
}
|
|
332
665
|
function readJsonFile(file, fallback) {
|
|
333
666
|
try {
|
|
@@ -337,9 +670,9 @@ function readJsonFile(file, fallback) {
|
|
|
337
670
|
}
|
|
338
671
|
}
|
|
339
672
|
function loadInterests(paths) {
|
|
340
|
-
const userPath =
|
|
673
|
+
const userPath = path4.join(paths.settingsDir, "interests.json");
|
|
341
674
|
if (fs.existsSync(userPath)) return readJsonFile(userPath, { interests: [], _schedule: {} });
|
|
342
|
-
return readJsonFile(
|
|
675
|
+
return readJsonFile(path4.join(paths.configDir, "interests.json"), { interests: [], _schedule: {} });
|
|
343
676
|
}
|
|
344
677
|
function normalizeBrowseState(raw) {
|
|
345
678
|
const out = emptyBrowseState();
|
|
@@ -484,7 +817,7 @@ function browseStatus(guard, paths) {
|
|
|
484
817
|
}
|
|
485
818
|
|
|
486
819
|
// src/notify/notify.ts
|
|
487
|
-
import
|
|
820
|
+
import path5 from "path";
|
|
488
821
|
|
|
489
822
|
// src/core/ps.ts
|
|
490
823
|
import { execFile } from "child_process";
|
|
@@ -529,7 +862,7 @@ function runPowerShellFile(script, args, timeoutMs, signal) {
|
|
|
529
862
|
|
|
530
863
|
// src/notify/notify.ts
|
|
531
864
|
async function runNotify(paths, args) {
|
|
532
|
-
const r = await runPowerShellFile(
|
|
865
|
+
const r = await runPowerShellFile(path5.join(paths.assetsDir, "notify.ps1"), args, 2e4);
|
|
533
866
|
return { status: r.status, out: `${r.stdout}${r.stderr}`.trim() };
|
|
534
867
|
}
|
|
535
868
|
async function ensureRegistered(paths) {
|
|
@@ -549,13 +882,13 @@ async function sendWeeklyReadyHint(paths) {
|
|
|
549
882
|
|
|
550
883
|
// src/weekly/report.ts
|
|
551
884
|
import fs2 from "fs";
|
|
552
|
-
import
|
|
885
|
+
import path6 from "path";
|
|
553
886
|
var WEEKLY_INTERVAL_MS = 7 * 864e5;
|
|
554
887
|
function weeklyDirPath(dataDir) {
|
|
555
|
-
return
|
|
888
|
+
return path6.join(dataDir, "weekly");
|
|
556
889
|
}
|
|
557
890
|
function stateFilePath(dataDir) {
|
|
558
|
-
return
|
|
891
|
+
return path6.join(weeklyDirPath(dataDir), "state.json");
|
|
559
892
|
}
|
|
560
893
|
function reportFileName(endIso) {
|
|
561
894
|
return `report-${endIso.slice(0, 10)}.json`;
|
|
@@ -575,9 +908,9 @@ function lastWeeklyGeneratedAt(guard, dataDir) {
|
|
|
575
908
|
} catch {
|
|
576
909
|
}
|
|
577
910
|
try {
|
|
578
|
-
appendAuditLine(
|
|
911
|
+
appendAuditLine(path6.join(workspace().logsDir, "heartbeat.jsonl"), {
|
|
579
912
|
event: "weekly_anchor_corrupt",
|
|
580
|
-
file:
|
|
913
|
+
file: path6.basename(file),
|
|
581
914
|
bytes: Buffer.byteLength(raw)
|
|
582
915
|
});
|
|
583
916
|
} catch {
|
|
@@ -601,7 +934,7 @@ function ensureWeeklyAnchor(guard, dataDir, now = Date.now()) {
|
|
|
601
934
|
function saveWeeklyReport(guard, dataDir, report) {
|
|
602
935
|
const dir = weeklyDirPath(dataDir);
|
|
603
936
|
fs2.mkdirSync(dir, { recursive: true });
|
|
604
|
-
const file =
|
|
937
|
+
const file = path6.join(dir, reportFileName(report.end));
|
|
605
938
|
saveEncryptedText(guard, file, JSON.stringify(report, null, 1));
|
|
606
939
|
atomicWriteJsonSync(stateFilePath(dataDir), { lastGeneratedAt: Date.parse(report.generatedAt) });
|
|
607
940
|
return file;
|
|
@@ -625,7 +958,7 @@ function pruneWeeklyReports(guard, dataDir, keep) {
|
|
|
625
958
|
const removed = [];
|
|
626
959
|
for (const meta of listWeeklyReports(guard, dataDir).slice(keep)) {
|
|
627
960
|
try {
|
|
628
|
-
fs2.rmSync(guard.assert(
|
|
961
|
+
fs2.rmSync(guard.assert(path6.join(weeklyDirPath(dataDir), meta.file)));
|
|
629
962
|
removed.push(meta.file);
|
|
630
963
|
} catch {
|
|
631
964
|
}
|
|
@@ -635,7 +968,7 @@ function pruneWeeklyReports(guard, dataDir, keep) {
|
|
|
635
968
|
function readWeeklyReport(guard, dataDir, file) {
|
|
636
969
|
if (!/^report-\d{4}-\d{2}-\d{2}\.json$/.test(file)) return null;
|
|
637
970
|
try {
|
|
638
|
-
const raw = loadEncryptedText(guard,
|
|
971
|
+
const raw = loadEncryptedText(guard, path6.join(weeklyDirPath(dataDir), file));
|
|
639
972
|
if (!raw) return null;
|
|
640
973
|
const rep = JSON.parse(raw);
|
|
641
974
|
if (typeof rep.text !== "string") return null;
|
|
@@ -697,9 +1030,16 @@ function renderTemplateReport(facts) {
|
|
|
697
1030
|
}
|
|
698
1031
|
|
|
699
1032
|
export {
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
1033
|
+
seedsFilePath,
|
|
1034
|
+
loadPool,
|
|
1035
|
+
activeSeeds,
|
|
1036
|
+
archivedSeeds,
|
|
1037
|
+
addSeed,
|
|
1038
|
+
gcPool,
|
|
1039
|
+
surfaceSeed,
|
|
1040
|
+
archiveSeedById,
|
|
1041
|
+
restoreSeed,
|
|
1042
|
+
deleteSeed,
|
|
703
1043
|
LEDGER_HEADER,
|
|
704
1044
|
LEDGER_DONE_HEADER,
|
|
705
1045
|
CORRECTIONS_HEADER,
|
|
@@ -738,4 +1078,4 @@ export {
|
|
|
738
1078
|
buildWeeklyPrompt,
|
|
739
1079
|
renderTemplateReport
|
|
740
1080
|
};
|
|
741
|
-
//# sourceMappingURL=chunk-
|
|
1081
|
+
//# sourceMappingURL=chunk-MHRAPKES.js.map
|