Elffuss BraidHub

utopia-ia/elffuss-braid public

52e22f93c9da · 26.3 KiBSourceRawPermalink

AGENTS.md

Instructions for coding agents. Part 1 is for when someone asks you to install braid in a project and set it up for agents. Part 2 is for working on braid itself, in this repository.

Braid is a version control system for repositories where many agents land changes at the same time. Each agent works in a workspace of its own over one shared store, and landing on the main branch is one short step that agents take in turn: no fetch, rebase and push loop. Git stays at the edges: braid imports a project's whole history from git, and sends what lands back to the project's git remote.

Keep git as the source of truth: the project's git remote holds everything that matters, and nothing lives only in braid.

1. Installing braid and setting it up in a project

Read this part whole before you run anything, and run its commands exactly as they are written.

Install

Braid builds from source with cargo. It needs:

cargo install --locked --git https://github.com/KikoCis/elffuss-braid --tag v0.1.1 elffuss-braid-cli

It compiles braid and its dependencies from source, which takes a while: give the command a long timeout, or run it in the background, since a command cut short starts again from the beginning. It puts a braid binary in cargo's bin directory, ~/.cargo/bin unless CARGO_HOME says otherwise. If that directory is not on PATH, call braid as "$HOME/.cargo/bin/braid" wherever this file says braid: a harness may not keep a changed PATH from one command to the next. Running the same command again replaces an older braid with this version.

Is it installed, and set up here?

In the project root, on its main branch:

braid --version     # "braid 0.1.1", or newer, once it is installed
braid cat-object "$(git branch --show-current):AGENTS.md" | grep -q '^<!-- braid:start -->' && grep -q '^name = .' .braid/config && grep -q '^email = .' .braid/config && echo "braid is set up here"

Set up a project

First read the project's AGENTS.md, CLAUDE.md and the like for rules about git: branches, pull requests, how to commit. Look too for what braid does not run, or does not check out as git does:

Tell the user what you found, and if any of it clashes with agents landing on the main branch, ask how to go on before you set anything up. Where the hooks run checks, such as tests or a linter, offer to write those commands into AGENTS.md, outside the braid section, so that every session runs them before it lands.

Then list the project's git remotes (git remote -v): what agents land reaches the user's checkout through one, which the commands here call origin; if it has another name, write that instead. With no remote at all, what agents land stays in braid: offer to make one (see "No remote?" below). And if the project has no CLAUDE.md, ask whether to add one, since Claude Code reads CLAUDE.md rather than AGENTS.md: with a yes, run touch CLAUDE.md before the commands below, and braid agents init makes it include AGENTS.md.

Then work in the root of the project's git checkout, on its main branch, with nothing to commit (git status shows no staged or modified files; untracked files do not matter) and nothing new to pull (git pull --ff-only; skip it when the branch has no upstream, which is when git rev-parse --abbrev-ref '@{u}' fails). In a shallow clone, run git fetch --unshallow first: braid imports the whole history. Run the lines of each block below one at a time, and stop at the first one that fails. Every line is safe to run again, so once the cause is fixed, start again from the first line of its block.

test -d .braid || braid init     # .braid/: the store and the agents' workspaces
mkdir -p "$(git rev-parse --git-path info)"
grep -qsxF .braid "$(git rev-parse --git-path info/exclude)" || echo .braid >> "$(git rev-parse --git-path info/exclude)"
test -f .braid/config || printf 'name = %s\nemail = %s\n' "$(git config user.name)" "$(git config user.email)" > .braid/config
if grep -q '^name = .' .braid/config && grep -q '^email = .' .braid/config; then braid git import . && braid agents init; else echo 'braid: .braid/config has no name or no email: ask the user who signs' >&2; false; fi

Commit what braid agents init wrote, so that every workspace and every clone has the rules, then import that commit too. From this commit on, agents in every clone of the project read the section: tell the user so before you commit. Where braid agents init found the files as they should be already, there is nothing to commit, and the git commit line does nothing.

git add AGENTS.md
if [ -f CLAUDE.md ]; then git add CLAUDE.md; fi
if [ -f GEMINI.md ]; then git add GEMINI.md; fi
git diff --cached --quiet || git -c user.useConfigOnly=true commit -m "Set up braid for agents"
braid git import .               # braid's copy of the branch takes the commit
braid cat-object "$(git branch --show-current):AGENTS.md" | grep -q '^<!-- braid:start -->' && grep -q '^name = .' .braid/config && grep -q '^email = .' .braid/config && echo "braid is set up here"

The last line is the check from "Is it installed, and set up here?": it prints "braid is set up here" once the commit is in braid. With user.useConfigOnly, git refuses to commit rather than make up a name and an address from the machine; where it says "Author identity unknown", set them as above, then run this block again. If AGENTS.md, CLAUDE.md or GEMINI.md is a symbolic link, braid agents init writes the file it points to, and names it: git add that file as well.

The commit stays in this checkout until it is pushed: the next braid git sync takes it to the git remote, with what agents land, and other clones get the rules from there (see "Keeping the git remote up to date"). Tell the user so, and offer to sync now.

Every agent session, from then on

The section in the project's AGENTS.md tells each session what to do, with the branch it names where the commands here say main. In short:

Conflicts

When another agent landed a change to the same lines first, land exits with status 2 and leaves main where it was, and update exits with 2 as it writes the conflicts into the files. Exit status 2 also means a command line braid could not parse, which prints only an error, so read the JSON: a conflict's outcome is conflicts. Each conflict names its sides: base, branch (what main holds now) and workspace (the session's own change), and next says what to run.

braid --agent agent-1 update      # exit status 2: conflict markers in the files
# edit the files until no marker is left; `braid status` lists what still blocks
braid --agent agent-1 commit      # keeps the change's message; -m gives another
braid --agent agent-1 land --json

braid resolve <path> settles conflicts that are not about content, such as a file deleted on one side and changed on the other. Never commit --force while a rebase is in progress: it would commit the conflict markers as they are.

Keeping the git remote up to date

Landings change braid's main, not git's. braid git sync brings the two together: it fetches the remote's branch and imports it, then fast-forwards braid's branch when only the remote moved, pushes braid's branch when only braid moved, and when both moved, merges them and pushes the merge. The push is a fast-forward, with a lease on what was just fetched, so the remote's history is never rewritten. Braid runs no git hooks. It runs the git binary with your environment, so it needs the network and the credentials git push uses. Run it from the project root, when the remote should have what landed: who runs it, and when, is the user's call. Braid pushes to the remote's address, so the shared checkout's own record of the remote (origin/main) does not move: git pull --ff-only brings the checkout up to date. The commands here say main: write the branch that the section names.

braid --agent agent-1 git sync "$(git remote get-url origin)" --branch main --dry-run --json
braid --agent agent-1 git sync "$(git remote get-url origin)" --branch main
git pull --ff-only               # the shared checkout follows

No remote? Any bare repository will do. With the user's yes, these lines, run in the shared checkout on its main branch, make one next to the project, named after it (../<project>.git, by an absolute path, which braid reads the same from any directory), and push the main branch to it. If something has that name already, choose another.

git init --bare -b "$(git branch --show-current)" "$PWD.git"
git remote add origin "$PWD.git"
git push -u origin "$(git branch --show-current)"

Across machines: BraidHub

Braid's store lives in one checkout. For agents on several machines, a repository can live on BraidHub, a braid server where people and agents sign in with GitHub and land on the same main branch, and which can keep a GitHub repository as its mirror. Its own guide for agents, https://braidhub.elffuss.com/agents.md, says how to sign in, publish a project, clone one, and when to ask the user first.

Undoing

What not to do

2. Working on braid itself

Braid is written in Rust, edition 2024, and needs Rust 1.89 or newer. CI pins the toolchain to 1.99.0.

PathWhat it is
crates/formatObject ids and the hashed format (frozen; see docs/spec/format-v1.md). No I/O.
crates/engineChange lists, merges, landing, ancestry and blame. No I/O.
crates/storeThe store: objects in shared segments, refs in the op log.
crates/gitThe git bridge, through gitoxide.
crates/repoA repository on disk: discovery, names, git import, export and sync.
crates/wcWorkspaces: checkouts, status, commit, land and update.
crates/cliThe braid command; braid agents init is crates/cli/src/agents.rs.
crates/hubprotoThe BraidHub protocol the client speaks: names, tokens, scopes, the object stream and the JSON bodies; docs/hub.md is its prose.
crates/simDeterministic simulation of many agents landing at once.
docs/The design, the format specification and the hub's protocol (docs/hub.md, docs/hub-agents.md).
site/braid.elffuss.com, in English and Spanish; site/agents.md is part 1 of this file.

Build and test

cargo build
cargo test --workspace

The tests and the quick benchmark read this repository's whole history: in a shallow clone, run git fetch --unshallow first.

Before every commit

All of these must pass:

cargo fmt --all
cargo +1.99.0 clippy --workspace --all-targets --locked -- -D warnings
cargo clippy --workspace --all-targets -- -D warnings
cargo test --workspace
RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --all-features --locked
cargo deny check
cargo +1.89 check --workspace --all-targets
cargo +1.99.0 check -p elffuss-braid-format -p elffuss-braid-engine --target wasm32-unknown-unknown --locked
python3 docs/spec/check_vectors.py
python3 bench/run.py --quick
scripts/privacy-check.sh

CI runs all of them but the unpinned clippy, which catches the lints of newer toolchains early, and it also runs each fuzz target for a minute with cargo-fuzz, on nightly. The WebAssembly check needs its target (rustup +1.99.0 target add wasm32-unknown-unknown), and the vector check b3sum (cargo install b3sum --locked).

Rules

From CONTRIBUTING.md, in short: