refactor(core): reuse read newline locator (#45616)

Reuse the reader newline locator for the terminal tree leaf while preserving accumulated offsets and the whole-tree fallback. Add chunk-boundary coverage.
This commit is contained in:
Kit Langton 2026-08-27 14:47:58 -04:00 committed by GitHub
parent 705606face
commit 284b222489
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 6 deletions

View file

@ -361,12 +361,8 @@ const textOffset = (tree: TextNode, newline: number) => {
if (!child) return tree.summary.bytes
node = child
}
for (const [index, byte] of node.bytes.entries()) {
if (byte !== 10) continue
remaining--
if (remaining === 0) return offset + index + 1
}
return tree.summary.bytes
const end = nthNewline(node.bytes, remaining)
return end === undefined ? tree.summary.bytes : offset + end
}
const nthNewline = (bytes: Uint8Array, count: number) => {

View file

@ -257,6 +257,21 @@ describe("ReadToolFileSystem", () => {
}),
)
it.effect("reads after a newline at the first chunk boundary", () =>
Effect.gen(function* () {
const { environment, files, directory } = yield* fixture
const file = path.join(directory, "boundary.txt")
yield* files.writeFileString(file, `${"a".repeat(256 * 1024 - 1)}\nsecond\n`)
const result = yield* ReadToolFileSystem.read(environment, absolute(file), "boundary.txt", {
offset: 2,
limit: 1,
})
expect(result).toMatchObject({ type: "text-page", content: "second", offset: 2, truncated: false })
}),
)
it.effect("preserves the media ingestion limit message", () =>
Effect.gen(function* () {
const { environment, files, directory } = yield* fixture