@inflatable-cookie/longhorn 0.5.0 → 0.7.0

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 (167) hide show
  1. package/README.md +2 -2
  2. package/package.json +1 -7
  3. package/src/agent-control/generated/protocol.ts +228 -0
  4. package/src/agent-control/index.ts +1 -5
  5. package/src/agent-control/inject.ts +4 -1
  6. package/src/agent-control/selection.ts +42 -25
  7. package/src/agent-control/shim.ts +428 -198
  8. package/src/bridge/direct.ts +1 -1
  9. package/src/bridge/generated/fields.ts +4 -3
  10. package/src/bridge/generated/protocol.ts +62 -7
  11. package/src/bridge/generated/variant-fields.ts +1 -1
  12. package/src/bridge/job.ts +49 -12
  13. package/src/bridge/loopback.ts +7 -25
  14. package/src/bridge/operation.ts +13 -43
  15. package/src/bridge/router.ts +10 -23
  16. package/src/bridge/runtime/connection.ts +78 -109
  17. package/src/bridge/runtime/policy.ts +8 -28
  18. package/src/bridge/runtime/retry.ts +6 -18
  19. package/src/bridge/session.ts +4 -14
  20. package/src/bridge/stream/connection.ts +24 -55
  21. package/src/bridge/stream/source.ts +2 -6
  22. package/src/bridge/stream/tracker.ts +12 -12
  23. package/src/bridge/supervision.ts +104 -97
  24. package/src/bridge/validation/base.ts +21 -63
  25. package/src/bridge/validation/jobs.ts +11 -25
  26. package/src/bridge/validation/negotiation.ts +28 -43
  27. package/src/bridge/validation/operations.ts +29 -65
  28. package/src/bridge/validation/streams.ts +3 -10
  29. package/src/commands/client.ts +37 -28
  30. package/src/commands/controller.ts +119 -96
  31. package/src/commands/generated/fields.ts +7 -6
  32. package/src/commands/generated/protocol.ts +28 -1
  33. package/src/commands/generated/variant-fields.ts +1 -1
  34. package/src/commands/keyboard.ts +94 -100
  35. package/src/commands/physical-code.ts +11 -0
  36. package/src/commands/ports.ts +8 -13
  37. package/src/commands/projectors.ts +100 -75
  38. package/src/commands/validation.ts +434 -241
  39. package/src/config/client.ts +9 -22
  40. package/src/config/generated/base.ts +22 -6
  41. package/src/config/generated/fields.ts +4 -3
  42. package/src/config/generated/label-template.ts +1 -1
  43. package/src/config/generated/labels.ts +1 -1
  44. package/src/config/generated/protocol.ts +1 -1
  45. package/src/config/generated/restore.ts +35 -12
  46. package/src/config/generated/variant-fields.ts +1 -1
  47. package/src/config/names.ts +6 -12
  48. package/src/config/validation/commands.ts +11 -25
  49. package/src/config/validation/outcomes.ts +163 -70
  50. package/src/config/validation/primitives.ts +35 -80
  51. package/src/config/validation/projection.ts +100 -128
  52. package/src/core/connection.ts +187 -83
  53. package/src/core/controller-lifecycle.ts +144 -0
  54. package/src/core/generated/identifiers.ts +4 -0
  55. package/src/core/identifiers.ts +40 -0
  56. package/src/core/index.ts +10 -1
  57. package/src/core/json-value.ts +58 -0
  58. package/src/core/listeners.ts +35 -0
  59. package/src/core/record.ts +80 -0
  60. package/src/core/transport.ts +9 -11
  61. package/src/core/wire-copy.ts +7 -0
  62. package/src/history/client.ts +11 -13
  63. package/src/history/controller.ts +106 -108
  64. package/src/history/direct.ts +1 -3
  65. package/src/history/generated/fields.ts +4 -3
  66. package/src/history/generated/protocol.ts +8 -5
  67. package/src/history/generated/variant-fields.ts +1 -1
  68. package/src/history/ports.ts +7 -4
  69. package/src/history/serialized.ts +9 -13
  70. package/src/history/validation.ts +69 -75
  71. package/src/history-tree/client.ts +89 -11
  72. package/src/history-tree/controller.ts +380 -96
  73. package/src/history-tree/direct.ts +3 -1
  74. package/src/history-tree/generated/fields.ts +5 -3
  75. package/src/history-tree/generated/protocol.ts +43 -7
  76. package/src/history-tree/generated/variant-fields.ts +7 -1
  77. package/src/history-tree/ports.ts +12 -5
  78. package/src/history-tree/serialized.ts +39 -11
  79. package/src/history-tree/validation.ts +286 -78
  80. package/src/layout/generated/protocol.ts +4 -3
  81. package/src/layout/generated/variant-fields.ts +47 -0
  82. package/src/layout/index.ts +1 -1
  83. package/src/layout/ratio.ts +10 -9
  84. package/src/layout/validation.ts +98 -32
  85. package/src/layout/visibility.ts +1 -2
  86. package/src/licence/client.ts +48 -7
  87. package/src/licence/controller.ts +127 -70
  88. package/src/licence/direct.ts +3 -1
  89. package/src/licence/generated/fields.ts +4 -3
  90. package/src/licence/generated/key-conformance.json +28 -0
  91. package/src/licence/generated/protocol.ts +9 -5
  92. package/src/licence/generated/variant-fields.ts +1 -1
  93. package/src/licence/key.ts +42 -18
  94. package/src/licence/ports.ts +10 -3
  95. package/src/licence/serialized.ts +32 -9
  96. package/src/licence/validation.ts +134 -43
  97. package/src/native-content/client.ts +207 -234
  98. package/src/native-content/generated/fields.ts +6 -6
  99. package/src/native-content/generated/protocol.ts +16 -5
  100. package/src/native-content/generated/variant-fields.ts +1 -1
  101. package/src/native-content/ports.ts +11 -16
  102. package/src/native-content/serialized.ts +12 -19
  103. package/src/native-content/validation/base.ts +57 -80
  104. package/src/native-content/validation/messages.ts +24 -35
  105. package/src/native-content/validation/state.ts +31 -33
  106. package/src/notifications/client.ts +85 -51
  107. package/src/notifications/controller.ts +400 -145
  108. package/src/notifications/direct.ts +3 -1
  109. package/src/notifications/generated/fields.ts +4 -3
  110. package/src/notifications/generated/labels.ts +1 -1
  111. package/src/notifications/generated/protocol.ts +5 -1
  112. package/src/notifications/generated/variant-fields.ts +1 -1
  113. package/src/notifications/ports.ts +9 -4
  114. package/src/notifications/serialized.ts +11 -9
  115. package/src/notifications/tone.ts +1 -1
  116. package/src/notifications/validation.ts +165 -71
  117. package/src/operation/bridge.ts +187 -60
  118. package/src/operation/client.ts +48 -62
  119. package/src/operation/controller.ts +67 -97
  120. package/src/operation/direct.ts +1 -3
  121. package/src/operation/generated/fields.ts +4 -3
  122. package/src/operation/generated/labels.ts +1 -1
  123. package/src/operation/generated/protocol.ts +5 -1
  124. package/src/operation/generated/variant-fields.ts +1 -1
  125. package/src/operation/ports.ts +12 -10
  126. package/src/operation/serialized.ts +8 -9
  127. package/src/operation/validation.ts +189 -104
  128. package/src/settings/client.ts +11 -44
  129. package/src/settings/connection.ts +35 -53
  130. package/src/settings/generated/fields.ts +5 -4
  131. package/src/settings/generated/protocol.ts +14 -5
  132. package/src/settings/generated/variant-fields.ts +1 -1
  133. package/src/settings/names.ts +2 -4
  134. package/src/settings/registry.ts +23 -25
  135. package/src/settings/validation/authority.ts +21 -19
  136. package/src/settings/validation/command.ts +22 -41
  137. package/src/settings/validation/error.ts +2 -8
  138. package/src/settings/validation/primitives.ts +46 -97
  139. package/src/settings/validation/projection.ts +8 -24
  140. package/src/settings/validation/registry.ts +33 -79
  141. package/src/surface-transfer/client.ts +6 -18
  142. package/src/surface-transfer/generated/fields.ts +3 -3
  143. package/src/surface-transfer/generated/protocol.ts +3 -3
  144. package/src/surface-transfer/generated/variant-fields.ts +1 -1
  145. package/src/surface-transfer/validation.ts +91 -86
  146. package/src/surfaces/client.ts +33 -55
  147. package/src/surfaces/generated/fields.ts +3 -3
  148. package/src/surfaces/generated/protocol.ts +1 -1
  149. package/src/surfaces/generated/variant-fields.ts +1 -1
  150. package/src/surfaces/validation.ts +227 -99
  151. package/src/transfer/client.ts +26 -60
  152. package/src/transfer/connection.ts +29 -51
  153. package/src/transfer/generated/fields.ts +7 -6
  154. package/src/transfer/generated/protocol.ts +13 -5
  155. package/src/transfer/generated/variant-fields.ts +1 -1
  156. package/src/transfer/index.ts +8 -1
  157. package/src/transfer/payload.ts +20 -35
  158. package/src/transfer/validation.ts +169 -119
  159. package/src/update/client.ts +86 -9
  160. package/src/update/controller.ts +264 -116
  161. package/src/update/direct.ts +3 -1
  162. package/src/update/generated/fields.ts +5 -4
  163. package/src/update/generated/protocol.ts +25 -9
  164. package/src/update/generated/variant-fields.ts +4 -2
  165. package/src/update/ports.ts +14 -6
  166. package/src/update/serialized.ts +36 -10
  167. package/src/update/validation.ts +118 -48
@@ -1,6 +1,8 @@
1
+ import { CheckedSnapshotConnection } from "@inflatable-cookie/longhorn/core";
1
2
  import type {
2
3
  ConnectionFailure,
3
4
  ConnectionFailureReporter,
5
+ SnapshotEventAction,
4
6
  } from "@inflatable-cookie/longhorn/core";
5
7
 
6
8
  import {
@@ -11,21 +13,23 @@ import {
11
13
  assertValidNativeContentDecisionResult,
12
14
  assertValidNativeContentDesiredUpdateRequest,
13
15
  assertValidNativeContentDesiredUpdateResult,
16
+ assertValidNativeContentSnapshot,
14
17
  assertValidNativeContentSnapshotRequest,
15
18
  assertValidNativeContentSnapshotResult,
16
19
  } from "./validation.ts";
17
- import {
18
- NATIVE_CONTENT_PROTOCOL_VERSION,
19
- type ContentSizeDecision,
20
- type ContentSizeProposal,
21
- type DesiredUpdate,
22
- type NativeContentChangedEvent,
23
- type NativeContentContentSizeDecisionResult,
24
- type NativeContentDesiredUpdateResult,
25
- type NativeContentIslandId,
26
- type NativeContentSnapshot,
20
+ import { NATIVE_CONTENT_PROTOCOL_VERSION } from "./generated/protocol.ts";
21
+ import type {
22
+ ContentSizeDecision,
23
+ ContentSizeProposal,
24
+ DesiredUpdate,
25
+ NativeContentContentSizeDecisionResult,
26
+ NativeContentCursor,
27
+ NativeContentDesiredUpdateResult,
28
+ NativeContentIslandId,
29
+ NativeContentProtocolRejection,
30
+ NativeContentSnapshot,
27
31
  } from "./generated/protocol.ts";
28
- import type { NativeContentPort, NativeContentUnlisten } from "./ports.ts";
32
+ import type { NativeContentPort } from "./ports.ts";
29
33
 
30
34
  const DEFAULT_MAXIMUM_PENDING_REQUESTS = 32;
31
35
 
@@ -36,7 +40,7 @@ export interface NativeContentClientOptions {
36
40
  export interface NativeContentConnection {
37
41
  readonly ready: Promise<NativeContentSnapshot>;
38
42
  current(): NativeContentSnapshot | undefined;
39
- failures(): readonly ConnectionFailure[];
43
+ failures(): ReadonlyArray<ConnectionFailure>;
40
44
  updateDesired(update: DesiredUpdate): Promise<NativeContentDesiredUpdateResult>;
41
45
  decideContentSize(
42
46
  proposal: ContentSizeProposal,
@@ -55,8 +59,7 @@ export class NativeContentClient {
55
59
  islandId: NativeContentIslandId,
56
60
  options: NativeContentClientOptions = {},
57
61
  ) {
58
- const maximum =
59
- options.maximumPendingRequests ?? DEFAULT_MAXIMUM_PENDING_REQUESTS;
62
+ const maximum = options.maximumPendingRequests ?? DEFAULT_MAXIMUM_PENDING_REQUESTS;
60
63
  if (!Number.isSafeInteger(maximum) || maximum < 1) {
61
64
  throw new RangeError("maximumPendingRequests must be a positive integer");
62
65
  }
@@ -79,21 +82,19 @@ export class NativeContentClient {
79
82
  }
80
83
  }
81
84
 
85
+ /**
86
+ * Native-content lifetime on the shared snapshot connection: listener first,
87
+ * connect for the first authoritative snapshot, then refresh on events. This
88
+ * class adds only what is native-content specific: the connect/snapshot
89
+ * requests, the event cursor rules and the bounded mutation requests.
90
+ */
82
91
  class CheckedNativeContentConnection implements NativeContentConnection {
83
92
  readonly ready: Promise<NativeContentSnapshot>;
84
93
  readonly #port: NativeContentPort;
85
94
  readonly #islandId: NativeContentIslandId;
86
95
  readonly #maximumPendingRequests: number;
87
- readonly #listener: ((snapshot: NativeContentSnapshot) => void) | undefined;
88
- readonly #onFailure: ConnectionFailureReporter | undefined;
89
- readonly #failures: ConnectionFailure[] = [];
96
+ readonly #connection: CheckedSnapshotConnection<NativeContentSnapshot>;
90
97
  readonly #pending = new Set<string>();
91
- readonly #queuedEvents: NativeContentChangedEvent[] = [];
92
- #snapshot: NativeContentSnapshot | undefined;
93
- #unlisten: NativeContentUnlisten | undefined;
94
- #cleanup: Promise<void> | undefined;
95
- #pump: Promise<void> | undefined;
96
- #refreshPending = false;
97
98
  #disposed = false;
98
99
 
99
100
  constructor(
@@ -106,39 +107,58 @@ class CheckedNativeContentConnection implements NativeContentConnection {
106
107
  this.#port = port;
107
108
  this.#islandId = islandId;
108
109
  this.#maximumPendingRequests = maximumPendingRequests;
109
- this.#listener = listener;
110
- this.#onFailure = onFailure;
111
- this.ready = this.#start();
110
+ this.#connection = new CheckedSnapshotConnection<NativeContentSnapshot>({
111
+ listen: async (receive) => (port.listen === undefined ? () => {} : port.listen(receive)),
112
+ loadSnapshot: () => this.#loadSnapshot(),
113
+ validateSnapshot: (value) => {
114
+ const snapshot = parseSnapshot(value);
115
+ assertIsland(snapshot.cursor, islandId);
116
+ return snapshot;
117
+ },
118
+ handleEvent: (payload, current) => eventAction(payload, current, islandId),
119
+ isNewer: isNewerSessionSnapshot,
120
+ onSnapshot: listener,
121
+ onFailure,
122
+ disposedBeforeReadyError: () => new NativeContentConnectionDisposedError(),
123
+ });
124
+ this.ready = this.#connection.ready.then((snapshot) => {
125
+ if (snapshot === undefined) {
126
+ throw new NativeContentConnectionUnavailableError();
127
+ }
128
+ return snapshot;
129
+ });
112
130
  }
113
131
 
114
132
  current(): NativeContentSnapshot | undefined {
115
- return this.#snapshot;
133
+ return this.#connection.current();
116
134
  }
117
135
 
118
- failures(): readonly ConnectionFailure[] {
119
- return [...this.#failures];
136
+ failures(): ReadonlyArray<ConnectionFailure> {
137
+ return this.#connection.failures();
120
138
  }
121
139
 
122
- async updateDesired(
123
- update: DesiredUpdate,
124
- ): Promise<NativeContentDesiredUpdateResult> {
140
+ async updateDesired(update: DesiredUpdate): Promise<NativeContentDesiredUpdateResult> {
125
141
  const current = await this.#requireCurrent();
126
142
  const requestId = this.#beginRequest();
127
- const session = sessionCursor(current);
128
- const request = {
129
- protocol_version: NATIVE_CONTENT_PROTOCOL_VERSION,
130
- request_id: requestId,
131
- island_id: this.#islandId,
132
- client_epoch: current.cursor.client_epoch,
133
- expected_desired_revision: current.cursor.desired_revision,
134
- update,
135
- };
136
- assertValidNativeContentDesiredUpdateRequest(request);
137
143
  try {
144
+ const request = {
145
+ protocol_version: NATIVE_CONTENT_PROTOCOL_VERSION,
146
+ request_id: requestId,
147
+ island_id: this.#islandId,
148
+ authority_epoch: current.cursor.authority_epoch,
149
+ client_epoch: current.cursor.client_epoch,
150
+ expected_desired_revision: current.cursor.desired_revision,
151
+ update,
152
+ };
153
+ assertValidNativeContentDesiredUpdateRequest(request);
138
154
  const value = await this.#port.updateDesired(request);
139
155
  assertValidNativeContentDesiredUpdateResult(value);
140
156
  assertCorrelation(value.request_id, requestId);
141
- if (value.status === "committed") this.#offer(value.snapshot, session);
157
+ if (value.status === "committed") {
158
+ assertIsland(value.event.cursor, this.#islandId);
159
+ assertIsland(value.snapshot.cursor, this.#islandId);
160
+ this.#connection.offer(value.snapshot);
161
+ }
142
162
  return value;
143
163
  } finally {
144
164
  this.#pending.delete(requestId);
@@ -151,21 +171,25 @@ class CheckedNativeContentConnection implements NativeContentConnection {
151
171
  ): Promise<NativeContentContentSizeDecisionResult> {
152
172
  const current = await this.#requireCurrent();
153
173
  const requestId = this.#beginRequest();
154
- const session = sessionCursor(current);
155
- const request = {
156
- protocol_version: NATIVE_CONTENT_PROTOCOL_VERSION,
157
- request_id: requestId,
158
- island_id: this.#islandId,
159
- client_epoch: current.cursor.client_epoch,
160
- proposal,
161
- decision,
162
- };
163
- assertValidNativeContentDecisionRequest(request);
164
174
  try {
175
+ const request = {
176
+ protocol_version: NATIVE_CONTENT_PROTOCOL_VERSION,
177
+ request_id: requestId,
178
+ island_id: this.#islandId,
179
+ authority_epoch: current.cursor.authority_epoch,
180
+ client_epoch: current.cursor.client_epoch,
181
+ proposal,
182
+ decision,
183
+ };
184
+ assertValidNativeContentDecisionRequest(request);
165
185
  const value = await this.#port.decideContentSize(request);
166
186
  assertValidNativeContentDecisionResult(value);
167
187
  assertCorrelation(value.request_id, requestId);
168
- if (value.status === "decided") this.#offer(value.snapshot, session);
188
+ if (value.status === "decided") {
189
+ assertIsland(value.event.cursor, this.#islandId);
190
+ assertIsland(value.snapshot.cursor, this.#islandId);
191
+ this.#connection.offer(value.snapshot);
192
+ }
169
193
  return value;
170
194
  } finally {
171
195
  this.#pending.delete(requestId);
@@ -175,170 +199,76 @@ class CheckedNativeContentConnection implements NativeContentConnection {
175
199
  async dispose(): Promise<void> {
176
200
  this.#disposed = true;
177
201
  this.#pending.clear();
178
- await this.#closeListener();
179
- try {
180
- await this.ready;
181
- } catch {
182
- // Registration/start failures are already retained in failures().
183
- }
184
- await this.#closeListener();
185
- }
186
-
187
- async #start(): Promise<NativeContentSnapshot> {
188
202
  try {
189
- this.#unlisten =
190
- this.#port.listen === undefined
191
- ? () => {}
192
- : await this.#port.listen((value) => this.#handleEvent(value));
203
+ await this.#connection.dispose();
193
204
  } catch (error) {
194
- this.#recordFailure("registration", error);
195
- throw error;
196
- }
197
-
198
- if (this.#disposed) {
199
- await this.#closeListener();
200
- throw new NativeContentConnectionDisposedError();
201
- }
202
-
203
- try {
204
- const requestId = this.#port.nextRequestId();
205
- const request = {
206
- protocol_version: NATIVE_CONTENT_PROTOCOL_VERSION,
207
- request_id: requestId,
208
- island_id: this.#islandId,
209
- };
210
- assertValidNativeContentConnectRequest(request);
211
- const value = await this.#port.connect(request);
212
- assertValidNativeContentConnectResult(value);
213
- assertCorrelation(value.request_id, requestId);
214
- if (value.status === "rejected") {
215
- throw new NativeContentRequestRejectedError(value.rejection);
216
- }
217
- if (this.#disposed) {
218
- await this.#closeListener();
219
- throw new NativeContentConnectionDisposedError();
205
+ // A failed start is already retained in failures(). Disposing the
206
+ // connection is not a second failure, so only a teardown error rejects.
207
+ const reported = this.#connection
208
+ .failures()
209
+ .some((failure) => failure.phase !== "unlisten" && failure.error === error);
210
+ if (!reported) {
211
+ throw error;
220
212
  }
221
- this.#install(value.snapshot);
222
- for (const event of this.#queuedEvents.splice(0)) this.#acceptEvent(event);
223
- await this.#synchronize();
224
- const current = this.#snapshot;
225
- if (current === undefined) throw new Error("native-content connect returned no snapshot");
226
- return current;
227
- } catch (error) {
228
- this.#recordFailure("snapshot", error);
229
- await this.#closeAfterFailure();
230
- throw error;
231
- }
232
- }
233
-
234
- #handleEvent(value: unknown): void {
235
- if (this.#disposed || this.#failures.length > 0) return;
236
- try {
237
- assertValidNativeContentChangedEvent(value);
238
- if (value.cursor.island_id !== this.#islandId) return;
239
- if (this.#snapshot === undefined) {
240
- this.#queuedEvents.push(value);
241
- return;
242
- }
243
- this.#acceptEvent(value);
244
- } catch (error) {
245
- this.#recordFailure("event", error);
246
- void this.#closeAfterFailure();
247
- }
248
- }
249
-
250
- #acceptEvent(event: NativeContentChangedEvent): void {
251
- const current = this.#snapshot;
252
- if (current === undefined) return;
253
- const currentCursor = current.cursor;
254
- if (event.cursor.authority_epoch < currentCursor.authority_epoch) return;
255
- if (
256
- event.cursor.authority_epoch > currentCursor.authority_epoch ||
257
- event.cursor.client_epoch > currentCursor.client_epoch
258
- ) {
259
- throw new NativeContentSessionReplacedError();
260
213
  }
261
- if (event.cursor.client_epoch < currentCursor.client_epoch) return;
262
- if (
263
- event.cursor.desired_revision <= currentCursor.desired_revision &&
264
- event.cursor.observed_revision <= currentCursor.observed_revision
265
- ) {
266
- return;
267
- }
268
- this.#refreshPending = true;
269
- void this.#synchronize().catch((error) => {
270
- this.#recordFailure("snapshot", error);
271
- void this.#closeAfterFailure();
272
- });
273
214
  }
274
215
 
275
- #synchronize(): Promise<void> {
276
- if (this.#disposed || this.#failures.length > 0 || !this.#refreshPending) {
277
- return Promise.resolve();
278
- }
279
- this.#pump ??= this.#runPump().finally(() => {
280
- this.#pump = undefined;
281
- });
282
- return this.#pump;
216
+ async #loadSnapshot(): Promise<unknown> {
217
+ const current = this.#connection.current();
218
+ return current === undefined ? this.#connect() : this.#refresh(current);
283
219
  }
284
220
 
285
- async #runPump(): Promise<void> {
286
- while (this.#refreshPending && !this.#disposed && this.#failures.length === 0) {
287
- this.#refreshPending = false;
288
- const current = this.#snapshot;
289
- if (current === undefined) return;
290
- const requestId = this.#port.nextRequestId();
291
- const request = {
292
- protocol_version: NATIVE_CONTENT_PROTOCOL_VERSION,
293
- request_id: requestId,
294
- island_id: this.#islandId,
295
- client_epoch: current.cursor.client_epoch,
296
- };
297
- assertValidNativeContentSnapshotRequest(request);
298
- const value = await this.#port.snapshot(request);
299
- assertValidNativeContentSnapshotResult(value);
300
- assertCorrelation(value.request_id, requestId);
301
- if (value.status === "rejected") {
302
- throw new NativeContentRequestRejectedError(value.rejection);
303
- }
304
- this.#offer(value.snapshot, sessionCursor(current));
221
+ async #connect(): Promise<unknown> {
222
+ const requestId = this.#port.nextRequestId();
223
+ const request = {
224
+ protocol_version: NATIVE_CONTENT_PROTOCOL_VERSION,
225
+ request_id: requestId,
226
+ island_id: this.#islandId,
227
+ };
228
+ assertValidNativeContentConnectRequest(request);
229
+ const value = await this.#port.connect(request);
230
+ assertValidNativeContentConnectResult(value);
231
+ assertCorrelation(value.request_id, requestId);
232
+ if (value.status === "rejected") {
233
+ throw new NativeContentRequestRejectedError(value.rejection);
305
234
  }
235
+ return value.snapshot;
306
236
  }
307
237
 
308
- #offer(snapshot: NativeContentSnapshot, session: SessionCursor): void {
309
- if (this.#disposed) return;
310
- const current = this.#snapshot;
311
- if (current === undefined) return;
312
- if (
313
- current.cursor.authority_epoch !== session.authorityEpoch ||
314
- current.cursor.client_epoch !== session.clientEpoch ||
315
- snapshot.cursor.authority_epoch !== session.authorityEpoch ||
316
- snapshot.cursor.client_epoch !== session.clientEpoch
317
- ) {
318
- return;
238
+ async #refresh(current: NativeContentSnapshot): Promise<unknown> {
239
+ const requestId = this.#port.nextRequestId();
240
+ const request = {
241
+ protocol_version: NATIVE_CONTENT_PROTOCOL_VERSION,
242
+ request_id: requestId,
243
+ island_id: this.#islandId,
244
+ authority_epoch: current.cursor.authority_epoch,
245
+ client_epoch: current.cursor.client_epoch,
246
+ };
247
+ assertValidNativeContentSnapshotRequest(request);
248
+ const value = await this.#port.snapshot(request);
249
+ assertValidNativeContentSnapshotResult(value);
250
+ assertCorrelation(value.request_id, requestId);
251
+ if (value.status === "rejected") {
252
+ throw new NativeContentRequestRejectedError(value.rejection);
319
253
  }
320
- if (!isNewerSnapshot(snapshot, current)) return;
321
- this.#install(snapshot);
322
- }
323
-
324
- #install(snapshot: NativeContentSnapshot): void {
325
- this.#snapshot = snapshot;
326
- this.#listener?.(snapshot);
254
+ return value.snapshot;
327
255
  }
328
256
 
329
257
  async #requireCurrent(): Promise<NativeContentSnapshot> {
330
258
  await this.ready;
331
- if (this.#disposed) throw new NativeContentConnectionDisposedError();
332
- const current = this.#snapshot;
333
- if (current === undefined) throw new NativeContentConnectionUnavailableError();
259
+ if (this.#disposed) {
260
+ throw new NativeContentConnectionDisposedError();
261
+ }
262
+ const current = this.#connection.current();
263
+ if (current === undefined) {
264
+ throw new NativeContentConnectionUnavailableError();
265
+ }
334
266
  return current;
335
267
  }
336
268
 
337
269
  #beginRequest(): string {
338
270
  if (this.#pending.size >= this.#maximumPendingRequests) {
339
- throw new NativeContentPendingRequestLimitError(
340
- this.#maximumPendingRequests,
341
- );
271
+ throw new NativeContentPendingRequestLimitError(this.#maximumPendingRequests);
342
272
  }
343
273
  const requestId = this.#port.nextRequestId();
344
274
  if (this.#pending.has(requestId)) {
@@ -347,53 +277,84 @@ class CheckedNativeContentConnection implements NativeContentConnection {
347
277
  this.#pending.add(requestId);
348
278
  return requestId;
349
279
  }
280
+ }
350
281
 
351
- #recordFailure(phase: ConnectionFailure["phase"], error: unknown): void {
352
- if (this.#failures.some((failure) => failure.phase === phase && failure.error === error)) return;
353
- const failure = { phase, error };
354
- this.#failures.push(failure);
355
- this.#onFailure?.(failure);
356
- }
282
+ function parseSnapshot(value: unknown): NativeContentSnapshot {
283
+ assertValidNativeContentSnapshot(value);
284
+ return value;
285
+ }
357
286
 
358
- async #closeAfterFailure(): Promise<void> {
359
- try {
360
- await this.#closeListener();
361
- } catch (error) {
362
- this.#recordFailure("unlisten", error);
363
- }
287
+ function eventAction(
288
+ payload: unknown,
289
+ current: NativeContentSnapshot | undefined,
290
+ islandId: NativeContentIslandId,
291
+ ): SnapshotEventAction {
292
+ assertValidNativeContentChangedEvent(payload);
293
+ const event = payload.cursor;
294
+ if (event.island_id !== islandId) {
295
+ return { kind: "ignore" };
364
296
  }
365
-
366
- #closeListener(): Promise<void> {
367
- if (this.#cleanup !== undefined) return this.#cleanup;
368
- const unlisten = this.#unlisten;
369
- if (unlisten === undefined) return Promise.resolve();
370
- this.#unlisten = undefined;
371
- this.#cleanup = Promise.resolve().then(() => unlisten());
372
- return this.#cleanup;
297
+ if (current === undefined) {
298
+ return { kind: "refresh" };
373
299
  }
374
- }
375
300
 
376
- interface SessionCursor {
377
- readonly authorityEpoch: number;
378
- readonly clientEpoch: number;
301
+ const currentCursor = current.cursor;
302
+ if (event.authority_epoch < currentCursor.authority_epoch) {
303
+ return { kind: "ignore" };
304
+ }
305
+ if (
306
+ event.authority_epoch > currentCursor.authority_epoch ||
307
+ event.client_epoch > currentCursor.client_epoch
308
+ ) {
309
+ throw new NativeContentSessionReplacedError();
310
+ }
311
+ if (event.client_epoch < currentCursor.client_epoch) {
312
+ return { kind: "ignore" };
313
+ }
314
+ if (
315
+ event.desired_revision <= currentCursor.desired_revision &&
316
+ event.observed_revision <= currentCursor.observed_revision
317
+ ) {
318
+ return { kind: "ignore" };
319
+ }
320
+ return { kind: "refresh" };
379
321
  }
380
322
 
381
- function sessionCursor(snapshot: NativeContentSnapshot): SessionCursor {
382
- return {
383
- authorityEpoch: snapshot.cursor.authority_epoch,
384
- clientEpoch: snapshot.cursor.client_epoch,
385
- };
323
+ /**
324
+ * A snapshot only replaces the current one within the same authority and
325
+ * client epoch. A different session cannot arrive by refresh or by request
326
+ * result: an authority or client epoch change is a session replacement, which
327
+ * the event path reports as a failure.
328
+ */
329
+ function isNewerSessionSnapshot(
330
+ candidate: NativeContentSnapshot,
331
+ current: NativeContentSnapshot | undefined,
332
+ ): boolean {
333
+ if (current === undefined) {
334
+ return true;
335
+ }
336
+ if (
337
+ candidate.cursor.authority_epoch !== current.cursor.authority_epoch ||
338
+ candidate.cursor.client_epoch !== current.cursor.client_epoch
339
+ ) {
340
+ return false;
341
+ }
342
+ return isNewerNativeContentSnapshot(candidate, current);
386
343
  }
387
344
 
388
345
  export function isNewerNativeContentSnapshot(
389
346
  candidate: NativeContentSnapshot,
390
347
  current: NativeContentSnapshot | undefined,
391
348
  ): boolean {
392
- if (current === undefined) return true;
349
+ if (current === undefined) {
350
+ return true;
351
+ }
393
352
  if (candidate.cursor.authority_epoch !== current.cursor.authority_epoch) {
394
353
  return candidate.cursor.authority_epoch > current.cursor.authority_epoch;
395
354
  }
396
- if (candidate.cursor.client_epoch !== current.cursor.client_epoch) return false;
355
+ if (candidate.cursor.client_epoch !== current.cursor.client_epoch) {
356
+ return false;
357
+ }
397
358
  if (
398
359
  candidate.cursor.desired_revision < current.cursor.desired_revision ||
399
360
  candidate.cursor.observed_revision < current.cursor.observed_revision
@@ -406,11 +367,11 @@ export function isNewerNativeContentSnapshot(
406
367
  );
407
368
  }
408
369
 
409
- function isNewerSnapshot(
410
- candidate: NativeContentSnapshot,
411
- current: NativeContentSnapshot,
412
- ): boolean {
413
- return isNewerNativeContentSnapshot(candidate, current);
370
+ /** A host reply that carries state is only accepted for the island that asked. */
371
+ function assertIsland(cursor: NativeContentCursor, expected: NativeContentIslandId): void {
372
+ if (cursor.island_id !== expected) {
373
+ throw new NativeContentIslandCorrelationError(expected, cursor.island_id);
374
+ }
414
375
  }
415
376
 
416
377
  function assertCorrelation(received: string, expected: string): void {
@@ -431,8 +392,20 @@ export class NativeContentResponseCorrelationError extends Error {
431
392
  }
432
393
  }
433
394
 
395
+ export class NativeContentIslandCorrelationError extends Error {
396
+ constructor(
397
+ readonly expectedIslandId: NativeContentIslandId,
398
+ readonly receivedIslandId: NativeContentIslandId,
399
+ ) {
400
+ super(
401
+ `native-content reply is for another island: expected ${expectedIslandId}; received ${receivedIslandId}`,
402
+ );
403
+ this.name = "NativeContentIslandCorrelationError";
404
+ }
405
+ }
406
+
434
407
  export class NativeContentRequestRejectedError extends Error {
435
- constructor(readonly rejection: import("./generated/protocol.ts").NativeContentProtocolRejection) {
408
+ constructor(readonly rejection: NativeContentProtocolRejection) {
436
409
  super(rejection.message);
437
410
  this.name = "NativeContentRequestRejectedError";
438
411
  }
@@ -1,9 +1,9 @@
1
- // @generated by `effigy generate:native-content`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Allowed field names per protocol type, from the Rust structs.
3
3
  // A `record()` given one of these rejects unknown and missing keys —
4
4
  // contract 010's Boundary Validation Target.
5
5
 
6
- export const NATIVE_CONTENT_FIELDS: Record<string, readonly string[]> = {
6
+ export const NATIVE_CONTENT_FIELDS = {
7
7
  "ClientPoint": ["x", "y"],
8
8
  "ClientSize": ["width", "height"],
9
9
  "ClientRect": ["origin", "size"],
@@ -25,8 +25,8 @@ export const NATIVE_CONTENT_FIELDS: Record<string, readonly string[]> = {
25
25
  "NativeContentCursor": ["authority_epoch", "client_epoch", "island_id", "attach_generation", "desired_revision", "observed_revision"],
26
26
  "NativeContentSnapshot": ["protocol_version", "cursor", "desired", "observed", "invalidated_generation"],
27
27
  "NativeContentConnectRequest": ["protocol_version", "request_id", "island_id"],
28
- "NativeContentSnapshotRequest": ["protocol_version", "request_id", "island_id", "client_epoch"],
29
- "NativeContentDesiredUpdateRequest": ["protocol_version", "request_id", "island_id", "client_epoch", "expected_desired_revision", "update"],
30
- "NativeContentContentSizeDecisionRequest": ["protocol_version", "request_id", "island_id", "client_epoch", "proposal", "decision"],
28
+ "NativeContentSnapshotRequest": ["protocol_version", "request_id", "island_id", "authority_epoch", "client_epoch"],
29
+ "NativeContentDesiredUpdateRequest": ["protocol_version", "request_id", "island_id", "authority_epoch", "client_epoch", "expected_desired_revision", "update"],
30
+ "NativeContentContentSizeDecisionRequest": ["protocol_version", "request_id", "island_id", "authority_epoch", "client_epoch", "proposal", "decision"],
31
31
  "NativeContentChangedEvent": ["protocol_version", "cursor", "change"],
32
- };
32
+ } as const;
@@ -1,8 +1,7 @@
1
- // @generated by `effigy generate:native-content`; do not edit.
1
+ // @generated by `effigy generate:bindings`; do not edit.
2
2
  // Rust serde types are the wire authority. Browser, plugin, GPU, and product payloads are absent.
3
3
 
4
- export const NATIVE_CONTENT_PROTOCOL_VERSION = 1 as const;
5
- export const NATIVE_CONTENT_MAXIMUM_OPAQUE_ID_BYTES = 128 as const;
4
+ export const NATIVE_CONTENT_PROTOCOL_VERSION = 2 as const;
6
5
  export const NATIVE_CONTENT_MECHANISMS = ["child_view","isolated_window","backing_surface"] as const;
7
6
  export const NATIVE_CONTENT_DETACH_POLICIES = ["reversible","owner_process_termination","process_lifetime"] as const;
8
7
  export const NATIVE_CONTENT_INPUT_ROUTING_MODES = ["native_direct","renderer_forwarded","disabled"] as const;
@@ -18,7 +17,7 @@ export const NATIVE_CONTENT_OPERATION_KINDS = ["attach","set_child_bounds","set_
18
17
  export const NATIVE_CONTENT_OPERATION_OUTCOME_KINDS = ["applied","failed","dependency_skipped","not_attempted"] as const;
19
18
  export const NATIVE_CONTENT_SIZE_DECISION_KINDS = ["accepted","constrained","rejected"] as const;
20
19
  export const NATIVE_CONTENT_HOST_DESTROY_OUTCOMES = ["invalidated","already_invalidated"] as const;
21
- export const NATIVE_CONTENT_REJECTION_CODES = ["unsupported_protocol_version","island_mismatch","stale_client_epoch","future_client_epoch","client_epoch_exhausted","stale_revision","stale_attach_generation","future_attach_generation","attach_generation_gap","generation_still_attached","host_change_requires_generation","host_binding_mismatch","illegal_lifecycle_transition","terminal_generation","invalidated_generation","lifecycle_busy","geometry_mechanism_mismatch","unsupported_visibility_observation","unsupported_focus_observation","unsupported_input_routing","readiness_without_attachment","absent_with_native_evidence","content_size_requests_unsupported","viewport_conversion","revision_exhausted","attach_generation_exhausted","receipt_island_mismatch","receipt_stale_desired","receipt_stale_observed","receipt_invalid_generation","receipt_unknown_step","receipt_duplicate_step","receipt_blocked_dependency"] as const;
20
+ export const NATIVE_CONTENT_REJECTION_CODES = ["unsupported_protocol_version","island_mismatch","authority_epoch_mismatch","stale_client_epoch","future_client_epoch","client_epoch_exhausted","stale_revision","stale_attach_generation","future_attach_generation","attach_generation_gap","generation_still_attached","host_change_requires_generation","host_binding_mismatch","illegal_lifecycle_transition","terminal_generation","invalidated_generation","lifecycle_busy","geometry_mechanism_mismatch","unsupported_visibility_observation","unsupported_focus_observation","unsupported_input_routing","readiness_without_attachment","absent_with_native_evidence","content_size_requests_unsupported","viewport_conversion","revision_exhausted","attach_generation_exhausted","receipt_island_mismatch","receipt_stale_desired","receipt_stale_observed","receipt_invalid_generation","receipt_unknown_step","receipt_duplicate_step","receipt_blocked_dependency"] as const;
22
21
  export const NATIVE_CONTENT_FAILURE_PHASES = ["compatibility","admission","coordination"] as const;
23
22
  export const NATIVE_CONTENT_RETRY_CLASSES = ["never","refresh"] as const;
24
23
  export const NATIVE_CONTENT_CONNECT_STATUSES = ["connected","rejected"] as const;
@@ -189,7 +188,7 @@ export type HostDestroyOutcome = "invalidated" | "already_invalidated";
189
188
 
190
189
  export type HostDestroyReceipt = { previous_observed_revision: NativeContentRevision, current_observed_revision: NativeContentRevision, generation: AttachGeneration, outcome: HostDestroyOutcome, };
191
190
 
192
- export type NativeContentRejectionCode = "unsupported_protocol_version" | "island_mismatch" | "stale_client_epoch" | "future_client_epoch" | "client_epoch_exhausted" | "stale_revision" | "stale_attach_generation" | "future_attach_generation" | "attach_generation_gap" | "generation_still_attached" | "host_change_requires_generation" | "host_binding_mismatch" | "illegal_lifecycle_transition" | "terminal_generation" | "invalidated_generation" | "lifecycle_busy" | "geometry_mechanism_mismatch" | "unsupported_visibility_observation" | "unsupported_focus_observation" | "unsupported_input_routing" | "readiness_without_attachment" | "absent_with_native_evidence" | "content_size_requests_unsupported" | "viewport_conversion" | "revision_exhausted" | "attach_generation_exhausted" | "receipt_island_mismatch" | "receipt_stale_desired" | "receipt_stale_observed" | "receipt_invalid_generation" | "receipt_unknown_step" | "receipt_duplicate_step" | "receipt_blocked_dependency";
191
+ export type NativeContentRejectionCode = "unsupported_protocol_version" | "island_mismatch" | "authority_epoch_mismatch" | "stale_client_epoch" | "future_client_epoch" | "client_epoch_exhausted" | "stale_revision" | "stale_attach_generation" | "future_attach_generation" | "attach_generation_gap" | "generation_still_attached" | "host_change_requires_generation" | "host_binding_mismatch" | "illegal_lifecycle_transition" | "terminal_generation" | "invalidated_generation" | "lifecycle_busy" | "geometry_mechanism_mismatch" | "unsupported_visibility_observation" | "unsupported_focus_observation" | "unsupported_input_routing" | "readiness_without_attachment" | "absent_with_native_evidence" | "content_size_requests_unsupported" | "viewport_conversion" | "revision_exhausted" | "attach_generation_exhausted" | "receipt_island_mismatch" | "receipt_stale_desired" | "receipt_stale_observed" | "receipt_invalid_generation" | "receipt_unknown_step" | "receipt_duplicate_step" | "receipt_blocked_dependency";
193
192
 
194
193
  export type NativeContentFailurePhase = "compatibility" | "admission" | "coordination";
195
194
 
@@ -288,6 +287,10 @@ request_id: NativeContentRequestId,
288
287
  * Canonical island identity.
289
288
  */
290
289
  island_id: NativeContentIslandId,
290
+ /**
291
+ * Host authority lifetime recorded by connect.
292
+ */
293
+ authority_epoch: NativeContentAuthorityEpoch,
291
294
  /**
292
295
  * Current host-issued renderer session.
293
296
  */
@@ -306,6 +309,10 @@ request_id: NativeContentRequestId,
306
309
  * Canonical island identity.
307
310
  */
308
311
  island_id: NativeContentIslandId,
312
+ /**
313
+ * Host authority lifetime recorded by connect.
314
+ */
315
+ authority_epoch: NativeContentAuthorityEpoch,
309
316
  /**
310
317
  * Current host-issued renderer session.
311
318
  */
@@ -332,6 +339,10 @@ request_id: NativeContentRequestId,
332
339
  * Canonical island identity.
333
340
  */
334
341
  island_id: NativeContentIslandId,
342
+ /**
343
+ * Host authority lifetime recorded by connect.
344
+ */
345
+ authority_epoch: NativeContentAuthorityEpoch,
335
346
  /**
336
347
  * Current host-issued renderer session.
337
348
  */