toolroll 0.9.116 → 0.9.117
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 +36 -0
- package/README.md +43 -26
- package/dist/acp-agents.js +14 -1
- package/dist/acp-client.js +19 -1
- package/dist/agent-change-check.js +60 -0
- package/dist/agent-change-schema.js +19 -0
- package/dist/agent-detect.js +11 -1
- package/dist/agent-fence.js +39 -10
- package/dist/agent-network-request.js +108 -0
- package/dist/agent-requests.js +105 -0
- package/dist/approval-summary.js +3 -3
- package/dist/backup.js +17 -1
- package/dist/batch-checks.js +81 -18
- package/dist/browser/terminal.js +41 -41
- package/dist/browser/workspace.css +2 -2
- package/dist/browser/workspace.js +36 -36
- package/dist/builder.js +3 -1
- package/dist/candidate-materialize.js +119 -0
- package/dist/central-enrollment-contract.js +156 -0
- package/dist/central-recovery-stage.js +329 -0
- package/dist/chat-actions.js +101 -2
- package/dist/chat-channel.js +40 -0
- package/dist/check-only-standing.js +13 -0
- package/dist/check-only.js +25 -13
- package/dist/check-recovery-hold.js +199 -0
- package/dist/check-recovery-index-schema.js +3 -0
- package/dist/check-screenshots.js +83 -0
- package/dist/cli.js +125 -6
- package/dist/codex-limits.js +4 -0
- package/dist/connect-cli.js +257 -11
- package/dist/contracts/chat-actions.js +4 -0
- package/dist/contracts/console-api.js +14 -3
- package/dist/contracts/engine-api.js +331 -2
- package/dist/contracts/lead-tools.js +56 -2
- package/dist/contracts/provider.js +1 -1
- package/dist/contracts/route.js +1 -1
- package/dist/contracts/terminal-connection.js +50 -0
- package/dist/converse.js +80 -4
- package/dist/device-pair.js +280 -0
- package/dist/direct-chat-artifacts.js +347 -0
- package/dist/direct-chat-catalog.js +311 -0
- package/dist/direct-chat-fence.js +96 -0
- package/dist/direct-chat-git.js +343 -0
- package/dist/direct-chat-laptop.js +19 -0
- package/dist/direct-chat-schema.js +181 -0
- package/dist/direct-chat-ship.js +861 -0
- package/dist/direct-chat-workspace.js +389 -0
- package/dist/direct-chat.js +2107 -0
- package/dist/doctor.js +1 -1
- package/dist/easy-start.js +18 -0
- package/dist/egress.js +25 -4
- package/dist/engine-chat.js +454 -0
- package/dist/engine-reads.js +141 -13
- package/dist/exec.js +4 -3
- package/dist/flexible-agents.js +8 -16
- package/dist/flexible-session.js +91 -47
- package/dist/flexible-ship.js +87 -7
- package/dist/flexible-start.js +4 -1
- package/dist/flexible-thread.js +2 -3
- package/dist/flexible-turns.js +26 -5
- package/dist/flow-triggers.js +13 -4
- package/dist/flows-ui.js +54 -20
- package/dist/handoff-cli.js +630 -0
- package/dist/invoke.js +55 -13
- package/dist/lead-contract.js +5 -4
- package/dist/lead-doors.js +80 -1
- package/dist/lead-progress.js +9 -0
- package/dist/lead-tools.js +681 -10
- package/dist/lead.js +37 -9
- package/dist/live-bus.js +11 -0
- package/dist/live-terminal.js +132 -12
- package/dist/local-handoff.js +363 -0
- package/dist/local-owner-acts.js +264 -2
- package/dist/local-owner.js +16 -1
- package/dist/multiplayer-handoff.js +528 -0
- package/dist/multiplayer-presence.js +471 -0
- package/dist/names.js +17 -4
- package/dist/needs-you.js +10 -0
- package/dist/operate.js +280 -19
- package/dist/policy.js +2 -2
- package/dist/project-delete-ui.js +18 -11
- package/dist/project-delete.js +65 -4
- package/dist/project-tools.js +1 -1
- package/dist/prompt.js +1 -1
- package/dist/provider-connection.js +2 -0
- package/dist/provider-home.js +251 -8
- package/dist/provider.js +3 -2
- package/dist/publish.js +119 -64
- package/dist/pull-request-flow.js +35 -14
- package/dist/read-executor-worker.js +1 -1
- package/dist/read-executor.js +95 -1
- package/dist/restore-retirement.js +152 -0
- package/dist/result-follow-ups.js +116 -37
- package/dist/route-change.js +44 -4
- package/dist/scope.js +62 -5
- package/dist/serve.js +49 -5
- package/dist/server/chat.js +94 -13
- package/dist/server/direct-chat.js +583 -0
- package/dist/server/engine-api.js +219 -21
- package/dist/server/flows.js +43 -10
- package/dist/server/found-repos.js +25 -0
- package/dist/server/guards.js +20 -6
- package/dist/server/live.js +81 -23
- package/dist/server/pages.js +67 -13
- package/dist/server/people-tokens.js +47 -6
- package/dist/server/remote.js +29 -0
- package/dist/server/render-results.js +6 -2
- package/dist/server/render-settings.js +4 -2
- package/dist/server/render-tasks.js +18 -17
- package/dist/server/route-table.js +69 -2
- package/dist/server/settings.js +22 -7
- package/dist/server/task-views.js +15 -3
- package/dist/server/tasks.js +39 -2
- package/dist/server/terminals.js +185 -11
- package/dist/settings-proofs.js +1 -1
- package/dist/store.js +499 -56
- package/dist/subscription-chat.js +33 -6
- package/dist/subscription-usage.js +233 -0
- package/dist/task-live.js +2 -1
- package/dist/teardown-git.js +34 -4
- package/dist/terminal-connection.js +159 -0
- package/dist/tui/darwin-arm64/toolroll-tui +0 -0
- package/dist/tui/darwin-x64/toolroll-tui +0 -0
- package/dist/tui/linux-arm64/toolroll-tui +0 -0
- package/dist/tui/linux-x64/toolroll-tui +0 -0
- package/dist/tui-contract.js +1 -1
- package/dist/tui-launch.js +32 -1
- package/dist/workspace-identity-schema.js +21 -0
- package/package.json +3 -1
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { resultRunsOf } from "./engine-reads.js";
|
|
4
|
+
import { TERMINAL_PROGRAMS, TerminalError } from "./live-terminal.js";
|
|
5
|
+
import { remoteAllows, reproveRemote } from "./operate-remote.js";
|
|
6
|
+
import { projectName, rowVisible } from "./project.js";
|
|
7
|
+
import { teardownGit } from "./teardown-git.js";
|
|
8
|
+
import { admitRemoteTerminalRequest, isRemoteTerminalRequest } from "./terminal-connection.js";
|
|
9
|
+
const APPROVAL_PING_PATH = /^\/api\/handoff\/task\/([^/]+)\/approval-ping$/;
|
|
10
|
+
const PING_DEDUPE_WINDOW_MS = 60_000;
|
|
11
|
+
function sendJson(response, status, body, extraHeaders = {}) {
|
|
12
|
+
const payload = JSON.stringify(body);
|
|
13
|
+
response.writeHead(status, {
|
|
14
|
+
"content-type": "application/json; charset=utf-8",
|
|
15
|
+
"cache-control": "no-store",
|
|
16
|
+
"content-length": String(Buffer.byteLength(payload)),
|
|
17
|
+
...extraHeaders,
|
|
18
|
+
});
|
|
19
|
+
response.end(payload);
|
|
20
|
+
}
|
|
21
|
+
function taskOwnersFromStore(store, canSeeRepo) {
|
|
22
|
+
const rows = store.handle.prepare(`
|
|
23
|
+
SELECT r.external_id AS task_id, r.repo AS repo, a.act AS act, a.account AS account, a.person AS person
|
|
24
|
+
FROM task_act a
|
|
25
|
+
JOIN task_ref r ON r.id = a.task_ref
|
|
26
|
+
WHERE a.id IN (
|
|
27
|
+
SELECT MAX(id) FROM task_act WHERE act IN ('filed', 'approved', 'asked') GROUP BY task_ref
|
|
28
|
+
) AND r.repo IS NOT NULL
|
|
29
|
+
`).all();
|
|
30
|
+
const owners = {};
|
|
31
|
+
for (const row of rows) {
|
|
32
|
+
const repo = typeof row["repo"] === "string" ? row["repo"] : null;
|
|
33
|
+
if (repo === null || !canSeeRepo(repo))
|
|
34
|
+
continue;
|
|
35
|
+
const taskId = String(row["task_id"]);
|
|
36
|
+
const act = String(row["act"]);
|
|
37
|
+
const owner = act === "asked"
|
|
38
|
+
? (typeof row["person"] === "string" && row["person"] !== "" ? row["person"] : null)
|
|
39
|
+
: (typeof row["account"] === "string" && row["account"] !== "" ? row["account"] : null);
|
|
40
|
+
owners[taskId] = owner;
|
|
41
|
+
}
|
|
42
|
+
return owners;
|
|
43
|
+
}
|
|
44
|
+
function pairedChannelsFor(store, account) {
|
|
45
|
+
const channels = [];
|
|
46
|
+
const pushRow = store.handle.prepare("SELECT COUNT(*) AS n FROM push_subscription WHERE approver = ? AND retired_at IS NULL").get(account);
|
|
47
|
+
if (Number(pushRow?.["n"] ?? 0) > 0)
|
|
48
|
+
channels.push("web-push");
|
|
49
|
+
try {
|
|
50
|
+
const bindings = store.handle.prepare("SELECT DISTINCT provider FROM chat_binding WHERE approver = ? AND revoked IS NULL ORDER BY provider").all(account);
|
|
51
|
+
for (const row of bindings) {
|
|
52
|
+
const provider = typeof row["provider"] === "string" ? row["provider"] : "";
|
|
53
|
+
if (provider !== "" && !channels.includes(provider))
|
|
54
|
+
channels.push(provider);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
}
|
|
59
|
+
return channels;
|
|
60
|
+
}
|
|
61
|
+
function isWaitingOnApproval(store, family) {
|
|
62
|
+
const ref = store.lookupRef(family.current.id);
|
|
63
|
+
if (ref?.plan === "drafted")
|
|
64
|
+
return true;
|
|
65
|
+
if (family.current.state === "queued") {
|
|
66
|
+
const scope = store.getScope(family.current.id);
|
|
67
|
+
if (scope !== null && (scope.approvedAt === null || scope.approvedDigest !== scope.digest))
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
const refIds = family.versions.map(v => v.refId);
|
|
71
|
+
if (refIds.length === 0)
|
|
72
|
+
return false;
|
|
73
|
+
const placeholders = refIds.map(() => "?").join(", ");
|
|
74
|
+
const pending = store.handle.prepare(`
|
|
75
|
+
SELECT 1 AS hit FROM decision d
|
|
76
|
+
JOIN run r ON r.id = d.run
|
|
77
|
+
WHERE r.task_ref IN (${placeholders}) AND d.state = 'pending'
|
|
78
|
+
LIMIT 1
|
|
79
|
+
`).get(...refIds);
|
|
80
|
+
return pending !== undefined;
|
|
81
|
+
}
|
|
82
|
+
function admitRemoteToken(request, response, store, clock, authenticateBearer, admitBearer) {
|
|
83
|
+
const principal = authenticateBearer(request);
|
|
84
|
+
const admitted = admitRemoteTerminalRequest(request, store, principal ?? undefined, clock);
|
|
85
|
+
if (!admitted.ok) {
|
|
86
|
+
sendJson(response, admitted.status, { ok: false, error: admitted.error, message: `Remote terminal rejected (${admitted.error}).` });
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
const budget = admitBearer(principal);
|
|
90
|
+
if (!budget.ok) {
|
|
91
|
+
const headers = {};
|
|
92
|
+
if (budget.status !== 503)
|
|
93
|
+
headers["retry-after"] = String(budget.retryAfter);
|
|
94
|
+
sendJson(response, budget.status === 503 ? 503 : 429, { ok: false, error: "rate-limited", message: "Too many requests. Try again shortly." }, headers);
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
return { principal: principal, scope: admitted.scope };
|
|
98
|
+
}
|
|
99
|
+
export function handlePresenceHttp(request, response, options) {
|
|
100
|
+
if (request.method !== "GET") {
|
|
101
|
+
return sendJson(response, 405, { ok: false, error: "method_not_allowed" }, { allow: "GET" });
|
|
102
|
+
}
|
|
103
|
+
const { store, clock, liveCeiling, taskRooms, terminalHost, identify, authenticateBearer, admitBearer } = options;
|
|
104
|
+
let me;
|
|
105
|
+
let bearerPrincipal = null;
|
|
106
|
+
if (isRemoteTerminalRequest(request)) {
|
|
107
|
+
const auth = admitRemoteToken(request, response, store, clock, authenticateBearer, admitBearer);
|
|
108
|
+
if (auth === null)
|
|
109
|
+
return;
|
|
110
|
+
me = auth.principal.account;
|
|
111
|
+
bearerPrincipal = auth.principal;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
const who = identify(request, false);
|
|
115
|
+
if (who === null || who.via !== "cookie") {
|
|
116
|
+
return sendJson(response, 401, { ok: false, error: "unauthenticated", message: "Sign in to view presence." });
|
|
117
|
+
}
|
|
118
|
+
const account = store.accountOf(who.name);
|
|
119
|
+
if (account === null || account.revokedAt !== null || account.generation !== who.session.generation) {
|
|
120
|
+
return sendJson(response, 401, { ok: false, error: "unauthenticated", message: "Your session has ended." });
|
|
121
|
+
}
|
|
122
|
+
me = who.name;
|
|
123
|
+
}
|
|
124
|
+
const ceiling = liveCeiling();
|
|
125
|
+
const canSeeRepo = (repo) => {
|
|
126
|
+
if (!rowVisible(ceiling, repo))
|
|
127
|
+
return false;
|
|
128
|
+
if (!store.accountCanAccess(me, repo))
|
|
129
|
+
return false;
|
|
130
|
+
if (bearerPrincipal !== null && !remoteAllows(store, bearerPrincipal, repo))
|
|
131
|
+
return false;
|
|
132
|
+
return true;
|
|
133
|
+
};
|
|
134
|
+
const tasks = {};
|
|
135
|
+
const ensure = (taskId) => {
|
|
136
|
+
let entry = tasks[taskId];
|
|
137
|
+
if (entry === undefined) {
|
|
138
|
+
entry = { viewers: [], terminalController: null, terminalProgram: null, terminalViewers: 0 };
|
|
139
|
+
tasks[taskId] = entry;
|
|
140
|
+
}
|
|
141
|
+
return entry;
|
|
142
|
+
};
|
|
143
|
+
for (const [rootId, viewers] of taskRooms.snapshot()) {
|
|
144
|
+
const family = store.taskFamilyOf(rootId, null, false);
|
|
145
|
+
const repo = family?.root.repo ?? family?.current.repo ?? null;
|
|
146
|
+
if (family === null || family.problem !== null || repo === null || !canSeeRepo(repo))
|
|
147
|
+
continue;
|
|
148
|
+
const others = viewers.filter(name => name !== me);
|
|
149
|
+
if (others.length === 0)
|
|
150
|
+
continue;
|
|
151
|
+
for (const id of new Set([family.root.id, family.current.id])) {
|
|
152
|
+
const entry = ensure(id);
|
|
153
|
+
entry.viewers = [...new Set([...entry.viewers, ...others])].sort((a, b) => a.localeCompare(b));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const host = terminalHost();
|
|
157
|
+
if (host !== null) {
|
|
158
|
+
for (const session of host.activePresence()) {
|
|
159
|
+
if (session.taskId === null || !canSeeRepo(session.repo))
|
|
160
|
+
continue;
|
|
161
|
+
const family = store.taskFamilyOf(session.taskId, null, false);
|
|
162
|
+
const ids = family === null || family.problem !== null
|
|
163
|
+
? [session.taskId]
|
|
164
|
+
: [...new Set([session.taskId, family.root.id, family.current.id])];
|
|
165
|
+
for (const id of ids) {
|
|
166
|
+
const entry = ensure(id);
|
|
167
|
+
if (entry.terminalController === null && (session.controller !== null || entry.terminalProgram === null)) {
|
|
168
|
+
entry.terminalController = session.controller;
|
|
169
|
+
entry.terminalProgram = session.program;
|
|
170
|
+
entry.terminalViewers = session.viewers.length;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const owners = taskOwnersFromStore(store, canSeeRepo);
|
|
176
|
+
const accounts = store.handle.prepare("SELECT name FROM approver WHERE revoked_at IS NULL ORDER BY name").all().map(row => String(row["name"]));
|
|
177
|
+
if (bearerPrincipal !== null && !reproveRemote(store, bearerPrincipal, clock()).ok) {
|
|
178
|
+
return sendJson(response, 401, { ok: false, error: "unauthenticated", message: "Your access changed." });
|
|
179
|
+
}
|
|
180
|
+
const body = { ok: true, me, tasks, owners, accounts };
|
|
181
|
+
return sendJson(response, 200, body);
|
|
182
|
+
}
|
|
183
|
+
export function handleApprovalPingHttp(request, response, url, options) {
|
|
184
|
+
if (request.method !== "POST") {
|
|
185
|
+
return sendJson(response, 405, { ok: false, error: "method_not_allowed" }, { allow: "POST" });
|
|
186
|
+
}
|
|
187
|
+
const match = APPROVAL_PING_PATH.exec(url.pathname);
|
|
188
|
+
if (match === null) {
|
|
189
|
+
return sendJson(response, 404, { ok: false, error: "not_found", message: "Unknown approval ping route." });
|
|
190
|
+
}
|
|
191
|
+
let taskId;
|
|
192
|
+
try {
|
|
193
|
+
taskId = decodeURIComponent(match[1]);
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
return sendJson(response, 400, { ok: false, error: "bad_task", message: "Malformed task identifier." });
|
|
197
|
+
}
|
|
198
|
+
const { store, clock, liveCeiling, consoleOrigin, authenticateBearer, admitBearer } = options;
|
|
199
|
+
const auth = admitRemoteToken(request, response, store, clock, authenticateBearer, admitBearer);
|
|
200
|
+
if (auth === null)
|
|
201
|
+
return;
|
|
202
|
+
if (auth.scope !== "act") {
|
|
203
|
+
return sendJson(response, 403, { ok: false, error: "read_only_token", message: "This token is read-only and cannot trigger approval pings." });
|
|
204
|
+
}
|
|
205
|
+
const account = store.accountOf(auth.principal.account);
|
|
206
|
+
if (account === null || account.revokedAt !== null || account.role !== "approver") {
|
|
207
|
+
return sendJson(response, 403, { ok: false, error: "approver_required", message: "Only an approver can request an approval ping." });
|
|
208
|
+
}
|
|
209
|
+
const allowedRepos = auth.principal.projects === null
|
|
210
|
+
? null
|
|
211
|
+
: auth.principal.projects.filter(repo => store.accountCanAccess(auth.principal.account, repo));
|
|
212
|
+
const family = store.taskFamilyOf(taskId, allowedRepos, false);
|
|
213
|
+
const repo = family?.root.repo ?? family?.current.repo ?? null;
|
|
214
|
+
if (family === null || family.problem !== null || repo === null || !rowVisible(liveCeiling(), repo) || !remoteAllows(store, auth.principal, repo)) {
|
|
215
|
+
return sendJson(response, 404, { ok: false, error: "not_found", message: "Task not found or outside your access." });
|
|
216
|
+
}
|
|
217
|
+
if (!isWaitingOnApproval(store, family)) {
|
|
218
|
+
return sendJson(response, 409, { ok: false, error: "not_waiting", message: "This task is not waiting on approval right now." });
|
|
219
|
+
}
|
|
220
|
+
const now = clock();
|
|
221
|
+
if (!reproveRemote(store, auth.principal, now).ok) {
|
|
222
|
+
return sendJson(response, 401, { ok: false, error: "unauthenticated", message: "Your access changed." });
|
|
223
|
+
}
|
|
224
|
+
const bucket = Math.floor(now.getTime() / PING_DEDUPE_WINDOW_MS);
|
|
225
|
+
const enqueued = store.enqueueNotification({
|
|
226
|
+
dedupeKey: `approval-ping:${family.current.refId}:${auth.principal.account}:${bucket}`,
|
|
227
|
+
kind: "approval-ping",
|
|
228
|
+
pushClass: "decision",
|
|
229
|
+
recipient: auth.principal.account,
|
|
230
|
+
subject: `Approval requested from remote terminal: ${family.current.title.slice(0, 80)}`,
|
|
231
|
+
body: "Open this task in Toolroll to review and approve.",
|
|
232
|
+
link: `/n/${family.current.refId}`,
|
|
233
|
+
source: { taskRef: family.current.refId },
|
|
234
|
+
}, now);
|
|
235
|
+
const channels = pairedChannelsFor(store, auth.principal.account);
|
|
236
|
+
store.recordAction({
|
|
237
|
+
at: now.toISOString(),
|
|
238
|
+
actor: auth.principal.account,
|
|
239
|
+
repo,
|
|
240
|
+
taskId: family.current.id,
|
|
241
|
+
runId: null,
|
|
242
|
+
action: "approval.ping",
|
|
243
|
+
outcome: "recorded",
|
|
244
|
+
source: "api",
|
|
245
|
+
detail: channels.length > 0 ? `channels=${channels.join(",")}` : "no paired push/chat channels",
|
|
246
|
+
});
|
|
247
|
+
const path = `/t/${encodeURIComponent(family.current.id)}`;
|
|
248
|
+
const origin = consoleOrigin(request.headers.host);
|
|
249
|
+
const payload = {
|
|
250
|
+
ok: true,
|
|
251
|
+
taskId: family.current.id,
|
|
252
|
+
enqueued,
|
|
253
|
+
channels,
|
|
254
|
+
path,
|
|
255
|
+
url: origin === null ? null : `${origin}${path}`,
|
|
256
|
+
};
|
|
257
|
+
return sendJson(response, 200, payload);
|
|
258
|
+
}
|
|
259
|
+
export function handleRemoteTerminalsHttp(request, response, options) {
|
|
260
|
+
if (request.method !== "GET") {
|
|
261
|
+
return sendJson(response, 405, { ok: false, error: "method_not_allowed" }, { allow: "GET" });
|
|
262
|
+
}
|
|
263
|
+
const { store, clock, liveCeiling, terminalHost, authenticateBearer, admitBearer } = options;
|
|
264
|
+
const auth = admitRemoteToken(request, response, store, clock, authenticateBearer, admitBearer);
|
|
265
|
+
if (auth === null)
|
|
266
|
+
return;
|
|
267
|
+
if (auth.scope !== "act") {
|
|
268
|
+
return sendJson(response, 403, { ok: false, error: "read_only_token", message: "Attaching to a remote terminal requires an act API token." });
|
|
269
|
+
}
|
|
270
|
+
const account = store.accountOf(auth.principal.account);
|
|
271
|
+
if (account === null || account.revokedAt !== null || account.role !== "approver" || store.isDemo()) {
|
|
272
|
+
return sendJson(response, 403, { ok: false, error: "approver_required", message: "Only an approver can attach to remote terminals." });
|
|
273
|
+
}
|
|
274
|
+
const ceiling = liveCeiling();
|
|
275
|
+
const canSeeRepo = (repo) => rowVisible(ceiling, repo) && store.accountCanAccess(auth.principal.account, repo) && remoteAllows(store, auth.principal, repo);
|
|
276
|
+
const host = terminalHost();
|
|
277
|
+
const sessions = host === null
|
|
278
|
+
? []
|
|
279
|
+
: host.listAll()
|
|
280
|
+
.filter(one => one.agent === null && one.localGeneration === null && canSeeRepo(one.repo))
|
|
281
|
+
.map(one => ({
|
|
282
|
+
id: one.id,
|
|
283
|
+
repo: one.repo,
|
|
284
|
+
project: projectName(one.repo),
|
|
285
|
+
program: one.program,
|
|
286
|
+
branch: one.branch,
|
|
287
|
+
startedAt: one.startedAt,
|
|
288
|
+
endedAt: one.endedAt,
|
|
289
|
+
exit: one.exit,
|
|
290
|
+
cleanup: one.cleanup,
|
|
291
|
+
task: one.task,
|
|
292
|
+
run: one.run,
|
|
293
|
+
}));
|
|
294
|
+
if (!reproveRemote(store, auth.principal, clock()).ok) {
|
|
295
|
+
return sendJson(response, 401, { ok: false, error: "unauthenticated", message: "Your access changed." });
|
|
296
|
+
}
|
|
297
|
+
return sendJson(response, 200, {
|
|
298
|
+
available: host !== null,
|
|
299
|
+
problem: host === null ? "Terminals aren't available on this server." : null,
|
|
300
|
+
programs: [...TERMINAL_PROGRAMS],
|
|
301
|
+
projects: [],
|
|
302
|
+
sessions,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
const REMOTE_TERMINAL_START_PATH = /^\/api\/handoff\/task\/([^/]+)\/terminal$/;
|
|
306
|
+
const SHA_RE = /^[a-f0-9]{40}$/;
|
|
307
|
+
function readSmallJsonBody(request, maxBytes = 8 * 1024) {
|
|
308
|
+
return new Promise(resolve => {
|
|
309
|
+
const chunks = [];
|
|
310
|
+
let total = 0;
|
|
311
|
+
request.on("data", chunk => {
|
|
312
|
+
total += chunk.length;
|
|
313
|
+
if (total > maxBytes) {
|
|
314
|
+
request.resume();
|
|
315
|
+
resolve(null);
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
chunks.push(Buffer.from(chunk));
|
|
319
|
+
});
|
|
320
|
+
request.on("end", () => {
|
|
321
|
+
if (chunks.length === 0)
|
|
322
|
+
return resolve({});
|
|
323
|
+
try {
|
|
324
|
+
const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
325
|
+
resolve(parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : null);
|
|
326
|
+
}
|
|
327
|
+
catch {
|
|
328
|
+
resolve(null);
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
request.on("error", () => resolve(null));
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
export async function handleStartRemoteTerminalHttp(request, response, url, options) {
|
|
335
|
+
if (request.method !== "POST") {
|
|
336
|
+
return sendJson(response, 405, { ok: false, error: "method_not_allowed" }, { allow: "POST" });
|
|
337
|
+
}
|
|
338
|
+
const match = REMOTE_TERMINAL_START_PATH.exec(url.pathname);
|
|
339
|
+
if (match === null) {
|
|
340
|
+
return sendJson(response, 404, { ok: false, error: "not_found" });
|
|
341
|
+
}
|
|
342
|
+
let taskId;
|
|
343
|
+
try {
|
|
344
|
+
taskId = decodeURIComponent(match[1]);
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
return sendJson(response, 400, { ok: false, error: "bad_task", message: "Invalid task id." });
|
|
348
|
+
}
|
|
349
|
+
const { store, clock, liveCeiling, terminalHost, authenticateBearer, admitBearer, codingActorAllowed, codingProjectAllowed } = options;
|
|
350
|
+
const auth = admitRemoteToken(request, response, store, clock, authenticateBearer, admitBearer);
|
|
351
|
+
if (auth === null)
|
|
352
|
+
return;
|
|
353
|
+
if (auth.scope !== "act") {
|
|
354
|
+
return sendJson(response, 403, { ok: false, error: "read_only_token", message: "Starting a central Live Terminal requires an act API token." });
|
|
355
|
+
}
|
|
356
|
+
const account = store.accountOf(auth.principal.account);
|
|
357
|
+
if (account === null || account.revokedAt !== null || account.role !== "approver" || store.isDemo()) {
|
|
358
|
+
return sendJson(response, 403, { ok: false, error: "approver_required", message: "Only an approver can start a central Live Terminal." });
|
|
359
|
+
}
|
|
360
|
+
const body = await readSmallJsonBody(request);
|
|
361
|
+
if (body === null) {
|
|
362
|
+
return sendJson(response, 400, { ok: false, error: "invalid_body", message: "Send a JSON object." });
|
|
363
|
+
}
|
|
364
|
+
const requestedProgram = typeof body["program"] === "string" && body["program"].trim() !== "" ? body["program"].trim() : "shell";
|
|
365
|
+
if (!TERMINAL_PROGRAMS.includes(requestedProgram)) {
|
|
366
|
+
return sendJson(response, 400, { ok: false, error: "bad_program", message: "Choose shell, claude, codex, or antigravity." });
|
|
367
|
+
}
|
|
368
|
+
const program = requestedProgram;
|
|
369
|
+
const host = terminalHost();
|
|
370
|
+
if (host === null) {
|
|
371
|
+
return sendJson(response, 503, { ok: false, error: "terminals_unavailable", message: "Live Terminals are not available on this central server." });
|
|
372
|
+
}
|
|
373
|
+
const allowedRepos = auth.principal.projects === null
|
|
374
|
+
? null
|
|
375
|
+
: auth.principal.projects.filter(repo => store.accountCanAccess(auth.principal.account, repo));
|
|
376
|
+
const family = store.taskFamilyOf(taskId, allowedRepos, false);
|
|
377
|
+
const repo = family?.root.repo ?? family?.current.repo ?? null;
|
|
378
|
+
const ceiling = liveCeiling();
|
|
379
|
+
const canSeeRepo = (targetRepo) => rowVisible(ceiling, targetRepo) && store.accountCanAccess(auth.principal.account, targetRepo) && remoteAllows(store, auth.principal, targetRepo);
|
|
380
|
+
if (family === null || family.problem !== null || repo === null || !canSeeRepo(repo)) {
|
|
381
|
+
return sendJson(response, 404, { ok: false, error: "not_found", message: "Task not found or outside your projects." });
|
|
382
|
+
}
|
|
383
|
+
const codingAllowed = () => codingActorAllowed({ name: auth.principal.account, generation: account.generation }) && codingProjectAllowed(repo);
|
|
384
|
+
if (!codingAllowed()) {
|
|
385
|
+
return sendJson(response, 403, { ok: false, error: "operator_required", message: "Live Terminals need operator access to the whole installation and this project." });
|
|
386
|
+
}
|
|
387
|
+
if (program !== "shell") {
|
|
388
|
+
const stopped = store.sessionPolicyRefusal(program, null, "live terminals");
|
|
389
|
+
if (stopped !== null) {
|
|
390
|
+
return sendJson(response, 403, { ok: false, error: "policy_refused", message: stopped });
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
const existing = host.listAll().find(one => one.task === family.current.id && one.endedAt === null && one.agent === null && one.localGeneration === null && canSeeRepo(one.repo));
|
|
394
|
+
if (existing !== undefined) {
|
|
395
|
+
return sendJson(response, 200, {
|
|
396
|
+
ok: true,
|
|
397
|
+
id: existing.id,
|
|
398
|
+
branch: existing.branch,
|
|
399
|
+
program: existing.program,
|
|
400
|
+
reused: true,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
const latest = resultRunsOf(store, family)[0] ?? null;
|
|
404
|
+
let revision = (latest?.headRevision && SHA_RE.test(latest.headRevision))
|
|
405
|
+
? latest.headRevision
|
|
406
|
+
: (latest?.baseRevision && SHA_RE.test(latest.baseRevision))
|
|
407
|
+
? latest.baseRevision
|
|
408
|
+
: null;
|
|
409
|
+
if (revision === null && existsSync(join(repo, ".git"))) {
|
|
410
|
+
const git = teardownGit();
|
|
411
|
+
const head = await git(repo, ["rev-parse", "HEAD"]).catch(() => null);
|
|
412
|
+
if (head !== null && head.code === 0 && SHA_RE.test(head.stdout.trim()))
|
|
413
|
+
revision = head.stdout.trim();
|
|
414
|
+
}
|
|
415
|
+
if (revision === null) {
|
|
416
|
+
return sendJson(response, 409, { ok: false, error: "no_commit", message: "This project has no Git commit to open a Live Terminal on yet." });
|
|
417
|
+
}
|
|
418
|
+
const stillAllowed = () => {
|
|
419
|
+
const live = reproveRemote(store, auth.principal, clock());
|
|
420
|
+
if (!live.ok || live.scope !== "act")
|
|
421
|
+
return "Your remote sign-in changed. Reconnect.";
|
|
422
|
+
const acc = store.accountOf(auth.principal.account);
|
|
423
|
+
if (acc === null || acc.revokedAt !== null || acc.role !== "approver")
|
|
424
|
+
return "Your account access changed.";
|
|
425
|
+
if (!canSeeRepo(repo))
|
|
426
|
+
return "That project is no longer available to your account.";
|
|
427
|
+
if (!codingAllowed())
|
|
428
|
+
return "Live Terminals need operator access to the whole installation and this project.";
|
|
429
|
+
return null;
|
|
430
|
+
};
|
|
431
|
+
const precheck = stillAllowed();
|
|
432
|
+
if (precheck !== null) {
|
|
433
|
+
return sendJson(response, 401, { ok: false, error: "unauthenticated", message: precheck });
|
|
434
|
+
}
|
|
435
|
+
try {
|
|
436
|
+
const terminal = await host.start({ name: auth.principal.account, generation: account.generation, localGeneration: null }, repo, program, undefined, stillAllowed, { revision, task: family.current.id, run: latest?.id ?? 0 });
|
|
437
|
+
const after = stillAllowed();
|
|
438
|
+
if (after !== null) {
|
|
439
|
+
await host.close(terminal.id);
|
|
440
|
+
await host.remove(terminal.id).catch(() => { });
|
|
441
|
+
return sendJson(response, 403, { ok: false, error: "unauthenticated", message: after });
|
|
442
|
+
}
|
|
443
|
+
const now = clock();
|
|
444
|
+
store.recordAction({
|
|
445
|
+
at: now.toISOString(),
|
|
446
|
+
actor: auth.principal.account,
|
|
447
|
+
repo,
|
|
448
|
+
taskId: family.current.id,
|
|
449
|
+
runId: latest?.id ?? null,
|
|
450
|
+
action: "terminal.remote-start",
|
|
451
|
+
outcome: "started",
|
|
452
|
+
source: "api",
|
|
453
|
+
detail: `started central Live Terminal (${program}) ${terminal.id} on ${terminal.branch}`,
|
|
454
|
+
});
|
|
455
|
+
store.bumpWake();
|
|
456
|
+
return sendJson(response, 200, {
|
|
457
|
+
ok: true,
|
|
458
|
+
id: terminal.id,
|
|
459
|
+
branch: terminal.branch,
|
|
460
|
+
program: terminal.program,
|
|
461
|
+
reused: false,
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
catch (error) {
|
|
465
|
+
return sendJson(response, 409, {
|
|
466
|
+
ok: false,
|
|
467
|
+
error: "start_failed",
|
|
468
|
+
message: error instanceof TerminalError ? error.message : "Could not start central Live Terminal.",
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
}
|
package/dist/names.js
CHANGED
|
@@ -3,18 +3,31 @@ import { join } from "node:path";
|
|
|
3
3
|
export const NAME = "toolroll";
|
|
4
4
|
export const LEGACY_NAMES = ["standing-orders", "nightorders"];
|
|
5
5
|
export const NAMES = [NAME, ...LEGACY_NAMES];
|
|
6
|
+
function hasActiveConsoleMarker(dir) {
|
|
7
|
+
return existsSync(join(dir, "up-console.json")) || existsSync(join(dir, ".toolroll-run"));
|
|
8
|
+
}
|
|
6
9
|
export function namedPath(base, rest, options = {}) {
|
|
7
10
|
const names = options.names ?? NAMES;
|
|
8
11
|
const folder = (name) => join(base, options.dot ? `.${name}` : name);
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
const matches = names.filter(name => existsSync(join(folder(name), ...rest)));
|
|
13
|
+
if (matches.length > 0) {
|
|
14
|
+
if (matches.length > 1 && rest.length === 1 && rest[0] === "orders.db") {
|
|
15
|
+
const active = matches.find(name => hasActiveConsoleMarker(folder(name)));
|
|
16
|
+
if (active !== undefined)
|
|
17
|
+
return join(folder(active), ...rest);
|
|
18
|
+
}
|
|
19
|
+
return join(folder(matches[0]), ...rest);
|
|
20
|
+
}
|
|
12
21
|
return join(namedFolder(base, options), ...rest);
|
|
13
22
|
}
|
|
14
23
|
export function namedFolder(base, options = {}) {
|
|
15
24
|
const names = options.names ?? NAMES;
|
|
16
25
|
const folder = (name) => join(base, options.dot ? `.${name}` : name);
|
|
17
|
-
const
|
|
26
|
+
const dbMatches = names.filter(name => existsSync(join(folder(name), "orders.db")));
|
|
27
|
+
const chosen = (dbMatches.length > 1 ? dbMatches.find(name => hasActiveConsoleMarker(folder(name))) : undefined) ??
|
|
28
|
+
dbMatches[0] ??
|
|
29
|
+
names.find(name => existsSync(folder(name))) ??
|
|
30
|
+
names[0];
|
|
18
31
|
return folder(chosen);
|
|
19
32
|
}
|
|
20
33
|
export function existingFolders(base, options = {}) {
|
package/dist/needs-you.js
CHANGED
|
@@ -313,6 +313,16 @@ export function stopSuggestionOf(reason) {
|
|
|
313
313
|
const code = reason?.trim() ?? "";
|
|
314
314
|
return STOP_SUGGESTIONS[code] ?? GENERAL_SUGGESTION;
|
|
315
315
|
}
|
|
316
|
+
const OFFLINE_REGISTRY_SIGNAL = /\b(?:ENOTFOUND|EAI_AGAIN|ERR_PNPM_FETCH|registry\.npmjs\.org|registry\.yarnpkg\.com|pypi\.org|crates\.io|proxy\.golang\.org|rubygems\.org)\b|offline store|registry (?:lookup|could not be reached|unreachable)|could(?:n't| not) resolve/i;
|
|
317
|
+
export function codexOfflineDependencySuggestion(options) {
|
|
318
|
+
if (options.provider !== "codex" || !options.agentSaid || options.agentSaid.length === 0)
|
|
319
|
+
return null;
|
|
320
|
+
if (!OFFLINE_REGISTRY_SIGNAL.test(options.agentSaid.join("\n")))
|
|
321
|
+
return null;
|
|
322
|
+
return options.hasSetup
|
|
323
|
+
? "Codex runs commands offline in its sandbox. Check that the project's worktree setup command installs every dependency needed before the agent starts, then retry."
|
|
324
|
+
: "Codex runs commands offline in its sandbox, so packages can't install during a build. Set a project worktree setup command (Settings → Project or toolroll setup set) to install dependencies before the agent starts, then retry.";
|
|
325
|
+
}
|
|
316
326
|
function inSentence(statement) {
|
|
317
327
|
const plain = statement.trim().replace(/[.\s]+$/, "");
|
|
318
328
|
return /^[A-Z][a-z]/.test(plain) ? `${plain.charAt(0).toLowerCase()}${plain.slice(1)}` : plain;
|