@henryqw/pi-herdr 0.4.7 → 0.4.9
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/README.md +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +61 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ const herdr = createHerdrClient(pi.exec.bind(pi));
|
|
|
21
21
|
const response = await herdr.json(["agent", "list"], { cwd: ctx.cwd });
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
-
For a ready-to-use Pi workflow, see [`@henryqw/pi-herdr-
|
|
24
|
+
For a ready-to-use Pi workflow, see [`@henryqw/pi-herdr-tools`](https://pi.henry.wang/extensions/pi-herdr-tools).
|
|
25
25
|
|
|
26
26
|
## API
|
|
27
27
|
|
|
@@ -31,6 +31,6 @@ For a ready-to-use Pi workflow, see [`@henryqw/pi-herdr-clone`](https://pi.henry
|
|
|
31
31
|
| `run` | function | Requires a successful exit and returns stdout. |
|
|
32
32
|
| `json` | function | Requires a successful exit and parses a JSON object. |
|
|
33
33
|
| `hasHerdrErrorCode` | function | Detects structured CLI errors in stdout or stderr. |
|
|
34
|
-
| `startPiAgent` | function | Validates and builds the shared `agent start --kind pi --pane ... --` boundary. It retries structured `agent_pane_busy` responses allowed by the caller's result-aware policy, with a five-attempt limit, and returns the raw final result. |
|
|
34
|
+
| `startPiAgent` | function | Validates and builds the shared `agent start --kind pi --pane ... --` boundary. Before each start it checks with `pane process-info` that the pane shell still owns the foreground process group, runs `pane run <pane> "echo <token>"`, and polls `pane read` for up to 20 seconds until the token appears as its own output line with a prompt drawn after it. That proves the shell is reading input; without it, a launch command longer than 1024 bytes typed into a shell that is still starting or running a prompt hook loses its Enter on macOS and the pane waits forever. An occupied pane is not probed, so `agent start` still reports `agent_pane_busy`. A failed probe is returned as the result without starting the agent. These commands exist in Herdr 0.7.4 and later. It retries structured `agent_pane_busy` responses allowed by the caller's result-aware policy, with a five-attempt limit, and returns the raw final result. |
|
|
35
35
|
|
|
36
36
|
Callers build Pi-specific arguments and validate responses. The client does not mirror the Herdr command catalog. Herdr remains the source of truth for supported commands and response shapes.
|
package/dist/index.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ export interface StartPiAgentOptions<Options> {
|
|
|
20
20
|
onPaneBusy?: () => Promise<string>;
|
|
21
21
|
shouldRetry?: (result: HerdrExecResult) => boolean;
|
|
22
22
|
}
|
|
23
|
-
/** Start a Pi agent in a Herdr pane, retrying only transient pane contention. */
|
|
23
|
+
/** Start a Pi agent in a Herdr pane once its shell is reading input, retrying only transient pane contention. */
|
|
24
24
|
export declare function startPiAgent<Options>(client: Pick<HerdrClient<Options>, "exec">, input: StartPiAgentOptions<Options>): Promise<HerdrExecResult>;
|
|
25
25
|
export declare function createHerdrClient<Options>(execute: HerdrExecutor<Options>): HerdrClient<Options>;
|
|
26
26
|
export declare function herdrCommandFailure(args: readonly string[], result: HerdrExecResult): string;
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,65 @@ import { lock } from "proper-lockfile";
|
|
|
2
2
|
const delay = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
3
3
|
const MAX_AGENT_START_ATTEMPTS = 5;
|
|
4
4
|
const AGENT_START_RETRY_DELAY_MS = 250;
|
|
5
|
-
|
|
5
|
+
const SHELL_PROMPT_TIMEOUT_MS = 20_000;
|
|
6
|
+
const SHELL_PROMPT_POLL_MS = 100;
|
|
7
|
+
const PANE_READ_LINES = "40";
|
|
8
|
+
/**
|
|
9
|
+
* Whether `pane process-info` shows the pane shell still owning the foreground process group. Startup
|
|
10
|
+
* file children share the shell's group, while a job started from the prompt (such as a Pi agent)
|
|
11
|
+
* runs in its own group.
|
|
12
|
+
*/
|
|
13
|
+
function shellOwnsForeground(stdout) {
|
|
14
|
+
let parsed;
|
|
15
|
+
try {
|
|
16
|
+
parsed = JSON.parse(stdout);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
throw new Error("herdr pane process-info returned invalid JSON");
|
|
20
|
+
}
|
|
21
|
+
const info = parsed.result?.process_info;
|
|
22
|
+
if (!info || typeof info.shell_pid !== "number")
|
|
23
|
+
throw new Error("herdr pane process-info returned malformed output");
|
|
24
|
+
return info.foreground_process_group_id === info.shell_pid;
|
|
25
|
+
}
|
|
26
|
+
/** Whether the screen shows the probe token as its own output line with a prompt drawn after it. */
|
|
27
|
+
function shellPromptFollows(screen, token) {
|
|
28
|
+
const lines = screen.split("\n");
|
|
29
|
+
const index = lines.findIndex((line) => line.trim() === token);
|
|
30
|
+
return index >= 0 && lines.slice(index + 1).some((line) => line.trim());
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Prove the pane shell is reading input before `agent start` types a long command. A shell still
|
|
34
|
+
* running its startup files or a prompt hook leaves the tty in canonical mode, where macOS caps one
|
|
35
|
+
* pending input line at 1024 bytes and silently drops the Enter of a longer launch command. The
|
|
36
|
+
* prompt is drawn only when the shell is about to read, so the probe waits for the token output and
|
|
37
|
+
* a non-blank line after it. An occupied pane is left untouched so `agent start` reports
|
|
38
|
+
* `agent_pane_busy` itself. Returns the failed Herdr result, or undefined when ready.
|
|
39
|
+
*/
|
|
40
|
+
async function awaitShellPrompt(client, pane, options, wait) {
|
|
41
|
+
const info = await client.exec(["pane", "process-info", "--pane", pane], options);
|
|
42
|
+
if (info.code !== 0 || info.killed)
|
|
43
|
+
return info;
|
|
44
|
+
if (!shellOwnsForeground(info.stdout))
|
|
45
|
+
return undefined;
|
|
46
|
+
const token = `pi-herdr-ready-${Math.random().toString(16).slice(2, 18).padEnd(16, "0")}`;
|
|
47
|
+
const ran = await client.exec(["pane", "run", pane, `echo ${token}`], options);
|
|
48
|
+
if (ran.code !== 0 || ran.killed)
|
|
49
|
+
return ran;
|
|
50
|
+
const deadline = Date.now() + SHELL_PROMPT_TIMEOUT_MS;
|
|
51
|
+
for (;;) {
|
|
52
|
+
const read = await client.exec(["pane", "read", pane, "--source", "recent-unwrapped", "--lines", PANE_READ_LINES, "--format", "text"], options);
|
|
53
|
+
if (read.code !== 0 || read.killed)
|
|
54
|
+
return read;
|
|
55
|
+
if (shellPromptFollows(read.stdout, token))
|
|
56
|
+
return undefined;
|
|
57
|
+
if (Date.now() >= deadline) {
|
|
58
|
+
return { code: 1, stdout: "", stderr: `pane ${pane} did not draw a shell prompt within ${SHELL_PROMPT_TIMEOUT_MS}ms after the readiness probe` };
|
|
59
|
+
}
|
|
60
|
+
await wait(SHELL_PROMPT_POLL_MS);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/** Start a Pi agent in a Herdr pane once its shell is reading input, retrying only transient pane contention. */
|
|
6
64
|
export async function startPiAgent(client, input) {
|
|
7
65
|
const name = nonEmptyString(input.name, "Herdr Pi agent name");
|
|
8
66
|
let pane = nonEmptyString(input.pane, "Herdr Pi agent pane");
|
|
@@ -13,8 +71,8 @@ export async function startPiAgent(client, input) {
|
|
|
13
71
|
let onPaneBusy = input.onPaneBusy;
|
|
14
72
|
let result;
|
|
15
73
|
for (let attempt = 1; attempt <= MAX_AGENT_START_ATTEMPTS; attempt += 1) {
|
|
16
|
-
|
|
17
|
-
|
|
74
|
+
result = await awaitShellPrompt(client, pane, input.options, input.delay ?? delay)
|
|
75
|
+
?? await client.exec(["agent", "start", name, "--kind", "pi", "--pane", pane, "--", ...piArgs], input.options);
|
|
18
76
|
if (result.code === 0 && !result.killed)
|
|
19
77
|
return result;
|
|
20
78
|
if (!hasHerdrErrorCode(result, "agent_pane_busy")
|