Repository navigation
Conversation
…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
0dc3dda to
3da7442
Compare
There was a problem hiding this comment.
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
executedrops 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 affectsexploreand any user rule that deniesexecute, not justexplore. - Copilot "auto" sessions lose
patch. Tools are now chosen from the selectedautomodel rather than the routed model, so they always get edit/write and no Code Mode, even when routed to GPT.
| } | ||
| if (!usePatch) hidden.add("patch") | ||
|
|
||
| if (isStrongModel(input.model)) { |
There was a problem hiding this comment.
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)), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| return yield* new ModelNotSelectedError({ sessionID: session.id }) | ||
| }) | ||
| return Service.of({ | ||
| select, |
There was a problem hiding this comment.
opencode bot why did search need to be exported here?
There was a problem hiding this comment.
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:152callsmodels.select(session, model.available)and passesselected?.infointomodelTools(). Those tools go intoregistry.snapshot(...)in the same pass as the permission filter. - Why not
resolve:resolve(runner/model.ts:111) callsselect, then runs theexperimental.model.resolveplugin hook and does full provider resolution. Calling it here would run the hook twice per turn (here and inload). 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.selectonly applies the fallback order (session model → agent model → default) and returns{ info, variant }, which is allmodelTools()needs. - Failures: errors from
selectbecomeundefined(orElseSucceed), so a missing or unavailable model gets the default tools instead of failingselect. The real error still comes fromload.
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.
| }) | ||
| const canExecute = Permission.evaluate("execute", "*", permissions).effect !== "deny" | ||
| const canExecute = | ||
| options?.codemode !== false && Permission.evaluate("execute", "*", permissions).effect !== "deny" |
There was a problem hiding this comment.
tell me what passes in options here opencode bot
There was a problem hiding this comment.
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 matchingexecutewhatever its resource, and counts as denied only if that rule isdeny *.Permission.evaluate("execute", "*")also matches on resource. Take rules[deny execute *, allow execute foo]: the snapshot keeps Code Mode on, butevaluatemost likely finds thedenyand 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
There was a problem hiding this comment.
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.
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). Theexperimental.model.resolvehook still runs inload, so Copilot "auto" routing is unaffected.The three rules (
packages/core/src/tool/model-policy.ts,modelTools()):patch; other models useedit+write. Same predicate as before; it moves here frompatch.ts'scontexthook.websearchis hidden when web search is turned off or no provider is available (moved fromwebsearch.ts'scontexthook).read,write,edit,patchandshellinside Code Mode, withglob,grepandwebfetchhidden (scripts useshellandfetch). 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.full_codemode.shellandreadcarry 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.executewhen Code Mode is off; the "Code Mode no longer available" message says tools are now direct; the worktree hint names whichever tool exists.full_codemodeflag: GPT and Claude users get file tools and shell inside Code Mode and loseglob,grepandwebfetch. Draft for evaluation.Known open items: the
exploreagent deniesexecute, so on GPT/Claude it losesglob/grep/webfetchwithout gaining Code Mode;read's description still mentions grep.📊 Proof
tool-model-policy.test.ts(rules per model family, websearch, andSessionContext.selectend to end), cache and Code Mode variant tests intool-registry.test.ts.typecheck:tscpasses in schema, core, plugin, server, cli and sdk;check:generatedpasses.Added by the opencode bot (AI-generated).
Fixes #57711
Related: #55087
Public API: adds optional
codemodeoverrides toTool.Infoand an options object toTool.snapshot; removes the patch/websearch session hooks (backwards compatible for plugins)