Skip to content
Open
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
3 changes: 2 additions & 1 deletion packages/core/src/codemode/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ export const make = (inventory?: CodeModeCatalog.Inventory): Instructions.List =
// Rendered once, against the inventory that produced `current`, and stored with the update.
changed: (previous, current) =>
update(previous, current, inventory === undefined ? () => undefined : CodeModeCatalog.lookup(inventory)),
removed: () => "Code Mode tools are no longer available. Do not use any previously listed Code Mode tools.",
removed: () =>
"The `execute` tool is no longer available. Permitted tools are now available as direct tool calls.",
},
})
}
12 changes: 7 additions & 5 deletions packages/core/src/mcp/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ const update = Instructions.listUpdate<Summary>({
})

export interface Interface {
/** Lists server instructions reachable under the given ruleset; callers pass the merged agent and Session permissions. */
readonly load: (permissions: Permission.Ruleset) => Effect.Effect<Instructions.List>
/**
* Lists server instructions reachable under the given ruleset; callers pass the merged agent and Session permissions.
* `codeMode` says whether the Session's tool snapshot has Code Mode, which decides whether servers are reached through `execute`.
*/
readonly load: (permissions: Permission.Ruleset, codeMode: boolean) => Effect.Effect<Instructions.List>
}

export class Service extends Context.Service<Service, Interface>()("@opencode/McpInstructions") {}
Expand All @@ -55,7 +58,7 @@ export const layer = Layer.effect(
const mcp = yield* Mcp.Service

return Service.of({
load: Effect.fn("McpInstructions.load")(function* (permissions) {
load: Effect.fn("McpInstructions.load")(function* (permissions, codeMode) {
const source = (value: ReadonlyArray<Summary> | Instructions.Removed) =>
Instructions.make<ReadonlyArray<Summary>>({
key: Instructions.Key.make("core/mcp-guidance"),
Expand All @@ -70,13 +73,12 @@ export const layer = Layer.effect(
const [instructions, tools] = yield* Effect.all([mcp.instructions(), mcp.tools()], {
concurrency: "unbounded",
})
const canExecute = Permission.evaluate("execute", "*", permissions).effect !== "deny"
// Instructions are useful only when this Session can reach at least one server tool.
const visible = instructions
.flatMap((item) => {
const owned = tools.filter((tool) => tool.server === item.server)
// Without execute, Code Mode server tools are exposed directly.
const codemode = canExecute && owned[0]?.codemode !== false
const codemode = codeMode && owned[0]?.codemode !== false
if (
!owned.some(
(tool) =>
Expand Down
28 changes: 24 additions & 4 deletions packages/core/src/session/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { McpTool } from "../tool/mcp.js"
import { ReferenceInstructions } from "../reference/instructions.js"
import { SkillInstructions } from "../skill/instructions.js"
import { Tool } from "../tool.js"
import { modelTools } from "../tool/model-policy.js"
import { WebSearch } from "../websearch.js"
import { AgentNotFoundError } from "./error.js"
import { SessionHistory } from "./history.js"
import { InstructionEntry } from "./instruction-entry.js"
Expand Down Expand Up @@ -90,6 +92,7 @@ const layer = Layer.effect(
const skillInstructions = yield* SkillInstructions.Service
const store = yield* SessionStore.Service
const registry = yield* Tool.Service
const websearch = yield* WebSearch.Service

const resolveModel = (
session: SessionSchema.Info,
Expand Down Expand Up @@ -144,14 +147,30 @@ const layer = Layer.effect(
if (!agent.info) return yield* new AgentNotFoundError({ sessionID: session.id, agent: session.agent ?? agent.id })
// Session permissions narrow discovery the same way they narrow the tool snapshot.
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

Effect.all({
disabled: websearch.default().pipe(
Effect.as(false),
Effect.catchTag("WebSearch.Disabled", () => Effect.succeed(true)),
),
providers: websearch.providers(),
}).pipe(Effect.map((result) => !result.disabled && result.providers.length > 0)),
],
{ concurrency: "unbounded" },
)
const tools = yield* registry.snapshot(permissions, {
location,
tools: modelTools({ model: selected?.info, websearch: hasWebSearch }),
})
const loaded = yield* Effect.all(
{
tools: registry.snapshot(permissions, location),
builtins: builtins.load(),
discovery: discovery.load(),
skills: skillInstructions.load(permissions),
references: referenceInstructions.load(),
mcp: mcpInstructions.load(permissions),
mcp: mcpInstructions.load(permissions, tools.codeModeCatalog !== undefined),
entries: entries.load(sessionID),
},
{ concurrency: "unbounded" },
Expand All @@ -163,15 +182,15 @@ const layer = Layer.effect(
// prompt-cache prefix: user-level catalog and guidance, then project instructions, then
// the date and environment, which vary by day and directory.
instructions: Instructions.combine([
CodeModeInstructions.make(loaded.tools.codeModeCatalog),
CodeModeInstructions.make(tools.codeModeCatalog),
loaded.mcp,
loaded.references,
loaded.skills,
loaded.discovery,
loaded.builtins,
loaded.entries,
]),
tools: loaded.tools,
tools,
}
})

Expand Down Expand Up @@ -231,5 +250,6 @@ export const node = makeLocationNode({
SessionStore.node,
SkillInstructions.node,
Tool.node,
WebSearch.node,
],
})
9 changes: 9 additions & 0 deletions packages/core/src/session/runner/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,16 @@ export class ModelUnavailableError extends Schema.TaggedError<ModelUnavailableEr
}
export type Error = ModelNotSelectedError | ModelUnavailableError | ModelResolver.Error
export type Resolved = ModelResolver.Resolved
export type Selected = {
readonly info: Model.Info
readonly variant: Model.VariantID | undefined
}

export interface Interface {
readonly select: (
session: SessionSchema.Info,
available: () => Effect.Effect<ReadonlyArray<Model.Info>>,
) => Effect.Effect<Selected, ModelNotSelectedError | ModelUnavailableError>
/** Availability is sampled lazily for each explicitly selected model resolution. */
readonly resolve: (
session: SessionSchema.Info,
Expand Down Expand Up @@ -99,6 +107,7 @@ const layer = Layer.effect(
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.

resolve: Effect.fn("SessionRunnerModel.resolve")(function* (session, available, request) {
const selected = yield* select(session, available)
const ref = Model.Ref.make({
Expand Down
Loading
Loading