@north-light/cli 0.3.755 → 0.3.757
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/client.d.ts +1 -2
- package/dist/api/client.js +3 -3
- package/dist/api/contract.d.ts +1 -1
- package/dist/api/contract.js +1 -1
- package/dist/api/dto/health.d.ts +0 -54
- package/dist/api/routes.d.ts +0 -1
- package/dist/api/routes.js +1 -1
- package/dist/build-root.d.ts +0 -3
- package/dist/build-root.js +1 -1
- package/dist/builtin-memory/internal/storage-tiers.md +3 -3
- package/dist/cli.js +8 -9
- package/dist/clients/attach/viewer.js +474 -474
- package/dist/commands/profile/new.js +1 -1
- package/dist/commands/profile/project.js +1 -1
- package/dist/commands/sys/branch.js +1 -1
- package/dist/commands/sys/daemon.js +1 -1
- package/dist/commands/sys.js +1 -1
- package/dist/core/bootstrap.js +1 -1
- package/dist/core/canvas/boot.d.ts +0 -25
- package/dist/core/canvas/boot.js +1 -1
- package/dist/core/canvas/executions.d.ts +0 -3
- package/dist/core/canvas/executions.js +1 -1
- package/dist/core/canvas/pid.d.ts +0 -10
- package/dist/core/canvas/pid.js +5 -5
- package/dist/core/directory/join.js +1 -1
- package/dist/core/grants/mirror.d.ts +0 -5
- package/dist/core/grants/mirror.js +6 -6
- package/dist/core/grants/sync.js +1 -1
- package/dist/core/human/page-schema.d.ts +1 -1
- package/dist/core/io.js +4 -4
- package/dist/core/layout.d.ts +0 -2
- package/dist/core/layout.js +2 -2
- package/dist/core/profiles/manifest.js +1 -1
- package/dist/core/profiles/project-dir.d.ts +3 -0
- package/dist/core/profiles/project-dir.js +1 -0
- package/dist/core/scoped-state/db.js +2 -2
- package/dist/daemon/api/handlers/daemon.js +1 -1
- package/dist/daemon/api/handlers/profiles.js +1 -1
- package/dist/daemon/api/map.d.ts +1 -6
- package/dist/daemon/api/map.js +2 -2
- package/dist/daemon/api/operations.js +1 -1
- package/dist/daemon/api/server.js +1 -1
- package/dist/daemon/control.d.ts +0 -8
- package/dist/daemon/crtrd.js +18 -20
- package/dist/daemon/manage.js +3 -3
- package/package.json +3 -3
- package/packages/crouter-api/dist/api/client.js +0 -3
- package/packages/crouter-api/dist/api/contract.js +1 -1
- package/packages/crouter-api/dist/api/routes.js +0 -1
- package/packages/crouter-api/dist/build-root.js +0 -9
- package/packages/crouter-api/dist/commands/profile/new.js +2 -2
- package/packages/crouter-api/dist/commands/profile/project.js +2 -1
- package/packages/crouter-api/dist/commands/sys/branch.js +1 -1
- package/packages/crouter-api/dist/commands/sys/daemon.js +1 -1
- package/packages/crouter-api/dist/commands/sys.js +1 -2
- package/packages/crouter-api/dist/core/canvas/executions.js +1 -17
- package/packages/crouter-api/dist/core/canvas/pid.js +0 -16
- package/packages/crouter-api/dist/core/directory/join.js +18 -1
- package/packages/crouter-api/dist/core/grants/mirror.js +0 -7
- package/packages/crouter-api/dist/core/grants/sync.js +2 -2
- package/packages/crouter-api/dist/core/io.js +0 -5
- package/packages/crouter-api/dist/core/layout.js +2 -19
- package/packages/crouter-api/dist/core/profiles/manifest.js +12 -18
- package/packages/crouter-api/dist/core/profiles/project-dir.js +20 -0
- package/packages/crouter-api/dist/daemon/api/map.js +0 -16
- package/packages/crouter-api/dist/daemon/api/operations.js +0 -1
- package/packages/crouter-api/dist/daemon/manage.js +1 -1
- package/packages/crouter-api/package.json +1 -1
- package/packages/crouter-identity/package.json +1 -1
- package/packages/crouter-plugin/package.json +3 -3
- package/runtime.lock.json +14 -14
- package/scripts/install-runtime.mjs +1 -30
- package/dist/commands/sys/migrate.d.ts +0 -1
- package/dist/commands/sys/migrate.js +0 -1
- package/dist/core/canvas/import-sqlite.d.ts +0 -46
- package/dist/core/canvas/import-sqlite.js +0 -19
- package/dist/core/canvas/migrations.d.ts +0 -16
- package/dist/core/canvas/migrations.js +0 -1073
- package/dist/core/feed/inbox-migrate.d.ts +0 -11
- package/dist/core/feed/inbox-migrate.js +0 -3
- package/dist/core/graph/bodies-boot-copy.d.ts +0 -28
- package/dist/core/graph/bodies-boot-copy.js +0 -1
- package/dist/core/layout-migrate/index.d.ts +0 -38
- package/dist/core/layout-migrate/index.js +0 -4
- package/dist/core/layout-migrate/loose-keys.d.ts +0 -2
- package/dist/core/layout-migrate/loose-keys.js +0 -1
- package/dist/core/layout-migrate/paths.d.ts +0 -30
- package/dist/core/layout-migrate/paths.js +0 -1
- package/dist/core/layout-migrate/stored-paths.d.ts +0 -9
- package/dist/core/layout-migrate/stored-paths.js +0 -11
- package/dist/core/layout-migrate/stores.d.ts +0 -30
- package/dist/core/layout-migrate/stores.js +0 -3
- package/dist/core/scoped-state/migrate.d.ts +0 -15
- package/dist/core/scoped-state/migrate.js +0 -3
- package/dist/daemon/boot-migrations.d.ts +0 -14
- package/dist/daemon/boot-migrations.js +0 -1
- package/dist/migrations/004-canvas-documents/apply.d.ts +0 -8
- package/dist/migrations/004-canvas-documents/apply.js +0 -1
- package/dist/migrations/004-canvas-documents/index.d.ts +0 -16
- package/dist/migrations/004-canvas-documents/index.js +0 -2
- package/dist/migrations/004-canvas-documents/legacy/discover.d.ts +0 -16
- package/dist/migrations/004-canvas-documents/legacy/discover.js +0 -1
- package/dist/migrations/004-canvas-documents/legacy/history.d.ts +0 -5
- package/dist/migrations/004-canvas-documents/legacy/history.js +0 -2
- package/dist/migrations/004-canvas-documents/legacy/nested.d.ts +0 -7
- package/dist/migrations/004-canvas-documents/legacy/nested.js +0 -1
- package/dist/migrations/004-canvas-documents/legacy/parse.d.ts +0 -14
- package/dist/migrations/004-canvas-documents/legacy/parse.js +0 -1
- package/dist/migrations/004-canvas-documents/legacy/types.d.ts +0 -91
- package/dist/migrations/004-canvas-documents/legacy/types.js +0 -0
- package/dist/migrations/004-canvas-documents/legacy-edges.d.ts +0 -6
- package/dist/migrations/004-canvas-documents/legacy-edges.js +0 -18
- package/dist/migrations/004-canvas-documents/plan.d.ts +0 -102
- package/dist/migrations/004-canvas-documents/plan.js +0 -3
- package/dist/migrations/004-canvas-documents/pointers.d.ts +0 -23
- package/dist/migrations/004-canvas-documents/pointers.js +0 -4
- package/dist/migrations/activation.d.ts +0 -17
- package/dist/migrations/activation.js +0 -2
- package/packages/crouter-api/dist/commands/sys/migrate.js +0 -79
- package/packages/crouter-api/dist/core/layout-migrate/index.js +0 -523
- package/packages/crouter-api/dist/core/layout-migrate/paths.js +0 -118
- package/packages/crouter-api/dist/core/layout-migrate/stored-paths.js +0 -424
- package/packages/crouter-api/dist/core/layout-migrate/stores.js +0 -543
- package/packages/crouter-api/dist/core/locked-file.js +0 -121
- package/packages/crouter-api/dist/core/scoped-state/db.js +0 -271
- package/packages/crouter-api/dist/core/scoped-state/legacy-profiles.js +0 -65
- package/packages/crouter-api/dist/core/scoped-state/migrate.js +0 -332
- package/packages/crouter-api/dist/core/scoped-state/schema.js +0 -52
- package/packages/crouter-api/dist/migrations/004-canvas-documents/apply.js +0 -113
- package/packages/crouter-api/dist/migrations/004-canvas-documents/index.js +0 -89
- package/packages/crouter-api/dist/migrations/004-canvas-documents/legacy/discover.js +0 -251
- package/packages/crouter-api/dist/migrations/004-canvas-documents/legacy/history.js +0 -37
- package/packages/crouter-api/dist/migrations/004-canvas-documents/legacy/nested.js +0 -37
- package/packages/crouter-api/dist/migrations/004-canvas-documents/legacy/parse.js +0 -24
- package/packages/crouter-api/dist/migrations/004-canvas-documents/legacy/types.js +0 -3
- package/packages/crouter-api/dist/migrations/004-canvas-documents/plan.js +0 -410
- package/packages/crouter-api/dist/migrations/004-canvas-documents/pointers.js +0 -95
- package/packages/crouter-api/dist/migrations/activation.js +0 -46
|
@@ -1,1073 +0,0 @@
|
|
|
1
|
-
var ae=Object.defineProperty;var E=(e,t)=>ae(e,"name",{value:t,configurable:!0});import{randomBytes as Y}from"node:crypto";import{parseScope as de}from"@north-light/identity";import{importNodeSqlAuthority as _e,recreateNodeRowsView as Ne}from"./node-sql-migration.js";import{addFaultNotifiedSince as ce,migrateRecoveryWitnesses as Le}from"./recovery.js";import{migrateMessagingState as le}from"./deliveries.js";import{migrateHumanRequestsState as ue}from"../human/requests.js";import{existsSync as U,readFileSync as k,readdirSync as j,realpathSync as pe,statSync as Re}from"node:fs";import{basename as Ae,join as H,resolve as K}from"node:path";import{appSpace as Oe}from"../layout.js";import{emitEvent as Ie}from"../events/emit.js";import{isSafeNodeId as me,nodesRoot as x,nodeMetaPath as F}from"./paths.js";import{LEGACY_LOCAL_PERSON as g}from"./legacy-person.js";import{convertLegacyEdges as fe}from"../../migrations/004-canvas-documents/legacy-edges.js";import{customExtensionPaths as Xe}from"./extensions.js";import{readMetaObject as q,updateJsonFileDurably as $,withMetaLock as Ue,writeMetaObjectLocked as Ce}from"./meta-file.js";function Se(e){e.exec(`
|
|
2
|
-
CREATE TABLE IF NOT EXISTS nodes (
|
|
3
|
-
node_id TEXT PRIMARY KEY,
|
|
4
|
-
name TEXT NOT NULL,
|
|
5
|
-
kind TEXT NOT NULL,
|
|
6
|
-
mode TEXT NOT NULL DEFAULT 'base',
|
|
7
|
-
lifecycle TEXT NOT NULL DEFAULT 'terminal',
|
|
8
|
-
status TEXT NOT NULL DEFAULT 'active',
|
|
9
|
-
cwd TEXT NOT NULL,
|
|
10
|
-
parent TEXT,
|
|
11
|
-
created TEXT NOT NULL
|
|
12
|
-
);
|
|
13
|
-
|
|
14
|
-
CREATE TABLE IF NOT EXISTS edges (
|
|
15
|
-
type TEXT NOT NULL, -- 'subscribes_to' | 'spawned_by'
|
|
16
|
-
from_id TEXT NOT NULL,
|
|
17
|
-
to_id TEXT NOT NULL,
|
|
18
|
-
active INTEGER NOT NULL DEFAULT 1,
|
|
19
|
-
created TEXT NOT NULL,
|
|
20
|
-
PRIMARY KEY (type, from_id, to_id)
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
CREATE INDEX IF NOT EXISTS idx_edges_to ON edges(type, to_id);
|
|
24
|
-
CREATE INDEX IF NOT EXISTS idx_edges_from ON edges(type, from_id);
|
|
25
|
-
CREATE INDEX IF NOT EXISTS idx_nodes_status ON nodes(status);
|
|
26
|
-
`)}E(Se,"baselineSchema");function B(e,t){return new Set(e.prepare(`PRAGMA table_info(${t})`).all().map(n=>n.name))}E(B,"tableColumns");function J(e,t,n,o){t.has(n)||(e.exec(o),t.add(n))}E(J,"addColumnIfMissing");function O(e){return B(e,"nodes")}E(O,"nodeColumns");const c=J;function De(e){const t=O(e);c(e,t,"intent","ALTER TABLE nodes ADD COLUMN intent TEXT;"),c(e,t,"pi_pid","ALTER TABLE nodes ADD COLUMN pi_pid INTEGER;"),c(e,t,"window","ALTER TABLE nodes ADD COLUMN window TEXT;"),c(e,t,"tmux_session","ALTER TABLE nodes ADD COLUMN tmux_session TEXT;")}E(De,"addRuntimeColumns");function ge(e){const t=x();if(!U(t))return;const n=e.prepare('UPDATE nodes SET intent = ?, pi_pid = ?, "window" = ?, tmux_session = ? WHERE node_id = ?');for(const o of j(t)){const T=F(o);if(!U(T))continue;let i;try{i=JSON.parse(k(T,"utf8"))}catch{continue}n.run(i.intent??null,i.pi_pid??null,i.window??null,i.tmux_session??null,o)}}E(ge,"backfillRuntime");function we(e){W(e,3,()=>{if(!(e.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'edges'").get()!==void 0)){e.exec(`
|
|
27
|
-
CREATE TABLE edges (
|
|
28
|
-
type TEXT NOT NULL,
|
|
29
|
-
from_id TEXT NOT NULL,
|
|
30
|
-
to_id TEXT NOT NULL,
|
|
31
|
-
active INTEGER NOT NULL DEFAULT 1,
|
|
32
|
-
created TEXT NOT NULL,
|
|
33
|
-
PRIMARY KEY (type, from_id, to_id),
|
|
34
|
-
FOREIGN KEY (from_id) REFERENCES nodes(node_id) ON DELETE CASCADE,
|
|
35
|
-
FOREIGN KEY (to_id) REFERENCES nodes(node_id) ON DELETE CASCADE
|
|
36
|
-
);
|
|
37
|
-
CREATE INDEX IF NOT EXISTS idx_edges_to ON edges(type, to_id);
|
|
38
|
-
CREATE INDEX IF NOT EXISTS idx_edges_from ON edges(type, from_id);
|
|
39
|
-
`);return}const n=e.prepare(`SELECT COUNT(*) AS n FROM edges
|
|
40
|
-
WHERE from_id IN (SELECT node_id FROM nodes)
|
|
41
|
-
AND to_id IN (SELECT node_id FROM nodes)`).get().n;e.exec(`
|
|
42
|
-
CREATE TABLE edges_new (
|
|
43
|
-
type TEXT NOT NULL,
|
|
44
|
-
from_id TEXT NOT NULL,
|
|
45
|
-
to_id TEXT NOT NULL,
|
|
46
|
-
active INTEGER NOT NULL DEFAULT 1,
|
|
47
|
-
created TEXT NOT NULL,
|
|
48
|
-
PRIMARY KEY (type, from_id, to_id),
|
|
49
|
-
FOREIGN KEY (from_id) REFERENCES nodes(node_id) ON DELETE CASCADE,
|
|
50
|
-
FOREIGN KEY (to_id) REFERENCES nodes(node_id) ON DELETE CASCADE
|
|
51
|
-
);
|
|
52
|
-
INSERT INTO edges_new (type, from_id, to_id, active, created)
|
|
53
|
-
SELECT type, from_id, to_id, active, created FROM edges
|
|
54
|
-
WHERE from_id IN (SELECT node_id FROM nodes)
|
|
55
|
-
AND to_id IN (SELECT node_id FROM nodes);
|
|
56
|
-
DROP TABLE edges;
|
|
57
|
-
ALTER TABLE edges_new RENAME TO edges;
|
|
58
|
-
CREATE INDEX IF NOT EXISTS idx_edges_to ON edges(type, to_id);
|
|
59
|
-
CREATE INDEX IF NOT EXISTS idx_edges_from ON edges(type, from_id);
|
|
60
|
-
`);const o=e.prepare("SELECT COUNT(*) AS n FROM edges").get().n;if(o!==n)throw new Error(`edges FK rebuild lost rows: expected ${n} non-orphan edge(s), got ${o}`);const T=e.prepare("PRAGMA foreign_key_check").all();if(T.length>0)throw new Error(`edges FK rebuild left ${T.length} foreign-key violation(s)`)})}E(we,"edgesForeignKeyCascade");function ye(e){const t=O(e);c(e,t,"pane","ALTER TABLE nodes ADD COLUMN pane TEXT;")}E(ye,"addPaneColumn");function ve(e){e.exec(`
|
|
61
|
-
CREATE TABLE IF NOT EXISTS focuses (
|
|
62
|
-
focus_id TEXT PRIMARY KEY, -- stable internal id for the viewport
|
|
63
|
-
pane TEXT, -- the durable %pane_id realizing the focus
|
|
64
|
-
session TEXT, -- derived cache of the user session (reconciled from pane)
|
|
65
|
-
node_id TEXT NOT NULL UNIQUE -- the node shown; UNIQUE \u2192 a node occupies <=1 focus
|
|
66
|
-
);
|
|
67
|
-
`)}E(ve,"addFocusesTable");function he(e){e.exec(`
|
|
68
|
-
CREATE TABLE IF NOT EXISTS wakeups (
|
|
69
|
-
wakeup_id TEXT PRIMARY KEY,
|
|
70
|
-
node_id TEXT, -- TARGET + node-anchored cancel anchor; NULL = canvas-detached
|
|
71
|
-
owner_id TEXT NOT NULL, -- ARMER; set on EVERY row. PLAIN column, NO FK (design D1/D2)
|
|
72
|
-
fire_at TEXT NOT NULL, -- ISO 8601 UTC; NEXT occurrence; lexicographic <=
|
|
73
|
-
kind TEXT NOT NULL, -- 'bare' | 'noted' | 'deadline' | 'spawn'
|
|
74
|
-
recur TEXT, -- NULL = one-shot; else the pinned recur JSON
|
|
75
|
-
payload TEXT, -- JSON per kind (shared contracts)
|
|
76
|
-
created TEXT NOT NULL,
|
|
77
|
-
FOREIGN KEY (node_id) REFERENCES nodes(node_id) ON DELETE CASCADE
|
|
78
|
-
);
|
|
79
|
-
CREATE INDEX IF NOT EXISTS idx_wakeups_fire ON wakeups(fire_at); -- per-tick due query
|
|
80
|
-
CREATE INDEX IF NOT EXISTS idx_wakeups_node ON wakeups(node_id); -- by-node cancel / list
|
|
81
|
-
CREATE INDEX IF NOT EXISTS idx_wakeups_owner ON wakeups(owner_id); -- by-owner reap (ruling A)
|
|
82
|
-
CREATE UNIQUE INDEX IF NOT EXISTS idx_wakeups_deadline ON wakeups(node_id) WHERE kind = 'deadline';
|
|
83
|
-
`)}E(he,"appendWakeupsTable");function xe(e){e.exec(`
|
|
84
|
-
CREATE TABLE IF NOT EXISTS triggers (
|
|
85
|
-
trigger_id TEXT PRIMARY KEY,
|
|
86
|
-
owner_id TEXT NOT NULL,
|
|
87
|
-
node_id TEXT, -- TARGET + node-anchored cancel anchor; NULL = canvas-detached
|
|
88
|
-
action TEXT NOT NULL, -- 'msg' | 'new'
|
|
89
|
-
action_mode TEXT NOT NULL, -- 'inbox_message' | 'fresh_revive' | 'deadline_message' | 'node_birth'
|
|
90
|
-
trigger_type TEXT NOT NULL, -- 'at' | 'every' | 'when' | 'until'
|
|
91
|
-
fire_at TEXT, -- clock triggers: NEXT occurrence, ISO 8601 UTC; lexicographic <=
|
|
92
|
-
recur TEXT, -- NULL = one-shot; else the recur JSON the daemon advances
|
|
93
|
-
payload TEXT, -- JSON action payload, discriminated by action_mode
|
|
94
|
-
when_script TEXT, -- predicate triggers: the --when bash source
|
|
95
|
-
eval_every TEXT, -- predicate triggers: evaluation cadence (recur-shaped JSON)
|
|
96
|
-
next_eval_at TEXT, -- predicate triggers: next evaluation instant
|
|
97
|
-
timeout_at TEXT, -- predicate triggers: wall-clock expiry
|
|
98
|
-
eval_state TEXT NOT NULL DEFAULT 'idle', -- 'idle' | 'running'
|
|
99
|
-
eval_started_at TEXT,
|
|
100
|
-
eval_pid INTEGER,
|
|
101
|
-
eval_lease_owner TEXT,
|
|
102
|
-
last_eval_at TEXT,
|
|
103
|
-
last_exit INTEGER,
|
|
104
|
-
last_stderr TEXT,
|
|
105
|
-
created TEXT NOT NULL,
|
|
106
|
-
FOREIGN KEY (node_id) REFERENCES nodes(node_id) ON DELETE CASCADE
|
|
107
|
-
);
|
|
108
|
-
CREATE INDEX IF NOT EXISTS idx_triggers_fire ON triggers(fire_at); -- clock per-tick due query
|
|
109
|
-
CREATE INDEX IF NOT EXISTS idx_triggers_node ON triggers(node_id); -- by-node cancel / list
|
|
110
|
-
CREATE INDEX IF NOT EXISTS idx_triggers_owner ON triggers(owner_id); -- by-owner reap
|
|
111
|
-
CREATE INDEX IF NOT EXISTS idx_triggers_eval ON triggers(eval_state, next_eval_at); -- predicate due query
|
|
112
|
-
CREATE UNIQUE INDEX IF NOT EXISTS idx_triggers_deadline ON triggers(node_id) WHERE action_mode = 'deadline_message';
|
|
113
|
-
|
|
114
|
-
INSERT INTO triggers (trigger_id, owner_id, node_id, action, action_mode, trigger_type, fire_at, recur, payload, created)
|
|
115
|
-
SELECT
|
|
116
|
-
wakeup_id,
|
|
117
|
-
owner_id,
|
|
118
|
-
node_id,
|
|
119
|
-
CASE kind WHEN 'spawn' THEN 'new' ELSE 'msg' END,
|
|
120
|
-
CASE kind
|
|
121
|
-
WHEN 'bare' THEN 'fresh_revive'
|
|
122
|
-
WHEN 'noted' THEN 'inbox_message'
|
|
123
|
-
WHEN 'deadline' THEN 'deadline_message'
|
|
124
|
-
WHEN 'spawn' THEN 'node_birth'
|
|
125
|
-
END,
|
|
126
|
-
CASE WHEN kind = 'deadline' THEN 'until' WHEN recur IS NULL THEN 'at' ELSE 'every' END,
|
|
127
|
-
fire_at,
|
|
128
|
-
recur,
|
|
129
|
-
payload,
|
|
130
|
-
created
|
|
131
|
-
FROM wakeups;
|
|
132
|
-
|
|
133
|
-
DROP TABLE wakeups;
|
|
134
|
-
`)}E(xe,"migrateWakeupsToTriggers");function Fe(e){const t=O(e);c(e,t,"host_kind","ALTER TABLE nodes ADD COLUMN host_kind TEXT;")}E(Fe,"addHostKindColumn");function Me(e){e.exec(`
|
|
135
|
-
CREATE TABLE IF NOT EXISTS canvas_meta (
|
|
136
|
-
key TEXT PRIMARY KEY,
|
|
137
|
-
value TEXT NOT NULL
|
|
138
|
-
);
|
|
139
|
-
`)}E(Me,"addCanvasMetaTable");function ke(e){const t=O(e);c(e,t,"profile_id","ALTER TABLE nodes ADD COLUMN profile_id TEXT;")}E(ke,"addProfileIdColumn");function je(e){const t=O(e);c(e,t,"pi_pid_identity","ALTER TABLE nodes ADD COLUMN pi_pid_identity TEXT;")}E(je,"addPidIdentityColumn");function He(e){const t=O(e);c(e,t,"waiting_for","ALTER TABLE nodes ADD COLUMN waiting_for TEXT;")}E(He,"addMessageWaitColumn");function Q(e){const t=O(e);if(t.size===0)return;c(e,t,"final_report","ALTER TABLE nodes ADD COLUMN final_report TEXT;"),c(e,t,"finalized_at","ALTER TABLE nodes ADD COLUMN finalized_at TEXT;");const n=e.prepare("SELECT node_id FROM nodes WHERE status = 'done' AND final_report IS NULL").all(),o=e.prepare("UPDATE nodes SET final_report = ?, finalized_at = ? WHERE node_id = ? AND final_report IS NULL");for(const{node_id:T}of n){const i=H(x(),T,"reports");if(!U(i))continue;let a=null;for(const N of j(i))if(N.endsWith(".md"))try{const L=k(H(i,N),"utf8"),l=/^---\nnode: [^\n]+\nkind: final\nts: ([^\n]+)\n---\n/.exec(L);if(l===null||Number.isNaN(Date.parse(l[1])))continue;const r={name:N,ts:l[1]};(a===null||r.ts>a.ts||r.ts===a.ts&&r.name>a.name)&&(a=r)}catch{}a!==null&&o.run(a.name,a.ts,T)}}E(Q,"addCanonicalFinalColumns");function Ge(e){const t=x();if(U(t))for(const n of j(t)){const o=F(n);U(o)&&$(o,T=>{const i=T.launch;if(typeof i!="object"||i===null||Array.isArray(i))return!1;const a=i.extensions;if(!Array.isArray(a)||!a.every(L=>typeof L=="string"))return!1;const N=Xe(a);return N.length===a.length?!1:(i.extensions=N,!0)})}}E(Ge,"normalizePersistedLaunchExtensions");function be(e){const t=O(e);c(e,t,"waiting_for_generation","ALTER TABLE nodes ADD COLUMN waiting_for_generation INTEGER NOT NULL DEFAULT 0;")}E(be,"addMessageWaitGenerationColumn");function Be(e){e.exec(`
|
|
140
|
-
CREATE TABLE IF NOT EXISTS consult_outbox (
|
|
141
|
-
node_id TEXT PRIMARY KEY, -- legacy work node id
|
|
142
|
-
dir TEXT NOT NULL, -- legacy ticket directory
|
|
143
|
-
root TEXT NOT NULL, -- legacy registered root
|
|
144
|
-
request_id TEXT NOT NULL, -- legacy request id
|
|
145
|
-
status TEXT NOT NULL, -- armed | pending | acked | errored | canceled
|
|
146
|
-
final_report TEXT, -- legacy final report basename
|
|
147
|
-
updated_at TEXT NOT NULL
|
|
148
|
-
);
|
|
149
|
-
CREATE INDEX IF NOT EXISTS idx_consult_outbox_status ON consult_outbox(status);
|
|
150
|
-
CREATE INDEX IF NOT EXISTS idx_consult_outbox_dir ON consult_outbox(dir);
|
|
151
|
-
CREATE INDEX IF NOT EXISTS idx_consult_outbox_dir_request_status ON consult_outbox(dir, request_id, status);
|
|
152
|
-
CREATE INDEX IF NOT EXISTS idx_consult_outbox_dir_status ON consult_outbox(dir, status);
|
|
153
|
-
`)}E(Be,"addHumanWorkOutboxTable");function We(e){const t=B(e,"consult_outbox"),n=E((o,T)=>J(e,t,o,T),"add");n("work_kind","ALTER TABLE consult_outbox ADD COLUMN work_kind TEXT NOT NULL DEFAULT 'follow_up';"),n("interaction_id","ALTER TABLE consult_outbox ADD COLUMN interaction_id TEXT;"),n("generation_id","ALTER TABLE consult_outbox ADD COLUMN generation_id TEXT;"),n("interaction_snapshot","ALTER TABLE consult_outbox ADD COLUMN interaction_snapshot TEXT;"),n("handler_command","ALTER TABLE consult_outbox ADD COLUMN handler_command TEXT;"),n("handler_args_json","ALTER TABLE consult_outbox ADD COLUMN handler_args_json TEXT;"),n("claim_token","ALTER TABLE consult_outbox ADD COLUMN claim_token TEXT;"),e.exec(`
|
|
154
|
-
CREATE INDEX IF NOT EXISTS idx_consult_outbox_human_work_status ON consult_outbox(work_kind, status);
|
|
155
|
-
CREATE INDEX IF NOT EXISTS idx_consult_outbox_human_work_dir_status ON consult_outbox(dir, work_kind, status);
|
|
156
|
-
CREATE INDEX IF NOT EXISTS idx_consult_outbox_human_work_request_status ON consult_outbox(dir, request_id, work_kind, status);
|
|
157
|
-
CREATE UNIQUE INDEX IF NOT EXISTS idx_consult_outbox_visual_request ON consult_outbox(dir, request_id, work_kind) WHERE work_kind = 'visual';
|
|
158
|
-
`)}E(We,"addHumanWorkColumns");function Pe(e){e.exec(`
|
|
159
|
-
CREATE INDEX IF NOT EXISTS idx_nodes_parent ON nodes(parent);
|
|
160
|
-
CREATE INDEX IF NOT EXISTS idx_nodes_created ON nodes(created);
|
|
161
|
-
`)}E(Pe,"addNodeParentCreatedIndexes");function Ye(e){e.exec(`
|
|
162
|
-
CREATE TABLE IF NOT EXISTS crons (
|
|
163
|
-
cron_id TEXT PRIMARY KEY,
|
|
164
|
-
name TEXT NOT NULL, -- agent-legible label; how it shows in list
|
|
165
|
-
created_by TEXT, -- provenance only, NO FK, never cascades
|
|
166
|
-
command TEXT NOT NULL, -- the bash
|
|
167
|
-
|
|
168
|
-
fire_at TEXT NOT NULL, -- next occurrence, ISO 8601 UTC; lexicographic <=
|
|
169
|
-
recur TEXT, -- NULL = one-shot; else recur JSON the daemon advances
|
|
170
|
-
tz TEXT, -- IANA zone for calendar cadence
|
|
171
|
-
expires_at TEXT, -- clock bound; row is deleted when passed
|
|
172
|
-
|
|
173
|
-
anchor_node TEXT, -- opt-in node coupling; NULL = outlives every node
|
|
174
|
-
cancel_on_wake INTEGER NOT NULL DEFAULT 0, -- deleted when anchor revives (deadline rule)
|
|
175
|
-
|
|
176
|
-
cwd TEXT NOT NULL, -- snapshotted at creation
|
|
177
|
-
env_json TEXT,
|
|
178
|
-
profile TEXT,
|
|
179
|
-
scope TEXT NOT NULL DEFAULT 'profile', -- 'profile' | 'global'
|
|
180
|
-
|
|
181
|
-
run_timeout_s INTEGER NOT NULL DEFAULT 300,
|
|
182
|
-
overlap TEXT NOT NULL DEFAULT 'skip', -- 'skip' | 'queue' | 'replace'
|
|
183
|
-
on_output TEXT NOT NULL DEFAULT 'on-failure',-- 'silent'|'on-failure'|'always'|'on-change'
|
|
184
|
-
sink TEXT NOT NULL, -- JSON: {node}|{spawn,kind,...}|{human}
|
|
185
|
-
tier TEXT NOT NULL DEFAULT 'normal',
|
|
186
|
-
last_output_hash TEXT, -- drives 'on-change'
|
|
187
|
-
|
|
188
|
-
state TEXT NOT NULL DEFAULT 'active', -- 'active' | 'paused'
|
|
189
|
-
run_state TEXT NOT NULL DEFAULT 'idle', -- 'idle' | 'running'
|
|
190
|
-
run_pid INTEGER,
|
|
191
|
-
run_pid_identity TEXT, -- v23: launch-time pid fingerprint; a kill requires a MATCH
|
|
192
|
-
run_started_at TEXT,
|
|
193
|
-
run_lease_owner TEXT,
|
|
194
|
-
run_out_of_band INTEGER NOT NULL DEFAULT 0, -- v24: this lease is a manual cron run, not a scheduled fire
|
|
195
|
-
|
|
196
|
-
created TEXT NOT NULL,
|
|
197
|
-
updated TEXT NOT NULL,
|
|
198
|
-
FOREIGN KEY (anchor_node) REFERENCES nodes(node_id) ON DELETE CASCADE
|
|
199
|
-
);
|
|
200
|
-
CREATE UNIQUE INDEX IF NOT EXISTS idx_crons_deadline ON crons(anchor_node) WHERE cancel_on_wake = 1;
|
|
201
|
-
CREATE INDEX IF NOT EXISTS idx_crons_fire ON crons(fire_at);
|
|
202
|
-
|
|
203
|
-
CREATE TABLE IF NOT EXISTS cron_runs ( -- bounded ring, last 20 per cron
|
|
204
|
-
run_id TEXT PRIMARY KEY,
|
|
205
|
-
cron_id TEXT NOT NULL,
|
|
206
|
-
started TEXT NOT NULL,
|
|
207
|
-
finished TEXT,
|
|
208
|
-
duration_ms INTEGER,
|
|
209
|
-
exit_code INTEGER, -- NULL while running; -1 for timeout kill
|
|
210
|
-
stdout_head TEXT, -- capped at 4KB
|
|
211
|
-
stderr_head TEXT,
|
|
212
|
-
delivered TEXT, -- what the sink did, or why it didn't
|
|
213
|
-
FOREIGN KEY (cron_id) REFERENCES crons(cron_id) ON DELETE CASCADE
|
|
214
|
-
);
|
|
215
|
-
CREATE INDEX IF NOT EXISTS idx_cron_runs_cron ON cron_runs(cron_id, started);
|
|
216
|
-
`)}E(Ye,"addCronsTables");function w(e){return`'${e.replace(/'/g,`'"'"'`)}'`}E(w,"shellSingleQuote");function Ke(e,t){let n=0;for(;;){const o=`${t}_${n}`;if(!e.split(`
|
|
217
|
-
`).includes(o))return o;n++}}E(Ke,"uniqueHeredocDelimiter");function z(e,t,n){const o=Ke(t,n);return`${e} <<'${o}'
|
|
218
|
-
${t}
|
|
219
|
-
${o}`}E(z,"heredocCommand");function qe(e){if(typeof e!="string")return{};try{const t=JSON.parse(e);return typeof t!="object"||t===null||Array.isArray(t)?{}:t}catch{return{}}}E(qe,"migrationPayload");function X(e,t,n){const o=e[t];if(typeof o!="string"||o.trim()==="")throw new Error(`trigger ${n} payload requires non-empty ${t}`);return o}E(X,"requiredString");function G(e,t){const n=e[t];return typeof n=="string"&&n.trim()!==""?n:void 0}E(G,"optionalString");function y(e,t){const n=e[t];return typeof n=="string"&&n.trim()!==""?n:void 0}E(y,"optionalColumn");function V(e,t,n,o){return z(`crtr node message send --to ${w(e)} --tier ${w(t)}`,n,`CRTR_MIGRATED_${o.replace(/[^A-Za-z0-9]/g,"_")}`)}E(V,"migratedMessageCommand");function $e(e,t){return`printf '%s\\n' "$CRTR_GATE_STDOUT" | crtr node message send --to ${w(e)} --tier ${w(t)}`}E($e,"migratedGateStdoutMessageCommand");function Je(e,t){const n=[`crtr node new --kind ${w(X(e,"kind",t))}`],o=[["mode","mode"],["cwd","cwd"],["name","name"],["parent","parent"],["worktreeBase","worktree"],["forkFrom","fork-from"],["model","model"],["profile","profile"],["situationalContext","situational-context"]];for(const[i,a]of o){const N=G(e,i);N!==void 0&&n.push(`--${a} ${w(N)}`)}e.root===!0&&n.push("--root");const T=X(e,"prompt",t);return z(n.join(" "),T,`CRTR_MIGRATED_${t.replace(/[^A-Za-z0-9]/g,"_")}`)}E(Je,"migratedSpawnCommand");const Z="migration.v22.skipped";function Qe(e){const t=e.prepare("SELECT cwd FROM nodes WHERE parent IS NULL ORDER BY created LIMIT 1").get();return(t!==void 0?y(t,"cwd"):void 0)??process.cwd()}E(Qe,"ambientMigrationCwd");function ze(e){let t;try{t=e.prepare("SELECT eval_pid FROM triggers WHERE eval_state = 'running' AND eval_pid IS NOT NULL").all()}catch{return}for(const n of t){const o=n.eval_pid;if(!(typeof o!="number"||!Number.isInteger(o)||o<=1))try{process.kill(-o,"SIGTERM")}catch{}}}E(ze,"reapRunningPredicateEvaluations");function Ve(e){const t=[],n=e.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'triggers'").get()!==void 0;n&&ze(e);let o=[];if(n)try{o=e.prepare("SELECT * FROM triggers ORDER BY created, trigger_id").all()}catch(a){t.push({trigger_id:"<unreadable table>",reason:`triggers table could not be read: ${a instanceof Error?a.message:String(a)}`})}const T=Qe(e),i=e.prepare(`
|
|
220
|
-
INSERT OR REPLACE INTO crons (
|
|
221
|
-
cron_id, name, created_by, command, fire_at, recur, tz, expires_at,
|
|
222
|
-
anchor_node, cancel_on_wake, cwd, env_json, profile, scope,
|
|
223
|
-
run_timeout_s, overlap, on_output, sink, tier, created, updated
|
|
224
|
-
) VALUES (?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, NULL, ?, 'profile', ?, 'skip', 'on-failure', '{}', ?, ?, ?)`);for(const a of o){const N=y(a,"trigger_id");if(N===void 0){t.push({trigger_id:"<unidentified row>",reason:"row carries no trigger_id"});continue}try{Ze(e,i,a,N,T)}catch(L){t.push({trigger_id:N,reason:L instanceof Error?L.message:String(L)})}}t.length>0&&(e.prepare("INSERT INTO canvas_meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value").run(Z,JSON.stringify(t)),console.warn(`[canvas migration v22] ${t.length} retired schedule(s) could not be converted to crons and were dropped; see canvas_meta['${Z}']: `+t.map(a=>`${a.trigger_id} (${a.reason})`).join("; "))),e.exec(`
|
|
225
|
-
DROP INDEX IF EXISTS idx_triggers_fire;
|
|
226
|
-
DROP INDEX IF EXISTS idx_triggers_node;
|
|
227
|
-
DROP INDEX IF EXISTS idx_triggers_owner;
|
|
228
|
-
DROP INDEX IF EXISTS idx_triggers_eval;
|
|
229
|
-
DROP INDEX IF EXISTS idx_triggers_deadline;
|
|
230
|
-
DROP TABLE IF EXISTS triggers;
|
|
231
|
-
`)}E(Ve,"migrateTriggersToCrons");function Ze(e,t,n,o,T){const i=X(n,"owner_id",o),a=e.prepare("SELECT cwd, profile_id FROM nodes WHERE node_id = ?").get(i),N=(a===void 0?void 0:y(a,"cwd"))??T,L=a===void 0?null:y(a,"profile_id")??null,l=y(n,"node_id")??null,r=X(n,"action",o),_=X(n,"action_mode",o),d=X(n,"trigger_type",o),u=qe(n.payload),p=y(n,"created")??new Date().toISOString(),I=_==="deadline_message"?"urgent":G(u,"tier")??"normal",R=G(u,"label");let s,A,m=!1;if(r==="msg"&&(_==="inbox_message"||_==="deadline_message")){if(l===null)throw new Error("has no message target");const f=G(u,"body");f!==void 0?s=V(l,I,f,o):d==="when"?(m=!0,s=$e(l,I)):s=V(l,I,R??"Scheduled message (no body was recorded when this schedule was armed).",o),A=R??(_==="deadline_message"?"deadline":"scheduled message")}else if(r==="msg"&&_==="fresh_revive"){if(l===null)throw new Error("has no revive target");s=`crtr node lifecycle revive ${w(l)} --fresh`,A=R??"scheduled fresh revive"}else if(r==="new"&&_==="node_birth")s=Je(u,o),A=G(u,"name")??`scheduled ${X(u,"kind",o)} node`;else throw new Error(`has unsupported action ${r}/${_}`);let D,v,b=null;if(d==="when"){D=X(n,"next_eval_at",o),v=X(n,"eval_every",o),b=y(n,"timeout_at")??null;const f=X(n,"when_script",o);s=`${m?`if ! CRTR_GATE_STDOUT="$(
|
|
232
|
-
${f}
|
|
233
|
-
)"; then`:`if ! (
|
|
234
|
-
${f}
|
|
235
|
-
); then`}
|
|
236
|
-
exit 0
|
|
237
|
-
fi
|
|
238
|
-
{
|
|
239
|
-
${s}
|
|
240
|
-
} && crtr cron cancel "$CRTR_CRON_ID"`}else if(d==="at"||d==="every"||d==="until")D=X(n,"fire_at",o),v=d==="every"?X(n,"recur",o):null;else throw new Error(`has unknown timing type ${d}`);if(_==="deadline_message"&&l!==null){const f=e.prepare("SELECT cron_id FROM crons WHERE anchor_node = ? AND cancel_on_wake = 1 AND cron_id <> ?").get(l,o);if(f!==void 0)throw new Error(`node ${l} already has a migrated deadline (${String(f.cron_id)}); only one is allowed`)}t.run(o,A,i,s,D,v,b,l,_==="deadline_message"?1:0,N,L,300,I,p,p)}E(Ze,"convertTriggerRow");function et(e){const t=B(e,"crons");t.size===0||t.has("run_pid_identity")||e.exec("ALTER TABLE crons ADD COLUMN run_pid_identity TEXT;")}E(et,"addCronRunPidIdentityColumn");function tt(e){const t=B(e,"crons");t.size!==0&&(t.has("run_out_of_band")||e.exec("ALTER TABLE crons ADD COLUMN run_out_of_band INTEGER NOT NULL DEFAULT 0;"),t.has("queued")&&e.exec("ALTER TABLE crons DROP COLUMN queued;"))}E(tt,"addCronOutOfBandColumn");function nt(e){e.exec(`
|
|
241
|
-
CREATE TABLE IF NOT EXISTS warm_pool (
|
|
242
|
-
node_id TEXT PRIMARY KEY REFERENCES nodes(node_id) ON DELETE CASCADE,
|
|
243
|
-
recipe_key TEXT NOT NULL, -- frozen launch tuple this spare was minted for
|
|
244
|
-
created TEXT NOT NULL -- ISO 8601, mint time (oldest spare claimed first)
|
|
245
|
-
);
|
|
246
|
-
CREATE INDEX IF NOT EXISTS idx_warm_pool_recipe ON warm_pool(recipe_key, created);
|
|
247
|
-
`)}E(nt,"addWarmPoolTable");function Et(e){const t=e.prepare("SELECT node_id, dir, root, request_id, work_kind, status, final_report, updated_at FROM consult_outbox").all();e.exec(`
|
|
248
|
-
CREATE TABLE consult_outbox_new (
|
|
249
|
-
node_id TEXT PRIMARY KEY,
|
|
250
|
-
ticket_node_id TEXT NOT NULL,
|
|
251
|
-
request_id TEXT NOT NULL,
|
|
252
|
-
work_kind TEXT NOT NULL DEFAULT 'follow_up',
|
|
253
|
-
status TEXT NOT NULL,
|
|
254
|
-
final_report TEXT,
|
|
255
|
-
updated_at TEXT NOT NULL
|
|
256
|
-
);
|
|
257
|
-
`);const n=e.prepare(`
|
|
258
|
-
INSERT INTO consult_outbox_new (node_id, ticket_node_id, request_id, work_kind, status, final_report, updated_at)
|
|
259
|
-
VALUES (?, ?, ?, ?, ?, ?, ?)`);let o=0;for(const T of t){const i=Ae(T.dir);if(!(T.root===K(T.root)&&T.dir===H(T.root,i))||!me(i)){o+=1;continue}n.run(T.node_id,i,T.request_id,T.work_kind,T.status,T.final_report,T.updated_at)}o>0&&console.warn(`[canvas migration v26] dropped ${o} consult_outbox row(s) whose legacy ticket path cannot safely map to a ticket node id`),e.exec(`
|
|
260
|
-
DROP TABLE consult_outbox;
|
|
261
|
-
ALTER TABLE consult_outbox_new RENAME TO consult_outbox;
|
|
262
|
-
|
|
263
|
-
CREATE INDEX IF NOT EXISTS idx_consult_outbox_status ON consult_outbox(status);
|
|
264
|
-
CREATE INDEX IF NOT EXISTS idx_consult_outbox_human_work_status ON consult_outbox(work_kind, status);
|
|
265
|
-
CREATE INDEX IF NOT EXISTS idx_consult_outbox_ticket_status ON consult_outbox(ticket_node_id, work_kind, status);
|
|
266
|
-
CREATE INDEX IF NOT EXISTS idx_consult_outbox_ticket_request_status ON consult_outbox(ticket_node_id, request_id, work_kind, status);
|
|
267
|
-
CREATE UNIQUE INDEX IF NOT EXISTS idx_consult_outbox_visual_request ON consult_outbox(ticket_node_id, request_id, work_kind) WHERE work_kind = 'visual';
|
|
268
|
-
`)}E(Et,"narrowHumanWorkOutbox");function ot(e){e.exec(`
|
|
269
|
-
CREATE TABLE IF NOT EXISTS reviews (
|
|
270
|
-
review_id TEXT PRIMARY KEY,
|
|
271
|
-
idempotency_key TEXT NOT NULL UNIQUE,
|
|
272
|
-
origin_kind TEXT NOT NULL CHECK (origin_kind IN ('ticket', 'inline')),
|
|
273
|
-
origin_node_id TEXT NOT NULL,
|
|
274
|
-
bridge_node_id TEXT,
|
|
275
|
-
companion_node_id TEXT NOT NULL UNIQUE,
|
|
276
|
-
review_dir TEXT NOT NULL,
|
|
277
|
-
file TEXT NOT NULL,
|
|
278
|
-
origin_session_file TEXT NOT NULL,
|
|
279
|
-
origin_session_id TEXT NOT NULL,
|
|
280
|
-
origin_leaf_id TEXT NOT NULL,
|
|
281
|
-
model_spec TEXT NOT NULL,
|
|
282
|
-
branch_file TEXT NOT NULL,
|
|
283
|
-
baseline_sha256 TEXT NOT NULL,
|
|
284
|
-
baseline_bytes INTEGER NOT NULL,
|
|
285
|
-
anchor_base_sha256 TEXT NOT NULL,
|
|
286
|
-
anchor_base_bytes BLOB NOT NULL,
|
|
287
|
-
state TEXT NOT NULL CHECK (state IN ('binding', 'open', 'approved', 'canceled')),
|
|
288
|
-
created TEXT NOT NULL,
|
|
289
|
-
captured_at TEXT NOT NULL,
|
|
290
|
-
opened_at TEXT,
|
|
291
|
-
output_path TEXT,
|
|
292
|
-
subtitle TEXT,
|
|
293
|
-
approved_at TEXT,
|
|
294
|
-
approved_sha256 TEXT,
|
|
295
|
-
changed INTEGER,
|
|
296
|
-
comments_total INTEGER,
|
|
297
|
-
comments_unresolved INTEGER,
|
|
298
|
-
result_json TEXT,
|
|
299
|
-
canceled_at TEXT,
|
|
300
|
-
cancel_reason TEXT,
|
|
301
|
-
cancel_actor TEXT,
|
|
302
|
-
delivery_entry_id TEXT,
|
|
303
|
-
delivered_at TEXT,
|
|
304
|
-
delivery_error TEXT,
|
|
305
|
-
projected_at TEXT,
|
|
306
|
-
projection_error TEXT
|
|
307
|
-
);
|
|
308
|
-
CREATE INDEX IF NOT EXISTS idx_reviews_state ON reviews(state);
|
|
309
|
-
CREATE INDEX IF NOT EXISTS idx_reviews_origin ON reviews(origin_node_id);
|
|
310
|
-
CREATE INDEX IF NOT EXISTS idx_reviews_file ON reviews(file);
|
|
311
|
-
CREATE INDEX IF NOT EXISTS idx_reviews_bridge ON reviews(bridge_node_id);
|
|
312
|
-
`)}E(ot,"addReviewsTable");function it(e){e.exec(`
|
|
313
|
-
CREATE TABLE IF NOT EXISTS review_comments (
|
|
314
|
-
comment_id TEXT PRIMARY KEY,
|
|
315
|
-
review_id TEXT NOT NULL,
|
|
316
|
-
status TEXT NOT NULL CHECK (status IN ('open', 'resolved', 'deleted')),
|
|
317
|
-
revision INTEGER NOT NULL,
|
|
318
|
-
text TEXT NOT NULL,
|
|
319
|
-
anchor_line INTEGER NOT NULL,
|
|
320
|
-
anchor_end_line INTEGER NOT NULL,
|
|
321
|
-
anchor_col_start INTEGER,
|
|
322
|
-
anchor_col_end INTEGER,
|
|
323
|
-
anchor_quote TEXT,
|
|
324
|
-
anchor_line_text TEXT NOT NULL,
|
|
325
|
-
anchor_doc_hash TEXT NOT NULL,
|
|
326
|
-
author_kind TEXT NOT NULL CHECK (author_kind IN ('human', 'node')),
|
|
327
|
-
author_node_id TEXT,
|
|
328
|
-
updated_by_kind TEXT NOT NULL CHECK (updated_by_kind IN ('human', 'node')),
|
|
329
|
-
updated_by_node_id TEXT,
|
|
330
|
-
created_at TEXT NOT NULL,
|
|
331
|
-
updated_at TEXT NOT NULL,
|
|
332
|
-
resolved_at TEXT,
|
|
333
|
-
deleted_at TEXT,
|
|
334
|
-
created_seq INTEGER NOT NULL
|
|
335
|
-
);
|
|
336
|
-
CREATE TABLE IF NOT EXISTS review_comment_events (
|
|
337
|
-
seq INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
338
|
-
review_id TEXT NOT NULL,
|
|
339
|
-
comment_id TEXT NOT NULL,
|
|
340
|
-
kind TEXT NOT NULL CHECK (kind IN ('created', 'edited', 'resolved', 'reopened', 'deleted', 'reanchored', 'notified', 'notify_failed')),
|
|
341
|
-
state_change INTEGER NOT NULL,
|
|
342
|
-
revision_after INTEGER,
|
|
343
|
-
actor_kind TEXT NOT NULL CHECK (actor_kind IN ('human', 'node', 'system')),
|
|
344
|
-
actor_node_id TEXT,
|
|
345
|
-
at TEXT NOT NULL,
|
|
346
|
-
operation_id TEXT NOT NULL,
|
|
347
|
-
payload TEXT
|
|
348
|
-
);
|
|
349
|
-
CREATE INDEX IF NOT EXISTS idx_review_comments_review_line_seq
|
|
350
|
-
ON review_comments(review_id, anchor_line, created_seq);
|
|
351
|
-
CREATE INDEX IF NOT EXISTS idx_review_comments_review_status
|
|
352
|
-
ON review_comments(review_id, status);
|
|
353
|
-
CREATE INDEX IF NOT EXISTS idx_review_comment_events_review_seq
|
|
354
|
-
ON review_comment_events(review_id, seq);
|
|
355
|
-
CREATE INDEX IF NOT EXISTS idx_review_comment_events_comment_seq
|
|
356
|
-
ON review_comment_events(comment_id, seq);
|
|
357
|
-
`)}E(it,"addReviewCommentTables");function st(e){e.exec(`
|
|
358
|
-
ALTER TABLE review_comments
|
|
359
|
-
ADD COLUMN anchor_state TEXT NOT NULL DEFAULT 'live' CHECK (anchor_state IN ('live', 'detached'));
|
|
360
|
-
`)}E(st,"addReviewCommentAnchorState");function rt(e){e.exec("DROP TABLE IF EXISTS consult_outbox")}E(rt,"dropConsultOutbox");function Tt(e){e.exec("ALTER TABLE reviews ADD COLUMN submit_requested_at TEXT")}E(Tt,"addReviewSubmitRequested");function at(e){e.exec(`
|
|
361
|
-
ALTER TABLE crons ADD COLUMN held INTEGER NOT NULL DEFAULT 0;
|
|
362
|
-
ALTER TABLE crons ADD COLUMN last_poke_at TEXT;
|
|
363
|
-
`)}E(at,"addCronHeldColumns");function dt(e){e.exec(`
|
|
364
|
-
CREATE TABLE human_action_deliveries (
|
|
365
|
-
request_id TEXT PRIMARY KEY,
|
|
366
|
-
state TEXT NOT NULL,
|
|
367
|
-
attempt INTEGER NOT NULL DEFAULT 0,
|
|
368
|
-
argv_json TEXT NOT NULL,
|
|
369
|
-
cwd TEXT NOT NULL,
|
|
370
|
-
completion_json TEXT NOT NULL,
|
|
371
|
-
next_attempt_at INTEGER NOT NULL,
|
|
372
|
-
claimed_at INTEGER,
|
|
373
|
-
claim_owner TEXT,
|
|
374
|
-
accepted_at INTEGER,
|
|
375
|
-
permanent_failed_at INTEGER,
|
|
376
|
-
last_failure_json TEXT,
|
|
377
|
-
created_at INTEGER NOT NULL,
|
|
378
|
-
updated_at INTEGER NOT NULL
|
|
379
|
-
);
|
|
380
|
-
CREATE INDEX idx_human_action_deliveries_due ON human_action_deliveries(state, next_attempt_at);
|
|
381
|
-
`)}E(dt,"addHumanActionDeliveries");function _t(e){const t=O(e);c(e,t,"pi_session_file","ALTER TABLE nodes ADD COLUMN pi_session_file TEXT;");for(const n of j(x(),{withFileTypes:!0}))if(n.isDirectory())try{const o=JSON.parse(k(F(n.name),"utf8"));typeof o.pi_session_file=="string"&&e.prepare("UPDATE nodes SET pi_session_file = ? WHERE node_id = ?").run(o.pi_session_file,n.name)}catch{}e.exec("CREATE INDEX IF NOT EXISTS idx_nodes_pi_session_file ON nodes(pi_session_file);")}E(_t,"addNodeSessionFileIndex");function ee(e,t){const n=E(N=>{const L=K(N);return U(L)?pe(L):L},"canonical"),o=e.prepare("UPDATE nodes SET pi_session_file = ? WHERE node_id = ?");e.exec("DROP INDEX IF EXISTS idx_nodes_pi_session_file;");const T=e.prepare("SELECT node_id FROM nodes ORDER BY created ASC, node_id ASC").all();for(const{node_id:N}of T){const L=F(N);if(q(L)===null){o.run(null,N);continue}const l=Ue(L,()=>{const r=q(L);if(r===null)throw new Error(`node ${N} session metadata changed during ownership migration`);if(typeof r.pi_session_file!="string")return null;const _=r.pi_session_file,d=n(_);return d!==_&&(r.pi_session_file=d,Ce(L,r)),d});o.run(l,N)}const i=e.prepare(`
|
|
382
|
-
SELECT pi_session_file
|
|
383
|
-
FROM nodes
|
|
384
|
-
WHERE pi_session_file IS NOT NULL
|
|
385
|
-
GROUP BY pi_session_file
|
|
386
|
-
HAVING COUNT(*) > 1
|
|
387
|
-
`).all();if(!t&&i.length>0){const N=i.map(({pi_session_file:L})=>L).join(", ");throw new Error(`cannot restore unique Pi session ownership: authoritative metadata has concurrent claimants for ${N}`)}const a=e.prepare("UPDATE nodes SET pi_session_file = NULL WHERE node_id = ?");for(const{pi_session_file:N}of i){const L=e.prepare(`
|
|
388
|
-
SELECT node_id
|
|
389
|
-
FROM nodes
|
|
390
|
-
WHERE pi_session_file = ?
|
|
391
|
-
ORDER BY created ASC, node_id ASC
|
|
392
|
-
`).all(N);for(const{node_id:l}of L.slice(1)){if(!$(F(l),_=>{const d=_.pi_session_file;if(typeof d!="string"||n(d)!==N)throw new Error(`node ${l} session metadata changed during ownership migration`);return _.pi_session_id=null,_.pi_session_file=null,!0}))throw new Error(`node ${l} session metadata changed during ownership migration`);a.run(l)}}e.exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_nodes_pi_session_file ON nodes(pi_session_file);")}E(ee,"reconcileNodeSessionFileOwners");function Nt(e){ee(e,!0)}E(Nt,"makeNodeSessionFileUnique");function ct(e){ee(e,!1)}E(ct,"repairNodeSessionFileUniqueIndex");function Lt(e){const t=O(e);c(e,t,"frozen_at","ALTER TABLE nodes ADD COLUMN frozen_at TEXT;"),c(e,t,"respawn_failures","ALTER TABLE nodes ADD COLUMN respawn_failures INTEGER NOT NULL DEFAULT 0;"),c(e,t,"respawn_not_before","ALTER TABLE nodes ADD COLUMN respawn_not_before TEXT;"),c(e,t,"launched_at","ALTER TABLE nodes ADD COLUMN launched_at TEXT;"),c(e,t,"last_settled_at","ALTER TABLE nodes ADD COLUMN last_settled_at TEXT;"),c(e,t,"terminal_reason","ALTER TABLE nodes ADD COLUMN terminal_reason TEXT;"),e.exec(`
|
|
393
|
-
UPDATE nodes SET terminal_reason = CASE
|
|
394
|
-
WHEN status = 'done' AND final_report IS NOT NULL THEN 'finalized'
|
|
395
|
-
WHEN status = 'done' AND intent = 'parked' THEN 'parked'
|
|
396
|
-
WHEN status = 'done' THEN 'finished'
|
|
397
|
-
WHEN status = 'canceled' THEN 'stranded'
|
|
398
|
-
WHEN status = 'dead' THEN 'crashed'
|
|
399
|
-
END
|
|
400
|
-
WHERE status IN ('done','canceled','dead') AND terminal_reason IS NULL;
|
|
401
|
-
`)}E(Lt,"addNodeLifecycleColumns");function lt(e){const t=O(e);c(e,t,"description","ALTER TABLE nodes ADD COLUMN description TEXT;"),c(e,t,"cycles","ALTER TABLE nodes ADD COLUMN cycles INTEGER;");const n=x();if(!U(n))return;const o=e.prepare("UPDATE nodes SET description = ?, cycles = ? WHERE node_id = ?");for(const T of j(n)){const i=F(T);if(!U(i))continue;let a;try{a=JSON.parse(k(i,"utf8"))}catch{continue}const N=typeof a.description=="string"?a.description:null,L=typeof a.cycles=="number"?a.cycles:null;o.run(N,L,T)}}E(lt,"addNodeLabelColumns");function ut(e){const t=O(e);c(e,t,"outcome_kind","ALTER TABLE nodes ADD COLUMN outcome_kind TEXT;"),c(e,t,"outcome_reason","ALTER TABLE nodes ADD COLUMN outcome_reason TEXT;"),c(e,t,"outcome_at","ALTER TABLE nodes ADD COLUMN outcome_at TEXT;"),c(e,t,"outcome_revision","ALTER TABLE nodes ADD COLUMN outcome_revision INTEGER NOT NULL DEFAULT 0;"),c(e,t,"outcome_detail_json","ALTER TABLE nodes ADD COLUMN outcome_detail_json TEXT;"),c(e,t,"deadline_at","ALTER TABLE nodes ADD COLUMN deadline_at TEXT;"),e.exec(`
|
|
402
|
-
CREATE TABLE IF NOT EXISTS node_outcome_deliveries (
|
|
403
|
-
node_id TEXT PRIMARY KEY,
|
|
404
|
-
state TEXT NOT NULL,
|
|
405
|
-
attempt INTEGER NOT NULL DEFAULT 0,
|
|
406
|
-
action_name TEXT NOT NULL,
|
|
407
|
-
argv_json TEXT NOT NULL,
|
|
408
|
-
cwd TEXT NOT NULL,
|
|
409
|
-
config_origin TEXT NOT NULL,
|
|
410
|
-
payload_json TEXT,
|
|
411
|
-
document_json TEXT,
|
|
412
|
-
next_attempt_at INTEGER NOT NULL DEFAULT 0,
|
|
413
|
-
claimed_at INTEGER,
|
|
414
|
-
claim_owner TEXT,
|
|
415
|
-
accepted_at INTEGER,
|
|
416
|
-
permanent_failed_at INTEGER,
|
|
417
|
-
last_failure_json TEXT,
|
|
418
|
-
created_at INTEGER NOT NULL,
|
|
419
|
-
updated_at INTEGER NOT NULL,
|
|
420
|
-
FOREIGN KEY(node_id) REFERENCES nodes(node_id) ON DELETE CASCADE
|
|
421
|
-
);
|
|
422
|
-
CREATE INDEX IF NOT EXISTS idx_node_outcome_deliveries_due
|
|
423
|
-
ON node_outcome_deliveries(state, next_attempt_at);
|
|
424
|
-
CREATE INDEX IF NOT EXISTS idx_nodes_deadline ON nodes(deadline_at) WHERE deadline_at IS NOT NULL;
|
|
425
|
-
|
|
426
|
-
UPDATE nodes SET outcome_kind='result', outcome_reason='finalized',
|
|
427
|
-
outcome_at=finalized_at, outcome_revision=1
|
|
428
|
-
WHERE final_report IS NOT NULL AND outcome_at IS NULL;
|
|
429
|
-
|
|
430
|
-
UPDATE nodes SET outcome_kind='failure',
|
|
431
|
-
outcome_reason=COALESCE(terminal_reason,'crashed'),
|
|
432
|
-
outcome_at=COALESCE(finalized_at, created), outcome_revision=1
|
|
433
|
-
WHERE final_report IS NULL AND status IN ('done','dead','canceled') AND outcome_at IS NULL;
|
|
434
|
-
`)}E(ut,"addNodeOutcomeAndDeadline");function pt(e){const t=O(e);c(e,t,"telemetry_tokens_in","ALTER TABLE nodes ADD COLUMN telemetry_tokens_in INTEGER;"),c(e,t,"telemetry_context_tokens","ALTER TABLE nodes ADD COLUMN telemetry_context_tokens INTEGER;"),c(e,t,"telemetry_last_activity","ALTER TABLE nodes ADD COLUMN telemetry_last_activity TEXT;"),c(e,t,"telemetry_updated_at","ALTER TABLE nodes ADD COLUMN telemetry_updated_at TEXT;");const n=e.prepare(`UPDATE nodes
|
|
435
|
-
SET telemetry_tokens_in = ?, telemetry_context_tokens = ?,
|
|
436
|
-
telemetry_last_activity = ?, telemetry_updated_at = ?
|
|
437
|
-
WHERE node_id = ?`);for(const{node_id:o}of e.prepare("SELECT node_id FROM nodes").all()){const T=H(x(),o,"job","telemetry.json");if(U(T))try{const i=JSON.parse(k(T,"utf8")),a=typeof i.tokens_in=="number"&&Number.isFinite(i.tokens_in)?i.tokens_in:null,N=typeof i.context_tokens=="number"&&Number.isFinite(i.context_tokens)?i.context_tokens:null,L=typeof i.last_activity=="string"?i.last_activity:null,l=Re(T).mtime.toISOString();n.run(a,N,L,l,o)}catch{}}}E(pt,"addNodeTelemetryColumns");function Rt(e){e.exec(`
|
|
438
|
-
CREATE INDEX IF NOT EXISTS deliveries_external_due_order
|
|
439
|
-
ON deliveries(state, next_attempt_at, created_at, delivery_id)
|
|
440
|
-
WHERE destination_kind = 'external_action' AND canceled_at IS NULL;
|
|
441
|
-
DROP INDEX IF EXISTS deliveries_external_due;
|
|
442
|
-
CREATE INDEX IF NOT EXISTS idx_nodes_parent_created ON nodes(parent, created);
|
|
443
|
-
CREATE INDEX IF NOT EXISTS idx_edges_type_created ON edges(type, created);
|
|
444
|
-
`)}E(Rt,"addRecurringLaneIndexes");function At(e){Ne(e)}E(At,"projectLiveNodeState");function Ot(e){c(e,O(e),"scopes_json","ALTER TABLE nodes ADD COLUMN scopes_json TEXT;")}E(Ot,"addNodeScopes");function It(e){const t=O(e);c(e,t,"grantee",`ALTER TABLE nodes ADD COLUMN grantee TEXT NOT NULL DEFAULT '${g}';`),c(e,t,"sub","ALTER TABLE nodes ADD COLUMN sub TEXT;"),c(e,t,"grant_id","ALTER TABLE nodes ADD COLUMN grant_id TEXT;"),c(e,t,"client_id","ALTER TABLE nodes ADD COLUMN client_id TEXT;"),c(e,t,"metadata_json","ALTER TABLE nodes ADD COLUMN metadata_json TEXT;"),c(e,t,"run_id","ALTER TABLE nodes ADD COLUMN run_id TEXT;"),c(e,t,"event_cursor","ALTER TABLE nodes ADD COLUMN event_cursor INTEGER;"),c(e,t,"usage_json","ALTER TABLE nodes ADD COLUMN usage_json TEXT;"),c(e,t,"deleting_at","ALTER TABLE nodes ADD COLUMN deleting_at TEXT;"),e.exec(`
|
|
445
|
-
ALTER TABLE sessions ADD COLUMN event_offset INTEGER;
|
|
446
|
-
CREATE INDEX idx_nodes_run_id ON nodes(run_id);
|
|
447
|
-
-- Child nodes may be pruned while their root run and event history remain.
|
|
448
|
-
CREATE TABLE run_events (
|
|
449
|
-
run_id TEXT NOT NULL REFERENCES nodes(node_id) ON DELETE CASCADE,
|
|
450
|
-
sequence_number INTEGER NOT NULL,
|
|
451
|
-
node_id TEXT NOT NULL,
|
|
452
|
-
type TEXT NOT NULL,
|
|
453
|
-
ts TEXT NOT NULL,
|
|
454
|
-
body_json TEXT NOT NULL,
|
|
455
|
-
grantee TEXT NOT NULL,
|
|
456
|
-
PRIMARY KEY (run_id, sequence_number)
|
|
457
|
-
);
|
|
458
|
-
CREATE INDEX idx_run_events_ts ON run_events(ts);
|
|
459
|
-
CREATE TABLE idempotency_keys (
|
|
460
|
-
grantee TEXT NOT NULL,
|
|
461
|
-
key TEXT NOT NULL,
|
|
462
|
-
body_hash TEXT NOT NULL,
|
|
463
|
-
run_id TEXT NOT NULL REFERENCES nodes(node_id) ON DELETE CASCADE,
|
|
464
|
-
response_json TEXT NOT NULL,
|
|
465
|
-
created_at TEXT NOT NULL,
|
|
466
|
-
PRIMARY KEY (grantee, key)
|
|
467
|
-
);
|
|
468
|
-
CREATE INDEX idx_idempotency_keys_created_at ON idempotency_keys(created_at);
|
|
469
|
-
`)}E(It,"addRunFoundation");function mt(e){e.exec("ALTER TABLE nodes ADD COLUMN last_activity_at TEXT;"),e.exec(`UPDATE nodes SET last_activity_at = (
|
|
470
|
-
SELECT e.ts FROM run_events e WHERE e.run_id = nodes.node_id
|
|
471
|
-
ORDER BY e.sequence_number DESC LIMIT 1
|
|
472
|
-
) WHERE run_id = node_id`)}E(mt,"addRunLastActivity");function ft(e){e.exec(`
|
|
473
|
-
CREATE TABLE structured_requests (
|
|
474
|
-
request_id TEXT PRIMARY KEY,
|
|
475
|
-
node_id TEXT REFERENCES nodes(node_id) ON DELETE CASCADE,
|
|
476
|
-
mode TEXT NOT NULL,
|
|
477
|
-
submission TEXT NOT NULL,
|
|
478
|
-
dedicated INTEGER NOT NULL,
|
|
479
|
-
state TEXT NOT NULL,
|
|
480
|
-
schema_json TEXT NOT NULL,
|
|
481
|
-
result_json TEXT,
|
|
482
|
-
deadline_at INTEGER,
|
|
483
|
-
created_at INTEGER NOT NULL,
|
|
484
|
-
settled_at INTEGER,
|
|
485
|
-
effects_done_at INTEGER,
|
|
486
|
-
result_path TEXT,
|
|
487
|
-
declined_reason TEXT,
|
|
488
|
-
declined_code TEXT,
|
|
489
|
-
declined_retryable INTEGER,
|
|
490
|
-
failure_reason TEXT,
|
|
491
|
-
failure_detail_json TEXT,
|
|
492
|
-
request_message_id TEXT
|
|
493
|
-
);
|
|
494
|
-
CREATE UNIQUE INDEX idx_structured_requests_one_pending_node ON structured_requests(node_id) WHERE state = 'pending';
|
|
495
|
-
CREATE INDEX idx_structured_requests_expiry ON structured_requests(state, deadline_at);
|
|
496
|
-
CREATE INDEX idx_structured_requests_effects ON structured_requests(effects_done_at) WHERE effects_done_at IS NULL;
|
|
497
|
-
`)}E(ft,"addStructuredRequests");function Xt(e){e.exec(`
|
|
498
|
-
CREATE TABLE grants (
|
|
499
|
-
id TEXT PRIMARY KEY, runtime TEXT NOT NULL, sub TEXT NOT NULL, grantee TEXT NOT NULL,
|
|
500
|
-
rev INTEGER NOT NULL, scopes_json TEXT NOT NULL, resolved_scopes_json TEXT NOT NULL,
|
|
501
|
-
spend_cap REAL, expires_at TEXT, status TEXT NOT NULL, revoked_at TEXT,
|
|
502
|
-
approved_at TEXT, payer TEXT, synced_at TEXT NOT NULL
|
|
503
|
-
);
|
|
504
|
-
CREATE UNIQUE INDEX idx_grants_grantee_sub ON grants(grantee, sub);
|
|
505
|
-
CREATE TABLE runtime_state (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
506
|
-
`)}E(Xt,"addGrantMirror");function Ut(e){e.exec(`UPDATE nodes SET grantee = 'app:terminal' WHERE grantee = '${g}';`)}E(Ut,"migrateTerminalGrantees");function Ct(e){const t={llm:"crtr:llm",act:"crtr:act",schedule:"crtr:schedule","memory:read":"crtr:memory:read:user","memory:write":"crtr:memory:rw:user"},n=e.prepare("SELECT node_id, scopes_json FROM nodes WHERE scopes_json IS NOT NULL").all(),o=e.prepare("UPDATE nodes SET scopes_json = ? WHERE node_id = ?");for(const T of n){const i=JSON.parse(T.scopes_json);if(!Array.isArray(i))throw new Error(`invalid launched scopes for node ${T.node_id}`);const a=[];for(const N of i){if(typeof N!="string")continue;const L=t[N]??N;try{de(L),a.push(L)}catch(l){if(!(l instanceof TypeError))throw l}}o.run(JSON.stringify([...new Set(a)]),T.node_id)}}E(Ct,"migrateLaunchedScopes");function St(e){e.exec(`CREATE TABLE node_tokens (
|
|
507
|
-
broker_process_id TEXT PRIMARY KEY REFERENCES executions(execution_id) ON DELETE CASCADE,
|
|
508
|
-
node_id TEXT NOT NULL REFERENCES nodes(node_id) ON DELETE CASCADE,
|
|
509
|
-
token_hash TEXT NOT NULL UNIQUE, grantee TEXT NOT NULL, uid INTEGER NOT NULL,
|
|
510
|
-
sub TEXT NOT NULL, grant TEXT, client_id TEXT, scopes_json TEXT,
|
|
511
|
-
issued_at TEXT NOT NULL, revoked_at TEXT
|
|
512
|
-
);`)}E(St,"addNodeTokens");function C(e,t){return e.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?").get(t)!==void 0}E(C,"hasTable");function S(e,t,n){return e.prepare(`PRAGMA table_info(${t})`).all().some(o=>o.name===n)}E(S,"hasColumn");function te(e){C(e,"memory_reads")||e.exec(`CREATE TABLE memory_reads (
|
|
513
|
-
node_id TEXT NOT NULL REFERENCES nodes(node_id) ON DELETE CASCADE,
|
|
514
|
-
name TEXT NOT NULL,
|
|
515
|
-
scope TEXT NOT NULL,
|
|
516
|
-
at TEXT NOT NULL,
|
|
517
|
-
type TEXT NOT NULL CHECK (type IN ('read','loaded'))
|
|
518
|
-
);
|
|
519
|
-
CREATE INDEX idx_memory_reads_at ON memory_reads(at);
|
|
520
|
-
CREATE INDEX idx_memory_reads_node ON memory_reads(node_id);
|
|
521
|
-
INSERT OR IGNORE INTO canvas_meta(key, value) VALUES ('memory_reads.started_at', strftime('%Y-%m-%dT%H:%M:%fZ','now'));`)}E(te,"addMemoryReads");function ne(e){S(e,"sessions","indexed_entry_id")||e.exec("ALTER TABLE sessions ADD COLUMN indexed_entry_id TEXT; ALTER TABLE sessions ADD COLUMN input_ids_json TEXT NOT NULL DEFAULT '[]';")}E(ne,"addConversationIndex");function Ee(e){C(e,"app_users")||e.exec(`CREATE TABLE app_users (
|
|
522
|
-
app TEXT PRIMARY KEY, uid INTEGER NOT NULL UNIQUE,
|
|
523
|
-
created TEXT NOT NULL, status TEXT NOT NULL CHECK (status IN ('active', 'retired'))
|
|
524
|
-
);`)}E(Ee,"addAppUsers");function oe(e){S(e,"crons","grantee")||(e.exec("ALTER TABLE crons ADD COLUMN grantee TEXT NOT NULL DEFAULT 'app:terminal'"),e.exec(`UPDATE crons SET grantee = COALESCE(
|
|
525
|
-
(SELECT COALESCE(nodes.grantee, 'app:terminal') FROM nodes WHERE nodes.node_id = crons.anchor_node),
|
|
526
|
-
'app:terminal') WHERE anchor_node IS NOT NULL`))}E(oe,"addCronGrantee");function Dt(e){te(e),ne(e),Ee(e),oe(e)}E(Dt,"convergeSharedVersions");function gt(e){(e.prepare("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'app_users'").get()?.sql??"").includes("'removed'")||e.exec(`CREATE TABLE app_users_new (
|
|
527
|
-
uid INTEGER PRIMARY KEY, app TEXT NOT NULL,
|
|
528
|
-
created TEXT NOT NULL, status TEXT NOT NULL CHECK (status IN ('active', 'removed')),
|
|
529
|
-
removed_at TEXT
|
|
530
|
-
);
|
|
531
|
-
INSERT INTO app_users_new (uid, app, created, status, removed_at)
|
|
532
|
-
SELECT uid, app, created, CASE status WHEN 'active' THEN 'active' ELSE 'removed' END,
|
|
533
|
-
CASE status WHEN 'active' THEN NULL ELSE created END FROM app_users;
|
|
534
|
-
DROP TABLE app_users;
|
|
535
|
-
ALTER TABLE app_users_new RENAME TO app_users;
|
|
536
|
-
CREATE UNIQUE INDEX idx_app_users_active_app ON app_users(app) WHERE status = 'active';`)}E(gt,"appUsersRemoved");function ie(e){C(e,"objects")||(e.exec(`
|
|
537
|
-
CREATE TABLE objects (
|
|
538
|
-
rowid INTEGER PRIMARY KEY,
|
|
539
|
-
id TEXT NOT NULL UNIQUE,
|
|
540
|
-
type TEXT NOT NULL CHECK (type IN ('document','node','job','person','app','repo','profile')),
|
|
541
|
-
kind TEXT,
|
|
542
|
-
owner INTEGER REFERENCES objects(rowid),
|
|
543
|
-
privacy TEXT NOT NULL DEFAULT 'public' CHECK (privacy IN ('public','friends','private')),
|
|
544
|
-
name TEXT NOT NULL,
|
|
545
|
-
preview TEXT NOT NULL DEFAULT '',
|
|
546
|
-
created_at TEXT NOT NULL,
|
|
547
|
-
deleted_at TEXT
|
|
548
|
-
);
|
|
549
|
-
CREATE UNIQUE INDEX objects_doc_name ON objects(owner, name) WHERE type = 'document' AND deleted_at IS NULL;
|
|
550
|
-
CREATE UNIQUE INDEX objects_handle ON objects(type, name) WHERE type IN ('person','app','repo','profile');
|
|
551
|
-
CREATE INDEX objects_owner_type ON objects(owner, type, kind);
|
|
552
|
-
CREATE INDEX objects_type_kind ON objects(type, kind);
|
|
553
|
-
|
|
554
|
-
CREATE TABLE documents (
|
|
555
|
-
object INTEGER PRIMARY KEY REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
556
|
-
summary TEXT,
|
|
557
|
-
heads_key TEXT NOT NULL,
|
|
558
|
-
revision INTEGER NOT NULL,
|
|
559
|
-
why_it_exists TEXT,
|
|
560
|
-
unlisted INTEGER NOT NULL DEFAULT 0,
|
|
561
|
-
lint_ignore_json TEXT,
|
|
562
|
-
extensions_json TEXT,
|
|
563
|
-
source_path TEXT
|
|
564
|
-
);
|
|
565
|
-
|
|
566
|
-
CREATE TABLE revisions (
|
|
567
|
-
rowid INTEGER PRIMARY KEY,
|
|
568
|
-
document INTEGER NOT NULL REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
569
|
-
rev_id TEXT NOT NULL UNIQUE,
|
|
570
|
-
number INTEGER NOT NULL,
|
|
571
|
-
body_hash TEXT NOT NULL,
|
|
572
|
-
author INTEGER REFERENCES objects(rowid),
|
|
573
|
-
rationale TEXT,
|
|
574
|
-
verbatim INTEGER NOT NULL DEFAULT 0,
|
|
575
|
-
created_at TEXT NOT NULL
|
|
576
|
-
);
|
|
577
|
-
CREATE TABLE revision_parents (
|
|
578
|
-
child INTEGER NOT NULL REFERENCES revisions(rowid) ON DELETE CASCADE,
|
|
579
|
-
parent INTEGER NOT NULL REFERENCES revisions(rowid) ON DELETE CASCADE,
|
|
580
|
-
PRIMARY KEY (child, parent)
|
|
581
|
-
) WITHOUT ROWID;
|
|
582
|
-
CREATE INDEX revisions_doc ON revisions(document, number);
|
|
583
|
-
|
|
584
|
-
CREATE TABLE jobs (
|
|
585
|
-
object INTEGER PRIMARY KEY REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
586
|
-
command TEXT NOT NULL,
|
|
587
|
-
status TEXT NOT NULL CHECK (status IN ('running','exited','killed')),
|
|
588
|
-
exit_code INTEGER,
|
|
589
|
-
job_dir TEXT NOT NULL,
|
|
590
|
-
log_path TEXT NOT NULL
|
|
591
|
-
);
|
|
592
|
-
|
|
593
|
-
CREATE TABLE identities (
|
|
594
|
-
object INTEGER PRIMARY KEY REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
595
|
-
scheme TEXT NOT NULL,
|
|
596
|
-
external_id TEXT NOT NULL,
|
|
597
|
-
UNIQUE (scheme, external_id)
|
|
598
|
-
);
|
|
599
|
-
|
|
600
|
-
CREATE TABLE watches (
|
|
601
|
-
rowid INTEGER PRIMARY KEY,
|
|
602
|
-
watcher INTEGER NOT NULL REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
603
|
-
object INTEGER NOT NULL REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
604
|
-
in_tree INTEGER NOT NULL DEFAULT 0,
|
|
605
|
-
read_heads_key TEXT,
|
|
606
|
-
created_at TEXT NOT NULL,
|
|
607
|
-
UNIQUE (watcher, object)
|
|
608
|
-
);
|
|
609
|
-
CREATE INDEX watches_object ON watches(object, watcher);
|
|
610
|
-
CREATE INDEX watches_tree_down ON watches(watcher, object) WHERE in_tree = 1;
|
|
611
|
-
CREATE INDEX watches_tree_up ON watches(object, watcher) WHERE in_tree = 1;
|
|
612
|
-
|
|
613
|
-
CREATE TABLE watch_subscriptions (
|
|
614
|
-
rowid INTEGER PRIMARY KEY,
|
|
615
|
-
watch INTEGER NOT NULL REFERENCES watches(rowid) ON DELETE CASCADE,
|
|
616
|
-
pattern TEXT NOT NULL,
|
|
617
|
-
filter_json TEXT,
|
|
618
|
-
config_json TEXT,
|
|
619
|
-
started_by TEXT NOT NULL CHECK (started_by IN ('spawn','job-start','ask','read','write','act','message','deliver','fork')),
|
|
620
|
-
ends_on TEXT,
|
|
621
|
-
created_at TEXT NOT NULL
|
|
622
|
-
);
|
|
623
|
-
CREATE INDEX watch_subscriptions_watch ON watch_subscriptions(watch);
|
|
624
|
-
|
|
625
|
-
CREATE TABLE context_sessions (
|
|
626
|
-
node INTEGER PRIMARY KEY REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
627
|
-
preference_snapshot TEXT,
|
|
628
|
-
listed_json TEXT NOT NULL DEFAULT '{}'
|
|
629
|
-
);
|
|
630
|
-
|
|
631
|
-
CREATE TABLE exposures (
|
|
632
|
-
node INTEGER NOT NULL REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
633
|
-
object INTEGER NOT NULL REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
634
|
-
channel TEXT NOT NULL CHECK (channel IN ('system','transcript')),
|
|
635
|
-
level TEXT NOT NULL CHECK (level IN ('name','preview','content')),
|
|
636
|
-
heads_key TEXT,
|
|
637
|
-
content_hash TEXT,
|
|
638
|
-
PRIMARY KEY (node, object, channel)
|
|
639
|
-
) WITHOUT ROWID;
|
|
640
|
-
|
|
641
|
-
CREATE TABLE rules (
|
|
642
|
-
rowid INTEGER PRIMARY KEY,
|
|
643
|
-
object INTEGER NOT NULL REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
644
|
-
owner INTEGER,
|
|
645
|
-
position INTEGER NOT NULL,
|
|
646
|
-
on_event TEXT NOT NULL CHECK (on_event IN ('boot','workspace-open','file-read','document-read','command','pre-command','slash-command')),
|
|
647
|
-
match_json TEXT, match_frontmatter_json TEXT, if_json TEXT,
|
|
648
|
-
deliver TEXT NOT NULL CHECK (deliver IN ('name','preview','content'))
|
|
649
|
-
);
|
|
650
|
-
CREATE INDEX rules_on_owner ON rules(on_event, owner);
|
|
651
|
-
CREATE INDEX rules_object ON rules(object);
|
|
652
|
-
|
|
653
|
-
ALTER TABLE nodes ADD COLUMN listen_default_json TEXT;
|
|
654
|
-
ALTER TABLE messages ADD COLUMN event TEXT;
|
|
655
|
-
ALTER TABLE messages ADD COLUMN object_id TEXT;
|
|
656
|
-
ALTER TABLE messages ADD COLUMN level INTEGER;
|
|
657
|
-
ALTER TABLE messages ADD COLUMN actor_id TEXT;
|
|
658
|
-
`),se(e),e.exec("ANALYZE"))}E(ie,"canvasObjectsSchema");function se(e){const t=new Date().toISOString(),n=e.prepare("INSERT INTO objects (id, type, kind, owner, privacy, name, preview, created_at) VALUES (?, ?, NULL, NULL, 'public', ?, '', ?) RETURNING rowid"),o=e.prepare("INSERT INTO identities (object, scheme, external_id) VALUES (?, ?, ?)"),T=e.prepare(`SELECT DISTINCT n.grantee FROM nodes n
|
|
659
|
-
WHERE NOT EXISTS (SELECT 1 FROM identities i WHERE i.external_id = n.grantee AND i.scheme IN ('app', 'local-user'))`).all();for(const{grantee:i}of T){let a;if(i===g)a={type:"person",scheme:"local-user",handle:"user"};else if(/^app:[a-z0-9][a-z0-9._-]*$/.test(i))a={type:"app",scheme:"app",handle:i.slice(4)};else throw new Error(`canvas objects migration: node grantee ${JSON.stringify(i)} is neither app:<name> nor ${g}`);const{rowid:N}=n.get(`${Date.now().toString(36)}-${Y(4).toString("hex")}`,a.type,a.handle,t);o.run(N,a.scheme,i)}e.exec(`INSERT INTO objects (id, type, kind, owner, privacy, name, preview, created_at)
|
|
660
|
-
SELECT n.node_id, 'node', n.kind, i.object, 'public', n.name, '', n.created
|
|
661
|
-
FROM nodes n JOIN identities i ON i.external_id = n.grantee AND i.scheme IN ('app', 'local-user')
|
|
662
|
-
WHERE NOT EXISTS (SELECT 1 FROM objects o WHERE o.id = n.node_id)`)}E(se,"copyNodesIntoObjects");function wt(e){if(ie(e),!S(e,"nodes","name"))return;se(e);const t=e.prepare("SELECT COUNT(*) AS n FROM nodes WHERE node_id NOT IN (SELECT id FROM objects)").get();if(t.n>0)throw new Error(`canvasObjectsCut: ${t.n} node(s) have no objects row`);e.exec(`
|
|
663
|
-
ALTER TABLE edges RENAME TO legacy_edges;
|
|
664
|
-
CREATE TABLE edges (
|
|
665
|
-
type TEXT NOT NULL CHECK (type IN ('acted-on','links-to')),
|
|
666
|
-
src INTEGER NOT NULL REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
667
|
-
dst INTEGER NOT NULL REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
668
|
-
written_as TEXT,
|
|
669
|
-
created_at TEXT NOT NULL,
|
|
670
|
-
PRIMARY KEY (type, src, dst)
|
|
671
|
-
) WITHOUT ROWID;
|
|
672
|
-
CREATE INDEX edges_dst ON edges(type, dst, src);
|
|
673
|
-
DROP VIEW node_rows;
|
|
674
|
-
ALTER TABLE nodes DROP COLUMN name;
|
|
675
|
-
ALTER TABLE nodes DROP COLUMN kind;
|
|
676
|
-
ALTER TABLE nodes DROP COLUMN grantee;
|
|
677
|
-
`),e.exec(`
|
|
678
|
-
CREATE VIEW node_rows AS
|
|
679
|
-
SELECT n.*,
|
|
680
|
-
o.name,
|
|
681
|
-
o.kind,
|
|
682
|
-
oi.external_id AS grantee,
|
|
683
|
-
CASE WHEN e.state = 'running' THEN e.pid ELSE NULL END AS pi_pid,
|
|
684
|
-
CASE WHEN e.state = 'running' THEN e.process_identity ELSE NULL END AS pi_pid_identity,
|
|
685
|
-
e.turn_open_at,
|
|
686
|
-
e.turn_closed_at,
|
|
687
|
-
s.session_id AS pi_session_id,
|
|
688
|
-
s.transcript_path AS pi_session_file,
|
|
689
|
-
f.fault_id,
|
|
690
|
-
f.link AS fault_link,
|
|
691
|
-
f.op AS fault_op,
|
|
692
|
-
f.kind AS fault_kind,
|
|
693
|
-
f.message AS fault_message,
|
|
694
|
-
f.since AS fault_since,
|
|
695
|
-
f.operation_id AS fault_operation_id,
|
|
696
|
-
f.anchor_entry_id AS fault_anchor_entry_id,
|
|
697
|
-
f.retry_disposition AS fault_retry_disposition,
|
|
698
|
-
f.retry_by AS fault_retry_by,
|
|
699
|
-
f.retry_attempt AS fault_retry_attempt,
|
|
700
|
-
f.retry_max AS fault_retry_max,
|
|
701
|
-
f.retry_next_at AS fault_retry_next_at,
|
|
702
|
-
f.provider_error_json AS fault_provider_error_json
|
|
703
|
-
FROM nodes n
|
|
704
|
-
JOIN objects o ON o.id = n.node_id
|
|
705
|
-
LEFT JOIN identities oi ON oi.object = o.owner
|
|
706
|
-
LEFT JOIN executions e ON e.node_id = n.node_id AND e.state != 'exited'
|
|
707
|
-
LEFT JOIN sessions s ON s.node_id = n.node_id AND s.unbound_at IS NULL
|
|
708
|
-
LEFT JOIN node_faults f ON f.node_id = n.node_id AND f.active = 1;
|
|
709
|
-
ANALYZE;
|
|
710
|
-
`)}E(wt,"canvasObjectsCut");function yt(e){e.exec(`CREATE TABLE IF NOT EXISTS uploads (
|
|
711
|
-
upload_id TEXT PRIMARY KEY, grantee TEXT NOT NULL, name TEXT NOT NULL, media_type TEXT NOT NULL,
|
|
712
|
-
size INTEGER NOT NULL, created_at TEXT NOT NULL, delete_pending_since TEXT
|
|
713
|
-
);
|
|
714
|
-
CREATE INDEX IF NOT EXISTS idx_uploads_grantee ON uploads(grantee);
|
|
715
|
-
CREATE INDEX IF NOT EXISTS idx_uploads_delete_pending ON uploads(delete_pending_since) WHERE delete_pending_since IS NOT NULL;
|
|
716
|
-
CREATE TABLE IF NOT EXISTS upload_uses (
|
|
717
|
-
upload_id TEXT NOT NULL, run_id TEXT NOT NULL, path TEXT NOT NULL,
|
|
718
|
-
PRIMARY KEY (run_id, path)
|
|
719
|
-
);
|
|
720
|
-
CREATE INDEX IF NOT EXISTS idx_upload_uses_upload ON upload_uses(upload_id);
|
|
721
|
-
CREATE TABLE IF NOT EXISTS shares (
|
|
722
|
-
share_id TEXT PRIMARY KEY, source_space TEXT NOT NULL, source_path TEXT NOT NULL, storage_key TEXT NOT NULL,
|
|
723
|
-
url TEXT NOT NULL, created_by TEXT NOT NULL, created_at TEXT NOT NULL, expires_at TEXT,
|
|
724
|
-
status TEXT NOT NULL CHECK (status IN ('on', 'off')), object_deleted_at TEXT
|
|
725
|
-
);
|
|
726
|
-
CREATE INDEX IF NOT EXISTS idx_shares_created_by ON shares(created_by);`)}E(yt,"addFileTransfer");function vt(e){e.exec(`CREATE TABLE IF NOT EXISTS daemon_actions (
|
|
727
|
-
action_id TEXT PRIMARY KEY,
|
|
728
|
-
adapter_json TEXT NOT NULL,
|
|
729
|
-
body_json TEXT,
|
|
730
|
-
idempotency_key TEXT NOT NULL UNIQUE,
|
|
731
|
-
state TEXT NOT NULL CHECK (state IN ('pending', 'handed_off', 'acknowledged', 'permanent_failed')),
|
|
732
|
-
attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0),
|
|
733
|
-
next_attempt_at INTEGER,
|
|
734
|
-
claimed_at INTEGER,
|
|
735
|
-
claim_owner TEXT,
|
|
736
|
-
acknowledged_at INTEGER,
|
|
737
|
-
failed_at INTEGER,
|
|
738
|
-
last_failure_json TEXT,
|
|
739
|
-
canceled_at INTEGER,
|
|
740
|
-
created_at INTEGER NOT NULL,
|
|
741
|
-
updated_at INTEGER NOT NULL
|
|
742
|
-
);
|
|
743
|
-
CREATE INDEX IF NOT EXISTS daemon_actions_due ON daemon_actions(state, next_attempt_at, created_at, action_id) WHERE canceled_at IS NULL;
|
|
744
|
-
CREATE TEMP TABLE moved_actions AS
|
|
745
|
-
SELECT delivery_id, message_id FROM deliveries
|
|
746
|
-
WHERE destination_kind = 'external_action' AND adapter_json IS NOT NULL
|
|
747
|
-
AND (json_extract(adapter_json, '$.adapter') IN ('cron_spawn', 'cron_human') OR idempotency_key LIKE 'human-action:%');
|
|
748
|
-
INSERT OR IGNORE INTO daemon_actions
|
|
749
|
-
(action_id, adapter_json, body_json, idempotency_key, state, attempts, next_attempt_at, claimed_at, claim_owner,
|
|
750
|
-
acknowledged_at, failed_at, last_failure_json, canceled_at, created_at, updated_at)
|
|
751
|
-
SELECT d.delivery_id, d.adapter_json, m.body_json, COALESCE(d.idempotency_key, 'delivery:' || d.delivery_id),
|
|
752
|
-
CASE WHEN d.state = 'armed' THEN 'pending' ELSE d.state END, d.attempts,
|
|
753
|
-
CASE WHEN d.state = 'armed' THEN COALESCE(d.next_attempt_at, d.updated_at) ELSE d.next_attempt_at END,
|
|
754
|
-
d.claimed_at, d.claim_owner, d.acknowledged_at, d.failed_at, d.last_failure_json, d.canceled_at, d.created_at, d.updated_at
|
|
755
|
-
FROM deliveries d JOIN moved_actions ma ON ma.delivery_id = d.delivery_id
|
|
756
|
-
LEFT JOIN messages m ON m.message_id = d.message_id;
|
|
757
|
-
UPDATE human_requests
|
|
758
|
-
SET action_delivery_id = (SELECT a.action_id FROM daemon_actions a WHERE a.idempotency_key = 'human-action:' || human_requests.inbox_ticket_id)
|
|
759
|
-
WHERE action_delivery_id IS NOT NULL
|
|
760
|
-
AND EXISTS (SELECT 1 FROM daemon_actions a WHERE a.idempotency_key = 'human-action:' || human_requests.inbox_ticket_id);
|
|
761
|
-
DELETE FROM deliveries WHERE delivery_id IN (SELECT delivery_id FROM moved_actions);
|
|
762
|
-
DELETE FROM messages
|
|
763
|
-
WHERE message_id IN (SELECT message_id FROM moved_actions WHERE message_id IS NOT NULL)
|
|
764
|
-
AND NOT EXISTS (SELECT 1 FROM deliveries d WHERE d.message_id = messages.message_id);
|
|
765
|
-
DROP TABLE moved_actions;`)}E(vt,"moveDaemonActions");function ht(e){W(e,61,()=>{if(!C(e,"objects"))return;const t=e.prepare("SELECT COUNT(*) AS n FROM objects").get().n,n=e.prepare("SELECT name, sql FROM sqlite_master WHERE type = 'view' AND sql LIKE '%objects%'").all();for(const i of n)e.exec(`DROP VIEW "${i.name}"`);e.exec(`
|
|
766
|
-
CREATE TABLE objects_new (
|
|
767
|
-
rowid INTEGER PRIMARY KEY,
|
|
768
|
-
id TEXT NOT NULL UNIQUE,
|
|
769
|
-
type TEXT NOT NULL,
|
|
770
|
-
kind TEXT,
|
|
771
|
-
owner INTEGER REFERENCES objects(rowid),
|
|
772
|
-
privacy TEXT NOT NULL DEFAULT 'public' CHECK (privacy IN ('public','friends','private')),
|
|
773
|
-
name TEXT NOT NULL,
|
|
774
|
-
preview TEXT NOT NULL DEFAULT '',
|
|
775
|
-
created_at TEXT NOT NULL,
|
|
776
|
-
deleted_at TEXT
|
|
777
|
-
);
|
|
778
|
-
INSERT INTO objects_new (rowid, id, type, kind, owner, privacy, name, preview, created_at, deleted_at)
|
|
779
|
-
SELECT rowid, id, type, kind, owner, privacy, name, preview, created_at, deleted_at FROM objects;
|
|
780
|
-
DROP TABLE objects;
|
|
781
|
-
ALTER TABLE objects_new RENAME TO objects;
|
|
782
|
-
CREATE UNIQUE INDEX objects_doc_name ON objects(owner, name) WHERE type = 'document' AND deleted_at IS NULL;
|
|
783
|
-
CREATE UNIQUE INDEX objects_handle ON objects(type, name) WHERE type IN ('person','app','repo','profile');
|
|
784
|
-
CREATE INDEX objects_owner_type ON objects(owner, type, kind);
|
|
785
|
-
CREATE INDEX objects_type_kind ON objects(type, kind);
|
|
786
|
-
`);for(const i of n)e.exec(i.sql);const o=e.prepare("SELECT COUNT(*) AS n FROM objects").get().n;if(o!==t)throw new Error(`objects rebuild lost rows: expected ${t}, got ${o}`);const T=e.prepare("PRAGMA foreign_key_check").all();if(T.length>0)throw new Error(`objects rebuild left ${T.length} foreign-key violation(s)`)})}E(ht,"objectsWithoutTypeCheck");function xt(e){C(e,"deliveries")&&e.exec(`
|
|
787
|
-
CREATE INDEX IF NOT EXISTS deliveries_destination ON deliveries(destination_id, wake, state, created_at);
|
|
788
|
-
DROP INDEX IF EXISTS deliveries_node_inbox;
|
|
789
|
-
`)}E(xt,"addDeliveriesDestinationIndex");function Ft(e){e.exec(`
|
|
790
|
-
CREATE TABLE object_kinds (
|
|
791
|
-
name TEXT PRIMARY KEY, owner INTEGER NOT NULL REFERENCES objects(rowid),
|
|
792
|
-
description TEXT NOT NULL DEFAULT '', events_json TEXT NOT NULL,
|
|
793
|
-
created_at TEXT NOT NULL, updated_at TEXT NOT NULL
|
|
794
|
-
);
|
|
795
|
-
ALTER TABLE custom_objects ADD COLUMN description TEXT;
|
|
796
|
-
ALTER TABLE custom_objects ADD COLUMN summary TEXT;
|
|
797
|
-
ALTER TABLE custom_objects ADD COLUMN state_json TEXT;
|
|
798
|
-
ALTER TABLE custom_objects ADD COLUMN published_at TEXT;
|
|
799
|
-
ALTER TABLE custom_objects ADD COLUMN token_hash TEXT;
|
|
800
|
-
ALTER TABLE custom_objects ADD COLUMN dropped INTEGER NOT NULL DEFAULT 0;
|
|
801
|
-
ALTER TABLE messages ADD COLUMN cause_message_id TEXT;
|
|
802
|
-
ALTER TABLE messages ADD COLUMN depth INTEGER NOT NULL DEFAULT 0;
|
|
803
|
-
CREATE TABLE message_sequence (id INTEGER PRIMARY KEY CHECK (id = 1), seq INTEGER NOT NULL);
|
|
804
|
-
INSERT INTO message_sequence (id, seq) SELECT 1, COALESCE(MAX(seq), 0) FROM messages;
|
|
805
|
-
CREATE TABLE message_causes (message_id TEXT PRIMARY KEY, depth INTEGER NOT NULL);
|
|
806
|
-
CREATE TRIGGER preserve_message_cause BEFORE DELETE ON messages BEGIN
|
|
807
|
-
INSERT OR IGNORE INTO message_causes (message_id, depth) VALUES (OLD.message_id, OLD.depth);
|
|
808
|
-
END;
|
|
809
|
-
DROP INDEX objects_doc_name;
|
|
810
|
-
CREATE UNIQUE INDEX objects_named ON objects(owner, name)
|
|
811
|
-
WHERE deleted_at IS NULL AND (type = 'document' OR instr(type, '.') > 0);
|
|
812
|
-
`)}E(Ft,"customObjectKinds");function Mt(e){e.exec("CREATE INDEX IF NOT EXISTS revision_parents_parent ON revision_parents(parent);")}E(Mt,"addRevisionParentsParentIndex");function kt(e){C(e,"run_events")&&e.exec(`
|
|
813
|
-
DELETE FROM run_events
|
|
814
|
-
WHERE type IN ('node.thinking.delta', 'node.output_text.delta')
|
|
815
|
-
AND EXISTS (SELECT 1 FROM run_events done
|
|
816
|
-
WHERE done.run_id = run_events.run_id AND done.node_id = run_events.node_id
|
|
817
|
-
AND done.type IN ('node.thinking.done', 'node.output_text.done', 'usage')
|
|
818
|
-
AND done.sequence_number > run_events.sequence_number);
|
|
819
|
-
`)}E(kt,"dropSpentRunEventDeltas");function jt(e){e.exec("CREATE INDEX IF NOT EXISTS idx_run_events_node_type ON run_events(run_id, node_id, type);")}E(jt,"addRunEventsNodeTypeIndex");function Ht(e){W(e,63,()=>{if(!C(e,"objects")||!C(e,"deliveries"))return;if(S(e,"objects","reception")||e.exec("ALTER TABLE objects ADD COLUMN reception INTEGER NOT NULL DEFAULT 0"),S(e,"objects","listen_json")||e.exec("ALTER TABLE objects ADD COLUMN listen_json TEXT"),e.exec(`UPDATE objects SET reception = CASE WHEN n.status IN ('active','idle') THEN 2 ELSE 1 END
|
|
820
|
-
FROM nodes n WHERE n.node_id = objects.id`),S(e,"nodes","listen_default_json")){e.exec(`UPDATE objects SET listen_json = n.listen_default_json FROM nodes n
|
|
821
|
-
WHERE n.node_id = objects.id AND n.listen_default_json IS NOT NULL`);const n=e.prepare("SELECT name, sql FROM sqlite_master WHERE type = 'view' AND sql LIKE '%nodes%'").all();for(const o of n)e.exec(`DROP VIEW "${o.name}"`);e.exec("ALTER TABLE nodes DROP COLUMN listen_default_json");for(const o of n)e.exec(o.sql)}if(e.exec(`
|
|
822
|
-
DROP TRIGGER IF EXISTS nodes_reception_insert;
|
|
823
|
-
DROP TRIGGER IF EXISTS nodes_reception_status;
|
|
824
|
-
CREATE TRIGGER nodes_reception_insert AFTER INSERT ON nodes BEGIN
|
|
825
|
-
UPDATE objects SET reception = CASE WHEN NEW.status IN ('active','idle') THEN 2 ELSE 1 END WHERE id = NEW.node_id;
|
|
826
|
-
END;
|
|
827
|
-
CREATE TRIGGER nodes_reception_status AFTER UPDATE OF status ON nodes BEGIN
|
|
828
|
-
UPDATE objects SET reception = CASE WHEN NEW.status IN ('active','idle') THEN 2 ELSE 1 END WHERE id = NEW.node_id;
|
|
829
|
-
END;
|
|
830
|
-
CREATE INDEX IF NOT EXISTS objects_receiving ON objects(rowid) WHERE reception = 2;
|
|
831
|
-
`),S(e,"deliveries","destination_kind")){const n=e.prepare("SELECT COUNT(*) AS n FROM deliveries").get().n;e.exec(`
|
|
832
|
-
CREATE TABLE deliveries_new (
|
|
833
|
-
delivery_id TEXT PRIMARY KEY,
|
|
834
|
-
message_id TEXT REFERENCES messages(message_id),
|
|
835
|
-
destination_id TEXT NOT NULL,
|
|
836
|
-
urgency TEXT NOT NULL CHECK (urgency IN ('critical', 'urgent', 'normal', 'deferred')),
|
|
837
|
-
wake INTEGER NOT NULL CHECK (wake IN (0, 1)),
|
|
838
|
-
state TEXT NOT NULL CHECK (state IN ('armed', 'pending', 'handed_off', 'acknowledged', 'permanent_failed')),
|
|
839
|
-
attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0),
|
|
840
|
-
next_attempt_at INTEGER,
|
|
841
|
-
claimed_at INTEGER,
|
|
842
|
-
claim_owner TEXT,
|
|
843
|
-
acknowledged_at INTEGER,
|
|
844
|
-
failed_at INTEGER,
|
|
845
|
-
last_failure_json TEXT,
|
|
846
|
-
adapter_json TEXT,
|
|
847
|
-
idempotency_key TEXT,
|
|
848
|
-
canceled_at INTEGER,
|
|
849
|
-
created_at INTEGER NOT NULL,
|
|
850
|
-
updated_at INTEGER NOT NULL,
|
|
851
|
-
CHECK (state = 'armed' OR message_id IS NOT NULL)
|
|
852
|
-
);
|
|
853
|
-
INSERT INTO deliveries_new (rowid, delivery_id, message_id, destination_id, urgency, wake, state, attempts, next_attempt_at,
|
|
854
|
-
claimed_at, claim_owner, acknowledged_at, failed_at, last_failure_json, adapter_json, idempotency_key, canceled_at, created_at, updated_at)
|
|
855
|
-
SELECT rowid, delivery_id, message_id, destination_id, urgency, wake, state, attempts, next_attempt_at,
|
|
856
|
-
claimed_at, claim_owner, acknowledged_at, failed_at, last_failure_json, adapter_json, idempotency_key, canceled_at, created_at, updated_at
|
|
857
|
-
FROM deliveries;
|
|
858
|
-
DROP TABLE deliveries;
|
|
859
|
-
ALTER TABLE deliveries_new RENAME TO deliveries;
|
|
860
|
-
CREATE INDEX deliveries_destination ON deliveries(destination_id, wake, state, created_at);
|
|
861
|
-
CREATE INDEX deliveries_due ON deliveries(state, next_attempt_at, created_at, delivery_id) WHERE next_attempt_at IS NOT NULL AND canceled_at IS NULL;
|
|
862
|
-
CREATE INDEX deliveries_handed_off ON deliveries(claim_owner) WHERE state = 'handed_off';
|
|
863
|
-
CREATE INDEX deliveries_message ON deliveries(message_id);
|
|
864
|
-
CREATE UNIQUE INDEX deliveries_idempotency ON deliveries(idempotency_key) WHERE idempotency_key IS NOT NULL;
|
|
865
|
-
`);const o=e.prepare("SELECT COUNT(*) AS n FROM deliveries").get().n;if(o!==n)throw new Error(`deliveries rebuild lost rows: expected ${n}, got ${o}`)}const t=e.prepare("PRAGMA foreign_key_check").all();if(t.length>0)throw new Error(`v64 left ${t.length} foreign-key violation(s)`)})}E(Ht,"nodesOnObjectInterface");function Gt(e){W(e,66,()=>{if(e.exec(`CREATE TABLE IF NOT EXISTS custom_objects (
|
|
866
|
-
object INTEGER PRIMARY KEY REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
867
|
-
receiver_json TEXT,
|
|
868
|
-
payload_json TEXT
|
|
869
|
-
)`),S(e,"deliveries","adapter_json")){const n=e.prepare(`SELECT d.delivery_id, d.state, d.idempotency_key
|
|
870
|
-
FROM deliveries d
|
|
871
|
-
WHERE d.adapter_json IS NOT NULL
|
|
872
|
-
AND NOT EXISTS (SELECT 1 FROM nodes n JOIN objects o ON o.id = n.node_id AND o.type = 'node'
|
|
873
|
-
WHERE d.idempotency_key = 'node-outcome:' || n.node_id)`).all(),o=n.filter(s=>s.idempotency_key?.startsWith("node-outcome:")!==!0||s.state!=="acknowledged"&&s.state!=="permanent_failed");if(o.length)throw new Error(`v67 cannot migrate ${o.length} unmappable external-action deliver${o.length===1?"y":"ies"}: ${o.slice(0,5).map(s=>`${s.delivery_id} (${s.state}, ${s.idempotency_key??"no key"})`).join(", ")}; pending work requires a live outcome node`);const T=e.prepare("DELETE FROM deliveries WHERE delivery_id = ?");for(const s of n)T.run(s.delivery_id);const i=e.prepare(`SELECT d.delivery_id, d.message_id, d.destination_id,
|
|
874
|
-
d.adapter_json, d.state, d.created_at, n.node_id, o.owner, o.privacy
|
|
875
|
-
FROM deliveries d
|
|
876
|
-
JOIN nodes n ON d.idempotency_key = 'node-outcome:' || n.node_id
|
|
877
|
-
JOIN objects o ON o.id = n.node_id AND o.type = 'node'
|
|
878
|
-
WHERE d.idempotency_key LIKE 'node-outcome:%'`).all(),a=e.prepare(`INSERT INTO objects
|
|
879
|
-
(id, type, kind, owner, privacy, name, preview, created_at, reception)
|
|
880
|
-
VALUES (?, 'action', NULL, ?, ?, ?, '', ?, 2) RETURNING rowid`),N=e.prepare("INSERT INTO custom_objects (object, receiver_json, payload_json) VALUES (?, ?, ?)"),L=e.prepare(`INSERT INTO watches (watcher, object, in_tree, read_heads_key, created_at)
|
|
881
|
-
VALUES (?, ?, 1, NULL, ?) RETURNING rowid`),l=e.prepare(`INSERT INTO watch_subscriptions
|
|
882
|
-
(watch, pattern, filter_json, config_json, started_by, ends_on, created_at)
|
|
883
|
-
VALUES (?, 'outcome', NULL, '{"1":"after-turn"}', 'spawn', NULL, ?)`),r=e.prepare("UPDATE deliveries SET destination_id = ?, idempotency_key = NULL WHERE delivery_id = ?"),_=e.prepare("SELECT body_json FROM messages WHERE message_id = ?"),d=e.prepare(`UPDATE messages SET body_json = ?, event = 'outcome', object_id = ?, level = 1
|
|
884
|
-
WHERE message_id = ?`);for(const s of i){if(s.owner===null)throw new Error(`v67 outcome node ${s.node_id} has no owner`);const A=JSON.parse(s.adapter_json);if(A.adapter!=="subprocess"||!Array.isArray(A.argv)||typeof A.cwd!="string"||typeof A.config_origin!="string")throw new Error(`v67 invalid outcome adapter on ${s.delivery_id}`);const m=Y(16).toString("hex"),D=new Date(s.created_at).toISOString(),v=a.get(m,s.owner,s.privacy,`outcome/${s.node_id}`,D);N.run(v.rowid,JSON.stringify({argv:A.argv,cwd:A.cwd,config_origin:A.config_origin,name:s.destination_id}),JSON.stringify(A.payload??null));const b=L.get(v.rowid,e.prepare("SELECT rowid FROM objects WHERE id = ?").get(s.node_id).rowid,D);if(l.run(b.rowid,D),s.message_id===null){if(s.state!=="armed")throw new Error(`v67 delivery ${s.delivery_id} has no message but is ${s.state}`);continue}const f=_.get(s.message_id);if(!f||f.body_json===null)throw new Error(`v67 delivery ${s.delivery_id} has no frozen outcome document`);if(e.prepare("SELECT COUNT(*) AS n FROM deliveries WHERE message_id = ?").get(s.message_id).n!==1)throw new Error(`v67 outcome message ${s.message_id} is shared`);const h=JSON.parse(f.body_json);if(h===null||Array.isArray(h)||typeof h!="object")throw new Error(`v67 invalid outcome body on ${s.delivery_id}`);delete h.action,delete h.delivery,d.run(JSON.stringify(h),s.node_id,s.message_id),r.run(m,s.delivery_id)}const u=e.prepare("SELECT COUNT(*) AS n FROM deliveries WHERE state = 'armed'").get();if(u.n!==i.filter(s=>s.state==="armed").length)throw new Error(`v67 found ${u.n} armed deliveries but only ${i.length} mapped outcome rows`);const p=e.prepare("SELECT COUNT(*) AS n FROM deliveries").get().n-u.n,I=e.prepare("SELECT name, sql FROM sqlite_master WHERE type = 'view' AND sql LIKE '%deliveries%'").all();for(const s of I)e.exec(`DROP VIEW "${s.name.replaceAll('"','""')}"`);e.exec(`
|
|
885
|
-
CREATE TABLE deliveries_new (
|
|
886
|
-
delivery_id TEXT PRIMARY KEY,
|
|
887
|
-
message_id TEXT NOT NULL REFERENCES messages(message_id),
|
|
888
|
-
destination_id TEXT NOT NULL,
|
|
889
|
-
urgency TEXT NOT NULL CHECK (urgency IN ('critical', 'urgent', 'normal', 'deferred')),
|
|
890
|
-
wake INTEGER NOT NULL CHECK (wake IN (0, 1)),
|
|
891
|
-
state TEXT NOT NULL CHECK (state IN ('pending', 'handed_off', 'acknowledged', 'permanent_failed')),
|
|
892
|
-
attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0),
|
|
893
|
-
next_attempt_at INTEGER,
|
|
894
|
-
claimed_at INTEGER,
|
|
895
|
-
claim_owner TEXT,
|
|
896
|
-
acknowledged_at INTEGER,
|
|
897
|
-
failed_at INTEGER,
|
|
898
|
-
last_failure_json TEXT,
|
|
899
|
-
idempotency_key TEXT,
|
|
900
|
-
canceled_at INTEGER,
|
|
901
|
-
created_at INTEGER NOT NULL,
|
|
902
|
-
updated_at INTEGER NOT NULL
|
|
903
|
-
);
|
|
904
|
-
INSERT INTO deliveries_new (rowid, delivery_id, message_id, destination_id, urgency, wake, state,
|
|
905
|
-
attempts, next_attempt_at, claimed_at, claim_owner, acknowledged_at, failed_at,
|
|
906
|
-
last_failure_json, idempotency_key, canceled_at, created_at, updated_at)
|
|
907
|
-
SELECT rowid, delivery_id, message_id, destination_id, urgency, wake, state,
|
|
908
|
-
attempts, next_attempt_at, claimed_at, claim_owner, acknowledged_at, failed_at,
|
|
909
|
-
last_failure_json, idempotency_key, canceled_at, created_at, updated_at
|
|
910
|
-
FROM deliveries WHERE state != 'armed';
|
|
911
|
-
DROP TABLE deliveries;
|
|
912
|
-
ALTER TABLE deliveries_new RENAME TO deliveries;
|
|
913
|
-
CREATE INDEX deliveries_destination ON deliveries(destination_id, wake, state, created_at);
|
|
914
|
-
CREATE INDEX deliveries_due ON deliveries(state, next_attempt_at, created_at, delivery_id)
|
|
915
|
-
WHERE next_attempt_at IS NOT NULL AND canceled_at IS NULL;
|
|
916
|
-
CREATE INDEX deliveries_handed_off ON deliveries(claim_owner) WHERE state = 'handed_off';
|
|
917
|
-
CREATE INDEX deliveries_message ON deliveries(message_id);
|
|
918
|
-
CREATE UNIQUE INDEX deliveries_idempotency ON deliveries(idempotency_key) WHERE idempotency_key IS NOT NULL;
|
|
919
|
-
`);for(const s of I)e.exec(s.sql);const R=e.prepare("SELECT COUNT(*) AS n FROM deliveries").get().n;if(R!==p)throw new Error(`v67 deliveries rebuild lost rows: expected ${p}, got ${R}`)}const t=e.prepare("PRAGMA foreign_key_check").all();if(t.length)throw new Error(`v67 left ${t.length} foreign-key violation(s)`)})}E(Gt,"outcomeActionsOnObjects");function bt(e){e.exec(`
|
|
920
|
-
CREATE TABLE IF NOT EXISTS create_requests (
|
|
921
|
-
request_key TEXT PRIMARY KEY,
|
|
922
|
-
node_id TEXT,
|
|
923
|
-
expires_at INTEGER NOT NULL,
|
|
924
|
-
state TEXT NOT NULL DEFAULT 'reserved' CHECK (state IN ('reserved', 'warm_claimed', 'complete'))
|
|
925
|
-
);
|
|
926
|
-
CREATE INDEX IF NOT EXISTS idx_create_requests_expires ON create_requests(expires_at);
|
|
927
|
-
`)}E(bt,"addCreateRequests");function Bt(e){for(const[r,_]of[["sub","TEXT"],["grant_id","TEXT"],["scopes_json","TEXT"],["origin_run_id","TEXT"],["run_execution_id","TEXT"]])S(e,"crons",r)||e.exec(`ALTER TABLE crons ADD COLUMN ${r} ${_}`);const t=[],n=E(r=>e.prepare("SELECT id, sub, resolved_scopes_json FROM grants WHERE grantee = ? ORDER BY CASE status WHEN 'active' THEN 0 ELSE 1 END, rev DESC LIMIT 1").get(r),"grantRow"),o=E(r=>e.prepare("SELECT grantee, sub, resolved_scopes_json FROM grants WHERE id = ?").get(r),"grantById"),T=E(r=>{try{const _=JSON.parse(r);return Array.isArray(_)?_.flatMap(d=>d&&d.mode==="always"&&typeof d.scope=="string"?[d.scope]:[]):[]}catch{return[]}},"snapshot"),i=E((r,_)=>{if(_===null)return null;const d=e.prepare("SELECT grantee, sub, grant_id, scopes_json, run_id FROM node_rows WHERE node_id = ?").get(_);if(d===void 0||d.grantee!==r)return null;const u=r==="app:terminal";let p=null;if(d.scopes_json!==null){let A;try{A=JSON.parse(d.scopes_json)}catch{return{pause:"bad_scopes"}}if(!Array.isArray(A)||!A.every(m=>typeof m=="string"))return{pause:"bad_scopes"};p=A}const I=u?n(r):void 0;let R=d.grant_id??I?.id??null;R===null&&!u&&d.sub!==null&&(R=e.prepare("SELECT id FROM grants WHERE grantee = ? AND sub = ? ORDER BY CASE status WHEN 'active' THEN 0 ELSE 1 END, rev DESC LIMIT 1").get(r,d.sub)?.id??null);const s=R===null?void 0:o(R);return R!==null&&(s===void 0||s.grantee!==r||d.sub!==null&&s.sub!==d.sub)?{pause:"no_grant"}:R===null&&!u?{pause:"no_grant"}:(p===null&&!u&&(p=T(s.resolved_scopes_json)),{principal:{sub:d.sub??s?.sub??I?.sub??g,grant_id:R,scopes:p,origin_run_id:d.run_id}})},"fromCreator"),a=E(r=>{const _=n(r);return r==="app:terminal"?{sub:_?.sub??g,grant_id:_?.id??null,scopes:null,origin_run_id:null}:_===void 0?null:{sub:_.sub,grant_id:_.id,scopes:T(_.resolved_scopes_json),origin_run_id:null}},"fromGrantee"),N=e.prepare("SELECT cron_id, grantee, created_by, isolated, cwd FROM crons WHERE sub IS NULL").all(),L=e.prepare("UPDATE crons SET sub = ?, grant_id = ?, scopes_json = ?, origin_run_id = ? WHERE cron_id = ?"),l=new Map;for(const r of N){const _=i(r.grantee,r.created_by),d=_===null?a(r.grantee):"principal"in _?_.principal:null;if(d===null){const u=_!==null&&"pause"in _?_.pause:"no_grant";L.run(g,null,null,null,r.cron_id),e.prepare("UPDATE crons SET state = 'paused' WHERE cron_id = ?").run(r.cron_id),t.push({event:`cron.migrated.${u}`,fields:{cron_id:r.cron_id,grantee:r.grantee}})}else l.set(r.cron_id,d),L.run(d.sub,d.grant_id,d.scopes===null?null:JSON.stringify(d.scopes),d.origin_run_id,r.cron_id);if(r.isolated!==0){const u=H(Oe(r.grantee).root,"crons",r.cron_id);e.prepare("UPDATE crons SET cwd = ? WHERE cron_id = ?").run(u,r.cron_id),t.push({event:"cron.migrated.isolated_cwd",fields:{cron_id:r.cron_id,old_cwd:r.cwd,cwd:u}})}}if(C(e,"daemon_actions")){const r=e.prepare("SELECT action_id, adapter_json FROM daemon_actions WHERE state IN ('pending', 'handed_off') AND json_extract(adapter_json, '$.adapter') = 'cron_spawn' AND json_extract(adapter_json, '$.principal') IS NULL").all();for(const _ of r){const d=JSON.parse(_.adapter_json),u=typeof d.cronId=="string"?d.cronId:null,p=u===null?void 0:e.prepare("SELECT grantee, sub, grant_id, scopes_json, origin_run_id, isolated, created_by FROM crons WHERE cron_id = ?").get(u);let I=null;if(p!==void 0&&!(p.grant_id===null&&p.grantee!=="app:terminal"))I={cron_id:u,grantee:p.grantee,sub:p.sub,grant_id:p.grant_id,scopes:p.scopes_json===null?null:JSON.parse(p.scopes_json),origin_run_id:p.origin_run_id,isolated:p.isolated!==0,creator:p.created_by};else if(p===void 0){const R=typeof d.parent=="string"?d.parent:null,s=R===null?void 0:e.prepare("SELECT grantee FROM node_rows WHERE node_id = ?").get(R),A=s===void 0?null:i(s.grantee,R),m=A===null||!("principal"in A)?null:A.principal;s!==void 0&&m!==null&&(I={cron_id:u,grantee:s.grantee,sub:m.sub,grant_id:m.grant_id,scopes:m.scopes,origin_run_id:m.origin_run_id,isolated:d.isolated===!0,creator:R})}if(I!==null)e.prepare("UPDATE daemon_actions SET adapter_json = ? WHERE action_id = ?").run(JSON.stringify({...d,principal:I}),_.action_id);else{const R=Date.now();e.prepare("UPDATE daemon_actions SET state = 'permanent_failed', failed_at = ?, updated_at = ?, claimed_at = NULL, claim_owner = NULL, next_attempt_at = NULL, last_failure_json = ? WHERE action_id = ?").run(R,R,JSON.stringify({kind:"grant_refused",message:"the cron has no derivable principal to authorize this birth"}),_.action_id),t.push({event:"cron.birth.refused",fields:{cron_id:u,action_id:_.action_id,reason:"grant_missing"}})}}}for(const{event:r,fields:_}of t)Ie({level:"warn",event:r,fields:_})}E(Bt,"addCronPrincipal");const M=[Se,De,ge,we,ye,ve,he,Fe,xe,Me,ke,je,He,Q,Ge,Q,be,Be,We,Pe,Ye,Ve,et,tt,nt,Et,ot,it,st,rt,Tt,at,dt,_t,Lt,Nt,ct,lt,ut,e=>{_e(e),Le(e),le(e),ue(e)},Rt,At,pt,Ot,It,mt,Xt,St,Ut,Ct,te,ne,Ee,oe,Dt,gt,ie,wt,fe,yt,vt,ht,xt,Ht,jt,kt,Gt,Mt,e=>e.exec(`
|
|
928
|
-
CREATE TABLE audit_rows (
|
|
929
|
-
id INTEGER PRIMARY KEY, request_id TEXT NOT NULL, time TEXT NOT NULL, grantee TEXT, sub TEXT,
|
|
930
|
-
client_id TEXT, class TEXT, operation TEXT NOT NULL, scope_checked TEXT,
|
|
931
|
-
outcome TEXT NOT NULL, node_id TEXT, run_id TEXT, payer TEXT,
|
|
932
|
-
resource TEXT, initiator_chain TEXT, provider TEXT, tool TEXT,
|
|
933
|
-
jti TEXT, receipt_id TEXT, responder TEXT
|
|
934
|
-
);
|
|
935
|
-
CREATE INDEX audit_rows_grantee_time ON audit_rows(grantee, id DESC);
|
|
936
|
-
CREATE INDEX audit_rows_request ON audit_rows(request_id);
|
|
937
|
-
CREATE INDEX audit_rows_time ON audit_rows(id DESC);
|
|
938
|
-
CREATE TABLE grantee_usage (
|
|
939
|
-
grantee TEXT NOT NULL, month TEXT NOT NULL, usd_equivalent REAL NOT NULL DEFAULT 0,
|
|
940
|
-
PRIMARY KEY (grantee, month)
|
|
941
|
-
);
|
|
942
|
-
CREATE TABLE provider_receipts (
|
|
943
|
-
provider TEXT NOT NULL, receipt_id TEXT NOT NULL, grantee TEXT NOT NULL,
|
|
944
|
-
run_id TEXT, usd_equivalent REAL NOT NULL, received_at TEXT NOT NULL,
|
|
945
|
-
PRIMARY KEY (provider, receipt_id)
|
|
946
|
-
);
|
|
947
|
-
CREATE TABLE provider_approvals (
|
|
948
|
-
question_id TEXT PRIMARY KEY, grantee TEXT NOT NULL, sub TEXT NOT NULL,
|
|
949
|
-
provider TEXT NOT NULL, tool TEXT NOT NULL, version INTEGER,
|
|
950
|
-
args_hash TEXT NOT NULL, request_id TEXT NOT NULL,
|
|
951
|
-
status TEXT NOT NULL CHECK (status IN ('open','answered','consumed','canceled')),
|
|
952
|
-
option_id TEXT
|
|
953
|
-
);
|
|
954
|
-
`),e=>e.exec(`
|
|
955
|
-
ALTER TABLE audit_rows ADD COLUMN question_id TEXT;
|
|
956
|
-
ALTER TABLE provider_approvals ADD COLUMN call_shape TEXT;
|
|
957
|
-
ALTER TABLE provider_approvals ADD COLUMN prepared_files TEXT;
|
|
958
|
-
`),Ft,e=>e.exec(`
|
|
959
|
-
CREATE INDEX idx_human_requests_page_source ON human_requests(
|
|
960
|
-
CASE WHEN json_valid(specification_json) THEN json_extract(specification_json, '$.source.nodeId') END
|
|
961
|
-
) WHERE kind = 'page';
|
|
962
|
-
CREATE INDEX idx_human_requests_page_invalid_spec ON human_requests(created_at, request_id)
|
|
963
|
-
WHERE kind = 'page' AND (NOT json_valid(specification_json)
|
|
964
|
-
OR CASE WHEN json_valid(specification_json) THEN json_type(specification_json) != 'object' END);
|
|
965
|
-
`),e=>e.exec(`
|
|
966
|
-
CREATE INDEX idx_human_requests_page_result_exception ON human_requests(created_at, request_id)
|
|
967
|
-
WHERE kind = 'page' AND result_json IS NOT NULL AND
|
|
968
|
-
CASE WHEN json_valid(result_json) THEN
|
|
969
|
-
json_extract(result_json, '$.kind') = 'page'
|
|
970
|
-
AND json_type(result_json, '$.summary') IS NOT 'text'
|
|
971
|
-
ELSE 1 END;
|
|
972
|
-
`),e=>e.exec(`
|
|
973
|
-
CREATE INDEX idx_nodes_terminal_placement_created ON nodes(created)
|
|
974
|
-
WHERE status IN ('done', 'canceled', 'dead')
|
|
975
|
-
AND (pane IS NOT NULL OR "window" IS NOT NULL OR tmux_session IS NOT NULL);
|
|
976
|
-
`),e=>e.exec(`
|
|
977
|
-
CREATE INDEX idx_nodes_supervision_frozen ON nodes(node_id) WHERE frozen_at IS NOT NULL;
|
|
978
|
-
`),e=>e.exec(`
|
|
979
|
-
ALTER TABLE nodes ADD COLUMN isolated INTEGER NOT NULL DEFAULT 0 CHECK (isolated IN (0, 1));
|
|
980
|
-
ALTER TABLE crons ADD COLUMN isolated INTEGER NOT NULL DEFAULT 0 CHECK (isolated IN (0, 1));
|
|
981
|
-
`),e=>e.exec(`
|
|
982
|
-
ALTER TABLE nodes ADD COLUMN last_turn_error_json TEXT;
|
|
983
|
-
`),e=>e.exec(`
|
|
984
|
-
-- Run confinement (Silas, 2026-09-30): the run of a deleted node of an isolated
|
|
985
|
-
-- run outlives its nodes row, so its kept objects stay confined; and the node
|
|
986
|
-
-- that registered a custom object fixes the run that object reads from.
|
|
987
|
-
CREATE TABLE deleted_isolated_nodes (node_id TEXT PRIMARY KEY, run_id TEXT NOT NULL);
|
|
988
|
-
ALTER TABLE custom_objects ADD COLUMN registered_by TEXT;
|
|
989
|
-
`),ft,bt,e=>e.exec("ALTER TABLE grants ADD COLUMN dependencies_json TEXT NOT NULL DEFAULT '{}'"),e=>e.exec("ALTER TABLE grants ADD COLUMN run_notice_url TEXT"),e=>e.exec(`
|
|
990
|
-
CREATE TABLE usage (
|
|
991
|
-
grantee TEXT NOT NULL, month TEXT NOT NULL, payer TEXT NOT NULL, source TEXT NOT NULL, served_model TEXT NOT NULL,
|
|
992
|
-
input_tokens INTEGER NOT NULL DEFAULT 0, output_tokens INTEGER NOT NULL DEFAULT 0, cached_tokens INTEGER NOT NULL DEFAULT 0,
|
|
993
|
-
usd_equivalent REAL,
|
|
994
|
-
PRIMARY KEY (grantee, month, payer, source, served_model)
|
|
995
|
-
);
|
|
996
|
-
`),Bt,e=>e.exec("ALTER TABLE audit_rows ADD COLUMN via TEXT"),e=>e.exec(`
|
|
997
|
-
ALTER TABLE objects ADD COLUMN home_machine TEXT;
|
|
998
|
-
CREATE TABLE peer_stubs (
|
|
999
|
-
object INTEGER PRIMARY KEY REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
1000
|
-
home_machine TEXT NOT NULL,
|
|
1001
|
-
envelope_json TEXT,
|
|
1002
|
-
created_at TEXT NOT NULL
|
|
1003
|
-
);
|
|
1004
|
-
CREATE TABLE peer_outbox (
|
|
1005
|
-
id INTEGER PRIMARY KEY,
|
|
1006
|
-
peer TEXT NOT NULL,
|
|
1007
|
-
kind TEXT NOT NULL,
|
|
1008
|
-
payload_json TEXT NOT NULL,
|
|
1009
|
-
idempotency_key TEXT NOT NULL UNIQUE,
|
|
1010
|
-
state TEXT NOT NULL CHECK (state IN ('pending','waiting','claimed','done','expired')),
|
|
1011
|
-
attempts INTEGER NOT NULL DEFAULT 0,
|
|
1012
|
-
next_at INTEGER,
|
|
1013
|
-
claimed_by TEXT,
|
|
1014
|
-
claimed_at INTEGER,
|
|
1015
|
-
first_failed_at INTEGER,
|
|
1016
|
-
created_at INTEGER NOT NULL
|
|
1017
|
-
);
|
|
1018
|
-
CREATE INDEX peer_outbox_due ON peer_outbox(next_at) WHERE state = 'pending';
|
|
1019
|
-
CREATE INDEX peer_outbox_peer ON peer_outbox(peer, state);
|
|
1020
|
-
CREATE TABLE peer_locations (
|
|
1021
|
-
id TEXT PRIMARY KEY,
|
|
1022
|
-
machine_id TEXT NOT NULL,
|
|
1023
|
-
learned_at INTEGER NOT NULL
|
|
1024
|
-
);
|
|
1025
|
-
`),e=>e.exec(`
|
|
1026
|
-
CREATE TABLE peer_ingested (
|
|
1027
|
-
key TEXT PRIMARY KEY,
|
|
1028
|
-
ingested_at INTEGER NOT NULL
|
|
1029
|
-
);
|
|
1030
|
-
`),e=>e.exec(`
|
|
1031
|
-
UPDATE nodes SET run_id = NULL
|
|
1032
|
-
WHERE run_id IS NOT NULL AND run_id != node_id AND isolated = 0
|
|
1033
|
-
AND NOT EXISTS (SELECT 1 FROM nodes r WHERE r.node_id = nodes.run_id);
|
|
1034
|
-
`),e=>e.exec(`
|
|
1035
|
-
ALTER TABLE object_kinds ADD COLUMN read_json TEXT;
|
|
1036
|
-
ALTER TABLE object_kinds ADD COLUMN source TEXT NOT NULL DEFAULT 'register' CHECK (source IN ('register','manifest','peer'));
|
|
1037
|
-
ALTER TABLE object_kinds ADD COLUMN declared INTEGER NOT NULL DEFAULT 1;
|
|
1038
|
-
`),e=>e.exec(`
|
|
1039
|
-
CREATE TABLE peer_ingested_destinations (
|
|
1040
|
-
key TEXT PRIMARY KEY,
|
|
1041
|
-
ingested_at INTEGER NOT NULL
|
|
1042
|
-
);
|
|
1043
|
-
`),e=>e.exec("ALTER TABLE watches ADD COLUMN link_app INTEGER REFERENCES objects(rowid) ON DELETE CASCADE"),e=>e.exec(`
|
|
1044
|
-
DROP INDEX objects_named;
|
|
1045
|
-
DROP INDEX objects_handle;
|
|
1046
|
-
CREATE UNIQUE INDEX objects_named ON objects(owner, name)
|
|
1047
|
-
WHERE deleted_at IS NULL AND home_machine IS NULL AND (type = 'document' OR instr(type, '.') > 0);
|
|
1048
|
-
CREATE UNIQUE INDEX objects_handle ON objects(type, name) WHERE home_machine IS NULL AND type IN ('person','app','repo','profile');
|
|
1049
|
-
`),e=>e.exec("ALTER TABLE grants ADD COLUMN inference_endpoint_json TEXT"),e=>e.exec("ALTER TABLE nodes ADD COLUMN reply_at TEXT; ALTER TABLE nodes ADD COLUMN read_at TEXT;"),Pt,Wt,e=>e.exec(`
|
|
1050
|
-
CREATE TABLE IF NOT EXISTS unresolved_links (
|
|
1051
|
-
src INTEGER NOT NULL REFERENCES objects(rowid) ON DELETE CASCADE,
|
|
1052
|
-
name TEXT NOT NULL,
|
|
1053
|
-
PRIMARY KEY (src, name)
|
|
1054
|
-
) WITHOUT ROWID;
|
|
1055
|
-
`),ce,e=>e.exec("ALTER TABLE uploads ADD COLUMN sub TEXT"),e=>e.exec("ALTER TABLE shares ADD COLUMN sub TEXT")];function Wt(e){e.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'waits'").get()===void 0&&e.exec("CREATE TABLE waits (wait_id TEXT PRIMARY KEY, node_id TEXT NOT NULL REFERENCES nodes(node_id) ON DELETE CASCADE, kind TEXT, target_id TEXT, generation INTEGER, created_at INTEGER, satisfied_at INTEGER)");const t=e.prepare("SELECT COUNT(*) AS n FROM waits").get().n;e.exec(`
|
|
1056
|
-
CREATE TABLE waits_new (
|
|
1057
|
-
wait_id TEXT PRIMARY KEY,
|
|
1058
|
-
node_id TEXT NOT NULL REFERENCES nodes(node_id) ON DELETE CASCADE,
|
|
1059
|
-
kind TEXT NOT NULL CHECK (kind IN ('controller', 'object')),
|
|
1060
|
-
target_id TEXT NOT NULL,
|
|
1061
|
-
generation INTEGER NOT NULL,
|
|
1062
|
-
created_at INTEGER NOT NULL,
|
|
1063
|
-
satisfied_at INTEGER
|
|
1064
|
-
);
|
|
1065
|
-
INSERT INTO waits_new (rowid, wait_id, node_id, kind, target_id, generation, created_at, satisfied_at)
|
|
1066
|
-
SELECT rowid, wait_id, node_id, kind, target_id, generation, created_at, satisfied_at FROM waits;
|
|
1067
|
-
DROP TABLE waits;
|
|
1068
|
-
ALTER TABLE waits_new RENAME TO waits;
|
|
1069
|
-
CREATE UNIQUE INDEX waits_live ON waits(node_id) WHERE satisfied_at IS NULL AND kind = 'controller';
|
|
1070
|
-
CREATE UNIQUE INDEX waits_live_object ON waits(node_id, target_id) WHERE satisfied_at IS NULL AND kind = 'object';
|
|
1071
|
-
CREATE INDEX waits_target ON waits(target_id) WHERE satisfied_at IS NULL;
|
|
1072
|
-
CREATE INDEX waits_node ON waits(node_id);
|
|
1073
|
-
`);const n=e.prepare("SELECT COUNT(*) AS n FROM waits").get().n;if(n!==t)throw new Error(`v96 waits rebuild lost rows: expected ${t}, got ${n}`)}E(Wt,"addObjectWaits");function Pt(e){const t=O(e);for(const n of["waiting_for","waiting_for_generation"])t.has(n)&&e.exec(`ALTER TABLE nodes DROP COLUMN ${n}`)}E(Pt,"dropNodeWaitColumns");function W(e,t,n){const o=e.prepare("PRAGMA foreign_keys").get().foreign_keys;e.exec("PRAGMA foreign_keys = OFF;");try{e.exec("BEGIN IMMEDIATE");try{P(e)===t&&(n(),e.exec(`PRAGMA user_version = ${t+1}`)),e.exec("COMMIT")}catch(T){try{e.exec("ROLLBACK")}catch{}throw T}}finally{e.exec(`PRAGMA foreign_keys = ${o};`)}}E(W,"selfTransactionalMigration");const Yt=new Set([3,61,63,66]);function P(e){return e.prepare("PRAGMA user_version").get().user_version}E(P,"currentUserVersion");function re(e,t){M[t](e),e.exec(`PRAGMA user_version = ${t+1}`)}E(re,"applyMigration");function dn(e){const t=P(e);if(t>M.length)throw new Error(`unsupported canvas schema version ${t} (latest supported ${M.length})`);if(t===M.length)return;const n=e.prepare("PRAGMA busy_timeout").get().timeout;e.exec("PRAGMA busy_timeout = 60000;");try{Kt(e)}finally{e.exec(`PRAGMA busy_timeout = ${n};`)}}E(dn,"migrate");function Kt(e){for(;;){const t=P(e);if(t>=M.length)return;if(Yt.has(t)){if(e.isTransaction)throw new Error(`canvas migration v${t+1} requires autocommit`);M[t](e);continue}if(e.isTransaction){re(e,t);continue}e.exec("BEGIN IMMEDIATE");try{P(e)===t&&re(e,t),e.exec("COMMIT")}catch(n){try{e.exec("ROLLBACK")}catch{}throw n}}}E(Kt,"runPendingMigrations");export{M as MIGRATIONS,dn as migrate};
|