@playcraft/cli 0.0.59 → 0.1.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/dist/agent/local-backend.js +1 -1
- package/dist/workspace-runtime/codex/app-server.js +46 -121
- package/dist/workspace-runtime/codex/jsonrpc-stdio.js +19 -2
- package/dist/workspace-runtime/codex/loopback.js +90 -39
- package/dist/workspace-runtime/codex/native-adapter.js +3 -0
- package/dist/workspace-runtime/persistence/journal.js +213 -200
- package/dist/workspace-runtime/persistence/sqlite-client.js +61 -0
- package/dist/workspace-runtime/persistence/sqlite-worker.js +1025 -0
- package/dist/workspace-runtime/serve.js +27 -1
- package/dist/workspace-runtime/server/desktop-host.js +314 -0
- package/dist/workspace-runtime/server/dispatch.js +107 -48
- package/dist/workspace-runtime/server/http.js +196 -11
- package/dist/workspace-runtime/server/terminal-host.js +243 -0
- package/package.json +6 -3
|
@@ -0,0 +1,1025 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dedicated SQLite writer for the CLI platform state file.
|
|
3
|
+
* The main thread never opens runtime-state.sqlite.
|
|
4
|
+
*
|
|
5
|
+
* Binding pin: better-sqlite3@13.0.3 vendors SQLite 3.53.4, which includes the
|
|
6
|
+
* official WAL-reset fix (3.51.3). Node's built-in node:sqlite on Node 22.22.1
|
|
7
|
+
* reports 3.51.2 and must not be used for this file.
|
|
8
|
+
* Do not read Codex JSONL from this worker.
|
|
9
|
+
*/
|
|
10
|
+
import { randomBytes, randomUUID } from 'node:crypto';
|
|
11
|
+
import { chmodSync, mkdirSync } from 'node:fs';
|
|
12
|
+
import { dirname } from 'node:path';
|
|
13
|
+
import { parentPort } from 'node:worker_threads';
|
|
14
|
+
import Database from 'better-sqlite3-pinned';
|
|
15
|
+
import { hashOperationPayload, operationRecordSchema, pendingSourceEventSchema, checkpointRecordSchema, terminalRecordSchema, workspaceRegistrationSchema, transitionOperation, canTransitionTurn, } from '@playcraft/common/cloud-workspace/runtime';
|
|
16
|
+
const SCHEMA_VERSION = 1;
|
|
17
|
+
const MIN_SQLITE = [3, 51, 3];
|
|
18
|
+
const BINDING = 'better-sqlite3@13.0.3';
|
|
19
|
+
const MAX_PINS = 32;
|
|
20
|
+
const MAX_PIN_MS = 120_000;
|
|
21
|
+
let db;
|
|
22
|
+
const port = parentPort;
|
|
23
|
+
if (!port)
|
|
24
|
+
throw new Error('sqlite_worker_without_parent');
|
|
25
|
+
port.on('message', (message) => {
|
|
26
|
+
try {
|
|
27
|
+
const result = dispatch(message.type, message.payload ?? {});
|
|
28
|
+
port.postMessage({ id: message.id, ok: true, result });
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
port.postMessage({
|
|
32
|
+
id: message.id,
|
|
33
|
+
ok: false,
|
|
34
|
+
error: error instanceof Error ? error.message : 'sqlite_worker_failed',
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
function dispatch(type, payload) {
|
|
39
|
+
if (type === 'open')
|
|
40
|
+
return openDatabase(asRecord(payload));
|
|
41
|
+
if (type === 'close') {
|
|
42
|
+
db?.close();
|
|
43
|
+
db = undefined;
|
|
44
|
+
return { closed: true };
|
|
45
|
+
}
|
|
46
|
+
const database = requireDb();
|
|
47
|
+
const body = asRecord(payload);
|
|
48
|
+
const write = database.transaction(() => {
|
|
49
|
+
switch (type) {
|
|
50
|
+
case 'upsertRecord':
|
|
51
|
+
upsertRecord(database, String(body.bucket), String(body.key), body.body);
|
|
52
|
+
return;
|
|
53
|
+
case 'deleteRecord':
|
|
54
|
+
database.prepare('DELETE FROM records WHERE bucket = ? AND record_key = ?').run(body.bucket, body.key);
|
|
55
|
+
return;
|
|
56
|
+
case 'replaceBucket':
|
|
57
|
+
replaceBucket(database, String(body.bucket), Array.isArray(body.entries) ? body.entries : []);
|
|
58
|
+
return;
|
|
59
|
+
case 'forgetWorkspaces':
|
|
60
|
+
database.prepare(`DELETE FROM records WHERE bucket = 'workspaces'`).run();
|
|
61
|
+
return;
|
|
62
|
+
case 'putOperation':
|
|
63
|
+
putOperation(database, String(body.scopeKey), body.operation);
|
|
64
|
+
return;
|
|
65
|
+
case 'setAcceptedFence':
|
|
66
|
+
setMeta(database, 'accepted_lease_fence', String(Math.max(metaNumber(database, 'accepted_lease_fence'), Number(body.value) || 0)));
|
|
67
|
+
return;
|
|
68
|
+
case 'setDestroyed':
|
|
69
|
+
setMeta(database, 'destroyed', '1');
|
|
70
|
+
return;
|
|
71
|
+
case 'setCheckpointId':
|
|
72
|
+
setMeta(database, body.field === 'consistent' ? 'consistent_checkpoint_id' : 'file_checkpoint_id', String(body.id));
|
|
73
|
+
return;
|
|
74
|
+
case 'noteExecutionGroup':
|
|
75
|
+
setMeta(database, 'execution_group', JSON.stringify({ ...(executionGroup(database)), ...asRecord(body.group) }));
|
|
76
|
+
return;
|
|
77
|
+
case 'setStopConfirmed':
|
|
78
|
+
setMeta(database, 'execution_group', JSON.stringify({ ...executionGroup(database), stopConfirmed: body.stopConfirmed === true }));
|
|
79
|
+
return;
|
|
80
|
+
case 'writeExecutionEpoch':
|
|
81
|
+
writeExecutionEpoch(database, body);
|
|
82
|
+
return;
|
|
83
|
+
case 'bindRuntime':
|
|
84
|
+
bindRuntime(database, body);
|
|
85
|
+
return;
|
|
86
|
+
case 'allocateEvent':
|
|
87
|
+
return allocateEvent(database, body);
|
|
88
|
+
case 'commitIdentity':
|
|
89
|
+
commitIdentity(database, body.event);
|
|
90
|
+
return;
|
|
91
|
+
case 'ingest':
|
|
92
|
+
return ingest(database, body);
|
|
93
|
+
case 'bindTurn':
|
|
94
|
+
return bindTurn(database, body);
|
|
95
|
+
case 'ack':
|
|
96
|
+
acknowledge(database, body);
|
|
97
|
+
return;
|
|
98
|
+
case 'createPin':
|
|
99
|
+
return createPin(database, body);
|
|
100
|
+
case 'deletePin':
|
|
101
|
+
database.prepare('DELETE FROM replay_pins WHERE pin_id = ?').run(String(body.pinId));
|
|
102
|
+
return { deleted: true };
|
|
103
|
+
case 'reclaim':
|
|
104
|
+
reclaim(database, Number(body.minRetainMs) || 0);
|
|
105
|
+
return;
|
|
106
|
+
case 'maintain':
|
|
107
|
+
database.pragma('wal_checkpoint(PASSIVE)');
|
|
108
|
+
return;
|
|
109
|
+
default:
|
|
110
|
+
throw new Error(`unknown_sqlite_command:${type}`);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
const extra = write();
|
|
114
|
+
return { ...dump(database), ...(extra && typeof extra === 'object' ? extra : {}) };
|
|
115
|
+
}
|
|
116
|
+
function openDatabase(payload) {
|
|
117
|
+
if (db)
|
|
118
|
+
throw new Error('sqlite_already_open');
|
|
119
|
+
const filePath = String(payload.filePath ?? '');
|
|
120
|
+
if (!filePath)
|
|
121
|
+
throw new Error('sqlite_path_missing');
|
|
122
|
+
mkdirSync(dirname(filePath), { recursive: true, mode: 0o700 });
|
|
123
|
+
const database = new Database(filePath);
|
|
124
|
+
db = database;
|
|
125
|
+
const version = String(database.prepare('select sqlite_version() as v').get().v);
|
|
126
|
+
if (!sqliteAtLeast(version, MIN_SQLITE)) {
|
|
127
|
+
database.close();
|
|
128
|
+
db = undefined;
|
|
129
|
+
throw new Error(`sqlite_engine_too_old:${version}`);
|
|
130
|
+
}
|
|
131
|
+
const existing = readSchemaVersion(database);
|
|
132
|
+
if (existing !== null && existing !== SCHEMA_VERSION) {
|
|
133
|
+
database.close();
|
|
134
|
+
db = undefined;
|
|
135
|
+
throw new Error(`unsupported_runtime_state:${existing}`);
|
|
136
|
+
}
|
|
137
|
+
database.pragma('journal_mode = WAL');
|
|
138
|
+
database.pragma('synchronous = FULL');
|
|
139
|
+
database.pragma('wal_autocheckpoint = 0');
|
|
140
|
+
database.pragma('busy_timeout = 2000');
|
|
141
|
+
const journalMode = String(database.pragma('journal_mode', { simple: true }));
|
|
142
|
+
const synchronous = Number(database.pragma('synchronous', { simple: true }));
|
|
143
|
+
if (journalMode !== 'wal')
|
|
144
|
+
throw new Error(`journal_mode_not_wal:${journalMode}`);
|
|
145
|
+
if (synchronous !== 2)
|
|
146
|
+
throw new Error(`synchronous_not_full:${synchronous}`);
|
|
147
|
+
chmodSync(filePath, 0o600);
|
|
148
|
+
let imported = false;
|
|
149
|
+
if (existing === null) {
|
|
150
|
+
createSchema(database);
|
|
151
|
+
if (payload.legacy && typeof payload.legacy === 'object') {
|
|
152
|
+
importLegacy(database, payload.legacy);
|
|
153
|
+
imported = true;
|
|
154
|
+
}
|
|
155
|
+
quarantineReservedTurns(database);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
quarantineReservedTurns(database);
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
...dump(database),
|
|
162
|
+
imported,
|
|
163
|
+
engine: { version, journalMode, synchronous, binding: BINDING },
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function createSchema(database) {
|
|
167
|
+
database.exec(`
|
|
168
|
+
CREATE TABLE IF NOT EXISTS library_meta (
|
|
169
|
+
key TEXT PRIMARY KEY,
|
|
170
|
+
value TEXT NOT NULL
|
|
171
|
+
);
|
|
172
|
+
CREATE TABLE IF NOT EXISTS source_epochs (
|
|
173
|
+
runtime_id TEXT NOT NULL,
|
|
174
|
+
generation INTEGER NOT NULL,
|
|
175
|
+
runtime_epoch INTEGER NOT NULL,
|
|
176
|
+
next_sequence INTEGER NOT NULL,
|
|
177
|
+
local_durable_through INTEGER NOT NULL,
|
|
178
|
+
cloud_ack_through INTEGER NOT NULL,
|
|
179
|
+
retained_from INTEGER NOT NULL,
|
|
180
|
+
PRIMARY KEY (runtime_id, generation, runtime_epoch)
|
|
181
|
+
);
|
|
182
|
+
CREATE TABLE IF NOT EXISTS source_events (
|
|
183
|
+
runtime_id TEXT NOT NULL,
|
|
184
|
+
generation INTEGER NOT NULL,
|
|
185
|
+
runtime_epoch INTEGER NOT NULL,
|
|
186
|
+
source_sequence INTEGER NOT NULL,
|
|
187
|
+
payload_hash TEXT NOT NULL,
|
|
188
|
+
workspace_id TEXT,
|
|
189
|
+
session_id TEXT,
|
|
190
|
+
operation_id TEXT,
|
|
191
|
+
thread_id TEXT,
|
|
192
|
+
turn_id TEXT,
|
|
193
|
+
item_id TEXT,
|
|
194
|
+
type TEXT NOT NULL,
|
|
195
|
+
payload_json TEXT NOT NULL,
|
|
196
|
+
occurred_at TEXT NOT NULL,
|
|
197
|
+
PRIMARY KEY (runtime_id, generation, runtime_epoch, source_sequence)
|
|
198
|
+
);
|
|
199
|
+
CREATE TABLE IF NOT EXISTS native_inbox (
|
|
200
|
+
ingress_sequence INTEGER PRIMARY KEY,
|
|
201
|
+
runtime_epoch INTEGER NOT NULL,
|
|
202
|
+
kind TEXT NOT NULL,
|
|
203
|
+
method TEXT,
|
|
204
|
+
thread_id TEXT,
|
|
205
|
+
turn_id TEXT,
|
|
206
|
+
item_id TEXT,
|
|
207
|
+
payload_json TEXT NOT NULL,
|
|
208
|
+
converted INTEGER NOT NULL DEFAULT 0,
|
|
209
|
+
source_sequence INTEGER,
|
|
210
|
+
received_at TEXT NOT NULL
|
|
211
|
+
);
|
|
212
|
+
CREATE TABLE IF NOT EXISTS operation_intents (
|
|
213
|
+
scope_key TEXT PRIMARY KEY,
|
|
214
|
+
operation_json TEXT NOT NULL,
|
|
215
|
+
gateway_operation_id TEXT,
|
|
216
|
+
sandbox_operation_id TEXT NOT NULL,
|
|
217
|
+
payload_hash TEXT NOT NULL,
|
|
218
|
+
native_request_id TEXT,
|
|
219
|
+
thread_id TEXT,
|
|
220
|
+
turn_id TEXT,
|
|
221
|
+
reconcile_status TEXT NOT NULL
|
|
222
|
+
);
|
|
223
|
+
CREATE TABLE IF NOT EXISTS replay_pins (
|
|
224
|
+
pin_id TEXT PRIMARY KEY,
|
|
225
|
+
epochs_json TEXT NOT NULL,
|
|
226
|
+
created_at TEXT NOT NULL,
|
|
227
|
+
expires_at TEXT NOT NULL
|
|
228
|
+
);
|
|
229
|
+
CREATE TABLE IF NOT EXISTS records (
|
|
230
|
+
bucket TEXT NOT NULL,
|
|
231
|
+
record_key TEXT NOT NULL,
|
|
232
|
+
body_json TEXT NOT NULL,
|
|
233
|
+
PRIMARY KEY (bucket, record_key)
|
|
234
|
+
);
|
|
235
|
+
`);
|
|
236
|
+
setMeta(database, 'schema_version', String(SCHEMA_VERSION));
|
|
237
|
+
setMeta(database, 'migration_status', 'complete');
|
|
238
|
+
setMeta(database, 'source_read_token', randomBytes(32).toString('base64url'));
|
|
239
|
+
setMeta(database, 'accepted_lease_fence', '0');
|
|
240
|
+
setMeta(database, 'destroyed', '0');
|
|
241
|
+
setMeta(database, 'runtime_epoch', '0');
|
|
242
|
+
setMeta(database, 'ingress_counter', '0');
|
|
243
|
+
setMeta(database, 'execution_group', JSON.stringify({ stopConfirmed: false, replaced: false }));
|
|
244
|
+
}
|
|
245
|
+
function readSchemaVersion(database) {
|
|
246
|
+
const table = database
|
|
247
|
+
.prepare(`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'library_meta'`)
|
|
248
|
+
.get();
|
|
249
|
+
if (!table)
|
|
250
|
+
return null;
|
|
251
|
+
const row = database.prepare(`SELECT value FROM library_meta WHERE key = 'schema_version'`).get();
|
|
252
|
+
if (!row?.value)
|
|
253
|
+
return null;
|
|
254
|
+
const parsed = Number(row.value);
|
|
255
|
+
return Number.isInteger(parsed) ? parsed : null;
|
|
256
|
+
}
|
|
257
|
+
function importLegacy(database, legacy) {
|
|
258
|
+
const snapshot = asRecord(legacy);
|
|
259
|
+
const operations = asRecord(snapshot.operations);
|
|
260
|
+
for (const [scopeKey, value] of Object.entries(operations)) {
|
|
261
|
+
const parsed = operationRecordSchema.safeParse(value);
|
|
262
|
+
if (!parsed.success)
|
|
263
|
+
continue;
|
|
264
|
+
putOperation(database, scopeKey, parsed.data);
|
|
265
|
+
}
|
|
266
|
+
const events = asRecord(snapshot.events);
|
|
267
|
+
const sequences = [];
|
|
268
|
+
for (const value of Object.values(events)) {
|
|
269
|
+
const parsed = pendingSourceEventSchema.safeParse(value);
|
|
270
|
+
if (!parsed.success)
|
|
271
|
+
continue;
|
|
272
|
+
const hash = hashOperationPayload(parsed.data.payload);
|
|
273
|
+
if (hash !== parsed.data.payloadHash)
|
|
274
|
+
continue;
|
|
275
|
+
insertEventRow(database, parsed.data, {});
|
|
276
|
+
sequences.push(parsed.data.source.sourceSequence);
|
|
277
|
+
}
|
|
278
|
+
const declared = Number(snapshot.lastSourceSequence) || 0;
|
|
279
|
+
const maxImported = sequences.reduce((max, value) => Math.max(max, value), 0);
|
|
280
|
+
const counter = Math.max(declared, maxImported, 0);
|
|
281
|
+
const runtimeId = firstString(snapshot, 'runtimeId') || eventRuntime(events, 'runtimeId');
|
|
282
|
+
const generation = Number.isInteger(snapshot.generation) ? Number(snapshot.generation) : eventRuntimeNumber(events, 'generation');
|
|
283
|
+
const runtimeEpoch = Number.isInteger(snapshot.runtimeEpoch)
|
|
284
|
+
? Number(snapshot.runtimeEpoch)
|
|
285
|
+
: eventRuntimeNumber(events, 'runtimeEpoch');
|
|
286
|
+
if (runtimeId) {
|
|
287
|
+
ensureEpoch(database, runtimeId, generation, runtimeEpoch);
|
|
288
|
+
database
|
|
289
|
+
.prepare(`UPDATE source_epochs SET next_sequence = ?, local_durable_through = ?
|
|
290
|
+
WHERE runtime_id = ? AND generation = ? AND runtime_epoch = ?`)
|
|
291
|
+
.run(counter, counter, runtimeId, generation, runtimeEpoch);
|
|
292
|
+
setMeta(database, 'runtime_id', runtimeId);
|
|
293
|
+
setMeta(database, 'generation', String(generation));
|
|
294
|
+
}
|
|
295
|
+
importBucket(database, 'workspaces', asRecord(snapshot.workspaces), (value) => workspaceRegistrationSchema.safeParse(value).success);
|
|
296
|
+
importBucket(database, 'sessions', asRecord(snapshot.sessions), (value) => {
|
|
297
|
+
const session = asRecord(value);
|
|
298
|
+
return typeof session.sessionId === 'string' && typeof session.threadId === 'string';
|
|
299
|
+
});
|
|
300
|
+
importBucket(database, 'terminals', asRecord(snapshot.terminals), (value) => terminalRecordSchema.safeParse(value).success);
|
|
301
|
+
importBucket(database, 'checkpoints', asRecord(snapshot.checkpoints), (value) => checkpointRecordSchema.safeParse(value).success);
|
|
302
|
+
importBucket(database, 'approvals', asRecord(snapshot.approvals), (value) => isApproval(value));
|
|
303
|
+
importBucket(database, 'managed_writes', asRecord(snapshot.managedWrites), () => true);
|
|
304
|
+
importBucket(database, 'file_revisions', asRecord(snapshot.fileRevisions), () => true);
|
|
305
|
+
importBucket(database, 'receipts', asRecord(snapshot.receipts), () => true);
|
|
306
|
+
const workspaceFiles = asRecord(snapshot.workspaceFileRevisions);
|
|
307
|
+
for (const [workspaceId, files] of Object.entries(workspaceFiles)) {
|
|
308
|
+
importBucket(database, `workspace_files:${workspaceId}`, asRecord(files), () => true);
|
|
309
|
+
}
|
|
310
|
+
if (typeof snapshot.fileCheckpointId === 'string')
|
|
311
|
+
setMeta(database, 'file_checkpoint_id', snapshot.fileCheckpointId);
|
|
312
|
+
if (typeof snapshot.consistentCheckpointId === 'string') {
|
|
313
|
+
setMeta(database, 'consistent_checkpoint_id', snapshot.consistentCheckpointId);
|
|
314
|
+
}
|
|
315
|
+
setMeta(database, 'accepted_lease_fence', String(Number(snapshot.acceptedLeaseFence) || 0));
|
|
316
|
+
setMeta(database, 'destroyed', snapshot.destroyed === true ? '1' : '0');
|
|
317
|
+
setMeta(database, 'runtime_epoch', String(runtimeEpoch));
|
|
318
|
+
if (snapshot.executionGroup && typeof snapshot.executionGroup === 'object') {
|
|
319
|
+
setMeta(database, 'execution_group', JSON.stringify(snapshot.executionGroup));
|
|
320
|
+
}
|
|
321
|
+
setMeta(database, 'migration_status', 'complete');
|
|
322
|
+
}
|
|
323
|
+
function quarantineReservedTurns(database) {
|
|
324
|
+
const rows = database.prepare('SELECT scope_key, operation_json FROM operation_intents').all();
|
|
325
|
+
for (const row of rows) {
|
|
326
|
+
const parsed = operationRecordSchema.safeParse(JSON.parse(row.operation_json));
|
|
327
|
+
if (!parsed.success)
|
|
328
|
+
continue;
|
|
329
|
+
const operation = parsed.data;
|
|
330
|
+
if (operation.kind !== 'session.turn' || operation.status !== 'dispatched')
|
|
331
|
+
continue;
|
|
332
|
+
if (!operation.nativeRequestId || operation.nativeTurnId)
|
|
333
|
+
continue;
|
|
334
|
+
const uncertain = transitionOperation(operation, 'uncertain', { error: 'native_receipt_lost' });
|
|
335
|
+
putOperation(database, row.scope_key, uncertain);
|
|
336
|
+
if (operation.sessionId) {
|
|
337
|
+
const session = readSession(database, operation.sessionId);
|
|
338
|
+
if (session) {
|
|
339
|
+
upsertRecord(database, 'sessions', operation.sessionId, { ...session, turnBlock: 'uncertain', turnStatus: 'starting' });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
function allocateEvent(database, body) {
|
|
345
|
+
const runtimeId = String(body.runtimeId);
|
|
346
|
+
const generation = Number(body.generation);
|
|
347
|
+
const runtimeEpoch = Number(body.runtimeEpoch);
|
|
348
|
+
ensureEpoch(database, runtimeId, generation, runtimeEpoch);
|
|
349
|
+
const allocated = database
|
|
350
|
+
.prepare(`UPDATE source_epochs
|
|
351
|
+
SET next_sequence = next_sequence + 1,
|
|
352
|
+
local_durable_through = next_sequence + 1
|
|
353
|
+
WHERE runtime_id = ? AND generation = ? AND runtime_epoch = ?
|
|
354
|
+
RETURNING next_sequence`)
|
|
355
|
+
.get(runtimeId, generation, runtimeEpoch);
|
|
356
|
+
const payload = body.payload ?? {};
|
|
357
|
+
const event = pendingSourceEventSchema.parse({
|
|
358
|
+
source: { runtimeId, generation, runtimeEpoch, sourceSequence: allocated.next_sequence },
|
|
359
|
+
payloadHash: hashOperationPayload(payload),
|
|
360
|
+
type: String(body.type),
|
|
361
|
+
payload,
|
|
362
|
+
occurredAt: String(body.occurredAt),
|
|
363
|
+
});
|
|
364
|
+
insertEventRow(database, event, {
|
|
365
|
+
workspaceId: optionalString(body.workspaceId),
|
|
366
|
+
sessionId: optionalString(body.sessionId),
|
|
367
|
+
operationId: optionalString(body.operationId),
|
|
368
|
+
threadId: optionalString(body.threadId),
|
|
369
|
+
turnId: optionalString(body.turnId),
|
|
370
|
+
itemId: optionalString(body.itemId),
|
|
371
|
+
});
|
|
372
|
+
return { committedEvents: [event] };
|
|
373
|
+
}
|
|
374
|
+
function commitIdentity(database, value) {
|
|
375
|
+
const event = pendingSourceEventSchema.parse(value);
|
|
376
|
+
const existing = database
|
|
377
|
+
.prepare(`SELECT payload_hash, payload_json, type, occurred_at FROM source_events
|
|
378
|
+
WHERE runtime_id = ? AND generation = ? AND runtime_epoch = ? AND source_sequence = ?`)
|
|
379
|
+
.get(event.source.runtimeId, event.source.generation, event.source.runtimeEpoch, event.source.sourceSequence);
|
|
380
|
+
if (existing) {
|
|
381
|
+
if (existing.payload_hash !== event.payloadHash)
|
|
382
|
+
throw new Error('source_payload_mismatch');
|
|
383
|
+
return { committedEvents: [] };
|
|
384
|
+
}
|
|
385
|
+
ensureEpoch(database, event.source.runtimeId, event.source.generation, event.source.runtimeEpoch);
|
|
386
|
+
insertEventRow(database, event, {});
|
|
387
|
+
database
|
|
388
|
+
.prepare(`UPDATE source_epochs
|
|
389
|
+
SET next_sequence = CASE WHEN next_sequence < ? THEN ? ELSE next_sequence END,
|
|
390
|
+
local_durable_through = CASE WHEN local_durable_through < ? THEN ? ELSE local_durable_through END
|
|
391
|
+
WHERE runtime_id = ? AND generation = ? AND runtime_epoch = ?`)
|
|
392
|
+
.run(event.source.sourceSequence, event.source.sourceSequence, event.source.sourceSequence, event.source.sourceSequence, event.source.runtimeId, event.source.generation, event.source.runtimeEpoch);
|
|
393
|
+
return { committedEvents: [event] };
|
|
394
|
+
}
|
|
395
|
+
function ingest(database, body) {
|
|
396
|
+
const native = nativeIds(body.params);
|
|
397
|
+
const threadId = optionalString(body.threadId) || native.threadId;
|
|
398
|
+
const turnId = optionalString(body.turnId) || native.turnId;
|
|
399
|
+
const itemId = optionalString(body.itemId) || native.itemId;
|
|
400
|
+
const ingress = nextIngress(database);
|
|
401
|
+
const kind = String(body.kind);
|
|
402
|
+
const method = optionalString(body.method);
|
|
403
|
+
const session = threadId ? sessionByThread(database, threadId) : undefined;
|
|
404
|
+
const hintedTurn = turnId || nativeIds(body.params).turnId;
|
|
405
|
+
const operation = hintedTurn ? operationByTurn(database, hintedTurn) : undefined;
|
|
406
|
+
const linked = canConvert(kind, threadId, hintedTurn, session, operation);
|
|
407
|
+
database
|
|
408
|
+
.prepare(`INSERT INTO native_inbox (
|
|
409
|
+
ingress_sequence, runtime_epoch, kind, method, thread_id, turn_id, item_id, payload_json, converted, received_at
|
|
410
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, ?)`)
|
|
411
|
+
.run(ingress, Number(body.runtimeEpoch) || 0, kind, method ?? null, threadId ?? null, hintedTurn ?? null, itemId ?? null, JSON.stringify(body.params ?? {}), String(body.occurredAt));
|
|
412
|
+
if (!linked)
|
|
413
|
+
return { committedEvents: [] };
|
|
414
|
+
const earlier = operation
|
|
415
|
+
? releaseInbox(database, threadId ?? '', hintedTurn ?? '', { scopeKey: operation.scopeKey, operation: operation.operation }, ingress)
|
|
416
|
+
: [];
|
|
417
|
+
const event = convertInboxRow(database, ingress, {
|
|
418
|
+
runtimeId: String(body.runtimeId),
|
|
419
|
+
generation: Number(body.generation) || 0,
|
|
420
|
+
runtimeEpoch: Number(body.runtimeEpoch) || 0,
|
|
421
|
+
kind,
|
|
422
|
+
method,
|
|
423
|
+
threadId,
|
|
424
|
+
turnId: hintedTurn,
|
|
425
|
+
itemId,
|
|
426
|
+
occurredAt: String(body.occurredAt),
|
|
427
|
+
session,
|
|
428
|
+
operation,
|
|
429
|
+
});
|
|
430
|
+
return { committedEvents: event ? [...earlier, event] : earlier };
|
|
431
|
+
}
|
|
432
|
+
function bindTurn(database, body) {
|
|
433
|
+
const operation = operationRecordSchema.parse(body.operation);
|
|
434
|
+
putOperation(database, String(body.scopeKey), operation);
|
|
435
|
+
const sessionId = String(body.sessionId);
|
|
436
|
+
const session = readSession(database, sessionId);
|
|
437
|
+
if (session) {
|
|
438
|
+
upsertRecord(database, 'sessions', sessionId, {
|
|
439
|
+
...session,
|
|
440
|
+
turnStatus: 'running',
|
|
441
|
+
turnBlock: undefined,
|
|
442
|
+
activeTurnId: String(body.turnId),
|
|
443
|
+
threadId: String(body.threadId),
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
const threadId = String(body.threadId);
|
|
447
|
+
const turnId = String(body.turnId);
|
|
448
|
+
const released = releaseInbox(database, threadId, turnId, { scopeKey: String(body.scopeKey), operation });
|
|
449
|
+
applyRecordedTurn(database, threadId, turnId);
|
|
450
|
+
return { committedEvents: released };
|
|
451
|
+
}
|
|
452
|
+
function releaseInbox(database, threadId, turnId, bound, beforeIngress) {
|
|
453
|
+
const rows = database
|
|
454
|
+
.prepare(`SELECT ingress_sequence FROM native_inbox
|
|
455
|
+
WHERE converted = 0
|
|
456
|
+
AND (? IS NULL OR ingress_sequence < ?)
|
|
457
|
+
AND (turn_id = ? OR (thread_id = ? AND turn_id IS NULL))
|
|
458
|
+
ORDER BY ingress_sequence`)
|
|
459
|
+
.all(beforeIngress ?? null, beforeIngress ?? null, turnId, threadId);
|
|
460
|
+
const events = [];
|
|
461
|
+
for (const row of rows) {
|
|
462
|
+
const stored = database.prepare('SELECT * FROM native_inbox WHERE ingress_sequence = ?').get(row.ingress_sequence);
|
|
463
|
+
if (stored.turn_id && stored.turn_id !== turnId)
|
|
464
|
+
continue;
|
|
465
|
+
const session = sessionByThread(database, threadId);
|
|
466
|
+
const operation = operationByTurn(database, turnId) ?? bound;
|
|
467
|
+
const event = convertInboxRow(database, row.ingress_sequence, {
|
|
468
|
+
runtimeId: metaString(database, 'runtime_id') || 'runtime',
|
|
469
|
+
generation: metaNumber(database, 'generation'),
|
|
470
|
+
runtimeEpoch: Number(stored.runtime_epoch) || metaNumber(database, 'runtime_epoch'),
|
|
471
|
+
kind: String(stored.kind),
|
|
472
|
+
method: optionalString(stored.method),
|
|
473
|
+
threadId,
|
|
474
|
+
turnId: optionalString(stored.turn_id) || turnId,
|
|
475
|
+
itemId: optionalString(stored.item_id),
|
|
476
|
+
occurredAt: String(stored.received_at),
|
|
477
|
+
session,
|
|
478
|
+
operation,
|
|
479
|
+
params: JSON.parse(String(stored.payload_json || '{}')),
|
|
480
|
+
});
|
|
481
|
+
if (event)
|
|
482
|
+
events.push(event);
|
|
483
|
+
}
|
|
484
|
+
return events;
|
|
485
|
+
}
|
|
486
|
+
function convertInboxRow(database, ingress, input) {
|
|
487
|
+
const row = database.prepare('SELECT payload_json, converted FROM native_inbox WHERE ingress_sequence = ?').get(ingress);
|
|
488
|
+
if (!row || row.converted)
|
|
489
|
+
return undefined;
|
|
490
|
+
const params = input.params ?? JSON.parse(row.payload_json || '{}');
|
|
491
|
+
const payload = linkedPayload(params, input.session);
|
|
492
|
+
const type = input.kind === 'process_exit' ? 'process.exited' : input.method || input.kind;
|
|
493
|
+
const allocated = allocateEvent(database, {
|
|
494
|
+
runtimeId: input.runtimeId || metaString(database, 'runtime_id'),
|
|
495
|
+
generation: input.generation,
|
|
496
|
+
runtimeEpoch: input.runtimeEpoch,
|
|
497
|
+
type,
|
|
498
|
+
payload,
|
|
499
|
+
occurredAt: input.occurredAt,
|
|
500
|
+
workspaceId: optionalString(input.session?.workspaceId),
|
|
501
|
+
sessionId: optionalString(input.session?.sessionId),
|
|
502
|
+
operationId: input.operation?.operation.operationId,
|
|
503
|
+
threadId: input.threadId,
|
|
504
|
+
turnId: input.turnId,
|
|
505
|
+
itemId: input.itemId,
|
|
506
|
+
});
|
|
507
|
+
const event = allocated.committedEvents[0];
|
|
508
|
+
if (!event)
|
|
509
|
+
return undefined;
|
|
510
|
+
database
|
|
511
|
+
.prepare(`UPDATE native_inbox
|
|
512
|
+
SET converted = 1, source_sequence = ?, payload_json = '{}'
|
|
513
|
+
WHERE ingress_sequence = ?`)
|
|
514
|
+
.run(event.source.sourceSequence, ingress);
|
|
515
|
+
applyNativeToState(database, { ...input, operation: resolveOperation(database, input, payload) }, payload);
|
|
516
|
+
return event;
|
|
517
|
+
}
|
|
518
|
+
function applyRecordedTurn(database, threadId, turnId) {
|
|
519
|
+
const rows = database
|
|
520
|
+
.prepare(`SELECT type, payload_json, thread_id, session_id FROM source_events
|
|
521
|
+
WHERE turn_id = ? OR payload_json LIKE ?
|
|
522
|
+
ORDER BY source_sequence`)
|
|
523
|
+
.all(turnId, `%"${turnId}"%`);
|
|
524
|
+
for (const row of rows) {
|
|
525
|
+
if (!row.type.includes('requestApproval') && !row.type.includes('requestUserInput') && !row.type.includes('completed') && !row.type.includes('interrupted') && row.type !== 'turn.failed' && row.type !== 'approval.resolved') {
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
528
|
+
const payload = asRecord(JSON.parse(row.payload_json));
|
|
529
|
+
const session = sessionByThread(database, threadId) ?? (row.session_id ? readSession(database, row.session_id) : undefined);
|
|
530
|
+
applyNativeToState(database, {
|
|
531
|
+
method: row.type,
|
|
532
|
+
kind: 'notification',
|
|
533
|
+
threadId: optionalString(row.thread_id) || threadId,
|
|
534
|
+
turnId,
|
|
535
|
+
session,
|
|
536
|
+
operation: operationByTurn(database, turnId),
|
|
537
|
+
}, payload);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
function resolveOperation(database, input, payload) {
|
|
541
|
+
const turnId = input.turnId || nativeIds(payload).turnId;
|
|
542
|
+
if (turnId)
|
|
543
|
+
return operationByTurn(database, turnId) ?? input.operation;
|
|
544
|
+
return input.operation;
|
|
545
|
+
}
|
|
546
|
+
function applyNativeToState(database, input, payload) {
|
|
547
|
+
const method = input.method ?? '';
|
|
548
|
+
const session = input.session;
|
|
549
|
+
if (session?.sessionId) {
|
|
550
|
+
const next = { ...session };
|
|
551
|
+
const approval = method.includes('requestApproval') || method.includes('requestUserInput');
|
|
552
|
+
if (approval)
|
|
553
|
+
next.turnStatus = moveTurn(String(session.turnStatus ?? 'running'), 'waiting_for_user');
|
|
554
|
+
else if (method === 'approval.resolved')
|
|
555
|
+
next.turnStatus = moveTurn(String(session.turnStatus ?? 'waiting_for_user'), 'running');
|
|
556
|
+
else if (method.includes('delta') || method === 'turn/started' || method === 'turn.started') {
|
|
557
|
+
const current = String(session.turnStatus ?? 'starting');
|
|
558
|
+
if (current !== 'waiting_for_user')
|
|
559
|
+
next.turnStatus = moveTurn(current, 'running');
|
|
560
|
+
}
|
|
561
|
+
const terminal = terminalTurn(method, payload);
|
|
562
|
+
if (terminal) {
|
|
563
|
+
let status = String(next.turnStatus ?? session.turnStatus ?? 'running');
|
|
564
|
+
if (status === 'waiting_for_user')
|
|
565
|
+
status = moveTurn(status, 'running');
|
|
566
|
+
next.turnStatus = moveTurn(status, terminal);
|
|
567
|
+
next.turnBlock = undefined;
|
|
568
|
+
}
|
|
569
|
+
upsertRecord(database, 'sessions', String(session.sessionId), next);
|
|
570
|
+
}
|
|
571
|
+
if (!input.operation)
|
|
572
|
+
return;
|
|
573
|
+
let operation = input.operation.operation;
|
|
574
|
+
const approvalId = optionalString(payload.itemId) || optionalString(payload.approvalId) || input.itemId;
|
|
575
|
+
if ((method.includes('requestApproval') || method.includes('requestUserInput')) && approvalId && operation.sessionId) {
|
|
576
|
+
upsertRecord(database, 'approvals', `${operation.sessionId}:${approvalId}`, {
|
|
577
|
+
approvalId,
|
|
578
|
+
sessionId: operation.sessionId,
|
|
579
|
+
runtimeEpoch: operation.runtimeEpoch ?? 0,
|
|
580
|
+
nativeRequestId: operation.nativeRequestId ?? `approval:${approvalId}`,
|
|
581
|
+
status: 'pending',
|
|
582
|
+
});
|
|
583
|
+
operation = operationRecordSchema.parse({
|
|
584
|
+
...operation,
|
|
585
|
+
nativeApprovalId: approvalId,
|
|
586
|
+
updatedAt: new Date().toISOString(),
|
|
587
|
+
});
|
|
588
|
+
putOperation(database, input.operation.scopeKey, operation);
|
|
589
|
+
}
|
|
590
|
+
const terminal = terminalTurn(method, payload);
|
|
591
|
+
if (terminal && operation.status === 'accepted') {
|
|
592
|
+
putOperation(database, input.operation.scopeKey, transitionOperation(operation, terminal, {
|
|
593
|
+
nativeTurnId: input.turnId ?? operation.nativeTurnId,
|
|
594
|
+
nativeThreadId: input.threadId ?? operation.nativeThreadId,
|
|
595
|
+
}));
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
function acknowledge(database, body) {
|
|
599
|
+
const leaseFence = Number(body.leaseFence);
|
|
600
|
+
const accepted = metaNumber(database, 'accepted_lease_fence');
|
|
601
|
+
if (!Number.isInteger(leaseFence) || leaseFence !== accepted)
|
|
602
|
+
throw new Error('lease_fence_mismatch');
|
|
603
|
+
const epochs = Array.isArray(body.epochs) ? body.epochs : [];
|
|
604
|
+
for (const value of epochs) {
|
|
605
|
+
const epoch = asRecord(value);
|
|
606
|
+
const row = epochRow(database, String(epoch.runtimeId), Number(epoch.generation), Number(epoch.runtimeEpoch));
|
|
607
|
+
if (!row)
|
|
608
|
+
throw new Error('unknown_source_epoch');
|
|
609
|
+
const next = Number(epoch.confirmedThrough);
|
|
610
|
+
if (!Number.isInteger(next) || next < row.cloud_ack_through)
|
|
611
|
+
throw new Error('ack_not_monotonic');
|
|
612
|
+
if (next > row.local_durable_through)
|
|
613
|
+
throw new Error('ack_ahead_of_local');
|
|
614
|
+
database
|
|
615
|
+
.prepare(`UPDATE source_epochs SET cloud_ack_through = ?
|
|
616
|
+
WHERE runtime_id = ? AND generation = ? AND runtime_epoch = ?`)
|
|
617
|
+
.run(next, epoch.runtimeId, epoch.generation, epoch.runtimeEpoch);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
function createPin(database, body) {
|
|
621
|
+
const now = Date.now();
|
|
622
|
+
database.prepare('DELETE FROM replay_pins WHERE expires_at <= ?').run(new Date(now).toISOString());
|
|
623
|
+
const count = database.prepare('SELECT count(*) AS n FROM replay_pins').get().n;
|
|
624
|
+
if (count >= MAX_PINS)
|
|
625
|
+
throw new Error('pin_quota');
|
|
626
|
+
const ttl = Math.min(Number(body.ttlMs) || 60_000, MAX_PIN_MS);
|
|
627
|
+
const identity = asRecord(body.identity);
|
|
628
|
+
ensureEpoch(database, String(identity.runtimeId), Number(identity.generation), Number(identity.runtimeEpoch));
|
|
629
|
+
const epochs = database.prepare('SELECT * FROM source_epochs').all().map((row) => ({
|
|
630
|
+
runtimeId: row.runtime_id,
|
|
631
|
+
generation: row.generation,
|
|
632
|
+
runtimeEpoch: row.runtime_epoch,
|
|
633
|
+
retainedFrom: row.retained_from,
|
|
634
|
+
highWater: row.local_durable_through,
|
|
635
|
+
}));
|
|
636
|
+
const current = epochs.find((epoch) => epoch.runtimeId === identity.runtimeId &&
|
|
637
|
+
epoch.generation === Number(identity.generation) &&
|
|
638
|
+
epoch.runtimeEpoch === Number(identity.runtimeEpoch));
|
|
639
|
+
const pinId = `pin_${randomUUID()}`;
|
|
640
|
+
const createdAt = new Date(now).toISOString();
|
|
641
|
+
const expiresAt = new Date(now + ttl).toISOString();
|
|
642
|
+
database
|
|
643
|
+
.prepare('INSERT INTO replay_pins (pin_id, epochs_json, created_at, expires_at) VALUES (?, ?, ?, ?)')
|
|
644
|
+
.run(pinId, JSON.stringify(epochs), createdAt, expiresAt);
|
|
645
|
+
const highWater = current?.highWater ?? 0;
|
|
646
|
+
return {
|
|
647
|
+
pin: {
|
|
648
|
+
pinId,
|
|
649
|
+
epochs,
|
|
650
|
+
followBoundary: {
|
|
651
|
+
runtimeId: String(identity.runtimeId),
|
|
652
|
+
generation: Number(identity.generation),
|
|
653
|
+
runtimeEpoch: Number(identity.runtimeEpoch),
|
|
654
|
+
sourceSequence: Math.max(highWater, 1),
|
|
655
|
+
},
|
|
656
|
+
},
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
function reclaim(database, minRetainMs) {
|
|
660
|
+
const pins = activePins(database);
|
|
661
|
+
const epochs = database.prepare('SELECT * FROM source_epochs').all();
|
|
662
|
+
const now = Date.now();
|
|
663
|
+
for (const epoch of epochs) {
|
|
664
|
+
let retained = epoch.retained_from;
|
|
665
|
+
while (true) {
|
|
666
|
+
const row = database
|
|
667
|
+
.prepare(`SELECT source_sequence, occurred_at FROM source_events
|
|
668
|
+
WHERE runtime_id = ? AND generation = ? AND runtime_epoch = ? AND source_sequence >= ?
|
|
669
|
+
ORDER BY source_sequence LIMIT 1`)
|
|
670
|
+
.get(epoch.runtime_id, epoch.generation, epoch.runtime_epoch, retained);
|
|
671
|
+
if (!row || row.source_sequence !== retained)
|
|
672
|
+
break;
|
|
673
|
+
if (row.source_sequence > epoch.cloud_ack_through)
|
|
674
|
+
break;
|
|
675
|
+
if (pins.some((pin) => row.source_sequence >= pin.retainedFrom && row.source_sequence <= pin.highWater && pin.runtimeId === epoch.runtime_id && pin.generation === epoch.generation && pin.runtimeEpoch === epoch.runtime_epoch)) {
|
|
676
|
+
break;
|
|
677
|
+
}
|
|
678
|
+
const age = now - Date.parse(row.occurred_at);
|
|
679
|
+
if (Number.isFinite(age) && age < minRetainMs)
|
|
680
|
+
break;
|
|
681
|
+
database
|
|
682
|
+
.prepare(`DELETE FROM source_events
|
|
683
|
+
WHERE runtime_id = ? AND generation = ? AND runtime_epoch = ? AND source_sequence = ?`)
|
|
684
|
+
.run(epoch.runtime_id, epoch.generation, epoch.runtime_epoch, row.source_sequence);
|
|
685
|
+
retained = row.source_sequence + 1;
|
|
686
|
+
}
|
|
687
|
+
if (retained !== epoch.retained_from) {
|
|
688
|
+
database
|
|
689
|
+
.prepare(`UPDATE source_epochs SET retained_from = ?
|
|
690
|
+
WHERE runtime_id = ? AND generation = ? AND runtime_epoch = ?`)
|
|
691
|
+
.run(retained, epoch.runtime_id, epoch.generation, epoch.runtime_epoch);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
function dump(database) {
|
|
696
|
+
const snapshot = {
|
|
697
|
+
workspaces: bucketObject(database, 'workspaces'),
|
|
698
|
+
workspaceFileRevisions: workspaceFiles(database),
|
|
699
|
+
operations: operationsObject(database),
|
|
700
|
+
receipts: bucketObject(database, 'receipts'),
|
|
701
|
+
events: eventsObject(database),
|
|
702
|
+
sessions: bucketObject(database, 'sessions'),
|
|
703
|
+
terminals: bucketObject(database, 'terminals'),
|
|
704
|
+
checkpoints: bucketObject(database, 'checkpoints'),
|
|
705
|
+
fileRevisions: bucketObject(database, 'file_revisions'),
|
|
706
|
+
approvals: bucketObject(database, 'approvals'),
|
|
707
|
+
managedWrites: bucketObject(database, 'managed_writes'),
|
|
708
|
+
fileCheckpointId: metaString(database, 'file_checkpoint_id') || undefined,
|
|
709
|
+
consistentCheckpointId: metaString(database, 'consistent_checkpoint_id') || undefined,
|
|
710
|
+
executionGroup: executionGroup(database),
|
|
711
|
+
runtimeEpoch: metaNumber(database, 'runtime_epoch'),
|
|
712
|
+
lastSourceSequence: currentCounter(database),
|
|
713
|
+
acceptedLeaseFence: metaNumber(database, 'accepted_lease_fence'),
|
|
714
|
+
destroyed: metaString(database, 'destroyed') === '1',
|
|
715
|
+
};
|
|
716
|
+
return {
|
|
717
|
+
snapshot,
|
|
718
|
+
epochs: database.prepare('SELECT * FROM source_epochs').all(),
|
|
719
|
+
sourceReadToken: metaString(database, 'source_read_token'),
|
|
720
|
+
pendingInbox: database.prepare('SELECT count(*) AS n FROM native_inbox WHERE converted = 0').get().n,
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
function eventsObject(database) {
|
|
724
|
+
const rows = database.prepare('SELECT * FROM source_events ORDER BY runtime_epoch, source_sequence').all();
|
|
725
|
+
const events = {};
|
|
726
|
+
for (const row of rows) {
|
|
727
|
+
const event = pendingSourceEventSchema.parse({
|
|
728
|
+
source: {
|
|
729
|
+
runtimeId: row.runtime_id,
|
|
730
|
+
generation: row.generation,
|
|
731
|
+
runtimeEpoch: row.runtime_epoch,
|
|
732
|
+
sourceSequence: row.source_sequence,
|
|
733
|
+
},
|
|
734
|
+
payloadHash: row.payload_hash,
|
|
735
|
+
type: row.type,
|
|
736
|
+
payload: JSON.parse(String(row.payload_json)),
|
|
737
|
+
occurredAt: row.occurred_at,
|
|
738
|
+
});
|
|
739
|
+
events[`${event.source.runtimeId}:${event.source.generation}:${event.source.runtimeEpoch}:${event.source.sourceSequence}`] = event;
|
|
740
|
+
}
|
|
741
|
+
return events;
|
|
742
|
+
}
|
|
743
|
+
function operationsObject(database) {
|
|
744
|
+
const rows = database.prepare('SELECT scope_key, operation_json FROM operation_intents').all();
|
|
745
|
+
const operations = {};
|
|
746
|
+
for (const row of rows) {
|
|
747
|
+
const parsed = operationRecordSchema.safeParse(JSON.parse(row.operation_json));
|
|
748
|
+
if (parsed.success)
|
|
749
|
+
operations[row.scope_key] = parsed.data;
|
|
750
|
+
}
|
|
751
|
+
return operations;
|
|
752
|
+
}
|
|
753
|
+
function putOperation(database, scopeKey, value) {
|
|
754
|
+
const operation = operationRecordSchema.parse(value);
|
|
755
|
+
database
|
|
756
|
+
.prepare(`INSERT INTO operation_intents (
|
|
757
|
+
scope_key, operation_json, gateway_operation_id, sandbox_operation_id, payload_hash,
|
|
758
|
+
native_request_id, thread_id, turn_id, reconcile_status
|
|
759
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
760
|
+
ON CONFLICT(scope_key) DO UPDATE SET
|
|
761
|
+
operation_json = excluded.operation_json,
|
|
762
|
+
gateway_operation_id = excluded.gateway_operation_id,
|
|
763
|
+
sandbox_operation_id = excluded.sandbox_operation_id,
|
|
764
|
+
payload_hash = excluded.payload_hash,
|
|
765
|
+
native_request_id = excluded.native_request_id,
|
|
766
|
+
thread_id = excluded.thread_id,
|
|
767
|
+
turn_id = excluded.turn_id,
|
|
768
|
+
reconcile_status = excluded.reconcile_status`)
|
|
769
|
+
.run(scopeKey, JSON.stringify(operation), null, operation.sandboxOperationId ?? operation.operationId, operation.payloadHash, operation.nativeRequestId ?? null, operation.nativeThreadId ?? null, operation.nativeTurnId ?? null, operation.status);
|
|
770
|
+
}
|
|
771
|
+
function insertEventRow(database, event, links) {
|
|
772
|
+
database
|
|
773
|
+
.prepare(`INSERT INTO source_events (
|
|
774
|
+
runtime_id, generation, runtime_epoch, source_sequence, payload_hash, workspace_id, session_id,
|
|
775
|
+
operation_id, thread_id, turn_id, item_id, type, payload_json, occurred_at
|
|
776
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
777
|
+
.run(event.source.runtimeId, event.source.generation, event.source.runtimeEpoch, event.source.sourceSequence, event.payloadHash, links.workspaceId ?? null, links.sessionId ?? null, links.operationId ?? null, links.threadId ?? null, links.turnId ?? null, links.itemId ?? null, event.type, JSON.stringify(event.payload), event.occurredAt);
|
|
778
|
+
}
|
|
779
|
+
function ensureEpoch(database, runtimeId, generation, runtimeEpoch) {
|
|
780
|
+
database
|
|
781
|
+
.prepare(`INSERT INTO source_epochs (
|
|
782
|
+
runtime_id, generation, runtime_epoch, next_sequence, local_durable_through, cloud_ack_through, retained_from
|
|
783
|
+
) VALUES (?, ?, ?, 0, 0, 0, 1)
|
|
784
|
+
ON CONFLICT DO NOTHING`)
|
|
785
|
+
.run(runtimeId, generation, runtimeEpoch);
|
|
786
|
+
}
|
|
787
|
+
function writeExecutionEpoch(database, body) {
|
|
788
|
+
const runtimeEpoch = Number(body.runtimeEpoch);
|
|
789
|
+
setMeta(database, 'runtime_epoch', String(runtimeEpoch));
|
|
790
|
+
setMeta(database, 'execution_group', JSON.stringify({
|
|
791
|
+
pid: body.pid,
|
|
792
|
+
startedAt: body.startedAt,
|
|
793
|
+
previousPid: body.previousPid,
|
|
794
|
+
stopConfirmed: body.stopConfirmed === true,
|
|
795
|
+
replaced: true,
|
|
796
|
+
}));
|
|
797
|
+
const approvals = database.prepare(`SELECT record_key, body_json FROM records WHERE bucket = 'approvals'`).all();
|
|
798
|
+
for (const row of approvals) {
|
|
799
|
+
const approval = asRecord(JSON.parse(row.body_json));
|
|
800
|
+
if (approval.status === 'pending') {
|
|
801
|
+
upsertRecord(database, 'approvals', row.record_key, { ...approval, status: 'expired' });
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
const runtimeId = metaString(database, 'runtime_id');
|
|
805
|
+
const generation = metaNumber(database, 'generation');
|
|
806
|
+
if (runtimeId)
|
|
807
|
+
ensureEpoch(database, runtimeId, generation, runtimeEpoch);
|
|
808
|
+
}
|
|
809
|
+
function bindRuntime(database, body) {
|
|
810
|
+
setMeta(database, 'runtime_id', String(body.runtimeId));
|
|
811
|
+
setMeta(database, 'generation', String(Number(body.generation) || 0));
|
|
812
|
+
const current = metaNumber(database, 'runtime_epoch');
|
|
813
|
+
const next = Math.max(current, Number(body.runtimeEpoch) || 0);
|
|
814
|
+
setMeta(database, 'runtime_epoch', String(next));
|
|
815
|
+
ensureEpoch(database, String(body.runtimeId), Number(body.generation) || 0, next);
|
|
816
|
+
}
|
|
817
|
+
function upsertRecord(database, bucket, key, body) {
|
|
818
|
+
database
|
|
819
|
+
.prepare(`INSERT INTO records (bucket, record_key, body_json) VALUES (?, ?, ?)
|
|
820
|
+
ON CONFLICT(bucket, record_key) DO UPDATE SET body_json = excluded.body_json`)
|
|
821
|
+
.run(bucket, key, JSON.stringify(body));
|
|
822
|
+
}
|
|
823
|
+
function replaceBucket(database, bucket, entries) {
|
|
824
|
+
database.prepare('DELETE FROM records WHERE bucket = ?').run(bucket);
|
|
825
|
+
for (const entry of entries) {
|
|
826
|
+
const record = asRecord(entry);
|
|
827
|
+
upsertRecord(database, bucket, String(record.key), record.body);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
function importBucket(database, bucket, records, accept) {
|
|
831
|
+
for (const [key, value] of Object.entries(records)) {
|
|
832
|
+
if (accept(value))
|
|
833
|
+
upsertRecord(database, bucket, key, value);
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
function bucketObject(database, bucket) {
|
|
837
|
+
const rows = database.prepare('SELECT record_key, body_json FROM records WHERE bucket = ? ORDER BY rowid').all(bucket);
|
|
838
|
+
return Object.fromEntries(rows.map((row) => [row.record_key, JSON.parse(row.body_json)]));
|
|
839
|
+
}
|
|
840
|
+
function workspaceFiles(database) {
|
|
841
|
+
const rows = database
|
|
842
|
+
.prepare(`SELECT bucket, record_key, body_json FROM records WHERE bucket LIKE 'workspace_files:%'`)
|
|
843
|
+
.all();
|
|
844
|
+
const files = {};
|
|
845
|
+
for (const row of rows) {
|
|
846
|
+
const workspaceId = row.bucket.slice('workspace_files:'.length);
|
|
847
|
+
(files[workspaceId] ??= {})[row.record_key] = JSON.parse(row.body_json);
|
|
848
|
+
}
|
|
849
|
+
return files;
|
|
850
|
+
}
|
|
851
|
+
function readSession(database, sessionId) {
|
|
852
|
+
const row = database
|
|
853
|
+
.prepare(`SELECT body_json FROM records WHERE bucket = 'sessions' AND record_key = ?`)
|
|
854
|
+
.get(sessionId);
|
|
855
|
+
return row ? asRecord(JSON.parse(row.body_json)) : undefined;
|
|
856
|
+
}
|
|
857
|
+
function sessionByThread(database, threadId) {
|
|
858
|
+
const rows = database.prepare(`SELECT body_json FROM records WHERE bucket = 'sessions'`).all();
|
|
859
|
+
const matches = rows.map((row) => asRecord(JSON.parse(row.body_json))).filter((session) => session.threadId === threadId);
|
|
860
|
+
if (matches.length !== 1)
|
|
861
|
+
return undefined;
|
|
862
|
+
return matches[0];
|
|
863
|
+
}
|
|
864
|
+
function operationByTurn(database, turnId) {
|
|
865
|
+
const row = database
|
|
866
|
+
.prepare('SELECT scope_key, operation_json FROM operation_intents WHERE turn_id = ?')
|
|
867
|
+
.get(turnId);
|
|
868
|
+
if (!row)
|
|
869
|
+
return undefined;
|
|
870
|
+
const parsed = operationRecordSchema.safeParse(JSON.parse(row.operation_json));
|
|
871
|
+
if (!parsed.success)
|
|
872
|
+
return undefined;
|
|
873
|
+
return { scopeKey: row.scope_key, operation: parsed.data };
|
|
874
|
+
}
|
|
875
|
+
function canConvert(kind, threadId, turnId, session, operation) {
|
|
876
|
+
if (kind === 'process_exit')
|
|
877
|
+
return true;
|
|
878
|
+
if (turnId && !operation)
|
|
879
|
+
return false;
|
|
880
|
+
if (turnId && operation)
|
|
881
|
+
return true;
|
|
882
|
+
if (!session || !threadId)
|
|
883
|
+
return false;
|
|
884
|
+
return true;
|
|
885
|
+
}
|
|
886
|
+
function linkedPayload(params, session) {
|
|
887
|
+
const payload = asRecord(params);
|
|
888
|
+
delete payload.workspaceId;
|
|
889
|
+
if (typeof session?.workspaceId === 'string')
|
|
890
|
+
payload.workspaceId = session.workspaceId;
|
|
891
|
+
if (typeof session?.sessionId === 'string')
|
|
892
|
+
payload.sessionId = session.sessionId;
|
|
893
|
+
return payload;
|
|
894
|
+
}
|
|
895
|
+
function nativeIds(params) {
|
|
896
|
+
const record = asRecord(params);
|
|
897
|
+
const turn = asRecord(record.turn);
|
|
898
|
+
const thread = asRecord(record.thread);
|
|
899
|
+
return {
|
|
900
|
+
threadId: optionalString(record.threadId) || optionalString(thread.id),
|
|
901
|
+
turnId: optionalString(record.turnId) || optionalString(turn.id),
|
|
902
|
+
itemId: optionalString(record.itemId) || optionalString(record.approvalId),
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
function terminalTurn(method, payload) {
|
|
906
|
+
if (method !== 'turn/completed' && method !== 'turn.completed' && method !== 'turn.interrupted' && method !== 'turn.failed') {
|
|
907
|
+
return undefined;
|
|
908
|
+
}
|
|
909
|
+
if (method === 'turn.interrupted')
|
|
910
|
+
return 'interrupted';
|
|
911
|
+
if (method === 'turn.failed')
|
|
912
|
+
return 'failed';
|
|
913
|
+
const status = String(asRecord(payload.turn).status ?? payload.status ?? 'completed');
|
|
914
|
+
if (status === 'interrupted')
|
|
915
|
+
return 'interrupted';
|
|
916
|
+
if (status === 'failed')
|
|
917
|
+
return 'failed';
|
|
918
|
+
return 'completed';
|
|
919
|
+
}
|
|
920
|
+
function moveTurn(current, to) {
|
|
921
|
+
if (current === to)
|
|
922
|
+
return to;
|
|
923
|
+
if (current === 'starting' || current === 'running' || current === 'waiting_for_user' || current === 'completed' || current === 'failed' || current === 'interrupted') {
|
|
924
|
+
return canTransitionTurn(current, to) ? to : current;
|
|
925
|
+
}
|
|
926
|
+
return to;
|
|
927
|
+
}
|
|
928
|
+
function nextIngress(database) {
|
|
929
|
+
const next = metaNumber(database, 'ingress_counter') + 1;
|
|
930
|
+
setMeta(database, 'ingress_counter', String(next));
|
|
931
|
+
return next;
|
|
932
|
+
}
|
|
933
|
+
function epochRow(database, runtimeId, generation, runtimeEpoch) {
|
|
934
|
+
return database
|
|
935
|
+
.prepare('SELECT * FROM source_epochs WHERE runtime_id = ? AND generation = ? AND runtime_epoch = ?')
|
|
936
|
+
.get(runtimeId, generation, runtimeEpoch);
|
|
937
|
+
}
|
|
938
|
+
function activePins(database) {
|
|
939
|
+
const now = new Date().toISOString();
|
|
940
|
+
const rows = database.prepare('SELECT epochs_json, expires_at FROM replay_pins').all();
|
|
941
|
+
const pins = [];
|
|
942
|
+
for (const row of rows) {
|
|
943
|
+
if (row.expires_at <= now)
|
|
944
|
+
continue;
|
|
945
|
+
const epochs = JSON.parse(row.epochs_json);
|
|
946
|
+
for (const epoch of epochs)
|
|
947
|
+
pins.push(epoch);
|
|
948
|
+
}
|
|
949
|
+
return pins;
|
|
950
|
+
}
|
|
951
|
+
function currentCounter(database) {
|
|
952
|
+
const row = epochRow(database, metaString(database, 'runtime_id'), metaNumber(database, 'generation'), metaNumber(database, 'runtime_epoch'));
|
|
953
|
+
return row?.next_sequence ?? 0;
|
|
954
|
+
}
|
|
955
|
+
function executionGroup(database) {
|
|
956
|
+
const raw = metaString(database, 'execution_group');
|
|
957
|
+
if (!raw)
|
|
958
|
+
return { stopConfirmed: false, replaced: false };
|
|
959
|
+
try {
|
|
960
|
+
return asRecord(JSON.parse(raw));
|
|
961
|
+
}
|
|
962
|
+
catch {
|
|
963
|
+
return { stopConfirmed: false, replaced: false };
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
function setMeta(database, key, value) {
|
|
967
|
+
database
|
|
968
|
+
.prepare(`INSERT INTO library_meta (key, value) VALUES (?, ?)
|
|
969
|
+
ON CONFLICT(key) DO UPDATE SET value = excluded.value`)
|
|
970
|
+
.run(key, value);
|
|
971
|
+
}
|
|
972
|
+
function metaString(database, key) {
|
|
973
|
+
const row = database.prepare('SELECT value FROM library_meta WHERE key = ?').get(key);
|
|
974
|
+
return row?.value ?? '';
|
|
975
|
+
}
|
|
976
|
+
function metaNumber(database, key) {
|
|
977
|
+
const parsed = Number(metaString(database, key));
|
|
978
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
979
|
+
}
|
|
980
|
+
function requireDb() {
|
|
981
|
+
if (!db)
|
|
982
|
+
throw new Error('sqlite_not_open');
|
|
983
|
+
return db;
|
|
984
|
+
}
|
|
985
|
+
function sqliteAtLeast(version, minimum) {
|
|
986
|
+
const parts = version.split('.').map((item) => Number(item));
|
|
987
|
+
for (let index = 0; index < minimum.length; index += 1) {
|
|
988
|
+
const left = parts[index] ?? 0;
|
|
989
|
+
const right = minimum[index] ?? 0;
|
|
990
|
+
if (left > right)
|
|
991
|
+
return true;
|
|
992
|
+
if (left < right)
|
|
993
|
+
return false;
|
|
994
|
+
}
|
|
995
|
+
return true;
|
|
996
|
+
}
|
|
997
|
+
function asRecord(value) {
|
|
998
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? { ...value } : {};
|
|
999
|
+
}
|
|
1000
|
+
function optionalString(value) {
|
|
1001
|
+
return typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
1002
|
+
}
|
|
1003
|
+
function firstString(snapshot, key) {
|
|
1004
|
+
return typeof snapshot[key] === 'string' ? String(snapshot[key]) : undefined;
|
|
1005
|
+
}
|
|
1006
|
+
function eventRuntime(events, key) {
|
|
1007
|
+
for (const value of Object.values(events)) {
|
|
1008
|
+
const source = asRecord(asRecord(value).source);
|
|
1009
|
+
if (typeof source[key] === 'string')
|
|
1010
|
+
return String(source[key]);
|
|
1011
|
+
}
|
|
1012
|
+
return undefined;
|
|
1013
|
+
}
|
|
1014
|
+
function eventRuntimeNumber(events, key) {
|
|
1015
|
+
for (const value of Object.values(events)) {
|
|
1016
|
+
const source = asRecord(asRecord(value).source);
|
|
1017
|
+
if (Number.isInteger(source[key]))
|
|
1018
|
+
return Number(source[key]);
|
|
1019
|
+
}
|
|
1020
|
+
return 0;
|
|
1021
|
+
}
|
|
1022
|
+
function isApproval(value) {
|
|
1023
|
+
const record = asRecord(value);
|
|
1024
|
+
return typeof record.approvalId === 'string' && typeof record.sessionId === 'string' && typeof record.nativeRequestId === 'string';
|
|
1025
|
+
}
|