# Gating implementation

## Gate sites

Each gate runs where the model is about to see the extension.

| Extension | Seen when | Gate | On rejection |
| --- | --- | --- | --- |
| Skill | `/skill X` injects SKILL.md | `_skill_gate_error` in `interfaces/server/ws_actions/chat/turn_skill.py`, calling `gate()` | `skill_blocked`, "Skill blocked: reason"; body not expanded |
| Tool | `resolve_tools()` builds the turn's tools | `runtime/conversation/model_tools.py` | Filtered out |
| MCP | Same pipeline, names `<server>__<tool>` | `_apply_profile_gates`, inner to `resolve_tools`, on every return path | Filtered by server. A missing `required` raises `RequiredMcpUnavailable` |

No single earlier chokepoint: skills arrive as objects with a category, tools as bare names, MCP tools as namespaced names. Only the input shape differs.

## Shared helpers

`openprogram/extensions/gating.py`: pure functions, importable from any layer.

| Function | Returns |
| --- | --- |
| `match_any(name, patterns)` | Whether any `fnmatchcase` pattern matches; empty patterns give false |
| `gate(*, name, category="", disabled=(), allowed=(), categories=())` | `None` if it passes, otherwise a reason string |
| `check_required(installed, required)` | Required patterns nothing matches |

## Schema

`AgentSpec` in `runtime/agent_registry/manager.py`. Each block is a plain `dict`.

| Block | Default |
| --- | --- |
| `skills` | `{"disabled": [], "allowed": [], "categories": []}` |
| `tools` | `{"mode": "automatic"}` |
| `mcp` | `{"disabled": [], "allowed": [], "required": []}` |

## `resolve_tools`

A per-turn `override` beats `profile.tools`.

| Wanted | Result |
| --- | --- |
| `None` | `agent_tools(source=…, only_available=True)` |
| `[]`, `mode: none` | No tools |
| `mode: automatic`, or a dict with no `enabled`, `toolset`, `preset`, `allowed` | Full toolset minus `disabled` |
| `mode: selected` or `preset` | Those names minus `disabled`; adds `tool_search` when any is deferred |
| `enabled` list | Exactly those names; beats `allowed` |
| `toolset` | That set, filtered by both pattern lists |
| Other dict | `DEFAULT_TOOLS` filtered by both pattern lists |
| List of names | Exactly those, resident unless `tool_search` is present |

Profile MCP gates apply on every path, including per-turn lists. `web_search` is overlaid separately by its own boolean. `required` is checked after filtering, before provider execution.

## Compatibility

`skills`, `tools` and `mcp` must be objects. `AgentSpec.from_dict` passes them through `dict()`, so legacy lists such as `skills: ["pdf", "drawio"]` raise `ValueError` on load and are not migrated. A tools dict holding only empty lists counts as `automatic`.

## Testing

No dedicated unit tests: `match_any` is `fnmatchcase` plus iteration. `openprogram doctor` checks that skills load and MCP configs read, not gating. Add tests if semantics diverge from `fnmatchcase`, such as `**` support.
