TL;DR
- Branch selection and
git worktree addused to be separate. Two Sessions could pick the same free name; the second failed withbranch_in_use. - One preparation lock now covers minting the branch through creating the worktree. Registry locks alone were too late.
- Idle archive waits for daemon idle and executor children gone, uploads with size and SHA-256, persists a local receipt, then retires the original. Restore refuses to overwrite. An old writer still alive blocks start, claim, and edit.
I will start with the broken shape, because that is how the race showed up.
First break: two Sessions, one free branch name
Each Session on a Task wants its own branch, numbered so a person can read it: task-77-1, then -2. The naive path was:
- Look for the first free
task-<id>-<n>. - Later, under a registry lock, run
git worktree addon that name.
Git serializes some worktree directory updates, but it does not treat “I looked and this branch name is free” as a reservation. Two Sessions on the same Task both saw -1 free. Both tried to add it. One won. The other got branch_in_use and the turn failed. Retrying did not help: both had already decided on the same name.
// Registry locks alone start too late: two Sessions can both
// select the same unused branch before either calls worktree add.
var preparationLocks sync.Map
func lockWorkspacePreparation(root string) func() {
value, _ := preparationLocks.LoadOrStore(kernel.ResolvePath(root), &sync.Mutex{})
mu := value.(*sync.Mutex)
mu.Lock()
return mu.Unlock
}
ensureSessionWorkspace now takes that lock for the whole path from plan load through branch mint and layout. LockRegistry still serializes add / remove / prune on one repository so this process does not race itself on .git/worktrees. Another OS process can still race; that stays outside the daemon’s ownership contract. Git’s own worktree lock is not used as an edit lock.
After the change, the same Task’s parallel Sessions pick different numbers. Preparation after an interrupt reuses the durable plan instead of minting again.
Second break: archive while a writer is still alive
Stopping a shared EC2 host is useless if the worktrees on disk are the only copy. Idle AWS work areas need a cold copy. The unsafe version is “tar the directory when the last HTTP request ends.”
That fails when an executor child is still writing after the turn’s HTTP span, or when a non-turn writer holds the tree. The archive path therefore shares the same activity and removal gate as the reaper: refuse removal while busy, claim the path, require the runtime-busy probe to be clear, then export.
Before any deletion is authorized, the bytes are restored into a temp directory with the real restore reader. Only then does upload proceed. The control plane returns a grant; the client PUTs to S3 with the expected size and SHA-256; commit must echo the same receipt. A local marker is fsynced before the original tree is retired. Any failure keeps the original. There is no path that falls through to ordinary quota deletion after a failed archive.
sum := sha256.Sum256(data)
hash := hex.EncodeToString(sum[:])
// upload grant must match next revision + size + hash
// commit receipt must match again before local retirement
Restore loads only the receipt that was committed before retirement. It stages into a temp directory and publishes with rename. An existing target is not overwritten. If an old writer is still alive, start, claim, and edit stay blocked until that writer is gone — the same fencing idea as rejecting a stale writer after a lease changes hands.
What stayed out of scope
Direct folders are not archived. External SSH writers are outside the daemon’s ownership contract. Archive stays off until the host is explicitly configured with a an explicit host identity and enable flags. The branch lock and the archive fence solve different failures; they share one rule: do not pretend a check that raced is still true when you mutate.