@jive-sh/workflow-platform 0.7.0 → 1.0.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/README.md +66 -435
- package/component/_generated/api.ts +72 -0
- package/component/_generated/component.ts +621 -0
- package/component/_generated/dataModel.ts +60 -0
- package/component/_generated/server.ts +169 -0
- package/component/convex.config.ts +3 -0
- package/component/engine.ts +755 -0
- package/component/errors.ts +66 -0
- package/component/events.ts +17 -0
- package/component/notes.ts +13 -0
- package/component/places.ts +136 -0
- package/component/retention.ts +205 -0
- package/component/runEngine.ts +25 -0
- package/component/schema.ts +132 -0
- package/component/steps.ts +188 -0
- package/component/store.ts +512 -0
- package/component/tasks.ts +289 -0
- package/component/worker.ts +109 -0
- package/component/workflows.ts +83 -0
- package/dist/definition/agent-runner.js +9 -0
- package/dist/definition/agent-runner.js.map +1 -0
- package/dist/definition/file-store.js +9 -0
- package/dist/definition/file-store.js.map +1 -0
- package/dist/definition/program-build.js +10 -0
- package/dist/definition/program-build.js.map +1 -0
- package/dist/definition/step.js +1 -0
- package/dist/definition/step.js.map +1 -0
- package/dist/definition/task-schema.js +5 -0
- package/dist/definition/task-schema.js.map +1 -0
- package/dist/definition/tasks.js +9 -0
- package/dist/definition/tasks.js.map +1 -0
- package/dist/definition/workflow.js +111 -0
- package/dist/definition/workflow.js.map +1 -0
- package/dist/graph.js +301 -9
- package/dist/graph.js.map +1 -1
- package/dist/index.js +7 -196
- package/dist/index.js.map +1 -1
- package/package.json +9 -4
- package/src/definition/agent-runner.ts +59 -0
- package/src/definition/file-store.ts +20 -0
- package/src/definition/program-build.ts +30 -0
- package/src/definition/step.ts +106 -0
- package/src/definition/task-schema.ts +15 -0
- package/src/definition/tasks.ts +43 -0
- package/src/definition/workflow.ts +255 -0
- package/src/graph.ts +341 -19
- package/src/index.ts +8 -238
- package/worker/directory-file-store.ts +42 -0
- package/worker/index.ts +12 -0
- package/worker/platform-client.ts +138 -0
- package/worker/step-meter.ts +80 -0
- package/worker/worker-tasks.ts +27 -0
- package/worker/worker.test.ts +200 -0
- package/worker/worker.ts +240 -0
- package/dist/budget-states.js +0 -30
- package/dist/budget-states.js.map +0 -1
- package/dist/cpu-limit/limiter.js +0 -839
- package/dist/cpu-limit/limiter.js.map +0 -1
- package/dist/cpu-limit/use.js +0 -139
- package/dist/cpu-limit/use.js.map +0 -1
- package/dist/dashboard/agent-usage.js +0 -25
- package/dist/dashboard/agent-usage.js.map +0 -1
- package/dist/dashboard/dashboard.css +0 -148
- package/dist/dashboard/drilldown.js +0 -47
- package/dist/dashboard/drilldown.js.map +0 -1
- package/dist/dashboard/queue-lines.js +0 -44
- package/dist/dashboard/queue-lines.js.map +0 -1
- package/dist/dashboard/queue-table.js +0 -16
- package/dist/dashboard/queue-table.js.map +0 -1
- package/dist/dashboard/run-doing-words.js +0 -29
- package/dist/dashboard/run-doing-words.js.map +0 -1
- package/dist/dashboard/setting-words.js +0 -18
- package/dist/dashboard/setting-words.js.map +0 -1
- package/dist/dashboard/spending-words.js +0 -24
- package/dist/dashboard/spending-words.js.map +0 -1
- package/dist/failure.js +0 -35
- package/dist/failure.js.map +0 -1
- package/dist/keep-running.js +0 -79
- package/dist/keep-running.js.map +0 -1
- package/dist/outside-failure.js +0 -31
- package/dist/outside-failure.js.map +0 -1
- package/dist/pick-wakes.js +0 -125
- package/dist/pick-wakes.js.map +0 -1
- package/dist/project.js +0 -1
- package/dist/project.js.map +0 -1
- package/dist/queue-counts.js +0 -51
- package/dist/queue-counts.js.map +0 -1
- package/dist/reasons.js +0 -33
- package/dist/reasons.js.map +0 -1
- package/dist/retention.js +0 -59
- package/dist/retention.js.map +0 -1
- package/dist/settings.js +0 -88
- package/dist/settings.js.map +0 -1
- package/dist/spending-limit.js +0 -82
- package/dist/spending-limit.js.map +0 -1
- package/dist/step-files.js +0 -200
- package/dist/step-files.js.map +0 -1
- package/dist/step-meter.js +0 -97
- package/dist/step-meter.js.map +0 -1
- package/dist/step-times.js +0 -114
- package/dist/step-times.js.map +0 -1
- package/dist/step-usage.js +0 -19
- package/dist/step-usage.js.map +0 -1
- package/dist/steps.js +0 -1205
- package/dist/steps.js.map +0 -1
- package/dist/storage/agent-claims.js +0 -3
- package/dist/storage/agent-claims.js.map +0 -1
- package/dist/storage/budgets.js +0 -121
- package/dist/storage/budgets.js.map +0 -1
- package/dist/storage/engine-records.js +0 -91
- package/dist/storage/engine-records.js.map +0 -1
- package/dist/storage/engine-storage.js +0 -67
- package/dist/storage/engine-storage.js.map +0 -1
- package/dist/storage/input-records.js +0 -12
- package/dist/storage/input-records.js.map +0 -1
- package/dist/storage/move-store.js +0 -251
- package/dist/storage/move-store.js.map +0 -1
- package/dist/storage/sqlite-store.js +0 -901
- package/dist/storage/sqlite-store.js.map +0 -1
- package/dist/storage/step-records.js +0 -11
- package/dist/storage/step-records.js.map +0 -1
- package/dist/storage/storage-error.js +0 -19
- package/dist/storage/storage-error.js.map +0 -1
- package/dist/storage/task-store.js +0 -30
- package/dist/storage/task-store.js.map +0 -1
- package/dist/system-workflows.js +0 -54
- package/dist/system-workflows.js.map +0 -1
- package/dist/tasks.js +0 -500
- package/dist/tasks.js.map +0 -1
- package/dist/views/format.js +0 -37
- package/dist/views/format.js.map +0 -1
- package/dist/views/runs-now.js +0 -117
- package/dist/views/runs-now.js.map +0 -1
- package/dist/views/step-funnel.js +0 -98
- package/dist/views/step-funnel.js.map +0 -1
- package/dist/views/step-graph.js +0 -39
- package/dist/views/step-graph.js.map +0 -1
- package/dist/views/workflows-view.js +0 -236
- package/dist/views/workflows-view.js.map +0 -1
- package/src/budget-states.ts +0 -54
- package/src/cpu-limit/limiter.ts +0 -935
- package/src/cpu-limit/use.ts +0 -166
- package/src/dashboard/agent-usage-line.tsx +0 -28
- package/src/dashboard/agent-usage.ts +0 -48
- package/src/dashboard/dashboard.css +0 -148
- package/src/dashboard/drilldown.ts +0 -70
- package/src/dashboard/index.tsx +0 -4
- package/src/dashboard/limits-and-runs.tsx +0 -191
- package/src/dashboard/machine-controls.tsx +0 -53
- package/src/dashboard/queue-bar.tsx +0 -62
- package/src/dashboard/queue-chart.tsx +0 -149
- package/src/dashboard/queue-lines.ts +0 -87
- package/src/dashboard/queue-table.ts +0 -37
- package/src/dashboard/run-doing-words.ts +0 -41
- package/src/dashboard/setting-words.ts +0 -30
- package/src/dashboard/spending-limit-control.tsx +0 -47
- package/src/dashboard/spending-words.ts +0 -32
- package/src/dashboard/time-distribution.tsx +0 -61
- package/src/dashboard/workflow-dag.tsx +0 -322
- package/src/dashboard/workflow-drilldown.tsx +0 -31
- package/src/dashboard/workflows-dashboard.tsx +0 -532
- package/src/failure.ts +0 -36
- package/src/keep-running.ts +0 -85
- package/src/outside-failure.ts +0 -39
- package/src/pick-wakes.ts +0 -182
- package/src/project.ts +0 -127
- package/src/queue-counts.ts +0 -64
- package/src/reasons.ts +0 -30
- package/src/retention.ts +0 -90
- package/src/settings.ts +0 -171
- package/src/spending-limit.ts +0 -139
- package/src/step-files.ts +0 -208
- package/src/step-meter.ts +0 -120
- package/src/step-times.ts +0 -151
- package/src/step-usage.ts +0 -23
- package/src/steps.ts +0 -1616
- package/src/storage/agent-claims.ts +0 -63
- package/src/storage/budgets.ts +0 -152
- package/src/storage/engine-records.ts +0 -119
- package/src/storage/engine-storage.ts +0 -82
- package/src/storage/input-records.ts +0 -43
- package/src/storage/move-store.ts +0 -265
- package/src/storage/sqlite-store.ts +0 -1022
- package/src/storage/step-records.ts +0 -358
- package/src/storage/storage-error.ts +0 -35
- package/src/storage/task-store.ts +0 -235
- package/src/system-workflows.ts +0 -70
- package/src/tasks.ts +0 -586
- package/src/views/format.ts +0 -40
- package/src/views/runs-now.ts +0 -188
- package/src/views/step-funnel.ts +0 -163
- package/src/views/step-graph.ts +0 -77
- package/src/views/workflows-view.ts +0 -484
- package/src/workflow-dag.test.tsx +0 -89
- package/src/workflow-page.test.tsx +0 -170
|
@@ -1,265 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* THE STEP RECORDS AND TASKS MOVE INTO THE PROJECT'S ONE DATABASE, LIVE, from the file they had of their own. Alex,
|
|
3
|
-
* 2026-11-14: "the workflow platform should only have 1 [database]"; the design is `STORAGE.md`.
|
|
4
|
-
*
|
|
5
|
-
* Every process opens the new file. The first open copies the old file's rows across, highest ids first, so the new
|
|
6
|
-
* file's next id is above every old one from its first chunk on and a row written meanwhile never takes an old row's id;
|
|
7
|
-
* each chunk is a transaction of its own, so no writer waits on the copy for long, and copying again changes nothing.
|
|
8
|
-
* A process still on the old code (a runner draining for up to 600 s) keeps writing the old file: `catchUpMove` copies
|
|
9
|
-
* what it wrote after, with new ids, and the rows it closed after the copy. Once the old file has been quiet for
|
|
10
|
-
* `QUIET_BEFORE_RENAME_SECONDS`, it is caught up once more and renamed `.moved`, never deleted.
|
|
11
|
-
*/
|
|
12
|
-
import { Database } from 'bun:sqlite';
|
|
13
|
-
import fs from 'node:fs';
|
|
14
|
-
import { applyStepRecordsSchema } from './sqlite-store';
|
|
15
|
-
|
|
16
|
-
/** Rows copied per transaction: a chunk of step records is tens of milliseconds. */
|
|
17
|
-
const CHUNK = 5_000;
|
|
18
|
-
|
|
19
|
-
/** How long the old file must go unwritten before it is renamed: past any old runner's 600 s handover. */
|
|
20
|
-
export const QUIET_BEFORE_RENAME_SECONDS = 20 * 60;
|
|
21
|
-
|
|
22
|
-
const MOVES_SCHEMA = `
|
|
23
|
-
CREATE TABLE IF NOT EXISTS store_moves (
|
|
24
|
-
from_file TEXT PRIMARY KEY,
|
|
25
|
-
copied_at REAL NOT NULL,
|
|
26
|
-
step_runs_max INTEGER NOT NULL,
|
|
27
|
-
step_items_max INTEGER NOT NULL,
|
|
28
|
-
tasks_max INTEGER NOT NULL,
|
|
29
|
-
notes_max INTEGER NOT NULL,
|
|
30
|
-
renamed_at REAL
|
|
31
|
-
);`;
|
|
32
|
-
|
|
33
|
-
/** The tables moved, in the order a row's references are copied before it. */
|
|
34
|
-
const MOVED: ReadonlyArray<{ readonly table: string; readonly key: string | null }> = [
|
|
35
|
-
{ table: 'step_runs', key: 'id' },
|
|
36
|
-
{ table: 'step_functions', key: 'id' },
|
|
37
|
-
{ table: 'tasks', key: 'id' },
|
|
38
|
-
{ table: 'task_notes', key: 'id' },
|
|
39
|
-
{ table: 'input_runs', key: null },
|
|
40
|
-
{ table: 'cleared_but_listed', key: null },
|
|
41
|
-
{ table: 'undeclared_writes', key: null },
|
|
42
|
-
];
|
|
43
|
-
|
|
44
|
-
const columnsOf = (db: Database, table: string): ReadonlyArray<string> =>
|
|
45
|
-
(db.query(`SELECT name FROM pragma_table_info(?, 'main')`).all(table) as Array<{ name: string }>).map((c) => c.name);
|
|
46
|
-
|
|
47
|
-
const oldColumns = (db: Database, table: string): ReadonlyArray<string> =>
|
|
48
|
-
(db.query(`PRAGMA old.table_info(${table})`).all() as Array<{ name: string }>).map((c) => c.name);
|
|
49
|
-
|
|
50
|
-
/** The columns both files have, as `a, b, c`: the old file may predate a column. */
|
|
51
|
-
const shared = (db: Database, table: string): string => {
|
|
52
|
-
const old = new Set(oldColumns(db, table));
|
|
53
|
-
return columnsOf(db, table).filter((c) => old.has(c)).map((c) => `"${c}"`).join(', ');
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const maxOf = (db: Database, schema: 'main' | 'old', table: string): number =>
|
|
57
|
-
(db.query(`SELECT COALESCE(MAX(id), 0) AS n FROM ${schema}.${table}`).get() as { n: number }).n;
|
|
58
|
-
|
|
59
|
-
const withOld = <A>(db: Database, from: string, go: () => A): A => {
|
|
60
|
-
db.exec(`ATTACH DATABASE '${from.replaceAll("'", "''")}' AS old`);
|
|
61
|
-
try {
|
|
62
|
-
return go();
|
|
63
|
-
} finally {
|
|
64
|
-
db.exec('DETACH DATABASE old');
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const recorded = (db: Database, from: string) =>
|
|
69
|
-
db.query('SELECT copied_at, step_runs_max, step_items_max, tasks_max, notes_max, renamed_at FROM store_moves WHERE from_file = ?').get(from) as
|
|
70
|
-
| { copied_at: number; step_runs_max: number; step_items_max: number; tasks_max: number; notes_max: number; renamed_at: number | null }
|
|
71
|
-
| null;
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* The first copy, on a writer's first open of the new file: a no-op once recorded, or with no old file. `db` has the
|
|
75
|
-
* platform's tables; the caller retries a lock it meets.
|
|
76
|
-
*/
|
|
77
|
-
export const copyOldStore = (db: Database, from: string): void => {
|
|
78
|
-
db.exec(MOVES_SCHEMA);
|
|
79
|
-
if (!fs.existsSync(from) || recorded(db, from) !== null) return;
|
|
80
|
-
withOld(db, from, () => {
|
|
81
|
-
const max = { step_runs: maxOf(db, 'old', 'step_runs'), step_functions: maxOf(db, 'old', 'step_functions'), tasks: maxOf(db, 'old', 'tasks'), task_notes: maxOf(db, 'old', 'task_notes') };
|
|
82
|
-
for (const { table, key } of MOVED) {
|
|
83
|
-
const columns = shared(db, table);
|
|
84
|
-
if (key === null) {
|
|
85
|
-
db.transaction(() => db.exec(`INSERT OR IGNORE INTO main.${table} (${columns}) SELECT ${columns} FROM old.${table}`))();
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
// Highest first, up to the highest when the copy began: the first chunk puts the new file's next id above every old
|
|
89
|
-
// one, and a row the old code writes meanwhile is the catch-up's, with a new id.
|
|
90
|
-
for (let below = max[table as keyof typeof max] + 1; ;) {
|
|
91
|
-
const n = db.transaction(() => {
|
|
92
|
-
const rows = db.query(`SELECT ${key} AS k FROM old.${table} WHERE ${key} < ? ORDER BY ${key} DESC LIMIT ${CHUNK}`).all(below) as Array<{ k: number }>;
|
|
93
|
-
if (rows.length === 0) return { n: 0, low: below };
|
|
94
|
-
const low = rows[rows.length - 1]!.k;
|
|
95
|
-
db.query(`INSERT OR IGNORE INTO main.${table} (${columns}) SELECT ${columns} FROM old.${table} WHERE ${key} >= ? AND ${key} < ?`).run(low, below);
|
|
96
|
-
return { n: rows.length, low };
|
|
97
|
-
})();
|
|
98
|
-
if (n.n === 0) break;
|
|
99
|
-
below = n.low;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
db.query('INSERT OR IGNORE INTO store_moves (from_file, copied_at, step_runs_max, step_items_max, tasks_max, notes_max) VALUES (?, ?, ?, ?, ?, ?)')
|
|
103
|
-
.run(from, Date.now() / 1000, max.step_runs, max.step_functions, max.tasks, max.task_notes);
|
|
104
|
-
});
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
/** What a catch-up copied, and whether it renamed the old file. */
|
|
108
|
-
export interface CatchUp {
|
|
109
|
-
readonly stepRuns: number;
|
|
110
|
-
readonly closed: number;
|
|
111
|
-
readonly tasks: number;
|
|
112
|
-
readonly notes: number;
|
|
113
|
-
readonly renamed: boolean;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* WHAT THE OLD CODE WROTE AFTER THE COPY: its new rows, with new ids and their references mapped (a step record's items,
|
|
118
|
-
* a note's task), and the records it closed. Run while the old file may still be written; once it has been quiet long
|
|
119
|
-
* enough, the last catch-up renames it. A no-op before the first copy and after the rename.
|
|
120
|
-
*/
|
|
121
|
-
export const catchUpMove = (db: Database, from: string, now: number): CatchUp | null => {
|
|
122
|
-
db.exec(MOVES_SCHEMA);
|
|
123
|
-
const was = recorded(db, from);
|
|
124
|
-
if (was === null || was.renamed_at !== null || !fs.existsSync(from)) return null;
|
|
125
|
-
// Quiet means no row came across for that long: the old file's times say nothing, since opening it to read changes them.
|
|
126
|
-
const done = withOld(db, from, () => db.transaction(() => {
|
|
127
|
-
const runColumns = shared(db, 'step_runs').split(', ').filter((c) => c !== '"id"').join(', ');
|
|
128
|
-
// Records the old code opened after the copy: new ids, each old id's new one kept for its items.
|
|
129
|
-
const late = db.query(`SELECT id FROM old.step_runs WHERE id > ? ORDER BY id`).all(was.step_runs_max) as Array<{ id: number }>;
|
|
130
|
-
const newIds = new Map<number, number>();
|
|
131
|
-
for (const r of late) {
|
|
132
|
-
const inserted = db.query(`INSERT INTO main.step_runs (${runColumns}) SELECT ${runColumns} FROM old.step_runs WHERE id = ?`).run(r.id);
|
|
133
|
-
newIds.set(r.id, Number(inserted.lastInsertRowid));
|
|
134
|
-
}
|
|
135
|
-
const itemColumns = shared(db, 'step_functions').split(', ').filter((c) => c !== '"id"' && c !== '"step_run"').join(', ');
|
|
136
|
-
for (const [oldId, newId] of newIds) {
|
|
137
|
-
db.query(`INSERT INTO main.step_functions (step_run, ${itemColumns}) SELECT ?, ${itemColumns} FROM old.step_functions WHERE step_run = ?`).run(newId, oldId);
|
|
138
|
-
}
|
|
139
|
-
// Records copied open that the old code closed after: their ends.
|
|
140
|
-
const closedColumns = ['ended', 'outcome', 'error', 'reason', 'fresh', 'cache_write', 'cache_read', 'output', 'compiles', 'requests', 'model', 'subject', 'files', 'scope', 'cpu_seconds', 'child_cpu_seconds', 'own_cpu_seconds', 'peak_memory_mb', 'files_written', 'beat', 'waiting']
|
|
141
|
-
.filter((c) => shared(db, 'step_runs').includes(`"${c}"`));
|
|
142
|
-
const closed = db.query(
|
|
143
|
-
`UPDATE main.step_runs SET (${closedColumns.join(', ')}) = (SELECT ${closedColumns.join(', ')} FROM old.step_runs o WHERE o.id = main.step_runs.id)
|
|
144
|
-
WHERE ended IS NULL AND id <= ? AND id IN (SELECT id FROM old.step_runs WHERE ended IS NOT NULL AND id <= ?)`,
|
|
145
|
-
).run(was.step_runs_max, was.step_runs_max).changes;
|
|
146
|
-
// Tasks by their workflow and item, and notes with their tasks mapped the same way; an outcome already moved is kept once.
|
|
147
|
-
const tasks = db.query(
|
|
148
|
-
`INSERT OR IGNORE INTO main.tasks (workflow, item, item_key, created_at, listed, listed_at)
|
|
149
|
-
SELECT workflow, item, item_key, created_at, listed, listed_at FROM old.tasks WHERE id > ?`,
|
|
150
|
-
).run(was.tasks_max).changes;
|
|
151
|
-
const noteColumns = shared(db, 'task_notes').split(', ').filter((c) => !['"id"', '"task"', '"about"'].includes(c));
|
|
152
|
-
const notes = db.query(
|
|
153
|
-
`INSERT OR IGNORE INTO main.task_notes (task, about, ${noteColumns.join(', ')})
|
|
154
|
-
SELECT (SELECT m.id FROM main.tasks m WHERE m.workflow = t.workflow AND m.item_key = t.item_key),
|
|
155
|
-
(SELECT m.id FROM main.tasks m JOIN old.tasks a ON a.workflow = m.workflow AND a.item_key = m.item_key WHERE a.id = n.about),
|
|
156
|
-
${noteColumns.map((c) => `n.${c}`).join(', ')}
|
|
157
|
-
FROM old.task_notes n JOIN old.tasks t ON t.id = n.task WHERE n.id > ? ORDER BY n.id`,
|
|
158
|
-
).run(was.notes_max).changes;
|
|
159
|
-
for (const table of ['input_runs', 'cleared_but_listed']) {
|
|
160
|
-
const columns = shared(db, table);
|
|
161
|
-
db.exec(`INSERT OR REPLACE INTO main.${table} (${columns}) SELECT ${columns} FROM old.${table}`);
|
|
162
|
-
}
|
|
163
|
-
const writes = shared(db, 'undeclared_writes');
|
|
164
|
-
db.exec(`INSERT OR IGNORE INTO main.undeclared_writes (${writes}) SELECT ${writes} FROM old.undeclared_writes`);
|
|
165
|
-
const changed = late.length + closed + tasks + notes > 0;
|
|
166
|
-
const quiet = !changed && now - was.copied_at >= QUIET_BEFORE_RENAME_SECONDS;
|
|
167
|
-
db.query('UPDATE store_moves SET step_runs_max = (SELECT COALESCE(MAX(id), 0) FROM old.step_runs), tasks_max = (SELECT COALESCE(MAX(id), 0) FROM old.tasks), notes_max = (SELECT COALESCE(MAX(id), 0) FROM old.task_notes), copied_at = ?, renamed_at = ? WHERE from_file = ?')
|
|
168
|
-
.run(changed ? now : was.copied_at, quiet ? now : null, from);
|
|
169
|
-
return { stepRuns: late.length, closed, tasks, notes, quiet };
|
|
170
|
-
})());
|
|
171
|
-
const quiet = done.quiet;
|
|
172
|
-
if (quiet) for (const f of [from, `${from}-wal`, `${from}-shm`]) if (fs.existsSync(f)) fs.renameSync(f, `${f}.moved`);
|
|
173
|
-
return { stepRuns: done.stepRuns, closed: done.closed, tasks: done.tasks, notes: done.notes, renamed: quiet };
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* The move, as the project's job runner makes it once at its start, before any turn: the first copy, then a catch-up.
|
|
178
|
-
* Every process reads and writes the old file until the copy is recorded in `file` (`sqliteStore` `movedFrom`). A
|
|
179
|
-
* held lock throws, for the caller to retry; copying again changes nothing.
|
|
180
|
-
*/
|
|
181
|
-
export const moveRecordsInto = (r: { readonly file: string; readonly from: string; readonly now: number }): CatchUp | null => {
|
|
182
|
-
if (!fs.existsSync(r.from) || !fs.existsSync(r.file)) return null;
|
|
183
|
-
const db = new Database(r.file);
|
|
184
|
-
try {
|
|
185
|
-
db.exec('PRAGMA busy_timeout = 0');
|
|
186
|
-
applyStepRecordsSchema(db);
|
|
187
|
-
copyOldStore(db, r.from);
|
|
188
|
-
return catchUpMove(db, r.from, r.now);
|
|
189
|
-
} finally {
|
|
190
|
-
db.close();
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* WHETHER THE ENGINE'S OLD FILE IS FREE TO MOVE: no runner but `self` holds a shard lock there refreshed in the last
|
|
196
|
-
* minute. A runner refreshes its locks every few seconds while its engine lives and releases them when it hands its work
|
|
197
|
-
* over, so the old runner's engine has stopped writing once this is true.
|
|
198
|
-
*/
|
|
199
|
-
export const engineFileFree = (r: { readonly from: string; readonly self: string }): boolean => {
|
|
200
|
-
if (!fs.existsSync(r.from)) return true;
|
|
201
|
-
const db = new Database(r.from, { readonly: true });
|
|
202
|
-
try {
|
|
203
|
-
if (db.query(`SELECT 1 FROM sqlite_master WHERE name = 'cluster_locks'`).get() === null) return true;
|
|
204
|
-
const held = db.query(`SELECT COUNT(*) AS n FROM cluster_locks WHERE address <> ? AND acquired_at > datetime('now', '-60 seconds')`).get(r.self) as { n: number };
|
|
205
|
-
return held.n === 0;
|
|
206
|
-
} finally {
|
|
207
|
-
db.close();
|
|
208
|
-
}
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
/** The engine's tables, every row; its locks and runners are this moment's only, and start clean. */
|
|
212
|
-
const ENGINE_RUNTIME_TABLES = new Set(['cluster_locks', 'cluster_runners']);
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* THE ENGINE'S RECORDS MOVE INTO THE PROJECT'S ONE DATABASE, once its old file is free (\`engineFileFree\`): its tables
|
|
216
|
-
* made from the old file's own definitions, every row copied by rowid in chunks, so no writer waits on the copy for long,
|
|
217
|
-
* its indexes, and the move recorded; then the old file is renamed \`.moved\`, never deleted. A held lock throws, for the
|
|
218
|
-
* caller to retry; copying again changes nothing. What it copied.
|
|
219
|
-
*/
|
|
220
|
-
export const moveEngineInto = (r: { readonly file: string; readonly from: string; readonly now: number }): { readonly tables: number; readonly rows: number } | null => {
|
|
221
|
-
if (!fs.existsSync(r.from) || !fs.existsSync(r.file)) return null;
|
|
222
|
-
const db = new Database(r.file);
|
|
223
|
-
let rows = 0;
|
|
224
|
-
let tables = 0;
|
|
225
|
-
try {
|
|
226
|
-
db.exec('PRAGMA busy_timeout = 0');
|
|
227
|
-
db.exec(MOVES_SCHEMA);
|
|
228
|
-
if (recorded(db, r.from) !== null) return null;
|
|
229
|
-
withOld(db, r.from, () => {
|
|
230
|
-
const objects = db.query(`SELECT type, name, sql FROM old.sqlite_master WHERE name LIKE 'cluster_%' AND sql IS NOT NULL ORDER BY type DESC`).all() as Array<{ type: string; name: string; sql: string }>;
|
|
231
|
-
for (const o of objects.filter((x) => x.type === 'table')) {
|
|
232
|
-
db.exec(o.sql.replace(/^CREATE TABLE\s+(?:IF NOT EXISTS\s+)?/i, 'CREATE TABLE IF NOT EXISTS '));
|
|
233
|
-
tables += 1;
|
|
234
|
-
if (ENGINE_RUNTIME_TABLES.has(o.name)) continue;
|
|
235
|
-
// Each chunk the rows after the last rowid already copied, the ids never leaving SQLite: the engine's are snowflakes
|
|
236
|
-
// (2.5e17 on 2026-11-17), sparse and past a JavaScript number's exact range. A try again after a held lock starts
|
|
237
|
-
// where the last stopped.
|
|
238
|
-
const copy = db.query(`INSERT OR IGNORE INTO main."${o.name}" SELECT * FROM old."${o.name}"
|
|
239
|
-
WHERE (SELECT MAX(rowid) FROM main."${o.name}") IS NULL OR rowid > (SELECT MAX(rowid) FROM main."${o.name}") ORDER BY rowid LIMIT ${CHUNK}`);
|
|
240
|
-
for (;;) {
|
|
241
|
-
const copied = db.transaction(() => copy.run().changes)();
|
|
242
|
-
if (copied === 0) break;
|
|
243
|
-
rows += copied;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
for (const o of objects.filter((x) => x.type === 'index')) db.exec(o.sql.replace(/^CREATE (UNIQUE )?INDEX\s+(?:IF NOT EXISTS\s+)?/i, 'CREATE $1INDEX IF NOT EXISTS '));
|
|
247
|
-
});
|
|
248
|
-
db.query('INSERT OR IGNORE INTO store_moves (from_file, copied_at, step_runs_max, step_items_max, tasks_max, notes_max, renamed_at) VALUES (?, ?, 0, 0, 0, 0, ?)').run(r.from, r.now, r.now);
|
|
249
|
-
} finally {
|
|
250
|
-
db.close();
|
|
251
|
-
}
|
|
252
|
-
for (const f of [r.from, `${r.from}-wal`, `${r.from}-shm`]) if (fs.existsSync(f)) fs.renameSync(f, `${f}.moved`);
|
|
253
|
-
return { tables, rows };
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
/** Whether a file's records moved into \`file\`: its move is recorded there. */
|
|
257
|
-
export const movedInto = (r: { readonly file: string; readonly from: string }): boolean => {
|
|
258
|
-
if (!fs.existsSync(r.file)) return false;
|
|
259
|
-
const db = new Database(r.file, { readonly: true });
|
|
260
|
-
try {
|
|
261
|
-
return db.query(`SELECT 1 FROM sqlite_master WHERE name = 'store_moves'`).get() !== null && db.query('SELECT 1 FROM store_moves WHERE from_file = ?').get(r.from) !== null;
|
|
262
|
-
} finally {
|
|
263
|
-
db.close();
|
|
264
|
-
}
|
|
265
|
-
};
|