Graph-Planned Execution (Deterministic State Machine Protocol)
Provides a deterministic, graph-planned execution primitive for tasks requiring formal state tracking, non-negotiable human approval gates, isolated worktree sandboxing, and guaranteed safe rollback.
1. Overview: Loops vs. Graphs
While standard agent loops rely on conversational context to decide next steps, Graph Execution models tasks as a formal Directed Acyclic Graph (DAG) or finite state machine:
stateDiagram-v2
[*] --> TRIAGE: Task / Error Trigger
TRIAGE --> PLAN: 4-Box Automation Gate Passed
PLAN --> AWAITING_APPROVAL: Transaction Manifest Formulated
AWAITING_APPROVAL --> AUTHORIZED: User Explicit Approval ("Proceed")
AUTHORIZED --> CREATE_WORKTREE: Worktree Sandbox Initialized
CREATE_WORKTREE --> EXECUTE: Mutation Attempt (1-3)
EXECUTE --> VERIFY_GATE: Objective Verifier Run
VERIFY_GATE --> PRE_COMMIT_RECEIPT: Verifier Passed (exit 0)
VERIFY_GATE --> PLAN: Verifier Failed (Attempts < 3)
VERIFY_GATE --> ROLLBACK: Verifier Failed (Attempts == 3)
PRE_COMMIT_RECEIPT --> COMMIT: Staged Tree Validated & Committed
ROLLBACK --> FINAL_RECEIPT: Layer 2 Transferred & Worktree Removed
COMMIT --> FINAL_RECEIPT: Local Cycle Successful
FINAL_RECEIPT --> [*]: Terminal Receipt Logged
2. The Core Invariants
- State in Files, Not Prompts:
The active node, attempt count, and operation bounds are stored in
.agent/learning/evolution_state.json and mediated by a deterministic controller script (e.g., evolution_state.py).
- Proposal Mode Invariant (Read-Only Planning):
Prior to explicit user approval (
AWAITING_APPROVAL), the agent executes zero state-changing operations, creates no git worktrees, and runs no mutations.
- Transactional Worktree Sandboxing:
Mutations are executed inside an isolated git worktree (
../worktree-evolution-<cid>), keeping the main checkout 100% clean during execution.
- Verifier Sovereignty:
The agent cannot modify the test scripts, holdout sets, or evaluation criteria that judge its own work. Pre-execution hashes of verifiers are locked.
- Asymmetric Persistence:
If a task fails after maximum retry attempts (e.g., 3 attempts):
- Code changes are safely rolled back and the worktree is deleted.
- Failure learnings, negative constraints, and reproduction notes are permanently preserved in Layer 2 Markdown playbooks and debt logs before worktree teardown.
- Integrity Receipts:
No commit is permitted without an Evolution Integrity Receipt (
EVO-INTEGRITY-<cid>-<hash>) binding the exact staged git tree to the audit event sequence.
3. The Canonical Graph Node Lifecycle
Node 1: TRIAGE (Intake & Qualification)
- Ingests the task, error signature, or trigger.
- Evaluates the 4-Box Automation Gate:
- Is the problem structural or recurring?
- Is there an objective programmatic verifier (
exit 0)?
- Is there an iteration ceiling (max 3 attempts)?
- Is there an immutable persistence sink for learnings?
Node 2: PLAN (Proposal Mode)
- Formulates an immutable Transaction Manifest declaring:
mutation_targets: Exact relative file paths to edit.
verifier: The objective verification command (argv array).
forbidden_paths: Protected files (verifiers, test runners, root policies).
- Halts at
AWAITING_APPROVAL and presents the plan to the human.
Node 3: AUTHORIZED & CREATE_WORKTREE
- Entered only upon explicit user confirmation ("Proceed").
- Spawns the isolated worktree (
git worktree add -b evolution/<cid> ../worktree-evolution-<cid>).
- Captures baseline untracked files.
Node 4: EXECUTE (Surgical Mutation)
- Applies surgical edits against declared
mutation_targets inside the worktree.
- Records all created or modified files.
Node 5: VERIFY_GATE (Objective Proof Check)
- Validates verifier hashes are unmodified.
- Runs the verifier command in non-mutating mode.
- If
exit 0: transitions to PRE_COMMIT_RECEIPT.
- If
exit != 0 and attempts < 3: increments attempt count and transitions to PLAN.
- If
exit != 0 and attempts == 3: transitions to ROLLBACK.
Node 6: COMMIT or ROLLBACK (Asymmetric Resolution)
- On Pass: Persists confirmed knowledge to Layer 2 wiki $\rightarrow$ Stages files $\rightarrow$ Generates Pre-Commit Receipt $\rightarrow$ Commits.
- On 3rd Failure: Transfers failure insights to main checkout $\rightarrow$ Removes worktree $\rightarrow$ Logs open debt item.
Node 7: FINAL_RECEIPT (Closure)
- Emits final receipt token and marks cycle
COMPLETED or ESCALATED.
Operational References
See references/PATTERN_GUIDE.md and references/acceptance-criteria.md for complete state machine contracts.
1---2name: graph-execution3plugin: agent-orchestration4description: "Executes complex workflows using deterministic graph-state machines, explicit transition guards, transactional worktree isolation, receipt gates, and automatic rollback on verification failure."5allowed-tools: Bash, Read, Write6---78# Graph-Planned Execution (Deterministic State Machine Protocol)910Provides a deterministic, graph-planned execution primitive for tasks requiring formal state tracking, non-negotiable human approval gates, isolated worktree sandboxing, and guaranteed safe rollback.1112---1314## 1. Overview: Loops vs. Graphs1516While standard agent loops rely on conversational context to decide next steps, **Graph Execution** models tasks as a formal Directed Acyclic Graph (DAG) or finite state machine:1718```mermaid19stateDiagram-v220 [*] --> TRIAGE: Task / Error Trigger21 TRIAGE --> PLAN: 4-Box Automation Gate Passed22 PLAN --> AWAITING_APPROVAL: Transaction Manifest Formulated23 AWAITING_APPROVAL --> AUTHORIZED: User Explicit Approval ("Proceed")24 AUTHORIZED --> CREATE_WORKTREE: Worktree Sandbox Initialized25 CREATE_WORKTREE --> EXECUTE: Mutation Attempt (1-3)26 EXECUTE --> VERIFY_GATE: Objective Verifier Run27 VERIFY_GATE --> PRE_COMMIT_RECEIPT: Verifier Passed (exit 0)28 VERIFY_GATE --> PLAN: Verifier Failed (Attempts < 3)29 VERIFY_GATE --> ROLLBACK: Verifier Failed (Attempts == 3)30 PRE_COMMIT_RECEIPT --> COMMIT: Staged Tree Validated & Committed31 ROLLBACK --> FINAL_RECEIPT: Layer 2 Transferred & Worktree Removed32 COMMIT --> FINAL_RECEIPT: Local Cycle Successful33 FINAL_RECEIPT --> [*]: Terminal Receipt Logged34```3536---3738## 2. The Core Invariants39401. **State in Files, Not Prompts:**41 The active node, attempt count, and operation bounds are stored in `.agent/learning/evolution_state.json` and mediated by a deterministic controller script (e.g., `evolution_state.py`).422. **Proposal Mode Invariant (Read-Only Planning):**43 Prior to explicit user approval (`AWAITING_APPROVAL`), the agent executes **zero state-changing operations**, creates **no git worktrees**, and runs **no mutations**.443. **Transactional Worktree Sandboxing:**45 Mutations are executed inside an isolated git worktree (`../worktree-evolution-<cid>`), keeping the main checkout 100% clean during execution.464. **Verifier Sovereignty:**47 The agent cannot modify the test scripts, holdout sets, or evaluation criteria that judge its own work. Pre-execution hashes of verifiers are locked.485. **Asymmetric Persistence:**49 If a task fails after maximum retry attempts (e.g., 3 attempts):50 - **Code changes are safely rolled back** and the worktree is deleted.51 - **Failure learnings, negative constraints, and reproduction notes** are permanently preserved in Layer 2 Markdown playbooks and debt logs before worktree teardown.526. **Integrity Receipts:**53 No commit is permitted without an **Evolution Integrity Receipt** (`EVO-INTEGRITY-<cid>-<hash>`) binding the exact staged git tree to the audit event sequence.5455---5657## 3. The Canonical Graph Node Lifecycle5859### Node 1: `TRIAGE` (Intake & Qualification)60- Ingests the task, error signature, or trigger.61- Evaluates the **4-Box Automation Gate**:62 1. Is the problem structural or recurring?63 2. Is there an objective programmatic verifier (`exit 0`)?64 3. Is there an iteration ceiling (max 3 attempts)?65 4. Is there an immutable persistence sink for learnings?6667### Node 2: `PLAN` (Proposal Mode)68- Formulates an immutable **Transaction Manifest** declaring:69 - `mutation_targets`: Exact relative file paths to edit.70 - `verifier`: The objective verification command (`argv` array).71 - `forbidden_paths`: Protected files (verifiers, test runners, root policies).72- Halts at `AWAITING_APPROVAL` and presents the plan to the human.7374### Node 3: `AUTHORIZED` & `CREATE_WORKTREE`75- Entered **only** upon explicit user confirmation ("Proceed").76- Spawns the isolated worktree (`git worktree add -b evolution/<cid> ../worktree-evolution-<cid>`).77- Captures baseline untracked files.7879### Node 4: `EXECUTE` (Surgical Mutation)80- Applies surgical edits against declared `mutation_targets` inside the worktree.81- Records all created or modified files.8283### Node 5: `VERIFY_GATE` (Objective Proof Check)84- Validates verifier hashes are unmodified.85- Runs the verifier command in non-mutating mode.86- If `exit 0`: transitions to `PRE_COMMIT_RECEIPT`.87- If `exit != 0` and attempts < 3: increments attempt count and transitions to `PLAN`.88- If `exit != 0` and attempts == 3: transitions to `ROLLBACK`.8990### Node 6: `COMMIT` or `ROLLBACK` (Asymmetric Resolution)91- **On Pass:** Persists confirmed knowledge to Layer 2 wiki $\rightarrow$ Stages files $\rightarrow$ Generates Pre-Commit Receipt $\rightarrow$ Commits.92- **On 3rd Failure:** Transfers failure insights to main checkout $\rightarrow$ Removes worktree $\rightarrow$ Logs open debt item.9394### Node 7: `FINAL_RECEIPT` (Closure)95- Emits final receipt token and marks cycle `COMPLETED` or `ESCALATED`.9697---9899## Operational References100See [`references/PATTERN_GUIDE.md`](../references/PATTERN_GUIDE.md) and [`references/acceptance-criteria.md`](references/acceptance-criteria.md) for complete state machine contracts.