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
12 changes: 12 additions & 0 deletions packages/core/src/flag/flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@ export const Flag = {
get OPENCODE_CLIENT() {
return process.env["OPENCODE_CLIENT"] ?? "cli"
},
/**
* When `OPENCODE_TOOL_CHOICE=required` (or `VERDICT_FORCE_TOOL_CHOICE=1`),
* non-final agent steps send OpenAI-compatible `tool_choice: "required"`
* so local models cannot answer with prose/JSON instead of tool calls.
* Final step still forces `none` (max-steps exit).
*/
get OPENCODE_TOOL_CHOICE() {
const raw = (process.env["OPENCODE_TOOL_CHOICE"] ?? "").toLowerCase()
if (raw === "required" || raw === "auto" || raw === "none") return raw
if (truthy("VERDICT_FORCE_TOOL_CHOICE")) return "required"
return undefined
},
}
13 changes: 12 additions & 1 deletion packages/core/src/session/runner/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { Cause, DateTime, Effect, FiberSet, Layer, Option, Semaphore, Stream } from "effect"
import { AgentV2 } from "../../agent"
import { Config } from "../../config"
import { Flag } from "../../flag/flag"
import { Database } from "../../database/database"
import { EventV2 } from "../../event"
import { Location } from "../../location"
Expand Down Expand Up @@ -205,7 +206,17 @@ const layer = Layer.effect(
.map(SystemPart.make),
messages: [...toLLMMessages(context, model), ...(isLastStep ? [Message.assistant(MAX_STEPS_PROMPT)] : [])],
tools: toolMaterialization?.definitions ?? [],
toolChoice: isLastStep ? "none" : undefined,
// Default: auto (undefined). OPENCODE_TOOL_CHOICE=required forces tool
// calls on non-final steps so weak local models cannot print JSON prose.
toolChoice: isLastStep
? "none"
: Flag.OPENCODE_TOOL_CHOICE === "required"
? "required"
Comment on lines +213 to +214

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve a final turn when forcing tool choice

When OPENCODE_TOOL_CHOICE=required or VERDICT_FORCE_TOOL_CHOICE=1 is set for an agent without a configured steps limit (the built-in build agent leaves steps unset), isLastStep never becomes true, so every provider turn is sent with toolChoice: "required". Any local tool call then sets needsContinuation and the runner starts another turn that again cannot produce a final text answer, causing default sessions to keep issuing tools until an error/context limit instead of settling. Only force required when there is a finite final step or otherwise disable it after tool settlement.

Useful? React with 👍 / 👎.

: Flag.OPENCODE_TOOL_CHOICE === "none"
? "none"
: Flag.OPENCODE_TOOL_CHOICE === "auto"
? "auto"
: undefined,
})
if (yield* compaction.compactIfNeeded({ sessionID: session.id, entries, model, request }))
return yield* Effect.die(continueAfterCompaction(currentStep))
Expand Down
36 changes: 36 additions & 0 deletions packages/core/test/tool-choice-flag.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, test } from "bun:test"

describe("OPENCODE_TOOL_CHOICE / VERDICT_FORCE_TOOL_CHOICE", () => {
test("reads required from OPENCODE_TOOL_CHOICE", async () => {
const prev = process.env["OPENCODE_TOOL_CHOICE"]
const prevForce = process.env["VERDICT_FORCE_TOOL_CHOICE"]
try {
delete process.env["VERDICT_FORCE_TOOL_CHOICE"]
process.env["OPENCODE_TOOL_CHOICE"] = "required"
// Re-import would cache Flag getters — getters read process.env at access time.
const { Flag } = await import("../src/flag/flag")
expect(Flag.OPENCODE_TOOL_CHOICE).toBe("required")
} finally {
if (prev === undefined) delete process.env["OPENCODE_TOOL_CHOICE"]
else process.env["OPENCODE_TOOL_CHOICE"] = prev
if (prevForce === undefined) delete process.env["VERDICT_FORCE_TOOL_CHOICE"]
else process.env["VERDICT_FORCE_TOOL_CHOICE"] = prevForce
}
})

test("VERDICT_FORCE_TOOL_CHOICE=1 maps to required", async () => {
const prev = process.env["OPENCODE_TOOL_CHOICE"]
const prevForce = process.env["VERDICT_FORCE_TOOL_CHOICE"]
try {
delete process.env["OPENCODE_TOOL_CHOICE"]
process.env["VERDICT_FORCE_TOOL_CHOICE"] = "1"
const { Flag } = await import("../src/flag/flag")
expect(Flag.OPENCODE_TOOL_CHOICE).toBe("required")
} finally {
if (prev === undefined) delete process.env["OPENCODE_TOOL_CHOICE"]
else process.env["OPENCODE_TOOL_CHOICE"] = prev
if (prevForce === undefined) delete process.env["VERDICT_FORCE_TOOL_CHOICE"]
else process.env["VERDICT_FORCE_TOOL_CHOICE"] = prevForce
}
})
})
Loading