Skip to content

Tools: save any Tool from the close prompt, through a path it announces - #1910

Merged
nedtwigg merged 16 commits into
mainfrom
tool-save-request
Nov 29, 2026
Merged

nedtwigg merged 16 commits into
mainfrom
tool-save-request

Conversation

@nedtwigg

@nedtwigg nedtwigg commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Any Dor Tool can now be saved from the close prompt, under any renderer. Until now only the built-in editors could, through a page-level channel that only their iframe was given; every other Tool, and every Tool shown through agent-browser or Playwright, closed with "Save this Tool in its own view". That privilege was never intended. This is stage 1 of removing it.

How it works

  1. A Tool announces a save path beside its port in its OSC 367 serve:
    printf '\033]367;serve;{"v":1,"port":%s,"save":"/%s/save"}\007' "$PORT" "$TOKEN"
  2. When the user presses Save for that Tool, the host sends POST /<token>/save to the Tool's own port on loopback, typed application/json, with the body {"v":1}.
  3. The Tool saves and answers 200 with {"v":1,"dirty":false}, or {"v":1,"dirty":true,"error":"Disk full"} to keep the pane open and show its reason.

Save asks the first that applies: a connected built-in editor's iframe channel (unchanged), then the host request, then the old "save it in its own view" refusal. It works for a minimized Tool and one showing its terminal, because it does not depend on the panel.

No OSC 367 verb is added: save is a field of serve. dor-tools-lib gains the request body, the reply's encoder and its parser, and stays dependency-free. dor skill tools documents it.

Why HTTP to the Tool's own server, not terminal input

Writing the request to the Tool's terminal was the first idea and was rejected: nothing reads a Tool's stdin (the built-ins do not, and third-party Tools sit behind pnpm or are TUIs), a raw-mode reader stops receiving Ctrl+C as an interrupt, which the reap and retarget rely on, and unread bytes land on the shell's command line (S124). Every Tool already has a server. The rationale entry has the detail.

Who may ask, and what is checked

Audited as S311 in docs/specs/security-local.md → "Dor Tool configuration".

  • The PTY owner authorizes the save, not the renderer. The host keeps a save endpoint only from a serve printed during the run it launched itself, the same gate open requests pass (ToolLaunchLatch). Any run boundary discards it. The renderer's request must equal that live endpoint, and the endpoint must still be the same one after the port scan and again after the reply.
  • The window must own the Session. Standalone refuses a save from a window that does not own the Tool; VS Code checks PTY ownership.
  • The port must be the Tool's own. The host rescans the Session's process tree when it sends, and connects to the numeric loopback address that listener holds, never localhost.
  • The exchange is bounded. No redirect is followed, a 101 upgrade is a refusal, the HTTP exchange has a 15 s deadline, and the reply is read to at most 4,096 UTF-8 bytes. The Tool's error is shown bounded with terminal controls removed.
  • The path is percent-encoded as a URL would be, so a non-ASCII announced path is sent as UTF-8 escapes.
  • Windows with a Tool inside WSL gets no host save. The host cannot tell a WSL listener from a native one on Windows loopback, so it refuses whenever a WSL listener holds the save port.

The token in the path is the Tool's to choose; the guide tells authors to make it unguessable and fresh each run, since any page in the user's browser can reach a loopback port. scripts/outbound-lint.mjs classifies the new client call as loopback, with a self-test case.

A reply's dirty is recorded at once for the run that was asked, so the prompt's "no newer edits" check does not race the Tool's OSC state report. A clean reply never clears a dirty report that arrived while the request was out.

Not in this PR

The built-in editors still use the iframe channel. Remaining stages, recorded in docs/specs/dor-tools-lib.md → Future: the built-ins announce save and answer the request; then the iframe channel is retired once reading position, the prompt's document label and Pocket's own save have another way. A Tool saved through the host is labelled "Unsaved Tool" in the prompt until then.

Review history

After the first review this branch took a second, adversarial review that produced nine findings, all fixed here: authorization moved from the renderer's word to the PTY owner's launched run, window ownership, URL encoding, byte-accurate reply bounds, deadlines that include the port scan on every bridge, WSL provenance, and replies matched to the announcing run. A simplify pass followed each round.

Verified

  • Live, in the dev harness on macOS:
    • Before the second review: Save under agent-browser-screencast and iframe, a minimized Tool under both, an error reply, a Tool with no save, and a Tool that never answers (timeout after about 15 s).
    • After its fixes: Save and close end to end with an announced non-ASCII path.
  • On this head: the repo checks, lib (tsc -b and its suite), dor, dor-tools-lib, vscode-ext, standalone (tsc --noEmit and its suites), the sidecar suites, and the 185 Rust library tests. tool-save.test.ts runs the request against a real loopback server for each bound above.
  • New tests were mutation-checked.

Not verified

  • The last simplify commit (23 lines, including how the request URL is built and when the latch exposes its endpoint) has unit tests only; no live run since.
  • Windows and WSL on a real machine: that coverage is simulated socket tables on macOS. VS Code, live.

🤖 Generated with Claude Code

nedtwigg and others added 8 commits November 28, 2026 20:14
…e request

serve gains an optional save path, read under the path rule. dor-tools-lib
carries the request body and the reply's type, parser, and encoder. The new
lib/src/host/tool-save.ts POSTs {"v":1} to that path, on loopback, only at a
port the Session's process tree listens on, without following redirects,
within 15 s, reading at most 4 KiB.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Standalone (Tauri adapter -> tool_save -> sidecar), the browser-dev harness,
and VS Code (tool:save -> message router) each hand the request to the shared
requestToolSave with that Session's own port scan. A failure is never a
rejection. outbound-lint classifies the request as loopback, with its
self-test case.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
saveEditor keeps a connected editor's save channel first; otherwise a Tool
whose live announcement carries save, serving on its announced port (which
ToolPanel registers), is asked through PlatformAdapter.saveTool. Its answer is
recorded as the dirty state at once, unless the announcing run has ended.
Anything else keeps the save-in-its-own-view refusal.

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

The guide test now parses the save example's announcement and both replies.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
dor-tool.md: serve's save field and the close prompt's order (channel, host
request, refusal), with the rejected alternatives in the rationale.
security-local.md: S311 and its guarantee row under Dor Tool configuration,
which the local audit prompt now reads with Closing unsaved Tools.
dor-tools-lib.md: the osc entry point's save helpers; the Future item becomes
moving the built-ins to the request and retiring the iframe save channel.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The serving-port registry ToolPanel filled tied Save to a mounted panel: a Tool
minimized before it ever mounted (a restored or born-minimized Door) got
"save in its own view". The gate is now the live run's announcement with its
save path and port; run end retires it, and the host checks the port. Wall
tests cover a never-mounted Door and a Tool showing its terminal context.
Comments that cited a spec for a fact they state drop the citation.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
- requestToolSave picks the loopback address with Tool serving's own
  listenerUrlsByPort instead of a copied wildcard map; validates the
  request inline (readToolSaveRequest and loopbackAddressFor go); takes
  the batched port scan both hosts already expose, so the sidecar and
  the extension host pass it directly; times out with AbortSignal; and
  a non-2xx status is an error from post itself.
- TOOL_SAVE_TIMEOUT_MS sits beside the bridge timeout, which is now
  derived from it.
- Drop tests another test already pins: the Wall block (saveEditor reads
  no mount state; tool-editor.test.ts covers it), the adapter forwarding
  tests (the contract covers failure), pty-core's openPortsFor (its
  getOpenPortsMany test runs it), the cancelled-prompt copy, the
  constant-restating cases; fold the timeout tests and the off-loopback
  cases into the scan-refusal test.
- Specs: dor-tool.md no longer restates S311's from-the-host rule or Run
  end's retirement; the rationale's PTY alternative keeps its deciding
  reasons. Callers' repeated port-check comments go.

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

# Conflicts:
#	docs/specs/terminal-escapes.md
#	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: 9b63b0a
Status: ✅  Deploy successful!
Preview URL: https://fb5871cd.mouseterm.pages.dev
Branch Preview URL: https://tool-save-request.mouseterm.pages.dev

View logs

@argos-ci
argos-ci Bot temporarily deployed to dormouse/preview November 29, 2026 05:13 Inactive
nedtwigg and others added 8 commits November 28, 2026 21:46
- ToolLaunchLatch hides its save endpoint outside a run instead of
  clearing it at every boundary; only a start resets it.
- requestToolSave encodes the path against the listener's own URL
  (listenerUrlsByPort) rather than respelling the origin; the router
  uses ownsPty.
- One run-end test covers a new run at the same address, through the
  real forgetToolReports boundary.
- dor-tool.md: the launched-run rule for `save` joins the `open` rule
  under OSC 367; the reply rule says the dirty is recorded at once; the
  WSL refusal is stated where its rationale points. Guide says a WSL Tool
  on Windows gets no host save. transport.md drops the tagging mechanism.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@argos-ci
argos-ci Bot temporarily deployed to dormouse/preview November 29, 2026 06:24 Inactive
@nedtwigg
nedtwigg merged commit 41b462a into main Nov 29, 2026
19 checks passed
@nedtwigg
nedtwigg deleted the tool-save-request branch November 29, 2026 06:25

This branch was previously deployed

1 inactive deployment
dormouse/preview — 9b63b0ac 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