import { commitBody, gitObject, pushRef, treeBody } from "./gitwrite.ts"; const encoder = new TextEncoder(); /** The files a new repository starts with, so agents that open it know to work over the ref protocol. */ export function starterFiles(input: { readonly repo: string; readonly name: string; readonly description: string; readonly origin: string }): { readonly path: string; readonly text: string }[] { const agents = `# AGENTS.md ## ref protocol This repository lives on refs at ${input.origin}/${input.repo}. Agents work on it over the ref protocol. - 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. - 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. - 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. - Never force-push. If a push is rejected, run \`git pull --rebase\` and push again. - Answer mentions and report on your work with \`refs_reply\`. Call \`refs_release\` when the work is done. `; return [ { path: "AGENTS.md", text: agents }, { path: "CLAUDE.md", text: "@AGENTS.md\n" }, { path: "README.md", text: `# ${input.name}\n${input.description ? `\n${input.description}\n` : ""}` }, ]; } /** Writes `files` as the first commit of the empty branch `branch`. Fails, without changing anything, when the branch already exists. */ export async function writeFirstCommit( handle: ArtifactsRepo, branch: string, files: readonly { readonly path: string; readonly text: string }[], author: { readonly name: string; readonly email: string }, at: number, message: string, ): Promise<{ ok: true; commit: string } | { ok: false; reason: string }> { const blobs = await Promise.all(files.map(async (file) => ({ path: file.path, blob: await gitObject("blob", encoder.encode(file.text)) }))); const tree = await gitObject("tree", treeBody(blobs.map(({ path, blob }) => ({ mode: "100644", name: path, hash: blob.hash })))); const commit = await gitObject("commit", commitBody({ tree: tree.hash, parents: [], author, at, message })); const pushed = await pushRef(handle, `refs/heads/${branch}`, "0".repeat(40), commit.hash, [...blobs.map(({ blob }) => blob), tree, commit]); return pushed.ok ? { ok: true, commit: commit.hash } : pushed; }