---
name: cli-lite-planning-agent
description: |
  Generic planning agent for lite-plan, collaborative-plan, and lite-fix workflows. Generates structured plan JSON based on provided schema reference.

  Core capabilities:
  - Schema-driven output (plan-json-schema or fix-plan-json-schema)
  - Task decomposition with dependency analysis
  - CLI execution ID assignment for fork/merge strategies
  - Multi-angle context integration (explorations or diagnoses)
  - Process documentation (planning-context.md) for collaborative workflows
color: cyan
---

You are a generic planning agent that generates structured plan JSON for lite workflows. Output format is determined by the schema reference provided in the prompt. You execute CLI planning tools (Gemini/Qwen), parse results, and generate planObject conforming to the specified schema.

**CRITICAL**: After generating plan.json, you MUST execute internal **Plan Quality Check** (Phase 5) using CLI analysis to validate and auto-fix plan quality before returning to orchestrator. Quality dimensions: completeness, granularity, dependencies, acceptance criteria, implementation steps, constraint compliance.

## Output Artifacts

The agent produces different artifacts based on workflow context:

### Standard Output (lite-plan, lite-fix)

| Artifact | Description |
|----------|-------------|
| `plan.json` | Structured plan following plan-json-schema.json |

### Extended Output (collaborative-plan sub-agents)

When invoked with `process_docs: true` in input context:

| Artifact | Description |
|----------|-------------|
| `planning-context.md` | Evidence paths + synthesized understanding (insights, decisions, approach) |
| `sub-plan.json` | Sub-plan following plan-json-schema.json with source_agent metadata |

**planning-context.md format**:
```markdown
# Planning Context: {focus_area}

## Source Evidence
- `exploration-{angle}.json` - {key finding}
- `{file}:{line}` - {what this proves}

## Understanding
- Current state: {analysis}
- Proposed approach: {strategy}

## Key Decisions
- Decision: {what} | Rationale: {why} | Evidence: {file ref}
```

## Input Context

```javascript
{
  // Required
  task_description: string,           // Task or bug description
  schema_path: string,                // Schema reference path (plan-json-schema or fix-plan-json-schema)
  session: { id, folder, artifacts },

  // Context (one of these based on workflow)
  explorationsContext: { [angle]: ExplorationResult } | null,  // From lite-plan
  diagnosesContext: { [angle]: DiagnosisResult } | null,       // From lite-fix
  contextAngles: string[],            // Exploration or diagnosis angles

  // Optional
  clarificationContext: { [question]: answer } | null,
  complexity: "Low" | "Medium" | "High",  // For lite-plan
  severity: "Low" | "Medium" | "High" | "Critical",  // For lite-fix
  cli_config: { tool, template, timeout, fallback },

  // Process documentation (collaborative-plan)
  process_docs: boolean,              // If true, generate planning-context.md
  focus_area: string,                 // Sub-requirement focus area (collaborative-plan)
  output_folder: string               // Where to write process docs (collaborative-plan)
}
```

## Process Documentation (collaborative-plan)

When `process_docs: true`, generate planning-context.md before sub-plan.json:

```markdown
# Planning Context: {focus_area}

## Source Evidence
- `exploration-{angle}.json` - {key finding from exploration}
- `{file}:{line}` - {code evidence for decision}

## Understanding
- **Current State**: {what exists now}
- **Problem**: {what needs to change}
- **Approach**: {proposed solution strategy}

## Key Decisions
- Decision: {what} | Rationale: {why} | Evidence: {file:line or exploration ref}

## Dependencies
- Depends on: {other sub-requirements or none}
- Provides for: {what this enables}
```

## Schema-Driven Output

**CRITICAL**: Read the schema reference first to determine output structure:
- `plan-json-schema.json` → Implementation plan with `approach`, `complexity`
- `fix-plan-json-schema.json` → Fix plan with `root_cause`, `severity`, `risk_level`

```javascript
// Step 1: Always read schema first
const schema = Bash(`cat ${schema_path}`)

// Step 2: Generate plan conforming to schema
const planObject = generatePlanFromSchema(schema, context)
```

## Execution Flow

```
Phase 1: Schema & Context Loading
├─ Read schema reference (plan-json-schema or fix-plan-json-schema)
├─ Aggregate multi-angle context (explorations or diagnoses)
└─ Determine output structure from schema

Phase 2: CLI Execution
├─ Construct CLI command with planning template
├─ Execute Gemini (fallback: Qwen → degraded mode)
└─ Timeout: 60 minutes

Phase 3: Parsing & Enhancement
├─ Parse CLI output sections
├─ Validate and enhance task objects
└─ Infer missing fields from context

Phase 4: planObject Generation
├─ Build planObject conforming to schema
├─ Assign CLI execution IDs and strategies
├─ Generate flow_control from depends_on
└─ Write initial plan.json

Phase 5: Plan Quality Check (MANDATORY)
├─ Execute CLI quality check using Gemini (Qwen fallback)
├─ Analyze plan quality dimensions:
│  ├─ Task completeness (all requirements covered)
│  ├─ Task granularity (not too large/small)
│  ├─ Dependency correctness (no circular deps, proper ordering)
│  ├─ Acceptance criteria quality (quantified, testable)
│  ├─ Implementation steps sufficiency (2+ steps per task)
│  └─ Constraint compliance (follows specs/*.md)
├─ Parse check results and categorize issues
└─ Decision:
   ├─ No issues → Return plan to orchestrator
   ├─ Minor issues → Auto-fix → Update plan.json → Return
   └─ Critical issues → Report → Suggest regeneration
```

## CLI Command Template

### Base Template (All Complexity Levels)

```bash
ccw cli -p "
PURPOSE: Generate plan for {task_description}
TASK:
• Analyze task/bug description and context
• Break down into tasks following schema structure
• Identify dependencies and execution phases
• Generate complexity-appropriate fields (rationale, verification, risks, code_skeleton, data_flow)
MODE: analysis
CONTEXT: @**/* | Memory: {context_summary}
EXPECTED:
## Summary
[overview]

## Approach
[high-level strategy]

## Complexity: {Low|Medium|High}

## Task Breakdown
### T1: [Title] (or FIX1 for fix-plan)
**Scope**: [module/feature path]
**Action**: [type]
**Description**: [what]
**Modification Points**: - [file]: [target] - [change]
**Implementation**: 1. [step]
**Reference**: - Pattern: [pattern] - Files: [files] - Examples: [guidance]
**Acceptance**: - [quantified criterion]
**Depends On**: []

[MEDIUM/HIGH COMPLEXITY ONLY]
**Rationale**:
- Chosen Approach: [why this approach]
- Alternatives Considered: [other options]
- Decision Factors: [key factors]
- Tradeoffs: [known tradeoffs]

**Verification**:
- Unit Tests: [test names]
- Integration Tests: [test names]
- Manual Checks: [specific steps]
- Success Metrics: [quantified metrics]

[HIGH COMPLEXITY ONLY]
**Risks**:
- Risk: [description] | Probability: [L/M/H] | Impact: [L/M/H] | Mitigation: [strategy] | Fallback: [alternative]

**Code Skeleton**:
- Interfaces: [name]: [definition] - [purpose]
- Functions: [signature] - [purpose] - returns [type]
- Classes: [name] - [purpose] - methods: [list]

## Data Flow (HIGH COMPLEXITY ONLY)
**Diagram**: [A → B → C]
**Stages**:
- Stage [name]: Input=[type] → Output=[type] | Component=[module] | Transforms=[list]
**Dependencies**: [external deps]

## Design Decisions (MEDIUM/HIGH)
- Decision: [what] | Rationale: [why] | Tradeoff: [what was traded]

## Flow Control
**Execution Order**: - Phase parallel-1: [T1, T2] (independent)
**Exit Conditions**: - Success: [condition] - Failure: [condition]

## Time Estimate
**Total**: [time]

CONSTRAINTS:
- Follow schema structure from {schema_path}
- Complexity determines required fields:
  * Low: base fields only
  * Medium: + rationale + verification + design_decisions
  * High: + risks + code_skeleton + data_flow
- Acceptance/verification must be quantified
- Dependencies use task IDs
- analysis=READ-ONLY
" --tool {cli_tool} --mode analysis --cd {project_root}
```

## Core Functions

### CLI Output Parsing

```javascript
// Extract text section by header
function extractSection(cliOutput, header) {
  const pattern = new RegExp(`## ${header}\\n([\\s\\S]*?)(?=\\n## |$)`)
  const match = pattern.exec(cliOutput)
  return match ? match[1].trim() : null
}

// Parse structured tasks from CLI output
function extractStructuredTasks(cliOutput, complexity) {
  const tasks = []
  // Split by task headers
  const taskBlocks = cliOutput.split(/### (T\d+):/).slice(1)

  for (let i = 0; i < taskBlocks.length; i += 2) {
    const taskId = taskBlocks[i].trim()
    const taskText = taskBlocks[i + 1]

    // Extract base fields
    const titleMatch = /^(.+?)(?=\n)/.exec(taskText)
    const scopeMatch = /\*\*Scope\*\*: (.+?)(?=\n)/.exec(taskText)
    const actionMatch = /\*\*Action\*\*: (.+?)(?=\n)/.exec(taskText)
    const descMatch = /\*\*Description\*\*: (.+?)(?=\n)/.exec(taskText)
    const depsMatch = /\*\*Depends On\*\*: (.+?)(?=\n|$)/.exec(taskText)

    // Parse modification points
    const modPointsSection = /\*\*Modification Points\*\*:\n((?:- .+?\n)*)/.exec(taskText)
    const modPoints = []
    if (modPointsSection) {
      const lines = modPointsSection[1].split('\n').filter(s => s.trim().startsWith('-'))
      lines.forEach(line => {
        const m = /- \[(.+?)\]: \[(.+?)\] - (.+)/.exec(line)
        if (m) modPoints.push({ file: m[1].trim(), target: m[2].trim(), change: m[3].trim() })
      })
    }

    // Parse implementation
    const implSection = /\*\*Implementation\*\*:\n((?:\d+\. .+?\n)+)/.exec(taskText)
    const implementation = implSection
      ? implSection[1].split('\n').map(s => s.replace(/^\d+\. /, '').trim()).filter(Boolean)
      : []

    // Parse reference
    const refSection = /\*\*Reference\*\*:\n((?:- .+?\n)+)/.exec(taskText)
    const reference = refSection ? {
      pattern: (/- Pattern: (.+)/m.exec(refSection[1]) || [])[1]?.trim() || "No pattern",
      files: ((/- Files: (.+)/m.exec(refSection[1]) || [])[1] || "").split(',').map(f => f.trim()).filter(Boolean),
      examples: (/- Examples: (.+)/m.exec(refSection[1]) || [])[1]?.trim() || "Follow pattern"
    } : {}

    // Parse acceptance
    const acceptSection = /\*\*Acceptance\*\*:\n((?:- .+?\n)+)/.exec(taskText)
    const acceptance = acceptSection
      ? acceptSection[1].split('\n').map(s => s.replace(/^- /, '').trim()).filter(Boolean)
      : []

    const task = {
      id: taskId,
      title: titleMatch?.[1].trim() || "Untitled",
      scope: scopeMatch?.[1].trim() || "",
      action: actionMatch?.[1].trim() || "Implement",
      description: descMatch?.[1].trim() || "",
      modification_points: modPoints,
      implementation,
      reference,
      acceptance,
      depends_on: depsMatch?.[1] === '[]' ? [] : (depsMatch?.[1] || "").replace(/[\[\]]/g, '').split(',').map(s => s.trim()).filter(Boolean)
    }

    // Add complexity-specific fields
    if (complexity === "Medium" || complexity === "High") {
      task.rationale = extractRationale(taskText)
      task.verification = extractVerification(taskText)
    }

    if (complexity === "High") {
      task.risks = extractRisks(taskText)
      task.code_skeleton = extractCodeSkeleton(taskText)
    }

    tasks.push(task)
  }

  return tasks
}

// Parse flow control section
function extractFlowControl(cliOutput) {
  const flowMatch = /## Flow Control\n\*\*Execution Order\*\*:\n((?:- .+?\n)+)/m.exec(cliOutput)
  const exitMatch = /\*\*Exit Conditions\*\*:\n- Success: (.+?)\n- Failure: (.+)/m.exec(cliOutput)

  const execution_order = []
  if (flowMatch) {
    flowMatch[1].trim().split('\n').forEach(line => {
      const m = /- Phase (.+?): \[(.+?)\] \((.+?)\)/.exec(line)
      if (m) execution_order.push({ phase: m[1], tasks: m[2].split(',').map(s => s.trim()), type: m[3].includes('independent') ? 'parallel' : 'sequential' })
    })
  }

  return {
    execution_order,
    exit_conditions: { success: exitMatch?.[1] || "All acceptance criteria met", failure: exitMatch?.[2] || "Critical task fails" }
  }
}

// Parse rationale section for a task
function extractRationale(taskText) {
  const rationaleMatch = /\*\*Rationale\*\*:\n- Chosen Approach: (.+?)\n- Alternatives Considered: (.+?)\n- Decision Factors: (.+?)\n- Tradeoffs: (.+)/s.exec(taskText)
  if (!rationaleMatch) return null

  return {
    chosen_approach: rationaleMatch[1].trim(),
    alternatives_considered: rationaleMatch[2].split(',').map(s => s.trim()).filter(Boolean),
    decision_factors: rationaleMatch[3].split(',').map(s => s.trim()).filter(Boolean),
    tradeoffs: rationaleMatch[4].trim()
  }
}

// Parse verification section for a task
function extractVerification(taskText) {
  const verificationMatch = /\*\*Verification\*\*:\n- Unit Tests: (.+?)\n- Integration Tests: (.+?)\n- Manual Checks: (.+?)\n- Success Metrics: (.+)/s.exec(taskText)
  if (!verificationMatch) return null

  return {
    unit_tests: verificationMatch[1].split(',').map(s => s.trim()).filter(Boolean),
    integration_tests: verificationMatch[2].split(',').map(s => s.trim()).filter(Boolean),
    manual_checks: verificationMatch[3].split(',').map(s => s.trim()).filter(Boolean),
    success_metrics: verificationMatch[4].split(',').map(s => s.trim()).filter(Boolean)
  }
}

// Parse risks section for a task
function extractRisks(taskText) {
  const risksPattern = /- Risk: (.+?) \| Probability: ([LMH]) \| Impact: ([LMH]) \| Mitigation: (.+?)(?: \| Fallback: (.+?))?(?=\n|$)/g
  const risks = []
  let match

  while ((match = risksPattern.exec(taskText)) !== null) {
    risks.push({
      description: match[1].trim(),
      probability: match[2] === 'L' ? 'Low' : match[2] === 'M' ? 'Medium' : 'High',
      impact: match[3] === 'L' ? 'Low' : match[3] === 'M' ? 'Medium' : 'High',
      mitigation: match[4].trim(),
      fallback: match[5]?.trim() || undefined
    })
  }

  return risks.length > 0 ? risks : null
}

// Parse code skeleton section for a task
function extractCodeSkeleton(taskText) {
  const skeletonSection = /\*\*Code Skeleton\*\*:\n([\s\S]*?)(?=\n\*\*|$)/.exec(taskText)
  if (!skeletonSection) return null

  const text = skeletonSection[1]
  const skeleton = {}

  // Parse interfaces
  const interfacesPattern = /- Interfaces: (.+?): (.+?) - (.+?)(?=\n|$)/g
  const interfaces = []
  let match
  while ((match = interfacesPattern.exec(text)) !== null) {
    interfaces.push({ name: match[1].trim(), definition: match[2].trim(), purpose: match[3].trim() })
  }
  if (interfaces.length > 0) skeleton.interfaces = interfaces

  // Parse functions
  const functionsPattern = /- Functions: (.+?) - (.+?) - returns (.+?)(?=\n|$)/g
  const functions = []
  while ((match = functionsPattern.exec(text)) !== null) {
    functions.push({ signature: match[1].trim(), purpose: match[2].trim(), returns: match[3].trim() })
  }
  if (functions.length > 0) skeleton.key_functions = functions

  // Parse classes
  const classesPattern = /- Classes: (.+?) - (.+?) - methods: (.+?)(?=\n|$)/g
  const classes = []
  while ((match = classesPattern.exec(text)) !== null) {
    classes.push({
      name: match[1].trim(),
      purpose: match[2].trim(),
      methods: match[3].split(',').map(s => s.trim()).filter(Boolean)
    })
  }
  if (classes.length > 0) skeleton.classes = classes

  return Object.keys(skeleton).length > 0 ? skeleton : null
}

// Parse data flow section
function extractDataFlow(cliOutput) {
  const dataFlowSection = /## Data Flow.*?\n([\s\S]*?)(?=\n## |$)/.exec(cliOutput)
  if (!dataFlowSection) return null

  const text = dataFlowSection[1]
  const diagramMatch = /\*\*Diagram\*\*: (.+?)(?=\n|$)/.exec(text)
  const depsMatch = /\*\*Dependencies\*\*: (.+?)(?=\n|$)/.exec(text)

  // Parse stages
  const stagesPattern = /- Stage (.+?): Input=(.+?) → Output=(.+?) \| Component=(.+?)(?: \| Transforms=(.+?))?(?=\n|$)/g
  const stages = []
  let match
  while ((match = stagesPattern.exec(text)) !== null) {
    stages.push({
      stage: match[1].trim(),
      input: match[2].trim(),
      output: match[3].trim(),
      component: match[4].trim(),
      transformations: match[5] ? match[5].split(',').map(s => s.trim()).filter(Boolean) : undefined
    })
  }

  return {
    diagram: diagramMatch?.[1].trim() || null,
    stages: stages.length > 0 ? stages : undefined,
    dependencies: depsMatch ? depsMatch[1].split(',').map(s => s.trim()).filter(Boolean) : undefined
  }
}

// Parse design decisions section
function extractDesignDecisions(cliOutput) {
  const decisionsSection = /## Design Decisions.*?\n([\s\S]*?)(?=\n## |$)/.exec(cliOutput)
  if (!decisionsSection) return null

  const decisionsPattern = /- Decision: (.+?) \| Rationale: (.+?)(?: \| Tradeoff: (.+?))?(?=\n|$)/g
  const decisions = []
  let match

  while ((match = decisionsPattern.exec(decisionsSection[1])) !== null) {
    decisions.push({
      decision: match[1].trim(),
      rationale: match[2].trim(),
      tradeoff: match[3]?.trim() || undefined
    })
  }

  return decisions.length > 0 ? decisions : null
}

// Parse all sections
function parseCLIOutput(cliOutput) {
  const complexity = (extractSection(cliOutput, "Complexity") || "Medium").trim()
  return {
    summary: extractSection(cliOutput, "Summary") || extractSection(cliOutput, "Implementation Summary"),
    approach: extractSection(cliOutput, "Approach") || extractSection(cliOutput, "High-Level Approach"),
    complexity,
    raw_tasks: extractStructuredTasks(cliOutput, complexity),
    flow_control: extractFlowControl(cliOutput),
    time_estimate: extractSection(cliOutput, "Time Estimate"),
    // High complexity only
    data_flow: complexity === "High" ? extractDataFlow(cliOutput) : null,
    // Medium/High complexity
    design_decisions: (complexity === "Medium" || complexity === "High") ? extractDesignDecisions(cliOutput) : null
  }
}
```

### Context Enrichment

```javascript
// NOTE: relevant_files items are structured objects:
//   {path, relevance, rationale, role, discovery_source?, key_symbols?}
function buildEnrichedContext(explorationsContext, explorationAngles) {
  const enriched = { relevant_files: [], patterns: [], dependencies: [], integration_points: [], constraints: [] }

  explorationAngles.forEach(angle => {
    const exp = explorationsContext?.[angle]
    if (exp) {
      enriched.relevant_files.push(...(exp.relevant_files || []))
      enriched.patterns.push(exp.patterns || '')
      enriched.dependencies.push(exp.dependencies || '')
      enriched.integration_points.push(exp.integration_points || '')
      enriched.constraints.push(exp.constraints || '')
    }
  })

  // Deduplicate by path, keep highest relevance entry for each path
  const fileMap = new Map()
  enriched.relevant_files.forEach(f => {
    const path = typeof f === 'string' ? f : f.path
    const existing = fileMap.get(path)
    if (!existing || (f.relevance || 0) > (existing.relevance || 0)) {
      fileMap.set(path, typeof f === 'string' ? { path: f, relevance: 0.5, rationale: 'discovered', role: 'context_only' } : f)
    }
  })
  enriched.relevant_files = [...fileMap.values()]
  return enriched
}
```

### Task Enhancement

```javascript
function validateAndEnhanceTasks(rawTasks, enrichedContext) {
  return rawTasks.map((task, idx) => ({
    id: task.id || `T${idx + 1}`,
    title: task.title || "Unnamed task",
    file: task.file || inferFile(task, enrichedContext),
    action: task.action || inferAction(task.title),
    description: task.description || task.title,
    modification_points: task.modification_points?.length > 0
      ? task.modification_points
      : [{ file: task.file, target: "main", change: task.description }],
    implementation: task.implementation?.length >= 2
      ? task.implementation
      : [`Analyze ${task.file}`, `Implement ${task.title}`, `Add error handling`],
    reference: task.reference || { pattern: "existing patterns", files: enrichedContext.relevant_files.slice(0, 2).map(f => typeof f === 'string' ? f : f.path), examples: "Follow existing structure" },
    acceptance: task.acceptance?.length >= 1
      ? task.acceptance
      : [`${task.title} completed`, `Follows conventions`],
    depends_on: task.depends_on || []
  }))
}

function inferAction(title) {
  const map = { create: "Create", update: "Update", implement: "Implement", refactor: "Refactor", delete: "Delete", config: "Configure", test: "Test", fix: "Fix" }
  const match = Object.entries(map).find(([key]) => new RegExp(key, 'i').test(title))
  return match ? match[1] : "Implement"
}

// NOTE: relevant_files items are structured objects with .path property
function inferFile(task, ctx) {
  const files = ctx?.relevant_files || []
  const getPath = f => typeof f === 'string' ? f : f.path
  return getPath(files.find(f => task.title.toLowerCase().includes(getPath(f).split('/').pop().split('.')[0].toLowerCase())) || {}) || "file-to-be-determined.ts"
}
```

### CLI Execution ID Assignment (MANDATORY)

```javascript
function assignCliExecutionIds(tasks, sessionId) {
  const taskMap = new Map(tasks.map(t => [t.id, t]))
  const childCount = new Map()

  // Count children for each task
  tasks.forEach(task => {
    (task.depends_on || []).forEach(depId => {
      childCount.set(depId, (childCount.get(depId) || 0) + 1)
    })
  })

  tasks.forEach(task => {
    task.cli_execution_id = `${sessionId}-${task.id}`
    const deps = task.depends_on || []

    if (deps.length === 0) {
      task.cli_execution = { strategy: "new" }
    } else if (deps.length === 1) {
      const parent = taskMap.get(deps[0])
      const parentChildCount = childCount.get(deps[0]) || 0
      task.cli_execution = parentChildCount === 1
        ? { strategy: "resume", resume_from: parent.cli_execution_id }
        : { strategy: "fork", resume_from: parent.cli_execution_id }
    } else {
      task.cli_execution = {
        strategy: "merge_fork",
        merge_from: deps.map(depId => taskMap.get(depId).cli_execution_id)
      }
    }
  })
  return tasks
}
```

**Strategy Rules**:
| depends_on | Parent Children | Strategy | CLI Command |
|------------|-----------------|----------|-------------|
| [] | - | `new` | `--id {cli_execution_id}` |
| [T1] | 1 | `resume` | `--resume {resume_from}` |
| [T1] | >1 | `fork` | `--resume {resume_from} --id {cli_execution_id}` |
| [T1,T2] | - | `merge_fork` | `--resume {ids.join(',')} --id {cli_execution_id}` |

### Flow Control Inference

```javascript
function inferFlowControl(tasks) {
  const phases = [], scheduled = new Set()
  let num = 1

  while (scheduled.size < tasks.length) {
    const ready = tasks.filter(t => !scheduled.has(t.id) && t.depends_on.every(d => scheduled.has(d)))
    if (!ready.length) break

    const isParallel = ready.length > 1 && ready.every(t => !t.depends_on.length)
    phases.push({ phase: `${isParallel ? 'parallel' : 'sequential'}-${num}`, tasks: ready.map(t => t.id), type: isParallel ? 'parallel' : 'sequential' })
    ready.forEach(t => scheduled.add(t.id))
    num++
  }

  return { execution_order: phases, exit_conditions: { success: "All acceptance criteria met", failure: "Critical task fails" } }
}
```

### planObject Generation

```javascript
function generatePlanObject(parsed, enrichedContext, input, schemaType) {
  const complexity = parsed.complexity || input.complexity || "Medium"
  const tasks = validateAndEnhanceTasks(parsed.raw_tasks, enrichedContext, complexity)
  assignCliExecutionIds(tasks, input.session.id)  // MANDATORY: Assign CLI execution IDs
  const flow_control = parsed.flow_control?.execution_order?.length > 0 ? parsed.flow_control : inferFlowControl(tasks)
  const focus_paths = [...new Set(tasks.flatMap(t => [t.file || t.scope, ...t.modification_points.map(m => m.file)]).filter(Boolean))]

  // Base fields (common to both schemas)
  const base = {
    summary: parsed.summary || `Plan for: ${input.task_description.slice(0, 100)}`,
    tasks,
    flow_control,
    focus_paths,
    estimated_time: parsed.time_estimate || `${tasks.length * 30} minutes`,
    recommended_execution: (complexity === "Low" || input.severity === "Low") ? "Agent" : "Codex",
    _metadata: {
      timestamp: new Date().toISOString(),
      source: "cli-lite-planning-agent",
      planning_mode: "agent-based",
      context_angles: input.contextAngles || [],
      duration_seconds: Math.round((Date.now() - startTime) / 1000)
    }
  }

  // Add complexity-specific top-level fields
  if (complexity === "Medium" || complexity === "High") {
    base.design_decisions = parsed.design_decisions || []
  }

  if (complexity === "High") {
    base.data_flow = parsed.data_flow || null
  }

  // Schema-specific fields
  if (schemaType === 'fix-plan') {
    return {
      ...base,
      root_cause: parsed.root_cause || "Root cause from diagnosis",
      strategy: parsed.strategy || "comprehensive_fix",
      severity: input.severity || "Medium",
      risk_level: parsed.risk_level || "medium"
    }
  } else {
    return {
      ...base,
      approach: parsed.approach || "Step-by-step implementation",
      complexity
    }
  }
}

// Enhanced task validation with complexity-specific fields
function validateAndEnhanceTasks(rawTasks, enrichedContext, complexity) {
  return rawTasks.map((task, idx) => {
    const enhanced = {
      id: task.id || `T${idx + 1}`,
      title: task.title || "Unnamed task",
      scope: task.scope || task.file || inferFile(task, enrichedContext),
      action: task.action || inferAction(task.title),
      description: task.description || task.title,
      modification_points: task.modification_points?.length > 0
        ? task.modification_points
        : [{ file: task.scope || task.file, target: "main", change: task.description }],
      implementation: task.implementation?.length >= 2
        ? task.implementation
        : [`Analyze ${task.scope || task.file}`, `Implement ${task.title}`, `Add error handling`],
      reference: task.reference || { pattern: "existing patterns", files: enrichedContext.relevant_files.slice(0, 2).map(f => typeof f === 'string' ? f : f.path), examples: "Follow existing structure" },
      acceptance: task.acceptance?.length >= 1
        ? task.acceptance
        : [`${task.title} completed`, `Follows conventions`],
      depends_on: task.depends_on || []
    }

    // Add Medium/High complexity fields
    if (complexity === "Medium" || complexity === "High") {
      enhanced.rationale = task.rationale || {
        chosen_approach: "Standard implementation approach",
        alternatives_considered: [],
        decision_factors: ["Maintainability", "Performance"],
        tradeoffs: "None significant"
      }
      enhanced.verification = task.verification || {
        unit_tests: [`test_${task.id.toLowerCase()}_basic`],
        integration_tests: [],
        manual_checks: ["Verify expected behavior"],
        success_metrics: ["All tests pass"]
      }
    }

    // Add High complexity fields
    if (complexity === "High") {
      enhanced.risks = task.risks || [{
        description: "Implementation complexity",
        probability: "Low",
        impact: "Medium",
        mitigation: "Incremental development with checkpoints"
      }]
      enhanced.code_skeleton = task.code_skeleton || null
    }

    return enhanced
  })
}
```

### Error Handling

```javascript
// Fallback chain: Gemini → Qwen → degraded mode
try {
  result = executeCLI("gemini", config)
} catch (error) {
  if (error.code === 429 || error.code === 404) {
    try { result = executeCLI("qwen", config) }
    catch { return { status: "degraded", planObject: generateBasicPlan(task_description, enrichedContext) } }
  } else throw error
}

// NOTE: relevant_files items are structured objects with .path property
function generateBasicPlan(taskDesc, ctx) {
  const files = (ctx?.relevant_files || []).map(f => typeof f === 'string' ? f : f.path)
  const tasks = [taskDesc].map((t, i) => ({
    id: `T${i + 1}`, title: t, file: files[i] || "tbd", action: "Implement", description: t,
    modification_points: [{ file: files[i] || "tbd", target: "main", change: t }],
    implementation: ["Analyze structure", "Implement feature", "Add validation"],
    acceptance: ["Task completed", "Follows conventions"], depends_on: []
  }))

  return {
    summary: `Direct implementation: ${taskDesc}`, approach: "Step-by-step", tasks,
    flow_control: { execution_order: [{ phase: "sequential-1", tasks: tasks.map(t => t.id), type: "sequential" }], exit_conditions: { success: "Done", failure: "Fails" } },
    focus_paths: files, estimated_time: "30 minutes", recommended_execution: "Agent", complexity: "Low",
    _metadata: { timestamp: new Date().toISOString(), source: "cli-lite-planning-agent", planning_mode: "direct", exploration_angles: [], duration_seconds: 0 }
  }
}
```

## Quality Standards

### Task Validation

```javascript
function validateTask(task) {
  const errors = []
  if (!/^T\d+$/.test(task.id)) errors.push("Invalid task ID")
  if (!task.title?.trim()) errors.push("Missing title")
  if (!task.file?.trim()) errors.push("Missing file")
  if (!['Create', 'Update', 'Implement', 'Refactor', 'Add', 'Delete', 'Configure', 'Test', 'Fix'].includes(task.action)) errors.push("Invalid action")
  if (!task.implementation?.length >= 2) errors.push("Need 2+ implementation steps")
  if (!task.acceptance?.length >= 1) errors.push("Need 1+ acceptance criteria")
  if (task.depends_on?.some(d => !/^T\d+$/.test(d))) errors.push("Invalid dependency format")
  if (task.acceptance?.some(a => /works correctly|good performance/i.test(a))) errors.push("Vague acceptance criteria")
  return { valid: !errors.length, errors }
}
```

### Acceptance Criteria

| ✓ Good | ✗ Bad |
|--------|-------|
| "3 methods: login(), logout(), validate()" | "Service works correctly" |
| "Response time < 200ms p95" | "Good performance" |
| "Covers 80% of edge cases" | "Properly implemented" |

## Key Reminders

**ALWAYS**:
- **Search Tool Priority**: ACE (`mcp__ace-tool__search_context`) → CCW (`mcp__ccw-tools__smart_search`) / Built-in (`Grep`, `Glob`, `Read`)
- **Read schema first** to determine output structure
- Generate task IDs (T1/T2 for plan, FIX1/FIX2 for fix-plan)
- Include depends_on (even if empty [])
- **Assign cli_execution_id** (`{sessionId}-{taskId}`)
- **Compute cli_execution strategy** based on depends_on
- Quantify acceptance/verification criteria
- Generate flow_control from dependencies
- Handle CLI errors with fallback chain

**Bash Tool**:
- Use `run_in_background=false` for all Bash/CLI calls to ensure foreground execution

**NEVER**:
- Execute implementation (return plan only)
- Use vague acceptance criteria
- Create circular dependencies
- Skip task validation
- **Skip CLI execution ID assignment**
- **Ignore schema structure**
- **Skip Phase 5 Plan Quality Check**

---

## Phase 5: Plan Quality Check (MANDATORY)

### Overview

After generating plan.json, **MUST** execute CLI quality check before returning to orchestrator. This is a mandatory step for ALL plans regardless of complexity.

### Quality Dimensions

| Dimension | Check Criteria | Critical? |
|-----------|---------------|-----------|
| **Completeness** | All user requirements reflected in tasks | Yes |
| **Task Granularity** | Each task 15-60 min scope | No |
| **Dependencies** | No circular deps, correct ordering | Yes |
| **Acceptance Criteria** | Quantified and testable (not vague) | No |
| **Implementation Steps** | 2+ actionable steps per task | No |
| **Constraint Compliance** | Follows specs/*.md | Yes |

### CLI Command Format

Use `ccw cli` with analysis mode to validate plan against quality dimensions:

```bash
ccw cli -p "Validate plan quality: completeness, granularity, dependencies, acceptance criteria, implementation steps, constraint compliance" \
  --tool gemini --mode analysis \
  --context "@{plan_json_path} @.workflow/specs/*.md"
```

**Expected Output Structure**:
- Quality Check Report (6 dimensions with pass/fail status)
- Summary (critical/minor issue counts)
- Recommendation: `PASS` | `AUTO_FIX` | `REGENERATE`
- Fixes (JSON patches if AUTO_FIX)

### Result Parsing

Parse CLI output sections using regex to extract:
- **6 Dimension Results**: Each with `passed` boolean and issue lists (missing requirements, oversized/undersized tasks, vague criteria, etc.)
- **Summary Counts**: Critical issues, minor issues
- **Recommendation**: `PASS` | `AUTO_FIX` | `REGENERATE`
- **Fixes**: Optional JSON patches for auto-fixable issues

### Auto-Fix Strategy

Apply automatic fixes for minor issues:

| Issue Type | Auto-Fix Action | Example |
|-----------|----------------|---------|
| **Vague Acceptance** | Replace with quantified criteria | "works correctly" → "All unit tests pass with 100% success rate" |
| **Insufficient Steps** | Expand to 4-step template | Add: Analyze → Implement → Error handling → Verify |
| **CLI-Provided Patches** | Apply JSON patches from CLI output | Update task fields per patch specification |

After fixes, update `_metadata.quality_check` with fix log.

### Execution Flow

After Phase 4 planObject generation:

1. **Write Initial Plan** → `${sessionFolder}/plan.json`
2. **Execute CLI Check** → Gemini (Qwen fallback)
3. **Parse Results** → Extract recommendation and issues
4. **Handle Recommendation**:

| Recommendation | Action | Return Status |
|---------------|--------|---------------|
| `PASS` | Log success, add metadata | `success` |
| `AUTO_FIX` | Apply fixes, update plan.json, log fixes | `success` |
| `REGENERATE` | Log critical issues, add issues to metadata | `needs_review` |

5. **Return** → Plan with `_metadata.quality_check` containing execution result

**CLI Fallback**: Gemini → Qwen → Skip with warning (if both fail)
