Skip to content

refactor(agent): extract image budget helpers - #513

Merged
esokullu merged 2 commits into
webbrain-one:mainfrom
alectimison-maker:refactor/extract-image-budget
Jul 26, 2026
Merged

refactor(agent): extract image budget helpers#513
esokullu merged 2 commits into
webbrain-one:mainfrom
alectimison-maker:refactor/extract-image-budget

Conversation

@alectimison-maker

Copy link
Copy Markdown
Contributor

Summary

  • move image token estimation, default budget constants, and dimension fitting into browser-free production modules
  • keep the existing Agent.IMAGE_BUDGET, _estimateImageTokens, and _fitImageDimensions surfaces as compatibility wrappers
  • make test/run.js import the production sizing logic instead of maintaining a copied shim
  • enforce byte-identical Chrome and Firefox image-budget modules

Motivation

TODOs.md identified image sizing as the next high-value case where tests duplicated pure logic because importing agent.js brings browser APIs with it. The copied implementation could drift from production while both appeared tested.

This follows the same production-module pattern established for loop detection in #490.

Design

agent/image-budget.js owns the complete default budget object plus the two browser-neutral sizing functions. Each Agent imports those exports and retains its static methods so existing capture paths and tests keep the same call surface.

Only deterministic sizing moved. Chrome's OffscreenCanvas compression and Firefox's DOM canvas encoding remain platform-specific inside their Agent implementations.

The unit suite now imports image-budget.js directly for fast-path, token-cap, aspect-ratio, extreme-strip, and custom-budget cases. A parity assertion prevents the browser copies from drifting.

Testing

  • node test/run.js — 1,329 passed; 1 inherited baseline failure (package.json is 25.9.7 while the newest changelog entry is 25.9.0)
  • npm run test:security — 60/60 passed
  • npm run test:fixtures — 125/125 passed
  • npm run test:webmcp — passed against Chrome 150
  • npm run test:ci — passed
  • node --check src/{chrome,firefox}/src/agent/{agent,image-budget}.js — passed

Compatibility and risks

The existing Agent static fields and methods are preserved. Default values and the binary-search algorithm are unchanged. This does not alter screenshot capture, compression, encoding, or provider behavior.

Scope

This intentionally leaves browser-dependent image compression in agent.js. Other pure helper extraction and broader parser/context tests remain follow-ups in TODOs.md.

@vercel

vercel Bot commented Jul 26, 2026

Copy link
Copy Markdown

@alectimison-maker is attempting to deploy a commit to the esokullu's projects Team on Vercel.

A member of the Team first needs to authorize it.

alectimison-maker and others added 2 commits July 26, 2026 20:59
Object.freeze the module-level IMAGE_BUDGET so accidental mutation of the
shared default throws (modules are strict mode) instead of silently changing
every caller. Per-capture overrides already spread into fresh objects.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@esokullu
esokullu force-pushed the refactor/extract-image-budget branch from efd4690 to 700aef9 Compare July 26, 2026 18:01
@esokullu
esokullu merged commit 4834191 into webbrain-one:main Jul 26, 2026
1 of 2 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.

2 participants