Skip to content

Commit dd1db47

Browse files
1rgsrgs_ramprekram1-node
authored
feat(truncate): allow configuring tool output truncation limits (anomalyco#23770)
Co-authored-by: rgs_ramp <[email protected]> Co-authored-by: Aiden Cline <[email protected]>
1 parent 25d8f4f commit dd1db47

4 files changed

Lines changed: 106 additions & 12 deletions

File tree

packages/opencode/src/config/config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,19 @@ export const Info = Schema.Struct({
201201
url: Schema.optional(Schema.String).annotate({ description: "Enterprise URL" }),
202202
}),
203203
),
204+
tool_output: Schema.optional(
205+
Schema.Struct({
206+
max_lines: Schema.optional(PositiveInt).annotate({
207+
description: "Maximum lines of tool output before it is truncated and saved to disk (default: 2000)",
208+
}),
209+
max_bytes: Schema.optional(PositiveInt).annotate({
210+
description: "Maximum bytes of tool output before it is truncated and saved to disk (default: 51200)",
211+
}),
212+
}),
213+
).annotate({
214+
description:
215+
"Thresholds for truncating tool output. When output exceeds either limit, the full text is written to the truncation directory and a preview is returned.",
216+
}),
204217
compaction: Schema.optional(
205218
Schema.Struct({
206219
auto: Schema.optional(Schema.Boolean).annotate({

packages/opencode/src/tool/bash.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,8 @@ export const BashTool = Tool.define(
416416
},
417417
ctx: Tool.Context,
418418
) {
419-
const bytes = Truncate.MAX_BYTES
420-
const lines = Truncate.MAX_LINES
421-
const keep = bytes * 2
419+
const limits = yield* trunc.limits()
420+
const keep = limits.maxBytes * 2
422421
let full = ""
423422
let last = ""
424423
const list: Chunk[] = []
@@ -458,7 +457,7 @@ export const BashTool = Tool.define(
458457
sink?.write(chunk)
459458
} else {
460459
full += chunk
461-
if (Buffer.byteLength(full, "utf-8") > bytes) {
460+
if (Buffer.byteLength(full, "utf-8") > limits.maxBytes) {
462461
return trunc.write(full).pipe(
463462
Effect.andThen((next) =>
464463
Effect.sync(() => {
@@ -525,7 +524,7 @@ export const BashTool = Tool.define(
525524
}
526525
if (aborted) meta.push("User aborted the command")
527526
const raw = list.map((item) => item.text).join("")
528-
const end = tail(raw, lines, bytes)
527+
const end = tail(raw, limits.maxLines, limits.maxBytes)
529528
if (end.cut) cut = true
530529
if (!file && end.cut) {
531530
file = yield* trunc.write(raw)
@@ -566,7 +565,7 @@ export const BashTool = Tool.define(
566565
})
567566

568567
return () =>
569-
Effect.sync(() => {
568+
Effect.gen(function* () {
570569
const shell = Shell.acceptable()
571570
const name = Shell.name(shell)
572571
const chain =
@@ -575,13 +574,15 @@ export const BashTool = Tool.define(
575574
: "If the commands depend on each other and must run sequentially, use a single Bash call with '&&' to chain them together (e.g., `git add . && git commit -m \"message\" && git push`). For instance, if one operation must complete before another starts (like mkdir before cp, Write before Bash for git operations, or git add before git commit), run these operations sequentially instead."
576575
log.info("bash tool using shell", { shell })
577576

577+
const limits = yield* trunc.limits()
578+
578579
return {
579580
description: DESCRIPTION.replaceAll("${directory}", Instance.directory)
580581
.replaceAll("${os}", process.platform)
581582
.replaceAll("${shell}", name)
582583
.replaceAll("${chaining}", chain)
583-
.replaceAll("${maxLines}", String(Truncate.MAX_LINES))
584-
.replaceAll("${maxBytes}", String(Truncate.MAX_BYTES)),
584+
.replaceAll("${maxLines}", String(limits.maxLines))
585+
.replaceAll("${maxBytes}", String(limits.maxBytes)),
585586
parameters: Parameters,
586587
execute: (params: Schema.Schema.Type<typeof Parameters>, ctx: Tool.Context) =>
587588
Effect.gen(function* () {

packages/opencode/src/tool/truncate.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { NodePath } from "@effect/platform-node"
2-
import { Cause, Duration, Effect, Layer, Schedule, Context } from "effect"
2+
import { Cause, Duration, Effect, Layer, Option, Schedule, Context } from "effect"
33
import path from "path"
44
import type { Agent } from "../agent/agent"
55
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
66
import { evaluate } from "@/permission/evaluate"
7+
import { Config } from "../config"
78
import { Identifier } from "../id/id"
89
import { Log } from "../util"
910
import { ToolID } from "./schema"
@@ -38,6 +39,10 @@ export interface Interface {
3839
* to the truncation directory and returns a preview plus a hint to inspect the saved file.
3940
*/
4041
readonly output: (text: string, options?: Options, agent?: Agent.Info) => Effect.Effect<Result>
42+
/**
43+
* Resolved truncation limits: values from `tool_output` in opencode config, or MAX_LINES / MAX_BYTES if unset.
44+
*/
45+
readonly limits: () => Effect.Effect<{ maxLines: number; maxBytes: number }>
4146
}
4247

4348
export class Service extends Context.Service<Service, Interface>()("@opencode/Truncate") {}
@@ -68,9 +73,20 @@ export const layer = Layer.effect(
6873
return file
6974
})
7075

76+
const limits = Effect.fn("Truncate.limits")(function* () {
77+
const configSvc = yield* Effect.serviceOption(Config.Service)
78+
if (Option.isNone(configSvc)) return { maxLines: MAX_LINES, maxBytes: MAX_BYTES }
79+
const cfg = yield* configSvc.value.get().pipe(Effect.catch(() => Effect.succeed(undefined)))
80+
return {
81+
maxLines: cfg?.tool_output?.max_lines ?? MAX_LINES,
82+
maxBytes: cfg?.tool_output?.max_bytes ?? MAX_BYTES,
83+
}
84+
})
85+
7186
const output = Effect.fn("Truncate.output")(function* (text: string, options: Options = {}, agent?: Agent.Info) {
72-
const maxLines = options.maxLines ?? MAX_LINES
73-
const maxBytes = options.maxBytes ?? MAX_BYTES
87+
const resolved = yield* limits()
88+
const maxLines = options.maxLines ?? resolved.maxLines
89+
const maxBytes = options.maxBytes ?? resolved.maxBytes
7490
const direction = options.direction ?? "head"
7591
const lines = text.split("\n")
7692
const totalBytes = Buffer.byteLength(text, "utf-8")
@@ -135,7 +151,7 @@ export const layer = Layer.effect(
135151
Effect.forkScoped,
136152
)
137153

138-
return Service.of({ cleanup, write, output })
154+
return Service.of({ cleanup, write, output, limits })
139155
}),
140156
)
141157

packages/opencode/test/tool/truncation.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, test, expect } from "bun:test"
22
import { NodeFileSystem } from "@effect/platform-node"
33
import { Effect, FileSystem, Layer } from "effect"
44
import { Truncate } from "../../src/tool"
5+
import { Config } from "../../src/config"
56
import { Identifier } from "../../src/id/id"
67
import { Process } from "../../src/util"
78
import { Filesystem } from "../../src/util"
@@ -14,6 +15,14 @@ const ROOT = path.resolve(import.meta.dir, "..", "..")
1415

1516
const it = testEffect(Layer.mergeAll(Truncate.defaultLayer, NodeFileSystem.layer))
1617

18+
const configuredLayer = (cfg: Config.Info) =>
19+
Layer.mergeAll(
20+
Truncate.defaultLayer,
21+
NodeFileSystem.layer,
22+
Layer.mock(Config.Service)({ get: () => Effect.succeed(cfg) }),
23+
)
24+
const configuredIt = (cfg: Config.Info) => testEffect(configuredLayer(cfg))
25+
1726
describe("Truncate", () => {
1827
describe("output", () => {
1928
it.live("truncates large json file by bytes", () =>
@@ -94,6 +103,61 @@ describe("Truncate", () => {
94103
expect(Truncate.MAX_BYTES).toBe(50 * 1024)
95104
})
96105

106+
it.live("limits() falls back to MAX_LINES/MAX_BYTES when Config is not provided", () =>
107+
Effect.gen(function* () {
108+
const svc = yield* Truncate.Service
109+
const resolved = yield* svc.limits()
110+
expect(resolved.maxLines).toBe(Truncate.MAX_LINES)
111+
expect(resolved.maxBytes).toBe(Truncate.MAX_BYTES)
112+
}),
113+
)
114+
115+
describe("with tool_output config", () => {
116+
const limitsIt = configuredIt({ tool_output: { max_lines: 123, max_bytes: 456 } })
117+
limitsIt.live("limits() reflects config overrides", () =>
118+
Effect.gen(function* () {
119+
const resolved = yield* (yield* Truncate.Service).limits()
120+
expect(resolved.maxLines).toBe(123)
121+
expect(resolved.maxBytes).toBe(456)
122+
}),
123+
)
124+
125+
// Huge byte budget isolates line truncation. 100 lines against max_lines: 10
126+
// proves the configured line limit is what `output()` enforces.
127+
const lineIt = configuredIt({ tool_output: { max_lines: 10, max_bytes: 1024 * 1024 } })
128+
lineIt.live("output() truncates to configured max_lines", () =>
129+
Effect.gen(function* () {
130+
const content = Array.from({ length: 100 }, (_, i) => `line${i}`).join("\n")
131+
const result = yield* (yield* Truncate.Service).output(content)
132+
expect(result.truncated).toBe(true)
133+
expect(result.content).toContain("...90 lines truncated...")
134+
}),
135+
)
136+
137+
// Huge line budget isolates byte truncation.
138+
const byteIt = configuredIt({ tool_output: { max_lines: 1_000_000, max_bytes: 100 } })
139+
byteIt.live("output() truncates to configured max_bytes", () =>
140+
Effect.gen(function* () {
141+
const content = "a".repeat(1000)
142+
const result = yield* (yield* Truncate.Service).output(content)
143+
expect(result.truncated).toBe(true)
144+
expect(result.content).toContain("bytes truncated...")
145+
}),
146+
)
147+
148+
const overrideIt = configuredIt({ tool_output: { max_lines: 10, max_bytes: 100 } })
149+
overrideIt.live("per-call options still override config", () =>
150+
Effect.gen(function* () {
151+
const content = Array.from({ length: 50 }, (_, i) => `line${i}`).join("\n")
152+
const result = yield* (yield* Truncate.Service).output(content, {
153+
maxLines: 1000,
154+
maxBytes: 1024 * 1024,
155+
})
156+
expect(result.truncated).toBe(false)
157+
}),
158+
)
159+
})
160+
97161
it.live("large single-line file truncates with byte message", () =>
98162
Effect.gen(function* () {
99163
const svc = yield* Truncate.Service

0 commit comments

Comments
 (0)