skills/iterate-on-implementation/SKILL.md
Iteratively refine a feature implementation by identifying and fixing bugs, edge cases, and improvements
npx skillsauth add jankneumann/agentic-coding-tools iterate-on-implementationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Iteratively refine a feature implementation after /implement-feature completes. Each iteration reviews the code, identifies improvements, implements fixes, and commits — repeating until only low-criticality findings remain or max iterations are reached.
$ARGUMENTS - OpenSpec change-id (required), optionally followed by --max <N> (default: 5), --threshold <level> (default: "medium"; values: "critical", "high", "medium", "low"), and --vendor-review (dispatch multi-vendor review after iterate loop converges; automatic in coordinated tier)
openspec/<change-id> exists with implementation commits (or the operator-mandated branch when OPENSPEC_BRANCH_OVERRIDE is set)openspec/changes/<change-id>//implement-feature first if no implementation existsWhen this skill delegates refinement or validation work, treat the
provider-neutral dispatch adapter as the canonical cross-provider path. Claude
Code, Codex, and Gemini/Jules are first-class providers when configured;
Claude-style Task(...) or Agent(...) snippets are provider-specific
examples, with inline execution as the fallback.
Use OpenSpec-generated runtime assets first, then CLI fallback:
.claude/commands/opsx/*.md or .claude/skills/openspec-*/SKILL.md.codex/skills/openspec-*/SKILL.md.gemini/commands/opsx/*.toml or .gemini/skills/openspec-*/SKILL.mdopenspec CLI commandsUse docs/coordination-detection-template.md as the shared detection preamble.
CAN_* flag is trueAt skill start, run the coordination detection preamble and set:
COORDINATOR_AVAILABLECOORDINATION_TRANSPORT (mcp|http|none)CAN_LOCK, CAN_QUEUE_WORK, CAN_HANDOFF, CAN_MEMORY, CAN_GUARDRAILSIf CAN_HANDOFF=true, read recent handoff context:
read_handoff"<skill-base-dir>/../coordination-bridge/scripts/coordination_bridge.py" try_handoff_read(...)If CAN_MEMORY=true, recall relevant implementation-iteration memories:
recall"<skill-base-dir>/../coordination-bridge/scripts/coordination_bridge.py" try_recall(...)On recall/handoff failure, continue with standalone iteration and log informationally.
# Parse change-id from argument or current branch
BRANCH=$(git branch --show-current)
CHANGE_ID=${ARGUMENTS%% *} # First arg, or detect from branch
CHANGE_ID=${CHANGE_ID:-$(echo $BRANCH | sed 's/^openspec\///')}
# Defaults
MAX_ITERATIONS=5
THRESHOLD="medium" # critical > high > medium > low
# Detect worktree context and resolve OpenSpec path
# Note: detect auto-discovers context from the working directory;
# agent-id information is available via the worktree registry if needed.
eval "$(python3 "<skill-base-dir>/../worktree/scripts/worktree.py" detect)"
if [[ "$IN_WORKTREE" == "true" ]]; then
echo "Running in worktree. OpenSpec path: $OPENSPEC_PATH"
fi
Parse optional flags from $ARGUMENTS:
--max <N> overrides MAX_ITERATIONS--threshold <level> overrides THRESHOLD--vendor-review sets VENDOR_REVIEW=true# Vendor review: explicit flag OR auto-enable in coordinated tier
VENDOR_REVIEW=false
if [[ "$ARGUMENTS" == *"--vendor-review"* ]] || [[ "$COORDINATOR_AVAILABLE" == "true" ]]; then
VENDOR_REVIEW=true
fi
# Resolve the expected feature branch. This reads from the worktree registry
# (preferred — reflects what plan/implement actually used) and falls back to
# OPENSPEC_BRANCH_OVERRIDE env var or the openspec/<change-id> default.
eval "$(python3 "<skill-base-dir>/../worktree/scripts/worktree.py" resolve-branch "$CHANGE_ID")"
FEATURE_BRANCH="$BRANCH"
# Verify on feature branch
CURRENT_BRANCH="$(git branch --show-current)"
if [[ "$CURRENT_BRANCH" != "$FEATURE_BRANCH" ]]; then
echo "ERROR: on '$CURRENT_BRANCH' but expected '$FEATURE_BRANCH' (source: $BRANCH_SOURCE)" >&2
echo "Hint: check out '$FEATURE_BRANCH' and re-run, or run /implement-feature first" >&2
exit 1
fi
# Verify proposal exists
openspec show $CHANGE_ID
# Verify implementation commits exist
git log --oneline main..HEAD
If not on the feature branch, check out $FEATURE_BRANCH (which honors OPENSPEC_BRANCH_OVERRIDE). If no implementation commits exist, abort and recommend running /implement-feature first.
Preferred path:
opsx:continue equivalent) to create or extend impl-findings.CLI fallback path:
openspec instructions impl-findings --change "$CHANGE_ID"
openspec status --change "$CHANGE_ID"
Ensure openspec/changes/<change-id>/impl-findings.md exists and append each iteration's findings there.
If openspec/changes/<change-id>/rework-report.json exists, load it as the primary input for prioritizing fixes. The rework report provides machine-readable failure routing: scenario IDs, visibility, requirement refs, implicated files, and recommended actions.
REWORK_REPORT="openspec/changes/$CHANGE_ID/rework-report.json"
if [[ -f "$REWORK_REPORT" ]]; then
echo "Rework report found — using as primary iteration input"
# Parse failures with recommended_action == "iterate"
# Prioritize those failures over code-review-discovered findings
fi
When a rework report is present:
recommended_action: "iterate" over self-discovered findingsrecommended_action: "defer" unless the threshold is set to "low"recommended_action: "revise-spec" for spec revision rather than code changes/cleanup-feature)ITERATION=1
Read the following files to understand intent and current state:
$OPENSPEC_PATH/changes/<change-id>/proposal.md$OPENSPEC_PATH/changes/<change-id>/design.md$OPENSPEC_PATH/changes/<change-id>/tasks.mdgit diff --name-only main..HEAD)Note: In worktree mode, OpenSpec files are in the main repository, not the worktree.
Produce a structured improvement analysis with findings in this format:
| # | Type | Criticality | Description | Proposed Fix | |---|------|-------------|-------------|--------------| | 1 | bug/security/edge-case/workflow/performance/UX/observability/resilience | critical/high/medium/low | What the issue is | How to fix it |
Type categories:
Criticality levels:
Schema type mapping (for translating implementation findings to review-findings.schema.json types at the dispatch/consensus boundary):
| Impl Dimension | Schema Type(s) | Notes |
|---|---|---|
| bug | correctness | Logic errors and crashes |
| security | security | Direct mapping |
| edge-case | correctness, resilience | Unhandled error recovery → resilience; boundary conditions → correctness |
| workflow | style, architecture | DX/tooling concerns |
| performance | performance | Direct mapping |
| UX | style, correctness | Bad error messages = style; wrong output = correctness |
| observability | observability | Direct mapping |
| resilience | resilience | Direct mapping |
Schema types spec_gap, contract_mismatch, and compatibility have no matching implementation dimension — these are evaluated by parallel-review-implementation (spec/contract compliance) and iterate-on-plan (compatibility) respectively.
Stop iterating if:
If stopping, skip to the After Loop section below.
Otherwise, continue to step 6.
Before dispatching fix agents, resolve the implementer archetype for escalation:
from src.agents_config import load_archetypes_config, resolve_model
archetypes = load_archetypes_config()
implementer = archetypes.get("implementer")
runner = archetypes.get("runner")
impl_model = resolve_model(implementer, package_metadata) if implementer else "sonnet"
runner_model = resolve_model(runner, {}) if runner else "haiku"
When multiple findings target different files, fix them concurrently:
# Spawn parallel agents for independent fixes
Task(
subagent_type="general-purpose",
model=impl_model, # archetype: implementer (sonnet, or opus if escalated)
description="Fix finding 1: <type> in <file>",
prompt="Fix this issue in OpenSpec <change-id> implementation:
## Finding
Type: <type>
Criticality: <criticality>
Description: <description>
Proposed Fix: <fix>
## File Scope
You MAY modify: <specific file(s)>
You must NOT modify any other files.
## Process
1. Read the file and understand the issue
2. Implement the fix
3. Run relevant tests
4. Report changes made
Do NOT commit - the orchestrator handles commits.",
run_in_background=true
)
Rules:
Run all quality checks concurrently using Task() with run_in_background=true:
# Launch all checks in parallel (single message, multiple Task calls)
Task(subagent_type="Bash", model=runner_model, prompt="Run pytest and report pass/fail with summary", run_in_background=true)
Task(subagent_type="Bash", model=runner_model, prompt="Run mypy src/ and report any type errors", run_in_background=true)
Task(subagent_type="Bash", model=runner_model, prompt="Run ruff check . and report any linting issues", run_in_background=true)
Task(subagent_type="Bash", model=runner_model, prompt="Run openspec validate $CHANGE_ID --strict", run_in_background=true)
Result Aggregation:
Example output format:
Quality Check Results:
✓ pytest: 42 tests passed
✗ mypy: 3 type errors in src/auth.py
✓ ruff: No issues
✓ openspec validate: Valid
Failures to address this iteration:
- mypy: src/auth.py:15 - Missing return type annotation
Fix any failures before proceeding. If fixes introduce new issues, address them within this iteration.
Review whether genuinely new patterns, lessons, or gotchas were discovered in this iteration. If so, update:
Follow the existing convention:
Do NOT add redundant documentation for findings that are variations of already-documented patterns.
Review whether the current OpenSpec documents accurately reflect the refined implementation. When this iteration's findings reveal spec drift, incorrect assumptions, or missing requirements, update:
openspec/changes/<change-id>/proposal.md — if the proposal's described behavior no longer matches realityopenspec/changes/<change-id>/design.md — if design decisions or trade-offs changed during refinementopenspec/changes/<change-id>/specs/ — if requirements or scenarios need correctionopenspec/changes/<change-id>/change-context.md — if this iteration added new files, tests, or changed requirement mappings, update the Requirement Traceability Matrix rows (Files Changed, Test(s) columns). Update Coverage Summary if new tests were added or requirements were discovered. If a finding reveals a missing spec requirement, add a new row to the matrix and write the corresponding test before fixing.Do NOT make unnecessary changes if the OpenSpec documents are still accurate after this iteration's fixes.
Construct a PhaseRecord for the Implementation Iteration <N> phase and call write_both(). The iteration number is auto-computed from prior Implementation Iteration entries in the session-log.
Capture from this iteration:
Persist via PhaseRecord.write_both():
This step MUST run BEFORE the git add . in Step 10 so the session-log entry is included in that commit.
python3 - <<'EOF'
import sys
sys.path.insert(0, "skills/session-log/scripts")
from phase_record import PhaseRecord, Decision, Alternative, TradeOff
from extract_session_log import count_phase_iterations
n = count_phase_iterations(
"Implementation Iteration", "openspec/changes/<change-id>/session-log.md"
) + 1
record = PhaseRecord(
change_id="<change-id>",
phase_name=f"Implementation Iteration {n}",
agent_type="<agent-type>",
summary="<2-3 sentences: findings addressed, what changed>",
decisions=[
Decision(title="<title>", rationale="<rationale>"),
],
alternatives=[Alternative(alternative="<approach>", reason="<rejection reason>")],
trade_offs=[TradeOff(accepted="<X>", over="<Y>", reason="<reason>")],
open_questions=["<question>"],
completed_work=["<finding addressed>"],
)
result = record.write_both()
print(f"markdown_path={result.markdown_path}")
print(f"sanitized={result.sanitized}")
print(f"handoff_id={result.handoff_id or '(local fallback)'}")
print(f"handoff_local_path={result.handoff_local_path}")
for w in result.warnings:
print(f"WARN: {w}", file=sys.stderr)
EOF
write_both() runs three best-effort steps internally: append rendered markdown → sanitize in-place → coordinator handoff (or local fallback at openspec/changes/<change-id>/handoffs/implementation-iteration-<n>-<N>.json). Each step logs warnings on failure but does not raise. The session-log.md is included in git add . in the existing commit step.
# Review all changes
git status
git diff
# Stage all changes
git add .
# Commit with structured message
git commit -m "$(cat <<'EOF'
refine(<scope>): iteration <N> - <summary of key changes>
Iterate-on-implementation: <change-id>, iteration <N>/<max>
Findings addressed:
- [<criticality>] <type>: <description>
- [<criticality>] <type>: <description>
Co-Authored-By: Claude <[email protected]>
EOF
)"
# Increment and loop
ITERATION=$((ITERATION + 1))
Loop back to Step 4.
Skip this step if VENDOR_REVIEW=false.
After the iterate loop converges (all findings below threshold) or max iterations are reached, dispatch a multi-vendor review for a final independent validation pass.
For implementations with work packages (work-packages.yaml exists), dispatch per-package reviews via /parallel-review-implementation. For simpler implementations, dispatch a whole-branch review.
Per-package dispatch (if work-packages.yaml exists):
# Dispatch per-package reviews to other vendors
for PKG_ID in $(python3 -c "
import yaml
pkgs = yaml.safe_load(open('openspec/changes/$CHANGE_ID/work-packages.yaml'))
for p in pkgs.get('packages', []): print(p['id'])
"); do
python3 "<skill-base-dir>/../parallel-infrastructure/scripts/review_dispatcher.py" \
--review-type implementation \
--mode review \
--prompt-file "openspec/changes/$CHANGE_ID/reviews/review-prompt-$PKG_ID.md" \
--cwd "$(pwd)" \
--output-dir "openspec/changes/$CHANGE_ID/reviews" \
--exclude-vendor claude_code \
--timeout 600
done
Whole-branch dispatch (no work packages):
# Create review prompt for the full implementation diff
mkdir -p openspec/changes/$CHANGE_ID/reviews
cat > openspec/changes/$CHANGE_ID/reviews/review-prompt.md <<'PROMPT'
Review the implementation on this branch against the OpenSpec proposal.
Run: git diff main..HEAD to see all changes.
Read openspec/changes/$CHANGE_ID/proposal.md and spec deltas for requirements.
Output ONLY valid JSON conforming to review-findings.schema.json.
Focus on: correctness, security, contract compliance, test coverage, and performance.
PROMPT
python3 "<skill-base-dir>/../parallel-infrastructure/scripts/review_dispatcher.py" \
--review-type implementation \
--mode review \
--prompt-file "openspec/changes/$CHANGE_ID/reviews/review-prompt.md" \
--cwd "$(pwd)" \
--output-dir "openspec/changes/$CHANGE_ID/reviews" \
--exclude-vendor claude_code \
--timeout 600
Also produce your own findings as the primary reviewer: review the implementation diff against spec requirements and write findings to openspec/changes/$CHANGE_ID/review-findings-impl.json.
python3 "<skill-base-dir>/../parallel-infrastructure/scripts/consensus_synthesizer.py" \
--review-type implementation \
--target "$CHANGE_ID" \
--findings "openspec/changes/$CHANGE_ID/review-findings-impl.json" \
"openspec/changes/$CHANGE_ID/reviews/findings-"*"-implementation.json" \
--output "openspec/changes/$CHANGE_ID/reviews/consensus-impl.json"
Present consensus summary:
If no other vendors are available (CLIs not installed), skip dispatch and proceed with single-vendor findings only.
The remediation threshold is the user's --threshold setting if provided, otherwise medium.
If the consensus or vendor review surfaces new findings at or above the remediation threshold:
openspec/changes/$CHANGE_ID/impl-findings.mdrefine(<scope>): vendor-review remediation - <summary>If all vendor review findings are below the remediation threshold, proceed to the summary.
Present a summary of all iterations:
If `CAN_MEMORY=true`, remember implementation iteration outcomes:
- MCP path: `remember`
- HTTP path: `"<skill-base-dir>/../coordination-bridge/scripts/coordination_bridge.py"` `try_remember(...)`
If `CAN_HANDOFF=true`, write a completion handoff containing:
- Fixes applied and critical findings resolved
- Remaining risks or manual follow-ups
- Validation status and recommended next command
## Iteration Summary
### Iteration 1
- Findings: <count> (<count by criticality>)
- Fixed: <list>
### Iteration 2
- Findings: <count> (<count by criticality>)
- Fixed: <list>
...
### Final State
- Total iterations: <N>
- Total findings addressed: <count>
- Remaining findings (below threshold): <list or "none">
- Termination reason: <threshold met | max iterations reached>
### Vendor Review (if dispatched)
- Vendors dispatched: <list or "skipped">
- Consensus findings: <confirmed count> confirmed, <unconfirmed count> unconfirmed, <disagreement count> disagreements
- Remediation cycle: <ran / not needed>
- New findings addressed in remediation: <count or "N/A">
openspec/<change-id> by default, or the operator-mandated branch)--vendor-review or coordinated tier): openspec/changes/<change-id>/reviews/consensus-impl.jsonValidate the deployed feature (recommended):
/validate-feature <change-id>
Or skip validation and proceed to cleanup:
/cleanup-feature <change-id>
development
Open the artifacts relevant to a review (OpenSpec proposal, branch changes, or explicit paths) in VS Code, in a curated read-order, in the right worktree.
tools
Render and seed coordinator-owned task status block in OpenSpec tasks.md
testing
User-invocable skill that omits the tail block
tools
Missing several required keys