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.
- package/CHANGELOG.md +69 -0
- package/dist/advanced-view.js +0 -1
- package/dist/agent-detect.js +244 -0
- package/dist/agent-fence.js +47 -5
- package/dist/agent-state.js +5 -2
- package/dist/agentconfig.js +2 -2
- package/dist/approval-summary.js +344 -0
- package/dist/assignment-ui.js +5 -5
- package/dist/assignment.js +5 -2
- package/dist/attest.js +2 -2
- package/dist/browser/terminal.js +1 -1
- package/dist/browser/workspace.css +2 -2
- package/dist/browser/workspace.js +37 -34
- package/dist/browser-crew.js +4 -4
- package/dist/browser-workspace.js +4 -11
- package/dist/builder.js +45 -3
- package/dist/chat-actions.js +24 -17
- package/dist/chat-controls.js +6 -5
- package/dist/chat-core.js +3 -1
- package/dist/chat-decide.js +102 -47
- 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-quiet.js +3 -3
- package/dist/chat-task-actions.js +6 -0
- package/dist/chat-voice.js +2 -2
- package/dist/check-report.js +1 -1
- package/dist/cli-help.js +12 -13
- package/dist/cli.js +6 -3
- package/dist/console-chrome.js +10 -3
- package/dist/console-instance.js +10 -1
- package/dist/contracts/console-api.js +14 -8
- package/dist/contracts/engine-api.js +271 -0
- package/dist/contracts/handoff.js +4 -0
- package/dist/coordinator.js +2 -2
- package/dist/decision.js +17 -0
- package/dist/demo-chat.js +1 -1
- package/dist/demo.js +6 -0
- package/dist/design/kit.js +4 -3
- package/dist/discord-api.js +4 -3
- package/dist/discord-chat.js +3 -1
- package/dist/dispatch.js +3 -8
- package/dist/easy-start-args.js +7 -3
- package/dist/easy-start.js +25 -0
- package/dist/egress.js +72 -10
- package/dist/engine-handoff.js +34 -0
- package/dist/engine-reads.js +261 -0
- package/dist/eval-world.js +3 -2
- package/dist/evidence.js +2 -0
- package/dist/fast-start.js +55 -14
- 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-available.js +1 -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-schema.js +32 -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-schema.js +30 -0
- package/dist/flexible-thread.js +676 -0
- package/dist/flexible-turns-schema.js +69 -0
- package/dist/flexible-turns.js +490 -0
- package/dist/flexible-ui.js +44 -0
- package/dist/flexible.js +368 -0
- 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 +44 -41
- package/dist/guides.js +11 -10
- package/dist/install-mode.js +225 -0
- package/dist/keymap.js +1 -1
- package/dist/lead-context.js +1 -1
- package/dist/lead-doors.js +7 -1
- package/dist/limits-ui.js +8 -1
- package/dist/live-bus.js +70 -13
- package/dist/live-reply.js +6 -0
- package/dist/live-terminal.js +425 -29
- package/dist/local-owner.js +6 -0
- package/dist/model-choices.js +12 -6
- package/dist/needs-you.js +3 -3
- package/dist/nightly-github.js +2 -2
- package/dist/notification-delivery.js +11 -0
- package/dist/operate.js +185 -33
- package/dist/outbound-privacy.js +204 -0
- package/dist/passwordless.js +2 -1
- package/dist/project-delete-ui.js +2 -1
- package/dist/project-delete.js +15 -0
- package/dist/provider-auth.js +1 -1
- package/dist/provider-connection.js +5 -3
- 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 +11 -1
- package/dist/result-actions.js +3 -0
- package/dist/result-evidence-readers.js +8 -2
- package/dist/result-review.js +20 -9
- package/dist/route-store.js +2 -2
- 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 +90 -40
- package/dist/server/chat.js +9 -9
- package/dist/server/chrome.js +19 -17
- 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/guards.js +10 -4
- package/dist/server/home.js +37 -5
- package/dist/server/http.js +11 -2
- package/dist/server/in-place.js +5 -4
- package/dist/server/kit.js +2 -2
- package/dist/server/link-redemption.js +52 -0
- package/dist/server/live.js +100 -10
- package/dist/server/mutation-key.js +21 -7
- package/dist/server/pages.js +129 -74
- package/dist/server/people-tokens.js +17 -41
- package/dist/server/render-chat.js +2 -1
- package/dist/server/render-history.js +18 -4
- package/dist/server/render-new-task.js +4 -4
- package/dist/server/render-pages.js +21 -39
- package/dist/server/render-people.js +2 -2
- package/dist/server/render-projects.js +5 -5
- package/dist/server/render-results.js +30 -14
- package/dist/server/render-tasks.js +155 -155
- package/dist/server/retired-routes.js +37 -4
- package/dist/server/route-policy.js +8 -0
- package/dist/server/route-table.js +43 -5
- package/dist/server/settings-computers.js +140 -0
- package/dist/server/settings-scripts.js +15 -0
- package/dist/server/settings-section-table.js +59 -0
- package/dist/server/settings-sections.js +291 -0
- package/dist/server/settings.js +247 -28
- package/dist/server/task-views.js +55 -15
- package/dist/server/tasks.js +168 -98
- package/dist/server/terminals.js +181 -28
- package/dist/server/welcome.js +40 -14
- package/dist/settings-groups-ui.js +26 -107
- package/dist/settings-proofs.js +18 -11
- package/dist/slack-api.js +4 -3
- package/dist/slack-chat.js +5 -3
- package/dist/spend.js +46 -12
- package/dist/sqlite-telemetry.js +70 -21
- package/dist/status-probe.js +343 -0
- package/dist/store.js +206 -10
- package/dist/task-link.js +64 -0
- 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/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 +2 -2
- package/dist/typed-words-schema.js +18 -0
- package/dist/webhooks.js +8 -4
- package/dist/welcome-facts.js +25 -17
- package/dist/work-index.js +126 -29
- package/dist/work-reads.js +279 -0
- package/dist/workspace-ui.js +5 -0
- package/package.json +4 -2
- package/scripts/crash-canary.mjs +1 -1
- package/scripts/pilot.mjs +1 -1
- package/scripts/provider-canary.mjs +5 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const FLEXIBLE_AVAILABLE = false;
|
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
2
|
+
import { constants } from "node:os";
|
|
3
|
+
import { readdirSync, readFileSync, readlinkSync, realpathSync, statSync } from "node:fs";
|
|
4
|
+
import { dirname } from "node:path";
|
|
5
|
+
import { provenDeadByBootChange } from "./boot-identity.js";
|
|
6
|
+
function startValue(start) {
|
|
7
|
+
if (start === null)
|
|
8
|
+
return null;
|
|
9
|
+
const match = /^[ts](\d{1,20})$/.exec(start);
|
|
10
|
+
return match === null ? null : Number(match[1]);
|
|
11
|
+
}
|
|
12
|
+
function bornSinceLeader(row, leader) {
|
|
13
|
+
if (leader === null)
|
|
14
|
+
return false;
|
|
15
|
+
const born = startValue(row.start), since = startValue(leader.start);
|
|
16
|
+
return born !== null && since !== null && born >= since && row.start.charAt(0) === leader.start.charAt(0);
|
|
17
|
+
}
|
|
18
|
+
export function scanCustody(custody, io, options = {}) {
|
|
19
|
+
const rows = io.table();
|
|
20
|
+
if (rows === null)
|
|
21
|
+
return { members: [], blockers: [], incomplete: "the process table couldn't be read", namespaces: [], witnesses: [], table: [] };
|
|
22
|
+
const byPid = new Map(rows.map(row => [row.pid, row]));
|
|
23
|
+
const live = (row) => row !== undefined && !row.zombie && row.pid !== io.self && row.pid > 1;
|
|
24
|
+
const members = new Map();
|
|
25
|
+
const blockers = new Map();
|
|
26
|
+
const leader = custody.leader;
|
|
27
|
+
if (leader !== null) {
|
|
28
|
+
const holder = byPid.get(leader.pid);
|
|
29
|
+
const leaderLives = live(holder) && holder.start === leader.start;
|
|
30
|
+
for (const row of rows) {
|
|
31
|
+
if (!live(row) || row.pgid !== leader.pid)
|
|
32
|
+
continue;
|
|
33
|
+
if (leaderLives && (row.pid === leader.pid || bornSinceLeader(row, leader)))
|
|
34
|
+
members.set(row.pid, row);
|
|
35
|
+
else if (!leaderLives && bornSinceLeader(row, leader))
|
|
36
|
+
blockers.set(row.pid, "orphaned-group");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (const witness of custody.witnesses) {
|
|
40
|
+
const row = byPid.get(witness.pid);
|
|
41
|
+
if (live(row) && row.start === witness.start)
|
|
42
|
+
members.set(row.pid, row);
|
|
43
|
+
}
|
|
44
|
+
let incomplete = null;
|
|
45
|
+
const namespaces = new Set(custody.namespaces);
|
|
46
|
+
if (io.platform === "linux" && namespaces.size > 0) {
|
|
47
|
+
for (const row of rows) {
|
|
48
|
+
if (!live(row) || members.has(row.pid))
|
|
49
|
+
continue;
|
|
50
|
+
const ns = io.namespaceOf(row.pid);
|
|
51
|
+
if (ns === "unreadable") {
|
|
52
|
+
if (row.uid === process.getuid?.() && bornSinceLeader(row, leader))
|
|
53
|
+
incomplete ??= `process ${row.pid}'s namespace couldn't be read`;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (ns !== null && namespaces.has(ns))
|
|
57
|
+
members.set(row.pid, row);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
for (let grew = true; grew;) {
|
|
61
|
+
grew = false;
|
|
62
|
+
for (const row of rows)
|
|
63
|
+
if (live(row) && !members.has(row.pid) && members.has(row.ppid)) {
|
|
64
|
+
members.set(row.pid, row);
|
|
65
|
+
grew = true;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (options.holders !== false) {
|
|
69
|
+
const holding = io.holdersOf(custody.folder);
|
|
70
|
+
if (holding === null)
|
|
71
|
+
incomplete ??= "the programs using the thread's folder couldn't all be listed";
|
|
72
|
+
else
|
|
73
|
+
for (const pid of holding) {
|
|
74
|
+
if (pid === io.self)
|
|
75
|
+
continue;
|
|
76
|
+
const row = byPid.get(pid);
|
|
77
|
+
if (row === undefined) {
|
|
78
|
+
incomplete ??= `process ${pid} appeared while the scan ran`;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (live(row) && !members.has(pid))
|
|
82
|
+
blockers.set(pid, "in-folder");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
for (const [pid, row] of members)
|
|
86
|
+
if (row.start === null) {
|
|
87
|
+
members.delete(pid);
|
|
88
|
+
blockers.set(pid, "unidentified");
|
|
89
|
+
}
|
|
90
|
+
for (const pid of members.keys())
|
|
91
|
+
blockers.delete(pid);
|
|
92
|
+
const known = new Set(custody.witnesses.map(one => `${one.pid}:${one.start}`));
|
|
93
|
+
const proven = [...members.values()];
|
|
94
|
+
const witnesses = proven.filter(row => !known.has(`${row.pid}:${row.start}`)).map(row => ({ pid: row.pid, start: row.start }));
|
|
95
|
+
const freshNamespaces = [];
|
|
96
|
+
if (io.platform === "linux") {
|
|
97
|
+
const own = io.namespaceOf(io.self);
|
|
98
|
+
for (const row of proven) {
|
|
99
|
+
const ns = io.namespaceOf(row.pid);
|
|
100
|
+
if (typeof ns === "string" && ns !== "unreadable" && ns !== own && !namespaces.has(ns)) {
|
|
101
|
+
namespaces.add(ns);
|
|
102
|
+
freshNamespaces.push(ns);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return { members: proven.sort((a, b) => a.pid - b.pid), blockers: [...blockers].map(([pid, why]) => ({ pid, why })).sort((a, b) => a.pid - b.pid), incomplete, namespaces: freshNamespaces, witnesses, table: rows };
|
|
107
|
+
}
|
|
108
|
+
export function observeCustody(custody, io) {
|
|
109
|
+
const scan = scanCustody(custody, io, { holders: false });
|
|
110
|
+
return { namespaces: scan.namespaces, witnesses: scan.witnesses };
|
|
111
|
+
}
|
|
112
|
+
function bootChanged(custody, io) {
|
|
113
|
+
return provenDeadByBootChange({ host: custody.host, bootId: custody.bootId, machineId: custody.machineId }, { bootId: io.bootId() });
|
|
114
|
+
}
|
|
115
|
+
const BLOCKER_WORDS = Object.freeze({
|
|
116
|
+
"in-folder": "is using the thread's folder and isn't proven to be the agent's, so it wasn't stopped. Close it, then try again",
|
|
117
|
+
"orphaned-group": "is left in the agent's process group after the agent ended, and can't be proven to be the agent's, so it wasn't stopped. Stop it, then try again",
|
|
118
|
+
"unidentified": "couldn't be identified well enough to stop safely. Stop it, then try again",
|
|
119
|
+
});
|
|
120
|
+
function why(scan, problem) {
|
|
121
|
+
if (problem !== null)
|
|
122
|
+
return problem;
|
|
123
|
+
if (scan.incomplete !== null)
|
|
124
|
+
return scan.incomplete;
|
|
125
|
+
if (scan.members.length > 0)
|
|
126
|
+
return `${scan.members.length === 1 ? "1 of the agent's processes is" : `${scan.members.length} of the agent's processes are`} still running`;
|
|
127
|
+
const first = scan.blockers[0];
|
|
128
|
+
return `process ${first.pid} ${BLOCKER_WORDS[first.why]}${scan.blockers.length > 1 ? ` (and ${scan.blockers.length - 1} more)` : ""}`;
|
|
129
|
+
}
|
|
130
|
+
export function custodySettled(custody, io) {
|
|
131
|
+
if (bootChanged(custody, io))
|
|
132
|
+
return { verified: true, how: "boot-changed", namespaces: [], witnesses: [] };
|
|
133
|
+
const scan = scanCustody(custody, io);
|
|
134
|
+
if (scan.members.length === 0 && scan.blockers.length === 0 && scan.incomplete === null)
|
|
135
|
+
return { verified: true, how: "none-left", namespaces: scan.namespaces, witnesses: scan.witnesses };
|
|
136
|
+
return { verified: false, survivors: scan.members.map(row => row.pid), blockers: scan.blockers, why: why(scan, null), namespaces: scan.namespaces, witnesses: scan.witnesses };
|
|
137
|
+
}
|
|
138
|
+
export async function stopCustody(custody, io, options = {}) {
|
|
139
|
+
if (bootChanged(custody, io))
|
|
140
|
+
return { verified: true, how: "boot-changed", namespaces: [], witnesses: [] };
|
|
141
|
+
const deadline = Date.now() + (options.deadlineMs ?? 10_000);
|
|
142
|
+
let current = custody;
|
|
143
|
+
const seenNamespaces = [], seenWitnesses = [];
|
|
144
|
+
const learn = (scan) => {
|
|
145
|
+
seenNamespaces.push(...scan.namespaces);
|
|
146
|
+
seenWitnesses.push(...scan.witnesses);
|
|
147
|
+
current = { ...current, namespaces: [...current.namespaces, ...scan.namespaces], witnesses: [...current.witnesses, ...scan.witnesses] };
|
|
148
|
+
};
|
|
149
|
+
const frozen = new Map();
|
|
150
|
+
const signalled = new Map();
|
|
151
|
+
let problem = null;
|
|
152
|
+
let bare = false;
|
|
153
|
+
const send = (targets, signal) => {
|
|
154
|
+
const leader = custody.leader;
|
|
155
|
+
const viaHandle = options.leaderSignal !== undefined && leader !== null ? targets.find(one => one.pid === leader.pid && one.start === leader.start) : undefined;
|
|
156
|
+
const rest = viaHandle === undefined ? targets : targets.filter(one => one !== viaHandle);
|
|
157
|
+
const results = rest.length > 0 ? io.signal(rest, signal) : new Map();
|
|
158
|
+
if (viaHandle !== undefined)
|
|
159
|
+
results.set(viaHandle.pid, options.leaderSignal(signal));
|
|
160
|
+
for (const target of targets) {
|
|
161
|
+
const result = results.get(target.pid) ?? "unavailable";
|
|
162
|
+
if (result === "sent" && target !== viaHandle && !io.identityBound)
|
|
163
|
+
bare = true;
|
|
164
|
+
if (result === "sent")
|
|
165
|
+
signalled.set(target.pid, target.start);
|
|
166
|
+
if (result === "unavailable")
|
|
167
|
+
problem ??= "this computer can't send a signal bound to a process's identity, so nothing was stopped";
|
|
168
|
+
}
|
|
169
|
+
return results;
|
|
170
|
+
};
|
|
171
|
+
const raced = (table) => {
|
|
172
|
+
for (const row of table) {
|
|
173
|
+
const was = signalled.get(row.pid);
|
|
174
|
+
if (was !== undefined && row.start !== was)
|
|
175
|
+
return `process ${row.pid} was replaced by another while it was being stopped, so the stop can't be proven`;
|
|
176
|
+
}
|
|
177
|
+
return null;
|
|
178
|
+
};
|
|
179
|
+
const unresolved = (scan) => {
|
|
180
|
+
for (const [pid, start] of frozen)
|
|
181
|
+
if (scan.members.some(row => row.pid === pid && row.start === start))
|
|
182
|
+
io.signal([{ pid, start }], "SIGCONT");
|
|
183
|
+
return { verified: false, survivors: scan.members.map(row => row.pid), blockers: scan.blockers, why: why(scan, problem), namespaces: seenNamespaces, witnesses: seenWitnesses };
|
|
184
|
+
};
|
|
185
|
+
let full = true;
|
|
186
|
+
for (;;) {
|
|
187
|
+
const scan = scanCustody(current, io, { holders: full });
|
|
188
|
+
learn(scan);
|
|
189
|
+
problem ??= raced(scan.table);
|
|
190
|
+
if (problem !== null)
|
|
191
|
+
return unresolved(scan);
|
|
192
|
+
if (scan.members.length === 0) {
|
|
193
|
+
if (!full) {
|
|
194
|
+
full = true;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (scan.blockers.length === 0 && scan.incomplete === null)
|
|
198
|
+
return { verified: true, how: bare ? "best-effort" : "none-left", namespaces: seenNamespaces, witnesses: seenWitnesses };
|
|
199
|
+
return unresolved(scan);
|
|
200
|
+
}
|
|
201
|
+
full = false;
|
|
202
|
+
const notYetFrozen = scan.members.filter(row => frozen.get(row.pid) !== row.start);
|
|
203
|
+
if (notYetFrozen.length > 0) {
|
|
204
|
+
const results = send(notYetFrozen, "SIGSTOP");
|
|
205
|
+
for (const row of notYetFrozen)
|
|
206
|
+
if (results.get(row.pid) === "sent")
|
|
207
|
+
frozen.set(row.pid, row.start);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
send(scan.members, "SIGKILL");
|
|
211
|
+
}
|
|
212
|
+
if (problem !== null)
|
|
213
|
+
return unresolved(scan);
|
|
214
|
+
if (Date.now() >= deadline) {
|
|
215
|
+
const last = scanCustody(current, io);
|
|
216
|
+
learn(last);
|
|
217
|
+
problem ??= raced(last.table);
|
|
218
|
+
if (problem === null && last.members.length === 0 && last.blockers.length === 0 && last.incomplete === null)
|
|
219
|
+
return { verified: true, how: bare ? "best-effort" : "none-left", namespaces: seenNamespaces, witnesses: seenWitnesses };
|
|
220
|
+
return unresolved(last);
|
|
221
|
+
}
|
|
222
|
+
await io.sleep(notYetFrozen.length > 0 ? 20 : 100);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
226
|
+
function lstartSeconds(text) {
|
|
227
|
+
const match = /^(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) {1,2}(\d{1,2}) (\d{2}):(\d{2}):(\d{2}) (\d{4})$/.exec(text.trim());
|
|
228
|
+
if (match === null)
|
|
229
|
+
return null;
|
|
230
|
+
const ms = Date.UTC(Number(match[6]), MONTHS.indexOf(match[1]), Number(match[2]), Number(match[3]), Number(match[4]), Number(match[5]));
|
|
231
|
+
return Number.isFinite(ms) ? Math.floor(ms / 1000) : null;
|
|
232
|
+
}
|
|
233
|
+
function macTable() {
|
|
234
|
+
let text;
|
|
235
|
+
try {
|
|
236
|
+
text = execFileSync("/bin/ps", ["-axo", "pid=,ppid=,pgid=,uid=,stat=,lstart="], {
|
|
237
|
+
encoding: "utf8", env: { PATH: "/usr/bin:/bin", LC_ALL: "C", TZ: "UTC" }, stdio: ["ignore", "pipe", "ignore"], timeout: 5_000, killSignal: "SIGKILL", maxBuffer: 16 * 1024 * 1024,
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
const rows = [];
|
|
244
|
+
for (const line of text.split("\n")) {
|
|
245
|
+
if (line.trim() === "")
|
|
246
|
+
continue;
|
|
247
|
+
const match = /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(-?\d+)\s+(\S+)\s+(.+)$/.exec(line);
|
|
248
|
+
if (match === null)
|
|
249
|
+
return null;
|
|
250
|
+
const seconds = lstartSeconds(match[6]);
|
|
251
|
+
rows.push({ pid: Number(match[1]), ppid: Number(match[2]), pgid: Number(match[3]), uid: Number(match[4]), zombie: match[5].startsWith("Z"), start: seconds === null ? null : `s${seconds}` });
|
|
252
|
+
}
|
|
253
|
+
return rows;
|
|
254
|
+
}
|
|
255
|
+
const REAL_PROC_FS = { readdir: path => readdirSync(path), readFile: path => readFileSync(path, "utf8"), statUid: path => statSync(path).uid };
|
|
256
|
+
const goneCode = (error) => { const code = error.code; return code === "ENOENT" || code === "ESRCH"; };
|
|
257
|
+
function linuxRow(name, fs = REAL_PROC_FS) {
|
|
258
|
+
let stat;
|
|
259
|
+
try {
|
|
260
|
+
stat = fs.readFile(`/proc/${name}/stat`);
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
return goneCode(error) ? "gone" : null;
|
|
264
|
+
}
|
|
265
|
+
const close = stat.lastIndexOf(")");
|
|
266
|
+
const fields = stat.slice(close + 2).trim().split(/\s+/);
|
|
267
|
+
if (close < 0 || fields.length < 20)
|
|
268
|
+
return null;
|
|
269
|
+
let uid;
|
|
270
|
+
try {
|
|
271
|
+
uid = fs.statUid(`/proc/${name}`);
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
return goneCode(error) ? "gone" : null;
|
|
275
|
+
}
|
|
276
|
+
return { pid: Number(name), ppid: Number(fields[1]), pgid: Number(fields[2]), uid, zombie: fields[0] === "Z" || fields[0] === "X", start: /^\d+$/.test(fields[19]) ? `t${fields[19]}` : null };
|
|
277
|
+
}
|
|
278
|
+
export function linuxTable(fs = REAL_PROC_FS) {
|
|
279
|
+
let names;
|
|
280
|
+
try {
|
|
281
|
+
names = fs.readdir("/proc").filter(name => /^\d+$/.test(name));
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
const rows = [];
|
|
287
|
+
for (const name of names) {
|
|
288
|
+
const row = linuxRow(name, fs);
|
|
289
|
+
if (row === "gone")
|
|
290
|
+
continue;
|
|
291
|
+
if (row === null)
|
|
292
|
+
return null;
|
|
293
|
+
rows.push(row);
|
|
294
|
+
}
|
|
295
|
+
return rows;
|
|
296
|
+
}
|
|
297
|
+
function linuxNamespace(pid) {
|
|
298
|
+
try {
|
|
299
|
+
return readlinkSync(`/proc/${pid}/ns/pid`);
|
|
300
|
+
}
|
|
301
|
+
catch (error) {
|
|
302
|
+
const code = error.code;
|
|
303
|
+
return code === "ENOENT" || code === "ESRCH" ? null : "unreadable";
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
const insideOf = (folder) => (path) => path === folder || path.startsWith(`${folder}/`);
|
|
307
|
+
function linuxHolders(folder) {
|
|
308
|
+
const inside = insideOf(folder);
|
|
309
|
+
let names;
|
|
310
|
+
try {
|
|
311
|
+
names = readdirSync("/proc").filter(name => /^\d+$/.test(name));
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
const found = [];
|
|
317
|
+
const uid = process.getuid?.();
|
|
318
|
+
for (const name of names) {
|
|
319
|
+
const links = [];
|
|
320
|
+
try {
|
|
321
|
+
links.push(readlinkSync(`/proc/${name}/cwd`));
|
|
322
|
+
for (const fd of readdirSync(`/proc/${name}/fd`)) {
|
|
323
|
+
try {
|
|
324
|
+
links.push(readlinkSync(`/proc/${name}/fd/${fd}`));
|
|
325
|
+
}
|
|
326
|
+
catch { }
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
catch (error) {
|
|
330
|
+
const code = error.code;
|
|
331
|
+
if (code === "ENOENT" || code === "ESRCH")
|
|
332
|
+
continue;
|
|
333
|
+
try {
|
|
334
|
+
if (statSync(`/proc/${name}`).uid !== uid)
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
catch {
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (links.some(inside))
|
|
343
|
+
found.push(Number(name));
|
|
344
|
+
}
|
|
345
|
+
return found;
|
|
346
|
+
}
|
|
347
|
+
function macHolders(folder) {
|
|
348
|
+
const inside = insideOf(folder);
|
|
349
|
+
const out = spawnSync("/usr/sbin/lsof", ["-n", "-P", "-w", "-u", String(process.getuid?.() ?? ""), "-F", "pn"], {
|
|
350
|
+
encoding: "utf8", env: { PATH: "/usr/bin:/bin:/usr/sbin", LC_ALL: "C" }, timeout: 20_000, killSignal: "SIGKILL", maxBuffer: 128 * 1024 * 1024,
|
|
351
|
+
});
|
|
352
|
+
if (out.error !== undefined || out.signal !== null || out.status !== 0)
|
|
353
|
+
return null;
|
|
354
|
+
const found = new Set();
|
|
355
|
+
let pid = 0;
|
|
356
|
+
for (const line of out.stdout.split("\n")) {
|
|
357
|
+
if (line.startsWith("p"))
|
|
358
|
+
pid = Number(line.slice(1));
|
|
359
|
+
else if (line.startsWith("n") && pid > 0 && inside(line.slice(1)))
|
|
360
|
+
found.add(pid);
|
|
361
|
+
}
|
|
362
|
+
return [...found];
|
|
363
|
+
}
|
|
364
|
+
const PIDFD_HELPER = `
|
|
365
|
+
import json, os, signal, sys
|
|
366
|
+
request = json.load(sys.stdin)
|
|
367
|
+
out = {}
|
|
368
|
+
for target in request["targets"]:
|
|
369
|
+
pid, start = target["pid"], target["start"]
|
|
370
|
+
try:
|
|
371
|
+
fd = os.pidfd_open(pid)
|
|
372
|
+
except ProcessLookupError:
|
|
373
|
+
out[str(pid)] = "gone"; continue
|
|
374
|
+
except OSError:
|
|
375
|
+
out[str(pid)] = "denied"; continue
|
|
376
|
+
try:
|
|
377
|
+
try:
|
|
378
|
+
with open("/proc/%d/stat" % pid) as handle: stat = handle.read()
|
|
379
|
+
except OSError:
|
|
380
|
+
out[str(pid)] = "gone"; continue
|
|
381
|
+
fields = stat[stat.rindex(")") + 2:].split()
|
|
382
|
+
if "t" + fields[19] != start:
|
|
383
|
+
out[str(pid)] = "changed"; continue
|
|
384
|
+
try:
|
|
385
|
+
signal.pidfd_send_signal(fd, request["signal"]); out[str(pid)] = "sent"
|
|
386
|
+
except ProcessLookupError:
|
|
387
|
+
out[str(pid)] = "gone"
|
|
388
|
+
except OSError:
|
|
389
|
+
out[str(pid)] = "denied"
|
|
390
|
+
finally:
|
|
391
|
+
os.close(fd)
|
|
392
|
+
print(json.dumps(out))
|
|
393
|
+
`;
|
|
394
|
+
export const TRUSTED_PYTHONS = Object.freeze(["/usr/bin/python3", "/opt/homebrew/bin/python3", "/usr/local/bin/python3"]);
|
|
395
|
+
let pinnedPython;
|
|
396
|
+
export function trustedPython(candidates = TRUSTED_PYTHONS) {
|
|
397
|
+
if (pinnedPython !== undefined && candidates === TRUSTED_PYTHONS)
|
|
398
|
+
return pinnedPython;
|
|
399
|
+
const uid = process.getuid?.();
|
|
400
|
+
const safe = (path) => { try {
|
|
401
|
+
const held = statSync(path);
|
|
402
|
+
return (held.uid === 0 || held.uid === uid) && (held.mode & 0o022) === 0;
|
|
403
|
+
}
|
|
404
|
+
catch {
|
|
405
|
+
return false;
|
|
406
|
+
} };
|
|
407
|
+
let found = null;
|
|
408
|
+
for (const candidate of candidates) {
|
|
409
|
+
try {
|
|
410
|
+
const real = realpathSync(candidate);
|
|
411
|
+
if (statSync(real).isFile() && (statSync(real).mode & 0o111) !== 0 && safe(real) && safe(dirname(real))) {
|
|
412
|
+
found = real;
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
catch { }
|
|
417
|
+
}
|
|
418
|
+
if (candidates === TRUSTED_PYTHONS)
|
|
419
|
+
pinnedPython = found;
|
|
420
|
+
return found;
|
|
421
|
+
}
|
|
422
|
+
function linuxSignal(targets, signal) {
|
|
423
|
+
const results = new Map(targets.map(one => [one.pid, "unavailable"]));
|
|
424
|
+
if (targets.length === 0)
|
|
425
|
+
return results;
|
|
426
|
+
const python = trustedPython();
|
|
427
|
+
if (python === null)
|
|
428
|
+
return results;
|
|
429
|
+
const ran = spawnSync(python, ["-I", "-c", PIDFD_HELPER], { input: JSON.stringify({ targets, signal: constants.signals[signal] }), encoding: "utf8", timeout: 10_000, killSignal: "SIGKILL",
|
|
430
|
+
env: { PATH: "/usr/bin:/bin", LC_ALL: "C" } });
|
|
431
|
+
if (ran.error !== undefined || ran.status !== 0)
|
|
432
|
+
return results;
|
|
433
|
+
try {
|
|
434
|
+
const answer = JSON.parse(ran.stdout);
|
|
435
|
+
for (const one of targets) {
|
|
436
|
+
const said = answer[String(one.pid)];
|
|
437
|
+
if (said === "sent" || said === "gone" || said === "changed" || said === "denied")
|
|
438
|
+
results.set(one.pid, said);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
catch { }
|
|
442
|
+
return results;
|
|
443
|
+
}
|
|
444
|
+
function macSignal(targets, signal) {
|
|
445
|
+
const results = new Map();
|
|
446
|
+
for (const one of targets) {
|
|
447
|
+
const ran = spawnSync("/bin/ps", ["-o", "lstart=", "-p", String(one.pid)], { encoding: "utf8", env: { PATH: "/usr/bin:/bin", LC_ALL: "C", TZ: "UTC" }, timeout: 2_000, killSignal: "SIGKILL" });
|
|
448
|
+
if (ran.error !== undefined || ran.signal !== null) {
|
|
449
|
+
results.set(one.pid, "unavailable");
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
const seconds = ran.stdout.trim() === "" ? null : lstartSeconds(ran.stdout);
|
|
453
|
+
if (ran.stdout.trim() === "") {
|
|
454
|
+
results.set(one.pid, "gone");
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
if (seconds === null || `s${seconds}` !== one.start) {
|
|
458
|
+
results.set(one.pid, "changed");
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
try {
|
|
462
|
+
process.kill(one.pid, signal);
|
|
463
|
+
results.set(one.pid, "sent");
|
|
464
|
+
}
|
|
465
|
+
catch (error) {
|
|
466
|
+
results.set(one.pid, error.code === "ESRCH" ? "gone" : "denied");
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return results;
|
|
470
|
+
}
|
|
471
|
+
export function realCustodyIO(bootId) {
|
|
472
|
+
const platform = process.platform;
|
|
473
|
+
return {
|
|
474
|
+
platform,
|
|
475
|
+
self: process.pid,
|
|
476
|
+
identityBound: platform === "linux",
|
|
477
|
+
table: () => platform === "linux" ? linuxTable() : platform === "darwin" ? macTable() : null,
|
|
478
|
+
namespaceOf: pid => platform === "linux" ? linuxNamespace(pid) : null,
|
|
479
|
+
holdersOf: folder => platform === "linux" ? linuxHolders(folder) : platform === "darwin" ? macHolders(folder) : null,
|
|
480
|
+
signal: (targets, signal) => {
|
|
481
|
+
const safe = targets.filter(one => Number.isSafeInteger(one.pid) && one.pid > 1 && one.pid !== process.pid);
|
|
482
|
+
const results = platform === "linux" ? linuxSignal(safe, signal) : platform === "darwin" ? macSignal(safe, signal) : new Map(safe.map(one => [one.pid, "unavailable"]));
|
|
483
|
+
for (const one of targets)
|
|
484
|
+
if (!safe.includes(one))
|
|
485
|
+
results.set(one.pid, "denied");
|
|
486
|
+
return results;
|
|
487
|
+
},
|
|
488
|
+
bootId,
|
|
489
|
+
sleep: ms => new Promise(done => setTimeout(done, ms)),
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
export function startIdentityOf(pid, io) {
|
|
493
|
+
return io.table()?.find(row => row.pid === pid && !row.zombie)?.start ?? null;
|
|
494
|
+
}
|