@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
65 lines (64 loc) • 3.2 kB
JavaScript
import path from "node:path";
import { z } from "zod";
import { BridgeError } from "./protocol.js";
const CHECKPOINT_ROUTES = new Map([
["/v1/git/checkpoints", "list"],
["/v1/git/checkpoints/preview", "preview"],
["/v1/git/checkpoints/restore", "restore"],
]);
export function checkpointAction(route) {
return CHECKPOINT_ROUTES.get(route);
}
const WRITE_ROUTES = new Set(["worktrees/remove", "stage", "unstage", "discard", "commit", "push", "pr", "pr/merge", "host-token", "checkout", "fetch", "pull", "apply"]
.map(action => `/v1/git/${action}`));
export const isGitWriteRoute = (route) => {
const checkpoint = checkpointAction(route);
return WRITE_ROUTES.has(route) || checkpoint === "preview" || checkpoint === "restore";
};
export function requireGitWrite(route, expectedRepository, headers, diagnostics) {
if (headers["x-phren-origin"] === "peer")
throw new BridgeError(403, "Another computer can't change Git or remove worktrees here.");
if (expectedRepository === undefined) {
diagnostics?.record(route, headers);
throw new BridgeError(400, "This app is too old to change Git safely. Update it.", { code: "expected-repository-required" });
}
}
export function requireGitCheckout(root, expectedRepository) {
const expected = z.string().min(1).max(4096).refine(value => path.isAbsolute(value) && !value.includes("\0"), "Expected an absolute repository path.").parse(expectedRepository);
if (root !== expected)
throw new BridgeError(409, "The checkout changed. Refresh before writing.", { code: "git-checkout-changed" });
return root;
}
/** Request metadata is a diagnostic hint, never an authentication identity.
* The raw SSH byte pipe does not expose the paired key's label to the Hook. */
function clientIdentity(headers) {
const clean = (value) => (Array.isArray(value) ? value.join(", ") : value ?? "")
.replace(/[\x00-\x1f\x7f]/g, " ").trim().slice(0, 200);
const client = clean(headers["x-phren-client"]);
const version = clean(headers["x-phren-app-version"] || headers["x-phren-client-version"]);
const agent = clean(headers["user-agent"]);
return [client, version, agent].filter(Boolean).join(" / ") || "unknown client";
}
/** Per Hook run, bounded client counts and one warning per client and route. */
export class GitWriteDiagnostics {
clients = new Map();
record(route, headers) {
let client = clientIdentity(headers);
if (!this.clients.has(client) && this.clients.size >= 128)
client = "other clients";
let entry = this.clients.get(client);
if (!entry) {
entry = { count: 0, routes: new Set() };
this.clients.set(client, entry);
}
entry.count++;
if (!entry.routes.has(route)) {
entry.routes.add(route);
console.warn(`[phren-hook] ${route}: refused git write without expectedRepository from ${client}. Update the app.`);
}
}
snapshot() {
return [...this.clients].map(([client, entry]) => ({ client, count: entry.count,
warning: `${entry.count} git writes without expectedRepository from ${client}` }));
}
}