Skip to content

Draft Workflow (draft & draft_batch) ​

When an agent is not ready to mutate your live working tree — because a build is running, another agent holds an edit lock, you want a human or quorum reviewer to inspect a diff first, or a subagent is running in read-and-draft mode — Kompile provides a version-controlled draft store backed by a bare JGit repository at <project>/.kompile/drafts.git.

Every draft revision, apply, and revert is attributed to the exact session, agent, and model, checked against live file drift and edit_coordinator locks, and automatically recorded in diff_index.

WARNING

Why draft exists (DraftEnforcerPolicy): Ad-hoc workarounds like copying repo files into ~/.cache/<session>-overlay-vN directories, writing patch hunks into *-draft.md, or keeping custom manifest.txt / diff3 scripts are blocked by the Enforcer (DraftEnforcerPolicy, configurable via draftEnforcerEnabled). Always use draft / draft_batch instead.


Lifecycle at a glance ​

mermaid
flowchart LR
    C["create\n(files / edits / patches\nagainst live base)"] --> R["revise\n(iterate, add files,\ndrop_paths)"]
    R --> S["show / log / list\n(inspect diff vs. base\n& revision trail)"]
    S --> CHK{"Live files moved\nsince draft base?"}
    CHK -->|Yes| RB["rebase\n(3-way merge onto\ncurrent project files)"]
    RB --> APP["apply\n(atomic all-or-nothing;\nrefuses drift & locks)"]
    CHK -->|No| APP
    APP --> REV["revert\n(undo apply while files\nremain unchanged)"]
    R --> SUP["discard / supersede\n(fold into another draft\nvia supersede_with)"]

How drafts are stored & audited ​

  • Bare JGit repository: <project>/.kompile/drafts.git — completely isolated from your project's own .git history and working tree until you explicitly apply.
  • Refs:
    • Draft heads: refs/kompile/drafts/<draft_id>
    • Apply/revert records: refs/kompile/applies/<apply_id>
  • Mandatory model attribution: Every mutating commit carries Kompile-Session, Kompile-Agent, and Kompile-Model trailers. If the caller's agent config doesn't already specify a model, draft requires model=<model-id> explicitly.
  • diff_index integration (DraftDiffIndexFeed): Every create, revise, rebase, apply, and revert writes an indexed entry (source=kompile-draft, diffType=draft|draft-apply|draft-revert) carrying model, draftId, and commitId so historical draft activity is searchable alongside live edits.

All 11 draft actions ​

ActionMutating?Key ParametersWhat It Does
createYestitle, files / edits / patches, optional note, modelCreates a new draft based on the current live project files and commits revision 1
reviseYesdraft_id, files / edits / patches / drop_paths, optional expected_revision, noteAppends a new revision on top of the draft's current head (fails if expected_revision drifted)
showNodraft_id, optional revisionRenders the unified diff of a revision against its base (bounded to 60k chars)
logNodraft_idShows the full revision history with timestamps, notes, session, agent, and model
listNooptional status (open default, all, applied, reverted, discarded, superseded), session_idLists drafts and their current status
rebaseYesdraft_id, optional notePerforms a three-way merge of the draft onto the current live project files
applyYesdraft_id, optional revision, note, agent_nameWrites the revision into the project all-or-nothing — refuses if any target file drifted from the draft's base or is locked by another agent via edit_coordinator
revertYesapply_id, optional noteCleanly reverses an apply — only allowed while the working-tree files still match the exact applied state
discardYesdraft_id, optional noteMarks an unapplied draft as discarded without touching project files
supersedeYesdraft_id, supersede_with, optional noteCloses a draft because another draft's revision now carries its changes
auditNooptional session_idPrints the complete apply / revert audit trail

How changes are supplied (files, edits, patches) ​

create and revise accept three familiar input forms (matching write, edit, and edit_patch rules):

  • files: [{"file_path": "...", "content": "..."}] — whole-file content (ideal for new files).
  • edits: [{"file_path": "...", "old_string": "...", "new_string": "...", "replace_all": false}] — exact replacements applied against the draft's current content (or the live project file the first time that file enters the draft).
  • patches: [{"file_path": "...", "patch": "..."}] — per-file unified hunks located by content.
  • drop_paths: ["path/to/file"] (on revise) — removes a file from the draft.

Important invariant: folding instead of stacking drafts ​

Every draft is anchored to the live project files, never stacked on top of another unapplied draft. If Draft B needs to build on an unapplied Draft A:

  1. Fold Draft B's edits into a new revision of Draft A (draft action=revise draft_id=<A>).
  2. Close Draft B with draft action=supersede draft_id=<B> supersede_with=<A>.

This guarantees that rebase, apply, and revert always operate on a clean, unambiguous base.


Batching multiple draft operations (draft_batch) ​

When reviewing, revising, or applying a set of drafts, use draft_batch instead of sequential draft calls:

  • Runs up to 20 operations (operations[]) in order through the same DraftTool engine.
  • Top-level model and agent_name are automatically inherited by every operation that omits them.
  • Each apply inside the batch stays atomic, drift-checked, lock-checked, and audited in diff_index.
  • By default, one failing operation reports its error while the remaining operations continue (with optional stop_on_error to halt on the first failure).

Ask in chat: "Show draft-a1b2, rebase it onto the latest working-tree files, and apply it atomically if the rebase is clean — stopping on the first error."

The agent bundles show → rebase → apply into one draft_batch call instead of three separate model round-trips.


Standard patterns for using drafts ​

1. Staging edits during a busy build window ​

When a long build/test process is running and modifying live files would corrupt the compile, tell the agent:

"While the Maven build finishes, stage the next fix in a draft so we can rebase and apply cleanly as soon as the build exits."

2. Read-and-draft subagents + lead review ​

Dispatch parallel workers to investigate and propose fixes as drafts without ever racing over live files:

  1. Worker creates a draft (draft action=create ...) and posts the draft_id in its progress report.
  2. Lead agent (or you) inspects the diff (draft action=show), optionally verifies it with lsp action=check using overlay buffers, and applies atomically (draft action=apply) once approved.

3. Safe experimentation with guaranteed rollback ​

Because apply records an apply_id and checks exact content hashes on revert, you can apply a draft, run your test suite, and immediately revert cleanly with draft action=revert apply_id=<id> if anything fails.


Released under the Apache-2.0 License.