@tintinweb/pi-subagents 0.4.1 → 0.4.4
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 +61 -0
- package/README.md +85 -1
- package/package.json +1 -1
- package/src/agent-manager.ts +74 -9
- package/src/agent-runner.ts +71 -15
- package/src/agent-types.ts +26 -0
- package/src/custom-agents.ts +34 -5
- package/src/index.ts +86 -3
- package/src/memory.ts +165 -0
- package/src/prompts.ts +24 -2
- package/src/skill-loader.ts +79 -0
- package/src/types.ts +18 -0
- package/src/worktree.ts +162 -0
package/src/worktree.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* worktree.ts — Git worktree isolation for agents.
|
|
3
|
+
*
|
|
4
|
+
* Creates a temporary git worktree so the agent works on an isolated copy of the repo.
|
|
5
|
+
* On completion, if no changes were made, the worktree is cleaned up.
|
|
6
|
+
* If changes exist, a branch is created and returned in the result.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { execFileSync } from "node:child_process";
|
|
10
|
+
import { existsSync } from "node:fs";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { randomUUID } from "node:crypto";
|
|
14
|
+
|
|
15
|
+
export interface WorktreeInfo {
|
|
16
|
+
/** Absolute path to the worktree directory. */
|
|
17
|
+
path: string;
|
|
18
|
+
/** Branch name created for this worktree (if changes exist). */
|
|
19
|
+
branch: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface WorktreeCleanupResult {
|
|
23
|
+
/** Whether changes were found in the worktree. */
|
|
24
|
+
hasChanges: boolean;
|
|
25
|
+
/** Branch name if changes were committed. */
|
|
26
|
+
branch?: string;
|
|
27
|
+
/** Worktree path if it was kept. */
|
|
28
|
+
path?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Create a temporary git worktree for an agent.
|
|
33
|
+
* Returns the worktree path, or undefined if not in a git repo.
|
|
34
|
+
*/
|
|
35
|
+
export function createWorktree(cwd: string, agentId: string): WorktreeInfo | undefined {
|
|
36
|
+
// Verify we're in a git repo with at least one commit (HEAD must exist)
|
|
37
|
+
try {
|
|
38
|
+
execFileSync("git", ["rev-parse", "--is-inside-work-tree"], { cwd, stdio: "pipe", timeout: 5000 });
|
|
39
|
+
execFileSync("git", ["rev-parse", "HEAD"], { cwd, stdio: "pipe", timeout: 5000 });
|
|
40
|
+
} catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const branch = `pi-agent-${agentId}`;
|
|
45
|
+
const suffix = randomUUID().slice(0, 8);
|
|
46
|
+
const worktreePath = join(tmpdir(), `pi-agent-${agentId}-${suffix}`);
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
// Create detached worktree at HEAD
|
|
50
|
+
execFileSync("git", ["worktree", "add", "--detach", worktreePath, "HEAD"], {
|
|
51
|
+
cwd,
|
|
52
|
+
stdio: "pipe",
|
|
53
|
+
timeout: 30000,
|
|
54
|
+
});
|
|
55
|
+
return { path: worktreePath, branch };
|
|
56
|
+
} catch {
|
|
57
|
+
// If worktree creation fails, return undefined (agent runs in normal cwd)
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Clean up a worktree after agent completion.
|
|
64
|
+
* - If no changes: remove worktree entirely.
|
|
65
|
+
* - If changes exist: create a branch, commit changes, return branch info.
|
|
66
|
+
*/
|
|
67
|
+
export function cleanupWorktree(
|
|
68
|
+
cwd: string,
|
|
69
|
+
worktree: WorktreeInfo,
|
|
70
|
+
agentDescription: string,
|
|
71
|
+
): WorktreeCleanupResult {
|
|
72
|
+
if (!existsSync(worktree.path)) {
|
|
73
|
+
return { hasChanges: false };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
// Check for uncommitted changes in the worktree
|
|
78
|
+
const status = execFileSync("git", ["status", "--porcelain"], {
|
|
79
|
+
cwd: worktree.path,
|
|
80
|
+
stdio: "pipe",
|
|
81
|
+
timeout: 10000,
|
|
82
|
+
}).toString().trim();
|
|
83
|
+
|
|
84
|
+
if (!status) {
|
|
85
|
+
// No changes — remove worktree
|
|
86
|
+
removeWorktree(cwd, worktree.path);
|
|
87
|
+
return { hasChanges: false };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Changes exist — stage, commit, and create a branch
|
|
91
|
+
execFileSync("git", ["add", "-A"], { cwd: worktree.path, stdio: "pipe", timeout: 10000 });
|
|
92
|
+
// Truncate description for commit message (no shell sanitization needed — execFileSync uses argv)
|
|
93
|
+
const safeDesc = agentDescription.slice(0, 200);
|
|
94
|
+
const commitMsg = `pi-agent: ${safeDesc}`;
|
|
95
|
+
execFileSync("git", ["commit", "-m", commitMsg], {
|
|
96
|
+
cwd: worktree.path,
|
|
97
|
+
stdio: "pipe",
|
|
98
|
+
timeout: 10000,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Create a branch pointing to the worktree's HEAD.
|
|
102
|
+
// If the branch already exists, append a suffix to avoid overwriting previous work.
|
|
103
|
+
let branchName = worktree.branch;
|
|
104
|
+
try {
|
|
105
|
+
execFileSync("git", ["branch", branchName], {
|
|
106
|
+
cwd: worktree.path,
|
|
107
|
+
stdio: "pipe",
|
|
108
|
+
timeout: 5000,
|
|
109
|
+
});
|
|
110
|
+
} catch {
|
|
111
|
+
// Branch already exists — use a unique suffix
|
|
112
|
+
branchName = `${worktree.branch}-${Date.now()}`;
|
|
113
|
+
execFileSync("git", ["branch", branchName], {
|
|
114
|
+
cwd: worktree.path,
|
|
115
|
+
stdio: "pipe",
|
|
116
|
+
timeout: 5000,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
// Update branch name in worktree info for the caller
|
|
120
|
+
worktree.branch = branchName;
|
|
121
|
+
|
|
122
|
+
// Remove the worktree (branch persists in main repo)
|
|
123
|
+
removeWorktree(cwd, worktree.path);
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
hasChanges: true,
|
|
127
|
+
branch: worktree.branch,
|
|
128
|
+
path: worktree.path,
|
|
129
|
+
};
|
|
130
|
+
} catch {
|
|
131
|
+
// Best effort cleanup on error
|
|
132
|
+
try { removeWorktree(cwd, worktree.path); } catch { /* ignore */ }
|
|
133
|
+
return { hasChanges: false };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Force-remove a worktree.
|
|
139
|
+
*/
|
|
140
|
+
function removeWorktree(cwd: string, worktreePath: string): void {
|
|
141
|
+
try {
|
|
142
|
+
execFileSync("git", ["worktree", "remove", "--force", worktreePath], {
|
|
143
|
+
cwd,
|
|
144
|
+
stdio: "pipe",
|
|
145
|
+
timeout: 10000,
|
|
146
|
+
});
|
|
147
|
+
} catch {
|
|
148
|
+
// If git worktree remove fails, try pruning
|
|
149
|
+
try {
|
|
150
|
+
execFileSync("git", ["worktree", "prune"], { cwd, stdio: "pipe", timeout: 5000 });
|
|
151
|
+
} catch { /* ignore */ }
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Prune any orphaned worktrees (crash recovery).
|
|
157
|
+
*/
|
|
158
|
+
export function pruneWorktrees(cwd: string): void {
|
|
159
|
+
try {
|
|
160
|
+
execFileSync("git", ["worktree", "prune"], { cwd, stdio: "pipe", timeout: 5000 });
|
|
161
|
+
} catch { /* ignore */ }
|
|
162
|
+
}
|