Skip to content

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

fix(selection): preserve bounded handoff context

Emre Sokulluba1f6ecefe07
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.js
index 06db1830a..556fc3b32 100644
--- a/src/chrome/src/background.js
+++ b/src/chrome/src/background.js
@@ -1809,7 +1809,10 @@ async function answerInlineSelection(msg) {
agent._newCostRunState(),
);
const { text, cut } = capInlineAnswer(String(result?.content || '').trim());
- if (!text) return { ok: false, error: 'The model returned an empty answer.' };
+ if (!text) {
+ const notice = inlineCostNotice(result);
+ return { ok: false, error: 'The model returned an empty answer.', ...(notice ? { notice } : {}) };
+ }
// 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);
diff --git a/src/chrome/src/content/selection-shortcut.js b/src/chrome/src/content/selection-shortcut.js
index 9d64ac05c..7a8793b70 100644
--- a/src/chrome/src/content/selection-shortcut.js
+++ b/src/chrome/src/content/selection-shortcut.js
@@ -931,8 +931,8 @@
parts.text.hidden = status !== 'ready';
parts.error.hidden = status !== 'error' || cutOffOnly;
parts.cut.hidden = !((status === 'ready' || cutOffOnly) && cardState.truncated);
- parts.notice.hidden = !((status === 'ready' || cutOffOnly) && cardState.notice);
- parts.notice.textContent = status === 'ready' || cutOffOnly ? cardState.notice : '';
+ parts.notice.hidden = !(status !== 'loading' && cardState.notice);
+ parts.notice.textContent = status !== 'loading' ? cardState.notice : '';
if (status === 'ready') renderAnswer(parts.text, cardState.text, kind !== 'translate');
if (status === 'error') {
parts.errorText.textContent = str('inlineFailed');
diff --git a/src/chrome/src/context-menu-storage.js b/src/chrome/src/context-menu-storage.js
index 0cc23bac5..d2517c0a1 100644
--- a/src/chrome/src/context-menu-storage.js
+++ b/src/chrome/src/context-menu-storage.js
@@ -420,9 +420,10 @@ export function buildFollowUpSelectionText(selectionText, priorAction, priorAnsw
// 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}`;
+ if (room > 0) {
+ const markerTokens = room > INLINE_FOLLOW_UP_OMITTED_TOKENS ? INLINE_FOLLOW_UP_OMITTED_TOKENS : 0;
+ const start = sliceWithinTokens(answer, room - markerTokens, false);
+ if (start.length < answer.length) answer = `${start}${markerTokens ? INLINE_FOLLOW_UP_OMITTED_NOTE : ''}`;
}
return `${head}${answer}`;
}
@@ -502,7 +503,7 @@ export function buildContinuationSelectionPrompt(selectionText, action, partialA
if (room !== null && room >= 0) {
// A zero room comes from the conservative reply reserve. Keep the last
// token so the side panel can still resume a selection that itself fits.
- const tail = sliceWithinTokens(body, Math.max(room, 1), true);
+ const tail = sliceWithinTokens(body, Math.max(room, 4), true);
if (tail.length < body.length) {
body = tail;
trimmed = true;
diff --git a/src/firefox/src/background.js b/src/firefox/src/background.js
index 0a80b9c37..902158d3c 100644
--- a/src/firefox/src/background.js
+++ b/src/firefox/src/background.js
@@ -1560,7 +1560,10 @@ async function answerInlineSelection(msg) {
agent._newCostRunState(),
);
const { text, cut } = capInlineAnswer(String(result?.content || '').trim());
- if (!text) return { ok: false, error: 'The model returned an empty answer.' };
+ if (!text) {
+ const notice = inlineCostNotice(result);
+ return { ok: false, error: 'The model returned an empty answer.', ...(notice ? { notice } : {}) };
+ }
// 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);
diff --git a/src/firefox/src/content/selection-shortcut.js b/src/firefox/src/content/selection-shortcut.js
index 807063710..638970293 100644
--- a/src/firefox/src/content/selection-shortcut.js
+++ b/src/firefox/src/content/selection-shortcut.js
@@ -931,8 +931,8 @@
parts.text.hidden = status !== 'ready';
parts.error.hidden = status !== 'error' || cutOffOnly;
parts.cut.hidden = !((status === 'ready' || cutOffOnly) && cardState.truncated);
- parts.notice.hidden = !((status === 'ready' || cutOffOnly) && cardState.notice);
- parts.notice.textContent = status === 'ready' || cutOffOnly ? cardState.notice : '';
+ parts.notice.hidden = !(status !== 'loading' && cardState.notice);
+ parts.notice.textContent = status !== 'loading' ? cardState.notice : '';
if (status === 'ready') renderAnswer(parts.text, cardState.text, kind !== 'translate');
if (status === 'error') {
parts.errorText.textContent = str('inlineFailed');
diff --git a/src/firefox/src/context-menu-storage.js b/src/firefox/src/context-menu-storage.js
index a090c08d2..4d7e50da4 100644
--- a/src/firefox/src/context-menu-storage.js
+++ b/src/firefox/src/context-menu-storage.js
@@ -420,9 +420,10 @@ export function buildFollowUpSelectionText(selectionText, priorAction, priorAnsw
// 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}`;
+ if (room > 0) {
+ const markerTokens = room > INLINE_FOLLOW_UP_OMITTED_TOKENS ? INLINE_FOLLOW_UP_OMITTED_TOKENS : 0;
+ const start = sliceWithinTokens(answer, room - markerTokens, false);
+ if (start.length < answer.length) answer = `${start}${markerTokens ? INLINE_FOLLOW_UP_OMITTED_NOTE : ''}`;
}
return `${head}${answer}`;
}
@@ -502,7 +503,7 @@ export function buildContinuationSelectionPrompt(selectionText, action, partialA
if (room !== null && room >= 0) {
// A zero room comes from the conservative reply reserve. Keep the last
// token so the side panel can still resume a selection that itself fits.
- const tail = sliceWithinTokens(body, Math.max(room, 1), true);
+ const tail = sliceWithinTokens(body, Math.max(room, 4), true);
if (tail.length < body.length) {
body = tail;
trimmed = true;
diff --git a/test/fixtures/run.mjs b/test/fixtures/run.mjs
index 3d73d1254..bf6794964 100644
--- a/test/fixtures/run.mjs
+++ b/test/fixtures/run.mjs
@@ -2565,6 +2565,23 @@ for (const [label, sourcePath, manualOpen] of [
}
});
+ test(`${label}: an empty card response preserves its cost allowance notice`, async (page) => {
+ await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen });
+ const notice = 'Cloud cost allowance reached: total cloud/router usage is $5.00 against the $5.00 limit.';
+ await page.evaluate((text) => {
+ window.__selectionInlineHandler = async () => ({
+ ok: false,
+ notice: text,
+ error: 'The model returned an empty answer.',
+ });
+ }, notice);
+ await openInlineCard(page, 'proofread');
+ const state = await waitForCardStatus(page, 'error');
+ if (state.cardNotice !== notice || state.cardTruncated || !state.cardError) {
+ throw new Error(`an empty response should show its allowance notice: ${JSON.stringify(state)}`);
+ }
+ });
+
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(() => {