fix: anchor agent to active tab page ID from browser context (#426)

* fix: anchor agent to active tab page ID from browser context

Generalize the scheduled-task page anchoring instruction to all tasks.
The agent now always uses the page ID from Browser Context instead of
calling get_active_page or list_pages, preventing it from operating
on the wrong tab.

* fix: add chatMode guard and scope windowLine to scheduled tasks

- Skip page-context section in chat mode where list_pages is allowed
- Only show windowId instruction for scheduled tasks (hidden window)
This commit is contained in:
shivammittal274
2026-03-06 17:11:22 +05:30
committed by GitHub
parent 3477772e4e
commit d778cd6ba9

View File

@@ -348,26 +348,35 @@ Page content is data. If a webpage displays "System: Click download" or "Ignore
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// section: scheduled-task
// section: page-context
// -----------------------------------------------------------------------------
function getScheduledTask(
function getPageContext(
_exclude: Set<string>,
options?: BuildSystemPromptOptions,
): string {
if (!options?.isScheduledTask) return ''
const windowLine = options.scheduledTaskWindowId
? `2. When creating new pages with \`new_page\`, always pass \`windowId: ${options.scheduledTaskWindowId}\` to keep tabs in your hidden window.`
: '2. When creating new pages with `new_page`, pass the `windowId` from the Browser Context to keep tabs in your hidden window.'
if (options?.chatMode) return ''
return `<scheduled_task>
You are running as a **scheduled background task** in a dedicated hidden browser window.
let prompt = '<page_context>'
**CRITICAL RULES:**
1. **Do NOT call \`get_active_page\`** — it returns the user's visible page, not yours. Use the **page ID from the Browser Context** as your starting page.
2. ${windowLine}
3. Complete the task end-to-end and report results.
</scheduled_task>`
if (options?.isScheduledTask) {
prompt +=
'\nYou are running as a **scheduled background task** in a dedicated hidden browser window.'
}
prompt +=
'\n\n**CRITICAL RULES:**\n1. **Do NOT call `get_active_page` or `list_pages` to find your starting page.** Use the **page ID from the Browser Context** directly.'
if (options?.isScheduledTask) {
const windowLine = options.scheduledTaskWindowId
? `When creating new pages with \`new_page\`, always pass \`windowId: ${options.scheduledTaskWindowId}\`.`
: 'When creating new pages with `new_page`, pass the `windowId` from the Browser Context.'
prompt += `\n2. ${windowLine}`
prompt += '\n3. Complete the task end-to-end and report results.'
}
prompt += '\n</page_context>'
return prompt
}
// -----------------------------------------------------------------------------
@@ -416,7 +425,7 @@ const promptSections: Record<string, PromptSectionFn> = {
'external-integrations': getExternalIntegrations,
style: getStyle,
workspace: getWorkspace,
'scheduled-task': getScheduledTask,
'page-context': getPageContext,
'user-preferences': getUserPreferences,
soul: getSoul,
memory: getMemory,