@awebai/oats 0.62.0 → 0.64.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.
Files changed (40) hide show
  1. package/README.md +14 -1
  2. package/bin/oats.mjs +58 -23
  3. package/docs/desktop-cli-api.md +73 -3
  4. package/docs/execution-targets.md +45 -2
  5. package/docs/first-team.md +6 -0
  6. package/docs/official-catalog.md +1 -1
  7. package/docs/packages.md +5 -5
  8. package/docs/release-lane.md +13 -0
  9. package/docs/release-notes/v0.63.0.md +135 -0
  10. package/docs/release-notes/v0.64.0.md +105 -0
  11. package/docs/souls-and-instances.md +32 -7
  12. package/docs/upgrading.md +6 -3
  13. package/docs/workspaces.md +1 -1
  14. package/lib/core.mjs +100 -28
  15. package/lib/session-input.mjs +12 -2
  16. package/lib/tree-copy.mjs +188 -7
  17. package/package-catalog.json +1 -1
  18. package/package.json +1 -1
  19. package/packages/client/cli-locator.mjs +2 -2
  20. package/packages/client/instance-tree.mjs +53 -3
  21. package/packages/client/roster-sections.mjs +22 -6
  22. package/packages/record/bin/capture.mjs +59 -17
  23. package/packages/record/bin/setup.mjs +2 -1
  24. package/packages/record/docs/capture-service.md +89 -5
  25. package/packages/record/lib/capture-command-operation.mjs +2 -2
  26. package/packages/record/lib/capture-command-output.mjs +3 -2
  27. package/packages/record/lib/capture-command.mjs +20 -3
  28. package/packages/record/lib/capture-hook-runner.mjs +1 -1
  29. package/packages/record/lib/capture-index-reset.mjs +1 -0
  30. package/packages/record/lib/capture-lock.mjs +67 -33
  31. package/packages/record/lib/capture-pass.mjs +22 -8
  32. package/packages/record/lib/capture-run.mjs +1 -1
  33. package/packages/record/lib/capture-service-runner.mjs +1 -1
  34. package/packages/record/lib/capture-supervisor.mjs +24 -5
  35. package/packages/record/lib/capture-work-session.mjs +7 -4
  36. package/packages/record/lib/capture-worker.mjs +20 -4
  37. package/packages/record/lib/derived-state.mjs +30 -2
  38. package/packages/record/lib/index-db.mjs +118 -49
  39. package/packages/record/lib/store.mjs +13 -6
  40. package/skills/oats-getting-started/SKILL.md +438 -170
@@ -23,12 +23,18 @@ export function fileVersion(path) {
23
23
  } catch (err) { if (err.code === "ENOENT") return null; throw err; }
24
24
  }
25
25
  export const sameVersion = (a, b) => JSON.stringify(a) === JSON.stringify(b);
26
+ function fileIdentity(path) {
27
+ try { const st = statSync(path, { bigint: true }); return { dev: String(st.dev), ino: String(st.ino) }; }
28
+ catch (err) { if (err.code === "ENOENT") return null; throw err; }
29
+ }
26
30
 
27
31
  export class DerivedState {
28
32
  constructor(root) {
29
33
  const { DatabaseSync } = require("node:sqlite");
30
34
  mkdirSync(join(root, "index"), { recursive: true });
31
- this.db = new DatabaseSync(join(root, "index", "capture.db"));
35
+ this.path = join(root, "index", "capture.db");
36
+ const before = fileIdentity(this.path);
37
+ this.db = new DatabaseSync(this.path);
32
38
  try {
33
39
  this.db.exec("PRAGMA busy_timeout = 60000; PRAGMA cache_size = -2048; PRAGMA temp_store = FILE");
34
40
  // A healthy accelerator needs no schema write transaction on every open.
@@ -46,14 +52,36 @@ export class DerivedState {
46
52
  this.db.exec("COMMIT");
47
53
  } catch (err) { this.db.exec("ROLLBACK"); throw err; }
48
54
  }
55
+ // The file this connection writes, for callers that hold it across
56
+ // streams. SQLite does not expose the opened descriptor, so the path's
57
+ // identity binds the connection only when it is the same before the
58
+ // open and once the schema is on disk; otherwise it stays unbound and
59
+ // assertFile refuses.
60
+ const after = fileIdentity(this.path);
61
+ this.file = after && (!before || before.dev === after.dev && before.ino === after.ino) ? after : null;
49
62
  this.depth = 0;
63
+ this.statements = new Map();
50
64
  this.read = this.db.prepare("SELECT value FROM state WHERE kind = ? AND key = ?");
51
65
  this.write = this.db.prepare("INSERT INTO state VALUES (?, ?, ?) ON CONFLICT(kind,key) DO UPDATE SET value=excluded.value WHERE state.value != excluded.value");
52
66
  } catch (err) { this.db.close(); throw err; }
53
67
  }
54
68
  get(kind, key) { const row = this.read.get(kind, key); return row ? JSON.parse(row.value) : null; }
55
69
  set(kind, key, value) { this.write.run(kind, key, JSON.stringify(value)); }
56
- delete(kind, key) { this.db.prepare("DELETE FROM state WHERE kind = ? AND key = ?").run(kind, key); }
70
+ delete(kind, key) { this.statement("DELETE FROM state WHERE kind = ? AND key = ?").run(kind, key); }
71
+ // One prepared statement per SQL text for this connection's lifetime. Only
72
+ // for statements run to completion (run/get/all), never left iterating.
73
+ statement(sql) {
74
+ let prepared = this.statements.get(sql);
75
+ if (!prepared) { prepared = this.db.prepare(sql); this.statements.set(sql, prepared); }
76
+ return prepared;
77
+ }
78
+ // A connection held across many streams must not publish checkpoint proof
79
+ // into a capture.db that was replaced or removed after it opened.
80
+ assertFile() {
81
+ const now = fileIdentity(this.path);
82
+ if (!now || !this.file || now.dev !== this.file.dev || now.ino !== this.file.ino)
83
+ throw new Error(`capture.db changed during the pass; retry: ${this.path}`);
84
+ }
57
85
  *entries(kind) {
58
86
  for (const row of keysetRows(this.db, "SELECT key, value FROM state WHERE kind = ?", "key", [kind])) {
59
87
  yield { key: row.key, value: JSON.parse(row.value) };
@@ -14,6 +14,9 @@
14
14
  import { closeSync, existsSync, mkdirSync, openSync, readSync, rmSync, unlinkSync } from "node:fs";
15
15
  import { join } from "node:path";
16
16
  import { keysetRows } from "./sqlite-rows.mjs";
17
+ import { captureContext } from "./capture-context.mjs";
18
+ import { DerivedState, fileVersion, sameVersion } from "./derived-state.mjs";
19
+ import { validJournalCheckpoint } from "./store.mjs";
17
20
  import { INDEX_SCHEMA_VERSION as SCHEMA_VERSION, TEXT_SCHEMA, resetIndexTables } from "./index-reset.mjs";
18
21
  import { createRequire } from "node:module";
19
22
  const require = createRequire(import.meta.url);
@@ -138,12 +141,22 @@ export class RecordIndex {
138
141
  }
139
142
  this.db.exec(SCHEMA);
140
143
  this.db.exec(`PRAGMA user_version = ${SCHEMA_VERSION}`);
144
+ this.statements = new Map();
141
145
  }
142
146
 
143
147
  close() {
148
+ this.statements.clear();
144
149
  this.db.close();
145
150
  }
146
151
 
152
+ // One prepared statement per SQL text for this connection. Only for
153
+ // statements run to completion (run/get/all), never left iterating.
154
+ statement(sql) {
155
+ let prepared = this.statements.get(sql);
156
+ if (!prepared) { prepared = this.db.prepare(sql); this.statements.set(sql, prepared); }
157
+ return prepared;
158
+ }
159
+
147
160
  recordOwner() {
148
161
  if (this.store.owner) return this.store.owner;
149
162
  const row = this.db.prepare("SELECT value FROM meta WHERE key = 'owner'").get();
@@ -153,35 +166,88 @@ export class RecordIndex {
153
166
  // Old releases keep writing index_state's count. A separate cursor table
154
167
  // leaves that schema/writer compatible; differing counts invalidate our
155
168
  // cursor rather than trusting a byte position left behind by a downgrade.
169
+ //
170
+ // One capture.db connection serves the whole pass. A census without locks
171
+ // proves a stream current only when its complete validation checkpoint
172
+ // names the journal's exact current version and the search cursor agrees
173
+ // with it in bytes, count (both tables) and epoch; such a stream needs no
174
+ // lock and no validation. Every other stream is a candidate and is
175
+ // re-read under its stream lock. The census is not an atomic snapshot of
176
+ // concurrent writers: a change after a stream is observed is indexed by a
177
+ // later pass, as with any sequential pass.
156
178
  update() {
157
- let reset = false;
158
- for (const row of keysetRows(this.db, `SELECT s.stream, s.indexed_count, b.bytes,
179
+ const context = captureContext(this.store);
180
+ const state = context ? context.state : new DerivedState(this.store.root);
181
+ try {
182
+ // Skip evidence is read from this connection: it must still be the file.
183
+ if (!context) state.assertFile();
184
+ const current = this.#reconcile(state, !context);
185
+ this.#catchUp(state, !context, current);
186
+ if (!context) state.assertFile();
187
+ } finally { if (!context) state.close(); }
188
+ }
189
+
190
+ // Indexed streams whose evidence is incomplete are validated under their
191
+ // locks first: if any proves the union stale (missing or replaced journal,
192
+ // new epoch, invalid cursor, legacy count disagreement), the whole union
193
+ // resets before any stream catches up, and no stream is current against
194
+ // the new, empty generation. → the streams proven current, each with the
195
+ // search-cursor row its proof used.
196
+ #reconcile(state, owned) {
197
+ const current = new Map();
198
+ const rows = keysetRows(this.db, `SELECT s.stream, s.indexed_count, b.bytes,
159
199
  b.indexed_count AS byte_count, b.epoch FROM index_state s
160
- LEFT JOIN index_bytes b ON s.stream = b.stream`, "stream")) {
161
- const checkpoint = this.store.journalCheckpoint(row.stream);
200
+ LEFT JOIN index_bytes b ON s.stream = b.stream`, "stream");
201
+ for (const row of rows) {
202
+ const path = this.store.journalPath(row.stream);
203
+ // Inside a capture work group every stream still goes through the
204
+ // group's membership check: only a pass owning its connection skips.
205
+ if (owned && proven(row, state.get("journal", row.stream), fileVersion(path))) { current.set(row.stream, row); continue; }
206
+ const checkpoint = this.store.withStreamLock(row.stream, () => {
207
+ if (owned) state.assertFile();
208
+ return this.store.validateJournal(row.stream, state);
209
+ });
162
210
  let boundary = true;
163
211
  if (Number.isSafeInteger(row.bytes) && row.bytes > 0 && row.bytes < checkpoint.bytes) {
164
- const fd = openSync(this.store.journalPath(row.stream), "r"), byte = Buffer.alloc(1);
212
+ const fd = openSync(path, "r"), byte = Buffer.alloc(1);
165
213
  try { boundary = readSync(fd, byte, 0, 1, row.bytes - 1) === 1 && byte[0] === 10; }
166
214
  finally { closeSync(fd); }
167
215
  }
168
216
  if (!Number.isSafeInteger(row.bytes) || row.bytes < 0 || !boundary || row.indexed_count !== row.byte_count ||
169
217
  row.epoch !== checkpoint.epoch || row.bytes > checkpoint.bytes || row.byte_count > checkpoint.count) {
170
- reset = true;
171
- break;
218
+ // Deleting a stream or changing its history can change tombstone authority
219
+ // and cross-stream dedupe. Rebuild the derived union, not only its FTS rows.
220
+ this.reset();
221
+ return new Map();
172
222
  }
173
223
  }
174
- // Deleting a stream or changing its history can change tombstone authority
175
- // and cross-stream dedupe. Rebuild the derived union, not only its FTS rows.
176
- if (reset) this.reset();
224
+ return current;
225
+ }
226
+
227
+ // Index the new bytes of every live stream not proven current, each under
228
+ // its stream lock, in bounded transactions that commit rows and cursors
229
+ // together. No transaction is open while the next stream lock is taken.
230
+ // A proof holds only for the cursor it read: another index connection may
231
+ // have reset the union or moved the cursor since, so the cursor is read
232
+ // again when the stream's turn comes and a changed one makes it a candidate.
233
+ #catchUp(state, owned, current) {
177
234
  const owner = this.store.owner;
178
235
  if (owner) this.db.prepare("INSERT INTO meta (key, value) VALUES ('owner', ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value").run(owner);
179
236
  const effectiveOwner = this.recordOwner();
180
- const getState = this.db.prepare("SELECT * FROM index_bytes WHERE stream = ?");
181
- const setCount = this.db.prepare("INSERT INTO index_state VALUES (?, ?) ON CONFLICT(stream) DO UPDATE SET indexed_count=excluded.indexed_count");
182
- const setBytes = this.db.prepare("INSERT INTO index_bytes VALUES (?, ?, ?, ?) ON CONFLICT(stream) DO UPDATE SET bytes=excluded.bytes, indexed_count=excluded.indexed_count, epoch=excluded.epoch");
237
+ const getState = this.statement("SELECT * FROM index_bytes WHERE stream = ?");
238
+ const setCount = this.statement("INSERT INTO index_state VALUES (?, ?) ON CONFLICT(stream) DO UPDATE SET indexed_count=excluded.indexed_count");
239
+ const setBytes = this.statement("INSERT INTO index_bytes VALUES (?, ?, ?, ?) ON CONFLICT(stream) DO UPDATE SET bytes=excluded.bytes, indexed_count=excluded.indexed_count, epoch=excluded.epoch");
240
+ const cursor = this.statement(`SELECT s.indexed_count, b.bytes, b.indexed_count AS byte_count, b.epoch FROM index_state s
241
+ LEFT JOIN index_bytes b ON s.stream = b.stream WHERE s.stream = ?`);
183
242
  for (const streamId of this.store.iterateStreams()) {
184
- this.store.withStreamLock(streamId, () => this.store.withDerived(state => {
243
+ const proof = current.get(streamId);
244
+ if (proof) {
245
+ const now = cursor.get(streamId);
246
+ if (now?.indexed_count === proof.indexed_count && now.bytes === proof.bytes &&
247
+ now.byte_count === proof.byte_count && now.epoch === proof.epoch) continue;
248
+ }
249
+ this.store.withStreamLock(streamId, () => {
250
+ if (owned) state.assertFile();
185
251
  const checkpoint = this.store.validateJournal(streamId, state);
186
252
  const row = getState.get(streamId);
187
253
  const previous = row?.stream != null ? row : null;
@@ -208,12 +274,13 @@ export class RecordIndex {
208
274
  } catch (err) { this.db.exec("ROLLBACK"); throw err; }
209
275
  }
210
276
  } finally { iterator.return(); }
211
- }));
277
+ });
212
278
  }
213
279
  }
214
280
 
215
281
  // Full rebuild = reset + update from zero. Same code path as update().
216
282
  reset() {
283
+ this.statements.clear();
217
284
  this.db.exec("BEGIN");
218
285
  try {
219
286
  resetIndexTables(this.db);
@@ -231,20 +298,20 @@ export class RecordIndex {
231
298
  // streams: first seen wins.
232
299
  addTurn(turn, streamId, owner) {
233
300
  if (typeof turn.id !== "string") return;
234
- const existing = this.db.prepare("SELECT stream FROM turns WHERE id = ?").get(turn.id);
301
+ const existing = this.statement("SELECT stream FROM turns WHERE id = ?").get(turn.id);
235
302
  if (existing) {
236
303
  // Duplicate id across streams. Keep attribution deterministic and in
237
304
  // agreement with RecordStore.readAll(): alphabetically first stream,
238
305
  // regardless of indexing order.
239
306
  if (streamId < existing.stream) {
240
- this.db.prepare("UPDATE turns SET stream = ? WHERE id = ?").run(streamId, turn.id);
307
+ this.statement("UPDATE turns SET stream = ? WHERE id = ?").run(streamId, turn.id);
241
308
  }
242
309
  return;
243
310
  }
244
311
 
245
312
  const fromName = String(turn.from ?? "");
246
313
  // Hidden if a valid tombstone already arrived for this id.
247
- const hidden = this.db.prepare("SELECT 1 AS found FROM tombstones WHERE ref = ? AND (from_name = ? OR from_name = ?) LIMIT 1")
314
+ const hidden = this.statement("SELECT 1 AS found FROM tombstones WHERE ref = ? AND (from_name = ? OR from_name = ?) LIMIT 1")
248
315
  .get(turn.id, fromName, owner ?? fromName)?.found === 1 ? 1 : 0;
249
316
 
250
317
  // Sessions are one turn per native event (no snapshots, nothing to
@@ -252,11 +319,10 @@ export class RecordIndex {
252
319
  const events = 0;
253
320
  const superseded = 0;
254
321
 
255
- this.db
256
- .prepare(
257
- `INSERT INTO turns (id, ts, from_name, to_name, thread, kind, source, fidelity, stream, events, hidden, superseded)
258
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
259
- )
322
+ this.statement(
323
+ `INSERT INTO turns (id, ts, from_name, to_name, thread, kind, source, fidelity, stream, events, hidden, superseded)
324
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
325
+ )
260
326
  .run(
261
327
  turn.id,
262
328
  String(turn.ts ?? ""),
@@ -275,24 +341,22 @@ export class RecordIndex {
275
341
  if (turn.kind === "tombstone") {
276
342
  for (const link of turn.links ?? []) {
277
343
  if (link.rel !== "tombstones" || typeof link.ref !== "string") continue;
278
- this.db
279
- .prepare("INSERT OR IGNORE INTO tombstones (ref, from_name) VALUES (?, ?)")
344
+ this.statement("INSERT OR IGNORE INTO tombstones (ref, from_name) VALUES (?, ?)")
280
345
  .run(link.ref, fromName);
281
346
  // Apply to a target that arrived first.
282
- const target = this.db
283
- .prepare("SELECT id, from_name FROM turns WHERE id = ? AND hidden = 0")
347
+ const target = this.statement("SELECT id, from_name FROM turns WHERE id = ? AND hidden = 0")
284
348
  .get(link.ref);
285
349
  if (target && (target.from_name === fromName || (owner && fromName === owner))) {
286
- this.db.prepare("UPDATE turns SET hidden = 1 WHERE id = ?").run(target.id);
287
- this.db.prepare("DELETE FROM turn_text WHERE turn_id = ?").run(target.id);
288
- this.db.prepare("DELETE FROM segments WHERE note_id = ?").run(target.id);
289
- this.db.prepare("DELETE FROM spawns WHERE note_id = ?").run(target.id);
350
+ this.statement("UPDATE turns SET hidden = 1 WHERE id = ?").run(target.id);
351
+ this.statement("DELETE FROM turn_text WHERE turn_id = ?").run(target.id);
352
+ this.statement("DELETE FROM segments WHERE note_id = ?").run(target.id);
353
+ this.statement("DELETE FROM spawns WHERE note_id = ?").run(target.id);
290
354
  }
291
355
  }
292
356
  }
293
357
 
294
358
  if (!hidden && !superseded) {
295
- const insText = this.db.prepare(
359
+ const insText = this.statement(
296
360
  "INSERT INTO turn_text (turn_id, loc, role, text) VALUES (?, ?, ?, ?)",
297
361
  );
298
362
  for (const doc of this.extractText(turn)) {
@@ -323,26 +387,23 @@ export class RecordIndex {
323
387
  }
324
388
  }
325
389
  if (tag) {
326
- this.db
327
- .prepare(
328
- "INSERT OR IGNORE INTO tags (tag_id, target_ref, about, acts, case_slug, note) VALUES (?, ?, ?, ?, ?, ?)",
329
- )
390
+ this.statement(
391
+ "INSERT OR IGNORE INTO tags (tag_id, target_ref, about, acts, case_slug, note) VALUES (?, ?, ?, ?, ?, ?)",
392
+ )
330
393
  .run(turn.id, tag.target, JSON.stringify(tag.about), JSON.stringify(tag.acts), tag.caseSlug, tag.note);
331
394
  }
332
395
  const outfit = outfitParsed;
333
396
  if (outfit) {
334
- this.db
335
- .prepare("INSERT OR IGNORE INTO outfits (outfit_id, task, status, members) VALUES (?, ?, ?, ?)")
397
+ this.statement("INSERT OR IGNORE INTO outfits (outfit_id, task, status, members) VALUES (?, ?, ?, ?)")
336
398
  .run(turn.id, outfit.task, outfit.status, JSON.stringify(outfit.members));
337
399
  }
338
400
  if (segParsed && !hidden) {
339
401
  const locN = (r) => Number(/line:(\d+)/.exec(r ?? "")?.[1] ?? null);
340
402
  const tsMs = Date.parse(String(turn.ts ?? ""));
341
- this.db
342
- .prepare(
343
- `INSERT OR IGNORE INTO segments (note_id, thread, start_n, end_n, type, outcome, about, established, lesson, ts, ts_ms)
344
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
345
- )
403
+ this.statement(
404
+ `INSERT OR IGNORE INTO segments (note_id, thread, start_n, end_n, type, outcome, about, established, lesson, ts, ts_ms)
405
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
406
+ )
346
407
  .run(
347
408
  turn.id,
348
409
  segParsed.thread,
@@ -357,15 +418,13 @@ export class RecordIndex {
357
418
  Number.isFinite(tsMs) ? tsMs : 0,
358
419
  );
359
420
  // Full established+lesson searchable under role "segment".
360
- this.db
361
- .prepare("INSERT INTO turn_text (turn_id, loc, role, text) VALUES (?, ?, ?, ?)")
421
+ this.statement("INSERT INTO turn_text (turn_id, loc, role, text) VALUES (?, ?, ?, ?)")
362
422
  .run(turn.id, "", "segment", segParsed.established + (segParsed.lesson ? "\n" + segParsed.lesson : ""));
363
423
  }
364
424
  if (spawnParsed && !hidden) {
365
- this.db
366
- .prepare(
367
- "INSERT OR IGNORE INTO spawns (note_id, agent_thread, outfit_id, task, harness, grant_ref, ts) VALUES (?, ?, ?, ?, ?, ?, ?)",
368
- )
425
+ this.statement(
426
+ "INSERT OR IGNORE INTO spawns (note_id, agent_thread, outfit_id, task, harness, grant_ref, ts) VALUES (?, ?, ?, ?, ?, ?, ?)",
427
+ )
369
428
  .run(turn.id, spawnParsed.agent, spawnParsed.outfit, spawnParsed.task, spawnParsed.harness, spawnParsed.grant, String(turn.ts ?? ""));
370
429
  }
371
430
  }
@@ -538,6 +597,16 @@ export function extractTurnText(turn) {
538
597
  // per-format extractors live in formats.mjs.
539
598
  export const extractSessionText = extractCcText;
540
599
 
600
+ // A stream whose search cursor is complete against a complete validation of
601
+ // the journal's exact current version. Anything missing, malformed, partial
602
+ // or disagreeing is unproven, never a reason to skip.
603
+ function proven(row, checkpoint, version) {
604
+ return version !== null && validJournalCheckpoint(checkpoint, version.size) && checkpoint.complete && !checkpoint.resetting &&
605
+ sameVersion(checkpoint.version, version) && row.epoch === checkpoint.epoch &&
606
+ Number.isSafeInteger(row.bytes) && row.bytes === checkpoint.bytes &&
607
+ row.byte_count === row.indexed_count && row.byte_count === checkpoint.count;
608
+ }
609
+
541
610
  export function dropIndex(store) {
542
611
  rmSync(join(store.root, "index"), { recursive: true, force: true });
543
612
  }
@@ -316,6 +316,16 @@ function describeHolder(held) {
316
316
  return `held by pid ${held.token.pid} on ${held.token.host} since ${held.token.acquiredAt}`;
317
317
  }
318
318
 
319
+ /** A journal validation checkpoint whose fields can describe a journal of
320
+ * `size` bytes. Shape only: whether it is complete and still names the
321
+ * current file version is the caller's question. */
322
+ export function validJournalCheckpoint(checkpoint, size) {
323
+ return Boolean(checkpoint) && Number.isSafeInteger(checkpoint.bytes) && checkpoint.bytes >= 0 && checkpoint.bytes <= size &&
324
+ Number.isSafeInteger(checkpoint.count) && checkpoint.count >= 0 && checkpoint.count <= checkpoint.bytes &&
325
+ typeof checkpoint.epoch === "string" && typeof checkpoint.complete === "boolean" &&
326
+ (checkpoint.resetting === undefined || typeof checkpoint.resetting === "boolean") && !(checkpoint.resetting && checkpoint.complete);
327
+ }
328
+
319
329
  export class RecordStore {
320
330
  constructor(root, { owner, lockTimeoutMs = 10000, lockStaleMs = 30000 } = {}) {
321
331
  if (!root) throw new StoreError("record root is required");
@@ -466,10 +476,7 @@ export class RecordStore {
466
476
  validateJournal(streamId, state, { maxRecords = Infinity, maxBytes = Infinity, stopped = () => false, work } = {}) {
467
477
  const path = this.journalPath(streamId), version = fileVersion(path);
468
478
  let checkpoint = state.get("journal", streamId);
469
- if (checkpoint && (!Number.isSafeInteger(checkpoint.bytes) || checkpoint.bytes < 0 || checkpoint.bytes > (version?.size ?? 0) ||
470
- !Number.isSafeInteger(checkpoint.count) || checkpoint.count < 0 || checkpoint.count > checkpoint.bytes ||
471
- typeof checkpoint.epoch !== "string" || typeof checkpoint.complete !== "boolean" ||
472
- (checkpoint.resetting !== undefined && typeof checkpoint.resetting !== "boolean") || checkpoint.resetting && checkpoint.complete)) checkpoint = null;
479
+ if (checkpoint && !validJournalCheckpoint(checkpoint, version?.size ?? 0)) checkpoint = null;
473
480
  if (checkpoint && sameVersion(checkpoint.version, version) && checkpoint.complete) return checkpoint;
474
481
  if (!checkpoint || !sameVersion(checkpoint.version, version)) {
475
482
  checkpoint = { version, epoch: randomBytes(16).toString("hex"), bytes: 0, count: 0, complete: false, resetting: true };
@@ -482,14 +489,14 @@ export class RecordStore {
482
489
  if (recordsUsed >= maxRecords || stopped()) return checkpoint;
483
490
  state.transaction(() => {
484
491
  const limit = Math.min(2000, maxRecords - recordsUsed);
485
- const removed = Number(state.db.prepare(`DELETE FROM journal_ids WHERE rowid IN
492
+ const removed = Number(state.statement(`DELETE FROM journal_ids WHERE rowid IN
486
493
  (SELECT rowid FROM journal_ids WHERE stream = ? LIMIT ?)`).run(streamId, limit).changes);
487
494
  recordsUsed += removed;
488
495
  if (work) work.deletedIds = (work.deletedIds ?? 0) + removed;
489
496
  if (removed < limit) { delete checkpoint.resetting; state.set("journal", streamId, checkpoint); }
490
497
  });
491
498
  }
492
- const addId = state.db.prepare("INSERT OR IGNORE INTO journal_ids VALUES (?, ?)");
499
+ const addId = state.statement("INSERT OR IGNORE INTO journal_ids VALUES (?, ?)");
493
500
  const iterator = this.iterateJournal(streamId, { start: checkpoint.bytes, end: version?.size ?? 0,
494
501
  check: () => { stopped(); } });
495
502
  let done = false;