Skip to content

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

Keep a long cut-off card answer resumable; surface the allowance notice

Claudeb3a660b63dad
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f42f3bcca..3cb4231d6 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.
+- 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.
- 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 a7d7e12a1..0067ed459 100644
--- a/docs/architecture.md
+++ b/docs/architecture.md
@@ -546,11 +546,11 @@ cut-off, the error), and the card shows it under that answer, since the next car
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 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
+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
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
`partialContent`, whose text is then shown), so a long remainder never passes as complete
-and can be continued by asking. Each call carries a job id, and a card
+and can be continued by asking. The call that reaches the cost allowance says so under the reply, whether it finished or was recovered from that error, so the next continuation does not fail unexplained. Each call carries a job id, and a card
that is closed, retried or retargeted sends `WB_SELECTION_SHORTCUT_INLINE_CANCEL` so the
background aborts the paid request it no longer needs (a page that unloads mid-answer sends the
same cancel from `pagehide`, since it can no longer close the card itself; for the in-browser WebGPU models the
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 8c19423d0..5c92c9dc1 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -26282,6 +26282,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
content: Agent._stripReasoningTags(partial),
toolCalls: [],
finishReason: String(error.incompleteReason),
+ // The call that was cut off can also be the one that reached the cost allowance.
+ ...(error.costAllowanceMessage ? { costAllowanceMessage: error.costAllowanceMessage } : {}),
};
}
const latencyMs = Date.now() - startedAt;
@@ -26312,9 +26314,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const displayText = repairAssistantDisplayText(displayContent);
// A reply that ran into the output limit must not read as finished. A selection
// card's "Continue" lands here too, and the remainder can be cut off again.
- return phase === 'response_only' && displayText && outputWasCutOff(result)
- ? `${displayText}${RESPONSE_CUT_OFF_NOTICE}`
- : displayText;
+ if (phase !== 'response_only' || !displayText) return displayText;
+ const answer = outputWasCutOff(result) ? `${displayText}${RESPONSE_CUT_OFF_NOTICE}` : displayText;
+ // The call that reached the cost allowance says so, so the next one does not fail unexplained.
+ return result?.costAllowanceMessage ? `${answer}\n\n${result.costAllowanceMessage}` : answer;
}
_consumeContextOnlyAbort(tabId, messages, onUpdate) {
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.js
index 5f9c87275..16d2cc994 100644
--- a/src/chrome/src/background.js
+++ b/src/chrome/src/background.js
@@ -1865,7 +1865,14 @@ function queueSelectionShortcutPrompt(msg, tab, sendResponse, { panelOpened = fa
const resumesCutOffAnswer = selectionAction !== 'custom'
&& typeof msg.continueFrom === 'string' && msg.continueFrom.trim() !== '';
const text = resumesCutOffAnswer
- ? buildContinuationSelectionPrompt(msg.selectionText, selectionAction, msg.continueFrom.slice(-INLINE_CONTINUE_PARTIAL_MAX_CHARS), msg.language)
+ ? buildContinuationSelectionPrompt(
+ msg.selectionText,
+ 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 },
+ )
: includePageContext
? buildFullContextSelectionPrompt(selectionText, msg.question)
: buildSelectionPrompt(
diff --git a/src/chrome/src/context-menu-storage.js b/src/chrome/src/context-menu-storage.js
index 06214c5eb..f665595d3 100644
--- a/src/chrome/src/context-menu-storage.js
+++ b/src/chrome/src/context-menu-storage.js
@@ -424,7 +424,31 @@ const INLINE_CONTINUATION_INSTRUCTIONS = Object.freeze({
proofread: 'Proofread this selected text, tying every claimed error to exact selected wording and distinguishing actual errors from optional style suggestions. If the earlier text stopped inside the corrected version, carry that version on rather than starting it again.',
});
-export function buildContinuationSelectionPrompt(selectionText, action, partialAnswer, language = '') {
+// 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.
+const INLINE_CONTINUATION_REPLY_TOKENS = 2048;
+
+// The end of `text` that fits `budget` tokens under the same worst-case estimate.
+function tailWithinTokens(text, budget) {
+ 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;
+ if (used + cost > budget) break;
+ used += cost;
+ start -= 1;
+ }
+ return chars.slice(start).join('');
+}
+
+export function buildContinuationSelectionPrompt(selectionText, action, partialAnswer, language = '', { contextWindow } = {}) {
const actionId = String(action || '').trim();
const label = INLINE_FOLLOW_UP_LABELS[actionId];
const selection = String(selectionText || '').trim();
@@ -434,11 +458,31 @@ export function buildContinuationSelectionPrompt(selectionText, action, partialA
? `${INLINE_CONTINUATION_INSTRUCTIONS[actionId]}${responseLanguageInstruction(language)}`
: selectionActionInstruction(actionId, '', language);
if (!instruction) return '';
- const kept = partial.length > INLINE_CONTINUE_PARTIAL_MAX_CHARS
- ? `…${partial.slice(-INLINE_CONTINUE_PARTIAL_MAX_CHARS)}`
- : partial;
- const withPartial = `${selection}\n\n[Unfinished ${label} of the text above. It stops partway and may contain mistakes.]\n${kept}`;
- return wrapSelectedPageText(withPartial, `${instruction}${INLINE_CONTINUE_NOTE}`, SELECTION_ONLY_SOURCE_GROUNDING);
+ const overCap = partial.length > INLINE_CONTINUE_PARTIAL_MAX_CHARS;
+ let body = overCap ? partial.slice(-INLINE_CONTINUE_PARTIAL_MAX_CHARS) : partial;
+ let trimmed = overCap;
+ const head = `${selection}\n\n[Unfinished ${label} of the text above. It stops partway and may contain mistakes.]\n`;
+ const trusted = `${instruction}${INLINE_CONTINUE_NOTE}`;
+ // The card keeps the whole unfinished answer to show, but a small context window cannot take
+ // 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;
+ }
+ }
+ }
+ return wrapSelectedPageText(`${head}${trimmed ? '…' : ''}${body}`, trusted, SELECTION_ONLY_SOURCE_GROUNDING);
}
const CONTEXT_MENU_PENDING_PREFIX = 'contextMenuPrompt:';
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index a3b4793cf..e649937b4 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -23423,6 +23423,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
content: Agent._stripReasoningTags(partial),
toolCalls: [],
finishReason: String(error.incompleteReason),
+ // The call that was cut off can also be the one that reached the cost allowance.
+ ...(error.costAllowanceMessage ? { costAllowanceMessage: error.costAllowanceMessage } : {}),
};
}
const latencyMs = Date.now() - startedAt;
@@ -23453,9 +23455,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const displayText = repairAssistantDisplayText(displayContent);
// A reply that ran into the output limit must not read as finished. A selection
// card's "Continue" lands here too, and the remainder can be cut off again.
- return phase === 'response_only' && displayText && outputWasCutOff(result)
- ? `${displayText}${RESPONSE_CUT_OFF_NOTICE}`
- : displayText;
+ if (phase !== 'response_only' || !displayText) return displayText;
+ const answer = outputWasCutOff(result) ? `${displayText}${RESPONSE_CUT_OFF_NOTICE}` : displayText;
+ // The call that reached the cost allowance says so, so the next one does not fail unexplained.
+ return result?.costAllowanceMessage ? `${answer}\n\n${result.costAllowanceMessage}` : answer;
}
_consumeContextOnlyAbort(tabId, messages, onUpdate) {
diff --git a/src/firefox/src/background.js b/src/firefox/src/background.js
index 186635b2b..16f8c9daf 100644
--- a/src/firefox/src/background.js
+++ b/src/firefox/src/background.js
@@ -1617,7 +1617,14 @@ function queueFirefoxSelectionShortcutPrompt(msg, tab, sendResponse) {
const resumesCutOffAnswer = selectionAction !== 'custom'
&& typeof msg.continueFrom === 'string' && msg.continueFrom.trim() !== '';
const text = resumesCutOffAnswer
- ? buildContinuationSelectionPrompt(msg.selectionText, selectionAction, msg.continueFrom.slice(-INLINE_CONTINUE_PARTIAL_MAX_CHARS), msg.language)
+ ? buildContinuationSelectionPrompt(
+ msg.selectionText,
+ 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 },
+ )
: includePageContext
? buildFullContextSelectionPrompt(selectionText, msg.question)
: buildSelectionPrompt(
diff --git a/src/firefox/src/context-menu-storage.js b/src/firefox/src/context-menu-storage.js
index c992ecd8d..2bb293060 100644
--- a/src/firefox/src/context-menu-storage.js
+++ b/src/firefox/src/context-menu-storage.js
@@ -424,7 +424,31 @@ const INLINE_CONTINUATION_INSTRUCTIONS = Object.freeze({
proofread: 'Proofread this selected text, tying every claimed error to exact selected wording and distinguishing actual errors from optional style suggestions. If the earlier text stopped inside the corrected version, carry that version on rather than starting it again.',
});
-export function buildContinuationSelectionPrompt(selectionText, action, partialAnswer, language = '') {
+// 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.
+const INLINE_CONTINUATION_REPLY_TOKENS = 2048;
+
+// The end of `text` that fits `budget` tokens under the same worst-case estimate.
+function tailWithinTokens(text, budget) {
+ 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;
+ if (used + cost > budget) break;
+ used += cost;
+ start -= 1;
+ }
+ return chars.slice(start).join('');
+}
+
+export function buildContinuationSelectionPrompt(selectionText, action, partialAnswer, language = '', { contextWindow } = {}) {
const actionId = String(action || '').trim();
const label = INLINE_FOLLOW_UP_LABELS[actionId];
const selection = String(selectionText || '').trim();
@@ -434,11 +458,31 @@ export function buildContinuationSelectionPrompt(selectionText, action, partialA
? `${INLINE_CONTINUATION_INSTRUCTIONS[actionId]}${responseLanguageInstruction(language)}`
: selectionActionInstruction(actionId, '', language);
if (!instruction) return '';
- const kept = partial.length > INLINE_CONTINUE_PARTIAL_MAX_CHARS
- ? `…${partial.slice(-INLINE_CONTINUE_PARTIAL_MAX_CHARS)}`
- : partial;
- const withPartial = `${selection}\n\n[Unfinished ${label} of the text above. It stops partway and may contain mistakes.]\n${kept}`;
- return wrapSelectedPageText(withPartial, `${instruction}${INLINE_CONTINUE_NOTE}`, SELECTION_ONLY_SOURCE_GROUNDING);
+ const overCap = partial.length > INLINE_CONTINUE_PARTIAL_MAX_CHARS;
+ let body = overCap ? partial.slice(-INLINE_CONTINUE_PARTIAL_MAX_CHARS) : partial;
+ let trimmed = overCap;
+ const head = `${selection}\n\n[Unfinished ${label} of the text above. It stops partway and may contain mistakes.]\n`;
+ const trusted = `${instruction}${INLINE_CONTINUE_NOTE}`;
+ // The card keeps the whole unfinished answer to show, but a small context window cannot take
+ // 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;
+ }
+ }
+ }
+ return wrapSelectedPageText(`${head}${trimmed ? '…' : ''}${body}`, trusted, SELECTION_ONLY_SOURCE_GROUNDING);
}
const CONTEXT_MENU_PENDING_PREFIX = 'contextMenuPrompt:';
diff --git a/test/run.js b/test/run.js
index d6ad63d71..0b8d92a28 100644
--- a/test/run.js
+++ b/test/run.js
@@ -700,6 +700,7 @@ const {
capInlineAnswer: capInlineAnswerCh,
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,
buildContinuationSelectionPrompt: buildContinuationSelectionPromptCh,
INLINE_CONTINUE_PARTIAL_MAX_CHARS: INLINE_CONTINUE_PARTIAL_MAX_CHARS_CH,
buildFollowUpSelectionText: buildFollowUpSelectionTextCh,
@@ -729,6 +730,7 @@ const {
capInlineAnswer: capInlineAnswerFx,
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,
buildContinuationSelectionPrompt: buildContinuationSelectionPromptFx,
INLINE_CONTINUE_PARTIAL_MAX_CHARS: INLINE_CONTINUE_PARTIAL_MAX_CHARS_FX,
buildFollowUpSelectionText: buildFollowUpSelectionTextFx,
@@ -52262,7 +52264,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\(msg\.selectionText, selectionAction, msg\.continueFrom\.slice\(-INLINE_CONTINUE_PARTIAL_MAX_CHARS\), msg\.language\)/,
+ 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*\)/,
`${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 \} : \{\}\)/,
@@ -52272,6 +52274,88 @@ test('a cut-off card answer is resumed in the side panel, not run again', () =>
}
});
+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],
+ ]) {
+ // What the side panel sends: its own system prompt and scope note (about 2,500 characters) and the prompt.
+ const sidePanelRoom = (prompt, contextWindow) => fit({
+ contextWindow,
+ messages: [{ role: 'system', content: 's'.repeat(2600) }, { role: 'user', content: prompt }],
+ wanted: 8192,
+ });
+ const selection = 'The quick brown fox jumps over the lazy dog. '.repeat(20);
+ const ascii = `HEAD-${'a'.repeat(63_000)}-TAIL`;
+ const cjk = `开头-${'汉'.repeat(15_000)}-结尾`;
+ const emoji = `🙂${'😀'.repeat(20_000)}🙃`;
+
+ for (const contextWindow of [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)`);
+ const prompt = buildContinuation(selection, 'explain', partial, 'en', { contextWindow });
+ assert.ok(sidePanelRoom(prompt, contextWindow) >= 256, `${label}: a ${kind} answer trimmed for a ${contextWindow}-token window leaves room to answer: ${sidePanelRoom(prompt, contextWindow)}`);
+ assert.ok(prompt.length < whole.length, `${label}: ${kind} was shortened`);
+ assert.ok(block(prompt).includes(tail), `${label}: the end of the ${kind} answer, where it must pick up, is kept`);
+ assert.match(block(prompt), /\[Unfinished explanation of the text above\.[^\n]*\n…/, `${label}: ${kind} shows that the start was left out`);
+ assert.equal(block(prompt).includes(selection.trim()), true, `${label}: the selection itself is kept whole`);
+ assert.ok(wellFormed(prompt), `${label}: ${kind} is trimmed on a character boundary`);
+ if (contextWindow >= 16_384) {
+ assert.ok(sidePanelRoom(prompt, contextWindow) >= 2048, `${label}: a ${contextWindow}-token window keeps a generous reply for ${kind}: ${sidePanelRoom(prompt, contextWindow)}`);
+ }
+ assert.ok(prompt.length > 400, `${label}: ${kind} still carries a useful tail`);
+ }
+ }
+ // However the room falls against a character that takes several units, the cut never splits one.
+ for (let pad = 0; pad < 9; pad += 1) {
+ const trimmedEmoji = buildContinuation(`${selection}${'x'.repeat(pad)}`, 'explain', emoji, 'en', { contextWindow: 8192 });
+ assert.ok(wellFormed(trimmedEmoji), `${label}: with ${pad} more selected characters the trimmed text is still whole characters`);
+ assert.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 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`);
+ // A larger window takes more of the tail than a smaller one.
+ const small = buildContinuation(selection, 'translate', ascii, 'de', { contextWindow: 8192 });
+ const 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`);
+
+ // Nothing to trim, or nothing known to trim against: the whole text goes, exactly as before.
+ const plain = buildContinuation(selection, 'explain', ascii, 'en');
+ for (const contextWindow of [undefined, null, 0, -5, NaN, 'abc', 1_000_000]) {
+ assert.equal(normalizeCtx(buildContinuation(selection, 'explain', ascii, 'en', { contextWindow })), normalizeCtx(plain), `${label}: ${String(contextWindow)} is not a window to trim for`);
+ }
+ assert.equal(normalizeCtx(buildContinuation(selection, 'explain', ascii, 'en', {})), normalizeCtx(plain), `${label}: no options keep the whole text`);
+ const short = buildContinuation(selection, 'explain', 'A short unfinished answer.', 'en', { contextWindow: 8192 });
+ assert.match(block(short), /\n A short unfinished answer\.\n<\/untrusted_page_content>$|\nA short unfinished answer\.\n<\/untrusted_page_content>$/, `${label}: an answer that fits is not changed`);
+ assert.doesNotMatch(short, /…/, `${label}: and gets no ellipsis`);
+
+ // A selection that leaves no room at all is not made worse: the whole text goes and the run refuses it with a reason.
+ const huge = 'long words '.repeat(1200);
+ const 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 });
+ 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(
+ normalizeCtx(buildContinuationSelectionPromptCh('Same.', 'proofread', 'p'.repeat(40_000), 'en', { contextWindow: 8192 })),
+ normalizeCtx(buildContinuationSelectionPromptFx('Same.', 'proofread', 'p'.repeat(40_000), 'en', { contextWindow: 8192 })),
+ '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],
@@ -54415,6 +54499,27 @@ test('an OpenAI Responses call that ends at the output limit is still metered',
const under = await agent._chatWithCostAllowance(provider, messages, {}, agent._newCostRunState()).catch(error => error);
assert.equal('costAllowanceMessage' in under, false, `${label}: below the limit the error carries no notice`);
+ // The side-panel path that recovers the partial text says so too, so the next Continue does not fail unexplained.
+ const ALLOWANCE = 'Cloud cost allowance reached: total cloud/router usage is $5.00 against the $5.00 limit.';
+ const generate = (agentToRun, providerToRun, phase = 'response_only') => agentToRun._generateContextOnlyResponse(
+ 1, messages, providerToRun, agentToRun._newCostRunState(), null, { phase, step: 1 },
+ );
+ ({ agent, provider } = setup(incomplete({ input_tokens: 120, output_tokens: 800, total_tokens: 920 })));
+ agent._recordCostUsage = async () => ALLOWANCE;
+ const recoveredAtLimit = await generate(agent, provider);
+ assert.match(recoveredAtLimit, /^Half of it[\s\S]*output limit[\s\S]*\n\nCloud cost allowance reached: total cloud\/router usage is \$5\.00/, `${label}: a recovered partial that reached the allowance says so after the cut-off notice`);
+ assert.ok(recoveredAtLimit.endsWith(`\n\n${ALLOWANCE}`), `${label}: the allowance message closes the reply`);
+ ({ agent, provider } = setup(incomplete({ input_tokens: 120, output_tokens: 800, total_tokens: 920 })));
+ assert.doesNotMatch(await generate(agent, provider), /allowance/, `${label}: a recovered partial below the allowance carries no notice`);
+ // An ordinary completed reply that reaches the allowance says so as well; a recovery phase is left to its caller.
+ provider.chat = async () => ({ content: 'Whole translation', toolCalls: null, finishReason: 'stop' });
+ agent._recordCostUsage = async () => ALLOWANCE;
+ assert.equal(await generate(agent, provider), `Whole translation\n\n${ALLOWANCE}`, `${label}: a finished reply that reached the allowance says so`);
+ provider.chat = async () => ({ content: '{"final_text":"Recovered"}', toolCalls: null, finishReason: 'stop' });
+ assert.doesNotMatch(await generate(agent, provider, 'terminal_recovery'), /allowance/, `${label}: only the response-only phase appends it`);
+ provider.chat = async () => ({ content: '', toolCalls: null, finishReason: 'stop' });
+ assert.equal(await generate(agent, provider), '', `${label}: an empty reply keeps its empty-output handling`);
+
({ agent, provider, recorded } = setup(incomplete(null)));
await assert.rejects(agent._chatWithCostAllowance(provider, messages, {}, agent._newCostRunState()), /Responses incomplete/);
assert.equal(recorded.length, 0, `${label}: nothing reported, nothing recorded`);
@@ -54431,9 +54536,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] of [
- ['chrome', AgentCh, SELECTION_ONLY_SOURCE_GROUNDING_CH, fitAnswerToContextCh, SELECTION_TOO_LONG_FOR_CONTEXT_CH],
- ['firefox', AgentFx, SELECTION_ONLY_SOURCE_GROUNDING_FX, fitAnswerToContextFx, SELECTION_TOO_LONG_FOR_CONTEXT_FX],
+ 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],
]) {
// 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) }];
@@ -54443,12 +54548,13 @@ test('a side-panel selection reply is sized to the room the context window leave
assert.equal(fit({ contextWindow: 8192, messages: prompt(8000), wanted: 8192 }), 0, `${label}: a prompt that leaves under the floor is refused`);
assert.equal(fit({ contextWindow: 8192, messages: prompt(8000), wanted: 100 }), 0, `${label}: and so is one that cannot fit even a small ceiling`);
+ const systems = [];
const run = async ({ contextWindow, chars, maxOutputTokens = 8192, runOptions = { sourceGrounding: grounding }, phase = 'response_only' }) => {
const requested = [];
const provider = {
supportsTools: false, supportsVision: false, promptTier: 'full', contextWindow, maxOutputTokens,
model: 'test-model', name: 'test-provider',
- chat: async (_messages, options) => { requested.push(options.maxTokens); return { content: 'The rest.', toolCalls: null }; },
+ chat: async (sent, options) => { requested.push(options.maxTokens); systems.push(sent[0]); return { content: 'The rest.', toolCalls: null }; },
};
const agent = new AgentClass({ getActive: () => provider, getVisionProvider: async () => null });
agent._checkCostAllowance = async () => null;
@@ -54478,6 +54584,13 @@ test('a side-panel selection reply is sized to the room the context window leave
assert.equal(result.requested[0], 8192, `${label}: an unknown window is not guessed at`);
result = await run({ contextWindow: 8192, chars: 9000, phase: 'step_limit_recovery' });
assert.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];
+ 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`);
}
});