name: refine_proposal
description: "Proposal Refinement Loop -- examine an edited company proposal and produce a stronger version. Runs up to 10 iterations."
debug: true
system: agent_prompt
requires:
  - proposal_document

agent_prompt:
  - "= identity.md"

sections:
  - agent
  - project
  - history
  - rag
  - roster
  - deliverables
  - message
  - instructions

steps:
  - type: think
    hint: |
      You are {agent.name}, running the Proposal Refinement Protocol for Crimson Leaf LLC.

      An operator has returned a company proposal for your review and improvement.
      Your task is to study it carefully and produce a BETTER version.

      ============================================================
      PROPOSAL DOCUMENT (full content follows):
      ============================================================
      {proposal_document}
      ============================================================

      STEP 1 -- PARSE PROPOSAL_META
      Find the <!-- PROPOSAL_META ... PROPOSAL_META --> block at the bottom.
      Extract:
        - task_id (the genesis task ID)
        - iteration (current iteration number, starting at 0)
        - company_slug
        - max_iterations (default 10 if not present)
        - history (list of prior iteration records)
        - refined_by (list of prior refiner task IDs)

      If no PROPOSAL_META block exists, treat iteration as 0 and proceed.

      STEP 2 -- ITERATION LIMIT CHECK
      If iteration >= 9 (meaning this would be iteration 10 or beyond):
        - Output the proposal content as-is
        - Append a note at the top: "NOTE: Maximum refinement iterations reached (10/10). This is the final version."
        - Output the PROPOSAL_META with iteration unchanged (do NOT increment)
        - Skip the improvement analysis

      STEP 3 -- IMPROVEMENT ANALYSIS
      Otherwise, systematically examine every section of the proposal:

      3a. CONSTITUTIONAL ALIGNMENT
          - Does the company stay within Crimson Leaf charter boundaries?
          - Are forbidden activities clearly defined?
          - Are non-goals explicit?

      3b. NICHE SPECIFICITY
          - Is the company sharply focused, or does it drift into generalism?
          - Does the name and slug accurately reflect the specific niche?

      3c. FINANCIAL PROJECTIONS
          - Are revenue projections realistic and grounded in task-volume estimates?
          - Are model compute costs specifically broken down?
          - Is the break-even timeline credible?
          - Does Year 1 net projection follow from the monthly figures?

      3d. CEO SEED QUALITY
          - Does the CEO seed have rich enough detail for downstream identity generation?
          - Executive mandate, domain expertise, temperament, operating boundaries -- all present?

      3e. STRATEGIC JUSTIFICATION
          - Is the case for a new sovereign company strong and specific?
          - Does it address why existing tenants and tools are insufficient?

      3f. OPERATOR FEEDBACK (if any was provided in the task message)
          - Incorporate any specific feedback from the operator directly

      STEP 4 -- PRODUCE IMPROVED VERSION
      - Keep every section that is already strong
      - Improve every section that is weak, vague, or missing detail
      - Do NOT change the company slug or name unless they are clearly wrong
      - The improved version must score higher than the previous iteration on all criteria
      - Increment "iteration" by 1 in the updated PROPOSAL_META block
      - Add your task_id ({task.id}) to the "refined_by" list
      - Append a history entry: "{task.id} (v{new_iteration} - refined)"

  - type: document
    filename: "{task.id}_refined_proposal.md"
    primary_deliverable: true
    hint: |
      Write the COMPLETE improved proposal document.

      Structure your document with all original sections preserved and improved.
      Include every section: Company Name, Problem Statement, Target Customer, Why New Company,
      Why Not Existing Tenant, Why Not Tool, Business Plan, Value Thesis, Charter,
      Forbidden Activities, Non-Goals, Overlap Risk, Recursion Risk, Budget,
      Budget Justification, CEO profile, and the full Finance section.

      At the very end, after ALL content, append the updated PROPOSAL_META block.
      It must be valid JSON inside <!-- PROPOSAL_META ... PROPOSAL_META --> markers.
      Increment the iteration field by 1 from the previous value.
      Add {task.id} to the refined_by list.
      Add a history entry for this refinement.

      Example updated footer:
      <!-- PROPOSAL_META
      {
        "task_id": "ORIGINAL_TASK_ID",
        "iteration": 1,
        "version": "0.2",
        "company_slug": "the_company_slug",
        "max_iterations": 10,
        "refined_by": ["ORIGINAL_TASK_ID", "{task.id}"],
        "history": ["ORIGINAL (v0.1 - genesis)", "{task.id} (v0.2 - refined)"]
      }
      PROPOSAL_META -->

  - type: close
    rag_update: true

adjudication:
  enabled: true
  pass_threshold: 90
  deliverable_type: coordination
  criteria:
    constitutional_alignment:
      weight: 20
      description: "Proposal stays within Crimson Leaf charter and governance boundaries"
    niche_specificity:
      weight: 15
      description: "Company is sharply focused, non-generalist, and clearly differentiated"
    financial_projections:
      weight: 15
      description: "Income and expense projections are specific, grounded, and internally consistent"
    strategic_justification:
      weight: 15
      description: "Strong case for why a new company should exist and why existing tenants cannot cover it"
    ceo_alignment:
      weight: 15
      description: "Founding CEO seed is rich, role-specific, and sufficient for identity generation"
    iteration_improvement:
      weight: 15
      description: "The refined version is demonstrably stronger than a basic first draft -- new detail, tighter logic, better financials"
    structural_safety:
      weight: 5
      description: "Design avoids recursion, dependency loops, and unstable operating models"
