@phren/cli 0.3.72 → 0.3.74
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/dist/bridge/agent-hooks.js +11 -1
- package/dist/bridge/browser-env.d.ts +1 -1
- package/dist/bridge/browser-env.js +34 -10
- package/dist/bridge/browser-watch.d.ts +12 -0
- package/dist/bridge/browser-watch.js +83 -0
- package/dist/bridge/claude-account-setup.js +3 -1
- package/dist/bridge/claude-accounts.d.ts +5 -1
- package/dist/bridge/claude-accounts.js +36 -6
- package/dist/bridge/claude-question-dialog.d.ts +2 -3
- package/dist/bridge/claude-question-dialog.js +129 -46
- package/dist/bridge/codex-servers.d.ts +2 -0
- package/dist/bridge/codex-servers.js +1 -1
- package/dist/bridge/command.js +24 -5
- package/dist/bridge/conductor-sets.d.ts +2 -1
- package/dist/bridge/conductor-sets.js +3 -2
- package/dist/bridge/dialog-observation.d.ts +4 -0
- package/dist/bridge/dialog-observation.js +24 -0
- package/dist/bridge/dispatch-clone.d.ts +33 -0
- package/dist/bridge/dispatch-clone.js +176 -0
- package/dist/bridge/dispatch-command.d.ts +14 -0
- package/dist/bridge/dispatch-command.js +98 -13
- package/dist/bridge/dispatch-idempotency.d.ts +85 -0
- package/dist/bridge/dispatch-idempotency.js +235 -0
- package/dist/bridge/dispatch-returns.d.ts +6 -0
- package/dist/bridge/dispatch-returns.js +131 -1
- package/dist/bridge/dispatch-wait.d.ts +32 -0
- package/dist/bridge/dispatch-wait.js +177 -0
- package/dist/bridge/dispatch.d.ts +68 -2
- package/dist/bridge/dispatch.js +839 -25
- package/dist/bridge/fanouts.js +20 -3
- package/dist/bridge/files.d.ts +1 -1
- package/dist/bridge/git-credential.d.ts +44 -0
- package/dist/bridge/git-credential.js +113 -0
- package/dist/bridge/git-host-auth.d.ts +9 -1
- package/dist/bridge/git-host-auth.js +18 -5
- package/dist/bridge/git-host-gitboy.js +8 -1
- package/dist/bridge/git-hosts.d.ts +12 -1
- package/dist/bridge/git-publish.js +3 -2
- package/dist/bridge/herdr.d.ts +4 -0
- package/dist/bridge/herdr.js +29 -3
- package/dist/bridge/install.d.ts +1 -1
- package/dist/bridge/install.js +8 -4
- package/dist/bridge/launch-worktree.d.ts +2 -0
- package/dist/bridge/launch-worktree.js +5 -1
- package/dist/bridge/pane-status.js +2 -19
- package/dist/bridge/protocol.d.ts +7 -0
- package/dist/bridge/schedule-watch.js +13 -2
- package/dist/bridge/server-launch.d.ts +2 -0
- package/dist/bridge/server-launch.js +251 -145
- package/dist/bridge/server-pane-routes.d.ts +1 -1
- package/dist/bridge/server-pane-routes.js +12 -9
- package/dist/bridge/server-routes.js +26 -8
- package/dist/bridge/server.js +12 -1
- package/dist/bridge/status-line-fast.d.ts +9 -0
- package/dist/bridge/status-line-fast.js +56 -0
- package/dist/bridge/terminal-herdr.d.ts +5 -1
- package/dist/bridge/terminal-herdr.js +65 -1
- package/dist/bridge/terminal-tmux.d.ts +6 -0
- package/dist/bridge/terminal-tmux.js +38 -11
- package/dist/bridge/terminal.d.ts +5 -1
- package/dist/bridge/transcript-claude.d.ts +7 -0
- package/dist/bridge/transcript-claude.js +227 -112
- package/dist/bridge/transcript-preview.d.ts +2 -1
- package/dist/bridge/transcript-preview.js +85 -3
- package/dist/bridge/transcripts.js +17 -11
- package/dist/bridge/turn-records.js +16 -4
- package/dist/bridge/usage.d.ts +7 -1
- package/dist/bridge/usage.js +47 -31
- package/dist/bridge/worker-close.d.ts +15 -0
- package/dist/bridge/worker-close.js +304 -3
- package/dist/bridge/worker-identity.d.ts +51 -0
- package/dist/bridge/worker-identity.js +289 -0
- package/dist/bridge-hook.mjs +22097 -19129
- package/dist/cli/browser-hint.d.ts +2 -0
- package/dist/cli/browser-hint.js +21 -0
- package/dist/cli/hooks-context.d.ts +2 -2
- package/dist/cli/hooks-context.js +3 -3
- package/dist/cli/hooks.js +20 -11
- package/dist/cli-registry.js +5 -3
- package/dist/modules/registry.js +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { link, lstat, mkdir, readFile, readdir, unlink, writeFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { atomic, BridgeError, bridgeRoot } from "./protocol.js";
|
|
6
|
+
/**
|
|
7
|
+
* Idempotency keys for local dispatches (`phren dispatch local ... --idempotency-key`).
|
|
8
|
+
* A reservation is written to a temp file and published with `link`, which is
|
|
9
|
+
* atomic and fails with EEXIST: an empty or half-written file is never a
|
|
10
|
+
* reservation another caller can reclaim. Every update or delete checks the
|
|
11
|
+
* owner nonce. The per-key mutex is in-process because only the Hook server
|
|
12
|
+
* process mutates key records; the CLI reaches it through Hook routes.
|
|
13
|
+
*/
|
|
14
|
+
export const IDEMPOTENCY_TTL_MS = 30 * 24 * 60 * 60 * 1000;
|
|
15
|
+
/** A reservation that has not reached "launched" is interrupted after this. */
|
|
16
|
+
export const LAUNCH_INTERRUPTED_MS = 5 * 60_000;
|
|
17
|
+
export const idempotencyKeySchema = z.string().min(1).max(200).regex(/^[A-Za-z0-9:._@/-]+$/, "An idempotency key is 1 to 200 characters of letters, digits, and : . _ @ / -");
|
|
18
|
+
const phaseSchema = z.enum(["reserved", "launching", "launched", "failed"]);
|
|
19
|
+
const recordSchema = z.object({
|
|
20
|
+
key: z.string().min(1).max(200),
|
|
21
|
+
dispatchId: z.string().uuid(),
|
|
22
|
+
owner: z.string().uuid(),
|
|
23
|
+
createdAt: z.string().datetime(),
|
|
24
|
+
phase: phaseSchema,
|
|
25
|
+
project: z.string().max(100).optional(),
|
|
26
|
+
harness: z.string().max(32).optional(),
|
|
27
|
+
label: z.string().max(200).optional(),
|
|
28
|
+
computer: z.string().max(200).optional(),
|
|
29
|
+
cloneDir: z.string().max(4096).optional(),
|
|
30
|
+
gitConfigFile: z.string().max(4096).optional(),
|
|
31
|
+
agentTokenFile: z.string().max(4096).optional(),
|
|
32
|
+
workerFile: z.string().max(4096).optional(),
|
|
33
|
+
startingToken: z.string().regex(/^[a-f0-9]{64}$/).optional(),
|
|
34
|
+
cancelRequestedAt: z.string().datetime().optional(),
|
|
35
|
+
}).strict();
|
|
36
|
+
const keyRoot = () => path.join(bridgeRoot(), "idempotency");
|
|
37
|
+
const hashOf = (key) => createHash("sha256").update(key).digest("hex");
|
|
38
|
+
export const idempotencyKeyFile = (key) => path.join(keyRoot(), `${hashOf(key)}.json`);
|
|
39
|
+
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
40
|
+
function expired(record, now = Date.now()) {
|
|
41
|
+
const created = Date.parse(record.createdAt);
|
|
42
|
+
return Number.isFinite(created) && now - created > IDEMPOTENCY_TTL_MS;
|
|
43
|
+
}
|
|
44
|
+
async function readKey(file) {
|
|
45
|
+
const info = await lstat(file).catch(error => {
|
|
46
|
+
if (error.code !== "ENOENT")
|
|
47
|
+
throw error;
|
|
48
|
+
return undefined;
|
|
49
|
+
});
|
|
50
|
+
if (!info?.isFile() || info.isSymbolicLink() || info.size > 4096)
|
|
51
|
+
return undefined;
|
|
52
|
+
try {
|
|
53
|
+
return recordSchema.parse(JSON.parse(await readFile(file, "utf8")));
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** Publish a full record atomically. False means the key already exists. */
|
|
60
|
+
async function publish(key, record) {
|
|
61
|
+
await mkdir(keyRoot(), { recursive: true, mode: 0o700 });
|
|
62
|
+
const temp = path.join(keyRoot(), `.${hashOf(key)}.${record.owner}.tmp`);
|
|
63
|
+
await writeFile(temp, JSON.stringify(record), { mode: 0o600, flag: "wx" });
|
|
64
|
+
try {
|
|
65
|
+
await link(temp, idempotencyKeyFile(key));
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (error.code === "EEXIST")
|
|
70
|
+
return false;
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
await unlink(temp).catch(() => undefined);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
let hookServerProcess = false;
|
|
78
|
+
/** Called by the Hook server before it handles dispatches. */
|
|
79
|
+
export function markHookServerProcess() { hookServerProcess = true; }
|
|
80
|
+
function assertHookServerProcess() {
|
|
81
|
+
if (!hookServerProcess)
|
|
82
|
+
throw new BridgeError(403, "Idempotency key locks are only available inside the Phren Hook server process.");
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* A per-key async mutex. Only the Hook server process mutates key records (the
|
|
86
|
+
* CLI reaches it through Hook routes), so a Map of promise chains serializes
|
|
87
|
+
* every read-modify-write without a lock file or a reclaim race.
|
|
88
|
+
*/
|
|
89
|
+
const keyMutexes = new Map();
|
|
90
|
+
async function withKeyMutex(key, run) {
|
|
91
|
+
const previous = keyMutexes.get(key) ?? Promise.resolve();
|
|
92
|
+
let release;
|
|
93
|
+
const current = new Promise(resolve => { release = resolve; });
|
|
94
|
+
const tail = previous.then(() => current);
|
|
95
|
+
keyMutexes.set(key, tail);
|
|
96
|
+
await previous;
|
|
97
|
+
try {
|
|
98
|
+
return await run();
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
release();
|
|
102
|
+
if (keyMutexes.get(key) === tail)
|
|
103
|
+
keyMutexes.delete(key);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** Delete lock and reclaim files left by an older Hook or a crash. */
|
|
107
|
+
export async function cleanupIdempotencyLocks() {
|
|
108
|
+
const root = keyRoot();
|
|
109
|
+
const names = await readdir(root).catch(error => {
|
|
110
|
+
if (error.code !== "ENOENT")
|
|
111
|
+
throw error;
|
|
112
|
+
return [];
|
|
113
|
+
});
|
|
114
|
+
await Promise.all(names.filter(name => name.endsWith(".lock") || name.includes(".lock.reclaim-"))
|
|
115
|
+
.map(name => unlink(path.join(root, name)).catch(() => undefined)));
|
|
116
|
+
}
|
|
117
|
+
async function replaceExpired(key, fresh, now) {
|
|
118
|
+
const current = await readKey(idempotencyKeyFile(key));
|
|
119
|
+
if (current && !expired(current, now))
|
|
120
|
+
return { reserved: false, record: current };
|
|
121
|
+
if (current)
|
|
122
|
+
await unlink(idempotencyKeyFile(key)).catch(() => undefined);
|
|
123
|
+
if (await publish(key, fresh))
|
|
124
|
+
return { reserved: true, record: fresh };
|
|
125
|
+
const raced = await readKey(idempotencyKeyFile(key));
|
|
126
|
+
if (!raced)
|
|
127
|
+
throw new BridgeError(503, "The idempotency key's reservation is unreadable; refusing to start another dispatch.");
|
|
128
|
+
return { reserved: false, record: raced };
|
|
129
|
+
}
|
|
130
|
+
/** Reserve `key` for this caller, with its dispatch id, atomically. */
|
|
131
|
+
export async function reserveIdempotencyKey(key, dispatchId, owner, seed = {}, now = Date.now()) {
|
|
132
|
+
return withKeyMutex(key, async () => {
|
|
133
|
+
const record = { key, dispatchId, owner, createdAt: new Date(now).toISOString(), phase: "reserved", ...seed };
|
|
134
|
+
if (await publish(key, record))
|
|
135
|
+
return { reserved: true, record };
|
|
136
|
+
const existing = await readKey(idempotencyKeyFile(key));
|
|
137
|
+
// An unreadable reservation is not free: never unlink it and start a second agent.
|
|
138
|
+
if (!existing)
|
|
139
|
+
throw new BridgeError(503, "The idempotency key's reservation is unreadable; refusing to start another dispatch.");
|
|
140
|
+
if (!expired(existing, now))
|
|
141
|
+
return { reserved: false, record: existing };
|
|
142
|
+
return replaceExpired(key, record, now);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/** Move the reservation's phase. False when this caller no longer owns it. */
|
|
146
|
+
export async function updateIdempotencyPhase(key, owner, phase) {
|
|
147
|
+
return withKeyMutex(key, async () => {
|
|
148
|
+
const file = idempotencyKeyFile(key);
|
|
149
|
+
const record = await readKey(file);
|
|
150
|
+
if (!record || record.owner !== owner)
|
|
151
|
+
return false;
|
|
152
|
+
await atomic(file, JSON.stringify({ ...record, phase }), 0o600);
|
|
153
|
+
return true;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
/** Run under the key's in-process mutex, with the current reservation record. */
|
|
157
|
+
export async function withIdempotencyKeyLock(key, run) {
|
|
158
|
+
assertHookServerProcess();
|
|
159
|
+
return withKeyMutex(key, async () => {
|
|
160
|
+
const file = idempotencyKeyFile(key);
|
|
161
|
+
const record = await readKey(file);
|
|
162
|
+
return await run(record, async (next) => {
|
|
163
|
+
const current = await readKey(file);
|
|
164
|
+
if (!current)
|
|
165
|
+
throw new BridgeError(409, "The idempotency key no longer exists.");
|
|
166
|
+
if (current.owner !== next.owner)
|
|
167
|
+
throw new BridgeError(409, "This dispatch no longer owns its idempotency key.");
|
|
168
|
+
await atomic(file, JSON.stringify(next), 0o600);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
/** Record a cancellation on the reservation itself, under its mutex, before any receipt exists. */
|
|
173
|
+
export async function requestIdempotencyCancel(key, now = Date.now()) {
|
|
174
|
+
return withKeyMutex(key, async () => {
|
|
175
|
+
const file = idempotencyKeyFile(key);
|
|
176
|
+
const record = await readKey(file);
|
|
177
|
+
if (!record)
|
|
178
|
+
return undefined;
|
|
179
|
+
if (record.cancelRequestedAt)
|
|
180
|
+
return record;
|
|
181
|
+
record.cancelRequestedAt = new Date(now).toISOString();
|
|
182
|
+
await atomic(file, JSON.stringify(record), 0o600);
|
|
183
|
+
return record;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/** Release a reservation this caller owns and never launched. */
|
|
187
|
+
export async function releaseIdempotencyKey(key, owner) {
|
|
188
|
+
return withKeyMutex(key, async () => {
|
|
189
|
+
const file = idempotencyKeyFile(key);
|
|
190
|
+
const record = await readKey(file);
|
|
191
|
+
if (!record || record.owner !== owner)
|
|
192
|
+
return false;
|
|
193
|
+
await unlink(file).catch(() => undefined);
|
|
194
|
+
return true;
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
/** The live record for a key, or undefined when there is none or it expired. */
|
|
198
|
+
export async function findIdempotencyKey(key) {
|
|
199
|
+
const record = await readKey(idempotencyKeyFile(key));
|
|
200
|
+
return record && !expired(record) ? record : undefined;
|
|
201
|
+
}
|
|
202
|
+
/** Every live reservation, for reconciliation on Hook start. */
|
|
203
|
+
export async function listIdempotencyRecords() {
|
|
204
|
+
const names = await readdir(keyRoot()).catch(error => {
|
|
205
|
+
if (error.code !== "ENOENT")
|
|
206
|
+
throw error;
|
|
207
|
+
return [];
|
|
208
|
+
});
|
|
209
|
+
const records = [];
|
|
210
|
+
for (const name of names.filter(name => /^[a-f0-9]{64}\.json$/.test(name))) {
|
|
211
|
+
const record = await readKey(path.join(keyRoot(), name));
|
|
212
|
+
if (record && !expired(record))
|
|
213
|
+
records.push(record);
|
|
214
|
+
}
|
|
215
|
+
return records;
|
|
216
|
+
}
|
|
217
|
+
/** Wait for the reserving caller to name its dispatch. Undefined when the key disappears or the wait runs out. */
|
|
218
|
+
export async function waitForIdempotencyKey(key, timeoutMs = 180_000) {
|
|
219
|
+
const deadline = Date.now() + timeoutMs;
|
|
220
|
+
while (Date.now() < deadline) {
|
|
221
|
+
const record = await findIdempotencyKey(key);
|
|
222
|
+
if (!record)
|
|
223
|
+
return undefined;
|
|
224
|
+
if (record.phase !== "reserved")
|
|
225
|
+
return record;
|
|
226
|
+
await sleep(100);
|
|
227
|
+
}
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
/** A branch slug for a key: `agent/<key-slug>` or `agent/<dispatch id>` when there is no key. */
|
|
231
|
+
export function worktreeBranchFor(key, dispatchId) {
|
|
232
|
+
const slug = (key ?? "").toLowerCase().replace(/[^a-z0-9._-]+/g, "-").replace(/\.{2,}/g, ".")
|
|
233
|
+
.replace(/^[-.]+|[-.]+$/g, "").slice(0, 80).replace(/[-.]+$/g, "");
|
|
234
|
+
return `agent/${slug || dispatchId}`;
|
|
235
|
+
}
|
|
@@ -338,6 +338,12 @@ export declare class DispatchReturns {
|
|
|
338
338
|
forwardPrs(): Promise<void>;
|
|
339
339
|
cleanup(): Promise<void>;
|
|
340
340
|
private closeRemote;
|
|
341
|
+
/**
|
|
342
|
+
* Mark one dispatch's return read, leaving every other return untouched:
|
|
343
|
+
* `phren dispatch wait` follows one worker and must not consume a
|
|
344
|
+
* conductor's returns on the same Hook.
|
|
345
|
+
*/
|
|
346
|
+
acknowledgeOne(id: string): Promise<Json>;
|
|
341
347
|
/** A remote successor acknowledges only the exact return version it received. */
|
|
342
348
|
acknowledge(rows: Json[]): Promise<void>;
|
|
343
349
|
/** Every unread return, oldest first, marked read as it is handed over. */
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { intentionallyClosed } from "./worker-close.js";
|
|
2
|
+
import { dispatchClonePaths, removeDispatchClone, removeDispatchSecrets } from "./dispatch-clone.js";
|
|
3
|
+
import { removeWorkerFile, workerAlive } from "./worker-identity.js";
|
|
2
4
|
import { workerPrs, readIntegrator } from "./worker-reports.js";
|
|
3
5
|
import { prsSchema } from "./return-contract.js";
|
|
4
6
|
import { sessionStalls } from "./stalls.js";
|
|
@@ -8,7 +10,7 @@ import { setTimeout as sleep } from "node:timers/promises";
|
|
|
8
10
|
import { promisify } from "node:util";
|
|
9
11
|
import { z } from "zod";
|
|
10
12
|
import { logger } from "../logger.js";
|
|
11
|
-
import { arrivalOf, dispatchStatus, updateReceipt } from "./dispatch.js";
|
|
13
|
+
import { arrivalOf, boundWorkerGone, clearWorkerEnded, dispatchStatus, updateReceipt } from "./dispatch.js";
|
|
12
14
|
import { briefArrival, briefId } from "./launch-brief.js";
|
|
13
15
|
import { codexServers, LOST_TURN } from "./codex-servers.js";
|
|
14
16
|
import { findGrant } from "./grants.js";
|
|
@@ -1014,6 +1016,109 @@ export class DispatchReturns {
|
|
|
1014
1016
|
}
|
|
1015
1017
|
async cleanup() {
|
|
1016
1018
|
for (const receipt of await dispatchStatus()) {
|
|
1019
|
+
const paths = receipt.idempotencyKey
|
|
1020
|
+
? dispatchClonePaths(receipt.id)
|
|
1021
|
+
: { cloneDir: receipt.cloneDir, gitConfigFile: receipt.gitConfigFile, agentTokenFile: receipt.agentTokenFile };
|
|
1022
|
+
const state = receipt.returned?.state;
|
|
1023
|
+
// Only the Hook that created the clone/token owns their filesystem.
|
|
1024
|
+
const local = this.isSelf(receipt) || this.isLocal(receipt.computer);
|
|
1025
|
+
// Keyed dispatches are local-only; never use a peer path for one.
|
|
1026
|
+
if (receipt.idempotencyKey && !local)
|
|
1027
|
+
continue;
|
|
1028
|
+
if (local && (state === "failed" || state === "gone")) {
|
|
1029
|
+
if (receipt.idempotencyKey) {
|
|
1030
|
+
// A keyed worker's durable PID/start time decides, not its return.
|
|
1031
|
+
const alive = await workerAlive(receipt.id, receipt.workerFile ?? dispatchClonePaths(receipt.id).workerFile, receipt.submittedAt);
|
|
1032
|
+
if (alive !== "gone") {
|
|
1033
|
+
await clearWorkerEnded(receipt.id);
|
|
1034
|
+
continue;
|
|
1035
|
+
}
|
|
1036
|
+
await removeDispatchSecrets(paths);
|
|
1037
|
+
await removeWorkerFile(receipt.id);
|
|
1038
|
+
const at = receipt.closedAt ?? new Date(this.now()).toISOString();
|
|
1039
|
+
const until = new Date(Date.parse(at) + 7 * 24 * 60 * 60_000).toISOString();
|
|
1040
|
+
await updateReceipt(receipt.id, value => {
|
|
1041
|
+
if (!value.endedAt)
|
|
1042
|
+
value.endedAt = at;
|
|
1043
|
+
if (!value.retainedUntil)
|
|
1044
|
+
value.retainedUntil = until;
|
|
1045
|
+
return true;
|
|
1046
|
+
}).catch(() => undefined);
|
|
1047
|
+
continue;
|
|
1048
|
+
}
|
|
1049
|
+
// A launch that never reached its request has no worker: its secrets
|
|
1050
|
+
// go immediately. Any other outcome waits for confirmed termination.
|
|
1051
|
+
if (!receipt.target && receipt.launchOutcome === "not-started") {
|
|
1052
|
+
await removeDispatchSecrets(paths);
|
|
1053
|
+
const at = receipt.closedAt ?? new Date(this.now()).toISOString();
|
|
1054
|
+
const until = new Date(Date.parse(at) + 7 * 24 * 60 * 60_000).toISOString();
|
|
1055
|
+
await updateReceipt(receipt.id, value => {
|
|
1056
|
+
if (!value.endedAt)
|
|
1057
|
+
value.endedAt = at;
|
|
1058
|
+
if (!value.retainedUntil)
|
|
1059
|
+
value.retainedUntil = until;
|
|
1060
|
+
return true;
|
|
1061
|
+
}).catch(() => undefined);
|
|
1062
|
+
continue;
|
|
1063
|
+
}
|
|
1064
|
+
// Retention and token removal wait for confirmed termination: a failed
|
|
1065
|
+
// return alone does not prove the pane stopped.
|
|
1066
|
+
if (receipt.endedAt) {
|
|
1067
|
+
const until = receipt.retainedUntil ? Date.parse(receipt.retainedUntil) : NaN;
|
|
1068
|
+
if (!Number.isFinite(until) || this.now() < until)
|
|
1069
|
+
continue;
|
|
1070
|
+
if (!await boundWorkerGone(receipt)) {
|
|
1071
|
+
await clearWorkerEnded(receipt.id);
|
|
1072
|
+
continue;
|
|
1073
|
+
}
|
|
1074
|
+
continue;
|
|
1075
|
+
}
|
|
1076
|
+
if (!await boundWorkerGone(receipt)) {
|
|
1077
|
+
await clearWorkerEnded(receipt.id);
|
|
1078
|
+
continue;
|
|
1079
|
+
}
|
|
1080
|
+
await removeDispatchSecrets(paths);
|
|
1081
|
+
const at = receipt.closedAt ?? new Date(this.now()).toISOString();
|
|
1082
|
+
const until = new Date(Date.parse(at) + 7 * 24 * 60 * 60_000).toISOString();
|
|
1083
|
+
await updateReceipt(receipt.id, value => {
|
|
1084
|
+
if (!value.endedAt)
|
|
1085
|
+
value.endedAt = at;
|
|
1086
|
+
if (!value.retainedUntil)
|
|
1087
|
+
value.retainedUntil = until;
|
|
1088
|
+
return true;
|
|
1089
|
+
}).catch(() => undefined);
|
|
1090
|
+
continue;
|
|
1091
|
+
}
|
|
1092
|
+
if (local && state === "done") {
|
|
1093
|
+
if (receipt.idempotencyKey) {
|
|
1094
|
+
if (receipt.closedAt) {
|
|
1095
|
+
await removeDispatchClone(paths);
|
|
1096
|
+
continue;
|
|
1097
|
+
}
|
|
1098
|
+
if (receipt.closeOnFinish === false) {
|
|
1099
|
+
await clearWorkerEnded(receipt.id);
|
|
1100
|
+
continue;
|
|
1101
|
+
}
|
|
1102
|
+
const alive = await workerAlive(receipt.id, receipt.workerFile ?? dispatchClonePaths(receipt.id).workerFile, receipt.submittedAt);
|
|
1103
|
+
if (alive === "gone") {
|
|
1104
|
+
await removeDispatchClone(paths);
|
|
1105
|
+
continue;
|
|
1106
|
+
}
|
|
1107
|
+
await clearWorkerEnded(receipt.id);
|
|
1108
|
+
}
|
|
1109
|
+
else {
|
|
1110
|
+
if (receipt.closedAt) {
|
|
1111
|
+
await removeDispatchClone(paths);
|
|
1112
|
+
continue;
|
|
1113
|
+
}
|
|
1114
|
+
if (receipt.closeOnFinish === false) {
|
|
1115
|
+
// Keep-open work stays alive; it has no termination time yet.
|
|
1116
|
+
await clearWorkerEnded(receipt.id);
|
|
1117
|
+
continue;
|
|
1118
|
+
}
|
|
1119
|
+
// A done worker's pane may still run: keep its token until the verified close.
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1017
1122
|
if (!receipt.closePending || receipt.closedAt || receipt.returned?.at !== receipt.closePending.at || receipt.returned.state !== "done" || !receipt.returned.read)
|
|
1018
1123
|
continue;
|
|
1019
1124
|
const peer = this.isSelf(receipt) ? undefined : (await this.peers().catch(() => [])).find(peer => peer.name === receipt.computer);
|
|
@@ -1033,11 +1138,36 @@ export class DispatchReturns {
|
|
|
1033
1138
|
}
|
|
1034
1139
|
}
|
|
1035
1140
|
async closeRemote(receipt, verifiedPeer) {
|
|
1141
|
+
// Keyed dispatches are local-only; never close one over a peer path.
|
|
1142
|
+
if (receipt.idempotencyKey)
|
|
1143
|
+
return { closed: false };
|
|
1036
1144
|
const peer = verifiedPeer ?? (await this.peers()).find(peer => peer.name === receipt.computer);
|
|
1037
1145
|
if (!peer)
|
|
1038
1146
|
return { closed: false };
|
|
1039
1147
|
return this.request(peer, "/v1/dispatch/close", { target: receipt.target, dispatch: receipt.moves?.slice().reverse().find(move => move.to.computer)?.id ?? receipt.id, turn: receipt.closePending.turn });
|
|
1040
1148
|
}
|
|
1149
|
+
/**
|
|
1150
|
+
* Mark one dispatch's return read, leaving every other return untouched:
|
|
1151
|
+
* `phren dispatch wait` follows one worker and must not consume a
|
|
1152
|
+
* conductor's returns on the same Hook.
|
|
1153
|
+
*/
|
|
1154
|
+
async acknowledgeOne(id) {
|
|
1155
|
+
let acknowledged = false;
|
|
1156
|
+
await updateReceipt(id, value => {
|
|
1157
|
+
if (!value.returned || value.returned.read)
|
|
1158
|
+
return false;
|
|
1159
|
+
value.returned.read = true;
|
|
1160
|
+
if (value.returned.state === "done" && value.closeOnFinish !== false && !value.returned.background && value.returned.turn && !value.closedAt)
|
|
1161
|
+
value.closePending = { at: value.returned.at, turn: value.returned.turn };
|
|
1162
|
+
acknowledged = true;
|
|
1163
|
+
return true;
|
|
1164
|
+
}).catch(() => undefined);
|
|
1165
|
+
if (acknowledged) {
|
|
1166
|
+
await this.forwardPrs();
|
|
1167
|
+
await this.cleanup();
|
|
1168
|
+
}
|
|
1169
|
+
return { ok: true, dispatch: id, acknowledged };
|
|
1170
|
+
}
|
|
1041
1171
|
/** A remote successor acknowledges only the exact return version it received. */
|
|
1042
1172
|
async acknowledge(rows) {
|
|
1043
1173
|
for (const row of rows)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { hookRequest } from "./client.js";
|
|
2
|
+
import type { Json } from "./protocol.js";
|
|
3
|
+
/**
|
|
4
|
+
* `phren dispatch wait`: follow one local dispatch to its end. It peeks at the
|
|
5
|
+
* Hook's returns (never taking them), filters to this dispatch, and reads the
|
|
6
|
+
* worker's live state to tell a gone pane from one still working. The output
|
|
7
|
+
* is NDJSON: one line per state change, then one final line. Only the final
|
|
8
|
+
* dispatch's own return is acknowledged, so a conductor's returns on the same
|
|
9
|
+
* Hook stay unread.
|
|
10
|
+
*/
|
|
11
|
+
/** How long an id the Hook does not know gets before it is "unknown dispatch". */
|
|
12
|
+
export declare const WAIT_GRACE_MS = 30000;
|
|
13
|
+
/** Continuous Hook unavailability before the wait ends failed. */
|
|
14
|
+
export declare const WAIT_UNAVAILABLE_MS: number;
|
|
15
|
+
/** Longest wait a caller may ask for. */
|
|
16
|
+
export declare const WAIT_MAX_MS: number;
|
|
17
|
+
export interface DispatchWaitOptions {
|
|
18
|
+
/** No timeout by default. Capped at seven days. */
|
|
19
|
+
timeoutMs?: number;
|
|
20
|
+
request?: typeof hookRequest;
|
|
21
|
+
out?: (line: Json) => void;
|
|
22
|
+
now?: () => number;
|
|
23
|
+
sleep?: (ms: number) => Promise<void>;
|
|
24
|
+
/** First poll interval; it backs off to 10 s. */
|
|
25
|
+
intervalMs?: number;
|
|
26
|
+
jitter?: boolean;
|
|
27
|
+
graceMs?: number;
|
|
28
|
+
unavailableMs?: number;
|
|
29
|
+
}
|
|
30
|
+
/** About 2 s at first, backing off to 10 s, with jitter. */
|
|
31
|
+
export declare function waitDelay(attempt: number, baseMs?: number, jitter?: boolean): number;
|
|
32
|
+
export declare function waitForDispatch(id: string, options?: DispatchWaitOptions): Promise<number>;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { setTimeout as sleepTimer } from "node:timers/promises";
|
|
2
|
+
import { hookRequest } from "./client.js";
|
|
3
|
+
/**
|
|
4
|
+
* `phren dispatch wait`: follow one local dispatch to its end. It peeks at the
|
|
5
|
+
* Hook's returns (never taking them), filters to this dispatch, and reads the
|
|
6
|
+
* worker's live state to tell a gone pane from one still working. The output
|
|
7
|
+
* is NDJSON: one line per state change, then one final line. Only the final
|
|
8
|
+
* dispatch's own return is acknowledged, so a conductor's returns on the same
|
|
9
|
+
* Hook stay unread.
|
|
10
|
+
*/
|
|
11
|
+
/** How long an id the Hook does not know gets before it is "unknown dispatch". */
|
|
12
|
+
export const WAIT_GRACE_MS = 30_000;
|
|
13
|
+
/** Continuous Hook unavailability before the wait ends failed. */
|
|
14
|
+
export const WAIT_UNAVAILABLE_MS = 5 * 60_000;
|
|
15
|
+
/** Longest wait a caller may ask for. */
|
|
16
|
+
export const WAIT_MAX_MS = 7 * 24 * 60 * 60 * 1000;
|
|
17
|
+
function objects(value) {
|
|
18
|
+
return Array.isArray(value) ? value.filter((item) => !!item && typeof item === "object" && !Array.isArray(item)) : [];
|
|
19
|
+
}
|
|
20
|
+
function object(value) {
|
|
21
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
22
|
+
}
|
|
23
|
+
function reason(error) {
|
|
24
|
+
const message = error instanceof Error ? error.message.split("\n")[0] : String(error);
|
|
25
|
+
return message.replace(/[\x00-\x1f\x7f]/g, " ").slice(0, 200);
|
|
26
|
+
}
|
|
27
|
+
function questionOf(value) {
|
|
28
|
+
if (!value)
|
|
29
|
+
return undefined;
|
|
30
|
+
if (typeof value.question === "string" && value.question)
|
|
31
|
+
return value.question;
|
|
32
|
+
const approval = object(value.approval);
|
|
33
|
+
for (const key of ["request", "title", "tool"])
|
|
34
|
+
if (typeof approval[key] === "string" && approval[key])
|
|
35
|
+
return String(approval[key]);
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
/** About 2 s at first, backing off to 10 s, with jitter. */
|
|
39
|
+
export function waitDelay(attempt, baseMs = 2_000, jitter = true) {
|
|
40
|
+
const target = Math.min(10_000, baseMs * 1.5 ** Math.max(0, attempt - 1));
|
|
41
|
+
return Math.max(1, Math.round(target * (jitter ? 0.8 + Math.random() * 0.4 : 1)));
|
|
42
|
+
}
|
|
43
|
+
export async function waitForDispatch(id, options = {}) {
|
|
44
|
+
const request = options.request ?? hookRequest;
|
|
45
|
+
const out = options.out ?? ((line) => console.log(JSON.stringify(line)));
|
|
46
|
+
const now = options.now ?? Date.now;
|
|
47
|
+
const sleep = options.sleep ?? ((ms) => sleepTimer(ms));
|
|
48
|
+
const base = options.intervalMs ?? 2_000;
|
|
49
|
+
const jitter = options.jitter ?? true;
|
|
50
|
+
const grace = options.graceMs ?? WAIT_GRACE_MS;
|
|
51
|
+
const unavailableLimit = options.unavailableMs ?? WAIT_UNAVAILABLE_MS;
|
|
52
|
+
const timeoutMs = options.timeoutMs === undefined ? undefined : Math.min(Math.max(0, options.timeoutMs), WAIT_MAX_MS);
|
|
53
|
+
const started = now();
|
|
54
|
+
let lastState;
|
|
55
|
+
let unknownSince;
|
|
56
|
+
let failingSince;
|
|
57
|
+
let lastWorkingAt;
|
|
58
|
+
let attempt = 0;
|
|
59
|
+
const emit = (state, extra = {}) => {
|
|
60
|
+
if (state === lastState)
|
|
61
|
+
return;
|
|
62
|
+
lastState = state;
|
|
63
|
+
out({ dispatch: id, state, ...extra, at: new Date(now()).toISOString() });
|
|
64
|
+
};
|
|
65
|
+
const final = (state, extra = {}) => out({ dispatch: id, state, final: true, ...extra });
|
|
66
|
+
for (;;) {
|
|
67
|
+
if (timeoutMs !== undefined && now() - started >= timeoutMs) {
|
|
68
|
+
final("timeout");
|
|
69
|
+
return 124;
|
|
70
|
+
}
|
|
71
|
+
let receipt, returned, worker, peeked;
|
|
72
|
+
let listError, peekError, workerError;
|
|
73
|
+
try {
|
|
74
|
+
const list = await request("/v1/dispatch");
|
|
75
|
+
receipt = objects(list.dispatches).find(row => row.id === id);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
listError = error;
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
const peek = await request("/v1/dispatch/returns?peek=1", {});
|
|
82
|
+
peeked = objects(peek.returns).find(row => row.id === id);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
peekError = error;
|
|
86
|
+
}
|
|
87
|
+
if (!listError) {
|
|
88
|
+
if (receipt)
|
|
89
|
+
unknownSince = undefined;
|
|
90
|
+
else if (unknownSince === undefined)
|
|
91
|
+
unknownSince = now();
|
|
92
|
+
}
|
|
93
|
+
const fromReceipt = receipt?.returned ? object(receipt.returned) : undefined;
|
|
94
|
+
// A return the conductor already read is still this dispatch's latest;
|
|
95
|
+
// use whichever of the peek and the receipt names the newer one.
|
|
96
|
+
returned = fromReceipt && String(fromReceipt.at ?? "") > String(peeked?.at ?? "") ? fromReceipt : peeked ?? fromReceipt;
|
|
97
|
+
// Live state is read on every poll, even when a return exists: a historical
|
|
98
|
+
// needs-you return must not mask a worker that has resumed.
|
|
99
|
+
if (receipt?.target) {
|
|
100
|
+
try {
|
|
101
|
+
const answer = await request("/v1/dispatch/workers", { targets: [{ ...object(receipt.target), dispatch: id }] });
|
|
102
|
+
worker = objects(answer.workers)[0];
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
workerError = error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (listError || peekError || workerError) {
|
|
109
|
+
if (failingSince === undefined)
|
|
110
|
+
failingSince = now();
|
|
111
|
+
else if (now() - failingSince >= unavailableLimit) {
|
|
112
|
+
final("failed", { error: `Phren Hook unavailable: ${reason(workerError ?? peekError ?? listError)}` });
|
|
113
|
+
return 1;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else
|
|
117
|
+
failingSince = undefined;
|
|
118
|
+
if (!listError && !receipt && !returned && unknownSince !== undefined && now() - unknownSince >= grace) {
|
|
119
|
+
final("failed", { error: "unknown dispatch" });
|
|
120
|
+
return 1;
|
|
121
|
+
}
|
|
122
|
+
const acknowledge = async () => {
|
|
123
|
+
const own = receipt?.returned ? object(receipt.returned) : undefined;
|
|
124
|
+
if (!peeked && own?.read !== false)
|
|
125
|
+
return;
|
|
126
|
+
await request("/v1/dispatch/returns/ack", { dispatch: id }).catch(() => undefined);
|
|
127
|
+
};
|
|
128
|
+
// A placement that failed before any worker existed has no return of its own.
|
|
129
|
+
if (!returned && receipt?.state === "failed") {
|
|
130
|
+
final("failed", { ...(typeof receipt.error === "string" ? { error: receipt.error } : {}) });
|
|
131
|
+
return 1;
|
|
132
|
+
}
|
|
133
|
+
const state = typeof returned?.state === "string" ? returned.state : undefined;
|
|
134
|
+
if (state === "done") {
|
|
135
|
+
final("done", { ...(typeof returned.reply === "string" ? { final_reply: returned.reply } : {}), ...(returned.prs ? { prs: returned.prs } : {}) });
|
|
136
|
+
await acknowledge();
|
|
137
|
+
return 0;
|
|
138
|
+
}
|
|
139
|
+
if (state === "failed") {
|
|
140
|
+
final("failed", { ...(typeof returned.error === "string" ? { error: returned.error } : {}) });
|
|
141
|
+
await acknowledge();
|
|
142
|
+
return 1;
|
|
143
|
+
}
|
|
144
|
+
if (state === "gone") {
|
|
145
|
+
final("gone");
|
|
146
|
+
await acknowledge();
|
|
147
|
+
return 1;
|
|
148
|
+
}
|
|
149
|
+
const workerState = typeof worker?.state === "string" ? worker.state : typeof object(receipt?.worker).state === "string" ? String(object(receipt?.worker).state) : undefined;
|
|
150
|
+
if (workerState === "working")
|
|
151
|
+
lastWorkingAt = now();
|
|
152
|
+
const returnedAt = Date.parse(String(returned?.at ?? ""));
|
|
153
|
+
// A needs-you return from before the worker was last seen working is historical.
|
|
154
|
+
const needsYou = (state === "needs-you" || state === "blocked")
|
|
155
|
+
&& (lastWorkingAt === undefined || !Number.isFinite(returnedAt) || returnedAt >= lastWorkingAt);
|
|
156
|
+
if (workerState === "working") {
|
|
157
|
+
emit("running");
|
|
158
|
+
}
|
|
159
|
+
else if (workerState === "gone" || workerState === "closed") {
|
|
160
|
+
final("gone");
|
|
161
|
+
return 1;
|
|
162
|
+
}
|
|
163
|
+
else if (needsYou) {
|
|
164
|
+
const question = questionOf(returned);
|
|
165
|
+
emit("needs_you", question ? { question } : {});
|
|
166
|
+
}
|
|
167
|
+
else if (workerState === "needs-you" || workerState === "blocked") {
|
|
168
|
+
const question = questionOf(worker);
|
|
169
|
+
emit("needs_you", question ? { question } : {});
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
emit("running");
|
|
173
|
+
}
|
|
174
|
+
attempt++;
|
|
175
|
+
await sleep(waitDelay(attempt, base, jitter));
|
|
176
|
+
}
|
|
177
|
+
}
|