Skip to content

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

fix(newtab): round-46 review — sweep keeps live retry queues; exact check for a replaced draft

Claude6536cabea762
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.js
index 2a85dfdbd..2f6f9098f 100644
--- a/src/chrome/src/background.js
+++ b/src/chrome/src/background.js
@@ -1646,7 +1646,12 @@ async function sweepStaleNewtabHandoffs() {
// Past the longest age the panel still accepts (150 s), so nothing claimable is removed.
const cutoff = Date.now() - 180_000;
const stale = Object.keys(all).filter(
- (key) => key.startsWith('wbNewtabHandoff:') && !(Number(all[key]?.createdAt) > cutoff),
+ (key) => {
+ if (!key.startsWith('wbNewtabHandoff:')) return false;
+ // The panel's retry slot holds a list; it is stale once none of its tasks is still claimable.
+ const entries = Array.isArray(all[key]) ? all[key] : [all[key]];
+ return !entries.some((entry) => Number(entry?.createdAt) > cutoff);
+ },
);
if (stale.length) await chrome.storage.local.remove(stale);
} catch {
diff --git a/src/chrome/src/ui/newtab-handoff.js b/src/chrome/src/ui/newtab-handoff.js
index 3a0b92842..1df998a5e 100644
--- a/src/chrome/src/ui/newtab-handoff.js
+++ b/src/chrome/src/ui/newtab-handoff.js
@@ -213,7 +213,7 @@ export function createNewtabHandoff({
const current = String(input.value || '').trim();
if (!current) {
fill();
- } else if (!current.includes(handoff.text)) {
+ } else if (current !== handoff.text && !current.endsWith(`\n\n${handoff.text}`)) {
// The user typed something else while the send was pending: keep their draft and
// put the task below it (the record is gone, so this is the only copy left).
input.value = `${current}\n\n${handoff.text}`;
diff --git a/src/firefox/src/background.js b/src/firefox/src/background.js
index ee71745f4..3e6ad033a 100644
--- a/src/firefox/src/background.js
+++ b/src/firefox/src/background.js
@@ -1401,7 +1401,12 @@ async function sweepStaleNewtabHandoffs() {
// Past the longest age the panel still accepts (150 s), so nothing claimable is removed.
const cutoff = Date.now() - 180_000;
const stale = Object.keys(all).filter(
- (key) => key.startsWith('wbNewtabHandoff:') && !(Number(all[key]?.createdAt) > cutoff),
+ (key) => {
+ if (!key.startsWith('wbNewtabHandoff:')) return false;
+ // The panel's retry slot holds a list; it is stale once none of its tasks is still claimable.
+ const entries = Array.isArray(all[key]) ? all[key] : [all[key]];
+ return !entries.some((entry) => Number(entry?.createdAt) > cutoff);
+ },
);
if (stale.length) await browser.storage.local.remove(stale);
} catch {
diff --git a/src/firefox/src/ui/newtab-handoff.js b/src/firefox/src/ui/newtab-handoff.js
index 3a0b92842..1df998a5e 100644
--- a/src/firefox/src/ui/newtab-handoff.js
+++ b/src/firefox/src/ui/newtab-handoff.js
@@ -213,7 +213,7 @@ export function createNewtabHandoff({
const current = String(input.value || '').trim();
if (!current) {
fill();
- } else if (!current.includes(handoff.text)) {
+ } else if (current !== handoff.text && !current.endsWith(`\n\n${handoff.text}`)) {
// The user typed something else while the send was pending: keep their draft and
// put the task below it (the record is gone, so this is the only copy left).
input.value = `${current}\n\n${handoff.text}`;
diff --git a/test/newtab.mjs b/test/newtab.mjs
index 1201752d1..2a001dab2 100644
--- a/test/newtab.mjs
+++ b/test/newtab.mjs
@@ -522,6 +522,36 @@ for (const browser of ['chrome', 'firefox']) {
assert.deepEqual(store[retry3].map((r) => r.id), ['A-retry', 'B-retry'], 'both tasks are kept');
});
+ test(`${browser}: a short task is not mistaken for a longer replacement draft that merely contains it`, async () => {
+ const key3 = newtabHandoffKey(3);
+ const store = { [key3]: { id: 'A', tabId: 3, text: 'yes', mode: 'ask', createdAt: 99_000 } };
+ 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: () => 3,
+ getIsProcessing: () => false,
+ getIsBlocked: () => false,
+ getIsDocumentVisible: () => true,
+ doc: { addEventListener() {} },
+ setMode() {},
+ getInputEl: () => input,
+ autoResizeInput() {},
+ sendMessage: async () => { input.value = 'yesterday'; await gate; return false; },
+ now: () => 100_000,
+ });
+ await handoff.start();
+ release();
+ await new Promise((r) => setTimeout(r, 0));
+ await new Promise((r) => setTimeout(r, 0));
+ assert.equal(input.value, 'yesterday\n\nyes');
+ });
+
const rec = (over = {}) => ({ id: 'r1', tabId: 3, text: 'summarise', mode: 'ask', createdAt: 99_000, ...over });
const KEY = newtabHandoffKey(3);
@@ -1632,3 +1662,11 @@ test('round-43: todo delete icon is a keyboard control', () => {
const todo = readFileSync(nt('features/navbar/components/Todo.jsx'), 'utf8');
assert.match(todo, /<MdDelete\s+role="button"\s+tabIndex=\{0\}/);
});
+
+test('round-46: the stale-record sweep understands the retry queue', () => {
+ for (const [file, api] of [['src/chrome/src/background.js', 'chrome'], ['src/firefox/src/background.js', 'browser']]) {
+ const src = readFileSync(path.join(root, file), 'utf8');
+ assert.match(src, /Array\.isArray\(all\[key\]\) \? all\[key\] : \[all\[key\]\]/, file);
+ assert.match(src, /entries\.some\(\(entry\) => Number\(entry\?\.createdAt\) > cutoff\)/, file);
+ }
+});