Skip to content

refactor(betterread): smaller, faster reads aligned with host output - #14

Merged
dhaern merged 39 commits into
mainfrom
refactor/betterread-reduction
Sep 28, 2026
Merged

dhaern merged 39 commits into
mainfrom
refactor/betterread-reduction

Conversation

@dhaern

@dhaern dhaern commented Sep 28, 2026 •

Copy link
Copy Markdown
Owner

This branch rewrites the read plugin (opencode-betterread) from the inside: same reads, less code, faster, with output sized to what the host can display. No features removed.

Numbers

Measure Before Now
Production lines 2902 2123
Files 24 15
Tests 118 174, with 21 snapshots
Bundle 44,425 B 47,861 B
Commits 36

The bundle grows slightly because it now includes the JPEG dimension parser and the host output budget. Code is down 27%.

What got simplified

Six files folded into five pieces with one job each: attachments, directory output, formatting, limits and tool. The intermediate index, duplicated structured escaping and the dead factory parameter are gone. Reading a file, a directory or a notebook now passes through fewer layers.

Faster reads

The text reader scanned quadratically in 64 KiB blocks: 2 MB of newlines took 2.5 seconds. The single-pass scanner takes 95 ms. Symlinks resolved serially; now in concurrent batches of 256. The first read after startup loaded the whole effect module (~155 ms); detection now runs before loading and takes ~8 ms.

Case Before Now
Cold start 261 ms 68 ms
Reading 1 MB 6.30 ms 4.32 ms
16,000-entry directory 140.9 ms 71.1 ms
43 MB tail 663 ms 33 ms

Output respects what the host shows

The host truncates output at 2000 lines or ~50 KiB. The plugin used its own budget and the host cut blindly, with no continuation footer, forcing blind re-reads. The plugin now budgets against host limits and includes the footer with the exact offset to continue. The real saving is here: fewer wasted re-reads.

Fixed bugs

  • Binary detection: the high bit was stripped on compare, so Latin-1 text matched GIF or PDF. Comparison is now byte exact.
  • Symlinked notebooks showed the canonical path instead of the requested one. They now show the requested one.
  • Notebooks over 500,000 lines crashed with RangeError from a spread. Copied by loop now.
  • JPEGs with large headers returned empty dimensions or threw RangeError on short lengths. The parser requests only the bytes it needs, with tests per edge.
  • Directory order was dictionary-based and locale dependent: two names equal under different normalization tied and fell back to readdir order. It is now code-unit order, total and stable, documented and tested.

Dependencies

plugin 1.18.32, exact effect beta.83, TypeScript 7.0.2, Biome 2.5.14, @types/node from v24, bun-types 1.4.2 and packageManager [email protected].

Verification

Severity review with APTO verdict, 55 mutants evaluated (50 killed, 5 closed discards), 174/0 suite, clean check and typecheck. No behavior changes except directory order, which is intentional and documented.

@greptile-apps

greptile-apps Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 4/5

[High risk] Refactors file-reading tool with dependency and output-format changes.

The PR is not ready to merge because mtime searches can present incomplete results as complete after a partially failing replay batch.

Summary

The PR refactors betterread’s read and output-budget paths; changes since the previous review also refactor bettergrep’s streaming, fallback, resolver, and mtime search paths.

  • The new mtime replay batching can conceal a partial ripgrep failure and present incomplete matches as a complete search.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Discover matching files] --> B[Sort by mtime]
  B --> C[Replay up to 64 targets]
  C --> D{Result has error?}
  D -- Yes --> E[Retry files individually]
  D -- No --> F[Collect matches]
  F --> G[Finalize search result]
Loading

Reviews (3) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..."

@dhaern

dhaern commented Sep 28, 2026

Copy link
Copy Markdown
Owner Author

@greptile-apps updated review requested: the finding was real, fixed in two commits.

Confirmed (graded P2: unreachable with default 2000-line / 51,200 B budgets, reachable with small custom budgets and long lines). When no line fits the byte budget there was no way forward: the reader selected nothing, and the engine additionally raised a false "Offset out of range" error pointing at a valid offset. So retrying the same call repeated the same output forever.

Fixed in 23dfe93: the engine only errors on genuinely out-of-range offsets, and the empty-window footer now reads "(Line N exceeds budget. Use offset=N+1 to continue.)", so pagination skips exactly the lines that do not fit and shows the rest. Follow-up in 18396f8: when the over-budget line is the last one, the footer reads "(Line N exceeds budget. End of file.)" instead of pointing past the end. Both pinned by failing-first tests (red before, green after, mutants killed); ceilings hold (bundle 48,012 B, 174 tests, snapshots unchanged).

@greptile-apps

greptile-apps Bot commented Sep 28, 2026

Copy link
Copy Markdown

Verified the two follow-up commits. The original stall is fixed: an over-budget line now produces a continuation footer with offset=N+1, so the next read advances past it, and the engine no longer reports a valid offset as out of range. The final-line case correctly emits the EOF footer instead of an unusable continuation offset. The added failing-first coverage exercises both paths. Closing the finding.

@dhaern
dhaern merged commit be1356f into main Sep 28, 2026
1 check passed
@dhaern
dhaern deleted the refactor/betterread-reduction branch September 28, 2026 20:25
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.

1 participant