Dor Tools

The Dor Tool protocol

Status: 0.1. This is the protocol as Dormouse implements it. It may change before 1.0.

A Dor Tool is a command that runs in a terminal and serves a page. A host is a terminal that runs the command and shows its page in the same pane. Dormouse is the first host. This document is what a second host, or a Tool written without this repository's packages, needs to agree on.

A run is one execution of the command, from its start to its exit. Everything a Tool tells a host holds for that run alone, and the host forgets it when the run ends.

What this document says a host or a Tool does is a rule for all of them; what it says Dormouse does is that one host's choice. Every length is counted in UTF-8 bytes.

The two sides talk over four channels. Only the save request is answered:

Channel Direction Carries
Escape sequences in the Tool's terminal output Tool to host everything the Tool tells the host
The URL of the page's first request host to Tool a state to restore
One HTTP request to the Tool's own port host to Tool "save now", and the Tool's answer
The terminal's interrupt host to Tool "stop now"
flowchart LR
  tool["Tool: a command and its server"]
  host["Host: a terminal and a page view"]
  tool -- "OSC 367: serve, state, open, dehydrate" --> host
  host -- "GET the page, first URL carries rehydrate" --> tool
  host -- "POST: save now" --> tool
  host -- "interrupt: stop now" --> tool

What a host must do

A Tool never asks what its host supports. It sends the messages it has a use for, and a Tool that never sends one never sees what that message brings. So the burden is on the host: a host does everything a Tool would notice missing.

A Tool sends Every host
serve with a port shows the page, once it has checked that the port is the Tool's
state, and a save path marks the pane unsaved, asks before closing it, and can ask the Tool to save
open opens the named file in a pane other than the Tool's
open with place: "self" interrupts the Tool and puts the target in its pane, keeps a history, and offers Back and Forward once there is one
open without trusted: true asks the user before opening a file that would run script of its own
open with a fragment hands the fragment to the run it starts for the target
dehydrate, on the host's interrupt hands that state to the run it starts in the Tool's place

A host may leave out only what a Tool cannot notice:

A Tool sends A host may
dehydrate: true stop the Tool while it is out of sight and start it again later with its state; a host that never does has left nothing out
open with place: "preview" reuse one preview pane; a host without one opens the file as any other

Messages

A Tool writes a message to its terminal as one escape sequence. The spaces here are for reading; none is written:

ESC ] 367 ; <verb> ; <json> BEL

Terminal output is not trusted. A file printed with cat, or a program on another machine, can write these bytes too. A host must act on a message only when it comes from the output of a command the host itself started as a Tool, during that run. It must treat every field as untrusted text: bound it, drop control characters, and never let a path name another machine.

serve

Written once the Tool's server is listening, and again whenever a field changes.

Field Type Meaning
port integer, 1 to 65535 the loopback port to show
path string the path and query to open on that port; default /
save string the path and query on that port for the save request
dehydrate boolean true: this run can be stopped and started again from its arguments alone
name string reserved; hosts read and ignore it

state

{"v": 1, "dirty": true}

Written whenever the Tool's unsaved state changes. A host starts each run not knowing, and takes the latest report. A report is never a save and never permission to stop the Tool.

open

{"v": 1, "path": "/abs/notes/todo.md", "place": "preview", "trusted": true}
{"v": 1, "path": "/abs/notes/other.md", "place": "self", "fragment": "second-heading"}
Field Type Meaning
path string an absolute local path: starting with /, or a drive letter followed by :\ or :/; at most 2,048 bytes, with no control character (C0, DEL, C1)
place "preview", "self", or "new" where it opens: the host's reusable preview pane, this Tool's own pane, or a pane other than this one, which may be one already showing the file; default "new"
trusted boolean true: the user picked this exact file in the Tool's page; default false
fragment string a place in the target, such as a link's fragment, decoded: the text after #; at most 2,048 bytes, with no control character

dehydrate

{"v": 1, "state": {"tab": "open", "scroll": 1240}}

A small state the Tool writes after the host's interrupt, just before it exits. state is any JSON value except null, and means nothing to the host. A Tool with nothing to restore writes {"v": 1} with no state, which tells the host not to wait for one. A host keeps a state only from a run that announced dehydrate: true, only when it is written after the host's own interrupt, and only the last. It keeps the state value and nothing else from the message. Restoring state has the size limit.

The page

Saving

A Tool that announced a save path can be saved from the host's "unsaved changes" prompt.

  1. The host sends POST <save> to the announced port, at the page's address, with Content-Type: application/json, the body {"v":1}, and no Origin header. It sends nothing unless that port is still the Tool's own.
  2. The Tool saves, then answers 200 with {"v":1,"dirty":false}, or with {"v":1,"dirty":true,"error":"Disk full"} to say why it could not.
  3. The host counts it saved only on a 2xx reply with dirty: false and no error, and only if no dirty: true report came while it waited. It waits at most 15 seconds, follows no redirect, and reads at most 4,096 bytes. It takes the reply's dirty as a state report.
sequenceDiagram
  participant P as Tool's page
  participant T as Tool's process
  participant H as Host
  T->>H: serve, with a save path
  P->>T: an edit
  T->>H: state, dirty true
  Note over H: marks the pane unsaved
  Note over H: the user closes the pane and picks Save
  H->>T: POST to the save path
  T->>P: save, in the Tool's own way
  alt saved
    T-->>H: 200, dirty false
    Note over H: closes the pane
  else not saved
    T-->>H: 200, dirty true, and an error
    Note over H: keeps the pane and shows the error
  end

Put an unguessable token in the save path, made fresh each run. It is all that keeps another page from asking. A browser always sends Origin on a cross-site POST, so a Tool also refuses a save request that carries one.

Stopping and restoring

A host stops a Tool with the terminal's interrupt, the one a person sends by pressing Ctrl+C at a running command: the Tool's process receives SIGINT, or CTRL_C_EVENT on Windows.

A host interrupts a Tool Only if
to stop it while it is out of sight its latest serve says dehydrate: true, and it did not report dirty: true
to replace it, after its own open with place: "self", or on Back or Forward the user agreed, when it reported dirty: true
to show another file in a preview pane it did not report dirty: true

A Tool that announced dehydrate: true promises that its command and arguments alone start it correctly: the host runs the same command again, in the same directory. A host that may stop such a Tool may also close its pane without asking. Such a Tool may write a dehydrate message before it exits. The state only adds fidelity: a scroll position, an open tab. It is never a document; unsaved work belongs to state and saving.

Restoring state

The run a host starts in a stopped Tool's place gets the state back in the URL of its page's first request. The host appends one query parameter to the path the Tool announced:

http://<address>:<port><path>?rehydrate=<base64url of JSON>

The host builds the JSON: {"v": 1}, with "state" when it holds one, and "fragment" when an open named one. The value is base64url of the JSON's UTF-8 bytes, with no padding, so it uses only letters, digits, -, and _. For a state of {"top": 40} and a fragment setup:

?rehydrate=eyJ2IjoxLCJzdGF0ZSI6eyJ0b3AiOjQwfSwiZnJhZ21lbnQiOiJzZXR1cCJ9

A path that already has a query gets the parameter after &, and before any #. A Tool's own path and save never have a rehydrate parameter; a host ignores one that does.

A Tool stopped while hidden, and shown again:

sequenceDiagram
  participant T as Tool, first run
  participant H as Host
  participant N as Tool, next run
  T->>H: serve, with a port and dehydrate true
  H->>T: GET the page
  Note over H: the pane goes out of sight
  H->>T: interrupt
  T->>H: dehydrate, with a state
  Note over T: exits
  Note over H: keeps the state
  Note over H: the pane is shown again
  H->>N: runs the same command
  N->>H: serve
  H->>N: GET the page, rehydrate on this first URL
  Note over N: restores from the state
  H->>N: any later load, no rehydrate

A link from one document to a heading in another, then Back. Each run is its own process, and the two Tools need not be the same program:

sequenceDiagram
  participant A as Tool on a.md
  participant H as Host
  participant B as Tool on b.md
  Note over A: announced dehydrate true
  Note over A: the user clicks a link to b.md#second
  A->>H: open b.md, place self, fragment second
  Note over H: checks it can open b.md, and asks first if a.md is unsaved
  H->>A: interrupt
  A->>H: dehydrate, with its position
  Note over A: exits
  Note over H: history is a.md with that state, then b.md
  H->>B: runs the command for b.md in the same pane
  B->>H: serve
  H->>B: GET the page, rehydrate carries the fragment
  Note over H: offers Back, and the user presses it
  H->>B: interrupt
  B->>H: dehydrate, with its position
  Note over B: exits
  H->>A: runs the command for a.md again
  A->>H: serve
  H->>A: GET the page, rehydrate carries the state a.md left

Not in 0.1