name: chapter_production
description: "CLP self-driving chapter dispatch -- checks project pipeline status and spawns the next unwritten chapter."
debug: true
model: power
system: agent_prompt
agent_prompt:
  - "= identity.md"
sections:
  - agent
  - project
  - deliverables
  - rag
  - message
  - instructions
steps:
  - type: tool
    action: git_read_file
    optional: true
    output_key: book_outline_text
    params:
      path: "outline.md"

  - type: think
    max_tokens: 2000
    output_key: next_chapter_decision
    hint: |
      You are {agent.name}, production manager for {project.name}.

      Review the DELIVERABLES section above.

      STEP 1 -- Count completed (polished) chapters:
      Look for files matching the pattern: staging/polished/chapter-ch-XX.md
      Count how many such files exist (ch-01, ch-02, etc.)
      These are the chapters that have been fully written AND polished.

      STEP 2 -- Count in-progress (drafted) chapters:
      Look for any staging files that suggest a chapter has been drafted but not polished.
      Examples: review files, roundtable files, or draft files for a given chapter ref.

      STEP 3 -- Determine next chapter:
      The next chapter to write is the first chapter that has NO draft files at all.
      - If polished: ch-01 through ch-04 and draft evidence for ch-05: next is ch-06
      - If polished: ch-01 and draft evidence for ch-02: next is ch-03
      - If nothing exists for any chapter: next is ch-01

      Also check the BOOK OUTLINE if available above for chapter titles and total count.
      If all chapters exist in some form up to the outline limit: the book is DONE.

      Output:
      STATUS: NEEDS_CHAPTER or COMPLETE
      NEXT_CHAPTER: ch-01 (or ch-02, etc., or N/A if COMPLETE)
      CHAPTER_TITLE: <from outline if available, otherwise "Chapter N">
      POLISHED_COUNT: <number of staging/polished/chapter-ch-*.md files found>
      DRAFTED_COUNT: <number of chapters with any staging evidence>

  - type: think
    max_tokens: 100
    output_key: production_status
    hint: |
      Read the STATUS line from the chapter production decision above.
      Output EXACTLY one of these values, nothing else:
      - NEEDS_CHAPTER
      - COMPLETE

  - type: think
    max_tokens: 400
    output_key: chapter_dispatch_message
    hint: |
      Read the next chapter decision above.

      If STATUS is COMPLETE: output exactly: none

      If STATUS is NEEDS_CHAPTER, write a chapter dispatch message for Iris (lead author).
      Include:
      - Book: {project.name} ({genre_name})
      - Chapter to write: the NEXT_CHAPTER value from the decision
      - Chapter title: the CHAPTER_TITLE value
      - Characters: {character_profiles}
      - Word count target: {chapter_target_words} words
      - Instruction: check the most recent polished chapter for continuity,
        then write this chapter following the outline

      Output ONLY the dispatch message. No preamble.

  - type: tool
    action: enqueue_strategy
    optional: true
    params:
      company_slug: "crimson_leaf_publishing"
      project_slug: "{project.slug}"
      task_type: "book_chapter"
      content: "{chapter_dispatch_message}"

  - type: reply
    target: discussion

adjudication:
  enabled: false