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