Skip to content

fix: remove duplicate hidden window and improve scheduled task prompt#496

Merged
shadowfax92 merged 2 commits intomainfrom
fix/hidden-windows-use-in-schedule
Mar 13, 2026
Merged

fix: remove duplicate hidden window and improve scheduled task prompt#496
shadowfax92 merged 2 commits intomainfrom
fix/hidden-windows-use-in-schedule

Conversation

@shadowfax92
Copy link
Contributor

@shadowfax92 shadowfax92 commented Mar 12, 2026

Summary

  • Removed duplicate hidden window creation — the frontend (scheduledJobRuns.ts) was creating a minimized Chrome window that the server (ChatService) immediately overwrote with its own hidden window. Two windows per scheduled task, only one used.
  • Improved scheduled task prompt — expanded windowId rules to cover both new_page and new_hidden_page, added rules forbidding window closure/creation

Changes

scheduledJobRuns.ts (-28 lines)

  • Removed chrome.windows.create() — server handles window lifecycle
  • Removed 1-second race condition delay hack (FIXME)
  • Removed chrome.windows.remove() cleanup in finally block
  • Removed windowId/activeTab params to getChatServerResponse()

prompt.ts (+9 lines, -5 lines)

Test plan

  • Run a scheduled task and verify only 1 hidden window is created (by the server)
  • Verify the agent passes windowId to page creation calls
  • Verify the agent doesn't close its hidden window mid-task
  • Check list_windows during scheduled task shows exactly 1 hidden window

🤖 Generated with Claude Code

@shadowfax92
Copy link
Contributor Author

@greptileai review

@github-actions github-actions bot added the fix label Mar 12, 2026
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 12, 2026

Greptile Summary

This PR fixes the scheduled task agent's prompt rules so that the windowId constraint is enforced on both new_page and new_hidden_page, and adds explicit guardrails preventing the agent from closing its dedicated hidden window or spawning new windows mid-task.

Key changes:

  • new_hidden_page(url, windowId?) is now included in the getCdpToolReference() Page Management section for MCP prompt consumers, matching the existing new_page entry
  • getPageContext() rule 2 now explicitly covers both new_page and new_hidden_page when injecting the windowId requirement
  • Two new rules added for scheduled tasks: prohibit close_window on the dedicated hidden window (rule 3) and prohibit creating new windows via create_window / create_hidden_window (rule 4)
  • The old "Complete the task end-to-end" rule shifts from rule 3 → rule 5
  • create_hidden_window is correctly called out in rule 4 but is itself missing from the MCP tool reference section, along with show_page (referenced in the new new_hidden_page description) — both are minor consistency gaps worth addressing

Confidence Score: 4/5

  • Safe to merge; changes are scoped to prompt engineering with no runtime logic modifications.
  • The PR makes targeted, low-risk prompt text changes to fix a real behavioral gap. The logic is correct — both tools are now covered for windowId, and the new window-management prohibitions are sensible. Two style-level gaps exist in the MCP tool reference (create_hidden_window and show_page are referenced by name but not listed), which don't affect runtime correctness for the ToolLoopAgent but are worth cleaning up for MCP consumers.
  • apps/server/src/agent/prompt.ts — minor MCP tool reference omissions worth addressing

Important Files Changed

Filename Overview
apps/server/src/agent/prompt.ts Expanded scheduled task prompt rules to include new_hidden_page windowId requirement and added window management guardrails; create_hidden_window is referenced in Rule 4 but missing from the MCP tool reference section in getCdpToolReference().
apps/server/package.json Routine patch version bump from 0.0.73 to 0.0.74.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[buildSystemPrompt called] --> B{isScheduledTask?}
    B -- No --> C[Standard page_context: Rule 1 only]
    B -- Yes --> D{scheduledTaskWindowId provided?}
    D -- Yes --> E["Rule 2: Always pass windowId: <id>\nfor new_page / new_hidden_page"]
    D -- No --> F["Rule 2: Always pass windowId\nfrom Browser Context\nfor new_page / new_hidden_page"]
    E --> G["Rule 3: Do NOT close\ndedicated hidden window"]
    F --> G
    G --> H["Rule 4: Do NOT create\nnew windows"]
    H --> I["Rule 5: Complete task\nend-to-end"]
    C --> J[Emit page_context block]
    I --> J
Loading
Prompt To Fix All With AI
This is a comment left during a code review.
Path: apps/server/src/agent/prompt.ts
Line: 176-181

Comment:
**`create_hidden_window` missing from MCP tool reference**

The Window Management section in `getCdpToolReference()` lists `create_window(hidden?)` but omits `create_hidden_window`, which exists as a distinct callable tool (`tools/windows.ts:79`). The newly added Rule 4 in the scheduled task context correctly references it by name ("Do NOT create new windows (via `create_window` or `create_hidden_window`)"), so MCP prompt consumers can follow the prohibition — but they wouldn't discover the tool from the reference itself.

Since the PR's explicit goal is to improve MCP prompt tool coverage (adding `new_hidden_page`), it would be consistent to add `create_hidden_window` here too:

```suggestion
## Window Management
- `list_windows` - Get all browser windows
- `create_window(hidden?)` - Create a new browser window
- `create_hidden_window` - Create a new hidden browser window (not visible to user, take_screenshot not supported)
- `close_window(windowId)` - Close a browser window
- `activate_window(windowId)` - Activate (focus) a browser window
```

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: apps/server/src/agent/prompt.ts
Line: 144

Comment:
**`show_page` referenced but not listed in tool reference**

The new `new_hidden_page` entry correctly notes "use `show_page` first", but `show_page` itself is not listed anywhere in `getCdpToolReference()`. MCP prompt consumers won't know the tool name or its signature unless they discover it via tool schemas. Consider adding it to the Page Management section alongside `new_hidden_page`:

```suggestion
- `new_hidden_page(url, windowId?)` - Open a hidden background page. Not visible to user. take_screenshot not supported — use show_page first.
- `show_page(page, windowId?, index?, activate?)` - Restore a hidden page to a visible window. Required before take_screenshot on hidden pages.
```

How can I resolve this? If you propose a fix, please make it concise.

Last reviewed commit: 946cd4d

shadowfax92 and others added 2 commits March 12, 2026 16:53
The agent prompt only told the agent to pass windowId with `new_page`
but not `new_hidden_page`, which the agent prefers for background work.
The agent also had no instruction against closing or replacing its
dedicated hidden window, causing pages to scatter across uncontrolled
windows.

Expanded the scheduled task prompt rules to:
- Cover both `new_page` and `new_hidden_page` windowId requirement
- Forbid closing the dedicated hidden window
- Forbid creating new windows
- Added `new_hidden_page` to tool reference for MCP consumers

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…tend

The server's ChatService already creates a hidden window for scheduled
tasks (chat-service.ts:99-126), but the frontend (scheduledJobRuns.ts)
was also creating a minimized Chrome window that the server immediately
overwrote. This caused two windows to be created per scheduled task run,
with only one being used.

Removed from scheduledJobRuns.ts:
- chrome.windows.create() call
- 1-second race condition delay hack (FIXME)
- chrome.windows.remove() cleanup
- windowId/activeTab params to getChatServerResponse()

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@shadowfax92 shadowfax92 force-pushed the fix/hidden-windows-use-in-schedule branch from 946cd4d to 45efa8d Compare March 12, 2026 23:59
@shadowfax92 shadowfax92 changed the title fix: scheduled task agent not using hidden window for new pages fix: remove duplicate hidden window and improve scheduled task prompt Mar 12, 2026
@shadowfax92 shadowfax92 merged commit 2d1e989 into main Mar 13, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant