# Workspace transitions

A session's workspace root is part of what its log decides with: the
permission algorithm resolves paths against it, file observations and
evidence describe files under it, and the working directory is relative to
it. A session can **move**: a
recorded transition from one root to another, in three durable phases. This
document is the protocol; `core/loop/move.go` folds it and
`core/driver/move.go` drives it.

## Phases

A move has an ID, the root it leaves (`From`), the root it goes to (`To`)
and whether `To` is a git worktree of the session's own. Each phase is
its own event, appended on its own, so a crash leaves the move at a phase
boundary, never between.

1. **Prepared** (`WorkspaceMovePrepared`). The intent, before anything
   changes. Folding it interrupts the current run, if one is active, as an
   interrupt command does: effects that haven't started are dropped and
   settle as interrupted, a suspended approval with them, and started ones
   end or are reconciled as recovery always does. No new effect starts
   while a move is prepared, and no run starts: user messages wait in the
   queue.
2. **Committed** (`WorkspaceMoveCommitted`). The switch. It folds only when
   the session is idle (no run, no pending effect, not suspended) and
   nothing of the old root is still live: no running background process and
   no unfinished subagent. Folding it sets the configuration's root and
   worktree flag to the move's, bumps the policy revision (so approvals and
   grants made for the old root never match again), advances the mutation
   generation, and drops what described the old root: file observations and
   the recently observed files, check evidence and the runs it passed in,
   the workspace baseline, and the working directory, which returns to the
   new root's top. It adds to the dynamic context a note telling the model
   where it now works and that files it read before must be read again.
   From commit until handoff nothing is dispatched.
3. **Handed off** (`WorkspaceMoveHandedOff`). An execution whose root is
   `To` took the session up. Only a driver at `To` records it. Dispatch
   resumes; queued messages start the next run.

An execution at any other root that loads a committed move it can't hand
off returns `driver.ErrMoved`, which names `To`: the host rebuilds its
environment, tools and views at `To` and takes the session up there.

## Who moves a session

A host opts in with `zelk.WithWorkspaceMoves()`. Without it, taking up a
session at another root refuses with `ErrIncompatible`.
With it, a driver that loads a session recorded at another root (resuming
from another directory, or falling back from a worktree that is gone) records
the three phases itself: Prepared at load, Committed once the interrupted
run has settled, and Handed off at once, since it already runs at `To`.

The model's own moves (the EnterWorktree and ExitWorktree tools) use the same
phases, keyed by the tool call's effect ID. A tool asks through
`tool.Mover`: its Prepared, with `CallID` set, is appended in the same batch
as the call's result, and it does **not** interrupt the run. The calls after
it in the turn wait: the scheduler decides nothing while a move is in
progress, so they are authorized at the new root, under the new policy
revision, after the handoff. The moving call's own PostToolUse hooks are
the exception: they see a call that ran at `From`, so they run there,
before the commit. The move commits once nothing is pending (those hooks
included), with the run still open, and the turn goes on at the new root
after the handoff.
A tool can't move a session that has no root, is
already moving, owns workspace-bound state or has something live.

An execution that commits a move to another root returns `ErrMoved` at
once, and never supersedes a move it committed itself.

## Keys and recovery

A host move's ID is `move-<seq>`, the sequence its Prepared event takes, so
the ID is fixed by the log and a retry after a crash finds the move it
started rather than beginning another. Recovery always goes forward:

- **Prepared, not committed.** Any execution may commit it once the
  session is idle (for a tool's move, once nothing is pending); one at `To`
  then hands it off. One at `From` commits it and returns `ErrMoved`.
- **Committed, not handed off.** One at `To` hands it off. One elsewhere
  returns `ErrMoved`, unless it may move sessions, in which case it
  prepares a new move to its own root, which supersedes the unfinished one
  (the worktree a session moved to is gone).

A move to the root the session is already at is refused, as is a second
move while one is prepared, and a move while a rewind or file restore is
under way. A rewind or file restore is refused while a move is under way,
from its preparation to its handoff: either would write files at the root
the move is leaving.

## What can't move

Skill publications are owned under namespaces that
bind the canonical workspace path, and scoped requirement evidence under a
workspace ID. A session that owns any of them refuses to move (the fold
returns `ErrInconsistent`, and the driver checks first and returns
`ErrIncompatible` naming what it owns), rather than clearing ownership to
pass. Per-workspace ownership is future work.

## Invariants

- Replay never moves anything: the fold reads the recorded phases, and the
  root every decision uses is the one the log reached.
- Every decision after commit uses the new root and a new policy revision;
  no decision made before commit is reused after it.
- No effect starts between Prepared and Handed off, except, for a tool's
  move, the PostToolUse hooks of the call that moved, which run at `From`
  before the commit.
- A session's root changes only at Committed.
- Instructions loaded from the old root's files stay in the context until a
  built-in tool next touches their directory, as after any change to them.

## Tests

`core/loop/move_test.go` folds each phase and its refusals, replays logs
cut at every phase boundary, and covers A→B→A, a suspended approval, a
pending effect, a tool's move with a call after it, and a tool's move
whose call has PostToolUse hooks. `core/driver/move_test.go`
and `move_tool_test.go` inject a crash after each append and check that the
next execution, at either root, goes forward as above; `TestToolMoveAcrossAgents`
in `core` takes a tool's move from one Agent to another.
