@graview/ship 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +312 -13
  2. package/dist/browser.d.ts +4 -4
  3. package/dist/browser.d.ts.map +1 -1
  4. package/dist/browser.js +2 -2
  5. package/dist/browser.js.map +1 -1
  6. package/dist/handler.d.ts +192 -26
  7. package/dist/handler.d.ts.map +1 -1
  8. package/dist/handler.js +383 -340
  9. package/dist/handler.js.map +1 -1
  10. package/dist/health.d.ts +12 -1
  11. package/dist/health.d.ts.map +1 -1
  12. package/dist/health.js +15 -4
  13. package/dist/health.js.map +1 -1
  14. package/dist/index.d.ts +7 -5
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +3 -2
  17. package/dist/index.js.map +1 -1
  18. package/dist/live-protocol.d.ts +451 -0
  19. package/dist/live-protocol.d.ts.map +1 -0
  20. package/dist/live-protocol.js +957 -0
  21. package/dist/live-protocol.js.map +1 -0
  22. package/dist/live.d.ts +156 -9
  23. package/dist/live.d.ts.map +1 -1
  24. package/dist/live.js +31 -1
  25. package/dist/live.js.map +1 -1
  26. package/dist/remote.d.ts +180 -5
  27. package/dist/remote.d.ts.map +1 -1
  28. package/dist/remote.js +793 -76
  29. package/dist/remote.js.map +1 -1
  30. package/dist/runtime.d.ts +7 -5
  31. package/dist/runtime.d.ts.map +1 -1
  32. package/dist/runtime.js +3 -2
  33. package/dist/runtime.js.map +1 -1
  34. package/dist/seat-headers.d.ts +6 -0
  35. package/dist/seat-headers.d.ts.map +1 -1
  36. package/dist/seat-headers.js +6 -0
  37. package/dist/seat-headers.js.map +1 -1
  38. package/dist/serve.d.ts +5 -3
  39. package/dist/serve.d.ts.map +1 -1
  40. package/dist/serve.js +18 -4
  41. package/dist/serve.js.map +1 -1
  42. package/dist/websocket.d.ts +1 -1
  43. package/dist/websocket.d.ts.map +1 -1
  44. package/dist/websocket.js +10 -2
  45. package/dist/websocket.js.map +1 -1
  46. package/package.json +3 -3
package/README.md CHANGED
@@ -42,7 +42,8 @@ declaration plus one persistence adapter is a running deployment.
42
42
  `Response`, for any runtime: a Cloudflare Worker or Durable Object, Deno, Bun. `serveStore`
43
43
  is a thin `node:http` wrapper around it. Import it from `@graview/ship/runtime`, the entry
44
44
  that reaches no `node:` builtin: the store, migrations, the handler and `openRemote`, without
45
- the file adapter or the page's localStorage.
45
+ the file adapter or the page's localStorage. A host that opens, migrates and heals its own
46
+ `Store` hands it over instead of an adapter: `createStoreHandler({ app, store, seatOf, flush })`.
46
47
  - **Content moves as steps.** The step DSL (`stepsMigration`) has five content steps beside
47
48
  the schema ones — `put-node`, `patch-node`, `drop-node`, `put-edge`, `drop-edge` — each
48
49
  judged against the stored graph when it runs, so a default that is already there is not
@@ -69,14 +70,14 @@ so a host in front of it knows what it must keep answering for `openRemote`, `gr
69
70
  | Method | Path | Says |
70
71
  |---|---|---|
71
72
  | GET | `/graview/state` | the graph, the log, the stored version and the modules on |
72
- | POST | `/graview/ops` | calls in, the ops they produced out — or `undo`, batches to take back; 409 with the policy's sentence when refused |
73
+ | POST | `/graview/ops` | calls in, the ops they produced out — or `undo`, batches to take back; a `batch` the asking seat already landed is answered with the ops it made, and one that is somebody else's or not `batch:<tag>:<n>` is refused `invalid`; 409 with the policy's sentence and a `reason` when refused, 429 with `Retry-After` when the host is busy |
73
74
  | GET | `/graview/since?seq=N` | the ops appended after N — everyone else's |
74
75
  | GET | `/graview/health` | ship's own report, plus where the data is |
75
76
  | GET | `/graview/export` | the whole store as one bundle, the way out |
76
- | POST | `/graview/here` | say where you are; answers with who else is, and the ops since `seq` |
77
+ | POST | `/graview/here` | say where you are; answers with who else is, the ops since `seq`, and the `participant` key you are held under |
77
78
  | GET | `/graview/who` | who is here right now |
78
- | POST | `/graview/leave` | say you have gone |
79
- | GET | `/graview/live` | the live wire: a WebSocket of hello/welcome, call/undo/ack/refused/conflict, ops and presence; 426 to a plain request |
79
+ | POST | `/graview/leave` | say you have gone — only ever yourself |
80
+ | GET | `/graview/live` | the live wire: a WebSocket of hello/welcome, call/undo/ack/refused/conflict/busy, ops and presence, declaration and reload; 426 to a plain request |
80
81
 
81
82
  Who is asking is the host's to say: `serveStore({ seatOf })` reads its own credential from
82
83
  the `Request` and returns the principal every call is judged under, or a promise of it. The framework's clients also send the seat
@@ -84,8 +85,14 @@ as headers (`SEAT_HEADERS`: who, as what, by what name, and for whom), and a sto
84
85
  them only when told to — `serveStore({ trustSeatHeaders: true })`, which `graview serve`
85
86
  sets for a server on 127.0.0.1 and says so. A store with neither answers 401 on every route
86
87
  but health. Whatever a host asks for rides along: `openRemote({ headers })` sends them with
87
- every request, and the framework never reads them. A call says what it came through
88
- (`via`: `web` from `openRemote`, `mcp` and `cli` from the commands), recorded on the op. `openRemote(...).settled()` resolves once every call sent
88
+ every request, and the framework never reads them. What a call came through (`via`: `web`,
89
+ `api`, `mcp:Claude`), recorded on the op, is the host's word too: `viaOf(request, seat)` says
90
+ it, or `SEAT_HEADERS.via` where the seat headers are trusted (`openRemote({ via })` sends it,
91
+ `mcp` and `cli` from the commands). A `via` in a call's body or message is a claim, never read
92
+ on its own: `openRemote` sends the one a call was applied with (a guest view applies with
93
+ `view:<name>`), and `liveProtocol({ viaOf: (peer, claimed) => … })` lets a host judge it —
94
+ asked of every change, it answers the via to record, and nothing keeps the host's own. Without
95
+ any of these, a socket's calls are `web` and an HTTP call's `api`. `openRemote(...).settled()` resolves once every call sent
89
96
  so far has been answered — a browser never waits for it; a host that must report the
90
97
  server's verdict before it exits does.
91
98
 
@@ -95,22 +102,224 @@ server's verdict before it exits does.
95
102
  it as it lands. Calls go down it too; while it is down the client polls and posts, reconnects,
96
103
  and catches up from the last op it has. Polling stays: it is the wire `curl` can drive.
97
104
 
105
+ **A host can watch it.** `remote.status()` is `connecting`, `online` or `offline`
106
+ (`RemoteStatus`), and `remote.onStatus(listener)` is told each time it changes — an offline
107
+ banner's switch. It is offline while a socket that was welcomed is down and nothing has been
108
+ heard since, or when a poll or a call does not reach the server (a network failure, or a 502,
109
+ 503 or 504 from a gateway); online once a welcome or any answer lands, a refusal included. A
110
+ call made while the server is away is not taken back: it stays shown, `remote.pending()`
111
+ counts it, and it goes again the moment the server is reached. `remote.counters()`
112
+ (`RemoteCounters`) counts `reconnects`, `rebases` (the server's ops landing under pending
113
+ calls), `conflicts` and `resyncs`, for a beacon. Three options shape it: `backoff`, a function
114
+ of the attempt in milliseconds or `{ min, max, factor }` for the jittered default (250, 10000,
115
+ 2); `presenceEveryMs`, a heartbeat down the socket opted into (by default the socket says
116
+ `here` only when where the client stands changes — see below); and
117
+ `visible`, a predicate — while it answers false no presence is said, and in a page it reads
118
+ `document.visibilityState` unless given.
119
+
98
120
  | From | Message | Carries |
99
121
  |---|---|---|
100
- | client | `hello` | `seq`, the last op it has (none: the welcome carries the whole state); `protocol` |
101
- | client | `call` | `cid`, `calls`, `intent`, `batch`, `via`, and `base`: the revision of each field it changes |
102
- | client | `undo` | `cid`, `batches` to take back |
122
+ | client | `hello` | `seq`, the last op it has (none: the welcome carries the whole state); `protocol`; `wire` (`LIVE_WIRE`); `build`, the host's build the page runs |
123
+ | client | `call` | `cid`, `calls`, `intent`, `batch`, `base` (the revision of each field it changes), and `via`: a claim of the channel, such as a guest view's `view:<name>`, which a server records only if it accepts it |
124
+ | client | `undo` | `cid`, `batches` to take back, and a `via` claim as on `call` |
103
125
  | client | `here` / `bye` | a presence, as `/graview/here` takes it; gone |
104
- | server | `welcome` | `protocol`, `seq` (the server's last), and the `ops` after the client's seq |
126
+ | server | `welcome` | `protocol`, `wire`, `version` (the declaration it serves), `build`, `participant` (this socket's own key, built from its seat), `seq` (the server's last), and the `ops` after the client's seq |
105
127
  | server | `ack` | `cid`, `batch`, `seq` and the `ops` the call made |
106
- | server | `refused` / `conflict` | `cid` and the sentence; a conflict names each field, theirs, yours and who wrote theirs |
128
+ | server | `refused` / `conflict` | `cid` and the sentence; a refusal's `reason` and, when the policy knows who could, `wouldNeed`; a conflict names each field, theirs, yours and who wrote theirs |
129
+ | server | `busy` | `cid` and `retryAfter` in milliseconds: not now, and not refused — the client sends the call again after the wait |
107
130
  | server | `ops` / `presence` | everybody's ops as they land, in seq order; who is here |
131
+ | server | `declaration` | `version`: the declaration changed, and the socket is served again after a new hello |
132
+ | server | `reload` | `reason` and `protocol`, the lowest served: the hello's protocol is no longer served |
108
133
 
109
134
  Seqs mean what `/graview/since?seq=N` means, and `hello` and `welcome` carry `WIRE_PROTOCOL`.
110
135
  A host on any runtime attaches a socket with `createStoreHandler(...).connect(request, { send,
111
136
  close })`, which reads the seat from the upgrade and answers with the connection to hand each
112
137
  message to; `serveStore` does this for Node's upgrade. What a seat may not see holds on the
113
- socket as on the routes.
138
+ socket as on the routes, and so does its id (FR-55): no record id the seat may not see is in
139
+ anything it is sent — a welcome, ops, an ack, a conflict, a route's answer. A seen record's
140
+ field that names a hidden one is cleared, or, when the record cannot do without that field,
141
+ the record is withheld from the seat whole; a withheld op keeps no hidden id in any
142
+ primitive. `seatLens(store, principal)` is that judgement, the one every surface reads.
143
+
144
+ A host that hibernates — a Durable Object wakes on a message with no closure left — holds
145
+ each socket's state itself. `liveProtocol({ store })` (from `@graview/ship/runtime`) is the
146
+ same protocol as functions over the store and one plain-JSON `LiveSocketState` per socket,
147
+ `{ seat, via, cursor?, participant?, build?, hostBuild?, held? }`, the thing
148
+ `serializeAttachment` keeps:
149
+
150
+ ```ts
151
+ // A seat's key outlives a wake: kept in the Durable Object's storage, never only in a map in memory.
152
+ // And it is the seat's, not the tab's: seatOfTab(principal) leaves the session out, so every tab is one key.
153
+ const keyOf = (principal) => {
154
+ const seat = seatOfTab(principal);
155
+ const key = `seat:${sha256Hex(JSON.stringify(seat)).slice(0, 24)}`;
156
+ ctx.storage.sql.exec("INSERT OR IGNORE INTO seats (key, seat) VALUES (?, ?)", key, JSON.stringify(seat));
157
+ return key;
158
+ };
159
+ const seatOf = (key) => {
160
+ const row = ctx.storage.sql.exec("SELECT seat FROM seats WHERE key = ?", key).toArray()[0];
161
+ return row ? JSON.parse(row.seat) : undefined;
162
+ };
163
+ const live = liveProtocol({ store, version: app.version, flush, seatOf });
164
+ // On the upgrade: the seat and channel, read once (handler.seatFor(request) does this with seatOf and viaOf).
165
+ ws.serializeAttachment(live.open(keyOf(principal), "web", { build }));
166
+ // On each message, after any wake:
167
+ const peer = { ...ws.deserializeAttachment(), send: (text) => ws.send(text), close: (code, reason) => ws.close(code, reason) };
168
+ const { landed, presence } = await live.receive(peer, text, whoIsHere);
169
+ const { send, ...state } = peer;
170
+ ws.serializeAttachment(state);
171
+ if (landed) live.publish(landed, otherPeers); // each as its own seat sees them, from its own cursor
172
+ ```
173
+
174
+ `publish` and `tell` make each seat's view once, however many sockets it holds: a seat key is
175
+ resolved once, and each run of ops is redacted and written once and sent down every socket of
176
+ that seat (200 ops to 50 sockets of 5 seats: 4.7 ms, where it was 43 ms; 50 sockets of 50
177
+ seats are still 50 views). A withheld op is served under an opaque batch, `withheld:<16 hex>`,
178
+ a keyed hash of its batch — the same for every op of one batch — so a seat cannot tell which
179
+ session made a change it may not see; the seat that made it is served its own. The key is
180
+ drawn once per store held, so a host that wakes holds a new one; `liveProtocol({ withheldKey })`
181
+ keeps it the same across wakes, and is never sent to a client.
182
+
183
+ **The attachment budget.** Cloudflare refuses an attachment over 2,048 bytes, and a seat
184
+ held whole is most of that: a person with fifty roles is about 1.9 kB of state on its own.
185
+ So `seat` may be a host's key (a string) — `live.open("user:6b3f…", "web")` — which
186
+ `liveProtocol({ seatOf: (key) => principal })` resolves on every message. Keep what it reads
187
+ where a wake does not empty it — the sketch above keeps it in the Durable Object's storage; a
188
+ map in memory is empty after the first wake — and make the key from `seatOfTab(principal)`,
189
+ the seat without its session: a key with the session in it is one view per tab where it would
190
+ be one per seat. A key the host no longer resolves is told to open again (`{ t: "error",
191
+ reopen: true }` — "The host no longer knows who this socket is…"), closed with code 4000 when
192
+ the peer has a `close`, and served nothing more; `openRemote` opens a new socket, the host
193
+ reads the seat from its upgrade again, and what the tab had not had answered is sent again and
194
+ lands once. A call's `cid` is at most 64 characters (a
195
+ longer one is refused `invalid`, in words), so `held` stays small, and the client's and the
196
+ host's builds are kept to 64. The budget, held by a test: a socket's state with a seat key,
197
+ a busy call held and the host's own presence beside it stays within 1 KB (measured: 350
198
+ bytes of state, 692 with a presence), half the attachment, the rest the host's.
199
+
200
+ `connect()` is this protocol with the state in memory, so there is one implementation.
201
+ Every `liveProtocol` option is an option of `createStoreHandler` and `serveStore` too, so a
202
+ host gets it either way: `limit`, `admit`, `build` (a string, or a function of the socket),
203
+ `minProtocol`, `minHostProtocol`, `refusal` and `withheldKey` by the same names; and, where
204
+ the handler already has a name that reads the request, the protocol's `viaOf` as
205
+ `viaClaimed` and its `seatOf` as `seatOfKey` — with `seatKey(seat)`, handed the seat without
206
+ its session (`seatOfTab`), the key a socket the
207
+ handler opens keeps in its state instead of the principal.
208
+
209
+ **An ack waits for `flush`, and a failed flush is never acked.** `flush(landed)` is handed
210
+ every op the protocol landed that is not durable yet, in seq order — what just landed, after
211
+ any a failed flush left — so a host that writes ops to a ledger writes exactly those (and
212
+ skips one it is handed again). Flushes run one at a time. When one rejects, the client is
213
+ refused `unavailable` ("Your change could not be saved just now. It is kept, and sent again
214
+ until it is."), keeps the change, and sends it again; the socket's cursor does not move,
215
+ `receive` answers no `landed`, and the ops stay in the store but are held back from every
216
+ socket until a flush holds them — the change sent again, or the next change, which hands
217
+ them over first. A change sent again after a failed flush is never made twice and never
218
+ acked before it is durable. `createStoreHandler({ store, flush })` takes the same `flush`.
219
+ **A change anybody else hears is a change that is written**: while an op's flush is under way
220
+ it is held back from every socket — pushes, and the ops a `hello` is welcomed with — and it
221
+ goes down once the flush resolves (a hibernating host publishes the `landed` that `receive`
222
+ or `post` answers; `createStoreHandler` does it itself). A flush that fails was heard by
223
+ nobody. A host without a `flush` pushes every op the moment it lands. A whole-state read
224
+ (`/graview/state`, a hello without a seq) is the store as it stands in memory.
225
+
226
+ **A host that routes its own requests** answers the routes with meaning through the same
227
+ protocol, so it keeps what `createStoreHandler` does — a batch sent again answered once, a
228
+ stale write a conflict, its `limit`, a refusal's reason, the ops as the seat may see them:
229
+
230
+ ```ts
231
+ // POST /graview/ops, in the host's own router, after it has said who is asking:
232
+ const { status, body, headers, landed } = await live.post(await request.text(), { seat, via: "api" });
233
+ if (landed) live.publish(landed, peers);
234
+ return Response.json(body, { status, headers });
235
+ // GET /graview/state and /graview/since?seq=N, as the seat sees them:
236
+ live.state({ seat, via: "api" });
237
+ live.since(seq, { seat, via: "api" });
238
+ ```
239
+
240
+ `post` answers a `WireAnswer` — `{ status, body, headers }` — and is the handler's
241
+ `POST /graview/ops`: the handler calls it, so the two answer every body the same. `WireAsked`
242
+ is who asks, `{ seat, via, build? }`.
243
+
244
+ **Who is here is the server's to say.** A presence carries its `kind`, a display `name`
245
+ and, for an agent, `onBehalfOf` (the person's id) and `onBehalfOfName`, all built from the
246
+ seat by `presenceFrom`; a client's own claim of any of them, or of another key, is not read.
247
+ A socket's key is built at `hello` and said back as `welcome.participant`, and
248
+ `openRemote(...).participant()` hands it on, so a client leaves itself out of who is here.
249
+ Somebody without a socket — an agent acting over MCP or an RPC, a polling tab — is announced:
250
+ `handler.announce(visitorPresence(seat), ttlMs)` tells every socket and poll at once and
251
+ stands until `until`. The handler does this itself for every op an agent seat lands in its
252
+ store (`POST /graview/ops`, an MCP handler over the same store, the host's own loop), for
253
+ `VISITOR_PRESENCE_TTL_MS`; `createStoreHandler({ announceAgents: false })` turns it off and a
254
+ number sets the time. An agent that only reads lands no op, so hand `handler.onCall` to
255
+ `createMcpHttpHandler`'s `onCall` (`@graview/tools`): every tool call announces the agent seat
256
+ the same way, reads included. A hibernating host keeps `who` itself, with `announcePresence(who,
257
+ visitorPresence(seat), ttlMs)`, and hands it to `receive` and `tell`; a visitor past its
258
+ `until` is never told. `nextExpiry(who)` (from `@graview/core`) says when the next visitor
259
+ goes, in epoch ms, for the host's alarm: on it, `tell` the room again and the visitor is gone
260
+ from every map. The handler keeps that timer itself. Each seat is told as it may see: an agent acting for a person the
261
+ seat may not see is shown without `onBehalfOf` or the name. The Shell draws it as "Claude,
262
+ for Ada" (`presenceName`).
263
+
264
+ **A socket holds its presence; it needs no heartbeat.** The `here` a socket says comes back
265
+ from the server stamped `held: "socket"` (a client's own claim of it is dropped), and such a
266
+ presence stands for as long as the server lists it — `presenceStands` and `foldPresence` do
267
+ not expire it by its `at`. The host drops it when the socket closes, at once. So
268
+ `openRemote` says `here` down a socket only when where it stands changes: an idle tab says
269
+ nothing, and a hibernating host is not woken to hear it. A host that rebuilds `who` from its
270
+ own records cannot drop the stamp: `tell(who, peers)` — and `receive(peer, text, who,
271
+ peers)`, for the presence a welcome is followed by — says `held: "socket"` on every presence
272
+ whose participant one of the sockets it is handed holds, whatever the host built, and says a
273
+ visitor's `until` as the host gave it. A poller is still held by time
274
+ (the handler's own time to live) and says where it is with every poll. A client whose own socket dropped
275
+ lets the held presences it was told go after `REMOTE_PRESENCE_TTL_MS` without a list from
276
+ the server. A host that keeps a socket's presence by time can ask for the old heartbeat
277
+ with `presenceEveryMs`.
278
+
279
+ **The declaration changes under open tabs.** A host that adds a field calls
280
+ `handler.declarationChanged({ app })` — over an adapter the handler opens the store again
281
+ with `openStore`, which migrates it; over a store the host holds it is handed
282
+ `{ app, store, flush?, migrated? }`. Every socket is told `declaration`; a sleeping host
283
+ makes a `liveProtocol` over the new store and calls `declared(peers)`. `openRemote` asks its
284
+ `resolveApp(version)` for the app at that version (a TS app imports it; a document-declared
285
+ app fetches and compiles its document), opens a new remote store on the server's migrated
286
+ state and hands it to `remote.onDeclaration((next, version) => …)`. The calls still on the
287
+ way are offered again there under the batch they were sent in, so one the server already
288
+ made is not made twice, and one that no longer fits is refused in words on `onRefusal`. No
289
+ page reloads. A host whose own store judges by more than the declaration says — an owner
290
+ sight Graview Cloud adds — passes `localApp: (app) => app` to shape the app the browser's
291
+ store is built from, the first one and every one `resolveApp` gives, so the browser refuses
292
+ only what the server would. **The host's wiring goes with it:** every listener put on the first store —
293
+ `onRefusal`, `onConflict`, `onStatus`, `onBuild`, `presence.onWho`, `onDeclaration` — is
294
+ carried to each store that replaces it, and `counters()` run on, so a host writes its
295
+ listeners once and only swaps which store it mounts (one added again is told twice).
296
+ `presence.onWho` tells a listener added late who is here already, at once, and
297
+ `remote.who()` says it any time. A polling client learns it the same way: every answer a
298
+ poll reads (`/graview/state`, `/graview/since`, `/graview/here`, the `/graview/ops` answer)
299
+ says the declaration `version` and the host's `build`, and `openRemote` compares it on each.
300
+ `version` is the host's own monotonic number for the declaration it serves — `app.version`,
301
+ or a host's document version — handed to `liveProtocol({ version })` and, on the client, to
302
+ `resolveApp(version)`: it names one declaration and only ever grows. A `declaration` push or
303
+ a welcome with the number a client already serves is ignored, so a host that pushes twice
304
+ reopens nothing.
305
+
306
+ **Version skew.** A host says its `build` (an opaque string) in every welcome; a page that
307
+ said another in its hello keeps working and `remote.onBuild(…)` is told once. A host with
308
+ `minProtocol` answers an older protocol's hello with `reload`: `openRemote` writes every
309
+ call not yet answered to its `carry` (`{ storage, key }`, `sessionStorage` in a page),
310
+ calls `reloadPage`, and the next `openRemote` with the same `carry` offers them again.
311
+ A host whose build differs per upgrade — the worker that carried the socket says which shell
312
+ it serves — says it per socket: `live.open(seat, via, { build })` keeps it in the socket's
313
+ state as `hostBuild`, or `liveProtocol({ build: (peer) => … })` answers it from the socket.
314
+ A host that changes its own half of the wire (its routing, its auth, its shell) numbers it:
315
+ `openRemote({ hostProtocol })` says the number in `hello.hostProtocol`, and
316
+ `liveProtocol({ minHostProtocol })` (or `createStoreHandler`'s) answers a hello below it —
317
+ absent is 0 — with `reload` carrying `hostProtocol`, so an old page reloads with its calls
318
+ carried exactly as for `minProtocol`. `hello.protocol` stays `WIRE_PROTOCOL`, ship's.
319
+ Two codecs on one path are told apart by `hello.wire`, and by the WebSocket subprotocol
320
+ `LIVE_SUBPROTOCOL` (`graview.ship.1`): `serveStore` answers it, a Worker answers its upgrade
321
+ with `liveSubprotocol(request)`, and `openRemote` sends it with `subprotocol: true` or hands
322
+ it to its `socket` factory. `WIRE_PROTOCOL` stays 1: every message and field is additive.
114
323
 
115
324
  **A stale write is a conflict, not a loss.** A field's revision is the seq of the op that last
116
325
  wrote it (`FieldRevisions`, derived from the log). A call that carries a `base` older than the
@@ -118,6 +327,96 @@ field is refused, before anything is written, naming the field, theirs and yours
118
327
  `openRemote` sends one with every call and hands the refusal to `remote.onConflict(…)`,
119
328
  with `conflict.keepTheirs()` and `conflict.useMine()`.
120
329
 
330
+ **A batch is answered only to whoever made it.** A client names the batch its call lands in,
331
+ so a call sent again after a lost answer is answered with the ops it made the first time —
332
+ and only ever with the asking seat's own ops: a batch that holds somebody else's (another
333
+ person, or the same agent acting for somebody else) is refused `invalid` in words, and so is
334
+ any batch not shaped as a `Store` mints it, `batch:<tag>:<n>` or `undo:<tag>:<n>`
335
+ (`isClientBatch`). A call that names none lands in a batch the server mints outside that
336
+ shape. A host's own store mints there too with `new Store({ batchIds: serverBatchIds() })`,
337
+ so nothing a host lands itself — a migration, a seed, an agent's RPC — is in a batch a client
338
+ could have named first; `createStoreHandler` does this for the store it opens.
339
+ `authoredBy(author, seat)` is the judgement of "the same seat". A batch's tag belongs to the
340
+ first seat that landed under it, so nobody names another client's next batch
341
+ (`batch:<her tag>:<n+1>`) before she does: a batch under somebody else's tag is refused
342
+ `invalid`, and so is one under the store's own minting tag (`store.batchTag`), so even a held
343
+ store that still mints `batch:<tag>:<n>` cannot have its next batch named first. **A host
344
+ holding its own store should still give it `batchIds: serverBatchIds()`**: then its batches
345
+ are not in a client's shape at all, and a reader of the log can tell a client's from the
346
+ host's.
347
+
348
+ **A refusal says why** (FR-46). Every `refused` on the socket, and every refusing answer of
349
+ `POST /graview/ops` (409, or 413 at a host's cap), carries a `reason` from a closed set,
350
+ `REFUSAL_REASONS`, which never changes meaning:
351
+
352
+ | `reason` | What it means |
353
+ |---|---|
354
+ | `forbidden` | the seat may not: the policy, an agent's declared acts, a module turned off (for the host's own seat); `wouldNeed` names the roles that could, when the policy knows them |
355
+ | `missing` | what the call names is not there for this seat: a record that does not exist or one it may not see — answered alike, so a refusal never tells a guessed id from a real one (FR-55) — or a batch to take back |
356
+ | `invalid` | the call as asked does not fit: its arguments, the kind, a rule, a call before `hello` |
357
+ | `limit` | the host's hard cap: the call can never succeed as asked, however long the caller waits |
358
+ | `unavailable` | the host takes no changes for a while and cannot say how long — a room read-only while it is checked, a write to storage that failed; the one refusal that is not final |
359
+
360
+ `openRemote`'s `remote.onRefusal((sentence, refusal) => …)` is handed the reason beside the
361
+ sentence, and `remote.send` throws a `RemoteRefusedError` carrying it. What a person reads is
362
+ said for them (`wireRefusalOf`): arguments that do not fit field by field, in the words the
363
+ form asked in ("“Add a task” was not made: Name — …"), and an act the app does not have by
364
+ the name the call gave and no other ("This app has no act called “dance”."). A host words a
365
+ refusal itself with `liveProtocol({ refusal: (error, calls, peer) => … })`, on the socket
366
+ and through `post`: it answers the `{ reason, sentence, wouldNeed? }` to send, or nothing
367
+ for ship's. The routes' other
368
+ refusing answers say one too: 401 `forbidden`, 404 `missing`, 400 `invalid`.
369
+
370
+ **Busy is not refused** (FR-45). A host's `limit` option — on `createStoreHandler`, `serveStore`
371
+ and `liveProtocol` — is asked of every change before it is judged, with the seat, the channel,
372
+ the size in bytes and the calls. It answers nothing, `{ retryAfter }`, `{ refuse }` or `{ unavailable }`:
373
+
374
+ - **busy** — `{ retryAfter }` in milliseconds, for a rate or a queue: "not now". The socket says
375
+ `busy` and HTTP answers 429 with `Retry-After`. Nothing is judged and nothing is refused:
376
+ `openRemote` keeps the change shown and pending and sends it again after the wait. Every later
377
+ call on that socket is busy too until the held one comes again, and HTTP posts go one at a
378
+ time, so a burst lands in the order it was made.
379
+
380
+ **The units, loudly.** `retryAfter` — in `limit`'s answer, in the socket's `busy`, and in the
381
+ JSON body of a 429 — is **milliseconds**. The `Retry-After` header is **seconds** (or an
382
+ HTTP date), as HTTP says; ship's handler rounds the wait up to whole seconds there.
383
+ `openRemote` reads each in its own unit, and a host that puts seconds in the JSON body too
384
+ (`retryAfter: 2` beside `Retry-After: 2`) is caught: a JSON wait under 50 beside a header
385
+ that says the same number of seconds is read as those seconds.
386
+ - **refused, `limit`** — `{ refuse }`, a sentence, for a hard cap such as a message over the
387
+ size a host takes: it would be refused however long the client waited, so it is refused now
388
+ and taken back.
389
+ - **refused, `unavailable`** — `{ unavailable }`, a sentence, for a spell with no known end,
390
+ such as a room read-only while it is checked. The socket says `refused` with the reason
391
+ `unavailable` and HTTP answers 503 with it. Nothing is judged and nothing is taken back:
392
+ `openRemote` keeps the change shown and pending, and sends it again after its `backoff`,
393
+ with every call made behind it, until the host takes it.
394
+
395
+ `limit` is asked only of a change that has not landed: a call sent again after it did — its
396
+ ack lost with the socket — is answered with the ops it made, whatever the host would say now.
397
+
398
+ **A cap on what a change would do is `admit`.** `limit` is asked before a change is compiled,
399
+ so it cannot count what the change would do — how many records a call removes, how many the
400
+ store would hold after it — and a host's own wrapper around its acts is gone round by the
401
+ socket and `POST /graview/ops`. `admit(asked, planned)` — on `liveProtocol`,
402
+ `createStoreHandler` and `serveStore` — is asked after the calls (or the undo) are compiled
403
+ and applied, before anything is kept, with `limit`'s `asked` and the store's own plan of the
404
+ change (`PlannedChange`, from `@graview/core`): its ops and primitives, `added`, `removed` and
405
+ `changed` records and links (a removed record's links among the links removed), and
406
+ `nodesAfter` and `edgesAfter`. No second rehearsal on a copy: it is the store's `applyAll`
407
+ asking (`ApplyOptions.admit`). Its answers mean what `limit`'s do — `{ refuse }` refused
408
+ `limit` (413), `{ retryAfter }` busy (429), `{ unavailable }` refused `unavailable` (503) — and
409
+ a change it answers is put back: not kept, not flushed, not sent to anybody.
410
+
411
+ ```ts
412
+ admit: (asked, planned) =>
413
+ asked.seat.kind === "agent" && planned.removed.nodes > 25
414
+ ? { refuse: `That would remove ${planned.removed.nodes} records; an agent may remove at most 25 in one call.` }
415
+ : planned.nodesAfter > MAX_NODES
416
+ ? { refuse: `This app holds ${MAX_NODES} records, the most an app may hold for now.` }
417
+ : undefined,
418
+ ```
419
+
121
420
  ## The hosted-store contract
122
421
 
123
422
  A third party stands up their own host — a file store or SQLite, `graview serve`,
package/dist/browser.d.ts CHANGED
@@ -18,13 +18,13 @@ export type { AppBundle, ExportOptions } from "./export.js";
18
18
  export type { StoredMeta } from "./meta.js";
19
19
  export { health } from "./health.js";
20
20
  export type { HealthReport } from "./health.js";
21
- export { openRemote, seatHeaders } from "./remote.js";
21
+ export { openRemote, RemoteRefusedError, seatHeaders } from "./remote.js";
22
22
  export { SEAT_HEADERS } from "./seat-headers.js";
23
23
  export { createBroadcastPresence, presenceChannelName } from "./presence.js";
24
24
  export type { BroadcastPresenceOptions, ChannelLike } from "./presence.js";
25
- export type { LiveSocketLike, RemoteConflict, RemoteOptions, RemoteStore } from "./remote.js";
26
- export { conflictSentence, LIVE_PATH } from "./live.js";
27
- export type { LiveClientMessage, LiveServerMessage } from "./live.js";
25
+ export type { LiveSocketLike, RemoteBackoff, RemoteConflict, RemoteCounters, RemoteOptions, RemoteRefusal, RemoteStatus, RemoteStore } from "./remote.js";
26
+ export { conflictSentence, LIVE_PATH, LIVE_SUBPROTOCOL, LIVE_WIRE, REFUSAL_REASONS } from "./live.js";
27
+ export type { LiveClientMessage, LiveServerMessage, RefusalReason, WireRefusal } from "./live.js";
28
28
  export { assertPhotoFits, photosUsed, storageBytes, PhotoTooLarge, PHOTO_BUDGET_BYTES, PHOTO_MAX_BYTES, } from "./photos.js";
29
29
  export type { PhotoBudget, PhotoField } from "./photos.js";
30
30
  //# sourceMappingURL=browser.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,gBAAgB,EAChB,SAAS,GACV,MAAM,sBAAsB,CAAC;AAC9B,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,EAAE,UAAU,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,uBAAuB,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAC7E,YAAY,EAAE,wBAAwB,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC3E,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,cAAc,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1J,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,gBAAgB,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AACtG,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAClG,OAAO,EACL,eAAe,EACf,UAAU,EACV,YAAY,EACZ,aAAa,EACb,kBAAkB,EAClB,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC"}
package/dist/browser.js CHANGED
@@ -10,9 +10,9 @@ export { openStore } from "./open-store.js";
10
10
  export { applyToSnapshot } from "./snapshot.js";
11
11
  export { assertBundle, exportBundle } from "./export.js";
12
12
  export { health } from "./health.js";
13
- export { openRemote, seatHeaders } from "./remote.js";
13
+ export { openRemote, RemoteRefusedError, seatHeaders } from "./remote.js";
14
14
  export { SEAT_HEADERS } from "./seat-headers.js";
15
15
  export { createBroadcastPresence, presenceChannelName } from "./presence.js";
16
- export { conflictSentence, LIVE_PATH } from "./live.js";
16
+ export { conflictSentence, LIVE_PATH, LIVE_SUBPROTOCOL, LIVE_WIRE, REFUSAL_REASONS } from "./live.js";
17
17
  export { assertPhotoFits, photosUsed, storageBytes, PhotoTooLarge, PHOTO_BUDGET_BYTES, PHOTO_MAX_BYTES, } from "./photos.js";
18
18
  //# sourceMappingURL=browser.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"browser.js","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,gBAAgB,EAChB,SAAS,GACV,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAGhF,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGzD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,uBAAuB,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAG7E,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAExD,OAAO,EACL,eAAe,EACf,UAAU,EACV,YAAY,EACZ,aAAa,EACb,kBAAkB,EAClB,eAAe,GAChB,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"browser.js","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,gBAAgB,EAChB,SAAS,GACV,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,OAAO,EAAE,cAAc,EA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