# ship-feature: clarify → design → plan → plan-review loop → implement → impl-review loop → audit loop → finalize+notify.
# Tier-2 flow with phase contracts: the plan phase materializes ai_plan/<slug>/ and
# publishes {slug, plan_dir, plan_doc}; implement requires the slug, prepares a
# worktree, and asserts the tracker advanced; the audit loop runs canonical-delta.
# args.flow + args.slug are passed by sf_flow_auto; every checkpoint uses ai_plan/${args.slug}.
name: ship-feature
description: Clarify, design, plan, implement, and audit a feature end-to-end
input: prompt
agents:
  elicitor:
    tools: [read, grep, find, ls]
    thinking: high
    isolated: true
    schema:
      questions: array
  designer:
    tools: [read, grep, find, ls]
    thinking: high
    isolated: true
  planner:
    tools: [read, grep, find, ls, write]
    thinking: medium
    isolated: true
  developer:
    tools: [read, grep, find, ls, write, bash]
    thinking: medium
  reviewer:
    tools: [read, grep, find, ls]
    thinking: high
    isolated: true
    schema:
      verdict: APPROVED|REVISE
      findings: array
  auditor:
    tools: [read, grep, find, ls]
    thinking: high
    isolated: true
    schema:
      verdict: APPROVED|REVISE
      findings: array
  notifier:
    tools: [bash]
    thinking: low
    isolated: true
    schema:
      status: sent|skipped|failed
      detail: string?
groups:
  plan-review:
    phases: [review-plan, fix-plan]
  impl-review:
    phases: [review-impl, fix-impl]
  audit-loop:
    phases: [review-audit, fix-audit]
phases:
  - id: clarify
    questions: elicitor
    prompt: "Given this feature request, identify what is unclear about scope, constraints, success criteria, and edge cases. Return a questions array — empty if the task is clear enough to design."
    max_rounds: 5
  - id: design
    agent: designer
    out: design_doc
    prompt: "Given the clarified requirements (from the preceding Q&A, in context), produce a design. Auto-finalize: skip the STATUS relay, pick the recommended approach, return the design directly."
  - id: plan
    agent: planner
    out: plan_doc
    prompt: "Given the design, produce an exhaustive milestone plan with bite-sized stories (S-M{m}-{n}). Write each story into the prepared files under the plan dir (original-plan.md, milestone-plan.md, story-tracker.md, continuation-runbook.md)."
    inputs:
      require: [design_doc]
      inject: ["Design: {{design_doc}}"]
    outputs:
      slug: { from: input, prefix: date }
      dir: "ai_plan/{{slug}}"
      artifacts:
        - { file: original-plan.md, template: "@flow/plan/original-plan.md" }
        - { file: milestone-plan.md, template: "@flow/plan/milestone-plan.md" }
        - { file: story-tracker.md, template: "@flow/plan/story-tracker.md" }
        - { file: continuation-runbook.md, template: "@flow/plan/continuation-runbook.md" }
      assert: [nonempty]
      publish: { slug: "{{slug}}", plan_dir: "{{dir}}", plan_doc: plan_doc }
  - id: review-plan
    agent: reviewer
    in: plan_doc
    prompt: "Review the plan for completeness, correctness, and risk. Return findings P0-P3 + verdict."
  - id: fix-plan
    agent: planner
    in: plan_doc
    prompt: "Address the reviewer findings. Minimal changes, no regressions. Report per-finding."
  - id: implement
    agent: developer
    out: impl_result
    prompt: "Implement the plan in the prepared flow/<slug> worktree using TDD (red/green/refactor). Run tests + typecheck. Commit locally. Advance the main-checkout ai_plan/<slug>/story-tracker.md per story (legal transitions + commit SHA)."
    inputs:
      require: [slug, plan_doc]
      inject: ["Slug: {{slug}}", "Plan: {{plan_doc}}"]
    outputs:
      dir: "ai_plan/{{slug}}"
      assert: [tracker_updated]
    worktree: prepare
  - id: review-impl
    agent: reviewer
    in: impl_result
    prompt: "Review the implementation diff against the plan. Return findings P0-P3 + verdict."
  - id: fix-impl
    agent: developer
    in: impl_result
    prompt: "Fix the reviewer findings. TDD: test first. Minimal diff, no regressions. Report per-finding."
  - id: review-audit
    agent: auditor
    in: impl_result
    prompt: "Audit the implementation for correctness, security, performance, and clarity. Return findings P0-P3 + verdict. Round >=2: verify each prior [Fn] as FIXED/PARTIALLY-FIXED/NOT-FIXED/NEW-ISSUE-INTRODUCED."
  - id: fix-audit
    agent: developer
    in: impl_result
    prompt: "Fix the audit findings. TDD: test first. Minimal diff, no regressions. Report per-finding."
  - id: notify
    agent: notifier
    worktree: finalize
    prompt: "ship-feature complete"
    out: notify_result
loops:
  plan-review:
    until: approved
    fail_on: [P0, P1, P2]
    max_rounds: 10
  impl-review:
    until: approved
    fail_on: [P0, P1, P2]
    max_rounds: 5
  audit-loop:
    until: approved
    fail_on: [P0, P1, P2]
    max_rounds: 5
    protocol: canonical-delta
