Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions apps/agent/entrypoints/background/scheduledJobRuns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,34 +109,13 @@ export const scheduledJobRuns = async () => {
throw new Error(`Job not found: ${jobId}`)
}

const backgroundWindow = await chrome.windows.create({
url: 'chrome://newtab',
focused: false,
state: 'minimized',
type: 'normal',
})

// FIXME: Race condition - the controller-ext extension sends a window_created
// WebSocket message to register window ownership, but our HTTP request may arrive
// at the server before that registration completes. This delay is a temporary fix.
// Proper solution: ControllerBridge should wait/poll for window ownership registration.
await new Promise((resolve) => setTimeout(resolve, 1000))

const backgroundTab = backgroundWindow?.tabs?.[0]

if (!backgroundWindow || !backgroundTab) {
throw new Error('Failed to create background window')
}

const jobRun = await createJobRun(jobId, 'running')
const abortController = new AbortController()
runAbortControllers.set(jobRun.id, abortController)

try {
const response = await getChatServerResponse({
message: job.query,
activeTab: backgroundTab,
windowId: backgroundWindow.id,
signal: abortController.signal,
})

Expand All @@ -163,13 +142,6 @@ export const scheduledJobRuns = async () => {
})
} finally {
runAbortControllers.delete(jobRun.id)
if (backgroundWindow.id) {
try {
await chrome.windows.remove(backgroundWindow.id)
} catch {
// Window may already be closed
}
}
await updateJobLastRunAt(jobId)
}
}
Expand Down
14 changes: 9 additions & 5 deletions apps/server/src/agent/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,15 @@ function getPageContext(
'\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.'
const windowRef = options.scheduledTaskWindowId
? `\`windowId: ${options.scheduledTaskWindowId}\``
: 'the `windowId` from the Browser Context'
prompt += `\n2. **Always pass ${windowRef}** when calling \`new_page\` or \`new_hidden_page\`. Never omit the \`windowId\` parameter.`
prompt +=
'\n3. **Do NOT close your dedicated hidden window** (via `close_window`). It is managed by the system and will be cleaned up automatically.'
prompt +=
'\n4. **Do NOT create new windows** (via `create_window` or `create_hidden_window`). Use your existing hidden window for all pages.'
prompt += '\n5. Complete the task end-to-end and report results.'
}

prompt += '\n</page_context>'
Expand Down
Loading