Skip to content

Tools: save and dirty state leave the iframe channel; the viewer tracks its pages - #1923

Merged
nedtwigg merged 14 commits into
mainfrom
tool-close-label
Nov 30, 2026
Merged

nedtwigg merged 14 commits into
mainfrom
tool-close-label

Conversation

@nedtwigg

@nedtwigg nedtwigg commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Save and unsaved state no longer travel over the built-in editors' iframe channel. Every Tool, built-in or forked, now reports dirty through OSC 367 state and saves through the host save request (#1910, #1915), under any renderer. The channel that remains carries only Markdown reading position, and is renamed the reading-position channel. This is stage 3 of removing the iframe's privilege.

Why it took more than deleting the channel

The channel was covering gaps in the path meant to replace it. This PR closes them first.

Gap on main Fix
Dirty was one bit per Tool, last report wins. A second page loading the same document (the phone's frame reaches the same viewer) reported the Tool clean over the first page's edits. Each page names itself in its reports; the Tool is dirty while any page is.
A page's dirty report was sent once; a failed one was lost. A page delivers its latest state until it lands, retrying with the poll's backoff.
A Markdown link's open could print before the page's own dirty report. The link waits for the report.
The close prompt named a file only while its iframe was mounted; under a screencast renderer or minimized it said "Unsaved Tool". The label comes from what the Wall holds for the Surface.
The phone learned its page was dirty only through the channel. The directory entry carries dirty (protocol-v1, additive); the phone's own "Save changes?" step is gone.
The website playground saved only through the channel. It answers the host save request in the window.

How a page leaves

A page reports clean on pagehide, with a keepalive request. Silence never clears a dirty page: one that sleeps, loses its tunnel, or crashes stays dirty until it reports again or the viewer exits. This errs toward prompting. The viewer prints every dirty report, a repeat included, so an edit is never hidden behind a page that vanished.

Not built: two live copies of one document

One document live on the laptop and the phone at once, kept coherent between them, is not a use case (decided in review). An earlier commit tracked each page's save state, remembered departed pages, bounded their count, and waited for a dirty page between polls; a later commit removes all of it. The save round is main's: it asks the pages polling at that moment. dor-tools-builtin/src/host-save.ts is main's file plus a set of dirty page ids, 26 lines added.

Accepted:

  • A dirty page between polls when every asked page has answered is not asked, as on main.
  • A page that vanishes without its clean report (app killed, tunnel gone) leaves the Tool reading dirty until the viewer exits: an extra prompt, whose Save still works.
  • A dirty report in flight as its page unloads can land after the clean one, with the same result.

Pocket

  • DirectoryBrowser.dirty is filled by the Burrow from its dirty store, and a flip re-lists the directory.
  • Closing a Tool from the phone shows the Burrow's unsaved answer directly, with the laptop's label. Save there saves the phone's page too, while it is connected.
  • The phone keeps a frame loaded while its entry is dirty.

Not built: asking the Tool before acting

An earlier commit on this branch added a serve field state and had the host ask a Tool's dirty state before every close, kill, reload, navigation and preview retarget. A later commit removes it. Measured in the dev harness on macOS: the OSC report reached the host 4 to 6 ms after the retired channel's message, while one ask cost about 70 ms of ps plus lsof, uncached, on every action (about 600 ms on Windows). The host goes by the last report, as it already did for every non-iframe renderer. The rationale records the measurement. S150, S311, the launch latch and dor-tools-lib/src/osc.ts are unchanged from main.

What a user could notice

  • The close prompt names the file under every renderer, and names a third-party Tool by its pane title where it used to say "Unsaved Tool".
  • On the phone, one prompt instead of two.
  • In the playground, the close prompt's Save works for the built-in editors.
  • A Tool that announces no save can no longer be saved from the prompt through its frame; the prompt says to save it in its own view. No built-in is in that position.
  • A dirty report now reaches the host a few milliseconds later than the channel delivered it inside an iframe.

Verified

  • Live, on this head, in the dev harness on macOS (Chromium):
    • Text editor under iframe: edit, Kill, the prompt names the file, Save writes it and closes the pane.
    • Changing the renderer on a dirty editor prompts; after Discard the page reloads clean. On the same viewer, a later edit then shows unsaved and an in-page save clears it, so the old page's pagehide report arrived.
  • Live, on an earlier head only (before the page tracking was cut back; not re-run):
    • The close prompt's label and Save under agent-browser-screencast.
    • A dirty preview is kept and the next dor open --preview opens a new slot.
    • Markdown: scroll to the end, follow a link, Back restores the scroll position over the slimmed channel.
  • Tests, on this head (main merged in): pnpm test:checks, pnpm test:audit, pnpm test:packages, and pnpm test:lib (7,471 passed, 24 skipped), run one after another. New tests were mutation-checked.

Not verified

  • Pocket, on a real phone: the dirty bit over a tunnel, the single prompt, eviction, and whether a pagehide report survives the frame-origin relay. Unit tests only.
  • Two pages on one editor, live. Unit tests only.
  • The playground in a browser: the 25 s poll relay and Save through it. Unit tests only; the service worker change has none.
  • pagehide in WebKit and WebView2. The live check ran in Chromium.
  • Reading position under the screencast renderers, which use the host-owned path this PR does not change.
  • VS Code and Tauri, live.

🤖 Generated with Claude Code

nedtwigg and others added 11 commits November 29, 2026 17:57
A dirty Tool shown under a screencast renderer, minimized, or showing its
terminal had no connected editor, so its close prompt named it "Unsaved
Tool". The label now comes from the Surface's leaf meta through its Wall
handle (`toolTarget`, else the title, the expression IframePanel passed
before), whatever renders it and whether or not it is mounted. The same
label answers a remote Client's `unsaved` close.

connectToolEditor and the editor map no longer carry a label. Pocket
labels its own unsaved copy from the directory entry's title at close.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
A viewer's Tool was one dirty bit, the last page's report winning, so the
phone's page saving marked the Tool clean while the laptop's page still
held edits; and a host save asked only the pages waiting in a poll at that
instant, missing a dirty page between polls.

Each editor page now names itself by a random id in every state report and
save poll. `editorPages` (host-save.ts, replacing `hostSaves`) reports the
Tool dirty while any page last reported dirty, printing the first report
and each change of that union. A page leaves only by saying so on
`pagehide`, in a keepalive POST through the same guarded state route;
silence never clears it. A page that left is remembered so its late
reports count for nothing, and one restored from the back/forward cache
takes a new id. A host save asks every dirty page, waiting for one between
polls until its next, and is clean at once with no dirty page, so the
not-connected grace is gone. The registry refuses a page past 64.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…g pages

The viewer never clears a page on silence and prints only when the union of
its pages changes, so a lost report stuck: a lost clean report kept the page
dirty after a save, and the page's next edit printed nothing; a lost dirty
one hid its edits. A page now delivers its latest state until it lands, one
report at a time, each attempt sending the state as it is then, retrying
with the save poll's backoff (shared as `untilSent`). A failure that recovers
shows nothing.

A host save round also asks every page with a poll open, including one that
starts polling mid-round: it answers at once from its own state, which covers
a registry left stale by a report still under way.

A Markdown link's open now waits for the page's state report to land, so the
viewer prints `state` before `open`.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
A Tool's dirty report travels its PTY stream and can lose a race with a
destructive host action: edit, then at once select another file or press
Back, and the interrupt can reach the Tool before its `state` OSC reaches the
host.

A `serve` announcement may now carry `state`, a path validated as `save`
is. The host sends the save request there (same `TOOL_SAVE_REQUEST`, same
reply, same host request through `saveTool` with a `kind` of `save` or
`state`), and the Tool only reports. The launch latch, the renderer's
announce store, and `requestToolSave` track both endpoints and re-check the
one asked for its kind; every restriction on the save request applies. The
ask has its own deadline, `TOOL_STATE_TIMEOUT_MS` (1 s), end to end in the
renderer and for the host's HTTP exchange.

`refreshToolsDirty` asks and records the reply as a save reply is recorded,
a clean answer never clearing a dirty report that arrived during the ask; a
Tool it cannot ask, or whose ask fails or times out, keeps the store's value.
`confirmToolEditorsClose`, `withToolEditorConsent`, a close that can prompt
(Kill, a clean built-in's immediate close), and a preview retarget ask
first; a slot that answers dirty is pinned and kept.

The built-in viewers announce `host-state` and answer it from their pages'
union without asking a page; the guard admits it exactly as `host-save`.
No transport changed: each passes the request through as it was.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…ce close ask

The state answer comes from the Tool's own record, which can lag a report
still being retried, while a dirty store came from a real report: only the
ordered OSC stream clears it. `askToolState` records a dirty answer alone,
for the run and endpoint asked; the save reply's handling is unchanged.

`dor kill` asks before its refusal check, and a Workspace close asks every
member once before its one question, so no member turns dirty mid-loop.
`dor skill tools` says when the host asks.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…annel

A Tool's page on the phone is one of its viewer's pages, so the Burrow's
`unsaved` answer covers it and `unsaved: 'save'` saves it through the
viewer. The phone's own-edits step goes, with the `title` its close took and
the label lookup that fed it, and `PocketBrowserFrame` connects only the
theme channel: the phone imports nothing from `tool-editor` or the dirty
store.

The directory marks a serving Tool's `browser` `dirty` (additive, absent
when clean) while it reports unsaved changes, and re-lists when that flips.
The phone keeps a frame so marked past `MAX_FRAMES_PER_SESSION`, and never
drops the frame shown last before the current one, whose report may still
be on its way.

A Client's close asks the Tool's state first, as the laptop's does.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The playground's editor announcements carry `save` and `state`, and
`installPlaygroundFs` installs a `saveTool` that answers both from that
viewer's pages, no HTTP: the path's token must name the run's own viewer and
the route its kind. Its reply goes through the wire's own reader and
`toolSaveResult`, which `requestToolSave` now shares.

The save poll and the `dor-host-save` meta move into `editorRoutes`, so the
playground's pages poll as the Node viewer's do; a request may carry the
signal that ends its poll. The service worker relay gives a save poll 25 s,
past the viewer's 20 s hold, keeping 5 s for every other route.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Nothing needs the channel for save or dirty state since the host's save and
state requests reach every built-in under every host. It loses `save`,
`saved`, `state`, and the dirty field; `connect` is answered by a `ready`
that carries nothing, and the three reading-position messages remain.

`connectToolFrame` takes the page's reading position alone, and only the
Markdown editor connects it. The host's half moves out of `tool-editor.ts`
into `tool-navigation.ts` as `connectToolView`; `IframePanel` connects only a
Tool that has a reading position, without the `title` reconnect that guarded
dirty reports. `saveEditor` loses its channel fallback: without a host save
request, Save says to save in the Tool's own view.

It is the reading-position channel now, in code, the package, and the specs.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
- Shared helpers: `announcedEndpoints` builds both kinds' endpoints for the
  launch latch and the announce store; `askToolState` races through
  `settleAllWithin`; `editorPages().request(kind)` holds the rule that a state
  request never saves, for the Node viewer and the playground alike;
  `hostPaths` builds the announced save and state paths for both.
- `refreshToolsDirty` skips a Tool the store holds dirty already, since an
  answer can only raise the bit: native teardown and cross-window drag, which
  pass only dirty ids, ask nothing again.
- Remove `HOST_SAVE_META`: every editor page carried it, so it no longer told
  pages apart. Remove the phone close dialog's `saving` and `error`, and the
  story for them, which only the retired own-edits step set.
- Rename `ToolSaveEndpoint` / `tool-save-endpoint.ts` to `ToolEndpoint` /
  `tool-endpoint.ts`, now that the endpoint is either kind.
- Specs: cut mechanism the branch added (page ids, retry cadence, round
  waiting, eviction detail, playground refusals, label wording); ratchet
  dor-tool, dor-tools-builtin, remote-api, and tutorial back down.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The ask cost about 70 ms of ownership scan per close and preview switch on
macOS (about 600 ms on Windows) to close a window the OSC report leaves of
4–6 ms, and it added a `serve` field to keep. Measured in the dev harness,
2026-11; the trade is recorded in the dor-tool rationale.

Removed: the `serve` field `state` and its docs; the built-in viewers'
`host-state` route, its guard admission, and `request(kind)`; the
playground's state answer; `kind` on the save request, `TOOL_STATE_TIMEOUT_MS`,
and the per-kind endpoints in the launch latch, owner PTY, sidecar host,
VS Code router, and announce store; `refreshToolsDirty` and every caller
(close prompts, consent, Kill, `dor kill`, Workspace close, preview retarget,
a Client's close).

Every file the ask alone had touched is back to main's text, `getSaveEndpoint`,
`getToolSaveEndpoint`, and `tool-save-endpoint.ts` included, as are rules S150
and S311, the audit prompt, and the launch-latch canary. The registry, reliable
reports, the link's wait, Pocket's directory bit, the playground's save, and
the slimmed reading-position channel stay.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@cloudflare-workers-and-pages

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

Copy link
Copy Markdown

Deploying mouseterm with  Cloudflare Pages  Cloudflare Pages

Latest commit: d82796f
Status: ✅  Deploy successful!
Preview URL: https://6d530571.mouseterm.pages.dev
Branch Preview URL: https://tool-close-label.mouseterm.pages.dev

View logs

@argos-ci
argos-ci Bot temporarily deployed to dormouse/preview November 30, 2026 04:39 Inactive
nedtwigg and others added 3 commits November 29, 2026 22:05
# Conflicts:
#	scripts/spec-word-budgets.json
Two live copies of one document is not a supported use, so the viewer
keeps only the ids of the pages that last reported dirty; the Tool is
dirty while any did. The save round returns to main's logic (ask the
pages polling, a short grace, an error when none is connected), and the
poll no longer names its page. A page reports clean as it unloads and
reports again when restored from the back/forward cache, keeping its id.
Drops the page map, per-page asks, the remembered departures, the page
bound, and Pocket's rule keeping the frame shown last.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The host records a Tool clean after its own save request. With a page
that vanished without reporting clean still counted, a later edit changed
nothing the viewer printed, and the host read the Tool clean over it.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@argos-ci
argos-ci Bot temporarily deployed to dormouse/preview November 30, 2026 06:34 Inactive
@nedtwigg
nedtwigg merged commit 64b80ef into main Nov 30, 2026
19 checks passed
@nedtwigg
nedtwigg deleted the tool-close-label branch November 30, 2026 06:41

This branch was previously deployed

1 inactive deployment
dormouse/preview — d82796f5 Deployed Nov 30, 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