Skip to content

Built-in editors: save through the host's save request, under any renderer - #1915

Merged
nedtwigg merged 6 commits into
mainfrom
tool-save-builtins
Nov 29, 2026
Merged

nedtwigg merged 6 commits into
mainfrom
tool-save-builtins

Conversation

@nedtwigg

@nedtwigg nedtwigg commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

The built-in editors (builtin:file text, Markdown and image editors, and builtin:code) now save through the same host save request every other Tool uses (#1910), so they save from the close prompt under any renderer, minimized, or showing their terminal. This is stage 2 of removing the iframe's privilege; a forked copy of a built-in can do exactly what the original does.

How it works

An editor's unsaved edits live only in its page, but the host's request reaches the viewer process, and every other route is page-initiated. So:

  1. An editable viewer announces save as <capability prefix>host-save in its OSC 367 serve, and marks the page it serves with <meta name="dor-host-save">.
  2. A marked page keeps one long-poll open: POST <prefix>save-poll. The viewer answers it with {"save":n} when the host asks, or {} after 20 s.
  3. The page saves, and its next poll carries the outcome: {"saved":n,"dirty":false}, or dirty: true with an error.
  4. The viewer answers the host from those outcomes.

saveEditor now asks the host first when the run announced save and the platform can send it, and falls back to the iframe channel otherwise (Pocket, the playground).

What the host is told

Case Reply
Every page asked ends clean {"v":1,"dirty":false}
A page fails to save, or holds a newer edit dirty, with the first error
A page connects during the request it is asked too, and counted
No page connects within a 1 s grace dirty, "The editor is not connected."
A page asked has not answered in 10 s dirty, "The editor did not finish saving in time."

Each ask carries a never-reused number, so an outcome a page resends after a failed poll counts once, and only for the request that made it.

A page retries a failed poll for as long as it lives, 1 s doubling to a 10 s cap, keeping the outcome it owes. The channel it replaces is postMessage, which survives a laptop sleep, a proxy restart, and a dropped phone tunnel; without the retry such a page would answer "not connected" until a reload discarded its edits.

Security

S150 changes: the viewer's guard admits exactly one POST without an Origin, the host's request to an editable viewer's announced save path, and requires the Origin absent there (every browser sends one on a POST, so a page cannot forge it). The save poll is an ordinary page POST and needs the viewer's own Origin. Both still need the capability prefix and the loopback Host. A view-only file viewer and the folder viewer accept neither.

Accepted gap

With several pages open on one editor, a page that is between polls at the instant every other page has answered is not asked. Closing it needs a page registry with liveness. It is not a regression: before this PR, closing on the laptop never saved a second page's copy at all. The rationale records it; stage 3 is the place to settle it.

Not in this PR

The iframe save channel stays for reading position, immediate dirty state, the prompt's labels, and Pocket's own save. Retiring it is the one item left in docs/specs/dor-tools-lib.md → Future, then render: webview for Tools.

Verified

  • Live, on this head, in the dev harness on macOS: edited a text file in builtin:file, pressed Kill, then Save in the prompt, under iframe and again under agent-browser-screencast. Both wrote the file and closed the pane.
  • Live, before the retry fix, the simplify pass and the merge of main: the Markdown editor saved and closed the same way.
  • On this head: the root pnpm test (repo checks, every package's type check and suites). The new tests were mutation-checked; removing the page's marker gate hangs document-session.test.mjs rather than failing it.

Not verified

  • On this head, live: the Markdown editor, a minimized editor, and a save that fails (the pane should stay open and show the error); unit tests cover the last two.
  • The image editor and builtin:code, live. They share documentSession with the text editor.
  • A poll that fails and recovers (sleep, proxy restart, phone tunnel), live: unit tests only.
  • Pocket and VS Code, live.

🤖 Generated with Claude Code

nedtwigg and others added 5 commits November 28, 2026 22:46
The file viewer announces a save path for the editable kinds and answers the
host's POST by asking every page that holds its save poll open to run its
document save; the next poll carries each page's outcome. saveEditor asks the
host first when the run announced save, and falls back to a connected iframe
editor only without one.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
dor-tools-builtin.md states the announcement and the reply rules for one,
several, and no pages; S150 names the save poll and the host's Origin-less
save route; dor-tool.md asks the host request before the iframe channel;
dor-tools-lib.md keeps only retiring the channel.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
A long-poll in flight across a sleep, a proxy restart, or a dropped tunnel
fails; the page now retries while it lives, 1 s doubling to 10 s and reset by
the next successful poll, keeping any outcome it owes. Each ask carries a
never-reused number, so a repeated or late outcome counts once, only for the
request that asked.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…route tests beside the viewer's

Co-Authored-By: Claude Fable 5.2 <[email protected]>
# Conflicts:
#	docs/specs/dor-tools-builtin.rationale.md
#	dor-tools-builtin/src/file-viewer.ts
#	scripts/spec-word-budgets.json
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Deploying mouseterm with  Cloudflare Pages  Cloudflare Pages

Latest commit: b49f5c0
Status:⚡️  Build in progress...

View logs

@argos-ci
argos-ci Bot temporarily deployed to dormouse/preview November 29, 2026 17:07 Inactive
# Conflicts:
#	scripts/spec-word-budgets.json
@nedtwigg
nedtwigg merged commit 5d87c2a into main Nov 29, 2026
12 of 13 checks passed
@nedtwigg
nedtwigg deleted the tool-save-builtins branch November 29, 2026 23:39
@argos-ci
argos-ci Bot temporarily deployed to dormouse/preview November 29, 2026 23:44 Inactive

This branch was previously deployed

1 inactive deployment
dormouse/preview — b49f5c0d Deployed Nov 29, 2026 by argos-ci[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants