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,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tool-execution.ts — Pi-facing tool adapter: validates params into a
|
|
3
|
+
* delegation intent and wraps the dual payload as a `ToolResult`.
|
|
4
|
+
*
|
|
5
|
+
* - `executeAgentTool`: the cowboy_agent handler. Each call dispatches one or
|
|
6
|
+
* more spawns through the batch runner in `src/orchestrators/protocol.ts`;
|
|
7
|
+
* single-item runs with `run_in_background: false` block until the agent
|
|
8
|
+
* settles and return its inline result, every other shape returns a
|
|
9
|
+
* background acknowledgement.
|
|
10
|
+
* - `executeStopAgentTool`: the stop_cowboy_agent handler.
|
|
11
|
+
* - `toolCallListener`: injects the configured model/thinking level per item.
|
|
12
|
+
*/
|
|
13
|
+
import { isActivePhase, lifecycleStatus } from "../types.js";
|
|
14
|
+
import { CONFIGURED_MODEL_KEY, readModelKey } from "../models/model-request.js";
|
|
15
|
+
import { getManager, getStore } from "../shell.js";
|
|
16
|
+
import { formatActiveAgents, runDelegationBatch, } from "../orchestrators/protocol.js";
|
|
17
|
+
import { getAgentConfig } from "./agent-types.js";
|
|
18
|
+
import { resolveAgentTypeParam } from "./spawn-defaults.js";
|
|
19
|
+
import { runBatch } from "./batch.js";
|
|
20
|
+
import { successResult } from "./tool-result.js";
|
|
21
|
+
/**
|
|
22
|
+
* cowboy_agent handler. Validates the call shape and dispatches one spawn per
|
|
23
|
+
* item in `agents`. A non-background call requires exactly one item — the
|
|
24
|
+
* guard lives in `runDelegationBatch` so it fires before any side effect.
|
|
25
|
+
*/
|
|
26
|
+
export async function executeAgentTool(_toolCallId, params, signal, _onUpdate, ctx) {
|
|
27
|
+
const { message, details } = await runDelegationBatch(params, ctx, _toolCallId, signal);
|
|
28
|
+
return successResult(message, details);
|
|
29
|
+
}
|
|
30
|
+
/** Per-item result line the model reads back, in input order. */
|
|
31
|
+
export function renderStopOutcome(outcome) {
|
|
32
|
+
switch (outcome.kind) {
|
|
33
|
+
case "stopped":
|
|
34
|
+
return `Stopped agent ${outcome.agentId}`;
|
|
35
|
+
case "already-settled":
|
|
36
|
+
return `Agent ${outcome.agentId} is already ${outcome.status}. Active agents: ${outcome.activeAgents}`;
|
|
37
|
+
case "unknown":
|
|
38
|
+
return `Agent ${outcome.agentId} not found. Active agents: ${outcome.activeAgents}`;
|
|
39
|
+
case "failed":
|
|
40
|
+
return `Failed to stop agent ${outcome.agentId}: ${outcome.error}`;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* stop_cowboy_agent handler. Stops every id in `agent_ids` sequentially in
|
|
45
|
+
* input order — teardown shares herdr/git state, so items never run in
|
|
46
|
+
* parallel. Best effort: one id's outcome never blocks the rest. `agent_ids`
|
|
47
|
+
* is non-empty by the schema boundary; whole-call throws (before anything is
|
|
48
|
+
* stopped) only for a repeated id.
|
|
49
|
+
*/
|
|
50
|
+
export async function executeStopAgentTool(_toolCallId, params, _signal, _onUpdate, _ctx) {
|
|
51
|
+
const agentIds = params.agent_ids;
|
|
52
|
+
return runBatch({
|
|
53
|
+
toolName: "stop_cowboy_agent",
|
|
54
|
+
param: "agent_ids",
|
|
55
|
+
detailsKey: "agents",
|
|
56
|
+
items: agentIds,
|
|
57
|
+
render: renderStopOutcome,
|
|
58
|
+
handleItem: async (agentId) => {
|
|
59
|
+
const spawn = getManager().getSpawn(agentId);
|
|
60
|
+
if (!spawn) {
|
|
61
|
+
return {
|
|
62
|
+
kind: "unknown",
|
|
63
|
+
agentId,
|
|
64
|
+
activeAgents: formatActiveAgents(),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (!isActivePhase(spawn.lifecycle.phase)) {
|
|
68
|
+
return {
|
|
69
|
+
kind: "already-settled",
|
|
70
|
+
agentId,
|
|
71
|
+
status: lifecycleStatus(spawn.lifecycle),
|
|
72
|
+
activeAgents: formatActiveAgents(),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
return (await getManager().abort(agentId, "agent"))
|
|
77
|
+
? { kind: "stopped", agentId }
|
|
78
|
+
: {
|
|
79
|
+
kind: "failed",
|
|
80
|
+
agentId,
|
|
81
|
+
error: "the agent could not be stopped",
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
return {
|
|
86
|
+
kind: "failed",
|
|
87
|
+
agentId,
|
|
88
|
+
error: err instanceof Error ? err.message : String(err),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Inject the configured default model and the agent-type-resolved thinking
|
|
96
|
+
* level into each item in `agents`. Per-item: a batch may carry items with
|
|
97
|
+
* different `agent_type`s and different effective models.
|
|
98
|
+
*/
|
|
99
|
+
export async function toolCallListener(event, ctx) {
|
|
100
|
+
if (event.toolName !== "cowboy_agent")
|
|
101
|
+
return;
|
|
102
|
+
const input = event.input;
|
|
103
|
+
const items = input.agents ?? [];
|
|
104
|
+
const parentModelId = ctx.model
|
|
105
|
+
? `${ctx.model.provider}/${ctx.model.id}`
|
|
106
|
+
: null;
|
|
107
|
+
for (const item of items) {
|
|
108
|
+
const calledType = item.agent_type;
|
|
109
|
+
// Look up the type the spawn will actually resolve.
|
|
110
|
+
const subagentType = resolveAgentTypeParam(calledType);
|
|
111
|
+
const agentConfig = getAgentConfig(subagentType);
|
|
112
|
+
// The configured default travels in its own key so a caller-supplied model
|
|
113
|
+
// is never mistaken for the injected default across repeated hook runs.
|
|
114
|
+
const calledModel = readModelKey(item.model);
|
|
115
|
+
const effectiveModel = calledModel ??
|
|
116
|
+
getStore().modelFor(subagentType, parentModelId, agentConfig);
|
|
117
|
+
if (effectiveModel && !calledModel) {
|
|
118
|
+
item[CONFIGURED_MODEL_KEY] = effectiveModel;
|
|
119
|
+
}
|
|
120
|
+
if (item.thinking === undefined) {
|
|
121
|
+
item.thinking =
|
|
122
|
+
agentConfig?.thinkingLevel ?? getStore().agent.defaultThinking;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tool-merge.ts — merge_cowboy_branch tool implementation.
|
|
3
|
+
*
|
|
4
|
+
* Merges a finished subagent's branch via the guardrailed
|
|
5
|
+
* `mergeBranchIntoTarget` helper (conflicts are reported, never
|
|
6
|
+
* auto-resolved). The `repo` param can point at any on-disk repository, so
|
|
7
|
+
* an out-of-session merge warns loudly (notification + first-line banner);
|
|
8
|
+
* an unresolvable session scope throws instead of skipping the check.
|
|
9
|
+
*/
|
|
10
|
+
import * as fs from "node:fs";
|
|
11
|
+
import * as path from "node:path";
|
|
12
|
+
import { getPiInstance, getSessionCtx } from "../shell.js";
|
|
13
|
+
import { mergeBranchIntoTarget, assertValidBranchRef, } from "../infrastructure/git-client.js";
|
|
14
|
+
import { GitCommandRunner, locateMainCheckout, } from "../infrastructure/git/git-runner.js";
|
|
15
|
+
import { runBatch } from "./batch.js";
|
|
16
|
+
/** Case is preserved: both roots come from `git rev-parse`, which spells one repo one way. */
|
|
17
|
+
export function normalizeRepoRoot(root) {
|
|
18
|
+
const normalized = path.normalize(root);
|
|
19
|
+
const { root: fsRoot } = path.parse(normalized);
|
|
20
|
+
return normalized.length > fsRoot.length
|
|
21
|
+
? normalized.replace(/[/\\]+$/, "")
|
|
22
|
+
: normalized;
|
|
23
|
+
}
|
|
24
|
+
export function isSameRepoRoot(a, b) {
|
|
25
|
+
return normalizeRepoRoot(a) === normalizeRepoRoot(b);
|
|
26
|
+
}
|
|
27
|
+
/** First-line banner naming both checkouts: where the merge landed versus the session repo. */
|
|
28
|
+
export function formatOutsideRepoWarning(mainRoot, sessionRoot) {
|
|
29
|
+
return `WARNING: merging in ${mainRoot}, outside your session repo (${sessionRoot}) — verify this was intended.`;
|
|
30
|
+
}
|
|
31
|
+
/** Throws instead of guessing, so a merge never proceeds without a scope check. */
|
|
32
|
+
async function resolveSessionMainRoot(pi, sessionCwd) {
|
|
33
|
+
const runner = new GitCommandRunner(pi);
|
|
34
|
+
const toplevel = await runner.probe(["rev-parse", "--show-toplevel"], sessionCwd);
|
|
35
|
+
if (toplevel === undefined) {
|
|
36
|
+
throw new Error(`merge_cowboy_branch cannot verify the session repo: ${sessionCwd} is not inside a git repository (git rev-parse --show-toplevel failed). ` +
|
|
37
|
+
`Refusing to merge without a repo-scope check — run the session from inside a git checkout or pass an explicit \`repo\` inside one.`);
|
|
38
|
+
}
|
|
39
|
+
const common = await runner.probe(["rev-parse", "--git-common-dir"], toplevel);
|
|
40
|
+
if (common === undefined) {
|
|
41
|
+
throw new Error(`merge_cowboy_branch cannot verify the session repo: could not resolve the git directory for ${sessionCwd}. ` +
|
|
42
|
+
`Refusing to merge without a repo-scope check.`);
|
|
43
|
+
}
|
|
44
|
+
const { mainRoot } = locateMainCheckout(common, toplevel);
|
|
45
|
+
if (!fs.existsSync(path.join(mainRoot, ".git"))) {
|
|
46
|
+
throw new Error(`merge_cowboy_branch cannot verify the session repo: no main checkout found for ${sessionCwd} (unsupported repo layout). ` +
|
|
47
|
+
`Refusing to merge without a repo-scope check.`);
|
|
48
|
+
}
|
|
49
|
+
return mainRoot;
|
|
50
|
+
}
|
|
51
|
+
/** A conflict throw carries its file list between these markers (see git-merger.ts). */
|
|
52
|
+
const CONFLICT_MARKER = "has conflicts in:";
|
|
53
|
+
function isConflictError(message) {
|
|
54
|
+
return message.includes(CONFLICT_MARKER);
|
|
55
|
+
}
|
|
56
|
+
/** Conflicted paths from a conflict throw; empty when the message does not parse. */
|
|
57
|
+
function parseConflictFiles(message) {
|
|
58
|
+
const match = message.match(/has conflicts in:\n([\s\S]*?)\nThe merge is still/);
|
|
59
|
+
if (!match)
|
|
60
|
+
return [];
|
|
61
|
+
return match[1]
|
|
62
|
+
.split("\n")
|
|
63
|
+
.map((line) => line.trim())
|
|
64
|
+
.filter((line) => line !== "");
|
|
65
|
+
}
|
|
66
|
+
function isUnknownBranchError(message, branch) {
|
|
67
|
+
return message.includes(`branch "${branch}" does not exist`);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* merge_cowboy_branch handler. Merges every branch in `branches` sequentially
|
|
71
|
+
* in input order — merges share one checkout, so items never run in
|
|
72
|
+
* parallel. `target` and `repo` apply to the whole call. Best effort except
|
|
73
|
+
* conflicts: a conflict halts the batch (the checkout is mid-merge, so
|
|
74
|
+
* nothing further can merge) and the remaining branches report as
|
|
75
|
+
* not-attempted. `branches` is non-empty by the schema boundary; whole-call
|
|
76
|
+
* throws (before anything merges) only for a repeated branch, a ref that is not
|
|
77
|
+
* a branch name, or an unverifiable session scope.
|
|
78
|
+
*/
|
|
79
|
+
export async function executeMergeBranchTool(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
80
|
+
const target = params.target !== undefined && params.target.trim() !== ""
|
|
81
|
+
? params.target.trim()
|
|
82
|
+
: "main";
|
|
83
|
+
const repo = params.repo !== undefined && params.repo.trim() !== ""
|
|
84
|
+
? params.repo.trim()
|
|
85
|
+
: undefined;
|
|
86
|
+
// Probed on first use and memoized: an unverifiable session scope fails
|
|
87
|
+
// before anything is mutated, and the batch probes once, not once per branch.
|
|
88
|
+
let scope;
|
|
89
|
+
const mergeScope = () => {
|
|
90
|
+
scope ??= (async () => {
|
|
91
|
+
const sessionCwd = getSessionCtx().cwd;
|
|
92
|
+
if (typeof sessionCwd !== "string" || sessionCwd.trim() === "") {
|
|
93
|
+
throw new Error("merge_cowboy_branch cannot verify the session repo: the session working directory is unavailable, so the repo-scope check is impossible. " +
|
|
94
|
+
"Refusing to merge without a repo-scope check.");
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
sessionRoot: await resolveSessionMainRoot(getPiInstance(), sessionCwd),
|
|
98
|
+
cwd: repo ?? sessionCwd,
|
|
99
|
+
};
|
|
100
|
+
})();
|
|
101
|
+
return scope;
|
|
102
|
+
};
|
|
103
|
+
return runBatch({
|
|
104
|
+
toolName: "merge_cowboy_branch",
|
|
105
|
+
param: "branches",
|
|
106
|
+
detailsKey: "branches",
|
|
107
|
+
items: params.branches,
|
|
108
|
+
prepare: async () => {
|
|
109
|
+
// Once for the whole call: a ref that is not a branch name would otherwise be
|
|
110
|
+
// refused once per branch, after the earlier branches had already merged.
|
|
111
|
+
assertValidBranchRef(target, "target");
|
|
112
|
+
for (const rawBranch of params.branches) {
|
|
113
|
+
assertValidBranchRef(rawBranch.trim(), "branch");
|
|
114
|
+
}
|
|
115
|
+
await mergeScope();
|
|
116
|
+
},
|
|
117
|
+
halt: {
|
|
118
|
+
on: (outcome) => outcome.kind === "conflict",
|
|
119
|
+
skip: (rawBranch, haltedBy) => {
|
|
120
|
+
const branch = rawBranch.trim();
|
|
121
|
+
const reason = `not attempted — the merge of "${haltedBy.trim()}" conflicted and is still in progress`;
|
|
122
|
+
return {
|
|
123
|
+
outcome: { kind: "not-attempted", branch, reason },
|
|
124
|
+
text: `"${branch}" ${reason}.`,
|
|
125
|
+
};
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
handleItem: async (rawBranch) => {
|
|
129
|
+
const { sessionRoot, cwd } = await mergeScope();
|
|
130
|
+
const branch = rawBranch.trim();
|
|
131
|
+
try {
|
|
132
|
+
const result = await mergeBranchIntoTarget(getPiInstance(), { cwd, branch, target });
|
|
133
|
+
// The merge proceeds on a warning, not a refusal; the banner is the
|
|
134
|
+
// durable signal because notifications can be missed or unavailable.
|
|
135
|
+
const warning = isSameRepoRoot(result.mainRoot, sessionRoot)
|
|
136
|
+
? undefined
|
|
137
|
+
: formatOutsideRepoWarning(result.mainRoot, sessionRoot);
|
|
138
|
+
if (warning !== undefined) {
|
|
139
|
+
try {
|
|
140
|
+
ctx.ui.notify(warning, "warning");
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
// A broken UI sink must never fail the merge itself.
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const prefix = warning !== undefined ? `${warning}\n` : "";
|
|
147
|
+
if (result.alreadyMerged) {
|
|
148
|
+
return {
|
|
149
|
+
outcome: { kind: "already-merged", branch, target },
|
|
150
|
+
text: `${prefix}"${branch}" is already merged into "${result.target}" — nothing to do.`,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
outcome: {
|
|
155
|
+
kind: "merged",
|
|
156
|
+
branch,
|
|
157
|
+
target: result.target,
|
|
158
|
+
output: result.output,
|
|
159
|
+
},
|
|
160
|
+
text: `${prefix}Merged "${branch}" into "${result.target}" in ${result.mainRoot}.` +
|
|
161
|
+
(result.output ? `\n${result.output}` : ""),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
166
|
+
if (isConflictError(message)) {
|
|
167
|
+
return {
|
|
168
|
+
outcome: {
|
|
169
|
+
kind: "conflict",
|
|
170
|
+
branch,
|
|
171
|
+
target,
|
|
172
|
+
files: parseConflictFiles(message),
|
|
173
|
+
},
|
|
174
|
+
text: `"${branch}": ${message}`,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
if (isUnknownBranchError(message, branch)) {
|
|
178
|
+
return {
|
|
179
|
+
outcome: { kind: "unknown", branch },
|
|
180
|
+
text: `"${branch}": ${message}`,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
outcome: { kind: "failed", branch, error: message },
|
|
185
|
+
text: `"${branch}": ${message}`,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tool-result.ts — shared tool result types.
|
|
3
|
+
*
|
|
4
|
+
* Pi's `AgentToolResult<T>` does not resolve from this repo, so the shape is
|
|
5
|
+
* declared structurally here; `ToolResult<TDetails>` stays assignable to it.
|
|
6
|
+
*/
|
|
7
|
+
export function successResult(text, details) {
|
|
8
|
+
return { content: [{ type: "text", text }], details };
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* No `details` key at runtime (pi reads `result.details` straight off the
|
|
12
|
+
* object, so missing and `undefined` are identical); asserted here because
|
|
13
|
+
* `ToolResult` requires the field for the registration site to typecheck.
|
|
14
|
+
*/
|
|
15
|
+
export function textOnlyResult(text) {
|
|
16
|
+
return { content: [{ type: "text", text }] };
|
|
17
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tool-steer.ts — steer_cowboy_agent tool implementation.
|
|
3
|
+
*
|
|
4
|
+
* Delivers one message to every id in `agent_ids` (a spawned agent queues it
|
|
5
|
+
* before its next model call; a settled one is revived for another
|
|
6
|
+
* completion). Delivery is fire-and-forget; refusals are reported, never
|
|
7
|
+
* thrown — a throw inside one item is caught and reported as that item's
|
|
8
|
+
* refusal, so only a malformed call fails outright.
|
|
9
|
+
*/
|
|
10
|
+
import { formatActiveAgents } from "../orchestrators/protocol.js";
|
|
11
|
+
import { getManager } from "../shell.js";
|
|
12
|
+
import { lifecycleStatus } from "../types.js";
|
|
13
|
+
import { errorMessage } from "../utils.js";
|
|
14
|
+
import { runBatch } from "./batch.js";
|
|
15
|
+
/**
|
|
16
|
+
* steer_cowboy_agent handler. Delivers `message` to every id in `agent_ids`
|
|
17
|
+
* sequentially in input order — delivery shares pane state, so items never
|
|
18
|
+
* run in parallel. Best effort: one id's refusal (a queued agent has no
|
|
19
|
+
* pane; a cleaned-up agent is gone) never blocks the rest. `agent_ids` is
|
|
20
|
+
* non-empty by the schema boundary; whole-call throws (before anything is
|
|
21
|
+
* delivered) only for a repeated id or a blank message.
|
|
22
|
+
*/
|
|
23
|
+
export async function executeSteerAgentTool(_toolCallId, params, _signal, _onUpdate, _ctx) {
|
|
24
|
+
const message = params.message.trim();
|
|
25
|
+
return runBatch({
|
|
26
|
+
toolName: "steer_cowboy_agent",
|
|
27
|
+
param: "agent_ids",
|
|
28
|
+
detailsKey: "agents",
|
|
29
|
+
items: params.agent_ids,
|
|
30
|
+
prepare: () => {
|
|
31
|
+
if (message === "") {
|
|
32
|
+
throw new Error("message is required");
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
handleItem: async (agentId) => {
|
|
36
|
+
const spawn = getManager().getSpawn(agentId);
|
|
37
|
+
if (!spawn) {
|
|
38
|
+
const reason = `not found. Active agents: ${formatActiveAgents()}`;
|
|
39
|
+
return {
|
|
40
|
+
outcome: { kind: "refused", agentId, reason },
|
|
41
|
+
text: `Agent ${agentId} was not steered: ${reason}`,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
// Capture before delivery: the refusal text must name the state the
|
|
45
|
+
// message reached, which delivery is about to change.
|
|
46
|
+
const status = lifecycleStatus(spawn.lifecycle);
|
|
47
|
+
let outcome;
|
|
48
|
+
try {
|
|
49
|
+
outcome = await getManager().steer(agentId, message);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
// Nothing has reached the pane yet: a refusal is the honest verdict,
|
|
53
|
+
// and one id's failure never takes the rest of the batch with it.
|
|
54
|
+
outcome = {
|
|
55
|
+
kind: "refused",
|
|
56
|
+
reason: `steering failed: ${errorMessage(error)}`,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
if (outcome.kind === "refused") {
|
|
60
|
+
return {
|
|
61
|
+
outcome: { kind: "refused", agentId, reason: outcome.reason },
|
|
62
|
+
text: `Agent ${agentId} was not steered (${status}): ${outcome.reason}`,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (outcome.kind === "delivered-not-revived") {
|
|
66
|
+
return {
|
|
67
|
+
outcome: {
|
|
68
|
+
kind: "delivered-not-revived",
|
|
69
|
+
agentId,
|
|
70
|
+
reason: outcome.reason,
|
|
71
|
+
},
|
|
72
|
+
text: `Agent ${agentId} had settled (${status}) and the message reached its pane, but it was not revived: ${outcome.reason}`,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
// Read the lifecycle after delivery: a revive re-projects the spawn to
|
|
76
|
+
// `spawned`, so revival is what the arrival status was not and what
|
|
77
|
+
// delivery left behind.
|
|
78
|
+
const revived = status !== "spawned" && spawn.lifecycle.phase === "spawned";
|
|
79
|
+
return {
|
|
80
|
+
outcome: { kind: "delivered", agentId },
|
|
81
|
+
text: revived
|
|
82
|
+
? `Agent ${agentId} had settled (${status}) — revived: the message was delivered to its pane and the agent is spawned again. Merge nothing yet; a new completion message will arrive when it settles again.`
|
|
83
|
+
: status === "spawned"
|
|
84
|
+
? `Message delivered to spawned agent ${agentId}. Delivery is fire-and-forget — the agent picks it up on its next turn; its next completion message will report the result.`
|
|
85
|
+
: `Agent ${agentId} was delivered to (${status}), but it was not revived — it has no process left to act on the message.`,
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* availability.ts — which values of a fixed vocabulary this machine can actually
|
|
3
|
+
* use, and the narrowing that turns that answer into the values a setting offers.
|
|
4
|
+
*
|
|
5
|
+
* A probe answers for its whole vocabulary at once, because every member of a
|
|
6
|
+
* vocabulary is judged the same way (one probe of a volume, one scan of PATH).
|
|
7
|
+
*
|
|
8
|
+
* The answer is carried, not awaited: a settings list is built synchronously, so
|
|
9
|
+
* a probe records its verdict in the shell and every reader reads it there. A
|
|
10
|
+
* verdict that is not in yet rules nothing out — "unknown" is the state before
|
|
11
|
+
* the probe lands, and until it does every value stays on offer and every
|
|
12
|
+
* configured value still resolves, so a slow probe never locks the user out of
|
|
13
|
+
* the setting or silently rewrites it.
|
|
14
|
+
*/
|
|
15
|
+
/** A probe that has answered: these are the values the machine can use. */
|
|
16
|
+
export function probed(available) {
|
|
17
|
+
return { status: "known", available: new Set(available) };
|
|
18
|
+
}
|
|
19
|
+
/** A probe that has not answered yet. */
|
|
20
|
+
export function unprobed() {
|
|
21
|
+
return { status: "unknown" };
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Whether one value is usable. `NoInfer` keeps the value from setting the
|
|
25
|
+
* vocabulary: which values exist is the availability's business, not the
|
|
26
|
+
* caller's.
|
|
27
|
+
*/
|
|
28
|
+
export function isAvailable(id, availability) {
|
|
29
|
+
return availability.status === "unknown" || availability.available.has(id);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The members of `all` this machine can use, in `all`'s order — the setting's own
|
|
33
|
+
* order, so a narrowed list never reshuffles. Nothing is dropped before a probe
|
|
34
|
+
* answers.
|
|
35
|
+
*/
|
|
36
|
+
export function selectable(all, availability) {
|
|
37
|
+
return all.filter((id) => isAvailable(id, availability));
|
|
38
|
+
}
|