schema_version: 1
workflow_id: parallel_kanban_review
vars:
  review_topic: "Implement the next safe increment"
steps:
  - id: plan_kanban_tasks
    command:
      - yy
      - pi
      - "Plan mode: create the concrete kanban tasks needed for this topic, then print TASK_IDS=<comma-separated-kanban-task-ids>. Topic: {{ review_topic }}. Use ./.juno_task/scripts/kanban.sh as the source of truth. Keep task bodies complete enough for parallel agents."
  - id: resolve_task_ids
    capture_session: false
    command: |
      set -eu
      mkdir -p "{{ out_dir }}"
      cp "{{ steps.plan_kanban_tasks.response_path }}" "{{ out_dir }}/plan_response.txt"
      task_ids=$(python3 - <<'PY'
      import re
      from pathlib import Path
      text = Path("{{ out_dir }}/plan_response.txt").read_text(encoding="utf-8")
      match = re.search(r"^TASK_IDS=([^\n]+)", text, re.MULTILINE)
      print(match.group(1).strip() if match else "")
      PY
      )
      if [ -z "$task_ids" ]; then
        echo "plan_kanban_tasks must print TASK_IDS=<comma-separated-kanban-task-ids>" >&2
        exit 2
      fi
      printf '%s\n' "$task_ids" | tee "{{ out_dir }}/kanban_task_ids.txt"
  - id: run_parallel_kanban
    capture_session: false
    command: |
      set -eu
      task_ids=$(cat "{{ out_dir }}/kanban_task_ids.txt")
      python3 - <<'PY'
      from pathlib import Path
      out_dir = Path("{{ out_dir }}")
      task_placeholder = f"{chr(123) * 2}task_id{chr(125) * 2}"
      (out_dir / "kanban_worker_prompt.md").write_text(
          f"Implement exactly kanban task ##{task_placeholder}.\n"
          "Keep the kanban response current, run focused validation, and ensure final output includes commit hash, changed files, validation commands, and any preserved session id.\n",
          encoding="utf-8",
      )
      PY
      ./.juno_task/scripts/parallel_runner.sh \
        --kanban "$task_ids" \
        --parallel 3 \
        --prompt-file "{{ out_dir }}/kanban_worker_prompt.md" \
        --output-dir "{{ out_dir }}/parallel"
  - id: prepare_master_review
    capture_session: false
    command: |
      set -eu
      latest=$(find "{{ out_dir }}/parallel" -name 'aggregation_*.json' -print 2>/dev/null | sort | tail -n 1)
      if [ -z "$latest" ]; then
        echo "No aggregation_*.json found under {{ out_dir }}/parallel" >&2
        exit 2
      fi
      {
        printf 'Review the completed parallel kanban batch for topic: %s.\n\n' "{{ review_topic }}"
        printf 'Read the latest aggregation artifact at: %s\n' "$latest"
        printf 'It preserves each worker final response, session id, commit hash, status, and cost so this master review does not need to reconstruct history from raw logs.\n\n'
        printf 'Aggregation JSON:\n'
        cat "$latest"
        printf '\n\nProduce a concise merge/review plan with: completed tasks, failures needing follow-up, commits to inspect, validation gaps, and recommended next kanban updates.\n'
      } > "{{ out_dir }}/master_review_prompt.md"
  - id: master_review
    capture_session: true
    command:
      - yy
      - pi
      - --prompt-file
      - "{{ out_dir }}/master_review_prompt.md"
summary: |
  # Parallel kanban review
  Plan session: {{ steps.plan_kanban_tasks.session_id }}
  Task ids: {{ steps.resolve_task_ids.response }}
  Parallel artifacts: {{ out_dir }}/parallel
  Master review session: {{ steps.master_review.session_id }}
  Master review response: {{ steps.master_review.response }}
