Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Hold a card answer to an enforced limit instead of a token-ratio estimate

Claudea77e2d9ccbd7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 01ce65739..e4e61d995 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. 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 is handed to the side panel on a 4k local 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.
+- 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 is handed to the side panel on a 4k local 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.
- 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.md
index 07a985ff3..72bc5c92c 100644
--- a/docs/architecture.md
+++ b/docs/architecture.md
@@ -531,12 +531,12 @@ still stopped at the cap (finish reason, Anthropic `stop_reason` or Responses
Responses reports the limit as an error, which the background treats the same way and
answers with whatever text the provider returned) the response
is flagged `truncated`, and the card says so and offers **Continue in side panel** instead
-of passing a cut-off translation off as complete. The call that reaches the configured cost allowance still returns its answer (the
+of passing a cut-off translation off as complete. An answer is held to 64,000 characters, a limit enforced rather than inferred from the token budget (tokens are not a fixed number of characters): the background cuts a longer one there (never inside a surrogate pair) and flags it `truncated`, and the card applies the same limit to whatever it receives, so a follow-up or a continuation carries exactly the text the card shows and nothing is silently shortened afterwards. The call that reaches the configured cost allowance still returns its answer (the
background passes the allowance's own message as `notice`, from the result or, for a Responses
cut-off, the error), and the card shows it under that answer, since the next card would otherwise
fail with no warning. Continue sends the unfinished text as
`continueFrom`; the background wraps it inside the selection's untrusted block (whole, up to the
-most a card can produce, 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
+most 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 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
response-only path appends a notice when the provider reports a length finish, WebGPU
reports `outputTruncated`, or OpenAI Responses raises the limit as an error carrying
@@ -577,7 +577,7 @@ click and queues the verified prompt with `panelOpened`, because a panel that is
up has not reported in yet and a second `open()` would close it. The card's
answer travels inside the selection's untrusted boundary, labelled as an earlier answer that
may contain mistakes and
-capped at 32,000 characters (the most a card can produce; a longer one is shortened and says so), so model output derived from page text never becomes trusted
+capped at 64,000 characters (the most a card is ever held to), so model output derived from page text never becomes trusted
instruction. The selection and the answer go to the configured provider like any other
model call.
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.js
index d731a6634..56a5567fd 100644
--- a/src/chrome/src/background.js
+++ b/src/chrome/src/background.js
@@ -75,6 +75,7 @@ import {
buildContextMenuPrompt,
buildContinuationSelectionPrompt,
INLINE_CONTINUE_PARTIAL_MAX_CHARS,
+ capInlineAnswer,
buildFollowUpSelectionText,
buildFullContextSelectionPrompt,
buildInlineSelectionMessages,
@@ -1806,18 +1807,18 @@ async function answerInlineSelection(msg) {
{ maxTokens, temperature: 0, signal: controller.signal },
agent._newCostRunState(),
);
- const text = String(result?.content || '').trim();
+ const { text, cut } = capInlineAnswer(String(result?.content || '').trim());
if (!text) return { ok: false, error: 'The model returned an empty answer.' };
// The call that reaches the cost allowance still returns its answer; the card is told
// so it can say the next one will not run.
const notice = inlineCostNotice(result);
const answer = { ok: true, text, ...(notice ? { notice } : {}) };
- return inlineAnswerWasCut(result, maxTokens) ? { ...answer, truncated: true } : answer;
+ return cut || inlineAnswerWasCut(result, maxTokens) ? { ...answer, truncated: true } : answer;
} catch (error) {
if (inlineErrorWasCut(error)) {
// The provider reported the output limit as an error. Show what it wrote, if
// anything, and let the card hand the rest to the side panel.
- const partial = String(error.partialContent || '').trim();
+ const partial = capInlineAnswer(String(error.partialContent || '').trim()).text;
const notice = inlineCostNotice(error);
return partial
? { ok: true, text: partial, truncated: true, ...(notice ? { notice } : {}) }
diff --git a/src/chrome/src/content/selection-shortcut.js b/src/chrome/src/content/selection-shortcut.js
index cc5039545..9fb851980 100644
--- a/src/chrome/src/content/selection-shortcut.js
+++ b/src/chrome/src/content/selection-shortcut.js
@@ -44,6 +44,9 @@
// Keep in step with INLINE_SELECTION_MAX_CHARS in context-menu-storage.js.
// Longer selections are read better in the side panel than in a small card.
const INLINE_MAX_CHARS = 4000;
+ // Keep in step with INLINE_ANSWER_MAX_CHARS in context-menu-storage.js. An answer is never
+ // held beyond it, so a follow-up or a continuation carries the card's text whole.
+ const INLINE_ANSWER_MAX_CHARS = 64000;
const INLINE_ACTIONS = Object.freeze(['translate', 'explain', 'proofread']);
const MAX_SELECTION_HIGHLIGHT_RECTS = 200;
const TRANSLATION_LANGUAGES = Object.freeze([
@@ -1047,11 +1050,18 @@
});
if (requestId !== cardRequestId || !cardState) return;
cardJobId = '';
- const answer = typeof response?.text === 'string' ? response.text.trim() : '';
+ let answer = typeof response?.text === 'string' ? response.text.trim() : '';
cardState.truncated = response?.truncated === true;
if (!response?.ok || !answer) {
throw new Error(typeof response?.error === 'string' && !cardState.truncated ? response.error : '');
}
+ if (answer.length > INLINE_ANSWER_MAX_CHARS) {
+ let end = INLINE_ANSWER_MAX_CHARS;
+ const last = answer.charCodeAt(end - 1);
+ if (last >= 0xd800 && last <= 0xdbff) end -= 1;
+ answer = answer.slice(0, end);
+ cardState.truncated = true;
+ }
cardState.status = 'ready';
cardState.text = answer;
// Something the person should know about the answer they are looking at, such as
diff --git a/src/chrome/src/context-menu-storage.js b/src/chrome/src/context-menu-storage.js
index 40f1d8d1b..2658a7247 100644
--- a/src/chrome/src/context-menu-storage.js
+++ b/src/chrome/src/context-menu-storage.js
@@ -372,12 +372,26 @@ const INLINE_FOLLOW_UP_LABELS = Object.freeze({
explain: 'explanation',
proofread: 'proofreading result',
});
-// The most a card answer can hold: 8,000 output tokens at four characters each. Both a
-// follow-up and a continuation carry the whole answer up to this, because the side panel
-// shows it to the user and a question may refer to any part of it.
-const INLINE_CARD_ANSWER_MAX_CHARS = 32000;
+// The most a card holds. Tokens are not a fixed number of characters (whitespace and long
+// vocabulary tokens run past the usual four), so the limit is enforced where the answer
+// is produced and where the card receives it, not derived from the 8,000-token budget: an
+// answer beyond it is cut here and flagged, so what the card shows is exactly what a
+// follow-up or a continuation carries to the side panel, never a silently shortened copy.
+// Eight characters per token leaves twice the room an ordinary answer needs. Keep in step
+// with INLINE_ANSWER_MAX_CHARS in the content script.
+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;
+export function capInlineAnswer(text) {
+ const full = String(text || '');
+ if (full.length <= INLINE_ANSWER_MAX_CHARS) return { text: full, cut: false };
+ let end = INLINE_ANSWER_MAX_CHARS;
+ const last = full.charCodeAt(end - 1);
+ if (last >= 0xd800 && last <= 0xdbff) end -= 1; // never end on half of a surrogate pair
+ return { text: full.slice(0, end), cut: true };
+}
+
// 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.
@@ -398,7 +412,7 @@ export function buildFollowUpSelectionText(selectionText, priorAction, priorAnsw
// same limit. The unfinished text is model output derived from untrusted page
// text, so it travels inside the selection's untrusted block. The side panel shows that
// block to the user, so the whole unfinished text is kept (the cap is the most a card
-// can produce, 8,000 tokens at four characters each) and only a longer one loses its start.
+// holds) and only a longer one, which a card never produces, loses its start.
export const INLINE_CONTINUE_PARTIAL_MAX_CHARS = INLINE_CARD_ANSWER_MAX_CHARS;
const INLINE_CONTINUE_NOTE = ' An earlier attempt at this stopped partway; the text it produced is included after the selection. Continue it from exactly where it stops and give only the missing remainder, without repeating anything already written.';
diff --git a/src/firefox/src/background.js b/src/firefox/src/background.js
index ef95f1543..3c68e527a 100644
--- a/src/firefox/src/background.js
+++ b/src/firefox/src/background.js
@@ -68,6 +68,7 @@ import {
buildContextMenuPrompt,
buildContinuationSelectionPrompt,
INLINE_CONTINUE_PARTIAL_MAX_CHARS,
+ capInlineAnswer,
buildFollowUpSelectionText,
buildFullContextSelectionPrompt,
buildInlineSelectionMessages,
@@ -1557,18 +1558,18 @@ async function answerInlineSelection(msg) {
{ maxTokens, temperature: 0, signal: controller.signal },
agent._newCostRunState(),
);
- const text = String(result?.content || '').trim();
+ const { text, cut } = capInlineAnswer(String(result?.content || '').trim());
if (!text) return { ok: false, error: 'The model returned an empty answer.' };
// The call that reaches the cost allowance still returns its answer; the card is told
// so it can say the next one will not run.
const notice = inlineCostNotice(result);
const answer = { ok: true, text, ...(notice ? { notice } : {}) };
- return inlineAnswerWasCut(result, maxTokens) ? { ...answer, truncated: true } : answer;
+ return cut || inlineAnswerWasCut(result, maxTokens) ? { ...answer, truncated: true } : answer;
} catch (error) {
if (inlineErrorWasCut(error)) {
// The provider reported the output limit as an error. Show what it wrote, if
// anything, and let the card hand the rest to the side panel.
- const partial = String(error.partialContent || '').trim();
+ const partial = capInlineAnswer(String(error.partialContent || '').trim()).text;
const notice = inlineCostNotice(error);
return partial
? { ok: true, text: partial, truncated: true, ...(notice ? { notice } : {}) }
diff --git a/src/firefox/src/content/selection-shortcut.js b/src/firefox/src/content/selection-shortcut.js
index cc5039545..9fb851980 100644
--- a/src/firefox/src/content/selection-shortcut.js
+++ b/src/firefox/src/content/selection-shortcut.js
@@ -44,6 +44,9 @@
// Keep in step with INLINE_SELECTION_MAX_CHARS in context-menu-storage.js.
// Longer selections are read better in the side panel than in a small card.
const INLINE_MAX_CHARS = 4000;
+ // Keep in step with INLINE_ANSWER_MAX_CHARS in context-menu-storage.js. An answer is never
+ // held beyond it, so a follow-up or a continuation carries the card's text whole.
+ const INLINE_ANSWER_MAX_CHARS = 64000;
const INLINE_ACTIONS = Object.freeze(['translate', 'explain', 'proofread']);
const MAX_SELECTION_HIGHLIGHT_RECTS = 200;
const TRANSLATION_LANGUAGES = Object.freeze([
@@ -1047,11 +1050,18 @@
});
if (requestId !== cardRequestId || !cardState) return;
cardJobId = '';
- const answer = typeof response?.text === 'string' ? response.text.trim() : '';
+ let answer = typeof response?.text === 'string' ? response.text.trim() : '';
cardState.truncated = response?.truncated === true;
if (!response?.ok || !answer) {
throw new Error(typeof response?.error === 'string' && !cardState.truncated ? response.error : '');
}
+ if (answer.length > INLINE_ANSWER_MAX_CHARS) {
+ let end = INLINE_ANSWER_MAX_CHARS;
+ const last = answer.charCodeAt(end - 1);
+ if (last >= 0xd800 && last <= 0xdbff) end -= 1;
+ answer = answer.slice(0, end);
+ cardState.truncated = true;
+ }
cardState.status = 'ready';
cardState.text = answer;
// Something the person should know about the answer they are looking at, such as
diff --git a/src/firefox/src/context-menu-storage.js b/src/firefox/src/context-menu-storage.js
index 99d2d9dbb..ecb654ada 100644
--- a/src/firefox/src/context-menu-storage.js
+++ b/src/firefox/src/context-menu-storage.js
@@ -372,12 +372,26 @@ const INLINE_FOLLOW_UP_LABELS = Object.freeze({
explain: 'explanation',
proofread: 'proofreading result',
});
-// The most a card answer can hold: 8,000 output tokens at four characters each. Both a
-// follow-up and a continuation carry the whole answer up to this, because the side panel
-// shows it to the user and a question may refer to any part of it.
-const INLINE_CARD_ANSWER_MAX_CHARS = 32000;
+// The most a card holds. Tokens are not a fixed number of characters (whitespace and long
+// vocabulary tokens run past the usual four), so the limit is enforced where the answer
+// is produced and where the card receives it, not derived from the 8,000-token budget: an
+// answer beyond it is cut here and flagged, so what the card shows is exactly what a
+// follow-up or a continuation carries to the side panel, never a silently shortened copy.
+// Eight characters per token leaves twice the room an ordinary answer needs. Keep in step
+// with INLINE_ANSWER_MAX_CHARS in the content script.
+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;
+export function capInlineAnswer(text) {
+ const full = String(text || '');
+ if (full.length <= INLINE_ANSWER_MAX_CHARS) return { text: full, cut: false };
+ let end = INLINE_ANSWER_MAX_CHARS;
+ const last = full.charCodeAt(end - 1);
+ if (last >= 0xd800 && last <= 0xdbff) end -= 1; // never end on half of a surrogate pair
+ return { text: full.slice(0, end), cut: true };
+}
+
// 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.
@@ -398,7 +412,7 @@ export function buildFollowUpSelectionText(selectionText, priorAction, priorAnsw
// same limit. The unfinished text is model output derived from untrusted page
// text, so it travels inside the selection's untrusted block. The side panel shows that
// block to the user, so the whole unfinished text is kept (the cap is the most a card
-// can produce, 8,000 tokens at four characters each) and only a longer one loses its start.
+// holds) and only a longer one, which a card never produces, loses its start.
export const INLINE_CONTINUE_PARTIAL_MAX_CHARS = INLINE_CARD_ANSWER_MAX_CHARS;
const INLINE_CONTINUE_NOTE = ' An earlier attempt at this stopped partway; the text it produced is included after the selection. Continue it from exactly where it stops and give only the missing remainder, without repeating anything already written.';
diff --git a/test/fixtures/run.mjs b/test/fixtures/run.mjs
index 1833b9ffd..584651b9d 100644
--- a/test/fixtures/run.mjs
+++ b/test/fixtures/run.mjs
@@ -2547,6 +2547,40 @@ for (const [label, sourcePath, manualOpen] of [
}
});
+ test(`${label}: an answer beyond the card limit is cut, flagged and carried whole to the side panel`, async (page) => {
+ await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen });
+ await page.evaluate(() => {
+ // 70,000 characters, as a background without the limit might hand back.
+ window.__selectionInlineHandler = async () => ({ ok: true, text: 'word '.repeat(14000) });
+ });
+ await openInlineCard(page, 'explain');
+ const state = await waitForCardStatus(page, 'ready');
+ if (state.cardText.length !== 64000 || !state.cardTruncated || !state.continueRect || !state.cutNoticeText) {
+ throw new Error(`an over-long answer should be held at the limit and offered to the side panel: ${JSON.stringify({ length: state.cardText.length, truncated: state.cardTruncated, continueRect: state.continueRect })}`);
+ }
+ await clickRectCenter(page, state.continueRect);
+ await page.waitForFunction(() => window.__selectionMessages.length === 1);
+ const message = await page.evaluate(() => ({
+ length: window.__selectionMessages[0].continueFrom.length,
+ same: window.__selectionMessages[0].continueFrom === ('word '.repeat(14000)).trim().slice(0, 64000),
+ }));
+ if (message.length !== 64000 || !message.same) {
+ throw new Error(`Continue should carry exactly the text the card shows: ${JSON.stringify(message)}`);
+ }
+ });
+
+ test(`${label}: cutting an over-long answer never leaves half of an emoji`, async (page) => {
+ await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen });
+ await page.evaluate(() => {
+ window.__selectionInlineHandler = async () => ({ ok: true, text: `${'x'.repeat(63999)}😀 and more` });
+ });
+ await openInlineCard(page, 'explain');
+ const state = await waitForCardStatus(page, 'ready');
+ if (state.cardText !== 'x'.repeat(63999) || !state.cardTruncated) {
+ throw new Error(`the cut should fall before the emoji, not inside it: ${JSON.stringify({ length: state.cardText.length, tail: state.cardText.slice(-3), truncated: state.cardTruncated })}`);
+ }
+ });
+
test(`${label}: an older Translate answer never replaces a newer one`, async (page) => {
await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen });
await page.evaluate(() => {
diff --git a/test/run.js b/test/run.js
index c17dd4682..c85de2e46 100644
--- a/test/run.js
+++ b/test/run.js
@@ -697,6 +697,8 @@ const {
inlineErrorWasCut: inlineErrorWasCutCh,
inlineSelectionMaxTokens: inlineSelectionMaxTokensCh,
fitAnswerToContext: fitAnswerToContextCh,
+ capInlineAnswer: capInlineAnswerCh,
+ INLINE_ANSWER_MAX_CHARS: INLINE_ANSWER_MAX_CHARS_CH,
SELECTION_TOO_LONG_FOR_CONTEXT: SELECTION_TOO_LONG_FOR_CONTEXT_CH,
buildContinuationSelectionPrompt: buildContinuationSelectionPromptCh,
INLINE_CONTINUE_PARTIAL_MAX_CHARS: INLINE_CONTINUE_PARTIAL_MAX_CHARS_CH,
@@ -724,6 +726,8 @@ const {
inlineErrorWasCut: inlineErrorWasCutFx,
inlineSelectionMaxTokens: inlineSelectionMaxTokensFx,
fitAnswerToContext: fitAnswerToContextFx,
+ capInlineAnswer: capInlineAnswerFx,
+ INLINE_ANSWER_MAX_CHARS: INLINE_ANSWER_MAX_CHARS_FX,
SELECTION_TOO_LONG_FOR_CONTEXT: SELECTION_TOO_LONG_FOR_CONTEXT_FX,
buildContinuationSelectionPrompt: buildContinuationSelectionPromptFx,
INLINE_CONTINUE_PARTIAL_MAX_CHARS: INLINE_CONTINUE_PARTIAL_MAX_CHARS_FX,
@@ -52198,7 +52202,7 @@ test('a cut-off card answer is resumed in the side panel, not run again', () =>
assert.equal((block.match(/<\/untrusted_page_content>/g) || []).length, 1, `${label}: the boundary closes exactly once`);
// The side panel shows this block, so a partial answer is kept whole up to the most a card can produce.
- assert.equal(maxChars, 32000, `${label}: the cap is the largest card answer (8,000 tokens at four characters)`);
+ assert.equal(maxChars, 64000, `${label}: the cap is the most a card holds, enforced when an answer is produced`);
const wholePartial = `FIRST-${'y'.repeat(11_000)}-LAST`;
const wholePrompt = buildContinuation('Some text.', 'translate', wholePartial, 'de');
assert.ok(wholePrompt.includes(wholePartial), `${label}: an unfinished answer well past the old 6,000-character cap is kept in full`);
@@ -52298,11 +52302,11 @@ test('inline card prompts keep the selection and any earlier answer inside the u
// The whole card answer reaches the side panel, up to the most a card can produce.
const wholeAnswer = `FIRST-${'y'.repeat(11_000)}-LAST`;
assert.equal(buildFollowUp('sel', 'translate', wholeAnswer).endsWith(`\n${wholeAnswer}`), true, `${label}: a long card answer is carried in full, end included`);
- assert.equal(label === 'chrome' ? INLINE_FOLLOW_UP_ANSWER_MAX_CHARS_CH : INLINE_FOLLOW_UP_ANSWER_MAX_CHARS_FX, 32000, `${label}: the follow-up cap is the largest card answer`);
- const oversized = buildFollowUp('sel', 'translate', 'x'.repeat(40000));
- assert.equal(oversized.length < 'sel'.length + 32300 + 120, true, `${label}: an answer beyond that is bounded`);
+ assert.equal(label === 'chrome' ? INLINE_FOLLOW_UP_ANSWER_MAX_CHARS_CH : INLINE_FOLLOW_UP_ANSWER_MAX_CHARS_FX, 64000, `${label}: the follow-up cap is the most a card holds`);
+ const oversized = buildFollowUp('sel', 'translate', 'x'.repeat(70000));
+ assert.equal(oversized.length < 'sel'.length + 64300 + 120, true, `${label}: an answer beyond that is bounded`);
assert.match(oversized, /\[The answer continues beyond this point\.\]$/, `${label}: and says it was shortened`);
- assert.doesNotMatch(buildFollowUp('sel', 'translate', 'x'.repeat(32000)), /continues beyond/, `${label}: an answer at the cap is not marked`);
+ assert.doesNotMatch(buildFollowUp('sel', 'translate', 'x'.repeat(64000)), /continues beyond/, `${label}: an answer at the cap is not marked`);
const prompt = buildSelectionPrompt(follow, 'custom', 'Why?', '', grounding);
const answerAt = prompt.indexOf('do evil');
assert.equal(answerAt > prompt.indexOf('<untrusted_page_content id="'), true, `${label}: the earlier answer must sit inside the untrusted block`);
@@ -52322,6 +52326,11 @@ test('inline card answers are one tool-less, bounded model call behind validated
storage.match(/export const INLINE_SELECTION_MAX_CHARS = (\d+);/)?.[1],
`${prefix}: the card limit must match the background's`,
);
+ assert.equal(
+ content.match(/const INLINE_ANSWER_MAX_CHARS = (\d+);/)?.[1],
+ storage.match(/export const INLINE_ANSWER_MAX_CHARS = (\d+);/)?.[1],
+ `${prefix}: the card and the background hold an answer to the same limit`,
+ );
const contentActions = content.match(/const INLINE_ACTIONS = Object\.freeze\(\[([^\]]*)\]\)/)?.[1].replace(/[\s']/g, '');
const storageActions = storage.match(/export const INLINE_SELECTION_ACTIONS = Object\.freeze\(\[([^\]]*)\]\)/)?.[1].replace(/[\s']/g, '');
assert.equal(contentActions, 'translate,explain,proofread');
@@ -52365,7 +52374,7 @@ test('inline card answers are one tool-less, bounded model call behind validated
test('inline card answers tell the card when that request reached the cost allowance', async () => {
const NOTICE = 'Cloud cost allowance reached: total cloud/router usage is $5.00 against the $5.00 limit. Stopping before further cloud/router model calls.';
- for (const prefix of ['src/chrome', 'src/firefox']) {
+ for (const [prefix, capAnswer] of [['src/chrome', capInlineAnswerCh], ['src/firefox', capInlineAnswerFx]]) {
const background = fs.readFileSync(path.join(ROOT, prefix, 'src/background.js'), 'utf8');
const helperStart = background.indexOf('function inlineCostNotice(source) {');
const start = background.indexOf('async function answerInlineSelection(msg) {');
@@ -52383,6 +52392,7 @@ test('inline card answers tell the card when that request reached the cost allow
inlineSelectionMaxTokens: () => 500,
inlineAnswerWasCut: (result) => result?.finishReason === 'length',
inlineErrorWasCut: (error) => error?.incompleteReason === 'max_output_tokens',
+ capInlineAnswer: capAnswer,
providerManager: { providers: { size: 1 }, getActive: () => ({}) },
agent: { _newCostRunState: () => ({}), _chatWithCostAllowance: chat },
});
@@ -52438,6 +52448,57 @@ test('inline card answers tell the card when that request reached the cost allow
}
});
+test('a card never holds more than its answer limit, so a follow-up or continuation carries it whole', async () => {
+ for (const [label, prefix, cap, maxChars] of [
+ ['chrome', 'src/chrome', capInlineAnswerCh, INLINE_ANSWER_MAX_CHARS_CH],
+ ['firefox', 'src/firefox', capInlineAnswerFx, INLINE_ANSWER_MAX_CHARS_FX],
+ ]) {
+ assert.equal(maxChars, 64000, `${label}: eight characters per token of the largest answer`);
+ assert.deepEqual(cap('x'.repeat(maxChars)), { text: 'x'.repeat(maxChars), cut: false }, `${label}: an answer at the limit is kept whole`);
+ const over = cap('x'.repeat(maxChars + 5000));
+ assert.equal(over.text.length, maxChars, `${label}: a longer one is cut at the limit`);
+ assert.equal(over.cut, true, `${label}: and says so`);
+ const pair = cap(`${'x'.repeat(maxChars - 1)}😀tail`);
+ assert.equal(pair.text, 'x'.repeat(maxChars - 1), `${label}: a cut never leaves half of a surrogate pair`);
+ assert.deepEqual(cap(undefined), { text: '', cut: false });
+
+ const background = fs.readFileSync(path.join(ROOT, prefix, 'src/background.js'), 'utf8');
+ const helperStart = background.indexOf('function inlineCostNotice(source) {');
+ const start = background.indexOf('async function answerInlineSelection(msg) {');
+ const source = background.slice(helperStart, background.indexOf('\n}\n', start) + 2);
+ const run = async (chat) => {
+ const context = vm.createContext({
+ AbortController, setTimeout, clearTimeout, String, Map,
+ INLINE_SELECTION_TIMEOUT_MS: 1000,
+ INLINE_JOB_ID_PATTERN: /^[a-z0-9]{8,64}$/,
+ inlineSelectionJobs: new Map(),
+ buildInlineSelectionMessages: () => [{ role: 'user', content: 'x' }],
+ inlineSelectionMaxTokens: () => 500,
+ inlineAnswerWasCut: () => false,
+ inlineErrorWasCut: (error) => error?.incompleteReason === 'max_output_tokens',
+ capInlineAnswer: cap,
+ providerManager: { providers: { size: 1 }, getActive: () => ({}) },
+ agent: { _newCostRunState: () => ({}), _chatWithCostAllowance: chat },
+ });
+ vm.runInContext(`${source}\nthis.answerInlineSelection = answerInlineSelection;`, context);
+ return JSON.parse(JSON.stringify(await context.answerInlineSelection({ selectionText: 'x', action: 'translate', language: 'fr' })));
+ };
+ let result = await run(async () => ({ content: 'y'.repeat(maxChars + 9000) }));
+ assert.equal(result.text.length, maxChars, `${label}: the background does not hand the card more than it holds`);
+ assert.equal(result.truncated, true, `${label}: and flags the answer as cut off so the rest can go to the side panel`);
+ result = await run(async () => ({ content: 'y'.repeat(maxChars) }));
+ assert.deepEqual([result.text.length, result.truncated], [maxChars, undefined], `${label}: an answer that fits is not flagged`);
+ result = await run(async () => {
+ throw Object.assign(new Error('Responses incomplete'), { incompleteReason: 'max_output_tokens', partialContent: 'z'.repeat(maxChars + 100) });
+ });
+ assert.equal(result.text.length, maxChars, `${label}: a partial answer from an output-limit error is bounded too`);
+ assert.equal(result.truncated, true);
+
+ const content = fs.readFileSync(path.join(ROOT, prefix, 'src/content/selection-shortcut.js'), 'utf8');
+ assert.match(content, /if \(answer\.length > INLINE_ANSWER_MAX_CHARS\) \{[\s\S]*?answer = answer\.slice\(0, end\);\s*cardState\.truncated = true;/, `${label}: the card enforces the limit itself on whatever it receives`);
+ }
+});
+
test('inline card token budget follows the selection and never exceeds the provider ceiling', () => {
for (const [label, min, max, budget] of [
['chrome', INLINE_ANSWER_MIN_TOKENS_CH, INLINE_ANSWER_MAX_TOKENS_CH, inlineSelectionMaxTokensCh],