Run Flow
Overview
┌──────────┐ hotkey ┌────────────┐ diff + prompt ┌───────┐
│ Editor │ ──────> │ agent-doc │ ──────────────> │ Agent │
│ │ │ │ <────────────── │ API │
│ reload │ <────── │ write+snap │ └───────┘
└──────────┘ │ git commit │
└────────────┘
Step by step
- Read document and load snapshot (last-known state from previous run)
- Compute diff — if empty, exit early (double-run guard)
- Pre-commit user's changes via
git add -f+git commit(baseline for diff gutters) - Send diff + full document to agent, resuming session if one exists
- Build response — original content + session ID update +
## Assistantblock +## Userblock - Check for concurrent edits — re-read the file
- Merge if needed — 3-way merge via
git merge-fileif file changed during agent response - Write merged content back to file
- Save snapshot and close out — the response is committed in the same cycle, then post-commit cleanup brings the live document, snapshot, and editor-facing state back in line with committed
HEAD. Only genuine later local edits remain uncommitted.
Session continuity
- Empty
session:— forks from the most recent agent session (inherits context) session: <uuid>— resumes that specific session- Delete
session:value — next run starts fresh
Merge-safe writes
If you edit the document while the agent is responding:
- Clean merge (edits in different regions) — merged automatically. Message: "Merge successful — user edits preserved."
- Conflict (edits in the same region as the response) — conflict markers written to the file with labels
agent-response,original,your-edits. Message: "WARNING: Merge conflicts detected."
The merge uses git merge-file -p --diff3, which handles edge cases (whitespace, encoding, partial overlaps) better than a custom implementation.
Git integration
| Flag | Behavior |
|---|---|
-b | Auto-create branch agent-doc/<filename> on first run |
| (none) | Pre-commit user changes to current branch |
--no-git | Skip git entirely |
The closeout flow still commits the user's baseline before generating a response, but a successful response turn now also crosses the binary-owned commit boundary before it exits. Agent-owned (HEAD) / boundary churn is cleaned up in the same cycle instead of being left behind for the next run. Only real follow-up edits after that closeout remain as uncommitted local changes.
Cleanup: agent-doc clean <file> squashes all session commits into one.