toolroll 0.9.108 → 0.9.110

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.
Files changed (187) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/dist/advanced-view.js +0 -1
  3. package/dist/agent-detect.js +244 -0
  4. package/dist/agent-fence.js +47 -5
  5. package/dist/agent-state.js +5 -2
  6. package/dist/agentconfig.js +2 -2
  7. package/dist/approval-summary.js +344 -0
  8. package/dist/assignment-ui.js +5 -5
  9. package/dist/assignment.js +5 -2
  10. package/dist/attest.js +2 -2
  11. package/dist/browser/terminal.js +1 -1
  12. package/dist/browser/workspace.css +2 -2
  13. package/dist/browser/workspace.js +37 -34
  14. package/dist/browser-crew.js +4 -4
  15. package/dist/browser-workspace.js +4 -11
  16. package/dist/builder.js +45 -3
  17. package/dist/chat-actions.js +24 -17
  18. package/dist/chat-controls.js +6 -5
  19. package/dist/chat-core.js +3 -1
  20. package/dist/chat-decide.js +102 -47
  21. package/dist/chat-delivery-state.js +2 -0
  22. package/dist/chat-delivery.js +3 -2
  23. package/dist/chat-live.js +2 -1
  24. package/dist/chat-quiet.js +3 -3
  25. package/dist/chat-task-actions.js +6 -0
  26. package/dist/chat-voice.js +2 -2
  27. package/dist/check-report.js +1 -1
  28. package/dist/cli-help.js +12 -13
  29. package/dist/cli.js +6 -3
  30. package/dist/console-chrome.js +10 -3
  31. package/dist/console-instance.js +10 -1
  32. package/dist/contracts/console-api.js +14 -8
  33. package/dist/contracts/engine-api.js +271 -0
  34. package/dist/contracts/handoff.js +4 -0
  35. package/dist/coordinator.js +2 -2
  36. package/dist/decision.js +17 -0
  37. package/dist/demo-chat.js +1 -1
  38. package/dist/demo.js +6 -0
  39. package/dist/design/kit.js +4 -3
  40. package/dist/discord-api.js +4 -3
  41. package/dist/discord-chat.js +3 -1
  42. package/dist/dispatch.js +3 -8
  43. package/dist/easy-start-args.js +7 -3
  44. package/dist/easy-start.js +25 -0
  45. package/dist/egress.js +72 -10
  46. package/dist/engine-handoff.js +34 -0
  47. package/dist/engine-reads.js +261 -0
  48. package/dist/eval-world.js +3 -2
  49. package/dist/evidence.js +2 -0
  50. package/dist/fast-start.js +55 -14
  51. package/dist/fast-terms.js +2 -2
  52. package/dist/first-run.js +10 -2
  53. package/dist/flexible-agents.js +234 -0
  54. package/dist/flexible-answers-schema.js +61 -0
  55. package/dist/flexible-answers.js +889 -0
  56. package/dist/flexible-available.js +1 -0
  57. package/dist/flexible-custody.js +494 -0
  58. package/dist/flexible-import.js +782 -0
  59. package/dist/flexible-launch-schema.js +72 -0
  60. package/dist/flexible-launch.js +331 -0
  61. package/dist/flexible-launcher.js +88 -0
  62. package/dist/flexible-levels.js +34 -0
  63. package/dist/flexible-merge-guard.js +47 -0
  64. package/dist/flexible-refs.js +47 -0
  65. package/dist/flexible-relay-cancel.js +19 -0
  66. package/dist/flexible-relay-schema.js +55 -0
  67. package/dist/flexible-relay-upstreams.js +36 -0
  68. package/dist/flexible-relay.js +1032 -0
  69. package/dist/flexible-schema.js +32 -0
  70. package/dist/flexible-session.js +732 -0
  71. package/dist/flexible-ship-schema.js +81 -0
  72. package/dist/flexible-ship.js +694 -0
  73. package/dist/flexible-start.js +80 -0
  74. package/dist/flexible-thread-schema.js +30 -0
  75. package/dist/flexible-thread.js +676 -0
  76. package/dist/flexible-turns-schema.js +69 -0
  77. package/dist/flexible-turns.js +490 -0
  78. package/dist/flexible-ui.js +44 -0
  79. package/dist/flexible.js +368 -0
  80. package/dist/flow-actions.js +6 -4
  81. package/dist/flow-steps.js +1 -1
  82. package/dist/flows-cli.js +2 -1
  83. package/dist/git-views.js +44 -41
  84. package/dist/guides.js +11 -10
  85. package/dist/install-mode.js +225 -0
  86. package/dist/keymap.js +1 -1
  87. package/dist/lead-context.js +1 -1
  88. package/dist/lead-doors.js +7 -1
  89. package/dist/limits-ui.js +8 -1
  90. package/dist/live-bus.js +70 -13
  91. package/dist/live-reply.js +6 -0
  92. package/dist/live-terminal.js +425 -29
  93. package/dist/local-owner.js +6 -0
  94. package/dist/model-choices.js +12 -6
  95. package/dist/needs-you.js +3 -3
  96. package/dist/nightly-github.js +2 -2
  97. package/dist/notification-delivery.js +11 -0
  98. package/dist/operate.js +185 -33
  99. package/dist/outbound-privacy.js +204 -0
  100. package/dist/passwordless.js +2 -1
  101. package/dist/project-delete-ui.js +2 -1
  102. package/dist/project-delete.js +15 -0
  103. package/dist/provider-auth.js +1 -1
  104. package/dist/provider-connection.js +5 -3
  105. package/dist/publish.js +5 -0
  106. package/dist/pull-request-flow.js +26 -7
  107. package/dist/push.js +5 -1
  108. package/dist/release-card.js +11 -3
  109. package/dist/release.js +11 -1
  110. package/dist/result-actions.js +3 -0
  111. package/dist/result-evidence-readers.js +8 -2
  112. package/dist/result-review.js +20 -9
  113. package/dist/route-store.js +2 -2
  114. package/dist/sandbox-launch.js +32 -8
  115. package/dist/sandbox-runtime.js +16 -7
  116. package/dist/sandbox-tools.js +176 -0
  117. package/dist/scope.js +12 -36
  118. package/dist/scout-net.js +7 -2
  119. package/dist/serve.js +90 -40
  120. package/dist/server/chat.js +9 -9
  121. package/dist/server/chrome.js +19 -17
  122. package/dist/server/console.css +33 -6
  123. package/dist/server/engine-api.js +378 -0
  124. package/dist/server/flows.js +2 -2
  125. package/dist/server/guards.js +10 -4
  126. package/dist/server/home.js +37 -5
  127. package/dist/server/http.js +11 -2
  128. package/dist/server/in-place.js +5 -4
  129. package/dist/server/kit.js +2 -2
  130. package/dist/server/link-redemption.js +52 -0
  131. package/dist/server/live.js +100 -10
  132. package/dist/server/mutation-key.js +21 -7
  133. package/dist/server/pages.js +129 -74
  134. package/dist/server/people-tokens.js +17 -41
  135. package/dist/server/render-chat.js +2 -1
  136. package/dist/server/render-history.js +18 -4
  137. package/dist/server/render-new-task.js +4 -4
  138. package/dist/server/render-pages.js +21 -39
  139. package/dist/server/render-people.js +2 -2
  140. package/dist/server/render-projects.js +5 -5
  141. package/dist/server/render-results.js +30 -14
  142. package/dist/server/render-tasks.js +155 -155
  143. package/dist/server/retired-routes.js +37 -4
  144. package/dist/server/route-policy.js +8 -0
  145. package/dist/server/route-table.js +43 -5
  146. package/dist/server/settings-computers.js +140 -0
  147. package/dist/server/settings-scripts.js +15 -0
  148. package/dist/server/settings-section-table.js +59 -0
  149. package/dist/server/settings-sections.js +291 -0
  150. package/dist/server/settings.js +247 -28
  151. package/dist/server/task-views.js +55 -15
  152. package/dist/server/tasks.js +168 -98
  153. package/dist/server/terminals.js +181 -28
  154. package/dist/server/welcome.js +40 -14
  155. package/dist/settings-groups-ui.js +26 -107
  156. package/dist/settings-proofs.js +18 -11
  157. package/dist/slack-api.js +4 -3
  158. package/dist/slack-chat.js +5 -3
  159. package/dist/spend.js +46 -12
  160. package/dist/sqlite-telemetry.js +70 -21
  161. package/dist/status-probe.js +343 -0
  162. package/dist/store.js +206 -10
  163. package/dist/task-link.js +64 -0
  164. package/dist/teams-api.js +4 -3
  165. package/dist/teams-chat.js +4 -2
  166. package/dist/telegram-lead.js +2 -1
  167. package/dist/telegram-live.js +2 -1
  168. package/dist/telegram-team.js +2 -1
  169. package/dist/telegram.js +17 -5
  170. package/dist/trusted-path.js +162 -0
  171. package/dist/tui/darwin-arm64/toolroll-tui +0 -0
  172. package/dist/tui/darwin-x64/toolroll-tui +0 -0
  173. package/dist/tui/linux-arm64/toolroll-tui +0 -0
  174. package/dist/tui/linux-x64/toolroll-tui +0 -0
  175. package/dist/tui-launch.js +282 -0
  176. package/dist/tui-source.js +5 -3
  177. package/dist/tui.js +2 -2
  178. package/dist/typed-words-schema.js +18 -0
  179. package/dist/webhooks.js +8 -4
  180. package/dist/welcome-facts.js +25 -17
  181. package/dist/work-index.js +126 -29
  182. package/dist/work-reads.js +279 -0
  183. package/dist/workspace-ui.js +5 -0
  184. package/package.json +4 -2
  185. package/scripts/crash-canary.mjs +1 -1
  186. package/scripts/pilot.mjs +1 -1
  187. package/scripts/provider-canary.mjs +5 -3
@@ -1,3 +1,9 @@
1
+ let writes = 0;
2
+ export const writesSoFar = () => writes;
3
+ const ownTemp = (op) => /^(?:CREATE\s+TEMP\s+TABLE\s+"so_work_ranked_\d+"|DROP\s+TABLE\s+IF\s+EXISTS\s+temp\."so_work_ranked_\d+"$)/i.test(op);
4
+ const opening = (op) => /^(?:BEGIN(?:\s+(?:DEFERRED|IMMEDIATE|EXCLUSIVE))?(?:\s+TRANSACTION)?|SAVEPOINT\s+\w+)\s*;?$/i.test(op);
5
+ const settingOnly = (op) => /^PRAGMA\s+busy_timeout\s*=\s*\d+\s*;?$/i.test(op);
6
+ const ending = (op) => /^(?:COMMIT|END|ROLLBACK|RELEASE)\b[^;]*;?\s*$/i.test(op);
1
7
  const states = new WeakMap();
2
8
  export function measureWriteWait(db, body) {
3
9
  const state = states.get(db);
@@ -34,34 +40,77 @@ export function instrumentDatabase(db, telemetry) {
34
40
  telemetry.writeHold.observe(elapsed(heldAt));
35
41
  heldAt = null;
36
42
  } };
43
+ let openedAt = null;
44
+ const native = db;
45
+ const counted = (counts, body) => {
46
+ if (counts)
47
+ writes++;
48
+ try {
49
+ return body();
50
+ }
51
+ catch (error) {
52
+ writes++;
53
+ throw error;
54
+ }
55
+ };
56
+ const execNow = (sql, op) => {
57
+ if (/^BEGIN\s+(?:IMMEDIATE|EXCLUSIVE)\b/i.test(op)) {
58
+ const at = telemetry.clock();
59
+ try {
60
+ db.exec(sql);
61
+ heldAt = telemetry.clock();
62
+ }
63
+ finally {
64
+ if (!state.measuringWait)
65
+ telemetry.writeWait.observe(elapsed(at));
66
+ }
67
+ }
68
+ else if (/^(?:COMMIT|END|ROLLBACK(?!\s+TO))\b/i.test(op)) {
69
+ db.exec(sql);
70
+ finish();
71
+ }
72
+ else
73
+ standalone(writing(op), () => db.exec(sql));
74
+ };
75
+ const counts = (op, path, write) => {
76
+ if (ownTemp(op) || settingOnly(op))
77
+ return false;
78
+ if (opening(op)) {
79
+ if (openedAt === null && native.isTransaction !== true)
80
+ openedAt = writes;
81
+ return false;
82
+ }
83
+ if (ending(op))
84
+ return openedAt === null || writes !== openedAt;
85
+ return path !== 'read' || write;
86
+ };
87
+ const settled = (op) => {
88
+ if (native.isTransaction === false || (native.isTransaction === undefined && /^(?:COMMIT|END|ROLLBACK(?!\s+TO))\b/i.test(op)))
89
+ openedAt = null;
90
+ };
91
+ const tracked = (op, path, write, body) => {
92
+ try {
93
+ return counted(counts(op, path, write), body);
94
+ }
95
+ finally {
96
+ settled(op);
97
+ }
98
+ };
37
99
  const methods = {
38
100
  exec(sql) {
39
101
  const op = operation(sql);
40
- if (/^BEGIN\s+(?:IMMEDIATE|EXCLUSIVE)\b/i.test(op)) {
41
- const at = telemetry.clock();
42
- try {
43
- db.exec(sql);
44
- heldAt = telemetry.clock();
45
- }
46
- finally {
47
- if (!state.measuringWait)
48
- telemetry.writeWait.observe(elapsed(at));
49
- }
50
- }
51
- else if (/^(?:COMMIT|END|ROLLBACK(?!\s+TO))\b/i.test(op)) {
52
- db.exec(sql);
53
- finish();
54
- }
55
- else
56
- standalone(writing(op), () => db.exec(sql));
102
+ tracked(op, 'exec', true, () => execNow(sql, op));
57
103
  },
58
104
  prepare(sql) {
59
- const statement = db.prepare(sql), write = writing(operation(sql));
105
+ const op = operation(sql), statement = db.prepare(sql), write = writing(op);
60
106
  const methods = {
61
- run: (...params) => standalone(write, () => statement.run(...params)),
62
- get: (...params) => standalone(write, () => statement.get(...params)),
63
- all: (...params) => standalone(write, () => statement.all(...params)),
107
+ run: (...params) => tracked(op, 'run', write, () => standalone(write, () => statement.run(...params))),
108
+ get: (...params) => tracked(op, 'read', write, () => standalone(write, () => statement.get(...params))),
109
+ all: (...params) => tracked(op, 'read', write, () => standalone(write, () => statement.all(...params))),
64
110
  };
111
+ const rows = statement;
112
+ if (typeof rows.iterate === 'function')
113
+ Object.assign(methods, { iterate: (...params) => tracked(op, 'read', write, () => rows.iterate(...params)) });
65
114
  return preservingNative(statement, methods);
66
115
  },
67
116
  close() { try {
@@ -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
+ }