Files
loki/docs/architecture/worktree-per-agent.md
CatoandClaude Fable 5.1 95c40a9cd8 refactor: rename the product from FleetCrown to Loki (#695)
The product, its assistant, the repo, the host and every unit on the box now
share one name. 638 files, ~3,000 mentions: UI copy, config, env keys
(FLEETCROWN_* -> LOKI_*), systemd units (fleetcrown-* -> loki-*), docs,
articles, the deploy workflow other repos reuse, and nine file names.

- Three article slugs carried the old name and had been shared; permanent
  redirects keep those links alive (next.config.ts).
- test-no-retired-name.sh now also forbids the old product name across the
  whole repo, so the rename cannot creep back through templates and examples.
- ASSISTANT_STACK collapses the product and the assistant into one entry,
  because they are now the same name.

Infra was cut over ahead of this merge: /opt/loki, loki-* units, the `loki`
database and role, loki.orangecat.ch (old host 308s to it; /api, /widget,
/embed keep proxying), GitHub repos bitbaum/loki and bitbaum/loki-releases.
The Resend sending domain stays fleetcrown.orangecat.ch until the new domain
is verified in DNS.


Claude-Session: https://claude.ai/code/session_016YJVAanfj8keVSXyZnTemh

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-13 16:13:10 -07:00

4.7 KiB

Worktree-per-agent dispatch isolation

Status: implemented, opt-in via LOKI_WORKTREE_DISPATCH=true on the runner. Module: src/lib/agent-execution/worktree-workspace.ts · wiring: desktop/src/main/poller.ts (dispatch + close_tab) · net: scripts/test/worktree-workspace.ts (14 checks).

The incident this kills

2026-07-17: an autopilot mid-run executed git add -A in a project's primary checkout while a second agent had a feature branch checked out there. The autopilot's commit swallowed the other agent's files and landed on the wrong branch — two agents were sharing one worktree/index/HEAD. Prompt-level rules ("only stage your files") cannot survive add -A; only filesystem isolation can.

How it works

dispatch(tab, dir, runId)                       primary checkout: <dir>
   └─ fresh launch only (never inject-into-live)
        ├─ pruneWorktrees(tab, dir)             sweep clean leftovers
        ├─ ensureWorktreeWorkspace(...)         ~/.loki/worktrees/<tab>/<runId>
        │     git worktree add -B fc/<runId> … HEAD
        │     symlink node_modules + .env* from the primary
        └─ prompt := worktreePromptNote(runId) + prompt
                     ("you are on branch fc/<runId>; land with
                       git pull --rebase origin main && git push origin HEAD:main")
  • Verification follows the agent. Transcript/auth checks (waitForAgentGenerating, detectAuthFailure) are cwd-keyed, so the poller threads the effective dir everywhere (worktreeByTab map). The session handoff is tab-keyed (~/.loki/sessions/<tab>.md), so the close loop (close-from-session.ts) is unaffected by the cwd change.
  • Graceful degradation. Any failure (not a git repo, git missing, disk full) logs and launches in the primary dir — the flag can only add isolation, never break a dispatch. Mirrors the LOKI_BOX_PREPARE precedent.
  • Cleanup never destroys work. close_tab prunes only clean worktrees: no uncommitted changes AND no commits unreachable from every other ref (pushed/merged work counts as shared). Dirty worktrees outlive their session and are re-swept on the next dispatch. Artifact symlinks we created are disregarded when judging cleanliness.

Semantics preserved

The loop's autonomous commit-to-main model is unchanged: the agent still lands its work on origin/main (rebase-first push from its branch), the handoff still carries the commit SHA, and isProjectBusy still serializes same-project dispatches. What changed is only where the agent's index lives.

Phase 2 — same-project parallel dispatch (implemented, opt-in)

Flag: LOKI_PARALLEL_DISPATCH=true on the SERVER (flip only after the worktree flag has been dogfooded). SSOT: src/lib/run-tab.ts.

When a project is busy, instead of queueing (queuedBehind) the dispatch fires immediately under a derived tab alias <project>~<runId8>. Because the whole loop is tab-keyed, every mechanism composes unchanged: own PTY workspace (runner:<alias>), own session file (<alias>.md), own sentinels, own worktree. The pieces that ARE alias-aware:

  • Dispatch route (run/route.ts): mints the alias, bakes the Exit contract with the alias session path, stores payload.sessionTab on the run — the run row keeps the BASE projectKey so analytics/busy checks aggregate per project.
  • Runner guard (poller.ts): a derived tab FORCES worktree isolation regardless of the env flag — parallel-without-isolation is impossible.
  • Ingestion (runtime-state): alias tabs are never persisted as project_states rows (no ghost cards; no clobbering the base project's runtime facts). A READY alias handoff closes exactly the run whose payload.sessionTab matches (closeOpenRunBySessionTab), from the pushed session fields directly.
  • Close-sweep guard: runs carrying a sessionTab are never closed from the base project's state row — run A's ready handoff can't close run B. The reaper remains the backstop for lost alias runs.

Trade-off (deliberate): parallel alias runs have no live project card — they surface in Activity/runs, not /control cards. Net: scripts/test/run-tab.ts.

Deliberate limits

  1. Both flags default off — worktree isolation first, then parallelism.
  2. Box runner: LOKI_BOX_PREPARE resolves its clone first; combining both flags worktrees the box clone only when the box dir exists locally — fine, but the flags target the laptop runner first.
  3. switch_agent / manual launch_agent stay on the primary — isolation is for dispatched runs, not the human's own session.
  4. Desktop runner needs a release to pick up the poller changes; the box runner gets them on the next deploy from main.