# Remember Window Size and Position in an Electron App

Yes. An agent makes your Electron app reopen at the size, position and maximized state it was closed with, clamped to a connected display, with Jest tests and a PR.

[Run this on Rush](https://cloud.getrush.ai/run/remember-window-state/electron/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the main process before it stores anything. In an Electron 44 app shaped like electron-react-boilerplate, src/main/main.ts creates one BrowserWindow in createWindow with show false, width 1024 and height 728, an icon from the assets path and a preload script, shows it on ready-to-show unless START_MINIMIZED is set, nulls mainWindow on closed, builds the menu through MenuBuilder, and quits on window-all-closed except on macOS, where activate recreates the window. startAutoUpdates runs once after the first window. There is no persistence of any kind: every launch opens at the same 1024 by 728 in the operating system's default position.

The state goes in a small module of its own, src/main/window-state.ts, so it can be tested without Electron. It reads and writes a JSON file under app.getPath('userData'), holding x, y, width, height and isMaximized, and exposes two functions: one that returns BrowserWindow options from the file, with the defaults from main.ts when the file is missing or malformed, and one that attaches listeners to a window. Saving happens on resize and move, debounced, and on close, using getNormalBounds when the window is maximized so the un-maximized size is what gets remembered; the write goes to a temporary file first and is renamed into place, so a crash mid-write never leaves half a JSON file for the next launch to choke on.

The part that goes wrong in practice is a saved position on a monitor that is no longer attached. The module checks the stored bounds against screen.getAllDisplays, which is only safe after the app is ready, and falls back to the defaults when no display contains at least part of the saved rectangle, and it clamps width and height to the work area of the display it lands on. The agent also keeps the window hidden until ready-to-show as today, so the restored bounds apply before anything is painted, and it leaves the macOS activate path to call the same createWindow so a reopened window restores too.

Tests follow the pattern the repository already has. src/__tests__/updates.test.ts mocks electron and electron-updater with jest.mock, calls jest.resetModules before each case and declares a node environment in a docblock, because jest.config.js defaults to jsdom for the renderer tests and resolves ts-jest from the separate .erb/tooling install. The agent adds src/__tests__/window-state.test.ts in that style: a mocked app.getPath pointing at a temporary directory, a mocked screen with one display, cases for a missing file, a corrupt file, a round trip, an off-screen rectangle and a maximized window. Then it runs what .github/workflows/test.yml runs on Ubuntu after npm ci: npm run lint, npm exec tsc with noEmit, npm test, and the smoke test under xvfb-run, which packages the app with electron-builder first and checks that Electron starts.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Make this Electron app remember its window size, position and maximized state between launches. Read package.json, release/app/package.json, electron.vite.config.ts, jest.config.js, tsconfig.json, src/main/main.ts, src/main/util.ts, src/main/updates.ts, src/__tests__/updates.test.ts and .github/workflows/test.yml first. Add src/main/window-state.ts that stores x, y, width, height and isMaximized as JSON under app.getPath('userData'), written to a temporary file and renamed into place; returns BrowserWindow options with the current 1024 by 728 defaults when the file is missing or invalid; validates the saved bounds against screen.getAllDisplays after the app is ready and falls back to defaults when they fit no display; saves on debounced resize and move and on close, using getNormalBounds when maximized; and re-applies maximized state. Wire it into createWindow in main.ts without changing the ready-to-show or START_MINIMIZED behaviour. Add src/__tests__/window-state.test.ts in the style of updates.test.ts with a node environment and mocked electron, covering a missing file, a corrupt file, a round trip, an off-screen rectangle and a maximized window. Run npm ci, npm run lint, npm exec tsc -- --noEmit, npm test and the smoke test under xvfb-run as test.yml does, and open a pull request that explains the clamping rule and quotes the test output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request on its own branch that adds src/main/window-state.ts and src/__tests__/window-state.test.ts and edits src/main/main.ts in createWindow to spread the restored options into the BrowserWindow constructor and attach the save listeners. No dependency is added: the module uses app, screen and node:fs, and the debounce is a timer in the module. The description explains the file location per platform, the clamping rule and what happens when the file is corrupt.

The session read names the repository and the pull request URL. The transcript at GET /o/{org}/sessions/{id}/messages keeps each command from test.yml with its output, the first failing run of the new tests against a window-state module that did not yet exist, and the smoke test start under xvfb, so a reviewer sees that Electron launched with the change without packaging it themselves.

Before merging, a reviewer runs npm start on the branch, moves and resizes the window, quits and relaunches, then unplugs or disables a display and relaunches again to see the fallback. They also check that getNormalBounds is what gets saved while maximized, since saving the maximized rectangle would make the restored window fill the screen without being maximized.

## Run it on every Linear issue labelled agent

Add an automation to the Project with this prompt and a linear trigger on linear.issue.labeled, with the label you reserve for the agent. Labelling an issue starts one run with the issue added after the prompt, so a report that the app always opens in the corner reaches the agent as context. Changes made by an app integration never fire the trigger, and the automation's max_runs_per_hour cap bounds how many runs it starts.

From your own server, the same run starts with this request when your Linear webhook handler sees the label, with the issue URL in resources beside the repository and a request_id that is a UUID v5 of the delivery so a retried webhook returns the run already started. A webhook URL on the key reports run.completed with data.session_url for the runs that key starts; a reviewer who wants the debounce shorter sends a follow-up on the same session.

## Why run it on Rush?

This is main-process code, and main-process code in this template is only exercised by the packaged smoke test, which runs electron-builder and then starts Electron under a virtual display on Linux. Doing that on a laptop while writing the module means a long package step for every attempt. The agent runs the unit tests, the type check, the lint and the smoke test in its own sandbox with xvfb available, and the output is kept.

The proof is the transcript and the tests. The failing first run, the passing run, and the smoke test start are on the session beside the diff, so a reviewer can tell what was exercised from what was typed. The result is a branch and a pull request, and the merge is a person's decision.

## Frequently asked questions

### Where is the window state stored?

In a JSON file under app.getPath('userData'), which Electron resolves per platform and per app name from the productName in the build section of package.json. The pull request names the path on each platform. Deleting the file restores the 1024 by 728 default.

### What happens when a saved position is on a monitor that is gone?

The module compares the saved rectangle with screen.getAllDisplays after the app is ready. If no display contains part of it, the defaults are used, and width and height are clamped to the work area of the display the window lands on, so it never opens off screen or larger than the screen.

### Does it use electron-store or electron-window-state?

Not by default. The job is a few dozen lines over app, screen and node:fs with an atomic write, and the repository's tests already mock electron directly, so a dependency would add more than it saves. If your app already uses electron-store for settings, say so in the prompt and the agent stores the bounds there instead.

### Will the maximized state come back?

Yes. isMaximized is saved on close, and on launch the window is created at the remembered normal bounds and then maximized before ready-to-show, so the first paint is already the maximized window. Un-maximizing returns to the saved normal size because getNormalBounds is what was stored.

## More Electron jobs

- [Review Pull Requests on an Electron App](https://getrush.ai/run/review-pull-requests/electron/)
- [Run a Security Audit on an Electron App](https://getrush.ai/run/security-audit/electron/)
- [Upgrade Electron and npm Dependencies in an Electron App](https://getrush.ai/run/upgrade-electron/electron/)

## More jobs like this: Turn a Linear label into a PR

- [Reproduce a Bug from a Linear Issue in a Next.js App](https://getrush.ai/run/reproduce-linear-bug/nextjs/)
- [Reproduce a Bug from a Linear Issue in a Phoenix App](https://getrush.ai/run/reproduce-linear-bug/phoenix/)
- [Reproduce a Bug from a Linear Issue in a Rails App](https://getrush.ai/run/reproduce-linear-bug/rails/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
