@henryqw/pi-herdr 0.3.1 → 0.4.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.
- package/README.md +1 -1
- package/dist/index.d.ts +11 -0
- package/dist/index.js +39 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ const herdr = createHerdrClient(pi.exec.bind(pi));
|
|
|
22
22
|
const response = await herdr.json(["agent", "list"], { cwd: ctx.cwd });
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
`exec` validates string argv and returns the raw process result. `run` requires a successful exit and returns stdout. `json` also parses a JSON object. `hasHerdrErrorCode` detects structured CLI errors in stdout or stderr.
|
|
25
|
+
`exec` validates string argv and returns the raw process result. `run` requires a successful exit and returns stdout. `json` also parses a JSON object. `hasHerdrErrorCode` detects structured CLI errors in stdout or stderr. `startPiAgent` validates and builds the shared `agent start --kind pi --pane ... --` boundary, retries structured `agent_pane_busy` responses allowed by the caller's result-aware policy within a five-attempt limit, and returns the raw final result; callers still build Pi-specific args and validate responses.
|
|
26
26
|
|
|
27
27
|
Client does not mirror the Herdr command catalog. Herdr remains the source of truth for supported commands and response shapes.
|
|
28
28
|
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,17 @@ export interface HerdrClient<Options> {
|
|
|
11
11
|
run(args: readonly string[], options: Options): Promise<string>;
|
|
12
12
|
json(args: readonly string[], options: Options): Promise<Record<string, unknown>>;
|
|
13
13
|
}
|
|
14
|
+
export interface StartPiAgentOptions<Options> {
|
|
15
|
+
name: string;
|
|
16
|
+
pane: string;
|
|
17
|
+
args: readonly string[];
|
|
18
|
+
options: Options;
|
|
19
|
+
delay?: (milliseconds: number) => Promise<void>;
|
|
20
|
+
onPaneBusy?: () => Promise<string>;
|
|
21
|
+
shouldRetry?: (result: HerdrExecResult) => boolean;
|
|
22
|
+
}
|
|
23
|
+
/** Start a Pi agent in a Herdr pane, retrying only transient pane contention. */
|
|
24
|
+
export declare function startPiAgent<Options>(client: Pick<HerdrClient<Options>, "exec">, input: StartPiAgentOptions<Options>): Promise<HerdrExecResult>;
|
|
14
25
|
export declare function createHerdrClient<Options>(execute: HerdrExecutor<Options>): HerdrClient<Options>;
|
|
15
26
|
export declare function herdrCommandFailure(args: readonly string[], result: HerdrExecResult): string;
|
|
16
27
|
export declare function hasHerdrErrorCode(result: Pick<HerdrExecResult, "stdout" | "stderr">, expected: string): boolean;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,38 @@
|
|
|
1
1
|
import { lock } from "proper-lockfile";
|
|
2
|
+
const delay = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
3
|
+
const MAX_AGENT_START_ATTEMPTS = 5;
|
|
4
|
+
const AGENT_START_RETRY_DELAY_MS = 250;
|
|
5
|
+
/** Start a Pi agent in a Herdr pane, retrying only transient pane contention. */
|
|
6
|
+
export async function startPiAgent(client, input) {
|
|
7
|
+
const name = nonEmptyString(input.name, "Herdr Pi agent name");
|
|
8
|
+
let pane = nonEmptyString(input.pane, "Herdr Pi agent pane");
|
|
9
|
+
if (!Array.isArray(input.args) || input.args.some((arg) => typeof arg !== "string")) {
|
|
10
|
+
throw new TypeError("Herdr Pi agent arguments must be an array of strings");
|
|
11
|
+
}
|
|
12
|
+
const piArgs = [...input.args];
|
|
13
|
+
let onPaneBusy = input.onPaneBusy;
|
|
14
|
+
let result;
|
|
15
|
+
for (let attempt = 1; attempt <= MAX_AGENT_START_ATTEMPTS; attempt += 1) {
|
|
16
|
+
const args = ["agent", "start", name, "--kind", "pi", "--pane", pane, "--", ...piArgs];
|
|
17
|
+
result = await client.exec(args, input.options);
|
|
18
|
+
if (result.code === 0 && !result.killed)
|
|
19
|
+
return result;
|
|
20
|
+
if (!hasHerdrErrorCode(result, "agent_pane_busy")
|
|
21
|
+
|| attempt === MAX_AGENT_START_ATTEMPTS
|
|
22
|
+
|| input.shouldRetry?.(result) === false)
|
|
23
|
+
return result;
|
|
24
|
+
if (onPaneBusy) {
|
|
25
|
+
pane = nonEmptyString(await onPaneBusy(), "Herdr Pi agent pane returned by onPaneBusy");
|
|
26
|
+
onPaneBusy = undefined;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
await (input.delay ?? delay)(AGENT_START_RETRY_DELAY_MS);
|
|
30
|
+
}
|
|
31
|
+
if (input.shouldRetry?.(result) === false)
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
2
36
|
export function createHerdrClient(execute) {
|
|
3
37
|
const exec = async (args, options) => {
|
|
4
38
|
if (!Array.isArray(args) || args.some((arg) => typeof arg !== "string")) {
|
|
@@ -56,6 +90,11 @@ export async function withWorktreeLock(checkout, operation) {
|
|
|
56
90
|
function herdrCommandName(args) {
|
|
57
91
|
return ["herdr", ...args.slice(0, 2)].join(" ");
|
|
58
92
|
}
|
|
93
|
+
function nonEmptyString(value, label) {
|
|
94
|
+
if (typeof value !== "string" || !value.trim())
|
|
95
|
+
throw new TypeError(`${label} must be a non-empty string`);
|
|
96
|
+
return value;
|
|
97
|
+
}
|
|
59
98
|
function containsErrorCode(value, expected) {
|
|
60
99
|
if (!value || typeof value !== "object")
|
|
61
100
|
return false;
|