Switching Coding Agents Mid-Task: Checkpoints, Git Facts, and Staleness Checks
Walking through a concrete local feature task in Agent Workspace to show what a checkpoint captures — read-only Git facts, honest test evidence, and next steps — and how recovery decides whether old notes are still trustworthy.
Using two or three coding agents in the same week is normal now. The friction hits the moment you have to switch mid-task: one agent edits three files, runs half the test suite, and hits a quota wall or fills its context window. When you open another client over a dirty working tree, pasting old chat logs is noisy, cross-harness transcripts are incompatible, and the exhausted source model cannot even write a closing summary for you.
Agent Workspace (antigravity-env) sidesteps chat portability altogether. It splits handoff state into two layers: deterministic, read-only Git facts captured directly from the local repository, and structured semantic checkpoints explicitly evaluated for staleness when a new agent takes over. Here is how that plays out on a concrete local task.
The Local Task Setup
Suppose we are working in personal-site on branch feat/home-featured to filter homepage cards by featured, add a tools category, and create the agent-workspace project page:
- I create a
Taskwith an immutable, owner-approvedDevelopmentBrief(anchored bysha256) and issue aTaskGrantscoped tofeat/home-featuredfor Antigravity. - Antigravity runs as
exec-01(owner_epoch = 1), updatessrc/config/categories.tsandsrc/components/home/HomeInteractive.tsx, runs a typecheck, and records Checkpoint #2. - Next, it creates
content/projects/agent-workspace/zh.mdx— and hits its quota limit before writing the English version or running a build. - I switch to Codex (
exec-02) on the exact same local worktree.
1. What a Checkpoint Actually Records
At meaningful boundaries, workspace.py exec checkpoint combines a read-only Git capture (core/git_facts.py, which never runs reset, clean, stash, or any LLM call) with concise semantic fields:
{
"checkpoint_id": "cp_019a7c",
"sequence": 2,
"reason": "progress",
"semantic_note": {
"completed": "Added tools category enum and switched homepage Projects card to featured filter.",
"remaining": "Create zh/en pages for agent-workspace, update featured flags, run production build.",
"decisions": "Placed Personal-Site under tools; left Lab hub layout unchanged.",
"known_errors": null,
"next_action": "Create content/projects/agent-workspace/zh.mdx and en.mdx, then run npm run build."
},
"machine_snapshot": {
"canonical_workspace": "E:\\SelfLib\\personal-site",
"git_worktree_id": "8f21b9d4c0e7a135...",
"branch": "feat/home-featured",
"head": "beb59108a41f...",
"dirty": true,
"staged": [],
"unstaged": [
{ "status": "M", "path": "src/config/categories.ts" },
{ "status": "M", "path": "src/components/home/HomeInteractive.tsx" }
],
"untracked": [],
"diff_stat": {
"unstaged": "src/components/home/HomeInteractive.tsx | 4 ++-\nsrc/config/categories.ts | 6 +++++-"
},
"test_evidence": {
"status": "passed",
"provenance": "reported",
"command": "npx tsc --noEmit",
"exit_code": 0,
"tested_head": "beb59108a41f...",
"dirty_at_capture": true
}
}
}Three design details matter here:
- Canonical Worktree Identity (
git_worktree_id): Derived by hashing normalizedgit rev-parse --git-common-dirplus--show-toplevel. Symlinks or Windows case variants collapse to the same worktree lock, while separate Git worktrees stay independent. - Honest Test Provenance (
test_evidence): Recordsprovenance(measuredvs.reportedvs.none),tested_head, anddirty_at_capture. If no test ran,statusis explicitlynot_run; if a test ran on a dirty tree, that fact stays attached so later edits cannot masquerade as verified. - Zero Secrets, Zero Raw Transcripts: Only structured notes and bounded
diff --statoutput (capped at 4,000 chars) enter the packet — never API tokens or chat logs.
2. Atomic Transfer Without Losing Dirty Files or Allowing Dual Writers
When Antigravity stops on quota after creating zh.mdx, we do not ask the unavailable model for a summary. Transfer happens deterministically in two steps:
handoff prepare: Read-only preflight that validates the target profile andTaskGrant, captures a freshcurrent_gitsnapshot from disk, and previewsHandoff Packet v1.handoff transfer: After the owner confirms the source agent has stopped (explicitly markedadvisoryinMANUALmode, not OS process-tree proof), an immediate SQLite transaction closesexec-01(outcome = "quota"), incrementsowner_epochfrom1to2, opensexec-02under the sametask_id, and leaves every staged, unstaged, and untracked file intact in the worktree.
If the old session somehow wakes up later and tries to record a checkpoint, its stale execution_id and owner_epoch are rejected immediately.
3. How Recovery Judges Whether the Checkpoint Is Still Valid
The main hazard on resume is blindly trusting Checkpoint #2's note — which still says untracked: [] and "next: create zh.mdx", even though zh.mdx already exists on disk. Agent Workspace applies four progressive checks before the target agent edits a single line.
Gate 1: Hard Continuity Check on Worktree, Branch, and HEAD
_revalidate_workspace_and_branch() compares the live worktree against the latest checkpoint:
- Unexpected branch switch (e.g.,
feat/home-featured→main) fails closed; - Same-branch rollback or divergence is checked via
git merge-base --is-ancestor <prev_head> <current_head>. Forward commits continue smoothly, whereas agit reset --hardor rebase requires explicit reconciliation (allow_branch_reconcile=True+reconcile_reason); - Grant target-branch mismatch cannot be bypassed even with reconciliation and requires a new
TaskGrant.
Gate 2: Automatic potentially_stale Flag
In build_handoff_packet(), the latest semantic checkpoint is automatically flagged potentially_stale = true whenever:
potentially_stale = bool(
(prev_exec and (prev_exec.get("outcome") == "quota" or prev_exec.get("state") == "INTERRUPTED"))
or (latest_cp and int(last_sem["sequence"]) < int(latest_cp["sequence"]))
)Because exec-01 ended with outcome == "quota", Codex immediately sees potentially_stale: true — warning it that edits may have occurred after the last semantic note.
Gate 3: Diffing Snapshot Facts Against Live current_git
Handoff Packet v1 places latest_machine_snapshot side by side with fresh current_git, making drift obvious at a glance:
| Field | Checkpoint #2 Snapshot | Live current_git at Handoff | Verdict |
|---|---|---|---|
branch / head | feat/home-featured / beb5910... | feat/home-featured / beb5910... | Base matches; safe to continue |
unstaged | categories.ts, HomeInteractive.tsx | Same two files | Completed edits match notes |
untracked | [] | ["content/projects/agent-workspace/zh.mdx"] | Notes lag disk: inspect zh.mdx instead of overwriting it |
test_evidence | npx tsc --noEmit passed (dirty_at_capture=true) | New file added afterwards | Historical reference only; re-verify after finishing |
Gate 4: Mandatory First Action Before Editing
Regardless of potentially_stale, every packet pins first_mandatory_action to:
- Run
git status --porcelain=v1 --untracked-files=all; - Run
git diff --cachedandgit diff; - Inspect all untracked files and reconcile disk reality against
completed/remaining/next_action— never runninggit reset,git clean, orgit stashto discard prior work.
Codex sees the partial zh.mdx, picks up cleanly from where Antigravity stopped, writes en.mdx, and runs a fresh build before closing the task.
Takeaway
Clean handoffs across coding agents do not require migrating chat history. Separating immutable intent, monotonic owner_epoch locks, read-only Git facts, honest test provenance, and explicit potentially_stale detection keeps recovery deterministic even when the previous agent dies mid-edit.
See the Agent Workspace project page for the full architecture and antigravity-env for the implementation and unit tests.