--- name: task-agent description: > Reads a list of tasks in a yaml to be done in certain github repos. Agents clone the repo, do the task, commit the changes and create a PR. Not to be called automatically by Claude by any means. tools: Bash, PowerShell, Read, Write, Edit, Glob, Grep, Agent, TaskCreate, TaskUpdate, mcp__github__search_repositories, mcp__github__create_pull_request model: opus --- # Task Agent Each invocation does **exactly one task**: the next pending item from the tasks file. The same file (or a sibling state file, in legacy mode) tracks completion, so reruns always pick up where the previous run left off. **Arguments** (all optional, `key=value` form): - `tasks=` — path to the tasks file. Default: `agent-tasks.yml` or `agent-tasks.yaml` in cwd. - `state=` — path to a separate state file. Forces legacy mode. Default: auto-derived (`-state.yml`) and only used if the tasks file is in legacy format. **Two file formats are supported:** - **Unified** (preferred): one file holds tasks *and* their completion status. Each task has an `id`, a `description`, and a `status` (`pending` | `done` | `failed` | `skipped`). On completion the same entry gains `branch`, `pr_url`, `date`. Unknown keys (e.g. `external_ref`, `labels`, `priority`) are preserved verbatim across rewrites so external sync tools can attach metadata. - **Legacy**: two files — a tasks file with bare-string tasks and a separate `*-state.yml` listing completed entries. Auto-detected when the tasks file has no `status:` field. Existing setups keep working unchanged. See [`../../references/format.md`](../../references/format.md) for the full schema, the status enum, and the passthrough-metadata contract. **Prereqs:** `python3`, `git`. --- ## Task Tracking Before doing any work, call `TaskCreate` for each phase below. Call `TaskUpdate` (status `in_progress`) when you begin a phase and `TaskUpdate` (status `completed`) when you finish it. - Load config and state - Prepare repository - Execute task and open PR - Update state and report --- ## Phase 1 — Load config and state ### 1.1 Resolve file paths from arguments Parse `tasks=` and `state=` from the invocation arguments. If `tasks=` is not given, search the current directory for `agent-tasks.yml` then `agent-tasks.yaml`. Stop and show the expected format if no file is found. ### 1.2 Parse the tasks file and auto-detect the mode All YAML reading and writing goes through the helper script `${CLAUDE_PLUGIN_ROOT}/scripts/tasks_io.py`, which guarantees that unknown keys on task entries (e.g. `external_ref`, `labels`) survive every round-trip. The script's contract: - **Unified mode** — auto-detected when any task in the file is an object carrying a `status:` field. The tasks file is the single source of truth; no state file is read. - **Legacy mode** — every other shape (bare strings, dicts without `status`). The script reads the state file from the explicit `state=` argument if given, else from `-state.yml` next to the tasks file. A missing state file is treated as empty. - **Normalization** — every task is returned as `{repo, id, description, status, ...}`. Missing ids are synthesized as `md5(repo + "\n" + description)[:6]`; missing statuses default to `done` (legacy task whose `(repo, description)` is in `state.completed`) or `pending`. Invoke it and capture the JSON: ```bash python3 "${CLAUDE_PLUGIN_ROOT}/scripts/tasks_io.py" load "$TASKS_PATH" \ ${STATE_PATH:+--state "$STATE_PATH"} ``` Remember the `mode` field from the output — Phase 4 needs it to write back in the same format. ### 1.3 Pick the next pending task Walk the normalized list in order and pick the first task whose `status == 'pending'`. Tasks with `status` of `done`, `failed`, or `skipped` are passed over silently (failures and skips were intentional outcomes of earlier runs). If nothing is pending, tell the user — all done, nothing left to do — and end with the result block `{"status": "none"}` (Phase 4.4). Print the chosen task clearly before proceeding: ``` Today's task: Repo: owner/repo-name ID: abc123 Task: "Add unit tests for the authentication module" ``` --- ## Phase 2 — Prepare the repo Only clone the **one repo** containing today's task if not already cloned locally. Each repo lives under a `task-agent` directory in the OS temp dir (so the local clone is reused across runs and Windows/Linux/macOS all work). The exact base directory is: ```bash WORKDIR="${TASK_AGENT_WORKDIR:-$(python3 -c 'import os,tempfile;print(os.path.join(tempfile.gettempdir(),"task-agent"))')}" ``` Override with the `TASK_AGENT_WORKDIR` env var if you need a different location (e.g. a persistent disk on a build agent). Run this phase inline. It is a lookup and a `git clone`/`fetch`, which does not need a fresh agent's context. ### 2.1 Determine the default branch Call `mcp__github__search_repositories` with `query: "repo:OWNER/REPO_NAME"` and `minimal_output: false`. Read the `default_branch` field from the returned repository object. ### 2.2 Clone or update the repo locally ```bash LOCAL_PATH="$WORKDIR/REPO_NAME" if [ -d "$LOCAL_PATH/.git" ]; then git -C "$LOCAL_PATH" fetch origin git -C "$LOCAL_PATH" checkout DEFAULT_BRANCH git -C "$LOCAL_PATH" reset --hard origin/DEFAULT_BRANCH # reset --hard keeps untracked files. Drop leftovers from earlier runs so they # cannot end up in this task's commit, but only in a clone task-agent owns: # one it created (marker file), or any clone in the default temp workdir, which # Phase 5 deletes anyway. A clone the user keeps under a custom # TASK_AGENT_WORKDIR is never cleaned; the implementer's by-path staging still # keeps its untracked files out of the commit. if [ -f "$LOCAL_PATH/.git/task-agent-clone" ] || [ -z "$TASK_AGENT_WORKDIR" ]; then git -C "$LOCAL_PATH" clean -fd # ignored files such as node_modules stay fi else mkdir -p "$WORKDIR" git clone "https://github.com/OWNER/REPO_NAME.git" "$LOCAL_PATH" touch "$LOCAL_PATH/.git/task-agent-clone" # marks the clone as task-agent-owned fi ``` --- ## Phase 3 — Execute the task Run 3.1, 3.2, 3.4 and 3.5 inline. Only the implementation itself (3.3) goes to a subagent, so the code-writing agent is one level deep, not two. ### 3.1 Create a branch name Slugify the task text and append a short hash for uniqueness: ```bash TASK="" SLUG=$(echo "$TASK" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/-\+/-/g' | sed 's/^-\|-$//g' | cut -c1-45) HASH=$(echo "$TASK" | python3 -c "import sys,hashlib; print(hashlib.md5(sys.stdin.read().encode()).hexdigest()[:6])") BRANCH="task/${SLUG}-${HASH}" ``` ### 3.2 Create the branch ```bash git -C "$LOCAL_PATH" checkout DEFAULT_BRANCH # -B is idempotent: creates the branch, or resets it to the current commit if a stale # copy was left behind by an interrupted prior run. git -C "$LOCAL_PATH" checkout -B "$BRANCH" ``` ### 3.3 Spawn the implementer agent Spawn the plugin's dedicated agent, `task-agent:task-implementer` (defined in [`../../agents/task-implementer.md`](../../agents/task-implementer.md)). Its role instructions live there: read the repo's CLAUDE.md/CONTRIBUTING/CI config, implement, **run the repo's own checks**, and commit only when they pass. It has no `Agent` tool and never pushes. Pass only the context: ``` Agent( subagent_type: "task-agent:task-implementer", description: "Implement task ", prompt: """ LOCAL_PATH: Repository: BRANCH: REFERENCES_DIR: ${CLAUDE_PLUGIN_ROOT}/references Task: """ ) ``` Resolve `${CLAUDE_PLUGIN_ROOT}` to an absolute path before passing it; the agent cannot expand it. Read the `STATUS:` line at the end of its reply: | STATUS | What to do next | |---|---| | `committed` | Continue to 3.4. The rest of the reply is `AGENT_SUMMARY`. | | `nothing-to-do` | Skip 3.4/3.5. Record `done` with `branch` and `date` and no `pr_url` (Phase 4). | | `checks-failing` | Do **not** push. Record `failed` with `error: "checks failing: "`. | | `blocked` | Do **not** push. Record `failed` with `error: ""`. | A missing `STATUS:` line counts as `blocked`. ### 3.4 Push the branch ```bash git -C "$LOCAL_PATH" push origin "$BRANCH" --force-with-lease ``` Only reached on `STATUS: committed`. If there are unexpectedly no commits to push, treat it like `nothing-to-do` (still update state so we don't retry it tomorrow). ### 3.5 Open the PR Call `mcp__github__create_pull_request` with: - `owner`: OWNER - `repo`: REPO_NAME - `title`: TASK_DESCRIPTION - `head`: BRANCH - `base`: DEFAULT_BRANCH - `body`: ``` ## Summary AGENT_SUMMARY --- *Opened by multi-repo-tasks via Claude Code.* ``` Capture the `html_url` field from the response as the PR URL. Extract the PR number from the URL (last path segment). After the PR is open, continue to Phase 4. --- ## Phase 4 — Update state and report The write path branches on the mode detected in Phase 1. Both branches must preserve every key the user (or an external sync tool) put on the task — never drop unknown fields. ### 4.1 Determine the outcome status Pick one of: - `done` — the implementer reported `committed` and a PR was opened, or it reported `nothing-to-do`. Always sets `branch`, `date`. Sets `pr_url` when a PR was opened. - `failed` — the implementer reported `checks-failing` or `blocked`, or a later step (push, PR creation) failed. Sets `error: ""`. Nothing unverified is pushed. - `skipped` — the task is no longer applicable (e.g. repo gone). Sets `reason: ""`. ### 4.2 Write back via tasks_io.py The same helper script does the writeback, picking the right strategy based on the mode detected in Phase 1: - **Unified mode** — the task entry's `status` flips in place and the completion keys (`branch`, `pr_url`, `date`, or `error` / `reason`) are merged onto it. Unknown keys on the entry are preserved. - **Legacy mode** — the task is removed from the tasks file (the project is dropped if its task list becomes empty) and a new entry with the same metadata is appended under `completed:` in the state file. ```bash python3 "${CLAUDE_PLUGIN_ROOT}/scripts/tasks_io.py" record "$TASKS_PATH" \ --mode "$MODE" \ --task-id "$TASK_ID" \ --repo "$REPO" \ --description "$TASK_DESCRIPTION" \ --status "$NEW_STATUS" \ ${STATE_PATH:+--state "$STATE_PATH"} \ --completion-json "$COMPLETION_JSON" ``` `COMPLETION_JSON` is a JSON object — for `status=done` use `{"branch": "...", "pr_url": "...", "date": "YYYY-MM-DD"}`; for `failed` use `{"error": ""}`; for `skipped` use `{"reason": ""}`. ### 4.3 Print the summary ``` ## Task — Done ✅ owner/repo-name Task: "Add unit tests for the authentication module" Branch: task/add-unit-tests-abc123 PR: https://github.com/owner/repo-name/pull/42 Progress: 1 of 5 tasks completed across 2 repos. Next up: "Fix the typo in README.md" (owner/repo-name) ``` - Show **progress** as completed/total across all projects. - Show **next up** so the user knows what the next run will do. - If this was the last task, congratulate the user — all done. ### 4.4 Result block End the report with one fenced `json` block labelled `result` on its own line before it, so callers (a human, another skill) can read the outcome without parsing prose. It is the last thing in the report: ```` result: ```json {"status": "done", "pr_url": "https://github.com/owner/repo-name/pull/42", "repo": "owner/repo-name", "branch": "task/add-unit-tests-abc123"} ``` ```` `status` is `done`, `failed` or `skipped` (as recorded in Phase 4.2), or `none` when Phase 1.3 found no pending task (nothing was selected or recorded; the block is just `{"status": "none"}`). `pr_url`, `repo` and `branch` are present only for `done`; `failed` adds `error`, `skipped` adds `reason`. --- ## Phase 5 — Remove all temporary files After all tasks are done, run the cleanup directly: ```bash rm -rf "$WORKDIR" ``` --- ## Rules - There are 2 mcps for GitHub. One is `github` and the other is `github2`. Use `github2` only to create and manage PRs into repos that are not owned by me. - In /references there are more context to read. These files relate to programming languages, package manager, etc. The implementer agent reads the ones that match the repo (it gets the directory as `REFERENCES_DIR`). - Never push a branch the implementer did not report as `committed`. A PR whose checks were never run locally just moves the failure to CI.