@justjoshn/pi-pstack-v2 0.1.6 → 0.2.0

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.
@@ -3,6 +3,8 @@ import * as fs from 'node:fs';
3
3
  import * as os from 'node:os';
4
4
  import * as path from 'node:path';
5
5
 
6
+ import { hostEnv } from '../host-env.ts';
7
+
6
8
  export interface ProcessOutcome {
7
9
  code: number | null;
8
10
  signal: NodeJS.Signals | null;
@@ -18,13 +20,28 @@ function shellQuote(value: string): string {
18
20
  return `'${value.replace(/'/g, `'"'"'`)}'`;
19
21
  }
20
22
 
21
- const tmuxHasSession = (session: string): boolean => spawnSync('tmux', ['has-session', '-t', `=${session}`], { stdio: 'ignore' }).status === 0;
23
+ const SOCKET_NAME = 'tmux.sock';
22
24
 
23
- /** A session that is already gone makes this fail, which is expected. */
24
- function tmuxKillSession(session: string): void {
25
- spawnSync('tmux', ['kill-session', '-t', `=${session}`], { stdio: 'ignore' });
25
+ /**
26
+ * Each run gets its own tmux server, started with this process's environment, so the child inherits the dispatching
27
+ * Pi's agent dir, credentials, and PATH. A shared server would hand it the environment that server started with.
28
+ * tmux reaches the socket by a relative path from the run directory, which keeps it under the Unix socket path limit.
29
+ */
30
+ function tmuxArgs(dir: string, args: string[]): [string, string[], { cwd: string; env: NodeJS.ProcessEnv }] {
31
+ const { TMUX: _tmux, TMUX_PANE: _pane, ...env } = hostEnv();
32
+ return ['tmux', ['-S', SOCKET_NAME, ...args], { cwd: dir, env }];
26
33
  }
27
34
 
35
+ function tmuxOk(dir: string, args: string[]): boolean {
36
+ const [command, argv, options] = tmuxArgs(dir, args);
37
+ return spawnSync(command, argv, { ...options, stdio: 'ignore' }).status === 0;
38
+ }
39
+
40
+ /** A server that is already gone makes this fail, which is expected. */
41
+ const tmuxKillServer = (dir: string): void => {
42
+ tmuxOk(dir, ['kill-server']);
43
+ };
44
+
28
45
  const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
29
46
 
30
47
  function readRange(file: string, from: number, to: number): Buffer {
@@ -82,16 +99,17 @@ function prepareRunFiles(command: string, args: string[]): RunFiles {
82
99
 
83
100
  interface WaitContext {
84
101
  exitFile: string;
102
+ dir: string;
85
103
  session: string;
86
104
  pump: () => void;
87
105
  aborted: () => boolean;
88
106
  }
89
107
 
90
108
  /** Polls until the exit file appears. Resolves false when the session vanished without writing one. */
91
- async function waitForExit({ exitFile, session, pump, aborted }: WaitContext): Promise<boolean> {
109
+ async function waitForExit({ exitFile, dir, session, pump, aborted }: WaitContext): Promise<boolean> {
92
110
  while (!fs.existsSync(exitFile)) {
93
111
  pump();
94
- const alive = tmuxHasSession(session);
112
+ const alive = tmuxOk(dir, ['has-session', '-t', `=${session}`]);
95
113
  if (!alive && aborted()) return true;
96
114
  if (!alive) {
97
115
  await sleep(POLL_INTERVAL_MS);
@@ -121,26 +139,27 @@ export async function runPiInTmux(options: {
121
139
  signal?: AbortSignal | undefined;
122
140
  onStdoutChunk: (chunk: string) => void;
123
141
  onStderrChunk: (chunk: string) => void;
124
- /** Test hook: called after the tmux session exists and before waiting for exit. */
125
- onSessionStarted?: ((session: string) => void) | undefined;
142
+ /** Test hook: called after the tmux session exists and before waiting for exit, with the session and its server's socket. */
143
+ onSessionStarted?: ((session: string, socket: string) => void) | undefined;
126
144
  }): Promise<ProcessOutcome> {
127
145
  const session = `pi-subagent-${process.pid}-${Date.now().toString(BASE36)}-${Math.random().toString(BASE36).slice(2, SESSION_SUFFIX_END)}`;
128
146
  const files = prepareRunFiles(options.command, options.args);
129
147
 
130
148
  try {
131
- execFileSync('tmux', ['new-session', '-d', '-s', session, '-c', options.cwd, 'bash', files.wrapper], { stdio: 'ignore' });
149
+ const [command, argv, spawnOptions] = tmuxArgs(files.dir, ['new-session', '-d', '-s', session, '-c', options.cwd, 'bash', files.wrapper]);
150
+ execFileSync(command, argv, { ...spawnOptions, stdio: 'ignore' });
132
151
  } catch (error) {
133
152
  fs.rmSync(files.dir, { recursive: true, force: true });
134
153
  return { code: null, signal: null, spawnError: error instanceof Error ? error : new Error(String(error)) };
135
154
  }
136
155
 
137
- options.onSessionStarted?.(session);
156
+ options.onSessionStarted?.(session, path.join(files.dir, SOCKET_NAME));
138
157
 
139
158
  let wasAborted = false;
140
159
  const abort = () => {
141
160
  if (wasAborted) return;
142
161
  wasAborted = true;
143
- tmuxKillSession(session);
162
+ tmuxKillServer(files.dir);
144
163
  };
145
164
  if (options.signal?.aborted) abort();
146
165
  else options.signal?.addEventListener('abort', abort, { once: true });
@@ -153,13 +172,13 @@ export async function runPiInTmux(options: {
153
172
  };
154
173
 
155
174
  try {
156
- const exited = await waitForExit({ exitFile: files.exitFile, session, pump, aborted: () => wasAborted });
175
+ const exited = await waitForExit({ exitFile: files.exitFile, dir: files.dir, session, pump, aborted: () => wasAborted });
157
176
  if (!exited) return { code: null, signal: wasAborted ? 'SIGTERM' : null };
158
177
  pump();
159
178
  return readOutcome(files.exitFile, wasAborted);
160
179
  } finally {
161
180
  options.signal?.removeEventListener('abort', abort);
162
- tmuxKillSession(session);
181
+ tmuxKillServer(files.dir);
163
182
  fs.rmSync(files.dir, { recursive: true, force: true });
164
183
  }
165
184
  }
@@ -1,6 +1,6 @@
1
1
  import type { AgentToolResult, ThinkingLevel } from '@earendil-works/pi-agent-core';
2
2
  import type { Message } from '@earendil-works/pi-ai';
3
- import type { AgentConfig } from './agents.ts';
3
+ import type { AgentConfig, AgentSource } from './agents.ts';
4
4
 
5
5
  export interface UsageStats {
6
6
  input: number;
@@ -16,7 +16,7 @@ export type ChildStatus = 'pending' | 'running' | 'completed' | 'failed' | 'abor
16
16
 
17
17
  export interface SingleResult {
18
18
  agent: string;
19
- agentSource: 'user' | 'project' | 'unknown';
19
+ agentSource: AgentSource | 'unknown';
20
20
  task: string;
21
21
  status: ChildStatus;
22
22
  exitCode: number | null;
@@ -1,171 +0,0 @@
1
- import { copyFileSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, renameSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
2
- import { isAbsolute, join, resolve } from 'node:path';
3
-
4
- import { z } from 'zod';
5
-
6
- /**
7
- * Jobs never write your real `auth.json`. Pi runs a credential whose key starts with `!` as a shell command, so a job able
8
- * to write that file could plant a command for your next unsandboxed Pi to run. Instead each top-level job, and every job
9
- * it starts, uses its own Pi agent directory through Pi's `PI_CODING_AGENT_DIR`. It links to your real configuration and
10
- * holds a copy of your credentials without commands. Only refreshed OAuth tokens move between the copy and the real file.
11
- */
12
-
13
- type Credentials = Record<string, Record<string, unknown>>;
14
- /** Any JSON object is accepted, as Pi's own reader does. Entries are checked where they are used. */
15
- const CredentialsSchema = z.record(z.string(), z.any());
16
-
17
- export const jobAgentDir = (top: string): string => join(top, 'agent');
18
-
19
- const PRIVATE = new Set(['auth.json', 'auth.json.lock', 'models-store.json', 'models-store.json.lock', 'pstack-v2', 'sessions', 'settings.json', 'settings.json.lock']);
20
-
21
- /** A settings path as it resolves from your real agent directory: local ones made absolute, sources and names kept. */
22
- function anchored(realAgentDir: string, entry: string): string {
23
- const [, prefix = '', path = ''] = /^([!+-]?)(.*)$/s.exec(entry) ?? [];
24
- if (/^[a-z][a-z0-9+.-]*:/i.test(path) || isAbsolute(path) || path.startsWith('~')) return entry;
25
- return `${prefix}${resolve(realAgentDir, path)}`;
26
- }
27
-
28
- /** Anchors one packages entry, a path string or an object with a source path, to the real agent directory. */
29
- function anchoredPackage(realAgentDir: string, entry: unknown): unknown {
30
- if (typeof entry === 'string') return anchored(realAgentDir, entry);
31
- if (entry && typeof entry === 'object' && typeof (entry as { source?: unknown }).source === 'string') {
32
- return { ...entry, source: anchored(realAgentDir, (entry as { source: string }).source) };
33
- }
34
- return entry;
35
- }
36
-
37
- function anchoredSettings(realAgentDir: string, settings: Record<string, unknown>): Record<string, unknown> {
38
- const listed = Object.fromEntries(
39
- ['extensions', 'skills', 'prompts', 'themes'].flatMap((key) => {
40
- const list = settings[key];
41
- return Array.isArray(list) ? [[key, list.map((entry) => (typeof entry === 'string' ? anchored(realAgentDir, entry) : entry))]] : [];
42
- }),
43
- );
44
- const copy = { ...settings, ...listed };
45
- if (Array.isArray(copy['packages'])) {
46
- return {
47
- ...copy,
48
- packages: copy['packages'].map((entry: unknown) => anchoredPackage(realAgentDir, entry)),
49
- };
50
- }
51
- return copy;
52
- }
53
-
54
- function parse(text: string | undefined): Credentials | undefined {
55
- if (text === undefined) return undefined;
56
- try {
57
- const parsed = CredentialsSchema.safeParse(JSON.parse(text.replace(/^/, '')));
58
- return parsed.success ? parsed.data : undefined;
59
- } catch {
60
- return undefined;
61
- }
62
- }
63
-
64
- const readCredentials = (dir: string): Credentials | undefined => {
65
- const path = join(dir, 'auth.json');
66
- return existsSync(path) ? parse(readFileSync(path, 'utf8')) : undefined;
67
- };
68
-
69
- const isCommand = (entry: Record<string, unknown> | undefined): boolean => entry?.['type'] === 'api_key' && typeof entry['key'] === 'string' && entry['key'].startsWith('!');
70
-
71
- /** Providers whose credential in the real auth.json is a command, which the root resolves before handing it to a job. */
72
- export const commandProviders = (realAgentDir: string): string[] =>
73
- Object.entries(readCredentials(realAgentDir) ?? {})
74
- .filter(([, entry]) => isCommand(entry))
75
- .map(([provider]) => provider);
76
-
77
- /**
78
- * Creates the job's agent directory: links to each real entry Pi reads, and a credentials copy in which each command is
79
- * replaced by its resolved key when given, and dropped otherwise.
80
- */
81
- export function makeJobAgentDir(realAgentDir: string, dir: string, resolved: Record<string, string> = {}): void {
82
- mkdirSync(dir, { recursive: true });
83
- for (const name of readdirSync(realAgentDir)) {
84
- const link = join(dir, name);
85
- if (!PRIVATE.has(name) && !existsSync(link) && !isLink(link)) symlinkSync(join(realAgentDir, name), link);
86
- }
87
- const catalog = join(realAgentDir, 'models-store.json');
88
- if (existsSync(catalog)) copyFileSync(catalog, join(dir, 'models-store.json'));
89
- const settings = join(realAgentDir, 'settings.json');
90
- const parsed = existsSync(settings) ? parse(readFileSync(settings, 'utf8')) : undefined;
91
- if (parsed) writeFileSync(join(dir, 'settings.json'), `${JSON.stringify(anchoredSettings(realAgentDir, parsed), null, 2)}\n`);
92
- const copy: Credentials = Object.fromEntries(
93
- Object.entries(readCredentials(realAgentDir) ?? {}).flatMap(([provider, entry]) => {
94
- if (!isCommand(entry)) return [[provider, entry]];
95
- return resolved[provider] !== undefined ? [[provider, { ...entry, key: resolved[provider] }]] : [];
96
- }),
97
- );
98
- writeFileSync(join(dir, 'auth.json'), `${JSON.stringify(copy, null, 2)}\n`, { mode: 0o600 });
99
- }
100
-
101
- function isLink(path: string): boolean {
102
- try {
103
- return lstatSync(path).isSymbolicLink();
104
- } catch {
105
- return false;
106
- }
107
- }
108
-
109
- // Pi guards auth.json with a lock directory beside the file, resolved through links as proper-lockfile does, so a symlinked
110
- // auth.json shares one lock. Both files are locked before either is written, and a busy lock means waiting for the next
111
- // sync rather than writing over a refresh in progress.
112
- const resolved = (file: string): string => (existsSync(file) ? realpathSync(file) : file);
113
-
114
- export function withLocks(files: string[], fn: () => void): boolean {
115
- let held: readonly string[] = [];
116
- try {
117
- for (const file of files) {
118
- const lock = `${resolved(file)}.lock`;
119
- mkdirSync(lock);
120
- held = [...held, lock];
121
- }
122
- fn();
123
- return true;
124
- } catch {
125
- return false;
126
- } finally {
127
- // A job that shares a lock directory may have removed it already, which force tolerates.
128
- for (const lock of held) rmSync(lock, { recursive: true, force: true });
129
- }
130
- }
131
-
132
- // Pi rejects the whole file when an OAuth entry lacks string tokens or a finite expiry.
133
- const validOauth = (entry: Record<string, unknown> | undefined): entry is Record<string, unknown> =>
134
- entry?.['type'] === 'oauth' && typeof entry['access'] === 'string' && entry['access'] !== '' && typeof entry['refresh'] === 'string' && entry['refresh'] !== '' && typeof entry['expires'] === 'number' && Number.isFinite(entry['expires']);
135
-
136
- const expiry = (entry: Record<string, unknown> | undefined): number => (validOauth(entry) ? (entry['expires'] as number) : -1);
137
-
138
- /**
139
- * Carries refreshed OAuth tokens both ways between the real auth.json and a job's copy, the later expiry winning. Only OAuth
140
- * entries move, so nothing a job writes as a key or command ever reaches the real file. Returns false when a lock was busy.
141
- */
142
- export function syncTokens(realAgentDir: string, jobDir: string): boolean {
143
- return withLocks([join(realAgentDir, 'auth.json'), join(jobDir, 'auth.json')], () => {
144
- const real = readCredentials(realAgentDir);
145
- const copy = readCredentials(jobDir);
146
- if (!real || !copy) return;
147
- const providers = [...new Set([...Object.keys(real), ...Object.keys(copy)])];
148
- const realUpdates = providers.flatMap((provider) => {
149
- const [mine, theirs] = [real[provider], copy[provider]];
150
- // A provider you logged out of, or switched to a key, is never brought back from a job's copy.
151
- return expiry(theirs) > expiry(mine) && mine?.['type'] === 'oauth' ? [[provider, theirs as Record<string, unknown>] as const] : [];
152
- });
153
- const copyUpdates = providers.flatMap((provider) => {
154
- const [mine, theirs] = [real[provider], copy[provider]];
155
- const toReal = expiry(theirs) > expiry(mine) && mine?.['type'] === 'oauth';
156
- return !toReal && expiry(mine) > expiry(theirs) && (theirs === undefined || theirs['type'] === 'oauth') ? [[provider, mine as Record<string, unknown>] as const] : [];
157
- });
158
- const realChanged = realUpdates.length > 0;
159
- const copyChanged = copyUpdates.length > 0;
160
- const newReal = { ...real, ...Object.fromEntries(realUpdates) };
161
- const newCopy = { ...copy, ...Object.fromEntries(copyUpdates) };
162
- if (realChanged) {
163
- // Renamed into place so Pi never reads a half-written file. The job's copy is written in place instead, since the
164
- // job may have that file bound to its own view, and a new file would leave it holding the old one.
165
- const path = resolved(join(realAgentDir, 'auth.json'));
166
- writeFileSync(`${path}.pstack-tmp`, `${JSON.stringify(newReal, null, 2)}\n`, { mode: 0o600 });
167
- renameSync(`${path}.pstack-tmp`, path);
168
- }
169
- if (copyChanged) writeFileSync(join(jobDir, 'auth.json'), `${JSON.stringify(newCopy, null, 2)}\n`, { mode: 0o600 });
170
- });
171
- }
package/src/git.ts DELETED
@@ -1,94 +0,0 @@
1
- import { execFileSync, spawnSync } from 'node:child_process';
2
- import { existsSync, realpathSync } from 'node:fs';
3
- import { basename, dirname, join, resolve } from 'node:path';
4
-
5
- import type { ExtensionContext } from '@earendil-works/pi-coding-agent';
6
- import { landing, within } from './paths.ts';
7
- import { codePaths, PI_CODE } from './sweep.ts';
8
-
9
- export function git(cwd: string, args: string[]): string {
10
- return execFileSync('git', ['-C', cwd, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
11
- }
12
-
13
- /** Like `git`, but undefined when git exits non-zero, is killed, or does not start. */
14
- function tryGit(cwd: string, args: string[]): string | undefined {
15
- const result = spawnSync('git', ['-C', cwd, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
16
- return result.status === 0 ? result.stdout.trim() : undefined;
17
- }
18
-
19
- export function gitPath(cwd: string, flag: '--git-common-dir' | '--show-toplevel' | '--absolute-git-dir'): string | undefined {
20
- const found = tryGit(cwd, ['rev-parse', flag]);
21
- return found === undefined ? undefined : resolve(cwd, found);
22
- }
23
-
24
- /** A job may only work inside the session's project, so the model cannot widen its own sandbox by naming another directory. */
25
- export function projectPath(ctx: ExtensionContext, path: string | undefined): string {
26
- const root = realpathSync(gitPath(ctx.cwd, '--show-toplevel') ?? ctx.cwd);
27
- const target = realpathSync(path === undefined ? ctx.cwd : resolve(ctx.cwd, path));
28
- // A linked worktree of the same repository, such as a worker's checkout, is the same project on another branch.
29
- const common = gitPath(ctx.cwd, '--git-common-dir');
30
- const targetCommon = gitPath(target, '--git-common-dir');
31
- const sameRepository = common !== undefined && targetCommon !== undefined && landing(targetCommon) === landing(common);
32
- const worktree = sameRepository ? gitPath(target, '--show-toplevel') : undefined;
33
- if (!within(target, [root, ...(worktree ? [worktree] : [])])) throw new Error(`cwd must be inside this project (${root}), got ${target}. To work on another branch, pass worktree and the job gets its own checkout.`);
34
- // A checkout of another repository can sit inside the project, and a job there would be handed that repository's git directory.
35
- if (targetCommon !== undefined && !sameRepository && !within(targetCommon, [root])) throw new Error(`cwd ${target} is a checkout of another repository (${targetCommon}). A job works only in this session's repository.`);
36
- return target;
37
- }
38
-
39
- /** Each existing directory above `path` up to, and excluding, the checkout. */
40
- function existingAncestors(path: string, cwd: string): string[] {
41
- const dir = dirname(path);
42
- if (!within(dir, [cwd]) || dir === cwd) return [];
43
- return [...(existsSync(dir) ? [dir] : []), ...existingAncestors(dir, cwd)];
44
- }
45
-
46
- /**
47
- * Paths an unsandboxed process later executes from: git hooks and config, Pi's project extensions and settings, and
48
- * editor and agent-harness configuration. Git finds hooks through `.git`, and in a linked worktree through its `commondir`
49
- * and `gitdir` files, so each of those is protected too. A job that could repoint any of them would make the next
50
- * `git commit` run its own hooks unsandboxed.
51
- */
52
- export function protectedPaths(cwd: string, common: string | undefined): { protect: string[]; lock: string[]; gitDirs: string[] } {
53
- // Every code entry already in the checkout, at any depth, since Pi, git, editors, and harnesses find them anywhere.
54
- const found = codePaths([cwd]);
55
- const piDirs = [...new Set([join(cwd, '.pi'), ...found.filter((path) => basename(dirname(path)) === '.pi').map(dirname)])];
56
- const pi = piDirs.flatMap((dir) => PI_CODE.map((name) => join(dir, name)));
57
- // The checkout's own .git is its git directory, which the job may write. Any other is a repository git would run hooks
58
- // from, and an editor or harness entry runs as it stands. `pi` already covers each .pi directory, and a .pi link stays as it is.
59
- const others = found.filter((path) => basename(path) !== '.pi' && basename(dirname(path)) !== '.pi' && path !== join(cwd, '.git'));
60
- let protect = [...pi, ...others];
61
- let lock: string[] = [];
62
- if (common) {
63
- const own = gitPath(cwd, '--absolute-git-dir') ?? common;
64
- const worktree = own === common ? [] : [join(cwd, '.git'), ...['commondir', 'gitdir', 'config.worktree'].map((name) => join(own, name))];
65
- const configured = tryGit(cwd, ['config', '--get', 'core.hooksPath']);
66
- const hooksPath = configured ? [resolve(gitPath(cwd, '--show-toplevel') ?? cwd, configured)] : [];
67
- protect = [...protect, join(common, 'hooks'), join(common, 'config'), join(common, 'config.worktree'), join(common, 'info'), ...worktree, ...hooksPath];
68
- lock = [join(cwd, '.git'), own, common];
69
- }
70
- // A directory above a protected path could be renamed away and replaced, so each existing one up to the checkout is locked.
71
- const ancestors = protect.flatMap((path) => existingAncestors(path, cwd));
72
- return { protect, lock: [...new Set([...lock, ...ancestors])], gitDirs: common ? [common] : [] };
73
- }
74
-
75
- /** Adds the worktree and returns whether it created the branch, so a failed start can remove it. */
76
- export function addWorktree(cwd: string, path: string, branch: string, base: string | undefined): boolean {
77
- if (branch.startsWith('-') || base?.startsWith('-')) throw new Error('worktree and base cannot start with "-".');
78
- const exists = tryGit(cwd, ['rev-parse', '--verify', '--quiet', `refs/heads/${branch}`]) !== undefined;
79
- git(cwd, exists ? ['worktree', 'add', path, branch] : ['worktree', 'add', '-b', branch, path, base === undefined ? 'HEAD' : baseOrSoleRemoteBranch(cwd, base)]);
80
- return !exists;
81
- }
82
-
83
- export function dropWorktree(repo: string, createdBranch: string | undefined): void {
84
- git(repo, ['worktree', 'prune']);
85
- if (createdBranch !== undefined) git(repo, ['branch', '-D', createdBranch]);
86
- }
87
-
88
- function baseOrSoleRemoteBranch(cwd: string, base: string): string {
89
- if (tryGit(cwd, ['rev-parse', '--verify', '--quiet', `${base}^{commit}`]) !== undefined) return base;
90
- const remote = git(cwd, ['for-each-ref', '--format=%(refname:short)', `refs/remotes/*/${base}`])
91
- .split('\n')
92
- .filter(Boolean);
93
- return remote.length === 1 ? (remote[0] ?? base) : base;
94
- }
package/src/paths.ts DELETED
@@ -1,16 +0,0 @@
1
- import { existsSync, realpathSync } from 'node:fs';
2
- import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
3
-
4
- /** Resolves symlinks in the longest existing prefix, so a path that does not exist yet still compares by where it would land. */
5
- export function landing(path: string): string {
6
- const absolute = resolve(path);
7
- if (existsSync(absolute)) return realpathSync(absolute);
8
- const parent = dirname(absolute);
9
- return parent === absolute ? absolute : join(landing(parent), relative(parent, absolute));
10
- }
11
-
12
- export const within = (path: string, roots: string[]): boolean =>
13
- roots.some((root) => {
14
- const inside = relative(landing(root), landing(path));
15
- return inside !== '..' && !inside.startsWith(`..${sep}`) && !isAbsolute(inside);
16
- });
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
- }