fix: route browser tools through tool_fn so they honor RATE_LIMIT - #169
Open
karaposu wants to merge 1 commit into
Open
fix: route browser tools through tool_fn so they honor RATE_LIMIT#169karaposu wants to merge 1 commit into
karaposu wants to merge 1 commit into
Conversation
The 14 scraping_browser_* tools were registered without the tool_fn wrapper every other tool gets, so they bypassed rate-limiting, per-call logging, and session_stats counting. Move the wrapper to the single registration chokepoint (addTool), so every tool -- including the ones defined in browser_tools.js -- is wrapped uniformly, and make tool_fn idempotent so tools already wrapped at their definition site are not double-wrapped (no double count, no second rate-limit check). Behaviour change: browser tools now count against RATE_LIMIT. A browser automation is a sequence of calls, so under a low limit it can now throttle mid-workflow; this is intended -- the exemption was the bug. The 11 existing manual tool_fn(...) calls are now redundant but harmless (idempotency makes them no-ops); removing them is a separate cleanup. Adds test/browser-rate-limit.test.js: with RATE_LIMIT=1/1h, session_stats consumes the slot and a following browser tool call is rejected with "Rate limit exceeded", proving browser tools now run through tool_fn.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
tool_fnis the wrapper every tool is supposed to run through — rate limiting, entry/duration logging,session_statscounting, error translation. It is applied by hand at each definition site, and the 14scraping_browser_*tools inbrowser_tools.jswere never wrapped (importingtool_fnback into that file would be a circular dependency). Consequences for the whole browser family:RATE_LIMITdoes not apply at all — the one control built to stop a runaway agent is absent from exactly the tools that hold paid browser sessions and have unbounded navigate → snapshot → click fan-out;session_statssilently under-reports the most expensive tool family.Fix
Wrap at the single registration chokepoint instead of at 60 definition sites:
tool_fnis now idempotent — wrapping an already-wrapped function returns it unchanged (__tool_fn_wrappedmarker);addToolwraps every tool'sexecuteas it registers it, so tools defined without a manual wrapper — notably the browser tools — get rate limiting, logging, and counting uniformly.The 11 existing manual
tool_fn(...)calls become harmless no-ops (no double count, no second rate-limit check); removing them is a separate cleanup.Behaviour change (intended)
Browser tools now count against
RATE_LIMIT. A browser automation is a sequence of tool calls, so under a low limit it can now throttle mid-workflow — the exemption was the bug. Browser error surfaces are unchanged: their errors areUserErrors / plainErrors, whichtool_fnre-throws as-is.Tests
test/browser-rate-limit.test.js(spawn-based, network-free): withRATE_LIMIT=1/1h,session_statssucceeds and consumes the slot, thenscraping_browser_go_backis rejected withRate limit exceededbefore any connection attempt. On unfixed code the browser call bypasses the limiter (different failure), so the test discriminates fixed from unfixed.Notes
Independent of #163 (no shared commits). Touches
addTool, so it may need a trivial rebase against sibling PRs depending on merge order.