@pi-agents/orchid 0.1.0-beta.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.
- package/CHANGELOG.md +41 -0
- package/LICENSE +21 -0
- package/README.md +246 -0
- package/agents/AGENTS-MANIFEST.md +42 -0
- package/agents/brain.md +42 -0
- package/agents/context-builder.md +46 -0
- package/agents/delegate.md +12 -0
- package/agents/dev-1.md +42 -0
- package/agents/oracle.md +73 -0
- package/agents/planner.md +55 -0
- package/agents/researcher.md +52 -0
- package/agents/reviewer.md +79 -0
- package/agents/scout.md +50 -0
- package/agents/tester.md +45 -0
- package/agents/worker.md +55 -0
- package/extensions/ralph.ts +1 -0
- package/extensions/reviewer-extension.ts +125 -0
- package/extensions/task-orchestrator.ts +28 -0
- package/package.json +63 -0
- package/prompts/gather-context-and-clarify.md +13 -0
- package/prompts/parallel-cleanup.md +59 -0
- package/prompts/parallel-context-build.md +53 -0
- package/prompts/parallel-handoff-plan.md +59 -0
- package/prompts/parallel-research.md +50 -0
- package/prompts/parallel-review.md +54 -0
- package/prompts/review-loop.md +41 -0
- package/skills/orchid/SKILL.md +214 -0
- package/skills/orchid/orchid-cleanup/SKILL.md +122 -0
- package/skills/orchid/orchid-converge/SKILL.md +124 -0
- package/skills/orchid/orchid-decompose/SKILL.md +201 -0
- package/skills/orchid/orchid-doctor/SKILL.md +162 -0
- package/skills/orchid/orchid-investigate/SKILL.md +102 -0
- package/skills/orchid/orchid-launch/SKILL.md +147 -0
- package/skills/ralph/SKILL.md +73 -0
- package/skills/subagents/pi-subagents/SKILL.md +813 -0
- package/src/index.ts +7 -0
- package/src/orchestrator/abort.ts +534 -0
- package/src/orchestrator/agent-bridge-extension.ts +1020 -0
- package/src/orchestrator/agent-host.ts +954 -0
- package/src/orchestrator/cleanup.ts +776 -0
- package/src/orchestrator/config-loader.ts +1412 -0
- package/src/orchestrator/config-schema.ts +690 -0
- package/src/orchestrator/config.ts +81 -0
- package/src/orchestrator/context-window.ts +66 -0
- package/src/orchestrator/diagnostic-reports.ts +475 -0
- package/src/orchestrator/diagnostics.ts +394 -0
- package/src/orchestrator/discovery.ts +1833 -0
- package/src/orchestrator/engine-worker.ts +415 -0
- package/src/orchestrator/engine.ts +5940 -0
- package/src/orchestrator/execution.ts +3104 -0
- package/src/orchestrator/extension.ts +5934 -0
- package/src/orchestrator/formatting.ts +785 -0
- package/src/orchestrator/git.ts +88 -0
- package/src/orchestrator/index.ts +28 -0
- package/src/orchestrator/lane-runner.ts +1787 -0
- package/src/orchestrator/mailbox.ts +780 -0
- package/src/orchestrator/merge.ts +3414 -0
- package/src/orchestrator/messages.ts +1062 -0
- package/src/orchestrator/migrations.ts +278 -0
- package/src/orchestrator/naming.ts +117 -0
- package/src/orchestrator/path-resolver.ts +275 -0
- package/src/orchestrator/persistence.ts +2625 -0
- package/src/orchestrator/process-registry.ts +452 -0
- package/src/orchestrator/quality-gate.ts +1085 -0
- package/src/orchestrator/resume.ts +3488 -0
- package/src/orchestrator/sessions.ts +57 -0
- package/src/orchestrator/settings-loader.ts +136 -0
- package/src/orchestrator/settings-tui.ts +2208 -0
- package/src/orchestrator/sidecar-telemetry.ts +267 -0
- package/src/orchestrator/supervisor.ts +4548 -0
- package/src/orchestrator/task-executor-core.ts +675 -0
- package/src/orchestrator/tmux-compat.ts +37 -0
- package/src/orchestrator/tool-allowlist-constants.ts +37 -0
- package/src/orchestrator/types.ts +4465 -0
- package/src/orchestrator/verification.ts +547 -0
- package/src/orchestrator/waves.ts +1564 -0
- package/src/orchestrator/workspace.ts +707 -0
- package/src/orchestrator/worktree.ts +2725 -0
- package/src/ralph/index.ts +825 -0
- package/src/subagents/agents/agent-management.ts +648 -0
- package/src/subagents/agents/agent-scope.ts +6 -0
- package/src/subagents/agents/agent-selection.ts +23 -0
- package/src/subagents/agents/agent-serializer.ts +86 -0
- package/src/subagents/agents/agents.ts +832 -0
- package/src/subagents/agents/chain-serializer.ts +137 -0
- package/src/subagents/agents/frontmatter.ts +29 -0
- package/src/subagents/agents/identity.ts +30 -0
- package/src/subagents/agents/skills.ts +632 -0
- package/src/subagents/extension/config.ts +16 -0
- package/src/subagents/extension/control-notices.ts +92 -0
- package/src/subagents/extension/doctor.ts +199 -0
- package/src/subagents/extension/fanout-child.ts +170 -0
- package/src/subagents/extension/index.ts +573 -0
- package/src/subagents/extension/schemas.ts +168 -0
- package/src/subagents/intercom/intercom-bridge.ts +379 -0
- package/src/subagents/intercom/result-intercom.ts +377 -0
- package/src/subagents/runs/background/async-execution.ts +712 -0
- package/src/subagents/runs/background/async-job-tracker.ts +310 -0
- package/src/subagents/runs/background/async-resume.ts +345 -0
- package/src/subagents/runs/background/async-status.ts +325 -0
- package/src/subagents/runs/background/completion-dedupe.ts +63 -0
- package/src/subagents/runs/background/notify.ts +108 -0
- package/src/subagents/runs/background/parallel-groups.ts +45 -0
- package/src/subagents/runs/background/result-watcher.ts +307 -0
- package/src/subagents/runs/background/run-id-resolver.ts +83 -0
- package/src/subagents/runs/background/run-status.ts +269 -0
- package/src/subagents/runs/background/stale-run-reconciler.ts +336 -0
- package/src/subagents/runs/background/subagent-runner.ts +1808 -0
- package/src/subagents/runs/background/top-level-async.ts +13 -0
- package/src/subagents/runs/foreground/chain-clarify.ts +1333 -0
- package/src/subagents/runs/foreground/chain-execution.ts +938 -0
- package/src/subagents/runs/foreground/execution.ts +918 -0
- package/src/subagents/runs/foreground/subagent-executor.ts +2527 -0
- package/src/subagents/runs/shared/completion-guard.ts +147 -0
- package/src/subagents/runs/shared/long-running-guard.ts +175 -0
- package/src/subagents/runs/shared/mcp-direct-tool-allowlist.ts +365 -0
- package/src/subagents/runs/shared/model-fallback.ts +103 -0
- package/src/subagents/runs/shared/nested-events.ts +819 -0
- package/src/subagents/runs/shared/nested-path.ts +52 -0
- package/src/subagents/runs/shared/nested-render.ts +115 -0
- package/src/subagents/runs/shared/parallel-utils.ts +109 -0
- package/src/subagents/runs/shared/pi-args.ts +220 -0
- package/src/subagents/runs/shared/pi-spawn.ts +115 -0
- package/src/subagents/runs/shared/run-history.ts +60 -0
- package/src/subagents/runs/shared/single-output.ts +164 -0
- package/src/subagents/runs/shared/subagent-control.ts +226 -0
- package/src/subagents/runs/shared/subagent-prompt-runtime.ts +170 -0
- package/src/subagents/runs/shared/worktree.ts +577 -0
- package/src/subagents/shared/artifacts.ts +98 -0
- package/src/subagents/shared/atomic-json.ts +16 -0
- package/src/subagents/shared/file-coalescer.ts +40 -0
- package/src/subagents/shared/fork-context.ts +76 -0
- package/src/subagents/shared/formatters.ts +133 -0
- package/src/subagents/shared/jsonl-writer.ts +81 -0
- package/src/subagents/shared/model-info.ts +78 -0
- package/src/subagents/shared/post-exit-stdio-guard.ts +85 -0
- package/src/subagents/shared/session-identity.ts +10 -0
- package/src/subagents/shared/session-tokens.ts +44 -0
- package/src/subagents/shared/settings.ts +397 -0
- package/src/subagents/shared/status-format.ts +49 -0
- package/src/subagents/shared/types.ts +822 -0
- package/src/subagents/shared/utils.ts +450 -0
- package/src/subagents/slash/prompt-template-bridge.ts +397 -0
- package/src/subagents/slash/slash-bridge.ts +174 -0
- package/src/subagents/slash/slash-commands.ts +528 -0
- package/src/subagents/slash/slash-live-state.ts +292 -0
- package/src/subagents/tui/render-helpers.ts +80 -0
- package/src/subagents/tui/render.ts +1358 -0
- package/templates/agents/local/supervisor.md +33 -0
- package/templates/agents/local/task-merger.md +27 -0
- package/templates/agents/local/task-reviewer.md +30 -0
- package/templates/agents/local/task-worker.md +34 -0
- package/templates/agents/supervisor-routing.md +92 -0
- package/templates/agents/supervisor.md +229 -0
- package/templates/agents/task-merger.md +214 -0
- package/templates/agents/task-reviewer.md +260 -0
- package/templates/agents/task-worker-segment.md +44 -0
- package/templates/agents/task-worker.md +557 -0
- package/templates/tasks/CONTEXT.md +30 -0
- package/templates/tasks/EXAMPLE-001-hello-world/PROMPT.md +98 -0
- package/templates/tasks/EXAMPLE-001-hello-world/STATUS.md +73 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/PROMPT.md +97 -0
- package/templates/tasks/EXAMPLE-002-parallel-smoke/STATUS.md +73 -0
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as os from "node:os";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
|
|
6
|
+
export interface WorktreeSetup {
|
|
7
|
+
cwd: string;
|
|
8
|
+
worktrees: WorktreeInfo[];
|
|
9
|
+
baseCommit: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface WorktreeInfo {
|
|
13
|
+
path: string;
|
|
14
|
+
agentCwd: string;
|
|
15
|
+
branch: string;
|
|
16
|
+
index: number;
|
|
17
|
+
nodeModulesLinked: boolean;
|
|
18
|
+
syntheticPaths: string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface WorktreeDiff {
|
|
22
|
+
index: number;
|
|
23
|
+
agent: string;
|
|
24
|
+
branch: string;
|
|
25
|
+
diffStat: string;
|
|
26
|
+
filesChanged: number;
|
|
27
|
+
insertions: number;
|
|
28
|
+
deletions: number;
|
|
29
|
+
patchPath: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface WorktreeTaskCwdConflict {
|
|
33
|
+
index: number;
|
|
34
|
+
agent: string;
|
|
35
|
+
cwd: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface WorktreeSetupHookConfig {
|
|
39
|
+
hookPath: string;
|
|
40
|
+
timeoutMs?: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface CreateWorktreesOptions {
|
|
44
|
+
agents?: string[];
|
|
45
|
+
setupHook?: WorktreeSetupHookConfig;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface ResolvedWorktreeSetupHook {
|
|
49
|
+
hookPath: string;
|
|
50
|
+
timeoutMs: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface WorktreeSetupHookInput {
|
|
54
|
+
version: 1;
|
|
55
|
+
repoRoot: string;
|
|
56
|
+
worktreePath: string;
|
|
57
|
+
agentCwd: string;
|
|
58
|
+
branch: string;
|
|
59
|
+
index: number;
|
|
60
|
+
runId: string;
|
|
61
|
+
baseCommit: string;
|
|
62
|
+
agent?: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface WorktreeSetupHookOutput {
|
|
66
|
+
syntheticPaths?: string[];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface GitResult {
|
|
70
|
+
stdout: string;
|
|
71
|
+
stderr: string;
|
|
72
|
+
status: number | null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface RepoState {
|
|
76
|
+
toplevel: string;
|
|
77
|
+
cwdRelative: string;
|
|
78
|
+
baseCommit: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const DEFAULT_WORKTREE_SETUP_HOOK_TIMEOUT_MS = 30000;
|
|
82
|
+
|
|
83
|
+
function runGit(cwd: string, args: string[]): GitResult {
|
|
84
|
+
const result = spawnSync("git", ["-C", cwd, ...args], { encoding: "utf-8" });
|
|
85
|
+
return {
|
|
86
|
+
stdout: result.stdout ?? "",
|
|
87
|
+
stderr: result.stderr ?? "",
|
|
88
|
+
status: result.status,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function runGitChecked(cwd: string, args: string[]): string {
|
|
93
|
+
const result = runGit(cwd, args);
|
|
94
|
+
if (result.status !== 0) {
|
|
95
|
+
const command = `git -C ${cwd} ${args.join(" ")}`;
|
|
96
|
+
const message = result.stderr.trim() || result.stdout.trim() || `${command} failed`;
|
|
97
|
+
throw new Error(message);
|
|
98
|
+
}
|
|
99
|
+
return result.stdout;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function resolveRepoState(cwd: string): RepoState {
|
|
103
|
+
const cwdRelative = resolveRepoCwdRelative(cwd);
|
|
104
|
+
const toplevel = runGitChecked(cwd, ["rev-parse", "--show-toplevel"]).trim();
|
|
105
|
+
|
|
106
|
+
const status = runGitChecked(toplevel, ["status", "--porcelain"]);
|
|
107
|
+
if (status.trim().length > 0) {
|
|
108
|
+
throw new Error("worktree isolation requires a clean git working tree. Commit or stash changes first.");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const baseCommit = runGitChecked(toplevel, ["rev-parse", "HEAD"]).trim();
|
|
112
|
+
return { toplevel, cwdRelative, baseCommit };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function normalizeComparableCwd(cwd: string): string {
|
|
116
|
+
const resolved = path.resolve(cwd);
|
|
117
|
+
try {
|
|
118
|
+
return fs.realpathSync(resolved);
|
|
119
|
+
} catch {
|
|
120
|
+
// Use the unresolved absolute path when realpath resolution is unavailable.
|
|
121
|
+
return resolved;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function findWorktreeTaskCwdConflict(
|
|
126
|
+
tasks: ReadonlyArray<{ agent: string; cwd?: string }>,
|
|
127
|
+
sharedCwd: string,
|
|
128
|
+
): WorktreeTaskCwdConflict | undefined {
|
|
129
|
+
const normalizedSharedCwd = normalizeComparableCwd(sharedCwd);
|
|
130
|
+
for (let index = 0; index < tasks.length; index++) {
|
|
131
|
+
const task = tasks[index]!;
|
|
132
|
+
if (!task.cwd) continue;
|
|
133
|
+
const taskCwd = path.isAbsolute(task.cwd) ? task.cwd : path.resolve(sharedCwd, task.cwd);
|
|
134
|
+
if (normalizeComparableCwd(taskCwd) === normalizedSharedCwd) continue;
|
|
135
|
+
return { index, agent: task.agent, cwd: task.cwd };
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function formatWorktreeTaskCwdConflict(
|
|
141
|
+
conflict: WorktreeTaskCwdConflict,
|
|
142
|
+
sharedCwd: string,
|
|
143
|
+
): string {
|
|
144
|
+
return `worktree isolation uses the shared cwd (${sharedCwd}); task ${conflict.index + 1} (${conflict.agent}) sets cwd to ${conflict.cwd}. Remove task-level cwd overrides or disable worktree.`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function safePatchAgentName(agent: string): string {
|
|
148
|
+
return agent.replace(/[^\w.-]/g, "_");
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function buildWorktreeBranch(runId: string, index: number): string {
|
|
152
|
+
return `pi-parallel-${runId}-${index}`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function buildWorktreePath(runId: string, index: number): string {
|
|
156
|
+
return path.join(os.tmpdir(), `pi-worktree-${runId}-${index}`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function resolveRepoCwdRelative(cwd: string): string {
|
|
160
|
+
const repoCheck = runGit(cwd, ["rev-parse", "--is-inside-work-tree"]);
|
|
161
|
+
if (repoCheck.status !== 0 || repoCheck.stdout.trim() !== "true") {
|
|
162
|
+
throw new Error("worktree isolation requires a git repository");
|
|
163
|
+
}
|
|
164
|
+
const rawPrefix = runGitChecked(cwd, ["rev-parse", "--show-prefix"]).trim();
|
|
165
|
+
const normalizedPrefix = rawPrefix
|
|
166
|
+
? path.normalize(rawPrefix.replace(/[\\/]+$/, ""))
|
|
167
|
+
: "";
|
|
168
|
+
return normalizedPrefix === "." ? "" : normalizedPrefix;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function resolveExpectedWorktreeAgentCwd(cwd: string, runId: string, index: number): string {
|
|
172
|
+
const cwdRelative = resolveRepoCwdRelative(cwd);
|
|
173
|
+
const worktreePath = buildWorktreePath(runId, index);
|
|
174
|
+
return cwdRelative ? path.join(worktreePath, cwdRelative) : worktreePath;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function linkNodeModulesIfPresent(toplevel: string, worktreePath: string): boolean {
|
|
178
|
+
const nodeModulesPath = path.join(toplevel, "node_modules");
|
|
179
|
+
const nodeModulesLinkPath = path.join(worktreePath, "node_modules");
|
|
180
|
+
if (!fs.existsSync(nodeModulesPath) || fs.existsSync(nodeModulesLinkPath)) return false;
|
|
181
|
+
try {
|
|
182
|
+
fs.symlinkSync(nodeModulesPath, nodeModulesLinkPath);
|
|
183
|
+
return true;
|
|
184
|
+
} catch {
|
|
185
|
+
// Symlink creation is optional (e.g., unsupported filesystems on CI runners).
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function parseHookTimeout(timeoutMs: number | undefined): number {
|
|
191
|
+
if (timeoutMs === undefined) return DEFAULT_WORKTREE_SETUP_HOOK_TIMEOUT_MS;
|
|
192
|
+
if (!Number.isInteger(timeoutMs) || timeoutMs <= 0) {
|
|
193
|
+
throw new Error("worktree setup hook timeout must be an integer greater than 0");
|
|
194
|
+
}
|
|
195
|
+
return timeoutMs;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function resolveWorktreeSetupHook(
|
|
199
|
+
repoRoot: string,
|
|
200
|
+
config: WorktreeSetupHookConfig | undefined,
|
|
201
|
+
): ResolvedWorktreeSetupHook | undefined {
|
|
202
|
+
if (!config) return undefined;
|
|
203
|
+
const hookPath = config.hookPath.trim();
|
|
204
|
+
if (!hookPath) {
|
|
205
|
+
throw new Error("worktree setup hook path cannot be empty");
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const expandedHookPath = hookPath.startsWith("~/") ? path.join(os.homedir(), hookPath.slice(2)) : hookPath;
|
|
209
|
+
let resolvedPath: string;
|
|
210
|
+
if (path.isAbsolute(expandedHookPath)) {
|
|
211
|
+
resolvedPath = expandedHookPath;
|
|
212
|
+
} else if (expandedHookPath.includes("/") || expandedHookPath.includes("\\")) {
|
|
213
|
+
resolvedPath = path.resolve(repoRoot, expandedHookPath);
|
|
214
|
+
} else {
|
|
215
|
+
throw new Error("worktree setup hook must be an absolute path or a repo-relative path");
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (!fs.existsSync(resolvedPath)) {
|
|
219
|
+
throw new Error(`worktree setup hook not found: ${resolvedPath}`);
|
|
220
|
+
}
|
|
221
|
+
if (fs.statSync(resolvedPath).isDirectory()) {
|
|
222
|
+
throw new Error(`worktree setup hook must be a file, got directory: ${resolvedPath}`);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
hookPath: resolvedPath,
|
|
227
|
+
timeoutMs: parseHookTimeout(config.timeoutMs),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function normalizeSyntheticPath(worktreePath: string, rawPath: string): string {
|
|
232
|
+
const trimmed = rawPath.trim();
|
|
233
|
+
if (!trimmed) throw new Error("synthetic path cannot be empty");
|
|
234
|
+
if (path.isAbsolute(trimmed)) throw new Error(`synthetic path must be relative: ${rawPath}`);
|
|
235
|
+
|
|
236
|
+
const resolved = path.resolve(worktreePath, trimmed);
|
|
237
|
+
const relative = path.relative(worktreePath, resolved);
|
|
238
|
+
if (!relative || relative === ".") {
|
|
239
|
+
throw new Error(`synthetic path cannot target the worktree root: ${rawPath}`);
|
|
240
|
+
}
|
|
241
|
+
if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
|
|
242
|
+
throw new Error(`synthetic path escapes the worktree root: ${rawPath}`);
|
|
243
|
+
}
|
|
244
|
+
return path.normalize(relative);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function hasTrackedEntries(worktreePath: string, relativePath: string): boolean {
|
|
248
|
+
const result = runGit(worktreePath, ["ls-files", "--", relativePath]);
|
|
249
|
+
return result.status === 0 && result.stdout.trim().length > 0;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function parseWorktreeSetupHookOutput(rawStdout: string): WorktreeSetupHookOutput {
|
|
253
|
+
const trimmed = rawStdout.trim();
|
|
254
|
+
if (!trimmed) {
|
|
255
|
+
throw new Error("worktree setup hook returned empty stdout; expected JSON object");
|
|
256
|
+
}
|
|
257
|
+
let parsed: unknown;
|
|
258
|
+
try {
|
|
259
|
+
parsed = JSON.parse(trimmed);
|
|
260
|
+
} catch (error) {
|
|
261
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
262
|
+
throw new Error(`worktree setup hook returned invalid JSON: ${message}`);
|
|
263
|
+
}
|
|
264
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
265
|
+
throw new Error("worktree setup hook stdout must be a JSON object");
|
|
266
|
+
}
|
|
267
|
+
return parsed as WorktreeSetupHookOutput;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function runWorktreeSetupHook(
|
|
271
|
+
hook: ResolvedWorktreeSetupHook,
|
|
272
|
+
input: WorktreeSetupHookInput,
|
|
273
|
+
): string[] {
|
|
274
|
+
const result = spawnSync(hook.hookPath, [], {
|
|
275
|
+
cwd: input.worktreePath,
|
|
276
|
+
encoding: "utf-8",
|
|
277
|
+
input: JSON.stringify(input),
|
|
278
|
+
timeout: hook.timeoutMs,
|
|
279
|
+
shell: false,
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
if (result.error) {
|
|
283
|
+
const code = "code" in result.error ? result.error.code : undefined;
|
|
284
|
+
if (code === "ETIMEDOUT") {
|
|
285
|
+
throw new Error(`worktree setup hook timed out after ${hook.timeoutMs}ms`);
|
|
286
|
+
}
|
|
287
|
+
throw new Error(`worktree setup hook failed: ${result.error.message}`);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (result.status !== 0) {
|
|
291
|
+
const details = result.stderr.trim() || result.stdout.trim() || "no output";
|
|
292
|
+
throw new Error(`worktree setup hook failed with exit code ${result.status}: ${details}`);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const output = parseWorktreeSetupHookOutput(result.stdout);
|
|
296
|
+
if (output.syntheticPaths === undefined) return [];
|
|
297
|
+
if (!Array.isArray(output.syntheticPaths)) {
|
|
298
|
+
throw new Error("worktree setup hook output field 'syntheticPaths' must be an array of relative paths");
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const uniquePaths = new Set<string>();
|
|
302
|
+
for (const candidate of output.syntheticPaths) {
|
|
303
|
+
if (typeof candidate !== "string") {
|
|
304
|
+
throw new Error("worktree setup hook output field 'syntheticPaths' must contain only strings");
|
|
305
|
+
}
|
|
306
|
+
const normalizedPath = normalizeSyntheticPath(input.worktreePath, candidate);
|
|
307
|
+
if (hasTrackedEntries(input.worktreePath, normalizedPath)) {
|
|
308
|
+
throw new Error(`worktree setup hook cannot mark tracked paths as synthetic: ${normalizedPath}`);
|
|
309
|
+
}
|
|
310
|
+
uniquePaths.add(normalizedPath);
|
|
311
|
+
}
|
|
312
|
+
return [...uniquePaths];
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function createSingleWorktree(
|
|
316
|
+
toplevel: string,
|
|
317
|
+
cwdRelative: string,
|
|
318
|
+
runId: string,
|
|
319
|
+
index: number,
|
|
320
|
+
baseCommit: string,
|
|
321
|
+
setupHook: ResolvedWorktreeSetupHook | undefined,
|
|
322
|
+
agent: string | undefined,
|
|
323
|
+
): WorktreeInfo {
|
|
324
|
+
const branch = buildWorktreeBranch(runId, index);
|
|
325
|
+
const worktreePath = buildWorktreePath(runId, index);
|
|
326
|
+
const add = runGit(toplevel, ["worktree", "add", worktreePath, "-b", branch, "HEAD"]);
|
|
327
|
+
if (add.status !== 0) {
|
|
328
|
+
const message = add.stderr.trim() || add.stdout.trim() || `failed to create worktree ${worktreePath}`;
|
|
329
|
+
throw new Error(message);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const agentCwd = cwdRelative ? path.join(worktreePath, cwdRelative) : worktreePath;
|
|
333
|
+
try {
|
|
334
|
+
const nodeModulesLinked = linkNodeModulesIfPresent(toplevel, worktreePath);
|
|
335
|
+
const syntheticPaths = nodeModulesLinked ? ["node_modules"] : [];
|
|
336
|
+
|
|
337
|
+
if (setupHook) {
|
|
338
|
+
const hookSyntheticPaths = runWorktreeSetupHook(setupHook, {
|
|
339
|
+
version: 1,
|
|
340
|
+
repoRoot: toplevel,
|
|
341
|
+
worktreePath,
|
|
342
|
+
agentCwd,
|
|
343
|
+
branch,
|
|
344
|
+
index,
|
|
345
|
+
runId,
|
|
346
|
+
baseCommit,
|
|
347
|
+
agent,
|
|
348
|
+
});
|
|
349
|
+
syntheticPaths.push(...hookSyntheticPaths);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return {
|
|
353
|
+
path: worktreePath,
|
|
354
|
+
agentCwd,
|
|
355
|
+
branch,
|
|
356
|
+
index,
|
|
357
|
+
nodeModulesLinked,
|
|
358
|
+
syntheticPaths,
|
|
359
|
+
};
|
|
360
|
+
} catch (error) {
|
|
361
|
+
try { runGitChecked(toplevel, ["worktree", "remove", "--force", worktreePath]); } catch {
|
|
362
|
+
// Best-effort rollback; preserve the original setup failure.
|
|
363
|
+
}
|
|
364
|
+
try { runGitChecked(toplevel, ["branch", "-D", branch]); } catch {
|
|
365
|
+
// Best-effort rollback; preserve the original setup failure.
|
|
366
|
+
}
|
|
367
|
+
throw error;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function removeSyntheticPath(worktree: WorktreeInfo, syntheticPath: string): void {
|
|
372
|
+
const resolved = path.resolve(worktree.path, syntheticPath);
|
|
373
|
+
const relative = path.relative(worktree.path, resolved);
|
|
374
|
+
if (!relative || relative === "." || relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
let stat: fs.Stats;
|
|
379
|
+
try {
|
|
380
|
+
stat = fs.lstatSync(resolved);
|
|
381
|
+
} catch (error) {
|
|
382
|
+
const code = error && typeof error === "object" && "code" in error ? (error as { code?: unknown }).code : undefined;
|
|
383
|
+
if (code === "ENOENT") return;
|
|
384
|
+
throw error;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (stat.isSymbolicLink()) {
|
|
388
|
+
fs.unlinkSync(resolved);
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
if (stat.isDirectory()) {
|
|
392
|
+
fs.rmSync(resolved, { recursive: true, force: true });
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
fs.rmSync(resolved, { force: true });
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function removeSyntheticPathsBeforeDiff(worktree: WorktreeInfo): void {
|
|
399
|
+
if (worktree.syntheticPaths.length === 0) return;
|
|
400
|
+
const seen = new Set<string>();
|
|
401
|
+
for (const syntheticPath of worktree.syntheticPaths) {
|
|
402
|
+
if (seen.has(syntheticPath)) continue;
|
|
403
|
+
seen.add(syntheticPath);
|
|
404
|
+
removeSyntheticPath(worktree, syntheticPath);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function emptyDiff(index: number, agent: string, branch: string, patchPath: string): WorktreeDiff {
|
|
409
|
+
return {
|
|
410
|
+
index,
|
|
411
|
+
agent,
|
|
412
|
+
branch,
|
|
413
|
+
diffStat: "",
|
|
414
|
+
filesChanged: 0,
|
|
415
|
+
insertions: 0,
|
|
416
|
+
deletions: 0,
|
|
417
|
+
patchPath,
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function parseNumstat(numstat: string): { filesChanged: number; insertions: number; deletions: number } {
|
|
422
|
+
const lines = numstat
|
|
423
|
+
.split("\n")
|
|
424
|
+
.map((line) => line.trim())
|
|
425
|
+
.filter(Boolean);
|
|
426
|
+
let filesChanged = 0;
|
|
427
|
+
let insertions = 0;
|
|
428
|
+
let deletions = 0;
|
|
429
|
+
|
|
430
|
+
for (const line of lines) {
|
|
431
|
+
const [rawInsertions, rawDeletions] = line.split("\t");
|
|
432
|
+
if (rawInsertions === undefined || rawDeletions === undefined) continue;
|
|
433
|
+
filesChanged++;
|
|
434
|
+
if (/^\d+$/.test(rawInsertions)) insertions += parseInt(rawInsertions, 10);
|
|
435
|
+
if (/^\d+$/.test(rawDeletions)) deletions += parseInt(rawDeletions, 10);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return { filesChanged, insertions, deletions };
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function captureWorktreeDiff(
|
|
442
|
+
setup: WorktreeSetup,
|
|
443
|
+
worktree: WorktreeInfo,
|
|
444
|
+
agent: string,
|
|
445
|
+
patchPath: string,
|
|
446
|
+
): WorktreeDiff {
|
|
447
|
+
removeSyntheticPathsBeforeDiff(worktree);
|
|
448
|
+
runGitChecked(worktree.path, ["add", "-A"]);
|
|
449
|
+
const diffStat = runGitChecked(worktree.path, ["diff", "--cached", "--stat", setup.baseCommit]).trim();
|
|
450
|
+
const patch = runGitChecked(worktree.path, ["diff", "--cached", setup.baseCommit]);
|
|
451
|
+
const numstat = runGitChecked(worktree.path, ["diff", "--cached", "--numstat", setup.baseCommit]);
|
|
452
|
+
fs.writeFileSync(patchPath, patch, "utf-8");
|
|
453
|
+
|
|
454
|
+
if (!patch.trim()) {
|
|
455
|
+
return emptyDiff(worktree.index, agent, worktree.branch, patchPath);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const parsed = parseNumstat(numstat);
|
|
459
|
+
return {
|
|
460
|
+
index: worktree.index,
|
|
461
|
+
agent,
|
|
462
|
+
branch: worktree.branch,
|
|
463
|
+
diffStat,
|
|
464
|
+
filesChanged: parsed.filesChanged,
|
|
465
|
+
insertions: parsed.insertions,
|
|
466
|
+
deletions: parsed.deletions,
|
|
467
|
+
patchPath,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function writeEmptyPatch(patchPath: string): void {
|
|
472
|
+
try {
|
|
473
|
+
fs.writeFileSync(patchPath, "", "utf-8");
|
|
474
|
+
} catch {
|
|
475
|
+
// Diff artifact writing is best-effort in error paths.
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
function cleanupSingleWorktree(repoCwd: string, worktree: WorktreeInfo): void {
|
|
480
|
+
try { runGitChecked(repoCwd, ["worktree", "remove", "--force", worktree.path]); } catch {
|
|
481
|
+
// Cleanup is best-effort to avoid masking caller errors.
|
|
482
|
+
}
|
|
483
|
+
try { runGitChecked(repoCwd, ["branch", "-D", worktree.branch]); } catch {
|
|
484
|
+
// Cleanup is best-effort to avoid masking caller errors.
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function hasWorktreeChanges(diff: WorktreeDiff): boolean {
|
|
489
|
+
return diff.filesChanged > 0 || diff.insertions > 0 || diff.deletions > 0 || diff.diffStat.trim().length > 0;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
export function createWorktrees(cwd: string, runId: string, count: number, options?: CreateWorktreesOptions): WorktreeSetup {
|
|
493
|
+
const repo = resolveRepoState(cwd);
|
|
494
|
+
const setupHook = resolveWorktreeSetupHook(repo.toplevel, options?.setupHook);
|
|
495
|
+
const worktrees: WorktreeInfo[] = [];
|
|
496
|
+
|
|
497
|
+
try {
|
|
498
|
+
for (let index = 0; index < count; index++) {
|
|
499
|
+
worktrees.push(createSingleWorktree(
|
|
500
|
+
repo.toplevel,
|
|
501
|
+
repo.cwdRelative,
|
|
502
|
+
runId,
|
|
503
|
+
index,
|
|
504
|
+
repo.baseCommit,
|
|
505
|
+
setupHook,
|
|
506
|
+
options?.agents?.[index],
|
|
507
|
+
));
|
|
508
|
+
}
|
|
509
|
+
} catch (error) {
|
|
510
|
+
cleanupWorktrees({
|
|
511
|
+
cwd: repo.toplevel,
|
|
512
|
+
worktrees,
|
|
513
|
+
baseCommit: repo.baseCommit,
|
|
514
|
+
});
|
|
515
|
+
throw error;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
return {
|
|
519
|
+
cwd: repo.toplevel,
|
|
520
|
+
worktrees,
|
|
521
|
+
baseCommit: repo.baseCommit,
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
export function diffWorktrees(setup: WorktreeSetup, agents: string[], diffsDir: string): WorktreeDiff[] {
|
|
526
|
+
try {
|
|
527
|
+
fs.mkdirSync(diffsDir, { recursive: true });
|
|
528
|
+
} catch {
|
|
529
|
+
// Returning no diffs is safer than failing the whole command on artifact-dir issues.
|
|
530
|
+
return [];
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
const diffs: WorktreeDiff[] = [];
|
|
534
|
+
for (let index = 0; index < setup.worktrees.length; index++) {
|
|
535
|
+
const worktree = setup.worktrees[index]!;
|
|
536
|
+
const agent = agents[index] ?? `task-${index + 1}`;
|
|
537
|
+
const patchPath = path.join(diffsDir, `task-${index}-${safePatchAgentName(agent)}.patch`);
|
|
538
|
+
try {
|
|
539
|
+
diffs.push(captureWorktreeDiff(setup, worktree, agent, patchPath));
|
|
540
|
+
} catch {
|
|
541
|
+
// Preserve execution flow; failed diff capture maps to an empty per-task patch.
|
|
542
|
+
writeEmptyPatch(patchPath);
|
|
543
|
+
diffs.push(emptyDiff(index, agent, worktree.branch, patchPath));
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
return diffs;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
export function cleanupWorktrees(setup: WorktreeSetup): void {
|
|
551
|
+
for (let index = setup.worktrees.length - 1; index >= 0; index--) {
|
|
552
|
+
cleanupSingleWorktree(setup.cwd, setup.worktrees[index]!);
|
|
553
|
+
}
|
|
554
|
+
try { runGitChecked(setup.cwd, ["worktree", "prune"]); } catch {
|
|
555
|
+
// Pruning is best-effort cleanup.
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
export function formatWorktreeDiffSummary(diffs: WorktreeDiff[]): string {
|
|
560
|
+
const changed = diffs.filter(hasWorktreeChanges);
|
|
561
|
+
if (changed.length === 0) return "";
|
|
562
|
+
|
|
563
|
+
const lines: string[] = ["=== Worktree Changes ===", ""];
|
|
564
|
+
for (const diff of changed) {
|
|
565
|
+
lines.push(
|
|
566
|
+
`--- Task ${diff.index + 1} (${diff.agent}): ${diff.filesChanged} files changed, +${diff.insertions} -${diff.deletions} ---`,
|
|
567
|
+
);
|
|
568
|
+
if (diff.diffStat.trim().length > 0) {
|
|
569
|
+
lines.push(diff.diffStat);
|
|
570
|
+
}
|
|
571
|
+
lines.push("");
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const patchesDir = path.dirname(changed[0]!.patchPath);
|
|
575
|
+
lines.push(`Full patches: ${patchesDir}`);
|
|
576
|
+
return lines.join("\n").trimEnd();
|
|
577
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { TEMP_ARTIFACTS_DIR, type ArtifactPaths } from "./types.ts";
|
|
4
|
+
import { getAgentDir } from "./utils.ts";
|
|
5
|
+
const CLEANUP_MARKER_FILE = ".last-cleanup";
|
|
6
|
+
|
|
7
|
+
export function getArtifactsDir(sessionFile: string | null): string {
|
|
8
|
+
if (sessionFile) {
|
|
9
|
+
const sessionDir = path.dirname(sessionFile);
|
|
10
|
+
return path.join(sessionDir, "subagent-artifacts");
|
|
11
|
+
}
|
|
12
|
+
return TEMP_ARTIFACTS_DIR;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function getArtifactPaths(artifactsDir: string, runId: string, agent: string, index?: number): ArtifactPaths {
|
|
16
|
+
const suffix = index !== undefined ? `_${index}` : "";
|
|
17
|
+
const safeAgent = agent.replace(/[^\w.-]/g, "_");
|
|
18
|
+
const base = `${runId}_${safeAgent}${suffix}`;
|
|
19
|
+
return {
|
|
20
|
+
inputPath: path.join(artifactsDir, `${base}_input.md`),
|
|
21
|
+
outputPath: path.join(artifactsDir, `${base}_output.md`),
|
|
22
|
+
jsonlPath: path.join(artifactsDir, `${base}.jsonl`),
|
|
23
|
+
metadataPath: path.join(artifactsDir, `${base}_meta.json`),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function ensureArtifactsDir(dir: string): void {
|
|
28
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function writeArtifact(filePath: string, content: string): void {
|
|
32
|
+
fs.writeFileSync(filePath, content, "utf-8");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function writeMetadata(filePath: string, metadata: object): void {
|
|
36
|
+
fs.writeFileSync(filePath, JSON.stringify(metadata, null, 2), "utf-8");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function appendJsonl(filePath: string, line: string): void {
|
|
40
|
+
fs.appendFileSync(filePath, `${line}\n`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function cleanupOldArtifacts(dir: string, maxAgeDays: number): void {
|
|
44
|
+
if (!fs.existsSync(dir)) return;
|
|
45
|
+
|
|
46
|
+
const markerPath = path.join(dir, CLEANUP_MARKER_FILE);
|
|
47
|
+
const now = Date.now();
|
|
48
|
+
|
|
49
|
+
if (fs.existsSync(markerPath)) {
|
|
50
|
+
const stat = fs.statSync(markerPath);
|
|
51
|
+
if (now - stat.mtimeMs < 24 * 60 * 60 * 1000) return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const maxAgeMs = maxAgeDays * 24 * 60 * 60 * 1000;
|
|
55
|
+
const cutoff = now - maxAgeMs;
|
|
56
|
+
|
|
57
|
+
for (const file of fs.readdirSync(dir)) {
|
|
58
|
+
if (file === CLEANUP_MARKER_FILE) continue;
|
|
59
|
+
const filePath = path.join(dir, file);
|
|
60
|
+
try {
|
|
61
|
+
const stat = fs.statSync(filePath);
|
|
62
|
+
if (stat.mtimeMs < cutoff) {
|
|
63
|
+
fs.unlinkSync(filePath);
|
|
64
|
+
}
|
|
65
|
+
} catch {
|
|
66
|
+
// Artifact cleanup is best-effort housekeeping. Skip files that disappear
|
|
67
|
+
// or become unreadable while scanning so one bad entry does not block the rest.
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
fs.writeFileSync(markerPath, String(now));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function cleanupAllArtifactDirs(maxAgeDays: number): void {
|
|
75
|
+
cleanupOldArtifacts(TEMP_ARTIFACTS_DIR, maxAgeDays);
|
|
76
|
+
|
|
77
|
+
const sessionsBase = path.join(getAgentDir(), "sessions");
|
|
78
|
+
if (!fs.existsSync(sessionsBase)) return;
|
|
79
|
+
|
|
80
|
+
let dirs: string[];
|
|
81
|
+
try {
|
|
82
|
+
dirs = fs.readdirSync(sessionsBase);
|
|
83
|
+
} catch {
|
|
84
|
+
// Session artifact cleanup is best-effort. If the sessions root cannot be read,
|
|
85
|
+
// skip cleanup instead of failing extension startup.
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
for (const dir of dirs) {
|
|
90
|
+
const artifactsDir = path.join(sessionsBase, dir, "subagent-artifacts");
|
|
91
|
+
try {
|
|
92
|
+
cleanupOldArtifacts(artifactsDir, maxAgeDays);
|
|
93
|
+
} catch {
|
|
94
|
+
// Session cleanup is best-effort. Keep going so one unreadable session dir
|
|
95
|
+
// does not block cleanup for the rest.
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
|
|
4
|
+
export function writeAtomicJson(filePath: string, payload: object): void {
|
|
5
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
6
|
+
const tempPath = path.join(
|
|
7
|
+
path.dirname(filePath),
|
|
8
|
+
`.${path.basename(filePath)}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`,
|
|
9
|
+
);
|
|
10
|
+
try {
|
|
11
|
+
fs.writeFileSync(tempPath, JSON.stringify(payload, null, 2), "utf-8");
|
|
12
|
+
fs.renameSync(tempPath, filePath);
|
|
13
|
+
} finally {
|
|
14
|
+
fs.rmSync(tempPath, { force: true });
|
|
15
|
+
}
|
|
16
|
+
}
|