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-70 review — the panel's startup delay is credited to every task created before startup finished, on any tab
Claude1c5cc995017c
diff --git a/src/chrome/src/ui/newtab-handoff.js b/src/chrome/src/ui/newtab-handoff.jsindex 4611b623c..3eb5df92b 100644--- a/src/chrome/src/ui/newtab-handoff.js+++ b/src/chrome/src/ui/newtab-handoff.js@@ -77,6 +77,7 @@ export function createNewtabHandoff({const startedAt = now();let ready = false;let startupAllowanceMs = 0;+ let startedUpAt = -Infinity; // when start() ran; tasks created before it were delayed by the startuplet consuming = false;let rerun = false;let retryTimer = null;@@ -179,9 +180,7 @@ export function createNewtabHandoff({consuming = true;// 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.get(Number(tabId)) ?? 0);- startupAllowanceMs = 0;+ const heldAllowance = heldAllowanceMs.get(Number(tabId)) ?? 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// to the other tab's composer; the switch's own consume() picks up the right one.@@ -207,8 +206,12 @@ export function createNewtabHandoff({});};try {- const accept = (record) =>- normalizeNewtabHandoff(record, now(), NEWTAB_HANDOFF_MAX_AGE_MS + allowance);+ // Every task typed before the panel finished starting was delayed by that startup, whichever+ // tab it belongs to and whichever tab is looked at first.+ const accept = (record) => {+ const delayedByStartup = Number(record?.createdAt) <= startedUpAt ? startupAllowanceMs : 0;+ return normalizeNewtabHandoff(record, now(), NEWTAB_HANDOFF_MAX_AGE_MS + delayedByStartup + heldAllowance);+ };let stored = (await storage.get(primaryKey))?.[primaryKey];let handoff = stored == null ? null : accept(stored);// A stale or malformed primary record is left where it is: the new tab replaces it once@@ -331,6 +334,7 @@ export function createNewtabHandoff({Math.max(0, now() - startedAt),NEWTAB_HANDOFF_MAX_STARTUP_ALLOWANCE_MS,);+ startedUpAt = now();ready = true;return consume();}diff --git a/src/firefox/src/ui/newtab-handoff.js b/src/firefox/src/ui/newtab-handoff.jsindex 4611b623c..3eb5df92b 100644--- a/src/firefox/src/ui/newtab-handoff.js+++ b/src/firefox/src/ui/newtab-handoff.js@@ -77,6 +77,7 @@ export function createNewtabHandoff({const startedAt = now();let ready = false;let startupAllowanceMs = 0;+ let startedUpAt = -Infinity; // when start() ran; tasks created before it were delayed by the startuplet consuming = false;let rerun = false;let retryTimer = null;@@ -179,9 +180,7 @@ export function createNewtabHandoff({consuming = true;// 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.get(Number(tabId)) ?? 0);- startupAllowanceMs = 0;+ const heldAllowance = heldAllowanceMs.get(Number(tabId)) ?? 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// to the other tab's composer; the switch's own consume() picks up the right one.@@ -207,8 +206,12 @@ export function createNewtabHandoff({});};try {- const accept = (record) =>- normalizeNewtabHandoff(record, now(), NEWTAB_HANDOFF_MAX_AGE_MS + allowance);+ // Every task typed before the panel finished starting was delayed by that startup, whichever+ // tab it belongs to and whichever tab is looked at first.+ const accept = (record) => {+ const delayedByStartup = Number(record?.createdAt) <= startedUpAt ? startupAllowanceMs : 0;+ return normalizeNewtabHandoff(record, now(), NEWTAB_HANDOFF_MAX_AGE_MS + delayedByStartup + heldAllowance);+ };let stored = (await storage.get(primaryKey))?.[primaryKey];let handoff = stored == null ? null : accept(stored);// A stale or malformed primary record is left where it is: the new tab replaces it once@@ -331,6 +334,7 @@ export function createNewtabHandoff({Math.max(0, now() - startedAt),NEWTAB_HANDOFF_MAX_STARTUP_ALLOWANCE_MS,);+ startedUpAt = now();ready = true;return consume();}diff --git a/test/newtab.mjs b/test/newtab.mjsindex 91927ccd0..8a8d279d5 100644--- a/test/newtab.mjs+++ b/test/newtab.mjs@@ -602,6 +602,46 @@ for (const browser of ['chrome', 'firefox']) {assert.equal(sends.includes('task A2'), true, 'the waiting task is still accepted');});+ test(`${browser}: the startup delay is credited to every tab's task, not just the first one looked at`, async () => {+ const store = {+ [newtabHandoffKey(3)]: { id: 'T3', tabId: 3, text: 'task three', mode: 'act', createdAt: 100_500 },+ [newtabHandoffKey(4)]: { id: 'T4', tabId: 4, text: 'task four', mode: 'act', createdAt: 100_600 },+ };+ const state = { tabId: 3, clock: 100_000 };+ const input = { value: '', dispatchEvent() {}, focus() {} };+ 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 () => {},+ now: () => state.clock,+ });+ state.clock = 150_000; // a cold panel took 50 s to restore its chat; both tasks are ~50 s old+ await handoff.start();+ assert.equal(input.value, 'task three');+ state.tabId = 4;+ input.value = '';+ await handoff.consume();+ assert.equal(input.value, 'task four', "the second tab's equally old task is accepted too");+ // a task typed after startup gets no such credit+ store[newtabHandoffKey(3)] = { id: 'late', tabId: 3, text: 'late', mode: 'act', createdAt: 150_100 };+ state.clock = 190_000; // 40 s old, more than the ordinary 30 s+ state.tabId = 3;+ input.value = '';+ await handoff.consume();+ assert.equal(input.value, '', 'a task created after startup uses the ordinary window');+ });+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 } };