@inflatable-cookie/longhorn 0.4.3 → 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,7 +1,4 @@
1
- import type {
2
- ConnectionFailureReporter,
3
- EventTransport,
4
- } from "@inflatable-cookie/longhorn/core";
1
+ import type { ConnectionFailureReporter, EventTransport } from "@inflatable-cookie/longhorn/core";
5
2
 
6
3
  import type {
7
4
  SettingsApplyCommand,
@@ -24,9 +21,8 @@ import {
24
21
  connectSettingsRegistry,
25
22
  connectSettingsScope,
26
23
  SettingsAuthorityConsistencyError,
27
- type SettingsScopeConnectionOptions,
28
- type SettingsSubscription,
29
24
  } from "./connection.ts";
25
+ import type { SettingsScopeConnectionOptions, SettingsSubscription } from "./connection.ts";
30
26
  import {
31
27
  SETTINGS_APPLY_COMMAND,
32
28
  SETTINGS_LOAD_COMMAND,
@@ -42,10 +38,7 @@ export class SettingsClient {
42
38
  }
43
39
 
44
40
  async registry(): Promise<SettingsRegistrySnapshot> {
45
- const value = await this.#transport.invoke(
46
- SETTINGS_REGISTRY_COMMAND,
47
- {},
48
- );
41
+ const value = await this.#transport.invoke(SETTINGS_REGISTRY_COMMAND, {});
49
42
  assertValidSettingsRegistrySnapshot(value);
50
43
  return value;
51
44
  }
@@ -59,10 +52,7 @@ export class SettingsClient {
59
52
  const value = await this.#transport.invoke(SETTINGS_LOAD_COMMAND, {
60
53
  command,
61
54
  });
62
- assertValidSettingsLoadOutcome(
63
- value,
64
- registry.limits.maximumOpaqueValueBytes,
65
- );
55
+ assertValidSettingsLoadOutcome(value, registry.limits.maximumOpaqueValueBytes);
66
56
  return value;
67
57
  }
68
58
 
@@ -70,21 +60,12 @@ export class SettingsClient {
70
60
  registry: SettingsRegistrySnapshot,
71
61
  command: SettingsApplyCommand,
72
62
  ): Promise<SettingsMutationResult> {
73
- assertValidSettingsApplyCommand(
74
- command,
75
- registry.limits.maximumOpaqueValueBytes,
76
- );
77
- assertRegistryGeneration(
78
- registry,
79
- command.authority.registryGeneration,
80
- );
63
+ assertValidSettingsApplyCommand(command, registry.limits.maximumOpaqueValueBytes);
64
+ assertRegistryGeneration(registry, command.authority.registryGeneration);
81
65
  const value = await this.#transport.invoke(SETTINGS_APPLY_COMMAND, {
82
66
  command,
83
67
  });
84
- assertValidSettingsMutationResult(
85
- value,
86
- registry.limits.maximumOpaqueValueBytes,
87
- );
68
+ assertValidSettingsMutationResult(value, registry.limits.maximumOpaqueValueBytes);
88
69
  return value;
89
70
  }
90
71
 
@@ -93,17 +74,11 @@ export class SettingsClient {
93
74
  command: SettingsResetCommand,
94
75
  ): Promise<SettingsMutationResult> {
95
76
  assertValidSettingsResetCommand(command);
96
- assertRegistryGeneration(
97
- registry,
98
- command.authority.registryGeneration,
99
- );
77
+ assertRegistryGeneration(registry, command.authority.registryGeneration);
100
78
  const value = await this.#transport.invoke(SETTINGS_RESET_COMMAND, {
101
79
  command,
102
80
  });
103
- assertValidSettingsMutationResult(
104
- value,
105
- registry.limits.maximumOpaqueValueBytes,
106
- );
81
+ assertValidSettingsMutationResult(value, registry.limits.maximumOpaqueValueBytes);
107
82
  return value;
108
83
  }
109
84
 
@@ -111,12 +86,7 @@ export class SettingsClient {
111
86
  listener?: (snapshot: SettingsRegistrySnapshot) => void,
112
87
  onFailure?: ConnectionFailureReporter,
113
88
  ): SettingsSubscription<SettingsRegistrySnapshot> {
114
- return connectSettingsRegistry(
115
- this.#transport,
116
- () => this.registry(),
117
- listener,
118
- onFailure,
119
- );
89
+ return connectSettingsRegistry(this.#transport, () => this.registry(), listener, onFailure);
120
90
  }
121
91
 
122
92
  connectScope(
@@ -130,10 +100,7 @@ export class SettingsClient {
130
100
  }
131
101
  }
132
102
 
133
- function assertRegistryGeneration(
134
- registry: SettingsRegistrySnapshot,
135
- generation: number,
136
- ): void {
103
+ function assertRegistryGeneration(registry: SettingsRegistrySnapshot, generation: number): void {
137
104
  if (generation !== registry.generation) {
138
105
  throw new SettingsAuthorityConsistencyError(
139
106
  "settings command registry generation does not match its registry",
@@ -1,17 +1,16 @@
1
- import {
2
- CheckedSnapshotConnection,
3
- type ConnectionFailure,
4
- type ConnectionFailureReporter,
5
- type EventTransport,
1
+ import { CheckedSnapshotConnection } from "@inflatable-cookie/longhorn/core";
2
+ import type {
3
+ ConnectionFailure,
4
+ ConnectionFailureReporter,
5
+ EventTransport,
6
6
  } from "@inflatable-cookie/longhorn/core";
7
7
 
8
8
  import type {
9
9
  SettingsLoadCommand,
10
10
  SettingsLoadOutcome,
11
- SettingsRegistryChangedEvent,
11
+ SettingsPageId,
12
12
  SettingsRegistrySnapshot,
13
13
  SettingsRequestId,
14
- SettingsScopeChangedEvent,
15
14
  SettingsScopeId,
16
15
  SettingsScopeSnapshot,
17
16
  } from "./generated/protocol.ts";
@@ -21,20 +20,18 @@ import {
21
20
  assertValidSettingsScopeChangedEvent,
22
21
  assertValidSettingsScopeSnapshot,
23
22
  } from "./validation.ts";
24
- import {
25
- SETTINGS_REGISTRY_CHANGED_EVENT,
26
- SETTINGS_SCOPE_CHANGED_EVENT,
27
- } from "./names.ts";
23
+ import { SETTINGS_REGISTRY_CHANGED_EVENT, SETTINGS_SCOPE_CHANGED_EVENT } from "./names.ts";
28
24
 
29
25
  export interface SettingsSubscription<Snapshot> {
30
26
  readonly ready: Promise<Snapshot>;
31
27
  current(): Snapshot | undefined;
32
- failures(): readonly ConnectionFailure[];
28
+ failures(): ReadonlyArray<ConnectionFailure>;
33
29
  dispose(): Promise<void>;
34
30
  }
35
31
 
36
32
  export interface SettingsScopeConnectionOptions {
37
33
  readonly registry: SettingsRegistrySnapshot;
34
+ readonly pageId: SettingsPageId;
38
35
  readonly scopeId: SettingsScopeId;
39
36
  readonly nextRequestId: () => SettingsRequestId;
40
37
  readonly onSnapshot?: (snapshot: SettingsScopeSnapshot) => void;
@@ -49,16 +46,14 @@ export function connectSettingsRegistry(
49
46
  ): SettingsSubscription<SettingsRegistrySnapshot> {
50
47
  return requiredSubscription(
51
48
  new CheckedSnapshotConnection({
52
- listen: (receive) =>
53
- transport.listen(SETTINGS_REGISTRY_CHANGED_EVENT, receive),
49
+ listen: (receive) => transport.listen(SETTINGS_REGISTRY_CHANGED_EVENT, receive),
54
50
  loadSnapshot: load,
55
51
  validateSnapshot: parseRegistry,
56
52
  handleEvent: registryEventAction,
57
53
  isNewer: isNewerRegistry,
58
54
  onSnapshot: listener,
59
55
  onFailure,
60
- disposedBeforeReadyError: () =>
61
- new SettingsConnectionDisposedError("registry"),
56
+ disposedBeforeReadyError: () => new SettingsConnectionDisposedError("registry"),
62
57
  }),
63
58
  "registry",
64
59
  );
@@ -72,13 +67,13 @@ export function connectSettingsScope(
72
67
  let current: SettingsScopeSnapshot | undefined;
73
68
  const maximum = options.registry.limits.maximumOpaqueValueBytes;
74
69
  const connection = new CheckedSnapshotConnection<SettingsScopeSnapshot>({
75
- listen: (receive) =>
76
- transport.listen(SETTINGS_SCOPE_CHANGED_EVENT, receive),
70
+ listen: (receive) => transport.listen(SETTINGS_SCOPE_CHANGED_EVENT, receive),
77
71
  loadSnapshot: async () => {
78
72
  const outcome = await load({
79
73
  protocolVersion: 1,
80
74
  requestId: options.nextRequestId(),
81
75
  registryGeneration: options.registry.generation,
76
+ pageId: options.pageId,
82
77
  scopeId: options.scopeId,
83
78
  knownAuthority: current?.authority ?? null,
84
79
  });
@@ -92,19 +87,16 @@ export function connectSettingsScope(
92
87
  validateScopeIdentity(value, options);
93
88
  return value;
94
89
  },
95
- handleEvent: (value, snapshot) =>
96
- scopeEventAction(value, snapshot, options),
97
- isNewer: (candidate, snapshot) => {
98
- const accepted = isNewerScope(candidate, snapshot);
99
- if (accepted) {
100
- current = candidate;
101
- }
102
- return accepted;
90
+ handleEvent: (value, snapshot) => scopeEventAction(value, snapshot, options),
91
+ isNewer: isNewerScope,
92
+ // Recorded here, once the connection has accepted the snapshot, so the
93
+ // predicate itself stays a pure comparison.
94
+ onSnapshot: (snapshot) => {
95
+ current = snapshot;
96
+ options.onSnapshot?.(snapshot);
103
97
  },
104
- onSnapshot: options.onSnapshot,
105
98
  onFailure: options.onFailure,
106
- disposedBeforeReadyError: () =>
107
- new SettingsConnectionDisposedError(options.scopeId),
99
+ disposedBeforeReadyError: () => new SettingsConnectionDisposedError(options.scopeId),
108
100
  });
109
101
  return requiredSubscription(connection, options.scopeId);
110
102
  }
@@ -160,9 +152,8 @@ function registryEventAction(
160
152
  current: SettingsRegistrySnapshot | undefined,
161
153
  ): { kind: "ignore" } | { kind: "refresh" } {
162
154
  assertValidSettingsRegistryChangedEvent(value);
163
- const event = value as SettingsRegistryChangedEvent;
164
- return current !== undefined &&
165
- event.registryGeneration <= current.generation
155
+ const event = value;
156
+ return current !== undefined && event.registryGeneration <= current.generation
166
157
  ? { kind: "ignore" }
167
158
  : { kind: "refresh" };
168
159
  }
@@ -173,7 +164,7 @@ function scopeEventAction(
173
164
  options: SettingsScopeConnectionOptions,
174
165
  ): { kind: "ignore" } | { kind: "refresh" } {
175
166
  assertValidSettingsScopeChangedEvent(value);
176
- const event = value as SettingsScopeChangedEvent;
167
+ const event = value;
177
168
  if (event.scopeId !== options.scopeId) {
178
169
  return { kind: "ignore" };
179
170
  }
@@ -193,10 +184,7 @@ function isNewerRegistry(
193
184
  if (current === undefined || candidate.generation > current.generation) {
194
185
  return true;
195
186
  }
196
- if (
197
- candidate.generation === current.generation &&
198
- candidate.digest !== current.digest
199
- ) {
187
+ if (candidate.generation === current.generation && candidate.digest !== current.digest) {
200
188
  throw new SettingsAuthorityConsistencyError(
201
189
  "one settings registry generation produced multiple digests",
202
190
  );
@@ -236,9 +224,7 @@ function validateScopeIdentity(
236
224
  `loaded scope ${snapshot.scopeId}; expected ${options.scopeId}`,
237
225
  );
238
226
  }
239
- if (
240
- snapshot.authority.registryGeneration !== options.registry.generation
241
- ) {
227
+ if (snapshot.authority.registryGeneration !== options.registry.generation) {
242
228
  throw new SettingsAuthorityConsistencyError(
243
229
  "scope snapshot registry generation does not match the connection",
244
230
  );
@@ -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 SETTINGS_FIELDS: Record<string, readonly string[]> = {
6
+ export const SETTINGS_FIELDS = {
7
7
  "SettingsLimits": ["maximumModules", "maximumSections", "maximumPages", "maximumRenderers", "maximumScopes", "maximumApplyUnits", "maximumCapabilities", "maximumAnchorsPerPage", "maximumKeywordsPerPage", "maximumLabelBytes", "maximumKeywordBytes", "maximumOpaqueValueBytes"],
8
8
  "SettingsOpaqueValue": ["codecVersion", "value"],
9
9
  "SettingsModuleDefinition": ["id", "label", "order"],
@@ -25,10 +25,10 @@ export const SETTINGS_FIELDS: Record<string, readonly string[]> = {
25
25
  "SettingsRegistrySnapshot": ["protocolVersion", "generation", "digest", "limits", "composedCapabilities", "modules", "sections", "pages", "renderers", "scopes", "applyUnits", "capabilities"],
26
26
  "SettingsRegistryChangedEvent": ["protocolVersion", "registryGeneration"],
27
27
  "SettingsScopeChangedEvent": ["protocolVersion", "registryGeneration", "scopeId", "scopeRevision"],
28
- "SettingsLoadCommand": ["protocolVersion", "requestId", "registryGeneration", "scopeId", "knownAuthority"],
28
+ "SettingsLoadCommand": ["protocolVersion", "requestId", "registryGeneration", "pageId", "scopeId", "knownAuthority"],
29
29
  "SettingsApplyCommand": ["protocolVersion", "requestId", "pageId", "applyUnitId", "scopeId", "authority", "intent"],
30
30
  "SettingsResetCommand": ["protocolVersion", "requestId", "pageId", "applyUnitId", "scopeId", "authority", "entryIds"],
31
31
  "SettingsMutationReceipt": ["requestId", "pageId", "applyUnitId", "scopeId", "previousAuthority", "committedAuthority", "outcome", "durability", "activationRequirements"],
32
32
  "SettingsConflict": ["expected", "actual"],
33
33
  "SettingsRejection": ["code", "diagnostic"],
34
- };
34
+ } as const;
@@ -487,19 +487,24 @@ export type SettingsLoadCommand = {
487
487
  */
488
488
  protocolVersion: SettingsProtocolVersion,
489
489
  /**
490
- * Idempotency and correlation identity.
490
+ * Correlation identity. Reusing it does not deduplicate commands.
491
491
  */
492
492
  requestId: SettingsRequestId,
493
493
  /**
494
494
  * Registry generation expected by the client.
495
495
  */
496
496
  registryGeneration: SettingsRegistryGeneration,
497
+ /**
498
+ * Admitted page requesting access to the scope.
499
+ */
500
+ pageId: SettingsPageId,
497
501
  /**
498
502
  * Scope to project.
499
503
  */
500
504
  scopeId: SettingsScopeId,
501
505
  /**
502
- * Previously checked authority for conditional reload, if available.
506
+ * Previously checked authority, if available. An advisory hint only.
507
+ * A successful load still returns a complete snapshot.
503
508
  */
504
509
  knownAuthority: SettingsAuthorityExpectation | null, };
505
510
 
@@ -509,7 +514,7 @@ export type SettingsApplyCommand = {
509
514
  */
510
515
  protocolVersion: SettingsProtocolVersion,
511
516
  /**
512
- * Idempotency and correlation identity.
517
+ * Correlation identity. Reusing it does not deduplicate commands.
513
518
  */
514
519
  requestId: SettingsRequestId,
515
520
  /**
@@ -539,7 +544,7 @@ export type SettingsResetCommand = {
539
544
  */
540
545
  protocolVersion: SettingsProtocolVersion,
541
546
  /**
542
- * Idempotency and correlation identity.
547
+ * Correlation identity. Reusing it does not deduplicate commands.
543
548
  */
544
549
  requestId: SettingsRequestId,
545
550
  /**
@@ -2,7 +2,5 @@ export const SETTINGS_REGISTRY_COMMAND = "longhorn_settings_registry";
2
2
  export const SETTINGS_LOAD_COMMAND = "longhorn_settings_load";
3
3
  export const SETTINGS_APPLY_COMMAND = "longhorn_settings_apply";
4
4
  export const SETTINGS_RESET_COMMAND = "longhorn_settings_reset";
5
- export const SETTINGS_REGISTRY_CHANGED_EVENT =
6
- "longhorn://settings/registry-changed";
7
- export const SETTINGS_SCOPE_CHANGED_EVENT =
8
- "longhorn://settings/scope-changed";
5
+ export const SETTINGS_REGISTRY_CHANGED_EVENT = "longhorn://settings/registry-changed";
6
+ export const SETTINGS_SCOPE_CHANGED_EVENT = "longhorn://settings/scope-changed";
@@ -10,18 +10,18 @@ import type {
10
10
 
11
11
  export interface SettingsNavigationModule {
12
12
  readonly module: SettingsModuleDefinition;
13
- readonly sections: readonly SettingsNavigationSection[];
13
+ readonly sections: ReadonlyArray<SettingsNavigationSection>;
14
14
  }
15
15
 
16
16
  export interface SettingsNavigationSection {
17
17
  readonly section: SettingsSectionDefinition;
18
- readonly pages: readonly SettingsPageDefinition[];
18
+ readonly pages: ReadonlyArray<SettingsPageDefinition>;
19
19
  }
20
20
 
21
21
  export interface SettingsRegistryProjection {
22
22
  readonly generation: number;
23
- readonly modules: readonly SettingsNavigationModule[];
24
- readonly pages: readonly SettingsPageDefinition[];
23
+ readonly modules: ReadonlyArray<SettingsNavigationModule>;
24
+ readonly pages: ReadonlyArray<SettingsPageDefinition>;
25
25
  }
26
26
 
27
27
  export interface SettingsDeepLink {
@@ -55,13 +55,13 @@ export class SettingsDeepLinkResolutionError extends Error {
55
55
  export function projectSettingsRegistry(
56
56
  registry: SettingsRegistrySnapshot,
57
57
  ): SettingsRegistryProjection {
58
- const pagesBySection = new Map<string, SettingsPageDefinition[]>();
58
+ const pagesBySection = new Map<string, Array<SettingsPageDefinition>>();
59
59
  for (const page of registry.pages) {
60
60
  const pages = pagesBySection.get(page.sectionId) ?? [];
61
61
  pages.push(page);
62
62
  pagesBySection.set(page.sectionId, pages);
63
63
  }
64
- const sectionsByModule = new Map<string, SettingsNavigationSection[]>();
64
+ const sectionsByModule = new Map<string, Array<SettingsNavigationSection>>();
65
65
  for (const section of registry.sections) {
66
66
  const pages = pagesBySection.get(section.id) ?? [];
67
67
  if (pages.length === 0) {
@@ -82,27 +82,34 @@ export function projectSettingsRegistry(
82
82
  };
83
83
  }
84
84
 
85
+ /**
86
+ * Case folding for search, supplied by the host. This is the TypeScript
87
+ * counterpart of Rust's `HostServices::fold_case`: the host names the locale,
88
+ * and nothing in search guesses one. Used for matching only, never for display.
89
+ */
90
+ export interface SettingsSearchHostServices {
91
+ foldCase(value: string): string;
92
+ }
93
+
85
94
  export function searchSettingsRegistry(
86
95
  registry: SettingsRegistrySnapshot,
87
96
  query: string,
88
- ): readonly SettingsSearchResult[] {
89
- const needle = normalize(query);
97
+ services: SettingsSearchHostServices,
98
+ ): ReadonlyArray<SettingsSearchResult> {
99
+ const needle = services.foldCase(query.trim());
90
100
  if (needle.length === 0) {
91
101
  return [];
92
102
  }
93
- const results: SettingsSearchResult[] = [];
103
+ const results: Array<SettingsSearchResult> = [];
94
104
  for (const page of registry.pages) {
95
105
  const pageMatch = [page.label, ...page.keywords]
96
- .map(normalize)
106
+ .map((value) => services.foldCase(value))
97
107
  .some((value) => value.includes(needle));
98
108
  if (pageMatch) {
99
109
  results.push({ kind: "page", page, anchor: undefined });
100
110
  }
101
111
  for (const anchor of page.anchors) {
102
- if (
103
- anchor.label !== null &&
104
- normalize(anchor.label).includes(needle)
105
- ) {
112
+ if (anchor.label !== null && services.foldCase(anchor.label).includes(needle)) {
106
113
  results.push({ kind: "anchor", page, anchor });
107
114
  }
108
115
  }
@@ -116,17 +123,12 @@ export function resolveSettingsDeepLink(
116
123
  ): ResolvedSettingsDeepLink {
117
124
  const page = registry.pages.find((candidate) => candidate.id === link.pageId);
118
125
  if (page === undefined) {
119
- throw new SettingsDeepLinkResolutionError(
120
- `unknown settings page ${link.pageId}`,
121
- link,
122
- );
126
+ throw new SettingsDeepLinkResolutionError(`unknown settings page ${link.pageId}`, link);
123
127
  }
124
128
  if (link.anchorId === undefined) {
125
129
  return { page, anchor: undefined };
126
130
  }
127
- const anchor = page.anchors.find(
128
- (candidate) => candidate.id === link.anchorId,
129
- );
131
+ const anchor = page.anchors.find((candidate) => candidate.id === link.anchorId);
130
132
  if (anchor === undefined) {
131
133
  throw new SettingsDeepLinkResolutionError(
132
134
  `unknown settings anchor ${link.anchorId} on ${link.pageId}`,
@@ -135,7 +137,3 @@ export function resolveSettingsDeepLink(
135
137
  }
136
138
  return { page, anchor };
137
139
  }
138
-
139
- function normalize(value: string): string {
140
- return value.trim().toLocaleLowerCase("en-US");
141
- }
@@ -6,13 +6,10 @@ import {
6
6
  SETTINGS_HARD_MAXIMUM_TEXT_BYTES,
7
7
  SETTINGS_POLICY_EFFECTS,
8
8
  SETTINGS_RECOVERY_CODES,
9
- type SettingsScopeSnapshot,
10
9
  } from "../generated/protocol.ts";
11
- import {
12
- activation,
13
- policy,
14
- recovery,
15
- } from "./projection.ts";
10
+ import type { SettingsScopeSnapshot } from "../generated/protocol.ts";
11
+ import { incompatible } from "./error.ts";
12
+ import { activation, hasPolicyOverride, recovery } from "./projection.ts";
16
13
  import {
17
14
  array,
18
15
  authority,
@@ -34,20 +31,29 @@ export function assertValidSettingsScopeSnapshot(
34
31
  protocolVersion(snapshot.protocolVersion);
35
32
  identity(snapshot.scopeId);
36
33
  authority(snapshot.authority);
34
+ const entries = new Set<string>();
37
35
  array(snapshot.values).forEach((value) => {
38
36
  const projection = record(value, SETTINGS_FIELDS.SettingsValueProjection);
39
37
  identity(projection.entryId);
38
+ if (entries.has(projection.entryId)) {
39
+ incompatible("invalid_shape", projection.entryId);
40
+ }
41
+ entries.add(projection.entryId);
40
42
  optionalOpaque(projection.configured, maximumOpaqueValueBytes);
41
43
  opaque(projection.effective, maximumOpaqueValueBytes);
42
44
  opaque(projection.compiledDefault, maximumOpaqueValueBytes);
43
45
  known(projection.effectiveSource, SETTINGS_EFFECTIVE_SOURCES);
44
46
  known(projection.editability, SETTINGS_EDITABILITIES);
45
- if (projection.policy !== null) {
46
- policy(
47
- projection.policy,
48
- maximumOpaqueValueBytes,
49
- SETTINGS_POLICY_EFFECTS,
50
- );
47
+ const hasOverride =
48
+ projection.policy !== null &&
49
+ hasPolicyOverride(projection.policy, maximumOpaqueValueBytes, SETTINGS_POLICY_EFFECTS);
50
+ const managed = projection.effectiveSource === "managedPolicy";
51
+ if (
52
+ hasOverride !== managed ||
53
+ (managed && projection.editability === "editable") ||
54
+ (projection.effectiveSource === "userConfiguration" && projection.configured === null)
55
+ ) {
56
+ incompatible("invalid_shape", projection);
51
57
  }
52
58
  array(projection.sourceDiagnostics).forEach((value) => {
53
59
  const diagnostic = record(value, SETTINGS_FIELDS.SettingsSourceDiagnostic);
@@ -56,11 +62,7 @@ export function assertValidSettingsScopeSnapshot(
56
62
  });
57
63
  });
58
64
  if (snapshot.recovery !== null) {
59
- recovery(
60
- snapshot.recovery,
61
- maximumOpaqueValueBytes,
62
- SETTINGS_RECOVERY_CODES,
63
- );
65
+ recovery(snapshot.recovery, maximumOpaqueValueBytes, SETTINGS_RECOVERY_CODES);
64
66
  }
65
67
  activation(snapshot.activationRequirements, SETTINGS_ACTIVATION_STATES);
66
68
  }
@@ -6,13 +6,15 @@ import {
6
6
  SETTINGS_MUTATION_OUTCOMES,
7
7
  SETTINGS_MUTATION_RESULT_STATUSES,
8
8
  SETTINGS_REJECTION_CODES,
9
- type SettingsApplyCommand,
10
- type SettingsLoadCommand,
11
- type SettingsLoadOutcome,
12
- type SettingsMutationResult,
13
- type SettingsRegistryChangedEvent,
14
- type SettingsResetCommand,
15
- type SettingsScopeChangedEvent,
9
+ } from "../generated/protocol.ts";
10
+ import type {
11
+ SettingsApplyCommand,
12
+ SettingsLoadCommand,
13
+ SettingsLoadOutcome,
14
+ SettingsMutationResult,
15
+ SettingsRegistryChangedEvent,
16
+ SettingsResetCommand,
17
+ SettingsScopeChangedEvent,
16
18
  } from "../generated/protocol.ts";
17
19
  import { assertValidSettingsScopeSnapshot } from "./authority.ts";
18
20
  import { activation, rejection } from "./projection.ts";
@@ -37,6 +39,7 @@ export function assertValidSettingsLoadCommand(
37
39
  protocolVersion(command.protocolVersion);
38
40
  identity(command.requestId);
39
41
  unsigned(command.registryGeneration, "invalid_revision");
42
+ identity(command.pageId);
40
43
  identity(command.scopeId);
41
44
  if (command.knownAuthority !== null) {
42
45
  authority(command.knownAuthority);
@@ -66,16 +69,9 @@ export function assertValidSettingsLoadOutcome(
66
69
  known(outcome.status, SETTINGS_LOAD_OUTCOME_STATUSES);
67
70
  record(outcome, variantKeys("SettingsLoadOutcome", outcome));
68
71
  if (outcome.status === "loaded") {
69
- assertValidSettingsScopeSnapshot(
70
- outcome.snapshot,
71
- maximumOpaqueValueBytes,
72
- );
72
+ assertValidSettingsScopeSnapshot(outcome.snapshot, maximumOpaqueValueBytes);
73
73
  } else {
74
- rejection(
75
- outcome.rejection,
76
- maximumOpaqueValueBytes,
77
- SETTINGS_REJECTION_CODES,
78
- );
74
+ rejection(outcome.rejection, maximumOpaqueValueBytes, SETTINGS_REJECTION_CODES);
79
75
  }
80
76
  }
81
77
 
@@ -87,30 +83,17 @@ export function assertValidSettingsMutationResult(
87
83
  known(result.status, SETTINGS_MUTATION_RESULT_STATUSES);
88
84
  record(result, variantKeys("SettingsMutationResult", result));
89
85
  if (result.status === "applied") {
90
- assertValidSettingsScopeSnapshot(
91
- result.snapshot,
92
- maximumOpaqueValueBytes,
93
- );
86
+ assertValidSettingsScopeSnapshot(result.snapshot, maximumOpaqueValueBytes);
94
87
  receipt(result.receipt, maximumOpaqueValueBytes);
95
88
  } else if (result.status === "conflict") {
96
89
  const conflict = record(result.conflict, SETTINGS_FIELDS.SettingsConflict);
97
90
  authority(conflict.expected);
98
91
  authority(conflict.actual);
99
- assertValidSettingsScopeSnapshot(
100
- result.snapshot,
101
- maximumOpaqueValueBytes,
102
- );
92
+ assertValidSettingsScopeSnapshot(result.snapshot, maximumOpaqueValueBytes);
103
93
  } else {
104
- rejection(
105
- result.rejection,
106
- maximumOpaqueValueBytes,
107
- SETTINGS_REJECTION_CODES,
108
- );
94
+ rejection(result.rejection, maximumOpaqueValueBytes, SETTINGS_REJECTION_CODES);
109
95
  if (result.snapshot !== null) {
110
- assertValidSettingsScopeSnapshot(
111
- result.snapshot,
112
- maximumOpaqueValueBytes,
113
- );
96
+ assertValidSettingsScopeSnapshot(result.snapshot, maximumOpaqueValueBytes);
114
97
  }
115
98
  }
116
99
  }
@@ -133,10 +116,7 @@ export function assertValidSettingsScopeChangedEvent(
133
116
  unsigned(event.scopeRevision, "invalid_revision");
134
117
  }
135
118
 
136
- function mutationCommand(
137
- value: unknown,
138
- allowed?: readonly string[],
139
- ): Record<string, unknown> {
119
+ function mutationCommand(value: unknown, allowed?: ReadonlyArray<string>): Record<string, unknown> {
140
120
  const command = record(value, allowed);
141
121
  protocolVersion(command.protocolVersion);
142
122
  identity(command.requestId);
@@ -14,10 +14,7 @@ export class SettingsProtocolValidationError extends Error {
14
14
  readonly code: SettingsProtocolValidationCode;
15
15
  readonly actual: unknown;
16
16
 
17
- constructor(
18
- code: SettingsProtocolValidationCode,
19
- actual: unknown,
20
- ) {
17
+ constructor(code: SettingsProtocolValidationCode, actual: unknown) {
21
18
  super(`incompatible settings protocol: ${code}`);
22
19
  this.name = "SettingsProtocolValidationError";
23
20
  this.code = code;
@@ -25,9 +22,6 @@ export class SettingsProtocolValidationError extends Error {
25
22
  }
26
23
  }
27
24
 
28
- export function incompatible(
29
- code: SettingsProtocolValidationCode,
30
- actual: unknown,
31
- ): never {
25
+ export function incompatible(code: SettingsProtocolValidationCode, actual: unknown): never {
32
26
  throw new SettingsProtocolValidationError(code, actual);
33
27
  }