@phren/cli 0.3.51 → 0.3.53

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.
@@ -0,0 +1,25 @@
1
+ import type { StorePullIdentity } from "./store-pull.js";
2
+ export interface WebhookStatus {
3
+ configured: boolean;
4
+ listening: boolean;
5
+ listenPort?: number;
6
+ problem?: string;
7
+ lastDeliveryAt?: string;
8
+ lastOutcome?: string;
9
+ }
10
+ /** Optional private HTTP ingress. Invalid config disables only this listener, never the Hook. */
11
+ export declare class GitboyWebhooks {
12
+ private readonly root;
13
+ private readonly pull;
14
+ private readonly nudge;
15
+ readonly status: WebhookStatus;
16
+ private server?;
17
+ private readonly deliveries;
18
+ private readonly bodies;
19
+ private readonly nudges;
20
+ private closed;
21
+ constructor(root: string, pull: (storeId: string) => string | undefined, nudge?: (identity: StorePullIdentity) => Promise<void>);
22
+ private notify;
23
+ start(): Promise<void>;
24
+ stop(): void;
25
+ }
@@ -0,0 +1,200 @@
1
+ import { lstat, readFile } from "node:fs/promises";
2
+ import { createHash } from "node:crypto";
3
+ import { createServer } from "node:http";
4
+ import path from "node:path";
5
+ import { z } from "zod";
6
+ import { DeliveryLog, parseGitboyPush, storePushMatches, verifyGitboySignature } from "../sync/gitboy-webhook.js";
7
+ import { optionalHookPeers, peerRequest } from "./peers.js";
8
+ const storeSchema = z.object({
9
+ storeId: z.string().min(1).max(128), owner: z.string().min(1).max(256), name: z.string().min(1).max(256),
10
+ branch: z.string().min(1).max(1024), secret: z.string().min(1).max(4096).optional(), secretFile: z.string().min(1).optional(),
11
+ }).strict().refine(store => Boolean(store.secret) !== Boolean(store.secretFile));
12
+ const configSchema = z.object({ listenPort: z.number().int().min(1).max(65535), stores: z.array(storeSchema).min(1).max(64) }).strict();
13
+ async function privateText(file, cap) {
14
+ const info = await lstat(file);
15
+ // POSIX permission bits carry no meaning on Windows (ACLs); there only file type and size are checked.
16
+ if (!info.isFile() || info.isSymbolicLink() || info.size > cap || (process.platform !== "win32" && (info.mode & 0o077))
17
+ || (process.getuid && info.uid !== process.getuid()))
18
+ throw new Error("Unsafe private webhook file.");
19
+ return readFile(file, "utf8");
20
+ }
21
+ const BODY_CAP = 1024 * 1024;
22
+ function readBody(request) {
23
+ return new Promise((resolve, reject) => {
24
+ const chunks = [];
25
+ let size = 0;
26
+ const cleanup = () => { clearTimeout(timer); request.off("data", data); request.off("end", end); request.off("error", error); request.off("aborted", aborted); };
27
+ const fail = (status) => { cleanup(); request.resume(); reject(status); };
28
+ const data = (chunk) => { size += chunk.length; if (size > BODY_CAP)
29
+ fail(413);
30
+ else
31
+ chunks.push(chunk); };
32
+ const end = () => { cleanup(); resolve(Buffer.concat(chunks)); };
33
+ const error = () => fail(400);
34
+ const aborted = () => fail(400);
35
+ const timer = setTimeout(() => fail(408), 5000);
36
+ request.on("data", data);
37
+ request.once("end", end);
38
+ request.once("error", error);
39
+ request.once("aborted", aborted);
40
+ if (Number(request.headers["content-length"]) > BODY_CAP)
41
+ fail(413);
42
+ });
43
+ }
44
+ /** All peers come from the private, verified directory; peerRequest pins SSH host keys. */
45
+ async function nudgePeers(identity) {
46
+ const { peers } = await optionalHookPeers();
47
+ await Promise.allSettled(peers.map(peer => peerRequest(peer, "/v1/store/pull-now", { ...identity }, 15_000)));
48
+ }
49
+ /** Optional private HTTP ingress. Invalid config disables only this listener, never the Hook. */
50
+ export class GitboyWebhooks {
51
+ root;
52
+ pull;
53
+ nudge;
54
+ status = { configured: false, listening: false };
55
+ server;
56
+ deliveries = new DeliveryLog();
57
+ bodies = new DeliveryLog();
58
+ nudges = new Map();
59
+ closed = false;
60
+ constructor(root, pull, nudge = nudgePeers) {
61
+ this.root = root;
62
+ this.pull = pull;
63
+ this.nudge = nudge;
64
+ }
65
+ notify(store) {
66
+ // Configuration may name a local attachment. Only the ID carried by that store is portable.
67
+ let storeId = store.storeId;
68
+ try {
69
+ storeId = this.pull(storeId) ?? storeId;
70
+ }
71
+ catch { /* Peers may still have the configured portable ID. */ }
72
+ const identity = { storeId, repository: `${store.owner}/${store.name}`.toLowerCase(), branch: store.branch };
73
+ let state = this.nudges.get(storeId);
74
+ if (!state) {
75
+ state = { running: false, pending: false, last: -Infinity, identity };
76
+ this.nudges.set(storeId, state);
77
+ }
78
+ state.identity = identity;
79
+ state.pending = true;
80
+ const schedule = () => {
81
+ if (state.running || state.timer || this.closed)
82
+ return;
83
+ state.timer = setTimeout(() => {
84
+ state.timer = undefined;
85
+ if (this.closed)
86
+ return;
87
+ state.pending = false;
88
+ state.running = true;
89
+ state.last = Date.now();
90
+ void this.nudge(state.identity).catch(() => { }).finally(() => {
91
+ state.running = false;
92
+ if (state.pending)
93
+ schedule();
94
+ });
95
+ }, Math.max(0, state.last + 10_000 - Date.now()));
96
+ state.timer.unref();
97
+ };
98
+ schedule();
99
+ }
100
+ async start() {
101
+ const file = path.join(this.root, "gitboy-webhooks.json");
102
+ try {
103
+ await lstat(file);
104
+ }
105
+ catch (error) {
106
+ if (error.code === "ENOENT")
107
+ return;
108
+ this.status.problem = "Webhook config unavailable.";
109
+ return;
110
+ }
111
+ this.status.configured = true;
112
+ try {
113
+ const config = configSchema.parse(JSON.parse(await privateText(file, 65_536)));
114
+ const stores = await Promise.all(config.stores.map(async (store) => ({ ...store,
115
+ secret: store.secret ?? (await privateText(path.resolve(this.root, store.secretFile), 4096)).trim(),
116
+ })));
117
+ if (stores.some(store => !store.secret))
118
+ throw new Error("Empty secret.");
119
+ const server = createServer({ maxHeaderSize: 16_384 }, (request, response) => {
120
+ const reply = (status, outcome) => {
121
+ this.status.lastDeliveryAt = new Date().toISOString();
122
+ this.status.lastOutcome = outcome;
123
+ response.writeHead(status, { "Connection": "close", "Cache-Control": "no-store" });
124
+ response.end();
125
+ };
126
+ void (async () => {
127
+ if (request.method !== "POST" || request.url !== "/gitboy/push") {
128
+ reply(404, "not-found");
129
+ return;
130
+ }
131
+ if (![`127.0.0.1:${config.listenPort}`, `localhost:${config.listenPort}`].includes(request.headers.host ?? "")
132
+ || request.headers.origin !== undefined || Object.keys(request.headers).some(name => name.startsWith("sec-fetch-"))) {
133
+ reply(403, "browser-refused");
134
+ return;
135
+ }
136
+ if (!/^application\/json(?:\s*;.*)?$/i.test(request.headers["content-type"] ?? "")) {
137
+ reply(415, "content-type-refused");
138
+ return;
139
+ }
140
+ const raw = await readBody(request);
141
+ const signature = request.headers["x-hub-signature-256"];
142
+ // Authenticate the raw bytes against EVERY secret before parsing or traversing the payload.
143
+ const verified = stores.filter(store => verifyGitboySignature(store.secret, raw, typeof signature === "string" ? signature : undefined));
144
+ if (!verified.length) {
145
+ reply(401, "unauthorized");
146
+ return;
147
+ }
148
+ const push = parseGitboyPush(raw, request.headers);
149
+ if (!push?.repository) {
150
+ reply(400, "malformed");
151
+ return;
152
+ }
153
+ const candidates = verified.filter(store => store.owner.toLowerCase() === push.repository.owner.toLowerCase()
154
+ && store.name.toLowerCase() === push.repository.name.toLowerCase());
155
+ if (!candidates.length) {
156
+ reply(401, "unauthorized");
157
+ return;
158
+ }
159
+ const repo = push.repository;
160
+ const duplicateId = this.deliveries.seen(JSON.stringify([repo.owner.toLowerCase(), repo.name.toLowerCase(), push.deliveryId]));
161
+ // Per verified store set: the same body signed with another stores secret is that stores delivery.
162
+ const storeKey = candidates.map(store => store.storeId).sort().join("\u0000");
163
+ const duplicateBody = this.bodies.seen(`${storeKey}\u0000${createHash("sha256").update(raw).digest("hex")}`);
164
+ if (duplicateId || duplicateBody) {
165
+ reply(200, "duplicate");
166
+ return;
167
+ }
168
+ const matching = candidates.filter(store => storePushMatches(push, store));
169
+ if (!matching.length) {
170
+ reply(200, "non-matching");
171
+ return;
172
+ }
173
+ reply(202, "accepted");
174
+ setImmediate(() => { if (!this.closed)
175
+ for (const store of matching)
176
+ this.notify(store); });
177
+ })().catch(error => reply(typeof error === "number" ? error : 400, typeof error === "number" && error === 413 ? "too-large" : "malformed"));
178
+ });
179
+ this.server = server;
180
+ server.requestTimeout = 6000;
181
+ server.headersTimeout = 5000;
182
+ server.maxHeadersCount = 32;
183
+ await new Promise((resolve, reject) => { server.once("error", reject); server.listen(config.listenPort, "127.0.0.1", resolve); });
184
+ server.on("error", () => { this.status.listening = false; this.status.problem = "Webhook listener failed."; });
185
+ this.status.listening = true;
186
+ this.status.listenPort = config.listenPort;
187
+ }
188
+ catch {
189
+ this.status.problem = "Webhook config invalid, unsafe, or port unavailable.";
190
+ }
191
+ }
192
+ stop() {
193
+ this.closed = true;
194
+ this.status.listening = false;
195
+ for (const state of this.nudges.values())
196
+ clearTimeout(state.timer);
197
+ this.server?.close();
198
+ this.server?.closeAllConnections();
199
+ }
200
+ }
@@ -26,6 +26,7 @@ import { optionalHookPeers } from "./peers.js";
26
26
  import { AppServerRpcError, AppServerUnsupportedError } from "./codex-app-server.js";
27
27
  import { codexAppServerEnabled, codexServers } from "./codex-servers.js";
28
28
  import { logger } from "../logger.js";
29
+ import { stripTerminal } from "../terminal-text.js";
29
30
  import { JobRegistry } from "./job-registry.js";
30
31
  import { atomic, BridgeError, id, launchEfforts, objects, PERMISSION_MODES, provider } from "./protocol.js";
31
32
  import { CLAUDE_NAMES, CODEX_MODES, codexModeFlags, copilotModeFlags } from "./settings-switch.js";
@@ -237,6 +238,61 @@ async function startWhenShellReady(server, pane, agent) {
237
238
  }
238
239
  }
239
240
  }
241
+ /** Retry only a demonstrated exit, and serialize starters: neither terminal
242
+ * provider supports cancelling a start already submitted to its server. */
243
+ async function startCodex(server, pane, agent) {
244
+ const terminal = terminalProvider();
245
+ const screenText = async () => stripTerminal(await terminal.readScreen?.(server, pane, { scope: "pane", source: "recent", lines: 20 }).catch(() => "") ?? "").trim();
246
+ for (let attempt = 0; attempt < 2; attempt++) {
247
+ const before = await screenText();
248
+ const deadline = Date.now() + agent.timeoutMs;
249
+ let ran = false;
250
+ let result;
251
+ const starting = startWhenShellReady(server, pane, agent).then(() => { result = {}; ran = true; }, error => { result = { error }; });
252
+ try {
253
+ for (;;) {
254
+ await Promise.race([starting, new Promise(resolve => setTimeout(resolve, 250))]);
255
+ const processes = await terminal.processes?.(server, pane).catch(() => undefined);
256
+ const state = await terminal.snapshot(server);
257
+ const current = objects(state.panes).find(row => row.pane_id === pane);
258
+ const shell = processes?.shellPid;
259
+ const atShell = processes !== undefined && shell !== undefined && processes.foregroundPids.every(pid => pid === shell);
260
+ if (processes && !atShell && current?.agent === "codex")
261
+ ran = true;
262
+ if (atShell) {
263
+ const screen = await screenText();
264
+ const exitMessage = screen !== before && /please restart codex|codex exited(?: at start)?/i.test(screen);
265
+ if (ran || exitMessage) {
266
+ // A screen or process observation cannot cancel a pending start.
267
+ // Wait for it, then observe again: it might still launch Codex.
268
+ if (!result) {
269
+ await starting;
270
+ continue;
271
+ }
272
+ if (attempt === 0)
273
+ break;
274
+ const last = screen.split(/\r?\n/).map(line => line.trim()).filter(Boolean).at(-1);
275
+ throw new BridgeError(409, `Codex exited at start: ${last || "process returned to its shell"}`);
276
+ }
277
+ }
278
+ if (result && "error" in result)
279
+ throw result.error;
280
+ if (result) {
281
+ if (!processes || ["idle", "working", "blocked", "waiting"].includes(String(current?.agent_status)) || Date.now() >= deadline)
282
+ return;
283
+ await new Promise(resolve => setTimeout(resolve, 250));
284
+ }
285
+ else if (Date.now() >= deadline) {
286
+ await starting;
287
+ }
288
+ }
289
+ }
290
+ finally {
291
+ // Even a failed process/snapshot read must not leave a starter behind.
292
+ await starting;
293
+ }
294
+ }
295
+ }
240
296
  /** The Herdr agent-name slug for a human label: "Conductor smoke 4" becomes "conductor-smoke-4". */
241
297
  export function herdrAgentName(label) {
242
298
  const slug = label.toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^[^a-z]+/, "").replace(/-+$/, "").slice(0, 32).replace(/-+$/, "");
@@ -494,13 +550,16 @@ async function startSession(server, data, options) {
494
550
  briefTurn = error instanceof AppServerRpcError ? undefined : "uncertain";
495
551
  }
496
552
  }
497
- const agentArgs = structuredLaunch ? structuredLaunch.args : [...args, ...(briefLaunch ?? [])];
553
+ const agentArgs = [...(structuredLaunch ? structuredLaunch.args : [...args, ...(briefLaunch ?? [])]),
554
+ ...(kind === "codex" ? ["-c", "check_for_update_on_startup=false"] : [])];
498
555
  try {
499
- await startWhenShellReady(server, created.paneId, { name, kind, args: agentArgs, timeoutMs: timeout, ...(env ? { env } : {}) });
556
+ await (kind === "codex" ? startCodex : startWhenShellReady)(server, created.paneId, { name, kind, args: agentArgs, timeoutMs: timeout, ...(env ? { env } : {}) });
500
557
  }
501
558
  catch (error) {
502
559
  if (appServer && !agentNotReady(error))
503
560
  await codexServers.stop(appServer).catch(() => undefined);
561
+ if (error instanceof BridgeError && error.message.startsWith("Codex exited at start:"))
562
+ throw error;
504
563
  // A first-run screen (Claude's folder trust, a login notice) holds the
505
564
  // agent at startup. It did start: hand the pane back so the owner answers
506
565
  // that screen from the chat instead of stranding the workspace.
@@ -1,3 +1,5 @@
1
+ import type { StorePulls } from "./store-pull.js";
2
+ import type { GitboyWebhooks } from "./gitboy-webhooks.js";
1
3
  import { type SessionMover } from "./session-move.js";
2
4
  import type { OwnerInbox } from "./owner-inbox.js";
3
5
  import type { HandOffQueue } from "./hand-off-queue.js";
@@ -52,6 +54,8 @@ export interface RouteContext {
52
54
  computerID: string;
53
55
  scheduleStore: string;
54
56
  scheduler?: Scheduler;
57
+ storePulls?: StorePulls;
58
+ gitboyWebhooks?: GitboyWebhooks;
55
59
  dispatches?: DispatchService;
56
60
  returns?: DispatchReturns;
57
61
  handOffs?: HandOffQueue;
@@ -362,6 +362,13 @@ export function createRouteHandler(ctx) {
362
362
  ? await projectHostToken(modules.store, project, await journal.recent(), await body(request))
363
363
  : await projectGit(modules.store, project, await journal.recent());
364
364
  }
365
+ else if (url.pathname === "/v1/store/pull-now") {
366
+ if (request.method !== "POST")
367
+ throw new BridgeError(405, "Unsupported request method.");
368
+ if (!ctx.storePulls)
369
+ throw new BridgeError(503, "Store pulls unavailable.");
370
+ result = await ctx.storePulls.route(request, await body(request));
371
+ }
365
372
  else if (url.pathname.startsWith("/v1/store/")) {
366
373
  result = await storeRoute(modules.store, request.method ?? "", url, request.method === "POST" ? await body(request) : undefined);
367
374
  }
@@ -382,7 +389,7 @@ export function createRouteHandler(ctx) {
382
389
  case "/v1/health/details":
383
390
  result = { ...await healthDetails({ hookVersion: version, computerId: computerID, store: scheduleStore,
384
391
  scheduler: scheduler ? { running: true, lastTickAt: scheduler.lastTickAt } : undefined, push: agentHooks.push.status }),
385
- gitWrites: gitWrites.snapshot() };
392
+ gitboyWebhooks: ctx.gitboyWebhooks?.status ?? { configured: false, listening: false }, gitWrites: gitWrites.snapshot() };
386
393
  break;
387
394
  case "/v1/health/peers": {
388
395
  // A peer's health probe asks whether this computer lists it back.
@@ -1,3 +1,5 @@
1
+ import { StorePulls } from "./store-pull.js";
2
+ import { GitboyWebhooks } from "./gitboy-webhooks.js";
1
3
  import { setChoiceReaders } from "./account-choice.js";
2
4
  import { AccountFailover } from "./account-failover.js";
3
5
  import { ownerInboxSources } from "./owner-inbox-sources.js";
@@ -218,6 +220,8 @@ export async function serve(version, options = {}) {
218
220
  throw new Error("Phren Hook is already running.");
219
221
  await unlink(socketPath());
220
222
  }
223
+ const storePulls = new StorePulls(scheduleStore);
224
+ const gitboyWebhooks = new GitboyWebhooks(root, storeId => storePulls.request(storeId));
221
225
  const stopRetention = modules.has("git") ? await startChangeRetention() : () => { };
222
226
  if (modules.has("git"))
223
227
  await rm(path.join(root, "changes-scratch"), { recursive: true, force: true });
@@ -234,7 +238,7 @@ export async function serve(version, options = {}) {
234
238
  // 256 KiB: gitboy's /memory/files asks about up to 500 paths in one query string.
235
239
  const http = createServer({ maxHeaderSize: 256 * 1024 }, createRouteHandler({ version, modules, info, computerID, scheduleStore, scheduler, dispatches, agentHooks,
236
240
  journal, tabActivity, contextUsage, modelCatalog, modelSwitcher, settingsSwitcher, permissionModeSwitcher, sideQuestions, accountUsage, resources, codexQuestions, launches, locatedDirectories,
237
- fanoutMessages, canary, streams, returns, handOffs, inbox, mover }));
241
+ fanoutMessages, canary, streams, returns, handOffs, inbox, mover, storePulls, gitboyWebhooks }));
238
242
  http.requestTimeout = 20_000;
239
243
  http.headersTimeout = 10_000;
240
244
  http.maxHeadersCount = 32;
@@ -287,6 +291,7 @@ export async function serve(version, options = {}) {
287
291
  });
288
292
  await new Promise((resolve, reject) => { http.once("error", reject); http.listen(socketPath(), () => resolve()); });
289
293
  await chmod(socketPath(), 0o600);
294
+ await gitboyWebhooks.start();
290
295
  await agentHooks.start();
291
296
  // Codex panes on the Hook's own app-servers: their requests become
292
297
  // approval cards, and servers a previous Hook started are rejoined.
@@ -351,7 +356,7 @@ export async function serve(version, options = {}) {
351
356
  })().finally(() => { recording = false; }).catch(() => { });
352
357
  }, 5000);
353
358
  await new Promise(resolve => {
354
- const stop = () => { storeSync.stop(); fanoutMessages.close(); stopRetention(); clearInterval(scheduleTimer); clearInterval(canaryTimer); clearInterval(codexAuthTimer); clearInterval(activityTimer); scheduler?.close(); codeReindexer?.close(); codexServers.close(); agentHooks.close(); ws.clients.forEach(c => c.terminate()); ws.close(); http.close(() => resolve()); http.closeAllConnections(); };
359
+ const stop = () => { gitboyWebhooks.stop(); storePulls.stop(); storeSync.stop(); fanoutMessages.close(); stopRetention(); clearInterval(scheduleTimer); clearInterval(canaryTimer); clearInterval(codexAuthTimer); clearInterval(activityTimer); scheduler?.close(); codeReindexer?.close(); codexServers.close(); agentHooks.close(); ws.clients.forEach(c => c.terminate()); ws.close(); http.close(() => resolve()); http.closeAllConnections(); };
355
360
  process.once("SIGTERM", stop);
356
361
  process.once("SIGINT", stop);
357
362
  });
@@ -590,7 +590,9 @@ export class SessionMover {
590
590
  const deadline = this.now + handoffMs;
591
591
  try {
592
592
  const sent = await this.deps.deliver(from, handoffRequest(record.id, record.to, record.mode), `move-${record.id}`);
593
- if (sent.ok !== true && sent.queued !== true)
593
+ // A lost delivery acknowledgement can still have reached the agent;
594
+ // its marked transcript reply is authoritative for the normal window.
595
+ if (sent.state !== "uncertain" && sent.ok !== true && sent.queued !== true)
594
596
  return { reason: `the request could not be delivered (${String(sent.error ?? sent.state ?? "not confirmed")}).` };
595
597
  }
596
598
  catch (error) {
@@ -0,0 +1,22 @@
1
+ import type { IncomingMessage } from "node:http";
2
+ import { z } from "zod";
3
+ import { ImmediatePulls } from "../sync/pull-now.js";
4
+ declare const identitySchema: z.ZodObject<{
5
+ storeId: z.ZodString;
6
+ repository: z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>;
7
+ branch: z.ZodString;
8
+ }, z.core.$strict>;
9
+ export type StorePullIdentity = z.infer<typeof identitySchema>;
10
+ export declare class StorePulls {
11
+ private readonly primary;
12
+ private readonly pulls;
13
+ constructor(primary: string, pulls?: ImmediatePulls);
14
+ private localStores;
15
+ /** A receiver's trusted local config may name an attachment ID; return the store's portable ID. */
16
+ request(storeId: string): string | undefined;
17
+ route(request: Pick<IncomingMessage, "headers">, data: Record<string, unknown>): Promise<{
18
+ ok: true;
19
+ }>;
20
+ stop(): void;
21
+ }
22
+ export {};
@@ -0,0 +1,62 @@
1
+ import path from "node:path";
2
+ import { z } from "zod";
3
+ import { registeredStoreIdentity, resolveAllStores, storePathExists } from "../store-registry.js";
4
+ import { ImmediatePulls } from "../sync/pull-now.js";
5
+ import { runGitWithin } from "../sync/pull.js";
6
+ import { parseRemoteUrl } from "./git-hosts.js";
7
+ import { BridgeError } from "./protocol.js";
8
+ import { ownerRequest } from "./request-origin.js";
9
+ const identitySchema = z.object({
10
+ storeId: z.string().min(1).max(128),
11
+ repository: z.string().max(513).regex(/^[^/\s]+\/[^/\s]+$/).transform(value => value.toLowerCase()),
12
+ branch: z.string().min(1).max(1024),
13
+ }).strict();
14
+ export class StorePulls {
15
+ primary;
16
+ pulls;
17
+ constructor(primary, pulls = new ImmediatePulls()) {
18
+ this.primary = primary;
19
+ this.pulls = pulls;
20
+ }
21
+ localStores() {
22
+ // A synced primary entry can carry another computer's path. The Hook's configured root wins.
23
+ return resolveAllStores(this.primary).slice(0, 64).map(entry => {
24
+ const local = entry.role === "primary" ? path.resolve(this.primary) : entry.path;
25
+ return { ...entry, path: local, available: storePathExists(local) };
26
+ }).filter(entry => entry.available);
27
+ }
28
+ /** A receiver's trusted local config may name an attachment ID; return the store's portable ID. */
29
+ request(storeId) {
30
+ const store = this.localStores().find(entry => entry.id === storeId || registeredStoreIdentity(entry.path) === storeId);
31
+ if (!store)
32
+ return undefined;
33
+ this.pulls.request(store.path);
34
+ return registeredStoreIdentity(store.path);
35
+ }
36
+ async route(request, data) {
37
+ if (!(ownerRequest(request, data) || (request.headers["x-phren-origin"] === "peer" && data.origin === undefined))) {
38
+ throw new BridgeError(403, "Only the owner or a peer Hook can request a pull.");
39
+ }
40
+ const identity = identitySchema.parse(data);
41
+ // Attachment IDs never cross computers. Only inspect checkouts carrying the requested identity.
42
+ const candidates = this.localStores().filter(entry => registeredStoreIdentity(entry.path) === identity.storeId);
43
+ for (const store of candidates) {
44
+ // Local Git metadata only, bounded; follow the same tracking branch pollStore will pull.
45
+ const refs = await runGitWithin(1000, store.path, ["for-each-ref", "--format=%(HEAD)%09%(upstream:remotename)%09%(upstream:remoteref)", "refs/heads/"]);
46
+ if (!refs.ok)
47
+ continue;
48
+ const current = refs.output.split("\n").find(line => line.startsWith("*\t"));
49
+ const [, remote, branch] = current?.split("\t") ?? [];
50
+ if (!remote || remote === "." || branch !== `refs/heads/${identity.branch}`)
51
+ continue;
52
+ const url = await runGitWithin(1000, store.path, ["remote", "get-url", "--", remote]);
53
+ const repository = url.ok ? parseRemoteUrl(url.output)?.path.toLowerCase() : undefined;
54
+ if (repository !== identity.repository)
55
+ continue;
56
+ this.pulls.request(store.path);
57
+ return { ok: true };
58
+ }
59
+ throw new BridgeError(404, "Not found.");
60
+ }
61
+ stop() { this.pulls.stop(); }
62
+ }