Skip to content
refs
History
40 lines · 2.5 KBRaw
1import { commitBody, gitObject, pushRef, treeBody } from "./gitwrite.ts";
2
3const encoder = new TextEncoder();
4
5/** The files a new repository starts with, so agents that open it know to work over the ref protocol. */
6export function starterFiles(input: { readonly repo: string; readonly name: string; readonly description: string; readonly origin: string }): { readonly path: string; readonly text: string }[] {
7 const agents = `# AGENTS.md
8
9## ref protocol
10
11This repository lives on refs at ${input.origin}/${input.repo}. Agents work on it over the ref protocol.
12
13- At the start of every session, call \`refs_connect\` with \`${input.repo}\`. If you don't have the refs tools, read ${input.origin}/llms.txt and set them up.
14- Work on one branch at a time, never \`main\`. Take a pull request or a branch with \`refs_take\`, or push a new branch to claim it.
15- When you make a choice that the owner of the work would want to make themselves, record it in a \`decision\` block at the end of the commit message. Most commits have none. The test and the format are in ${input.origin}/llms.txt.
16- Never force-push. If a push is rejected, run \`git pull --rebase\` and push again.
17- Answer mentions and report on your work with \`refs_reply\`. Call \`refs_release\` when the work is done.
18`;
19 return [
20 { path: "AGENTS.md", text: agents },
21 { path: "CLAUDE.md", text: "@AGENTS.md\n" },
22 { path: "README.md", text: `# ${input.name}\n${input.description ? `\n${input.description}\n` : ""}` },
23 ];
24}
25
26/** Writes `files` as the first commit of the empty branch `branch`. Fails, without changing anything, when the branch already exists. */
27export async function writeFirstCommit(
28 handle: ArtifactsRepo,
29 branch: string,
30 files: readonly { readonly path: string; readonly text: string }[],
31 author: { readonly name: string; readonly email: string },
32 at: number,
33 message: string,
34): Promise<{ ok: true; commit: string } | { ok: false; reason: string }> {
35 const blobs = await Promise.all(files.map(async (file) => ({ path: file.path, blob: await gitObject("blob", encoder.encode(file.text)) })));
36 const tree = await gitObject("tree", treeBody(blobs.map(({ path, blob }) => ({ mode: "100644", name: path, hash: blob.hash }))));
37 const commit = await gitObject("commit", commitBody({ tree: tree.hash, parents: [], author, at, message }));
38 const pushed = await pushRef(handle, `refs/heads/${branch}`, "0".repeat(40), commit.hash, [...blobs.map(({ blob }) => blob), tree, commit]);
39 return pushed.ok ? { ok: true, commit: commit.hash } : pushed;
40}