toolroll 0.9.109 → 0.9.111
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 +73 -0
- package/README.md +5 -1
- package/THIRD_PARTY_NOTICES.md +30 -0
- package/dist/agent-detect.js +244 -0
- package/dist/agent-fence.js +12 -1
- package/dist/agent-said-schema.js +4 -0
- package/dist/agent-said.js +23 -0
- package/dist/agentconfig.js +2 -2
- package/dist/approval-summary.js +375 -0
- package/dist/attest.js +2 -2
- package/dist/browser/THIRD_PARTY_NOTICES.txt +30 -0
- package/dist/browser/workspace.css +2 -2
- package/dist/browser/workspace.js +27 -27
- package/dist/builder.js +42 -3
- package/dist/chat-actions.js +24 -17
- package/dist/chat-core.js +3 -1
- package/dist/chat-decide.js +101 -46
- package/dist/chat-delivery-state.js +2 -0
- package/dist/chat-delivery.js +3 -2
- package/dist/chat-live.js +2 -1
- package/dist/chat-task-actions.js +6 -0
- package/dist/check-report.js +1 -1
- package/dist/claim.js +28 -13
- package/dist/cli-help.js +1 -1
- package/dist/cli.js +2 -0
- package/dist/codex-limits.js +21 -8
- package/dist/console-instance.js +11 -1
- package/dist/contracts/console-api.js +13 -7
- package/dist/contracts/engine-api.js +283 -0
- package/dist/contracts/route.js +1 -0
- package/dist/coordinator.js +2 -2
- package/dist/discord-api.js +4 -3
- package/dist/discord-chat.js +3 -1
- package/dist/dispatch.js +1 -1
- package/dist/dispose.js +15 -9
- package/dist/easy-start-args.js +7 -3
- package/dist/easy-start.js +40 -1
- package/dist/egress.js +72 -10
- package/dist/engine-handoff.js +34 -0
- package/dist/engine-reads.js +338 -0
- package/dist/eval-world.js +3 -2
- package/dist/evidence.js +2 -0
- package/dist/fast-start.js +68 -18
- package/dist/fast-terms.js +2 -2
- package/dist/first-run.js +10 -2
- package/dist/flexible-agents.js +234 -0
- package/dist/flexible-answers-schema.js +61 -0
- package/dist/flexible-answers.js +889 -0
- package/dist/flexible-custody.js +494 -0
- package/dist/flexible-import.js +782 -0
- package/dist/flexible-launch-schema.js +72 -0
- package/dist/flexible-launch.js +331 -0
- package/dist/flexible-launcher.js +88 -0
- package/dist/flexible-levels.js +34 -0
- package/dist/flexible-merge-guard.js +47 -0
- package/dist/flexible-refs.js +47 -0
- package/dist/flexible-relay-cancel.js +19 -0
- package/dist/flexible-relay-schema.js +55 -0
- package/dist/flexible-relay-upstreams.js +36 -0
- package/dist/flexible-relay.js +1032 -0
- package/dist/flexible-session.js +732 -0
- package/dist/flexible-ship-schema.js +81 -0
- package/dist/flexible-ship.js +694 -0
- package/dist/flexible-start.js +80 -0
- package/dist/flexible-thread.js +175 -25
- package/dist/flexible-turns-schema.js +69 -0
- package/dist/flexible-turns.js +490 -0
- package/dist/flexible.js +10 -1
- package/dist/flow-actions.js +6 -4
- package/dist/flow-steps.js +1 -1
- package/dist/flows-cli.js +2 -1
- package/dist/git-views.js +6 -3
- package/dist/install-mode.js +225 -0
- package/dist/integrations.js +6 -1
- package/dist/lead-doors.js +7 -1
- package/dist/limits-ui.js +7 -0
- package/dist/live-reply.js +6 -0
- package/dist/live-terminal.js +425 -29
- package/dist/local-owner.js +7 -0
- package/dist/model-choices.js +12 -6
- package/dist/needs-you.js +2 -0
- package/dist/notification-delivery.js +11 -0
- package/dist/operate.js +128 -37
- package/dist/outbound-privacy.js +204 -0
- package/dist/passwordless.js +2 -1
- package/dist/phase-routing.js +3 -1
- package/dist/plane-review.js +1 -1
- package/dist/project-delete.js +14 -0
- package/dist/proposal.js +2 -2
- package/dist/provider-connection.js +19 -5
- package/dist/publish.js +5 -0
- package/dist/pull-request-flow.js +26 -7
- package/dist/push.js +5 -1
- package/dist/release-card.js +11 -3
- package/dist/release.js +10 -1
- package/dist/route-store.js +3 -3
- package/dist/sandbox-launch.js +32 -8
- package/dist/sandbox-runtime.js +16 -7
- package/dist/sandbox-tools.js +176 -0
- package/dist/scope.js +12 -36
- package/dist/scout-net.js +7 -2
- package/dist/serve.js +62 -13
- package/dist/server/chrome.js +3 -1
- package/dist/server/console.css +33 -6
- package/dist/server/engine-api.js +378 -0
- package/dist/server/flows.js +2 -2
- package/dist/server/http.js +7 -0
- package/dist/server/link-redemption.js +52 -0
- package/dist/server/mutation-key.js +21 -7
- package/dist/server/pages.js +72 -40
- package/dist/server/people-tokens.js +17 -41
- package/dist/server/render-chat.js +1 -0
- package/dist/server/render-new-task.js +3 -3
- package/dist/server/render-results.js +4 -4
- package/dist/server/render-tasks.js +133 -139
- package/dist/server/route-table.js +32 -1
- package/dist/server/settings-section-table.js +6 -0
- package/dist/server/settings-sections.js +69 -0
- package/dist/server/settings.js +120 -10
- package/dist/server/task-views.js +8 -2
- package/dist/server/tasks.js +56 -12
- package/dist/server/terminals.js +181 -28
- package/dist/server/welcome.js +35 -10
- package/dist/settings-groups-ui.js +2 -2
- package/dist/settings-proofs.js +14 -9
- package/dist/slack-api.js +4 -3
- package/dist/slack-chat.js +5 -3
- package/dist/spend.js +46 -12
- package/dist/status-probe.js +343 -0
- package/dist/store.js +181 -12
- package/dist/task-sizing-schema.js +14 -0
- package/dist/task-sizing.js +76 -10
- package/dist/teams-api.js +4 -3
- package/dist/teams-chat.js +4 -2
- package/dist/telegram-lead.js +2 -1
- package/dist/telegram-live.js +2 -1
- package/dist/telegram-team.js +2 -1
- package/dist/telegram.js +17 -5
- package/dist/toolroll-update.js +3 -1
- package/dist/trusted-path.js +162 -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-launch.js +282 -0
- package/dist/tui-source.js +5 -3
- package/dist/tui.js +1 -1
- package/dist/webhooks.js +14 -4
- package/dist/welcome-facts.js +22 -15
- package/dist/work-index.js +2 -2
- package/dist/workspace-ui.js +5 -0
- package/package.json +1 -1
- package/scripts/crash-canary.mjs +1 -1
- package/scripts/pilot.mjs +1 -1
- package/scripts/provider-canary.mjs +5 -3
package/dist/spend.js
CHANGED
|
@@ -55,14 +55,14 @@ export function ceilingPriceFor(db, provider) {
|
|
|
55
55
|
return row === undefined || row["input_usd"] == null || row["output_usd"] == null ? null
|
|
56
56
|
: { inputUsd: Number(row["input_usd"]), outputUsd: Number(row["output_usd"]), model: "highest listed price" };
|
|
57
57
|
}
|
|
58
|
-
export function priceWork(db, work) {
|
|
58
|
+
export function priceWork(db, work, lookup = (provider, model) => priceFor(db, provider, model) ?? ceilingPriceFor(db, provider)) {
|
|
59
59
|
if (work.billing === "subscription")
|
|
60
60
|
return { microusd: 0, source: "subscription", price: null };
|
|
61
61
|
if (work.costUsd !== null && Number.isFinite(work.costUsd))
|
|
62
62
|
return { microusd: Math.round(work.costUsd * 1_000_000), source: "reported", price: null };
|
|
63
63
|
if (work.tokensIn === null && work.tokensOut === null)
|
|
64
64
|
return { microusd: null, source: "unpriced", price: null };
|
|
65
|
-
const price =
|
|
65
|
+
const price = lookup(work.provider, work.model);
|
|
66
66
|
if (price === null)
|
|
67
67
|
return { microusd: null, source: "unpriced", price: null };
|
|
68
68
|
return { microusd: Math.round((work.tokensIn ?? 0) * price.inputUsd + (work.tokensOut ?? 0) * price.outputUsd), source: "estimated", price };
|
|
@@ -172,12 +172,33 @@ export function filersOf(db) {
|
|
|
172
172
|
return found;
|
|
173
173
|
};
|
|
174
174
|
}
|
|
175
|
-
export function spendItems(db, from, to,
|
|
176
|
-
const
|
|
175
|
+
export function spendItems(db, from, to, readMode = provider => billingOf(provider, db)) {
|
|
176
|
+
const modes = new Map();
|
|
177
|
+
const modeOf = (provider) => { let mode = modes.get(provider); if (mode === undefined) {
|
|
178
|
+
mode = readMode(provider);
|
|
179
|
+
modes.set(provider, mode);
|
|
180
|
+
} return mode; };
|
|
181
|
+
const prices = new Map();
|
|
182
|
+
const lookup = (provider, model) => {
|
|
183
|
+
const key = `${provider}\n${model ?? ""}`;
|
|
184
|
+
if (!prices.has(key))
|
|
185
|
+
prices.set(key, priceFor(db, provider, model) ?? ceilingPriceFor(db, provider));
|
|
186
|
+
return prices.get(key);
|
|
187
|
+
};
|
|
188
|
+
const has = db.prepare(`SELECT EXISTS(SELECT 1 FROM run WHERE started_at >= ?1 AND started_at < ?2) AS runs,
|
|
189
|
+
EXISTS(SELECT 1 FROM subagent_turn WHERE at >= ?1 AND at < ?2) AS subagent,
|
|
190
|
+
(EXISTS(SELECT 1 FROM chat_turn WHERE created_at >= ?1 AND created_at < ?2) OR EXISTS(SELECT 1 FROM mate_turn WHERE created_at >= ?1 AND created_at < ?2)) AS chat,
|
|
191
|
+
EXISTS(SELECT 1 FROM side_spend WHERE at >= ?1 AND at < ?2) AS side,
|
|
192
|
+
EXISTS(SELECT 1 FROM flow_step_run WHERE kind = 'sort' AND started_at >= ?1 AND started_at < ?2) AS sort,
|
|
193
|
+
EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'thread_turn') AS threads,
|
|
194
|
+
EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'thread_relay_pending') AS reservations`).get(from, to) ?? {};
|
|
195
|
+
const any = (key) => Number(has[key]) === 1;
|
|
196
|
+
let filers = null;
|
|
197
|
+
const subagentOf = (repo, filedBy, kind) => (filers ??= subagentFilers(db))(repo, filedBy, kind);
|
|
177
198
|
const filerOf = filersOf(db);
|
|
178
199
|
const text = (value) => value == null ? null : String(value);
|
|
179
200
|
const count = (value) => value == null ? null : Number(value);
|
|
180
|
-
const runs = db.prepare(`SELECT run.id, run.task_ref, run.provider, run.model, run.cost_usd, run.tokens_in, run.tokens_out, run.started_at, run.auth_mode,
|
|
201
|
+
const runs = !any("runs") ? [] : db.prepare(`SELECT run.id, run.task_ref, run.provider, run.model, run.cost_usd, run.tokens_in, run.tokens_out, run.started_at, run.auth_mode,
|
|
181
202
|
r.external_id AS task, r.repo, r.filed_by, r.filed_by_kind, r.revision_of, s.microusd, s.source, s.billing
|
|
182
203
|
FROM run JOIN task_ref r ON r.id = run.task_ref LEFT JOIN run_spend s ON s.run = run.id
|
|
183
204
|
WHERE run.started_at >= ? AND run.started_at < ? ORDER BY run.id`).all(from, to);
|
|
@@ -186,7 +207,7 @@ export function spendItems(db, from, to, modeOf = provider => billingOf(provider
|
|
|
186
207
|
const billing = row["billing"] === "subscription" || row["billing"] === "api-key" ? row["billing"]
|
|
187
208
|
: row["auth_mode"] === "subscription" || row["auth_mode"] === "api-key" ? row["auth_mode"] : modeOf(provider);
|
|
188
209
|
const settled = row["source"] != null;
|
|
189
|
-
const priced = settled ? null : priceWork(db, { provider, model: text(row["model"]), costUsd: count(row["cost_usd"]), tokensIn: count(row["tokens_in"]), tokensOut: count(row["tokens_out"]), billing });
|
|
210
|
+
const priced = settled ? null : priceWork(db, { provider, model: text(row["model"]), costUsd: count(row["cost_usd"]), tokensIn: count(row["tokens_in"]), tokensOut: count(row["tokens_out"]), billing }, lookup);
|
|
190
211
|
const filer = row["filed_by"] == null && row["revision_of"] != null ? filerOf(Number(row["task_ref"]))
|
|
191
212
|
: { repo: text(row["repo"]), filedBy: text(row["filed_by"]), kind: text(row["filed_by_kind"]) };
|
|
192
213
|
const kind = filer.kind;
|
|
@@ -200,15 +221,15 @@ export function spendItems(db, from, to, modeOf = provider => billingOf(provider
|
|
|
200
221
|
taskId: String(row["task"]), runId: Number(row["id"]), authMode: billing,
|
|
201
222
|
};
|
|
202
223
|
});
|
|
203
|
-
for (const row of db.prepare(`SELECT t.at, t.subagent, t.model, t.cost_usd, t.tokens_in, t.tokens_out, t.billing, m.repo FROM subagent_turn t JOIN subagent m ON m.id = t.subagent
|
|
224
|
+
for (const row of !any("subagent") ? [] : db.prepare(`SELECT t.at, t.subagent, t.model, t.cost_usd, t.tokens_in, t.tokens_out, t.billing, m.repo FROM subagent_turn t JOIN subagent m ON m.id = t.subagent
|
|
204
225
|
WHERE t.at >= ? AND t.at < ? ORDER BY t.id`).all(from, to)) {
|
|
205
226
|
const billing = row["billing"] === "api-key" ? "api-key" : "subscription";
|
|
206
|
-
const priced = priceWork(db, { provider: "claude", model: text(row["model"]), costUsd: count(row["cost_usd"]), tokensIn: count(row["tokens_in"]), tokensOut: count(row["tokens_out"]), billing });
|
|
227
|
+
const priced = priceWork(db, { provider: "claude", model: text(row["model"]), costUsd: count(row["cost_usd"]), tokensIn: count(row["tokens_in"]), tokensOut: count(row["tokens_out"]), billing }, lookup);
|
|
207
228
|
items.push({ at: String(row["at"]), kind: "subagent", microusd: priced.microusd, source: priced.source, project: String(row["repo"]), person: null,
|
|
208
229
|
subagent: Number(row["subagent"]), provider: "claude", model: text(row["model"]),
|
|
209
230
|
tokensIn: count(row["tokens_in"]), tokensOut: count(row["tokens_out"]), taskId: null, runId: null, authMode: billing });
|
|
210
231
|
}
|
|
211
|
-
for (const row of db.prepare(`SELECT created_at AS at, approver, provider, model, settled_microusd, tokens_in, tokens_out, NULL AS project
|
|
232
|
+
for (const row of !any("chat") ? [] : db.prepare(`SELECT created_at AS at, approver, provider, model, settled_microusd, tokens_in, tokens_out, NULL AS project
|
|
212
233
|
FROM chat_turn WHERE mate_turn IS NULL AND created_at >= ? AND created_at < ?
|
|
213
234
|
UNION ALL SELECT t.created_at, t.approver, COALESCE((SELECT c.provider FROM chat_turn c WHERE c.mate_turn = t.id ORDER BY c.id LIMIT 1), 'lead'),
|
|
214
235
|
(SELECT c.model FROM chat_turn c WHERE c.mate_turn = t.id ORDER BY c.id LIMIT 1), t.settled_microusd, t.tokens_in, t.tokens_out,
|
|
@@ -221,19 +242,32 @@ export function spendItems(db, from, to, modeOf = provider => billingOf(provider
|
|
|
221
242
|
project: text(row["project"]), person: text(row["approver"]), subagent: null, provider, model: text(row["model"]),
|
|
222
243
|
tokensIn: count(row["tokens_in"]), tokensOut: count(row["tokens_out"]), taskId: null, runId: null, authMode: subscription ? "subscription" : "api-key" });
|
|
223
244
|
}
|
|
224
|
-
for (const row of db.prepare("SELECT at, kind, repo, provider, model, cost_usd, billing FROM side_spend WHERE at >= ? AND at < ? ORDER BY id").all(from, to)) {
|
|
245
|
+
for (const row of !any("side") ? [] : db.prepare("SELECT at, kind, repo, provider, model, cost_usd, billing FROM side_spend WHERE at >= ? AND at < ? ORDER BY id").all(from, to)) {
|
|
225
246
|
const billing = row["billing"] === "api-key" ? "api-key" : "subscription";
|
|
226
|
-
const priced = priceWork(db, { provider: String(row["provider"]), model: text(row["model"]), costUsd: count(row["cost_usd"]), tokensIn: null, tokensOut: null, billing });
|
|
247
|
+
const priced = priceWork(db, { provider: String(row["provider"]), model: text(row["model"]), costUsd: count(row["cost_usd"]), tokensIn: null, tokensOut: null, billing }, lookup);
|
|
227
248
|
items.push({ at: String(row["at"]), kind: "draft", microusd: priced.microusd, source: priced.source, project: text(row["repo"]), person: null, subagent: null,
|
|
228
249
|
provider: String(row["provider"]), model: text(row["model"]), tokensIn: null, tokensOut: null, taskId: null, runId: null, authMode: billing });
|
|
229
250
|
}
|
|
230
|
-
for (const row of db.prepare(`SELECT fs.started_at AS at, json_extract(fs.decision_json, '$.cost') AS cost, json_extract(fs.decision_json, '$.model') AS model, f.repo
|
|
251
|
+
for (const row of !any("sort") ? [] : db.prepare(`SELECT fs.started_at AS at, json_extract(fs.decision_json, '$.cost') AS cost, json_extract(fs.decision_json, '$.model') AS model, f.repo
|
|
231
252
|
FROM flow_step_run fs JOIN flow_card c ON c.id = fs.card JOIN flow f ON f.id = c.flow
|
|
232
253
|
WHERE fs.kind = 'sort' AND fs.decision_json IS NOT NULL AND fs.started_at >= ? AND fs.started_at < ? ORDER BY fs.started_at`).all(from, to)) {
|
|
233
254
|
const cost = typeof row["cost"] === "number" && Number.isFinite(row["cost"]) && row["cost"] >= 0 ? row["cost"] : null;
|
|
234
255
|
items.push({ at: String(row["at"]), kind: "sort", microusd: cost === null ? null : Math.round(cost * 1_000_000), source: cost === null ? "unpriced" : "reported",
|
|
235
256
|
project: text(row["repo"]), person: null, subagent: null, provider: "openrouter", model: text(row["model"]), tokensIn: null, tokensOut: null, taskId: null, runId: null, authMode: "api-key" });
|
|
236
257
|
}
|
|
258
|
+
if (any("threads")) {
|
|
259
|
+
const reservations = any("reservations");
|
|
260
|
+
for (const row of db.prepare(`SELECT COALESCE(started_at, admitted_at) AS at, repo, owner, provider, model, billing, status, cost_basis, cost_microusd, tokens_in, tokens_out,
|
|
261
|
+
${reservations ? "(status IN ('finished', 'interrupted', 'unknown') AND id IN (SELECT turn FROM thread_relay_pending))" : "0"} AS unsettled
|
|
262
|
+
FROM thread_turn WHERE status <> 'withdrawn' AND COALESCE(started_at, admitted_at) >= ? AND COALESCE(started_at, admitted_at) < ? ORDER BY admitted_at, id`).all(from, to)) {
|
|
263
|
+
const billing = row["billing"] === "subscription" ? "subscription" : "api-key";
|
|
264
|
+
const blind = billing === "api-key" && (row["cost_basis"] === "unknown" || Number(row["unsettled"]) === 1);
|
|
265
|
+
items.push({ at: String(row["at"]), kind: "flexible", microusd: billing === "subscription" ? 0 : blind ? null : Number(row["cost_microusd"]),
|
|
266
|
+
source: billing === "subscription" ? "subscription" : blind ? "unpriced" : row["cost_basis"] === "measured" ? "reported" : "estimated",
|
|
267
|
+
project: String(row["repo"]), person: String(row["owner"]), subagent: null, provider: String(row["provider"]), model: text(row["model"]),
|
|
268
|
+
tokensIn: count(row["tokens_in"]), tokensOut: count(row["tokens_out"]), taskId: null, runId: null, authMode: billing });
|
|
269
|
+
}
|
|
270
|
+
}
|
|
237
271
|
return items;
|
|
238
272
|
}
|
|
239
273
|
export function countsToward(item, budget) {
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { accessSync, closeSync, constants, existsSync, lstatSync, mkdtempSync, openSync, readSync, realpathSync, rmSync } from "node:fs";
|
|
3
|
+
import { homedir, tmpdir } from "node:os";
|
|
4
|
+
import { delimiter, dirname, isAbsolute, join, resolve, sep } from "node:path";
|
|
5
|
+
import { boundedAcl, trustedBinary, trustedFolders } from "./trusted-path.js";
|
|
6
|
+
export const STATUS_TIMEOUT_MS = 800;
|
|
7
|
+
export const BACKGROUND_STATUS_TIMEOUT_MS = 3_000;
|
|
8
|
+
const KILL_GRACE_MS = 250;
|
|
9
|
+
const STATUS_MAX_BUFFER = 32 * 1024;
|
|
10
|
+
export const STATUS_ENV_NAMES = ["USER", "LOGNAME", "LANG", "LC_ALL", "LC_CTYPE", "TZ"];
|
|
11
|
+
export const STATUS_LOCATION_NAMES = ["HOME", "TMPDIR", "CLAUDE_CONFIG_DIR", "CODEX_HOME"];
|
|
12
|
+
const inside = (path, root) => path === root || path.startsWith(root.endsWith(sep) ? root : `${root}${sep}`);
|
|
13
|
+
const realOrNull = (path) => { try {
|
|
14
|
+
return realpathSync(path);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return null;
|
|
18
|
+
} };
|
|
19
|
+
const NOT_FOUND = { code: 127, stdout: "", stderr: "", timedOut: false, notFound: true };
|
|
20
|
+
const REFUSED = { code: 126, stdout: "", stderr: "", timedOut: false, notFound: false };
|
|
21
|
+
export function repositoryOf(dir) {
|
|
22
|
+
let at = realOrNull(dir);
|
|
23
|
+
while (at !== null) {
|
|
24
|
+
if (existsSync(join(at, ".git")))
|
|
25
|
+
return at;
|
|
26
|
+
const up = dirname(at);
|
|
27
|
+
if (up === at)
|
|
28
|
+
return null;
|
|
29
|
+
at = up;
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
export async function excludedFolders(source, extra = []) {
|
|
34
|
+
const { databasePath, openStoreReadOnly } = await import("./store.js");
|
|
35
|
+
const file = source.store?.databaseFile() ?? source.databaseFile ?? databasePath(process.env, homedir());
|
|
36
|
+
const found = [...extra];
|
|
37
|
+
let store = source.store ?? null;
|
|
38
|
+
let opened = null;
|
|
39
|
+
if (file !== null && file !== ":memory:") {
|
|
40
|
+
found.push(dirname(file));
|
|
41
|
+
const { readProjectRegistryNow } = await import("./repos.js");
|
|
42
|
+
const registry = readProjectRegistryNow(join(dirname(file), "repos.json"));
|
|
43
|
+
if ("error" in registry)
|
|
44
|
+
return null;
|
|
45
|
+
found.push(...registry.repos);
|
|
46
|
+
if (store === null && existsSync(file)) {
|
|
47
|
+
opened = openStoreReadOnly(file);
|
|
48
|
+
if (opened === null)
|
|
49
|
+
return null;
|
|
50
|
+
store = opened;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
if (store !== null)
|
|
55
|
+
found.push(...store.knownRepos(), ...store.listProjects().map(one => one.path), ...store.listWorktrees().map(one => one.path));
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
opened?.close();
|
|
62
|
+
}
|
|
63
|
+
const repo = repositoryOf(process.cwd());
|
|
64
|
+
if (repo !== null)
|
|
65
|
+
found.push(repo);
|
|
66
|
+
return [...new Set(found.flatMap(one => { const real = realOrNull(one); return isAbsolute(one) ? [resolve(one), ...(real === null ? [] : [real])] : real === null ? [] : [real]; }))];
|
|
67
|
+
}
|
|
68
|
+
function isScript(file) {
|
|
69
|
+
let fd = null;
|
|
70
|
+
try {
|
|
71
|
+
fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
72
|
+
const head = Buffer.alloc(2);
|
|
73
|
+
return readSync(fd, head, 0, 2, 0) === 2 && head[0] === 0x23 && head[1] === 0x21;
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
if (fd !== null)
|
|
80
|
+
closeSync(fd);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
export function packageFolderOf(script) {
|
|
84
|
+
for (let folder = dirname(script);;) {
|
|
85
|
+
if (existsSync(join(folder, "package.json")))
|
|
86
|
+
return folder;
|
|
87
|
+
const up = dirname(folder);
|
|
88
|
+
if (up === folder)
|
|
89
|
+
return dirname(script);
|
|
90
|
+
folder = up;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export function resolveStatusBinary(binary, trust) {
|
|
94
|
+
const refused = (why) => ({ ok: false, missing: false, why });
|
|
95
|
+
const excluded = (path) => trust.untrusted.some(root => inside(path, root));
|
|
96
|
+
for (const entry of trust.path.split(delimiter)) {
|
|
97
|
+
if (entry === "")
|
|
98
|
+
continue;
|
|
99
|
+
const candidate = join(isAbsolute(entry) ? entry : resolve(entry), binary);
|
|
100
|
+
try {
|
|
101
|
+
accessSync(candidate, constants.X_OK);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (!isAbsolute(entry))
|
|
107
|
+
return refused("it was found through a relative PATH entry");
|
|
108
|
+
if (trust.uid === undefined)
|
|
109
|
+
return refused("who owns it can't be checked on this system");
|
|
110
|
+
const folder = realOrNull(entry);
|
|
111
|
+
if (folder === null)
|
|
112
|
+
return refused("its PATH folder can't be read");
|
|
113
|
+
if (excluded(folder))
|
|
114
|
+
return refused("its PATH folder is inside a project, worktree or Toolroll's own folder");
|
|
115
|
+
const folders = trustedFolders(folder, trust.uid, trust.acl);
|
|
116
|
+
if (!folders.ok)
|
|
117
|
+
return refused(folders.why);
|
|
118
|
+
const file = trustedBinary(candidate, trust.uid, trust.acl);
|
|
119
|
+
if (!file.ok)
|
|
120
|
+
return refused(file.why);
|
|
121
|
+
if (excluded(file.real))
|
|
122
|
+
return refused("it is inside a project, worktree or Toolroll's own folder");
|
|
123
|
+
if (isScript(file.real)) {
|
|
124
|
+
const pack = packageFolderOf(file.real);
|
|
125
|
+
if (excluded(pack))
|
|
126
|
+
return refused("its package folder is inside a project, worktree or Toolroll's own folder");
|
|
127
|
+
const packed = trustedFolders(pack, trust.uid, trust.acl);
|
|
128
|
+
if (!packed.ok)
|
|
129
|
+
return refused(`its package folder: ${packed.why}`);
|
|
130
|
+
}
|
|
131
|
+
return { ok: true, real: file.real };
|
|
132
|
+
}
|
|
133
|
+
return { ok: false, missing: true };
|
|
134
|
+
}
|
|
135
|
+
export function shebangOf(script) {
|
|
136
|
+
let fd = null;
|
|
137
|
+
let head;
|
|
138
|
+
try {
|
|
139
|
+
fd = openSync(script, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
140
|
+
const buffer = Buffer.alloc(256);
|
|
141
|
+
const read = readSync(fd, buffer, 0, 256, 0);
|
|
142
|
+
head = buffer.subarray(0, read).toString("utf8");
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
finally {
|
|
148
|
+
if (fd !== null)
|
|
149
|
+
closeSync(fd);
|
|
150
|
+
}
|
|
151
|
+
const end = head.indexOf("\n");
|
|
152
|
+
if (!head.startsWith("#!") || end < 0)
|
|
153
|
+
return null;
|
|
154
|
+
const words = head.slice(2, end).replace(/\r$/, "").trim().split(/[ \t]+/).filter(one => one !== "");
|
|
155
|
+
const [first, ...rest] = words;
|
|
156
|
+
if (first === undefined || !isAbsolute(first))
|
|
157
|
+
return null;
|
|
158
|
+
if (first.split(sep).at(-1) === "env") {
|
|
159
|
+
const named = rest[0] === "-S" ? [...rest.slice(1, 2), "-S", ...rest.slice(2)] : rest;
|
|
160
|
+
const [name, ...args] = named;
|
|
161
|
+
if (name === undefined || !/^[A-Za-z0-9._+-]+$/.test(name))
|
|
162
|
+
return null;
|
|
163
|
+
return { kind: "env", name, args };
|
|
164
|
+
}
|
|
165
|
+
return { kind: "absolute", path: first, args: rest };
|
|
166
|
+
}
|
|
167
|
+
export function resolveLaunch(binary, trust) {
|
|
168
|
+
const found = resolveStatusBinary(binary, trust);
|
|
169
|
+
if (!found.ok)
|
|
170
|
+
return found;
|
|
171
|
+
if (!isScript(found.real))
|
|
172
|
+
return { ok: true, file: found.real, args: [], folders: [dirname(found.real)], real: found.real };
|
|
173
|
+
const refused = (why) => ({ ok: false, missing: false, why });
|
|
174
|
+
const bang = shebangOf(found.real);
|
|
175
|
+
if (bang === null)
|
|
176
|
+
return refused("its first line doesn't name an interpreter Toolroll can check");
|
|
177
|
+
if (bang.args.length > 0)
|
|
178
|
+
return refused(`its first line passes its interpreter arguments (${bang.args.join(" ").slice(0, 60)}), which Toolroll doesn't run`);
|
|
179
|
+
let interpreter;
|
|
180
|
+
if (bang.kind === "env") {
|
|
181
|
+
const named = resolveStatusBinary(bang.name, trust);
|
|
182
|
+
if (!named.ok)
|
|
183
|
+
return refused(named.missing ? `its interpreter \`${bang.name}\` isn't on PATH` : `its interpreter \`${bang.name}\`: ${named.why}`);
|
|
184
|
+
interpreter = named.real;
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
if (trust.uid === undefined)
|
|
188
|
+
return refused("who owns it can't be checked on this system");
|
|
189
|
+
const file = trustedBinary(bang.path, trust.uid, trust.acl);
|
|
190
|
+
if (!file.ok)
|
|
191
|
+
return refused(`its interpreter ${bang.path}: ${file.why}`);
|
|
192
|
+
if (trust.untrusted.some(root => inside(file.real, root) || inside(resolve(bang.path), root)))
|
|
193
|
+
return refused("its interpreter is inside a project, worktree or Toolroll's own folder");
|
|
194
|
+
interpreter = file.real;
|
|
195
|
+
}
|
|
196
|
+
if (isScript(interpreter))
|
|
197
|
+
return refused("its interpreter is itself a script");
|
|
198
|
+
return { ok: true, file: interpreter, args: [found.real], folders: [dirname(interpreter)], real: found.real };
|
|
199
|
+
}
|
|
200
|
+
export function launchPath(launch, trust) {
|
|
201
|
+
const kept = [];
|
|
202
|
+
for (const one of [...launch.folders, ...trustedPathOf(trust).split(delimiter)])
|
|
203
|
+
if (one !== "" && !kept.includes(one) && !trust.untrusted.some(root => inside(one, root)))
|
|
204
|
+
kept.push(one);
|
|
205
|
+
return kept.join(delimiter);
|
|
206
|
+
}
|
|
207
|
+
export function trustedPathOf(trust) {
|
|
208
|
+
if (trust.uid === undefined)
|
|
209
|
+
return "";
|
|
210
|
+
const kept = [];
|
|
211
|
+
for (const entry of trust.path.split(delimiter)) {
|
|
212
|
+
if (entry === "" || !isAbsolute(entry))
|
|
213
|
+
continue;
|
|
214
|
+
const folder = realOrNull(entry);
|
|
215
|
+
if (folder === null || trust.untrusted.some(root => inside(folder, root)) || !trustedFolders(folder, trust.uid, trust.acl).ok)
|
|
216
|
+
continue;
|
|
217
|
+
if (!kept.includes(folder))
|
|
218
|
+
kept.push(folder);
|
|
219
|
+
}
|
|
220
|
+
return kept.join(delimiter);
|
|
221
|
+
}
|
|
222
|
+
export function trustedLocation(value, trust) {
|
|
223
|
+
if (value === undefined || value === "" || !isAbsolute(value) || trust.uid === undefined)
|
|
224
|
+
return null;
|
|
225
|
+
const outside = (path) => !trust.untrusted.some(root => inside(path, root));
|
|
226
|
+
if (!outside(resolve(value)))
|
|
227
|
+
return null;
|
|
228
|
+
const real = realOrNull(value);
|
|
229
|
+
if (real === null || !outside(real))
|
|
230
|
+
return null;
|
|
231
|
+
try {
|
|
232
|
+
if (lstatSync(real).uid !== trust.uid)
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
return trustedFolders(real, trust.uid, trust.acl).ok ? real : null;
|
|
239
|
+
}
|
|
240
|
+
export function statusEnvironment(source, trust) {
|
|
241
|
+
const env = { PATH: trustedPathOf(trust) };
|
|
242
|
+
for (const name of STATUS_ENV_NAMES) {
|
|
243
|
+
const value = source[name];
|
|
244
|
+
if (value !== undefined && value !== "")
|
|
245
|
+
env[name] = value;
|
|
246
|
+
}
|
|
247
|
+
for (const name of STATUS_LOCATION_NAMES) {
|
|
248
|
+
const value = trustedLocation(source[name], trust);
|
|
249
|
+
if (value !== null)
|
|
250
|
+
env[name] = value;
|
|
251
|
+
}
|
|
252
|
+
return env;
|
|
253
|
+
}
|
|
254
|
+
function endGroup(pgid, signal) {
|
|
255
|
+
try {
|
|
256
|
+
process.kill(-pgid, signal);
|
|
257
|
+
}
|
|
258
|
+
catch { }
|
|
259
|
+
}
|
|
260
|
+
export function runBoundedStatus(file, args, options) {
|
|
261
|
+
const cap = options.maxBuffer ?? STATUS_MAX_BUFFER;
|
|
262
|
+
return new Promise(done => {
|
|
263
|
+
let stdout = "", stderr = "", timedOut = false, settled = false, code = null;
|
|
264
|
+
const timers = [];
|
|
265
|
+
const finish = (result) => { if (settled)
|
|
266
|
+
return; settled = true; for (const one of timers)
|
|
267
|
+
clearTimeout(one); done(result); };
|
|
268
|
+
if (options.timeoutMs <= 0)
|
|
269
|
+
return finish({ code: 124, stdout: "", stderr: "", timedOut: true, notFound: false });
|
|
270
|
+
let child;
|
|
271
|
+
try {
|
|
272
|
+
child = spawn(file, [...args], { cwd: options.cwd, env: options.env, detached: process.platform !== "win32", stdio: ["ignore", "pipe", "pipe"], windowsHide: true });
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
const missing = error.code === "ENOENT";
|
|
276
|
+
return finish({ code: missing ? 127 : -1, stdout: "", stderr: "", timedOut: false, notFound: missing });
|
|
277
|
+
}
|
|
278
|
+
const pgid = child.pid;
|
|
279
|
+
const end = (signal) => {
|
|
280
|
+
if (pgid === undefined)
|
|
281
|
+
return;
|
|
282
|
+
if (process.platform === "win32") {
|
|
283
|
+
try {
|
|
284
|
+
child.kill(signal);
|
|
285
|
+
}
|
|
286
|
+
catch { }
|
|
287
|
+
}
|
|
288
|
+
else
|
|
289
|
+
endGroup(pgid, signal);
|
|
290
|
+
};
|
|
291
|
+
child.stdout?.setEncoding("utf8").on("data", (chunk) => { if (stdout.length < cap)
|
|
292
|
+
stdout += chunk.slice(0, cap - stdout.length); });
|
|
293
|
+
child.stderr?.setEncoding("utf8").on("data", (chunk) => { if (stderr.length < cap)
|
|
294
|
+
stderr += chunk.slice(0, cap - stderr.length); });
|
|
295
|
+
child.once("error", error => {
|
|
296
|
+
const missing = error.code === "ENOENT";
|
|
297
|
+
finish({ code: missing ? 127 : -1, stdout, stderr, timedOut: false, notFound: missing });
|
|
298
|
+
});
|
|
299
|
+
child.once("exit", exitCode => { code = exitCode; end("SIGKILL"); });
|
|
300
|
+
child.once("close", () => finish({ code: timedOut ? 124 : code ?? -1, stdout, stderr, timedOut, notFound: false }));
|
|
301
|
+
timers.push(setTimeout(() => {
|
|
302
|
+
timedOut = true;
|
|
303
|
+
end("SIGTERM");
|
|
304
|
+
timers.push(setTimeout(() => end("SIGKILL"), KILL_GRACE_MS));
|
|
305
|
+
timers.push(setTimeout(() => { child.stdout?.destroy(); child.stderr?.destroy(); finish({ code: 124, stdout, stderr, timedOut: true, notFound: false }); }, KILL_GRACE_MS + 250));
|
|
306
|
+
}, options.timeoutMs));
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
export function givenRepos(given) {
|
|
310
|
+
return () => [...(given.repo === undefined ? [] : [given.repo]), ...(given.repos ?? []), ...(given.currentRepos?.() ?? [])];
|
|
311
|
+
}
|
|
312
|
+
export function statusProbe(options = {}) {
|
|
313
|
+
return async (file, args, runOptions) => {
|
|
314
|
+
const deadline = Date.now() + Math.min(runOptions?.timeoutMs ?? STATUS_TIMEOUT_MS, options.timeoutMs ?? STATUS_TIMEOUT_MS);
|
|
315
|
+
const env = options.env ?? process.env;
|
|
316
|
+
const untrusted = await excludedFolders({ store: options.store, databaseFile: options.databaseFile }, options.untrusted?.() ?? []);
|
|
317
|
+
if (untrusted === null)
|
|
318
|
+
return REFUSED;
|
|
319
|
+
const aclFor = options.aclFor ?? ((at) => boundedAcl(at));
|
|
320
|
+
const trustWith = (acl) => ({ path: env["PATH"] ?? "", uid: options.uid ?? process.getuid?.(), untrusted, acl });
|
|
321
|
+
const trust = trustWith(aclFor(deadline));
|
|
322
|
+
const found = resolveLaunch(file, trust);
|
|
323
|
+
if (!found.ok)
|
|
324
|
+
return found.missing ? NOT_FOUND : REFUSED;
|
|
325
|
+
const childEnv = { ...statusEnvironment(env, trust), PATH: launchPath(found, trust) };
|
|
326
|
+
const again = resolveLaunch(file, trustWith(aclFor(deadline)));
|
|
327
|
+
if (!again.ok || again.real !== found.real || again.file !== found.file)
|
|
328
|
+
return REFUSED;
|
|
329
|
+
let cwd;
|
|
330
|
+
try {
|
|
331
|
+
cwd = mkdtempSync(join(tmpdir(), "toolroll-status-"));
|
|
332
|
+
}
|
|
333
|
+
catch {
|
|
334
|
+
return REFUSED;
|
|
335
|
+
}
|
|
336
|
+
try {
|
|
337
|
+
return await runBoundedStatus(found.file, [...found.args, ...args], { env: childEnv, cwd, timeoutMs: deadline - Date.now(), maxBuffer: Math.min(runOptions?.maxBuffer ?? STATUS_MAX_BUFFER, STATUS_MAX_BUFFER) });
|
|
338
|
+
}
|
|
339
|
+
finally {
|
|
340
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
}
|