Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Estimate card prompts as a worst case, not as prose
Claudecbcf3f8932de
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a923dfe40..aaf1550e0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,7 +10,7 @@ This changelog was generated from the repository Git history and release tags. V- Selecting text on a page now shows a small bar, **Translate | Explain | Proofread | More…**, instead of the single `?` button. Translate, Explain and Proofread answer in a card next to the selection: Translate shows the detected source language, the original text and a target-language picker, and every card has an "Ask a follow-up" box that, once the answer is there, continues in the side panel with the card's answer as context. **More…** opens the same popup the `?` button opened (Summarize, Quiz me, Humanize, a question box and Hide this; Translate, Explain and Proofread are on the bar, so they are not repeated there). A card sends the selected text to your configured model provider, like any other selection action; selections over 4,000 characters go to the side panel instead of a card. A card answer that reaches the model's output limit says it was cut off and offers to continue in the side panel, which picks up from the unfinished text instead of starting over (a side-panel reply that is cut off again says so, including when an OpenAI Responses model reports the limit as an error, in which case the text it wrote is kept and the call still counts toward your cloud-cost allowance), and closing or redoing a card, or reloading or leaving the page, cancels the request it no longer needs. Chrome and Firefox.### Fixed-- Chrome: sending a follow-up or a selection action to WebBrain while the side panel is already open no longer closes it. Each side panel now tells the background whether it is on screen, and the background skips re-opening a panel that is already showing and hands it the prompt directly A panel that was open while the background restarted tells the new worker about itself at once instead of after a delay, so a request in that moment does not close it. The same holds for actions chosen on a selection in WebBrain's PDF viewer, where a panel that was closed is opened once rather than twice. A side panel that the browser moves to another window (its tab dragged out) now reports its new window, so an action in that window no longer closes it and an action in the old window opens a panel again. The PDF viewer keeps its own window up to date the same way, so a viewer tab dragged to another window reports where it is now, taking the new window from the tab-attached event rather than from a lookup a quick click could outrun. A card answer whose request is the one to reach the cloud cost allowance now says so under the answer instead of showing it as an ordinary success.+- Chrome: sending a follow-up or a selection action to WebBrain while the side panel is already open no longer closes it. Each side panel now tells the background whether it is on screen, and the background skips re-opening a panel that is already showing and hands it the prompt directly A panel that was open while the background restarted tells the new worker about itself at once instead of after a delay, so a request in that moment does not close it. The same holds for actions chosen on a selection in WebBrain's PDF viewer, where a panel that was closed is opened once rather than twice. A side panel that the browser moves to another window (its tab dragged out) now reports its new window, so an action in that window no longer closes it and an action in the old window opens a panel again. The PDF viewer keeps its own window up to date the same way, so a viewer tab dragged to another window reports where it is now, taking the new window from the tab-attached event rather than from a lookup a quick click could outrun. A card answer whose request is the one to reach the cloud cost allowance now says so under the answer instead of showing it as an ordinary success. The room a card leaves for the prompt in a small context window is now estimated as a worst case (a token per ASCII character, two for anything else), so dense text such as minified code or hashes is handed to the side panel on a 4k local model instead of being sent a request the backend rejects.- Stopping a task while WebBrain was waiting for page navigation to settle now stops it immediately. The pending settle step previously kept waiting and could let the run continue after you chose Stop.- Mastodon: replying to toots in a narrow window no longer posts the same reply several times and stops before the next toot. There, a toot's Reply button opens the composer at /publish, and after posting Mastodon goes back to the previous column by itself. WebBrain took both page changes for something you did, so it reported each click's outcome as unknown; the agent then concluded its reply had not been posted, wrote it again, and never reached the next toot. In Chrome, a page's own Back is now told apart from your browser's Back button: the page a click opens counts as that click's verified effect, and a click after which the page goes back by itself counts as delivered, to be checked on the page. The Mastodon notes now cover replies (start with the prefilled @mentions once, check the reply under the toot before any retry) and stay active on Favourites, Bookmarks and the other columns. Firefox still treats a page's own Back as yours until its Back button reporting is verified.- Finishing a form you asked WebBrain not to submit no longer takes most of a minute. The completion check removed every line that mentioned a password, so "fill out this form with a random password … don't submit" reached its judge as "[credential omitted]", and so did the approved plan and the agent's summary. Seeing only an unsubmitted signup form, the judge answered "pending" again and again; in the trace it took four attempts and about 50 seconds, ending only when a slow screenshot check ran out of time. The active model's check, which already holds these texts in the conversation, now receives every line of them, with only addresses shortened and bearer tokens and entered passwords hidden, as in all judge evidence. A decision model on this device, or WebBrain Cloud's own while WebBrain Cloud is also your provider, receives that same copy; any other decision model keeps whole-line redaction, and when that leaves it without the task, it is no longer asked while the active model can judge. A password WebBrain typed into a password field during the run is now hidden in the text judges receive, such as the agent's summary or a field a "show password" control revealed, unless the typing tool could not tell what kind of field it was; other entered values stay visible, so a judge can still catch a mistyped one. The judges are told that a task asking not to submit is complete once the staged values match, that a hidden value neither proves nor disproves an entry, and, for each recorded entry, whether its field read back what was entered, whether it is a credential field and which field it targeted; a credential field's line in the page evidence keeps its role and reference. Staged field edits are judged from the accessibility tree first: a screenshot shows passwords only as dots, and the focused field's blinking caret made each one stale before its verdict returned.diff --git a/docs/architecture.md b/docs/architecture.mdindex 9c563a046..98649822d 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -522,7 +522,7 @@ cost allowance (90 s timeout) and returns the text. The output budget is sized tselection (at least 2,000 tokens; Translate and Proofread grow to about two tokens percharacter, up to 8,000) and never exceeds the provider's own output ceiling or the room itscontext window leaves after the prompt (estimated on the high side, since the provider's tokenizer-is not available: half a token per Latin-range character, two per other character such as CJK,+is not available: a whole token per ASCII character, as dense text such as minified code or hashes can need, two per other character such as accented letters or CJK,plus a reserve), so a small local window is not sent a request it would reject. When the promptleaves less than 256 tokens for the answer, the background makes no call and answers with thecut-off shape, so the card hands the selection to the side panel. If the replydiff --git a/src/chrome/src/context-menu-storage.js b/src/chrome/src/context-menu-storage.jsindex 53e3eafad..a1794372d 100644--- a/src/chrome/src/context-menu-storage.js+++ b/src/chrome/src/context-menu-storage.js@@ -281,14 +281,15 @@ const INLINE_FALLBACK_OUTPUT_CEILING = 4096;// Room kept for the prompt in a small context window: backends that enforce// input + max_tokens <= window reject a request whose output budget ignores it. The// provider's tokenizer is not available here, so the prompt is estimated per-// character on the high side: Latin-range text at half a token, everything else-// (CJK, Indic, Arabic, Cyrillic, emoji) at two, which covers the worst tokenizers.-// Over-counting only makes a tight window end in a flagged cut-off or a hand-off to-// the side panel instead of a refused request. Chat-template tokens are covered by-// the reserve.-const INLINE_LATIN_TOKENS_PER_CHAR = 0.5;+// character as a worst case, not as prose: an ASCII character at one whole token (minified+// code, identifiers, hashes and other high-entropy text can take that, and prose takes far+// less), anything else (accented Latin, CJK, Indic, Arabic, Cyrillic, emoji) at two, which+// covers byte-level fallbacks. Over-counting only makes a tight window end in a flagged+// cut-off or a hand-off to the side panel instead of a refused request. Chat-template+// tokens are covered by the reserve.+const INLINE_ASCII_TOKENS_PER_CHAR = 1;const INLINE_OTHER_TOKENS_PER_CHAR = 2;-const INLINE_LATIN_RANGE_END = 0x250;+const INLINE_ASCII_RANGE_END = 0x80;const INLINE_PROMPT_TOKEN_RESERVE = 128;const INLINE_MIN_ANSWER_TOKENS = 256;@@ -297,8 +298,8 @@ function estimateInlinePromptTokens(messages) {for (const message of Array.isArray(messages) ? messages : []) {if (typeof message?.content !== 'string') continue;for (const char of message.content) {- tokens += char.codePointAt(0) < INLINE_LATIN_RANGE_END- ? INLINE_LATIN_TOKENS_PER_CHAR+ tokens += char.codePointAt(0) < INLINE_ASCII_RANGE_END+ ? INLINE_ASCII_TOKENS_PER_CHAR: INLINE_OTHER_TOKENS_PER_CHAR;}}diff --git a/src/firefox/src/context-menu-storage.js b/src/firefox/src/context-menu-storage.jsindex 101ba942c..729bd3ab9 100644--- a/src/firefox/src/context-menu-storage.js+++ b/src/firefox/src/context-menu-storage.js@@ -281,14 +281,15 @@ const INLINE_FALLBACK_OUTPUT_CEILING = 4096;// Room kept for the prompt in a small context window: backends that enforce// input + max_tokens <= window reject a request whose output budget ignores it. The// provider's tokenizer is not available here, so the prompt is estimated per-// character on the high side: Latin-range text at half a token, everything else-// (CJK, Indic, Arabic, Cyrillic, emoji) at two, which covers the worst tokenizers.-// Over-counting only makes a tight window end in a flagged cut-off or a hand-off to-// the side panel instead of a refused request. Chat-template tokens are covered by-// the reserve.-const INLINE_LATIN_TOKENS_PER_CHAR = 0.5;+// character as a worst case, not as prose: an ASCII character at one whole token (minified+// code, identifiers, hashes and other high-entropy text can take that, and prose takes far+// less), anything else (accented Latin, CJK, Indic, Arabic, Cyrillic, emoji) at two, which+// covers byte-level fallbacks. Over-counting only makes a tight window end in a flagged+// cut-off or a hand-off to the side panel instead of a refused request. Chat-template+// tokens are covered by the reserve.+const INLINE_ASCII_TOKENS_PER_CHAR = 1;const INLINE_OTHER_TOKENS_PER_CHAR = 2;-const INLINE_LATIN_RANGE_END = 0x250;+const INLINE_ASCII_RANGE_END = 0x80;const INLINE_PROMPT_TOKEN_RESERVE = 128;const INLINE_MIN_ANSWER_TOKENS = 256;@@ -297,8 +298,8 @@ function estimateInlinePromptTokens(messages) {for (const message of Array.isArray(messages) ? messages : []) {if (typeof message?.content !== 'string') continue;for (const char of message.content) {- tokens += char.codePointAt(0) < INLINE_LATIN_RANGE_END- ? INLINE_LATIN_TOKENS_PER_CHAR+ tokens += char.codePointAt(0) < INLINE_ASCII_RANGE_END+ ? INLINE_ASCII_TOKENS_PER_CHAR: INLINE_OTHER_TOKENS_PER_CHAR;}}diff --git a/test/run.js b/test/run.jsindex cf4946ebb..9ae5b91fd 100644--- a/test/run.js+++ b/test/run.js@@ -52442,20 +52442,22 @@ test('inline card token budget follows the selection and never exceeds the provi// The prompt shares the context window with the answer.const prompt = (chars) => [{ role: 'system', content: 'x'.repeat(500) }, { role: 'user', content: 'x'.repeat(chars) }];const longText = 'x'.repeat(3000);- assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 4096, messages: prompt(3500) }), 4096 - Math.ceil(4000 * 0.5) - 128,+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 4096, messages: prompt(2500) }), 4096 - 3000 - 128,`${label}: a 4k window leaves the answer only what the prompt does not use`);+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 4096, messages: prompt(3500) }), 0,+ `${label}: a prompt that nearly fills a 4k window leaves no room, even though plain text would take far fewer tokens than characters`);assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 131_072, messages: prompt(3500) }), 6000,`${label}: a large window changes nothing`);assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 16_384, messages: prompt(3500) }), 6000,`${label}: an ordinary local window changes nothing for a card-sized selection`);- // Prompt estimate: 128 reserved + half a token per Latin character; two per other character.- assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 2048, messages: prompt(2500) }), 2048 - 128 - 1500,+ // Prompt estimate: 128 reserved + one token per ASCII character; two per other character.+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 2048, messages: prompt(1000) }), 2048 - 128 - 1500,`${label}: the answer gets what is left once the prompt is counted`);assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 2048, messages: prompt(3500) }), 0,`${label}: a prompt that leaves less than the answer floor is handed to the side panel`);- assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 1900, messages: prompt(2500) }), 272,+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 1900, messages: prompt(1000) }), 272,`${label}: an answer at or above the floor is still attempted`);- assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 1800, messages: prompt(2500) }), 0,+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 1800, messages: prompt(1000) }), 0,`${label}: just under the floor is handed off`);assert.equal(budget('short', 'translate', 100, { contextWindow: 2048, messages: prompt(10) }), 100,`${label}: a ceiling lower than the floor still wins`);@@ -52466,12 +52468,20 @@ test('inline card token budget follows the selection and never exceeds the proviconst cjk = (chars) => [{ role: 'system', content: 'x'.repeat(500) }, { role: 'user', content: '漢'.repeat(chars) }];assert.equal(budget('漢'.repeat(4000), 'translate', 16_000, { contextWindow: 4096, messages: cjk(4000) }), 0,`${label}: a 4,000-character CJK selection does not fit a 4k window and is handed off`);- assert.equal(budget('漢'.repeat(1000), 'translate', 16_000, { contextWindow: 4096, messages: cjk(1000) }), 4096 - 128 - 250 - 2000,+ assert.equal(budget('漢'.repeat(1000), 'translate', 16_000, { contextWindow: 4096, messages: cjk(1000) }), 4096 - 128 - 500 - 2000,`${label}: a shorter one gets the window minus a two-token-per-character prompt`);assert.equal(budget('漢'.repeat(4000), 'translate', 16_000, { contextWindow: 131_072, messages: cjk(4000) }), 8000,`${label}: a large window still allows the full answer`);assert.equal(budget('Привет'.repeat(300), 'translate', 16_000, { contextWindow: 4096, messages: [{ role: 'user', content: 'Привет'.repeat(300) }] }), 4096 - 128 - 3600,`${label}: Cyrillic is counted as non-Latin text too`);+ // High-entropy ASCII (hashes, minified code, identifiers) is not prose: a token per character.+ const hashes = '0123456789abcdef'.repeat(250);+ assert.equal(budget(hashes, 'translate', 16_000, { contextWindow: 4096, messages: [{ role: 'user', content: hashes }] }), 0,+ `${label}: 4,000 characters of hex do not fit a 4k window, so the card hands them to the side panel`);+ assert.equal(budget(hashes, 'translate', 16_000, { contextWindow: 8192, messages: [{ role: 'user', content: hashes }] }), 8192 - 128 - 4000,+ `${label}: an 8k window keeps back a token per character of the prompt`);+ assert.equal(budget('é'.repeat(300), 'translate', 16_000, { contextWindow: 1000, messages: [{ role: 'user', content: 'é'.repeat(300) }] }), 1000 - 128 - 600,+ `${label}: accented letters can take two tokens, so they are counted as such`);assert.equal(budget('😀'.repeat(10), 'translate', 16_000, { contextWindow: 500, messages: [{ role: 'user', content: '😀'.repeat(10) }] }), 500 - 128 - 20,`${label}: an astral character counts once, not as two UTF-16 units`);assert.equal(budget(longText, 'translate', 16_000, { contextWindow: undefined, messages: prompt(3500) }), 6000,