@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
|
@@ -3,7 +3,7 @@ import { getPiInvocation, removePromptTempFile, writePromptToTempFile } from './
|
|
|
3
3
|
import { createResultStream, finalizeResult } from './result-stream.ts';
|
|
4
4
|
import { createUnstartedResult, emptyUsage, getFinalOutput } from './results.ts';
|
|
5
5
|
import { runPiInTmux } from './run-in-tmux.ts';
|
|
6
|
-
import type { DispatchDefaults, MakeDetails, OnUpdateCallback, ResolvedAgent, SingleResult } from './types.ts';
|
|
6
|
+
import type { CallModel, DispatchDefaults, MakeDetails, OnUpdateCallback, ResolvedAgent, SingleResult } from './types.ts';
|
|
7
7
|
|
|
8
8
|
interface AgentRun {
|
|
9
9
|
cwd: string;
|
|
@@ -16,9 +16,11 @@ interface AgentRun {
|
|
|
16
16
|
|
|
17
17
|
const ABORTED_BEFORE_START = 'Subagent was aborted before starting';
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
19
|
+
// A model named by the call or the agent runs at its own default thinking level unless the call names one.
|
|
20
|
+
function buildPiArgs(agent: AgentConfig, defaults: DispatchDefaults, call: CallModel, promptPath: string | undefined, task: string): string[] {
|
|
21
|
+
const namedModel = call.model ?? agent.model;
|
|
22
|
+
const model = namedModel ?? defaults.model;
|
|
23
|
+
const thinking = call.thinking ?? (namedModel ? undefined : defaults.thinkingLevel);
|
|
22
24
|
return [
|
|
23
25
|
'--mode',
|
|
24
26
|
'json',
|
|
@@ -63,7 +65,7 @@ async function runResolvedAgent(
|
|
|
63
65
|
onUpdate: OnUpdateCallback | undefined,
|
|
64
66
|
makeDetails: MakeDetails,
|
|
65
67
|
): Promise<SingleResult> {
|
|
66
|
-
const { agent, cwd } = resolved;
|
|
68
|
+
const { agent, cwd, call = {} } = resolved;
|
|
67
69
|
const initial: SingleResult = {
|
|
68
70
|
agent: agentName,
|
|
69
71
|
agentSource: agent.source,
|
|
@@ -73,14 +75,14 @@ async function runResolvedAgent(
|
|
|
73
75
|
messages: [],
|
|
74
76
|
stderr: '',
|
|
75
77
|
usage: emptyUsage(),
|
|
76
|
-
model: agent.model ?? defaults.model,
|
|
78
|
+
model: call.model ?? agent.model ?? defaults.model,
|
|
77
79
|
step,
|
|
78
80
|
};
|
|
79
81
|
let prompt: Awaited<ReturnType<typeof writePromptToTempFile>> | undefined;
|
|
80
82
|
try {
|
|
81
83
|
prompt = agent.systemPrompt.trim() ? await writePromptToTempFile(agent.name, agent.systemPrompt) : undefined;
|
|
82
84
|
if (signal?.aborted) return { ...initial, status: 'aborted', errorMessage: ABORTED_BEFORE_START };
|
|
83
|
-
const args = buildPiArgs(agent, defaults, prompt?.filePath, task);
|
|
85
|
+
const args = buildPiArgs(agent, defaults, call, prompt?.filePath, task);
|
|
84
86
|
return await streamAgent({ cwd, initial, args, signal, onUpdate, makeDetails });
|
|
85
87
|
} finally {
|
|
86
88
|
removePromptTempFile(prompt);
|
package/src/subagent/schema.ts
CHANGED
|
@@ -3,16 +3,25 @@ import { type Static, Type } from 'typebox';
|
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import type { AgentScope } from './agents.ts';
|
|
5
5
|
|
|
6
|
+
const THINKING_LEVELS = ['off', 'minimal', 'low', 'medium', 'high', 'xhigh'] as const;
|
|
7
|
+
type ThinkingLevelName = (typeof THINKING_LEVELS)[number];
|
|
8
|
+
const ModelParam = Type.Optional(Type.String({ description: 'Model as provider/id. Overrides the agent and session model.' }));
|
|
9
|
+
const ThinkingParam = Type.Optional(StringEnum(THINKING_LEVELS, { description: 'Thinking level. Defaults to the session level unless a model is named.' }));
|
|
10
|
+
|
|
6
11
|
const TaskItem = Type.Object({
|
|
7
12
|
agent: Type.String({ description: 'Name of the agent to invoke' }),
|
|
8
13
|
task: Type.String({ description: 'Task to delegate to the agent' }),
|
|
9
14
|
cwd: Type.Optional(Type.String({ description: 'Working directory for the agent process' })),
|
|
15
|
+
model: ModelParam,
|
|
16
|
+
thinking: ThinkingParam,
|
|
10
17
|
});
|
|
11
18
|
|
|
12
19
|
const ChainItem = Type.Object({
|
|
13
20
|
agent: Type.String({ description: 'Name of the agent to invoke' }),
|
|
14
21
|
task: Type.String({ description: 'Task with optional {previous} placeholder for prior output' }),
|
|
15
22
|
cwd: Type.Optional(Type.String({ description: 'Working directory for the agent process' })),
|
|
23
|
+
model: ModelParam,
|
|
24
|
+
thinking: ThinkingParam,
|
|
16
25
|
});
|
|
17
26
|
|
|
18
27
|
const AgentScopeSchema = StringEnum(['user', 'project', 'both'] as const, {
|
|
@@ -28,12 +37,23 @@ export const SubagentParams = Type.Object({
|
|
|
28
37
|
agentScope: Type.Optional(AgentScopeSchema),
|
|
29
38
|
confirmProjectAgents: Type.Optional(Type.Boolean({ description: 'Prompt before running project-local agents. Default: true.', default: true })),
|
|
30
39
|
cwd: Type.Optional(Type.String({ description: 'Working directory for the agent process (single mode)' })),
|
|
40
|
+
model: ModelParam,
|
|
41
|
+
thinking: ThinkingParam,
|
|
42
|
+
wake: Type.Optional(Type.String({ description: 'Return a job id at once and wake the session with this message and the report when every worker finishes.' })),
|
|
43
|
+
stop: Type.Optional(Type.String({ description: 'Abort the running wake job with this id.' })),
|
|
31
44
|
});
|
|
32
45
|
|
|
33
46
|
export type SubagentParamsType = Static<typeof SubagentParams>;
|
|
34
|
-
|
|
47
|
+
// StringEnum types its fields as unknown, so the parsed shape names them.
|
|
48
|
+
export type ParsedSubagentParams = Omit<SubagentParamsType, 'agentScope' | 'thinking' | 'tasks' | 'chain'> & {
|
|
49
|
+
agentScope?: AgentScope | undefined;
|
|
50
|
+
thinking?: ThinkingLevelName | undefined;
|
|
51
|
+
tasks?: WorkItem[] | undefined;
|
|
52
|
+
chain?: WorkItem[] | undefined;
|
|
53
|
+
};
|
|
35
54
|
|
|
36
|
-
const
|
|
55
|
+
const ThinkingSchema = z.enum(THINKING_LEVELS).optional();
|
|
56
|
+
const WorkItemSchema = z.looseObject({ agent: z.string(), task: z.string(), cwd: z.string().optional(), model: z.string().optional(), thinking: ThinkingSchema });
|
|
37
57
|
|
|
38
58
|
/**
|
|
39
59
|
* Mirrors SubagentParams, which Pi checks before the tool runs, so it accepts exactly the calls that reach the tool. The
|
|
@@ -47,6 +67,10 @@ export const SubagentParamsSchema = z.looseObject({
|
|
|
47
67
|
agentScope: z.enum(['user', 'project', 'both']).optional(),
|
|
48
68
|
confirmProjectAgents: z.boolean().optional(),
|
|
49
69
|
cwd: z.string().optional(),
|
|
70
|
+
model: z.string().optional(),
|
|
71
|
+
thinking: ThinkingSchema,
|
|
72
|
+
wake: z.string().optional(),
|
|
73
|
+
stop: z.string().optional(),
|
|
50
74
|
});
|
|
51
75
|
|
|
52
76
|
export const toAgentScope = (value: unknown): AgentScope => (value === 'project' || value === 'both' ? value : 'user');
|
|
@@ -55,4 +79,6 @@ export interface WorkItem {
|
|
|
55
79
|
agent: string;
|
|
56
80
|
task: string;
|
|
57
81
|
cwd?: string | undefined;
|
|
82
|
+
model?: string | undefined;
|
|
83
|
+
thinking?: ThinkingLevelName | undefined;
|
|
58
84
|
}
|
package/src/subagent/types.ts
CHANGED
|
@@ -53,10 +53,17 @@ export interface DispatchRun {
|
|
|
53
53
|
onUpdate: OnUpdateCallback | undefined;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
/** The model and thinking level one call names for one item, ahead of the agent's and the session's. */
|
|
57
|
+
export interface CallModel {
|
|
58
|
+
model?: string | undefined;
|
|
59
|
+
thinking?: ThinkingLevel | undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
56
62
|
export interface ResolvedAgent {
|
|
57
63
|
agent: AgentConfig | undefined;
|
|
58
64
|
available: AgentConfig[];
|
|
59
65
|
cwd: string;
|
|
66
|
+
call?: CallModel | undefined;
|
|
60
67
|
}
|
|
61
68
|
|
|
62
69
|
export type DisplayItem = { type: 'text'; text: string } | { type: 'toolCall'; name: string; args: Record<string, unknown> };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
import type { ExtensionAPI } from '@earendil-works/pi-coding-agent';
|
|
4
|
+
import type { ToolResult } from './results.ts';
|
|
5
|
+
|
|
6
|
+
export const WAKE_MESSAGE_TYPE = 'pstack-subagent';
|
|
7
|
+
const ID_BYTES = 3;
|
|
8
|
+
|
|
9
|
+
type Mode = { mode?: string };
|
|
10
|
+
type Running = { controller: AbortController; done: Promise<void> };
|
|
11
|
+
|
|
12
|
+
const interactive = (ctx: Mode): boolean => ctx.mode === 'tui' || ctx.mode === 'rpc';
|
|
13
|
+
|
|
14
|
+
const reportOf = (result: ToolResult): string =>
|
|
15
|
+
result.content
|
|
16
|
+
.flatMap((part) => (part.type === 'text' ? [part.text] : []))
|
|
17
|
+
.join('\n')
|
|
18
|
+
.trim() || '(no output)';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Subagent calls that return a job id at once and report through a wake message when every worker finishes. An
|
|
22
|
+
* interactive session is woken with a steering message. A print-mode Pi has no later turn to wake, so it collects
|
|
23
|
+
* the reports and waits for them before it settles.
|
|
24
|
+
*/
|
|
25
|
+
export function createWakeJobs(pi: ExtensionAPI) {
|
|
26
|
+
let running: ReadonlyMap<string, Running> = new Map();
|
|
27
|
+
let undelivered: readonly string[] = [];
|
|
28
|
+
|
|
29
|
+
const deliver = (ctx: Mode, content: string): void => {
|
|
30
|
+
if (interactive(ctx)) {
|
|
31
|
+
pi.sendMessage({ customType: WAKE_MESSAGE_TYPE, content, display: true }, { triggerTurn: true, deliverAs: 'steer' });
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
undelivered = [...undelivered, content];
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const start = (wake: string, ctx: Mode, run: (signal: AbortSignal) => Promise<ToolResult>): string => {
|
|
38
|
+
const id = `subagent-${randomBytes(ID_BYTES).toString('hex')}`;
|
|
39
|
+
const controller = new AbortController();
|
|
40
|
+
const done = run(controller.signal)
|
|
41
|
+
.then(reportOf, (error: unknown) => `Failed: ${error instanceof Error ? error.message : String(error)}`)
|
|
42
|
+
.then((report) => {
|
|
43
|
+
running = new Map([...running].filter(([key]) => key !== id));
|
|
44
|
+
deliver(ctx, `${wake}\n\nsubagent job ${id} finished.\n\n${report}`);
|
|
45
|
+
});
|
|
46
|
+
running = new Map([...running, [id, { controller, done }]]);
|
|
47
|
+
return id;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const stop = (id: string): boolean => {
|
|
51
|
+
const job = running.get(id);
|
|
52
|
+
job?.controller.abort();
|
|
53
|
+
return job !== undefined;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const stopAll = (): void => {
|
|
57
|
+
for (const job of running.values()) job.controller.abort();
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** Waits for every running job, then hands over the reports not yet delivered. */
|
|
61
|
+
const settle = async (): Promise<readonly string[]> => {
|
|
62
|
+
await Promise.all([...running.values()].map((job) => job.done));
|
|
63
|
+
const reports = undelivered;
|
|
64
|
+
undelivered = [];
|
|
65
|
+
return reports;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
return { start, stop, stopAll, settle };
|
|
69
|
+
}
|
package/src/credentials.ts
DELETED
|
@@ -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
|
-
});
|