export type SessionStatus = "running" | "stale" | "finished" | "failed"; export interface RefUpdate { readonly ref: string; readonly from: string | null; readonly to: string | null; } /** An update for one agent session. `id` is the event it comes from, which is also the session's delivery cursor. */ export type Delivery = | { readonly type: "event"; readonly id: string; readonly event: "repo.pushed"; readonly ref: string; readonly from: string | null; readonly to: string | null; /** `session` is null when a human pushed; `actor` is the agent runtime or the human's name. */ readonly by: { readonly session: string | null; readonly actor: string }; } | { readonly type: "instruction"; readonly id: string; readonly body: string; readonly by: { readonly person: string } } | { readonly type: "mention"; readonly id: string; /** The pull request the mention was made on. */ readonly pull: number; /** The title of the pull request. */ readonly title: string; readonly body: string; /** True when the mention also handed the agent the pull request's branch. */ readonly assigned: boolean; /** True when a sub-agent on the agent's machine is already handling it. */ readonly run: boolean; readonly by: { readonly person: string }; } | { readonly type: "run"; readonly id: string; readonly mention: string | null; readonly ok: boolean; readonly summary: string } | { readonly type: "problem"; readonly id: string; readonly commit: string; readonly message: string } | { readonly type: "stop"; readonly id: string; readonly reason: string }; /** Why an agent may not work on the branch `name`, or null when it may. */ export function branchProblem(name: string, defaultBranch: string): string | null { if (!/^[A-Za-z0-9._\/-]{1,200}$/.test(name) || /(^[./-]|[./]$|\.\.|\/\/|\/\.|\.lock$)/.test(name) || name === "HEAD") return `${name} is not a valid branch name.`; if (name === defaultBranch) return `${name} is the default branch. Agents work on their own branch and open a pull request into it.`; return null; } /** The new branch a session without a branch claims by pushing it first, or null when `updates` don't create one it may claim. */ export function claimableBranch(updates: readonly RefUpdate[], defaultBranch: string): string | null { if (updates.length !== 1) return null; const [update] = updates; if (update.from !== null || update.to === null || !update.ref.startsWith("refs/heads/")) return null; const name = update.ref.slice("refs/heads/".length); return branchProblem(name, defaultBranch) === null ? name : null; } /** Why a session working on `branch` may not apply `update`, or null when it may. Fast-forward checks need the object graph and stay with the origin. */ export function refProblem(branch: string | null, update: RefUpdate): string | null { if (branch === null) return `${update.ref}: this session has no branch. Push a new branch to claim it, or take a branch or pull request with refs_take.`; const allowed = `refs/heads/${branch}`; if (update.ref !== allowed) return `${update.ref}: this session may only push to ${allowed}.`; if (update.to === null) return `${update.ref}: sessions cannot delete their branch.`; return null; } function short(sha: string | null): string { return sha ? sha.slice(0, 7) : "nothing"; } export function describeDelivery(delivery: Delivery): string { switch (delivery.type) { case "event": { const who = delivery.by.session ? `Agent ${delivery.by.actor} (session ${delivery.by.session})` : `${delivery.by.actor} (human)`; const range = `${delivery.from ?? "origin/HEAD"}..${delivery.to}`; return `${who} pushed ${delivery.ref.replace("refs/heads/", "")} ${short(delivery.from)} → ${short(delivery.to)}. Before your next push: run \`git pull --rebase\`, read the decision records in \`git log ${range}\`, and treat every active decision as a constraint on your work. To overturn one, list it in your record's supersedes as #.`; } case "problem": return `Your commit ${short(delivery.commit)} has an invalid decision record, so it was not indexed: ${delivery.message} Fix it with a follow-up commit that carries a corrected record.`; case "instruction": return `Instruction from ${delivery.by.person}: ${delivery.body}`; case "mention": { const place = `pull request #${delivery.pull}`; if (delivery.run) return `${delivery.by.person} mentioned you on ${place} "${delivery.title}": "${delivery.body}" A sub-agent on your machine is handling it (mention ${delivery.id}); you'll hear when it finishes.`; return delivery.assigned ? `${delivery.by.person} assigned you ${place} "${delivery.title}" and wrote: "${delivery.body}" You now hold it. Call refs_take with pull ${delivery.pull} to get push access, then do the whole job: implement it, commit with decision records, push to its branch, reply with refs_reply (mention ${delivery.id}) when it's done, and call refs_release.` : `${delivery.by.person} mentioned you on ${place} "${delivery.title}": "${delivery.body}" Answer with refs_reply (mention ${delivery.id}).`; } case "run": return `Your sub-agent ${delivery.ok ? "finished" : "stopped without finishing"}${delivery.mention ? ` mention ${delivery.mention}` : ""}: ${delivery.summary || "no summary."}`; case "stop": return `Your refs session was stopped: ${delivery.reason} Commit your work locally, do not push, and stop.`; } } export function describeDeliveries(deliveries: readonly Delivery[]): string { return ["[refs] Updates:", ...deliveries.map((delivery) => `- ${describeDelivery(delivery)}`)].join("\n"); } /** * The text an `mcp_tool` hook hands to Claude Code: hook JSON that adds the deliveries to the * conversation for `event`, or nothing when there is nothing to say. */ export function hookOutput(event: string, context: string): string { return context ? JSON.stringify({ hookSpecificOutput: { hookEventName: event, additionalContext: context } }) : ""; }