@henryqw/pi-herdr 0.6.1 → 0.6.3
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 +11 -1
- package/dist/admission.d.ts +1 -1
- package/dist/admission.js +10 -9
- package/dist/index.js +16 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -52,12 +52,22 @@ Errors expose `code` and `dispatched`. If `dispatched` is true, a mutation can h
|
|
|
52
52
|
|
|
53
53
|
### Checkout admission
|
|
54
54
|
|
|
55
|
-
`checkoutAdmission(pi)` finds one coordinator through Pi's public event bus. Call `install(pi)` to register tool lifecycle guards.
|
|
55
|
+
`checkoutAdmission(pi)` finds one coordinator through Pi's public event bus. Call `install(pi)` to register tool lifecycle guards.
|
|
56
|
+
|
|
57
|
+
`guard(name, refresh)` installs an awaited recovery check before writes. The callback receives the exact canonical checkout set after target resolution and before any member is admitted. `reserve` passes its one checkout to the same checks. If recovery fails, no member is admitted. `reserve(owner, checkout)` returns a release function only when no admitted writer or Main write is active. `restore(owner, checkout)` restores durable ownership; `release(owner)` requires caller-proved completion.
|
|
56
58
|
|
|
57
59
|
`registerCheckoutAdmission(pi, directCanWrite)` keeps the direct-subagent policy: Main writes can overlap, but direct writers must wait for them and exclude other writes. A returned hold function keeps a direct request admitted beyond its tool result. Consumers must restore durable command ownership before allowing writes after restart. These mechanisms do not intercept external processes or provide a sandbox.
|
|
58
60
|
|
|
59
61
|
An optional third argument, `directCheckouts(input, ctx)`, returns additional canonical checkout paths for a direct request. Admission reserves the current checkout and the complete selected set together. A conflict in one member blocks the request without a partial reservation. The caller validates repository selection; the hold function releases the set only after caller-proved worker termination.
|
|
60
62
|
|
|
63
|
+
### Path locks
|
|
64
|
+
|
|
65
|
+
`withWorktreeLock(checkout, operation)` locks a path while the operation runs. Acquisition, heartbeat updates, and release use synchronous filesystem calls. Slow storage can block the event loop. The operation can remain asynchronous. A heartbeat cannot remain in flight when the helper releases the lock.
|
|
66
|
+
|
|
67
|
+
If a heartbeat detects lost ownership, the call rejects after the operation finishes and does not release the changed lock. If the operation also fails, an `AggregateError` contains both errors. Acquisition, operation, and release errors remain visible. The helper does not stop or undo the operation.
|
|
68
|
+
|
|
69
|
+
Heartbeats do not prove continuous ownership or detect all external lock removal or replacement between checks. Do not remove active lock files. Long event-loop pauses can delay heartbeats and permit stale-lock recovery by another process. A lock error does not prove that the operation had no effect. Inspect state before a retry.
|
|
70
|
+
|
|
61
71
|
### `startPiAgent`
|
|
62
72
|
|
|
63
73
|
On macOS, a shell that starts or runs a prompt hook can lose Enter for a launch command longer than 1024 bytes. The pane then waits forever. To prevent this, the helper first checks `pane process-info`. When the pane shell owns the foreground process group, it runs `pane run <pane> "echo <token>"` and polls `pane read` for up to 20 seconds. The probe passes when the token appears on its own line, with a prompt after it.
|
package/dist/admission.d.ts
CHANGED
|
@@ -41,7 +41,7 @@ declare function createAdmission(): {
|
|
|
41
41
|
classifyDirect: (classify: (input: unknown) => boolean, select?: ((input: unknown, ctx: {
|
|
42
42
|
cwd: string;
|
|
43
43
|
}) => Promise<readonly string[]>) | undefined) => void;
|
|
44
|
-
guard: (name: string, refresh: () => Promise<void>) => void;
|
|
44
|
+
guard: (name: string, refresh: (checkouts: readonly string[]) => Promise<void>) => void;
|
|
45
45
|
restore: (owner: string, checkout: string) => void;
|
|
46
46
|
release: (owner: string) => void;
|
|
47
47
|
reserve(owner: string, checkout: string): Promise<() => void>;
|
package/dist/admission.js
CHANGED
|
@@ -50,18 +50,12 @@ function createAdmission() {
|
|
|
50
50
|
if (ownerByCheckout.get(key) === toolCallId)
|
|
51
51
|
ownerByCheckout.delete(key);
|
|
52
52
|
};
|
|
53
|
-
const refresh = async () => { for (const guard of guards.values())
|
|
54
|
-
await guard(); };
|
|
53
|
+
const refresh = async (keys) => { for (const guard of guards.values())
|
|
54
|
+
await guard(keys); };
|
|
55
55
|
const admit = async (event, ctx, host) => {
|
|
56
56
|
const direct = event.toolName === "delegate_task";
|
|
57
57
|
if (event.toolName === "background_command" || (direct ? !directCanWrite(event.input) : MAIN_NON_WRITING_TOOLS.has(event.toolName)))
|
|
58
58
|
return;
|
|
59
|
-
try {
|
|
60
|
-
await refresh();
|
|
61
|
-
}
|
|
62
|
-
catch {
|
|
63
|
-
return { block: true, reason: "Command ownership recovery failed; writes are blocked. Inspect /commands." };
|
|
64
|
-
}
|
|
65
59
|
const call = { direct };
|
|
66
60
|
calls.set(event.toolCallId, call);
|
|
67
61
|
let keys;
|
|
@@ -72,6 +66,13 @@ function createAdmission() {
|
|
|
72
66
|
release(event.toolCallId);
|
|
73
67
|
throw error;
|
|
74
68
|
}
|
|
69
|
+
try {
|
|
70
|
+
await refresh(keys);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
release(event.toolCallId);
|
|
74
|
+
return { block: true, reason: "Command ownership recovery failed; writes are blocked. Inspect /commands." };
|
|
75
|
+
}
|
|
75
76
|
if (calls.get(event.toolCallId) !== call) {
|
|
76
77
|
return { block: true, reason: `Call ${event.toolCallId} already settled; use mode: isolated to keep Main free.` };
|
|
77
78
|
}
|
|
@@ -122,7 +123,7 @@ function createAdmission() {
|
|
|
122
123
|
restore: (owner, checkout) => { durable.set(owner, checkout); },
|
|
123
124
|
release: (owner) => { durable.delete(owner); },
|
|
124
125
|
async reserve(owner, checkout) {
|
|
125
|
-
await refresh();
|
|
126
|
+
await refresh([checkout]);
|
|
126
127
|
const conflict = [...durable].find(([, key]) => key === checkout)?.[0]
|
|
127
128
|
?? ownerByCheckout.get(checkout)
|
|
128
129
|
?? [...calls].find(([, call]) => call.keys?.includes(checkout))?.[0];
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { lockSync } from "proper-lockfile";
|
|
2
2
|
export { requestHerdr, HerdrRequestError } from "./socket.js";
|
|
3
3
|
export { checkoutAdmission, checkoutKey, registerCheckoutAdmission } from "./admission.js";
|
|
4
4
|
const delay = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
@@ -139,12 +139,24 @@ export function hasHerdrErrorCode(result, expected) {
|
|
|
139
139
|
}
|
|
140
140
|
/** Lock a Herdr worktree checkout path while mutating it via the Herdr CLI. */
|
|
141
141
|
export async function withWorktreeLock(checkout, operation) {
|
|
142
|
-
|
|
142
|
+
let compromised;
|
|
143
|
+
// Synchronous heartbeats cannot remain in flight when release removes the lock.
|
|
144
|
+
const release = lockSync(checkout, { onCompromised: (error) => { compromised = error; } });
|
|
143
145
|
try {
|
|
144
|
-
|
|
146
|
+
const result = await operation();
|
|
147
|
+
if (compromised)
|
|
148
|
+
throw compromised;
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
if (compromised && error !== compromised)
|
|
153
|
+
throw new AggregateError([error, compromised], "Operation failed and lock ownership was lost.");
|
|
154
|
+
throw error;
|
|
145
155
|
}
|
|
146
156
|
finally {
|
|
147
|
-
|
|
157
|
+
// A compromised lock is no longer ours to release.
|
|
158
|
+
if (!compromised)
|
|
159
|
+
release();
|
|
148
160
|
}
|
|
149
161
|
}
|
|
150
162
|
function herdrCommandName(args) {
|