Skip to content

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

fix(newtab): round-67 review — a task handed back after a tab switch gets a fresh age; pending sends are tracked per tab

Claude8d7d06844044
diff --git a/src/chrome/src/ui/newtab-handoff.js b/src/chrome/src/ui/newtab-handoff.js
index 828740f6e..db9a87dd4 100644
--- a/src/chrome/src/ui/newtab-handoff.js
+++ b/src/chrome/src/ui/newtab-handoff.js
@@ -83,7 +83,8 @@ export function createNewtabHandoff({
let retries = 0;
// 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.
- let pendingSend = null;
+ // One per tab: a send in preflight on one tab must stay known while another tab is shown.
+ 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;
@@ -118,7 +119,8 @@ export function createNewtabHandoff({
}
});
- const sendStillHoldsComposer = () => {
+ const sendStillHoldsComposer = (tabId) => {
+ const pendingSend = pendingSends.get(Number(tabId));
if (!pendingSend || pendingSend.settled) return false;
const value = String(getInputEl()?.value ?? '');
if (value === pendingSend.text) return true;
@@ -154,7 +156,7 @@ export function createNewtabHandoff({
}
const tabId = getCurrentTabId();
if (tabId == null) return false;
- if (sendStillHoldsComposer()) {
+ if (sendStillHoldsComposer(tabId)) {
if (heldSince == null) heldSince = now();
retrySoon();
return false;
@@ -240,7 +242,9 @@ export function createNewtabHandoff({
if (!sameTab()) {
// Switched during the removal: put the record back for when this tab is shown again.
handled.delete(handoff.id);
- await stashForLater(stored);
+ // The new tab already showed this task as handed over, so it must outlive the freshness
+ // window that has been running since it was typed.
+ await stashForLater({ ...stored, createdAt: now() });
return false;
}
@@ -267,7 +271,7 @@ export function createNewtabHandoff({
// here captures this task. It is not awaited: a send can last a whole run,
// and that must not hold up later hand-offs or the panel's own startup.
const send = { text: handoff.text, settled: false, queued: Boolean(getIsProcessing()) };
- pendingSend = send;
+ pendingSends.set(Number(tabId), send);
let sending;
try {
sending = Promise.resolve(sendMessage(getIsProcessing() ? { __deliveryMode: 'queue' } : undefined));
@@ -278,6 +282,7 @@ export function createNewtabHandoff({
.then((result) => result !== false, () => false)
.then((sent) => {
send.settled = true;
+ if (pendingSends.get(Number(tabId)) === send) pendingSends.delete(Number(tabId));
// A task that arrived while this send held the composer is waiting; look for it once
// the recovery below (which runs synchronously) is done.
queueMicrotask(() => { consume(); });
diff --git a/src/firefox/src/ui/newtab-handoff.js b/src/firefox/src/ui/newtab-handoff.js
index 828740f6e..db9a87dd4 100644
--- a/src/firefox/src/ui/newtab-handoff.js
+++ b/src/firefox/src/ui/newtab-handoff.js
@@ -83,7 +83,8 @@ export function createNewtabHandoff({
let retries = 0;
// 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.
- let pendingSend = null;
+ // One per tab: a send in preflight on one tab must stay known while another tab is shown.
+ 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;
@@ -118,7 +119,8 @@ export function createNewtabHandoff({
}
});
- const sendStillHoldsComposer = () => {
+ const sendStillHoldsComposer = (tabId) => {
+ const pendingSend = pendingSends.get(Number(tabId));
if (!pendingSend || pendingSend.settled) return false;
const value = String(getInputEl()?.value ?? '');
if (value === pendingSend.text) return true;
@@ -154,7 +156,7 @@ export function createNewtabHandoff({
}
const tabId = getCurrentTabId();
if (tabId == null) return false;
- if (sendStillHoldsComposer()) {
+ if (sendStillHoldsComposer(tabId)) {
if (heldSince == null) heldSince = now();
retrySoon();
return false;
@@ -240,7 +242,9 @@ export function createNewtabHandoff({
if (!sameTab()) {
// Switched during the removal: put the record back for when this tab is shown again.
handled.delete(handoff.id);
- await stashForLater(stored);
+ // The new tab already showed this task as handed over, so it must outlive the freshness
+ // window that has been running since it was typed.
+ await stashForLater({ ...stored, createdAt: now() });
return false;
}
@@ -267,7 +271,7 @@ export function createNewtabHandoff({
// here captures this task. It is not awaited: a send can last a whole run,
// and that must not hold up later hand-offs or the panel's own startup.
const send = { text: handoff.text, settled: false, queued: Boolean(getIsProcessing()) };
- pendingSend = send;
+ pendingSends.set(Number(tabId), send);
let sending;
try {
sending = Promise.resolve(sendMessage(getIsProcessing() ? { __deliveryMode: 'queue' } : undefined));
@@ -278,6 +282,7 @@ export function createNewtabHandoff({
.then((result) => result !== false, () => false)
.then((sent) => {
send.settled = true;
+ if (pendingSends.get(Number(tabId)) === send) pendingSends.delete(Number(tabId));
// A task that arrived while this send held the composer is waiting; look for it once
// the recovery below (which runs synchronously) is done.
queueMicrotask(() => { consume(); });
diff --git a/test/newtab.mjs b/test/newtab.mjs
index ffcf25632..5919c8f76 100644
--- a/test/newtab.mjs
+++ b/test/newtab.mjs
@@ -467,6 +467,91 @@ for (const browser of ['chrome', 'firefox']) {
assert.deepEqual(sent, ['for tab three']);
});
+ test(`${browser}: a task handed back after a tab switch is still fresh when its tab returns`, async () => {
+ const key3 = newtabHandoffKey(3);
+ const store = { [key3]: { id: 'r1', tabId: 3, text: 'for tab three', mode: 'ask', createdAt: 80_000 } };
+ const state = { tabId: 3, clock: 100_000 };
+ const input = { value: '', dispatchEvent() {}, focus() {} };
+ const sent = [];
+ const handoff = createNewtabHandoff({
+ storage: {
+ get: async (k) => (k in store ? { [k]: store[k] } : {}),
+ remove: async (k) => { delete store[k]; if (!state.switched) { state.switched = true; state.tabId = 4; } },
+ set: async (o) => Object.assign(store, o),
+ },
+ getCurrentTabId: () => state.tabId,
+ getIsProcessing: () => false,
+ getIsBlocked: () => false,
+ getIsDocumentVisible: () => true,
+ doc: { addEventListener() {} },
+ setMode() {},
+ getInputEl: () => input,
+ autoResizeInput() {},
+ sendMessage: async () => { sent.push(input.value); },
+ now: () => state.clock,
+ });
+ await handoff.start(); // claimed at 100 s when 20 s old, then the user switched away
+ assert.equal(store[newtabHandoffRetryKey(3)]?.[0]?.text, 'for tab three');
+ state.clock = 125_000; // back after 25 s: older than 30 s counted from the original typing
+ state.tabId = 3;
+ await handoff.consume();
+ assert.deepEqual(sent, ['for tab three'], 'the task the new tab already cleared is not dropped');
+ });
+
+ test(`${browser}: a send still in preflight on one tab stays known while another tab is shown`, async () => {
+ const store = {
+ [newtabHandoffKey(3)]: { id: 'A', tabId: 3, text: 'task A', mode: 'ask', createdAt: 99_000 },
+ };
+ const state = { tabId: 3 };
+ 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 = ''; } // A stays in preflight, then commits
+ return true;
+ },
+ now: () => 100_000,
+ });
+ await handoff.start();
+ assert.deepEqual(sends, ['task A']);
+ // the panel swaps in tab 4's composer; tab 4 gets and sends its own task
+ state.tabId = 4;
+ input.value = '';
+ store[newtabHandoffKey(4)] = { id: 'B', tabId: 4, text: 'task B', mode: 'ask', createdAt: 99_500 };
+ await handoff.consume();
+ await new Promise((r) => setTimeout(r, 0));
+ assert.deepEqual(sends, ['task A', 'task B']);
+ // back on tab 3 (its composer shows A again) a new task must not be applied over A
+ state.tabId = 3;
+ input.value = 'task A';
+ store[newtabHandoffKey(3)] = { id: 'A2', tabId: 3, text: 'task A2', mode: 'ask', createdAt: 99_800 };
+ await handoff.consume();
+ assert.equal(input.value, 'task A', "A's composer is untouched while A is still being sent");
+ assert.equal(store[newtabHandoffKey(3)]?.id, 'A2', 'A2 waits in storage');
+ releaseA();
+ await new Promise((r) => setTimeout(r, 0));
+ await new Promise((r) => setTimeout(r, 0));
+ await new Promise((r) => setTimeout(r, 0));
+ assert.equal(sends.includes('task A2'), true, 'A2 goes out once A has settled');
+ });
+
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 } };