@inflatable-cookie/longhorn 0.5.0 → 0.6.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 (126) hide show
  1. package/package.json +1 -1
  2. package/src/agent-control/inject.ts +4 -1
  3. package/src/agent-control/selection.ts +35 -20
  4. package/src/agent-control/shim.ts +302 -144
  5. package/src/bridge/direct.ts +1 -1
  6. package/src/bridge/generated/fields.ts +3 -2
  7. package/src/bridge/generated/protocol.ts +7 -3
  8. package/src/bridge/job.ts +4 -14
  9. package/src/bridge/loopback.ts +7 -25
  10. package/src/bridge/operation.ts +13 -43
  11. package/src/bridge/router.ts +10 -23
  12. package/src/bridge/runtime/connection.ts +77 -105
  13. package/src/bridge/runtime/policy.ts +8 -28
  14. package/src/bridge/runtime/retry.ts +6 -18
  15. package/src/bridge/session.ts +4 -14
  16. package/src/bridge/stream/connection.ts +16 -38
  17. package/src/bridge/stream/source.ts +2 -6
  18. package/src/bridge/stream/tracker.ts +7 -12
  19. package/src/bridge/supervision.ts +11 -26
  20. package/src/bridge/validation/base.ts +17 -39
  21. package/src/bridge/validation/jobs.ts +11 -25
  22. package/src/bridge/validation/negotiation.ts +27 -37
  23. package/src/bridge/validation/operations.ts +28 -52
  24. package/src/bridge/validation/streams.ts +3 -10
  25. package/src/commands/client.ts +21 -26
  26. package/src/commands/controller.ts +32 -47
  27. package/src/commands/generated/fields.ts +3 -2
  28. package/src/commands/generated/protocol.ts +4 -0
  29. package/src/commands/keyboard.ts +92 -89
  30. package/src/commands/ports.ts +6 -12
  31. package/src/commands/projectors.ts +50 -48
  32. package/src/commands/validation.ts +70 -97
  33. package/src/config/client.ts +6 -19
  34. package/src/config/generated/fields.ts +2 -2
  35. package/src/config/names.ts +6 -12
  36. package/src/config/validation/commands.ts +5 -17
  37. package/src/config/validation/outcomes.ts +31 -39
  38. package/src/config/validation/primitives.ts +36 -53
  39. package/src/config/validation/projection.ts +66 -94
  40. package/src/core/connection.ts +70 -61
  41. package/src/core/record.ts +4 -0
  42. package/src/core/transport.ts +4 -15
  43. package/src/core/wire-copy.ts +6 -0
  44. package/src/history/client.ts +6 -12
  45. package/src/history/controller.ts +46 -46
  46. package/src/history/direct.ts +1 -3
  47. package/src/history/generated/fields.ts +2 -2
  48. package/src/history/ports.ts +4 -6
  49. package/src/history/serialized.ts +9 -13
  50. package/src/history/validation.ts +71 -55
  51. package/src/history-tree/client.ts +87 -11
  52. package/src/history-tree/controller.ts +313 -70
  53. package/src/history-tree/direct.ts +3 -1
  54. package/src/history-tree/generated/fields.ts +2 -2
  55. package/src/history-tree/ports.ts +3 -1
  56. package/src/history-tree/serialized.ts +39 -11
  57. package/src/history-tree/validation.ts +262 -63
  58. package/src/layout/generated/protocol.ts +2 -2
  59. package/src/layout/ratio.ts +2 -8
  60. package/src/layout/validation.ts +10 -19
  61. package/src/layout/visibility.ts +1 -2
  62. package/src/licence/client.ts +44 -7
  63. package/src/licence/controller.ts +76 -40
  64. package/src/licence/direct.ts +3 -1
  65. package/src/licence/generated/fields.ts +2 -2
  66. package/src/licence/generated/key-conformance.json +28 -0
  67. package/src/licence/generated/protocol.ts +1 -1
  68. package/src/licence/key.ts +40 -12
  69. package/src/licence/ports.ts +3 -1
  70. package/src/licence/serialized.ts +32 -9
  71. package/src/licence/validation.ts +120 -36
  72. package/src/native-content/client.ts +178 -236
  73. package/src/native-content/generated/fields.ts +2 -2
  74. package/src/native-content/ports.ts +9 -15
  75. package/src/native-content/serialized.ts +12 -19
  76. package/src/native-content/validation/base.ts +57 -50
  77. package/src/native-content/validation/messages.ts +21 -35
  78. package/src/native-content/validation/state.ts +31 -33
  79. package/src/notifications/client.ts +87 -31
  80. package/src/notifications/controller.ts +215 -61
  81. package/src/notifications/direct.ts +3 -1
  82. package/src/notifications/generated/fields.ts +2 -2
  83. package/src/notifications/ports.ts +7 -3
  84. package/src/notifications/serialized.ts +11 -9
  85. package/src/notifications/validation.ts +147 -43
  86. package/src/operation/bridge.ts +117 -50
  87. package/src/operation/client.ts +38 -24
  88. package/src/operation/controller.ts +33 -42
  89. package/src/operation/direct.ts +1 -3
  90. package/src/operation/generated/fields.ts +2 -2
  91. package/src/operation/ports.ts +10 -9
  92. package/src/operation/serialized.ts +8 -9
  93. package/src/operation/validation.ts +152 -65
  94. package/src/settings/client.ts +11 -44
  95. package/src/settings/connection.ts +26 -40
  96. package/src/settings/generated/fields.ts +3 -3
  97. package/src/settings/generated/protocol.ts +9 -4
  98. package/src/settings/names.ts +2 -4
  99. package/src/settings/registry.ts +23 -25
  100. package/src/settings/validation/authority.ts +19 -17
  101. package/src/settings/validation/command.ts +17 -37
  102. package/src/settings/validation/error.ts +2 -8
  103. package/src/settings/validation/primitives.ts +26 -42
  104. package/src/settings/validation/projection.ts +8 -24
  105. package/src/settings/validation/registry.ts +25 -73
  106. package/src/surface-transfer/client.ts +6 -18
  107. package/src/surface-transfer/generated/fields.ts +2 -2
  108. package/src/surface-transfer/validation.ts +31 -49
  109. package/src/surfaces/client.ts +12 -22
  110. package/src/surfaces/generated/fields.ts +2 -2
  111. package/src/surfaces/validation.ts +40 -49
  112. package/src/transfer/client.ts +26 -60
  113. package/src/transfer/connection.ts +12 -20
  114. package/src/transfer/generated/fields.ts +5 -5
  115. package/src/transfer/generated/protocol.ts +4 -4
  116. package/src/transfer/payload.ts +13 -31
  117. package/src/transfer/validation.ts +39 -75
  118. package/src/update/client.ts +59 -9
  119. package/src/update/controller.ts +113 -48
  120. package/src/update/direct.ts +3 -1
  121. package/src/update/generated/fields.ts +3 -3
  122. package/src/update/generated/protocol.ts +7 -3
  123. package/src/update/generated/variant-fields.ts +1 -0
  124. package/src/update/ports.ts +6 -2
  125. package/src/update/serialized.ts +34 -10
  126. package/src/update/validation.ts +115 -35
@@ -1,9 +1,35 @@
1
1
  import { ForkHistoryClient } from "./client.ts";
2
- import { FORK_HISTORY_PROTOCOL_VERSION, MAXIMUM_FORK_HISTORY_PAGE_SIZE, type ForkBranchId, type ForkBranchPageSnapshot, type ForkChangedEvent, type ForkContinuationPageSnapshot, type ForkEntryRecord, type ForkPruneResult, type ForkRemovalReceiptProjection, type ForkNavigationResult, type ForkNavigationTargetProjection, type ForkPathPageSnapshot, type ForkPathTargetProjection, type ForkSnapshot } from "./generated/protocol.ts";
2
+ import {
3
+ FORK_HISTORY_PROTOCOL_VERSION,
4
+ MAXIMUM_FORK_HISTORY_PAGE_SIZE,
5
+ } from "./generated/protocol.ts";
6
+ import type {
7
+ ForkBranchId,
8
+ ForkBranchPageSnapshot,
9
+ ForkChangedEvent,
10
+ ForkContinuationPageSnapshot,
11
+ ForkEntryRecord,
12
+ ForkPruneResult,
13
+ ForkRemovalReceiptProjection,
14
+ ForkNavigationResult,
15
+ ForkNavigationTargetProjection,
16
+ ForkPathPageSnapshot,
17
+ ForkPathTargetProjection,
18
+ ForkSnapshot,
19
+ } from "./generated/protocol.ts";
3
20
  import type { ForkHistoryPort, ForkHistoryUnlisten } from "./ports.ts";
4
21
 
5
- export type ForkHistoryControllerStatus = { readonly kind: "idle" } | { readonly kind: "loading" } | { readonly kind: "ready" } | { readonly kind: "failed"; readonly error: unknown };
6
- export interface ForkHistoryControllerOptions { readonly port: ForkHistoryPort; readonly pathPageSize?: number; readonly branchPageSize?: number; readonly continuationPageSize?: number; }
22
+ export type ForkHistoryControllerStatus =
23
+ | { readonly kind: "idle" }
24
+ | { readonly kind: "loading" }
25
+ | { readonly kind: "ready" }
26
+ | { readonly kind: "failed"; readonly error: unknown };
27
+ export interface ForkHistoryControllerOptions {
28
+ readonly port: ForkHistoryPort;
29
+ readonly pathPageSize?: number;
30
+ readonly branchPageSize?: number;
31
+ readonly continuationPageSize?: number;
32
+ }
7
33
 
8
34
  export class ForkHistoryController {
9
35
  readonly #client: ForkHistoryClient;
@@ -12,46 +38,92 @@ export class ForkHistoryController {
12
38
  readonly #branchPageSize: number;
13
39
  readonly #continuationPageSize: number;
14
40
  #status: ForkHistoryControllerStatus = { kind: "idle" };
15
- #snapshot?: ForkSnapshot;
16
- #path?: ForkPathPageSnapshot;
17
- #branches?: ForkBranchPageSnapshot;
41
+ #snapshot: ForkSnapshot | undefined;
42
+ #path: ForkPathPageSnapshot | undefined;
43
+ #branches: ForkBranchPageSnapshot | undefined;
18
44
  #pathTarget: ForkPathTargetProjection = { kind: "default" };
19
45
  #started = false;
20
46
  #lifecycle = 0;
21
47
  #load = 0;
22
48
  #navigation = 0;
23
49
  #pending = false;
24
- #unlisten: ForkHistoryUnlisten[] = [];
50
+ #unlisten: Array<ForkHistoryUnlisten> = [];
25
51
 
26
- constructor(options: ForkHistoryControllerOptions) { this.#client = new ForkHistoryClient(options.port); this.#pathPageSize = pageSize(options.pathPageSize ?? 50); this.#branchPageSize = pageSize(options.branchPageSize ?? 50); this.#continuationPageSize = pageSize(options.continuationPageSize ?? 50); }
27
- get status() { return this.#status; }
28
- get snapshot() { return this.#snapshot; }
29
- get path() { return this.#path; }
30
- get branches() { return this.#branches; }
31
- get entries(): readonly ForkEntryRecord[] { return this.#path?.entries ?? []; }
32
- get navigationPending() { return this.#pending; }
33
- get canUndo() { return (this.#snapshot?.summary.undoDepth ?? 0) > 0; }
34
- get canRedo() { return (this.#snapshot?.summary.redoDepth ?? 0) > 0; }
35
- observe(observer: () => void): () => void { this.#observers.add(observer); return () => this.#observers.delete(observer); }
52
+ constructor(options: ForkHistoryControllerOptions) {
53
+ this.#client = new ForkHistoryClient(options.port);
54
+ this.#pathPageSize = pageSize(options.pathPageSize ?? 50);
55
+ this.#branchPageSize = pageSize(options.branchPageSize ?? 50);
56
+ this.#continuationPageSize = pageSize(options.continuationPageSize ?? 50);
57
+ }
58
+ get status() {
59
+ return this.#status;
60
+ }
61
+ get snapshot() {
62
+ return this.#snapshot;
63
+ }
64
+ get path() {
65
+ return this.#path;
66
+ }
67
+ get branches() {
68
+ return this.#branches;
69
+ }
70
+ get entries(): ReadonlyArray<ForkEntryRecord> {
71
+ return this.#path?.entries ?? [];
72
+ }
73
+ get navigationPending() {
74
+ return this.#pending;
75
+ }
76
+ get canUndo() {
77
+ return (this.#snapshot?.summary.undoDepth ?? 0) > 0;
78
+ }
79
+ get canRedo() {
80
+ return (this.#snapshot?.summary.redoDepth ?? 0) > 0;
81
+ }
82
+ observe(observer: () => void): () => void {
83
+ this.#observers.add(observer);
84
+ return () => this.#observers.delete(observer);
85
+ }
36
86
 
37
87
  async start(): Promise<void> {
38
- if (this.#started) return;
39
- this.#started = true; const lifecycle = ++this.#lifecycle; this.#setStatus({ kind: "loading" });
88
+ if (this.#started) {
89
+ return;
90
+ }
91
+ this.#started = true;
92
+ const lifecycle = ++this.#lifecycle;
93
+ this.#setStatus({ kind: "loading" });
40
94
  try {
41
95
  const unlisten = await this.#client.listen((event) => this.#changed(event));
42
- if (!this.#started || lifecycle !== this.#lifecycle) { await unlisten(); return; }
43
- this.#unlisten.push(unlisten); await this.refresh();
44
- } catch (error) { if (this.#started && lifecycle === this.#lifecycle) this.#setStatus({ kind: "failed", error }); }
96
+ if (!this.#started || lifecycle !== this.#lifecycle) {
97
+ await unlisten();
98
+ return;
99
+ }
100
+ this.#unlisten.push(unlisten);
101
+ await this.refresh();
102
+ } catch (error) {
103
+ if (this.#started && lifecycle === this.#lifecycle) {
104
+ this.#setStatus({ kind: "failed", error });
105
+ }
106
+ }
45
107
  }
46
108
 
47
109
  async stop(): Promise<void> {
48
- this.#started = false; this.#lifecycle += 1; this.#load += 1; this.#navigation += 1;
110
+ this.#started = false;
111
+ this.#lifecycle += 1;
112
+ this.#load += 1;
113
+ this.#navigation += 1;
49
114
  await Promise.allSettled(this.#unlisten.splice(0).map((dispose) => dispose()));
50
- this.#snapshot = undefined; this.#path = undefined; this.#branches = undefined; this.#pathTarget = { kind: "default" }; this.#pending = false; this.#setStatus({ kind: "idle" });
115
+ this.#snapshot = undefined;
116
+ this.#path = undefined;
117
+ this.#branches = undefined;
118
+ this.#pathTarget = { kind: "default" };
119
+ this.#pending = false;
120
+ this.#setStatus({ kind: "idle" });
51
121
  }
52
122
 
53
123
  async refresh(): Promise<void> {
54
- if (!this.#started) return;
124
+ if (!this.#started) {
125
+ return;
126
+ }
55
127
  const load = ++this.#load;
56
128
  // The offset a consumer had paged to, so a refresh returns the page they
57
129
  // were looking at rather than sending them back to the first one.
@@ -60,25 +132,41 @@ export class ForkHistoryController {
60
132
  for (let attempt = 0; attempt < 2; attempt += 1) {
61
133
  const snapshot = await this.#client.snapshot();
62
134
  const path = await this.#client.path(this.#pathCommand(snapshot, this.#pathTarget));
63
- if (!same(snapshot, path)) continue;
135
+ if (!same(snapshot, path)) {
136
+ continue;
137
+ }
64
138
  // Only for a consumer that asked for branches. Loading a page nobody
65
139
  // wanted would make every refresh pay for a projection it will not use.
66
- const branches = branchOffset === undefined
67
- ? undefined
68
- : await this.#client.branches(this.#branchCommand(snapshot, branchOffset));
69
- if (branches !== undefined && !same(snapshot, branches)) continue;
70
- if (this.#started && load === this.#load) { this.#install(snapshot, path, branches); this.#setStatus({ kind: "ready" }); }
140
+ const branches =
141
+ branchOffset === undefined
142
+ ? undefined
143
+ : await this.#client.branches(this.#branchCommand(snapshot, branchOffset));
144
+ if (branches !== undefined && !same(snapshot, branches)) {
145
+ continue;
146
+ }
147
+ if (this.#started && load === this.#load) {
148
+ this.#install(snapshot, path, branches);
149
+ this.#setStatus({ kind: "ready" });
150
+ }
71
151
  return;
72
152
  }
73
153
  throw new ForkHistoryProjectionGapError();
74
- } catch (error) { if (this.#started && load === this.#load) this.#setStatus({ kind: "failed", error }); }
154
+ } catch (error) {
155
+ if (this.#started && load === this.#load) {
156
+ this.#setStatus({ kind: "failed", error });
157
+ }
158
+ }
75
159
  }
76
160
 
77
161
  async loadBranches(offset = 0): Promise<ForkBranchPageSnapshot> {
78
162
  const snapshot = this.#required();
79
163
  const value = await this.#client.branches(this.#branchCommand(snapshot, offset));
80
- if (!same(snapshot, value)) throw new ForkHistoryProjectionGapError();
81
- this.#branches = value; this.#notify(); return value;
164
+ if (!same(snapshot, value)) {
165
+ throw new ForkHistoryProjectionGapError();
166
+ }
167
+ this.#branches = value;
168
+ this.#notify();
169
+ return value;
82
170
  }
83
171
 
84
172
  /**
@@ -91,10 +179,23 @@ export class ForkHistoryController {
91
179
  * Not cached and not refreshed. A consumer asks when the operator opens an
92
180
  * entry, and the revision check below tells them if it went stale meanwhile.
93
181
  */
94
- async loadContinuations(anchorEntryId: string | null, offset = 0): Promise<ForkContinuationPageSnapshot> {
182
+ async loadContinuations(
183
+ anchorEntryId: string | null,
184
+ offset = 0,
185
+ ): Promise<ForkContinuationPageSnapshot> {
95
186
  const snapshot = this.#required();
96
- const value = await this.#client.continuations({ protocolVersion: FORK_HISTORY_PROTOCOL_VERSION, authorityEpoch: snapshot.authorityEpoch, historyId: snapshot.summary.historyId, expectedRevision: snapshot.summary.revision, anchorEntryId, offset, limit: this.#continuationPageSize });
97
- if (!same(snapshot, value)) throw new ForkHistoryProjectionGapError();
187
+ const value = await this.#client.continuations({
188
+ protocolVersion: FORK_HISTORY_PROTOCOL_VERSION,
189
+ authorityEpoch: snapshot.authorityEpoch,
190
+ historyId: snapshot.summary.historyId,
191
+ expectedRevision: snapshot.summary.revision,
192
+ anchorEntryId,
193
+ offset,
194
+ limit: this.#continuationPageSize,
195
+ });
196
+ if (!same(snapshot, value)) {
197
+ throw new ForkHistoryProjectionGapError();
198
+ }
98
199
  return value;
99
200
  }
100
201
 
@@ -104,8 +205,13 @@ export class ForkHistoryController {
104
205
  */
105
206
  async loadContinuationRun(fromEntryId: string, offset = 0): Promise<ForkPathPageSnapshot> {
106
207
  const snapshot = this.#required();
107
- const value = await this.#client.path({ ...this.#pathCommand(snapshot, { kind: "continuation", fromEntryId }), offset });
108
- if (!same(snapshot, value)) throw new ForkHistoryProjectionGapError();
208
+ const value = await this.#client.path({
209
+ ...this.#pathCommand(snapshot, { kind: "continuation", fromEntryId }),
210
+ offset,
211
+ });
212
+ if (!same(snapshot, value)) {
213
+ throw new ForkHistoryProjectionGapError();
214
+ }
109
215
  return value;
110
216
  }
111
217
 
@@ -121,7 +227,13 @@ export class ForkHistoryController {
121
227
  */
122
228
  async deleteContinuation(entryId: string): Promise<ForkRemovalReceiptProjection> {
123
229
  const snapshot = this.#required();
124
- const receipt = await this.#client.deleteContinuation({ protocolVersion: FORK_HISTORY_PROTOCOL_VERSION, authorityEpoch: snapshot.authorityEpoch, historyId: snapshot.summary.historyId, expectedRevision: snapshot.summary.revision, entryId });
230
+ const receipt = await this.#client.deleteContinuation({
231
+ protocolVersion: FORK_HISTORY_PROTOCOL_VERSION,
232
+ authorityEpoch: snapshot.authorityEpoch,
233
+ historyId: snapshot.summary.historyId,
234
+ expectedRevision: snapshot.summary.revision,
235
+ entryId,
236
+ });
125
237
  // Every page the consumer holds may name entries that no longer exist, so
126
238
  // this refresh is not optional and is not left to the changed event.
127
239
  await this.refresh();
@@ -142,18 +254,41 @@ export class ForkHistoryController {
142
254
  */
143
255
  async prune(maximumEntries: number, maximumEncodedWeight: number): Promise<ForkPruneResult> {
144
256
  const snapshot = this.#required();
145
- const result = await this.#client.prune({ protocolVersion: FORK_HISTORY_PROTOCOL_VERSION, authorityEpoch: snapshot.authorityEpoch, historyId: snapshot.summary.historyId, expectedRevision: snapshot.summary.revision, maximumEntries, maximumEncodedWeight });
146
- if (result.status === "pruned") await this.refresh();
257
+ const result = await this.#client.prune({
258
+ protocolVersion: FORK_HISTORY_PROTOCOL_VERSION,
259
+ authorityEpoch: snapshot.authorityEpoch,
260
+ historyId: snapshot.summary.historyId,
261
+ expectedRevision: snapshot.summary.revision,
262
+ maximumEntries,
263
+ maximumEncodedWeight,
264
+ });
265
+ if (result.status === "pruned") {
266
+ await this.refresh();
267
+ }
147
268
  return result;
148
269
  }
149
270
 
150
- async selectDefaultPath(): Promise<void> { this.#pathTarget = { kind: "default" }; await this.refresh(); }
151
- async selectBranchPath(branchId: ForkBranchId): Promise<void> { this.#pathTarget = { kind: "branch", branchId }; await this.refresh(); }
152
- undo(): Promise<ForkNavigationResult> { return this.#navigate({ kind: "undo" }); }
153
- redo(): Promise<ForkNavigationResult> { return this.#navigate({ kind: "redo" }); }
154
- checkout(branchId: ForkBranchId, entryId: string): Promise<ForkNavigationResult> { return this.#navigate({ kind: "checkout", branchId, entryId }); }
271
+ async selectDefaultPath(): Promise<void> {
272
+ this.#pathTarget = { kind: "default" };
273
+ await this.refresh();
274
+ }
275
+ async selectBranchPath(branchId: ForkBranchId): Promise<void> {
276
+ this.#pathTarget = { kind: "branch", branchId };
277
+ await this.refresh();
278
+ }
279
+ undo(): Promise<ForkNavigationResult> {
280
+ return this.#navigate({ kind: "undo" });
281
+ }
282
+ redo(): Promise<ForkNavigationResult> {
283
+ return this.#navigate({ kind: "redo" });
284
+ }
285
+ checkout(branchId: ForkBranchId, entryId: string): Promise<ForkNavigationResult> {
286
+ return this.#navigate({ kind: "checkout", branchId, entryId });
287
+ }
155
288
  /** Move to a branch's root, the position before its first entry. */
156
- checkoutBranchRoot(branchId: ForkBranchId): Promise<ForkNavigationResult> { return this.#navigate({ kind: "checkoutBranchRoot", branchId }); }
289
+ checkoutBranchRoot(branchId: ForkBranchId): Promise<ForkNavigationResult> {
290
+ return this.#navigate({ kind: "checkoutBranchRoot", branchId });
291
+ }
157
292
  /**
158
293
  * Check out the run beginning at one entry: it becomes the current line,
159
294
  * and the previous line becomes a fork at the same entry. No delta of it is
@@ -163,37 +298,145 @@ export class ForkHistoryController {
163
298
  * This commits. Browsing the forks does not — use `loadContinuationRun` to
164
299
  * show a fork's entries without choosing it.
165
300
  */
166
- checkoutContinuation(entryId: string): Promise<ForkNavigationResult> { return this.#navigate({ kind: "checkoutContinuation", entryId }); }
301
+ checkoutContinuation(entryId: string): Promise<ForkNavigationResult> {
302
+ return this.#navigate({ kind: "checkoutContinuation", entryId });
303
+ }
167
304
 
168
305
  async #navigate(target: ForkNavigationTargetProjection): Promise<ForkNavigationResult> {
169
- const snapshot = this.#required(); const navigation = ++this.#navigation; this.#pending = true; this.#notify();
306
+ const snapshot = this.#required();
307
+ const navigation = ++this.#navigation;
308
+ this.#pending = true;
309
+ this.#notify();
170
310
  try {
171
- const result = await this.#client.navigate({ protocolVersion: FORK_HISTORY_PROTOCOL_VERSION, authorityEpoch: snapshot.authorityEpoch, historyId: snapshot.summary.historyId, planId: this.#client.nextPlanId(), expectedRevision: snapshot.summary.revision, target });
172
- if (!this.#started || navigation !== this.#navigation) throw new ForkHistoryLateResultError();
311
+ const result = await this.#client.navigate({
312
+ protocolVersion: FORK_HISTORY_PROTOCOL_VERSION,
313
+ authorityEpoch: snapshot.authorityEpoch,
314
+ historyId: snapshot.summary.historyId,
315
+ planId: this.#client.nextPlanId(),
316
+ expectedRevision: snapshot.summary.revision,
317
+ target,
318
+ });
319
+ if (!this.#started || navigation !== this.#navigation) {
320
+ throw new ForkHistoryLateResultError();
321
+ }
173
322
  const path = await this.#client.path(this.#pathCommand(result.snapshot, this.#pathTarget));
174
- if (!this.#started || navigation !== this.#navigation) throw new ForkHistoryLateResultError();
175
- if (!same(result.snapshot, path)) throw new ForkHistoryProjectionGapError();
323
+ if (!this.#started || navigation !== this.#navigation) {
324
+ throw new ForkHistoryLateResultError();
325
+ }
326
+ if (!same(result.snapshot, path)) {
327
+ throw new ForkHistoryProjectionGapError();
328
+ }
176
329
  const branchOffset = this.#branches?.offset;
177
- const branches = branchOffset === undefined
178
- ? undefined
179
- : await this.#client.branches(this.#branchCommand(result.snapshot, branchOffset));
180
- if (!this.#started || navigation !== this.#navigation) throw new ForkHistoryLateResultError();
181
- if (branches !== undefined && !same(result.snapshot, branches)) throw new ForkHistoryProjectionGapError();
182
- this.#install(result.snapshot, path, branches); this.#setStatus({ kind: "ready" }); return result;
183
- } finally { if (navigation === this.#navigation) { this.#pending = false; this.#notify(); } }
330
+ const branches =
331
+ branchOffset === undefined
332
+ ? undefined
333
+ : await this.#client.branches(this.#branchCommand(result.snapshot, branchOffset));
334
+ if (!this.#started || navigation !== this.#navigation) {
335
+ throw new ForkHistoryLateResultError();
336
+ }
337
+ if (branches !== undefined && !same(result.snapshot, branches)) {
338
+ throw new ForkHistoryProjectionGapError();
339
+ }
340
+ this.#install(result.snapshot, path, branches);
341
+ this.#setStatus({ kind: "ready" });
342
+ return result;
343
+ } finally {
344
+ if (navigation === this.#navigation) {
345
+ this.#pending = false;
346
+ this.#notify();
347
+ }
348
+ }
184
349
  }
185
350
 
186
- #pathCommand(snapshot: ForkSnapshot, target: ForkPathTargetProjection) { return { protocolVersion: FORK_HISTORY_PROTOCOL_VERSION, authorityEpoch: snapshot.authorityEpoch, historyId: snapshot.summary.historyId, expectedRevision: snapshot.summary.revision, target, offset: 0, limit: this.#pathPageSize } as const; }
187
- #required(): ForkSnapshot { if (!this.#started || this.#snapshot === undefined) throw new ForkHistoryUnavailableError(); return this.#snapshot; }
188
- #branchCommand(snapshot: ForkSnapshot, offset: number) { return { protocolVersion: FORK_HISTORY_PROTOCOL_VERSION, authorityEpoch: snapshot.authorityEpoch, historyId: snapshot.summary.historyId, expectedRevision: snapshot.summary.revision, offset, limit: this.#branchPageSize } as const; }
189
- #install(snapshot: ForkSnapshot, path: ForkPathPageSnapshot, branches?: ForkBranchPageSnapshot): void { const current = this.#snapshot; if (current !== undefined && snapshot.authorityEpoch === current.authorityEpoch && snapshot.summary.historyId === current.summary.historyId && snapshot.summary.revision < current.summary.revision) return; this.#snapshot = snapshot; this.#path = path; if (branches !== undefined) this.#branches = branches; else if (this.#branches !== undefined && !same(snapshot, this.#branches)) this.#branches = undefined; this.#notify(); }
190
- #changed(event: ForkChangedEvent): void { const snapshot = this.#snapshot; if (!this.#started || snapshot === undefined || event.authorityEpoch !== snapshot.authorityEpoch || event.historyId !== snapshot.summary.historyId || event.committedRevision > snapshot.summary.revision) void this.refresh(); }
191
- #setStatus(status: ForkHistoryControllerStatus): void { this.#status = status; this.#notify(); }
192
- #notify(): void { for (const observer of this.#observers) observer(); }
351
+ #pathCommand(snapshot: ForkSnapshot, target: ForkPathTargetProjection) {
352
+ return {
353
+ protocolVersion: FORK_HISTORY_PROTOCOL_VERSION,
354
+ authorityEpoch: snapshot.authorityEpoch,
355
+ historyId: snapshot.summary.historyId,
356
+ expectedRevision: snapshot.summary.revision,
357
+ target,
358
+ offset: 0,
359
+ limit: this.#pathPageSize,
360
+ } as const;
361
+ }
362
+ #required(): ForkSnapshot {
363
+ if (!this.#started || this.#snapshot === undefined) {
364
+ throw new ForkHistoryUnavailableError();
365
+ }
366
+ return this.#snapshot;
367
+ }
368
+ #branchCommand(snapshot: ForkSnapshot, offset: number) {
369
+ return {
370
+ protocolVersion: FORK_HISTORY_PROTOCOL_VERSION,
371
+ authorityEpoch: snapshot.authorityEpoch,
372
+ historyId: snapshot.summary.historyId,
373
+ expectedRevision: snapshot.summary.revision,
374
+ offset,
375
+ limit: this.#branchPageSize,
376
+ } as const;
377
+ }
378
+ #install(
379
+ snapshot: ForkSnapshot,
380
+ path: ForkPathPageSnapshot,
381
+ branches?: ForkBranchPageSnapshot,
382
+ ): void {
383
+ const current = this.#snapshot;
384
+ if (
385
+ current !== undefined &&
386
+ snapshot.authorityEpoch === current.authorityEpoch &&
387
+ snapshot.summary.historyId === current.summary.historyId &&
388
+ snapshot.summary.revision < current.summary.revision
389
+ ) {
390
+ return;
391
+ }
392
+ this.#snapshot = snapshot;
393
+ this.#path = path;
394
+ if (branches !== undefined) {
395
+ this.#branches = branches;
396
+ } else if (this.#branches !== undefined && !same(snapshot, this.#branches)) {
397
+ this.#branches = undefined;
398
+ }
399
+ this.#notify();
400
+ }
401
+ #changed(event: ForkChangedEvent): void {
402
+ const snapshot = this.#snapshot;
403
+ if (
404
+ !this.#started ||
405
+ snapshot === undefined ||
406
+ event.authorityEpoch !== snapshot.authorityEpoch ||
407
+ event.historyId !== snapshot.summary.historyId ||
408
+ event.committedRevision > snapshot.summary.revision
409
+ ) {
410
+ void this.refresh();
411
+ }
412
+ }
413
+ #setStatus(status: ForkHistoryControllerStatus): void {
414
+ this.#status = status;
415
+ this.#notify();
416
+ }
417
+ #notify(): void {
418
+ for (const observer of this.#observers) {
419
+ observer();
420
+ }
421
+ }
193
422
  }
194
423
 
195
- function same(snapshot: ForkSnapshot, page: { authorityEpoch: number; historyId: string; revision: number }): boolean { return snapshot.authorityEpoch === page.authorityEpoch && snapshot.summary.historyId === page.historyId && snapshot.summary.revision === page.revision; }
196
- function pageSize(value: number): number { if (!Number.isSafeInteger(value) || value < 1 || value > MAXIMUM_FORK_HISTORY_PAGE_SIZE) throw new RangeError(`fork-history page size must be 1..${MAXIMUM_FORK_HISTORY_PAGE_SIZE}`); return value; }
424
+ function same(
425
+ snapshot: ForkSnapshot,
426
+ page: { authorityEpoch: number; historyId: string; revision: number },
427
+ ): boolean {
428
+ return (
429
+ snapshot.authorityEpoch === page.authorityEpoch &&
430
+ snapshot.summary.historyId === page.historyId &&
431
+ snapshot.summary.revision === page.revision
432
+ );
433
+ }
434
+ function pageSize(value: number): number {
435
+ if (!Number.isSafeInteger(value) || value < 1 || value > MAXIMUM_FORK_HISTORY_PAGE_SIZE) {
436
+ throw new RangeError(`fork-history page size must be 1..${MAXIMUM_FORK_HISTORY_PAGE_SIZE}`);
437
+ }
438
+ return value;
439
+ }
197
440
  export class ForkHistoryUnavailableError extends Error {}
198
441
  export class ForkHistoryProjectionGapError extends Error {}
199
442
  export class ForkHistoryLateResultError extends Error {}
@@ -1,3 +1,5 @@
1
1
  import type { ForkHistoryPort } from "./ports.ts";
2
2
  export type DirectForkHistoryHandlers = ForkHistoryPort;
3
- export function createDirectForkHistoryPort(handlers: DirectForkHistoryHandlers): ForkHistoryPort { return handlers; }
3
+ export function createDirectForkHistoryPort(handlers: DirectForkHistoryHandlers): ForkHistoryPort {
4
+ return handlers;
5
+ }
@@ -3,7 +3,7 @@
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 HISTORY_TREE_FIELDS: Record<string, readonly string[]> = {
6
+ export const HISTORY_TREE_FIELDS = {
7
7
  "ForkSummaryProjection": ["historyId", "revision", "currentBranchId", "currentEntryId", "undoDepth", "redoDepth", "nextUndoLabel", "nextRedoLabel", "retainedEntryCount", "retainedEncodedWeight", "branchCount", "alternatePathCount"],
8
8
  "ForkSnapshot": ["protocolVersion", "authorityEpoch", "summary"],
9
9
  "ForkEntryRecord": ["entryId", "label", "kindId", "recordedAt", "groupId", "sequence", "committedRevision", "encodedWeight", "position", "continuationCount"],
@@ -23,4 +23,4 @@ export const HISTORY_TREE_FIELDS: Record<string, readonly string[]> = {
23
23
  "ForkNavigationReceiptProjection": ["historyId", "planId", "previousRevision", "committedRevision", "sourceEntryId", "targetEntryId", "targetBranchId", "movedEntryIds"],
24
24
  "ForkNavigationRejectionProjection": ["code", "detail", "refreshRequired"],
25
25
  "ForkChangedEvent": ["protocolVersion", "authorityEpoch", "historyId", "previousRevision", "committedRevision", "kind"],
26
- };
26
+ } as const;
@@ -26,7 +26,9 @@ export interface ForkHistoryPort {
26
26
  deleteContinuation(command: ForkDeleteContinuationCommand): Promise<unknown>;
27
27
  prune(command: ForkPruneCommand): Promise<unknown>;
28
28
  navigate(command: ForkNavigationCommand): Promise<unknown>;
29
- listen?(listener: (event: unknown) => void): ForkHistoryUnlisten | Promise<ForkHistoryUnlisten>;
29
+ listen?:
30
+ | ((listener: (event: unknown) => void) => ForkHistoryUnlisten | Promise<ForkHistoryUnlisten>)
31
+ | undefined;
30
32
  nextPlanId(): HistoryPlanId;
31
33
  }
32
34
 
@@ -1,15 +1,43 @@
1
- import type { ForkBranchPageCommand, ForkContinuationPageCommand, ForkDeleteContinuationCommand, ForkNavigationCommand, ForkPathPageCommand, ForkPruneCommand } from "./generated/protocol.ts";
1
+ import { wireCopy } from "../core/wire-copy.ts";
2
+ import type {
3
+ ForkBranchPageCommand,
4
+ ForkContinuationPageCommand,
5
+ ForkDeleteContinuationCommand,
6
+ ForkNavigationCommand,
7
+ ForkPathPageCommand,
8
+ ForkPruneCommand,
9
+ } from "./generated/protocol.ts";
2
10
  import type { ForkHistoryPort, ForkHistoryUnlisten } from "./ports.ts";
3
11
  export class SerializedForkHistoryPort implements ForkHistoryPort {
4
12
  constructor(readonly inner: ForkHistoryPort) {}
5
- async snapshot(): Promise<unknown> { return clone(await this.inner.snapshot()); }
6
- async path(command: ForkPathPageCommand): Promise<unknown> { return clone(await this.inner.path(clone(command))); }
7
- async branches(command: ForkBranchPageCommand): Promise<unknown> { return clone(await this.inner.branches(clone(command))); }
8
- async continuations(command: ForkContinuationPageCommand): Promise<unknown> { return clone(await this.inner.continuations(clone(command))); }
9
- async deleteContinuation(command: ForkDeleteContinuationCommand): Promise<unknown> { return clone(await this.inner.deleteContinuation(clone(command))); }
10
- async prune(command: ForkPruneCommand): Promise<unknown> { return clone(await this.inner.prune(clone(command))); }
11
- async navigate(command: ForkNavigationCommand): Promise<unknown> { return clone(await this.inner.navigate(clone(command))); }
12
- async listen(listener: (event: unknown) => void): Promise<ForkHistoryUnlisten> { if (this.inner.listen === undefined) return () => {}; return this.inner.listen((event) => listener(clone(event))); }
13
- nextPlanId(): string { return this.inner.nextPlanId(); }
13
+ async snapshot(): Promise<unknown> {
14
+ return wireCopy(await this.inner.snapshot());
15
+ }
16
+ async path(command: ForkPathPageCommand): Promise<unknown> {
17
+ return wireCopy(await this.inner.path(wireCopy(command)));
18
+ }
19
+ async branches(command: ForkBranchPageCommand): Promise<unknown> {
20
+ return wireCopy(await this.inner.branches(wireCopy(command)));
21
+ }
22
+ async continuations(command: ForkContinuationPageCommand): Promise<unknown> {
23
+ return wireCopy(await this.inner.continuations(wireCopy(command)));
24
+ }
25
+ async deleteContinuation(command: ForkDeleteContinuationCommand): Promise<unknown> {
26
+ return wireCopy(await this.inner.deleteContinuation(wireCopy(command)));
27
+ }
28
+ async prune(command: ForkPruneCommand): Promise<unknown> {
29
+ return wireCopy(await this.inner.prune(wireCopy(command)));
30
+ }
31
+ async navigate(command: ForkNavigationCommand): Promise<unknown> {
32
+ return wireCopy(await this.inner.navigate(wireCopy(command)));
33
+ }
34
+ async listen(listener: (event: unknown) => void): Promise<ForkHistoryUnlisten> {
35
+ if (this.inner.listen === undefined) {
36
+ return () => {};
37
+ }
38
+ return this.inner.listen((event) => listener(wireCopy(event)));
39
+ }
40
+ nextPlanId(): string {
41
+ return this.inner.nextPlanId();
42
+ }
14
43
  }
15
- function clone<T>(value: T): T { return JSON.parse(JSON.stringify(value)) as T; }