---
name: lanes
license: MIT
description: How several agent sessions work on one git repository at the same time without breaking each other. One worktree and branch per session, a main checkout that stays clean and is only the integration target, named-file staging, a check before every commit, and a bundled script that integrates a branch one at a time (merge and full check in a temporary worktree, fast-forward only). Use when starting work in a repository other sessions also use, when finishing a branch that must reach the default branch, or when the user asks to set up parallel agent sessions in a project. Not for projects that integrate only through hosted pull requests.
compatibility: The integration script needs Windows PowerShell 5.1 or newer and git (2.31 or newer, because the script uses `git rev-parse --path-format=absolute`). The guard hook is an optional Claude Code PreToolUse hook and needs Node.js. No macOS or Linux version of the script is bundled.
metadata:
  author: "Jakob Christensen"
  source: "https://jakobchristensen.com"
---

# Lanes

Goal: any number of sessions change one repository at once, and the default branch only ever moves to a state that passed the full check. Below, `<default>` is the repository's default branch (main or master) and `<repo>` the folder name of the main checkout. `<name>` is a short tag for your tool, only lowercase letters, digits and hyphens (for example `claude`) and `<topic>` is 2 to 4 lowercase words joined by hyphens for the change. Two sessions of the same tool differ by `<topic>`, so keep topics distinct.

## Rules for every session

1. **Own worktree, own branch.** Before the first edit, check whether you already work in an own linked worktree on an own branch (`git rev-parse --git-dir` differs from `git rev-parse --git-common-dir`, and your tool may have created it): then use it. Otherwise run `git worktree add ../<repo>-<name>-<topic> -b <name>/<topic> <default>` (add a short suffix if that folder exists), then install dependencies there with the project's documented install command (`npm ci` when `package-lock.json` exists). The main checkout is only the integration target: never edit or commit in it with any tool, shell edits included. Reading it is fine, and the only write into it is the final `git merge --ff-only`, made by the script or by the manual route below. If your session starts inside the main checkout, move to your worktree and tell the user. Cursor's automatic cleanup may delete worktrees made by `git worktree add`, so commit and integrate soon.
2. **Mini-fix** (one file, a few lines, no new dependency): reuse one standing worktree `../<repo>-quick`, created and installed once. Check first: `git -C ../<repo>-quick branch --show-current` must print nothing and `git -C ../<repo>-quick status --porcelain` must be empty. Otherwise another session uses it, so make your own worktree. If both are empty, run `git -C ../<repo>-quick switch -c <name>/<topic> <default>` and treat a failure as taken. Commit, integrate, and after OK run `git -C ../<repo>-quick switch --detach <default>` and then `git branch -d <name>/<topic>`. The worktree stays.
3. **Split work by files** where possible, so two branches rarely touch the same file. When two tasks really need the same file, both edit it on their own branch and integrate one after the other. The second resolves the conflict and keeps both sides' behavior.
4. **Stage named files only**: never `git add -A` or `git commit -a`. To commit only your own hunks of a file another session also changed, build that file's content from `git show HEAD:<file>` plus your hunks, stage it with `git update-index --add --cacheinfo 100644,<blob>,<file>` (mode, blob id from `git hash-object -w`, path, comma-separated), read `git diff --cached -- <file>` to see exactly what is staged, then commit and read `git show HEAD -- <file>` for lines you did not write. Attribute a change to a session only when a commit, a branch or the user says so.
5. **A check runs before every commit that touches code or scripts**, chained so a failing check never reaches the commit: use `&&` in bash or PowerShell 7. Windows PowerShell 5.1 has no `&&`, so write `npm run check > <logfile>` and then `if ($LASTEXITCODE -eq 0) { git commit -F <msgfile> }`, redirecting stdout only and never using `2>&1`. A small commit (one or two files, no shared interface or config) runs the linter plus only the tests that reach the changed files. Anything larger, or any project without a way to run just those tests, runs the project's full check. Stderr stays in the terminal output. Read it there. Never pipe the check into `grep`, `head` or `tail` inside the chain, because a pipe returns the last command's exit code: write the output to a log file outside the repository and read it after.
6. **Never touch other sessions' work**: no stash, reset, clean or commit of changes you did not make.
7. **A branch from another agent** can start from an old base. Before you measure or compare anything on it, check `git merge-base <branch> <default>` against the tip of `<default>`, and on an old base merge `<default>` into the branch first. For integrating, the integrate script already merges into the current `<default>` and runs the full check there. An agent that reported failure may still have written correct work: read `git diff <default>...<branch>` and the files in its worktree before discarding anything.

## Integrating

Integrating is part of finishing and needs no extra question. Small changes integrate at once: a tweak or fix starts on a fresh branch from `<default>` and is integrated right after its commit, never stacked on an older unintegrated branch. A session that picks up an unintegrated branch says in its first message what `<default>` is missing (`git log <default>..<branch> --oneline`).

1. Commit your own files by name. `git status --porcelain` must then print nothing. Delete your own scratch files or keep them outside the repository (`.git/info/exclude` is shared by all worktrees, so do not add patterns there). A file you did not create is not yours to delete or stage: stop and tell the user, because the script refuses a branch with uncommitted changes. Never run `git add -A` to make the script pass.
2. From the branch's worktree run, as one background command if your tool has them:
   `powershell -NoProfile -ExecutionPolicy Bypass -File scripts/integrate.ps1 -Detach -Wait`
   If your tool has no background mode, run it with `-Detach` only (it returns at once with the log path), end the turn, and run it with `-Wait` on the next turn (it waits up to 180 minutes). If `scripts/integrate.ps1` is missing in your worktree, use the main checkout's copy with `-File <that path>` from your worktree (it reads `integrate.config.json` next to itself, so pass the project's settings as parameters if they differ), or the manual route below if the project has none, and make the offer from Setup in a project once this integration is done.
   It starts the integration in its own hidden process (a plain background command dies when the agent session ends), prints the log path (report it), and returns once the log has its result line: exit 0 on `OK:`, exit 1 on `FAILED:`. Do not write a watcher of your own and do not poll: every poll costs a full turn, and `-Wait` does the waiting. The log is UTF-16, read it with PowerShell, never with `grep`.
3. Read the output once it returns. A log that only says another integration is running is not a result: run `-Wait` once more. `-Wait` follows the process recorded in the pid file next to the log and prints `FAILED:` itself when that process ended without a result line. Then start the script again with `-Detach`. Never kill a running integration.
4. On `FAILED:` fix the cause on the branch (merge `<default>` into it, resolve the named files, run the check) and integrate again without asking, at most 3 rounds. Then stop and report the log path, the failing step and what you tried. A red check is fixed, never skipped. Resolve a conflict yourself only when you can read the other side and keep it in full, otherwise stop and ask. Failures about git user settings, a missing worktree or a refused fast-forward are not code problems: report them and do not retry.
5. If the `OK:` line says `0 files`, nothing new was integrated: check that you committed on the right branch, or that the branch was already in `<default>` (`git log <default>..<branch>` is empty), before removing anything. Remove the branch's worktree only after a real `OK:`. Before integrating, note the values to clean up with: `git rev-parse --show-toplevel` (the worktree path) and `git branch --show-current` (the branch). After OK change into the main checkout, then run `git worktree remove <worktree path>` and `git branch -d <branch>`. Do not push unless the project says so, and say in the final message that `<default>` is updated locally only.

What the script does: it takes a lock so one integration runs at a time across all worktrees, merges the branch into a temporary worktree at `<default>`, runs the full check there, and moves `<default>` with `git merge --ff-only` only when it is green and no merged path has uncommitted changes in the main checkout (it waits up to 60 minutes for that work to be committed, then fails and names the files, and it gives the lock back while it waits). If the log shows that wait, tell the user once, naming the files, and do not touch them. If `<default>` moved during the check, it merges and checks again. Other sessions' uncommitted work is never stashed, reset or touched. Manual route without the script: create a temporary detached worktree at `<default>`, merge your branch there, run the project's full check, remove the temporary worktree, and only if the check is green and `git -C ../<repo> status --porcelain` is empty run `git -C ../<repo> merge --ff-only <tested commit>`.

## Setup in a project

Do this when the user asks for it, or after a clear yes to the offer below, and say which files you add.

**Offer, once per project.** When an integration ran by the manual route because the main checkout has no `scripts/integrate.ps1`, and Windows PowerShell 5.1 or newer is available (the bundled script needs it), ask one yes/no question that opens with this skill's name (`/lanes`), in the user's language, using its question or button UI if it has one. Skip the question when the file `lanes-declined` exists inside the git directory shared by all worktrees (`git rev-parse --git-common-dir`, it is never part of a commit and never shows in `git status`), or when you already asked this session. The question says what the script does (merges the branch and runs the full check in a temporary worktree, and moves `<default>` only when that is green), which files it adds to the project (`scripts/integrate.ps1`, optionally a config file and the guard hook), and what a no costs (every integration runs by hand by the manual route, which takes more steps and is easier to get wrong). Each option says what it means for the user. On a no, create an empty `lanes-declined` file at that path. Any other reply adds nothing and creates no file.

Work in your own worktree on a branch `<name>/setup-lanes`. The script does not exist yet for this first merge, so merge it by the manual route above. Before the script runs in a repository you do not know, read its `scripts/integrate.config.json` (its commands run as PowerShell) and tell the user.

1. Copy `scripts/integrate.ps1` from this skill's folder into the project's `scripts/` folder. Optionally copy the guard hook (step 4).
2. Settings go in an optional `scripts/integrate.config.json`, or as parameters of the script (a parameter wins). Empty or missing means auto:
   - `target`: branch to integrate into. Auto: origin/HEAD, else main, else master, else the branch of the main checkout.
   - `checkCommand`: the full check. Default `npm run check`. Set it for any other project, for example `{"checkCommand": "python -m pytest"}`.
   - `installCommand`: run in the temporary worktree before the check. Auto: `npm ci` when `package-lock.json` exists, `none` to skip.
   - `retryCheckCommand`: run once when the check fails, to tell a flaky red from a real one (for example the same check with one test worker). It must run the same tests with the same pass criteria, never a reduced set. Empty: no retry.
   - `mainInstallCommand`: run in the main checkout after OK when `package-lock.json` changed. Auto: `npm install --no-save`, `none` to skip. A `vite.config.*` file there is touched afterwards so a running Vite restarts.
   - `devServerPattern`: text such as `vite`. After OK, node processes whose command line holds it and that run from inside the branch's own worktree are stopped. Empty: none.
3. Make sure the project has a check command that exits 0 only when everything is fine, and tell the user which one the script will run. Optionally add an alias such as an `integrate` script in `package.json`.
4. Optional guard hook (Claude Code only, because it uses its PreToolUse hook): copy `scripts/guard-main-checkout.mjs`, then merge this into the project's `.claude/settings.json`, keeping every existing setting and hook and not adding it twice, with the absolute path of the project in the command:

   ```json
   {"hooks": {"PreToolUse": [{"matcher": "Edit|Write|NotebookEdit", "hooks": [{"type": "command", "command": "node \"<absolute project path>/scripts/guard-main-checkout.mjs\""}]}]}}
   ```

   It blocks Edit, Write and NotebookEdit on files in the main checkout outside its nested worktrees. It does not stop shell edits or other tools. Check that the file is still valid JSON.
5. Create the standing quick worktree if the user wants mini-fixes: `git worktree add --detach ../<repo>-quick <default>`, then install dependencies there.
