Skip to content

feat(opencode): add offset/limit paging to webfetch - #208

Merged
mdev34-lab merged 4 commits into
devfrom
arena/271a0e4b-alphacode
Oct 6, 2026
Merged

mdev34-lab merged 4 commits into
devfrom
arena/271a0e4b-alphacode

Conversation

@mdev34-lab

@mdev34-lab mdev34-lab commented Oct 6, 2026 •

Copy link
Copy Markdown
Owner

Issue for this PR

Closes #204

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

webfetch returned the whole converted page in one shot, so a model that wanted
lines 4000-6000 of a long document had to re-fetch and re-convert it. It now takes
offset (1-indexed start line) and limit (defaults to 2000, matching read's
DEFAULT_READ_LIMIT) and windows the post-conversion content, so a line number
means the same thing for text, markdown, and html.

Windowed output ends with read's continuation wording, e.g.
(Showing lines 4000-6000 of 12000. Use offset=6001 to continue.), or the byte-cap
variant (Output capped at 50 KB. Showing lines ...). Lines longer than 2000 chars
are clipped with the same suffix read uses, and an offset past the end fails with
Offset N is out of range for this content (M lines) as a normal tool failure
rather than a defect.

Oversized output still reaches the model. When the window itself cuts content, the
full converted body is spilled through the truncate service and the saved path is
appended after the notice. When the window fits webfetch's own budget but the
result is bigger than configured tool_output caps, metadata is left empty so the
generic tool wrapper still caps and saves it as before.

offset/limit stay NonNegativeInt for parity with read; windowContent
clamps their low end so limit: 0 yields a one-line window instead of an empty,
reversed notice that asks for the same call again. Supplying neither parameter and
staying inside the caps returns the converted body byte-identically to before.

How did you verify your code works?

  • bun test test/tool/webfetch.test.ts test/tool/parameters.test.ts -> 81 pass
    (21 webfetch cases, including paging per format, offset-only defaulting, CRLF
    bodies, empty bodies, byte-cap resume over mixed long/short lines, limit: 0
    with and without an offset, a >50 KB body with no paging params, a tighter
    configured tool_output.max_bytes, and outputPath on a truncated fetch).
  • bun test test/tool/ -> 505 pass. The 2 failures are pre-existing on dev
    (task wire-shape snapshot, background-subagent registry test); a third shell
    test is flaky and passes in isolation.
  • packages/opencode tsc --noEmit and packages/core tsgo --noEmit pass.
    Prettier clean, oxlint 0 errors.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

let bytes = 0
let cut = false
for (const line of lines.slice(start)) {
if (page.length >= limit) break

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: limit: 0 yields an empty window with a self-referential continuation notice

limit is NonNegativeInt, so limit: 0 passes validation. The page.length >= limit check breaks on the first iteration, leaving page empty, so last becomes offset - 1 and next becomes offset. The emitted notice reads e.g. "(Showing lines 2-1 of 5. Use offset=2 to continue.)" — a reversed range that instructs the model to re-issue the exact call it just made, which can loop tool calls until the turn budget is exhausted. Consider requiring limit >= 1 (e.g. PositiveInt, or normalizing 0 to 1 alongside the params.offset || 1 coercion). Note read.ts's lines helper has the same edge case, so fixing both tools together keeps their paging semantics aligned.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

@kilo-code-bot

kilo-code-bot Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Code Review Summary

Status: 2 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 2
SUGGESTION 0
Issue Details (click to expand)

WARNING

File Line Issue
packages/opencode/src/tool/webfetch.ts 187 Byte-only overflow is silently dropped when neither paging param is supplied
packages/opencode/src/tool/webfetch.ts 224 limit: 0 produces an empty window and a self-referential continuation notice
Files Reviewed (3 files)
  • packages/opencode/src/tool/webfetch.ts - 2 issues
  • packages/opencode/src/tool/webfetch.txt - 0 issues
  • packages/opencode/test/tool/webfetch.test.ts - 0 issues

Fix these issues in Kilo Cloud

Previous Review Summaries (3 snapshots, latest commit 6ae26bf)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 6ae26bf)

Status: 2 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 2
SUGGESTION 0
Issue Details (click to expand)

WARNING

File Line Issue
packages/opencode/src/tool/webfetch.ts 187 Byte-only overflow is silently dropped when neither paging param is supplied
packages/opencode/src/tool/webfetch.ts 224 limit: 0 produces an empty window and a self-referential continuation notice
Files Reviewed (3 files)
  • packages/opencode/src/tool/webfetch.ts - 2 issues
  • packages/opencode/src/tool/webfetch.txt - 0 issues
  • packages/opencode/test/tool/webfetch.test.ts - 0 issues

Fix these issues in Kilo Cloud

Previous review (commit 7b73bab)

Status: No Issues Found | Recommendation: Merge

Files Reviewed (5 files)
  • packages/opencode/src/tool/webfetch.ts
  • packages/opencode/src/tool/webfetch.txt
  • packages/opencode/test/tool/__snapshots__/parameters.test.ts.snap
  • packages/opencode/test/tool/parameters.test.ts
  • packages/opencode/test/tool/webfetch.test.ts

Previous review

Status: 1 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 0
SUGGESTION 1
Issue Details (click to expand)

SUGGESTION

File Line Issue
packages/opencode/src/tool/webfetch.ts 187 limit: 0 passes NonNegativeInt validation and produces an empty window whose continuation notice points back at the same offset ("Showing lines 2-1 of 5. Use offset=2 to continue."), which can loop the model's tool calls until the turn budget is exhausted. Consider requiring limit >= 1 (PositiveInt or normalizing 0 to 1); read.ts shares the same edge case.
Files Reviewed (5 files)
  • packages/opencode/src/tool/webfetch.ts - 1 issue
  • packages/opencode/src/tool/webfetch.txt
  • packages/opencode/test/tool/__snapshots__/parameters.test.ts.snap
  • packages/opencode/test/tool/parameters.test.ts
  • packages/opencode/test/tool/webfetch.test.ts

Fix these issues in Kilo Cloud


Reviewed by free · Input: 173.5K · Output: 21.3K · Cached: 14.3K

- When a window truncates, spill the full converted body through the
  truncate service like the generic tool wrapper used to, set
  metadata.outputPath when it was saved, and mention the saved path after
  the continuation notice. The default (no offset/limit) path is unchanged.
- Normalise offset/limit 0 to 1 at runtime; `limit: 0` no longer emits a
  reversed, self-referential notice.
- Report an out-of-range offset as a typed ToolFailure from outside the
  Effect.orDie boundary instead of dying as a defect.
- Split converted lines on /\r?\n/ so CRLF bodies do not keep a trailing \r.
- Add tests for the clamp, CRLF bodies, empty bodies with an explicit
  offset, an offset on the last line, byte-cap resume over mixed long and
  short lines, and outputPath on a truncated fetch.

Co-authored-by: arena-agent <[email protected]>

Copy link
Copy Markdown
Owner Author

Review fixes pushed in 7b73bab (same branch, PR updates in place). Per item:

  1. outputPath restored (major). metadata.truncated still short-circuits the
    generic wrapper in tool.ts, so webfetch now spills the full converted body
    itself when a window truncates: const saved = windowed.truncated ? yield* truncate.output(converted) : undefined.
    When the saved file is genuinely oversized, metadata is { truncated: true, outputPath }
    and the output ends with Full output saved to: <path> after the continuation notice.
    The no-param path with content inside the caps is untouched and still flows through
    the wrapper, so configured tool_output limits still apply there.
  2. limit: 0 normalised. Both params stay NonNegativeInt like read's, and
    execute clamps with Math.max(1, params.limit ?? DEFAULT_LIMIT) (and the same for
    offset). A zero limit now yields (Showing lines 2-2 of 3. Use offset=3 to continue.)
    instead of a reversed window.
  3. Out-of-range offset is a typed failure. The raw fetch/convert pipeline moved into
    fetchContent (still behind Effect.orDie); the new execute runs the windowing and
    range check outside it and fails with Effect.fail(new ToolFailure({ message: "Offset N is out of range..." })).
    Test asserts Cause.hasFails and not Cause.hasDies.
  4. CRLF. Converted content is split on /\r?\n/; a CRLF body returns lines with no \r.
  5. NIT documented. The clamp comment states 0 is normalised rather than silently ignored.

New tests: zero-limit clamp, CRLF body, empty body with explicit offset=1,
offset exactly on the last line, byte-cap resume over mixed long/short lines,
and a truncated fetch that yields metadata.outputPath whose saved file contains
the whole converted body (not just the window). Also added a schema test that
limit: 0 decodes and a hasDies === false assertion for the range failure.

Verification: bun test test/tool/ → 503 pass, 2 pre-existing failures unrelated to
this change (task wire-shape snapshot, background-subagent registry test; both
reproduce on a clean checkout). packages/opencode tsc --noEmit and
packages/core tsgo --noEmit pass. Prettier clean, oxlint 0 errors
(one pre-existing warning in the test file). No snapshot churn.

…h windows

- A window that fits webfetch's own line/byte budget now returns empty
  metadata, so the generic tool wrapper still re-truncates it when it
  exceeds configured `tool_output` caps. Reporting `truncated: false`
  previously short-circuited the wrapper and silently ignored those caps.
- Reword the description: the converted body is saved when it exceeds the
  output caps, and the saved path is appended after the notice.
- Test: with `tool_output.max_bytes` at 10 KB, a 38 KB window that fits the
  default caps is capped by the wrapper and yields outputPath.

Co-authored-by: arena-agent <[email protected]>

Copy link
Copy Markdown
Owner Author

Round-2 fixes pushed in 6ae26bf (same branch, PR updates in place):

  1. Configured tool_output caps are effective again. The !windowed.truncated
    early return that reported metadata: { truncated: false } is gone. A window
    that fits webfetch's own 2000-line/50 KB budget now falls through to the shared
    return, which emits metadata: {} when nothing was cut — so the wrapper at
    tool.ts:139 still runs truncate.output against the configured caps. A window
    that webfetch itself cut still reports { truncated: true, outputPath } and is
    saved by webfetch as before.
    New test re-truncates a fitting window through a tighter configured output cap
    uses tool_output.max_bytes: 10240 and a 100-line/~38 KB body with limit=100
    (whole body in the window, no window cut): the assertion sees the wrapper's
    ... bytes truncated ... hint plus metadata.outputPath. I confirmed this test
    fails against 7b73bab and passes with the fix.

  2. Description reworded (webfetch.txt): "When the converted body exceeds the
    output caps it is saved and the saved path is appended after the notice, so it
    does not need to be fetched again." No longer claims a save on every windowed
    fetch, and no longer says the path is part of the range notice.

Verification: bun test test/tool/ → 504 pass, same 2 pre-existing unrelated
failures (task wire-shape snapshot, background-subagent registry test).
packages/opencode tsc --noEmit and packages/core tsgo --noEmit pass;
Prettier clean; oxlint 0 errors. Only webfetch.ts, webfetch.txt, and
webfetch.test.ts touched.

let bytes = 0
let cut = false
for (const line of lines.slice(start)) {
if (page.length >= limit) break

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

WARNING: limit: 0 produces an empty window and a self-referential continuation notice.

limit is NonNegativeInt, so limit: 0 passes schema validation. In windowContent, the check if (page.length >= limit) break fires on the first iteration, leaving page empty. Then last = offset + page.length - 1 = offset - 1 and next = last + 1 = offset, so the notice reads e.g. "(Showing lines 2-1 of 5. Use offset=2 to continue.)" — a reversed range that instructs the model to re-issue the exact call it just made, which can loop tool calls until the turn budget is exhausted.

The Math.max(1, params.limit ?? DEFAULT_LIMIT) normalization at webfetch.ts:184 only clamps the low end for the call; it does not reject 0 at the schema level, so the bad value still reaches windowContent. Consider requiring limit >= 1 (e.g. PositiveInt, or normalizing 0 to 1 alongside the params.offset || 1 coercion). Note read.ts's lines helper has the same edge case, so fixing both tools together keeps their paging semantics aligned.

Suggested change
if (page.length >= limit) break
if (page.length >= limit) break

Comment thread packages/opencode/src/tool/webfetch.ts Outdated
Math.max(1, params.limit ?? DEFAULT_LIMIT),
)
if (!windowed.ok) return yield* Effect.fail(new ToolFailure({ message: windowed.message }))
if (!windowed.truncated && params.offset === undefined && params.limit === undefined) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

WARNING: Byte-only overflow is silently dropped when neither paging param is supplied.

The early-return at webfetch.ts:187 fires when !windowed.truncated && params.offset === undefined && params.limit === undefined. windowed.truncated is cut || more, where cut tracks the 50 KB byte cap and more tracks leftover lines. If the converted body exceeds 50 KB but fits in one page, cut is true and the early return is skipped — correct. But if the body exceeds 50 KB and the caller did not ask for paging, the early return still fires because more is false, so the wrapper never gets the full body to spill. The model then sees the raw converted output with no continuation notice and no saved-file path, and cannot page further without re-fetching.

The generic wrapper in tool.ts:140 only spills when metadata.truncated is undefined, so webfetch is the sole layer responsible for surfacing byte-cap overflow. Dropping that responsibility for the no-param case breaks the contract the PR description promises ("the converted body is saved to a file"). Guard should also require !cut, or move the byte-cap check before the early return.

Suggested change
if (!windowed.truncated && params.offset === undefined && params.limit === undefined) {
if (!windowed.truncated && params.offset === undefined && params.limit === undefined && !windowed.cut)

- Move the offset/limit low-end clamp into `windowContent`, next to the loop
  that relies on it, so a decoded 0 can never produce an empty reversed
  window ("Showing lines 2-1 ... Use offset=2").
- Return `cut` and `more` from `windowContent` and gate the unchanged-body
  fast path on both explicitly, so byte-cap-only overflow can never skip the
  saved-file path and continuation notice.
- Regression tests: limit 0 with and without an offset, and a >50 KB body
  fetched with no paging params that must spill and report the cap.

Co-authored-by: arena-agent <[email protected]>

Copy link
Copy Markdown
Owner Author

Kilo Code bot warnings addressed in 8b400a2 (same branch). Two notes on what I found while fixing them:

1. limit: 0 (webfetch.ts windowContent). The call site already clamped
(Math.max(1, params.limit ?? DEFAULT_LIMIT)) since round 1, so a zero produced a
non-empty one-line window rather than Showing lines 2-1. I moved the clamp into
windowContent itself, next to the page.length >= limit loop that depends on it,
so the invariant is local to the function and cannot be lost by a future caller.
Tests: clamps a zero limit instead of returning a reversed window now also asserts
the window starts at the requested line, plus a new
clamps a zero limit to one line when offset is omitted.

2. Byte-only overflow without paging params. The guard was already safe: cut
implies at least one line was left unpushed, so more is true, so truncated was
true and the fast path could not fire — I verified this empirically with a 96 KB,
120-line body and no params, which spilled and reported the cap correctly before any
change. I still hardened it as requested: windowContent now returns cut and more
separately and the guard is !windowed.cut && !windowed.more && offset/limit undefined,
so the two reasons are explicit at the call site instead of relying on
truncated = cut || more. New regression test
spills byte-only overflow and reports the cap when no params are given asserts the
byte-cap notice, metadata.outputPath, and that the saved file holds the full body.

Verification: bun test test/tool/webfetch.test.ts test/tool/parameters.test.ts → 81
pass; test/tool/ → 505 pass with the 2 pre-existing unrelated failures (task
wire-shape snapshot, background-subagent registry test) and one flaky shell test that
passes in isolation; packages/opencode tsc --noEmit and packages/core
tsgo --noEmit pass; Prettier clean; oxlint 0 errors.

PR body re-set on .github/pull_request_template.md as requested. Only
webfetch.ts and webfetch.test.ts changed in this commit.

@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@mdev34-lab
mdev34-lab merged commit 6290668 into dev Oct 6, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

webfetch: add offset/limit so the model can page through large fetched content

1 participant