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
Fit continuations and card follow-ups to a 4k context window
Claudeb5e94a62b885
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3cb4231d6..c77c440b5 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,7 +14,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. 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, four for anything else), so dense text such as minified code or hashes on a 4k local model is reported as too long for the model instead of being sent a request the backend rejects. Continuing a cut-off Proofread in the side panel no longer asks for the whole corrected version again alongside the request for only the remainder. Continuing a cut-off card answer, or asking a follow-up, in the side panel now sizes the reply to what the model's context window leaves, so a small local model is not sent a request it rejects; when nothing fits it says the selection is too long for the model instead of failing. A card answer is now held to a fixed 64,000-character limit that is enforced when it is produced and when the card receives it, so a follow-up or a continuation carries exactly what the card shows instead of an answer silently shortened by a token-ratio estimate; a longer one is cut and offered to the side panel. A selection too long for the model's context window is reported as an error in the card instead of being offered as a continuation the side panel would refuse as well. Continuing a long cut-off answer on a small-context model now sends the side panel only the end of it, trimmed to what the model's window leaves beside the selection, instead of the whole text, which left no room to reply and was refused as too long. A side-panel reply that reaches the cloud cost allowance (including a cut-off OpenAI Responses reply that is recovered) now says so under the reply instead of leaving the next continuation to fail unexplained.+- 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, four for anything else), so dense text such as minified code or hashes on a 4k local model is reported as too long for the model instead of being sent a request the backend rejects. Continuing a cut-off Proofread in the side panel no longer asks for the whole corrected version again alongside the request for only the remainder. Continuing a cut-off card answer, or asking a follow-up, in the side panel now sizes the reply to what the model's context window leaves, so a small local model is not sent a request it rejects; when nothing fits it says the selection is too long for the model instead of failing. A card answer is now held to a fixed 64,000-character limit that is enforced when it is produced and when the card receives it, so a follow-up or a continuation carries exactly what the card shows instead of an answer silently shortened by a token-ratio estimate; a longer one is cut and offered to the side panel. A selection too long for the model's context window is reported as an error in the card instead of being offered as a continuation the side panel would refuse as well. Continuing a long cut-off answer on a small-context model now sends the side panel only the end of it, and a follow-up question about a long card answer only the start of it, trimmed to what the model's window leaves beside the selection and the question, instead of the whole text, which left no room to reply and was refused as too long. WebBrain's own system prompt is now counted at the pace of prose rather than the worst case when this is estimated, so a 4k local model can continue or ask a follow-up about a moderately long answer from a short selection. A side-panel reply that reaches the cloud cost allowance (including a cut-off OpenAI Responses reply that is recovered) now says so under the reply instead of leaving the next continuation to fail unexplained.- 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 0067ed459..1959dd4b2 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -546,7 +546,7 @@ cut-off, the error), and the card shows it under that answer, since the next carfail with no warning. Continue sends the unfinished text as`continueFrom`; the background wraps it inside the selection's untrusted block (whole, up to themost a card holds, because the side panel shows that block to the user) with a trusted request to resume from where it stops, because running the action again-would meet the same output limit. The card keeps the whole unfinished answer to show, but once the active provider's context window is known the background sends only its tail (`buildContinuationSelectionPrompt`'s `contextWindow` option, marked with a leading ellipsis): it keeps what the window leaves after the selection, the side panel's own prompt (`SIDE_PANEL_SELECTION_SYSTEM_TOKENS`, which a test holds above the real prompt) and a reply of up to 2,048 tokens, or only the 256-token minimum when the window is tight, so a long answer produced on a small local model is not refused as too long when it is continued. A window that cannot take even the selection and that minimum keeps the whole text and is refused, with the reason, when it runs. The side-panel reply to a continuation or a card follow-up carries the earlier answer as well as the selection, and a selection-only run skips context management, so that reply is sized to the room the model's context window leaves (the same worst-case estimate, `fitAnswerToContext`) rather than asking for the provider's full output ceiling; when the window leaves no room it says the selection is too long for the model and sends nothing. A resumed Proofread gets its own instruction, without the ordinary request for one complete corrected version, so a model asked for only the remainder is not also asked to write the corrected text again. A side-panel reply that is cut off again says so (the+would meet the same output limit. The card keeps the whole unfinished answer to show, but once the active provider's context window is known the background sends only its tail (`buildContinuationSelectionPrompt`'s `contextWindow` option, marked with a leading ellipsis): it keeps what the window leaves after the rest of the prompt, the side panel's own prompt (`SIDE_PANEL_SELECTION_SYSTEM_TOKENS`, which a test holds above the real prompt) and a reply of up to 2,048 tokens, or only the 256-token minimum when the window is tight, so a long answer produced on a small local model is not refused as too long when it is continued. A card follow-up is sized the same way, but keeps the start of the earlier answer and says that the rest was left out (`buildFollowUpSelectionText`'s `contextWindow`, `question` and `language` options; a follow-up that brings the page as context manages its own context and is left alone). The estimate counts a system message, which is WebBrain's own prose and never page text, at a token per three characters instead of the worst case used for everything else, because otherwise the side panel's own prompt alone fills a 4k window. A window that cannot take the rest of the prompt and that minimum (about 2k) keeps the whole text and is refused, with the reason, when it runs. The side-panel reply to a continuation or a card follow-up carries the earlier answer as well as the selection, and a selection-only run skips context management, so that reply is sized to the room the model's context window leaves (the same worst-case estimate, `fitAnswerToContext`) rather than asking for the provider's full output ceiling; when the window leaves no room it says the selection is too long for the model and sends nothing. A resumed Proofread gets its own instruction, without the ordinary request for one complete corrected version, so a model asked for only the remainder is not also asked to write the corrected text again. A side-panel reply that is cut off again says so (theresponse-only path appends a notice when the provider reports a length finish, WebGPUreports `outputTruncated`, or OpenAI Responses raises the limit as an error carrying`partialContent`, whose text is then shown), so a long remainder never passes as completediff --git a/src/chrome/src/background.js b/src/chrome/src/background.jsindex 16d2cc994..72396467e 100644--- a/src/chrome/src/background.js+++ b/src/chrome/src/background.js@@ -1857,8 +1857,18 @@ function queueSelectionShortcutPrompt(msg, tab, sendResponse, { panelOpened = fa? SELECTION_CONTEXT_SOURCE_GROUNDING: SELECTION_ONLY_SOURCE_GROUNDING;// A follow-up typed in a page card carries that card's answer as context.+ // The model's context window is known only once the providers have loaded; until then the+ // run's own check decides.+ const contextWindow = providerManager.providers.size > 0 ? providerManager.getActive()?.contextWindow : undefined;const selectionText = selectionAction === 'custom'- ? buildFollowUpSelectionText(msg.selectionText, msg.priorAction, msg.priorAnswer)+ ? buildFollowUpSelectionText(+ msg.selectionText,+ msg.priorAction,+ msg.priorAnswer,+ // With the page as context the run manages its own context, so only a selection-only+ // follow-up is sized to the window.+ includePageContext ? {} : { contextWindow, question: msg.question, language: msg.language },+ ): msg.selectionText;// A cut-off card answer resumes where it stopped instead of running the action again,// which would meet the same output limit.@@ -1870,8 +1880,7 @@ function queueSelectionShortcutPrompt(msg, tab, sendResponse, { panelOpened = faselectionAction,msg.continueFrom.slice(-INLINE_CONTINUE_PARTIAL_MAX_CHARS),msg.language,- // Known only once the providers have loaded; until then the run's own check decides.- { contextWindow: providerManager.providers.size > 0 ? providerManager.getActive()?.contextWindow : undefined },+ { contextWindow },): includePageContext? buildFullContextSelectionPrompt(selectionText, msg.question)diff --git a/src/chrome/src/context-menu-storage.js b/src/chrome/src/context-menu-storage.jsindex f665595d3..299efba2f 100644--- a/src/chrome/src/context-menu-storage.js+++ b/src/chrome/src/context-menu-storage.js@@ -291,12 +291,20 @@ const INLINE_ASCII_TOKENS_PER_CHAR = 1;const INLINE_OTHER_TOKENS_PER_CHAR = 4;const INLINE_ASCII_RANGE_END = 0x80;const INLINE_PROMPT_TOKEN_RESERVE = 128;+const INLINE_SYSTEM_CHARS_PER_TOKEN = 3;const INLINE_MIN_ANSWER_TOKENS = 256;-function estimateInlinePromptTokens(messages) {+export function estimateInlinePromptTokens(messages) {let tokens = INLINE_PROMPT_TOKEN_RESERVE;for (const message of Array.isArray(messages) ? messages : []) {if (typeof message?.content !== 'string') continue;+ // A system message is WebBrain's own prose, never page text, so it is counted at the pace+ // of prose with room to spare instead of as a worst case; without that the side panel's+ // own prompt alone fills a 4k window.+ if (message.role === 'system') {+ tokens += message.content.length / INLINE_SYSTEM_CHARS_PER_TOKEN;+ continue;+ }for (const char of message.content) {tokens += char.codePointAt(0) < INLINE_ASCII_RANGE_END? INLINE_ASCII_TOKENS_PER_CHAR@@ -382,6 +390,8 @@ const INLINE_FOLLOW_UP_LABELS = Object.freeze({export const INLINE_ANSWER_MAX_CHARS = 64000;const INLINE_CARD_ANSWER_MAX_CHARS = INLINE_ANSWER_MAX_CHARS;export const INLINE_FOLLOW_UP_ANSWER_MAX_CHARS = INLINE_CARD_ANSWER_MAX_CHARS;+const INLINE_FOLLOW_UP_OMITTED_NOTE = "\n[The rest of the answer is left out to fit the model's context window.]";+const INLINE_FOLLOW_UP_OMITTED_TOKENS = INLINE_FOLLOW_UP_OMITTED_NOTE.length;export function capInlineAnswer(text) {const full = String(text || '');@@ -395,16 +405,26 @@ export function capInlineAnswer(text) {// A follow-up typed in a card continues in the side panel. The card's earlier// answer is model output derived from untrusted page text, so it travels inside// the selection's untrusted block, never in the trusted instruction before it.-export function buildFollowUpSelectionText(selectionText, priorAction, priorAnswer) {+export function buildFollowUpSelectionText(selectionText, priorAction, priorAnswer, { contextWindow, question = '', language = '' } = {}) {const selection = String(selectionText || '');const label = INLINE_FOLLOW_UP_LABELS[String(priorAction || '').trim()];const full = String(priorAnswer || '').trim();if (!label || !full) return selection;// A longer answer than a card can produce is shortened, and says so.- const answer = full.length > INLINE_FOLLOW_UP_ANSWER_MAX_CHARS+ let answer = full.length > INLINE_FOLLOW_UP_ANSWER_MAX_CHARS? `${full.slice(0, INLINE_FOLLOW_UP_ANSWER_MAX_CHARS)}\n[The answer continues beyond this point.]`: full;- return `${selection}\n\n[Earlier ${label} of the text above, shown to the user in WebBrain's page card. It may contain mistakes.]\n${answer}`;+ const head = `${selection}\n\n[Earlier ${label} of the text above, shown to the user in WebBrain's page card. It may contain mistakes.]\n`;+ // The card shows the whole answer, but a small context window cannot take all of it back beside+ // the selection and the question and still leave room to reply, so the model is sent its start.+ // A window that cannot take even the selection and the question keeps the whole answer and is+ // refused, with a reason, when it runs.+ const room = priorAnswerRoom(contextWindow, buildSelectionPrompt(head, 'custom', question, language, SELECTION_CONTEXT_SOURCE_GROUNDING));+ if (room > INLINE_FOLLOW_UP_OMITTED_TOKENS) {+ const start = sliceWithinTokens(answer, room - INLINE_FOLLOW_UP_OMITTED_TOKENS, false);+ if (start.length < answer.length) answer = `${start}${INLINE_FOLLOW_UP_OMITTED_NOTE}`;+ }+ return `${head}${answer}`;}// A card answer that hit the output limit is finished in the side panel by@@ -425,27 +445,38 @@ const INLINE_CONTINUATION_INSTRUCTIONS = Object.freeze({});// What the side panel adds to a selection run before the prompt: its own system prompt and-// scope note (about 2,500 characters, counted at a token each like everything else). Kept-// above the real length, and checked against it in the tests, so a continuation that is-// trimmed to fit here is never refused by the side panel's own context check.-export const SIDE_PANEL_SELECTION_SYSTEM_TOKENS = 3000;-// The room left for the rest of the answer when an unfinished one is trimmed to fit.+// scope note (about 2,500 characters, counted as a system message). Kept above the real+// estimate, and checked against it in the tests, so an earlier answer that is trimmed to fit+// here is never refused by the side panel's own context check.+export const SIDE_PANEL_SELECTION_SYSTEM_TOKENS = 1100;+// The room left for the reply when an earlier answer is trimmed to fit.const INLINE_CONTINUATION_REPLY_TOKENS = 2048;-// The end of `text` that fits `budget` tokens under the same worst-case estimate.-function tailWithinTokens(text, budget) {+// The tokens an earlier answer may take in a side-panel prompt whose other text is `frameText`:+// what the window leaves after that text, the side panel's own prompt and a reply (a generous one,+// or only the least a reply needs when the window is tight). Null when the window is not known.+function priorAnswerRoom(contextWindow, frameText) {+ const windowTokens = Math.floor(Number(contextWindow));+ if (!(windowTokens > 0)) return null;+ const fixedTokens = estimateInlinePromptTokens([{ role: 'user', content: frameText }]) + SIDE_PANEL_SELECTION_SYSTEM_TOKENS;+ const generous = windowTokens - fixedTokens - Math.min(INLINE_CONTINUATION_REPLY_TOKENS, Math.floor(windowTokens / 4));+ return generous >= INLINE_MIN_ANSWER_TOKENS ? generous : windowTokens - fixedTokens - INLINE_MIN_ANSWER_TOKENS;+}++// The end (or, with `fromEnd` false, the start) of `text` that fits `budget` tokens under the+// same worst-case estimate, cut on a character boundary.+function sliceWithinTokens(text, budget, fromEnd) {const chars = Array.from(text);let used = 0;- let start = chars.length;- while (start > 0) {- const cost = chars[start - 1].codePointAt(0) < INLINE_ASCII_RANGE_END- ? INLINE_ASCII_TOKENS_PER_CHAR- : INLINE_OTHER_TOKENS_PER_CHAR;+ let count = 0;+ while (count < chars.length) {+ const char = chars[fromEnd ? chars.length - 1 - count : count];+ const cost = char.codePointAt(0) < INLINE_ASCII_RANGE_END ? INLINE_ASCII_TOKENS_PER_CHAR : INLINE_OTHER_TOKENS_PER_CHAR;if (used + cost > budget) break;used += cost;- start -= 1;+ count += 1;}- return chars.slice(start).join('');+ return fromEnd ? chars.slice(chars.length - count).join('') : chars.slice(0, count).join('');}export function buildContinuationSelectionPrompt(selectionText, action, partialAnswer, language = '', { contextWindow } = {}) {@@ -467,19 +498,12 @@ export function buildContinuationSelectionPrompt(selectionText, action, partialA// all of it back beside the selection and still leave room to write the rest. It resumes from// the end, so only the end is sent, trimmed to what the window leaves. A window that cannot// even take the selection keeps the whole text and is refused, with a reason, when it runs.- const windowTokens = Math.floor(Number(contextWindow));- if (windowTokens > 0) {- const frame = wrapSelectedPageText(head, trusted, SELECTION_ONLY_SOURCE_GROUNDING);- const fixedTokens = estimateInlinePromptTokens([{ content: frame }]) + SIDE_PANEL_SELECTION_SYSTEM_TOKENS;- // Room for a generous reply first; a tight window gives up all but the least a reply needs.- let room = windowTokens - fixedTokens - Math.min(INLINE_CONTINUATION_REPLY_TOKENS, Math.floor(windowTokens / 4));- if (room < INLINE_MIN_ANSWER_TOKENS) room = windowTokens - fixedTokens - INLINE_MIN_ANSWER_TOKENS;- if (room > 0) {- const tail = tailWithinTokens(body, room);- if (tail.length < body.length) {- body = tail;- trimmed = true;- }+ const room = priorAnswerRoom(contextWindow, wrapSelectedPageText(head, trusted, SELECTION_ONLY_SOURCE_GROUNDING));+ if (room > 0) {+ const tail = sliceWithinTokens(body, room, true);+ if (tail.length < body.length) {+ body = tail;+ trimmed = true;}}return wrapSelectedPageText(`${head}${trimmed ? '…' : ''}${body}`, trusted, SELECTION_ONLY_SOURCE_GROUNDING);diff --git a/src/firefox/src/background.js b/src/firefox/src/background.jsindex 16f8c9daf..c5583f2cf 100644--- a/src/firefox/src/background.js+++ b/src/firefox/src/background.js@@ -1609,8 +1609,18 @@ function queueFirefoxSelectionShortcutPrompt(msg, tab, sendResponse) {? SELECTION_CONTEXT_SOURCE_GROUNDING: SELECTION_ONLY_SOURCE_GROUNDING;// A follow-up typed in a page card carries that card's answer as context.+ // The model's context window is known only once the providers have loaded; until then the+ // run's own check decides.+ const contextWindow = providerManager.providers.size > 0 ? providerManager.getActive()?.contextWindow : undefined;const selectionText = selectionAction === 'custom'- ? buildFollowUpSelectionText(msg.selectionText, msg.priorAction, msg.priorAnswer)+ ? buildFollowUpSelectionText(+ msg.selectionText,+ msg.priorAction,+ msg.priorAnswer,+ // With the page as context the run manages its own context, so only a selection-only+ // follow-up is sized to the window.+ includePageContext ? {} : { contextWindow, question: msg.question, language: msg.language },+ ): msg.selectionText;// A cut-off card answer resumes where it stopped instead of running the action again,// which would meet the same output limit.@@ -1622,8 +1632,7 @@ function queueFirefoxSelectionShortcutPrompt(msg, tab, sendResponse) {selectionAction,msg.continueFrom.slice(-INLINE_CONTINUE_PARTIAL_MAX_CHARS),msg.language,- // Known only once the providers have loaded; until then the run's own check decides.- { contextWindow: providerManager.providers.size > 0 ? providerManager.getActive()?.contextWindow : undefined },+ { contextWindow },): includePageContext? buildFullContextSelectionPrompt(selectionText, msg.question)diff --git a/src/firefox/src/context-menu-storage.js b/src/firefox/src/context-menu-storage.jsindex 2bb293060..cfb874daf 100644--- a/src/firefox/src/context-menu-storage.js+++ b/src/firefox/src/context-menu-storage.js@@ -291,12 +291,20 @@ const INLINE_ASCII_TOKENS_PER_CHAR = 1;const INLINE_OTHER_TOKENS_PER_CHAR = 4;const INLINE_ASCII_RANGE_END = 0x80;const INLINE_PROMPT_TOKEN_RESERVE = 128;+const INLINE_SYSTEM_CHARS_PER_TOKEN = 3;const INLINE_MIN_ANSWER_TOKENS = 256;-function estimateInlinePromptTokens(messages) {+export function estimateInlinePromptTokens(messages) {let tokens = INLINE_PROMPT_TOKEN_RESERVE;for (const message of Array.isArray(messages) ? messages : []) {if (typeof message?.content !== 'string') continue;+ // A system message is WebBrain's own prose, never page text, so it is counted at the pace+ // of prose with room to spare instead of as a worst case; without that the side panel's+ // own prompt alone fills a 4k window.+ if (message.role === 'system') {+ tokens += message.content.length / INLINE_SYSTEM_CHARS_PER_TOKEN;+ continue;+ }for (const char of message.content) {tokens += char.codePointAt(0) < INLINE_ASCII_RANGE_END? INLINE_ASCII_TOKENS_PER_CHAR@@ -382,6 +390,8 @@ const INLINE_FOLLOW_UP_LABELS = Object.freeze({export const INLINE_ANSWER_MAX_CHARS = 64000;const INLINE_CARD_ANSWER_MAX_CHARS = INLINE_ANSWER_MAX_CHARS;export const INLINE_FOLLOW_UP_ANSWER_MAX_CHARS = INLINE_CARD_ANSWER_MAX_CHARS;+const INLINE_FOLLOW_UP_OMITTED_NOTE = "\n[The rest of the answer is left out to fit the model's context window.]";+const INLINE_FOLLOW_UP_OMITTED_TOKENS = INLINE_FOLLOW_UP_OMITTED_NOTE.length;export function capInlineAnswer(text) {const full = String(text || '');@@ -395,16 +405,26 @@ export function capInlineAnswer(text) {// A follow-up typed in a card continues in the side panel. The card's earlier// answer is model output derived from untrusted page text, so it travels inside// the selection's untrusted block, never in the trusted instruction before it.-export function buildFollowUpSelectionText(selectionText, priorAction, priorAnswer) {+export function buildFollowUpSelectionText(selectionText, priorAction, priorAnswer, { contextWindow, question = '', language = '' } = {}) {const selection = String(selectionText || '');const label = INLINE_FOLLOW_UP_LABELS[String(priorAction || '').trim()];const full = String(priorAnswer || '').trim();if (!label || !full) return selection;// A longer answer than a card can produce is shortened, and says so.- const answer = full.length > INLINE_FOLLOW_UP_ANSWER_MAX_CHARS+ let answer = full.length > INLINE_FOLLOW_UP_ANSWER_MAX_CHARS? `${full.slice(0, INLINE_FOLLOW_UP_ANSWER_MAX_CHARS)}\n[The answer continues beyond this point.]`: full;- return `${selection}\n\n[Earlier ${label} of the text above, shown to the user in WebBrain's page card. It may contain mistakes.]\n${answer}`;+ const head = `${selection}\n\n[Earlier ${label} of the text above, shown to the user in WebBrain's page card. It may contain mistakes.]\n`;+ // The card shows the whole answer, but a small context window cannot take all of it back beside+ // the selection and the question and still leave room to reply, so the model is sent its start.+ // A window that cannot take even the selection and the question keeps the whole answer and is+ // refused, with a reason, when it runs.+ const room = priorAnswerRoom(contextWindow, buildSelectionPrompt(head, 'custom', question, language, SELECTION_CONTEXT_SOURCE_GROUNDING));+ if (room > INLINE_FOLLOW_UP_OMITTED_TOKENS) {+ const start = sliceWithinTokens(answer, room - INLINE_FOLLOW_UP_OMITTED_TOKENS, false);+ if (start.length < answer.length) answer = `${start}${INLINE_FOLLOW_UP_OMITTED_NOTE}`;+ }+ return `${head}${answer}`;}// A card answer that hit the output limit is finished in the side panel by@@ -425,27 +445,38 @@ const INLINE_CONTINUATION_INSTRUCTIONS = Object.freeze({});// What the side panel adds to a selection run before the prompt: its own system prompt and-// scope note (about 2,500 characters, counted at a token each like everything else). Kept-// above the real length, and checked against it in the tests, so a continuation that is-// trimmed to fit here is never refused by the side panel's own context check.-export const SIDE_PANEL_SELECTION_SYSTEM_TOKENS = 3000;-// The room left for the rest of the answer when an unfinished one is trimmed to fit.+// scope note (about 2,500 characters, counted as a system message). Kept above the real+// estimate, and checked against it in the tests, so an earlier answer that is trimmed to fit+// here is never refused by the side panel's own context check.+export const SIDE_PANEL_SELECTION_SYSTEM_TOKENS = 1100;+// The room left for the reply when an earlier answer is trimmed to fit.const INLINE_CONTINUATION_REPLY_TOKENS = 2048;-// The end of `text` that fits `budget` tokens under the same worst-case estimate.-function tailWithinTokens(text, budget) {+// The tokens an earlier answer may take in a side-panel prompt whose other text is `frameText`:+// what the window leaves after that text, the side panel's own prompt and a reply (a generous one,+// or only the least a reply needs when the window is tight). Null when the window is not known.+function priorAnswerRoom(contextWindow, frameText) {+ const windowTokens = Math.floor(Number(contextWindow));+ if (!(windowTokens > 0)) return null;+ const fixedTokens = estimateInlinePromptTokens([{ role: 'user', content: frameText }]) + SIDE_PANEL_SELECTION_SYSTEM_TOKENS;+ const generous = windowTokens - fixedTokens - Math.min(INLINE_CONTINUATION_REPLY_TOKENS, Math.floor(windowTokens / 4));+ return generous >= INLINE_MIN_ANSWER_TOKENS ? generous : windowTokens - fixedTokens - INLINE_MIN_ANSWER_TOKENS;+}++// The end (or, with `fromEnd` false, the start) of `text` that fits `budget` tokens under the+// same worst-case estimate, cut on a character boundary.+function sliceWithinTokens(text, budget, fromEnd) {const chars = Array.from(text);let used = 0;- let start = chars.length;- while (start > 0) {- const cost = chars[start - 1].codePointAt(0) < INLINE_ASCII_RANGE_END- ? INLINE_ASCII_TOKENS_PER_CHAR- : INLINE_OTHER_TOKENS_PER_CHAR;+ let count = 0;+ while (count < chars.length) {+ const char = chars[fromEnd ? chars.length - 1 - count : count];+ const cost = char.codePointAt(0) < INLINE_ASCII_RANGE_END ? INLINE_ASCII_TOKENS_PER_CHAR : INLINE_OTHER_TOKENS_PER_CHAR;if (used + cost > budget) break;used += cost;- start -= 1;+ count += 1;}- return chars.slice(start).join('');+ return fromEnd ? chars.slice(chars.length - count).join('') : chars.slice(0, count).join('');}export function buildContinuationSelectionPrompt(selectionText, action, partialAnswer, language = '', { contextWindow } = {}) {@@ -467,19 +498,12 @@ export function buildContinuationSelectionPrompt(selectionText, action, partialA// all of it back beside the selection and still leave room to write the rest. It resumes from// the end, so only the end is sent, trimmed to what the window leaves. A window that cannot// even take the selection keeps the whole text and is refused, with a reason, when it runs.- const windowTokens = Math.floor(Number(contextWindow));- if (windowTokens > 0) {- const frame = wrapSelectedPageText(head, trusted, SELECTION_ONLY_SOURCE_GROUNDING);- const fixedTokens = estimateInlinePromptTokens([{ content: frame }]) + SIDE_PANEL_SELECTION_SYSTEM_TOKENS;- // Room for a generous reply first; a tight window gives up all but the least a reply needs.- let room = windowTokens - fixedTokens - Math.min(INLINE_CONTINUATION_REPLY_TOKENS, Math.floor(windowTokens / 4));- if (room < INLINE_MIN_ANSWER_TOKENS) room = windowTokens - fixedTokens - INLINE_MIN_ANSWER_TOKENS;- if (room > 0) {- const tail = tailWithinTokens(body, room);- if (tail.length < body.length) {- body = tail;- trimmed = true;- }+ const room = priorAnswerRoom(contextWindow, wrapSelectedPageText(head, trusted, SELECTION_ONLY_SOURCE_GROUNDING));+ if (room > 0) {+ const tail = sliceWithinTokens(body, room, true);+ if (tail.length < body.length) {+ body = tail;+ trimmed = true;}}return wrapSelectedPageText(`${head}${trimmed ? '…' : ''}${body}`, trusted, SELECTION_ONLY_SOURCE_GROUNDING);diff --git a/test/run.js b/test/run.jsindex 0b8d92a28..0ba55977e 100644--- a/test/run.js+++ b/test/run.js@@ -701,6 +701,7 @@ const {INLINE_ANSWER_MAX_CHARS: INLINE_ANSWER_MAX_CHARS_CH,SELECTION_TOO_LONG_FOR_CONTEXT: SELECTION_TOO_LONG_FOR_CONTEXT_CH,SIDE_PANEL_SELECTION_SYSTEM_TOKENS: SIDE_PANEL_SELECTION_SYSTEM_TOKENS_CH,+ estimateInlinePromptTokens: estimateInlinePromptTokensCh,buildContinuationSelectionPrompt: buildContinuationSelectionPromptCh,INLINE_CONTINUE_PARTIAL_MAX_CHARS: INLINE_CONTINUE_PARTIAL_MAX_CHARS_CH,buildFollowUpSelectionText: buildFollowUpSelectionTextCh,@@ -731,6 +732,7 @@ const {INLINE_ANSWER_MAX_CHARS: INLINE_ANSWER_MAX_CHARS_FX,SELECTION_TOO_LONG_FOR_CONTEXT: SELECTION_TOO_LONG_FOR_CONTEXT_FX,SIDE_PANEL_SELECTION_SYSTEM_TOKENS: SIDE_PANEL_SELECTION_SYSTEM_TOKENS_FX,+ estimateInlinePromptTokens: estimateInlinePromptTokensFx,buildContinuationSelectionPrompt: buildContinuationSelectionPromptFx,INLINE_CONTINUE_PARTIAL_MAX_CHARS: INLINE_CONTINUE_PARTIAL_MAX_CHARS_FX,buildFollowUpSelectionText: buildFollowUpSelectionTextFx,@@ -52264,7 +52266,7 @@ test('a cut-off card answer is resumed in the side panel, not run again', () =>['firefox', 'src/firefox/src/background.js'],]) {const background = fs.readFileSync(path.join(ROOT, bgPath), 'utf8');- assert.match(background, /const resumesCutOffAnswer = selectionAction !== 'custom'\s*&& typeof msg\.continueFrom === 'string' && msg\.continueFrom\.trim\(\) !== '';\s*const text = resumesCutOffAnswer\s*\? buildContinuationSelectionPrompt\(\s*msg\.selectionText,\s*selectionAction,\s*msg\.continueFrom\.slice\(-INLINE_CONTINUE_PARTIAL_MAX_CHARS\),\s*msg\.language,\s*(?:\/\/[^\n]*\n\s*)*\{ contextWindow: providerManager\.providers\.size > 0 \? providerManager\.getActive\(\)\?\.contextWindow : undefined \},?\s*\)/,+ assert.match(background, /const resumesCutOffAnswer = selectionAction !== 'custom'\s*&& typeof msg\.continueFrom === 'string' && msg\.continueFrom\.trim\(\) !== '';\s*const text = resumesCutOffAnswer\s*\? buildContinuationSelectionPrompt\(\s*msg\.selectionText,\s*selectionAction,\s*msg\.continueFrom\.slice\(-INLINE_CONTINUE_PARTIAL_MAX_CHARS\),\s*msg\.language,\s*(?:\/\/[^\n]*\n\s*)*\{ contextWindow \},?\s*\)/,`${prefix}: the background resumes a cut-off answer for the card actions and bounds what it accepts`);const content = fs.readFileSync(path.join(ROOT, `src/${prefix}/src/content/selection-shortcut.js`), 'utf8');assert.match(content, /\.\.\.\(action !== 'custom' && extra\.continueFrom \? \{ continueFrom: extra\.continueFrom \} : \{\}\)/,@@ -52279,9 +52281,9 @@ const normalizeCtx = (prompt) => prompt.replace(/ctx-\d+-[a-z0-9]+/g, 'ctx-N');test('a long unfinished answer is trimmed to what the model window leaves, so Continue stays possible', () => {const wellFormed = (text) => /^(?:[^\uD800-\uDFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF])*$/.test(text);const block = (prompt) => prompt.slice(prompt.indexOf('<untrusted_page_content id="'));- for (const [label, buildContinuation, fit, reserve] of [- ['chrome', buildContinuationSelectionPromptCh, fitAnswerToContextCh, SIDE_PANEL_SELECTION_SYSTEM_TOKENS_CH],- ['firefox', buildContinuationSelectionPromptFx, fitAnswerToContextFx, SIDE_PANEL_SELECTION_SYSTEM_TOKENS_FX],+ for (const [label, buildContinuation, fit, reserve, estimate] of [+ ['chrome', buildContinuationSelectionPromptCh, fitAnswerToContextCh, SIDE_PANEL_SELECTION_SYSTEM_TOKENS_CH, estimateInlinePromptTokensCh],+ ['firefox', buildContinuationSelectionPromptFx, fitAnswerToContextFx, SIDE_PANEL_SELECTION_SYSTEM_TOKENS_FX, estimateInlinePromptTokensFx],]) {// What the side panel sends: its own system prompt and scope note (about 2,500 characters) and the prompt.const sidePanelRoom = (prompt, contextWindow) => fit({@@ -52294,7 +52296,7 @@ test('a long unfinished answer is trimmed to what the model window leaves, so Coconst cjk = `开头-${'汉'.repeat(15_000)}-结尾`;const emoji = `🙂${'😀'.repeat(20_000)}🙃`;- for (const contextWindow of [8192, 16_384, 32_768]) {+ for (const contextWindow of [4096, 8192, 16_384, 32_768]) {for (const [kind, partial, tail] of [['ASCII', ascii, '-TAIL'], ['CJK', cjk, '-结尾'], ['emoji', emoji, '🙃']]) {const whole = buildContinuation(selection, 'explain', partial, 'en');assert.equal(sidePanelRoom(whole, contextWindow), 0, `${label}: a ${kind} answer sent whole into a ${contextWindow}-token window leaves no room (the bug)`);@@ -52318,7 +52320,7 @@ test('a long unfinished answer is trimmed to what the model window leaves, so Coassert.ok(block(trimmedEmoji).includes('🙃'), `${label}: and still ends where the answer ended`);}// A selection that takes most of a tight window leaves only the least a reply needs, and the answer is still resumable.- const bulky = 'word '.repeat(600);+ const bulky = 'word '.repeat(800);const tight = buildContinuation(bulky, 'explain', ascii, 'en', { contextWindow: 8192 });assert.ok(sidePanelRoom(tight, 8192) >= 256 && sidePanelRoom(tight, 8192) < 1024, `${label}: a bulky selection keeps a short tail and the least reply room: ${sidePanelRoom(tight, 8192)}`);assert.ok(block(tight).includes('-TAIL') && block(tight).includes('…'), `${label}: and it is still the end of the answer`);@@ -52327,7 +52329,7 @@ test('a long unfinished answer is trimmed to what the model window leaves, so Coconst large = buildContinuation(selection, 'translate', ascii, 'de', { contextWindow: 32_768 });assert.ok(large.length > small.length * 2, `${label}: the room follows the window`);// The reserve really is what the side panel adds, and the reply room is never more than a quarter of the window.- assert.ok(reserve >= 2600, `${label}: the reserve covers the side panel's own prompt`);+ assert.ok(estimate([{ role: 'system', content: 's'.repeat(2600) }]) - estimate([]) <= reserve, `${label}: the reserve covers the side panel's own prompt`);// Nothing to trim, or nothing known to trim against: the whole text goes, exactly as before.const plain = buildContinuation(selection, 'explain', ascii, 'en');@@ -52344,10 +52346,16 @@ test('a long unfinished answer is trimmed to what the model window leaves, so Coconst refused = buildContinuation(huge, 'explain', ascii, 'en', { contextWindow: 8192 });assert.equal(normalizeCtx(refused), normalizeCtx(buildContinuation(huge, 'explain', ascii, 'en')), `${label}: a window the selection already fills trims nothing`);assert.equal(sidePanelRoom(refused, 8192), 0, `${label}: and is refused when it runs`);- // So is a 4k window, which the side panel's own prompt nearly fills before any selection is added.- const tiny = buildContinuation('Hi.', 'explain', ascii, 'en', { contextWindow: 4096 });+ // So is a window the side panel's own prompt and the request's wording already fill.+ const tiny = buildContinuation('Hi.', 'explain', ascii, 'en', { contextWindow: 2048 });assert.equal(normalizeCtx(tiny), normalizeCtx(buildContinuation('Hi.', 'explain', ascii, 'en')), `${label}: nothing is trimmed when not even the least reply fits`);- assert.equal(sidePanelRoom(tiny, 4096), 0, `${label}: and the run says why instead of calling the model`);+ assert.equal(sidePanelRoom(tiny, 2048), 0, `${label}: and the run says why instead of calling the model`);+ // A 4k local model, the usual llama.cpp default, can still resume a moderately long answer from a short selection.+ for (const length of [1000, 1500, 3000]) {+ const modest = buildContinuation('A short selection.', 'explain', `FIRST-${'m'.repeat(length)}-LAST`, 'en', { contextWindow: 4096 });+ assert.ok(sidePanelRoom(modest, 4096) >= 256, `${label}: a ${length}-character answer is resumable in a 4k window: ${sidePanelRoom(modest, 4096)}`);+ assert.ok(block(modest).includes('-LAST'), `${label}: and still ends where the answer ended`);+ }}assert.equal(normalizeCtx(buildContinuationSelectionPromptCh('Same.', 'proofread', 'p'.repeat(40_000), 'en', { contextWindow: 8192 })),@@ -52356,6 +52364,79 @@ test('a long unfinished answer is trimmed to what the model window leaves, so Co);});+test('a card follow-up sends the model as much of the earlier answer as its window leaves', () => {+ const wellFormed = (text) => /^(?:[^\uD800-\uDFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF])*$/.test(text);+ for (const [label, buildFollowUp, buildPrompt, grounding, fit] of [+ ['chrome', buildFollowUpSelectionTextCh, buildSelectionPromptCh, SELECTION_CONTEXT_SOURCE_GROUNDING_CH, fitAnswerToContextCh],+ ['firefox', buildFollowUpSelectionTextFx, buildSelectionPromptFx, SELECTION_CONTEXT_SOURCE_GROUNDING_FX, fitAnswerToContextFx],+ ]) {+ const selection = 'The quick brown fox jumps over the lazy dog. '.repeat(10);+ const question = 'Why does the fox jump?';+ // What the side panel sends for a follow-up: its own system prompt and the full request.+ const sent = (text, contextWindow) => fit({+ contextWindow,+ messages: [+ { role: 'system', content: 's'.repeat(2600) },+ { role: 'user', content: buildPrompt(text, 'custom', question, 'en', grounding) },+ ],+ wanted: 8192,+ });+ const options = (contextWindow) => ({ contextWindow, question, language: 'en' });+ for (const contextWindow of [4096, 8192, 32_768]) {+ for (const [kind, answer, start] of [+ ['ASCII', `FIRST-${'a'.repeat(60_000)}-LAST`, 'FIRST-'],+ ['CJK', `开头-${'汉'.repeat(12_000)}-结尾`, '开头-'],+ ['emoji', `🙂${'😀'.repeat(15_000)}🙃`, '🙂'],+ ]) {+ assert.equal(sent(buildFollowUp(selection, 'explain', answer), contextWindow), 0, `${label}: a ${kind} answer sent whole into a ${contextWindow}-token window leaves no room (the bug)`);+ const text = buildFollowUp(selection, 'explain', answer, options(contextWindow));+ assert.ok(sent(text, contextWindow) >= 256, `${label}: a ${kind} answer trimmed for a ${contextWindow}-token window leaves room to reply: ${sent(text, contextWindow)}`);+ assert.ok(text.includes(selection), `${label}: the selection is kept whole`);+ assert.ok(text.includes(start), `${label}: the start of the ${kind} answer is kept`);+ assert.match(text, /\n\[The rest of the answer is left out to fit the model's context window\.\]$/, `${label}: ${kind} says that the rest was left out`);+ assert.ok(wellFormed(text), `${label}: ${kind} is trimmed on a character boundary`);+ assert.ok(text.length > selection.length + (kind === 'ASCII' ? 300 : 60), `${label}: ${kind} still carries a useful part of the answer (${text.length})`);+ }+ }+ // A 4k local model asked about a moderately long answer from a short selection, which used to be refused.+ for (const length of [1500, 2500]) {+ const text = buildFollowUp('A short selection.', 'translate', `FIRST-${'m'.repeat(length)}-LAST`, options(4096));+ assert.ok(sent(text, 4096) >= 256, `${label}: a ${length}-character answer can be asked about in a 4k window: ${sent(text, 4096)}`);+ }+ // Every bit of room the window leaves is used, and the room follows the question.+ const small = buildFollowUp(selection, 'explain', 'a'.repeat(30_000), options(8192));+ const large = buildFollowUp(selection, 'explain', 'a'.repeat(30_000), options(32_768));+ assert.ok(large.length > small.length * 2, `${label}: the room follows the window`);+ const longQuestion = buildFollowUp(selection, 'explain', 'a'.repeat(30_000), { contextWindow: 8192, question: 'q'.repeat(1500), language: 'en' });+ assert.ok(longQuestion.length < small.length - 1000, `${label}: a long question leaves less of the answer`);++ // Nothing to trim, or nothing known to trim against: the whole answer goes, exactly as before.+ const plain = buildFollowUp(selection, 'explain', 'a'.repeat(30_000));+ for (const contextWindow of [undefined, null, 0, -5, NaN, 'abc', 1_000_000]) {+ assert.equal(buildFollowUp(selection, 'explain', 'a'.repeat(30_000), options(contextWindow)), plain, `${label}: ${String(contextWindow)} is not a window to trim for`);+ }+ assert.equal(buildFollowUp(selection, 'explain', 'a'.repeat(30_000), {}), plain);+ const short = buildFollowUp('A short selection.', 'explain', 'A short answer.', options(4096));+ assert.ok(short.endsWith('\nA short answer.'), `${label}: an answer that fits is not changed`);+ assert.doesNotMatch(short, /left out/, `${label}: and says nothing about leaving anything out`);+ // The page-wide limit still applies underneath.+ const capped = buildFollowUp(selection, 'explain', 'a'.repeat(70_000));+ assert.match(capped, /\[The answer continues beyond this point\.\]$/, `${label}: an answer past the card limit keeps its own note`);+ // A window the prompt already fills is not made worse: the whole answer goes and the run refuses it with the reason.+ const refused = buildFollowUp('A short selection.', 'explain', 'a'.repeat(3000), options(2048));+ assert.equal(refused, buildFollowUp('A short selection.', 'explain', 'a'.repeat(3000)), `${label}: nothing is trimmed when the window cannot take the rest of the prompt`);+ assert.equal(sent(refused, 2048), 0, `${label}: and the run says why instead of calling the model`);+ // No earlier answer, or an unknown action: the selection is returned untouched.+ assert.equal(buildFollowUp(selection, 'explain', '', options(4096)), selection);+ assert.equal(buildFollowUp(selection, 'bogus', 'An answer', options(4096)), selection);+ }+ assert.equal(+ buildFollowUpSelectionTextCh('Same.', 'explain', 'p'.repeat(40_000), { contextWindow: 8192, question: 'Why?', language: 'en' }),+ buildFollowUpSelectionTextFx('Same.', 'explain', 'p'.repeat(40_000), { contextWindow: 8192, question: 'Why?', language: 'en' }),+ 'Chrome and Firefox must trim the same way',+ );+});+test('inline card prompts keep the selection and any earlier answer inside the untrusted boundary', () => {for (const [label, actions, maxChars, buildMessages, buildFollowUp, buildSelectionPrompt, grounding] of [['chrome', INLINE_SELECTION_ACTIONS_CH, INLINE_SELECTION_MAX_CHARS_CH, buildInlineSelectionMessagesCh, buildFollowUpSelectionTextCh, buildSelectionPromptCh, SELECTION_CONTEXT_SOURCE_GROUNDING_CH],@@ -52443,7 +52524,7 @@ test('inline card answers are one tool-less, bounded model call behind validatedassert.match(content, /function submitFollowUp\(\) \{\s*const value = cardParts\?\.followUp\.value\.trim\(\);\s*if \(!value \|\| cardState\?\.status !== 'ready'\) return;[\s\S]*?submitSelection\('custom', value, '', \{\s*priorAction: cardState\.kind,\s*priorAnswer: cardState\.text,/, `${prefix}: a follow-up is only sent once the card has an answer to continue from`);assert.match(content, /cardParts\.send\.disabled = !\(cardParts\.followUp\.value\.trim\(\) && cardState\?\.status === 'ready'\);/, `${prefix}: the send button waits for an answer`);assert.match(content, /cardParts\.followUp\.addEventListener\('input', syncFollowUpSend\);[\s\S]*?syncFollowUpSend\(\);\s*positionCard\(\);/, `${prefix}: the send button follows the card's state`);- assert.match(background, /const selectionText = selectionAction === 'custom'\s*\? buildFollowUpSelectionText\(msg\.selectionText, msg\.priorAction, msg\.priorAnswer\)\s*: msg\.selectionText;/, `${prefix}: the background adds the earlier answer inside the selection, for custom questions only`);+ assert.match(background, /const contextWindow = providerManager\.providers\.size > 0 \? providerManager\.getActive\(\)\?\.contextWindow : undefined;\s*const selectionText = selectionAction === 'custom'\s*\? buildFollowUpSelectionText\(\s*msg\.selectionText,\s*msg\.priorAction,\s*msg\.priorAnswer,\s*(?:\/\/[^\n]*\n\s*)*includePageContext \? \{\} : \{ contextWindow, question: msg\.question, language: msg\.language \},?\s*\)\s*: msg\.selectionText;/, `${prefix}: the background adds the earlier answer inside the selection, for custom questions only`);assert.match(background, /if \(msg\?\.type !== 'WB_SELECTION_SHORTCUT_INLINE'\) return;\s*answerInlineSelection\(msg\)\.then\(sendResponse\);\s*return true;/, `${prefix}: the inline listener should answer asynchronously`);const start = background.indexOf('async function answerInlineSelection(msg) {');@@ -52642,7 +52723,7 @@ test('inline card token budget follows the selection and never exceeds the proviassert.equal(budget('x'.repeat(6000), 'translate', 'junk'), 4096, `${label}: a non-numeric ceiling is ignored`);// 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 prompt = (chars) => [{ role: 'user', 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(2500) }), 4096 - 3000 - 128,`${label}: a 4k window leaves the answer only what the prompt does not use`);@@ -52666,8 +52747,12 @@ test('inline card token budget follows the selection and never exceeds the proviassert.equal(budget('short', 'translate', 100, { contextWindow: 2048, messages: prompt(3500) }), 0,`${label}: but not when the prompt itself cannot fit`);+ // WebBrain's own system prompt is prose it wrote, not page text, so it is counted at the pace of prose.+ assert.equal(budget(longText, 'translate', 16_000, { contextWindow: 4096, messages: [{ role: 'system', content: 'x'.repeat(3000) }, { role: 'user', content: 'x'.repeat(1000) }] }), 4096 - 128 - 1000 - 1000,+ `${label}: a 3,000-character system prompt takes a third of that many tokens; the selection is still counted a token per character`);+// CJK text costs about a token per character or more, not half of one.- const cjk = (chars) => [{ role: 'system', content: 'x'.repeat(500) }, { role: 'user', content: '漢'.repeat(chars) }];+ const cjk = (chars) => [{ role: 'user', 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(500), 'translate', 16_000, { contextWindow: 4096, messages: cjk(500) }), 4096 - 128 - 500 - 2000,@@ -54536,9 +54621,9 @@ test('an OpenAI Responses call that ends at the output limit is still metered',});test('a side-panel selection reply is sized to the room the context window leaves', async () => {- for (const [label, AgentClass, grounding, fit, tooLong, SIDE_PANEL_SELECTION_SYSTEM_TOKENS] of [- ['chrome', AgentCh, SELECTION_ONLY_SOURCE_GROUNDING_CH, fitAnswerToContextCh, SELECTION_TOO_LONG_FOR_CONTEXT_CH, SIDE_PANEL_SELECTION_SYSTEM_TOKENS_CH],- ['firefox', AgentFx, SELECTION_ONLY_SOURCE_GROUNDING_FX, fitAnswerToContextFx, SELECTION_TOO_LONG_FOR_CONTEXT_FX, SIDE_PANEL_SELECTION_SYSTEM_TOKENS_FX],+ for (const [label, AgentClass, grounding, fit, tooLong, SIDE_PANEL_SELECTION_SYSTEM_TOKENS, estimate] of [+ ['chrome', AgentCh, SELECTION_ONLY_SOURCE_GROUNDING_CH, fitAnswerToContextCh, SELECTION_TOO_LONG_FOR_CONTEXT_CH, SIDE_PANEL_SELECTION_SYSTEM_TOKENS_CH, estimateInlinePromptTokensCh],+ ['firefox', AgentFx, SELECTION_ONLY_SOURCE_GROUNDING_FX, fitAnswerToContextFx, SELECTION_TOO_LONG_FOR_CONTEXT_FX, SIDE_PANEL_SELECTION_SYSTEM_TOKENS_FX, estimateInlinePromptTokensFx],]) {// The helper: an unknown window changes nothing, a roomy one allows the ceiling, a tight one is clamped, none refuses.const prompt = (chars) => [{ role: 'user', content: 'x'.repeat(chars) }];@@ -54586,11 +54671,11 @@ test('a side-panel selection reply is sized to the room the context window leaveassert.equal(result.requested[0], 8192, `${label}: only the reply itself is clamped, not a recovery phase`);// What the side panel adds before the prompt is what a trimmed continuation leaves room for.- const worstCaseTokens = (text) => [...text].reduce((sum, ch) => sum + (ch.codePointAt(0) < 0x80 ? 1 : 4), 0);const selectionSystem = systems[0];+ const systemTokens = estimate([selectionSystem]) - estimate([]);assert.equal(selectionSystem.role, 'system', `${label}: the side panel puts its own system prompt first`);- assert.ok(worstCaseTokens(selectionSystem.content) <= SIDE_PANEL_SELECTION_SYSTEM_TOKENS, `${label}: the side-panel prompt and scope note (${worstCaseTokens(selectionSystem.content)}) fit the room the trimming reserves for them`);- assert.ok(worstCaseTokens(selectionSystem.content) > SIDE_PANEL_SELECTION_SYSTEM_TOKENS / 2, `${label}: and the reserve is not wildly larger than what they take`);+ assert.ok(systemTokens <= SIDE_PANEL_SELECTION_SYSTEM_TOKENS, `${label}: the side-panel prompt and scope note (${systemTokens}) fit the room the trimming reserves for them`);+ assert.ok(systemTokens > SIDE_PANEL_SELECTION_SYSTEM_TOKENS / 2, `${label}: and the reserve is not wildly larger than what they take`);}});