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 — no post-commit, so agent additions appear as uncommitted changes in the editor
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 two-phase git flow (pre-commit user, no post-commit agent) means your editor shows green diff gutters for everything the agent added. On the next run, those changes get committed as part of the pre-commit step.
Cleanup: agent-doc clean <file> squashes all session commits into one.