# Task Agent Discovery and Selection This document describes how the task subsystem discovers agent definitions, merges multiple sources, and resolves a requested agent at execution time. It covers runtime behavior as implemented today, including precedence, invalid-definition handling, and spawn/depth constraints that can make an agent effectively unavailable. ## Implementation files - [`src/task/discovery.ts`](../packages/coding-agent/src/task/discovery.ts) - [`src/task/agents.ts`](../packages/coding-agent/src/task/agents.ts) - [`src/task/types.ts`](../packages/coding-agent/src/task/types.ts) - [`src/task/index.ts`](../packages/coding-agent/src/task/index.ts) - [`src/task/structured-subagent.ts`](../packages/coding-agent/src/task/structured-subagent.ts) - [`src/task/spawn-policy.ts`](../packages/coding-agent/src/task/spawn-policy.ts) - [`src/task/commands.ts`](../packages/coding-agent/src/task/commands.ts) - [`src/prompts/agents/task.md`](../packages/coding-agent/src/prompts/agents/task.md) - [`src/prompts/tools/task.md`](../packages/coding-agent/src/prompts/tools/task.md) - [`src/discovery/helpers.ts`](../packages/coding-agent/src/discovery/helpers.ts) - [`src/discovery/omp-extension-roots.ts`](../packages/coding-agent/src/discovery/omp-extension-roots.ts) - [`src/config.ts`](../packages/coding-agent/src/config.ts) - [`src/task/executor.ts`](../packages/coding-agent/src/task/executor.ts) --- ## Agent definition shape Task agents normalize into `AgentDefinition` (`src/task/types.ts`): - required `name`, `description`, and `systemPrompt` - optional `tools`, `spawns`, prioritized `model` list, `thinkingLevel`, `output`, `blocking`, `autoloadSkills`, `readSummarize`, `prewalk` - `source`: `"bundled" | "user" | "project"` (extension agents are tagged with their extension root's project/user level) - optional `filePath` Parsing comes from frontmatter via `parseAgentFields()` (`src/discovery/helpers.ts`): - missing `name` or `description` => invalid (`null`), caller treats as parse failure - `tools` accepts CSV or array; if provided, `yield` is auto-added - `spawns` accepts `*`, CSV, or array - backward-compat behavior: if `spawns` missing but `tools` includes `task`, `spawns` becomes `*` - `output` is passed through as opaque schema data - `read-summarize: false` (normalized to `readSummarize`) forces the subagent's `read` tool to return verbatim file content instead of structural summaries — `runSubprocess` applies it as a `read.summarize.enabled: false` override on the subagent's isolated settings (`src/task/executor.ts`). `scout` and `librarian` ship with it disabled. Defaults to enabled when the field is absent. - `model` accepts one selector, CSV, or an array. Entries are tried in order after role aliases are expanded. - `thinking-level` / `thinking` selects the agent's configured effort. When `task.enableEffort` (default `false`) exposes it, a task item's coarse `effort` (`lo`, `med`, `hi`) takes precedence at launch. OMP maps that hint to the selected model's lowest, middle, or highest supported effort, then clamps it to `task.maxEffort` (default `max`). The ceiling is carried across retry-fallback model switches. If the selected model has no supported effort at or below the ceiling, the spawn fails; models without a controllable effort surface instead fall back to their normal selector. - `blocking: true` makes the parent wait for that agent even when async task execution is enabled - `autoloadSkills` names skills from the parent session to inject before the first child prompt; unknown names are ignored - `prewalk: true` starts the subagent on its resolved model and hands off to the default prewalk target (the `smol` role) at its first edit/write, exactly like the session-level `--prewalk`; a string value (e.g. `prewalk: "@smol"` or `prewalk: "openai/gpt-5-mini"`) picks a custom target. The `task.agentPrewalk` settings record (agent name → `"on"` / `"off"` / pattern, toggled per agent from `/agents` with `P`) overrides the frontmatter. Resolution happens in `runSubprocess` (`src/task/executor.ts`). An unavailable target is skipped instead of failing the spawn. A resolved target is skipped only when both its model identity and its effective thinking mode/level match the starting selection after model clamping; a same-model effort downgrade is a real hand-off and still arms and switches at the first edit/write. ## Role-backed custom agents OMP discovers user agents from `~/.omp/agent/agents/*.md` and project agents from `.omp/agents/*.md`. Give the agent a role alias in frontmatter, then dispatch it by name. For model routing, task dispatch sets only `agent`; it does not set a worker model: `~/.omp/agent/agents/reviewer.md`: ```md --- name: reviewer description: Review a change for correctness. model: "@review" --- Review the assigned change and report concrete findings. ``` Set the role mapping in `~/.omp/agent/config.yml`: ```yaml modelRoles: review: openai/gpt-5.4:high ``` `@review` resolves through `modelRoles.review`. Each `modelRoles.` value stores a concrete model selector and may append a thinking suffix such as `:high` (`src/config/model-resolver.ts`). Changing that mapping affects subsequent task resolutions without editing agent definitions. For a dispatch, set the agent name and task: ```json { "context": "Review the current change in this repository.", "tasks": [ { "agent": "reviewer", "task": "Report concrete correctness findings." } ] } ``` `/model`'s Roles view can assign and persist custom role mappings such as `review`, `fast`, and `good`. Changing only the active or default session selection does not remap those roles. ## Watch running agents After dispatch, press `Alt+A` to open [Agent Hub](./agent-hub.md). Its live roster shows each task agent's status, current activity, model, age, and usage. Select an agent to read its transcript and steer it directly; parked agents can be revived from the same view. ### `vibe_spawn` tier routing `vibe_spawn` maps `fast` to bundled `sonic` and `good` to bundled `task`. Both resolve through `task.agentModelOverrides` before their bundled agent model defaults (`src/vibe/runtime.ts`, `src/task/agents.ts`). Route these tiers through roles by keeping aliases in `task.agentModelOverrides` and concrete selectors only in `modelRoles`: ```yaml task: agentModelOverrides: sonic: "@fast_worker" task: "@good_worker" modelRoles: fast_worker: openai/gpt-5-mini good_worker: openai/gpt-5.4:high ``` The `vibe_spawn` `cli` remains `fast` or `good`; update `modelRoles` to change the worker model. ## Bundled agents Bundled agents are embedded at build time (`src/task/agents.ts`) using text imports. `EMBEDDED_AGENT_DEFS` defines: - `scout`, `designer`, `reviewer`, `security-reviewer`, and `librarian` from prompt files - `task` and `sonic` from the shared `task.md` body plus injected frontmatter; no bundled agent sets `prewalk` — the generic `task` agent's hand-off is armed by the `task.prewalk` setting (default off), or per agent via `/agents` / `task.agentPrewalk` / user agent frontmatter Loading path: 1. `loadBundledAgents()` parses embedded markdown with `parseAgent(..., "bundled", "fatal")` 2. results are cached in-memory (`bundledAgentsCache`) 3. `clearBundledAgentsCache()` is test-only cache reset Because bundled parsing uses `level: "fatal"`, malformed bundled frontmatter throws and can fail discovery entirely. ## Filesystem and plugin discovery `discoverAgents(cwd, home)` (`src/task/discovery.ts`) merges agents from OMP-native roots, OMP extension packages, and Claude marketplace plugin roots before appending bundled definitions. Direct cross-harness roots such as `.claude/agents`, `.codex/agents`, and `.gemini/agents` are intentionally skipped — their frontmatter schema is not the OMP task-agent contract (`TASK_AGENT_CONFIG_SOURCE = ".omp"` filters the native config-dir lists). ### Discovery inputs and precedence 1. Nearest project `.omp/agents` dir from `findAllNearestProjectConfigDirs("agents", cwd)` (first `.omp` hit only) 2. User `.omp/agents` dir from `getConfigDirs("agents", { project: false })` (first `.omp` hit only) 3. `/agents` for every enabled OMP extension package returned by `listOmpExtensionRoots(...)`, in this order: - CLI `--extension` roots - project `extensions:` settings - user `extensions:` settings - installed npm/link plugins 4. Claude marketplace plugin roots (`listClaudePluginRoots(home, cwd)`) with `agents/` subdirs — only when `isProviderEnabled("claude-plugins")`; project-scope plugins sort before user-scope 5. Bundled agents (`loadBundledAgents()`) The OMP extension-package surface is disabled when the `omp-plugins` capability provider is disabled. Marketplace roots are excluded from `listOmpExtensionRoots` and enter only through the separately gated Claude-plugin path. ## Merge and collision rules Discovery uses first-wins dedup by exact `agent.name`: - A `Set` tracks seen names. - Loaded agents are flattened in directory order and kept only if name unseen. - Bundled agents are filtered against the same set and only added if still unseen. Implications: - Project `.omp` overrides user `.omp`. - Earlier extension roots override later extension roots, Claude marketplace plugins, and bundled agents. - Non-bundled agents override bundled agents with the same name. - Name matching is case-sensitive (`Task` and `task` are distinct). - Within one directory, markdown files are read in lexicographic filename order before dedup. ## Invalid/missing agent file behavior Per directory (`loadAgentsFromDir`): - unreadable/missing directory: treated as empty (`readdir(...).catch(() => [])`) - file read or parse failure: warning logged, file skipped - parse path uses `parseAgent(..., level: "warn")` Frontmatter failure behavior comes from `parseFrontmatter`: - parse error at `warn` level logs warning - parser falls back to a simple `key: value` line parser - if required fields are still missing, `parseAgentFields` fails, then `AgentParsingError` is thrown and caught by caller (file skipped) Net effect: one bad custom agent file does not abort discovery of other files. ## Agent lookup and selection Lookup is exact-name linear search: - `getAgent(agents, name)` => `agents.find(a => a.name === name)` - unrestricted sessions default an omitted `agent` field to `task` - a restricted parent `spawns` list defaults an omitted `agent` field to the first listed agent `resolveEffectiveSubagentPolicy()` is shared by task and eval-backed subagent launches. Before allocating artifacts it: 1. resolves the omitted or explicit agent name from the parent spawn policy 2. enforces depth, blocked-self-recursion, and parent spawn-policy guards 3. rediscovers agents with `discoverAgents(session.cwd)` and performs exact lookup 4. checks `task.disabledAgents` 5. resolves plan-mode restrictions, output schema, model policy, and isolation policy A missing name fails preflight with `Unknown agent "...". Available: ...`; no subprocess runs. ### Description vs execution-time discovery `TaskTool.create()` memoizes discovery per resolved working directory when building the model-facing tool description. Execution rediscovers agents, so the runtime set can differ from the earlier description if agent or extension files changed mid-session. Blocking behavior is determined after policy resolution rather than from a stale description-time agent object. ## Model and structured-output precedence For task dispatch, model precedence is: 1. `task.agentModelOverrides[agentName]` 2. the agent frontmatter's prioritized `model` list 3. the parent's active model, then its configured/default model fallback Role aliases in either of the first two sources are expanded through `modelRoles`. The shared eval bridge can also supply an invocation-local model override ahead of the settings override; the task wire schema does not expose that field. Runtime output schema precedence is: 1. the task item's explicit `outputSchema` 2. agent frontmatter `output` 3. parent session `outputSchema` The task item's optional `schemaMode` overrides the parent session mode; the default is `permissive`. The model-facing prompt (`src/prompts/tools/task.md`) tags read-only agents and warns against offloading reasoning to `scout`/`sonic`. ## Command discovery interaction `src/task/commands.ts` is parallel infrastructure for workflow commands (not agent definitions), but it follows the same overall pattern: - discover from capability providers first - deduplicate by name with first-wins - append bundled commands if still unseen - exact-name lookup via `getCommand` In `src/task/index.ts`, command helpers are re-exported with agent discovery helpers. Agent discovery itself does not depend on command discovery at runtime. ## Availability constraints beyond discovery An agent can be discoverable but still unavailable to run because of execution guardrails. ### Disabled-agent settings `resolveEffectiveSubagentPolicy()` checks `task.disabledAgents` after resolving the agent. A disabled name fails preflight and lists enabled alternatives when available. ### Parent spawn policy The resolver checks `session.getSessionSpawns()`: - `"*"` (also `true`, `null`, or absent) => allow any; omitted `agent` defaults to `task` - `""` or `false` => deny all - CSV list => allow only listed names; omitted `agent` defaults to its first name If denied: `Cannot spawn '...'. Allowed: ...`. ### Blocked self-recursion env guard `PI_BLOCKED_AGENT` (or the internal request override) rejects an attempt to spawn the same blocked agent before discovery. ### Recursion-depth gating `task.maxRecursionDepth` defaults to `2`; a negative value disables the cap. The shared policy rejects a spawn when the current task depth has already reached the cap. When a child reaches the cap, `runSubprocess` also removes `task` from its tool list and sets its spawn policy empty. For a restricted agent tool list, `runSubprocess` auto-adds `task` when `spawns` is declared and depth permits it. It also retains the host's `hub` collaboration tool unless the session is explicitly restricting tool names. ## Plan mode behavior When parent plan mode is enabled, `resolveEffectiveSubagentPolicy()` builds an `effectiveAgent` before launching subprocesses: - prepends the plan-mode subagent system prompt - restricts tools to `read`, `grep`, `glob`, and `web_search`, plus `ast_grep` when the agent's own tool list declares it - clears child spawns - clears `prewalk` (read-only exploration must not receive the prewalk plan/implement nudges) Plan mode also rejects per-spawn isolation, apply, and merge controls. The same `effectiveAgent` is used for subprocess launch, model/thinking overrides, and output-schema selection.