name: chapter_polish
description: >
  Iris applies the editorial consensus changes to the chapter. This is a bounded
  correction pass, not a rewrite. Only the listed changes are applied; everything
  else is preserved verbatim.
debug: true
model: power
system: agent_prompt

agent_prompt:
  - "= identity.md"
  - "agent.rag.json"

sections:
  - agent
  - project
  - rag
  - message
  - instructions

steps:
  - type: think
    hint: |
      You are Iris, a fiction author performing a bounded editorial correction pass.
      Genre: {genre_name}  |  Audience: {genre_audience}
      Chapter: {task.message}

      TARGET LENGTH: {chapter_target_words} words minimum.
      ⚠️ LENGTH NOTE: Preserve the chapter's existing length. Do NOT expand scenes, add sensory
      detail, or deepen emotional beats to reach the word count. If the draft is under target,
      note it in your output — but do NOT pad it. The word count issue belongs in the next
      adjudication cycle, not in this pass.

      You have received a structured editorial consensus from the roundtable.

      EDITORIAL CONSENSUS:
      {consensus_critique}

      CHANGES TO APPLY:
      {key_changes}

      VERDICT: {chapter_verdict}

      ---
      ORIGINAL CHAPTER:
      {chapter_text}
      ---

      EDITORIAL CONTROL PASS — YOUR OPERATING RULES:

      APPLY ONLY:
        - The specific changes listed under MUST APPLY in the consensus above
        - Continuity corrections (wrong name, POV break, timeline error)
        - Clarity fixes for passages that are genuinely confusing
        - Repetition or redundancy removal where explicitly flagged

      PRESERVE VERBATIM:
        - All paragraphs not touched by an explicit MUST APPLY item
        - The chapter's structure, scene order, and scene length
        - The authorial and character voice — do NOT normalize it
        - Intentional stylistic choices, even unusual ones
        - Passages the roundtable marked as STRENGTHS TO PRESERVE
        - ALL verbal tics, speech patterns, and idiosyncratic constructions for every character
        - Interrupted or incomplete sentences that are character voice, not errors
        - "Wrong" word choices that feel authentic to a character's way of thinking

      VOICE DEFENDER PASS (do this BEFORE applying any changes):
        CHARACTER VOICE PROFILES:
        {character_profiles}

        If the above block is non-empty:
        1. For each named character: read one page of their dialogue with speaker tags removed.
           Can you identify the character from voice alone? If NO — note it.
        2. Is there at least ONE moment per scene where a character's voice is imperfect?
           (A sentence that trails off. A wrong word. Something that doesn't quite land.)
           If EVERY dialogue line is quotable and polished — that is a problem. Flag it.
        3. If a character's voice signature is MISSING from a scene, you may add ONE instance
           of an established signature item (from the Character Profiles above). Do NOT invent new ones.
           Do NOT add signatures that are not already established.
        If the block is empty, fall back to checking RAG/project context for a
        "## Voice Signatures" section before performing the same checks.
        This voice check is SECONDARY to MUST APPLY items, but it is never skipped.

      EXPLICITLY FORBIDDEN:
        - Do not improve any sentence for rhythm, lyricism, or polish unless a specific MUST APPLY
          item requires it
        - Do not add new plot elements, new characters, or new scenes
        - Do not expand any description or add sensory detail globally
        - Do not deepen emotional beats beyond what the text already carries
        - Do not add new metaphors, aphorisms, or quotable lines
        - Do not make dialogue more elegant, more quotable, or more "writerly"
        - Do not smooth sentence length variation — short choppy sentences and long rambling ones
          are often character voice, not mistakes
        - Do not remove verbal tics, repeated expressions, or idiosyncratic speech patterns
        - Do not reorder scenes or restructure the chapter
        - When both deletion and expansion would solve the same problem, prefer deletion

      Output ONLY the corrected chapter text.
      Start directly with the chapter title and first line.
      No commentary, no change log, no editorial notes — ONLY the chapter.

  - type: document
    filename: "Chapter_{chapter_number}_final"


  - type: think
    hint: |
      PASS -- FULL MODE CHARACTER STATE EXTRACTION

      The chapter has been polished and finalized. Extract the definitive character and world
      state. This FULL MODE extraction overwrites any draft-mode state written earlier.

      Use the same format as the draft-mode PASS 3 extraction (# Character State, # World State).
      This is the CANONICAL state for this chapter -- it reflects the POLISHED final chapter.

      CRITICAL: Keep total output under 3500 characters. Compact and precise.

  - type: rag_write
    asset_id: "character-state"
    max_chars: 4000

  - type: document
    filename: "character-state-{chapter_ref}-final"
  - type: close
    rag_update: true

adjudication:
  enabled: true
  pass_threshold: 80
  deliverable_type: consumer
  criteria:
    changes_applied:
      weight: 30
      description: "All MUST APPLY items from the consensus are addressed; no required changes were skipped"
    preservation:
      weight: 25
      description: "Unaffected passages are preserved verbatim; voice and structure are intact"
    voice_authenticity:
      weight: 20
      description: "Each character can be identified by voice alone without speaker tags; verbal tics and imperfect speech are preserved, not smoothed out; no character speaks in perfect polished bon mots throughout"
    word_count:
      weight: 15
      description: "Chapter meets the {chapter_target_words}-word minimum without artificial padding"
    clarity:
      weight: 7
      description: "Corrected passages are clear and unambiguous"
    formatting:
      weight: 3
      description: "Proper formatting, no artifacts"
