name: market_intelligence
description: "Orion researches a specific market opportunity with 5-pass web research and produces a cited analysis document. Spawned by opportunity_scan."
debug: true
model: power
system: agent_prompt

agent_prompt:
  - "= identity.md"

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

steps:
  - type: think
    max_tokens: 200
    output_key: search_query_1
    hint: |
      The market opportunity to research is described in the task message above.

      Formulate ONE precise web search query to find:
      - Total Addressable Market size and growth rate for this niche
      - Industry reports, analyst data, credible statistics

      Output ONLY the search query. Nothing else. No preamble, no labels.

  - type: tool
    capability: Tool_WebSearcher
    input_from: search_query_1
    mode: research
    max_results: 8
    fetch_pages: 4
    optional: true
    output_key: market_data_1

  - type: think
    max_tokens: 200
    output_key: search_query_2
    hint: |
      Pass 2 -- Competitive landscape.
      Formulate ONE query to find the top 5 competitors in this niche:
      - What they sell, what they charge, who buys from them
      - Reviews, feature comparisons, pricing pages

      Output ONLY the search query. Nothing else.

  - type: tool
    capability: Tool_WebSearcher
    input_from: search_query_2
    mode: research
    max_results: 8
    fetch_pages: 3
    optional: true
    output_key: market_data_2

  - type: think
    max_tokens: 200
    output_key: search_query_3
    hint: |
      Pass 3 -- News and recent developments.
      Formulate ONE query to surface what has happened in this niche
      in the last 30-60 days. Look for:
      - New products, launches, regulatory changes
      - Funding events, acquisitions, market shifts
      - "Why now" signals that make this a timely opportunity

      Output ONLY the search query. Nothing else.

  - type: tool
    capability: Tool_WebSearcher
    input_from: search_query_3
    mode: news
    max_results: 8
    optional: true
    output_key: market_data_3

  - type: think
    max_tokens: 200
    output_key: search_query_4
    hint: |
      Pass 4 -- Community voice and buyer pain points.
      Formulate ONE query to find what real buyers or users say about
      this niche on Reddit, Quora, or forums. Append "site:reddit.com OR site:quora.com"
      to force community results.

      Output ONLY the search query. Nothing else.

  - type: tool
    capability: Tool_WebSearcher
    input_from: search_query_4
    mode: research
    max_results: 6
    fetch_pages: 2
    optional: true
    output_key: market_data_4

  - type: think
    max_tokens: 200
    output_key: search_query_5
    hint: |
      Pass 5 -- Revenue and monetization models.
      Formulate ONE query to find how businesses in this niche make money:
      - Subscription vs one-time vs affiliate vs advertising
      - Actual revenue numbers if public, pricing benchmarks
      - What the most profitable players do differently

      Output ONLY the search query. Nothing else.

  - type: tool
    capability: Tool_WebSearcher
    input_from: search_query_5
    mode: research
    max_results: 6
    fetch_pages: 2
    optional: true
    output_key: market_data_5

  - type: think
    max_tokens: 12000
    output_key: market_analysis_body
    hint: |
      You are Orion, Director of Research at Crimson Leaf Holdings.
      Produce a research-grade market analysis document for the opportunity in the task message.

      === PASS 1: MARKET SIZE AND TAM ===
      {market_data_1}
      === END PASS 1 ===

      === PASS 2: COMPETITIVE LANDSCAPE AND PRICING ===
      {market_data_2}
      === END PASS 2 ===

      === PASS 3: NEWS AND RECENT DEVELOPMENTS ===
      {market_data_3}
      === END PASS 3 ===

      === PASS 4: COMMUNITY VOICE AND BUYER PAIN POINTS ===
      {market_data_4}
      === END PASS 4 ===

      === PASS 5: REVENUE AND MONETIZATION MODELS ===
      {market_data_5}
      === END PASS 5 ===

      CITATION RULES -- READ CAREFULLY:
      - Every statistic must be followed immediately by [Title](URL) inline citation.
      - Use ONLY real URLs found in the research passes above.
      - Do NOT invent URLs, use placeholders, or cite "project.rag" as a source.
      - If a section has no real web data, write: "(estimate - no web source found)"
        and state the assumption explicitly.
      - Target at least 8 real cited URLs across the full document.

      Structure the document EXACTLY as follows. Do not skip sections.

      # Market Analysis: [opportunity name from task message]

      ---

      ## 1. Market Overview
      - Total Addressable Market: [size, growth rate -- cite source]
      - Key market drivers: [2-3 structural forces, each cited if possible]
      - Key risks and headwinds: [2-3 specific risks]

      ## 2. Recent Developments
      [What happened in this niche in the last 30-60 days from Pass 3 news data.
      If no news found, state "No significant developments found in the news pass."
      Cite each item with [Title](URL).]

      ## 3. Target Audience
      - Primary buyer: [specific demographic, job role, or persona]
      - Buyer behavior: [how they discover, evaluate, and purchase in this niche]
      - Pain points: [what they cannot get today -- cite community sources from Pass 4]

      ## 4. Competitive Landscape
      Top competitors from Pass 2:
      | Name | Product | Price | Audience | Strengths | Weaknesses |
      |------|---------|-------|----------|-----------|------------|
      [fill in table -- cite competitor URLs]

      White space: [what none of them do well that Crimson Leaf could own]

      ## 5. Monetization Models
      [How does the money flow in this niche -- from Pass 5.
      Subscription vs one-time vs affiliate vs advertising.
      Include specific revenue figures or rates if found. Cite each.]

      ## 6. Crimson Leaf Positioning
      - Recommended entry point: [specific niche within the broader market]
      - Differentiation: [how CL would be distinct, leveraging AI-speed and cost advantage]
      - Time-to-market: [realistic estimate given current CL capabilities]

      ## 7. Revenue Projections (12 months)
      - Conservative: [number with stated assumptions]
      - Base case: [number with stated assumptions]
      - Optimistic: [number with stated assumptions]
      Note: If revenue data is weak, state assumptions explicitly rather than guessing.

      ## 8. Recommendation
      **PROCEED** / **INVESTIGATE FURTHER** / **PASS**

      Rationale: [2-3 sentences grounded in data from above.
      If PROCEED: what makes this the right time?
      If INVESTIGATE FURTHER: what specific question must be answered first?
      If PASS: what structural barrier makes this unviable?]

      ---

      ## Sources
      [List every URL cited above, numbered, with title and domain.
      Format: N. [Title](URL) -- domain.com]

      ---

      BUSINESS_PLAN_FLAG: [write "BUSINESS_PLAN_NEEDED: true" if recommendation is PROCEED,
      otherwise write "BUSINESS_PLAN_NEEDED: false"]

  - type: document
    source_step: market_analysis_body
    dest_path: "deliverables/market-analysis/market-analysis-{task.id}.md"
    commit_msg: "research: market analysis task={task.id}"

  - type: close
    rag_update: true

  - type: tool
    action: enqueue_strategy
    optional: true
    condition: "BUSINESS_PLAN_NEEDED: true"
    params:
      company_slug: "crimson_leaf"
      project_slug: "incubation"
      task_type: "business_plan"
      content: "Develop a full business plan for the PROCEED opportunity identified in market analysis {task.id}. The full market analysis is in the deliverables folder. Use it as your foundation -- do not repeat research already done."
      agent_name: "orion"

  - type: tool
    action: enqueue_strategy
    optional: true
    params:
      company_slug: "crimson_leaf"
      project_slug: "incubation"
      task_type: "opportunity_scan"
      content: "Follow-up scan triggered by completed market analysis {task.id}. Prioritize opportunities adjacent to the niche just researched."
      agent_name: "orion"

  - type: reply
    target: channel
    channel_name: "crimson_leaf:live-feed"
    hint: |
      Write a 2-3 line summary of the market analysis just completed.
      Include: topic researched, recommendation (PROCEED/INVESTIGATE FURTHER/PASS),
      one key data point with citation, and whether a full business plan has been queued.
      Example: "Market analysis complete: [topic] -- PROCEED. [Key finding [Title](URL)]. Business plan queued."

adjudication:
  enabled: true
  pass_threshold: 70
  deliverable_type: analysis
  criteria:
    citation_density:
      weight: 35
      description: "At least 8 real URLs cited inline -- no invented URLs or placeholders"
    section_completeness:
      weight: 35
      description: "All 8 sections present with substantive data -- no empty sections"
    actionability:
      weight: 30
      description: "PROCEED/INVESTIGATE FURTHER/PASS recommendation is specific and grounded in cited data"
