Skip to content

feat(core): choose tools and Code Mode placement from the session's model - #64515

Open
jlongster wants to merge 2 commits into
devfrom
model-tool-policy
Open

jlongster wants to merge 2 commits into
devfrom
model-tool-policy

Conversation

@jlongster

@jlongster jlongster commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Chooses tools, and which of them go into Code Mode, from the session's model, in one hard-coded function. It's decided in SessionContext.select, in the same pass that filters tools by permissions, using the model the user selected (session model → agent model → default). The experimental.model.resolve hook still runs in load, so Copilot "auto" routing is unaffected.

The three rules (packages/core/src/tool/model-policy.ts, modelTools()):

  1. GPT models use patch; other models use edit + write. Same predicate as before; it moves here from patch.ts's context hook.
  2. websearch is hidden when web search is turned off or no provider is available (moved from websearch.ts's context hook).
  3. GPT and Anthropic models get read, write, edit, patch and shell inside Code Mode, with glob, grep and webfetch hidden (scripts use shell and fetch). Other models keep them as top-level tools. MCP, opencode.* and plugin tools stay in Code Mode as before.

How it works:

  • Tool.snapshot(permissions, { location, tools }) skips hidden tools and puts the listed tools into Code Mode in the same loop as the permission filter.
  • Tools register once, without checking full_codemode. shell and read carry a Code Mode variant (Tool.Info.codemode: description, input, pinned) used when they land in Code Mode; shell's 10-minute default applies only to Code Mode calls.
  • The Code Mode catalog cache keeps the 5 most recently used tool sets per location (was one slot), cleared on any registry change.
  • MCP instructions no longer mention execute when Code Mode is off; the "Code Mode no longer available" message says tools are now direct; the worktree hint names whichever tool exists.

⚠️ This applies to everyone, regardless of the full_codemode flag: GPT and Claude users get file tools and shell inside Code Mode and lose glob, grep and webfetch. Draft for evaluation.

Known open items: the explore agent denies execute, so on GPT/Claude it loses glob/grep/webfetch without gaining Code Mode; read's description still mentions grep.

📊 Proof

  • New tool-model-policy.test.ts (rules per model family, websearch, and SessionContext.select end to end), cache and Code Mode variant tests in tool-registry.test.ts.
  • 681 tests pass across the policy, tool, session and Code Mode suites; typecheck:tsc passes in schema, core, plugin, server, cli and sdk; check:generated passes.

Added by the opencode bot (AI-generated).

Fixes #57711

Related: #55087

Public API: adds optional codemode overrides to Tool.Info and an options object to Tool.snapshot; removes the patch/websearch session hooks (backwards compatible for plugins)

…odel

User decisions:
- GPT models use patch; other models use edit and write
- Hide websearch when it is turned off or no provider is available
- GPT and Anthropic models get file tools and shell in Code Mode, with glob, grep and webfetch hidden
- Other models keep file tools and shell as top-level tools
- Hard-code this in one function decided in the select phase, no layout or hook
- Apply regardless of the full_codemode flag
@jlongster
jlongster marked this pull request as ready for review November 17, 2026 21:09

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach holds up: one per-model policy applied in SessionContext.select, next to the permission filter, with registration no longer depending on full_codemode. I ran the #57711 repro (simulated model, execute denied) on the base and with this PR. The worktree hint now names opencode_session_move instead of execute. Related core tests pass (521, 23 skipped) and core typecheck:tsc passes.

Public API (additive, backwards compatible): Tool.Info gains an optional codemode field ({ description?, input?, pinned? }) in @opencode/schema/tool. Tool.snapshot(permissions, location) becomes Tool.snapshot(permissions, { location, tools }). McpInstructions.load takes an optional { codemode }. The built-in patch and websearch context/compaction/generate hooks are removed, so plugins reading event.tools now see the list after these rules. No HTTP, config or CLI change. As the description says, GPT and Claude users get the new tool set regardless of full_codemode.

Concerns (inline):

  • Denying execute drops search tools. glob, grep and webfetch are hidden even when the session can't use Code Mode. In the repro, that request had no glob, grep or webfetch. This affects explore and any user rule that denies execute, not just explore.
  • Copilot "auto" sessions lose patch. Tools are now chosen from the selected auto model rather than the routed model, so they always get edit/write and no Code Mode, even when routed to GPT.

Fixes #57711
Related: #55087

}
if (!usePatch) hidden.add("patch")

if (isStrongModel(input.model)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

glob, grep and webfetch are hidden for GPT and Claude models even when the session can't use Code Mode. In snapshot, codeModeEnabled is false when execute is wholly denied, so these tools are simply gone. I saw this with the #57711 setup (execute denied): the request had shell, read and patch but no glob, grep or webfetch. This affects the built-in explore agent (it denies * and allows grep, glob and webfetch) and any user config that turns Code Mode off.

Fix: only hide these tools, and only promote file tools into Code Mode, when execute is actually reachable. For example, return the 'replaced by Code Mode' set separately and apply it in snapshot after codeModeEnabled is known.

const permissions = Permission.merge(agent.info.permissions, session.permissions ?? [])
const [selected, hasWebSearch] = yield* Effect.all(
[
models.select(session, model.available).pipe(Effect.orElseSucceed(() => undefined)),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This picks tools from the model before experimental.model.resolve runs. The removed patch hook saw event.model, the resolved model. For Copilot "auto", the model here is auto, which is neither GPT nor Claude. Those sessions now always get edit/write and no Code Mode, even when the request is routed to a GPT model that previously got patch. If that's intended, it's worth noting. Otherwise the Copilot route could be resolved first, or auto handled explicitly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I know

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. Since the Copilot auto → edit/write, no Code Mode behaviour is intended, it would help to add it to the PR description's "Known open items" next to the explore note. That way reviewers won't flag it again. I haven't changed anything in the checkout.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fu

return yield* new ModelNotSelectedError({ sessionID: session.id })
})
return Service.of({
select,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opencode bot why did search need to be exported here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm reading "search" as select, since that's what line 110 adds to the service. SessionContext.select has to know the session's model to pick tools, and it needs that before load resolves the model.

  • Where it's used: packages/core/src/session/context.ts:152 calls models.select(session, model.available) and passes selected?.info into modelTools(). Those tools go into registry.snapshot(...) in the same pass as the permission filter.
  • Why not resolve: resolve (runner/model.ts:111) calls select, then runs the experimental.model.resolve plugin hook and does full provider resolution. Calling it here would run the hook twice per turn (here and in load). The tool set would then follow whatever model the hook routes to, such as Copilot "auto", instead of the model the user picked. It would also do the full provider resolution here, which this step doesn't need. select only applies the fallback order (session model → agent model → default) and returns { info, variant }, which is all modelTools() needs.
  • Failures: errors from select become undefined (orElseSucceed), so a missing or unavailable model gets the default tools instead of failing select. The real error still comes from load.

The alternative is to keep select internal and add a separate function that returns only the model info. That would expose the same logic, so exposing select seemed simpler.

One trade-off: when a hook sends the session to a different model family, tools still follow the model the user picked. That's what the PR description says ("hook still runs in load, so Copilot 'auto' routing is unaffected").

I only read the code and didn't run anything. I made no changes; the only local change in the checkout is a bun.lock modification I didn't make, and I left it alone.

Comment thread packages/core/src/mcp/instructions.ts Outdated
})
const canExecute = Permission.evaluate("execute", "*", permissions).effect !== "deny"
const canExecute =
options?.codemode !== false && Permission.evaluate("execute", "*", permissions).effect !== "deny"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tell me what passes in options here opencode bot

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one place passes it: SessionContext.select in packages/core/src/session/context.ts:173.

const tools = yield* registry.snapshot(permissions, { location, tools: modelTools({ model: selected?.info, websearch: hasWebSearch }) })
...
mcp: mcpInstructions.load(permissions, { codemode: tools.codeModeCatalog !== undefined }),

codemode is therefore true whenever this session's tool snapshot actually built a Code Mode execute tool, and false otherwise. The only other caller is the test at packages/core/test/mcp-instructions.test.ts:71, which passes { codemode: false }.

When it's false: in Tool.snapshot (packages/core/src/tool.ts:304-351), codeModeCatalog is undefined only when whollyDisabled("execute", rules) is true. The model policy (modelTools) never switches Code Mode off. It only hides tools or moves them into Code Mode. So today the option comes down to "the snapshot decided execute is denied".

Why it isn't redundant with the Permission.evaluate check on line 77: the two checks can give different answers.

  • whollyDisabled (tool.ts:444) looks at the last rule matching execute whatever its resource, and counts as denied only if that rule is deny *.
  • Permission.evaluate("execute", "*") also matches on resource. Take rules [deny execute *, allow execute foo]: the snapshot keeps Code Mode on, but evaluate most likely finds the deny and returns false. I traced this through the code but didn't run it.

With the option, the instructions follow whatever the snapshot actually exposed. If you're fine with canExecute coming only from the snapshot, line 77 could be just options?.codemode !== false, or the option could be made required and the evaluate call dropped.

I only read the code: I didn't change anything or run tests.

…ode decision

User decisions:
- Decide in one place whether MCP servers are reached through execute

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commit only changes how the MCP instructions learn whether Code Mode is on: McpInstructions.load now takes the tool snapshot's decision as a boolean. Behaviour is unchanged. I checked it with the MCP instructions and model policy tests (all pass) and core typecheck:tsc (passes). The McpInstructions.load signature is internal to core, so this commit adds no public API change beyond the earlier review.

The two earlier points are not addressed yet: glob, grep and webfetch are still hidden when execute is denied, and Copilot "auto" still picks tools from the auto model id.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opencode.tools adds the execute / session_move hint even when Code Mode is denied

1 participant