Skip to content

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

fix(newtab): round-45 review — retry slot is a queue so no handed-back task is lost

Clauded91cc75a32bb
diff --git a/src/chrome/src/ui/newtab-handoff.js b/src/chrome/src/ui/newtab-handoff.js
index 79f5cf1df..3a0b92842 100644
--- a/src/chrome/src/ui/newtab-handoff.js
+++ b/src/chrome/src/ui/newtab-handoff.js
@@ -82,6 +82,14 @@ export function createNewtabHandoff({
// 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;
+ // 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();
+ const serialRetryWrite = (fn) => {
+ retrySlotWrites = retrySlotWrites.then(fn, fn);
+ return retrySlotWrites;
+ };
+ const asList = (raw) => (Array.isArray(raw) ? raw : raw == null ? [] : [raw]);
const sendStillHoldsComposer = () => {
if (!pendingSend || pendingSend.settled) return false;
@@ -126,28 +134,39 @@ export function createNewtabHandoff({
const sameTab = () => Number(getCurrentTabId()) === Number(tabId);
// Hand a task back for when this tab is shown again, without touching the key the new tab
// writes to (it unlocks as soon as a record is removed and may already hold the next task).
- const stashForLater = async (record) => {
+ const stashForLater = (record) => serialRetryWrite(async () => {
try {
- const current = (await storage.get(retryKey))?.[retryKey];
- if (current == null) await storage.set?.({ [retryKey]: record });
+ const queue = asList((await storage.get(retryKey))?.[retryKey]);
+ if (queue.some((entry) => entry?.id === record.id)) return;
+ await storage.set?.({ [retryKey]: [...queue, record] });
} catch { /* best effort */ }
+ });
+ // Takes the record just read out of storage: the primary key is simply removed, the retry
+ // slot drops that one entry and keeps the others.
+ const removeClaimed = (record) => {
+ if (key !== retryKey) return storage.remove(key);
+ return serialRetryWrite(async () => {
+ const rest = asList((await storage.get(retryKey))?.[retryKey]).filter((entry) => entry?.id !== record?.id);
+ if (rest.length) await storage.set?.({ [retryKey]: rest });
+ else await storage.remove(retryKey);
+ });
};
try {
let stored = (await storage.get(primaryKey))?.[primaryKey];
if (stored == null && sameTab()) {
- stored = (await storage.get(retryKey))?.[retryKey];
+ stored = asList((await storage.get(retryKey))?.[retryKey])[0];
key = retryKey;
}
if (stored == null || !sameTab()) return false;
const handoff = normalizeNewtabHandoff(stored, now(), NEWTAB_HANDOFF_MAX_AGE_MS + allowance);
if (!handoff) {
- await storage.remove(key);
+ await removeClaimed(stored);
return false;
}
if (handoff.tabId !== Number(tabId) || handled.has(handoff.id)) return false;
handled.add(handoff.id);
- await storage.remove(key);
+ await removeClaimed(stored);
if (!sameTab()) {
// Switched during the removal: put the record back for when this tab is shown again.
handled.delete(handoff.id);
diff --git a/src/firefox/src/ui/newtab-handoff.js b/src/firefox/src/ui/newtab-handoff.js
index 79f5cf1df..3a0b92842 100644
--- a/src/firefox/src/ui/newtab-handoff.js
+++ b/src/firefox/src/ui/newtab-handoff.js
@@ -82,6 +82,14 @@ export function createNewtabHandoff({
// 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;
+ // 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();
+ const serialRetryWrite = (fn) => {
+ retrySlotWrites = retrySlotWrites.then(fn, fn);
+ return retrySlotWrites;
+ };
+ const asList = (raw) => (Array.isArray(raw) ? raw : raw == null ? [] : [raw]);
const sendStillHoldsComposer = () => {
if (!pendingSend || pendingSend.settled) return false;
@@ -126,28 +134,39 @@ export function createNewtabHandoff({
const sameTab = () => Number(getCurrentTabId()) === Number(tabId);
// Hand a task back for when this tab is shown again, without touching the key the new tab
// writes to (it unlocks as soon as a record is removed and may already hold the next task).
- const stashForLater = async (record) => {
+ const stashForLater = (record) => serialRetryWrite(async () => {
try {
- const current = (await storage.get(retryKey))?.[retryKey];
- if (current == null) await storage.set?.({ [retryKey]: record });
+ const queue = asList((await storage.get(retryKey))?.[retryKey]);
+ if (queue.some((entry) => entry?.id === record.id)) return;
+ await storage.set?.({ [retryKey]: [...queue, record] });
} catch { /* best effort */ }
+ });
+ // Takes the record just read out of storage: the primary key is simply removed, the retry
+ // slot drops that one entry and keeps the others.
+ const removeClaimed = (record) => {
+ if (key !== retryKey) return storage.remove(key);
+ return serialRetryWrite(async () => {
+ const rest = asList((await storage.get(retryKey))?.[retryKey]).filter((entry) => entry?.id !== record?.id);
+ if (rest.length) await storage.set?.({ [retryKey]: rest });
+ else await storage.remove(retryKey);
+ });
};
try {
let stored = (await storage.get(primaryKey))?.[primaryKey];
if (stored == null && sameTab()) {
- stored = (await storage.get(retryKey))?.[retryKey];
+ stored = asList((await storage.get(retryKey))?.[retryKey])[0];
key = retryKey;
}
if (stored == null || !sameTab()) return false;
const handoff = normalizeNewtabHandoff(stored, now(), NEWTAB_HANDOFF_MAX_AGE_MS + allowance);
if (!handoff) {
- await storage.remove(key);
+ await removeClaimed(stored);
return false;
}
if (handoff.tabId !== Number(tabId) || handled.has(handoff.id)) return false;
handled.add(handoff.id);
- await storage.remove(key);
+ await removeClaimed(stored);
if (!sameTab()) {
// Switched during the removal: put the record back for when this tab is shown again.
handled.delete(handoff.id);
diff --git a/test/newtab.mjs b/test/newtab.mjs
index 2e0af6004..1201752d1 100644
--- a/test/newtab.mjs
+++ b/test/newtab.mjs
@@ -352,7 +352,7 @@ for (const browser of ['chrome', 'firefox']) {
assert.deepEqual(sent, [], 'nothing was sent into the other tab');
assert.equal(input.value, '', "the other tab's composer was not touched");
// a read-time switch leaves the record where it was; a removal-time switch hands it back
- assert.equal((store[key3] ?? store[newtabHandoffRetryKey(3)])?.id, 'r1', "the old tab's record is still waiting");
+ assert.equal((store[key3] ?? store[newtabHandoffRetryKey(3)]?.[0])?.id, 'r1', "the old tab's record is still waiting");
// back on tab 3 the task is applied
state.tabId = 3;
await handoff.consume();
@@ -390,8 +390,8 @@ for (const browser of ['chrome', 'firefox']) {
await new Promise((r) => setTimeout(r, 0));
await new Promise((r) => setTimeout(r, 0));
assert.equal(input.value, '', "tab 4's composer stays empty");
- assert.equal(store[newtabHandoffRetryKey(3)]?.text, 'for tab three', 'the task is waiting for tab 3 again');
- assert.equal(store[newtabHandoffRetryKey(3)].id, 'r1-retry');
+ assert.equal(store[newtabHandoffRetryKey(3)]?.[0]?.text, 'for tab three', 'the task is waiting for tab 3 again');
+ assert.equal(store[newtabHandoffRetryKey(3)][0].id, 'r1-retry');
});
test(`${browser}: a declined send keeps a draft typed meanwhile and adds the task below it`, async () => {
@@ -455,13 +455,13 @@ for (const browser of ['chrome', 'firefox']) {
await new Promise((r) => setTimeout(r, 0));
await new Promise((r) => setTimeout(r, 0));
assert.equal(store[key3].id, 'B', 'the newer task is kept');
- assert.equal(store[newtabHandoffRetryKey(3)].id, 'A-retry', 'and the declined one is kept beside it');
+ assert.equal(store[newtabHandoffRetryKey(3)][0].id, 'A-retry', 'and the declined one is kept beside it');
});
test(`${browser}: a task handed back to its tab is applied when that tab is shown again, and the slot is cleared`, async () => {
const key3 = newtabHandoffKey(3);
const retry3 = newtabHandoffRetryKey(3);
- const store = { [retry3]: { id: 'A-retry', tabId: 3, text: 'task A', mode: 'ask', createdAt: 99_900 } };
+ const store = { [retry3]: [{ id: 'A-retry', tabId: 3, text: 'task A', mode: 'ask', createdAt: 99_900 }] };
const input = { value: '', dispatchEvent() {}, focus() {} };
const sent = [];
const handoff = createNewtabHandoff({
@@ -487,6 +487,41 @@ for (const browser of ['chrome', 'firefox']) {
assert.equal(key3 in store, false);
});
+ test(`${browser}: handing a task back never discards one already waiting in the retry slot`, async () => {
+ const key3 = newtabHandoffKey(3);
+ const retry3 = newtabHandoffRetryKey(3);
+ const store = {
+ [key3]: { id: 'B', tabId: 3, text: 'task B', mode: 'ask', createdAt: 99_500 },
+ [retry3]: [{ id: 'A-retry', tabId: 3, text: 'task A', mode: 'ask', createdAt: 99_900 }],
+ };
+ const state = { tabId: 3 };
+ const input = { value: '', dispatchEvent() {}, focus() {} };
+ let release;
+ const gate = new Promise((r) => { release = 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 () => { input.value = ''; await gate; return false; },
+ now: () => 100_000,
+ });
+ await handoff.start(); // B (the primary record) is claimed first and sent
+ state.tabId = 4;
+ release();
+ for (let i = 0; i < 4; i++) await new Promise((r) => setTimeout(r, 0));
+ assert.deepEqual(store[retry3].map((r) => r.id), ['A-retry', 'B-retry'], 'both tasks are kept');
+ });
+
const rec = (over = {}) => ({ id: 'r1', tabId: 3, text: 'summarise', mode: 'ask', createdAt: 99_000, ...over });
const KEY = newtabHandoffKey(3);