Skip to content

AI Agent Harness Checkpointing

AI coding agents often reformat untouched lines, rename local variables, or accidentally change public function signatures during a refactor. Wrapping edit turns with canontra snap lets harnesses verify what actually changed and enforce strict API boundaries without touching Git history.

Terminal window
# 1. Create .canontra/snaps/ + .canontra/.gitignore and record baseline snap_0
canontra snap init --json
# 2. Agent applies edits, then checkpoints the turn (--skip-noop skips formatting-only turns)
canontra snap mark --label "refactor-helper" --agent "coding-agent" --skip-noop --json
# 3. Verify public signatures (F2), dependencies (F3), and types (F_T) did not break
canontra snap guard --against snap_0 --forbid F2,F3,F_T --allow-paths "tests/**" --json
# 4. If guard exits 1, inspect compact semantic diff or preview/run rollback
canontra snap diff snap_0 HEAD --compact --json
canontra snap rewind snap_0 --dry-run --json
canontra snap rewind snap_0 --json
# 5. View checkpoint log or clean up old snapshots
canontra snap log -n 10 --json
canontra snap gc --keep-last 10 --json

2. Claude Code PostToolUse Hook Configuration

Section titled “2. Claude Code PostToolUse Hook Configuration”

Run canontra snap guard automatically after Edit or Write tool calls in .claude/settings.json:

{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "canontra snap guard --against snap_0 --forbid F2,F3,F_T --allow-paths \"tests/**\" --json"
}
]
}
]
}
}

3. Reading Compact Diffs (snap diff --compact --json)

Section titled “3. Reading Compact Diffs (snap diff --compact --json)”

Instead of passing noisy line-by-line Git diffs back into an LLM context window, snap diff --compact --json returns the turn classification (no_change, semantic_noop, internal_logic, or contract_change), mutated tiers, symbol_changes, and CSR recommended_tests:

  • level1_trivia_count: Files with only formatting, comment, or local variable rename edits.
  • level2_logic: Files where internal logic changed (F1, F_CF, F_DF) while public APIs stayed unchanged.
  • level3_contract: Files where public declarations (F2), dependencies (F3), or structural types (F_T) changed.
  • In-memory diffFileBundles: 733.1 ns (RQ19)
  • 1,000-file delta check (1 dirty file): 1.73 ms (RQ19)
  • Warm CLI canontra snap mark --skip-noop: 143.61 ms to 477.16 ms across small, medium, and large repos (chalk through ripgrep), and 3,978.55 ms on 994-file prometheus.