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,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cleanup-policy.ts — pure cleanup decisions (no I/O).
|
|
3
|
+
* Located artifacts: LocatedWorktree/LocatedPane/LocatedArtifacts.
|
|
4
|
+
* Plan: CleanupPlan/planRecovered. Gate: GateOutcome/assessRemovability.
|
|
5
|
+
* Settlement: SettlementVerdict/recordedSettlement/recoveryGate/recoveryRefusalReason.
|
|
6
|
+
* Report: CleanupReport/keptReport/ambiguousReport/assembleReport/shouldDropSpawn.
|
|
7
|
+
* Removal: RemovalOutcomes/RemovablePlan/WorktreeRemovalMechanism/PaneDisposition/removalMechanismOf/paneDispositionFor.
|
|
8
|
+
* Probes: WorktreeDirtyProbe. Options: CleanupOptions. Source: CleanupSource. Basis: DiscoveryBasis.
|
|
9
|
+
*/
|
|
10
|
+
import { assertNever } from "../utils.js";
|
|
11
|
+
export function recordedSettlement(spawn) {
|
|
12
|
+
return { kind: "recorded", phase: spawn.lifecycle.phase };
|
|
13
|
+
}
|
|
14
|
+
/** Narrow located artifacts into a plan (pure): resolved managed tree → worktree (missing workspace still goes via git); unresolved managed tree → worktree-unlocatable (never no-worktree, so nothing leaks); recovered panes are never self-created. */
|
|
15
|
+
export function planRecovered(artifacts) {
|
|
16
|
+
const pane = artifacts.pane
|
|
17
|
+
? {
|
|
18
|
+
paneId: artifacts.pane.paneId,
|
|
19
|
+
origin: artifacts.paneCreated ? "self-created" : "adopted",
|
|
20
|
+
}
|
|
21
|
+
: null;
|
|
22
|
+
const worktree = artifacts.worktree;
|
|
23
|
+
if (worktree !== null && worktree.repoCwd !== undefined) {
|
|
24
|
+
return {
|
|
25
|
+
kind: "worktree",
|
|
26
|
+
worktree: {
|
|
27
|
+
workspaceId: worktree.workspaceId,
|
|
28
|
+
worktreePath: worktree.path,
|
|
29
|
+
repoCwd: worktree.repoCwd,
|
|
30
|
+
branchName: artifacts.branch,
|
|
31
|
+
},
|
|
32
|
+
pane,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
if (artifacts.worktreeManaged) {
|
|
36
|
+
return {
|
|
37
|
+
kind: "worktree-unlocatable",
|
|
38
|
+
branchName: artifacts.branch,
|
|
39
|
+
worktreePath: worktree?.path ?? null,
|
|
40
|
+
pane,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return { kind: "no-worktree", pane };
|
|
44
|
+
}
|
|
45
|
+
/** Removability gate: dirty/unverifiable blocks (the only way removal loses work); unmerged does not (the branch survives in the repo); force skips the probe. */
|
|
46
|
+
export async function assessRemovability(isDirty, opts, worktree) {
|
|
47
|
+
if (opts?.force === true)
|
|
48
|
+
return { kind: "removable" };
|
|
49
|
+
const dirty = await isDirty(worktree.worktreePath);
|
|
50
|
+
if (dirty === undefined) {
|
|
51
|
+
return {
|
|
52
|
+
kind: "kept",
|
|
53
|
+
reason: { kind: "unverifiable", detail: "git status probe failed" },
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return dirty
|
|
57
|
+
? { kind: "kept", reason: { kind: "dirty" } }
|
|
58
|
+
: { kind: "removable" };
|
|
59
|
+
}
|
|
60
|
+
export function recoveryGate(observation) {
|
|
61
|
+
if (observation === undefined) {
|
|
62
|
+
return { kind: "allow", basis: { kind: "pane-absent" } };
|
|
63
|
+
}
|
|
64
|
+
switch (observation.state) {
|
|
65
|
+
case "working":
|
|
66
|
+
return { kind: "refuse", basis: { kind: "pane-working" } };
|
|
67
|
+
case "idle":
|
|
68
|
+
case "blocked":
|
|
69
|
+
case "done":
|
|
70
|
+
case "unknown":
|
|
71
|
+
return {
|
|
72
|
+
kind: "allow",
|
|
73
|
+
basis: { kind: "pane-not-working", state: observation.state },
|
|
74
|
+
};
|
|
75
|
+
default:
|
|
76
|
+
return assertNever(observation.state);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export function recoveryRefusalReason(basis) {
|
|
80
|
+
switch (basis.kind) {
|
|
81
|
+
case "pane-working":
|
|
82
|
+
return { kind: "recovered-refusal", basis };
|
|
83
|
+
case "pane-unreadable":
|
|
84
|
+
return {
|
|
85
|
+
kind: "unverifiable",
|
|
86
|
+
detail: `the pane state could not be read (${basis.detail})`,
|
|
87
|
+
};
|
|
88
|
+
case "pane-absent":
|
|
89
|
+
case "pane-not-working":
|
|
90
|
+
case "forced":
|
|
91
|
+
case "artifacts-ambiguous":
|
|
92
|
+
// The gate allows these bases and pre-gate ambiguity refuses before it,
|
|
93
|
+
// so no caller passes one here; the generic detail is a safety net.
|
|
94
|
+
return {
|
|
95
|
+
kind: "unverifiable",
|
|
96
|
+
detail: "the run's end could not be established",
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/* ── Stage 5: report + record-keeping (pure) ───────────────────────────── */
|
|
101
|
+
/** Refusal report shared by all early returns; the reason rides the verdict, so it survives even with no worktree to name. */
|
|
102
|
+
export function keptReport(agentId, source, plan, settlement, reason, paneStatus) {
|
|
103
|
+
const worktree = plan.kind === "worktree"
|
|
104
|
+
? { kind: "kept", path: plan.worktree.worktreePath }
|
|
105
|
+
: plan.kind === "worktree-unlocatable"
|
|
106
|
+
? { kind: "kept", path: plan.worktreePath }
|
|
107
|
+
: { kind: "none" };
|
|
108
|
+
return {
|
|
109
|
+
agentId,
|
|
110
|
+
source,
|
|
111
|
+
outcome: { kind: "refused", reason },
|
|
112
|
+
branchName: plan.kind === "worktree"
|
|
113
|
+
? plan.worktree.branchName
|
|
114
|
+
: plan.kind === "worktree-unlocatable"
|
|
115
|
+
? plan.branchName
|
|
116
|
+
: undefined,
|
|
117
|
+
settlement,
|
|
118
|
+
pane: plan.pane
|
|
119
|
+
? { kind: paneStatus, paneId: plan.pane.paneId }
|
|
120
|
+
: { kind: "none" },
|
|
121
|
+
worktree,
|
|
122
|
+
branch: { kind: "not-applicable" },
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/** Contested-id report: cleanup removes nothing (not even the branch) and lists candidates for the orchestrator. */
|
|
126
|
+
export function ambiguousReport(agentId, source, settlement, candidates) {
|
|
127
|
+
return {
|
|
128
|
+
agentId,
|
|
129
|
+
source,
|
|
130
|
+
outcome: {
|
|
131
|
+
kind: "refused",
|
|
132
|
+
reason: { kind: "ambiguous-artifacts", candidates },
|
|
133
|
+
},
|
|
134
|
+
settlement,
|
|
135
|
+
pane: { kind: "none" },
|
|
136
|
+
worktree: { kind: "kept", path: null },
|
|
137
|
+
branch: { kind: "not-applicable" },
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
export function assembleReport(agentId, source, plan, settlement, removal) {
|
|
141
|
+
return {
|
|
142
|
+
agentId,
|
|
143
|
+
source,
|
|
144
|
+
outcome: { kind: "torn-down" },
|
|
145
|
+
branchName: plan.kind === "worktree" ? plan.worktree.branchName : undefined,
|
|
146
|
+
settlement,
|
|
147
|
+
pane: removal.pane,
|
|
148
|
+
worktree: removal.worktree,
|
|
149
|
+
branch: removal.branch,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Drop a spawn once its tracked resources are gone; a kept/failed tree
|
|
154
|
+
* keeps its spawn so status still shows why. An absent tree drops too: a
|
|
155
|
+
* checkout that is not on disk cannot be removed and holds no work, and the
|
|
156
|
+
* branch is deliberately NOT deleted on that path (its commits live in the
|
|
157
|
+
* object store), so nothing recordable is lost by forgetting the record.
|
|
158
|
+
*/
|
|
159
|
+
export function shouldDropSpawn(report) {
|
|
160
|
+
if (report.worktree.kind === "removed")
|
|
161
|
+
return true;
|
|
162
|
+
if (report.worktree.kind === "absent")
|
|
163
|
+
return true;
|
|
164
|
+
if (report.worktree.kind === "none") {
|
|
165
|
+
return report.pane.kind === "none" || report.pane.kind === "closed";
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
export function removalMechanismOf(worktree) {
|
|
170
|
+
return worktree.workspaceId !== undefined ? "herdr" : "git";
|
|
171
|
+
}
|
|
172
|
+
export function paneDispositionFor(pane, mechanism, removed) {
|
|
173
|
+
if (pane === null)
|
|
174
|
+
return { kind: "none" };
|
|
175
|
+
if (!removed)
|
|
176
|
+
return { kind: "leave-open" };
|
|
177
|
+
switch (mechanism) {
|
|
178
|
+
case "herdr":
|
|
179
|
+
return { kind: "closed-with-workspace" };
|
|
180
|
+
// A git-side removal, or a plan with no tree at all, leaves the pane standing: only a pane the extension created may be closed.
|
|
181
|
+
case "git":
|
|
182
|
+
case null:
|
|
183
|
+
switch (pane.origin) {
|
|
184
|
+
case "self-created":
|
|
185
|
+
return { kind: "close" };
|
|
186
|
+
case "adopted":
|
|
187
|
+
return { kind: "leave-open" };
|
|
188
|
+
default:
|
|
189
|
+
return assertNever(pane.origin);
|
|
190
|
+
}
|
|
191
|
+
default:
|
|
192
|
+
return assertNever(mechanism);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cleanup-report.ts — a CleanupReport as prose: the summary the cleanup tool
|
|
3
|
+
* returns and the one line the menus notify. Each artifact's wording is
|
|
4
|
+
* written once here, so the tool's report and the menu's toast agree.
|
|
5
|
+
*/
|
|
6
|
+
function formatBasis(basis) {
|
|
7
|
+
switch (basis.kind) {
|
|
8
|
+
case "pane-absent":
|
|
9
|
+
return "pane-absent";
|
|
10
|
+
case "pane-not-working":
|
|
11
|
+
return `pane-not-working: ${basis.state}`;
|
|
12
|
+
case "forced":
|
|
13
|
+
return "forced";
|
|
14
|
+
case "pane-working":
|
|
15
|
+
return "pane-working";
|
|
16
|
+
case "pane-unreadable":
|
|
17
|
+
return `pane-unreadable: ${basis.detail}`;
|
|
18
|
+
case "artifacts-ambiguous":
|
|
19
|
+
return "artifacts-ambiguous";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** A recovered run reports `unrecorded`, never an end nobody witnessed. */
|
|
23
|
+
function formatSettlement(settlement) {
|
|
24
|
+
return settlement.kind === "recorded"
|
|
25
|
+
? settlement.phase
|
|
26
|
+
: `unrecorded (basis: ${formatBasis(settlement.basis)})`;
|
|
27
|
+
}
|
|
28
|
+
function formatKeptReason(reason) {
|
|
29
|
+
switch (reason.kind) {
|
|
30
|
+
case "dirty":
|
|
31
|
+
return "has uncommitted changes";
|
|
32
|
+
case "unverifiable":
|
|
33
|
+
return `state could not be verified (${reason.detail})`;
|
|
34
|
+
case "agent-active":
|
|
35
|
+
return `the agent is still ${reason.phase} and has not reported a result — stop it with stop_cowboy_agent, then clean up`;
|
|
36
|
+
case "recovered-refusal":
|
|
37
|
+
return `no settlement was recorded and herdr still reports the agent at work (${formatBasis(reason.basis)}) — stop it before retrying`;
|
|
38
|
+
case "ambiguous-artifacts":
|
|
39
|
+
return `two artifacts answer to this id (${reason.candidates.join(", ")}) — nothing was removed; work out which run owns the id before removing either by hand`;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function paneText(pane) {
|
|
43
|
+
switch (pane.kind) {
|
|
44
|
+
case "closed":
|
|
45
|
+
return "closed";
|
|
46
|
+
case "open":
|
|
47
|
+
return "open";
|
|
48
|
+
case "gone":
|
|
49
|
+
return "gone (herdr has no such pane)";
|
|
50
|
+
case "unknown":
|
|
51
|
+
return "unknown (the pane was not read)";
|
|
52
|
+
case "none":
|
|
53
|
+
return "none";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function worktreeText(worktree) {
|
|
57
|
+
switch (worktree.kind) {
|
|
58
|
+
case "removed":
|
|
59
|
+
return `removed (${worktree.path})`;
|
|
60
|
+
case "removal-failed":
|
|
61
|
+
return `NOT removed — ${worktree.detail} (${worktree.path})`;
|
|
62
|
+
case "kept":
|
|
63
|
+
return `kept${worktree.path ? ` (${worktree.path})` : " (worktree path not recorded)"}`;
|
|
64
|
+
case "absent":
|
|
65
|
+
return `already gone (${worktree.path})`;
|
|
66
|
+
case "none":
|
|
67
|
+
return "none (agent ran without a worktree)";
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function branchText(report) {
|
|
71
|
+
const branch = report.branch;
|
|
72
|
+
const text = branch.kind === "deleted"
|
|
73
|
+
? "deleted"
|
|
74
|
+
: branch.kind === "kept"
|
|
75
|
+
? `kept (${branch.reason})`
|
|
76
|
+
: branch.kind === "delete-failed"
|
|
77
|
+
? `NOT deleted — ${branch.detail}`
|
|
78
|
+
: "not applicable";
|
|
79
|
+
return `${text}${report.branchName ? ` (${report.branchName})` : ""}`;
|
|
80
|
+
}
|
|
81
|
+
/** The report's opening line: a refusal says it refused and why; a teardown says it finished. */
|
|
82
|
+
function headline(report) {
|
|
83
|
+
return report.outcome.kind === "refused"
|
|
84
|
+
? `Did not clean up agent ${report.agentId}: ${formatKeptReason(report.outcome.reason)}`
|
|
85
|
+
: `Cleaned up agent ${report.agentId}:`;
|
|
86
|
+
}
|
|
87
|
+
/** The tool's report: what happened to the run, its pane, worktree and branch. */
|
|
88
|
+
export function renderCleanupReport(report) {
|
|
89
|
+
const lines = [
|
|
90
|
+
headline(report),
|
|
91
|
+
` agent status: ${formatSettlement(report.settlement)}`,
|
|
92
|
+
` pane: ${paneText(report.pane)}`,
|
|
93
|
+
` worktree: ${worktreeText(report.worktree)}`,
|
|
94
|
+
` branch: ${branchText(report)}`,
|
|
95
|
+
];
|
|
96
|
+
if (report.source === "recovered") {
|
|
97
|
+
lines.push(" recovered: no spawn record survived — this verdict rests on discovery (herdr's live read plus the worktree on disk)");
|
|
98
|
+
}
|
|
99
|
+
if (report.settlement.kind === "discovered") {
|
|
100
|
+
const { path, present } = report.settlement.deliverable;
|
|
101
|
+
lines.push(present
|
|
102
|
+
? ` deliverable: UNREAD at ${path} — read it before deleting the directory`
|
|
103
|
+
: ` deliverable: none written (${path} does not exist)`);
|
|
104
|
+
}
|
|
105
|
+
return lines.join("\n");
|
|
106
|
+
}
|
|
107
|
+
/** The same report as one notification-sized line. */
|
|
108
|
+
export function summarizeCleanupReport(report) {
|
|
109
|
+
const pane = report.pane.kind === "closed"
|
|
110
|
+
? "pane closed"
|
|
111
|
+
: report.pane.kind === "open"
|
|
112
|
+
? "pane left open"
|
|
113
|
+
: report.pane.kind === "gone"
|
|
114
|
+
? "pane already gone"
|
|
115
|
+
: report.pane.kind === "unknown"
|
|
116
|
+
? "pane state unknown"
|
|
117
|
+
: "no pane";
|
|
118
|
+
return report.outcome.kind === "refused"
|
|
119
|
+
? `${headline(report)} ${pane}`
|
|
120
|
+
: `${headline(report)} ${pane}, worktree ${worktreeText(report.worktree)}, branch ${branchText(report)}`;
|
|
121
|
+
}
|
|
122
|
+
/** Whether the teardown left something behind that only the operator can settle. */
|
|
123
|
+
export function cleanupNeedsAttention(report) {
|
|
124
|
+
return (report.outcome.kind === "refused" ||
|
|
125
|
+
report.worktree.kind === "kept" ||
|
|
126
|
+
report.worktree.kind === "removal-failed" ||
|
|
127
|
+
report.branch.kind === "delete-failed");
|
|
128
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* default-agents.ts — Embedded default agent configurations.
|
|
3
|
+
*
|
|
4
|
+
* Always available; overridable by same-named .toml files. Only
|
|
5
|
+
* general-purpose is embedded — other types are scanned .toml templates.
|
|
6
|
+
*/
|
|
7
|
+
export const DEFAULT_AGENTS = new Map([
|
|
8
|
+
[
|
|
9
|
+
"general-purpose",
|
|
10
|
+
{
|
|
11
|
+
name: "general-purpose",
|
|
12
|
+
displayName: "Agent",
|
|
13
|
+
description: "General-purpose agent for complex, multi-step tasks",
|
|
14
|
+
// extensions, skills, and harness intentionally omitted — resolved by global default
|
|
15
|
+
inlinedSkills: [],
|
|
16
|
+
systemPrompt: "",
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
]);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* harness/pi-bolt.ts — the pi-bolt harness: a pi-compatible binary behind the
|
|
3
|
+
* pane's `pi`.
|
|
4
|
+
*
|
|
5
|
+
* Like pig, pi-bolt takes pi's `[harness]` section and argv unchanged; only its
|
|
6
|
+
* binary name differs from pi, which the pi-compatible factory reconciles by
|
|
7
|
+
* pointing the pane's `pi` at `pi-bolt`.
|
|
8
|
+
*/
|
|
9
|
+
import { createPiCompatibleHarness } from "./pi-compatible.js";
|
|
10
|
+
export const piBoltHarness = createPiCompatibleHarness({
|
|
11
|
+
id: "pi-bolt",
|
|
12
|
+
binary: "pi-bolt",
|
|
13
|
+
signature: "PI_BOLT_LAUNCHER_5e1c7a",
|
|
14
|
+
readyMarker: "PI_BOLT_VERIFIED",
|
|
15
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-compatible.ts — the shared preparation for a pi-compatible binary behind
|
|
3
|
+
* the pane's `pi`.
|
|
4
|
+
*
|
|
5
|
+
* herdr types the canonical name `pi` into the pane, so a harness whose binary
|
|
6
|
+
* is not named `pi` must point the pane's `pi` at its own binary before herdr
|
|
7
|
+
* starts the agent. That shell trick is identical for every such harness, so
|
|
8
|
+
* this factory builds one from a binary name and a signature that proves which
|
|
9
|
+
* launcher landed.
|
|
10
|
+
*/
|
|
11
|
+
import { HerdrError, runInPane, waitForPaneOutput, } from "../../infrastructure/herdr-client.js";
|
|
12
|
+
import { commandPath } from "../../infrastructure/exec-path.js";
|
|
13
|
+
import { sleep } from "../../utils.js";
|
|
14
|
+
/** Bounded prepare attempts: a `pane run` before the shell is at its prompt is silently lost. */
|
|
15
|
+
const PREPARE_MAX_ATTEMPTS = 5;
|
|
16
|
+
const PREPARE_OUTPUT_TIMEOUT_MS = 2_000;
|
|
17
|
+
const PREPARE_RETRY_DELAY_MS = 250;
|
|
18
|
+
/**
|
|
19
|
+
* Break the marker with a `""` so only the shell's evaluation of the command —
|
|
20
|
+
* not the command line's own echo, which `wait-output` also sees — produces the
|
|
21
|
+
* contiguous token.
|
|
22
|
+
*/
|
|
23
|
+
function splitMarker(marker) {
|
|
24
|
+
const at = marker.indexOf("_");
|
|
25
|
+
return at === -1
|
|
26
|
+
? marker
|
|
27
|
+
: `${marker.slice(0, at + 1)}""${marker.slice(at + 1)}`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Build the harness for one pi-compatible binary.
|
|
31
|
+
*
|
|
32
|
+
* The launcher runs the binary as a child of the pane's shell rather than via
|
|
33
|
+
* `exec`: replacing the shell would make the pane close when the binary exits,
|
|
34
|
+
* and the pane must outlive the process (only cleanup closes a pane it created).
|
|
35
|
+
* `HERDR_AGENT=pi` is what makes herdr's pi screen manifest detect the process —
|
|
36
|
+
* without it the start times out and the pane never registers as an agent.
|
|
37
|
+
*
|
|
38
|
+
* Verification reads the pane's `pi` back with `typeset -f` and matches the
|
|
39
|
+
* signature, so a match means the body is this launcher rather than an unrelated
|
|
40
|
+
* `pi` from the pane's rc. `type pi` is not used: its wording varies by shell
|
|
41
|
+
* (`is a function` in bash, `is a shell function` in zsh).
|
|
42
|
+
*/
|
|
43
|
+
export function createPiCompatibleHarness(spec) {
|
|
44
|
+
const launcher = `pi() { : "${spec.signature}"; export HERDR_AGENT=pi; ${spec.binary} "$@"; }`;
|
|
45
|
+
const verifyCommand = `typeset -f pi 2>/dev/null | grep -q "${spec.signature}" && echo ${splitMarker(spec.readyMarker)}`;
|
|
46
|
+
return {
|
|
47
|
+
id: spec.id,
|
|
48
|
+
// The launcher runs `spec.binary` inside the pane, so a machine without it
|
|
49
|
+
// can never launch this harness — however well the preparation itself would
|
|
50
|
+
// go.
|
|
51
|
+
available: () => commandPath(spec.binary) !== undefined,
|
|
52
|
+
async prepare({ pi, paneId }) {
|
|
53
|
+
for (let attempt = 1; attempt <= PREPARE_MAX_ATTEMPTS; attempt++) {
|
|
54
|
+
await runInPane(pi, paneId, launcher);
|
|
55
|
+
await runInPane(pi, paneId, verifyCommand);
|
|
56
|
+
try {
|
|
57
|
+
await waitForPaneOutput(pi, paneId, spec.readyMarker, PREPARE_OUTPUT_TIMEOUT_MS);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
if (attempt < PREPARE_MAX_ATTEMPTS)
|
|
62
|
+
await sleep(PREPARE_RETRY_DELAY_MS);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
throw new HerdrError(`failed to prepare pane ${paneId} for the ${spec.id} harness: the pi launcher never landed`, undefined);
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* harness/pig.ts — the pig harness: a pi-compatible binary behind the pane's
|
|
3
|
+
* `pi`.
|
|
4
|
+
*
|
|
5
|
+
* pig is a drop-in reimplementation of pi (https://github.com/MichaelKinsy/PiG),
|
|
6
|
+
* so it takes pi's `[harness]` section and argv unchanged. Only its binary name
|
|
7
|
+
* differs from pi, which the pi-compatible factory reconciles by pointing the
|
|
8
|
+
* pane's `pi` at `pig`.
|
|
9
|
+
*/
|
|
10
|
+
import { createPiCompatibleHarness } from "./pi-compatible.js";
|
|
11
|
+
export const pigHarness = createPiCompatibleHarness({
|
|
12
|
+
id: "pig",
|
|
13
|
+
binary: "pig",
|
|
14
|
+
signature: "PIG_LAUNCHER_8f9a2b",
|
|
15
|
+
readyMarker: "PIG_VERIFIED",
|
|
16
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* harness/registry.ts — the one module that knows every harness.
|
|
3
|
+
*
|
|
4
|
+
* It owns the id → implementation lookup, so a caller that holds a
|
|
5
|
+
* `harness_type` resolves it to the harness that launches it.
|
|
6
|
+
*/
|
|
7
|
+
import { pigHarness } from "./pig.js";
|
|
8
|
+
import { piBoltHarness } from "./pi-bolt.js";
|
|
9
|
+
/**
|
|
10
|
+
* Real pi is already the pane shell's `pi`: it is always launchable, and there is
|
|
11
|
+
* nothing to prepare.
|
|
12
|
+
*/
|
|
13
|
+
const piHarness = {
|
|
14
|
+
id: "pi",
|
|
15
|
+
available: () => true,
|
|
16
|
+
prepare: async () => { },
|
|
17
|
+
};
|
|
18
|
+
/** Keyed by id, so a harness added to `HARNESS_IDS` without an implementation fails to compile. */
|
|
19
|
+
const HARNESSES = {
|
|
20
|
+
pi: piHarness,
|
|
21
|
+
pig: pigHarness,
|
|
22
|
+
"pi-bolt": piBoltHarness,
|
|
23
|
+
};
|
|
24
|
+
/** The implementation for one harness id. */
|
|
25
|
+
export function harnessFor(id) {
|
|
26
|
+
return HARNESSES[id];
|
|
27
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* harness.ts — the harness contract: which process launches a subagent, and how.
|
|
3
|
+
*
|
|
4
|
+
* A template's `[harness]` table is harness-specific — its fields belong to the
|
|
5
|
+
* harness that owns them. This module holds only what every harness has in
|
|
6
|
+
* common plus the id vocabulary; each harness's implementation lives beside it
|
|
7
|
+
* (harness/pig.ts, harness/pi-bolt.ts) and is selected through
|
|
8
|
+
* harness/registry.ts.
|
|
9
|
+
*
|
|
10
|
+
* `HARNESS_IDS` stays the full vocabulary everywhere a value is read or written:
|
|
11
|
+
* a template written where a harness exists must still load where it does not.
|
|
12
|
+
* Which harnesses a machine can launch only narrows what the setting offers and
|
|
13
|
+
* which harness a spawn ends up using.
|
|
14
|
+
*/
|
|
15
|
+
import { isAvailable } from "../availability.js";
|
|
16
|
+
/** Every selectable harness. The `harness_type` enum and `HarnessId` derive from this. */
|
|
17
|
+
export const HARNESS_IDS = ["pi", "pig", "pi-bolt"];
|
|
18
|
+
/** A template that omits `harness_type` launches under pi. */
|
|
19
|
+
export const DEFAULT_HARNESS = "pi";
|
|
20
|
+
/** Narrow untrusted input (config, menu) to a canonical harness; invalid values fall back at the config boundary. */
|
|
21
|
+
export function parseHarnessId(value) {
|
|
22
|
+
return HARNESS_IDS.find((id) => id === value);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Which harness a spawn launches under. A harness this machine cannot launch
|
|
26
|
+
* falls back to pi whatever the setting or the template says — the same answer
|
|
27
|
+
* the setting itself is narrowed to, so the menu can never offer a harness a
|
|
28
|
+
* spawn would not use, and an unavailable value never fails a spawn.
|
|
29
|
+
*/
|
|
30
|
+
export function resolveHarness(configured, availability) {
|
|
31
|
+
return isAvailable(configured, availability) ? configured : DEFAULT_HARNESS;
|
|
32
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* result-file-permissions.ts — owner-only modes for per-agent result dirs.
|
|
3
|
+
*
|
|
4
|
+
* Spawns stage secret-bearing files under the spawn's result directory
|
|
5
|
+
* (`<tmpdir>/pi-cowboy/<agentId>/`);
|
|
6
|
+
* the process umask would leave them world-readable, so directories enforce
|
|
7
|
+
* `0o700` and files `0o600`. The `mkdir`/`write` mode option applies only on
|
|
8
|
+
* creation, so every helper also `chmod`s for paths that already exist.
|
|
9
|
+
* Both calls throw: a spawn that cannot lock its secrets down fails its
|
|
10
|
+
* launch instead of proceeding world-readable.
|
|
11
|
+
*/
|
|
12
|
+
import fs from "node:fs";
|
|
13
|
+
/** Mode for per-agent result directories (`<tmpdir>/pi-cowboy/<agentId>/`). */
|
|
14
|
+
export const RESULT_DIR_MODE = 0o700;
|
|
15
|
+
/** Mode for the secret-bearing files staged inside a result directory. */
|
|
16
|
+
export const RESULT_FILE_MODE = 0o600;
|
|
17
|
+
/** Throws on any failure — callers must let it fail the launch. */
|
|
18
|
+
export function ensureResultDir(dir) {
|
|
19
|
+
fs.mkdirSync(dir, { recursive: true, mode: RESULT_DIR_MODE });
|
|
20
|
+
fs.chmodSync(dir, RESULT_DIR_MODE);
|
|
21
|
+
}
|
|
22
|
+
/** Throws on any failure — callers must let it fail the launch. */
|
|
23
|
+
export function writeResultFile(file, content) {
|
|
24
|
+
fs.writeFileSync(file, content, {
|
|
25
|
+
encoding: "utf-8",
|
|
26
|
+
mode: RESULT_FILE_MODE,
|
|
27
|
+
});
|
|
28
|
+
fs.chmodSync(file, RESULT_FILE_MODE);
|
|
29
|
+
}
|