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
4 changes: 3 additions & 1 deletion packages/opencode/src/altimate/tools/tool-lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export const ToolLookupTool = Tool.define("tool_lookup", {
// Upstream's Tool rewrite made `init()` an Effect (was a Promise) and `parameters`
// an Effect Schema (was a zod schema). Describe from the JSON Schema instead — it's
// populated for both native tools and our legacy zod tools (via tool-zod-compat).
const tool = await Effect.runPromise(info.init())
// The registry shapes task.background and its description by the background flag; describe
// the same contract the model is offered.
const tool = ToolRegistry.applyRuntimeToolSchemaFlags(info.id, await Effect.runPromise(info.init()))
const params = describeJsonSchema(tool.jsonSchema)
const lines = [info.id, ` ${tool.description}`, ""]
if (params.length) {
Expand Down
35 changes: 23 additions & 12 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ import { ToolJsonSchema } from "@/tool/json-schema"
import { Context, Effect, Layer } from "effect"
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
// altimate_change end
// altimate_change start — upstream_fix: SessionPrompt.Service passes its RuntimeFlags to the loop.
import { Option } from "effect"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: Duplicate effect import

Line 103 already imports { Context, Effect, Layer } from "effect". Merge Option into that import instead of adding a second effect import statement here.

import { RuntimeFlags } from "@/effect/runtime-flags"
import { withRuntimeFlags } from "@/tool/task"
// altimate_change end
// altimate_change start - import fingerprint for env-based skill selection
import { Fingerprint } from "../altimate/fingerprint"
// altimate_change end
Expand Down Expand Up @@ -5138,23 +5143,29 @@ NOTE: At any point in time through this workflow you should feel free to ask the

export class Service extends Context.Service<Service, Interface>()("@opencode/SessionPrompt") {}

export const layer = Layer.succeed(
// The imperative loop takes this service's RuntimeFlags for the background-subagent decision
// instead of re-reading the environment.
export const layer = Layer.effect(
Service,
Service.of({
prompt: (input) => Effect.promise(() => prompt(input)),
loop: (input) => Effect.promise(() => loop(input)),
cancel: (sessionID) => Effect.promise(() => cancel(sessionID)),
shell: (input) =>
Effect.tryPromise({
try: () => shell(input),
catch: (e) => (e instanceof Session.BusyError ? e : new Session.BusyError(input.sessionID)),
}),
command: (input) => Effect.promise(() => command(input)),
Effect.gen(function* () {
const flags = Option.getOrUndefined(yield* Effect.serviceOption(RuntimeFlags.Service))
const scoped = <A>(fn: () => Promise<A>) => withRuntimeFlags(flags, fn)
return Service.of({
prompt: (input) => Effect.promise(() => scoped(() => prompt(input))),
loop: (input) => Effect.promise(() => scoped(() => loop(input))),
cancel: (sessionID) => Effect.promise(() => cancel(sessionID)),
shell: (input) =>
Effect.tryPromise({
try: () => scoped(() => shell(input)),
catch: (e) => (e instanceof Session.BusyError ? e : new Session.BusyError(input.sessionID)),
}),
command: (input) => Effect.promise(() => scoped(() => command(input))),
})
}),
)

export const defaultLayer = layer

export const node = LayerNode.make(layer, [])
export const node = LayerNode.make(layer, () => [RuntimeFlags.node])
// altimate_change end
}
53 changes: 40 additions & 13 deletions packages/opencode/src/tool/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ import { GrepTool } from "./grep"
import { BatchTool } from "./batch"
// altimate_change end
import { ReadTool } from "./read"
import { TaskTool } from "./task"
// altimate_change start — upstream_fix: the task tool owns the background schema, description and env fallback.
import {
TaskTool,
backgroundSubagentsEnabled as backgroundSubagentsFromEnv,
backgroundTaskDescription,
taskBackgroundJsonSchema,
} from "./task"
// altimate_change end
import { TodoWriteTool } from "./todo"
import { WebFetchTool } from "./webfetch"
import { WriteTool } from "./write"
Expand Down Expand Up @@ -343,9 +350,7 @@ export namespace ToolRegistry {

function backgroundSubagentsEnabled(flags?: ToolRuntimeFlags) {
if (flags) return flags.experimentalBackgroundSubagents
const value = process.env["OPENCODE_EXPERIMENTAL_BACKGROUND_SUBAGENTS"]
if (value !== undefined) return ["1", "true", "yes"].includes(value.toLowerCase())
return Flag.OPENCODE_EXPERIMENTAL
return backgroundSubagentsFromEnv()
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
}

// altimate_change start — upstream_fix: allow Parallel-enabled websearch for non-opencode providers.
Expand All @@ -360,19 +365,40 @@ export namespace ToolRegistry {
}
// altimate_change end

function applyRuntimeToolSchemaFlags<T extends Tool.DefWithoutID>(id: string, tool: T, flags?: ToolRuntimeFlags): T {
if (id !== "task" || backgroundSubagentsEnabled(flags)) return tool
const schema = tool.jsonSchema
/** Shapes a tool definition by the runtime flags; tool_lookup describes `task` through it too. */
export function applyRuntimeToolSchemaFlags<T extends Tool.DefWithoutID>(
id: string,
tool: T,
flags?: ToolRuntimeFlags,
): T {
if (id !== "task") return tool
// Execution reads the same decision that shapes the schema, so the two gates cannot disagree.
const enabled = backgroundSubagentsEnabled(flags)
const execute = tool.execute
const gated: T = {
...tool,
execute: (args, ctx) => execute(args, { ...ctx, extra: { ...ctx.extra, backgroundSubagents: enabled } }),
...(enabled ? { description: backgroundTaskDescription(tool.description) } : {}),
}
const schema = gated.jsonSchema
if (!schema || typeof schema !== "object" || !schema.properties || typeof schema.properties !== "object") {
return tool
return gated
}

const properties = { ...(schema.properties as Record<string, unknown>) }
if (!("background" in properties)) return tool
// The task tool's default contract omits `background`; the experiment flag publishes it.
if (enabled) {
if ("background" in properties || !taskBackgroundJsonSchema) return gated
return {
...gated,
jsonSchema: { ...schema, properties: { ...properties, background: taskBackgroundJsonSchema } },
}
}
if (!("background" in properties)) return gated

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: The flag-off strip branch is unreachable and duplicates task.ts's schema handling

TaskTool's def already publishes jsonSchema: taskJsonSchema (task.ts:72-84, 304), which deletes background and filters it out of required; ToolJsonSchema.fromTool returns that schema unchanged, so properties cannot contain background here while the flag is off. The delete properties.background branch (and the required filter below) is therefore dead code, while the enabled branch re-adds the same property - so the property's shape is maintained in two files, and taskBackgroundJsonSchema (task.ts:88) re-runs zodToJsonSchema(parameters) that taskJsonSchema already computed. Having the registry only add background when enabled, and capturing the stripped property from the first conversion, keeps the contract in one place.

delete properties.background

return {
...tool,
...gated,
jsonSchema: {
...schema,
properties,
Expand Down Expand Up @@ -608,8 +634,9 @@ export namespace ToolRegistry {
.map(async (t) => {
using _ = log.time(t.id)
// altimate_change start — v1.17.9: Tool.Info.init() returns an Effect of DefWithoutID
// altimate_change start — upstream_fix: pass caller agent through deferred tool init.
const tool = await Effect.runPromise(t.init({ agent }))
// altimate_change start — upstream_fix: pass caller agent through deferred tool init; runtime
// flags shape the definition before plugins see it.
const tool = applyRuntimeToolSchemaFlags(t.id, await Effect.runPromise(t.init({ agent })), runtimeFlags)
Comment thread
RaghwendraSingh marked this conversation as resolved.
Resolved
// altimate_change end
// altimate_change end
const output = {
Expand All @@ -623,7 +650,7 @@ export namespace ToolRegistry {
registrySource: t.registrySource,
// altimate_change end
// altimate_change start — upstream_fix: hide disabled runtime-gated tool schema fields.
...applyRuntimeToolSchemaFlags(t.id, tool, runtimeFlags),
...tool,
// altimate_change end
description: output.description,
parameters: output.parameters,
Expand Down
Loading
Loading