@phren/cli 0.3.51 → 0.3.52

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
+ }
@@ -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
  });
@@ -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
+ }