@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.
- package/README.md +2 -2
- package/package.json +1 -7
- package/src/agent-control/generated/protocol.ts +228 -0
- package/src/agent-control/index.ts +1 -5
- package/src/agent-control/inject.ts +4 -1
- package/src/agent-control/selection.ts +42 -25
- package/src/agent-control/shim.ts +428 -198
- package/src/bridge/direct.ts +1 -1
- package/src/bridge/generated/fields.ts +4 -3
- package/src/bridge/generated/protocol.ts +62 -7
- package/src/bridge/generated/variant-fields.ts +1 -1
- package/src/bridge/job.ts +49 -12
- package/src/bridge/loopback.ts +7 -25
- package/src/bridge/operation.ts +13 -43
- package/src/bridge/router.ts +10 -23
- package/src/bridge/runtime/connection.ts +78 -109
- package/src/bridge/runtime/policy.ts +8 -28
- package/src/bridge/runtime/retry.ts +6 -18
- package/src/bridge/session.ts +4 -14
- package/src/bridge/stream/connection.ts +24 -55
- package/src/bridge/stream/source.ts +2 -6
- package/src/bridge/stream/tracker.ts +12 -12
- package/src/bridge/supervision.ts +104 -97
- package/src/bridge/validation/base.ts +21 -63
- package/src/bridge/validation/jobs.ts +11 -25
- package/src/bridge/validation/negotiation.ts +28 -43
- package/src/bridge/validation/operations.ts +29 -65
- package/src/bridge/validation/streams.ts +3 -10
- package/src/commands/client.ts +37 -28
- package/src/commands/controller.ts +119 -96
- package/src/commands/generated/fields.ts +7 -6
- package/src/commands/generated/protocol.ts +28 -1
- package/src/commands/generated/variant-fields.ts +1 -1
- package/src/commands/keyboard.ts +94 -100
- package/src/commands/physical-code.ts +11 -0
- package/src/commands/ports.ts +8 -13
- package/src/commands/projectors.ts +100 -75
- package/src/commands/validation.ts +434 -241
- package/src/config/client.ts +9 -22
- package/src/config/generated/base.ts +22 -6
- package/src/config/generated/fields.ts +4 -3
- package/src/config/generated/label-template.ts +1 -1
- package/src/config/generated/labels.ts +1 -1
- package/src/config/generated/protocol.ts +1 -1
- package/src/config/generated/restore.ts +35 -12
- package/src/config/generated/variant-fields.ts +1 -1
- package/src/config/names.ts +6 -12
- package/src/config/validation/commands.ts +11 -25
- package/src/config/validation/outcomes.ts +163 -70
- package/src/config/validation/primitives.ts +35 -80
- package/src/config/validation/projection.ts +100 -128
- package/src/core/connection.ts +187 -83
- package/src/core/controller-lifecycle.ts +144 -0
- package/src/core/generated/identifiers.ts +4 -0
- package/src/core/identifiers.ts +40 -0
- package/src/core/index.ts +10 -1
- package/src/core/json-value.ts +58 -0
- package/src/core/listeners.ts +35 -0
- package/src/core/record.ts +80 -0
- package/src/core/transport.ts +9 -11
- package/src/core/wire-copy.ts +7 -0
- package/src/history/client.ts +11 -13
- package/src/history/controller.ts +106 -108
- package/src/history/direct.ts +1 -3
- package/src/history/generated/fields.ts +4 -3
- package/src/history/generated/protocol.ts +8 -5
- package/src/history/generated/variant-fields.ts +1 -1
- package/src/history/ports.ts +7 -4
- package/src/history/serialized.ts +9 -13
- package/src/history/validation.ts +69 -75
- package/src/history-tree/client.ts +89 -11
- package/src/history-tree/controller.ts +380 -96
- package/src/history-tree/direct.ts +3 -1
- package/src/history-tree/generated/fields.ts +5 -3
- package/src/history-tree/generated/protocol.ts +43 -7
- package/src/history-tree/generated/variant-fields.ts +7 -1
- package/src/history-tree/ports.ts +12 -5
- package/src/history-tree/serialized.ts +39 -11
- package/src/history-tree/validation.ts +286 -78
- package/src/layout/generated/protocol.ts +4 -3
- package/src/layout/generated/variant-fields.ts +47 -0
- package/src/layout/index.ts +1 -1
- package/src/layout/ratio.ts +10 -9
- package/src/layout/validation.ts +98 -32
- package/src/layout/visibility.ts +1 -2
- package/src/licence/client.ts +48 -7
- package/src/licence/controller.ts +127 -70
- package/src/licence/direct.ts +3 -1
- package/src/licence/generated/fields.ts +4 -3
- package/src/licence/generated/key-conformance.json +28 -0
- package/src/licence/generated/protocol.ts +9 -5
- package/src/licence/generated/variant-fields.ts +1 -1
- package/src/licence/key.ts +42 -18
- package/src/licence/ports.ts +10 -3
- package/src/licence/serialized.ts +32 -9
- package/src/licence/validation.ts +134 -43
- package/src/native-content/client.ts +207 -234
- package/src/native-content/generated/fields.ts +6 -6
- package/src/native-content/generated/protocol.ts +16 -5
- package/src/native-content/generated/variant-fields.ts +1 -1
- package/src/native-content/ports.ts +11 -16
- package/src/native-content/serialized.ts +12 -19
- package/src/native-content/validation/base.ts +57 -80
- package/src/native-content/validation/messages.ts +24 -35
- package/src/native-content/validation/state.ts +31 -33
- package/src/notifications/client.ts +85 -51
- package/src/notifications/controller.ts +400 -145
- package/src/notifications/direct.ts +3 -1
- package/src/notifications/generated/fields.ts +4 -3
- package/src/notifications/generated/labels.ts +1 -1
- package/src/notifications/generated/protocol.ts +5 -1
- package/src/notifications/generated/variant-fields.ts +1 -1
- package/src/notifications/ports.ts +9 -4
- package/src/notifications/serialized.ts +11 -9
- package/src/notifications/tone.ts +1 -1
- package/src/notifications/validation.ts +165 -71
- package/src/operation/bridge.ts +187 -60
- package/src/operation/client.ts +48 -62
- package/src/operation/controller.ts +67 -97
- package/src/operation/direct.ts +1 -3
- package/src/operation/generated/fields.ts +4 -3
- package/src/operation/generated/labels.ts +1 -1
- package/src/operation/generated/protocol.ts +5 -1
- package/src/operation/generated/variant-fields.ts +1 -1
- package/src/operation/ports.ts +12 -10
- package/src/operation/serialized.ts +8 -9
- package/src/operation/validation.ts +189 -104
- package/src/settings/client.ts +11 -44
- package/src/settings/connection.ts +35 -53
- package/src/settings/generated/fields.ts +5 -4
- package/src/settings/generated/protocol.ts +14 -5
- package/src/settings/generated/variant-fields.ts +1 -1
- package/src/settings/names.ts +2 -4
- package/src/settings/registry.ts +23 -25
- package/src/settings/validation/authority.ts +21 -19
- package/src/settings/validation/command.ts +22 -41
- package/src/settings/validation/error.ts +2 -8
- package/src/settings/validation/primitives.ts +46 -97
- package/src/settings/validation/projection.ts +8 -24
- package/src/settings/validation/registry.ts +33 -79
- package/src/surface-transfer/client.ts +6 -18
- package/src/surface-transfer/generated/fields.ts +3 -3
- package/src/surface-transfer/generated/protocol.ts +3 -3
- package/src/surface-transfer/generated/variant-fields.ts +1 -1
- package/src/surface-transfer/validation.ts +91 -86
- package/src/surfaces/client.ts +33 -55
- package/src/surfaces/generated/fields.ts +3 -3
- package/src/surfaces/generated/protocol.ts +1 -1
- package/src/surfaces/generated/variant-fields.ts +1 -1
- package/src/surfaces/validation.ts +227 -99
- package/src/transfer/client.ts +26 -60
- package/src/transfer/connection.ts +29 -51
- package/src/transfer/generated/fields.ts +7 -6
- package/src/transfer/generated/protocol.ts +13 -5
- package/src/transfer/generated/variant-fields.ts +1 -1
- package/src/transfer/index.ts +8 -1
- package/src/transfer/payload.ts +20 -35
- package/src/transfer/validation.ts +169 -119
- package/src/update/client.ts +86 -9
- package/src/update/controller.ts +264 -116
- package/src/update/direct.ts +3 -1
- package/src/update/generated/fields.ts +5 -4
- package/src/update/generated/protocol.ts +25 -9
- package/src/update/generated/variant-fields.ts +4 -2
- package/src/update/ports.ts +14 -6
- package/src/update/serialized.ts +36 -10
- package/src/update/validation.ts +118 -48
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
parseBridgeNegotiationReceipt,
|
|
19
19
|
parseBridgeStreamCursor,
|
|
20
20
|
} from "../validation.ts";
|
|
21
|
+
import { BRIDGE_ADMITTED_CONNECTION_TRANSITIONS } from "../generated/protocol.ts";
|
|
21
22
|
import { BridgeSession } from "../session.ts";
|
|
22
23
|
import {
|
|
23
24
|
checkedDeadline,
|
|
@@ -26,9 +27,8 @@ import {
|
|
|
26
27
|
checkedRetryClass,
|
|
27
28
|
checkedRetryLimit,
|
|
28
29
|
BridgeRuntimeError,
|
|
29
|
-
type BridgeRuntimeBackoff,
|
|
30
|
-
type BridgeRuntimeClock,
|
|
31
30
|
} from "./policy.ts";
|
|
31
|
+
import type { BridgeRuntimeBackoff, BridgeRuntimeClock } from "./policy.ts";
|
|
32
32
|
|
|
33
33
|
export class BridgeConnectionRuntime {
|
|
34
34
|
readonly #clock: BridgeRuntimeClock;
|
|
@@ -40,11 +40,7 @@ export class BridgeConnectionRuntime {
|
|
|
40
40
|
#reconnectNotBefore: number | undefined;
|
|
41
41
|
#session: BridgeSession | undefined;
|
|
42
42
|
|
|
43
|
-
constructor(
|
|
44
|
-
clock: BridgeRuntimeClock,
|
|
45
|
-
backoff: BridgeRuntimeBackoff,
|
|
46
|
-
reconnectLimit: number,
|
|
47
|
-
) {
|
|
43
|
+
constructor(clock: BridgeRuntimeClock, backoff: BridgeRuntimeBackoff, reconnectLimit: number) {
|
|
48
44
|
this.#clock = clock;
|
|
49
45
|
this.#backoff = backoff;
|
|
50
46
|
this.#reconnectLimit = checkedRetryLimit(reconnectLimit);
|
|
@@ -69,86 +65,85 @@ export class BridgeConnectionRuntime {
|
|
|
69
65
|
this.#reconnectNotBefore !== undefined &&
|
|
70
66
|
at < this.#reconnectNotBefore
|
|
71
67
|
) {
|
|
72
|
-
throw new BridgeRuntimeError(
|
|
73
|
-
"retry_not_due",
|
|
74
|
-
"reconnect backoff has not elapsed",
|
|
75
|
-
);
|
|
68
|
+
throw new BridgeRuntimeError("retry_not_due", "reconnect backoff has not elapsed");
|
|
76
69
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
"transportReady",
|
|
80
|
-
at,
|
|
81
|
-
null,
|
|
82
|
-
);
|
|
70
|
+
this.#requireTransition("negotiating");
|
|
71
|
+
const receipt = this.#prepareAt("negotiating", "transportReady", at, null, this.#session);
|
|
83
72
|
this.#reconnectNotBefore = undefined;
|
|
84
|
-
return receipt;
|
|
73
|
+
return this.#apply(receipt);
|
|
85
74
|
}
|
|
86
75
|
|
|
87
76
|
acceptNegotiation(
|
|
88
77
|
value: unknown,
|
|
89
|
-
requirements:
|
|
78
|
+
requirements: ReadonlyArray<BridgeAuthorityRequirement>,
|
|
90
79
|
request?: BridgeHelloRequest,
|
|
91
80
|
): BridgeConnectionTransitionReceipt {
|
|
92
81
|
this.#requireTransition("ready");
|
|
93
|
-
const checkedRequest = request === undefined
|
|
94
|
-
? undefined
|
|
95
|
-
: parseBridgeHelloRequest(request);
|
|
82
|
+
const checkedRequest = request === undefined ? undefined : parseBridgeHelloRequest(request);
|
|
96
83
|
const receipt = parseBridgeNegotiationReceipt(value, checkedRequest);
|
|
97
84
|
assertRequiredAuthorities(receipt, requirements);
|
|
98
|
-
|
|
85
|
+
const session = new BridgeSession(receipt);
|
|
86
|
+
const transition = this.#prepareAt(
|
|
87
|
+
"ready",
|
|
88
|
+
"negotiationAccepted",
|
|
89
|
+
checkedMonotonic(this.#clock.now()),
|
|
90
|
+
null,
|
|
91
|
+
session,
|
|
92
|
+
);
|
|
93
|
+
this.#session = session;
|
|
99
94
|
this.#reconnectAttempts = 0;
|
|
100
95
|
this.#reconnectNotBefore = undefined;
|
|
101
|
-
return this.#
|
|
96
|
+
return this.#apply(transition);
|
|
102
97
|
}
|
|
103
98
|
|
|
104
99
|
degrade(
|
|
105
|
-
reason: Extract<
|
|
106
|
-
BridgeConnectionReason,
|
|
107
|
-
"capabilityChanged" | "transportLost" | "hostFailure"
|
|
108
|
-
>,
|
|
100
|
+
reason: Extract<BridgeConnectionReason, "capabilityChanged" | "transportLost" | "hostFailure">,
|
|
109
101
|
): BridgeConnectionTransitionReceipt {
|
|
110
|
-
if (
|
|
111
|
-
|
|
112
|
-
reason !== "transportLost" &&
|
|
113
|
-
reason !== "hostFailure"
|
|
114
|
-
) {
|
|
115
|
-
throw new BridgeRuntimeError(
|
|
116
|
-
"invalid_transition",
|
|
117
|
-
"invalid degraded-state reason",
|
|
118
|
-
);
|
|
102
|
+
if (reason !== "capabilityChanged" && reason !== "transportLost" && reason !== "hostFailure") {
|
|
103
|
+
throw new BridgeRuntimeError("invalid_transition", "invalid degraded-state reason");
|
|
119
104
|
}
|
|
120
105
|
return this.#transition("degraded", reason);
|
|
121
106
|
}
|
|
122
107
|
|
|
123
108
|
reconnect(retryClass: BridgeRetryClass): BridgeConnectionTransitionReceipt {
|
|
124
109
|
retryClass = checkedRetryClass(retryClass);
|
|
110
|
+
if (retryClass !== "never" && this.#status.reason === "retryForbidden") {
|
|
111
|
+
// A retry-forbidden status is terminal for retries, as in the Rust machine.
|
|
112
|
+
throw new BridgeRuntimeError(
|
|
113
|
+
"invalid_transition",
|
|
114
|
+
`cannot transition from ${this.#status.state} to reconnecting`,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
125
117
|
const attempt = this.#reconnectAttempts + 1;
|
|
126
|
-
const admitted = retryClass !== "never" &&
|
|
127
|
-
attempt <= this.#reconnectLimit;
|
|
118
|
+
const admitted = retryClass !== "never" && attempt <= this.#reconnectLimit;
|
|
128
119
|
this.#requireTransition(admitted ? "reconnecting" : "offline");
|
|
129
120
|
const at = checkedMonotonic(this.#clock.now());
|
|
130
|
-
|
|
121
|
+
const reconnect: BridgeReconnectSchedule | null = admitted
|
|
122
|
+
? {
|
|
123
|
+
attempt,
|
|
124
|
+
retryClass,
|
|
125
|
+
notBefore: checkedDeadline(at, this.#backoff.delay(retryClass, attempt)),
|
|
126
|
+
}
|
|
127
|
+
: null;
|
|
128
|
+
let reason: BridgeConnectionReason = "transportLost";
|
|
131
129
|
if (admitted) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
notBefore: checkedDeadline(
|
|
136
|
-
at,
|
|
137
|
-
this.#backoff.delay(retryClass, attempt),
|
|
138
|
-
),
|
|
139
|
-
};
|
|
140
|
-
this.#reconnectAttempts = attempt;
|
|
141
|
-
this.#reconnectNotBefore = reconnect.notBefore;
|
|
142
|
-
} else {
|
|
143
|
-
this.#reconnectNotBefore = undefined;
|
|
130
|
+
reason = "retryScheduled";
|
|
131
|
+
} else if (retryClass === "never") {
|
|
132
|
+
reason = "retryForbidden";
|
|
144
133
|
}
|
|
145
|
-
|
|
146
|
-
return this.#commitAt(
|
|
134
|
+
const receipt = this.#prepareAt(
|
|
147
135
|
admitted ? "reconnecting" : "offline",
|
|
148
|
-
|
|
136
|
+
reason,
|
|
149
137
|
at,
|
|
150
138
|
reconnect,
|
|
139
|
+
undefined,
|
|
151
140
|
);
|
|
141
|
+
if (reconnect !== null) {
|
|
142
|
+
this.#reconnectAttempts = reconnect.attempt;
|
|
143
|
+
}
|
|
144
|
+
this.#reconnectNotBefore = reconnect?.notBefore;
|
|
145
|
+
this.#session = undefined;
|
|
146
|
+
return this.#apply(receipt);
|
|
152
147
|
}
|
|
153
148
|
|
|
154
149
|
incompatible(): BridgeConnectionTransitionReceipt {
|
|
@@ -187,16 +182,20 @@ export class BridgeConnectionRuntime {
|
|
|
187
182
|
}
|
|
188
183
|
|
|
189
184
|
#terminal(
|
|
190
|
-
state: Extract<
|
|
191
|
-
BridgeConnectionState,
|
|
192
|
-
"incompatible" | "unauthorized" | "failed" | "closed"
|
|
193
|
-
>,
|
|
185
|
+
state: Extract<BridgeConnectionState, "incompatible" | "unauthorized" | "failed" | "closed">,
|
|
194
186
|
reason: BridgeConnectionReason,
|
|
195
187
|
): BridgeConnectionTransitionReceipt {
|
|
196
188
|
this.#requireTransition(state);
|
|
189
|
+
const receipt = this.#prepareAt(
|
|
190
|
+
state,
|
|
191
|
+
reason,
|
|
192
|
+
checkedMonotonic(this.#clock.now()),
|
|
193
|
+
null,
|
|
194
|
+
undefined,
|
|
195
|
+
);
|
|
197
196
|
this.#session = undefined;
|
|
198
197
|
this.#reconnectNotBefore = undefined;
|
|
199
|
-
return this.#
|
|
198
|
+
return this.#apply(receipt);
|
|
200
199
|
}
|
|
201
200
|
|
|
202
201
|
#transition(
|
|
@@ -204,42 +203,34 @@ export class BridgeConnectionRuntime {
|
|
|
204
203
|
reason: BridgeConnectionReason,
|
|
205
204
|
): BridgeConnectionTransitionReceipt {
|
|
206
205
|
this.#requireTransition(state);
|
|
207
|
-
return this.#
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
#commit(
|
|
211
|
-
state: BridgeConnectionState,
|
|
212
|
-
reason: BridgeConnectionReason,
|
|
213
|
-
reconnect: BridgeReconnectSchedule | null,
|
|
214
|
-
): BridgeConnectionTransitionReceipt {
|
|
215
|
-
return this.#commitAt(
|
|
216
|
-
state,
|
|
217
|
-
reason,
|
|
218
|
-
checkedMonotonic(this.#clock.now()),
|
|
219
|
-
reconnect,
|
|
206
|
+
return this.#apply(
|
|
207
|
+
this.#prepareAt(state, reason, checkedMonotonic(this.#clock.now()), null, this.#session),
|
|
220
208
|
);
|
|
221
209
|
}
|
|
222
210
|
|
|
223
|
-
#
|
|
211
|
+
#prepareAt(
|
|
224
212
|
state: BridgeConnectionState,
|
|
225
213
|
reason: BridgeConnectionReason,
|
|
226
214
|
at: number,
|
|
227
215
|
reconnect: BridgeReconnectSchedule | null,
|
|
216
|
+
session: BridgeSession | undefined,
|
|
228
217
|
): BridgeConnectionTransitionReceipt {
|
|
229
|
-
const previous = this.#status;
|
|
230
|
-
const current = { state, reason } as BridgeConnectionStatus;
|
|
231
|
-
this.#sequence = checkedIncrement(this.#sequence);
|
|
232
|
-
this.#status = current;
|
|
233
218
|
return {
|
|
234
|
-
sequence: this.#sequence,
|
|
219
|
+
sequence: checkedIncrement(this.#sequence),
|
|
235
220
|
at,
|
|
236
|
-
previous,
|
|
237
|
-
current,
|
|
238
|
-
sessionId:
|
|
221
|
+
previous: this.#status,
|
|
222
|
+
current: { state, reason },
|
|
223
|
+
sessionId: session?.receipt.sessionId ?? null,
|
|
239
224
|
reconnect,
|
|
240
225
|
};
|
|
241
226
|
}
|
|
242
227
|
|
|
228
|
+
#apply(receipt: BridgeConnectionTransitionReceipt): BridgeConnectionTransitionReceipt {
|
|
229
|
+
this.#sequence = receipt.sequence;
|
|
230
|
+
this.#status = receipt.current;
|
|
231
|
+
return receipt;
|
|
232
|
+
}
|
|
233
|
+
|
|
243
234
|
#requireTransition(next: BridgeConnectionState): void {
|
|
244
235
|
if (!transitionAllowed(this.#status.state, next)) {
|
|
245
236
|
throw new BridgeRuntimeError(
|
|
@@ -252,7 +243,7 @@ export class BridgeConnectionRuntime {
|
|
|
252
243
|
|
|
253
244
|
function assertRequiredAuthorities(
|
|
254
245
|
receipt: BridgeNegotiationReceipt,
|
|
255
|
-
requirements:
|
|
246
|
+
requirements: ReadonlyArray<BridgeAuthorityRequirement>,
|
|
256
247
|
): void {
|
|
257
248
|
for (const requirement of requirements) {
|
|
258
249
|
const authority = receipt.domainAuthorities.find(
|
|
@@ -289,30 +280,8 @@ function authoritySatisfies(
|
|
|
289
280
|
}
|
|
290
281
|
}
|
|
291
282
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
)
|
|
296
|
-
if (next === "closed") {
|
|
297
|
-
return current !== "closed";
|
|
298
|
-
}
|
|
299
|
-
const transitions: Partial<
|
|
300
|
-
Record<BridgeConnectionState, readonly BridgeConnectionState[]>
|
|
301
|
-
> = {
|
|
302
|
-
idle: ["connecting"],
|
|
303
|
-
connecting: ["negotiating", "reconnecting", "offline", "failed"],
|
|
304
|
-
negotiating: [
|
|
305
|
-
"ready",
|
|
306
|
-
"reconnecting",
|
|
307
|
-
"offline",
|
|
308
|
-
"incompatible",
|
|
309
|
-
"unauthorized",
|
|
310
|
-
"failed",
|
|
311
|
-
],
|
|
312
|
-
ready: ["degraded", "reconnecting", "offline", "failed"],
|
|
313
|
-
degraded: ["ready", "reconnecting", "offline", "failed"],
|
|
314
|
-
reconnecting: ["negotiating", "reconnecting", "offline", "failed"],
|
|
315
|
-
offline: ["reconnecting", "failed"],
|
|
316
|
-
};
|
|
317
|
-
return transitions[current]?.includes(next) ?? false;
|
|
283
|
+
// Generated from `BridgeConnectionState::ADMITTED_TRANSITIONS`, the same table
|
|
284
|
+
// the Rust machine enforces. There is no hand-written copy here.
|
|
285
|
+
function transitionAllowed(current: BridgeConnectionState, next: BridgeConnectionState): boolean {
|
|
286
|
+
return BRIDGE_ADMITTED_CONNECTION_TRANSITIONS[current].includes(next);
|
|
318
287
|
}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
type BridgeRetryClass,
|
|
4
|
-
} from "../generated/protocol.ts";
|
|
1
|
+
import { BRIDGE_RETRY_CLASSES } from "../generated/protocol.ts";
|
|
2
|
+
import type { BridgeRetryClass } from "../generated/protocol.ts";
|
|
5
3
|
|
|
6
4
|
export const MAXIMUM_BRIDGE_AUTOMATIC_RETRIES = 64;
|
|
7
5
|
|
|
@@ -22,10 +20,7 @@ export class BridgeRuntimeError extends Error {
|
|
|
22
20
|
| "invalid_time"
|
|
23
21
|
| "retry_not_due";
|
|
24
22
|
|
|
25
|
-
constructor(
|
|
26
|
-
code: BridgeRuntimeError["code"],
|
|
27
|
-
detail: string,
|
|
28
|
-
) {
|
|
23
|
+
constructor(code: BridgeRuntimeError["code"], detail: string) {
|
|
29
24
|
super(`bridge runtime: ${detail}`);
|
|
30
25
|
this.name = "BridgeRuntimeError";
|
|
31
26
|
this.code = code;
|
|
@@ -33,11 +28,7 @@ export class BridgeRuntimeError extends Error {
|
|
|
33
28
|
}
|
|
34
29
|
|
|
35
30
|
export function checkedRetryLimit(value: number): number {
|
|
36
|
-
if (
|
|
37
|
-
!Number.isSafeInteger(value) ||
|
|
38
|
-
value < 0 ||
|
|
39
|
-
value > MAXIMUM_BRIDGE_AUTOMATIC_RETRIES
|
|
40
|
-
) {
|
|
31
|
+
if (!Number.isSafeInteger(value) || value < 0 || value > MAXIMUM_BRIDGE_AUTOMATIC_RETRIES) {
|
|
41
32
|
throw new BridgeRuntimeError(
|
|
42
33
|
"invalid_retry_limit",
|
|
43
34
|
`retry limit must be 0..${MAXIMUM_BRIDGE_AUTOMATIC_RETRIES}`,
|
|
@@ -46,24 +37,16 @@ export function checkedRetryLimit(value: number): number {
|
|
|
46
37
|
return value;
|
|
47
38
|
}
|
|
48
39
|
|
|
49
|
-
export function checkedRetryClass(
|
|
50
|
-
value: BridgeRetryClass,
|
|
51
|
-
): BridgeRetryClass {
|
|
40
|
+
export function checkedRetryClass(value: BridgeRetryClass): BridgeRetryClass {
|
|
52
41
|
if (!BRIDGE_RETRY_CLASSES.includes(value)) {
|
|
53
|
-
throw new BridgeRuntimeError(
|
|
54
|
-
"invalid_retry_class",
|
|
55
|
-
"unknown retry class",
|
|
56
|
-
);
|
|
42
|
+
throw new BridgeRuntimeError("invalid_retry_class", "unknown retry class");
|
|
57
43
|
}
|
|
58
44
|
return value;
|
|
59
45
|
}
|
|
60
46
|
|
|
61
47
|
export function checkedMonotonic(value: number): number {
|
|
62
48
|
if (!Number.isSafeInteger(value) || value < 0) {
|
|
63
|
-
throw new BridgeRuntimeError(
|
|
64
|
-
"invalid_time",
|
|
65
|
-
"clock must return a nonnegative safe integer",
|
|
66
|
-
);
|
|
49
|
+
throw new BridgeRuntimeError("invalid_time", "clock must return a nonnegative safe integer");
|
|
67
50
|
}
|
|
68
51
|
return value;
|
|
69
52
|
}
|
|
@@ -80,10 +63,7 @@ export function checkedDeadline(at: number, delay: number): number {
|
|
|
80
63
|
export function checkedIncrement(value: number): number {
|
|
81
64
|
const next = value + 1;
|
|
82
65
|
if (!Number.isSafeInteger(next)) {
|
|
83
|
-
throw new BridgeRuntimeError(
|
|
84
|
-
"invalid_time",
|
|
85
|
-
"transition sequence exhausted",
|
|
86
|
-
);
|
|
66
|
+
throw new BridgeRuntimeError("invalid_time", "transition sequence exhausted");
|
|
87
67
|
}
|
|
88
68
|
return next;
|
|
89
69
|
}
|
|
@@ -8,9 +8,8 @@ import {
|
|
|
8
8
|
checkedMonotonic,
|
|
9
9
|
checkedRetryClass,
|
|
10
10
|
checkedRetryLimit,
|
|
11
|
-
type BridgeRuntimeBackoff,
|
|
12
|
-
type BridgeRuntimeClock,
|
|
13
11
|
} from "./policy.ts";
|
|
12
|
+
import type { BridgeRuntimeBackoff, BridgeRuntimeClock } from "./policy.ts";
|
|
14
13
|
|
|
15
14
|
export class BridgeQueryRetryRuntime {
|
|
16
15
|
readonly #clock: BridgeRuntimeClock;
|
|
@@ -18,11 +17,7 @@ export class BridgeQueryRetryRuntime {
|
|
|
18
17
|
readonly #limit: number;
|
|
19
18
|
#scheduled = 0;
|
|
20
19
|
|
|
21
|
-
constructor(
|
|
22
|
-
clock: BridgeRuntimeClock,
|
|
23
|
-
backoff: BridgeRuntimeBackoff,
|
|
24
|
-
limit: number,
|
|
25
|
-
) {
|
|
20
|
+
constructor(clock: BridgeRuntimeClock, backoff: BridgeRuntimeBackoff, limit: number) {
|
|
26
21
|
this.#clock = clock;
|
|
27
22
|
this.#backoff = backoff;
|
|
28
23
|
this.#limit = checkedRetryLimit(limit);
|
|
@@ -32,23 +27,16 @@ export class BridgeQueryRetryRuntime {
|
|
|
32
27
|
decision: BridgeQueryRetryDecision,
|
|
33
28
|
retryClass: BridgeRetryClass,
|
|
34
29
|
): BridgeReconnectSchedule | undefined {
|
|
35
|
-
|
|
36
|
-
if (
|
|
37
|
-
decision !== "retry" ||
|
|
38
|
-
retryClass === "never" ||
|
|
39
|
-
this.#scheduled >= this.#limit
|
|
40
|
-
) {
|
|
30
|
+
const checkedClass = checkedRetryClass(retryClass);
|
|
31
|
+
if (decision !== "retry" || checkedClass === "never" || this.#scheduled >= this.#limit) {
|
|
41
32
|
return undefined;
|
|
42
33
|
}
|
|
43
34
|
const attempt = this.#scheduled + 1;
|
|
44
35
|
const at = checkedMonotonic(this.#clock.now());
|
|
45
36
|
const schedule = {
|
|
46
37
|
attempt,
|
|
47
|
-
retryClass,
|
|
48
|
-
notBefore: checkedDeadline(
|
|
49
|
-
at,
|
|
50
|
-
this.#backoff.delay(retryClass, attempt),
|
|
51
|
-
),
|
|
38
|
+
retryClass: checkedClass,
|
|
39
|
+
notBefore: checkedDeadline(at, this.#backoff.delay(checkedClass, attempt)),
|
|
52
40
|
};
|
|
53
41
|
this.#scheduled = attempt;
|
|
54
42
|
return schedule;
|
package/src/bridge/session.ts
CHANGED
|
@@ -5,10 +5,7 @@ import type {
|
|
|
5
5
|
DomainCapabilityDescriptor,
|
|
6
6
|
DomainId,
|
|
7
7
|
} from "./generated/protocol.ts";
|
|
8
|
-
import {
|
|
9
|
-
parseBridgeHelloRequest,
|
|
10
|
-
parseBridgeNegotiationReceipt,
|
|
11
|
-
} from "./validation.ts";
|
|
8
|
+
import { parseBridgeHelloRequest, parseBridgeNegotiationReceipt } from "./validation.ts";
|
|
12
9
|
|
|
13
10
|
export interface BridgeNegotiationAdapter {
|
|
14
11
|
negotiate(request: BridgeHelloRequest): Promise<unknown>;
|
|
@@ -39,16 +36,10 @@ export class BridgeSession {
|
|
|
39
36
|
constructor(receipt: BridgeNegotiationReceipt) {
|
|
40
37
|
this.receipt = parseBridgeNegotiationReceipt(receipt);
|
|
41
38
|
this.#capabilities = new Map(
|
|
42
|
-
this.receipt.domainCapabilities.map((value) => [
|
|
43
|
-
value.domainId,
|
|
44
|
-
value,
|
|
45
|
-
]),
|
|
39
|
+
this.receipt.domainCapabilities.map((value) => [value.domainId, value]),
|
|
46
40
|
);
|
|
47
41
|
this.#authorities = new Map(
|
|
48
|
-
this.receipt.domainAuthorities.map((value) => [
|
|
49
|
-
value.domainId,
|
|
50
|
-
value,
|
|
51
|
-
]),
|
|
42
|
+
this.receipt.domainAuthorities.map((value) => [value.domainId, value]),
|
|
52
43
|
);
|
|
53
44
|
}
|
|
54
45
|
|
|
@@ -61,7 +52,6 @@ export class BridgeSession {
|
|
|
61
52
|
}
|
|
62
53
|
|
|
63
54
|
supports(domainId: DomainId, capability: string): boolean {
|
|
64
|
-
return this.capability(domainId)?.capabilities.includes(capability) ??
|
|
65
|
-
false;
|
|
55
|
+
return this.capability(domainId)?.capabilities.includes(capability) ?? false;
|
|
66
56
|
}
|
|
67
57
|
}
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
type ConnectionFailure,
|
|
4
|
-
type ConnectionFailureReporter,
|
|
5
|
-
} from "@inflatable-cookie/longhorn/core";
|
|
1
|
+
import { CheckedSnapshotConnection, CheckedSubscription } from "@inflatable-cookie/longhorn/core";
|
|
2
|
+
import type { ConnectionFailureReporter } from "@inflatable-cookie/longhorn/core";
|
|
6
3
|
|
|
7
4
|
import type {
|
|
8
5
|
BridgeEventEnvelope,
|
|
@@ -12,10 +9,7 @@ import type {
|
|
|
12
9
|
BridgeSessionId,
|
|
13
10
|
} from "../generated/protocol.ts";
|
|
14
11
|
import type { BridgeCodec } from "../validation/base.ts";
|
|
15
|
-
import {
|
|
16
|
-
parseBridgeEventEnvelope,
|
|
17
|
-
parseBridgeSnapshotEnvelope,
|
|
18
|
-
} from "../validation/streams.ts";
|
|
12
|
+
import { parseBridgeEventEnvelope, parseBridgeSnapshotEnvelope } from "../validation/streams.ts";
|
|
19
13
|
import { BridgeStreamTracker, newer } from "./tracker.ts";
|
|
20
14
|
import type { BridgeStreamSource } from "./source.ts";
|
|
21
15
|
|
|
@@ -25,65 +19,40 @@ export interface BridgeStreamConnectionOptions<Snapshot, Event> {
|
|
|
25
19
|
readonly source: BridgeStreamSource;
|
|
26
20
|
readonly snapshot: BridgeCodec<Snapshot>;
|
|
27
21
|
readonly event: BridgeCodec<Event>;
|
|
28
|
-
readonly apply: (
|
|
29
|
-
|
|
30
|
-
event: Event,
|
|
31
|
-
) => Snapshot;
|
|
32
|
-
readonly onSnapshot?: (
|
|
33
|
-
snapshot: BridgeSnapshotEnvelope<Snapshot>,
|
|
34
|
-
) => void;
|
|
22
|
+
readonly apply: (current: Snapshot, event: Event) => Snapshot;
|
|
23
|
+
readonly onSnapshot?: (snapshot: BridgeSnapshotEnvelope<Snapshot>) => void;
|
|
35
24
|
readonly onFailure?: ConnectionFailureReporter;
|
|
36
25
|
}
|
|
37
26
|
|
|
38
|
-
export class BridgeStreamConnection<Snapshot>
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
constructor(
|
|
45
|
-
connection: CheckedSnapshotConnection<
|
|
46
|
-
BridgeSnapshotEnvelope<Snapshot>
|
|
47
|
-
>,
|
|
48
|
-
) {
|
|
49
|
-
this.#connection = connection;
|
|
50
|
-
this.ready = connection.ready;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
current(): BridgeSnapshotEnvelope<Snapshot> | undefined {
|
|
54
|
-
return this.#connection.current();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
failures(): readonly ConnectionFailure[] {
|
|
58
|
-
return this.#connection.failures();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
dispose(): Promise<void> {
|
|
62
|
-
return this.#connection.dispose();
|
|
27
|
+
export class BridgeStreamConnection<Snapshot> extends CheckedSubscription<
|
|
28
|
+
BridgeSnapshotEnvelope<Snapshot>,
|
|
29
|
+
BridgeSnapshotEnvelope<Snapshot> | undefined
|
|
30
|
+
> {
|
|
31
|
+
constructor(connection: CheckedSnapshotConnection<BridgeSnapshotEnvelope<Snapshot>>) {
|
|
32
|
+
super(connection, (snapshot) => snapshot);
|
|
63
33
|
}
|
|
64
34
|
}
|
|
65
35
|
|
|
66
36
|
export function connectBridgeStream<Snapshot, Event>(
|
|
67
37
|
options: BridgeStreamConnectionOptions<Snapshot, Event>,
|
|
68
38
|
): BridgeStreamConnection<Snapshot> {
|
|
69
|
-
const tracker = new BridgeStreamTracker(
|
|
70
|
-
|
|
71
|
-
options.domainId,
|
|
72
|
-
);
|
|
73
|
-
const connection = new CheckedSnapshotConnection<
|
|
74
|
-
BridgeSnapshotEnvelope<Snapshot>
|
|
75
|
-
>({
|
|
39
|
+
const tracker = new BridgeStreamTracker(options.sessionId, options.domainId);
|
|
40
|
+
const connection = new CheckedSnapshotConnection<BridgeSnapshotEnvelope<Snapshot>>({
|
|
76
41
|
listen: (listener) => options.source.listen(listener),
|
|
77
42
|
loadSnapshot: () => options.source.loadSnapshot(),
|
|
78
43
|
validateSnapshot: (value) => {
|
|
79
44
|
const snapshot = parseBridgeSnapshotEnvelope(value, options.snapshot);
|
|
80
45
|
const decision = tracker.acceptSnapshot(snapshot.cursor);
|
|
81
|
-
if (
|
|
82
|
-
decision === "supersededSession" ||
|
|
83
|
-
decision === "wrongDomain"
|
|
84
|
-
) {
|
|
46
|
+
if (decision === "supersededSession" || decision === "wrongDomain") {
|
|
85
47
|
throw new BridgeStreamAuthorityError(decision);
|
|
86
48
|
}
|
|
49
|
+
// The reply is behind an event already seen, and the events in between
|
|
50
|
+
// were not applied. Nothing else will ask again if the stream goes quiet.
|
|
51
|
+
if (decision === "acceptedResnapshotRequired") {
|
|
52
|
+
connection.requestRefresh();
|
|
53
|
+
}
|
|
54
|
+
// A stale reply is returned unchanged. The tracker kept its baseline, and
|
|
55
|
+
// the connection's isNewer check drops the reply, so the stream stays live.
|
|
87
56
|
return snapshot;
|
|
88
57
|
},
|
|
89
58
|
handleEvent: (value, current) => {
|
|
@@ -118,9 +87,9 @@ function eventAction<Snapshot, Event>(
|
|
|
118
87
|
| { readonly kind: "ignore" }
|
|
119
88
|
| { readonly kind: "refresh" }
|
|
120
89
|
| {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
90
|
+
readonly kind: "snapshot";
|
|
91
|
+
readonly value: BridgeSnapshotEnvelope<Snapshot>;
|
|
92
|
+
} {
|
|
124
93
|
switch (decision) {
|
|
125
94
|
case "apply":
|
|
126
95
|
if (current === undefined) {
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import type { Unlisten } from "@inflatable-cookie/longhorn/core";
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
deserialize,
|
|
5
|
-
serialize,
|
|
6
|
-
} from "../loopback.ts";
|
|
3
|
+
import { deserialize, serialize } from "../loopback.ts";
|
|
7
4
|
|
|
8
5
|
export interface BridgeStreamSource {
|
|
9
6
|
listen(listener: (value: unknown) => void): Promise<Unlisten>;
|
|
@@ -40,8 +37,7 @@ export class DirectBridgeStreamSource implements BridgeStreamSource {
|
|
|
40
37
|
}
|
|
41
38
|
}
|
|
42
39
|
|
|
43
|
-
export class SerializedLoopbackBridgeStreamSource
|
|
44
|
-
implements BridgeStreamSource {
|
|
40
|
+
export class SerializedLoopbackBridgeStreamSource implements BridgeStreamSource {
|
|
45
41
|
readonly #source: BridgeStreamSource;
|
|
46
42
|
|
|
47
43
|
constructor(source: BridgeStreamSource) {
|
|
@@ -34,14 +34,16 @@ export class BridgeStreamTracker {
|
|
|
34
34
|
if (cursor.domainId !== this.#domainId) {
|
|
35
35
|
return "wrongDomain";
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
// Checked before any state is written: an older snapshot must never move
|
|
38
|
+
// the accepted baseline back, or a redelivered event would apply twice.
|
|
39
|
+
if (this.#accepted !== undefined && newer(this.#accepted, cursor)) {
|
|
40
|
+
return "staleSnapshot";
|
|
41
|
+
}
|
|
42
|
+
const pendingIsNewer = this.#pending !== undefined && newer(this.#pending, cursor);
|
|
39
43
|
this.#accepted = cursor;
|
|
40
44
|
this.#pending = undefined;
|
|
41
45
|
this.#requiresSnapshot = pendingIsNewer;
|
|
42
|
-
return pendingIsNewer
|
|
43
|
-
? "acceptedResnapshotRequired"
|
|
44
|
-
: "accepted";
|
|
46
|
+
return pendingIsNewer ? "acceptedResnapshotRequired" : "accepted";
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
classifyEvent(cursor: BridgeStreamCursor): BridgeStreamDecision {
|
|
@@ -99,11 +101,9 @@ export class BridgeStreamTracker {
|
|
|
99
101
|
}
|
|
100
102
|
}
|
|
101
103
|
|
|
102
|
-
export function newer(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
(candidate.authorityEpoch === current.authorityEpoch &&
|
|
108
|
-
candidate.sequence > current.sequence);
|
|
104
|
+
export function newer(candidate: BridgeStreamCursor, current: BridgeStreamCursor): boolean {
|
|
105
|
+
return (
|
|
106
|
+
candidate.authorityEpoch > current.authorityEpoch ||
|
|
107
|
+
(candidate.authorityEpoch === current.authorityEpoch && candidate.sequence > current.sequence)
|
|
108
|
+
);
|
|
109
109
|
}
|