@open-tomato/rafa 0.46.0 → 0.47.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 (59) hide show
  1. package/README.md +65 -35
  2. package/dist/adapters/logger/settings.d.ts +2 -2
  3. package/dist/bugs/record.d.ts +1 -1
  4. package/dist/bundled/skills/dev-planner/SKILL.md +1 -1
  5. package/dist/cli.js +74711 -74226
  6. package/dist/config/index.d.ts +5 -0
  7. package/dist/config/index.js +6 -3
  8. package/dist/config/shape.d.ts +8 -5
  9. package/dist/config/templates.d.ts +64 -0
  10. package/dist/config-registry.d.ts +21 -12
  11. package/dist/config-schema-bugs.d.ts +3 -3
  12. package/dist/config-schema-checks.d.ts +114 -0
  13. package/dist/config-schema-errors.d.ts +3 -3
  14. package/dist/config-schema-features.d.ts +4 -4
  15. package/dist/config-schema-hub.d.ts +3 -1
  16. package/dist/config-schema-issues.d.ts +2 -2
  17. package/dist/config-schema-logger.d.ts +3 -3
  18. package/dist/config-schema-release.d.ts +11 -12
  19. package/dist/config-schema-tests.d.ts +32 -24
  20. package/dist/core/command/command.d.ts +1 -1
  21. package/dist/core/command/confirm.d.ts +11 -5
  22. package/dist/core/command/index.d.ts +27 -9
  23. package/dist/core/command/shared-flags.d.ts +40 -0
  24. package/dist/core/config/config-load-module.d.ts +43 -0
  25. package/dist/core/config/config-load.d.ts +43 -12
  26. package/dist/core/config/config-origins.d.ts +31 -30
  27. package/dist/core/config/config-schema.d.ts +5 -0
  28. package/dist/core/config/config.d.ts +90 -25
  29. package/dist/core/config/core-section.d.ts +1 -1
  30. package/dist/core/config/index.d.ts +10 -6
  31. package/dist/core/config/merge-method.d.ts +40 -0
  32. package/dist/core/config/scope.d.ts +2 -2
  33. package/dist/core/config/section.d.ts +81 -1
  34. package/dist/core/config/sections.d.ts +13 -5
  35. package/dist/core/loop/hop-record.d.ts +6 -6
  36. package/dist/core/loop/index.d.ts +6 -6
  37. package/dist/core/loop/session-record-parse.d.ts +9 -2
  38. package/dist/core/loop/sessions.d.ts +22 -10
  39. package/dist/core/plan/block-reader.d.ts +1 -1
  40. package/dist/effort/store/index.js +5 -5
  41. package/dist/errors/rafa-codes.d.ts +1 -1
  42. package/dist/{index-77ajfffn.js → index-38ktnyvz.js} +1 -1
  43. package/dist/{index-jh1r4kcn.js → index-3mwsr6wg.js} +333 -330
  44. package/dist/{index-gqz4jn6j.js → index-7dgc5g1t.js} +4755 -4722
  45. package/dist/{index-gvek4r8m.js → index-cbqvgda6.js} +82 -26
  46. package/dist/{index-cy98y87p.js → index-eay1bd2w.js} +143 -108
  47. package/dist/{index-2a7nb3an.js → index-f1y86zbb.js} +31 -31
  48. package/dist/{index-j4k2vdz3.js → index-n3e31fmw.js} +205 -289
  49. package/dist/{index-39kbcw68.js → index-nwzx41g1.js} +4 -4
  50. package/dist/index-pzpb1rf7.js +153 -0
  51. package/dist/{index-ykggr043.js → index-r5q2pdky.js} +10 -5
  52. package/dist/index.js +14602 -14328
  53. package/dist/learning/index.js +1 -1
  54. package/dist/plan/index.js +3 -3
  55. package/dist/ports/index.d.ts +1 -1
  56. package/dist/{ready-4f8edezt.js → ready-6p40c3gw.js} +260 -269
  57. package/package.json +22 -1
  58. package/dist/pr/checks.d.ts +0 -133
  59. package/dist/pr/types.d.ts +0 -422
@@ -27,52 +27,52 @@ import {
27
27
  storeProjectRoot,
28
28
  writeNames,
29
29
  writeStoreGeneration
30
- } from "./index-cy98y87p.js";
30
+ } from "./index-eay1bd2w.js";
31
31
  import {
32
32
  messageOf
33
- } from "./index-ykggr043.js";
33
+ } from "./index-r5q2pdky.js";
34
34
 
35
- // packages/rafa/src/effort/store/ndjson.ts
36
- import { join } from "path";
37
- function kindPath(repoRoot, kind) {
38
- return join(effortStoreDir(repoRoot), effortStoreFileName(kind));
39
- }
40
- function openedKindPath(repoRoot, kind) {
41
- const path = kindPath(repoRoot, kind);
42
- guardTestProcess(path);
43
- return path;
44
- }
45
- function readKind(repoRoot, kind) {
46
- return readStoreRows(openedKindPath(repoRoot, kind));
47
- }
48
- function keysOfKind(repoRoot, kind) {
49
- const path = openedKindPath(repoRoot, kind);
50
- return collectedKeys(path, EFFORT_KEY_PROJECTIONS[kind]);
51
- }
52
- function appendKind(repoRoot, kind, rows) {
53
- const path = openedKindPath(repoRoot, kind);
54
- return appendRows(path, rows, EFFORT_KEY_PROJECTIONS[kind]);
55
- }
56
- function openNdjsonStore(repoRoot) {
57
- return {
58
- append: (kind, rows) => appendKind(repoRoot, kind, rows),
59
- keys: (kind) => keysOfKind(repoRoot, kind),
60
- read: (kind) => readKind(repoRoot, kind).rows,
61
- path: (kind) => kindPath(repoRoot, kind),
62
- readRows: (kind) => readKind(repoRoot, kind)
63
- };
64
- }
65
-
66
- // packages/rafa/src/effort/store/merge-store.ts
67
- import { randomUUID } from "crypto";
35
+ // packages/rafa/src/effort/store/wire.ts
68
36
  import { existsSync as existsSync3, mkdtempSync, rmSync as rmSync3 } from "fs";
69
37
  import { tmpdir } from "os";
70
- import { dirname, join as join3 } from "path";
71
- import { Database as Database3, SQLiteError as SQLiteError2 } from "bun:sqlite";
38
+ import { join as join2 } from "path";
39
+ import { Database as Database2, SQLiteError } from "bun:sqlite";
40
+
41
+ // packages/rafa/src/effort/store/merge-rules.ts
42
+ var SET_ONCE = "set-once";
43
+ var RECOMPUTED = "recomputed";
44
+ var APPEND_ONLY = {};
45
+ function appendOnly(...identity) {
46
+ return { scope: "merged", identity, edited: APPEND_ONLY };
47
+ }
48
+ var MERGE_RULES = {
49
+ sessions: appendOnly("session_id"),
50
+ commits: { scope: "merged", identity: ["sha"], edited: { row_json: RECOMPUTED } },
51
+ findings: { scope: "merged", identity: ["id"], edited: { tracker_ref: SET_ONCE } },
52
+ blockers: appendOnly("id"),
53
+ out_of_scope_bugs: appendOnly("id"),
54
+ report_absences: appendOnly("id"),
55
+ task_reports: appendOnly("id"),
56
+ preflight: appendOnly("run_id", "position"),
57
+ dispatches: appendOnly("session_id"),
58
+ changes: appendOnly("id"),
59
+ skill_invocations: appendOnly("session_id", "name", "sidechain"),
60
+ plan_ci: appendOnly("pr", "head_sha", "read_at"),
61
+ stale_docs: appendOnly("id"),
62
+ bug_routes: appendOnly("id"),
63
+ schema_migrations: { scope: "local" },
64
+ store_meta: { scope: "local" },
65
+ store_replaced: { scope: "local" },
66
+ merges: { scope: "local" },
67
+ merge_conflicts: { scope: "local" }
68
+ };
69
+
70
+ // packages/rafa/src/effort/store/rebuild-aside.ts
71
+ import { closeSync, existsSync as existsSync2, fsyncSync, openSync, renameSync, rmSync as rmSync2 } from "fs";
72
72
 
73
73
  // packages/rafa/src/effort/store/copy.ts
74
74
  import { copyFileSync, constants, existsSync, mkdirSync, readdirSync, rmSync, statSync } from "fs";
75
- import { join as join2, resolve } from "path";
75
+ import { join, resolve } from "path";
76
76
  import { Database } from "bun:sqlite";
77
77
  var NDJSON_KINDS = { sessions: true, commits: true };
78
78
  var EFFORT_COPY_FILE_NAMES = [
@@ -116,16 +116,16 @@ function vacuumInto(source, target) {
116
116
  }
117
117
  function copyOne(name, source, target) {
118
118
  if (name === SQLITE_STORE_FILE_NAME) {
119
- vacuumInto(join2(source, name), join2(target, name));
119
+ vacuumInto(join(source, name), join(target, name));
120
120
  return;
121
121
  }
122
- copyFileSync(join2(source, name), join2(target, name), constants.COPYFILE_EXCL);
122
+ copyFileSync(join(source, name), join(target, name), constants.COPYFILE_EXCL);
123
123
  }
124
124
  function undo(target, written, madeDir) {
125
125
  try {
126
126
  if (madeDir === undefined) {
127
127
  for (const name of written)
128
- rmSync(join2(target, name), { force: true });
128
+ rmSync(join(target, name), { force: true });
129
129
  } else {
130
130
  rmSync(madeDir, { recursive: true, force: true });
131
131
  }
@@ -139,7 +139,7 @@ function copyEffortStore(request) {
139
139
  const target = resolve(request.target);
140
140
  guardTestProcess(source);
141
141
  guardTestProcess(target);
142
- const files = EFFORT_COPY_FILE_NAMES.filter((name) => existsSync(join2(source, name)));
142
+ const files = EFFORT_COPY_FILE_NAMES.filter((name) => existsSync(join(source, name)));
143
143
  if (files.length === 0) {
144
144
  throw new EffortCopyRefusal(`no effort store at ${source}: it holds none of ${EFFORT_COPY_FILE_NAMES.join(", ")}. Nothing to copy.`);
145
145
  }
@@ -159,11 +159,7 @@ function copyEffortStore(request) {
159
159
  return Object.freeze({ source, directory: target, files: Object.freeze(files) });
160
160
  }
161
161
 
162
- // packages/rafa/src/effort/store/fix-schema.ts
163
- import { Database as Database2 } from "bun:sqlite";
164
-
165
162
  // packages/rafa/src/effort/store/rebuild-aside.ts
166
- import { closeSync, existsSync as existsSync2, fsyncSync, openSync, renameSync, rmSync as rmSync2 } from "fs";
167
163
  class RebuildRefusal extends Error {
168
164
  constructor(message, options) {
169
165
  super(message, options);
@@ -294,12 +290,253 @@ function rebuildAside(options) {
294
290
  swapIn(path, parallelPath, backupPath);
295
291
  return { built, backupPath };
296
292
  }
293
+
294
+ // packages/rafa/src/effort/store/wire.ts
295
+ var WIRE_FORMAT = "rafa-effort-wire";
296
+ var WIRE_VERSION = 1;
297
+
298
+ class WireFormatError extends Error {
299
+ name = "WireFormatError";
300
+ }
301
+
302
+ class WireExportRefusal extends Error {
303
+ name = "WireExportRefusal";
304
+ }
305
+ var MERGED_TABLES = Object.entries(MERGE_RULES).filter(([, rule]) => rule.scope === "merged").map(([table]) => table);
306
+ var ORIGIN_COLUMNS = ["origin_store", "origin_seq"];
307
+ function isRecord(value) {
308
+ return typeof value === "object" && value !== null && !Array.isArray(value);
309
+ }
310
+ function isCount(value) {
311
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
312
+ }
313
+ function isMergedTable(name) {
314
+ return MERGED_TABLES.includes(name);
315
+ }
316
+ function checkCursor(value) {
317
+ if (!isRecord(value))
318
+ throw new WireFormatError("wire: cursor is not an object");
319
+ const entries = Object.entries(value).map(([table, seq]) => {
320
+ if (!isMergedTable(table))
321
+ throw new WireFormatError(`wire: cursor names ${JSON.stringify(table)}, which is no merged table`);
322
+ if (!isCount(seq))
323
+ throw new WireFormatError(`wire: cursor for ${table} is ${JSON.stringify(seq)}, expected a whole number of 0 or more`);
324
+ return [table, seq];
325
+ });
326
+ return Object.freeze(Object.fromEntries(entries));
327
+ }
328
+ function checkRow(table, value, index) {
329
+ const where = `wire: ${table} row ${String(index)}`;
330
+ if (!isRecord(value))
331
+ throw new WireFormatError(`${where} is not an object`);
332
+ for (const [column, cell] of Object.entries(value)) {
333
+ const carried = cell === null || typeof cell === "string" || typeof cell === "number" && Number.isFinite(cell);
334
+ if (!carried)
335
+ throw new WireFormatError(`${where} holds ${JSON.stringify(cell) ?? typeof cell} in ${column}, expected a string, a number or null`);
336
+ }
337
+ const seq = value.seq;
338
+ if (!isCount(seq) || seq === 0)
339
+ throw new WireFormatError(`${where} has seq ${JSON.stringify(seq) ?? "undefined"}, expected a whole number above 0`);
340
+ return Object.freeze({ ...value });
341
+ }
342
+ function checkMigrations(value) {
343
+ if (!Array.isArray(value))
344
+ throw new WireFormatError("wire: migrations is not a list");
345
+ const ids = value.map((id) => {
346
+ if (typeof id !== "string" || id === "")
347
+ throw new WireFormatError(`wire: migration id ${JSON.stringify(id)} is not a name`);
348
+ return id;
349
+ });
350
+ const twice = ids.filter((id, index) => ids.indexOf(id) !== index);
351
+ if (twice.length > 0)
352
+ throw new WireFormatError(`wire: migrations name ${twice.join(", ")} more than once`);
353
+ return Object.freeze(ids);
354
+ }
355
+ function checkTables(value) {
356
+ if (!isRecord(value))
357
+ throw new WireFormatError("wire: tables is not an object");
358
+ const entries = Object.entries(value).map(([table, rows]) => {
359
+ if (!isMergedTable(table))
360
+ throw new WireFormatError(`wire: tables names ${JSON.stringify(table)}, which is no merged table`);
361
+ if (!Array.isArray(rows))
362
+ throw new WireFormatError(`wire: tables.${table} is not a list`);
363
+ return [table, Object.freeze(rows.map((row, index) => checkRow(table, row, index)))];
364
+ });
365
+ return Object.freeze(Object.fromEntries(entries));
366
+ }
367
+ function checkPayload(value) {
368
+ if (!isRecord(value))
369
+ throw new WireFormatError("wire: the payload is not an object");
370
+ if (value.format !== WIRE_FORMAT)
371
+ throw new WireFormatError(`wire: format is ${JSON.stringify(value.format) ?? "undefined"}, expected ${JSON.stringify(WIRE_FORMAT)}`);
372
+ if (value.version !== WIRE_VERSION)
373
+ throw new WireFormatError(`wire: version is ${JSON.stringify(value.version) ?? "undefined"}; this rafa reads version ${String(WIRE_VERSION)}`);
374
+ return Object.freeze({
375
+ format: WIRE_FORMAT,
376
+ version: WIRE_VERSION,
377
+ migrations: checkMigrations(value.migrations),
378
+ tables: checkTables(value.tables),
379
+ cursor: checkCursor(value.cursor)
380
+ });
381
+ }
382
+ function encodeWirePayload(payload) {
383
+ return JSON.stringify(payload);
384
+ }
385
+ function decodeWirePayload(text) {
386
+ let parsed;
387
+ try {
388
+ parsed = JSON.parse(text);
389
+ } catch (error) {
390
+ const reason = error instanceof Error ? error.message : String(error);
391
+ throw new WireFormatError(`wire: the payload is not JSON (${reason})`);
392
+ }
393
+ return checkPayload(parsed);
394
+ }
395
+ function readLoggedIds(db) {
396
+ const hasLog = db.query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?").get(MIGRATION_LOG_TABLE) !== null;
397
+ if (!hasLog)
398
+ return null;
399
+ return db.query(`SELECT id FROM ${MIGRATION_LOG_TABLE} ORDER BY seq`).all().map(({ id }) => id);
400
+ }
401
+ function columnsOf(db, table) {
402
+ return db.query(`PRAGMA table_info(${quoted(table)})`).all().map(({ name }) => name);
403
+ }
404
+ function wireValue(value, where) {
405
+ if (typeof value === "bigint") {
406
+ const number = Number(value);
407
+ if (Number.isSafeInteger(number))
408
+ return number;
409
+ throw new WireExportRefusal(`wire: ${where} holds ${String(value)}, past the whole numbers JSON carries exactly`);
410
+ }
411
+ if (value instanceof Uint8Array)
412
+ throw new WireExportRefusal(`wire: ${where} holds a BLOB, which the wire does not carry`);
413
+ return value;
414
+ }
415
+ function readRows(db, table, columns, after, path) {
416
+ const named = columns.map(quoted).join(", ");
417
+ return db.query(`SELECT ${named} FROM ${quoted(table)} WHERE seq > ? ORDER BY seq`).all(after).map((row) => Object.freeze(Object.fromEntries(Object.entries(row).map(([column, value]) => [
418
+ column,
419
+ wireValue(value, `${path} ${table}.${column} at seq ${String(row.seq)}`)
420
+ ]))));
421
+ }
422
+ function readPayload(db, path, cursor) {
423
+ const migrations = readLoggedIds(db);
424
+ if (migrations === null) {
425
+ throw new WireExportRefusal(`wire: ${path} has no migration log; any rafa command that writes the store brings it forward`);
426
+ }
427
+ const tables = {};
428
+ const next = { ...cursor };
429
+ for (const table of MERGED_TABLES) {
430
+ const columns = columnsOf(db, table);
431
+ if (columns.length === 0)
432
+ continue;
433
+ const missing = ORIGIN_COLUMNS.filter((column) => !columns.includes(column));
434
+ if (missing.length > 0) {
435
+ throw new WireExportRefusal(`wire: ${path} holds no ${missing.map((column) => `${table}.${column}`).join(", ")}, so its rows carry no origin pair`);
436
+ }
437
+ const rows = readRows(db, table, columns, cursor[table] ?? 0, path);
438
+ tables[table] = Object.freeze(rows);
439
+ const last = rows[rows.length - 1];
440
+ next[table] = last === undefined ? cursor[table] ?? 0 : Number(last.seq);
441
+ }
442
+ return Object.freeze({
443
+ format: WIRE_FORMAT,
444
+ version: WIRE_VERSION,
445
+ migrations: Object.freeze([...migrations]),
446
+ tables: Object.freeze(tables),
447
+ cursor: Object.freeze(next)
448
+ });
449
+ }
450
+ function exportWirePayload(options) {
451
+ const { path } = options;
452
+ const cursor = checkCursor(options.cursor ?? {});
453
+ guardTestProcess(path);
454
+ if (!existsSync3(path))
455
+ throw new WireExportRefusal(`wire: ${path} is not there`);
456
+ const db = new Database2(path, { readonly: true, safeIntegers: true });
457
+ try {
458
+ db.run(`PRAGMA busy_timeout = ${String(activeStoreSettings().busyTimeoutMs)}`);
459
+ return db.transaction(() => readPayload(db, path, cursor))();
460
+ } finally {
461
+ db.close();
462
+ }
463
+ }
464
+ function namedMigrations(ids, catalogue) {
465
+ const known = new Set(catalogue.map(({ id }) => id));
466
+ const unknown = ids.filter((id) => !known.has(id));
467
+ if (unknown.length > 0) {
468
+ throw new WireFormatError(`wire: the payload's store holds migration ${unknown.join(", ")}, which this rafa does not know; update rafa to take its rows`);
469
+ }
470
+ return catalogue.filter(({ id }) => ids.includes(id));
471
+ }
472
+ function insertRows(db, table, rows) {
473
+ const columns = columnsOf(db, table);
474
+ if (columns.length === 0)
475
+ throw new WireFormatError(`wire: the payload carries ${table}, which its migrations do not make`);
476
+ const statement = db.query(`INSERT INTO ${quoted(table)} (${columns.map(quoted).join(", ")}) VALUES (${columns.map(() => "?").join(", ")})`);
477
+ const expected = [...columns].sort().join(",");
478
+ rows.forEach((row, index) => {
479
+ const where = `wire: ${table} row ${String(index)}`;
480
+ const held = Object.keys(row);
481
+ if ([...held].sort().join(",") !== expected) {
482
+ throw new WireFormatError(`${where} holds columns ${held.join(", ")}; its migrations make ${columns.join(", ")}`);
483
+ }
484
+ try {
485
+ statement.run(...columns.map((column) => row[column] ?? null));
486
+ } catch (error) {
487
+ if (!(error instanceof SQLiteError))
488
+ throw error;
489
+ throw new WireFormatError(`${where} (seq ${String(row.seq)}) is refused by the table: ${error.message}`);
490
+ }
491
+ });
492
+ }
493
+ function buildStore(path, payload, options) {
494
+ const migrations = namedMigrations(payload.migrations, options.migrations ?? SQLITE_MIGRATIONS);
495
+ const db = new Database2(path, { create: true, readwrite: true });
496
+ try {
497
+ bringForward(db, path, "write", "open", {
498
+ migrations,
499
+ appliedBy: appliedByName(options.identity ?? readRuntimeIdentity()),
500
+ now: options.now,
501
+ builtAside: true
502
+ });
503
+ db.transaction(() => {
504
+ for (const [table, rows] of Object.entries(payload.tables))
505
+ insertRows(db, table, rows);
506
+ })();
507
+ } finally {
508
+ db.close();
509
+ }
510
+ }
511
+ function materialiseWirePayload(payload, options = {}) {
512
+ const checked = checkPayload(payload);
513
+ const directory = mkdtempSync(join2(options.tempDir ?? tmpdir(), "rafa-wire-"));
514
+ const release = () => rmSync3(directory, { recursive: true, force: true });
515
+ const path = join2(directory, SQLITE_STORE_FILE_NAME);
516
+ try {
517
+ guardTestProcess(path);
518
+ buildStore(path, checked, options);
519
+ } catch (error) {
520
+ release();
521
+ throw error;
522
+ }
523
+ return Object.freeze({ directory, path, release });
524
+ }
525
+
526
+ // packages/rafa/src/effort/store/merge-store.ts
527
+ import { randomUUID } from "crypto";
528
+ import { existsSync as existsSync4, mkdtempSync as mkdtempSync2, rmSync as rmSync4 } from "fs";
529
+ import { tmpdir as tmpdir2 } from "os";
530
+ import { dirname, join as join3 } from "path";
531
+ import { Database as Database4, SQLiteError as SQLiteError3 } from "bun:sqlite";
532
+
297
533
  // packages/rafa/src/effort/store/fix-schema.ts
534
+ import { Database as Database3 } from "bun:sqlite";
298
535
  function tableNames(db, schema) {
299
536
  return db.query(`SELECT name FROM ${schema}.sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' AND name <> ? ORDER BY name`).all(MIGRATION_LOG_TABLE).map((row) => row.name);
300
537
  }
301
538
  function readOnlySchema(path) {
302
- const db = new Database2(path, { readonly: true });
539
+ const db = new Database3(path, { readonly: true });
303
540
  try {
304
541
  return readStoreSchema(db, path);
305
542
  } finally {
@@ -387,52 +624,23 @@ function recomputeCommitGaps(db, inserted) {
387
624
  const order = timeOrder(entries);
388
625
  const positions = new Map(order.map((entry, position) => [entry.seq, position]));
389
626
  const named = new Set(inserted.flatMap((seq) => {
390
- const entry = bySeq.get(seq);
391
- return entry === undefined ? [] : namedRows(entry, positions, order);
392
- }));
393
- const targets = [...named].map((seq) => bySeq.get(seq)).filter((entry) => entry?.row != null).sort((left, right) => (positions.get(left.seq) ?? -1) - (positions.get(right.seq) ?? -1));
394
- return targets.flatMap((entry) => {
395
- const before = storedGap(entry);
396
- const after = recomputedGap(entry, positions, order);
397
- if (before === after && entry.row !== null && Object.hasOwn(entry.row, GAP_FIELD))
398
- return [];
399
- db.query(UPDATE_GAP).run(JSON.stringify({ ...entry.row, [GAP_FIELD]: after }), entry.seq);
400
- return [{ seq: entry.seq, sha: entry.sha, before, after }];
401
- });
402
- })();
403
- }
404
-
405
- // packages/rafa/src/effort/store/merge-rules.ts
406
- var SET_ONCE = "set-once";
407
- var RECOMPUTED = "recomputed";
408
- var APPEND_ONLY = {};
409
- function appendOnly(...identity) {
410
- return { scope: "merged", identity, edited: APPEND_ONLY };
411
- }
412
- var MERGE_RULES = {
413
- sessions: appendOnly("session_id"),
414
- commits: { scope: "merged", identity: ["sha"], edited: { row_json: RECOMPUTED } },
415
- findings: { scope: "merged", identity: ["id"], edited: { tracker_ref: SET_ONCE } },
416
- blockers: appendOnly("id"),
417
- out_of_scope_bugs: appendOnly("id"),
418
- report_absences: appendOnly("id"),
419
- task_reports: appendOnly("id"),
420
- preflight: appendOnly("run_id", "position"),
421
- dispatches: appendOnly("session_id"),
422
- changes: appendOnly("id"),
423
- skill_invocations: appendOnly("session_id", "name", "sidechain"),
424
- plan_ci: appendOnly("pr", "head_sha", "read_at"),
425
- stale_docs: appendOnly("id"),
426
- bug_routes: appendOnly("id"),
427
- schema_migrations: { scope: "local" },
428
- store_meta: { scope: "local" },
429
- store_replaced: { scope: "local" },
430
- merges: { scope: "local" },
431
- merge_conflicts: { scope: "local" }
432
- };
627
+ const entry = bySeq.get(seq);
628
+ return entry === undefined ? [] : namedRows(entry, positions, order);
629
+ }));
630
+ const targets = [...named].map((seq) => bySeq.get(seq)).filter((entry) => entry?.row != null).sort((left, right) => (positions.get(left.seq) ?? -1) - (positions.get(right.seq) ?? -1));
631
+ return targets.flatMap((entry) => {
632
+ const before = storedGap(entry);
633
+ const after = recomputedGap(entry, positions, order);
634
+ if (before === after && entry.row !== null && Object.hasOwn(entry.row, GAP_FIELD))
635
+ return [];
636
+ db.query(UPDATE_GAP).run(JSON.stringify({ ...entry.row, [GAP_FIELD]: after }), entry.seq);
637
+ return [{ seq: entry.seq, sha: entry.sha, before, after }];
638
+ });
639
+ })();
640
+ }
433
641
 
434
642
  // packages/rafa/src/effort/store/merge-union.ts
435
- import { SQLiteError } from "bun:sqlite";
643
+ import { SQLiteError as SQLiteError2 } from "bun:sqlite";
436
644
  class UnionSchemaMismatch extends Error {
437
645
  constructor(message) {
438
646
  super(message);
@@ -482,7 +690,7 @@ function insertRow(local, table, columns, row, incomingSeq) {
482
690
  throw new Error(`the insert into ${table} returned no seq`);
483
691
  return { kind: "added", localSeq: inserted.seq };
484
692
  } catch (error) {
485
- if (!(error instanceof SQLiteError) || error.code !== UNIQUE_REFUSAL)
693
+ if (!(error instanceof SQLiteError2) || error.code !== UNIQUE_REFUSAL)
486
694
  throw error;
487
695
  return { kind: "collided", collision: { incomingSeq, reason: error.message } };
488
696
  }
@@ -641,21 +849,21 @@ function refuseNdjson(backend) {
641
849
  throw new MergeRefusal("ndjson", `REFUSED \u2014 this project keeps its sessions and commits as NDJSON (store: ndjson), and a merge needs the SQLite store. ${NOTHING_MERGED}` + ` Next safe step: ${MOVE_TO_SQLITE}`);
642
850
  }
643
851
  function refuseMissing(path, which) {
644
- if (existsSync3(path))
852
+ if (existsSync4(path))
645
853
  return;
646
854
  throw new MergeRefusal("missing", `REFUSED \u2014 ${which} ${path} is not there. ${NOTHING_MERGED}`);
647
855
  }
648
856
  function refuseDamaged(path) {
649
857
  let answers;
650
858
  try {
651
- const db = new Database3(path, { readonly: true });
859
+ const db = new Database4(path, { readonly: true });
652
860
  try {
653
861
  answers = db.query("PRAGMA integrity_check").all().map((row) => row.integrity_check);
654
862
  } finally {
655
863
  db.close();
656
864
  }
657
865
  } catch (error) {
658
- if (!(error instanceof SQLiteError2))
866
+ if (!(error instanceof SQLiteError3))
659
867
  throw error;
660
868
  throw new MergeRefusal("damaged", `REFUSED \u2014 ${path} cannot be read as a SQLite store (${error.message}). ${NOTHING_MERGED}` + " Take a consistent copy on its device with rafa effort copy, and merge that.");
661
869
  }
@@ -670,7 +878,7 @@ function writablePlan(path, migrations) {
670
878
  return plan;
671
879
  }
672
880
  function recordedMeta(path) {
673
- const db = new Database3(path, { readonly: true });
881
+ const db = new Database4(path, { readonly: true });
674
882
  try {
675
883
  const hasTable = db.query("SELECT count(*) AS n FROM sqlite_master WHERE type = 'table' AND name = 'store_meta'").get()?.n === 1;
676
884
  return hasTable ? readStoreMeta(db) : null;
@@ -696,18 +904,18 @@ function refuseLiveLoop(path, options, sessionId) {
696
904
  function openOther(otherPath, plan, stamp, tempRoot) {
697
905
  const needs = writeNames(plan);
698
906
  if (needs.length === 0) {
699
- const db = new Database3(otherPath, { readonly: true });
907
+ const db = new Database4(otherPath, { readonly: true });
700
908
  return { db, broughtForward: [], release: () => db.close() };
701
909
  }
702
- const dir = mkdtempSync(join3(tempRoot, "rafa-merge-other-"));
910
+ const dir = mkdtempSync2(join3(tempRoot, "rafa-merge-other-"));
703
911
  const release = (db) => {
704
912
  db?.close();
705
- rmSync3(dir, { recursive: true, force: true });
913
+ rmSync4(dir, { recursive: true, force: true });
706
914
  };
707
915
  try {
708
916
  const copyPath = join3(dir, "other.sqlite");
709
917
  vacuumInto(otherPath, copyPath);
710
- const db = new Database3(copyPath, { readwrite: true });
918
+ const db = new Database4(copyPath, { readwrite: true });
711
919
  try {
712
920
  bringForward(db, copyPath, "write", "open", { ...stamp, builtAside: true });
713
921
  } catch (error) {
@@ -751,7 +959,7 @@ function addedRows(merged) {
751
959
  }
752
960
  function buildMerged(path, parallelPath, other, merge) {
753
961
  vacuumInto(path, parallelPath);
754
- const db = new Database3(parallelPath, { readwrite: true });
962
+ const db = new Database4(parallelPath, { readwrite: true });
755
963
  try {
756
964
  bringForward(db, parallelPath, "write", "open", { ...merge.stamp, builtAside: true });
757
965
  const merged = mergeInto(db, other, merge.id, merge.otherStore, merge.stamp.now().toISOString());
@@ -795,7 +1003,7 @@ function mergeStore(options) {
795
1003
  now: options.now ?? (() => new Date)
796
1004
  };
797
1005
  const mergeId = (options.newMergeId ?? randomUUID)();
798
- const other = openOther(otherPath, otherPlan, mergeStamp, options.tempDir ?? tmpdir());
1006
+ const other = openOther(otherPath, otherPlan, mergeStamp, options.tempDir ?? tmpdir2());
799
1007
  try {
800
1008
  const { built, backupPath } = rebuildAside({
801
1009
  path,
@@ -811,240 +1019,35 @@ function mergeStore(options) {
811
1019
  }
812
1020
  }
813
1021
 
814
- // packages/rafa/src/effort/store/wire.ts
815
- import { existsSync as existsSync4, mkdtempSync as mkdtempSync2, rmSync as rmSync4 } from "fs";
816
- import { tmpdir as tmpdir2 } from "os";
1022
+ // packages/rafa/src/effort/store/ndjson.ts
817
1023
  import { join as join4 } from "path";
818
- import { Database as Database4, SQLiteError as SQLiteError3 } from "bun:sqlite";
819
- var WIRE_FORMAT = "rafa-effort-wire";
820
- var WIRE_VERSION = 1;
821
-
822
- class WireFormatError extends Error {
823
- name = "WireFormatError";
824
- }
825
-
826
- class WireExportRefusal extends Error {
827
- name = "WireExportRefusal";
828
- }
829
- var MERGED_TABLES = Object.entries(MERGE_RULES).filter(([, rule]) => rule.scope === "merged").map(([table]) => table);
830
- var ORIGIN_COLUMNS = ["origin_store", "origin_seq"];
831
- function isRecord(value) {
832
- return typeof value === "object" && value !== null && !Array.isArray(value);
833
- }
834
- function isCount(value) {
835
- return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
836
- }
837
- function isMergedTable(name) {
838
- return MERGED_TABLES.includes(name);
839
- }
840
- function checkCursor(value) {
841
- if (!isRecord(value))
842
- throw new WireFormatError("wire: cursor is not an object");
843
- const entries = Object.entries(value).map(([table, seq]) => {
844
- if (!isMergedTable(table))
845
- throw new WireFormatError(`wire: cursor names ${JSON.stringify(table)}, which is no merged table`);
846
- if (!isCount(seq))
847
- throw new WireFormatError(`wire: cursor for ${table} is ${JSON.stringify(seq)}, expected a whole number of 0 or more`);
848
- return [table, seq];
849
- });
850
- return Object.freeze(Object.fromEntries(entries));
851
- }
852
- function checkRow(table, value, index) {
853
- const where = `wire: ${table} row ${String(index)}`;
854
- if (!isRecord(value))
855
- throw new WireFormatError(`${where} is not an object`);
856
- for (const [column, cell] of Object.entries(value)) {
857
- const carried = cell === null || typeof cell === "string" || typeof cell === "number" && Number.isFinite(cell);
858
- if (!carried)
859
- throw new WireFormatError(`${where} holds ${JSON.stringify(cell) ?? typeof cell} in ${column}, expected a string, a number or null`);
860
- }
861
- const seq = value.seq;
862
- if (!isCount(seq) || seq === 0)
863
- throw new WireFormatError(`${where} has seq ${JSON.stringify(seq) ?? "undefined"}, expected a whole number above 0`);
864
- return Object.freeze({ ...value });
865
- }
866
- function checkMigrations(value) {
867
- if (!Array.isArray(value))
868
- throw new WireFormatError("wire: migrations is not a list");
869
- const ids = value.map((id) => {
870
- if (typeof id !== "string" || id === "")
871
- throw new WireFormatError(`wire: migration id ${JSON.stringify(id)} is not a name`);
872
- return id;
873
- });
874
- const twice = ids.filter((id, index) => ids.indexOf(id) !== index);
875
- if (twice.length > 0)
876
- throw new WireFormatError(`wire: migrations name ${twice.join(", ")} more than once`);
877
- return Object.freeze(ids);
878
- }
879
- function checkTables(value) {
880
- if (!isRecord(value))
881
- throw new WireFormatError("wire: tables is not an object");
882
- const entries = Object.entries(value).map(([table, rows]) => {
883
- if (!isMergedTable(table))
884
- throw new WireFormatError(`wire: tables names ${JSON.stringify(table)}, which is no merged table`);
885
- if (!Array.isArray(rows))
886
- throw new WireFormatError(`wire: tables.${table} is not a list`);
887
- return [table, Object.freeze(rows.map((row, index) => checkRow(table, row, index)))];
888
- });
889
- return Object.freeze(Object.fromEntries(entries));
890
- }
891
- function checkPayload(value) {
892
- if (!isRecord(value))
893
- throw new WireFormatError("wire: the payload is not an object");
894
- if (value.format !== WIRE_FORMAT)
895
- throw new WireFormatError(`wire: format is ${JSON.stringify(value.format) ?? "undefined"}, expected ${JSON.stringify(WIRE_FORMAT)}`);
896
- if (value.version !== WIRE_VERSION)
897
- throw new WireFormatError(`wire: version is ${JSON.stringify(value.version) ?? "undefined"}; this rafa reads version ${String(WIRE_VERSION)}`);
898
- return Object.freeze({
899
- format: WIRE_FORMAT,
900
- version: WIRE_VERSION,
901
- migrations: checkMigrations(value.migrations),
902
- tables: checkTables(value.tables),
903
- cursor: checkCursor(value.cursor)
904
- });
905
- }
906
- function encodeWirePayload(payload) {
907
- return JSON.stringify(payload);
908
- }
909
- function decodeWirePayload(text) {
910
- let parsed;
911
- try {
912
- parsed = JSON.parse(text);
913
- } catch (error) {
914
- const reason = error instanceof Error ? error.message : String(error);
915
- throw new WireFormatError(`wire: the payload is not JSON (${reason})`);
916
- }
917
- return checkPayload(parsed);
918
- }
919
- function readLoggedIds(db) {
920
- const hasLog = db.query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?").get(MIGRATION_LOG_TABLE) !== null;
921
- if (!hasLog)
922
- return null;
923
- return db.query(`SELECT id FROM ${MIGRATION_LOG_TABLE} ORDER BY seq`).all().map(({ id }) => id);
924
- }
925
- function columnsOf(db, table) {
926
- return db.query(`PRAGMA table_info(${quoted(table)})`).all().map(({ name }) => name);
927
- }
928
- function wireValue(value, where) {
929
- if (typeof value === "bigint") {
930
- const number = Number(value);
931
- if (Number.isSafeInteger(number))
932
- return number;
933
- throw new WireExportRefusal(`wire: ${where} holds ${String(value)}, past the whole numbers JSON carries exactly`);
934
- }
935
- if (value instanceof Uint8Array)
936
- throw new WireExportRefusal(`wire: ${where} holds a BLOB, which the wire does not carry`);
937
- return value;
938
- }
939
- function readRows(db, table, columns, after, path) {
940
- const named = columns.map(quoted).join(", ");
941
- return db.query(`SELECT ${named} FROM ${quoted(table)} WHERE seq > ? ORDER BY seq`).all(after).map((row) => Object.freeze(Object.fromEntries(Object.entries(row).map(([column, value]) => [
942
- column,
943
- wireValue(value, `${path} ${table}.${column} at seq ${String(row.seq)}`)
944
- ]))));
945
- }
946
- function readPayload(db, path, cursor) {
947
- const migrations = readLoggedIds(db);
948
- if (migrations === null) {
949
- throw new WireExportRefusal(`wire: ${path} has no migration log; any rafa command that writes the store brings it forward`);
950
- }
951
- const tables = {};
952
- const next = { ...cursor };
953
- for (const table of MERGED_TABLES) {
954
- const columns = columnsOf(db, table);
955
- if (columns.length === 0)
956
- continue;
957
- const missing = ORIGIN_COLUMNS.filter((column) => !columns.includes(column));
958
- if (missing.length > 0) {
959
- throw new WireExportRefusal(`wire: ${path} holds no ${missing.map((column) => `${table}.${column}`).join(", ")}, so its rows carry no origin pair`);
960
- }
961
- const rows = readRows(db, table, columns, cursor[table] ?? 0, path);
962
- tables[table] = Object.freeze(rows);
963
- const last = rows[rows.length - 1];
964
- next[table] = last === undefined ? cursor[table] ?? 0 : Number(last.seq);
965
- }
966
- return Object.freeze({
967
- format: WIRE_FORMAT,
968
- version: WIRE_VERSION,
969
- migrations: Object.freeze([...migrations]),
970
- tables: Object.freeze(tables),
971
- cursor: Object.freeze(next)
972
- });
1024
+ function kindPath(repoRoot, kind) {
1025
+ return join4(effortStoreDir(repoRoot), effortStoreFileName(kind));
973
1026
  }
974
- function exportWirePayload(options) {
975
- const { path } = options;
976
- const cursor = checkCursor(options.cursor ?? {});
1027
+ function openedKindPath(repoRoot, kind) {
1028
+ const path = kindPath(repoRoot, kind);
977
1029
  guardTestProcess(path);
978
- if (!existsSync4(path))
979
- throw new WireExportRefusal(`wire: ${path} is not there`);
980
- const db = new Database4(path, { readonly: true, safeIntegers: true });
981
- try {
982
- db.run(`PRAGMA busy_timeout = ${String(activeStoreSettings().busyTimeoutMs)}`);
983
- return db.transaction(() => readPayload(db, path, cursor))();
984
- } finally {
985
- db.close();
986
- }
1030
+ return path;
987
1031
  }
988
- function namedMigrations(ids, catalogue) {
989
- const known = new Set(catalogue.map(({ id }) => id));
990
- const unknown = ids.filter((id) => !known.has(id));
991
- if (unknown.length > 0) {
992
- throw new WireFormatError(`wire: the payload's store holds migration ${unknown.join(", ")}, which this rafa does not know; update rafa to take its rows`);
993
- }
994
- return catalogue.filter(({ id }) => ids.includes(id));
1032
+ function readKind(repoRoot, kind) {
1033
+ return readStoreRows(openedKindPath(repoRoot, kind));
995
1034
  }
996
- function insertRows(db, table, rows) {
997
- const columns = columnsOf(db, table);
998
- if (columns.length === 0)
999
- throw new WireFormatError(`wire: the payload carries ${table}, which its migrations do not make`);
1000
- const statement = db.query(`INSERT INTO ${quoted(table)} (${columns.map(quoted).join(", ")}) VALUES (${columns.map(() => "?").join(", ")})`);
1001
- const expected = [...columns].sort().join(",");
1002
- rows.forEach((row, index) => {
1003
- const where = `wire: ${table} row ${String(index)}`;
1004
- const held = Object.keys(row);
1005
- if ([...held].sort().join(",") !== expected) {
1006
- throw new WireFormatError(`${where} holds columns ${held.join(", ")}; its migrations make ${columns.join(", ")}`);
1007
- }
1008
- try {
1009
- statement.run(...columns.map((column) => row[column] ?? null));
1010
- } catch (error) {
1011
- if (!(error instanceof SQLiteError3))
1012
- throw error;
1013
- throw new WireFormatError(`${where} (seq ${String(row.seq)}) is refused by the table: ${error.message}`);
1014
- }
1015
- });
1035
+ function keysOfKind(repoRoot, kind) {
1036
+ const path = openedKindPath(repoRoot, kind);
1037
+ return collectedKeys(path, EFFORT_KEY_PROJECTIONS[kind]);
1016
1038
  }
1017
- function buildStore(path, payload, options) {
1018
- const migrations = namedMigrations(payload.migrations, options.migrations ?? SQLITE_MIGRATIONS);
1019
- const db = new Database4(path, { create: true, readwrite: true });
1020
- try {
1021
- bringForward(db, path, "write", "open", {
1022
- migrations,
1023
- appliedBy: appliedByName(options.identity ?? readRuntimeIdentity()),
1024
- now: options.now,
1025
- builtAside: true
1026
- });
1027
- db.transaction(() => {
1028
- for (const [table, rows] of Object.entries(payload.tables))
1029
- insertRows(db, table, rows);
1030
- })();
1031
- } finally {
1032
- db.close();
1033
- }
1039
+ function appendKind(repoRoot, kind, rows) {
1040
+ const path = openedKindPath(repoRoot, kind);
1041
+ return appendRows(path, rows, EFFORT_KEY_PROJECTIONS[kind]);
1034
1042
  }
1035
- function materialiseWirePayload(payload, options = {}) {
1036
- const checked = checkPayload(payload);
1037
- const directory = mkdtempSync2(join4(options.tempDir ?? tmpdir2(), "rafa-wire-"));
1038
- const release = () => rmSync4(directory, { recursive: true, force: true });
1039
- const path = join4(directory, SQLITE_STORE_FILE_NAME);
1040
- try {
1041
- guardTestProcess(path);
1042
- buildStore(path, checked, options);
1043
- } catch (error) {
1044
- release();
1045
- throw error;
1046
- }
1047
- return Object.freeze({ directory, path, release });
1043
+ function openNdjsonStore(repoRoot) {
1044
+ return {
1045
+ append: (kind, rows) => appendKind(repoRoot, kind, rows),
1046
+ keys: (kind) => keysOfKind(repoRoot, kind),
1047
+ read: (kind) => readKind(repoRoot, kind).rows,
1048
+ path: (kind) => kindPath(repoRoot, kind),
1049
+ readRows: (kind) => readKind(repoRoot, kind)
1050
+ };
1048
1051
  }
1049
1052
 
1050
1053
  // packages/rafa/src/effort/store/index.ts
@@ -1064,4 +1067,4 @@ function selectEffortStore(repoRoot, config) {
1064
1067
  return adapter.create({ repoRoot });
1065
1068
  }
1066
1069
 
1067
- export { openNdjsonStore, copyEffortStore, minutesBetween, MOVE_TO_SQLITE, mergeStore, WIRE_FORMAT, WIRE_VERSION, WireFormatError, WireExportRefusal, encodeWirePayload, decodeWirePayload, exportWirePayload, materialiseWirePayload, selectEffortStore };
1070
+ export { copyEffortStore, WIRE_FORMAT, WIRE_VERSION, WireFormatError, WireExportRefusal, encodeWirePayload, decodeWirePayload, exportWirePayload, materialiseWirePayload, minutesBetween, MOVE_TO_SQLITE, mergeStore, openNdjsonStore, selectEffortStore };