pi-cowboy 0.9.2 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -46,11 +46,11 @@ const PiHarnessSchema = z.strictObject(PI_HARNESS_SHAPE);
46
46
  const OpencodeHarnessSchema = z.strictObject({
47
47
  /** A model key in opencode's catalogue, as "provider/model-id". */
48
48
  model: z.string().optional(),
49
- /** The reasoning-effort variant opencode's agent definition carries (the `--variant` of `opencode run`, which is not a flag of the command pi-cowboy launches). */
49
+ /** The reasoning-effort variant, joined to this agent's model as "provider/model#variant". */
50
50
  variant: z.string().optional(),
51
- /** Tools available to an opencode launch, as opencode's permission keys. */
51
+ /** Tools available to an opencode launch, as opencode's permission actions. */
52
52
  tools: z.array(z.string()).optional(),
53
- /** Tools withheld from an opencode launch, as opencode's permission keys. */
53
+ /** Tools withheld from an opencode launch, as opencode's permission actions. */
54
54
  exclude_tools: z.array(z.string()).optional(),
55
55
  });
56
56
  /**
@@ -156,7 +156,9 @@ const CLAUDE_MODELS = [
156
156
  export const claudeHarness = {
157
157
  id: "claude",
158
158
  agentMuxKind: "claude",
159
- available: () => commandPath("claude") !== undefined,
159
+ availability: () => commandPath("claude") === undefined
160
+ ? { status: "unavailable" }
161
+ : { status: "available" },
160
162
  /**
161
163
  * Stage the config directory, install the launcher that points claude at it
162
164
  * and appends the task, and prove the pane sees the staged state behind it.
@@ -191,7 +191,9 @@ function fetchModels(pi) {
191
191
  export const codexHarness = {
192
192
  id: "codex",
193
193
  agentMuxKind: "codex",
194
- available: () => commandPath("codex") !== undefined,
194
+ availability: () => commandPath("codex") === undefined
195
+ ? { status: "unavailable" }
196
+ : { status: "available" },
195
197
  /**
196
198
  * Install the launcher that appends the task as the TUI's positional
197
199
  * prompt. The launcher is the whole of the pane state this harness needs,
@@ -23,21 +23,19 @@ const AGENT_FILE_SUFFIX = ".md";
23
23
  /**
24
24
  * The frontmatter opencode reads, in the order it is written.
25
25
  *
26
- * `mode: primary` is load-bearing: opencode falls back to its own default agent,
27
- * with no prompt at all, when the named agent is not a primary agent.
28
- * `description` carries the spawn identity, which stays visible in
29
- * `opencode debug agent <id>`.
26
+ * `mode: primary` is load-bearing: opencode drops back to its own default agent
27
+ * — no prompt at all, no rules — for a name that is not a primary agent.
28
+ * `description` carries the spawn identity, which stays visible wherever
29
+ * definitions are listed.
30
30
  *
31
- * `lineWidth: 0` writes each value on one line whatever its length: `variant`
32
- * is model-provided text, and folding a long one would make the scalar's
33
- * emitted style depend on how long it happens to be.
31
+ * `lineWidth: 0` writes each rule on one line whatever a template's action name
32
+ * is, so the name's length cannot decide how the emitter splits a rule.
34
33
  */
35
34
  function frontmatter(input) {
36
35
  return stringify({
37
36
  description: subagentTokenFor(input.subagentId),
38
37
  mode: "primary",
39
- permission: opencodePermissions(input.toolSelection),
40
- ...(input.variant === null ? {} : { variant: input.variant }),
38
+ permissions: opencodePermissions(input.toolSelection),
41
39
  }, { lineWidth: 0 });
42
40
  }
43
41
  /** The agent definition: YAML frontmatter between its delimiters, then the prompt. */
@@ -24,31 +24,42 @@
24
24
  * settings menu must never fail to open when a list command misbehaves. The
25
25
  * integration test is what keeps the verified shape true as opencode changes.
26
26
  *
27
- * The strict `[harness.opencode]` table carries `variant`, which has no argv
28
- * route: `--variant` exists on `opencode run`, not on the top-level command
29
- * pi-cowboy launches, so it rides the agent definition this harness stages.
27
+ * The strict `[harness.opencode]` table carries `variant`, which joins the model
28
+ * reference on the argv: a variant has no flag of its own, so `buildArgs`
29
+ * appends it to the model key as `provider/model#variant`.
30
30
  */
31
- import { commandPath } from "../../../infrastructure/exec-path.js";
32
31
  import { parseModelKey } from "../../../utils.js";
33
- import { installPaneLauncher, singleQuoteShellWord, } from "../pane-launcher.js";
32
+ import { installPaneLauncher, singleQuoteShellWord, splitMarker, } from "../pane-launcher.js";
34
33
  import { removeOpencodeStaging, writeOpencodeStaging } from "./staging.js";
34
+ import { probeOpencode } from "./version.js";
35
35
  /** Budget for the list command; a hung CLI must not hold a menu open. */
36
36
  const LIST_MODELS_TIMEOUT_MS = 10_000;
37
37
  /**
38
- * Budget for the pane's `opencode agent list`. The command boots the CLI, so it
39
- * is slower than a shell builtin but answers in about a second on 1.18.35.
38
+ * Budget for the pane's definition proof. The probe boots the CLI and a private
39
+ * server, so it is slower than a shell builtin but answers in a few seconds.
40
40
  */
41
- const AGENT_LIST_TIMEOUT_MS = 15_000;
41
+ const DEFINITION_PROOF_TIMEOUT_MS = 30_000;
42
42
  /** The command herdr types into the pane; the launcher is installed under it. */
43
43
  const OPENCODE_COMMAND = "opencode";
44
44
  /** Signature carried by this launcher, so pane verification proves which launcher landed. */
45
45
  const LAUNCHER_SIGNATURE = "OPENCODE_LAUNCHER_7d3e91";
46
46
  /** Token the verify command echoes once the launcher body is confirmed. */
47
47
  const LAUNCHER_READY_MARKER = "OPENCODE_VERIFIED";
48
+ /** Token the definition proof echoes once the staged definition is confirmed. */
49
+ const DEFINITION_READY_MARKER = "OPENCODE_DEFINITION_VERIFIED";
48
50
  /**
49
- * The launcher body: opencode's config directory and the two switches that keep
50
- * a spawn from reading anyone else's configuration, then the real binary with
51
- * the pane's own arguments and the task on stdin.
51
+ * The model the definition proof asks for: a key no catalogue can resolve, so
52
+ * the probe is answered by the model rather than by a request.
53
+ */
54
+ const PROOF_MODEL = "pi-cowboy/opencode-definition-proof";
55
+ /** The message the proof submits; it is never sent, because the model is not. */
56
+ const PROOF_MESSAGE = "definition probe";
57
+ /** What opencode reports when it resolved the agent but not the model. */
58
+ const MODEL_FAILURE = "Model unavailable";
59
+ /**
60
+ * The launcher body: opencode's config directory and the switch that keeps a
61
+ * spawn from reading anyone else's configuration, then the real binary with the
62
+ * pane's own arguments and the task on stdin.
52
63
  *
53
64
  * Bare `opencode` keeps the pane's `PATH` the authority on which binary runs,
54
65
  * and `command` bypasses this very function. The redirect is what delivers a
@@ -60,7 +71,6 @@ function launcherBody(configDir, taskFile) {
60
71
  return [
61
72
  `export OPENCODE_CONFIG_DIR=${singleQuoteShellWord(configDir)}`,
62
73
  "export OPENCODE_DISABLE_PROJECT_CONFIG=1",
63
- "export OPENCODE_DISABLE_CLAUDE_CODE_PROMPT=1",
64
74
  `command opencode "$@" < ${singleQuoteShellWord(taskFile)}`,
65
75
  ].join("; ");
66
76
  }
@@ -68,25 +78,44 @@ function launcherBody(configDir, taskFile) {
68
78
  * The proof that the staged definition is live, read back through the launcher
69
79
  * the pane just took.
70
80
  *
71
- * opencode falls back to its own default agent — no system prompt, no
72
- * permission map — when the named agent is missing or is not a primary agent,
73
- * and the fallback is a warning it prints rather than a failure: nothing in the
74
- * launch's argv or exit status reports one. `opencode agent list` is where the
75
- * answer can be read. The list is filtered in the pane to the one line this
76
- * wants: the unfiltered list runs past a thousand lines here (the staged agent
77
- * sorts last), which is deeper than the output herdr's literal `wait-output`
78
- * match searches, and `grep -x` pins the verified `^<subagentId> (primary)$`
79
- * line rather than accepting a mention of the id.
81
+ * opencode runs its own default agent — no system prompt, no permission rules —
82
+ * when the named agent is missing or is not a primary agent, and mini reports
83
+ * that by drawing the requested name over the default agent rather than by
84
+ * failing: nothing in the launch's argv or exit status says one happened. The
85
+ * proof asks a private server to resolve the agent, which is the only place the
86
+ * answer can be read. `--standalone` is load-bearing here: without it the CLI
87
+ * answers from the shared background server, which was started with another
88
+ * config directory and does not hold this definition at all.
89
+ *
90
+ * The probe names a model no catalogue can resolve, so a definition opencode
91
+ * read is reported by the model's failure and one it did not read is reported by
92
+ * the agent's, in that order, without a model request ever being made. The
93
+ * marker rides split, because the pane echoes the command line it was typed and
94
+ * that echo must not be able to answer the proof.
80
95
  */
81
96
  function definitionProof(subagentId) {
82
- const listed = `${subagentId} (primary)`;
97
+ const command = [
98
+ `${OPENCODE_COMMAND} run --standalone`,
99
+ `--agent ${singleQuoteShellWord(subagentId)}`,
100
+ `--model ${singleQuoteShellWord(PROOF_MODEL)}`,
101
+ `${singleQuoteShellWord(PROOF_MESSAGE)} 2>&1`,
102
+ `| grep -q ${singleQuoteShellWord(MODEL_FAILURE)}`,
103
+ `&& echo ${splitMarker(DEFINITION_READY_MARKER)}`,
104
+ ].join(" ");
83
105
  return {
84
- command: `${OPENCODE_COMMAND} agent list | grep -x ${singleQuoteShellWord(listed)}`,
85
- output: listed,
86
- timeoutMs: AGENT_LIST_TIMEOUT_MS,
87
- failure: `failed to prepare the pane: \`${OPENCODE_COMMAND} agent list\` never printed ${listed} — the definition staged for ${subagentId} is missing, unreadable, or not a primary agent, and opencode would fall back to its own default agent. The agent did not start.`,
106
+ command,
107
+ output: DEFINITION_READY_MARKER,
108
+ timeoutMs: DEFINITION_PROOF_TIMEOUT_MS,
109
+ failure: `failed to prepare the pane: \`${OPENCODE_COMMAND} run --standalone --agent ${subagentId}\` never reported the staged definition — the definition staged for ${subagentId} is missing, unreadable, or not a primary agent, and opencode would run its own default agent instead. The agent did not start.`,
88
110
  };
89
111
  }
112
+ /**
113
+ * The model reference opencode takes: the key, with the variant joined to it when
114
+ * one was requested.
115
+ */
116
+ function modelReference(modelKey, variant) {
117
+ return variant === null ? modelKey : `${modelKey}#${variant}`;
118
+ }
90
119
  /** Warns with the `[subagents]` prefix every other config-plane warning carries. */
91
120
  function warnListModels(reason) {
92
121
  console.warn(`[subagents] Could not list the opencode models: ${reason}. The OpenCode Default Model row offers no models until the command answers.`);
@@ -116,7 +145,7 @@ function parseOpencodeModels(stdout) {
116
145
  async function listOpencodeModels(pi) {
117
146
  let stdout;
118
147
  try {
119
- const result = await pi.exec("opencode", ["models"], {
148
+ const result = await pi.exec("opencode", ["models", "--standalone"], {
120
149
  timeout: LIST_MODELS_TIMEOUT_MS,
121
150
  });
122
151
  if (result.code !== 0) {
@@ -164,10 +193,38 @@ function fetchModels(pi) {
164
193
  });
165
194
  return fetch;
166
195
  }
196
+ /**
197
+ * Whether this machine's opencode is one this harness can launch, and the line
198
+ * the human needs when it is not: the argv this harness builds is OpenCode 2's,
199
+ * so a binary of another major answers it with a usage dump rather than an
200
+ * agent. An opencode that is simply not installed stays quiet — the settings row
201
+ * omitting it already says so — while one that is there but reports nothing this
202
+ * can read says that much, because a binary that disappears from the offer with
203
+ * no reason given is worse than the reason itself.
204
+ */
205
+ function opencodeAvailability() {
206
+ const probe = probeOpencode();
207
+ switch (probe.kind) {
208
+ case "supported":
209
+ return { status: "available" };
210
+ case "unsupported":
211
+ return {
212
+ status: "unavailable",
213
+ notice: `You seem to have opencode version ${probe.version} installed, pi-cowboy only supports v2.x.x.`,
214
+ };
215
+ case "unreadable":
216
+ return {
217
+ status: "unavailable",
218
+ notice: "Could not parse the version the opencode on your PATH reports, pi-cowboy only supports v2.x.x.",
219
+ };
220
+ case "absent":
221
+ return { status: "unavailable" };
222
+ }
223
+ }
167
224
  export const opencodeHarness = {
168
225
  id: "opencode",
169
226
  agentMuxKind: "opencode",
170
- available: () => commandPath("opencode") !== undefined,
227
+ availability: opencodeAvailability,
171
228
  /**
172
229
  * Stage the config directory, install the launcher that points opencode at it
173
230
  * and feeds it the task, and prove the staged definition is live behind it. A
@@ -178,7 +235,6 @@ export const opencodeHarness = {
178
235
  const configDir = writeOpencodeStaging({
179
236
  subagentId: launch.subagentId,
180
237
  systemPromptFile: launch.systemPromptFile,
181
- variant: launch.variant,
182
238
  toolSelection: launch.toolSelection,
183
239
  });
184
240
  await installPaneLauncher(pi, paneId, {
@@ -190,15 +246,23 @@ export const opencodeHarness = {
190
246
  });
191
247
  },
192
248
  /**
193
- * `--mini` is the minimal *interactive* UI and is load-bearing: `opencode run`
249
+ * `mini` is the minimal *interactive* UI and is load-bearing: `opencode run`
194
250
  * exits when the session goes idle and never reads the terminal again, so a
195
- * later steer would be typed into the pane's shell. The variant is not here —
196
- * `--variant` exists only on `opencode run`; it rides the staged definition.
251
+ * later steer would be typed into the pane's shell. `--standalone` is
252
+ * load-bearing for the same reason the argv names a subcommand at all: a
253
+ * launch without it reads the shared background server's config directory,
254
+ * where the staged definition is not. A variant has no flag of its own — it
255
+ * joins the model key after `#`.
197
256
  */
198
257
  buildArgs(request) {
199
- const args = ["--mini", "--agent", request.subagentId];
200
- if (request.modelKey !== null)
201
- args.push("--model", request.modelKey);
258
+ const args = ["mini", "--standalone", "--agent", request.subagentId];
259
+ if (request.modelKey === null) {
260
+ if (request.variant !== null) {
261
+ throw new Error(`the opencode variant ${JSON.stringify(request.variant)} has no model to attach to: opencode takes a variant as part of a model reference (\`provider/model#variant\`), and this launch resolved no model. Give the agent an opencode model, or drop the variant.`);
262
+ }
263
+ return args;
264
+ }
265
+ args.push("--model", modelReference(request.modelKey, request.variant));
202
266
  return args;
203
267
  },
204
268
  // opencode reads its own catalog; the injected pi catalog is the pi family's,
@@ -1,48 +1,48 @@
1
1
  /**
2
- * opencode/permissions.ts — this launch's tool selection as opencode's
3
- * permission map.
2
+ * opencode/permissions.ts — this launch's tool selection as opencode's ordered
3
+ * permission rules.
4
4
  *
5
5
  * Names arrive in opencode's own vocabulary — each family's slice is written in
6
6
  * the catalogue that defines it, so a launch reads only its own family's — and
7
- * become permission keys as written, with no translation.
7
+ * become rule actions as written, with no translation.
8
8
  *
9
- * opencode decides whether a tool may run by the last permission rule that
10
- * matches, taken in the map's own key order, so the order is part of the answer:
11
- * the catch-all leads and every specific key follows it.
9
+ * opencode decides whether a tool may run by the last rule that matches, so the
10
+ * order is part of the answer: the catch-all leads and every specific rule
11
+ * follows it.
12
12
  */
13
- /** opencode's catch-all permission key; every specific rule follows it. */
13
+ /** opencode's catch-all action; every specific rule follows it. */
14
14
  const ALL_TOOLS = "*";
15
+ /** Every rule this harness writes matches every resource. */
16
+ const ALL_RESOURCES = "*";
15
17
  /** opencode's permission for asking the user a question. */
16
18
  const QUESTION = "question";
19
+ /** One rule, over every resource. */
20
+ function rule(action, effect) {
21
+ return { action, resource: ALL_RESOURCES, effect };
22
+ }
17
23
  /**
18
- * `ToolSelection` -> opencode's permission map.
24
+ * `ToolSelection` -> opencode's permission rules.
19
25
  *
20
26
  * `question` is denied wherever the catch-all allows — nobody can answer an
21
27
  * approval in a background pane, and opencode's mini mode would wait for one
22
28
  * forever — while a denied catch-all already covers questions.
23
29
  */
24
30
  export function opencodePermissions(toolSelection) {
25
- const permissions = {};
26
31
  switch (toolSelection.kind) {
27
32
  case "default":
28
- permissions[ALL_TOOLS] = "allow";
29
- permissions[QUESTION] = "deny";
30
- return permissions;
33
+ return [rule(ALL_TOOLS, "allow"), rule(QUESTION, "deny")];
31
34
  case "none":
32
- permissions[ALL_TOOLS] = "deny";
33
- return permissions;
35
+ return [rule(ALL_TOOLS, "deny")];
34
36
  case "include":
35
- permissions[ALL_TOOLS] = "deny";
36
- for (const name of toolSelection.names) {
37
- permissions[name] = "allow";
38
- }
39
- return permissions;
37
+ return [
38
+ rule(ALL_TOOLS, "deny"),
39
+ ...toolSelection.names.map((name) => rule(name, "allow")),
40
+ ];
40
41
  case "exclude":
41
- permissions[ALL_TOOLS] = "allow";
42
- permissions[QUESTION] = "deny";
43
- for (const name of toolSelection.names) {
44
- permissions[name] = "deny";
45
- }
46
- return permissions;
42
+ return [
43
+ rule(ALL_TOOLS, "allow"),
44
+ rule(QUESTION, "deny"),
45
+ ...toolSelection.names.map((name) => rule(name, "deny")),
46
+ ];
47
47
  }
48
48
  }
@@ -4,15 +4,13 @@
4
4
  *
5
5
  * opencode launches with `OPENCODE_CONFIG_DIR` naming this directory and with
6
6
  * project config disabled, so everything the spawn gets is what lands here: the
7
- * definition its prompt is read from (definition.ts) and the npm seed that keeps
8
- * opencode from installing its plugin (npm-seed.ts). Nothing here launches
7
+ * definition its prompt is read from (definition.ts). Nothing here launches
9
8
  * anything or knows which pane the process will live in.
10
9
  */
11
10
  import fs from "node:fs";
12
11
  import * as path from "node:path";
13
12
  import { subagentResultDirFor } from "../../../paths.js";
14
13
  import { removeAgentDefinition, writeAgentDefinition } from "./definition.js";
15
- import { removeNpmSeed, writeNpmSeed } from "./npm-seed.js";
16
14
  /** The config directory's name inside a spawn's staging directory. */
17
15
  const CONFIG_DIR_NAME = "opencode";
18
16
  /**
@@ -26,7 +24,7 @@ export function opencodeConfigDir(subagentId) {
26
24
  *
27
25
  * The system prompt is read before anything is created, so an unreadable prompt
28
26
  * file fails the spawn without leaving a half-built tree behind. A definition
29
- * file already in place is overwritten, along with both seed files.
27
+ * file already in place is overwritten.
30
28
  */
31
29
  export function writeOpencodeStaging(input) {
32
30
  const systemPrompt = fs.readFileSync(input.systemPromptFile, "utf-8");
@@ -34,10 +32,8 @@ export function writeOpencodeStaging(input) {
34
32
  writeAgentDefinition(configDir, {
35
33
  subagentId: input.subagentId,
36
34
  systemPrompt,
37
- variant: input.variant,
38
35
  toolSelection: input.toolSelection,
39
36
  });
40
- writeNpmSeed(configDir);
41
37
  return configDir;
42
38
  }
43
39
  /**
@@ -48,7 +44,6 @@ export function writeOpencodeStaging(input) {
48
44
  export function removeOpencodeStaging(subagentId) {
49
45
  const configDir = opencodeConfigDir(subagentId);
50
46
  removeAgentDefinition(configDir, subagentId);
51
- removeNpmSeed(configDir);
52
47
  removeConfigDir(configDir);
53
48
  }
54
49
  /** Remove the directory the staged files lived in. Best effort. */
@@ -0,0 +1,43 @@
1
+ /**
2
+ * opencode/version.ts — what the installed binary reports, and whether this
3
+ * harness speaks it.
4
+ *
5
+ * The harness launches OpenCode 2's CLI (`mini`, `--standalone`, `--model` with
6
+ * a `#variant`), which another major refuses outright. A machine whose
7
+ * `opencode` is not 2.x is therefore not a machine this harness can launch on,
8
+ * and the reason it cannot is the version itself — so the read is judged here,
9
+ * once, and both the verdict and any line for the human come from it.
10
+ */
11
+ import { commandPath } from "../../../infrastructure/exec-path.js";
12
+ import { runSync } from "../../../infrastructure/exec-sync.js";
13
+ /** The major this harness launches: the CLI surface its argv is built against. */
14
+ const SUPPORTED_MAJOR = "2";
15
+ /** `opencode --version` prints one line: the program, a `v`, then the version. */
16
+ const VERSION_LINE = /^opencode v(\d+\.\d+\.\d+)/;
17
+ /** Budget for the read: a binary that hangs must not hold the session's probe. */
18
+ const VERSION_TIMEOUT_MS = 5_000;
19
+ /**
20
+ * The version a `--version` output names, or undefined when it is not that line.
21
+ * Nothing else about the output is read, so a banner added around it costs
22
+ * nothing.
23
+ */
24
+ export function parseOpencodeVersion(output) {
25
+ return VERSION_LINE.exec(output.trim())?.[1];
26
+ }
27
+ /** Whether a parsed version's CLI is the one this harness builds argv against. */
28
+ export function isSupportedOpencodeVersion(version) {
29
+ return version.startsWith(`${SUPPORTED_MAJOR}.`);
30
+ }
31
+ /** Read the installed binary's version. Total: every outcome is one member. */
32
+ export function probeOpencode() {
33
+ const bin = commandPath("opencode");
34
+ if (bin === undefined)
35
+ return { kind: "absent" };
36
+ const output = runSync(bin, ["--version"], VERSION_TIMEOUT_MS);
37
+ const version = output === undefined ? undefined : parseOpencodeVersion(output);
38
+ if (version === undefined)
39
+ return { kind: "unreadable" };
40
+ return isSupportedOpencodeVersion(version)
41
+ ? { kind: "supported", version }
42
+ : { kind: "unsupported", version };
43
+ }
@@ -33,11 +33,11 @@ const INSTALL_MAX_ATTEMPTS = 5;
33
33
  const INSTALL_OUTPUT_TIMEOUT_MS = 2_000;
34
34
  const INSTALL_RETRY_DELAY_MS = 250;
35
35
  /**
36
- * Break the marker with a `""` so only the shell's evaluation of the command —
36
+ * Split a marker with a `""` so that only the shell's evaluation of a command —
37
37
  * not the command line's own echo, which `wait-output` also sees — produces the
38
38
  * contiguous token.
39
39
  */
40
- function splitMarker(marker) {
40
+ export function splitMarker(marker) {
41
41
  const at = marker.indexOf("_");
42
42
  return at === -1
43
43
  ? marker
@@ -89,7 +89,9 @@ export function createPiCompatibleHarness(spec) {
89
89
  // The launcher runs `spec.binary` inside the pane, so a machine without it
90
90
  // can never launch this harness — however well the preparation itself would
91
91
  // go.
92
- available: () => commandPath(spec.binary) !== undefined,
92
+ availability: () => commandPath(spec.binary) === undefined
93
+ ? { status: "unavailable" }
94
+ : { status: "available" },
93
95
  // The binary is pi behind the shell trick, so the argv is pi's; only a
94
96
  // harness that stages filesystem state before launch has teardown work —
95
97
  // the launcher dies with the pane and the staged files belong to the
@@ -21,7 +21,7 @@ import { claudeHarness } from "./claude/index.js";
21
21
  const piHarness = {
22
22
  id: "pi",
23
23
  agentMuxKind: "pi",
24
- available: () => true,
24
+ availability: () => ({ status: "available" }),
25
25
  prepare: async () => { },
26
26
  buildArgs: buildPiLaunchArgs,
27
27
  listModels: piFamilyListModels,
@@ -1,7 +1,7 @@
1
1
  /**
2
- * harness-availability-launch.ts — ask PATH, once at launch, which harnesses this
3
- * machine can launch, and let the shell answer for every later reader; then
4
- * warm the slow model lists through each harness's own hook.
2
+ * harness-availability-launch.ts — ask each harness what this machine can do
3
+ * with it, once at launch, and let the shell answer for every later reader;
4
+ * then warm the slow model lists through each harness's own hook.
5
5
  *
6
6
  * The scan is synchronous, so the answer is in the shell before the first menu can
7
7
  * open and there is no claim to defend: unlike the volume probe, this verdict
@@ -9,21 +9,53 @@
9
9
  * probe is never awaited: a launch must not block on a CLI. Where a warmed
10
10
  * fetch lives is each harness's own business — this module names no harness
11
11
  * beyond iterating the runnable ones, and holds no model state itself.
12
+ *
13
+ * A harness whose binary is installed but cannot be launched answers with a line
14
+ * for the human. That line is shown once the UI exists, so the pass over the
15
+ * vocabulary happens here — once — and its notices are carried to the session
16
+ * that can display them.
12
17
  */
13
18
  import { HARNESS_IDS, runnableHarnessIds, } from "./agents/harness.js";
14
19
  import { harnessFor } from "./agents/harness/registry.js";
15
20
  import { probed } from "./availability.js";
16
21
  import { getHarnessAvailability, setHarnessAvailability } from "./shell.js";
17
22
  /**
18
- * What this machine can launch: each id its own harness reports as launchable.
19
- * Iterating the vocabulary, not a hand-written list, so a harness added to
20
- * `HARNESS_IDS` is judged by the same rule rather than silently dropped.
23
+ * What this machine can do with each harness, in the vocabulary's own order. One
24
+ * question per harness, so a verdict and the line explaining it can never come
25
+ * from two different reads of the same binary.
21
26
  */
22
- export function detectHarnessAvailability() {
23
- return probed(HARNESS_IDS.filter((id) => harnessFor(id).available()));
27
+ export function probeHarnessAvailability() {
28
+ const available = [];
29
+ const notices = [];
30
+ for (const id of HARNESS_IDS) {
31
+ const answer = harnessFor(id).availability();
32
+ if (answer.status === "available")
33
+ available.push(id);
34
+ else if (answer.notice !== undefined)
35
+ notices.push(answer.notice);
36
+ }
37
+ return { availability: probed(available), notices };
38
+ }
39
+ /** Record the probe's answer for every later reader, and queue what it has to say. */
40
+ export function startHarnessAvailabilityProbe(pi) {
41
+ const { availability, notices } = probeHarnessAvailability();
42
+ setHarnessAvailability(availability);
43
+ reportNotices(pi, notices);
24
44
  }
25
- export function startHarnessAvailabilityProbe() {
26
- setHarnessAvailability(detectHarnessAvailability());
45
+ /**
46
+ * Show each line once the UI exists. The entry point has no context, so the
47
+ * session event is the first moment there is somewhere to put it; a run without
48
+ * a UI has nowhere to show a notice and drops it.
49
+ */
50
+ function reportNotices(pi, notices) {
51
+ if (notices.length === 0)
52
+ return;
53
+ pi.on("session_start", (_event, ctx) => {
54
+ if (!ctx.hasUI)
55
+ return;
56
+ for (const notice of notices)
57
+ ctx.ui.notify(notice, "warning");
58
+ });
27
59
  }
28
60
  /**
29
61
  * Warm every runnable harness's slow model list, without naming any harness.
package/dist/index.js CHANGED
@@ -56,8 +56,9 @@ export default function (pi) {
56
56
  registerTools(pi);
57
57
  registerCowboyCommand(pi);
58
58
  setupEventListeners(pi);
59
- // Which harnesses are on PATH decides which stay on offer.
60
- startHarnessAvailabilityProbe();
59
+ // Which harnesses are on PATH — and spoken by a version this extension
60
+ // launches — decides which stay on offer.
61
+ startHarnessAvailabilityProbe(pi);
61
62
  // Slow model lists start warming now, never awaited; every later listing
62
63
  // joins whatever its harness started.
63
64
  prefetchHarnessModels(pi);
@@ -0,0 +1,30 @@
1
+ /**
2
+ * exec-sync.ts — the one place this extension waits for a child process.
3
+ *
4
+ * The harness availability probe is synchronous by design: its verdict is in the
5
+ * shell before the first menu can open, and a binary's version is part of that
6
+ * verdict. Everything else shells out asynchronously through `pi.exec`, so
7
+ * children inherit the session environment; nothing here needs that, because a
8
+ * version read is one word of output from a binary PATH has already resolved.
9
+ */
10
+ import { execFileSync } from "node:child_process";
11
+ /**
12
+ * Run a command and wait for its output, or undefined when it did not answer: a
13
+ * missing binary, a non-zero exit and a timeout are one answer here, because a
14
+ * caller that needs a successful run has no use for a failure's exit code.
15
+ *
16
+ * Stdin is discarded rather than inherited, so a command that reads it cannot
17
+ * wait on a terminal that is not there.
18
+ */
19
+ export function runSync(command, args, timeoutMs) {
20
+ try {
21
+ return execFileSync(command, [...args], {
22
+ encoding: "utf8",
23
+ timeout: timeoutMs,
24
+ stdio: ["ignore", "pipe", "ignore"],
25
+ });
26
+ }
27
+ catch {
28
+ return undefined;
29
+ }
30
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-cowboy",
3
- "version": "0.9.2",
3
+ "version": "0.10.0",
4
4
  "type": "module",
5
5
  "packageManager": "pnpm@12.9.1",
6
6
  "description": "Agent orchestrator for pi: can manage Codex, Claude Code, OpenCode, pi, pig and pi-bolt as subagents. Runs subagents in background Herdr panes, isolated in copy-on-write Git worktrees.",
@@ -1,74 +0,0 @@
1
- /**
2
- * opencode/npm-seed.ts — the fake npm project that keeps opencode from
3
- * installing its plugin into every config directory it loads.
4
- *
5
- * opencode treats a config directory as an npm project: on every load it asks
6
- * npm for `@opencode-ai/plugin`, which is 61 MB of `node_modules` per spawn
7
- * (measured on 1.18.35). The seed is a workaround for that check — an
8
- * undocumented dependency test inside someone else's binary — so it lives alone
9
- * here, with the rule it was derived from, rather than beside anything that
10
- * reads a config directory for its own sake.
11
- */
12
- import fs from "node:fs";
13
- import * as path from "node:path";
14
- import { ensureResultDir, writeResultFile, } from "../../result-file-permissions.js";
15
- /** Where opencode resolves a config directory's dependencies from. */
16
- const NODE_MODULES_DIR_NAME = "node_modules";
17
- /** The npm project opencode's check reads. */
18
- const PACKAGE_FILE_NAME = "package.json";
19
- const LOCK_FILE_NAME = "package-lock.json";
20
- /** The package opencode installs into every config directory it loads. */
21
- const PLUGIN_PACKAGE = "@opencode-ai/plugin";
22
- /**
23
- * The version the seed declares for `PLUGIN_PACKAGE`.
24
- *
25
- * opencode's check runs its install only when `<configDir>/node_modules` is
26
- * missing, or when `package-lock.json`'s root package (`packages[""]`) names
27
- * none of the dependencies that `package.json` and opencode's own `add` list
28
- * declare. Neither file's version is compared — a seed declaring `0.0.1`, or
29
- * `*`, short-circuits exactly like this one — so this pin can fall behind an
30
- * upgraded opencode without costing anything. Should opencode start comparing
31
- * versions, the check fails and opencode installs the version it pins, rewriting
32
- * both files: the same one-time install a directory with no seed at all pays.
33
- */
34
- const PLUGIN_VERSION = "1.18.35";
35
- /**
36
- * The seed manifest: exactly what a real install leaves, without the installed
37
- * bytes.
38
- */
39
- const PACKAGE_JSON = `${JSON.stringify({ dependencies: { [PLUGIN_PACKAGE]: PLUGIN_VERSION } }, null, 2)}\n`;
40
- /**
41
- * The seed lock for a directory. opencode's check reads one thing out of it —
42
- * the root package's declared dependency names — so it carries no resolved tree;
43
- * a real install rewrites it in full. The name is the directory's own, as npm
44
- * writes it.
45
- */
46
- function packageLockJson(configDir) {
47
- return `${JSON.stringify({
48
- name: path.basename(configDir),
49
- lockfileVersion: 3,
50
- requires: true,
51
- packages: { "": { dependencies: { [PLUGIN_PACKAGE]: PLUGIN_VERSION } } },
52
- }, null, 2)}\n`;
53
- }
54
- /** Write the seed into a config directory, creating the module directory it names. */
55
- export function writeNpmSeed(configDir) {
56
- ensureResultDir(path.join(configDir, NODE_MODULES_DIR_NAME));
57
- writeResultFile(path.join(configDir, PACKAGE_FILE_NAME), PACKAGE_JSON);
58
- writeResultFile(path.join(configDir, LOCK_FILE_NAME), packageLockJson(configDir));
59
- }
60
- /** Remove the seed. Best effort, and safe when it is already gone. */
61
- export function removeNpmSeed(configDir) {
62
- try {
63
- fs.rmSync(path.join(configDir, NODE_MODULES_DIR_NAME), {
64
- recursive: true,
65
- force: true,
66
- });
67
- fs.rmSync(path.join(configDir, PACKAGE_FILE_NAME), { force: true });
68
- fs.rmSync(path.join(configDir, LOCK_FILE_NAME), { force: true });
69
- }
70
- catch {
71
- // Best effort: the seed is a temp-dir artifact, like the rest of the spawn's
72
- // staging files.
73
- }
74
- }