104 lines · 6.0 KBRaw
| 1 | export type SessionStatus = "running" | "stale" | "finished" | "failed"; |
| 2 | |
| 3 | export interface RefUpdate { |
| 4 | readonly ref: string; |
| 5 | readonly from: string | null; |
| 6 | readonly to: string | null; |
| 7 | } |
| 8 | |
| 9 | /** An update for one agent session. `id` is the event it comes from, which is also the session's delivery cursor. */ |
| 10 | export type Delivery = |
| 11 | | { |
| 12 | readonly type: "event"; |
| 13 | readonly id: string; |
| 14 | readonly event: "repo.pushed"; |
| 15 | readonly ref: string; |
| 16 | readonly from: string | null; |
| 17 | readonly to: string | null; |
| 18 | /** `session` is null when a human pushed; `actor` is the agent runtime or the human's name. */ |
| 19 | readonly by: { readonly session: string | null; readonly actor: string }; |
| 20 | } |
| 21 | | { readonly type: "instruction"; readonly id: string; readonly body: string; readonly by: { readonly person: string } } |
| 22 | | { |
| 23 | readonly type: "mention"; |
| 24 | readonly id: string; |
| 25 | /** The pull request the mention was made on. */ |
| 26 | readonly pull: number; |
| 27 | /** The title of the pull request. */ |
| 28 | readonly title: string; |
| 29 | readonly body: string; |
| 30 | /** True when the mention also handed the agent the pull request's branch. */ |
| 31 | readonly assigned: boolean; |
| 32 | /** True when a sub-agent on the agent's machine is already handling it. */ |
| 33 | readonly run: boolean; |
| 34 | readonly by: { readonly person: string }; |
| 35 | } |
| 36 | | { readonly type: "run"; readonly id: string; readonly mention: string | null; readonly ok: boolean; readonly summary: string } |
| 37 | | { readonly type: "problem"; readonly id: string; readonly commit: string; readonly message: string } |
| 38 | | { readonly type: "stop"; readonly id: string; readonly reason: string }; |
| 39 | |
| 40 | /** Why an agent may not work on the branch `name`, or null when it may. */ |
| 41 | export function branchProblem(name: string, defaultBranch: string): string | null { |
| 42 | if (!/^[A-Za-z0-9._\/-]{1,200}$/.test(name) || /(^[./-]|[./]$|\.\.|\/\/|\/\.|\.lock$)/.test(name) || name === "HEAD") return `${name} is not a valid branch name.`; |
| 43 | if (name === defaultBranch) return `${name} is the default branch. Agents work on their own branch and open a pull request into it.`; |
| 44 | return null; |
| 45 | } |
| 46 | |
| 47 | /** The new branch a session without a branch claims by pushing it first, or null when `updates` don't create one it may claim. */ |
| 48 | export function claimableBranch(updates: readonly RefUpdate[], defaultBranch: string): string | null { |
| 49 | if (updates.length !== 1) return null; |
| 50 | const [update] = updates; |
| 51 | if (update.from !== null || update.to === null || !update.ref.startsWith("refs/heads/")) return null; |
| 52 | const name = update.ref.slice("refs/heads/".length); |
| 53 | return branchProblem(name, defaultBranch) === null ? name : null; |
| 54 | } |
| 55 | |
| 56 | /** 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. */ |
| 57 | export function refProblem(branch: string | null, update: RefUpdate): string | null { |
| 58 | 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.`; |
| 59 | const allowed = `refs/heads/${branch}`; |
| 60 | if (update.ref !== allowed) return `${update.ref}: this session may only push to ${allowed}.`; |
| 61 | if (update.to === null) return `${update.ref}: sessions cannot delete their branch.`; |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | function short(sha: string | null): string { |
| 66 | return sha ? sha.slice(0, 7) : "nothing"; |
| 67 | } |
| 68 | |
| 69 | export function describeDelivery(delivery: Delivery): string { |
| 70 | switch (delivery.type) { |
| 71 | case "event": { |
| 72 | const who = delivery.by.session ? `Agent ${delivery.by.actor} (session ${delivery.by.session})` : `${delivery.by.actor} (human)`; |
| 73 | const range = `${delivery.from ?? "origin/HEAD"}..${delivery.to}`; |
| 74 | 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 <full commit hash>#<block number>.`; |
| 75 | } |
| 76 | case "problem": |
| 77 | 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.`; |
| 78 | case "instruction": |
| 79 | return `Instruction from ${delivery.by.person}: ${delivery.body}`; |
| 80 | case "mention": { |
| 81 | const place = `pull request #${delivery.pull}`; |
| 82 | 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.`; |
| 83 | return delivery.assigned |
| 84 | ? `${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.` |
| 85 | : `${delivery.by.person} mentioned you on ${place} "${delivery.title}": "${delivery.body}" Answer with refs_reply (mention ${delivery.id}).`; |
| 86 | } |
| 87 | case "run": |
| 88 | return `Your sub-agent ${delivery.ok ? "finished" : "stopped without finishing"}${delivery.mention ? ` mention ${delivery.mention}` : ""}: ${delivery.summary || "no summary."}`; |
| 89 | case "stop": |
| 90 | return `Your refs session was stopped: ${delivery.reason} Commit your work locally, do not push, and stop.`; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | export function describeDeliveries(deliveries: readonly Delivery[]): string { |
| 95 | return ["[refs] Updates:", ...deliveries.map((delivery) => `- ${describeDelivery(delivery)}`)].join("\n"); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * The text the `refs hook events` command hands to Claude Code: hook JSON that adds the deliveries to the |
| 100 | * conversation for `event`, or nothing when there is nothing to say. |
| 101 | */ |
| 102 | export function hookOutput(event: string, context: string): string { |
| 103 | return context ? JSON.stringify({ hookSpecificOutput: { hookEventName: event, additionalContext: context } }) : ""; |
| 104 | } |