Skip to content

Desktop: assign each profile a runtime origin - #403

Merged
priethor merged 9 commits into
mainfrom
priethor/desktop-ephemeral-runtime-origin
Jul 31, 2026
Merged

Desktop: assign each profile a runtime origin#403
priethor merged 9 commits into
mainfrom
priethor/desktop-ephemeral-runtime-origin

Conversation

@priethor

@priethor priethor commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

What

Cortext Desktop now gives each profile its own loopback port instead of assuming 9402. It takes the first free port in a fixed band, reuses it across launches, moves when that port is occupied, and rejects requests whose Host or supplied Origin does not match. Starting Cortext again focuses the existing window instead of opening a second runtime against the same SQLite database.

Why

Changing the port on every launch would discard origin-scoped preferences and strand the local Notion key. A saved port per profile avoids fixed-port collisions without resetting that state on normal restarts. The band exists for the same reason. A port assigned by the operating system comes from the range the kernel recycles for outbound sockets, which is a poor place to park a port we intend to keep.

How

  • Selecting from 9403-9498, which the kernel never hands out on its own, and falling back to whatever the operating system offers only when the whole band is busy.
  • Trying the legacy port once for existing profiles so upgrades keep their previous Chromium origin.
  • Defining WordPress's home and site URLs from the active origin before the preserved config loads.
  • Binding every runtime to 127.0.0.1 and retrying once if the selected port is claimed during startup.

A collision that forces a new origin leaves browser-only preferences and the local Notion key under the old one. WordPress settings and uploads are unaffected, but blocks with embedded media keep the previous origin in their markup, so those images need re-inserting. Content stores absolute URLs today, which is also a problem for syncing documents with a remote site. This PR does not change that.

Testing Instructions

  1. Build the desktop snapshot and launch Cortext with a fresh --user-data-dir. Confirm that the window opens on a port between 9403 and 9498.
  2. Note the port in that profile's settings.json, change a browser-backed preference, quit, and relaunch the same profile. Confirm that the URL keeps the same port and the preference remains.
  3. Quit Cortext, occupy the saved port with another local server, and relaunch. Confirm that Cortext opens on another port in the band and your documents still open.
  4. Run curl -i <runtime-origin>/wp-includes/images/blank.gif without a token and confirm that it returns 403 Forbidden.
  5. Start Cortext a second time with the same profile and confirm that the second process exits while the existing window comes forward.

I used Claude to help implement these changes. I guided the work, then tested and reviewed the result.

@priethor priethor added area: desktop Desktop app, PHP runtime, snapshots, and desktop packaging. type: enhancement Improvement to existing behavior. labels Jul 28, 2026
@priethor
priethor force-pushed the priethor/desktop-ephemeral-runtime-origin branch from 0a0e69b to 5f3d085 Compare July 29, 2026 12:59
priethor added 7 commits July 30, 2026 17:30
listen(0) hands back a port from the ephemeral range, which is exactly where
the kernel draws from for outbound sockets. That is right for a port that
lives seconds and wrong for one we save across launches, since local servers
and proxies churn through that range constantly. 9403-9498 is never handed
out spontaneously, so only software that binds there explicitly can take a
saved port. Port 0 stays as the last resort.

Also fixes the collision note in the docs. Blocks with embedded media keep
the previous origin in their markup, so those images do need re-inserting.
@priethor
priethor force-pushed the priethor/desktop-ephemeral-runtime-origin branch from 9ed56b2 to 4a01b44 Compare July 30, 2026 15:31
@priethor
priethor merged commit 41f5a00 into main Jul 31, 2026
15 checks passed
@priethor
priethor deleted the priethor/desktop-ephemeral-runtime-origin branch July 31, 2026 09:58
@github-actions github-actions Bot added this to the 0.2.0 milestone Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: desktop Desktop app, PHP runtime, snapshots, and desktop packaging. type: enhancement Improvement to existing behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant