# Tool toggles and toolsets

The selected Agent decides which tools a branch may call. Each branch stores only intent, never an expanded list, so new tools remain available through the Agent template. Branches inherit intent once and then change independently.

## Who chooses

| Where | Sets | Stored in |
| --- | --- | --- |
| Agents → Capabilities → Programs | Scope: all, selected, named Access preset, or none. Plus disabled names and a `web_search` default. | Agent profile, `tools: {mode, allowed, preset, disabled, web_search}` |
| Composer Web Search switch | Layers `web_search` on or off over the Agent's set | Branch configuration, `web_search` |
| `/skill <name>` with `allowed-tools` | Narrows the turn to the skill's allowlist | Branch configuration, `tools_override` (`list[str]`) |

No composer Tools switch or per-chat preset. A chat without tools uses an Agent with Programs set to none. The chat action reads only `web_search` from a message, never `tools` or `tools_profile`. A message without `web_search` (the CLI, the TUI) leaves the stored intent as it is.

## Priority chain

`resolve_tools(agent_profile, override, source)` in `runtime/conversation/model_tools.py`; `tools_override_from_config(cfg)` in `session_config.py` produces the override.

| Session intent | Override | Result |
| --- | --- | --- |
| Nothing set | `None` | Agent profile's `tools` |
| `web_search` set | `{"inherit": True, "web_search": <bool>}` | Profile expanded live, then web_search added or removed, unless the Agent's `disabled` patterns exclude it |
| `/skill` allowlist | `list[str]` plus the web_search overlay | `agent_tools(names=[...])` |

Callers: Web chat (`ws_actions/chat/turn.py`, `routes/chat_branches.py`), channels (`channels/inbound/_conversation.py`), goal chat (`programs/tools/planning/goal/chat.py`).

## Available is not resident

| State | Provider tools array | Name in catalog | Callable |
| --- | --- | --- | --- |
| Resident | Full schema, every turn | No | Yes |
| Deferred, loaded | Full schema | No | Yes |
| Deferred, not loaded | No | Yes | After `tool_search` |
| Disabled or hidden | No | No | No |

The catalog names built-in deferred tools one by one and gives an MCP server or application one namespace line, within 4 KiB and then a count, so it stays small however many tools are deferred ([Deferred tools](https://fzkuji.github.io/OpenProgram/docs/reference/design/function/deferred-tools.html#catalog)).

| Group | Members |
| --- | --- |
| Resident | `DEFAULT_TOOLS` minus `DEFERRED_DEFAULT_TOOLS`, plus `read_conversation`. `DEFAULT_TOOLS` holds `tool_search`, which never defers: it is the only loader, so a set that names deferred tools carries it. |
| `DEFERRED_DEFAULT_TOOLS` | Memory tools, `scheduler`, `exit_plan_mode` (the plan-mode prompt names it), `send_message`, `skill`, `save_routine`. Available by default, large and rarely called. |
| Other exposed tools | Everything in `full` outside `DEFAULT_TOOLS` |
| Application entries | Deferred, also while their application is open in a window: opening it loads nothing |
| Explicit web search | A copy promoted to resident, so it ships without `tool_search`. The shared registry is untouched. |
| Tool list by name | Without `tool_search` in it, every tool ships resident |

**Mechanics in `programs/_host/runtime.py`**

| Part | Behaviour |
| --- | --- |
| `apply_default_deferral()` | Runs once at import in `programs/__init__.py` and gives every entry its default tier. Idempotent. Entries registered later get theirs at registration. |
| `split_tools_for_dispatch(tools, loaded)` | Partitions into `(provider_tools, catalog)` by tier and the request's loaded set |
| Loaded set | Not stored anywhere. Each request reads it from its own messages (`loaded_tools(messages)` in `shared/tool_loads.py`): the load records of its tool results (`details.loads`) and the names a compaction summary among them carries (`carried_tool_names`). A nested run reads its own messages, a branch its own history, an edited-and-resent turn the history before the edit. |
| Discovery | `tool_search` writes a load record on its result (`record_loads`), so the next provider request carries real schemas. A guide or an opened application names the tools and how to load them; it loads nothing. Schema as tool-result text alone is not enough for providers that accept only tools in the request array. |
| Execution | Uses the turn's resolved, permission-wrapped tools, not a new registry lookup. Without discovery the array is stable; discovery may change the cached prefix. |
| Checkpoints | A continuation needs no loaded names: the resumed request reads them from the messages it resumes with. The journal's old `loaded_deferred_tools` field is no longer written or read for loading. Tool-result text is never read as authorization or loading state. |
| Catalog | A conversation's own turn sends it as the `turn_context` section `deferred_catalog` beside the user message: in full with no baseline, then only what was added or removed. A call with its own system prompt (an Agent method, `runtime.exec`) keeps it inline there (`_build_deferred_catalog` in `runtime/context/prompt_assembly/components.py`). Empty when nothing is deferred. |
| `_estimate_one_tool` | In `runtime/context/token_accounting/budget.py`. Resident: description, parameters and a small wrapper. Deferred: bare name plus a newline. The two errors point in opposite directions, so `test_budget.py` checks each half against a real tokenized payload, within 15%. |

## Do not break

| Rule | Why |
| --- | --- |
| Expansion is deterministic | The tool array is the root of the prompt cache prefix (`models/providers/settings/cache_policy.py`; first breakpoint at the last tool, Anthropic and Bedrock explicit mode). Any wobble misses the whole cache silently. No `set()` iteration or dict churn in `agent_tools` and `_filter_agent_tools`. |
| Never store the Agent's tools on a session | The list would freeze: tools added to `DEFAULT_TOOLS` later would never arrive. `list[str]` means only a `/skill` allowlist. |
| Never filter history | Historical `tool_use` and `tool_result` render from messages regardless of `context.tools`. Breaking the pairing causes a provider 400. |
| Mid-turn array growth only through discovery | Anything else discards the cached prefix for the rest of the turn |

**Cost, cache and replay**

| Question | Answer |
| --- | --- |
| Do tools count as tokens? | `ContextCommit.total_tokens` excludes the array (`state/store/session/context_commit/types.py`); the provider bills it. Invisible to the compaction budget, visible in input cost. |
| Cache? | Any addition, removal or reorder misses the entire prompt cache. |
| Calls absent from the current array? | They replay fine; the model just cannot issue that call this turn. |
| Commit replay? | Unaffected: commits store no tool array. |

**How other projects do it**

| Project | Stores | Avoids staleness by |
| --- | --- | --- |
| opencode | Per-session `tools: Record<name, boolean>` | Builds the table fresh each turn, then filters by intent |
| claude-code | `--tools` (`""` none, `"default"` all), `--allowedTools`, `--disallowedTools` | Constant builtin set; intersection at run time |
| pi-ai | Builtin toggles and extension tools separately | Boolean intent; live extension loading unaffected |
| hermes | A preset name | Expands at run time (`TOOLSETS` is ported from it) |

All store intent; none freezes an expanded list.

**Carrying files, properties and tests**

| File | Carries |
| --- | --- |
| `runtime/conversation/session_settings/session_config.py` | `web_search` and `tools_override` on `SessionRunConfig`, save and load in git session meta (no schema change) |
| `interfaces/server/ws_actions/chat/turn.py` | `tools_flag` starts `None`; a `/skill` sets it; a new Agent conversation clears both. Nothing expands here. |
| `runtime/conversation/model_tools.py` | `_overlay_web_search` in the dict branch of `resolve_tools` (web_search is not in `DEFAULT_TOOLS`) |
| `programs/_host/runtime.py` | Deferred machinery above, `tool_search`, `deferred_catalog_text` |
| `runtime/agent/loop/` | Reads each request's loaded set from its messages (`streaming.py`); resolves calls by name against the full list, so a mid-turn load dispatches |

| Test | Locks |
| --- | --- |
| `tests/unit/programs/tools/test_tool_expansion_deterministic.py` | Same intent expands element-wise equal |
| `tests/unit/store/session/test_session_config_tools_intent.py` | No intent follows the Agent; web_search overlay added, removed, respecting `disabled`; `/skill` lists; cold-provider path |
| `tests/unit/store/session/test_session_config.py` | Round-trip: True reads True, a missing field reads None; enabling web_search leaves `tools_override` absent |
| `tests/unit/context/system_prompt/test_tool_defer.py` | Deferred tools out of the array and in the catalog and prompt; `tool_search` moves them and returns full schema; execution stays on permission-wrapped tools; checkpoint restore; never defers itself; idempotent |
| `tests/unit/context/statistics/test_budget.py` | Each half of tool pricing |

Also expected: new tools appear in automatic scope; two turns with unchanged intent show a cache hit; no client sends `tools` or `tools_profile`.

## Boundaries and extension

| Topic | State |
| --- | --- |
| Provider builtin web_search | Codex and OpenAI Responses use builtin `opts["web_search"]`; others are unconfirmed, so the tool-name path stays. Switch at `_overlay_web_search` once confirmed. |
| `allowed` without a mode | Filters `DEFAULT_TOOLS`; `selected` and presets expand by name over all tools |
| Exact array tokens | A server-side figure, not statically measurable here |
| New intent dimensions | Add scope to the Agent profile or a key to the dict intent. Never revert to a stored list. |
