DEV Community

Cover image for My agent builds its own windows and checks them by screenshot
Fred 🍓
Fred 🍓

Posted on

My agent builds its own windows and checks them by screenshot

At the start of November someone whose opinion I value looked at my app and said it felt like too many menus. I counted. 16 windows, a dozen overlays, 24 actions in the command palette. Fair point.

The obvious fix would have been one more panel, the one he was missing. But the panels I'd want myself are small and personal: "which session is waiting for me", "the open tasks in this project", "how many tokens did I burn today". I can't build all of them, and I shouldn't.

So I gave Claude Code a way to build them itself.

VibeDesk is a desktop app around Claude Code. It already had an MCP server for things like arranging windows. I added six tools to it: desk_app_create, desk_app_snapshot, desk_app_code, desk_app_list, desk_app_open and desk_app_delete. You ask in any session, "build me a window that shows which session needs me", and a few seconds later it's on your desktop, in the same glass look as everything else.

The one on my own desktop is a list of open tasks from the project's notes, newest first, with a filter and checkboxes to tick them off. Exactly the kind of window I'd never have built as a feature for everyone.

What a call looks like

Claude sends three strings and a name (shortened here):

{
  "name": "Waiting on me",
  "html": "<div class=\"row\"><h2>Waiting on me</h2><span class=\"spacer\"></span><span id=\"today\" class=\"muted small\"></span></div><ul id=\"l\" class=\"list\"></ul>",
  "code": "vd.watch('sessions', (all) => vd.mount('#l', all.map((x) => vd.h('li', { class: 'row' }, ...))))",
  "css": ""
}
Enter fullscreen mode Exit fullscreen mode

html is the body, without any <script>. code is JavaScript that runs after it. The window already has VibeDesk's styles loaded, so a button, a .badge or a .dot.waiting looks native without Claude writing CSS. The tool description lists those classes in one paragraph, plus a one-line example.

The script talks to a global vd object, and that's all it gets:

  • await vd.data('sessions' | 'brain' | 'usage' | 'project') reads, and vd.watch(kind, fn) calls fn on every change. Read only.
  • vd.store.get/set is the window's own storage, at most 200 kB of JSON.
  • vd.h(tag, props, ...children) builds elements. Text never gets parsed as HTML.

The loop: build, report, look

The app saves the files, opens the window and waits up to 6 seconds for it to report back. The window only reports "ok" after it has run for 500 ms without an error, so an error after the first await still makes it in. Only then does the tool return.

I broke a window on purpose to show it. I changed u.today.tokens to u.totals.tokens, a field that doesn't exist. The tool came back after 584 ms with:

Tool window "Waiting on me" saved (id: waiting-on-me, 460×250).
ERROR: Cannot read properties of undefined (reading 'tokens')
Fix it and call desk_app_create again with id "waiting-on-me" (only the changed parts are needed).
Enter fullscreen mode Exit fullscreen mode

(Translated. The real tool texts are in German, which is my language. Claude doesn't mind.)

The error also shows up in the window, with a button:

The broken window: the session list renders, and a red bar at the bottom shows the error and a "Let Claude fix it" button

"Let Claude fix it" sends the error as a prompt to the Claude session whose terminal is on top. If that session is busy, the prompt waits in its queue.

Then there's desk_app_snapshot. It brings the window to the front, asks the renderer where exactly it sits, takes a picture of only that rectangle with Electron's capturePage (scaled down to 1,100 px wide at most) and returns it to Claude as an image content block. Claude sees what you see.

That turned out to be the important part. Code that runs without an error can still look wrong: a row that wraps, a badge in the wrong color, a list that's empty because the data has a different shape than Claude assumed. Without the picture, "it runs" is all Claude knows. I first built this loop for animated backgrounds, and it was the biggest quality jump there too.

Fixed, sent again with only code changed, 620 ms:

The fixed window: three sessions, api waiting for you, shop-frontend working, docs-site done, 96 k tokens today

That's a test instance with demo projects, not my real desktop.

Where the walls are

Claude writes the code, but I still don't want code I haven't read running inside my app. So the window has no way out, and each layer is meant to hold even if another one fails.

An iframe with sandbox="allow-scripts" and nothing else. No allow-same-origin, so the frame gets an opaque origin. It can't reach the app's DOM, the preload API, cookies or storage. It can't open popups, submit forms or navigate the top window.

A strict CSP on every file. The window loads from a custom protocol (vibedesk-scene://app/<id>/…), and each response carries:

default-src 'none'; script-src vibedesk-scene:; style-src vibedesk-scene: 'unsafe-inline';
img-src vibedesk-scene: data: blob:; font-src data:; form-action 'none'; base-uri 'none'
Enter fullscreen mode Exit fullscreen mode

connect-src falls back to 'none', so no fetch, no XHR and no WebSocket. No outside scripts, and no inline scripts either.

WebRTC is gone. CSP doesn't cover it. Before Claude's code runs, the runtime replaces RTCPeerConnection and its relatives with undefined, as non-configurable properties, so they can't be put back.

The frame stays where it is. If a script sets location.href to a web page, the main window's own CSP (frame-src 'self' vibedesk-scene:) blocks the load.

Data is a short list. The renderer only answers messages whose source is that exact iframe. There are four kinds of data, all read only. Pushes are throttled to one per 800 ms and only go out when something changed. Code is capped at 150,000 characters and there's a limit of 40 windows.

What went wrong on the way

Every error said "Script error." In a sandboxed iframe, every script counts as cross-origin, even your own, so window.onerror hides the message. The fix: crossorigin="anonymous" on every script tag and access-control-allow-origin: * on the protocol's responses. Then the real message comes through.

Inline handlers fail silently. The CSP blocks onclick="..." without an error the window could report, so the button just does nothing, and the window still says "ok". Now desk_app_create rejects <script> and on...= in the HTML up front, with a message that says to use addEventListener in code instead.

Top-level await costs you line numbers. Claude's code is wrapped in an async function so it can await vd.data(...) right away. Anything that throws inside it becomes a rejected promise, and an unhandledrejection has no line number at all. That's why the error in my broken example above says what went wrong but not where. Errors in event handlers still arrive as normal error events, one line off because of the wrapper, and the runtime subtracts that line again.

My navigation guard never fired. I had a handler on Electron's will-frame-navigate to keep the frame from leaving. For a plain location.href change in the iframe, it didn't fire. The main window's CSP blocked the navigation anyway, so nothing leaked. An end-to-end test now checks exactly that. A fake web server on 127.0.0.1 was useless as a test, by the way: Chromium's Private Network Access blocks that on its own.

What this doesn't cover

  • The window can still show you anything. It can draw a fake dialog that asks for a password. It can't send what you type anywhere, but it can put it in its own storage.
  • The repair button forwards text from the window to Claude. The error message comes from the window's own code. Today Claude wrote that code itself, so it can only prompt itself. If windows ever become shareable, that button is a prompt injection path, and it needs a closer look before then.
  • Read only, local only. A window can't start a session, write a note or call GitHub. That rules out a lot of dashboards people would want. It's deliberate for now.
  • The snapshot needs VibeDesk on screen. If the app is minimized, the tool says so instead of returning an empty image.
  • New tools only show up in new sessions. Claude Code reads the MCP config when a session starts.

This is on main now and ships with VibeDesk 2.1 on December 13. The status side of it, which session is waiting, I wrote about here.

Question

Which window would you want your agent to build for you?

Or pick one:

A) a dashboard: sessions, tokens, cost
B) a tracker for one project: tasks, decisions, checklists
C) a calculator or converter for something I do every day
D) nothing, I want fewer windows, not more

(I build VibeDesk, a free, open-source Windows desktop for Claude Code. The tool windows live in src/main/appStore.ts, src/main/appTools.ts and resources/app-runtime.js if you want to read the actual code.)

Top comments (5)

Collapse
 
ayushdhanani369 profile image
Ayush Dhanani •

Your line about code that runs without an error still looking wrong is the part I kept coming back to. A screenshot is the only check that catches that.

I build Aladdin, a job agent. I use the same idea in a smaller way. A fresh worker reads the actual page and form for each application instead of trusting a success message. If it cannot read the form or an answer is unknown, it asks the user instead of guessing.

For your question, I would pick B. A tracker for one project, with tasks and decisions, is the window I would actually check every day.

Collapse
 
fxdgif profile image
Fred 🍓 •

Thanks. "Instead of trusting a success message" is a good way to put it: a success message tells you the code finished, not that it did the right thing.

Asking instead of guessing is the part my loop doesn't have. Nothing in it asks me. Claude fixes the window and takes another snapshot until it decides on its own that it looks right. For a job application, asking is clearly the right default, and I suspect it is for a new window too, at least the first time.

B is what ended up on my own desktop as well.

How does your worker decide that an answer is unknown? Only when nothing in the profile matches the field, or also when it found something but isn't sure it fits?

Collapse
 
ayushdhanani369 profile image
Ayush Dhanani •

It is also the second case. The worker only uses an answer when it is clearly covered by the facts the user gave. If something is found but it is not clear it fits the question as worded on the form, that counts as unknown, and it asks the user instead of guessing. A reworded question that changes the meaning also counts as not clearly known. The fresh worker reads the actual wording on the form each time, and that answer is then saved.

Thread Thread
 
fxdgif profile image
Fred 🍓 •

That's stricter than I expected, and it makes sense: "found something" and "it fits this question" are two separate checks, and it's easy to stop after the first. Reading the wording fresh each time also covers forms that change between applications.

For my windows the same rule would be: if Claude can't tell from the snapshot whether it matches what I asked for, it asks me instead of polishing one more round.

How often does a reworded question actually change the meaning in practice? Is that rare, or does it come up a lot?

Thread Thread
 
ayushdhanani369 profile image
Ayush Dhanani •

It is not rare enough to ignore. The cases I see most are small wording changes that change the answer. Years in a specific role is not the same as years of experience. Salary expectation is not the same as desired salary range. I do not have a clean rate to give you, because I do not track it as a separate count. When the wording is close but the answer could change, the worker asks instead of reusing the old answer.