digraph SpecDoD {
    graph [
        goal="Satisfy every Definition of Done checkbox across both specs (unified-llm-spec.md, coding-agent-loop-spec.md). The implementation is in Rust under crates/. Do NOT modify the spec files. Only modify implementation code.",
        default_max_retries="3",
        retry_target="triage",
        model_stylesheet="
            * { model: claude-opus-4-6;}
            .audit { reasoning_effort: high; }
            .fix { model: claude-opus-4-6; reasoning_effort: high; }
            #final_audit { reasoning_effort: high; }
        "
    ]

    start [shape=Mdiamond]
    exit  [shape=Msquare]

    /*------------------------------------------------------------------------
     * Phase 1: Baseline audit — read every DoD checkbox, check the code
     *----------------------------------------------------------------------*/

    audit_llm [
        label="Audit: Unified LLM Client DoD",
        shape=box,
        class="audit",
        prompt="Read docs/specs/unified-llm-spec.md Section 8 (Definition of Done) in full. Then read every source file under crates/llm/src/.

For EACH checkbox in sections 8.1 through 8.10, evaluate whether the current Rust implementation satisfies it. Be strict — a checkbox is only checked if the feature is fully implemented and would work correctly at runtime.

Respond with ONLY a JSON object (no prose) -- don't write it out as a file:
{
  \"spec\": \"unified-llm\",
  \"sections\": {
    \"8.1\": { \"title\": \"Core Infrastructure\", \"items\": [ {\"text\": \"...\", \"pass\": true/false, \"reason\": \"...\"} ] },
    ...
  },
  \"total\": N,
  \"passed\": M,
  \"failed\": K,
  \"failed_items\": [ {\"section\": \"8.2\", \"text\": \"...\", \"reason\": \"...\"} ]
}

Be thorough. Check every single checkbox."
    ]

    audit_agent [
        label="Audit: Coding Agent Loop DoD",
        shape=box,
        class="audit",
        prompt="Read docs/specs/coding-agent-loop-spec.md Section 9 (Definition of Done) in full. Then read every source file under crates/agent/.

For EACH checkbox in sections 9.1 through 9.13, evaluate whether the current Rust implementation satisfies it. Be strict.

Respond with ONLY a JSON object (no prose) -- don't write it out as a file:
{
  \"spec\": \"coding-agent-loop\",
  \"sections\": { ... },
  \"total\": N,
  \"passed\": M,
  \"failed\": K,
  \"failed_items\": [ {\"section\": \"9.1\", \"text\": \"...\", \"reason\": \"...\"} ]
}

Be thorough. Check every single checkbox."
    ]

    /*------------------------------------------------------------------------
     * Phase 2: Triage — merge results, prioritize failures, decide next step
     *----------------------------------------------------------------------*/

    triage [
        label="Triage & Prioritize",
        shape=box,
        prompt="You have two audit reports in context (from audit_llm, audit_agent). Merge them into a single prioritized list of ALL failing DoD checkboxes.

Group failures into:
1. IMPLEMENTABLE — can be fixed by writing/modifying Rust code (functions, handlers, logic)
2. STRUCTURAL — requires new files, new modules, or significant architecture changes
3. DEFERRED — requires external resources (real API keys, network access, interactive testing) that cannot be done in a code-only pass

For each IMPLEMENTABLE item, identify the exact file(s) to modify and briefly describe the fix.

Respond with JSON (not by writing out a file):
{
  \"total_failing\": N,
  \"implementable\": [ {\"spec\": \"...\", \"section\": \"...\", \"text\": \"...\", \"files\": [\"...\"], \"fix\": \"...\"} ],
  \"structural\": [ ... ],
  \"deferred\": [ ... ],
  \"verdict\": \"all_clear\" | \"has_fixes\" | \"only_deferred\"
}

If total_failing == 0 or verdict == \"only_deferred\", set preferred_next_label to \"Done\".
Otherwise set preferred_next_label to \"Fix\"."
    ]

    /*------------------------------------------------------------------------
     * Phase 3: Fix — implement the highest-priority fixes
     *----------------------------------------------------------------------*/

    fix_batch [
        label="Implement Fixes",
        shape=box,
        class="fix",
        goal_gate=true,
        prompt="The triage report identified IMPLEMENTABLE DoD failures. Pick the top 5 most impactful items (or all if fewer than 5) and implement the fixes in Rust.

Begin by making sure the build is green with `cargo test`

For each fix:
1. Read the relevant source file(s)
2. Make the minimal change needed to satisfy the DoD checkbox
3. Write the modified file(s) -- update tests as needed
4. Verify the fix and the tests pass (`cargo test`)

Constraints:
- Do NOT modify any files under docs/specs/ (those are the specs)
- Do NOT add external dependencies beyond what's already used
- Keep changes minimal and focused — one checkbox per fix
- Maintain the existing code style

Respond with JSON (not by writing out a file):
{
  \"fixes_applied\": [ {\"spec\": \"...\", \"section\": \"...\", \"text\": \"...\", \"files_changed\": [\"...\"], \"description\": \"...\"} ],
  \"count\": N,
  \"remaining_implementable\": M
}

If remaining_implementable > 0, set preferred_next_label to \"more_fixes_needed\".
Otherwise set preferred_next_label to \"Re-audit\"."
    ]

    /*------------------------------------------------------------------------
     * Phase 4: Build verification
     *----------------------------------------------------------------------*/

    build_check [
        label="Build & Smoke Test",
        shape=parallelogram,
        script="cargo build 2>&1 && echo '---BUILD OK---' && cargo test 2>&1 && echo '---ALL TESTS PASSED---'",
        timeout="120s"
    ]

    /*------------------------------------------------------------------------
     * Phase 5: Build failure recovery
     *----------------------------------------------------------------------*/

    build_fix [
        label="Fix Build Errors",
        shape=box,
        class="fix",
        prompt="The build or smoke tests failed. Read the build output from context (command.output key). Diagnose the compilation errors or test failures and fix them.

Read the relevant source files, identify the issue, and write corrected versions. Common issues:
- Missing includes
- Type mismatches
- Undeclared functions

Output the fixes applied and ensure the code will compile cleanly with: cargo build"
    ]

    /*------------------------------------------------------------------------
     * Phase 6: Final audit to confirm fixes worked
     *----------------------------------------------------------------------*/

    final_audit [
        label="Final Verification Audit",
        shape=box,
        prompt="This is a verification pass. Re-read both spec DoD sections:
- docs/specs/unified-llm-spec.md Section 8
- docs/specs/coding-agent-loop-spec.md Section 9

And re-read the implementation files that were changed in this iteration.

Check ONLY the items that were previously failing. Have they been fixed?

Respond with JSON (not by writing out a file):
{
  \"verified_fixed\": [ {\"spec\": \"...\", \"section\": \"...\", \"text\": \"...\"} ],
  \"still_failing\": [ {\"spec\": \"...\", \"section\": \"...\", \"text\": \"...\", \"reason\": \"...\"} ],
  \"newly_broken\": [ ... ],
  \"remaining_total\": N
}

If remaining_total == 0 (ignoring DEFERRED items), set preferred_next_label to \"Complete\".
Otherwise set preferred_next_label to \"more_work_needed\"."
    ]

    /*------------------------------------------------------------------------
     * Phase 7: Human gate — confirm completion or push for another round
     *----------------------------------------------------------------------*/

    review_gate [
        label="A) Accept & finish\nB) Push for another round",
        shape=hexagon
    ]

    /*------------------------------------------------------------------------
     * Edges
     *----------------------------------------------------------------------*/

    start -> audit_llm

    /* Sequential audit chain */
    audit_llm -> audit_agent
    audit_agent -> triage

    /* Triage decision */
    triage -> exit           [label="Done", condition="preferred_label=Done"]
    triage -> fix_batch      [label="Fix", condition="preferred_label=Fix", weight=10]
    triage -> exit           [label="Only deferred remain"]

    /* Fix -> build check */
    fix_batch -> build_check

    /* Build check outcomes */
    build_check -> final_audit     [label="Build OK", condition="outcome=success"]
    build_check -> build_fix       [label="Build failed"]

    /* Build fix loops back to build check */
    build_fix -> build_check

    /* Fix batch can loop for more fixes */
    fix_batch -> fix_batch   [label="More fixes needed", condition="preferred_label=more_fixes_needed", loop_restart=true]

    /* Final audit outcomes */
    final_audit -> review_gate    [label="Complete", condition="preferred_label=Complete"]
    final_audit -> triage         [label="More work needed"]

    /* Human review gate */
    review_gate -> exit           [label="A) Accept"]
    review_gate -> triage         [label="B) Another round"]
}
