Back to blog
Developer Tools··5 min read

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.

#Agent Workspace#Task Continuity#Git#Workflow#Coding Agents
—views

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:

  1. I create a Task with an immutable, owner-approved DevelopmentBrief (anchored by sha256) and issue a TaskGrant scoped to feat/home-featured for Antigravity.
  2. Antigravity runs as exec-01 (owner_epoch = 1), updates src/config/categories.ts and src/components/home/HomeInteractive.tsx, runs a typecheck, and records Checkpoint #2.
  3. Next, it creates content/projects/agent-workspace/zh.mdx — and hits its quota limit before writing the English version or running a build.
  4. I switch to Codex (exec-02) on the exact same local worktree.
Rendering diagram…

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:

json
{
  "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 normalized git rev-parse --git-common-dir plus --show-toplevel. Symlinks or Windows case variants collapse to the same worktree lock, while separate Git worktrees stay independent.
  • Honest Test Provenance (test_evidence): Records provenance (measured vs. reported vs. none), tested_head, and dirty_at_capture. If no test ran, status is explicitly not_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 --stat output (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:

  1. handoff prepare: Read-only preflight that validates the target profile and TaskGrant, captures a fresh current_git snapshot from disk, and previews Handoff Packet v1.
  2. handoff transfer: After the owner confirms the source agent has stopped (explicitly marked advisory in MANUAL mode, not OS process-tree proof), an immediate SQLite transaction closes exec-01 (outcome = "quota"), increments owner_epoch from 1 to 2, opens exec-02 under the same task_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 a git reset --hard or 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:

python
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:

FieldCheckpoint #2 SnapshotLive current_git at HandoffVerdict
branch / headfeat/home-featured / beb5910...feat/home-featured / beb5910...Base matches; safe to continue
unstagedcategories.ts, HomeInteractive.tsxSame two filesCompleted edits match notes
untracked[]["content/projects/agent-workspace/zh.mdx"]Notes lag disk: inspect zh.mdx instead of overwriting it
test_evidencenpx tsc --noEmit passed (dirty_at_capture=true)New file added afterwardsHistorical reference only; re-verify after finishing

Gate 4: Mandatory First Action Before Editing

Regardless of potentially_stale, every packet pins first_mandatory_action to:

  1. Run git status --porcelain=v1 --untracked-files=all;
  2. Run git diff --cached and git diff;
  3. Inspect all untracked files and reconcile disk reality against completed / remaining / next_action — never running git reset, git clean, or git stash to 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.