Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/core/src/session/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ const layer = Layer.effect(
const policyModel = selected?.info ?? (fallbackRef && Model.Info.default(fallbackRef.providerID, fallbackRef.id))
const tools = yield* registry.snapshot(permissions, {
services,
tools: modelTools({ model: policyModel, websearch: hasWebSearch, fullCodeMode }),
// An agent that can't run `execute` (such as explore) keeps every tool direct, so give it the flag-off policy.
tools: modelTools({
model: policyModel,
websearch: hasWebSearch,
fullCodeMode: fullCodeMode && Tool.codeModeAvailable(permissions),
}),
})
const loaded = yield* Effect.all(
{
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ const layer = Layer.effect(
Effect.gen(function* () {
const rules = permissions ?? []
// Without Code Mode its tools would be unreachable, so expose them as direct tools instead.
const codeModeEnabled = !whollyDisabled("execute", rules)
const codeModeEnabled = codeModeAvailable(rules)
const full = codeModeEnabled && options?.services !== undefined && (yield* flags.enabled("full_codemode"))
const data = state.get()
if (catalogData !== data) {
Expand Down Expand Up @@ -474,6 +474,9 @@ const whollyDisabled = (action: string, rules: Permission.Ruleset) => {
return rule?.resource === "*" && rule.effect === "deny"
}

/** Whether the rules leave Code Mode's `execute` reachable, as `snapshot` decides it. */
export const codeModeAvailable = (rules: Permission.Ruleset) => !whollyDisabled("execute", rules)

const CATALOG_CACHE_LIMIT = 5

const formatSchemaIssue = SchemaIssue.makeFormatterDefault()
Expand Down
55 changes: 28 additions & 27 deletions packages/core/src/tool/model-policy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Model } from "@opencode/schema/model"

const codeModeBuiltIns = ["read", "write", "edit", "patch", "shell", "websearch"] as const
const fullCodeModeHidden = ["glob", "grep", "webfetch"] as const

export interface ModelTools {
readonly hidden: Set<string>
Expand All @@ -13,40 +14,40 @@ export function modelTools(input: {
readonly fullCodeMode: boolean
}): ModelTools {
const hidden = new Set<string>()
const direct = new Set<string>()
if (!input.websearch) hidden.add("websearch")
if (!input.model) {
if (input.fullCodeMode) {
for (const name of codeModeBuiltIns) {
if (!hidden.has(name)) direct.add(name)
}
// Code Mode programs search and fetch through shell and their `fetch` global, so these tools only add noise.
if (input.fullCodeMode) for (const name of fullCodeModeHidden) hidden.add(name)

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 widens the known open item from #64515. The explore agent blocks execute, so with the flag on, explore sessions for every model now lose glob, grep and webfetch without getting Code Mode in return. They can still search through shell, but they have no fetch tool at all. Before, only GPT and Claude were affected. Is that intended, or should these stay visible when execute isn't available to the agent?

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.

Intended for agents that can use Code Mode only. Fixed in 5040657: with the flag on, an agent that can't run execute (like explore) gets the flag-off policy, so glob, grep and webfetch stay top-level tools. session/context.ts now passes fullCodeMode && Tool.codeModeAvailable(permissions) to modelTools, and Tool.snapshot uses the same helper. Added cases for GPT and DeepSeek with execute denied.

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 checked 5040657 and the fix works. modelTools has one caller, session/context.ts:170, and it now gets fullCodeMode && Tool.codeModeAvailable(permissions). The snapshot's codeModeEnabled check in tool.ts uses the same helper. Both places use the same execute rule: the last rule matching execute is resource: "*" + deny. So an agent without execute can no longer lose glob, grep and webfetch while also being left without Code Mode. The new GPT and DeepSeek cases with execute denied cover this.

What I ran, in a temporary worktree at pr/head that I've since removed:

  • bun test test/tool-model-policy.test.ts test/tool-registry.test.ts test/tool-search.test.ts test/codemode.test.ts: 78 pass.
  • tsc --noEmit in packages/core: clean.

I didn't trace how the real explore agent's permission set denies execute. Any rule that ends in a *-resource deny for execute, including a catch-all * deny, hits this path. An agent that denies execute only for some resources still counts as having Code Mode, so the tools stay hidden. That matches how the snapshot already behaves.

The shared repo/ checkout is still at 1ab2c0841, without this commit. I didn't change it.


// GPT-5+ edits with patch; every other model edits with edit and write.
if (input.model) {
if (editMode(input.model) === "patch") {
hidden.add("edit")
hidden.add("write")
} else {
hidden.add("patch")
}
return { hidden, direct }
}

const id = input.model.id.toLowerCase()
const modelID = input.model.modelID.toLowerCase()
const gpt = gptMode(id, modelID)
if (gpt === "patch") {
hidden.add("edit")
hidden.add("write")
} else {
hidden.add("patch")
// With full_codemode, strong models (GPT and Claude) use the built-ins from inside Code Mode;
// every other model, or an unresolved one, keeps them as direct tools.
const direct = new Set<string>()
if (input.fullCodeMode && !strongModel(input.model)) {
for (const name of codeModeBuiltIns) {
if (!hidden.has(name)) direct.add(name)
}
}
return { hidden, direct }
}

if (!input.fullCodeMode) return { hidden, direct }

if (gpt !== undefined || isAnthropic(input.model, id, modelID)) {
hidden.add("glob")
hidden.add("grep")
hidden.add("webfetch")
return { hidden, direct }
}
function editMode(model: Model.Info) {
return gptMode(model.id.toLowerCase(), model.modelID.toLowerCase())
}

for (const name of codeModeBuiltIns) {
if (!hidden.has(name)) direct.add(name)
}
return { hidden, direct }
function strongModel(model: Model.Info | undefined) {
if (!model) return false
const id = model.id.toLowerCase()
const modelID = model.modelID.toLowerCase()
return gptMode(id, modelID) !== undefined || isAnthropic(model, id, modelID)
}

function gptMode(id: string, modelID: string): "patch" | "edit" | undefined {
Expand Down
28 changes: 24 additions & 4 deletions packages/core/test/tool-model-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ describe("modelTools", () => {
})
})

test("hides glob, grep, and webfetch for every model when fullCodeMode is on", () => {
for (const model of [
makeModel("openai", "gpt-5"),
makeModel("anthropic", "claude-sonnet-4-5"),
makeModel("deepseek", "deepseek-reasoner"),
makeModel("zhipu", "glm-5.3"),
makeModel("google", "gemini-4-neon"),
undefined,
]) {
const { hidden } = policy(model)
for (const name of ["glob", "grep", "webfetch"]) expect(hidden).toContain(name)
}
})

test("needs no demotion when fullCodeMode is off while still applying patch and websearch rules", () => {
expect(policy(makeModel("openai", "gpt-5"), true, false)).toEqual({ direct: [], hidden: ["edit", "write"] })
expect(policy(makeModel("claude-agent", "opus"), false, false)).toEqual({
Expand All @@ -94,15 +108,15 @@ describe("modelTools", () => {
test("keeps non-strong and absent models direct when fullCodeMode is on, hiding websearch when unavailable", () => {
expect(policy(makeModel("deepseek", "deepseek-reasoner"))).toEqual({
direct: ["edit", "read", "shell", "websearch", "write"],
hidden: ["patch"],
hidden: ["glob", "grep", "patch", "webfetch"],
})
expect(policy(undefined, true, true)).toEqual({
direct: ["edit", "patch", "read", "shell", "websearch", "write"],
hidden: [],
hidden: ["glob", "grep", "webfetch"],
})
expect(policy(undefined, false, true)).toEqual({
direct: ["edit", "patch", "read", "shell", "write"],
hidden: ["websearch"],
hidden: ["glob", "grep", "webfetch", "websearch"],
})
})
})
Expand Down Expand Up @@ -267,13 +281,19 @@ describe("SessionContext.select model tool policy", () => {
const directInstructions = (yield* readInitial(gptDirect.instructions)).text
expect(directInstructions).toContain("Alpha docs")
expect(directInstructions).not.toContain("through `execute`")
// Without `execute`, as for the explore agent, the search and fetch tools stay direct.
for (const name of ["glob", "grep", "webfetch"]) expect(names(gptDirect)).toContain(name)
const deepseekDirect = yield* select("deepseek", "deepseek-reasoner", [
{ action: "execute", resource: "*", effect: "deny" },
])
for (const name of ["glob", "grep", "webfetch"]) expect(names(deepseekDirect)).toContain(name)

const claude = yield* select("anthropic", "claude-sonnet-4-5")
expect(names(claude)).toEqual(["execute"])
expect(paths(claude).join(",")).toBe("alpha.search,edit,opencode.session_move,read,shell,websearch,write")

const deepseek = yield* select("deepseek", "deepseek-reasoner")
expect(names(deepseek).join(",")).toBe("edit,glob,grep,read,shell,webfetch,websearch,write,execute")
expect(names(deepseek).join(",")).toBe("edit,read,shell,websearch,write,execute")
expect(paths(deepseek)).toEqual(["alpha.search", "opencode.session_move"])

yield* websearch.select(false)
Expand Down
Loading