pi-cowboy 0.0.0 → 0.3.2
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/LICENSE +21 -0
- package/README.md +281 -2
- package/agents/explore.toml +40 -0
- package/dist/agents/agent-assets.js +261 -0
- package/dist/agents/agent-cleanup.js +284 -0
- package/dist/agents/agent-discovery.js +108 -0
- package/dist/agents/agent-host.js +10 -0
- package/dist/agents/agent-manager.js +367 -0
- package/dist/agents/agent-reviver.js +64 -0
- package/dist/agents/agent-runner.js +288 -0
- package/dist/agents/agent-spawn-store.js +57 -0
- package/dist/agents/agent-template.js +46 -0
- package/dist/agents/agent-types.js +144 -0
- package/dist/agents/batch.js +92 -0
- package/dist/agents/cleanup-policy.js +194 -0
- package/dist/agents/cleanup-report.js +128 -0
- package/dist/agents/default-agents.js +19 -0
- package/dist/agents/harness/pi-bolt.js +15 -0
- package/dist/agents/harness/pi-compatible.js +68 -0
- package/dist/agents/harness/pig.js +16 -0
- package/dist/agents/harness/registry.js +27 -0
- package/dist/agents/harness.js +32 -0
- package/dist/agents/result-file-permissions.js +29 -0
- package/dist/agents/run-state.js +270 -0
- package/dist/agents/schemas/cleanup-batch.schema.js +19 -0
- package/dist/agents/schemas/cowboy-agents.schema.js +39 -0
- package/dist/agents/schemas/merge-batch.schema.js +25 -0
- package/dist/agents/schemas/steer-batch.schema.js +22 -0
- package/dist/agents/schemas/stop-batch.schema.js +18 -0
- package/dist/agents/spawn-defaults.js +70 -0
- package/dist/agents/subagent-session.js +601 -0
- package/dist/agents/tool-cleanup.js +57 -0
- package/dist/agents/tool-execution.js +125 -0
- package/dist/agents/tool-merge.js +190 -0
- package/dist/agents/tool-result.js +17 -0
- package/dist/agents/tool-steer.js +89 -0
- package/dist/agents/types.js +1 -0
- package/dist/availability.js +38 -0
- package/dist/config/config-io.js +261 -0
- package/dist/config/config-store.js +321 -0
- package/dist/config/config-validation.js +173 -0
- package/dist/config/layered-config.js +117 -0
- package/dist/config/types.js +23 -0
- package/dist/cow-support-launch.js +43 -0
- package/dist/events.js +146 -0
- package/dist/extension-toggle.js +28 -0
- package/dist/harness-availability-launch.js +23 -0
- package/dist/index.js +52 -0
- package/dist/infrastructure/exec-path.js +35 -0
- package/dist/infrastructure/git/cow-clone.js +398 -0
- package/dist/infrastructure/git/git-materializer.js +342 -0
- package/dist/infrastructure/git/git-merger.js +234 -0
- package/dist/infrastructure/git/git-retention.js +43 -0
- package/dist/infrastructure/git/git-runner.js +145 -0
- package/dist/infrastructure/git/git-worktree.js +143 -0
- package/dist/infrastructure/git/index.js +11 -0
- package/dist/infrastructure/git-client.js +11 -0
- package/dist/infrastructure/herdr/agent-stopper.js +60 -0
- package/dist/infrastructure/herdr/agents.js +329 -0
- package/dist/infrastructure/herdr/herdr-client.js +36 -0
- package/dist/infrastructure/herdr/herdr-transport.js +158 -0
- package/dist/infrastructure/herdr/index.js +15 -0
- package/dist/infrastructure/herdr/panes.js +174 -0
- package/dist/infrastructure/herdr/polling.js +44 -0
- package/dist/infrastructure/herdr/worktrees.js +76 -0
- package/dist/infrastructure/herdr-client.js +6 -0
- package/dist/infrastructure/herdr-host.js +163 -0
- package/dist/logger.js +79 -0
- package/dist/models/model-groups.js +72 -0
- package/dist/models/model-precedence.js +50 -0
- package/dist/models/model-request.js +108 -0
- package/dist/orchestrators/briefing-section.js +14 -0
- package/dist/orchestrators/context.js +116 -0
- package/dist/orchestrators/cues.js +90 -0
- package/dist/orchestrators/default-orchestrators.js +36 -0
- package/dist/orchestrators/orchestrator-discovery.js +68 -0
- package/dist/orchestrators/orchestrator-template.js +43 -0
- package/dist/orchestrators/orchestrator-types.js +78 -0
- package/dist/orchestrators/protocol.js +292 -0
- package/dist/orchestrators/types.js +7 -0
- package/dist/paths.js +66 -0
- package/dist/pi-settings.js +7 -0
- package/dist/predicates.js +25 -0
- package/dist/prompt/prompts.js +110 -0
- package/dist/prompt/skill-loader.js +153 -0
- package/dist/prompt/subagent-system-prompt.js +173 -0
- package/dist/registration.js +220 -0
- package/dist/shell.js +225 -0
- package/dist/spawn/cow-support.js +53 -0
- package/dist/spawn/herdr-launcher.js +80 -0
- package/dist/spawn/repo-lock.js +34 -0
- package/dist/spawn/sandbox.js +286 -0
- package/dist/spawn/spawn-coordinator.js +255 -0
- package/dist/spawn/spawn-id.js +39 -0
- package/dist/spawn/worktree-policy.js +137 -0
- package/dist/status-note.js +30 -0
- package/dist/subagent/ipc-protocol.js +68 -0
- package/dist/subagent/ipc.js +110 -0
- package/dist/subagent/pane-supervisor.js +24 -0
- package/dist/subagent/socket-ipc.js +240 -0
- package/dist/subagent/supervisor.js +315 -0
- package/dist/task-registry.js +258 -0
- package/dist/templates/template-files.js +84 -0
- package/dist/types.js +84 -0
- package/dist/ui/action-report.js +21 -0
- package/dist/ui/format.js +47 -0
- package/dist/ui/indicator.js +480 -0
- package/dist/ui/menu/helpers.js +227 -0
- package/dist/ui/menu/menu-agent-settings.js +182 -0
- package/dist/ui/menu/menu-agent-status.js +444 -0
- package/dist/ui/menu/menu-concurrency.js +226 -0
- package/dist/ui/menu/menu-model-settings.js +244 -0
- package/dist/ui/menu/menu-spawn-defaults.js +71 -0
- package/dist/ui/menu/menu-spawn-wizard.js +367 -0
- package/dist/ui/menu/menu-spawn-worktree.js +107 -0
- package/dist/ui/menu/menu-system-prompt.js +104 -0
- package/dist/ui/menu/menu-worktree-command.js +150 -0
- package/dist/ui/menu/menus.js +187 -0
- package/dist/ui/menu/model-picker.js +160 -0
- package/dist/ui/menu/screen-host.js +87 -0
- package/dist/ui/menu/submenus/confirm.js +23 -0
- package/dist/ui/menu/submenus/model-select.js +42 -0
- package/dist/ui/menu/submenus/name-field.js +43 -0
- package/dist/ui/menu/submenus/numeric-input.js +61 -0
- package/dist/ui/menu/submenus/target-select.js +129 -0
- package/dist/ui/menu/submenus/text-editor.js +49 -0
- package/dist/ui/menu/submenus/worktree-command-name.js +73 -0
- package/dist/ui/menu/submenus/worktree-name.js +53 -0
- package/dist/ui/menu/wrappers/settings-list.js +101 -0
- package/dist/ui/searchable-select.js +147 -0
- package/dist/ui/types.js +1 -0
- package/dist/utils.js +70 -0
- package/docs/assets/grazing.png +0 -0
- package/docs/assets/herdr-containers.png +0 -0
- package/docs/assets/logo-transparent.png +0 -0
- package/orchestrators/with-code-review.toml +20 -0
- package/package.json +109 -3
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-cleanup.ts — state-changing cleanup orchestration (policy lives in cleanup-policy.ts).
|
|
3
|
+
* createWorktreeTeardown: worktree/branch/pane removal over injected actuators.
|
|
4
|
+
* createCleanup: settled-or-recovered run cleanup over the assets seam.
|
|
5
|
+
* WorktreeTeardown/Cleanup: the two planes. WorktreeTeardownOutcome: removal result.
|
|
6
|
+
* WorktreeRemovalTarget: the address a teardown removes through.
|
|
7
|
+
* AgentCleanupRegistry: spawn lookup/drop surface.
|
|
8
|
+
* Report-gated (only ended runs are cleaned up; live runs are refused, never stopped);
|
|
9
|
+
* ordered: gate before removal, workspace before pane, branch only after confirmed checkout removal.
|
|
10
|
+
*/
|
|
11
|
+
import { hasOutcome } from "../types.js";
|
|
12
|
+
import { errorMessage } from "../utils.js";
|
|
13
|
+
import { ambiguousReport, assembleReport, assessRemovability, keptReport, paneDispositionFor, planRecovered, recordedSettlement, recoveryGate, recoveryRefusalReason, removalMechanismOf, shouldDropSpawn, } from "./cleanup-policy.js";
|
|
14
|
+
/* ── Addresses ─────────────────────────────────────────────────────────── */
|
|
15
|
+
/** Actuator address: memory's ref refreshed from herdr's live report; recovered runs rebuild it with `paneCreated` false; no pane address means no ref (git plane removes the checkout). */
|
|
16
|
+
function hostRefFor(located) {
|
|
17
|
+
const pane = located.pane;
|
|
18
|
+
const workspaceId = pane?.workspaceId ?? located.worktree?.workspaceId;
|
|
19
|
+
if (pane === null || workspaceId === undefined)
|
|
20
|
+
return undefined;
|
|
21
|
+
return {
|
|
22
|
+
engine: "herdr",
|
|
23
|
+
name: located.branch,
|
|
24
|
+
paneId: pane.paneId,
|
|
25
|
+
tabId: pane.tabId,
|
|
26
|
+
workspaceId,
|
|
27
|
+
paneCreated: located.paneCreated,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/* ── Removal ───────────────────────────────────────────────────────────── */
|
|
31
|
+
/** Close a self-created pane; a missing ref is incoherent (same address implies both), so it throws. */
|
|
32
|
+
async function closeOwnedPane(deps, pane, ref) {
|
|
33
|
+
if (!ref)
|
|
34
|
+
throw new Error(`cleanup has a pane (${pane.paneId}) with no host address`);
|
|
35
|
+
await deps.closePane(ref);
|
|
36
|
+
return { kind: "closed", paneId: pane.paneId };
|
|
37
|
+
}
|
|
38
|
+
/** Remove the extension-owned worktree (failures reported, not hidden); a missing ref/workspace means never adopted, so the git fallback removes it. */
|
|
39
|
+
async function removeOwnedTree(deps, worktree, ref) {
|
|
40
|
+
const viaHost = ref !== undefined && worktree.workspaceId !== undefined;
|
|
41
|
+
const removed = viaHost
|
|
42
|
+
? await deps.removeWorktree(ref)
|
|
43
|
+
: await deps.removeGitWorktree(worktree.worktreePath, worktree.repoCwd);
|
|
44
|
+
if (!removed) {
|
|
45
|
+
return {
|
|
46
|
+
kind: "removal-failed",
|
|
47
|
+
path: worktree.worktreePath,
|
|
48
|
+
detail: viaHost
|
|
49
|
+
? "herdr did not confirm the worktree removal"
|
|
50
|
+
: "git did not confirm the worktree removal",
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
return { kind: "removed", path: worktree.worktreePath };
|
|
54
|
+
}
|
|
55
|
+
/** Pane outcome for a removal: a removed tree takes its pane with it; a surviving tree still hosts its pane, which is never ours to close. */
|
|
56
|
+
async function paneOutcomeFor(deps, pane, mechanism, removed, ref) {
|
|
57
|
+
if (pane === null)
|
|
58
|
+
return { kind: "none" };
|
|
59
|
+
const disposition = paneDispositionFor(pane, mechanism, removed);
|
|
60
|
+
switch (disposition.kind) {
|
|
61
|
+
case "none":
|
|
62
|
+
return { kind: "none" };
|
|
63
|
+
case "closed-with-workspace":
|
|
64
|
+
return { kind: "closed", paneId: pane.paneId };
|
|
65
|
+
case "close":
|
|
66
|
+
return closeOwnedPane(deps, pane, ref);
|
|
67
|
+
case "leave-open":
|
|
68
|
+
return { kind: "open", paneId: pane.paneId };
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Pane outcome for a checkout proven absent: the settlement verdict never
|
|
73
|
+
* reads the pane on the tracked path, so read it here and state only what
|
|
74
|
+
* was observed. A live pane stays open; a gone address is reported gone
|
|
75
|
+
* (never closed — the extension did not close it); an unreadable pane is
|
|
76
|
+
* reported unknown (never evidence either way, and the drop does not rest
|
|
77
|
+
* on it). No pane address means none.
|
|
78
|
+
*/
|
|
79
|
+
async function paneOutcomeForAbsentTree(assets, pane) {
|
|
80
|
+
if (pane === null)
|
|
81
|
+
return { kind: "none" };
|
|
82
|
+
let observation;
|
|
83
|
+
try {
|
|
84
|
+
observation = await assets.observe(pane);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return { kind: "unknown", paneId: pane.paneId };
|
|
88
|
+
}
|
|
89
|
+
return observation === undefined
|
|
90
|
+
? { kind: "gone", paneId: pane.paneId }
|
|
91
|
+
: { kind: "open", paneId: pane.paneId };
|
|
92
|
+
}
|
|
93
|
+
async function removeResources(deps, plan, ref) {
|
|
94
|
+
if (plan.kind === "no-worktree") {
|
|
95
|
+
return {
|
|
96
|
+
pane: await paneOutcomeFor(deps, plan.pane, null, true, ref),
|
|
97
|
+
worktree: { kind: "none" },
|
|
98
|
+
branch: { kind: "not-applicable" },
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
const { worktree, pane } = plan;
|
|
102
|
+
const outcome = await removeOwnedTree(deps, worktree, ref);
|
|
103
|
+
if (outcome.kind === "removed") {
|
|
104
|
+
return {
|
|
105
|
+
pane: await paneOutcomeFor(deps, pane, removalMechanismOf(worktree), true, ref),
|
|
106
|
+
worktree: outcome,
|
|
107
|
+
branch: await deps.deleteBranch({
|
|
108
|
+
branch: worktree.branchName,
|
|
109
|
+
worktreePath: worktree.worktreePath,
|
|
110
|
+
repoCwd: worktree.repoCwd,
|
|
111
|
+
}),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
pane: await paneOutcomeFor(deps, pane, removalMechanismOf(worktree), false, ref),
|
|
116
|
+
worktree: outcome,
|
|
117
|
+
branch: { kind: "not-applicable" },
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/** Bind the teardown plane over its actuators; an absent tree reports before any gate or removal runs. */
|
|
121
|
+
export function createWorktreeTeardown(deps) {
|
|
122
|
+
return {
|
|
123
|
+
async removeWorktree(target, opts) {
|
|
124
|
+
const worktree = target.worktree;
|
|
125
|
+
const pane = target.pane;
|
|
126
|
+
const ref = target.kind === "detached" ? undefined : target.ref;
|
|
127
|
+
try {
|
|
128
|
+
// Applies to herdr and git targets alike: a rolled-back create has neither checkout nor workspace.
|
|
129
|
+
if (!(await deps.worktreeExists(worktree.worktreePath))) {
|
|
130
|
+
return { kind: "absent", path: worktree.worktreePath };
|
|
131
|
+
}
|
|
132
|
+
const before = await assessRemovability(deps.isWorktreeDirty, opts, worktree);
|
|
133
|
+
if (before.kind === "kept") {
|
|
134
|
+
return {
|
|
135
|
+
kind: "kept",
|
|
136
|
+
path: worktree.worktreePath,
|
|
137
|
+
reason: before.reason,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
const outcome = await removeOwnedTree(deps, worktree, ref);
|
|
141
|
+
if (outcome.kind === "removal-failed")
|
|
142
|
+
return outcome;
|
|
143
|
+
const mechanism = removalMechanismOf(worktree);
|
|
144
|
+
// Pane closes before branch cleanup so a git-owned pane only closes after confirmed checkout removal.
|
|
145
|
+
await paneOutcomeFor(deps, pane, mechanism, true, ref);
|
|
146
|
+
const branch = await deps.deleteBranch({
|
|
147
|
+
branch: worktree.branchName,
|
|
148
|
+
worktreePath: worktree.worktreePath,
|
|
149
|
+
repoCwd: worktree.repoCwd,
|
|
150
|
+
});
|
|
151
|
+
return {
|
|
152
|
+
kind: "removed",
|
|
153
|
+
path: worktree.worktreePath,
|
|
154
|
+
branchName: worktree.branchName,
|
|
155
|
+
via: mechanism,
|
|
156
|
+
branch,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
return {
|
|
161
|
+
kind: "removal-failed",
|
|
162
|
+
path: worktree.worktreePath,
|
|
163
|
+
detail: errorMessage(err),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/** Bind state-changing cleanup over the assets seam (the composition all callers use). */
|
|
170
|
+
export function createCleanup(assets) {
|
|
171
|
+
const teardown = createWorktreeTeardown(assets);
|
|
172
|
+
/** Recovered-path verdict carrying the unread deliverable. */
|
|
173
|
+
function recovered(agentId, basis) {
|
|
174
|
+
return {
|
|
175
|
+
kind: "discovered",
|
|
176
|
+
basis,
|
|
177
|
+
deliverable: assets.deliverable(agentId),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
async function verdictFor(agentId, located, hint, opts) {
|
|
181
|
+
// Memory is the only settlement witness; a live run is refused outright (no probe, no stop).
|
|
182
|
+
if (hint !== undefined) {
|
|
183
|
+
const settlement = recordedSettlement(hint);
|
|
184
|
+
return hasOutcome(hint.lifecycle)
|
|
185
|
+
? { kind: "torn-down", settlement }
|
|
186
|
+
: {
|
|
187
|
+
kind: "refused",
|
|
188
|
+
settlement,
|
|
189
|
+
reason: { kind: "agent-active", phase: hint.lifecycle.phase },
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
// No recorded end: force overrides without reading; otherwise herdr's live read decides (unreadable refuses — never evidence the run is over).
|
|
193
|
+
if (opts?.force === true) {
|
|
194
|
+
return {
|
|
195
|
+
kind: "torn-down",
|
|
196
|
+
settlement: recovered(agentId, { kind: "forced" }),
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
let observation;
|
|
200
|
+
if (located.pane !== null) {
|
|
201
|
+
try {
|
|
202
|
+
observation = await assets.observe(located.pane);
|
|
203
|
+
}
|
|
204
|
+
catch (err) {
|
|
205
|
+
const basis = {
|
|
206
|
+
kind: "pane-unreadable",
|
|
207
|
+
detail: errorMessage(err),
|
|
208
|
+
};
|
|
209
|
+
return {
|
|
210
|
+
kind: "refused",
|
|
211
|
+
settlement: recovered(agentId, basis),
|
|
212
|
+
reason: recoveryRefusalReason(basis),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const gate = recoveryGate(observation);
|
|
217
|
+
return gate.kind === "allow"
|
|
218
|
+
? { kind: "torn-down", settlement: recovered(agentId, gate.basis) }
|
|
219
|
+
: {
|
|
220
|
+
kind: "refused",
|
|
221
|
+
settlement: recovered(agentId, gate.basis),
|
|
222
|
+
reason: recoveryRefusalReason(gate.basis),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
/** Clean up one ended run end-to-end (best effort); a refusal removes nothing — no tree, branch, pane, or stop. */
|
|
226
|
+
async function cleanupAgent(agentId, registry, opts) {
|
|
227
|
+
const hint = registry.getSpawn(agentId);
|
|
228
|
+
const located = await assets.locate(agentId, hint);
|
|
229
|
+
if (located.kind === "not-found") {
|
|
230
|
+
throw new Error(`Agent ${agentId} not found.`);
|
|
231
|
+
}
|
|
232
|
+
if (located.kind === "ambiguous") {
|
|
233
|
+
// Contested id: cleanup arbitrates nothing, the orchestrator decides by hand.
|
|
234
|
+
return ambiguousReport(agentId, hint === undefined ? "recovered" : "tracked", hint === undefined
|
|
235
|
+
? recovered(agentId, { kind: "artifacts-ambiguous" })
|
|
236
|
+
: recordedSettlement(hint), located.candidates.map((candidate) => candidate.worktreePath));
|
|
237
|
+
}
|
|
238
|
+
const plan = planRecovered(located);
|
|
239
|
+
const verdict = await verdictFor(agentId, located, hint, opts);
|
|
240
|
+
if (verdict.kind === "refused") {
|
|
241
|
+
return keptReport(agentId, located.source, plan, verdict.settlement, verdict.reason, "open");
|
|
242
|
+
}
|
|
243
|
+
if (plan.kind === "worktree-unlocatable") {
|
|
244
|
+
// An unlocatable tree whose path is absent from disk is already gone:
|
|
245
|
+
// absence is established from the path alone (no repo cwd can address
|
|
246
|
+
// a removal or a dirty probe at a directory that is not there), and
|
|
247
|
+
// the branch stays — only a confirmed checkout removal earns a delete.
|
|
248
|
+
if (plan.worktreePath !== null &&
|
|
249
|
+
!(await assets.worktreeExists(plan.worktreePath))) {
|
|
250
|
+
const report = {
|
|
251
|
+
agentId,
|
|
252
|
+
source: located.source,
|
|
253
|
+
outcome: { kind: "torn-down" },
|
|
254
|
+
branchName: plan.branchName,
|
|
255
|
+
settlement: verdict.settlement,
|
|
256
|
+
pane: await paneOutcomeForAbsentTree(assets, located.pane),
|
|
257
|
+
worktree: { kind: "absent", path: plan.worktreePath },
|
|
258
|
+
branch: { kind: "not-applicable" },
|
|
259
|
+
};
|
|
260
|
+
// A recovered run has no record to forget; drop only tracked spawns.
|
|
261
|
+
if (shouldDropSpawn(report) && hint !== undefined)
|
|
262
|
+
registry.dropSpawn(hint);
|
|
263
|
+
return report;
|
|
264
|
+
}
|
|
265
|
+
return keptReport(agentId, located.source, plan, verdict.settlement, {
|
|
266
|
+
kind: "unverifiable",
|
|
267
|
+
detail: "missing worktree path or repo cwd",
|
|
268
|
+
}, "open");
|
|
269
|
+
}
|
|
270
|
+
if (plan.kind === "worktree") {
|
|
271
|
+
const gate = await assessRemovability(assets.isWorktreeDirty, opts, plan.worktree);
|
|
272
|
+
if (gate.kind === "kept") {
|
|
273
|
+
return keptReport(agentId, located.source, plan, verdict.settlement, gate.reason, "open");
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const removal = await removeResources(assets, plan, hostRefFor(located));
|
|
277
|
+
const report = assembleReport(agentId, located.source, plan, verdict.settlement, removal);
|
|
278
|
+
// A recovered run has no record to forget; drop only tracked spawns.
|
|
279
|
+
if (shouldDropSpawn(report) && hint !== undefined)
|
|
280
|
+
registry.dropSpawn(hint);
|
|
281
|
+
return report;
|
|
282
|
+
}
|
|
283
|
+
return { ...teardown, cleanupAgent };
|
|
284
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-discovery.ts — Agent file discovery, parsing, and config merging.
|
|
3
|
+
*
|
|
4
|
+
* Scans user, shared, and project `*.toml` dirs through
|
|
5
|
+
* AgentTemplateSchema into AgentConfigs. Per-field precedence:
|
|
6
|
+
* default < user < shared < project.
|
|
7
|
+
*/
|
|
8
|
+
import { compactDefined, parseTemplateToml, scanTemplateFilesInDir, } from "../templates/template-files.js";
|
|
9
|
+
import { AgentTemplateSchema } from "./agent-template.js";
|
|
10
|
+
/** Shape failures throw; the scanner turns that into a one-line skip warning. */
|
|
11
|
+
export function parseAgentFile(content) {
|
|
12
|
+
return AgentTemplateSchema.parse(parseTemplateToml(content));
|
|
13
|
+
}
|
|
14
|
+
const AGENT_SCAN_WARNING = {
|
|
15
|
+
label: "Agent",
|
|
16
|
+
problem: "is not a valid agent template and was skipped",
|
|
17
|
+
};
|
|
18
|
+
/** Empty array when the directory doesn't exist. Stays shell-free: `notify` is the injected warning sink. */
|
|
19
|
+
export async function scanAgentFilesInDir(dirPath, notify) {
|
|
20
|
+
return scanTemplateFilesInDir(dirPath, parseAgentFile, (agent) => agent.name, notify, AGENT_SCAN_WARNING);
|
|
21
|
+
}
|
|
22
|
+
/** Per-field precedence: project > shared > user > extension > defaults. A set field wins; undefined falls through. */
|
|
23
|
+
export function mergeAgents(defaults, userAgents, sharedAgents, projectAgents) {
|
|
24
|
+
const result = new Map();
|
|
25
|
+
for (const [name, config] of defaults) {
|
|
26
|
+
result.set(name, { ...config });
|
|
27
|
+
}
|
|
28
|
+
mergeAgentOverrides(result, userAgents);
|
|
29
|
+
mergeAgentOverrides(result, sharedAgents);
|
|
30
|
+
mergeAgentOverrides(result, projectAgents);
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
/** Arbitrate the one runtime boundary that can see both tool filter arms (parsed .toml): whitelist (string[]) wins, else blacklist. */
|
|
34
|
+
function arbitrateFilters(config) {
|
|
35
|
+
const { tools, excludeTools, ...rest } = config;
|
|
36
|
+
const out = { ...rest };
|
|
37
|
+
if (tools !== undefined && excludeTools !== undefined) {
|
|
38
|
+
if (Array.isArray(tools))
|
|
39
|
+
out.tools = tools;
|
|
40
|
+
else
|
|
41
|
+
out.excludeTools = excludeTools;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
if (tools !== undefined)
|
|
45
|
+
out.tools = tools;
|
|
46
|
+
if (excludeTools !== undefined)
|
|
47
|
+
out.excludeTools = excludeTools;
|
|
48
|
+
}
|
|
49
|
+
// The arbitration above guarantees a single arm; the compiler cannot infer that.
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
function mergeAgentOverrides(result, agents) {
|
|
53
|
+
for (const template of agents) {
|
|
54
|
+
if (!template.name)
|
|
55
|
+
continue;
|
|
56
|
+
const existing = result.get(template.name);
|
|
57
|
+
if (existing) {
|
|
58
|
+
// display_name and description identify this file's agent, so a same-named
|
|
59
|
+
// override replaces them rather than inheriting an earlier layer's values.
|
|
60
|
+
const identity = {
|
|
61
|
+
displayName: template.display_name,
|
|
62
|
+
description: template.description ?? "",
|
|
63
|
+
};
|
|
64
|
+
result.set(template.name, arbitrateFilters({
|
|
65
|
+
...existing,
|
|
66
|
+
...fromTemplate(template),
|
|
67
|
+
...identity,
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
result.set(template.name, arbitrateFilters({ ...BASE_DEFAULTS, ...fromTemplate(template) }));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Only explicitly-set fields (undefined falls through). May carry both filter
|
|
77
|
+
* arms; arbitrated by the caller. An empty `inlined_skills` list is a set value,
|
|
78
|
+
* so a higher layer clears inlining with it.
|
|
79
|
+
*/
|
|
80
|
+
function fromTemplate(template) {
|
|
81
|
+
const { harness } = template;
|
|
82
|
+
const obj = {
|
|
83
|
+
name: template.name,
|
|
84
|
+
displayName: template.display_name,
|
|
85
|
+
description: template.description,
|
|
86
|
+
harnessType: template.harness_type,
|
|
87
|
+
tools: harness?.tools,
|
|
88
|
+
excludeTools: harness?.exclude_tools,
|
|
89
|
+
extensions: harness?.extensions,
|
|
90
|
+
skills: harness?.skills,
|
|
91
|
+
inlinedSkills: harness?.inlined_skills,
|
|
92
|
+
model: harness?.model,
|
|
93
|
+
thinkingLevel: harness?.thinking,
|
|
94
|
+
hidden: template.hidden,
|
|
95
|
+
fork: harness?.fork,
|
|
96
|
+
includeContextFiles: harness?.include_context_files,
|
|
97
|
+
worktreeCheckoutType: template.worktree_checkout_type,
|
|
98
|
+
systemPrompt: template.system_prompt,
|
|
99
|
+
};
|
|
100
|
+
return compactDefined(obj);
|
|
101
|
+
}
|
|
102
|
+
const BASE_DEFAULTS = {
|
|
103
|
+
name: "unknown",
|
|
104
|
+
description: "",
|
|
105
|
+
// extensions and skills intentionally omitted — resolved by global default
|
|
106
|
+
inlinedSkills: [],
|
|
107
|
+
systemPrompt: "",
|
|
108
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-host.ts — execution-backend contracts (types only).
|
|
3
|
+
* AgentHost: hostAt/start/observe/stop/release/isAttached/findAttempts/deliver.
|
|
4
|
+
* AgentView: placement presentation (focus).
|
|
5
|
+
* HerdrHostRef/AgentHostRef: host address. CheckoutBinding/ViewRequest: placement input.
|
|
6
|
+
* DeliverOutcome: what a pane submit attempt did. HostObservation: live agent state.
|
|
7
|
+
* LiveAttempt: conflict identity for dedup. ExecutionRuntime: host + view for one
|
|
8
|
+
* parent session.
|
|
9
|
+
*/
|
|
10
|
+
export {};
|