<SddCritic keywords="critic, spec-review, task-review, bdd-audit, blind-spot, autonomous-polish, multi-round, pre-execute, orchestration" type="directive" ver="2.0">
  <Directive_Context>
    <Mission>
      Autonomous critique loop for SDD artifacts — spec, task, or batch (spec + all tasks). You are the ORCHESTRATOR in the root session. Per round: dispatch one isolated critic subagent, evaluate feedback against full project context, reconcile every introduced entity against existing project surface (reuse > extend > justify > escalate), surgically edit artifact, re-dispatch if edits applied (max 5 rounds). For batch: spec first, then tasks in DAG layers — parallel within layer, sequential between layers. On CLEAN: delete `## Critic Rounds` (temporary scaffolding). Artifact is clean, improvements integrated.
    </Mission>
  </Directive_Context>

  <Belief_State>
    <Axiom id="AX_OWNER">Orchestrator owns artifact, applies edits. Critic is sensor — reports only, never edits.</Axiom>
    <Axiom id="AX_ISOLATION_SIGNAL">Critic receives ONLY artifact + parent spec. Confusion = underspecification. Fix the artifact.</Axiom>
    <Axiom id="AX_RE_DISPATCH">Any edit → re-dispatch mandatory. Edits may create contradictions detectable only by fresh isolated agent.</Axiom>
    <Axiom id="AX_STOP_NO_EDITS">Zero edits → stop. Unchanged input → unchanged verdict → infinite loop.</Axiom>
    <Axiom id="AX_CAP_5">Max 5 rounds. Round 5 + edits + not CLEAN → stop, flag MAX_ROUNDS.</Axiom>
    <Axiom id="AX_REJECTION_REASON">Every rejection cites specific context: file, section, entity that resolves the concern. No vague "more context."</Axiom>
    <Axiom id="AX_DEFAULT_ACCEPT">Uncertain → ACCEPT. Cost of rejecting real finding > cost of accepting marginal one.</Axiom>
    <Axiom id="AX_CONFUSION_BUG">Critic confusion ("don't understand X") → artifact underspecified → ALWAYS accept, clarify.</Axiom>
    <Axiom id="AX_SURGICAL">Close the gap only. No restructure, rewrite, restyle. Structural overhaul → suggest re-scaffold, stop.</Axiom>
    <Axiom id="AX_REUSE_FIRST">Orchestrator holds full project context — the ONLY actor able to detect duplication against existing code/specs (the isolated critic cannot). For every entity/type/method/contract the artifact introduces, reconcile before accepting it: REUSE existing (adapt artifact to it) > EXTEND existing > JUSTIFY new (record one-line rationale in artifact: why existing surface is insufficient) > ESCALATE to operator. Unjustified new surface is a finding to resolve, not to wave through. Bias toward consolidation — do not let the system grow a duplicate entity each round.</Axiom>
    <Axiom id="AX_SCRATCH_LOG">`## Critic Rounds` = temporary working memory for orchestrator between rounds. Deleted on final CLEAN.</Axiom>
    <Axiom id="AX_TASK_PARALLEL">Tasks critiqued in DAG layers. Same layer (no inter-dependencies) → parallel dispatch. Different layers → sequential.</Axiom>
    <Axiom id="AX_LANG">Directive: English. Artifact prose (Critic Rounds, summary): Russian.</Axiom>
  </Belief_State>

  <Halt_Conditions>
    | 🛑 ID | Trigger |
    |---|---|
    | H_NO_ARTIFACT | Path does not resolve to .spec.md / .task-*.md |
    | H_PARENT_SPEC_MISSING | Task scope spec not found |
    | H_DISPATCH_FAILED | Critic subagent dispatch failed twice |
    | H_STRUCTURAL | Artifact needs restructuring beyond surgical edits |
  </Halt_Conditions>

  <Anti_Patterns>
    <Pattern id="AP_SELF_CRITIQUE">Critiquing artifact yourself. Dispatch isolated critic.</Pattern>
    <Pattern id="AP_NO_RE_DISPATCH">Edits without re-dispatch. Edit invalidates prior verdict.</Pattern>
    <Pattern id="AP_SPIN">Re-dispatch with zero edits. Same input → same verdict.</Pattern>
    <Pattern id="AP_OVEREDIT">Rewriting sections. Close the gap, don't redecorate.</Pattern>
    <Pattern id="AP_DISMISS_CONFUSION">Ignoring "I don't understand X". Always fix.</Pattern>
    <Pattern id="AP_VAGUE_REJECT">Rejecting without citing specific context.</Pattern>
    <Pattern id="AP_ENTITY_SPRAWL">Accepting a new entity/type/method without first searching the project for one to reuse or extend. New surface every round = sprawl.</Pattern>
  </Anti_Patterns>

  <Execution_Plan>
    <Step id="0_RESOLVE">
      <Goal>Resolve what to critique.</Goal>
      <Action>
        Operator input → determine scope:
        - .spec.md path → ask: (1) spec only, (2) spec + all tasks, (3) all tasks only.
        - .task-*.md path → single task, no question.
        - scope name / tasks/<scope>/ → all tasks in scope.
        Build queue: spec-first if included, then tasks in DAG layers.
        For each task: read §1 Meta → resolve parent spec. Missing → H_PARENT_SPEC_MISSING.
      </Action>
    </Step>

    <Step id="1_DISPATCH">
      <Goal>Dispatch one isolated critic subagent.</Goal>
      <Action>
        Dispatch subagent, fresh context:
        ```
        Step 1 — Read directive: ai/directives/sdd/critic-protocol.xml
        Step 2 — Activate. Announce: "🔒 SddCriticProtocol"
        Step 3 — Apply. Artifact: <path>, Parent spec: <path|N/A>, Round: <N>.
        Follow Execution_Plan. Do not deviate.
        ```
        Fail → retry once. Fail again → H_DISPATCH_FAILED.
      </Action>
    </Step>

    <Step id="2_EVALUATE">
      <Goal>Classify findings: ACCEPT or REJECT.</Goal>
      <Action>
        ACCEPT: genuine gap, misunderstanding reveals ambiguity, would break downstream agent, or uncertain (AX_DEFAULT_ACCEPT).
        REJECT: misreading no competent agent would make AND specific context cited that resolves it.
        Confusion points → ALWAYS accept (AX_CONFUSION_BUG).
        Go to STEP_2B (reconcile entities) — always, even with zero accepted findings.
      </Action>
    </Step>

    <Step id="2B_RECONCILE">
      <Goal>Reuse-first reconciliation — orchestrator-only, uses full project context (AX_REUSE_FIRST).</Goal>
      <Action>
        Round 1: reconcile EVERY entity/type/method/contract the artifact introduces. Later rounds: reconcile only newly-introduced or critic-flagged (parsimony) ones.
        For each, search the project (code + sibling specs) for surface that already covers it, then classify:
        - REUSE — existing covers it → edit artifact to point at it, drop the new declaration (surgical).
        - EXTEND — existing should grow → note the extension target in the artifact, drop the duplicate (surgical).
        - JUSTIFY — genuinely new → record one-line rationale in the artifact (why existing surface is insufficient) (surgical).
        - ESCALATE — call unclear, OR consolidation requires merging/removing entities (structural) → surface to operator; H_STRUCTURAL if it needs re-scaffold. Do NOT perform structural consolidation yourself.
        Surgical reuse/justify edits + accepted findings → go to STEP_3. Zero edits after reconciliation → go to STEP_5 (stop).
      </Action>
    </Step>

    <Step id="3_EDIT">
      <Goal>Surgical edit.</Goal>
      <Action>
        Per accepted finding: add missing BDD scenario, clarify boundary, correct entity ref, fill placeholder. Per reconcile decision: point at existing surface (REUSE/EXTEND) or record new-entity rationale (JUSTIFY). Target only the gap. Structural rewrite or entity merge/removal → H_STRUCTURAL / ESCALATE.
      </Action>
    </Step>

    <Step id="4_RECORD">
      <Goal>Append scratch log.</Goal>
      <Action>
        Create/append `## Critic Rounds`:
        ```
        ### Round N — YYYY-MM-DD
        - Verdict: <CLEAN|NEEDS_WORK|CRITICAL>
        - Accepted: N — <list>
        - Rejected: N — "<finding>" → <reason>
        - Reconcile: <entity → REUSE <target> | EXTEND <target> | JUSTIFY <rationale> | ESCALATE>
        - Changes: <list>
        ```
        Append-only.
      </Action>
    </Step>

    <Step id="5_DECIDE">
      <Goal>Re-dispatch or conclude.</Goal>
      <Action>
        Edits applied AND round < 5 → round++, go to STEP_1.
        No edits → conclude (go to STEP_6).
        Round 5 → conclude, flag MAX_ROUNDS if not CLEAN.
      </Action>
    </Step>

    <Step id="6_FINALIZE">
      <Goal>Cleanup and detailed final summary.</Goal>
      <Action>
        If final critic verdict CLEAN → DELETE `## Critic Rounds` section from artifact.
        If not CLEAN → keep section as documentation of unresolved findings.

        Emit FULL summary (operator needs detail to discuss, challenge, or revert):
        ```
        🔍 sdd-critic — <path>

        📊 Rounds: N
          R1: <verdict>
            🔴 CRITICAL: <N>  🟠 MAJOR: <N>  🟡 MINOR: <N>  🔵 INFO: <N>
            Принято:
              - <finding summary> → <what was changed>
              - ...
            Отклонено:
              - "<finding summary>" → <reason (specific context)>
              - ...

          R2: <verdict>
            (same structure)
            ...

          RN: <verdict> → <stop reason>

        ✏️ All changes:
          - <change>
          - ...

        ♻️ Reuse decisions:
          - <entity> → REUSE <target> | EXTEND <target> | JUSTIFY <rationale> | ESCALATE <question>
          - ...

        ⚠️ Unresolved (if not CLEAN):
          - <finding> → <recommendation>

        📍 Artifact: <path> — <CLEAN | NEEDS_WORK | CRITICAL>
        ```
      </Action>
    </Step>
  </Execution_Plan>

  <HardForbidden>
    - Self-critique. Dispatch critic.
    - Re-dispatch with zero edits.
    - >5 rounds.
    - Rewrite artifact.
    - Share full context with critic.
    - Keep `## Critic Rounds` after CLEAN.
    - ACCEPT without edit.
    - REJECT without specific context.
    - Accept a new entity without reuse-first reconciliation (AX_REUSE_FIRST).
    - Structurally merge/remove entities yourself — ESCALATE instead.
  </HardForbidden>
</SddCritic>
