fix read stream byte cap

This commit is contained in:
Dax Raad
2026-05-16 09:18:35 -04:00
parent f56263791c
commit 77db212a0a
2 changed files with 64 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
import { afterEach, describe, expect } from "bun:test"
import { Cause, Effect, Exit, Layer } from "effect"
import { Cause, Effect, Exit, Layer, Stream } from "effect"
import path from "path"
import { Agent } from "../../src/agent/agent"
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
@@ -366,6 +366,38 @@ describe("tool.read truncation", () => {
}),
)
it.instance("stops streaming after the byte cap", () =>
Effect.gen(function* () {
const test = yield* TestInstance
const filepath = path.join(test.directory, "huge.txt")
const content = `${"x".repeat(80)}\n`.repeat(50_000)
yield* put(filepath, content)
const fs = yield* AppFileSystem.Service
const counter = { bytes: 0 }
const result = yield* run({ filePath: filepath }).pipe(
Effect.provideService(
AppFileSystem.Service,
AppFileSystem.Service.of({
...fs,
stream: (file, options) =>
fs.stream(file, options).pipe(
Stream.tap((chunk) =>
Effect.sync(() => {
counter.bytes += chunk.length
}),
),
),
}),
),
)
expect(result.metadata.truncated).toBe(true)
expect(result.output).toContain("Output capped at")
expect(counter.bytes).toBeLessThan(Buffer.byteLength(content, "utf-8") / 2)
}),
)
it.instance("truncates by line count when limit is specified", () =>
Effect.gen(function* () {
const test = yield* TestInstance