toolroll 0.9.129 → 0.9.131
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/CHANGELOG.md +44 -0
- package/dist/agent-fence.js +0 -2
- package/dist/agent-state.js +1 -1
- package/dist/agents-board.js +0 -3
- package/dist/assignment.js +4 -3
- package/dist/backup-ui.js +6 -3
- package/dist/bounded-spawn.js +0 -1
- package/dist/browser/manifest.json +57 -0
- package/dist/browser/terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css.br +0 -0
- package/dist/browser/terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css.gz +0 -0
- package/dist/browser/{terminal.js → terminal.8978c8cbb268e1006f4b0be371c2401b58078e6d9f1380f4a5c2a12da843909f.js} +1 -1
- package/dist/browser/terminal.8978c8cbb268e1006f4b0be371c2401b58078e6d9f1380f4a5c2a12da843909f.js.br +0 -0
- package/dist/browser/terminal.8978c8cbb268e1006f4b0be371c2401b58078e6d9f1380f4a5c2a12da843909f.js.gz +0 -0
- package/dist/browser/{workspace.js → workspace.4f5935dc80574baadf1adafd39a08c567fb1fac1323052ca355546622f9b06d2.js} +25 -25
- package/dist/browser/workspace.4f5935dc80574baadf1adafd39a08c567fb1fac1323052ca355546622f9b06d2.js.br +0 -0
- package/dist/browser/workspace.4f5935dc80574baadf1adafd39a08c567fb1fac1323052ca355546622f9b06d2.js.gz +0 -0
- package/dist/browser/{workspace.css → workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css} +1 -1
- package/dist/browser/workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css.br +0 -0
- package/dist/browser/workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css.gz +0 -0
- package/dist/browser-shell.js +271 -25
- package/dist/builder-restart.js +13 -4
- package/dist/builder.js +4 -1
- package/dist/canvas-freshness.js +122 -0
- package/dist/canvas-fs.js +273 -0
- package/dist/canvas-reads.js +747 -0
- package/dist/canvas-state-key.js +45 -0
- package/dist/canvas-state-worker.js +12 -0
- package/dist/central-recovery-stage.js +3 -3
- package/dist/chat-actions.js +26 -0
- package/dist/chat-channel.js +4 -3
- package/dist/chat-follow.js +4 -0
- package/dist/chat-task-actions.js +1 -1
- package/dist/chat-warmth.js +0 -1
- package/dist/check-report.js +0 -6
- package/dist/commit-extract.js +79 -40
- package/dist/companion/access.js +1 -0
- package/dist/companion/actions.js +79 -16
- package/dist/companion/canvas.js +141 -0
- package/dist/companion/chat-origin.js +92 -0
- package/dist/companion/chat.js +571 -0
- package/dist/companion/daemon.js +46 -5
- package/dist/companion/finish.js +51 -0
- package/dist/companion/reads.js +5 -1
- package/dist/companion/schema.js +64 -0
- package/dist/companion/service.js +1 -0
- package/dist/companion/store.js +1 -0
- package/dist/companion/webauthn.js +11 -2
- package/dist/console-instance.js +0 -4
- package/dist/contracts/console-api.js +1 -1
- package/dist/converse.js +0 -1
- package/dist/coordinator.js +1 -1
- package/dist/db-tuning.js +16 -0
- package/dist/desktop-config.js +156 -0
- package/dist/desktop-host.js +6 -152
- package/dist/desktop-update.js +1 -1
- package/dist/discord.js +9 -3
- package/dist/dispatch.js +32 -4
- package/dist/doctor.js +30 -0
- package/dist/evidence.js +49 -3
- package/dist/exec.js +3 -0
- package/dist/finished-task-sql.js +14 -0
- package/dist/flexible-answers.js +0 -1
- package/dist/git-views.js +7 -1
- package/dist/github-read.js +41 -0
- package/dist/guides.js +4 -3
- package/dist/invoke.js +12 -2
- package/dist/lead-doors.js +50 -3
- package/dist/lead-queue-runtime.js +18 -6
- package/dist/lead-queue.js +25 -0
- package/dist/lead-status.js +3 -3
- package/dist/live-backup-change.js +93 -0
- package/dist/live-backup.js +75 -31
- package/dist/live-bus.js +3 -2
- package/dist/live-records.js +0 -1
- package/dist/local-owner.js +0 -1
- package/dist/loop-recovery.js +40 -0
- package/dist/maintenance.js +29 -1
- package/dist/model-choices.js +0 -11
- package/dist/needs-you.js +2 -0
- package/dist/nightly-check.js +0 -3
- package/dist/nightly-github.js +1 -2
- package/dist/notification-retention.js +119 -0
- package/dist/observations.js +0 -1
- package/dist/operate.js +393 -56
- package/dist/planner.js +4 -1
- package/dist/pull-request-flow.js +56 -15
- package/dist/qr.js +0 -25
- package/dist/query-index-schema.js +10 -0
- package/dist/read-executor-worker.js +9 -7
- package/dist/read-executor.js +18 -1
- package/dist/read-worker-connection.js +7 -0
- package/dist/release-adapters.js +63 -48
- package/dist/release-auto.js +17 -13
- package/dist/release-card.js +0 -4
- package/dist/release-cli.js +58 -10
- package/dist/release-next.js +1 -1
- package/dist/release-stops.js +71 -0
- package/dist/release-supersede.js +125 -0
- package/dist/release-supervise.js +90 -0
- package/dist/release.js +265 -42
- package/dist/restore-retirement.js +4 -1
- package/dist/result-review.js +0 -3
- package/dist/retention-ui.js +1 -1
- package/dist/retention.js +14 -21
- package/dist/review-context.js +0 -5
- package/dist/route-rules.js +1 -5
- package/dist/runner.js +36 -1
- package/dist/sandbox-runtime.js +0 -1
- package/dist/scope.js +1 -13
- package/dist/scout.js +4 -7
- package/dist/serve.js +122 -12
- package/dist/server/kit.js +1 -0
- package/dist/server/live.js +9 -2
- package/dist/server/pages.js +3 -2
- package/dist/server/phone-engine-api.js +22 -2
- package/dist/server/render-pages.js +1 -11
- package/dist/server/render-results.js +0 -1
- package/dist/server/render-tasks.js +0 -68
- package/dist/server/route-table.js +1 -1
- package/dist/server/settings-phone.js +32 -9
- package/dist/server/settings-scripts.js +1 -1
- package/dist/server/settings-section-table.js +0 -3
- package/dist/server/settings-sections.js +4 -2
- package/dist/server/settings.js +20 -5
- package/dist/server/terminals.js +2 -1
- package/dist/shutdown-proof.js +29 -0
- package/dist/slack.js +9 -3
- package/dist/sqlite-telemetry.js +26 -14
- package/dist/statement-cache.js +137 -0
- package/dist/store.js +86 -14
- package/dist/structured-output.js +0 -1
- package/dist/style-asset.js +20 -7
- package/dist/tailnet.js +0 -3
- package/dist/task-control.js +5 -0
- package/dist/task-sizing.js +0 -1
- package/dist/task-status.js +1 -1
- package/dist/teams.js +9 -3
- package/dist/teardown-git.js +10 -1
- package/dist/telegram.js +4 -1
- package/dist/toolroll-update-cli.js +19 -3
- package/dist/toolroll-update-ui.js +2 -2
- package/dist/toolroll-update.js +95 -15
- package/dist/up-processes.js +16 -1
- package/dist/up-role-child.js +2 -0
- package/dist/wake-hub.js +15 -6
- package/dist/work-index.js +101 -44
- package/dist/work-reads.js +53 -9
- package/dist/work-snapshots.js +185 -0
- package/dist/worker-statement-cache.js +83 -0
- package/dist/workspace-revision.js +145 -21
- package/package.json +1 -1
- package/dist/companion/webauthn-test-support.js +0 -44
- package/dist/contracts/contract-test.js +0 -55
- package/dist/unfenced-test-support.js +0 -6
- /package/dist/browser/{terminal.css → terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css} +0 -0
package/dist/work-reads.js
CHANGED
|
@@ -6,6 +6,7 @@ const MAX_SAVED = 50_000;
|
|
|
6
6
|
const FILL_TRIES = 3;
|
|
7
7
|
const scopes = new AsyncLocalStorage();
|
|
8
8
|
const busy = new WeakMap();
|
|
9
|
+
const made = new WeakMap();
|
|
9
10
|
const tableOf = (slot) => `so_work_ranked_${slot}`;
|
|
10
11
|
export function withWorkReads(run) {
|
|
11
12
|
if (scopes.getStore() !== undefined)
|
|
@@ -24,6 +25,33 @@ export function withWorkReads(run) {
|
|
|
24
25
|
close(scope);
|
|
25
26
|
return result;
|
|
26
27
|
}
|
|
28
|
+
export function pageKey(access, options, kind = 'index') {
|
|
29
|
+
const canonical = (record) => Object.keys(record).filter(name => record[name] !== undefined).sort()
|
|
30
|
+
.map(name => { const value = record[name]; return [name, Array.isArray(value) ? [...new Set(value.map(String))].sort() : value]; });
|
|
31
|
+
return JSON.stringify([kind, canonical(access), canonical(options)]);
|
|
32
|
+
}
|
|
33
|
+
export function providePages(store, held) {
|
|
34
|
+
const scope = scopes.getStore();
|
|
35
|
+
if (scope === undefined)
|
|
36
|
+
return;
|
|
37
|
+
scope.pages = { store: savingFor.get(store) ?? store, held, asked: scope.pages?.asked ?? new Map() };
|
|
38
|
+
}
|
|
39
|
+
export function prefetchedPage(store, access, options, kind = 'index') {
|
|
40
|
+
const scope = scopes.getStore();
|
|
41
|
+
if (scope === undefined)
|
|
42
|
+
return undefined;
|
|
43
|
+
const pages = scope.pages ??= { store: savingFor.get(store) ?? store, held: new Map(), asked: new Map() };
|
|
44
|
+
if (pages.store !== (savingFor.get(store) ?? store))
|
|
45
|
+
return undefined;
|
|
46
|
+
const key = pageKey(access, options, kind);
|
|
47
|
+
if (!pages.asked.has(key))
|
|
48
|
+
pages.asked.set(key, { ...(kind === 'index' ? {} : { kind }), access: structuredClone(access), options: structuredClone(options) });
|
|
49
|
+
const held = pages.held.get(key);
|
|
50
|
+
return held === undefined ? undefined : structuredClone(held);
|
|
51
|
+
}
|
|
52
|
+
export function askedPages() {
|
|
53
|
+
return [...(scopes.getStore()?.pages?.asked.values() ?? [])];
|
|
54
|
+
}
|
|
27
55
|
export function outsideWorkReads(run) {
|
|
28
56
|
return scopes.exit(run);
|
|
29
57
|
}
|
|
@@ -40,11 +68,22 @@ function close(scope) {
|
|
|
40
68
|
}
|
|
41
69
|
function release(db, slot) {
|
|
42
70
|
try {
|
|
43
|
-
db
|
|
71
|
+
empty(db, slot);
|
|
44
72
|
busy.get(db)?.delete(slot);
|
|
45
73
|
}
|
|
46
74
|
catch { }
|
|
47
75
|
}
|
|
76
|
+
function empty(db, slot) {
|
|
77
|
+
if (made.get(db)?.has(slot))
|
|
78
|
+
db.exec(`DELETE FROM temp."${tableOf(slot)}"`);
|
|
79
|
+
}
|
|
80
|
+
function discard(db, slot) {
|
|
81
|
+
try {
|
|
82
|
+
db.exec(`DROP TABLE IF EXISTS temp."${tableOf(slot)}"`);
|
|
83
|
+
}
|
|
84
|
+
catch { }
|
|
85
|
+
made.get(db)?.delete(slot);
|
|
86
|
+
}
|
|
48
87
|
const savingFor = new WeakMap();
|
|
49
88
|
export function readerOf(store) {
|
|
50
89
|
if (savingFor.has(store))
|
|
@@ -95,14 +134,17 @@ export function workReadsOf(given) {
|
|
|
95
134
|
if (before === null || before !== current.stamp)
|
|
96
135
|
reset(db, current, before);
|
|
97
136
|
try {
|
|
98
|
-
db
|
|
99
|
-
|
|
137
|
+
empty(db, slot);
|
|
138
|
+
const exists = made.get(db)?.has(slot) === true;
|
|
139
|
+
fill(table, exists);
|
|
140
|
+
if (!exists) {
|
|
141
|
+
const tables = made.get(db) ?? new Set();
|
|
142
|
+
made.set(db, tables);
|
|
143
|
+
tables.add(slot);
|
|
144
|
+
}
|
|
100
145
|
}
|
|
101
146
|
catch {
|
|
102
|
-
|
|
103
|
-
db.exec(`DROP TABLE IF EXISTS temp."${table}"`);
|
|
104
|
-
}
|
|
105
|
-
catch { }
|
|
147
|
+
discard(db, slot);
|
|
106
148
|
return null;
|
|
107
149
|
}
|
|
108
150
|
const after = stampOf(db, current, true);
|
|
@@ -114,9 +156,11 @@ export function workReadsOf(given) {
|
|
|
114
156
|
return table;
|
|
115
157
|
}
|
|
116
158
|
try {
|
|
117
|
-
db
|
|
159
|
+
empty(db, slot);
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
discard(db, slot);
|
|
118
163
|
}
|
|
119
|
-
catch { }
|
|
120
164
|
prove(db, current);
|
|
121
165
|
}
|
|
122
166
|
return null;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { projectSummaryOf } from './console-chrome.js';
|
|
2
|
+
import { workCountsByProject, workIndexPage, workStateRows } from './work-index.js';
|
|
3
|
+
import { pageKey, withWorkReads } from './work-reads.js';
|
|
4
|
+
import { workspaceRevisionOf } from './workspace-revision.js';
|
|
5
|
+
export const SNAPSHOT_MAX_AGE_MS = 5_000;
|
|
6
|
+
export const LAGGED_MS = 2_000;
|
|
7
|
+
const ACTIVE_MS = 10_000;
|
|
8
|
+
export const MAX_KEYS = 64;
|
|
9
|
+
const AGE_AHEAD_MS = 1_500;
|
|
10
|
+
const WATCH_MS = 100;
|
|
11
|
+
export function readSnapshot(store, job) {
|
|
12
|
+
const db = store.handle, now = new Date(job.now);
|
|
13
|
+
db.exec('BEGIN');
|
|
14
|
+
try {
|
|
15
|
+
const revision = workspaceRevisionOf(db);
|
|
16
|
+
const current = revision.current(), expiresAt = revision.expiresAt(now), placement = revision.placement();
|
|
17
|
+
return withWorkReads(() => {
|
|
18
|
+
const chrome = job.chrome;
|
|
19
|
+
const counts = chrome === null ? null : workCountsByProject(store, now, chrome.access);
|
|
20
|
+
const summary = chrome === null ? null : projectSummaryOf(store, now, chrome.access, chrome.actor, chrome.projects, chrome.evidenceRoot ?? undefined);
|
|
21
|
+
const pages = job.pages.map(one => {
|
|
22
|
+
try {
|
|
23
|
+
return one.kind === 'states' ? workStateRows(store, now, one.access) : workIndexPage(store, now, one.access, one.options);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return { revision: current, expiresAt, now: job.now, placement, keys: [], counts, summary, pages };
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
try {
|
|
34
|
+
db.exec('COMMIT');
|
|
35
|
+
}
|
|
36
|
+
catch { }
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export function snapshotKey(job) {
|
|
40
|
+
const chrome = job.chrome === null ? null
|
|
41
|
+
: [job.chrome.actor, pageKey(job.chrome.access, {}), job.chrome.projects.map(one => [one.path, one.name]), job.chrome.evidenceRoot];
|
|
42
|
+
return JSON.stringify([chrome, job.pages.map(one => pageKey(one.access, one.options, one.kind))]);
|
|
43
|
+
}
|
|
44
|
+
const revisionNumber = (revision) => Number(/^v1:(\d+)$/.exec(revision)?.[1] ?? NaN);
|
|
45
|
+
export class WorkSnapshots {
|
|
46
|
+
#kept = new Map();
|
|
47
|
+
#revision;
|
|
48
|
+
#clock;
|
|
49
|
+
#read;
|
|
50
|
+
#watchMs;
|
|
51
|
+
#watch = null;
|
|
52
|
+
#seen = null;
|
|
53
|
+
#closed = false;
|
|
54
|
+
constructor(revision, clock, read, options = {}) {
|
|
55
|
+
this.#revision = revision;
|
|
56
|
+
this.#clock = clock;
|
|
57
|
+
this.#read = read;
|
|
58
|
+
this.#watchMs = options.watchMs ?? WATCH_MS;
|
|
59
|
+
}
|
|
60
|
+
async take(job) {
|
|
61
|
+
if (this.#closed)
|
|
62
|
+
return null;
|
|
63
|
+
const key = snapshotKey(job);
|
|
64
|
+
let kept = this.#kept.get(key);
|
|
65
|
+
if (kept === undefined) {
|
|
66
|
+
if (this.#kept.size >= MAX_KEYS) {
|
|
67
|
+
const idle = [...this.#kept].find(([, one]) => one.flights.length === 0)?.[0];
|
|
68
|
+
if (idle === undefined)
|
|
69
|
+
return null;
|
|
70
|
+
this.#kept.delete(idle);
|
|
71
|
+
}
|
|
72
|
+
kept = { job, latest: null, flights: [], askedAt: 0 };
|
|
73
|
+
}
|
|
74
|
+
else
|
|
75
|
+
this.#kept.delete(key);
|
|
76
|
+
this.#kept.set(key, kept);
|
|
77
|
+
kept.askedAt = Date.now();
|
|
78
|
+
kept.job = job;
|
|
79
|
+
this.#arm();
|
|
80
|
+
const wanted = this.#current();
|
|
81
|
+
if (wanted === null)
|
|
82
|
+
return null;
|
|
83
|
+
if (this.#takes(kept.latest, wanted)) {
|
|
84
|
+
if (revisionNumber(kept.latest.revision) < wanted)
|
|
85
|
+
this.#refresh(kept, wanted);
|
|
86
|
+
return kept.latest;
|
|
87
|
+
}
|
|
88
|
+
for (let wait = 0; wait < 2; wait++) {
|
|
89
|
+
const at = wait === 0 ? wanted : this.#current();
|
|
90
|
+
if (at === null)
|
|
91
|
+
return null;
|
|
92
|
+
const running = [...kept.flights].reverse().find(flight => flight.started >= at - 1);
|
|
93
|
+
const flight = running ?? this.#start(kept, at);
|
|
94
|
+
if (flight === null)
|
|
95
|
+
return null;
|
|
96
|
+
const snapshot = await flight.promise;
|
|
97
|
+
if (snapshot === null)
|
|
98
|
+
return null;
|
|
99
|
+
const now = this.#current();
|
|
100
|
+
if (now !== null && this.#takes(snapshot, now))
|
|
101
|
+
return snapshot;
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
#takes(snapshot, wanted) {
|
|
106
|
+
if (snapshot === null)
|
|
107
|
+
return false;
|
|
108
|
+
const now = this.#clock().getTime(), read = Date.parse(snapshot.now), behind = wanted - revisionNumber(snapshot.revision);
|
|
109
|
+
if (!(behind >= 0 && behind <= 1) || now >= snapshot.expiresAt || now < read || now - read > SNAPSHOT_MAX_AGE_MS)
|
|
110
|
+
return false;
|
|
111
|
+
if (behind === 0)
|
|
112
|
+
return true;
|
|
113
|
+
let placement;
|
|
114
|
+
try {
|
|
115
|
+
placement = this.#revision.placement?.();
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
return now - read <= LAGGED_MS && placement !== undefined && placement === snapshot.placement;
|
|
121
|
+
}
|
|
122
|
+
#current() {
|
|
123
|
+
try {
|
|
124
|
+
const value = revisionNumber(this.#revision.current());
|
|
125
|
+
return Number.isFinite(value) ? value : null;
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
#start(kept, started) {
|
|
132
|
+
if (kept.flights.length >= 2 || this.#closed)
|
|
133
|
+
return null;
|
|
134
|
+
const job = { ...kept.job, now: this.#clock().toISOString() };
|
|
135
|
+
const keys = job.pages.map(one => pageKey(one.access, one.options, one.kind));
|
|
136
|
+
const flight = { started, promise: Promise.resolve(null) };
|
|
137
|
+
flight.promise = this.#read(job).then(read => {
|
|
138
|
+
const snapshot = { ...read, keys };
|
|
139
|
+
const latest = kept.latest;
|
|
140
|
+
if (latest === null || revisionNumber(snapshot.revision) >= revisionNumber(latest.revision))
|
|
141
|
+
kept.latest = snapshot;
|
|
142
|
+
return snapshot;
|
|
143
|
+
}, () => null).finally(() => { kept.flights = kept.flights.filter(one => one !== flight); });
|
|
144
|
+
kept.flights.push(flight);
|
|
145
|
+
return flight;
|
|
146
|
+
}
|
|
147
|
+
#refresh(kept, wanted) {
|
|
148
|
+
if (kept.flights.some(flight => flight.started >= wanted))
|
|
149
|
+
return;
|
|
150
|
+
this.#start(kept, wanted);
|
|
151
|
+
}
|
|
152
|
+
#arm() {
|
|
153
|
+
if (this.#watch !== null || this.#closed || this.#watchMs <= 0)
|
|
154
|
+
return;
|
|
155
|
+
this.#watch = setInterval(() => {
|
|
156
|
+
let revision;
|
|
157
|
+
try {
|
|
158
|
+
revision = this.#revision.current();
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const active = [...this.#kept.values()].filter(kept => Date.now() - kept.askedAt <= ACTIVE_MS);
|
|
164
|
+
if (active.length === 0) {
|
|
165
|
+
clearInterval(this.#watch);
|
|
166
|
+
this.#watch = null;
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const wanted = revisionNumber(revision), moved = revision !== this.#seen, now = this.#clock().getTime();
|
|
170
|
+
this.#seen = revision;
|
|
171
|
+
for (const kept of active)
|
|
172
|
+
if (kept.latest === null || (moved && revisionNumber(kept.latest.revision) < wanted)
|
|
173
|
+
|| now - Date.parse(kept.latest.now) >= SNAPSHOT_MAX_AGE_MS - AGE_AHEAD_MS)
|
|
174
|
+
this.#refresh(kept, wanted);
|
|
175
|
+
}, this.#watchMs);
|
|
176
|
+
this.#watch.unref();
|
|
177
|
+
}
|
|
178
|
+
close() {
|
|
179
|
+
this.#closed = true;
|
|
180
|
+
if (this.#watch !== null)
|
|
181
|
+
clearInterval(this.#watch);
|
|
182
|
+
this.#watch = null;
|
|
183
|
+
this.#kept.clear();
|
|
184
|
+
}
|
|
185
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
export const STATEMENT_CACHE_MAX = 256;
|
|
2
|
+
export const STATEMENT_CACHE_CHARS = 4 * 1024 * 1024;
|
|
3
|
+
export function cacheableRead(sql) {
|
|
4
|
+
const text = sql.replace(/^(?:\s|--[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/)+/, '');
|
|
5
|
+
return /^(?:SELECT|WITH)\b/i.test(text) && !/\btemp\s*\.|\bTEMP\b|so_work_ranked_/i.test(text)
|
|
6
|
+
&& !/\b(?:INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|ALTER|ATTACH|DETACH|PRAGMA|VACUUM)\b/i.test(text);
|
|
7
|
+
}
|
|
8
|
+
export function cacheStatements(db) {
|
|
9
|
+
const kept = new Map();
|
|
10
|
+
let chars = 0, schema = undefined;
|
|
11
|
+
const version = db.prepare('PRAGMA schema_version');
|
|
12
|
+
const recompileIfSchemaMoved = () => {
|
|
13
|
+
const now = version.get()?.['schema_version'];
|
|
14
|
+
if (now === schema)
|
|
15
|
+
return;
|
|
16
|
+
db.prepare('SELECT 1 AS loaded FROM sqlite_master LIMIT 1').get();
|
|
17
|
+
kept.clear();
|
|
18
|
+
chars = 0;
|
|
19
|
+
schema = now;
|
|
20
|
+
};
|
|
21
|
+
const compiled = (sql) => {
|
|
22
|
+
recompileIfSchemaMoved();
|
|
23
|
+
const hit = kept.get(sql);
|
|
24
|
+
if (hit !== undefined) {
|
|
25
|
+
kept.delete(sql);
|
|
26
|
+
kept.set(sql, hit);
|
|
27
|
+
return hit;
|
|
28
|
+
}
|
|
29
|
+
const statement = db.prepare(sql);
|
|
30
|
+
if (sql.length <= STATEMENT_CACHE_CHARS / 4) {
|
|
31
|
+
while (kept.size > 0 && (kept.size >= STATEMENT_CACHE_MAX || chars + sql.length > STATEMENT_CACHE_CHARS)) {
|
|
32
|
+
const oldest = kept.keys().next().value;
|
|
33
|
+
chars -= oldest.length;
|
|
34
|
+
kept.delete(oldest);
|
|
35
|
+
}
|
|
36
|
+
kept.set(sql, statement);
|
|
37
|
+
chars += sql.length;
|
|
38
|
+
}
|
|
39
|
+
return statement;
|
|
40
|
+
};
|
|
41
|
+
const prepare = (sql) => {
|
|
42
|
+
if (!cacheableRead(sql))
|
|
43
|
+
return db.prepare(sql);
|
|
44
|
+
compiled(sql);
|
|
45
|
+
let own = null;
|
|
46
|
+
const current = () => own ?? compiled(sql);
|
|
47
|
+
const methods = {
|
|
48
|
+
get: (...params) => current().get(...params),
|
|
49
|
+
all: (...params) => current().all(...params),
|
|
50
|
+
run: (...params) => current().run(...params),
|
|
51
|
+
};
|
|
52
|
+
return new Proxy(methods, {
|
|
53
|
+
get(target, property) {
|
|
54
|
+
if (own === null && Object.hasOwn(target, property))
|
|
55
|
+
return Reflect.get(target, property);
|
|
56
|
+
own ??= db.prepare(sql);
|
|
57
|
+
if (Object.hasOwn(target, property))
|
|
58
|
+
return Reflect.get(target, property);
|
|
59
|
+
const value = Reflect.get(own, property, own);
|
|
60
|
+
return typeof value === 'function' ? value.bind(own) : value;
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
return new Proxy(db, {
|
|
65
|
+
get(target, property) {
|
|
66
|
+
if (property === 'prepare')
|
|
67
|
+
return prepare;
|
|
68
|
+
if (property === 'exec')
|
|
69
|
+
return (sql) => { try {
|
|
70
|
+
return target.exec(sql);
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
if (!/^\s*(?:SELECT|WITH|BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE)\b/i.test(sql)) {
|
|
74
|
+
kept.clear();
|
|
75
|
+
chars = 0;
|
|
76
|
+
schema = undefined;
|
|
77
|
+
}
|
|
78
|
+
} };
|
|
79
|
+
const value = Reflect.get(target, property, target);
|
|
80
|
+
return typeof value === 'function' ? value.bind(target) : value;
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
@@ -18,8 +18,31 @@ const REVIVAL = {
|
|
|
18
18
|
claim: { rows: 'SELECT rowid AS id, heartbeat_at AS h, expires_at AS e FROM claim WHERE released_at IS NULL', revived: (before, after) => before.e !== null && after.h >= before.e },
|
|
19
19
|
watch_lease: { rows: 'SELECT rowid AS id, heartbeat_at AS h, expires_at AS e FROM watch_lease', revived: (before, after) => before.e !== null && after.h >= before.e },
|
|
20
20
|
};
|
|
21
|
+
const WITNESS_TABLE = 'run_process';
|
|
22
|
+
const WITNESS_FACTS = `SELECT r.outcome IS NULL AS live,
|
|
23
|
+
EXISTS(SELECT 1 FROM run_process p WHERE p.run = r.id) AS witnessed,
|
|
24
|
+
EXISTS(SELECT 1 FROM run_process p WHERE p.run = r.id AND p.exited_at IS NULL AND p.container_empty_at IS NULL) AS unsettled
|
|
25
|
+
FROM run r WHERE r.id = ?`;
|
|
26
|
+
const WITNESS_WRITES = new Map([
|
|
27
|
+
['INSERT INTO run_process (run,host,process_group,observed_at,boot_id,machine_id) VALUES (?,?,?,?,?,?)', { kind: 'run', index: 0 }],
|
|
28
|
+
['INSERT INTO run_process (run,pid,host,process_group,observed_at,boot_id,machine_id) VALUES (?,?,?,?,?,?,?)', { kind: 'run', index: 0 }],
|
|
29
|
+
['UPDATE run_process SET pid = ? WHERE id = ? AND run = ? AND pid IS NULL AND exited_at IS NULL', { kind: 'run', index: 2 }],
|
|
30
|
+
['UPDATE run_process SET containment = ?, container = ?, container_identity = ? WHERE id = ? AND container IS NULL AND exited_at IS NULL', { kind: 'id', index: 3 }],
|
|
31
|
+
['UPDATE run_process SET container_empty_at = ? WHERE id = ? AND container IS NOT NULL AND container_empty_at IS NULL', { kind: 'id', index: 1 }],
|
|
32
|
+
['UPDATE run_process SET exited_at = ? WHERE id = ? AND run = ? AND exited_at IS NULL AND pid IS ? AND host = ? AND process_group = ? AND observed_at = ? AND boot_id IS ? AND containment IS ? AND container IS ? AND container_identity IS ?', { kind: 'run', index: 2 }],
|
|
33
|
+
['UPDATE run_process SET exited_at = ? WHERE id = ? AND run = ? AND exited_at IS NULL AND pid IS ? AND host = ? AND machine_id IS ? AND process_group = ? AND observed_at = ? AND boot_id IS ? AND containment IS ? AND container IS ? AND container_identity IS ?', { kind: 'run', index: 2 }],
|
|
34
|
+
['UPDATE run_process SET exited_at = ? WHERE id = ? AND pid IS NULL AND ((containment IS NULL AND container IS NULL) OR container_empty_at IS NOT NULL)', { kind: 'id', index: 1 }],
|
|
35
|
+
['UPDATE run_process SET exited_at = ? WHERE run = ? AND exited_at IS NULL', { kind: 'run', index: 1 }],
|
|
36
|
+
]);
|
|
37
|
+
export function witnessLocator(sql) {
|
|
38
|
+
return WITNESS_WRITES.get(sql.replace(/\s+/g, ' ').trim()) ?? null;
|
|
39
|
+
}
|
|
21
40
|
const BUMP = `INSERT INTO service_cursor(key,value,updated_at) VALUES ('${KEY}',1,strftime('%Y-%m-%dT%H:%M:%fZ','now'))
|
|
22
41
|
ON CONFLICT(key) DO UPDATE SET value=service_cursor.value+1,updated_at=excluded.updated_at`;
|
|
42
|
+
export const WORKSPACE_PLACEMENT_KEY = 'workspace-placement:v1';
|
|
43
|
+
const PLACEMENT = new Set(['task_ref', 'project']);
|
|
44
|
+
const PLACEMENT_BUMP = `INSERT INTO service_cursor(key,value,updated_at) VALUES ('${WORKSPACE_PLACEMENT_KEY}',1,strftime('%Y-%m-%dT%H:%M:%fZ','now'))
|
|
45
|
+
ON CONFLICT(key) DO UPDATE SET value=service_cursor.value+1,updated_at=excluded.updated_at`;
|
|
23
46
|
const NAME = String.raw `(?:"((?:[^"]|"")+)"|\[([^\]]+)\]|([A-Za-z_][\w$]*))`;
|
|
24
47
|
const TABLE = new RegExp(String.raw `^(?:${NAME}\s*\.\s*)?${NAME}`);
|
|
25
48
|
function tableAt(text) {
|
|
@@ -121,24 +144,29 @@ export function trackWorkspaceWrites(db) {
|
|
|
121
144
|
}
|
|
122
145
|
return effect;
|
|
123
146
|
};
|
|
124
|
-
let open = false, dirty = false, bySavepoint = false;
|
|
147
|
+
let open = false, dirty = false, placed = false, bySavepoint = false;
|
|
125
148
|
let savepoints = [];
|
|
126
149
|
const native = db;
|
|
127
150
|
const inTransaction = () => typeof native.isTransaction === 'boolean' ? native.isTransaction : open;
|
|
128
151
|
let totals = null;
|
|
129
152
|
const total = () => Number((totals ??= db.prepare('SELECT total_changes() AS n')).get()?.['n'] ?? 0);
|
|
130
|
-
const bump = () => {
|
|
153
|
+
const bump = (placement = placed) => {
|
|
131
154
|
try {
|
|
132
|
-
db.exec(BUMP);
|
|
155
|
+
db.exec(placement ? `${BUMP};${PLACEMENT_BUMP}` : BUMP);
|
|
133
156
|
state.bumps++;
|
|
134
157
|
}
|
|
135
158
|
catch { }
|
|
136
159
|
};
|
|
137
|
-
const reset = () => { open = false; dirty = false; bySavepoint = false; savepoints = []; };
|
|
138
|
-
const wrote = () => {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
160
|
+
const reset = () => { open = false; dirty = false; placed = false; bySavepoint = false; savepoints = []; };
|
|
161
|
+
const wrote = (table = '') => {
|
|
162
|
+
const placement = table === '' || PLACEMENT.has(table);
|
|
163
|
+
if (inTransaction()) {
|
|
164
|
+
dirty = true;
|
|
165
|
+
placed ||= placement;
|
|
166
|
+
}
|
|
167
|
+
else
|
|
168
|
+
bump(placement);
|
|
169
|
+
};
|
|
142
170
|
const control = (effect, run) => {
|
|
143
171
|
switch (effect.kind) {
|
|
144
172
|
case 'begin':
|
|
@@ -150,6 +178,7 @@ export function trackWorkspaceWrites(db) {
|
|
|
150
178
|
if (dirty) {
|
|
151
179
|
bump();
|
|
152
180
|
dirty = false;
|
|
181
|
+
placed = false;
|
|
153
182
|
}
|
|
154
183
|
run();
|
|
155
184
|
reset();
|
|
@@ -169,13 +198,14 @@ export function trackWorkspaceWrites(db) {
|
|
|
169
198
|
}
|
|
170
199
|
run();
|
|
171
200
|
open = true;
|
|
172
|
-
savepoints.push({ name: effect.name, dirty });
|
|
201
|
+
savepoints.push({ name: effect.name, dirty, placed });
|
|
173
202
|
return;
|
|
174
203
|
case 'rollback-to': {
|
|
175
204
|
run();
|
|
176
205
|
const at = savepoints.map(one => one.name).lastIndexOf(effect.name);
|
|
177
206
|
if (at >= 0) {
|
|
178
207
|
dirty = savepoints[at].dirty;
|
|
208
|
+
placed = savepoints[at].placed;
|
|
179
209
|
savepoints.length = at + 1;
|
|
180
210
|
}
|
|
181
211
|
return;
|
|
@@ -185,6 +215,7 @@ export function trackWorkspaceWrites(db) {
|
|
|
185
215
|
if (at === 0 && bySavepoint && dirty) {
|
|
186
216
|
bump();
|
|
187
217
|
dirty = false;
|
|
218
|
+
placed = false;
|
|
188
219
|
}
|
|
189
220
|
run();
|
|
190
221
|
if (at >= 0)
|
|
@@ -196,9 +227,75 @@ export function trackWorkspaceWrites(db) {
|
|
|
196
227
|
default: run();
|
|
197
228
|
}
|
|
198
229
|
};
|
|
230
|
+
let witnessFacts = null, witnessRun = null;
|
|
231
|
+
const locators = new Map();
|
|
232
|
+
const factsOf = (run) => {
|
|
233
|
+
try {
|
|
234
|
+
const row = (witnessFacts ??= db.prepare(WITNESS_FACTS)).get(run);
|
|
235
|
+
return row === undefined ? null : `${Number(row['live'])}:${Number(row['witnessed'])}:${Number(row['unsettled'])}`;
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
const witnessOf = (locator, params) => {
|
|
242
|
+
try {
|
|
243
|
+
const given = params[locator.index];
|
|
244
|
+
if (typeof given !== 'number' && typeof given !== 'bigint')
|
|
245
|
+
return null;
|
|
246
|
+
const run = locator.kind === 'run' ? Number(given)
|
|
247
|
+
: Number((witnessRun ??= db.prepare('SELECT run FROM run_process WHERE id = ?')).get(given)?.['run'] ?? NaN);
|
|
248
|
+
if (!Number.isSafeInteger(run))
|
|
249
|
+
return null;
|
|
250
|
+
const facts = factsOf(run);
|
|
251
|
+
return facts === null ? null : { run, facts };
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
const witnessWrite = (sql, params, body) => {
|
|
258
|
+
let locator = locators.get(sql);
|
|
259
|
+
if (locator === undefined) {
|
|
260
|
+
locator = witnessLocator(sql);
|
|
261
|
+
if (locators.size >= 256)
|
|
262
|
+
locators.clear();
|
|
263
|
+
locators.set(sql, locator);
|
|
264
|
+
}
|
|
265
|
+
const before = locator === null ? null : witnessOf(locator, params);
|
|
266
|
+
if (locator === null || before === null || !before.facts.startsWith('1:'))
|
|
267
|
+
return measured({ kind: 'write', table: WITNESS_TABLE, quiet: false }, body);
|
|
268
|
+
const changes = total();
|
|
269
|
+
const result = body();
|
|
270
|
+
if (total() === changes)
|
|
271
|
+
return result;
|
|
272
|
+
if (factsOf(before.run) !== before.facts)
|
|
273
|
+
wrote(WITNESS_TABLE);
|
|
274
|
+
return result;
|
|
275
|
+
};
|
|
276
|
+
const atomically = (body) => {
|
|
277
|
+
db.exec('BEGIN IMMEDIATE');
|
|
278
|
+
try {
|
|
279
|
+
const changes = total();
|
|
280
|
+
const result = body();
|
|
281
|
+
if (total() !== changes)
|
|
282
|
+
bump(true);
|
|
283
|
+
db.exec('COMMIT');
|
|
284
|
+
return result;
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
try {
|
|
288
|
+
db.exec('ROLLBACK');
|
|
289
|
+
}
|
|
290
|
+
catch { }
|
|
291
|
+
throw error;
|
|
292
|
+
}
|
|
293
|
+
};
|
|
199
294
|
const measured = (effect, body) => {
|
|
200
295
|
if (effect.kind !== 'write')
|
|
201
296
|
return body();
|
|
297
|
+
if (!effect.quiet && (effect.table === '' || PLACEMENT.has(effect.table)) && !inTransaction())
|
|
298
|
+
return atomically(body);
|
|
202
299
|
const revival = effect.quiet ? REVIVAL[effect.table] : undefined;
|
|
203
300
|
const beats = () => new Map(db.prepare(revival.rows).all()
|
|
204
301
|
.map(row => [Number(row['id']), { h: String(row['h']), e: row['e'] == null ? null : String(row['e']) }]));
|
|
@@ -208,12 +305,12 @@ export function trackWorkspaceWrites(db) {
|
|
|
208
305
|
if (total() === changes)
|
|
209
306
|
return result;
|
|
210
307
|
if (!effect.quiet)
|
|
211
|
-
wrote();
|
|
308
|
+
wrote(effect.table);
|
|
212
309
|
else if (before !== null) {
|
|
213
310
|
for (const [id, after] of beats()) {
|
|
214
311
|
const was = before.get(id);
|
|
215
312
|
if (was !== undefined && was.h !== after.h && revival.revived(was, after)) {
|
|
216
|
-
wrote();
|
|
313
|
+
wrote(effect.table);
|
|
217
314
|
break;
|
|
218
315
|
}
|
|
219
316
|
}
|
|
@@ -222,9 +319,20 @@ export function trackWorkspaceWrites(db) {
|
|
|
222
319
|
};
|
|
223
320
|
const methods = {
|
|
224
321
|
exec(sql) {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
322
|
+
if (/;\s*\S/.test(sql.replace(/^(?:\s|--[^\n]*(?:\n|$)|\/\*[\s\S]*?\*\/)+/, ''))) {
|
|
323
|
+
const changes = total();
|
|
324
|
+
db.exec(sql);
|
|
325
|
+
if (total() === changes)
|
|
326
|
+
return;
|
|
327
|
+
if (inTransaction()) {
|
|
328
|
+
dirty = true;
|
|
329
|
+
placed = true;
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
bump(true);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const effect = effectOf(sql);
|
|
228
336
|
if (effect.kind === 'write' || effect.kind === 'none')
|
|
229
337
|
measured(effect, () => db.exec(sql));
|
|
230
338
|
else
|
|
@@ -236,7 +344,9 @@ export function trackWorkspaceWrites(db) {
|
|
|
236
344
|
return statement;
|
|
237
345
|
const run = (body) => effect.kind === 'write' ? measured(effect, body) : (control(effect, () => { body(); }), undefined);
|
|
238
346
|
return passThrough(statement, {
|
|
239
|
-
run: (...params) => effect.kind
|
|
347
|
+
run: (...params) => effect.kind !== 'write' ? runControl(effect, control, () => statement.run(...params))
|
|
348
|
+
: effect.table === WITNESS_TABLE && !effect.quiet ? witnessWrite(sql, params, () => statement.run(...params))
|
|
349
|
+
: measured(effect, () => statement.run(...params)),
|
|
240
350
|
get: (...params) => run(() => statement.get(...params)),
|
|
241
351
|
all: (...params) => run(() => statement.all(...params)),
|
|
242
352
|
});
|
|
@@ -274,10 +384,24 @@ export class WorkspaceValidatorCache {
|
|
|
274
384
|
export function prepareWorkspaceRevision(store) {
|
|
275
385
|
const db = store.raw();
|
|
276
386
|
db.prepare('INSERT OR IGNORE INTO service_cursor(key,value,updated_at) VALUES (?,0,?)').run(KEY, new Date().toISOString());
|
|
277
|
-
const cursor = db.prepare('SELECT CAST(value AS TEXT) AS value FROM service_cursor WHERE key = ?');
|
|
278
387
|
const dataVersion = db.prepare('PRAGMA data_version');
|
|
279
388
|
const counted = db[BUMPS];
|
|
389
|
+
const read = workspaceRevisionOf(db);
|
|
280
390
|
let seen = null;
|
|
391
|
+
const latest = () => {
|
|
392
|
+
const data = dataVersion.get()?.['data_version'], bumps = counted?.() ?? -1;
|
|
393
|
+
if (seen !== null && counted !== undefined && seen.data === data && seen.bumps === bumps)
|
|
394
|
+
return seen;
|
|
395
|
+
return seen = { data, bumps, revision: read.current(), placement: read.placement() };
|
|
396
|
+
};
|
|
397
|
+
return {
|
|
398
|
+
current: () => latest().revision,
|
|
399
|
+
placement: () => latest().placement,
|
|
400
|
+
expiresAt: now => read.expiresAt(now),
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
export function workspaceRevisionOf(db) {
|
|
404
|
+
const cursor = db.prepare('SELECT CAST(value AS TEXT) AS value FROM service_cursor WHERE key = ?');
|
|
281
405
|
const boundary = db.prepare(`SELECT MIN(at) AS at FROM (
|
|
282
406
|
SELECT MIN(expires_at) AS at FROM claim WHERE released_at IS NULL AND expires_at > ?
|
|
283
407
|
UNION ALL SELECT MIN(until) FROM hold WHERE until > ?
|
|
@@ -289,15 +413,15 @@ export function prepareWorkspaceRevision(store) {
|
|
|
289
413
|
UNION ALL SELECT MIN(reset_at) FROM quota WHERE reset_at > ?
|
|
290
414
|
)`);
|
|
291
415
|
return {
|
|
416
|
+
placement() {
|
|
417
|
+
const value = cursor.get(WORKSPACE_PLACEMENT_KEY)?.['value'];
|
|
418
|
+
return `p${typeof value === 'string' && /^\d+$/.test(value) ? value : '0'}`;
|
|
419
|
+
},
|
|
292
420
|
current() {
|
|
293
|
-
const data = dataVersion.get()?.['data_version'], bumps = counted?.() ?? -1;
|
|
294
|
-
if (seen !== null && counted !== undefined && seen.data === data && seen.bumps === bumps)
|
|
295
|
-
return seen.revision;
|
|
296
421
|
const value = cursor.get(KEY)?.['value'];
|
|
297
422
|
if (typeof value !== 'string' || !/^\d+$/.test(value))
|
|
298
423
|
throw new Error('Workspace revision is unavailable');
|
|
299
|
-
|
|
300
|
-
return seen.revision;
|
|
424
|
+
return `v1:${value}`;
|
|
301
425
|
},
|
|
302
426
|
expiresAt(now) {
|
|
303
427
|
const iso = now.toISOString();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "toolroll",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.131",
|
|
4
4
|
"description": "Your agents build. You decide. A control plane for coding agents — Claude Code, Codex, Gemini: approve the exact scope once, get results back Ready with the diff, the passing check and screenshots. Runs on your machine.",
|
|
5
5
|
"directories": {
|
|
6
6
|
"doc": "docs"
|