@justjoshn/pi-pstack-v2 0.1.7 → 0.2.1
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/README.md +19 -7
- package/docs/guide/01-setup.md +6 -6
- package/docs/guide/02-poteto-mode.md +1 -1
- package/docs/guide/07-overnight.md +3 -3
- package/docs/pi-mechanisms.md +35 -53
- package/package.json +5 -3
- package/skills/arena/SKILL.md +5 -5
- package/skills/automate-me/SKILL.md +2 -2
- package/skills/benchmark-checklist/SKILL.md +1 -1
- package/skills/control-cli/SKILL.md +1 -1
- package/skills/figure-it-out/SKILL.md +1 -1
- package/skills/how/SKILL.md +2 -2
- package/skills/interrogate/SKILL.md +2 -2
- package/skills/maintain-verification-skill/SKILL.md +1 -1
- package/skills/make-bot-ui/SKILL.md +2 -2
- package/skills/no-comments/SKILL.md +1 -1
- package/skills/poteto-help/SKILL.md +3 -3
- package/skills/poteto-mode/SKILL.md +9 -9
- package/skills/poteto-mode/playbooks/autopilot-full.md +5 -5
- package/skills/poteto-mode/playbooks/babysit.md +1 -1
- package/skills/poteto-mode/playbooks/bug-fix.md +1 -1
- package/skills/poteto-mode/playbooks/eval.md +3 -3
- package/skills/poteto-mode/playbooks/feature.md +2 -2
- package/skills/poteto-mode/playbooks/hillclimb.md +1 -1
- package/skills/poteto-mode/playbooks/opening-a-pr.md +1 -1
- package/skills/poteto-mode/playbooks/perf-issue.md +1 -1
- package/skills/poteto-mode/playbooks/refactoring.md +1 -1
- package/skills/poteto-mode/playbooks/shipping.md +2 -2
- package/skills/poteto-mode/playbooks/worktree-cleanup.md +1 -1
- package/skills/poteto-mode/worker.md +1 -1
- package/skills/recall/SKILL.md +3 -3
- package/skills/review-and-ship/SKILL.md +1 -1
- package/skills/setup-pstack/SKILL.md +2 -2
- package/skills/show-me-your-work/SKILL.md +2 -2
- package/skills/statechart/SKILL.md +1 -1
- package/skills/statechart/references/render.md +1 -1
- package/skills/swarm/SKILL.md +4 -4
- package/skills/workflow-from-chats/SKILL.md +1 -1
- package/src/gondolin/bash.ts +51 -0
- package/src/gondolin/fs-operations.ts +56 -0
- package/src/gondolin/grep.ts +96 -0
- package/src/gondolin/index.ts +58 -0
- package/src/gondolin/paths.ts +34 -0
- package/src/gondolin/search.ts +67 -0
- package/src/gondolin/tools.ts +40 -0
- package/src/gondolin/vm.ts +65 -0
- package/src/index.ts +31 -130
- package/src/jobs.ts +58 -315
- package/src/subagent/agents/poteto-agent.md +1 -1
- package/src/subagent/agents/worker.md +5 -0
- package/src/subagent/dispatch.ts +2 -2
- package/src/subagent/index.ts +33 -2
- package/src/subagent/render-call.ts +4 -2
- package/src/subagent/run-single.ts +9 -7
- package/src/subagent/schema.ts +28 -2
- package/src/subagent/types.ts +7 -0
- package/src/subagent/wake-jobs.ts +69 -0
- package/src/credentials.ts +0 -171
- package/src/git.ts +0 -94
- package/src/paths.ts +0 -16
- package/src/requests.ts +0 -182
- package/src/sandbox.ts +0 -284
- package/src/sweep.ts +0 -187
- package/src/transcript.ts +0 -79
package/src/requests.ts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import { spawnSync } from 'node:child_process';
|
|
2
|
-
import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
-
import { basename, join } from 'node:path';
|
|
4
|
-
|
|
5
|
-
import { getAgentDir } from '@earendil-works/pi-coding-agent';
|
|
6
|
-
import { z } from 'zod';
|
|
7
|
-
import { jobAgentDir } from './credentials.ts';
|
|
8
|
-
import { addWorktree, dropWorktree, gitPath, protectedPaths } from './git.ts';
|
|
9
|
-
import { brokeredSession, exitPath, type Job, type JobSpec, launchBrokered, readSpec, resultsDir, settleBrokered } from './jobs.ts';
|
|
10
|
-
import { landing, within } from './paths.ts';
|
|
11
|
-
import { type SandboxPolicy, sandboxed } from './sandbox.ts';
|
|
12
|
-
|
|
13
|
-
const MAX_REQUEST_DEPTH = 8;
|
|
14
|
-
const EXIT_REFUSED = 126;
|
|
15
|
-
const EXIT_LOST = 137;
|
|
16
|
-
const EXIT_LAUNCH_FAILED = 127;
|
|
17
|
-
const strings = z.array(z.string());
|
|
18
|
-
|
|
19
|
-
type CreatedWorktree = { path: string; repo: string; createdBranch: string | undefined };
|
|
20
|
-
|
|
21
|
-
const requestedWorktreePath = (out: string): string => landing(join(out, 'worktree'));
|
|
22
|
-
|
|
23
|
-
function createRequestedWorktree(top: Job, spec: JobSpec, out: string, grant: string[]): CreatedWorktree | { refused: string } {
|
|
24
|
-
const asked: unknown = spec.createWorktree;
|
|
25
|
-
if (spec.kind !== 'pi' || typeof asked !== 'object' || asked === null) return { refused: 'Only a Pi worker request can ask for a worktree.' };
|
|
26
|
-
const { branch, base } = asked as Record<string, unknown>;
|
|
27
|
-
if (typeof branch !== 'string' || (base !== undefined && typeof base !== 'string')) return { refused: 'Its worktree request is malformed.' };
|
|
28
|
-
const repo = top.spec.cwd;
|
|
29
|
-
const common = gitPath(repo, '--git-common-dir');
|
|
30
|
-
if (common === undefined || !within(common, grant)) return { refused: `The worker that asked may not write the repository at ${repo}, so it cannot get a worktree of it.` };
|
|
31
|
-
const path = requestedWorktreePath(out);
|
|
32
|
-
try {
|
|
33
|
-
return { path, repo, createdBranch: addWorktree(repo, path, branch, base) ? branch : undefined };
|
|
34
|
-
} catch (error) {
|
|
35
|
-
return { refused: `pstack could not create the worktree: ${error instanceof Error ? error.message : String(error)}` };
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** The checkouts a job may write, and the protected paths in them, which its wrapper sweeps after it for code that appeared. */
|
|
40
|
-
export const sweepFor = (policy: SandboxPolicy): JobSpec['sweep'] => ({
|
|
41
|
-
// A job's own session, nested, temp, and agent directories live under Pi's agent directory. A worktree does too, and is a checkout.
|
|
42
|
-
roots: policy.writable.filter((path) => (basename(path) === 'worktree' || !within(path, [policy.agentDir])) && !within(path, policy.gitDirs)),
|
|
43
|
-
protect: policy.protect.filter((path) => within(path, policy.writable)),
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
const readGrant = (out: string): string[] => {
|
|
47
|
-
try {
|
|
48
|
-
const grant = strings.safeParse(JSON.parse(readFileSync(join(out, 'granted.json'), 'utf8')));
|
|
49
|
-
return grant.success ? grant.data : [];
|
|
50
|
-
} catch {
|
|
51
|
-
return [];
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const isStrings = (value: unknown): value is string[] => strings.safeParse(value).success;
|
|
56
|
-
|
|
57
|
-
// A session that is already gone makes this fail, which is expected.
|
|
58
|
-
const tmuxKill = (socket: string, session: string): void => {
|
|
59
|
-
spawnSync('tmux', ['-S', socket, 'kill-session', '-t', `=${session}`], { stdio: 'ignore' });
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const hasSession = (socket: string, session: string): boolean => spawnSync('tmux', ['-S', socket, 'has-session', '-t', `=${session}`], { stdio: 'ignore' }).status === 0;
|
|
63
|
-
|
|
64
|
-
/** A worker writes these files, so anything malformed is skipped instead of stopping every other request. */
|
|
65
|
-
const wellFormed = (spec: JobSpec): boolean => {
|
|
66
|
-
const policy = spec.policy;
|
|
67
|
-
if (!policy || (spec.kind !== 'pi' && spec.kind !== 'command') || typeof spec.cwd !== 'string' || !isStrings(spec.command) || spec.command.length === 0) return false;
|
|
68
|
-
return [policy.writable, policy.protect, policy.lock, policy.gitDirs].every(isStrings);
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
/** What a request asked to write or run in that its requester may not, or undefined when it is allowed. */
|
|
72
|
-
function refusalFor(top: Job, spec: JobSpec, worktree: string | undefined, grant: string[]): string | undefined {
|
|
73
|
-
const checked = spec.policy.writable.map(landing).filter((path) => path !== worktree);
|
|
74
|
-
const cwdAllowed = worktree === undefined ? within(spec.cwd, [top.spec.cwd, ...grant]) : landing(spec.cwd) === worktree;
|
|
75
|
-
const refused = [...checked.filter((path) => !within(path, grant)), ...(cwdAllowed ? [] : [`cwd ${spec.cwd}`])];
|
|
76
|
-
return refused.length ? `pstack refused to launch this job. It asked to write outside what the job that requested it may write: ${refused.join(', ')}` : undefined;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Ends the tmux session of a launched request whose requester is done, and settles one that vanished. */
|
|
80
|
-
function superviseLaunched(socket: string, session: string, out: string, ending: boolean): void {
|
|
81
|
-
if (ending) tmuxKill(socket, session);
|
|
82
|
-
else if (!hasSession(socket, session)) settleBrokered(out, EXIT_LOST, 'pstack lost this job: its tmux session ended without an exit code.');
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
type Launch = { top: Job; socket: string; spec: JobSpec; dir: string; out: string; grant: string[] };
|
|
86
|
-
|
|
87
|
-
/** Builds the policy a request runs under from the paths just validated, so swapping a link afterwards changes nothing. */
|
|
88
|
-
function grantedPolicy({ top, dir }: Launch, writable: string[], guarded: { protect: string[]; lock: string[]; gitDirs: string[] }): SandboxPolicy {
|
|
89
|
-
const base: SandboxPolicy = {
|
|
90
|
-
writable,
|
|
91
|
-
protect: [...guarded.protect, ...top.spec.policy.protect],
|
|
92
|
-
lock: [...guarded.lock, ...top.spec.policy.lock],
|
|
93
|
-
gitDirs: [...guarded.gitDirs, ...top.spec.policy.gitDirs],
|
|
94
|
-
checkouts: [],
|
|
95
|
-
credentials: jobAgentDir(top.dir),
|
|
96
|
-
agentDir: getAgentDir(),
|
|
97
|
-
jobDir: dir,
|
|
98
|
-
};
|
|
99
|
-
return { ...base, checkouts: sweepFor(base).roots };
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function startRequest(request: Launch, made: CreatedWorktree | undefined, checked: string[], writesWorktree: boolean): boolean {
|
|
103
|
-
const { top, socket, spec, dir, out } = request;
|
|
104
|
-
const common = made === undefined ? undefined : gitPath(made.path, '--git-common-dir');
|
|
105
|
-
const guarded = made === undefined ? spec.policy : protectedPaths(made.path, common);
|
|
106
|
-
const writable = made !== undefined && writesWorktree ? [...checked, made.path, ...(common ? [landing(common)] : [])] : checked;
|
|
107
|
-
const granted = grantedPolicy(request, writable, guarded);
|
|
108
|
-
mkdirSync(out, { recursive: true });
|
|
109
|
-
writeFileSync(join(out, 'granted.json'), JSON.stringify(writable));
|
|
110
|
-
// The worker supplies tmp, so it is trusted only if it is one of the writable paths validated above.
|
|
111
|
-
const { tmp: asked, ...rest } = spec;
|
|
112
|
-
const tmp = typeof asked === 'string' && writable.includes(landing(asked)) ? landing(asked) : undefined;
|
|
113
|
-
return launchBrokered(top.dir, dir, { ...rest, ...(made ? { cwd: made.path } : {}), ...(tmp ? { tmp } : {}) }, sweepFor(granted), sandboxed(spec.command, granted), socket);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/** Validates a not yet launched request against its requester's grant, then launches it or settles it as refused. */
|
|
117
|
-
function launchRequest(request: Launch): void {
|
|
118
|
-
const { top, spec, out, grant } = request;
|
|
119
|
-
const worktree = spec.createWorktree === undefined ? undefined : requestedWorktreePath(out);
|
|
120
|
-
const asked = spec.policy.writable.map(landing);
|
|
121
|
-
const refusal = refusalFor(top, spec, worktree, grant);
|
|
122
|
-
if (refusal) {
|
|
123
|
-
settleBrokered(out, EXIT_REFUSED, refusal);
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
const made = worktree === undefined ? undefined : createRequestedWorktree(top, spec, out, grant);
|
|
127
|
-
if (made !== undefined && 'refused' in made) {
|
|
128
|
-
settleBrokered(out, EXIT_REFUSED, `pstack refused to launch this job. ${made.refused}`);
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
const checked = asked.filter((path) => path !== worktree);
|
|
132
|
-
let launched = false;
|
|
133
|
-
try {
|
|
134
|
-
launched = startRequest(request, made, checked, worktree !== undefined && asked.includes(worktree));
|
|
135
|
-
} catch (error) {
|
|
136
|
-
settleBrokered(out, EXIT_LAUNCH_FAILED, `pstack could not launch this job: ${error instanceof Error ? error.message : String(error)}`);
|
|
137
|
-
}
|
|
138
|
-
if (made !== undefined && !launched) {
|
|
139
|
-
rmSync(made.path, { recursive: true, force: true });
|
|
140
|
-
dropWorktree(made.repo, made.createdBranch);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function serveRequest(top: Job, socket: string, dir: string, requesterDone: boolean, depth: number, grant: string[]): void {
|
|
145
|
-
// A worker could link an entry back up its own tree, so only real directories are followed.
|
|
146
|
-
if (!lstatSync(dir).isDirectory()) return;
|
|
147
|
-
const spec = readSpec(dir);
|
|
148
|
-
if (!spec || !wellFormed(spec)) return;
|
|
149
|
-
const out = resultsDir(top.dir, dir);
|
|
150
|
-
const stopped = existsSync(join(dir, 'stop'));
|
|
151
|
-
const done = existsSync(exitPath(out)) || stopped;
|
|
152
|
-
// A request's own requests may write only what the root granted it, recorded where no worker can write.
|
|
153
|
-
serveRequests(top, socket, join(dir, 'nested'), requesterDone || done, depth + 1, readGrant(out));
|
|
154
|
-
if (!spec.brokered || existsSync(exitPath(out))) return;
|
|
155
|
-
if (existsSync(join(out, 'launched'))) {
|
|
156
|
-
superviseLaunched(socket, brokeredSession(dir), out, requesterDone || stopped);
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
if (requesterDone || stopped) return;
|
|
160
|
-
if (spec.kind === 'pi' && depth > 0) {
|
|
161
|
-
settleBrokered(out, EXIT_REFUSED, 'pstack refused to start this Pi worker. A worker started by another worker cannot start workers of its own.');
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
launchRequest({ top, socket, spec, dir, out, grant });
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Launches the requests that sandboxed workers under one of this session's jobs wrote. A request may write only inside
|
|
169
|
-
* what the root granted the top-level job, so no worker can hand a descendant more than it was given. The root only
|
|
170
|
-
* reads from the request directory, which a worker controls, and writes results under the top-level job.
|
|
171
|
-
*/
|
|
172
|
-
export function serveRequests(top: Job, socket: string, nested: string, requesterDone: boolean, depth = 0, grant: string[] = top.spec.policy.writable): void {
|
|
173
|
-
// A worker could link its nested directory back up its own tree, so only real directories are followed, and not far.
|
|
174
|
-
if (depth > MAX_REQUEST_DEPTH || !existsSync(nested) || !lstatSync(nested).isDirectory()) return;
|
|
175
|
-
for (const name of readdirSync(nested)) {
|
|
176
|
-
try {
|
|
177
|
-
serveRequest(top, socket, join(nested, name), requesterDone, depth, grant);
|
|
178
|
-
} catch {
|
|
179
|
-
// One request failing must not stop the others.
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
package/src/sandbox.ts
DELETED
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
import { spawnSync } from 'node:child_process';
|
|
2
|
-
import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, statSync, writeFileSync } from 'node:fs';
|
|
3
|
-
import { basename, dirname, join } from 'node:path';
|
|
4
|
-
|
|
5
|
-
import { hostEnv } from './host-env.ts';
|
|
6
|
-
import { passthroughObject } from './object-schema.ts';
|
|
7
|
-
import { EDITOR_CODE, HARNESS_DIRS, HARNESS_INSTRUCTIONS, WORKSPACE_SUFFIX } from './sweep.ts';
|
|
8
|
-
|
|
9
|
-
type Env = NodeJS.ProcessEnv;
|
|
10
|
-
type Arch = NodeJS.Architecture;
|
|
11
|
-
type Platform = NodeJS.Platform;
|
|
12
|
-
|
|
13
|
-
export const SANDBOX_ENV = 'PSTACK_V2_SANDBOXED';
|
|
14
|
-
/** The job directory of the sandboxed process. Jobs it asks the root to start are requested under its writable `nested/` directory. */
|
|
15
|
-
export const JOB_DIR_ENV = 'PSTACK_V2_JOB_DIR';
|
|
16
|
-
/** The directory of the top-level job a process descends from. Its `brokered/` directory holds what the root launched for descendants. */
|
|
17
|
-
export const TOP_DIR_ENV = 'PSTACK_V2_TOP_DIR';
|
|
18
|
-
|
|
19
|
-
export interface SandboxPolicy {
|
|
20
|
-
/** Paths the job may write. */
|
|
21
|
-
writable: string[];
|
|
22
|
-
/** Paths inside `writable` that stay read-only because an unsandboxed process would execute what they hold, such as git hooks. */
|
|
23
|
-
protect: string[];
|
|
24
|
-
/** Paths whose own entry may not be renamed, removed, or replaced, while writes inside them stay allowed, such as `.git`. */
|
|
25
|
-
lock: string[];
|
|
26
|
-
/** Git common directories the job may write. Every worktree and submodule redirection file under them stays read-only. */
|
|
27
|
-
gitDirs: string[];
|
|
28
|
-
/** Checkouts the job may write. No `.pi` code entry or `.git` entry anywhere in them may be written, even one that does not exist yet. */
|
|
29
|
-
checkouts: string[];
|
|
30
|
-
agentDir: string;
|
|
31
|
-
/**
|
|
32
|
-
* The job's own Pi agent directory, shared with every job its top-level job starts. Only the contents of its credentials
|
|
33
|
-
* copy and Pi's lock directories are writable, so no job can change what another job's Pi loads.
|
|
34
|
-
*/
|
|
35
|
-
credentials: string;
|
|
36
|
-
/** The job's own directory. Jobs it asks the root to start are requested under its `nested/` directory. */
|
|
37
|
-
jobDir: string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** A policy read back from a job's spec file, as written: its keys are not checked here, and requests.ts checks the lists it relies on. */
|
|
41
|
-
export const SandboxPolicySchema = passthroughObject<SandboxPolicy>();
|
|
42
|
-
|
|
43
|
-
const real = (path: string): string => (existsSync(path) ? realpathSync(path) : path);
|
|
44
|
-
/** A path as the kernel sees it when the entry itself is the target: the real parent plus the entry's own name, links unresolved. */
|
|
45
|
-
const entry = (path: string): string => join(real(dirname(path)), basename(path));
|
|
46
|
-
const escapeRegex = (text: string): string => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
47
|
-
const quote = (text: string): string => JSON.stringify(text);
|
|
48
|
-
|
|
49
|
-
// tmux servers outside the sandbox run commands unsandboxed, so a job may not connect to their sockets or replace them.
|
|
50
|
-
const TMUX_SOCKETS = '^/private/(tmp|var/folders/[^/]+/[^/]+/T)/(tmux|pstack-v2)-';
|
|
51
|
-
// Files through which git finds a worktree's or submodule's hooks and config.
|
|
52
|
-
const gitRedirects = (common: string): string => `^${escapeRegex(real(common))}/(config\\.worktree|worktrees/[^/]+/(commondir|gitdir|config\\.worktree)|modules/.+/(hooks|config|info|commondir|gitdir|config\\.worktree))(/|$)`;
|
|
53
|
-
|
|
54
|
-
/** The git directories of the linked worktrees the job works in, which it may write apart from their redirection files. */
|
|
55
|
-
const ownWorktrees = (policy: SandboxPolicy): string[] => policy.lock.filter((path) => policy.gitDirs.some((common) => dirname(path) === join(common, 'worktrees')));
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* bubblewrap can only make read-only what exists, so the git paths a job may not create are created first, empty. Git
|
|
59
|
-
* reads an empty `config.worktree` as no settings, and empty `hooks`, `info`, `worktrees`, and `modules` directories as none.
|
|
60
|
-
*/
|
|
61
|
-
function placeholders(policy: SandboxPolicy): void {
|
|
62
|
-
for (const common of policy.gitDirs) {
|
|
63
|
-
if (!existsSync(common)) continue;
|
|
64
|
-
for (const name of ['hooks', 'info', 'worktrees', 'modules']) mkdirSync(join(common, name), { recursive: true });
|
|
65
|
-
for (const dir of [common, ...ownWorktrees(policy)]) if (existsSync(dir) && !existsSync(join(dir, 'config.worktree'))) writeFileSync(join(dir, 'config.worktree'), '');
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/** Rules refusing a checkout's git entries, Pi code, and editor and harness configuration. */
|
|
70
|
-
function checkoutRules(policy: SandboxPolicy): string[] {
|
|
71
|
-
return [
|
|
72
|
-
// Any .git a checkout gains is a repository git would run hooks from, apart from the checkout's own git directory.
|
|
73
|
-
...policy.checkouts.map((root) => {
|
|
74
|
-
const git = existsSync(join(root, '.git')) && statSync(join(root, '.git')).isDirectory() ? '.+/\\.git' : '(.*/)?\\.git';
|
|
75
|
-
return `(deny file-write* (regex #"^${escapeRegex(real(root))}/(${git}|(.*/)?\\.pi/(extensions|settings\\.json|mcp\\.json|npm|git))(/|$)"))`;
|
|
76
|
-
}),
|
|
77
|
-
// Editor and agent-harness configuration runs unsandboxed when the checkout is opened, so it is refused at any depth,
|
|
78
|
-
// apart from the harnesses' instruction directories.
|
|
79
|
-
...policy.checkouts.map((root) => {
|
|
80
|
-
const under = `^${escapeRegex(real(root))}/(.*/)?`;
|
|
81
|
-
const alt = (names: readonly string[]): string => names.map(escapeRegex).join('|');
|
|
82
|
-
const harness = `${under}(${alt(HARNESS_DIRS)})`;
|
|
83
|
-
return `(deny file-write* (regex #"${under}(${alt(EDITOR_CODE)}|[^/]*${escapeRegex(WORKSPACE_SUFFIX)})(/|$)") (require-all (regex #"${harness}(/|$)") (require-not (regex #"${harness}/(${alt(HARNESS_INSTRUCTIONS)})(/|$)"))))`;
|
|
84
|
-
}),
|
|
85
|
-
];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** Rules that hold for every job: local sockets, launch services, signals, and preference writes. */
|
|
89
|
-
const SEATBELT_TAIL: string[] = [
|
|
90
|
-
// Any local service socket could start a process outside the sandbox, such as Docker's, so only DNS is reachable.
|
|
91
|
-
'(deny network-outbound (remote unix-socket))',
|
|
92
|
-
// biome-ignore lint/security/noSecrets: macOS DNS uses this fixed mDNSResponder Unix socket path.
|
|
93
|
-
'(allow network-outbound (remote unix-socket (path-literal "/private/var/run/mDNSResponder")))',
|
|
94
|
-
// Nor may it serve one, such as a tmux server of its own. Linux refuses the same through seccomp.
|
|
95
|
-
'(deny network-bind (local unix-socket))',
|
|
96
|
-
// Services that start processes on a caller's behalf would run them outside the sandbox: LaunchServices (`open`) and Apple Events.
|
|
97
|
-
// `open -a` on an app the job wrote launches it through launchd, so the lookup rules below cannot stop it, and lsopen does.
|
|
98
|
-
'(deny appleevent-send)',
|
|
99
|
-
'(deny lsopen)',
|
|
100
|
-
// A job may signal only processes in its own sandbox, never the wrapper that cleans up after it or the root Pi.
|
|
101
|
-
'(deny signal)',
|
|
102
|
-
'(allow signal (target same-sandbox))',
|
|
103
|
-
// launchservicesd stays reachable because Firefox checks in with it at startup and aborts without it. Opening goes through lsopen.
|
|
104
|
-
'(deny mach-lookup (global-name-regex #"^com\\.apple\\.lsd\\.") (global-name "com.apple.coreservices.appleevents"))',
|
|
105
|
-
// launchd would run a job it is handed outside the sandbox, and preference writes go through a daemon outside it.
|
|
106
|
-
'(deny mach-lookup (global-name-regex #"^com\\.apple\\.xpc\\.launchd\\."))',
|
|
107
|
-
'(deny user-preference-write)',
|
|
108
|
-
];
|
|
109
|
-
|
|
110
|
-
export function seatbeltProfile(policy: SandboxPolicy): string {
|
|
111
|
-
const subpaths = (paths: string[]): string => paths.map((path) => `(subpath ${quote(real(path))})`).join(' ');
|
|
112
|
-
// A protected path that is itself a symlink is guarded at the link as well as at its target, so it cannot be swapped.
|
|
113
|
-
const guarded = [...policy.protect.map((path) => `(subpath ${quote(real(path))}) (literal ${quote(entry(path))})`), ...policy.lock.map((path) => `(literal ${quote(real(path))}) (literal ${quote(entry(path))})`)];
|
|
114
|
-
const inAgentDir = policy.writable.filter((path) => real(path) === real(policy.agentDir) || real(path).startsWith(`${real(policy.agentDir)}/`));
|
|
115
|
-
const lines = [
|
|
116
|
-
'(version 1)',
|
|
117
|
-
'(allow default)',
|
|
118
|
-
// Later rules win, so the order is the job's own paths, then Pi's agent directory closed even when a job path holds
|
|
119
|
-
// it, then the job's own paths inside it, then the paths an unsandboxed process would execute from. Shared temp
|
|
120
|
-
// directories are not writable: unsandboxed programs load code cached there, so each job gets its own.
|
|
121
|
-
'(deny file-write*)',
|
|
122
|
-
`(allow file-write* (literal "/dev/null") (literal "/dev/ptmx") (regex #"^/dev/tty") (regex #"^/dev/fd/") ${subpaths(policy.writable)})`,
|
|
123
|
-
`(deny file-write* (subpath ${quote(real(policy.agentDir))}))`,
|
|
124
|
-
// Inside Pi's agent directory only the job's own paths are writable, its session, nested jobs, temp, and its own agent
|
|
125
|
-
// directory with its credentials copy. Your real auth.json and settings are never writable.
|
|
126
|
-
...(inAgentDir.length ? [`(allow file-write* ${subpaths(inAgentDir)})`] : []),
|
|
127
|
-
...(policy.credentials ? [`(allow file-write-data (literal ${quote(join(real(policy.credentials), 'auth.json'))}))`, `(allow file-write* (regex #"^${escapeRegex(real(policy.credentials))}/[^/]+\\.lock(/|$)"))`] : []),
|
|
128
|
-
// A job may not add or change a worktree or submodule, apart from the metadata of the worktree it works in.
|
|
129
|
-
...policy.gitDirs.map((common) => `(deny file-write* (subpath ${quote(real(join(common, 'worktrees')))}) (subpath ${quote(real(join(common, 'modules')))}))`),
|
|
130
|
-
...(ownWorktrees(policy).length ? [`(allow file-write* ${subpaths(ownWorktrees(policy))})`] : []),
|
|
131
|
-
`(deny file-write* (regex #"${TMUX_SOCKETS}") ${guarded.join(' ')} ${policy.gitDirs.map((common) => `(regex #"${gitRedirects(common)}")`).join(' ')})`,
|
|
132
|
-
...checkoutRules(policy),
|
|
133
|
-
...SEATBELT_TAIL,
|
|
134
|
-
];
|
|
135
|
-
return lines.join('\n');
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const REDIRECT_FILES = ['commondir', 'gitdir', 'config.worktree'];
|
|
139
|
-
const MODULE_REDIRECTS = ['hooks', 'config', 'info', ...REDIRECT_FILES];
|
|
140
|
-
const MAX_MODULE_DEPTH = 6;
|
|
141
|
-
|
|
142
|
-
/** Existing worktree and submodule redirection files under a git common directory, for bubblewrap, which can only bind what exists. */
|
|
143
|
-
function existingRedirects(common: string): string[] {
|
|
144
|
-
const worktrees = join(common, 'worktrees');
|
|
145
|
-
const linked = existsSync(worktrees) ? readdirSync(worktrees).flatMap((name) => REDIRECT_FILES.map((file) => join(worktrees, name, file))) : [];
|
|
146
|
-
const walk = (dir: string, depth: number): string[] => {
|
|
147
|
-
if (depth > MAX_MODULE_DEPTH || !existsSync(dir)) return [];
|
|
148
|
-
return readdirSync(dir).flatMap((name) => {
|
|
149
|
-
const path = join(dir, name);
|
|
150
|
-
if (MODULE_REDIRECTS.includes(name)) return [path];
|
|
151
|
-
return statSync(path).isDirectory() ? walk(path, depth + 1) : [];
|
|
152
|
-
});
|
|
153
|
-
};
|
|
154
|
-
return [join(common, 'config.worktree'), ...linked, ...walk(join(common, 'modules'), 0)].filter(existsSync);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const ROOT_DIR = '/';
|
|
158
|
-
const DEV_DIR = '/dev';
|
|
159
|
-
const PROC_DIR = '/proc';
|
|
160
|
-
const PRIVATE_TMP_DIR = '/tmp';
|
|
161
|
-
const RUN_DIR = '/run';
|
|
162
|
-
const VAR_RUN_DIR = '/var/run';
|
|
163
|
-
const SYSTEMD_RESOLVE_DIR = '/run/systemd/resolve';
|
|
164
|
-
const RESOLVCONF_DIR = '/run/resolvconf';
|
|
165
|
-
const RESOLVER_DIRS = [SYSTEMD_RESOLVE_DIR, RESOLVCONF_DIR];
|
|
166
|
-
const SHELL = '/bin/sh';
|
|
167
|
-
|
|
168
|
-
const bind = (flag: string, path: string): string[] => [flag, path, path];
|
|
169
|
-
|
|
170
|
-
/** The job's agent directory becomes a throwaway tmpfs holding its entries read-only, and only its credentials copy is bound writable. */
|
|
171
|
-
const credentialMounts = (credentials: string): string[] =>
|
|
172
|
-
credentials && existsSync(credentials) ? ['--tmpfs', credentials, ...readdirSync(credentials).flatMap((name) => bind(name === 'auth.json' ? '--bind' : '--ro-bind', join(credentials, name)))] : [];
|
|
173
|
-
|
|
174
|
-
export function bwrapArgs(policy: SandboxPolicy, env: Env = hostEnv(), uid: number = process.getuid?.() ?? 0): string[] {
|
|
175
|
-
// Service sockets live under /run, such as Docker's and the system bus, so it is hidden, except the DNS resolver's files.
|
|
176
|
-
const hidden = [...new Set([RUN_DIR, VAR_RUN_DIR, `/run/user/${uid}`, env['XDG_RUNTIME_DIR']])].filter((path): path is string => Boolean(path) && existsSync(path as string) && !lstatSync(path as string).isSymbolicLink());
|
|
177
|
-
const worktreeGitDirs = policy.gitDirs.flatMap((common) => ['worktrees', 'modules'].map((name) => join(common, name)).filter(existsSync));
|
|
178
|
-
return [
|
|
179
|
-
// Its own PID namespace means every process a job starts dies with it, and none can signal the wrapper outside.
|
|
180
|
-
// A private /tmp, since unsandboxed programs load code they cached in the shared one. It also hides outside tmux sockets.
|
|
181
|
-
...['--ro-bind', ROOT_DIR, ROOT_DIR, '--dev', DEV_DIR, '--proc', PROC_DIR, '--unshare-pid', '--tmpfs', PRIVATE_TMP_DIR],
|
|
182
|
-
// Pi's agent directory stays visible, read-only, even when it sits under the private /tmp.
|
|
183
|
-
...(existsSync(policy.agentDir) ? bind('--ro-bind', policy.agentDir) : []),
|
|
184
|
-
...policy.writable.filter(existsSync).flatMap((path) => bind('--bind', path)),
|
|
185
|
-
// Later mounts win, so everything below lands on top of the writable paths, even one that contains these.
|
|
186
|
-
...hidden.flatMap((path) => ['--tmpfs', path]),
|
|
187
|
-
...RESOLVER_DIRS.filter(existsSync).flatMap((path) => bind('--ro-bind', path)),
|
|
188
|
-
...credentialMounts(policy.credentials),
|
|
189
|
-
// A mount point cannot be renamed, so a locked directory is bound onto itself, still writable inside, and a locked file read-only.
|
|
190
|
-
...policy.lock.filter(existsSync).flatMap((path) => bind(statSync(path).isDirectory() ? '--bind' : '--ro-bind', path)),
|
|
191
|
-
...worktreeGitDirs.flatMap((path) => bind('--ro-bind', path)),
|
|
192
|
-
...ownWorktrees(policy).flatMap((path) => bind('--bind', path)),
|
|
193
|
-
...[...policy.protect, ...policy.gitDirs.flatMap(existingRedirects)].filter(existsSync).flatMap((path) => bind('--ro-bind', path)),
|
|
194
|
-
// Shared /tmp is read-only, as on macOS, once everything under it is mounted.
|
|
195
|
-
...['--remount-ro', PRIVATE_TMP_DIR, '--die-with-parent', '--'],
|
|
196
|
-
];
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/** Linux syscall numbers and audit architecture for the seccomp filter, per Node architecture. */
|
|
200
|
-
const SYSCALLS: Partial<Record<Arch, { audit: number; socket: number; ioUringSetup: number }>> = {
|
|
201
|
-
x64: { audit: 0xc000003e, socket: 41, ioUringSetup: 425 },
|
|
202
|
-
arm64: { audit: 0xc00000b7, socket: 198, ioUringSetup: 425 },
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
const ALLOW = 0x7fff0000;
|
|
206
|
-
const DENY = 0x00050001;
|
|
207
|
-
const SECCOMP_ARCH_OFFSET = 4;
|
|
208
|
-
const SECCOMP_FIRST_ARG_OFFSET = 16;
|
|
209
|
-
const SKIP_TO_DENY_FROM_ARCH = 7;
|
|
210
|
-
const SKIP_TO_DENY_FROM_X32 = 5;
|
|
211
|
-
const SKIP_TO_DENY_FROM_IO_URING = 4;
|
|
212
|
-
const SKIP_SOCKET_CHECK = 2;
|
|
213
|
-
const X32_SYSCALL_BIT = 0x40000000;
|
|
214
|
-
const AF_UNIX = 1;
|
|
215
|
-
const BPF_INSTRUCTION_BYTES = 8;
|
|
216
|
-
const BPF_JT_OFFSET = 2;
|
|
217
|
-
const BPF_JF_OFFSET = 3;
|
|
218
|
-
const BPF_K_OFFSET = 4;
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* A classic BPF program for bubblewrap's `--seccomp`: a job may not open a Unix socket, the way macOS denies binding or
|
|
222
|
-
* connecting one, so it reaches no local service, abstract socket, or tmux server. Socket pairs, which child processes
|
|
223
|
-
* use for pipes, stay allowed. io_uring could open a socket around the filter, and another architecture's syscall
|
|
224
|
-
* numbers around its checks, so both are refused.
|
|
225
|
-
*/
|
|
226
|
-
export function seccompFilter(arch: Arch = process.arch): Buffer {
|
|
227
|
-
const nr = SYSCALLS[arch];
|
|
228
|
-
if (!nr) throw new Error(`pstack's Linux sandbox supports x64 and arm64, not ${arch}.`);
|
|
229
|
-
const LD = 0x20;
|
|
230
|
-
const JEQ = 0x15;
|
|
231
|
-
const JGE = 0x35;
|
|
232
|
-
const RET = 0x06;
|
|
233
|
-
const program: [number, number, number, number][] = [
|
|
234
|
-
[LD, 0, 0, SECCOMP_ARCH_OFFSET],
|
|
235
|
-
[JEQ, 0, SKIP_TO_DENY_FROM_ARCH, nr.audit],
|
|
236
|
-
[LD, 0, 0, 0],
|
|
237
|
-
[JGE, SKIP_TO_DENY_FROM_X32, 0, X32_SYSCALL_BIT],
|
|
238
|
-
[JEQ, SKIP_TO_DENY_FROM_IO_URING, 0, nr.ioUringSetup],
|
|
239
|
-
[JEQ, 0, SKIP_SOCKET_CHECK, nr.socket],
|
|
240
|
-
[LD, 0, 0, SECCOMP_FIRST_ARG_OFFSET],
|
|
241
|
-
[JEQ, 1, 0, AF_UNIX],
|
|
242
|
-
[RET, 0, 0, ALLOW],
|
|
243
|
-
[RET, 0, 0, DENY],
|
|
244
|
-
];
|
|
245
|
-
const bytes = Buffer.alloc(program.length * BPF_INSTRUCTION_BYTES);
|
|
246
|
-
program.forEach(([code, jt, jf, k], index) => {
|
|
247
|
-
const at = index * BPF_INSTRUCTION_BYTES;
|
|
248
|
-
bytes.writeUInt16LE(code, at);
|
|
249
|
-
bytes.writeUInt8(jt, at + BPF_JT_OFFSET);
|
|
250
|
-
bytes.writeUInt8(jf, at + BPF_JF_OFFSET);
|
|
251
|
-
bytes.writeUInt32LE(k, at + BPF_K_OFFSET);
|
|
252
|
-
});
|
|
253
|
-
return bytes;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/** The filter's file in Pi's agent directory, which no job may write, so none can loosen the filter before a launch. */
|
|
257
|
-
function seccompFile(agentDir: string): string {
|
|
258
|
-
const path = join(agentDir, 'pstack-v2', `seccomp-${process.arch}.bpf`);
|
|
259
|
-
const filter = seccompFilter();
|
|
260
|
-
if (!existsSync(path) || !readFileSync(path).equals(filter)) {
|
|
261
|
-
mkdirSync(dirname(path), { recursive: true });
|
|
262
|
-
writeFileSync(path, filter);
|
|
263
|
-
}
|
|
264
|
-
return path;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
const available = (command: string): boolean => spawnSync(SHELL, ['-c', command], { stdio: 'ignore' }).status === 0;
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* Wraps argv so it runs with writes limited to the policy. bubblewrap nests, so a Linux job inside a sandbox gets its own
|
|
271
|
-
* narrower one. macOS refuses a second Seatbelt profile, so a sandboxed macOS worker asks the root Pi to launch instead.
|
|
272
|
-
*/
|
|
273
|
-
export function sandboxed(argv: string[], policy: SandboxPolicy, platform: Platform = process.platform, env: Env = hostEnv()): string[] {
|
|
274
|
-
if (platform === 'darwin' && available('command -v sandbox-exec')) {
|
|
275
|
-
placeholders(policy);
|
|
276
|
-
return ['sandbox-exec', '-p', seatbeltProfile(policy), ...argv];
|
|
277
|
-
}
|
|
278
|
-
if (platform === 'linux' && available('command -v bwrap')) {
|
|
279
|
-
placeholders(policy);
|
|
280
|
-
// bubblewrap reads the filter from a file descriptor, which the shell opens just before it becomes bwrap.
|
|
281
|
-
return [SHELL, '-c', 'exec 9<"$1"; shift; exec bwrap --seccomp 9 "$@"', 'sh', seccompFile(policy.agentDir), ...bwrapArgs(policy, env), ...argv];
|
|
282
|
-
}
|
|
283
|
-
throw new Error(`pstack runs every job in a sandbox and found none on ${platform}. Install bubblewrap (bwrap) on Linux or WSL2, or use macOS, where Seatbelt (sandbox-exec) is built in.`);
|
|
284
|
-
}
|
package/src/sweep.ts
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
import { appendFileSync, chmodSync, cpSync, lstatSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync } from 'node:fs';
|
|
2
|
-
import { basename, dirname, join } from 'node:path';
|
|
3
|
-
|
|
4
|
-
import { z } from 'zod';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* What a checkout held when a job started, so that the unsandboxed wrapper can quarantine, after the job, every path an
|
|
8
|
-
* unsandboxed process would later execute from that the job made appear. A job can make one appear even where it may
|
|
9
|
-
* not write it directly, by renaming a directory it built into place or by linking a parent elsewhere.
|
|
10
|
-
*/
|
|
11
|
-
export interface Inventory {
|
|
12
|
-
/** Checkouts the job may write, walked without following links. */
|
|
13
|
-
roots: string[];
|
|
14
|
-
/** Protected paths absent at launch, each with whether its parent was already a link. */
|
|
15
|
-
absent: { path: string; parentWasLink: boolean }[];
|
|
16
|
-
/** Code entries present at launch, which the sweep leaves alone. */
|
|
17
|
-
present: string[];
|
|
18
|
-
/** Where swept paths go, and where the job's output notes them. */
|
|
19
|
-
quarantine: string;
|
|
20
|
-
output: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// The inventory is read as written, since this file runs alone in the job's output directory and a missing or odd key is
|
|
24
|
-
// reported by the sweep's own catch into the job's output, as it always was.
|
|
25
|
-
const InventorySchema = z.custom<Inventory>((value) => typeof value === 'object' && value !== null);
|
|
26
|
-
|
|
27
|
-
/** Pi loads code from these entries of any `.pi` directory, at any depth, once the project is trusted. */
|
|
28
|
-
export const PI_CODE = ['extensions', 'settings.json', 'mcp.json', 'npm', 'git'] as const;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Editors and other agent harnesses run what these hold, unsandboxed, once you open the checkout in them: VS Code and
|
|
32
|
-
* JetBrains tasks, and agent-harness hooks and MCP servers. pstack's sandbox refuses writing them at any depth. Every
|
|
33
|
-
* entry of a harness directory is code apart from its instructions, which a job may still write.
|
|
34
|
-
*/
|
|
35
|
-
export const EDITOR_CODE = ['.vscode', '.idea', '.cursorignore'] as const;
|
|
36
|
-
export const WORKSPACE_SUFFIX = '.code-workspace';
|
|
37
|
-
export const HARNESS_DIRS = ['.cursor', '.claude'] as const;
|
|
38
|
-
export const HARNESS_INSTRUCTIONS = ['rules', 'commands', 'worktrees', 'skills', 'agents'] as const;
|
|
39
|
-
|
|
40
|
-
const includes = (list: readonly string[], name: string): boolean => list.includes(name);
|
|
41
|
-
/** Which entries of each such directory are code. */
|
|
42
|
-
const CODE_DIRS = new Map<string, (entry: string) => boolean>([['.pi', (entry) => includes(PI_CODE, entry)], ...HARNESS_DIRS.map((dir) => [dir, (entry: string) => !includes(HARNESS_INSTRUCTIONS, entry)] as const)]);
|
|
43
|
-
/** Entries that are code whole: any .git, and editor configuration. */
|
|
44
|
-
const wholeCode = (name: string): boolean => name === '.git' || includes(EDITOR_CODE, name) || name.endsWith(WORKSPACE_SUFFIX);
|
|
45
|
-
|
|
46
|
-
const exists = (path: string): boolean => {
|
|
47
|
-
try {
|
|
48
|
-
lstatSync(path);
|
|
49
|
-
return true;
|
|
50
|
-
} catch {
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Code entries (Pi, git, editor, and harness) under the roots, found without following links or descending into heavy or git
|
|
57
|
-
* directories. With `repair`, a directory the job made unreadable gets its owner permissions back so it can be searched,
|
|
58
|
-
* and anything still unreadable lands in `unreadable`, since a job could otherwise hide code from the sweep that way.
|
|
59
|
-
*/
|
|
60
|
-
export function codePaths(roots: string[], repair = false): string[] {
|
|
61
|
-
return scanCode(roots, repair).found;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
type Scan = { found: string[]; unreadable: string[] };
|
|
65
|
-
const EMPTY_SCAN: Scan = { found: [], unreadable: [] };
|
|
66
|
-
const joinScans = (scans: Scan[]): Scan => ({ found: scans.flatMap((scan) => scan.found), unreadable: scans.flatMap((scan) => scan.unreadable) });
|
|
67
|
-
|
|
68
|
-
/** Reads with `read`, and with `repair` retries once after restoring owner permissions. Fails with the path as unreadable. */
|
|
69
|
-
function attempt<T>(path: string, repair: boolean, read: () => T): { value?: T; unreadable: string[] } {
|
|
70
|
-
try {
|
|
71
|
-
return { value: read(), unreadable: [] };
|
|
72
|
-
} catch {
|
|
73
|
-
if (!repair) return { unreadable: [] };
|
|
74
|
-
}
|
|
75
|
-
try {
|
|
76
|
-
chmodSync(path, (lstatSync(path).mode & 0o7777) | 0o700);
|
|
77
|
-
return { value: read(), unreadable: [] };
|
|
78
|
-
} catch {
|
|
79
|
-
return { unreadable: [path] };
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Like `codePaths`, and also names every directory that stayed unreadable even after repair. */
|
|
84
|
-
function scanCode(roots: string[], repair: boolean): Scan {
|
|
85
|
-
// Links are never followed, so the walk cannot loop and needs no depth limit.
|
|
86
|
-
const walk = (dir: string): Scan => {
|
|
87
|
-
// Inside a code directory such as `.pi` or `.claude`, its code entries are found and the rest is searched like any directory.
|
|
88
|
-
const isCode = CODE_DIRS.get(basename(dir));
|
|
89
|
-
const listing = attempt(dir, repair, () => readdirSync(dir));
|
|
90
|
-
const entries = (listing.value ?? []).map((name) => {
|
|
91
|
-
const path = join(dir, name);
|
|
92
|
-
const stat = attempt(dir, repair, () => lstatSync(path));
|
|
93
|
-
return { name, path, stat: stat.value, unreadable: stat.unreadable };
|
|
94
|
-
});
|
|
95
|
-
const scans = entries.map(({ name, path, stat, unreadable }): Scan => {
|
|
96
|
-
if (!stat) return { found: [], unreadable };
|
|
97
|
-
if (wholeCode(name) || isCode?.(name) || (CODE_DIRS.has(name) && stat.isSymbolicLink())) return { found: [path], unreadable };
|
|
98
|
-
return stat.isDirectory() && name !== 'node_modules' ? joinScans([{ found: [], unreadable }, walk(path)]) : { found: [], unreadable };
|
|
99
|
-
});
|
|
100
|
-
return joinScans([{ found: [], unreadable: listing.unreadable }, ...scans]);
|
|
101
|
-
};
|
|
102
|
-
return joinScans([EMPTY_SCAN, ...roots.map(walk)]);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export function inventory(roots: string[], protect: string[], quarantine: string, output: string): Inventory {
|
|
106
|
-
const absent = protect.filter((path) => !exists(path)).map((path) => ({ path, parentWasLink: exists(dirname(path)) && lstatSync(dirname(path)).isSymbolicLink() }));
|
|
107
|
-
return { roots, absent, present: codePaths(roots), quarantine, output };
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// Moving a path out needs write permission on its parent, and a directory also on itself, which a job can take away.
|
|
111
|
-
// chmod follows links, so only real directories are touched.
|
|
112
|
-
function makeWritable(path: string): void {
|
|
113
|
-
const stat = lstatSync(path);
|
|
114
|
-
if (stat.isDirectory()) chmodSync(path, (stat.mode & 0o7777) | 0o700);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/** Renames into quarantine, which can sit on another filesystem. Copying keeps links as links and removing never follows one. */
|
|
118
|
-
function relocate(path: string, target: string): void {
|
|
119
|
-
try {
|
|
120
|
-
renameSync(path, target);
|
|
121
|
-
} catch (error) {
|
|
122
|
-
if ((error as NodeJS.ErrnoException).code !== 'EXDEV') throw error;
|
|
123
|
-
cpSync(path, target, { recursive: true, verbatimSymlinks: true });
|
|
124
|
-
rmSync(path, { recursive: true, force: true });
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** Quarantines one path and notes it in the job's output. Returns whether it moved. */
|
|
129
|
-
function quarantine(inv: Inventory, path: string, reason: string, serial: number): boolean {
|
|
130
|
-
if (!exists(path)) return false;
|
|
131
|
-
try {
|
|
132
|
-
mkdirSync(inv.quarantine, { recursive: true });
|
|
133
|
-
const target = join(inv.quarantine, `${Date.now()}-${serial}-${basename(path)}`);
|
|
134
|
-
makeWritable(dirname(path));
|
|
135
|
-
makeWritable(path);
|
|
136
|
-
relocate(path, target);
|
|
137
|
-
appendFileSync(inv.output, `pstack quarantined ${path}, ${reason}, in ${inv.quarantine}.\n`);
|
|
138
|
-
return true;
|
|
139
|
-
} catch (error) {
|
|
140
|
-
appendFileSync(inv.output, `pstack could not quarantine ${path}, ${reason}: ${String(error)}. Check it before you start Pi in this checkout.\n`);
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
type Move = { path: string; reason: string };
|
|
146
|
-
|
|
147
|
-
/** What to move for a protected path that was absent at launch: a link that appeared over its parent, else the path itself. */
|
|
148
|
-
function absentMoveOf({ path, parentWasLink }: Inventory['absent'][number]): Move {
|
|
149
|
-
const parent = dirname(path);
|
|
150
|
-
if (!parentWasLink && exists(parent) && lstatSync(parent).isSymbolicLink()) return { path: parent, reason: 'a link that appeared over a protected path while the job ran' };
|
|
151
|
-
return { path, reason: 'which appeared while the job ran' };
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/** Moves each path in order, then returns the earlier moves with the ones that happened. */
|
|
155
|
-
const moveAll = (inv: Inventory, moves: Move[], moved: string[]): string[] => {
|
|
156
|
-
const [head, ...rest] = moves;
|
|
157
|
-
if (head === undefined) return moved;
|
|
158
|
-
return moveAll(inv, rest, quarantine(inv, head.path, head.reason, moved.length) ? moved.concat(head.path) : moved);
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
/** Moves each path that appeared during the job into quarantine, never following a link, and returns what it moved. */
|
|
162
|
-
export function sweep(inv: Inventory): string[] {
|
|
163
|
-
// Each path is decided after the earlier moves, since a move can change what its parent is.
|
|
164
|
-
const afterAbsent = inv.absent.reduce((moved, entry) => moveAll(inv, [absentMoveOf(entry)], moved), [] as string[]);
|
|
165
|
-
const present = new Set(inv.present);
|
|
166
|
-
const scan = scanCode(inv.roots, true);
|
|
167
|
-
const moved = moveAll(
|
|
168
|
-
inv,
|
|
169
|
-
scan.found.filter((path) => !present.has(path)).map((path) => ({ path, reason: 'Pi, git, editor, or harness code that appeared while the job ran' })),
|
|
170
|
-
afterAbsent,
|
|
171
|
-
);
|
|
172
|
-
for (const path of new Set(scan.unreadable)) appendFileSync(inv.output, `pstack could not search ${path} for Pi, git, editor, or harness code the job may have left there. Check it before you start Pi in this checkout.\n`);
|
|
173
|
-
return moved;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// The wrapper runs this file directly once the job has ended: `node sweep.ts <inventory.json>`.
|
|
177
|
-
const argv = z.array(z.string()).parse(process.argv);
|
|
178
|
-
if (argv[1] && basename(argv[1]) === 'sweep.ts' && argv[2]) {
|
|
179
|
-
const inv = InventorySchema.parse(JSON.parse(readFileSync(argv[2], 'utf8')));
|
|
180
|
-
try {
|
|
181
|
-
sweep(inv);
|
|
182
|
-
} catch (error) {
|
|
183
|
-
appendFileSync(inv.output, `pstack could not finish checking this job's checkout for Pi, git, editor, or harness code it left behind: ${String(error)}\n`);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
export const inventoryFile = (out: string): string => join(out, 'inventory.json');
|