@nextclaw/kernel 0.19.3 → 0.19.4
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/app/worker/index.d.ts +2 -2
- package/dist/app/worker/index.js +3 -2
- package/dist/{automatic-update-check.utils-C6lQfErN.d.ts → automatic-update-check.utils-8_3eYuwY.d.ts} +1 -1
- package/dist/{automatic-update-check.utils-C6lQfErN.d.ts.map → automatic-update-check.utils-8_3eYuwY.d.ts.map} +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +651 -1510
- package/dist/index.js.map +1 -1
- package/dist/ncp-agent-session-journal-entry.utils-Dh0Ck_oW.js +112 -0
- package/dist/ncp-agent-session-journal-entry.utils-Dh0Ck_oW.js.map +1 -0
- package/dist/ncp-agent-session-journal.utils-B7a_PKCq.js +1457 -0
- package/dist/ncp-agent-session-journal.utils-B7a_PKCq.js.map +1 -0
- package/dist/ncp-agent-session-message-projection-tail.store-5sjmoFxv.js +117 -0
- package/dist/ncp-agent-session-message-projection-tail.store-5sjmoFxv.js.map +1 -0
- package/dist/{nextclaw-harness.manager-GK_qgrlk.d.ts → nextclaw-harness.manager-j0JHST4g.d.ts} +348 -80
- package/dist/nextclaw-harness.manager-j0JHST4g.d.ts.map +1 -0
- package/dist/{nextclaw-harness.manager-DHzYOORP.js → nextclaw-harness.manager-mJToWeCB.js} +1871 -1657
- package/dist/nextclaw-harness.manager-mJToWeCB.js.map +1 -0
- package/dist/session-journal-delta-coalescer.utils-CI-N-Zpj.js +25 -0
- package/dist/session-journal-delta-coalescer.utils-CI-N-Zpj.js.map +1 -0
- package/dist/session-journal-maintenance.store-DrZMlaxf.js +621 -0
- package/dist/session-journal-maintenance.store-DrZMlaxf.js.map +1 -0
- package/dist/session-journal-stream.utils-CupSA3YS.js +207 -0
- package/dist/session-journal-stream.utils-CupSA3YS.js.map +1 -0
- package/dist/types/update-contract.types.d.ts +1 -1
- package/dist/types/update-contract.types.js +1 -1
- package/dist/{update-manifest.types-CBp2VTM1.js → update-manifest.types-DUocefbs.js} +1 -1
- package/dist/{update-manifest.types-CBp2VTM1.js.map → update-manifest.types-DUocefbs.js.map} +1 -1
- package/dist/{update-manifest.types-C_60CVvw.d.ts → update-manifest.types-Dijq24Om.d.ts} +1 -1
- package/dist/{update-manifest.types-C_60CVvw.d.ts.map → update-manifest.types-Dijq24Om.d.ts.map} +1 -1
- package/dist/utils/automatic-update-check.utils.d.ts +1 -1
- package/package.json +13 -10
- package/dist/nextclaw-harness.manager-DHzYOORP.js.map +0 -1
- package/dist/nextclaw-harness.manager-GK_qgrlk.d.ts.map +0 -1
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { NcpEventType } from "@nextclaw/ncp";
|
|
2
|
+
//#region src/utils/session-journal-delta-coalescer.utils.ts
|
|
3
|
+
const SESSION_JOURNAL_DELTA_MAX_BYTES = 16 * 1024;
|
|
4
|
+
function isSessionJournalDelta(event) {
|
|
5
|
+
return event.type === NcpEventType.MessageTextDelta || event.type === NcpEventType.MessageReasoningDelta || event.type === NcpEventType.MessageToolCallArgsDelta;
|
|
6
|
+
}
|
|
7
|
+
function canCoalesceSessionJournalDeltas(left, right) {
|
|
8
|
+
if (!isSessionJournalDelta(left) || !isSessionJournalDelta(right) || left.type !== right.type) return false;
|
|
9
|
+
const { delta: _leftDelta, ...leftPayload } = left.payload;
|
|
10
|
+
const { delta: _rightDelta, ...rightPayload } = right.payload;
|
|
11
|
+
return JSON.stringify(leftPayload) === JSON.stringify(rightPayload);
|
|
12
|
+
}
|
|
13
|
+
function coalesceSessionJournalDeltas(left, right) {
|
|
14
|
+
return {
|
|
15
|
+
...left,
|
|
16
|
+
payload: {
|
|
17
|
+
...left.payload,
|
|
18
|
+
delta: left.payload.delta + right.payload.delta
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { isSessionJournalDelta as i, canCoalesceSessionJournalDeltas as n, coalesceSessionJournalDeltas as r, SESSION_JOURNAL_DELTA_MAX_BYTES as t };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=session-journal-delta-coalescer.utils-CI-N-Zpj.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-journal-delta-coalescer.utils-CI-N-Zpj.js","names":[],"sources":["../src/utils/session-journal-delta-coalescer.utils.ts"],"sourcesContent":["import { NcpEventType, type NcpEndpointEvent } from \"@nextclaw/ncp\";\n\nexport const SESSION_JOURNAL_DELTA_MAX_BYTES = 16 * 1024;\nexport const SESSION_JOURNAL_DELTA_MAX_AGE_MS = 100;\n\ntype DeltaEvent = Extract<NcpEndpointEvent, {\n type: NcpEventType.MessageTextDelta | NcpEventType.MessageReasoningDelta | NcpEventType.MessageToolCallArgsDelta;\n}>;\n\nexport function isSessionJournalDelta(event: NcpEndpointEvent): event is DeltaEvent {\n return event.type === NcpEventType.MessageTextDelta ||\n event.type === NcpEventType.MessageReasoningDelta ||\n event.type === NcpEventType.MessageToolCallArgsDelta;\n}\n\nexport function canCoalesceSessionJournalDeltas(left: NcpEndpointEvent, right: NcpEndpointEvent): boolean {\n if (!isSessionJournalDelta(left) || !isSessionJournalDelta(right) || left.type !== right.type) return false;\n const { delta: _leftDelta, ...leftPayload } = left.payload;\n const { delta: _rightDelta, ...rightPayload } = right.payload;\n return JSON.stringify(leftPayload) === JSON.stringify(rightPayload);\n}\n\nexport function coalesceSessionJournalDeltas(left: DeltaEvent, right: DeltaEvent): DeltaEvent {\n return {\n ...left,\n payload: { ...left.payload, delta: left.payload.delta + right.payload.delta },\n } as DeltaEvent;\n}\n"],"mappings":";;AAEA,MAAa,kCAAkC,KAAK;AAOpD,SAAgB,sBAAsB,OAA8C;AAClF,QAAO,MAAM,SAAS,aAAa,oBACjC,MAAM,SAAS,aAAa,yBAC5B,MAAM,SAAS,aAAa;;AAGhC,SAAgB,gCAAgC,MAAwB,OAAkC;AACxG,KAAI,CAAC,sBAAsB,KAAK,IAAI,CAAC,sBAAsB,MAAM,IAAI,KAAK,SAAS,MAAM,KAAM,QAAO;CACtG,MAAM,EAAE,OAAO,YAAY,GAAG,gBAAgB,KAAK;CACnD,MAAM,EAAE,OAAO,aAAa,GAAG,iBAAiB,MAAM;AACtD,QAAO,KAAK,UAAU,YAAY,KAAK,KAAK,UAAU,aAAa;;AAGrE,SAAgB,6BAA6B,MAAkB,OAA+B;AAC5F,QAAO;EACL,GAAG;EACH,SAAS;GAAE,GAAG,KAAK;GAAS,OAAO,KAAK,QAAQ,QAAQ,MAAM,QAAQ;GAAO;EAC9E"}
|
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
import { c as isRecord$1, d as safeNcpSessionFilename, f as toIsoString, l as normalizeNcpAgentId, m as SessionEventReplayer, o as createNcpAgentSessionJournalMetadataEntry, u as normalizeNcpSessionId, v as SessionReplaySupersessionTracker } from "./ncp-agent-session-journal.utils-B7a_PKCq.js";
|
|
2
|
+
import { i as isSessionJournalDelta, n as canCoalesceSessionJournalDeltas, r as coalesceSessionJournalDeltas } from "./session-journal-delta-coalescer.utils-CI-N-Zpj.js";
|
|
3
|
+
import { t as scanSessionJournal } from "./ncp-agent-session-journal-entry.utils-Dh0Ck_oW.js";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { constants, createReadStream, existsSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { copyFile, mkdir, open, readFile, readdir, rename, rm, stat } from "node:fs/promises";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import { createInterface } from "node:readline";
|
|
11
|
+
import { isDeepStrictEqual } from "node:util";
|
|
12
|
+
//#region \0rolldown/runtime.js
|
|
13
|
+
var __defProp = Object.defineProperty;
|
|
14
|
+
var __exportAll = (all, no_symbols) => {
|
|
15
|
+
let target = {};
|
|
16
|
+
for (var name in all) __defProp(target, name, {
|
|
17
|
+
get: all[name],
|
|
18
|
+
enumerable: true
|
|
19
|
+
});
|
|
20
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
21
|
+
return target;
|
|
22
|
+
};
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/stores/sqlite-database.store.ts
|
|
25
|
+
const require = createRequire(import.meta.url);
|
|
26
|
+
const portableDatabases = /* @__PURE__ */ new Map();
|
|
27
|
+
async function openSqliteDatabase(databasePath, options = {}) {
|
|
28
|
+
try {
|
|
29
|
+
return new (require("node:sqlite")).DatabaseSync(databasePath);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (!isMissingNodeSqlite(error)) throw error;
|
|
32
|
+
if (options.incremental) {
|
|
33
|
+
const { default: Database } = await import("libsql");
|
|
34
|
+
return new Database(databasePath);
|
|
35
|
+
}
|
|
36
|
+
return await openPortableDatabase(databasePath);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function runSqliteTransaction(database, operation, mode = "DEFERRED") {
|
|
40
|
+
database.exec(`BEGIN ${mode}`);
|
|
41
|
+
try {
|
|
42
|
+
const result = operation();
|
|
43
|
+
database.exec("COMMIT");
|
|
44
|
+
return result;
|
|
45
|
+
} catch (error) {
|
|
46
|
+
database.exec("ROLLBACK");
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function isMissingNodeSqlite(error) {
|
|
51
|
+
const code = typeof error === "object" && error !== null ? error.code : void 0;
|
|
52
|
+
return code === "ERR_UNKNOWN_BUILTIN_MODULE" || code === "MODULE_NOT_FOUND";
|
|
53
|
+
}
|
|
54
|
+
async function openPortableDatabase(databasePath) {
|
|
55
|
+
const existing = portableDatabases.get(databasePath);
|
|
56
|
+
if (existing) return (await existing).acquire();
|
|
57
|
+
const opening = createPortableDatabase(databasePath).catch((error) => {
|
|
58
|
+
portableDatabases.delete(databasePath);
|
|
59
|
+
throw error;
|
|
60
|
+
});
|
|
61
|
+
portableDatabases.set(databasePath, opening);
|
|
62
|
+
return (await opening).acquire();
|
|
63
|
+
}
|
|
64
|
+
async function createPortableDatabase(databasePath) {
|
|
65
|
+
const initialize = (await import("sql.js")).default;
|
|
66
|
+
const sqlite = await initialize({ locateFile: resolveSqlJsAsset });
|
|
67
|
+
return new PortableSqliteDatabase(databasePath, existsSync(databasePath) ? new sqlite.Database(readFileSync(databasePath)) : new sqlite.Database());
|
|
68
|
+
}
|
|
69
|
+
function resolveSqlJsAsset(file) {
|
|
70
|
+
try {
|
|
71
|
+
return require.resolve(`sql.js/dist/${file}`);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
const bundledAssetPath = fileURLToPath(new URL(file, import.meta.url));
|
|
74
|
+
if (file === "sql-wasm.wasm" && existsSync(bundledAssetPath)) return bundledAssetPath;
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
var PortableSqliteDatabase = class {
|
|
79
|
+
inTransaction = false;
|
|
80
|
+
dirty = false;
|
|
81
|
+
leaseCount = 0;
|
|
82
|
+
constructor(databasePath, database) {
|
|
83
|
+
this.databasePath = databasePath;
|
|
84
|
+
this.database = database;
|
|
85
|
+
}
|
|
86
|
+
acquire = () => {
|
|
87
|
+
this.leaseCount += 1;
|
|
88
|
+
return new PortableSqliteLease(this);
|
|
89
|
+
};
|
|
90
|
+
exec = (sql) => {
|
|
91
|
+
const command = sql.trim().toUpperCase();
|
|
92
|
+
this.database.run(sql);
|
|
93
|
+
if (command.startsWith("BEGIN")) {
|
|
94
|
+
this.inTransaction = true;
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (command.startsWith("ROLLBACK")) {
|
|
98
|
+
this.inTransaction = false;
|
|
99
|
+
this.dirty = false;
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (command.startsWith("COMMIT")) {
|
|
103
|
+
this.inTransaction = false;
|
|
104
|
+
this.persistIfDirty();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (!command.startsWith("PRAGMA")) this.markDirty();
|
|
108
|
+
};
|
|
109
|
+
prepare = (sql) => new PortableSqliteStatement(this, sql);
|
|
110
|
+
release = () => {
|
|
111
|
+
this.leaseCount -= 1;
|
|
112
|
+
if (this.leaseCount > 0) return;
|
|
113
|
+
this.persistIfDirty();
|
|
114
|
+
this.database.close();
|
|
115
|
+
portableDatabases.delete(this.databasePath);
|
|
116
|
+
};
|
|
117
|
+
run = (sql, params) => {
|
|
118
|
+
this.database.run(sql, normalizeParams(params));
|
|
119
|
+
this.markDirty();
|
|
120
|
+
};
|
|
121
|
+
readRows = (sql, params) => {
|
|
122
|
+
const statement = this.database.prepare(sql);
|
|
123
|
+
try {
|
|
124
|
+
bindStatement(statement, params);
|
|
125
|
+
const rows = [];
|
|
126
|
+
while (statement.step()) rows.push(statement.getAsObject());
|
|
127
|
+
return rows;
|
|
128
|
+
} finally {
|
|
129
|
+
statement.free();
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
markDirty = () => {
|
|
133
|
+
this.dirty = true;
|
|
134
|
+
if (!this.inTransaction) this.persistIfDirty();
|
|
135
|
+
};
|
|
136
|
+
persistIfDirty = () => {
|
|
137
|
+
if (!this.dirty) return;
|
|
138
|
+
const temporaryPath = `${this.databasePath}.tmp`;
|
|
139
|
+
writeFileSync(temporaryPath, this.database.export());
|
|
140
|
+
renameSync(temporaryPath, this.databasePath);
|
|
141
|
+
this.dirty = false;
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
var PortableSqliteLease = class {
|
|
145
|
+
closed = false;
|
|
146
|
+
constructor(owner) {
|
|
147
|
+
this.owner = owner;
|
|
148
|
+
}
|
|
149
|
+
close = () => {
|
|
150
|
+
if (this.closed) return;
|
|
151
|
+
this.closed = true;
|
|
152
|
+
this.owner.release();
|
|
153
|
+
};
|
|
154
|
+
exec = (sql) => this.owner.exec(sql);
|
|
155
|
+
prepare = (sql) => this.owner.prepare(sql);
|
|
156
|
+
};
|
|
157
|
+
var PortableSqliteStatement = class {
|
|
158
|
+
constructor(database, sql) {
|
|
159
|
+
this.database = database;
|
|
160
|
+
this.sql = sql;
|
|
161
|
+
}
|
|
162
|
+
all = (...params) => this.database.readRows(this.sql, params);
|
|
163
|
+
get = (...params) => this.all(...params)[0];
|
|
164
|
+
run = (...params) => {
|
|
165
|
+
this.database.run(this.sql, params);
|
|
166
|
+
return {};
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
function bindStatement(statement, params) {
|
|
170
|
+
const normalized = normalizeParams(params);
|
|
171
|
+
if (Array.isArray(normalized) && normalized.length === 0) return;
|
|
172
|
+
statement.bind(normalized);
|
|
173
|
+
}
|
|
174
|
+
function normalizeParams(params) {
|
|
175
|
+
if (params.length !== 1 || !isRecord(params[0])) return params;
|
|
176
|
+
return Object.fromEntries(Object.entries(params[0]).map(([key, value]) => [`@${key}`, value]));
|
|
177
|
+
}
|
|
178
|
+
function isRecord(value) {
|
|
179
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
180
|
+
}
|
|
181
|
+
//#endregion
|
|
182
|
+
//#region src/stores/ncp-agent-session-metadata.store.ts
|
|
183
|
+
var NcpAgentSessionMetadataStore = class {
|
|
184
|
+
constructor(journalDir) {
|
|
185
|
+
this.journalDir = journalDir;
|
|
186
|
+
}
|
|
187
|
+
write = async (record) => {
|
|
188
|
+
await mkdir(this.journalDir, { recursive: true });
|
|
189
|
+
const targetPath = this.metadataPath(record.sessionId);
|
|
190
|
+
const tempPath = `${targetPath}.${process.pid}.${randomUUID()}.tmp`;
|
|
191
|
+
try {
|
|
192
|
+
const output = await open(tempPath, "wx");
|
|
193
|
+
try {
|
|
194
|
+
await output.writeFile(`${JSON.stringify(createNcpAgentSessionJournalMetadataEntry(record))}\n`, "utf-8");
|
|
195
|
+
await output.sync();
|
|
196
|
+
} finally {
|
|
197
|
+
await output.close();
|
|
198
|
+
}
|
|
199
|
+
await rename(tempPath, targetPath);
|
|
200
|
+
} catch (error) {
|
|
201
|
+
await rm(tempPath, { force: true });
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
read = async (sessionId, activitySnapshot) => {
|
|
206
|
+
try {
|
|
207
|
+
const parsed = JSON.parse(await readFile(this.metadataPath(sessionId), "utf-8"));
|
|
208
|
+
if (!isRecord$1(parsed) || parsed._type !== "metadata" || !isRecord$1(parsed.metadata)) throw new Error(`invalid ncp agent session metadata sidecar: ${sessionId}`);
|
|
209
|
+
const createdAt = toIsoString(parsed.created_at, activitySnapshot.createdAt);
|
|
210
|
+
const sidecarUpdatedAt = toIsoString(parsed.updated_at, activitySnapshot.updatedAt);
|
|
211
|
+
const updatedAt = Date.parse(sidecarUpdatedAt) > Date.parse(activitySnapshot.updatedAt) ? sidecarUpdatedAt : activitySnapshot.updatedAt;
|
|
212
|
+
const agentId = normalizeNcpAgentId(typeof parsed.agent_id === "string" ? parsed.agent_id : void 0);
|
|
213
|
+
return {
|
|
214
|
+
metadata: structuredClone(parsed.metadata),
|
|
215
|
+
...agentId ? { agentId } : {},
|
|
216
|
+
createdAt,
|
|
217
|
+
updatedAt
|
|
218
|
+
};
|
|
219
|
+
} catch (error) {
|
|
220
|
+
if ((error && typeof error === "object" && "code" in error ? error.code : void 0) === "ENOENT") return activitySnapshot;
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
delete = async (sessionId) => {
|
|
225
|
+
await rm(this.metadataPath(sessionId), { force: true });
|
|
226
|
+
};
|
|
227
|
+
metadataPath = (sessionId) => {
|
|
228
|
+
return join(this.journalDir, `${safeNcpSessionFilename(sessionId.replace(/:/g, "_"))}.metadata.json`);
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
//#endregion
|
|
232
|
+
//#region src/stores/session-journal-loader.store.ts
|
|
233
|
+
/** Explicit full-record operations read the source, never the bounded display projection. */
|
|
234
|
+
var SessionJournalLoaderStore = class {
|
|
235
|
+
constructor(metadataStore) {
|
|
236
|
+
this.metadataStore = metadataStore;
|
|
237
|
+
}
|
|
238
|
+
load = async (sessionId, path) => {
|
|
239
|
+
let journalStat;
|
|
240
|
+
try {
|
|
241
|
+
journalStat = await stat(path);
|
|
242
|
+
} catch {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
const journalOffset = journalStat.size;
|
|
246
|
+
const supersessionTracker = new SessionReplaySupersessionTracker();
|
|
247
|
+
const parsedJournal = await scanSessionJournal({
|
|
248
|
+
path,
|
|
249
|
+
endOffset: journalOffset,
|
|
250
|
+
onEvent: (event, eventIndex) => supersessionTracker.observe(event, eventIndex)
|
|
251
|
+
});
|
|
252
|
+
const { agentId, createdAt, updatedAt, metadata } = await this.metadataStore.read(sessionId, {
|
|
253
|
+
...parsedJournal,
|
|
254
|
+
createdAt: Date.parse(parsedJournal.createdAt) > journalStat.birthtimeMs ? journalStat.birthtime.toISOString() : parsedJournal.createdAt,
|
|
255
|
+
updatedAt: Date.parse(parsedJournal.updatedAt) > journalStat.mtimeMs ? parsedJournal.updatedAt : journalStat.mtime.toISOString()
|
|
256
|
+
});
|
|
257
|
+
const replayer = await SessionEventReplayer.create();
|
|
258
|
+
const supersededSyntheticRecoveryIndexes = supersessionTracker.finish();
|
|
259
|
+
await scanSessionJournal({
|
|
260
|
+
path,
|
|
261
|
+
endOffset: journalOffset,
|
|
262
|
+
onEvent: async (event, eventIndex) => {
|
|
263
|
+
await replayer.append(event, eventIndex, supersededSyntheticRecoveryIndexes);
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
return {
|
|
267
|
+
record: {
|
|
268
|
+
sessionId,
|
|
269
|
+
...agentId ? { agentId } : {},
|
|
270
|
+
messages: replayer.finish(),
|
|
271
|
+
createdAt,
|
|
272
|
+
updatedAt,
|
|
273
|
+
metadata
|
|
274
|
+
},
|
|
275
|
+
nextSeq: parsedJournal.nextSeq,
|
|
276
|
+
journalOffset,
|
|
277
|
+
projectedJournalOffset: parsedJournal.projectedJournalOffset
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
};
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/stores/session-journal-maintenance.store.ts
|
|
283
|
+
var session_journal_maintenance_store_exports = /* @__PURE__ */ __exportAll({
|
|
284
|
+
assertSessionJournalTailComplete: () => assertSessionJournalTailComplete,
|
|
285
|
+
compactSessionJournal: () => compactSessionJournal,
|
|
286
|
+
recoverSessionJournalMaintenance: () => recoverSessionJournalMaintenance,
|
|
287
|
+
recoverSessionJournalTornTail: () => recoverSessionJournalTornTail,
|
|
288
|
+
restoreSessionJournalBackup: () => restoreSessionJournalBackup
|
|
289
|
+
});
|
|
290
|
+
const CATALOG_FILE = ".ncp-agent-session-catalog.sqlite";
|
|
291
|
+
function paths(journalDir, sessionId) {
|
|
292
|
+
const basename = safeNcpSessionFilename(sessionId.replace(/:/g, "_"));
|
|
293
|
+
const source = join(journalDir, `${basename}.jsonl`);
|
|
294
|
+
return {
|
|
295
|
+
source,
|
|
296
|
+
backup: `${source}.backup`,
|
|
297
|
+
pending: `${source}.pending`,
|
|
298
|
+
marker: `${source}.maintenance`,
|
|
299
|
+
markerDraft: `${source}.maintenance.tmp`,
|
|
300
|
+
projection: join(journalDir, ".message-projections", basename)
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
async function syncDirectory(path) {
|
|
304
|
+
const handle = await open(path, "r");
|
|
305
|
+
try {
|
|
306
|
+
await handle.sync();
|
|
307
|
+
} finally {
|
|
308
|
+
await handle.close();
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
async function syncFile(path) {
|
|
312
|
+
const handle = await open(path, "r");
|
|
313
|
+
try {
|
|
314
|
+
await handle.sync();
|
|
315
|
+
} finally {
|
|
316
|
+
await handle.close();
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
async function hashFile(path) {
|
|
320
|
+
const hash = createHash("sha256");
|
|
321
|
+
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
|
322
|
+
return hash.digest("hex");
|
|
323
|
+
}
|
|
324
|
+
async function assertSourceUnchanged(source, expected) {
|
|
325
|
+
const current = await stat(source);
|
|
326
|
+
if (current.size !== expected.size || current.mtimeMs !== expected.mtimeMs || current.ino !== expected.ino) throw new Error("Journal changed during compaction; refusing activation.");
|
|
327
|
+
}
|
|
328
|
+
/** Refuse to append after a partial write: another JSONL entry would hide the torn tail. */
|
|
329
|
+
async function assertSessionJournalTailComplete(source) {
|
|
330
|
+
let handle;
|
|
331
|
+
try {
|
|
332
|
+
handle = await open(source, "r");
|
|
333
|
+
} catch (error) {
|
|
334
|
+
if (error.code === "ENOENT") return;
|
|
335
|
+
throw error;
|
|
336
|
+
}
|
|
337
|
+
try {
|
|
338
|
+
const size = (await handle.stat()).size;
|
|
339
|
+
if (size === 0) return;
|
|
340
|
+
const suffix = Buffer.alloc(1);
|
|
341
|
+
await handle.read(suffix, 0, 1, size - 1);
|
|
342
|
+
if (suffix[0] !== 10) throw new Error("Journal has an incomplete tail line; refusing further writes.");
|
|
343
|
+
} finally {
|
|
344
|
+
await handle.close();
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
/** Maintenance only: preserve a crash-torn suffix before restoring the last committed newline. */
|
|
348
|
+
async function recoverSessionJournalTornTail(source, signal) {
|
|
349
|
+
const file = await open(source, "r+");
|
|
350
|
+
try {
|
|
351
|
+
const original = await file.stat();
|
|
352
|
+
if (!original.size) return;
|
|
353
|
+
const buffer = Buffer.alloc(64 * 1024);
|
|
354
|
+
await file.read(buffer, 0, 1, original.size - 1);
|
|
355
|
+
if (buffer[0] === 10) return;
|
|
356
|
+
let boundary = 0;
|
|
357
|
+
for (let end = original.size; end > 0;) {
|
|
358
|
+
signal?.throwIfAborted();
|
|
359
|
+
const start = Math.max(0, end - buffer.length);
|
|
360
|
+
const { bytesRead } = await file.read(buffer, 0, end - start, start);
|
|
361
|
+
const newline = buffer.subarray(0, bytesRead).lastIndexOf(10);
|
|
362
|
+
if (newline >= 0) {
|
|
363
|
+
boundary = start + newline + 1;
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
end = start;
|
|
367
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
368
|
+
}
|
|
369
|
+
const quarantine = await open(`${source}.torn-${original.size}-${Math.floor(original.mtimeMs)}`, "w");
|
|
370
|
+
try {
|
|
371
|
+
for (let offset = boundary; offset < original.size;) {
|
|
372
|
+
signal?.throwIfAborted();
|
|
373
|
+
const { bytesRead } = await file.read(buffer, 0, Math.min(buffer.length, original.size - offset), offset);
|
|
374
|
+
if (!bytesRead) throw new Error("Torn journal preservation made no progress.");
|
|
375
|
+
await quarantine.writeFile(buffer.subarray(0, bytesRead));
|
|
376
|
+
offset += bytesRead;
|
|
377
|
+
}
|
|
378
|
+
await quarantine.sync();
|
|
379
|
+
} finally {
|
|
380
|
+
await quarantine.close();
|
|
381
|
+
}
|
|
382
|
+
signal?.throwIfAborted();
|
|
383
|
+
await assertSourceUnchanged(source, original);
|
|
384
|
+
await file.truncate(boundary);
|
|
385
|
+
await file.sync();
|
|
386
|
+
} finally {
|
|
387
|
+
await file.close();
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
async function invalidateDerivedState(journalDir, sessionId, projection) {
|
|
391
|
+
await rm(projection, {
|
|
392
|
+
recursive: true,
|
|
393
|
+
force: true
|
|
394
|
+
});
|
|
395
|
+
const catalogPath = join(journalDir, CATALOG_FILE);
|
|
396
|
+
try {
|
|
397
|
+
await stat(catalogPath);
|
|
398
|
+
} catch (error) {
|
|
399
|
+
if (error.code === "ENOENT") return;
|
|
400
|
+
throw error;
|
|
401
|
+
}
|
|
402
|
+
const database = await openSqliteDatabase(catalogPath);
|
|
403
|
+
try {
|
|
404
|
+
database.exec("PRAGMA busy_timeout = 10000; PRAGMA synchronous = FULL; PRAGMA foreign_keys = ON;");
|
|
405
|
+
runSqliteTransaction(database, () => {
|
|
406
|
+
database.prepare("DELETE FROM run_recovery WHERE session_id = ?").run(sessionId);
|
|
407
|
+
database.prepare("DELETE FROM sessions WHERE session_id = ? AND deleted_at IS NULL").run(sessionId);
|
|
408
|
+
}, "IMMEDIATE");
|
|
409
|
+
} finally {
|
|
410
|
+
database.close();
|
|
411
|
+
}
|
|
412
|
+
await syncDirectory(journalDir);
|
|
413
|
+
}
|
|
414
|
+
/** Runs before the catalog opens. An interrupted switch always rolls back to the untouched backup. */
|
|
415
|
+
async function recoverSessionJournalMaintenance(journalDir) {
|
|
416
|
+
let names;
|
|
417
|
+
try {
|
|
418
|
+
names = await readdir(journalDir);
|
|
419
|
+
} catch (error) {
|
|
420
|
+
if (error.code === "ENOENT") return;
|
|
421
|
+
throw error;
|
|
422
|
+
}
|
|
423
|
+
for (const name of names.filter((item) => item.endsWith(".jsonl.maintenance") || item.endsWith(".jsonl.import-maintenance"))) {
|
|
424
|
+
const marker = JSON.parse(await readFile(join(journalDir, name), "utf8"));
|
|
425
|
+
const sessionId = normalizeNcpSessionId(marker.sessionId ?? "");
|
|
426
|
+
const suffix = name.endsWith(".import-maintenance") ? ".import-maintenance" : ".maintenance";
|
|
427
|
+
if (!sessionId || `${safeNcpSessionFilename(sessionId.replace(/:/g, "_"))}.jsonl${suffix}` !== name || suffix === ".import-maintenance" && !marker.snapshot) throw new Error(`Invalid journal maintenance marker: ${name}`);
|
|
428
|
+
const source = join(journalDir, name.slice(0, -suffix.length));
|
|
429
|
+
const backup = `${source}${marker.snapshot ? ".import-backup" : ".backup"}`;
|
|
430
|
+
const restore = `${source}.restore`;
|
|
431
|
+
if (marker.snapshot?.hadSource === false) await rm(source, { force: true });
|
|
432
|
+
else {
|
|
433
|
+
await copyFile(backup, restore);
|
|
434
|
+
await syncFile(restore);
|
|
435
|
+
await rename(restore, source);
|
|
436
|
+
}
|
|
437
|
+
if (marker.snapshot) {
|
|
438
|
+
const metadataStore = new NcpAgentSessionMetadataStore(journalDir);
|
|
439
|
+
if (marker.snapshot.hadSource) await metadataStore.write({
|
|
440
|
+
...marker.snapshot.metadata,
|
|
441
|
+
sessionId
|
|
442
|
+
});
|
|
443
|
+
else await metadataStore.delete(sessionId);
|
|
444
|
+
}
|
|
445
|
+
await invalidateDerivedState(journalDir, sessionId, join(journalDir, ".message-projections", name.slice(0, -(".jsonl" + suffix).length)));
|
|
446
|
+
await rm(`${source}.pending`, { force: true });
|
|
447
|
+
await rm(join(journalDir, name));
|
|
448
|
+
await syncDirectory(journalDir);
|
|
449
|
+
if (marker.snapshot) await rm(backup, { force: true });
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
async function validateColdReplay(journalDir, sessionId, source, candidate) {
|
|
453
|
+
const loader = new SessionJournalLoaderStore(new NcpAgentSessionMetadataStore(journalDir));
|
|
454
|
+
const original = await loader.load(sessionId, source);
|
|
455
|
+
const compacted = await loader.load(sessionId, candidate);
|
|
456
|
+
if (!original || !compacted || !isDeepStrictEqual(original.record.messages, compacted.record.messages)) throw new Error("Session journal compaction changed cold-replayed messages.");
|
|
457
|
+
}
|
|
458
|
+
async function writeCoalescedJournal(source, pending) {
|
|
459
|
+
const output = await open(pending, "wx");
|
|
460
|
+
let inputLines = 0;
|
|
461
|
+
let outputLines = 0;
|
|
462
|
+
let nextSeq = 1;
|
|
463
|
+
let pendingEntry = null;
|
|
464
|
+
const flush = async () => {
|
|
465
|
+
if (!pendingEntry) return;
|
|
466
|
+
if (pendingEntry._type === "event") pendingEntry.seq = nextSeq++;
|
|
467
|
+
await output.writeFile(`${JSON.stringify(pendingEntry)}\n`);
|
|
468
|
+
outputLines++;
|
|
469
|
+
pendingEntry = null;
|
|
470
|
+
};
|
|
471
|
+
try {
|
|
472
|
+
const lines = createInterface({
|
|
473
|
+
input: createReadStream(source),
|
|
474
|
+
crlfDelay: Infinity
|
|
475
|
+
});
|
|
476
|
+
for await (const line of lines) {
|
|
477
|
+
if (!line) throw new Error("Journal contains a blank line; compaction requires a valid complete source.");
|
|
478
|
+
inputLines++;
|
|
479
|
+
const entry = JSON.parse(line);
|
|
480
|
+
if (!entry || typeof entry !== "object" || !["event", "metadata"].includes(entry._type)) throw new Error(`Invalid journal entry at line ${inputLines}.`);
|
|
481
|
+
if (entry.version !== void 0 && entry.version !== 1) throw new Error(`Unsupported journal entry version at line ${inputLines}.`);
|
|
482
|
+
if (entry._type === "event") {
|
|
483
|
+
if (!Number.isSafeInteger(entry.seq) || entry.seq < 1 || !entry.event?.type || !("payload" in entry.event)) throw new Error(`Invalid journal sequence or event at line ${inputLines}.`);
|
|
484
|
+
}
|
|
485
|
+
if (pendingEntry?._type === "event" && entry._type === "event" && pendingEntry.event && entry.event && canCoalesceSessionJournalDeltas(pendingEntry.event, entry.event) && isSessionJournalDelta(pendingEntry.event) && isSessionJournalDelta(entry.event)) {
|
|
486
|
+
const merged = coalesceSessionJournalDeltas(pendingEntry.event, entry.event);
|
|
487
|
+
if (Buffer.byteLength(merged.payload.delta, "utf8") <= 16384) {
|
|
488
|
+
pendingEntry.event = merged;
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
await flush();
|
|
493
|
+
pendingEntry = entry;
|
|
494
|
+
}
|
|
495
|
+
await flush();
|
|
496
|
+
await output.sync();
|
|
497
|
+
} finally {
|
|
498
|
+
await output.close();
|
|
499
|
+
}
|
|
500
|
+
return {
|
|
501
|
+
inputLines,
|
|
502
|
+
outputLines
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
async function writeMaintenanceMarker(journalDir, target, sessionId) {
|
|
506
|
+
const marker = await open(target.markerDraft, "wx");
|
|
507
|
+
try {
|
|
508
|
+
await marker.writeFile(`${JSON.stringify({ sessionId })}\n`);
|
|
509
|
+
await marker.sync();
|
|
510
|
+
} finally {
|
|
511
|
+
await marker.close();
|
|
512
|
+
}
|
|
513
|
+
await rename(target.markerDraft, target.marker);
|
|
514
|
+
await syncDirectory(journalDir);
|
|
515
|
+
}
|
|
516
|
+
async function activateCompactedJournal(journalDir, sessionId, target, sourceStat) {
|
|
517
|
+
let renamed = false;
|
|
518
|
+
try {
|
|
519
|
+
await copyFile(target.source, target.backup, constants.COPYFILE_EXCL);
|
|
520
|
+
await syncFile(target.backup);
|
|
521
|
+
await assertSourceUnchanged(target.source, sourceStat);
|
|
522
|
+
await writeMaintenanceMarker(journalDir, target, sessionId);
|
|
523
|
+
await assertSourceUnchanged(target.source, sourceStat);
|
|
524
|
+
await rename(target.pending, target.source);
|
|
525
|
+
renamed = true;
|
|
526
|
+
await syncDirectory(journalDir);
|
|
527
|
+
await invalidateDerivedState(journalDir, sessionId, target.projection);
|
|
528
|
+
await rm(target.marker);
|
|
529
|
+
await syncDirectory(journalDir);
|
|
530
|
+
} catch (error) {
|
|
531
|
+
if (renamed) await recoverSessionJournalMaintenance(journalDir);
|
|
532
|
+
else {
|
|
533
|
+
await rm(target.markerDraft, { force: true });
|
|
534
|
+
await rm(target.marker, { force: true });
|
|
535
|
+
await rm(target.backup, { force: true });
|
|
536
|
+
}
|
|
537
|
+
throw error;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
/** Restores the original v1 journal after a successful offline compaction. All writers must be stopped. */
|
|
541
|
+
async function restoreSessionJournalBackup(input) {
|
|
542
|
+
const sessionId = normalizeNcpSessionId(input.sessionId);
|
|
543
|
+
if (!sessionId || input.writersStopped !== true) throw new Error("Stop all writers and explicitly acknowledge it before restoring a journal backup.");
|
|
544
|
+
const target = paths(input.journalDir, sessionId);
|
|
545
|
+
await stat(target.backup);
|
|
546
|
+
for (const candidate of [
|
|
547
|
+
target.pending,
|
|
548
|
+
target.marker,
|
|
549
|
+
target.markerDraft
|
|
550
|
+
]) try {
|
|
551
|
+
await stat(candidate);
|
|
552
|
+
throw new Error(`Unresolved journal maintenance file: ${candidate}`);
|
|
553
|
+
} catch (error) {
|
|
554
|
+
if (error.code !== "ENOENT") throw error;
|
|
555
|
+
}
|
|
556
|
+
const sourceStat = await stat(target.source);
|
|
557
|
+
let recoveryStarted = false;
|
|
558
|
+
try {
|
|
559
|
+
await writeCoalescedJournal(target.backup, target.pending);
|
|
560
|
+
const expected = await stat(target.pending);
|
|
561
|
+
if (sourceStat.size !== expected.size || await hashFile(target.source) !== await hashFile(target.pending)) throw new Error("Journal changed since compaction; refusing restore to protect later events.");
|
|
562
|
+
await assertSourceUnchanged(target.source, sourceStat);
|
|
563
|
+
await writeMaintenanceMarker(input.journalDir, target, sessionId);
|
|
564
|
+
await assertSourceUnchanged(target.source, sourceStat);
|
|
565
|
+
recoveryStarted = true;
|
|
566
|
+
await recoverSessionJournalMaintenance(input.journalDir);
|
|
567
|
+
} catch (error) {
|
|
568
|
+
await rm(target.markerDraft, { force: true });
|
|
569
|
+
if (!recoveryStarted) await rm(target.marker, { force: true });
|
|
570
|
+
throw error;
|
|
571
|
+
} finally {
|
|
572
|
+
await rm(target.pending, { force: true });
|
|
573
|
+
}
|
|
574
|
+
return {
|
|
575
|
+
sessionId,
|
|
576
|
+
restored: true
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
/** Explicit offline operation. The caller must stop every writer for this HOME before apply. */
|
|
580
|
+
async function compactSessionJournal(input) {
|
|
581
|
+
const sessionId = normalizeNcpSessionId(input.sessionId);
|
|
582
|
+
if (!sessionId) throw new Error("A session ID is required.");
|
|
583
|
+
if (input.apply && input.writersStopped !== true) throw new Error("Stop all writers and explicitly acknowledge it before applying compaction.");
|
|
584
|
+
const target = paths(input.journalDir, sessionId);
|
|
585
|
+
for (const candidate of [
|
|
586
|
+
target.backup,
|
|
587
|
+
target.pending,
|
|
588
|
+
target.marker,
|
|
589
|
+
target.markerDraft
|
|
590
|
+
]) try {
|
|
591
|
+
await stat(candidate);
|
|
592
|
+
throw new Error(`Unresolved journal maintenance file: ${candidate}`);
|
|
593
|
+
} catch (error) {
|
|
594
|
+
if (error.code !== "ENOENT") throw error;
|
|
595
|
+
}
|
|
596
|
+
const before = await stat(target.source);
|
|
597
|
+
await assertSessionJournalTailComplete(target.source);
|
|
598
|
+
try {
|
|
599
|
+
const { inputLines, outputLines } = await writeCoalescedJournal(target.source, target.pending);
|
|
600
|
+
await validateColdReplay(input.journalDir, sessionId, target.source, target.pending);
|
|
601
|
+
await assertSourceUnchanged(target.source, before);
|
|
602
|
+
const outputBytes = (await stat(target.pending)).size;
|
|
603
|
+
const result = {
|
|
604
|
+
sessionId,
|
|
605
|
+
applied: input.apply,
|
|
606
|
+
inputBytes: before.size,
|
|
607
|
+
outputBytes,
|
|
608
|
+
inputLines,
|
|
609
|
+
outputLines
|
|
610
|
+
};
|
|
611
|
+
if (!input.apply) return result;
|
|
612
|
+
await activateCompactedJournal(input.journalDir, sessionId, target, before);
|
|
613
|
+
return result;
|
|
614
|
+
} finally {
|
|
615
|
+
await rm(target.pending, { force: true });
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
//#endregion
|
|
619
|
+
export { restoreSessionJournalBackup as a, NcpAgentSessionMetadataStore as c, recoverSessionJournalTornTail as i, openSqliteDatabase as l, compactSessionJournal as n, session_journal_maintenance_store_exports as o, recoverSessionJournalMaintenance as r, SessionJournalLoaderStore as s, assertSessionJournalTailComplete as t, runSqliteTransaction as u };
|
|
620
|
+
|
|
621
|
+
//# sourceMappingURL=session-journal-maintenance.store-DrZMlaxf.js.map
|