Skip to content

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

fix(newtab): round-83 review — the Act eligibility check no longer switches the mode before the composer is re-read

Claude988b9420ee09
diff --git a/src/chrome/src/ui/newtab-handoff.js b/src/chrome/src/ui/newtab-handoff.js
index f0a83742f..07d3a5b2c 100644
--- a/src/chrome/src/ui/newtab-handoff.js
+++ b/src/chrome/src/ui/newtab-handoff.js
@@ -266,7 +266,8 @@ export function createNewtabHandoff({
const draftBefore = String(getInputEl()?.value || '').trim();
if (handoff.mode === 'act' && ensureActMode && !(draftBefore && draftBefore !== handoff.text)) {
try {
- actBlocked = (await ensureActMode()) === false;
+ // Eligibility only: the mode is switched below, after the composer has been looked at again.
+ actBlocked = (await ensureActMode({ switchMode: false })) === false;
} catch {
actBlocked = false; // a failed lookup must not block; sending is still checked by the panel
}
diff --git a/src/chrome/src/ui/sidepanel.js b/src/chrome/src/ui/sidepanel.js
index 28a6df12d..dcbb74fca 100644
--- a/src/chrome/src/ui/sidepanel.js
+++ b/src/chrome/src/ui/sidepanel.js
@@ -14602,7 +14602,9 @@ async function inBrowserProviderBlocksMode() {
}
}
-async function ensureActMode() {
+// `switchMode: false` only answers whether Act may run; the caller switches the mode itself (the
+// new-tab handoff does so after re-checking the composer, which can change while this awaits).
+async function ensureActMode({ switchMode = true } = {}) {
if (isSelectionGroundedForTab(currentTabId)) {
showComposerToast(t('sp.selection_scope.description'), { duration: 5000 });
return false;
@@ -14610,7 +14612,7 @@ async function ensureActMode() {
if (agentMode === 'act') return true;
if (await inBrowserProviderBlocksMode()) return false;
if (autoModeSelection) return true;
- setMode('act');
+ if (switchMode) setMode('act');
return true;
}
diff --git a/src/firefox/src/ui/newtab-handoff.js b/src/firefox/src/ui/newtab-handoff.js
index f0a83742f..07d3a5b2c 100644
--- a/src/firefox/src/ui/newtab-handoff.js
+++ b/src/firefox/src/ui/newtab-handoff.js
@@ -266,7 +266,8 @@ export function createNewtabHandoff({
const draftBefore = String(getInputEl()?.value || '').trim();
if (handoff.mode === 'act' && ensureActMode && !(draftBefore && draftBefore !== handoff.text)) {
try {
- actBlocked = (await ensureActMode()) === false;
+ // Eligibility only: the mode is switched below, after the composer has been looked at again.
+ actBlocked = (await ensureActMode({ switchMode: false })) === false;
} catch {
actBlocked = false; // a failed lookup must not block; sending is still checked by the panel
}
diff --git a/src/firefox/src/ui/sidepanel.js b/src/firefox/src/ui/sidepanel.js
index bc4af3a66..9cc809165 100644
--- a/src/firefox/src/ui/sidepanel.js
+++ b/src/firefox/src/ui/sidepanel.js
@@ -14208,14 +14208,16 @@ function normalizeAgentMode(mode) {
return mode === 'act' || mode === 'dev' ? mode : 'ask';
}
-async function ensureActMode() {
+// `switchMode: false` only answers whether Act may run; the caller switches the mode itself (the
+// new-tab handoff does so after re-checking the composer, which can change while this awaits).
+async function ensureActMode({ switchMode = true } = {}) {
if (isSelectionGroundedForTab(currentTabId)) {
showComposerToast(t('sp.selection_scope.description'), { duration: 5000 });
return false;
}
if (agentMode === 'act') return true;
if (autoModeSelection) return true;
- setMode('act');
+ if (switchMode) setMode('act');
return true;
}
diff --git a/test/newtab.mjs b/test/newtab.mjs
index f625a45d6..f5686378c 100644
--- a/test/newtab.mjs
+++ b/test/newtab.mjs
@@ -690,7 +690,7 @@ for (const browser of ['chrome', 'firefox']) {
});
test(`${browser}: Act is checked for eligibility before it is accepted`, async () => {
- const run = async ({ allowed, draft = '', throws = false }) => {
+ const run = async ({ allowed, draft = '', throws = false, typeDuringCheck = '' }) => {
const key3 = newtabHandoffKey(3);
const store = { [key3]: { id: 'r1', tabId: 3, text: 'book a table', mode: 'act', createdAt: 99_000 } };
const input = { value: draft, dispatchEvent() {}, focus() {} };
@@ -707,7 +707,7 @@ for (const browser of ['chrome', 'firefox']) {
getIsDocumentVisible: () => true,
doc: { addEventListener() {} },
setMode: (m) => log.modes.push(m),
- ensureActMode: async () => { log.checks += 1; if (throws) throw new Error('lookup failed'); return allowed; },
+ ensureActMode: async (options) => { log.checks += 1; log.options = options; if (typeDuringCheck) input.value = typeDuringCheck; if (throws) throw new Error('lookup failed'); return allowed; },
getInputEl: () => input,
autoResizeInput() {},
sendMessage: async (p) => { log.sent.push(p); input.value = ''; },
@@ -734,6 +734,12 @@ for (const browser of ['chrome', 'firefox']) {
assert.equal(withDraft.log.checks, 0);
assert.deepEqual(withDraft.log.modes, []);
assert.equal(withDraft.input.value, 'my own text\n\nbook a table');
+ // the check only answers eligibility; a draft typed while it was running keeps its own mode
+ const typing = await run({ allowed: true, typeDuringCheck: 'typed meanwhile' });
+ assert.deepEqual(typing.log.options, { switchMode: false });
+ assert.deepEqual(typing.log.modes, []);
+ assert.deepEqual(typing.log.sent, []);
+ assert.equal(typing.input.value, 'typed meanwhile\n\nbook a table');
});
test(`${browser}: a declined send never refills another tab's composer; the task goes back to its own tab`, async () => {
@@ -2679,3 +2685,10 @@ test('round-82: the Apps popup reads one appsPinned key everywhere', () => {
assert.doesNotMatch(apps, /AppsPinned/);
assert.equal((apps.match(/getItem\('appsPinned'\)/g) || []).length, 3);
});
+
+test('round-83: ensureActMode can answer eligibility without switching the panel mode', () => {
+ for (const browser of ['chrome', 'firefox']) {
+ const src = readFileSync(path.join(root, 'src', browser, 'src/ui/sidepanel.js'), 'utf8');
+ assert.match(src, /async function ensureActMode\(\{ switchMode = true \} = \{\}\)[\s\S]*?if \(switchMode\) setMode\('act'\);/, browser);
+ }
+});