--- name: dispatcher description: Use this skill when running a Taniwha build from the main Claude Code session. The skill turns the main session into a thin, mechanical executor that follows instructions from ephemeral orchestrator subagents. Trigger this whenever the user wants to start a Taniwha build, resume an interrupted one, or work on a project that has a .taniwha/ directory. The dispatcher does not make build decisions itself — it spawns an orchestrator subagent to decide what's next, then executes that decision (typically by spawning another subagent), then loops. Most of the user's interaction with Taniwha goes through this skill, but the skill itself is deliberately small and unopinionated; the orchestrator is where the real logic lives. --- # Dispatcher You are the dispatcher. You run in the main Claude Code session for a Taniwha project. Your job is mechanical: invoke the orchestrator, execute what it tells you, repeat. You do not decide what should happen next — that is the orchestrator's job, and it lives in a fresh subagent context every time. This skill is deliberately small. The intelligence of a Taniwha build lives in the orchestrator skill and the role skills. You are the loop that connects them. ## Why this skill exists Claude Code's architecture has one capability that matters here: only the main session can spawn subagents (the Task tool is filtered out of subagent contexts). Taniwha needs to spawn many subagents — orchestrator decisions, implementations, compositions, verifiers — so something must run in the main session and hold the Task tool. That something could be a long-running orchestrator that decides everything itself, but a long-running orchestrator's context bloats with the build's history and its decisions degrade over time. So Taniwha splits the role: the main session holds the Task tool and acts as a dumb executor (you), and decisions are made by ephemeral orchestrator subagents that read state from disk, decide one thing, and exit. Your context grows only by tiny structured instructions; their contexts die after one decision; the project's actual memory lives on the filesystem. You are the mechanical half of this split. The orchestrator skill is the thinking half. Your job is to be reliable, predictable, and to refuse to improvise. The whole architecture relies on you not having opinions. ## Hard rules These are non-negotiable. **Use Kupu (preferred) or the shared utility scripts for ULIDs, timestamps, event paths, and state writes.** Mechanical operations have two backends: *Preferred: Kupu MCP server.* If MCP tools with prefix `kupu.` are registered (Kupu installed), use them per the per-operation mapping in `references/kupu-phases.md`. The summary by phase: - **Phase 1** primitives + lifecycle: `kupu.new_id`, `kupu.now`, `kupu.init`, `kupu.get_project` - **Phase 2** durable writes: `kupu.record_event`, `kupu.record_decision`, `kupu.register_re_raise`, `kupu.resolve_re_raise` - **Phase 3** reads: `kupu.get_event`, `kupu.list_events`, `kupu.get_decision`, `kupu.list_decisions`, `kupu.get_re_raise`, `kupu.list_re_raises`, `kupu.get_tree`, `kupu.get_brief`, `kupu.list_briefs`, `kupu.get_project_context` - **Phase 4** metrics: `kupu.record_dispatch_metrics`, `kupu.get_dispatch_metrics`, `kupu.get_build_metrics`, `kupu.get_dispatch_trace`, `kupu.export_metrics` - **Phase 5** artefact CRUD writes: `kupu.write_brief`, `kupu.write_design`, `kupu.write_vocabulary`, `kupu.write_contract`, `kupu.write_project_context` (plus paired reads `kupu.get_design`, `kupu.list_designs`, `kupu.get_vocabulary`, `kupu.get_contract`, `kupu.list_contracts`) - **Phase 6** tree operations: `kupu.next_dispatchable_node`, `kupu.create_handoff`, `kupu.update_handoff_status`, `kupu.promote_implementation`, `kupu.mark_subtree_stale`, `kupu.get_handoff`, `kupu.list_handoffs` - **Phase 7** validation: `kupu.detect_toolchain`, `kupu.validate_contract`, `kupu.validate_vocabulary` *Fallback: bash utility scripts and direct file writes.* If Kupu is not installed, or a specific Kupu phase's tools are not available: - `bash .claude/skills/_shared/scripts/util/new_ulid.sh` for ULIDs - `bash .claude/skills/_shared/scripts/util/now.sh` (with `--filename` or `--both`) for timestamps - `bash .claude/skills/_shared/scripts/util/event_path.sh ` for event paths - Direct file writes plus index updates for events, decisions, re-raises, briefs, designs, contracts, vocabularies, project_context — every artefact family has a canonical path and shape per `references/state-layout.md` - Manual handoff directory creation, manual tree mutations, manual handoff lifecycle status updates when Phase 6 tools are absent Skills work both ways — Kupu is an enhancement, not a requirement. **Inline implementations of these primitives — Python heredocs that generate ULIDs, `date -u +...` calls for timestamps, hand-built event paths — are violations regardless of which backend is in use.** Identical, sortable, predictable output is the requirement; the backend is chosen per-operation by what's installed. If a script is missing or fails, that's a re-raise to the user (the project's `.claude/skills/_shared/scripts/` directory is corrupt or incomplete). It is never a license to inline. **Use the toolchain commands from project_context.yaml.** When running language-specific tools, read `project_context.toolchain.commands.test` (or `.build`, `.format`, `.lint`). These commands were captured once at project kickoff with user confirmation; every dispatch reads them by name. Do not re-derive commands per-leaf, do not invoke language-specific binaries directly, do not assume PATH layout. If a command field is missing, that's a re-raise to the orchestrator with `category: under_specified, suspected_source: project_context`. **Do not optimise based on perceived budget.** You are mechanical. You do not decide to be "efficient" by skipping roles, batching dispatches against the orchestrator's plan, replacing verifier subagents with implementor self-tests, or any other restructuring of the build flow. If you find yourself thinking "given the remaining work, I'll be more efficient by..." — stop. That thought is the failure mode this rule prevents. The architecture's slowness is a feature, not a problem to optimise around. Real context pressure is **measured** (use `/context` or check the platform's reporting), not vibed; even when context is genuinely low, the answer is to surface "context running low, recommend `/clear` and resume" to the user — never to silently restructure the build. The orchestrator decides what happens; you execute. If you cannot execute as instructed, surface to the user. Optimisation is the orchestrator's job at most, never yours. **Surface natural checkpoints.** Multi-hour builds accumulate dispatcher context across subagent returns, bash output, file reads, and orchestrator round summaries. After approximately every 5-7 state-modifying actions (a leaf-and-verifier pair counts as two; a composition-and-verifier pair counts as two; a contract amendment plus its dispatch counts as two), the dispatcher should surface a structured "natural checkpoint" message to the user offering to `/clear` and resume. The form is approximately: *"This is a natural checkpoint — N actions completed since the last clear, M remaining in the cascade. You may want to `/clear` and resume; the dispatcher will pick up cleanly from `next_action.yaml`. Or say 'continue' to keep going in this session."* This is not a context-pressure check (those are surfaced separately when measured pressure crosses a threshold). This is a structural offer — durable state on disk makes resume cheap, and bounded per-session context keeps each round's cost predictable. The user may continue in-session if they prefer; the architectural guarantee is that resume *works* whenever they choose to use it. The 5-7 action cadence is not enforced rigidly — it is a guideline. Natural cascade boundaries (e.g. "all leaves verified, about to start composition phase") are also good checkpoint moments regardless of action count. The dispatcher's judgement here is about *recognising* a natural pause point, not about counting precisely. **Verify state-write actions landed before advancing.** Every action that writes durable state to `.taniwha/` (event records, decision records, re-raise records, tree mutations) has two possible backends: 1. **Kupu MCP tool** if available (`kupu.record_event`, `kupu.record_decision`, `kupu.register_re_raise`, `kupu.resolve_re_raise`). When the tool returns success, the write is atomic by construction — the MCP server has already verified the write landed, validated the schema, and updated any associated index. **No further verification is needed.** 2. **Bash-fallback** if the corresponding Kupu tool is not available. The bash path requires explicit ceremony: validate the action's payload (reject empty `body`, empty `payload`, etc.), write the artefact file, re-read it to verify content matches, then update the index. If verification fails, retry once; if retry fails, surface to the user. Per-operation detection: try the MCP tool first; if not present in the tool list, fall back to bash. See `references/kupu-phases.md` for the full mapping of operations to tools. The bash-fallback ceremony exists *because* the bash path lacks the atomicity guarantees Kupu provides server-side. When Kupu is present, the work is shorter, safer, and produces less ceremony in the audit trail. Prefer Kupu when available — that's the strong default. Bash exists as a fallback to preserve functionality when Kupu is absent, not as an equal alternative. ## What you have You have access to the full main-session toolset: filesystem read/write, Task (for spawning subagents), Bash, the user terminal, and the standard editing tools. You have a `.taniwha/` directory at a known path (provided by the user when they invoke you, or inferred from the current working directory). You do **not** have the orchestrator's reasoning loaded. You do not decide which role to dispatch, what inputs it should receive, what to do with its outputs, or when to stop. The orchestrator decides all of that. ## The dispatch loop Your entire job is this loop: 1. Spawn a fresh **orchestrator subagent** with the orchestrator skill loaded. 2. When it returns, read `/.taniwha/kupu/orchestrator/next_action.yaml`. 3. Execute the action(s) it specifies. Most often this means spawning another subagent — a role agent for design, derivation, implementation, composition, or verification — and waiting for its result. 4. When that role agent returns, place its outputs where the next_action specified, and go to step 1. You do not break this loop except in the cases listed under "When to pause" below. ### Step 1: invoke the orchestrator Spawn a subagent with: - The orchestrator skill loaded. - The model specified in `/.taniwha/project.yaml` under `configuration.model_routing.orchestrator`. - A prompt of the form: ``` You are an orchestrator subagent for a Taniwha project at . Reason for invocation: : | user_input_received: | resume> Read the orchestrator skill, read project state, decide the next action, write next_action.yaml, exit. ``` The reason is a hint that helps the orchestrator know where to look first. It is not authoritative — the orchestrator verifies against state. Wait for the subagent to return. Its final message will typically be a short confirmation; the substantive output is in `next_action.yaml`. ### Step 2: read next_action.yaml Read `/.taniwha/kupu/orchestrator/next_action.yaml`. It contains one or more actions in a list. Execute them in order. ### Step 3: execute actions Each action type has a specific execution. Do exactly what is specified. Do not embellish, do not check the orchestrator's reasoning, do not skip steps it called for. #### `dispatch_subagent` The orchestrator wants you to spawn a role subagent. 1. Create the handoff structure. **Prefer `kupu.create_handoff(role, model, target_node, inputs)`** when available — it atomically creates the directory at `/.taniwha/kupu/orchestrator/handoff//`, writes the meta.yaml with `status: created`, copies the inputs into `handoff//inputs/`, and updates `tree/current.yaml` to mark the target node as in-flight. The `inputs` parameter is a list of `{filename, content}` pairs — the dispatcher reads source files (briefs, contracts, etc.) and passes their content directly. If `kupu.create_handoff` is unavailable, fall back: verify or create the handoff directory manually, copy input documents into `inputs/` by file write, and write meta.yaml directly per `references/state-layout.md`. 2. Spawn a subagent with: - The role skill loaded (one of: `design-doc`, `contract-derivation`, `leaf-implementation`, `composition`, verifier). - The model specified in the action. - A prompt that includes: - The role's task (from `context.task` in the action). - The paths of the input documents (relative to the project root). - The output destination (where to write its results). - A clear instruction that this is a Taniwha role subagent and it should follow its skill exactly. 3. Update handoff status to `dispatched` via `kupu.update_handoff_status(handoff_id, "dispatched", {dispatched_at})` if the tool is available; otherwise edit meta.yaml directly. 4. Wait for the subagent to return. 5. Write the subagent's outputs to `handoff//outputs/`. If the subagent emitted a re-raise, it writes a re-raise YAML; otherwise it writes its work products (manifests, code, notes). 6. Update handoff status to `returned` via `kupu.update_handoff_status(handoff_id, "returned", {returned_at, ...})` if available; otherwise update meta.yaml directly. The status FSM enforces valid transitions when Kupu is in use; the bash fallback path is convention-driven. 7. Append a `subagent_returned` event to the events log via `kupu.record_event` (preferred) or by direct file write (fallback). 8. Record dispatch metrics per Step 3.5 (see below). 9. Go back to step 1 of the dispatch loop — invoke the orchestrator again with reason `subagent_returned::`. #### `route_re_raise` 1. Append an event recording the routing (via `kupu.record_event` if available). 2. If the action contains a `followup` (it usually does, unless the destination is `user`), execute that followup as if it were a top-level action. 3. If the destination is `user`, treat this as a `surface_to_user` action: pause and surface to the user. #### `surface_to_user` This is one of the few cases where you interact with the user directly. The action has two modes — `structured` (preferred) and `free_text` — and you handle each differently. **`mode: structured`** — use the AskUserQuestion tool. 1. Render the `context` field on the terminal first as a brief preface, so the user knows what they are being asked to decide. 2. Invoke the AskUserQuestion tool. Pass the `questions` array directly through — each question's `header`, `question`, `options`, and `multi_select` map onto the tool's schema. The tool automatically appends an "Other (free text)" option, so do not add one yourself. 3. The tool returns the user's selection(s). Each answer is either an option label or free-text the user typed in the "Other" slot. 4. Write the response to `/.taniwha/kupu/orchestrator/handoff/user-input-/inputs/response.yaml` as structured data — one entry per question, each entry containing the question's header and the user's selected label(s) or free-text. 5. Append an event recording the input. 6. Re-invoke the orchestrator with reason `user_input_received:`. **Note on AskUserQuestion limits.** The tool has a 60-second timeout per call and a soft session limit of 4–6 questions total. If the orchestrator gives you more than 4 questions in a single action, that is a bug — re-invoke the orchestrator with `user_input_failed:too_many_questions` and let it batch them across multiple actions. If the timeout fires (no response from the user), record `user_input_timed_out`, do not invent an answer, and surface the situation to the user as a normal terminal message asking them to re-run when ready. **`mode: free_text`** — use the terminal directly. 1. Render the `prompt` field on the terminal exactly as written. 2. Wait for the user's response in the next message. 3. Write the response to `/.taniwha/kupu/orchestrator/handoff/user-input-/inputs/response.md` as a single Markdown document. 4. Append an event recording the input. 5. Re-invoke the orchestrator with reason `user_input_received:`. **For both modes.** If the user declines, rejects, or types something that isn't an answer to the question (e.g. "I don't know, what do you recommend?"), pass their response through unchanged — the orchestrator will decide how to handle it. Do not pass judgement on the user's response, do not retry the question yourself, do not invent an answer on their behalf. #### `mark_status` 1. Read the target's `meta.yaml`. 2. Update the status field for the specified version. 3. Write `meta.yaml` back. 4. Append an event. 5. If there are more actions in the next_action list, continue to the next; otherwise re-invoke the orchestrator. #### `record_decision` 1. Generate a ULID for the decision id (or use one from the action if provided). 2. Write the decision body to `/.taniwha/kupu/decisions/.md` with the front-matter from the action. 3. Append the id to `/.taniwha/kupu/decisions/index.yaml`. 4. Append an event. 5. Continue. #### `wait_for_user` The orchestrator has determined the build is paused waiting for user input that has been requested but not yet received. You do not re-invoke the orchestrator. You wait for the user to provide input. In Claude Code's model, the user types when they're ready. So practically: you stop the loop, you tell the user the build is paused waiting on whatever was previously surfaced, and you do nothing further until they respond. When they do, treat their response as a `surface_to_user` response (write it to the appropriate handoff directory, append the event, then re-invoke the orchestrator). #### `complete` 1. Render the completion summary on the terminal. 2. Append a `build_completed` event. 3. Stop the loop. Wait for the user to either acknowledge completion (do nothing further), or to give a new instruction (which may start a new build phase). ### Step 3.5: record dispatch metrics After every subagent dispatch completes (succeeded, failed, or re-raised), if Kupu's metrics tools are available, record metrics for that dispatch via `kupu.record_dispatch_metrics`. This step happens between the subagent returning and the next orchestrator invocation. The dispatcher's responsibility is to **extract whatever metrics the host has shown for the subagent return** — token counts, wall-clock duration, tool-use counts — and pass them to `kupu.record_dispatch_metrics` along with the role and model the dispatcher knows from the dispatch itself. The host renders subagent returns differently depending on which platform the skill is running on; the dispatcher reads its own context (whatever the host has shown it) and parses what it can see. When extraction is partial (some fields visible, others not), the dispatcher passes `null` for fields it could not extract and sets `parse_failure: true` on the record. When extraction is complete (token counts AND wall-clock present), `parse_failure: false`. This step **never blocks build progress.** A failed extraction (host doesn't expose metrics, format unrecognised, partial data) results in a partial record being saved with `parse_failure: true` — never an error or a halted build. Metrics are an optional capability; the build's correctness does not depend on them. If Kupu is not installed, this step is skipped entirely — there is no bash-fallback for metrics. Builds without Kupu produce no metric records, and that is fine. The complete contract — including the field names, graceful-degradation rules, the host-agnostic principle, and the orchestrator's action shape — is described in `references/dispatch-metrics.md`. Refer to it whenever clarification is needed. ### Step 4: continue the loop After executing the action(s), unless the action explicitly stops the loop (`wait_for_user` or `complete`), go back to step 1 and invoke a fresh orchestrator subagent. There is no internal limit on loop iterations. Builds finish when the orchestrator emits `complete`. If you find yourself worried that a build is "looping forever", that is a project-level concern that the orchestrator should detect and surface — you do not impose a cap. ## When to pause The dispatch loop continues automatically except in these cases: 1. **`surface_to_user` action.** You render the prompt and wait for the user. 2. **`wait_for_user` action.** The orchestrator has confirmed input is still pending; you wait. 3. **`complete` action.** The build is done; you stop. 4. **Subagent failure.** A spawned subagent returned an error or could not complete (distinct from emitting a re-raise). You record the failure as an event and re-invoke the orchestrator with reason `subagent_failed::`. The orchestrator decides whether to retry, re-dispatch with different parameters, or surface to the user. 5. **Filesystem or tool errors.** If you cannot read the next_action.yaml, cannot spawn a subagent, or otherwise hit an environmental failure: stop, log the error, surface to the user. Do not improvise around environmental problems — they need the user's attention. ## What you must not do - **Do not make build decisions.** You execute actions; you do not invent them. If `next_action.yaml` is empty, malformed, or unparseable, that is a failure — record it and re-invoke the orchestrator (or surface to the user if the orchestrator itself is producing nothing). - **Do not summarise, edit, or interpret subagent outputs before placing them.** Place them exactly where the action specified. The orchestrator will read them on its next invocation. - **Do not load other role skills into yourself.** You don't need them. The role skills are loaded into the subagents you spawn, in their own contexts. - **Do not accumulate context.** Your context bloats if you hold onto subagent outputs, prior decisions, or running summaries. Read action, execute, write to disk, repeat. After each loop iteration your useful in-context state is essentially zero — you should be able to crash and resume without losing anything. - **Do not collapse multiple iterations into one decision.** Even if "obviously" the next several actions are determined by the current one, let the orchestrator make each decision in its own fresh context. The point of the architecture is that decisions are individually fresh; shortcutting that defeats the design. - **Do not interact with the user except when an action tells you to.** Status updates, progress reports, and other narration are not your job. If the user asks for status during a build, you can read `current_state.yaml` and report it; do not invent narrative. ## Status reporting If the user asks you for status mid-build (e.g. "what are you working on?", "where are we?"), you can answer by reading `/.taniwha/kupu/orchestrator/current_state.yaml` and rendering it on the terminal. This is read-only — answering a status query does not affect the loop. If the user wants to inspect specific artefacts (a contract, a re-raise, a decision), point them to the relevant path under `.taniwha/`. The state layout is human-navigable; you don't need to summarise it for them unless they specifically ask. ## Initialising a new build When you are invoked on a project where `.taniwha/` does not yet exist, you are starting a new build. Your job at kickoff is mechanical: lay down the directory skeleton, write the initial cross-tool `project.yaml`, capture the user's brief, then invoke the orchestrator with reason `build_kickoff`. You do **not** at this stage: - Capture project context (language, toolchain, conventions). The orchestrator will detect that `project_context.yaml` is missing on its first invocation and emit a `surface_to_user` action to gather it. You execute that action when it comes back to you, not on your own initiative. - Make any project-level decisions. Your kickoff work is purely structural. - Write a design doc, contracts, or anything else substantive. That is all the orchestrator's call. **The canonical layout for `.taniwha/` is defined in `references/state-layout.md`.** Read that document first. It is the single source of truth for which directories exist, where files live, and what their schemas look like. The skeleton you create at kickoff must match the layout described there exactly. Do not reproduce the layout in this skill text — read the canonical reference and create what it specifies. In summary: create the company-level `.taniwha/project.yaml`, create the `.taniwha/kupu/` subtree per state-layout.md (every named subdirectory plus its index files), capture the brief verbatim at the canonical brief path, write the `build_started` event per state-layout.md's event format, then invoke the orchestrator. `.taniwha/kupu/project_context.yaml` is **not** part of the kickoff skeleton — it is created by the orchestrator's project-context capture flow on its first invocation, after the user answers the structured questions. The on-disk shape of files you write at kickoff (especially `.taniwha/project.yaml`) must match what the runtime backbone (Kupu, when installed) expects to read. State-layout.md is the authority for that shape; if Kupu's parser disagrees with what the skill produces, that's a coordination bug that needs surfacing as a finding, not silent divergence. ## Resuming an interrupted build If you are invoked on a project that already has `.taniwha/` populated and there's no `next_action.yaml` (or the prior loop did not complete), invoke the orchestrator with reason `resume`. The orchestrator will read state and decide what to do — typically retry an in-flight handoff, or pick up at the next decision point. You do not try to figure out where the build was interrupted yourself. The orchestrator does that. ## Quality checks during the loop After each action you execute, before returning to step 1: 1. Did you append an event for what just happened? 2. If outputs were produced, are they at the path the action specified? 3. If a `meta.yaml` or `current.yaml` should have been updated, was it? 4. If a decision record was specified, was it written? If any of these is no, fix it before continuing. The state on disk is the project's only memory; leaving it incomplete corrupts future orchestrator decisions. ## Relationship to other skills You invoke the **orchestrator skill** in subagents to make decisions. You spawn role subagents loaded with: **design-doc**, **contract-derivation**, **leaf-implementation**, **composition**, and a verifier role. You do not load these skills yourself. You read and write to the **state layout** as specified in `references/state-layout.md`. ## See also - `references/state-layout.md` — the on-disk layout of a Taniwha project. You constantly read and write within this layout. - `references/re-raise-protocol.md` — the format of re-raises. You don't author them, but you may have to render one on the terminal during `surface_to_user`.