Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
fix(newtab): round-71 review — the send-hold polling budget is tracked per tab
Claude5fda5e83e91b
diff --git a/src/chrome/src/ui/newtab-handoff.js b/src/chrome/src/ui/newtab-handoff.jsindex 3eb5df92b..d6534474a 100644--- a/src/chrome/src/ui/newtab-handoff.js+++ b/src/chrome/src/ui/newtab-handoff.js@@ -81,7 +81,7 @@ export function createNewtabHandoff({let consuming = false;let rerun = false;let retryTimer = null;- let retries = 0;+ const retries = new Map(); // tabId -> polls spent waiting for that tab's send// The Ask most recently handed to sendMessage, while its text may still be in// the composer waiting for the send's own checks to finish.// One per tab: a send in preflight on one tab must stay known while another tab is shown.@@ -131,18 +131,21 @@ export function createNewtabHandoff({return pendingSend.text.startsWith('/') && value === '' && (pendingSend.queued || !getIsProcessing());};- function retrySoon() {+ function retrySoon(tabId) {+ // Stop polling after the budget instead of forcing the send aside: a send that is still+ // running owns the composer, and putting the next task in beside it would be sent along+ // with it or duplicated when it settles. The waiting task stays in storage and is picked+ // up when that send settles (below) or when the panel is next triggered. The budget belongs+ // to the tab whose send is being waited for, so another tab's polling does not use it up.+ const polls = (retries.get(Number(tabId)) ?? 0) + 1;+ if (polls > NEWTAB_HANDOFF_SEND_HOLD_MAX_RETRIES) {+ retries.delete(Number(tabId));+ return;+ }+ retries.set(Number(tabId), polls);if (retryTimer) return;retryTimer = setTimeout(() => {retryTimer = null;- // Stop polling after the budget instead of forcing the send aside: a send that is still- // running owns the composer, and putting the next task in beside it would be sent along- // with it or duplicated when it settles. The waiting task stays in storage and is picked- // up when that send settles (below) or when the panel is next triggered.- if (++retries > NEWTAB_HANDOFF_SEND_HOLD_MAX_RETRIES) {- retries = 0;- return;- }consume();}, NEWTAB_HANDOFF_SEND_RETRY_MS);}@@ -159,10 +162,10 @@ export function createNewtabHandoff({if (tabId == null) return false;if (sendStillHoldsComposer(tabId)) {if (!heldSince.has(Number(tabId))) heldSince.set(Number(tabId), now());- retrySoon();+ retrySoon(tabId);return false;}- retries = 0;+ retries.delete(Number(tabId));if (heldSince.has(Number(tabId))) {heldAllowanceMs.set(Number(tabId),diff --git a/src/firefox/src/ui/newtab-handoff.js b/src/firefox/src/ui/newtab-handoff.jsindex 3eb5df92b..d6534474a 100644--- a/src/firefox/src/ui/newtab-handoff.js+++ b/src/firefox/src/ui/newtab-handoff.js@@ -81,7 +81,7 @@ export function createNewtabHandoff({let consuming = false;let rerun = false;let retryTimer = null;- let retries = 0;+ const retries = new Map(); // tabId -> polls spent waiting for that tab's send// The Ask most recently handed to sendMessage, while its text may still be in// the composer waiting for the send's own checks to finish.// One per tab: a send in preflight on one tab must stay known while another tab is shown.@@ -131,18 +131,21 @@ export function createNewtabHandoff({return pendingSend.text.startsWith('/') && value === '' && (pendingSend.queued || !getIsProcessing());};- function retrySoon() {+ function retrySoon(tabId) {+ // Stop polling after the budget instead of forcing the send aside: a send that is still+ // running owns the composer, and putting the next task in beside it would be sent along+ // with it or duplicated when it settles. The waiting task stays in storage and is picked+ // up when that send settles (below) or when the panel is next triggered. The budget belongs+ // to the tab whose send is being waited for, so another tab's polling does not use it up.+ const polls = (retries.get(Number(tabId)) ?? 0) + 1;+ if (polls > NEWTAB_HANDOFF_SEND_HOLD_MAX_RETRIES) {+ retries.delete(Number(tabId));+ return;+ }+ retries.set(Number(tabId), polls);if (retryTimer) return;retryTimer = setTimeout(() => {retryTimer = null;- // Stop polling after the budget instead of forcing the send aside: a send that is still- // running owns the composer, and putting the next task in beside it would be sent along- // with it or duplicated when it settles. The waiting task stays in storage and is picked- // up when that send settles (below) or when the panel is next triggered.- if (++retries > NEWTAB_HANDOFF_SEND_HOLD_MAX_RETRIES) {- retries = 0;- return;- }consume();}, NEWTAB_HANDOFF_SEND_RETRY_MS);}@@ -159,10 +162,10 @@ export function createNewtabHandoff({if (tabId == null) return false;if (sendStillHoldsComposer(tabId)) {if (!heldSince.has(Number(tabId))) heldSince.set(Number(tabId), now());- retrySoon();+ retrySoon(tabId);return false;}- retries = 0;+ retries.delete(Number(tabId));if (heldSince.has(Number(tabId))) {heldAllowanceMs.set(Number(tabId),diff --git a/test/newtab.mjs b/test/newtab.mjsindex 8a8d279d5..38870e64f 100644--- a/test/newtab.mjs+++ b/test/newtab.mjs@@ -971,6 +971,62 @@ for (const browser of ['chrome', 'firefox']) {}});+ test(`${browser}: the polling budget belongs to the tab being waited for`, async () => {+ const key3 = newtabHandoffKey(3);+ const key4 = newtabHandoffKey(4);+ const store = { [key3]: { id: 'A', tabId: 3, text: 'task A', mode: 'ask', createdAt: 99_900 } };+ const state = { tabId: 3, looks: 0 };+ const input = { value: '', dispatchEvent() {}, focus() {} };+ const timers = [];+ const realSetTimeout = globalThis.setTimeout;+ globalThis.setTimeout = (fn) => { timers.push(fn); return timers.length; };+ const step = async (n) => {+ for (let i = 0; i < n && timers.length; i++) {+ timers.shift()();+ for (let k = 0; k < 30; k++) await Promise.resolve();+ }+ };+ try {+ const never = new Promise(() => {}); // sends stay in preflight for the whole test+ const handoff = createNewtabHandoff({+ storage: {+ get: async (k) => (k in store ? { [k]: store[k] } : {}),+ remove: async (k) => { delete store[k]; },+ set: async (o) => Object.assign(store, o),+ },+ getCurrentTabId: () => state.tabId,+ getIsProcessing: () => false,+ getIsBlocked: () => false,+ getIsDocumentVisible: () => { state.looks += 1; return true; },+ doc: { addEventListener() {} },+ setMode() {},+ getInputEl: () => input,+ autoResizeInput() {},+ sendMessage: async () => { await never; },+ now: () => 100_000,+ });+ await handoff.start(); // A's send holds tab 3's composer+ state.tabId = 4;+ input.value = '';+ store[key4] = { id: 'C', tabId: 4, text: 'task C', mode: 'ask', createdAt: 99_990 };+ await handoff.consume(); // C's send holds tab 4's composer+ state.tabId = 3;+ input.value = 'task A';+ store[key3] = { id: 'B', tabId: 3, text: 'task B', mode: 'ask', createdAt: 99_950 };+ await handoff.consume(); // B waits behind A; polling starts+ await step(97); // most of tab 3's budget is spent+ state.tabId = 4;+ input.value = 'task C';+ store[key4] = { id: 'D', tabId: 4, text: 'task D', mode: 'ask', createdAt: 99_995 };+ await handoff.consume(); // D waits behind C+ const before = state.looks;+ await step(40);+ assert.ok(state.looks - before >= 35, `tab 4 keeps polling on its own budget (${state.looks - before} looks)`);+ } finally {+ globalThis.setTimeout = realSetTimeout;+ }+ });+test(`${browser}: a task delayed by the panel's own hold is not stale when the send settles`, async () => {const key3 = newtabHandoffKey(3);const clock = { t: 100_000 };