Skip to content

Commit 2d6bede

Browse files
authored
refactor(flags): migrate output token max to runtime flags (#27680)
1 parent 2080390 commit 2d6bede

7 files changed

Lines changed: 52 additions & 18 deletions

File tree

packages/core/src/flag/flag.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ function truthy(key: string) {
55
return value === "true" || value === "1"
66
}
77

8-
function number(key: string) {
9-
const value = process.env[key]
10-
if (!value) return undefined
11-
const parsed = Number(value)
12-
return Number.isInteger(parsed) && parsed > 0 ? parsed : undefined
13-
}
14-
158
const OPENCODE_EXPERIMENTAL = truthy("OPENCODE_EXPERIMENTAL")
169
const OPENCODE_DISABLE_CLAUDE_CODE = truthy("OPENCODE_DISABLE_CLAUDE_CODE")
1710
const copy = process.env["OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT"]
@@ -54,7 +47,6 @@ export const Flag = {
5447
OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT:
5548
copy === undefined ? process.platform === "win32" : truthy("OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT"),
5649
OPENCODE_ENABLE_EXA: truthy("OPENCODE_ENABLE_EXA") || OPENCODE_EXPERIMENTAL || truthy("OPENCODE_EXPERIMENTAL_EXA"),
57-
OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX: number("OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX"),
5850
OPENCODE_EXPERIMENTAL_LSP_TOOL: OPENCODE_EXPERIMENTAL || truthy("OPENCODE_EXPERIMENTAL_LSP_TOOL"),
5951
OPENCODE_EXPERIMENTAL_PLAN_MODE: OPENCODE_EXPERIMENTAL || truthy("OPENCODE_EXPERIMENTAL_PLAN_MODE"),
6052
OPENCODE_EXPERIMENTAL_SCOUT: OPENCODE_EXPERIMENTAL || truthy("OPENCODE_EXPERIMENTAL_SCOUT"),

packages/opencode/src/effect/runtime-flags.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class Service extends ConfigService.Service<Service>()("@opencode/Runtime
4141
experimentalEventSystem: enabledByExperimental("OPENCODE_EXPERIMENTAL_EVENT_SYSTEM"),
4242
experimentalWorkspaces: enabledByExperimental("OPENCODE_EXPERIMENTAL_WORKSPACES"),
4343
experimentalIconDiscovery: enabledByExperimental("OPENCODE_EXPERIMENTAL_ICON_DISCOVERY"),
44+
outputTokenMax: positiveInteger("OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX"),
4445
bashDefaultTimeoutMs: positiveInteger("OPENCODE_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MS"),
4546
client: Config.string("OPENCODE_CLIENT").pipe(Config.withDefault("cli")),
4647
}) {}

packages/opencode/src/provider/transform.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { JSONSchema7 } from "@ai-sdk/provider"
44
import type * as Provider from "./provider"
55
import type * as ModelsDev from "@opencode-ai/core/models"
66
import { iife } from "@/util/iife"
7-
import { Flag } from "@opencode-ai/core/flag/flag"
87

98
type Modality = NonNullable<ModelsDev.Model["modalities"]>["input"][number]
109

@@ -16,7 +15,7 @@ function mimeToModality(mime: string): Modality | undefined {
1615
return undefined
1716
}
1817

19-
export const OUTPUT_TOKEN_MAX = Flag.OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX || 32_000
18+
export const OUTPUT_TOKEN_MAX = 32_000
2019

2120
export function sanitizeSurrogates(content: string) {
2221
return content.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, "\uFFFD")
@@ -1251,8 +1250,8 @@ export function providerOptions(model: Provider.Model, options: { [x: string]: a
12511250
return { [key]: options }
12521251
}
12531252

1254-
export function maxOutputTokens(model: Provider.Model): number {
1255-
return Math.min(model.limit.output, OUTPUT_TOKEN_MAX) || OUTPUT_TOKEN_MAX
1253+
export function maxOutputTokens(model: Provider.Model, outputTokenMax = OUTPUT_TOKEN_MAX): number {
1254+
return Math.min(model.limit.output, outputTokenMax) || outputTokenMax
12561255
}
12571256

12581257
export function schema(model: Provider.Model, schema: JSONSchema7): JSONSchema7 {

packages/opencode/src/session/compaction.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,12 @@ export const layer = Layer.effect(
228228
tokens: MessageV2.Assistant["tokens"]
229229
model: Provider.Model
230230
}) {
231-
return overflow({ cfg: yield* config.get(), tokens: input.tokens, model: input.model })
231+
return overflow({
232+
cfg: yield* config.get(),
233+
tokens: input.tokens,
234+
model: input.model,
235+
outputTokenMax: flags.outputTokenMax,
236+
})
232237
})
233238

234239
const estimate = Effect.fn("SessionCompaction.estimate")(function* (input: {

packages/opencode/src/session/llm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ const live: Layer.Layer<
173173
: undefined,
174174
topP: input.agent.topP ?? ProviderTransform.topP(input.model),
175175
topK: ProviderTransform.topK(input.model),
176-
maxOutputTokens: ProviderTransform.maxOutputTokens(input.model),
176+
maxOutputTokens: ProviderTransform.maxOutputTokens(input.model, flags.outputTokenMax),
177177
options,
178178
},
179179
)

packages/opencode/src/session/overflow.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@ import type { MessageV2 } from "./message-v2"
55

66
const COMPACTION_BUFFER = 20_000
77

8-
export function usable(input: { cfg: Config.Info; model: Provider.Model }) {
8+
export function usable(input: { cfg: Config.Info; model: Provider.Model; outputTokenMax?: number }) {
99
const context = input.model.limit.context
1010
if (context === 0) return 0
1111

1212
const reserved =
13-
input.cfg.compaction?.reserved ?? Math.min(COMPACTION_BUFFER, ProviderTransform.maxOutputTokens(input.model))
13+
input.cfg.compaction?.reserved ??
14+
Math.min(COMPACTION_BUFFER, ProviderTransform.maxOutputTokens(input.model, input.outputTokenMax))
1415
return input.model.limit.input
1516
? Math.max(0, input.model.limit.input - reserved)
16-
: Math.max(0, context - ProviderTransform.maxOutputTokens(input.model))
17+
: Math.max(0, context - ProviderTransform.maxOutputTokens(input.model, input.outputTokenMax))
1718
}
1819

19-
export function isOverflow(input: { cfg: Config.Info; tokens: MessageV2.Assistant["tokens"]; model: Provider.Model }) {
20+
export function isOverflow(input: {
21+
cfg: Config.Info
22+
tokens: MessageV2.Assistant["tokens"]
23+
model: Provider.Model
24+
outputTokenMax?: number
25+
}) {
2026
if (input.cfg.compaction?.auto === false) return false
2127
if (input.model.limit.context === 0) return false
2228

packages/opencode/test/effect/runtime-flags.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ describe("RuntimeFlags", () => {
8888
expect(flags.enableExa).toBe(false)
8989
expect(flags.experimentalIconDiscovery).toBe(false)
9090
expect(flags.experimentalOxfmt).toBe(false)
91+
expect(flags.outputTokenMax).toBeUndefined()
9192
expect(flags.bashDefaultTimeoutMs).toBe(1_000)
9293
expect(flags.enableExperimentalModels).toBe(false)
9394
expect(flags.client).toBe("cli")
@@ -183,6 +184,35 @@ describe("RuntimeFlags", () => {
183184
)
184185
}
185186

187+
for (const input of [
188+
{ name: "absent", config: {}, expected: undefined },
189+
{
190+
name: "valid positive integer",
191+
config: { OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX: "1234" },
192+
expected: 1234,
193+
},
194+
{
195+
name: "invalid string",
196+
config: { OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX: "nope" },
197+
expected: undefined,
198+
},
199+
{ name: "zero", config: { OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX: "0" }, expected: undefined },
200+
{ name: "negative", config: { OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX: "-1" }, expected: undefined },
201+
{
202+
name: "non-integer",
203+
config: { OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX: "1.5" },
204+
expected: undefined,
205+
},
206+
]) {
207+
it.effect(`parses outputTokenMax from config: ${input.name}`, () =>
208+
Effect.gen(function* () {
209+
const flags = yield* readFlags.pipe(Effect.provide(fromConfig(input.config)))
210+
211+
expect(flags.outputTokenMax).toBe(input.expected)
212+
}),
213+
)
214+
}
215+
186216
it.effect("layer ignores the active ConfigProvider for omitted test overrides", () =>
187217
Effect.gen(function* () {
188218
const flags = yield* readFlags.pipe(
@@ -209,6 +239,7 @@ describe("RuntimeFlags", () => {
209239
expect(flags.enableExa).toBe(false)
210240
expect(flags.experimentalIconDiscovery).toBe(false)
211241
expect(flags.experimentalOxfmt).toBe(false)
242+
expect(flags.outputTokenMax).toBeUndefined()
212243
expect(flags.bashDefaultTimeoutMs).toBeUndefined()
213244
expect(flags.client).toBe("cli")
214245
}),

0 commit comments

Comments
 (0)