Skip to content

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

Tell the card when its request reached the cost allowance

Claudef0d05c66c745
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9ee6162f6..a923dfe40 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.
+- 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.
- 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 b4dff819f..9c563a046 100644
--- a/docs/architecture.md
+++ b/docs/architecture.md
@@ -531,7 +531,10 @@ 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. Continue sends the unfinished text as
+of passing a cut-off translation off as complete. 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
would meet the same output limit. A side-panel reply that is cut off again says so (the
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 0135d6a10..c1c2dcfbc 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -8484,7 +8484,12 @@ export class Agent extends LoopDetector {
// A call that ends in a provider error can still have been billed (OpenAI Responses
// stopped at max_output_tokens): count what it reports before the error moves on.
if (error?.billedUsage) {
- try { await this._recordCostUsage(provider, error.billedUsage, costState); } catch { /* the original error matters more */ }
+ try {
+ // A call that is the one to reach the allowance says so on the error, as a
+ // completed call says so on its result, for callers that show it.
+ const stopped = await this._recordCostUsage(provider, error.billedUsage, costState);
+ if (stopped) error.costAllowanceMessage = stopped;
+ } catch { /* the original error matters more */ }
}
throw error;
} finally {
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.js
index 8f7e2a487..d731a6634 100644
--- a/src/chrome/src/background.js
+++ b/src/chrome/src/background.js
@@ -1770,6 +1770,11 @@ const INLINE_SELECTION_TIMEOUT_MS = 90_000;
const inlineSelectionJobs = new Map();
const INLINE_JOB_ID_PATTERN = /^[a-z0-9]{8,64}$/;
+// The allowance notice a call carries when it was the one to reach the limit.
+function inlineCostNotice(source) {
+ return typeof source?.costAllowanceMessage === 'string' ? source.costAllowanceMessage.slice(0, 300) : '';
+}
+
async function answerInlineSelection(msg) {
const messages = buildInlineSelectionMessages(msg.selectionText, msg.action, msg.language);
if (!messages) return { ok: false, error: 'Invalid selection request.' };
@@ -1803,14 +1808,19 @@ async function answerInlineSelection(msg) {
);
const text = String(result?.content || '').trim();
if (!text) return { ok: false, error: 'The model returned an empty answer.' };
- return inlineAnswerWasCut(result, maxTokens) ? { ok: true, text, truncated: true } : { ok: true, text };
+ // 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;
} 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 notice = inlineCostNotice(error);
return partial
- ? { ok: true, text: partial, truncated: true }
+ ? { ok: true, text: partial, truncated: true, ...(notice ? { notice } : {}) }
: { ok: false, truncated: true, error: 'The answer was cut off before any text was produced.' };
}
return { ok: false, error: String(error?.message || error).slice(0, 300) };
diff --git a/src/chrome/src/content/selection-shortcut.js b/src/chrome/src/content/selection-shortcut.js
index 14e5f2a15..cc5039545 100644
--- a/src/chrome/src/content/selection-shortcut.js
+++ b/src/chrome/src/content/selection-shortcut.js
@@ -441,6 +441,7 @@
display:flex; flex:none; flex-wrap:wrap; align-items:center; gap:6px 10px; margin-top:10px;
color:var(--muted); font-size:13px; line-height:1.4;
}
+ .card-notice { flex:none; margin-top:10px; color:var(--muted); font-size:13px; line-height:1.4; }
.card-continue {
padding:5px 11px; border:1px solid var(--border); border-radius:10px; background:transparent;
color:var(--text); font-size:13px; font-weight:550; cursor:pointer;
@@ -558,6 +559,7 @@
<span class="card-cut-text">This answer was cut off.</span>
<button class="card-continue" type="button">Continue in side panel</button>
</div>
+ <div class="card-notice" role="status" hidden></div>
<div class="followup">
<input class="followup-input" type="text" maxlength="2000" aria-label="Ask a follow-up" placeholder="Ask a follow-up">
<button class="followup-send" type="button" aria-label="Send follow-up" disabled>
@@ -591,6 +593,7 @@
cut: shadow.querySelector('.card-cut'),
cutText: shadow.querySelector('.card-cut-text'),
continueButton: shadow.querySelector('.card-continue'),
+ notice: shadow.querySelector('.card-notice'),
followUp: shadow.querySelector('.followup-input'),
send: shadow.querySelector('.followup-send'),
};
@@ -925,6 +928,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' && cardState.notice);
+ parts.notice.textContent = status === 'ready' ? cardState.notice : '';
if (status === 'ready') renderAnswer(parts.text, cardState.text, kind !== 'translate');
if (status === 'error') {
parts.errorText.textContent = str('inlineFailed');
@@ -971,6 +976,7 @@
quote: kind === 'translate' ? snapshot.text : '',
text: '',
truncated: false,
+ notice: '',
error: '',
};
cardParts.followUp.value = '';
@@ -1028,6 +1034,7 @@
cardState.status = 'loading';
cardState.text = '';
cardState.truncated = false;
+ cardState.notice = '';
cardState.error = '';
renderCard();
try {
@@ -1047,6 +1054,9 @@
}
cardState.status = 'ready';
cardState.text = answer;
+ // Something the person should know about the answer they are looking at, such as
+ // the cost allowance having been reached by this very request.
+ cardState.notice = typeof response?.notice === 'string' ? response.notice.slice(0, 300) : '';
} catch (error) {
if (requestId !== cardRequestId || !cardState) return;
cardJobId = '';
@@ -1325,6 +1335,7 @@
retryRect: cardParts && !cardParts.error.hidden ? rectOf('.card-retry') : null,
cardTruncated: cardState?.truncated === true,
cutNoticeText: cardParts && !cardParts.cut.hidden ? cardParts.cutText.textContent : '',
+ cardNotice: cardParts && !cardParts.notice.hidden ? cardParts.notice.textContent : '',
continueRect: cardParts && !cardParts.cut.hidden ? rectOf('.card-continue') : null,
continueLabel: cardParts && !cardParts.cut.hidden ? cardParts.continueButton.textContent : '',
followUpPlaceholder: cardParts?.followUp.placeholder || '',
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 3dd92326d..9ab673fa5 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -8206,7 +8206,12 @@ export class Agent extends LoopDetector {
// A call that ends in a provider error can still have been billed (OpenAI Responses
// stopped at max_output_tokens): count what it reports before the error moves on.
if (error?.billedUsage) {
- try { await this._recordCostUsage(provider, error.billedUsage, costState); } catch { /* the original error matters more */ }
+ try {
+ // A call that is the one to reach the allowance says so on the error, as a
+ // completed call says so on its result, for callers that show it.
+ const stopped = await this._recordCostUsage(provider, error.billedUsage, costState);
+ if (stopped) error.costAllowanceMessage = stopped;
+ } catch { /* the original error matters more */ }
}
throw error;
} finally {
diff --git a/src/firefox/src/background.js b/src/firefox/src/background.js
index 0ce08cd1c..ef95f1543 100644
--- a/src/firefox/src/background.js
+++ b/src/firefox/src/background.js
@@ -1521,6 +1521,11 @@ const INLINE_SELECTION_TIMEOUT_MS = 90_000;
const inlineSelectionJobs = new Map();
const INLINE_JOB_ID_PATTERN = /^[a-z0-9]{8,64}$/;
+// The allowance notice a call carries when it was the one to reach the limit.
+function inlineCostNotice(source) {
+ return typeof source?.costAllowanceMessage === 'string' ? source.costAllowanceMessage.slice(0, 300) : '';
+}
+
async function answerInlineSelection(msg) {
const messages = buildInlineSelectionMessages(msg.selectionText, msg.action, msg.language);
if (!messages) return { ok: false, error: 'Invalid selection request.' };
@@ -1554,14 +1559,19 @@ async function answerInlineSelection(msg) {
);
const text = String(result?.content || '').trim();
if (!text) return { ok: false, error: 'The model returned an empty answer.' };
- return inlineAnswerWasCut(result, maxTokens) ? { ok: true, text, truncated: true } : { ok: true, text };
+ // 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;
} 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 notice = inlineCostNotice(error);
return partial
- ? { ok: true, text: partial, truncated: true }
+ ? { ok: true, text: partial, truncated: true, ...(notice ? { notice } : {}) }
: { ok: false, truncated: true, error: 'The answer was cut off before any text was produced.' };
}
return { ok: false, error: String(error?.message || error).slice(0, 300) };
diff --git a/src/firefox/src/content/selection-shortcut.js b/src/firefox/src/content/selection-shortcut.js
index 14e5f2a15..cc5039545 100644
--- a/src/firefox/src/content/selection-shortcut.js
+++ b/src/firefox/src/content/selection-shortcut.js
@@ -441,6 +441,7 @@
display:flex; flex:none; flex-wrap:wrap; align-items:center; gap:6px 10px; margin-top:10px;
color:var(--muted); font-size:13px; line-height:1.4;
}
+ .card-notice { flex:none; margin-top:10px; color:var(--muted); font-size:13px; line-height:1.4; }
.card-continue {
padding:5px 11px; border:1px solid var(--border); border-radius:10px; background:transparent;
color:var(--text); font-size:13px; font-weight:550; cursor:pointer;
@@ -558,6 +559,7 @@
<span class="card-cut-text">This answer was cut off.</span>
<button class="card-continue" type="button">Continue in side panel</button>
</div>
+ <div class="card-notice" role="status" hidden></div>
<div class="followup">
<input class="followup-input" type="text" maxlength="2000" aria-label="Ask a follow-up" placeholder="Ask a follow-up">
<button class="followup-send" type="button" aria-label="Send follow-up" disabled>
@@ -591,6 +593,7 @@
cut: shadow.querySelector('.card-cut'),
cutText: shadow.querySelector('.card-cut-text'),
continueButton: shadow.querySelector('.card-continue'),
+ notice: shadow.querySelector('.card-notice'),
followUp: shadow.querySelector('.followup-input'),
send: shadow.querySelector('.followup-send'),
};
@@ -925,6 +928,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' && cardState.notice);
+ parts.notice.textContent = status === 'ready' ? cardState.notice : '';
if (status === 'ready') renderAnswer(parts.text, cardState.text, kind !== 'translate');
if (status === 'error') {
parts.errorText.textContent = str('inlineFailed');
@@ -971,6 +976,7 @@
quote: kind === 'translate' ? snapshot.text : '',
text: '',
truncated: false,
+ notice: '',
error: '',
};
cardParts.followUp.value = '';
@@ -1028,6 +1034,7 @@
cardState.status = 'loading';
cardState.text = '';
cardState.truncated = false;
+ cardState.notice = '';
cardState.error = '';
renderCard();
try {
@@ -1047,6 +1054,9 @@
}
cardState.status = 'ready';
cardState.text = answer;
+ // Something the person should know about the answer they are looking at, such as
+ // the cost allowance having been reached by this very request.
+ cardState.notice = typeof response?.notice === 'string' ? response.notice.slice(0, 300) : '';
} catch (error) {
if (requestId !== cardRequestId || !cardState) return;
cardJobId = '';
@@ -1325,6 +1335,7 @@
retryRect: cardParts && !cardParts.error.hidden ? rectOf('.card-retry') : null,
cardTruncated: cardState?.truncated === true,
cutNoticeText: cardParts && !cardParts.cut.hidden ? cardParts.cutText.textContent : '',
+ cardNotice: cardParts && !cardParts.notice.hidden ? cardParts.notice.textContent : '',
continueRect: cardParts && !cardParts.cut.hidden ? rectOf('.card-continue') : null,
continueLabel: cardParts && !cardParts.cut.hidden ? cardParts.continueButton.textContent : '',
followUpPlaceholder: cardParts?.followUp.placeholder || '',
diff --git a/test/fixtures/run.mjs b/test/fixtures/run.mjs
index 57f9779bb..1833b9ffd 100644
--- a/test/fixtures/run.mjs
+++ b/test/fixtures/run.mjs
@@ -2513,6 +2513,40 @@ for (const [label, sourcePath, manualOpen] of [
}
});
+ test(`${label}: a card answer says so when its request reached the cost allowance`, 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 (message) => {
+ if (message.action === 'explain') return { ok: true, text: 'Plain explanation', notice: 42 };
+ if (message.action === 'translate') return { ok: true, text: 'Long notice answer', notice: 'n'.repeat(500) };
+ return { ok: true, text: 'Answer from the last allowed call', notice: text };
+ };
+ }, notice);
+ await openInlineCard(page, 'proofread');
+ let state = await waitForCardStatus(page, 'ready');
+ if (state.cardText !== 'Answer from the last allowed call' || state.cardNotice !== notice || state.cardError) {
+ throw new Error(`the answer should be shown together with the allowance notice: ${JSON.stringify(state)}`);
+ }
+ // The notice belongs to that answer: another card without one shows none.
+ await page.keyboard.press('Escape');
+ await page.waitForFunction(() => !window.__webbrainSelectionShortcut.getState().cardVisible);
+ await selectFixtureText(page);
+ await openInlineCard(page, 'explain');
+ state = await waitForCardStatus(page, 'ready');
+ if (state.cardText !== 'Plain explanation' || state.cardNotice) {
+ throw new Error(`a card whose answer has no usable notice must not show one: ${JSON.stringify(state)}`);
+ }
+ await page.keyboard.press('Escape');
+ await page.waitForFunction(() => !window.__webbrainSelectionShortcut.getState().cardVisible);
+ await selectFixtureText(page);
+ await openInlineCard(page, 'translate');
+ state = await waitForCardStatus(page, 'ready');
+ if (state.cardText !== 'Long notice answer' || state.cardNotice.length !== 300) {
+ throw new Error(`a notice is bounded: ${JSON.stringify(state)}`);
+ }
+ });
+
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 7e453d7a0..cf4946ebb 100644
--- a/test/run.js
+++ b/test/run.js
@@ -52333,8 +52333,8 @@ test('inline card answers are one tool-less, bounded model call behind validated
assert.match(body, /agent\._chatWithCostAllowance\(\s*provider,\s*messages,\s*\{ maxTokens, temperature: 0, signal: controller\.signal \},\s*agent\._newCostRunState\(\),/, `${prefix}: the call is bounded, abortable and goes through the cost allowance`);
assert.match(body, /const maxTokens = inlineSelectionMaxTokens\(msg\.selectionText, msg\.action, provider\?\.maxOutputTokens, \{\s*contextWindow: provider\?\.contextWindow,\s*messages,\s*\}\);/, `${prefix}: the budget is sized to the selection, the provider's ceiling and the room its context window leaves after the prompt`);
assert.match(body, /if \(maxTokens === 0\) \{\s*return \{ ok: false, truncated: true, error: [^}]*context window[^}]*\};\s*\}\s*const result = await agent\._chatWithCostAllowance/, `${prefix}: a prompt that fills the context window is handed to the side panel without a model call`);
- assert.match(body, /inlineAnswerWasCut\(result, maxTokens\) \? \{ ok: true, text, truncated: true \}/, `${prefix}: a capped answer is flagged instead of passing as complete`);
- assert.match(body, /catch \(error\) \{\s*if \(inlineErrorWasCut\(error\)\) \{[\s\S]*?\{ ok: true, text: partial, truncated: true \}[\s\S]*?\{ ok: false, truncated: true, error:/, `${prefix}: a provider that reports the output limit as an error still ends in the cut-off state`);
+ assert.match(body, /inlineAnswerWasCut\(result, maxTokens\) \? \{ \.\.\.answer, truncated: true \} : answer/, `${prefix}: a capped answer is flagged instead of passing as complete`);
+ assert.match(body, /catch \(error\) \{\s*if \(inlineErrorWasCut\(error\)\) \{[\s\S]*?\{ ok: true, text: partial, truncated: true, \.\.\.\(notice \? \{ notice \} : \{\}\) \}[\s\S]*?\{ ok: false, truncated: true, error:/, `${prefix}: a provider that reports the output limit as an error still ends in the cut-off state`);
// A card that is closed, retried or retargeted cancels the paid call it no longer needs.
assert.match(body, /const jobId = typeof msg\.jobId === 'string' && INLINE_JOB_ID_PATTERN\.test\(msg\.jobId\) \? msg\.jobId : '';\s*if \(jobId\) inlineSelectionJobs\.set\(jobId, controller\);/, `${prefix}: only well-formed job ids are tracked`);
@@ -52347,6 +52347,81 @@ 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']) {
+ 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) {');
+ assert.notEqual(helperStart, -1, `${prefix}: the notice helper is missing`);
+ assert.notEqual(start, -1, `${prefix}: inline answer handler missing`);
+ 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: (result) => result?.finishReason === 'length',
+ inlineErrorWasCut: (error) => error?.incompleteReason === 'max_output_tokens',
+ 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' })));
+ };
+
+ assert.deepEqual(
+ await run({ chat: async () => ({ content: ' Bonjour ', costAllowanceMessage: NOTICE }) }),
+ { ok: true, text: 'Bonjour', notice: NOTICE },
+ `${prefix}: the request that reaches the allowance still returns its answer, with the notice`,
+ );
+ assert.deepEqual(
+ await run({ chat: async () => ({ content: 'Bonjour' }) }),
+ { ok: true, text: 'Bonjour' },
+ `${prefix}: an answer under the allowance carries no notice`,
+ );
+ assert.deepEqual(
+ await run({ chat: async () => ({ content: 'Bon', finishReason: 'length', costAllowanceMessage: NOTICE }) }),
+ { ok: true, text: 'Bon', notice: NOTICE, truncated: true },
+ `${prefix}: a capped answer keeps both the cut-off flag and the notice`,
+ );
+ assert.equal(
+ (await run({ chat: async () => ({ content: 'Bonjour', costAllowanceMessage: 'x'.repeat(900) }) })).notice.length,
+ 300,
+ `${prefix}: the notice is bounded`,
+ );
+ assert.deepEqual(
+ await run({ chat: async () => ({ content: 'Bonjour', costAllowanceMessage: 42 }) }),
+ { ok: true, text: 'Bonjour' },
+ `${prefix}: only a text notice is passed on, and a malformed one does not cost the person their answer`,
+ );
+ assert.deepEqual(
+ await run({
+ chat: async () => {
+ throw Object.assign(new Error('Responses incomplete'), {
+ incompleteReason: 'max_output_tokens', partialContent: 'Half of it', costAllowanceMessage: NOTICE,
+ });
+ },
+ }),
+ { ok: true, text: 'Half of it', truncated: true, notice: NOTICE },
+ `${prefix}: a cut-off reported as an error that reached the allowance keeps its partial text and the notice`,
+ );
+ assert.deepEqual(
+ await run({
+ chat: async () => {
+ throw Object.assign(new Error('Responses incomplete'), { incompleteReason: 'max_output_tokens', partialContent: 'Half of it' });
+ },
+ }),
+ { ok: true, text: 'Half of it', truncated: true },
+ `${prefix}: and without reaching it there is no notice`,
+ );
+ }
+});
+
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],
@@ -54197,6 +54272,16 @@ test('an OpenAI Responses call that ends at the output limit is still metered',
assert.match(text, /^Half of it/, `${label}: the partial text is still shown`);
assert.equal(recorded.length, 1, `${label}: and the recovered call is counted once`);
+ // A call that is the one to reach the allowance says so on its error, as a completed one does on its result.
+ ({ agent, provider, recorded } = setup(incomplete({ input_tokens: 120, output_tokens: 800, total_tokens: 920 })));
+ agent._recordCostUsage = async () => 'Cloud cost allowance reached: total cloud/router usage is $5.00 against the $5.00 limit.';
+ const reached = await agent._chatWithCostAllowance(provider, messages, {}, agent._newCostRunState()).catch(error => error);
+ assert.match(reached.costAllowanceMessage, /^Cloud cost allowance reached/, `${label}: the limit being reached by an incomplete call is reported on the error`);
+ assert.equal(reached.partialContent, 'Half of it', `${label}: and the partial text is still on it`);
+ ({ agent, provider, recorded } = setup(incomplete({ input_tokens: 120, output_tokens: 800, total_tokens: 920 })));
+ 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`);
+
({ 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`);