--- name: plan description: Interactive wizard that scans the codebase, proposes a metric/guard/agent config, and writes a program.md run spec. With confirmation, runs cProfile on a Python file path to surface bottlenecks before prompting for optimization goal. Distinct from `/research:topic plan`, which turns prior SOTA research into an implementation roadmap, not a program.md. argument-hint: | [out.md] [--team] effort: medium allowed-tools: Read, Write, Edit, Bash, Grep, Glob, Agent, TaskCreate, TaskUpdate, AskUserQuestion disable-model-invocation: true --- Wizard: scans codebase, proposes metric/guard/agent config, writes `program.md` run spec. With confirmation, runs cProfile on a Python file path to surface bottlenecks before prompting for optimization goal. NOT for: running experiments (use `/research:run`); methodology validation (use `/research:judge`); full pipeline from goal to result (use `/research:sweep`); benchmarking or microbenchmark design (use `foundry:perf-optimizer` — plan's scope is ML metric optimization loops, not raw latency/throughput benchmarking). ## Agent Resolution **Agent resolution**: load and follow the protocol below. Contains: foundry check + fallback table. Foundry not installed → substitute each `foundry:X` with `general-purpose` per table. Agents this skill uses: `foundry:solution-architect`, `foundry:perf-optimizer`, `research:scientist`. ```bash # loads: unsupported-flag-protocol.md PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name research) || exit $?; export PLUGIN_ROOT export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}" python "$PLUGIN_ROOT/bin/resolve_shared.py" --persist research-shared --emit agent-resolution.md --emit unsupported-flag-protocol.md || exit 1 # timeout: 5000 — cold resolve; every later site reads the research-shared sentinel instead of re-running python ``` The same call prints the unsupported-flag protocol used by the flag check below. ## Plan Mode (Steps P-P0–P-P4) **Task tracking**: create tasks for P-P0, P-P1, P-P2, P-P2b, P-P3 at start, all in the same response as the first real tool call; add P-P4 only if `--team` detected in arguments. Every later `TaskUpdate` rides with the next real tool call — never a bookkeeping-only turn; the one standalone call is a `completed` right before a long report. **Unsupported flag check**: follow the unsupported-flag protocol printed by the Agent Resolution block above. Supported flags for this skill: `--team`. ### Step P-P0: Detect input type Parse `` from arguments. Determine: **file path** or **goal string**: Extract first positional token (strip all `--` tokens from `$ARGUMENTS`, take first remaining token as `FILE_ARG`). Then: **Disambiguation guard** — treat `FILE_ARG` as file path only if it matches `*.py` and exists on disk, and only when the non-flag token count is 1 or 2 (the 2-token form additionally requires the second token to end `.md` — see classification below). Other file types and 3+ non-flag tokens are always goal text — never run `test -f` on first token of a 3+-token goal: **Quoting note**: `$ARGUMENTS` raw string (not shell-tokenized). User-supplied quotes (e.g. `plan "train.py out.md"`) appear as literal characters. Before token counting, strip surrounding matched quotes from `$ARGUMENTS` so `FILE_ARG` resolves bare (`"train.py out.md"` → `train.py`, not `"train.py`): ```bash PLUGIN_ROOT=$(python "${CLAUDE_PLUGIN_ROOT}/bin/resolve_plugin_root.py" --plugin-name research) || exit $?; export PLUGIN_ROOT export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}" # --keep-state: no eval; --inlined: this step keeps no compaction contract, so it clears none python "$PLUGIN_ROOT/bin/parse-skill-flags.py" --flags team --keep-state research-plan --inlined -- "$ARGUMENTS" >/dev/null # timeout: 5000 IFS= read -r CLEAN_ARGS < "${TMPDIR:-/tmp}/research-plan-clean-args-${CSID}" 2>/dev/null || CLEAN_ARGS="" # CLEAN_ARGS has flags and --keep "" removed; sed pair still strips user's own # wrapping quotes — no flag parser touches those _STRIPPED=$(echo "$CLEAN_ARGS" | sed -E 's/^"(.*)"$/\1/; s/^'\''(.*)'\''$/\1/') # timeout: 5000 NONFLAG_TOKEN_COUNT=$(echo "$_STRIPPED" | tr ' ' '\n' | grep -v '^--' | grep -v '^$' | wc -l | tr -d ' ') # timeout: 5000 FILE_ARG=$(echo "$_STRIPPED" | tr ' ' '\n' | grep -v '^--' | grep -v '^$' | head -1) # timeout: 5000 SECOND_ARG=$(echo "$_STRIPPED" | tr ' ' '\n' | grep -v '^--' | grep -v '^$' | sed -n '2p') # timeout: 5000 echo "$FILE_ARG" > "${TMPDIR:-/tmp}/research-plan-file-arg-${CSID}" OUT_ARG=""; [ "$NONFLAG_TOKEN_COUNT" = "2" ] && case "$FILE_ARG" in *.py) [ -f "$FILE_ARG" ] && case "$SECOND_ARG" in *.md) OUT_ARG="$SECOND_ARG";; esac ;; esac echo "$OUT_ARG" > "${TMPDIR:-/tmp}/research-plan-out-arg-${CSID}" # empty = no override, P-P2b defaults to program.md FILE_EXISTS=false; { [ "$NONFLAG_TOKEN_COUNT" = "1" ] || [ "$NONFLAG_TOKEN_COUNT" = "2" ]; } && case "$FILE_ARG" in *.py) [ -f "$FILE_ARG" ] && FILE_EXISTS=true ;; esac # 3+ tokens are goal text: never test -f echo "NONFLAG_TOKEN_COUNT=$NONFLAG_TOKEN_COUNT FILE_ARG=$FILE_ARG FILE_EXISTS=$FILE_EXISTS OUT_ARG=$OUT_ARG" # classification rules below read these values ``` 1. `NONFLAG_TOKEN_COUNT == 1` AND `FILE_ARG` matches `*.py` AND `test -f "$FILE_ARG"` succeeds → **file path**, default output (`program.md`). Enter profiling flow. 2. `NONFLAG_TOKEN_COUNT == 2` AND `FILE_ARG` matches `*.py` AND `test -f "$FILE_ARG"` succeeds AND second token matches `*.md` → **file path**, output path = second token (persisted above as `OUT_ARG`). Enter profiling flow. 3. Otherwise (non-Python file, 3+ tokens, first token not on disk, or 2-token where second isn't `.md`) → **goal string**. Use `$_STRIPPED` (minus flags — not raw `$ARGUMENTS`) as ``. Skip to Step P-P1. **Profiling flow** (file path detected): Run baseline profiling using `FILE_ARG` only — never raw `$ARGUMENTS` in cProfile command. **Eligibility pre-check** — run this cheap file check before asking for profiling consent; it never executes the script: ```bash export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}" IFS= read -r FILE_ARG < "${TMPDIR:-/tmp}/research-plan-file-arg-${CSID}" 2>/dev/null || FILE_ARG="" PROFILE_ELIGIBLE=false case "$FILE_ARG" in *.py) [ -f "$FILE_ARG" ] && grep -q '__main__' "$FILE_ARG" 2>/dev/null && PROFILE_ELIGIBLE=true ;; esac echo "PROFILE_ELIGIBLE=$PROFILE_ELIGIBLE" ``` If `PROFILE_ELIGIBLE=false`, set `PROFILE_AVAILABLE=false` and go directly to the fallback path below, skipping consent and the cProfile block. **Profiling consent** — only when `PROFILE_ELIGIBLE=true`, before running the block below, invoke `AskUserQuestion`: "Profile ``? This executes the script for up to 600 seconds and can train models, write checkpoints, or perform downloads. To provide a goal without profiling, enter it in Other." Routes: (a) **Provide goal without profiling** — enter the goal as free text through the question's built-in **Other** input, skip execution, and use that text as `` to proceed directly to P-P1 without another question; (b) **Profile** — authorize this script execution; (c) **Abort** — stop. Supply only **Profile** and **Abort** as predefined options; route (a) uses **Other**. Only after the user selects **Profile** may the cProfile block run. A warning or a supplied file path is not confirmation. **Module-file guard**: `python -m cProfile` requires executable script (has `if __name__ == "__main__":` guard or runs directly). Library/module files without entry point produce empty cProfile output. One block pre-checks the guard and, when it passes, profiles — a failed guard only skips the profile run. The last line prints `PROFILE_AVAILABLE=true|false`: ```bash export CSID="${CLAUDE_CODE_SESSION_ID:-$PPID}" IFS= read -r FILE_ARG < "${TMPDIR:-/tmp}/research-plan-file-arg-${CSID}" 2>/dev/null || FILE_ARG="" # re-derive — bash state lost between Bash() calls PROFILE_AVAILABLE=true case "$FILE_ARG" in *.py) ;; *) echo "⚠ Not a Python file — falling back to goal-string path."; PROFILE_AVAILABLE=false ;; esac grep -q '__main__' "$FILE_ARG" 2>/dev/null || { echo "⚠ File has no __main__ guard — cProfile will produce empty output. Falling back to goal-string path."; PROFILE_AVAILABLE=false; } if [ "$PROFILE_AVAILABLE" != "false" ]; then CPROFILE_OUT=$(mktemp -t research-plan-XXXX) # timeout: 3000 echo "→ profiling $FILE_ARG under cProfile (≤600 s) — runs the script" python -m cProfile -s cumtime "$FILE_ARG" > "$CPROFILE_OUT" 2>&1 # timeout: 600000 PROFILE_EXIT=$? if [ $PROFILE_EXIT -ne 0 ]; then echo "⚠ cProfile failed (exit $PROFILE_EXIT) — continuing without profile data. Wizard will prompt for goal string instead." PROFILE_AVAILABLE=false else PROFILE_AVAILABLE=true head -40 "$CPROFILE_OUT" # timeout: 5000 # wall-clock number comes from cProfile's own header ("N function calls in X.XXX seconds") — label it "(measured under profiler)"; second bare `time python` run costs up to 10 min for number run skill re-measures via metric_cmd anyway fi fi echo "PROFILE_AVAILABLE=$PROFILE_AVAILABLE" ``` **Fallback path** — ONLY when `PROFILE_AVAILABLE=false`: skip bottleneck selection menu. Invoke `AskUserQuestion` with options: (a) **Provide goal** — enter optimization goal string directly (cProfile unavailable — note: cProfile requires self-contained runnable script with `if __name__ == '__main__'` guard; modules and test files not supported); (b) **Abort** — stop. Use user's response as ``. Proceed directly to P-P1. Skip profile-available path entirely — do not read or execute following block. **Profile-available path** — ONLY when `PROFILE_AVAILABLE=true` (skip entirely if fallback path taken above), present top up to 5 bottleneck functions (skip rows with no data available): ```markdown Top bottleneck functions: 1. — s (%) 2. — s (%) ... ``` Invoke `AskUserQuestion` — "What would you like to optimize?", options: (a) Overall execution time · (b) Memory usage · (c) Specific function: `` (currently `