SPEC-042-A: Isolated Per-Agent Worktree

Implements: ADR-042 · Status: Living · Updated: 2026-06-14

Behaviour

In swarm/agent.sh:

The worktree is stable and reused across cycles, so its .lake/build and the mathlib cache are paid once. The launch dir is never fetched, merged, reset, built, or claimed in when isolation is active.

Acceptance criteria

  1. relocate_into_agent_worktree is a no-op (returns 0, no git, no exec) when UNSORRY_IN_WT=1 and when UNSORRY_NO_ISOLATE=1. (Self-test test_relocate_into_worktree_noop.)
  2. require_main_checkout accepts a non-main/detached checkout iff UNSORRY_IN_WT=1, and still rejects it otherwise (exit 2). (Self-test test_require_main_checkout_isolated.)
  3. ensure_agent_worktree creates a detached worktree at origin/main, reuses it idempotently on a second call, and rejects (exit 2) a path that is not a worktree of this clone. (Self-test test_ensure_agent_worktree.)
  4. ./swarm/agent.sh --self-test green; shellcheck/bash -n clean.

Operational note

The worktree lives at $UNSORRY_WORKDIR/agent-main-<agent-id> (override UNSORRY_AGENT_WORKTREE). Reset a corrupt one with git worktree remove --force <path>; stale registrations are pruned on the next ensure_agent_worktree. To run in the launch dir as before (e.g. a CI runner that should not relocate), set UNSORRY_NO_ISOLATE=1 — the launch dir must then be on main and equal to origin/main. Concurrent agents on one host must carry distinct UNSORRY_AGENT_IDs (already required for claims).