fix(pr-daemon): match worktrees, accept bot identity, log rejects

Three bugs the first live run surfaced:

aoe reports `worktree.main_repo_path` with a trailing slash, so the
session lookup never matched and the daemon created a second session for
a PR that already had one.

The gitea token belongs to a separate bot account, so PRs opened by
agents looked like someone else's work and got `review-pr` instead of
`land`. `self` now takes a list of logins per forge.

A webhook whose signature fails logged nothing, which makes a mismatched
secret indistinguishable from no deliveries at all.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Miguel Palhas
2026-08-19 19:00:13 +01:00
co-authored by Claude Opus 5
parent 787f133fdc
commit f189abd4ee
+23 -6
View File
@@ -31,7 +31,7 @@ type Config = {
webhookPort?: number;
notifyWaiting?: boolean;
pathRoots?: string[]; // scanned one level deep to find clones by origin URL
forges: Record<string, { api: string; tokenEnv: string; self: string; webhookSecretEnv?: string }>;
forges: Record<string, { api: string; tokenEnv: string; self: string | string[]; webhookSecretEnv?: string }>;
repos: RepoConfig[];
};
@@ -104,6 +104,13 @@ const EPOCH = epoch();
// ---------------------------------------------------------------- forges
// A forge account can differ from the human one -- agents here push as a
// separate bot login -- so "mine" is a set, not a name.
function isSelf(forge: string, login: string): boolean {
const self = config.forges[forge].self;
return Array.isArray(self) ? self.includes(login) : self === login;
}
function token(forge: string): string {
const env = config.forges[forge]?.tokenEnv;
const value = env ? process.env[env] : undefined;
@@ -307,6 +314,11 @@ function isYolo(profile: string, title: string): boolean {
}
}
// aoe reports worktree.main_repo_path with a trailing slash; the clone index
// builds paths without one. Compare normalized or nothing ever matches.
const samePath = (a?: string, b?: string) =>
!!a && !!b && a.replace(/\/+$/, "") === b.replace(/\/+$/, "");
type Session = { id: string; title: string; path: string; profile: string; branch: string; mainRepo: string };
async function listSessions(): Promise<Session[]> {
@@ -338,14 +350,14 @@ function route(pr: Pr, all: Session[]): Session | undefined {
const want = title(pr);
return (
all.find((s) => s.title === want) ??
all.find((s) => s.branch === pr.headRef && s.mainRepo === pr.cfg.path)
all.find((s) => s.branch === pr.headRef && samePath(s.mainRepo, pr.cfg.path))
);
}
// ---------------------------------------------------------------- sessions
async function createSession(pr: Pr): Promise<void> {
const own = pr.author === config.forges[pr.forge].self;
const own = isSelf(pr.forge, pr.author);
const drive = own && (pr.cfg.mode ?? "drive") === "drive";
const t = title(pr);
const group = config.group ?? "pr";
@@ -449,7 +461,7 @@ async function evaluate(prs: Pr[], mentioned: Set<string>, budget: { sessions: n
continue;
}
const skill = known?.skill ?? (full.author === config.forges[full.forge].self ? "land" : "review-pr");
const skill = known?.skill ?? (isSelf(full.forge, full.author) ? "land" : "review-pr");
const st = state.get(session.id) ?? "unknown";
if (config.notifyWaiting && st === "waiting") {
@@ -511,8 +523,13 @@ function serveWebhooks(): void {
const forge = url.pathname.replace(/^\//, "");
if (!config.forges[forge]) return new Response("no", { status: 404 });
const body = await req.text();
// Verified before anything is parsed: this endpoint is public.
if (!verify(forge, body, req.headers)) return new Response("bad signature", { status: 401 });
// Verified before anything is parsed: this endpoint is public. Logged
// because a secret that doesn't match the hook is otherwise invisible --
// the daemon just looks quiet while every delivery is dropped.
if (!verify(forge, body, req.headers)) {
log(`webhook ${forge}: signature rejected (${body.length} bytes) — hook secret and ${config.forges[forge].webhookSecretEnv} disagree?`);
return new Response("bad signature", { status: 401 });
}
let payload: any;
try {