acp-kernel 0.0.39 → 0.0.41
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/dist/chunk-DPH62BGM.js +70 -0
- package/dist/chunk-DPH62BGM.js.map +1 -0
- package/dist/compress.d.ts.map +1 -1
- package/dist/index.js +19 -59
- package/dist/index.js.map +1 -1
- package/dist/persist/index.d.ts +19 -0
- package/dist/persist/index.d.ts.map +1 -0
- package/dist/persist/index.js +367 -0
- package/dist/persist/index.js.map +1 -0
- package/dist/persist/state-merge.d.ts +11 -0
- package/dist/persist/state-merge.d.ts.map +1 -0
- package/dist/persist/store.d.ts +161 -0
- package/dist/persist/store.d.ts.map +1 -0
- package/package.json +5 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Persistence subpath: crash-safe JSON state storage for downstream hosts.
|
|
3
|
+
*
|
|
4
|
+
* The kernel core is pure (no fs). This subpath is the ONLY kernel module
|
|
5
|
+
* that touches the filesystem, so hosts that never persist stay fs-free by
|
|
6
|
+
* not importing `acp-kernel/persist`.
|
|
7
|
+
*
|
|
8
|
+
* Division of responsibility:
|
|
9
|
+
* - store owns MECHANISM: atomic writes, rename retries, debounce,
|
|
10
|
+
* serialization, discovery, corrupt-file tolerance
|
|
11
|
+
* - downstream owns POLICY: storage location (dir is a constructor
|
|
12
|
+
* argument — the kernel has no default path), schema version, payload
|
|
13
|
+
* shape, validation, and lifecycle (when to save, what to load)
|
|
14
|
+
* - the store never deletes files; cleanup is a downstream decision
|
|
15
|
+
*/
|
|
16
|
+
export { StateStore, flatFileNameFor } from "./store.js";
|
|
17
|
+
export type { PersistedEnvelope, PersistLogger, StateStoreOptions } from "./store.js";
|
|
18
|
+
export { mergeCompressionState } from "./state-merge.js";
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/persist/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AACzD,YAAY,EAAE,iBAAiB,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AACtF,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createInitialState
|
|
3
|
+
} from "../chunk-DPH62BGM.js";
|
|
4
|
+
|
|
5
|
+
// src/persist/store.ts
|
|
6
|
+
import { createHash } from "crypto";
|
|
7
|
+
import fs from "fs";
|
|
8
|
+
import fsp from "fs/promises";
|
|
9
|
+
import path from "path";
|
|
10
|
+
var StateStore = class {
|
|
11
|
+
enabled;
|
|
12
|
+
dir;
|
|
13
|
+
version;
|
|
14
|
+
debounceMs;
|
|
15
|
+
log;
|
|
16
|
+
legacyFn;
|
|
17
|
+
relPathFn;
|
|
18
|
+
validateFn;
|
|
19
|
+
timers = /* @__PURE__ */ new Map();
|
|
20
|
+
pending = /* @__PURE__ */ new Map();
|
|
21
|
+
writeChains = /* @__PURE__ */ new Map();
|
|
22
|
+
/** id → absolute path, populated by writes and loadAll. */
|
|
23
|
+
discovered = /* @__PURE__ */ new Map();
|
|
24
|
+
/** Monotonic counter for unique temp filenames within a process. */
|
|
25
|
+
tmpSeq = 0;
|
|
26
|
+
constructor(opts) {
|
|
27
|
+
this.dir = opts.dir;
|
|
28
|
+
this.version = opts.version;
|
|
29
|
+
this.debounceMs = opts.debounceMs ?? 500;
|
|
30
|
+
this.enabled = opts.enabled ?? true;
|
|
31
|
+
this.log = opts.log ?? ((_level, _msg) => {
|
|
32
|
+
});
|
|
33
|
+
this.relPathFn = opts.relPath;
|
|
34
|
+
this.legacyFn = opts.legacy;
|
|
35
|
+
this.validateFn = opts.validate ?? defaultValidate;
|
|
36
|
+
}
|
|
37
|
+
/** Debounced save. Coalesces bursts; the builder runs at write time, so
|
|
38
|
+
* the freshest state is always persisted. Never throws. */
|
|
39
|
+
scheduleSave(id, build) {
|
|
40
|
+
if (!this.enabled) return;
|
|
41
|
+
this.pending.set(id, build);
|
|
42
|
+
const existing = this.timers.get(id);
|
|
43
|
+
if (existing) return;
|
|
44
|
+
const timer = setTimeout(() => {
|
|
45
|
+
this.timers.delete(id);
|
|
46
|
+
const builder = this.pending.get(id);
|
|
47
|
+
this.pending.delete(id);
|
|
48
|
+
if (!builder) return;
|
|
49
|
+
void this.writeNow(id, builder).catch(() => {
|
|
50
|
+
});
|
|
51
|
+
}, this.debounceMs);
|
|
52
|
+
timer.unref?.();
|
|
53
|
+
this.timers.set(id, timer);
|
|
54
|
+
}
|
|
55
|
+
/** Immediate save, serialized per id. Rejects on write failure; a
|
|
56
|
+
* failing write never breaks the chain for the next one. */
|
|
57
|
+
async writeNow(id, build) {
|
|
58
|
+
if (!this.enabled) return;
|
|
59
|
+
const prev = this.writeChains.get(id) ?? Promise.resolve();
|
|
60
|
+
const next = prev.catch(() => {
|
|
61
|
+
}).then(() => this.writeInner(id, build));
|
|
62
|
+
this.writeChains.set(id, next);
|
|
63
|
+
next.finally(() => {
|
|
64
|
+
if (this.writeChains.get(id) === next) this.writeChains.delete(id);
|
|
65
|
+
}).catch(() => {
|
|
66
|
+
});
|
|
67
|
+
return next;
|
|
68
|
+
}
|
|
69
|
+
/** Synchronous flush for one id. Used where the caller cannot await
|
|
70
|
+
* (memory eviction, sync shutdown paths). Cancels any pending debounce
|
|
71
|
+
* timer. Returns true on success, false on failure — callers that use
|
|
72
|
+
* the result to drop in-memory state must NOT drop it on failure. */
|
|
73
|
+
flushSync(id, build) {
|
|
74
|
+
if (!this.enabled) return true;
|
|
75
|
+
const timer = this.timers.get(id);
|
|
76
|
+
if (timer) {
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
this.timers.delete(id);
|
|
79
|
+
this.pending.delete(id);
|
|
80
|
+
}
|
|
81
|
+
let payload;
|
|
82
|
+
try {
|
|
83
|
+
payload = build();
|
|
84
|
+
} catch (e) {
|
|
85
|
+
this.log("error", `[persist] builder failed for ${id}: ${errText(e)}`);
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
const file = this.resolvePath(id, payload);
|
|
89
|
+
const data = JSON.stringify(this.envelope(id, payload));
|
|
90
|
+
try {
|
|
91
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
92
|
+
} catch (e) {
|
|
93
|
+
this.log("warn", `[persist] could not create dir ${path.dirname(file)}: ${errText(e)}`);
|
|
94
|
+
}
|
|
95
|
+
const tmp = this.tempPath(file);
|
|
96
|
+
try {
|
|
97
|
+
fs.writeFileSync(tmp, data, "utf8");
|
|
98
|
+
let lastErr;
|
|
99
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
100
|
+
try {
|
|
101
|
+
fs.renameSync(tmp, file);
|
|
102
|
+
lastErr = void 0;
|
|
103
|
+
break;
|
|
104
|
+
} catch (e) {
|
|
105
|
+
lastErr = e;
|
|
106
|
+
const code = e.code;
|
|
107
|
+
if (code !== "EPERM" && code !== "EBUSY" && code !== "EACCES") break;
|
|
108
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20 * (attempt + 1));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (lastErr) throw lastErr;
|
|
112
|
+
return true;
|
|
113
|
+
} catch (e) {
|
|
114
|
+
this.log("error", `[persist] flushSync failed for ${id}: ${errText(e)}`);
|
|
115
|
+
try {
|
|
116
|
+
fs.unlinkSync(tmp);
|
|
117
|
+
} catch {
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** Load one record. Checks the discovered path (from a prior
|
|
123
|
+
* write/loadAll), an optional relative-path hint, and the flat default
|
|
124
|
+
* name. Returns null when absent, disabled, corrupt, or rejected by
|
|
125
|
+
* validate. The hint covers namespaced records the store has not
|
|
126
|
+
* discovered (e.g. an evicted session re-requested with its meta,
|
|
127
|
+
* where the path depends on data the store cannot reconstruct from
|
|
128
|
+
* the id alone). */
|
|
129
|
+
loadSync(id, hint) {
|
|
130
|
+
if (!this.enabled) return null;
|
|
131
|
+
const candidates = [
|
|
132
|
+
this.discovered.get(id),
|
|
133
|
+
hint ? path.join(this.dir, hint) : void 0,
|
|
134
|
+
path.join(this.dir, flatFileNameFor(id))
|
|
135
|
+
];
|
|
136
|
+
for (const file of candidates) {
|
|
137
|
+
if (!file) continue;
|
|
138
|
+
const envelope = this.readEnvelope(file);
|
|
139
|
+
if (envelope && envelope.id === id) return envelope;
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
/** Load every record under dir. Populates the discovery map (enables
|
|
144
|
+
* loadSync for namespaced relPaths). Skips corrupt files, `.tmp-*`
|
|
145
|
+
* orphans, and records whose filename does not match their id — one
|
|
146
|
+
* bad file never blocks boot. Never throws. */
|
|
147
|
+
async loadAll() {
|
|
148
|
+
const out = /* @__PURE__ */ new Map();
|
|
149
|
+
if (!this.enabled) return out;
|
|
150
|
+
const files = await this.walkJsonFiles(this.dir);
|
|
151
|
+
for (const file of files) {
|
|
152
|
+
const envelope = this.readEnvelope(file);
|
|
153
|
+
if (!envelope) continue;
|
|
154
|
+
const expected = path.basename(this.relPathOf(envelope.id, envelope.payload)) === path.basename(file) || flatFileNameFor(envelope.id) === path.basename(file);
|
|
155
|
+
if (!expected) {
|
|
156
|
+
this.log("warn", `[persist] skipping ${rel(file, this.dir)}: filename does not match record id`);
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
this.discovered.set(envelope.id, file);
|
|
160
|
+
out.set(envelope.id, envelope);
|
|
161
|
+
}
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
/** Whether a debounced write is pending for an id. */
|
|
165
|
+
hasPending(id) {
|
|
166
|
+
return this.timers.has(id);
|
|
167
|
+
}
|
|
168
|
+
/** Ids with a pending debounced write. */
|
|
169
|
+
pendingIds() {
|
|
170
|
+
return [...this.timers.keys()];
|
|
171
|
+
}
|
|
172
|
+
/** Flush every pending debounced write immediately, then drain in-flight
|
|
173
|
+
* writes. For graceful shutdown (SIGTERM/SIGINT). Never rejects. */
|
|
174
|
+
async flushAll() {
|
|
175
|
+
const ids = this.pendingIds();
|
|
176
|
+
for (const timer of this.timers.values()) clearTimeout(timer);
|
|
177
|
+
this.timers.clear();
|
|
178
|
+
const builders = /* @__PURE__ */ new Map();
|
|
179
|
+
for (const id of ids) {
|
|
180
|
+
const build = this.pending.get(id);
|
|
181
|
+
if (build) builders.set(id, build);
|
|
182
|
+
this.pending.delete(id);
|
|
183
|
+
}
|
|
184
|
+
await Promise.all(
|
|
185
|
+
[...builders.entries()].map(
|
|
186
|
+
([id, build]) => this.writeNow(id, build).catch((e) => {
|
|
187
|
+
this.log("error", `[persist] shutdown flush failed for ${id}: ${errText(e)}`);
|
|
188
|
+
})
|
|
189
|
+
)
|
|
190
|
+
);
|
|
191
|
+
await Promise.allSettled([...this.writeChains.values()]);
|
|
192
|
+
}
|
|
193
|
+
/** Cancel all pending debounced writes without flushing (tests). */
|
|
194
|
+
cancelAll() {
|
|
195
|
+
for (const timer of this.timers.values()) clearTimeout(timer);
|
|
196
|
+
this.timers.clear();
|
|
197
|
+
this.pending.clear();
|
|
198
|
+
}
|
|
199
|
+
async writeInner(id, build) {
|
|
200
|
+
let payload;
|
|
201
|
+
try {
|
|
202
|
+
payload = build();
|
|
203
|
+
} catch (e) {
|
|
204
|
+
this.log("error", `[persist] builder failed for ${id}: ${errText(e)}`);
|
|
205
|
+
throw e;
|
|
206
|
+
}
|
|
207
|
+
const file = this.resolvePath(id, payload);
|
|
208
|
+
try {
|
|
209
|
+
await fsp.mkdir(path.dirname(file), { recursive: true });
|
|
210
|
+
} catch (e) {
|
|
211
|
+
this.log("warn", `[persist] could not create dir ${path.dirname(file)}: ${errText(e)}`);
|
|
212
|
+
}
|
|
213
|
+
const tmp = this.tempPath(file);
|
|
214
|
+
try {
|
|
215
|
+
await fsp.writeFile(tmp, JSON.stringify(this.envelope(id, payload)), "utf8");
|
|
216
|
+
await renameWithRetry(tmp, file);
|
|
217
|
+
this.discovered.set(id, file);
|
|
218
|
+
} catch (e) {
|
|
219
|
+
await fsp.unlink(tmp).catch(() => {
|
|
220
|
+
});
|
|
221
|
+
this.log("error", `[persist] write failed for ${id}: ${errText(e)}`);
|
|
222
|
+
throw e;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
envelope(id, payload) {
|
|
226
|
+
return { version: this.version, savedAt: Date.now(), id, payload };
|
|
227
|
+
}
|
|
228
|
+
/** Absolute path for a record: custom relPath (guarded against path
|
|
229
|
+
* escape) or the flat hash default. */
|
|
230
|
+
resolvePath(id, payload) {
|
|
231
|
+
return path.join(this.dir, this.relPathOf(id, payload));
|
|
232
|
+
}
|
|
233
|
+
relPathOf(id, payload) {
|
|
234
|
+
const custom = this.relPathFn?.(id, payload);
|
|
235
|
+
if (!custom) return flatFileNameFor(id);
|
|
236
|
+
const rel2 = path.normalize(custom);
|
|
237
|
+
if (path.isAbsolute(rel2) || rel2.split(/[\\/]+/).includes("..")) {
|
|
238
|
+
this.log("warn", `[persist] relPath for ${id} escapes dir; using flat name`);
|
|
239
|
+
return flatFileNameFor(id);
|
|
240
|
+
}
|
|
241
|
+
return rel2;
|
|
242
|
+
}
|
|
243
|
+
/** Unique temp file next to the destination (same dir ⇒ same volume ⇒
|
|
244
|
+
* rename is atomic). Prefixed `.tmp-` so loadAll skips orphans. */
|
|
245
|
+
tempPath(dest) {
|
|
246
|
+
const seq = this.tmpSeq++;
|
|
247
|
+
return path.join(path.dirname(dest), `.tmp-${path.basename(dest, ".json")}-${process.pid}-${seq}`);
|
|
248
|
+
}
|
|
249
|
+
/** Parse + validate one file. Corrupt or invalid records return null
|
|
250
|
+
* (logged) instead of throwing — load paths must never block boot. */
|
|
251
|
+
readEnvelope(file) {
|
|
252
|
+
let parsed;
|
|
253
|
+
try {
|
|
254
|
+
parsed = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
255
|
+
} catch (e) {
|
|
256
|
+
if (e.code !== "ENOENT") {
|
|
257
|
+
this.log("warn", `[persist] skipping corrupt file ${rel(file, this.dir)}: ${errText(e)}`);
|
|
258
|
+
}
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
if (isEnvelopeLike(parsed)) {
|
|
262
|
+
if (!this.validateFn(parsed)) {
|
|
263
|
+
this.log("warn", `[persist] skipping invalid record ${rel(file, this.dir)}`);
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
return parsed;
|
|
267
|
+
}
|
|
268
|
+
const adopted = this.adoptLegacy(parsed);
|
|
269
|
+
if (adopted) return adopted;
|
|
270
|
+
this.log("warn", `[persist] skipping invalid record ${rel(file, this.dir)}`);
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
/** Wrap a legacy (pre-envelope) record as an envelope via the `legacy`
|
|
274
|
+
* hook, then validate the adopted payload like any other. */
|
|
275
|
+
adoptLegacy(parsed) {
|
|
276
|
+
const adoption = this.legacyFn ? this.legacyFn(parsed) : null;
|
|
277
|
+
if (!adoption || typeof adoption.id !== "string" || adoption.id.length === 0 || adoption.payload == null) {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
const source = parsed;
|
|
281
|
+
const envelope = {
|
|
282
|
+
version: adoption.version ?? source.version ?? this.version,
|
|
283
|
+
savedAt: adoption.savedAt ?? source.savedAt ?? 0,
|
|
284
|
+
id: adoption.id,
|
|
285
|
+
payload: adoption.payload
|
|
286
|
+
};
|
|
287
|
+
if (!this.validateFn(envelope)) return null;
|
|
288
|
+
return envelope;
|
|
289
|
+
}
|
|
290
|
+
/** Iterative recursive walk (no readdir-recursive dependency), skipping
|
|
291
|
+
* `.tmp-*` names and non-.json files. */
|
|
292
|
+
async walkJsonFiles(root) {
|
|
293
|
+
const out = [];
|
|
294
|
+
const queue = [root];
|
|
295
|
+
while (queue.length > 0) {
|
|
296
|
+
const dir = queue.pop();
|
|
297
|
+
let dirents;
|
|
298
|
+
try {
|
|
299
|
+
dirents = await fsp.readdir(dir, { withFileTypes: true });
|
|
300
|
+
} catch {
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
for (const d of dirents) {
|
|
304
|
+
if (d.name.startsWith(".tmp-")) continue;
|
|
305
|
+
const full = path.join(dir, d.name);
|
|
306
|
+
if (d.isDirectory()) queue.push(full);
|
|
307
|
+
else if (d.isFile() && d.name.endsWith(".json")) out.push(full);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
function defaultValidate(envelope) {
|
|
314
|
+
return typeof envelope.id === "string" && envelope.id.length > 0 && envelope.payload != null;
|
|
315
|
+
}
|
|
316
|
+
function isEnvelopeLike(value) {
|
|
317
|
+
if (!value || typeof value !== "object") return false;
|
|
318
|
+
const v = value;
|
|
319
|
+
return typeof v.id === "string" && "payload" in v;
|
|
320
|
+
}
|
|
321
|
+
function flatFileNameFor(id) {
|
|
322
|
+
return createHash("sha256").update(id, "utf8").digest("hex").slice(0, 24) + ".json";
|
|
323
|
+
}
|
|
324
|
+
async function renameWithRetry(src, dest) {
|
|
325
|
+
let lastErr;
|
|
326
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
327
|
+
try {
|
|
328
|
+
await fsp.rename(src, dest);
|
|
329
|
+
return;
|
|
330
|
+
} catch (e) {
|
|
331
|
+
lastErr = e;
|
|
332
|
+
const code = e.code;
|
|
333
|
+
if (code !== "EPERM" && code !== "EBUSY" && code !== "EACCES") throw e;
|
|
334
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
335
|
+
setTimeout(resolve, 20 * (attempt + 1));
|
|
336
|
+
await promise;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
throw lastErr;
|
|
340
|
+
}
|
|
341
|
+
function errText(e) {
|
|
342
|
+
return e instanceof Error ? e.message : String(e);
|
|
343
|
+
}
|
|
344
|
+
function rel(p, base) {
|
|
345
|
+
const r = path.relative(base, p);
|
|
346
|
+
return r && !r.startsWith("..") ? r : p;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// src/persist/state-merge.ts
|
|
350
|
+
function mergeCompressionState(parsed) {
|
|
351
|
+
const fresh = createInitialState();
|
|
352
|
+
return {
|
|
353
|
+
blocks: parsed.blocks ?? fresh.blocks,
|
|
354
|
+
messageRefs: parsed.messageRefs ?? fresh.messageRefs,
|
|
355
|
+
nudge: { ...fresh.nudge, ...parsed.nudge ?? {} },
|
|
356
|
+
stats: { ...fresh.stats, ...parsed.stats ?? {} },
|
|
357
|
+
nextBlockId: parsed.nextBlockId ?? fresh.nextBlockId,
|
|
358
|
+
nextRunId: parsed.nextRunId ?? fresh.nextRunId,
|
|
359
|
+
tokenSnapshot: parsed.tokenSnapshot ?? fresh.tokenSnapshot
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
export {
|
|
363
|
+
StateStore,
|
|
364
|
+
flatFileNameFor,
|
|
365
|
+
mergeCompressionState
|
|
366
|
+
};
|
|
367
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/persist/store.ts","../../src/persist/state-merge.ts"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport fs from \"node:fs\";\nimport fsp from \"node:fs/promises\";\nimport path from \"node:path\";\n\n/** Logger callback. Downstream routes it wherever it wants (file, stderr). */\nexport type PersistLogger = (level: \"info\" | \"warn\" | \"error\", msg: string) => void;\n\n/**\n * On-disk record shape. The store owns the envelope (version stamp, save\n * time, id) so atomicity and discovery can rely on it; the payload schema\n * is entirely downstream-owned. `payload` must be JSON-serializable.\n */\nexport interface PersistedEnvelope<T> {\n version: number;\n savedAt: number;\n id: string;\n payload: T;\n}\n\n/** A legacy (pre-envelope) record adopted on load. `version`/`savedAt`\n * preserve the source record's own stamps when present. */\nexport interface LegacyAdoption<T> {\n id: string;\n payload: T;\n version?: number;\n savedAt?: number;\n}\n\nexport interface StateStoreOptions<T> {\n /**\n * Storage root. The kernel deliberately has NO default location: the\n * downstream decides where state lives (CLI dir, XDG data dir, plugin\n * dir, temp dir in tests). All records live under this directory.\n */\n dir: string;\n /**\n * Schema version stamped into every envelope. Owned by the downstream;\n * bump it when the payload shape changes. The store itself never\n * rejects a record over its version — migration policy belongs to the\n * reader.\n */\n version: number;\n /** Debounce window for scheduleSave, ms. Default 500. */\n debounceMs?: number;\n /** Default true. When false, all writes are silent no-ops and loads\n * return empty results. */\n enabled?: boolean;\n log?: PersistLogger;\n /**\n * Relative path (under `dir`) for a record, possibly namespaced into\n * subdirectories (e.g. `openai/host-hash.json`). Default: flat\n * `<sha256(id)[:24]>.json`.\n *\n * Because the path may depend on payload fields the store only learns\n * at write time, single-record loads resolve namespaced files only\n * after loadAll() has discovered them (or the store itself wrote\n * them). The flat default name is always checked as a fallback, so\n * downstreams using a custom relPath should call loadAll() at boot.\n */\n relPath?: (id: string, payload: T) => string;\n /**\n * Adopt records written by an older, pre-envelope schema. Receives the\n * parsed JSON of any file that failed the envelope-shape check; return\n * an adoption to load it as an envelope, or null to skip it. Adopted\n * records are re-persisted in the current envelope format on the next\n * dirty write — files migrate organically, and old files keep loading\n * (same policy billion-context's proxy used for its v1→v3 migration).\n */\n legacy?: (parsed: unknown) => LegacyAdoption<T> | null;\n /**\n * Payload validation on load. Return false to skip a record (foreign\n * schema, corrupt content). Default: envelope-shape check only\n * (string id, non-null payload).\n */\n validate?: (envelope: PersistedEnvelope<T>) => boolean;\n}\n\n/**\n * Crash-safe, debounce-coalescing JSON state store. Mechanism only — lifted\n * from billion-context's proxy SessionStore and generalized:\n *\n * - atomic writes: temp file + rename, so a crash mid-write never leaves a\n * truncated record (readers see either the old or the new file)\n * - rename retries on Windows transient locks (EPERM/EBUSY/EACCES)\n * - per-id serialization so concurrent writeNow calls never interleave\n * temp-file names or reorders writes\n * - debounced scheduleSave coalesces bursts into one write; the record is\n * built at WRITE time from a builder, so late mutations are picked up\n * - loadAll skips `.tmp-*` orphans, corrupt JSON, and records whose\n * filename does not match their id — one bad file never blocks boot\n *\n * The store never deletes files. Session cleanup is a downstream policy\n * decision (kernel position: persisted state should not be deleted\n * opportunistically).\n */\nexport class StateStore<T> {\n readonly enabled: boolean;\n private readonly dir: string;\n private readonly version: number;\n private readonly debounceMs: number;\n private readonly log: PersistLogger;\n private readonly legacyFn?: (parsed: unknown) => LegacyAdoption<T> | null;\n private readonly relPathFn?: (id: string, payload: T) => string;\n private readonly validateFn: (envelope: PersistedEnvelope<T>) => boolean;\n private readonly timers = new Map<string, NodeJS.Timeout>();\n private readonly pending = new Map<string, () => T>();\n private readonly writeChains = new Map<string, Promise<void>>();\n /** id → absolute path, populated by writes and loadAll. */\n private readonly discovered = new Map<string, string>();\n /** Monotonic counter for unique temp filenames within a process. */\n private tmpSeq = 0;\n\n constructor(opts: StateStoreOptions<T>) {\n this.dir = opts.dir;\n this.version = opts.version;\n this.debounceMs = opts.debounceMs ?? 500;\n this.enabled = opts.enabled ?? true;\n this.log = opts.log ?? ((_level, _msg) => {});\n this.relPathFn = opts.relPath;\n this.legacyFn = opts.legacy;\n this.validateFn = opts.validate ?? defaultValidate;\n }\n\n /** Debounced save. Coalesces bursts; the builder runs at write time, so\n * the freshest state is always persisted. Never throws. */\n scheduleSave(id: string, build: () => T): void {\n if (!this.enabled) return;\n this.pending.set(id, build);\n const existing = this.timers.get(id);\n if (existing) return;\n const timer = setTimeout(() => {\n this.timers.delete(id);\n const builder = this.pending.get(id);\n this.pending.delete(id);\n if (!builder) return;\n // Errors are logged inside writeInner; swallowing here keeps a\n // failed timer write from becoming an unhandledRejection.\n void this.writeNow(id, builder).catch(() => {});\n }, this.debounceMs);\n timer.unref?.();\n this.timers.set(id, timer);\n }\n\n /** Immediate save, serialized per id. Rejects on write failure; a\n * failing write never breaks the chain for the next one. */\n async writeNow(id: string, build: () => T): Promise<void> {\n if (!this.enabled) return;\n // The previous promise may reject (disk full, EPERM) — catch so our\n // chain doesn't break, then run our own write.\n const prev = this.writeChains.get(id) ?? Promise.resolve();\n const next = prev.catch(() => {}).then(() => this.writeInner(id, build));\n this.writeChains.set(id, next);\n // Clean up the chain entry once settled so the Map doesn't grow. The\n // .catch() is load-bearing: .finally() returns a derived promise that\n // nobody else holds — when the chain rejects it would surface as an\n // unhandledRejection and crash the host on default Node settings.\n next\n .finally(() => {\n if (this.writeChains.get(id) === next) this.writeChains.delete(id);\n })\n .catch(() => {});\n return next;\n }\n\n /** Synchronous flush for one id. Used where the caller cannot await\n * (memory eviction, sync shutdown paths). Cancels any pending debounce\n * timer. Returns true on success, false on failure — callers that use\n * the result to drop in-memory state must NOT drop it on failure. */\n flushSync(id: string, build: () => T): boolean {\n if (!this.enabled) return true;\n const timer = this.timers.get(id);\n if (timer) {\n clearTimeout(timer);\n this.timers.delete(id);\n this.pending.delete(id);\n }\n let payload: T;\n try {\n payload = build();\n } catch (e) {\n this.log(\"error\", `[persist] builder failed for ${id}: ${errText(e)}`);\n return false;\n }\n const file = this.resolvePath(id, payload);\n const data = JSON.stringify(this.envelope(id, payload));\n try {\n fs.mkdirSync(path.dirname(file), { recursive: true });\n } catch (e) {\n this.log(\"warn\", `[persist] could not create dir ${path.dirname(file)}: ${errText(e)}`);\n }\n const tmp = this.tempPath(file);\n try {\n fs.writeFileSync(tmp, data, \"utf8\");\n // renameSync can throw EPERM/EBUSY on Windows when the dest is\n // briefly held (AV scanner, indexer, SMB). Retry briefly —\n // transient locks usually release within ms.\n let lastErr: unknown;\n for (let attempt = 0; attempt < 3; attempt++) {\n try {\n fs.renameSync(tmp, file);\n lastErr = undefined;\n break;\n } catch (e) {\n lastErr = e;\n const code = (e as NodeJS.ErrnoException).code;\n if (code !== \"EPERM\" && code !== \"EBUSY\" && code !== \"EACCES\") break;\n // brief sync backoff (Atomics.wait is the sync sleep)\n Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20 * (attempt + 1));\n }\n }\n if (lastErr) throw lastErr;\n return true;\n } catch (e) {\n this.log(\"error\", `[persist] flushSync failed for ${id}: ${errText(e)}`);\n try {\n fs.unlinkSync(tmp);\n } catch {\n // best-effort cleanup of the orphan temp\n }\n return false;\n }\n }\n\n /** Load one record. Checks the discovered path (from a prior\n * write/loadAll), an optional relative-path hint, and the flat default\n * name. Returns null when absent, disabled, corrupt, or rejected by\n * validate. The hint covers namespaced records the store has not\n * discovered (e.g. an evicted session re-requested with its meta,\n * where the path depends on data the store cannot reconstruct from\n * the id alone). */\n loadSync(id: string, hint?: string): PersistedEnvelope<T> | null {\n if (!this.enabled) return null;\n const candidates = [\n this.discovered.get(id),\n hint ? path.join(this.dir, hint) : undefined,\n path.join(this.dir, flatFileNameFor(id)),\n ];\n for (const file of candidates) {\n if (!file) continue;\n const envelope = this.readEnvelope(file);\n if (envelope && envelope.id === id) return envelope;\n }\n return null;\n }\n\n /** Load every record under dir. Populates the discovery map (enables\n * loadSync for namespaced relPaths). Skips corrupt files, `.tmp-*`\n * orphans, and records whose filename does not match their id — one\n * bad file never blocks boot. Never throws. */\n async loadAll(): Promise<Map<string, PersistedEnvelope<T>>> {\n const out = new Map<string, PersistedEnvelope<T>>();\n if (!this.enabled) return out;\n const files = await this.walkJsonFiles(this.dir);\n for (const file of files) {\n const envelope = this.readEnvelope(file);\n if (!envelope) continue;\n const expected =\n path.basename(this.relPathOf(envelope.id, envelope.payload)) === path.basename(file) ||\n flatFileNameFor(envelope.id) === path.basename(file);\n if (!expected) {\n this.log(\"warn\", `[persist] skipping ${rel(file, this.dir)}: filename does not match record id`);\n continue;\n }\n this.discovered.set(envelope.id, file);\n out.set(envelope.id, envelope);\n }\n return out;\n }\n\n /** Whether a debounced write is pending for an id. */\n hasPending(id: string): boolean {\n return this.timers.has(id);\n }\n\n /** Ids with a pending debounced write. */\n pendingIds(): string[] {\n return [...this.timers.keys()];\n }\n\n /** Flush every pending debounced write immediately, then drain in-flight\n * writes. For graceful shutdown (SIGTERM/SIGINT). Never rejects. */\n async flushAll(): Promise<void> {\n const ids = this.pendingIds();\n for (const timer of this.timers.values()) clearTimeout(timer);\n this.timers.clear();\n const builders = new Map<string, () => T>();\n for (const id of ids) {\n const build = this.pending.get(id);\n if (build) builders.set(id, build);\n this.pending.delete(id);\n }\n await Promise.all(\n [...builders.entries()].map(([id, build]) =>\n this.writeNow(id, build).catch((e) => {\n this.log(\"error\", `[persist] shutdown flush failed for ${id}: ${errText(e)}`);\n }),\n ),\n );\n // Drain writes whose timer fired earlier but are still mid-flight.\n await Promise.allSettled([...this.writeChains.values()]);\n }\n\n /** Cancel all pending debounced writes without flushing (tests). */\n cancelAll(): void {\n for (const timer of this.timers.values()) clearTimeout(timer);\n this.timers.clear();\n this.pending.clear();\n }\n\n private async writeInner(id: string, build: () => T): Promise<void> {\n let payload: T;\n try {\n payload = build();\n } catch (e) {\n this.log(\"error\", `[persist] builder failed for ${id}: ${errText(e)}`);\n throw e;\n }\n const file = this.resolvePath(id, payload);\n try {\n await fsp.mkdir(path.dirname(file), { recursive: true });\n } catch (e) {\n this.log(\"warn\", `[persist] could not create dir ${path.dirname(file)}: ${errText(e)}`);\n }\n const tmp = this.tempPath(file);\n try {\n await fsp.writeFile(tmp, JSON.stringify(this.envelope(id, payload)), \"utf8\");\n await renameWithRetry(tmp, file);\n this.discovered.set(id, file);\n } catch (e) {\n // Wrap so a failure cleans up the .tmp orphan instead of leaving\n // it for the next write to collide with.\n await fsp.unlink(tmp).catch(() => {});\n this.log(\"error\", `[persist] write failed for ${id}: ${errText(e)}`);\n throw e;\n }\n }\n\n private envelope(id: string, payload: T): PersistedEnvelope<T> {\n return { version: this.version, savedAt: Date.now(), id, payload };\n }\n\n /** Absolute path for a record: custom relPath (guarded against path\n * escape) or the flat hash default. */\n private resolvePath(id: string, payload: T): string {\n return path.join(this.dir, this.relPathOf(id, payload));\n }\n\n private relPathOf(id: string, payload: T): string {\n const custom = this.relPathFn?.(id, payload);\n if (!custom) return flatFileNameFor(id);\n const rel = path.normalize(custom);\n if (path.isAbsolute(rel) || rel.split(/[\\\\/]+/).includes(\"..\")) {\n this.log(\"warn\", `[persist] relPath for ${id} escapes dir; using flat name`);\n return flatFileNameFor(id);\n }\n return rel;\n }\n\n /** Unique temp file next to the destination (same dir ⇒ same volume ⇒\n * rename is atomic). Prefixed `.tmp-` so loadAll skips orphans. */\n private tempPath(dest: string): string {\n const seq = this.tmpSeq++;\n return path.join(path.dirname(dest), `.tmp-${path.basename(dest, \".json\")}-${process.pid}-${seq}`);\n }\n\n /** Parse + validate one file. Corrupt or invalid records return null\n * (logged) instead of throwing — load paths must never block boot. */\n private readEnvelope(file: string): PersistedEnvelope<T> | null {\n let parsed: unknown;\n try {\n parsed = JSON.parse(fs.readFileSync(file, \"utf8\"));\n } catch (e) {\n // A missing candidate path is an expected miss (loadSync probes);\n // only genuinely unreadable/corrupt files get logged.\n if ((e as NodeJS.ErrnoException).code !== \"ENOENT\") {\n this.log(\"warn\", `[persist] skipping corrupt file ${rel(file, this.dir)}: ${errText(e)}`);\n }\n return null;\n }\n if (isEnvelopeLike(parsed)) {\n if (!this.validateFn(parsed as PersistedEnvelope<T>)) {\n this.log(\"warn\", `[persist] skipping invalid record ${rel(file, this.dir)}`);\n return null;\n }\n return parsed as PersistedEnvelope<T>;\n }\n const adopted = this.adoptLegacy(parsed);\n if (adopted) return adopted;\n this.log(\"warn\", `[persist] skipping invalid record ${rel(file, this.dir)}`);\n return null;\n }\n\n /** Wrap a legacy (pre-envelope) record as an envelope via the `legacy`\n * hook, then validate the adopted payload like any other. */\n private adoptLegacy(parsed: unknown): PersistedEnvelope<T> | null {\n const adoption = this.legacyFn ? this.legacyFn(parsed) : null;\n if (!adoption || typeof adoption.id !== \"string\" || adoption.id.length === 0 || adoption.payload == null) {\n return null;\n }\n const source = parsed as Partial<PersistedEnvelope<T>>;\n const envelope: PersistedEnvelope<T> = {\n version: adoption.version ?? source.version ?? this.version,\n savedAt: adoption.savedAt ?? source.savedAt ?? 0,\n id: adoption.id,\n payload: adoption.payload,\n };\n if (!this.validateFn(envelope)) return null;\n return envelope;\n }\n\n /** Iterative recursive walk (no readdir-recursive dependency), skipping\n * `.tmp-*` names and non-.json files. */\n private async walkJsonFiles(root: string): Promise<string[]> {\n const out: string[] = [];\n const queue: string[] = [root];\n while (queue.length > 0) {\n const dir = queue.pop()!;\n let dirents: fs.Dirent[];\n try {\n dirents = await fsp.readdir(dir, { withFileTypes: true });\n } catch {\n continue; // missing dir → no records yet\n }\n for (const d of dirents) {\n if (d.name.startsWith(\".tmp-\")) continue;\n const full = path.join(dir, d.name);\n if (d.isDirectory()) queue.push(full);\n else if (d.isFile() && d.name.endsWith(\".json\")) out.push(full);\n }\n }\n return out;\n }\n}\n\nfunction defaultValidate<T>(envelope: PersistedEnvelope<T>): boolean {\n return typeof envelope.id === \"string\" && envelope.id.length > 0 && envelope.payload != null;\n}\n\nfunction isEnvelopeLike(value: unknown): value is PersistedEnvelope<unknown> {\n if (!value || typeof value !== \"object\") return false;\n const v = value as Partial<PersistedEnvelope<unknown>>;\n return typeof v.id === \"string\" && \"payload\" in v;\n}\n\n/** Deterministic flat filename: `<sha256(id)[:24]>.json`. Truncated hash —\n * 96 bits keeps collisions unreachable for realistic id counts, and short\n * names stay greppable. */\nexport function flatFileNameFor(id: string): string {\n return createHash(\"sha256\").update(id, \"utf8\").digest(\"hex\").slice(0, 24) + \".json\";\n}\n\n/** fs.rename with brief retries on Windows transient locks (EPERM/EBUSY/\n * EACCES from AV scan, search indexer, SMB). A short delay + retry almost\n * always succeeds; other errors are real and rethrown immediately. */\nasync function renameWithRetry(src: string, dest: string): Promise<void> {\n let lastErr: unknown;\n for (let attempt = 0; attempt < 3; attempt++) {\n try {\n await fsp.rename(src, dest);\n return;\n } catch (e) {\n lastErr = e;\n const code = (e as NodeJS.ErrnoException).code;\n if (code !== \"EPERM\" && code !== \"EBUSY\" && code !== \"EACCES\") throw e;\n const { promise, resolve } = Promise.withResolvers<void>();\n setTimeout(resolve, 20 * (attempt + 1));\n await promise;\n }\n }\n throw lastErr;\n}\n\nfunction errText(e: unknown): string {\n return e instanceof Error ? e.message : String(e);\n}\n\n\nfunction rel(p: string, base: string): string {\n const r = path.relative(base, p);\n return r && !r.startsWith(\"..\") ? r : p;\n}\n","import type { CompressionState } from \"../types.js\";\nimport { createInitialState } from \"../state.js\";\n\n/**\n * Forward-compat: merge a parsed state with a fresh one so fields added in\n * later kernel versions get sane defaults instead of `undefined`. Nested\n * groups (nudge, stats) are shallow-merged per field so older snapshots\n * keep their values while newer counters default in.\n *\n * Lifted verbatim in behavior from billion-context's proxy mergeState.\n */\nexport function mergeCompressionState(parsed: CompressionState): CompressionState {\n const fresh = createInitialState();\n return {\n blocks: parsed.blocks ?? fresh.blocks,\n messageRefs: parsed.messageRefs ?? fresh.messageRefs,\n nudge: { ...fresh.nudge, ...(parsed.nudge ?? {}) },\n stats: { ...fresh.stats, ...(parsed.stats ?? {}) },\n nextBlockId: parsed.nextBlockId ?? fresh.nextBlockId,\n nextRunId: parsed.nextRunId ?? fresh.nextRunId,\n tokenSnapshot: parsed.tokenSnapshot ?? fresh.tokenSnapshot,\n };\n}\n"],"mappings":";;;;;AAAA,SAAS,kBAAkB;AAC3B,OAAO,QAAQ;AACf,OAAO,SAAS;AAChB,OAAO,UAAU;AA6FV,IAAM,aAAN,MAAoB;AAAA,EACd;AAAA,EACQ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,SAAS,oBAAI,IAA4B;AAAA,EACzC,UAAU,oBAAI,IAAqB;AAAA,EACnC,cAAc,oBAAI,IAA2B;AAAA;AAAA,EAE7C,aAAa,oBAAI,IAAoB;AAAA;AAAA,EAE9C,SAAS;AAAA,EAEjB,YAAY,MAA4B;AACpC,SAAK,MAAM,KAAK;AAChB,SAAK,UAAU,KAAK;AACpB,SAAK,aAAa,KAAK,cAAc;AACrC,SAAK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AAM;AAAA,IACV;AAAA,EACJ;AACA,QAAM;AACV;AAEA,SAAS,QAAQ,GAAoB;AACjC,SAAO,aAAa,QAAQ,EAAE,UAAU,OAAO,CAAC;AACpD;AAGA,SAAS,IAAI,GAAW,MAAsB;AAC1C,QAAM,IAAI,KAAK,SAAS,MAAM,CAAC;AAC/B,SAAO,KAAK,CAAC,EAAE,WAAW,IAAI,IAAI,IAAI;AAC1C;;;ACtdO,SAAS,sBAAsB,QAA4C;AAChF,QAAM,QAAQ,mBAAmB;AACjC,SAAO;AAAA,IACL,QAAQ,OAAO,UAAU,MAAM;AAAA,IAC/B,aAAa,OAAO,eAAe,MAAM;AAAA,IACzC,OAAO,EAAE,GAAG,MAAM,OAAO,GAAI,OAAO,SAAS,CAAC,EAAG;AAAA,IACjD,OAAO,EAAE,GAAG,MAAM,OAAO,GAAI,OAAO,SAAS,CAAC,EAAG;AAAA,IACjD,aAAa,OAAO,eAAe,MAAM;AAAA,IACzC,WAAW,OAAO,aAAa,MAAM;AAAA,IACrC,eAAe,OAAO,iBAAiB,MAAM;AAAA,EAC/C;AACF;","names":["rel"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { CompressionState } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Forward-compat: merge a parsed state with a fresh one so fields added in
|
|
4
|
+
* later kernel versions get sane defaults instead of `undefined`. Nested
|
|
5
|
+
* groups (nudge, stats) are shallow-merged per field so older snapshots
|
|
6
|
+
* keep their values while newer counters default in.
|
|
7
|
+
*
|
|
8
|
+
* Lifted verbatim in behavior from billion-context's proxy mergeState.
|
|
9
|
+
*/
|
|
10
|
+
export declare function mergeCompressionState(parsed: CompressionState): CompressionState;
|
|
11
|
+
//# sourceMappingURL=state-merge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state-merge.d.ts","sourceRoot":"","sources":["../../src/persist/state-merge.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAGpD;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,gBAAgB,GAAG,gBAAgB,CAWhF"}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/** Logger callback. Downstream routes it wherever it wants (file, stderr). */
|
|
2
|
+
export type PersistLogger = (level: "info" | "warn" | "error", msg: string) => void;
|
|
3
|
+
/**
|
|
4
|
+
* On-disk record shape. The store owns the envelope (version stamp, save
|
|
5
|
+
* time, id) so atomicity and discovery can rely on it; the payload schema
|
|
6
|
+
* is entirely downstream-owned. `payload` must be JSON-serializable.
|
|
7
|
+
*/
|
|
8
|
+
export interface PersistedEnvelope<T> {
|
|
9
|
+
version: number;
|
|
10
|
+
savedAt: number;
|
|
11
|
+
id: string;
|
|
12
|
+
payload: T;
|
|
13
|
+
}
|
|
14
|
+
/** A legacy (pre-envelope) record adopted on load. `version`/`savedAt`
|
|
15
|
+
* preserve the source record's own stamps when present. */
|
|
16
|
+
export interface LegacyAdoption<T> {
|
|
17
|
+
id: string;
|
|
18
|
+
payload: T;
|
|
19
|
+
version?: number;
|
|
20
|
+
savedAt?: number;
|
|
21
|
+
}
|
|
22
|
+
export interface StateStoreOptions<T> {
|
|
23
|
+
/**
|
|
24
|
+
* Storage root. The kernel deliberately has NO default location: the
|
|
25
|
+
* downstream decides where state lives (CLI dir, XDG data dir, plugin
|
|
26
|
+
* dir, temp dir in tests). All records live under this directory.
|
|
27
|
+
*/
|
|
28
|
+
dir: string;
|
|
29
|
+
/**
|
|
30
|
+
* Schema version stamped into every envelope. Owned by the downstream;
|
|
31
|
+
* bump it when the payload shape changes. The store itself never
|
|
32
|
+
* rejects a record over its version — migration policy belongs to the
|
|
33
|
+
* reader.
|
|
34
|
+
*/
|
|
35
|
+
version: number;
|
|
36
|
+
/** Debounce window for scheduleSave, ms. Default 500. */
|
|
37
|
+
debounceMs?: number;
|
|
38
|
+
/** Default true. When false, all writes are silent no-ops and loads
|
|
39
|
+
* return empty results. */
|
|
40
|
+
enabled?: boolean;
|
|
41
|
+
log?: PersistLogger;
|
|
42
|
+
/**
|
|
43
|
+
* Relative path (under `dir`) for a record, possibly namespaced into
|
|
44
|
+
* subdirectories (e.g. `openai/host-hash.json`). Default: flat
|
|
45
|
+
* `<sha256(id)[:24]>.json`.
|
|
46
|
+
*
|
|
47
|
+
* Because the path may depend on payload fields the store only learns
|
|
48
|
+
* at write time, single-record loads resolve namespaced files only
|
|
49
|
+
* after loadAll() has discovered them (or the store itself wrote
|
|
50
|
+
* them). The flat default name is always checked as a fallback, so
|
|
51
|
+
* downstreams using a custom relPath should call loadAll() at boot.
|
|
52
|
+
*/
|
|
53
|
+
relPath?: (id: string, payload: T) => string;
|
|
54
|
+
/**
|
|
55
|
+
* Adopt records written by an older, pre-envelope schema. Receives the
|
|
56
|
+
* parsed JSON of any file that failed the envelope-shape check; return
|
|
57
|
+
* an adoption to load it as an envelope, or null to skip it. Adopted
|
|
58
|
+
* records are re-persisted in the current envelope format on the next
|
|
59
|
+
* dirty write — files migrate organically, and old files keep loading
|
|
60
|
+
* (same policy billion-context's proxy used for its v1→v3 migration).
|
|
61
|
+
*/
|
|
62
|
+
legacy?: (parsed: unknown) => LegacyAdoption<T> | null;
|
|
63
|
+
/**
|
|
64
|
+
* Payload validation on load. Return false to skip a record (foreign
|
|
65
|
+
* schema, corrupt content). Default: envelope-shape check only
|
|
66
|
+
* (string id, non-null payload).
|
|
67
|
+
*/
|
|
68
|
+
validate?: (envelope: PersistedEnvelope<T>) => boolean;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Crash-safe, debounce-coalescing JSON state store. Mechanism only — lifted
|
|
72
|
+
* from billion-context's proxy SessionStore and generalized:
|
|
73
|
+
*
|
|
74
|
+
* - atomic writes: temp file + rename, so a crash mid-write never leaves a
|
|
75
|
+
* truncated record (readers see either the old or the new file)
|
|
76
|
+
* - rename retries on Windows transient locks (EPERM/EBUSY/EACCES)
|
|
77
|
+
* - per-id serialization so concurrent writeNow calls never interleave
|
|
78
|
+
* temp-file names or reorders writes
|
|
79
|
+
* - debounced scheduleSave coalesces bursts into one write; the record is
|
|
80
|
+
* built at WRITE time from a builder, so late mutations are picked up
|
|
81
|
+
* - loadAll skips `.tmp-*` orphans, corrupt JSON, and records whose
|
|
82
|
+
* filename does not match their id — one bad file never blocks boot
|
|
83
|
+
*
|
|
84
|
+
* The store never deletes files. Session cleanup is a downstream policy
|
|
85
|
+
* decision (kernel position: persisted state should not be deleted
|
|
86
|
+
* opportunistically).
|
|
87
|
+
*/
|
|
88
|
+
export declare class StateStore<T> {
|
|
89
|
+
readonly enabled: boolean;
|
|
90
|
+
private readonly dir;
|
|
91
|
+
private readonly version;
|
|
92
|
+
private readonly debounceMs;
|
|
93
|
+
private readonly log;
|
|
94
|
+
private readonly legacyFn?;
|
|
95
|
+
private readonly relPathFn?;
|
|
96
|
+
private readonly validateFn;
|
|
97
|
+
private readonly timers;
|
|
98
|
+
private readonly pending;
|
|
99
|
+
private readonly writeChains;
|
|
100
|
+
/** id → absolute path, populated by writes and loadAll. */
|
|
101
|
+
private readonly discovered;
|
|
102
|
+
/** Monotonic counter for unique temp filenames within a process. */
|
|
103
|
+
private tmpSeq;
|
|
104
|
+
constructor(opts: StateStoreOptions<T>);
|
|
105
|
+
/** Debounced save. Coalesces bursts; the builder runs at write time, so
|
|
106
|
+
* the freshest state is always persisted. Never throws. */
|
|
107
|
+
scheduleSave(id: string, build: () => T): void;
|
|
108
|
+
/** Immediate save, serialized per id. Rejects on write failure; a
|
|
109
|
+
* failing write never breaks the chain for the next one. */
|
|
110
|
+
writeNow(id: string, build: () => T): Promise<void>;
|
|
111
|
+
/** Synchronous flush for one id. Used where the caller cannot await
|
|
112
|
+
* (memory eviction, sync shutdown paths). Cancels any pending debounce
|
|
113
|
+
* timer. Returns true on success, false on failure — callers that use
|
|
114
|
+
* the result to drop in-memory state must NOT drop it on failure. */
|
|
115
|
+
flushSync(id: string, build: () => T): boolean;
|
|
116
|
+
/** Load one record. Checks the discovered path (from a prior
|
|
117
|
+
* write/loadAll), an optional relative-path hint, and the flat default
|
|
118
|
+
* name. Returns null when absent, disabled, corrupt, or rejected by
|
|
119
|
+
* validate. The hint covers namespaced records the store has not
|
|
120
|
+
* discovered (e.g. an evicted session re-requested with its meta,
|
|
121
|
+
* where the path depends on data the store cannot reconstruct from
|
|
122
|
+
* the id alone). */
|
|
123
|
+
loadSync(id: string, hint?: string): PersistedEnvelope<T> | null;
|
|
124
|
+
/** Load every record under dir. Populates the discovery map (enables
|
|
125
|
+
* loadSync for namespaced relPaths). Skips corrupt files, `.tmp-*`
|
|
126
|
+
* orphans, and records whose filename does not match their id — one
|
|
127
|
+
* bad file never blocks boot. Never throws. */
|
|
128
|
+
loadAll(): Promise<Map<string, PersistedEnvelope<T>>>;
|
|
129
|
+
/** Whether a debounced write is pending for an id. */
|
|
130
|
+
hasPending(id: string): boolean;
|
|
131
|
+
/** Ids with a pending debounced write. */
|
|
132
|
+
pendingIds(): string[];
|
|
133
|
+
/** Flush every pending debounced write immediately, then drain in-flight
|
|
134
|
+
* writes. For graceful shutdown (SIGTERM/SIGINT). Never rejects. */
|
|
135
|
+
flushAll(): Promise<void>;
|
|
136
|
+
/** Cancel all pending debounced writes without flushing (tests). */
|
|
137
|
+
cancelAll(): void;
|
|
138
|
+
private writeInner;
|
|
139
|
+
private envelope;
|
|
140
|
+
/** Absolute path for a record: custom relPath (guarded against path
|
|
141
|
+
* escape) or the flat hash default. */
|
|
142
|
+
private resolvePath;
|
|
143
|
+
private relPathOf;
|
|
144
|
+
/** Unique temp file next to the destination (same dir ⇒ same volume ⇒
|
|
145
|
+
* rename is atomic). Prefixed `.tmp-` so loadAll skips orphans. */
|
|
146
|
+
private tempPath;
|
|
147
|
+
/** Parse + validate one file. Corrupt or invalid records return null
|
|
148
|
+
* (logged) instead of throwing — load paths must never block boot. */
|
|
149
|
+
private readEnvelope;
|
|
150
|
+
/** Wrap a legacy (pre-envelope) record as an envelope via the `legacy`
|
|
151
|
+
* hook, then validate the adopted payload like any other. */
|
|
152
|
+
private adoptLegacy;
|
|
153
|
+
/** Iterative recursive walk (no readdir-recursive dependency), skipping
|
|
154
|
+
* `.tmp-*` names and non-.json files. */
|
|
155
|
+
private walkJsonFiles;
|
|
156
|
+
}
|
|
157
|
+
/** Deterministic flat filename: `<sha256(id)[:24]>.json`. Truncated hash —
|
|
158
|
+
* 96 bits keeps collisions unreachable for realistic id counts, and short
|
|
159
|
+
* names stay greppable. */
|
|
160
|
+
export declare function flatFileNameFor(id: string): string;
|
|
161
|
+
//# sourceMappingURL=store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/persist/store.ts"],"names":[],"mappings":"AAKA,8EAA8E;AAC9E,MAAM,MAAM,aAAa,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;AAEpF;;;;GAIG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,CAAC,CAAC;CACd;AAED;2DAC2D;AAC3D,MAAM,WAAW,cAAc,CAAC,CAAC;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,CAAC,CAAC;IACX,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,iBAAiB,CAAC,CAAC;IAChC;;;;OAIG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;;;;OAKG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB,yDAAyD;IACzD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;gCAC4B;IAC5B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB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,EAAE,KAAK,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAmBzD;;;0EAGsE;IACtE,SAAS,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,GAAG,OAAO;IAuD9C;;;;;;yBAMqB;IACrB,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAAC,CAAC,CAAC,GAAG,IAAI;IAehE;;;oDAGgD;IAC1C,OAAO,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC;IAoB3D,sDAAsD;IACtD,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAI/B,0CAA0C;IAC1C,UAAU,IAAI,MAAM,EAAE;IAItB;yEACqE;IAC/D,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAqB/B,oEAAoE;IACpE,SAAS,IAAI,IAAI;YAMH,UAAU;IA4BxB,OAAO,CAAC,QAAQ;IAIhB;4CACwC;IACxC,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,SAAS;IAWjB;wEACoE;IACpE,OAAO,CAAC,QAAQ;IAKhB;2EACuE;IACvE,OAAO,CAAC,YAAY;IAyBpB;kEAC8D;IAC9D,OAAO,CAAC,WAAW;IAgBnB;8CAC0C;YAC5B,aAAa;CAoB9B;AAYD;;4BAE4B;AAC5B,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAElD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "acp-kernel",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.41",
|
|
4
4
|
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "ranxianglei",
|
|
@@ -37,6 +37,10 @@
|
|
|
37
37
|
"./panel": {
|
|
38
38
|
"types": "./dist/panel/index.d.ts",
|
|
39
39
|
"import": "./dist/panel/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./persist": {
|
|
42
|
+
"types": "./dist/persist/index.d.ts",
|
|
43
|
+
"import": "./dist/persist/index.js"
|
|
40
44
|
}
|
|
41
45
|
},
|
|
42
46
|
"files": [
|