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-68 review — the time a task waited behind a send is tracked per tab
Claudee62e1fa99094
diff --git a/src/chrome/src/ui/newtab-handoff.js b/src/chrome/src/ui/newtab-handoff.jsindex db9a87dd4..4611b623c 100644--- a/src/chrome/src/ui/newtab-handoff.js+++ b/src/chrome/src/ui/newtab-handoff.js@@ -87,10 +87,10 @@ export function createNewtabHandoff({const pendingSends = new Map();// When a look at storage was first put off because a send still held the composer. A task that// sat waiting behind that send is not stale for the time the panel itself kept it waiting.- let heldSince = null;+ const heldSince = new Map(); // tabId -> when// The waiting time of tasks put off by that hold, kept until every task it delayed has been// looked at (a fresher one may be considered first).- let heldAllowanceMs = 0;+ const heldAllowanceMs = new Map(); // tabId -> ms// Writes to the retry slot (a list, written only by this panel) run one after another, so// two hand-backs or a hand-back and a claim never overwrite each other.let retrySlotWrites = Promise.resolve();@@ -157,17 +157,20 @@ export function createNewtabHandoff({const tabId = getCurrentTabId();if (tabId == null) return false;if (sendStillHoldsComposer(tabId)) {- if (heldSince == null) heldSince = now();+ if (!heldSince.has(Number(tabId))) heldSince.set(Number(tabId), now());retrySoon();return false;}retries = 0;- if (heldSince != null) {- heldAllowanceMs = Math.max(- heldAllowanceMs,- Math.min(now() - heldSince, NEWTAB_HANDOFF_MAX_STARTUP_ALLOWANCE_MS),+ if (heldSince.has(Number(tabId))) {+ heldAllowanceMs.set(+ Number(tabId),+ Math.max(+ heldAllowanceMs.get(Number(tabId)) ?? 0,+ Math.min(now() - heldSince.get(Number(tabId)), NEWTAB_HANDOFF_MAX_STARTUP_ALLOWANCE_MS),+ ),);- heldSince = null;+ heldSince.delete(Number(tabId));}const primaryKey = newtabHandoffKey(Number(tabId));const retryKey = newtabHandoffRetryKey(Number(tabId));@@ -177,7 +180,7 @@ export function createNewtabHandoff({// Anything that could not be saved earlier gets another try before storage is read.for (const record of unsaved.slice()) await stashRecord(record);// Only the first attempt after start() gets the startup allowance.- const allowance = startupAllowanceMs + heldAllowanceMs;+ const allowance = startupAllowanceMs + (heldAllowanceMs.get(Number(tabId)) ?? 0);startupAllowanceMs = 0;// The shared composer belongs to whichever tab is active now. If the user switches// tabs while storage is being read or written, this tab's task must not be applied@@ -217,7 +220,7 @@ export function createNewtabHandoff({handoff = stored == null ? null : accept(stored);}if (stored == null) {- heldAllowanceMs = 0; // nothing left that the hold could have delayed+ heldAllowanceMs.delete(Number(tabId)); // nothing left that the hold could have delayedreturn false;}if (!sameTab()) return false;diff --git a/src/firefox/src/ui/newtab-handoff.js b/src/firefox/src/ui/newtab-handoff.jsindex db9a87dd4..4611b623c 100644--- a/src/firefox/src/ui/newtab-handoff.js+++ b/src/firefox/src/ui/newtab-handoff.js@@ -87,10 +87,10 @@ export function createNewtabHandoff({const pendingSends = new Map();// When a look at storage was first put off because a send still held the composer. A task that// sat waiting behind that send is not stale for the time the panel itself kept it waiting.- let heldSince = null;+ const heldSince = new Map(); // tabId -> when// The waiting time of tasks put off by that hold, kept until every task it delayed has been// looked at (a fresher one may be considered first).- let heldAllowanceMs = 0;+ const heldAllowanceMs = new Map(); // tabId -> ms// Writes to the retry slot (a list, written only by this panel) run one after another, so// two hand-backs or a hand-back and a claim never overwrite each other.let retrySlotWrites = Promise.resolve();@@ -157,17 +157,20 @@ export function createNewtabHandoff({const tabId = getCurrentTabId();if (tabId == null) return false;if (sendStillHoldsComposer(tabId)) {- if (heldSince == null) heldSince = now();+ if (!heldSince.has(Number(tabId))) heldSince.set(Number(tabId), now());retrySoon();return false;}retries = 0;- if (heldSince != null) {- heldAllowanceMs = Math.max(- heldAllowanceMs,- Math.min(now() - heldSince, NEWTAB_HANDOFF_MAX_STARTUP_ALLOWANCE_MS),+ if (heldSince.has(Number(tabId))) {+ heldAllowanceMs.set(+ Number(tabId),+ Math.max(+ heldAllowanceMs.get(Number(tabId)) ?? 0,+ Math.min(now() - heldSince.get(Number(tabId)), NEWTAB_HANDOFF_MAX_STARTUP_ALLOWANCE_MS),+ ),);- heldSince = null;+ heldSince.delete(Number(tabId));}const primaryKey = newtabHandoffKey(Number(tabId));const retryKey = newtabHandoffRetryKey(Number(tabId));@@ -177,7 +180,7 @@ export function createNewtabHandoff({// Anything that could not be saved earlier gets another try before storage is read.for (const record of unsaved.slice()) await stashRecord(record);// Only the first attempt after start() gets the startup allowance.- const allowance = startupAllowanceMs + heldAllowanceMs;+ const allowance = startupAllowanceMs + (heldAllowanceMs.get(Number(tabId)) ?? 0);startupAllowanceMs = 0;// The shared composer belongs to whichever tab is active now. If the user switches// tabs while storage is being read or written, this tab's task must not be applied@@ -217,7 +220,7 @@ export function createNewtabHandoff({handoff = stored == null ? null : accept(stored);}if (stored == null) {- heldAllowanceMs = 0; // nothing left that the hold could have delayed+ heldAllowanceMs.delete(Number(tabId)); // nothing left that the hold could have delayedreturn false;}if (!sameTab()) return false;diff --git a/test/newtab.mjs b/test/newtab.mjsindex 5919c8f76..49f03d286 100644--- a/test/newtab.mjs+++ b/test/newtab.mjs@@ -552,6 +552,56 @@ for (const browser of ['chrome', 'firefox']) {assert.equal(sends.includes('task A2'), true, 'A2 goes out once A has settled');});+ test(`${browser}: the time a task waited behind a send is kept for its own tab`, async () => {+ const key3 = newtabHandoffKey(3);+ const store = { [key3]: { id: 'A', tabId: 3, text: 'task A', mode: 'ask', createdAt: 99_000 } };+ const state = { tabId: 3, clock: 100_000 };+ const input = { value: '', dispatchEvent() {}, focus() {} };+ const sends = [];+ let releaseA;+ const gateA = new Promise((r) => { releaseA = r; });+ 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: () => true,+ doc: { addEventListener() {} },+ setMode() {},+ getInputEl: () => input,+ autoResizeInput() {},+ sendMessage: async () => {+ const text = input.value;+ sends.push(text);+ if (text === 'task A') { await gateA; input.value = ''; }+ return true;+ },+ now: () => state.clock,+ });+ await handoff.start();+ store[key3] = { id: 'A2', tabId: 3, text: 'task A2', mode: 'ask', createdAt: 99_800 };+ await handoff.consume(); // held behind A: the wait starts counting here+ assert.deepEqual(sends, ['task A']);+ state.clock = 135_000; // the send is slow; A2 is now 35 s old+ // the user looks at another tab, which has nothing waiting+ state.tabId = 4;+ input.value = '';+ await handoff.consume();+ releaseA();+ await new Promise((r) => setTimeout(r, 0));+ await new Promise((r) => setTimeout(r, 0));+ // and returns: A2 is older than 30 s only because the panel kept it waiting+ state.tabId = 3;+ input.value = '';+ await handoff.consume();+ await new Promise((r) => setTimeout(r, 0));+ assert.equal(sends.includes('task A2'), true, 'the waiting task is still accepted');+ });+test(`${browser}: a declined send never refills another tab's composer; the task goes back to its own tab`, async () => {const key3 = newtabHandoffKey(3);const store = { [key3]: { id: 'r1', tabId: 3, text: 'for tab three', mode: 'ask', createdAt: 99_000 } };