@astrale-os/kernel-client 1.0.0-beta.4 → 1.0.0-beta.40

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 (194) hide show
  1. package/dist/auth/index.d.ts +1 -0
  2. package/dist/client/admission.js +3 -1
  3. package/dist/client/client.d.ts +19 -2
  4. package/dist/client/client.js +62 -30
  5. package/dist/client/index.d.ts +3 -3
  6. package/dist/client/index.js +2 -2
  7. package/dist/client/request.d.ts +8 -2
  8. package/dist/client/request.js +28 -7
  9. package/dist/client/result.d.ts +10 -1
  10. package/dist/client/result.js +10 -4
  11. package/dist/client/routing-control.d.ts +11 -15
  12. package/dist/client/routing-control.js +21 -29
  13. package/dist/errors/index.d.ts +3 -0
  14. package/dist/errors/index.js +3 -0
  15. package/dist/errors/query-input.d.ts +47 -0
  16. package/dist/errors/query-input.js +85 -0
  17. package/dist/errors/refusal.d.ts +24 -0
  18. package/dist/errors/refusal.js +78 -0
  19. package/dist/errors/response-too-large-error.d.ts +11 -0
  20. package/dist/errors/response-too-large-error.js +15 -0
  21. package/dist/errors/stale-route-error.d.ts +1 -1
  22. package/dist/errors/stale-route-error.js +1 -1
  23. package/dist/{schema → execution}/admission.d.ts +2 -2
  24. package/dist/{schema → execution}/admission.js +3 -3
  25. package/dist/execution/contracts.d.ts +76 -0
  26. package/dist/execution/contracts.js +1 -0
  27. package/dist/execution/deadline.d.ts +18 -0
  28. package/dist/execution/deadline.js +25 -0
  29. package/dist/execution/execution.d.ts +61 -0
  30. package/dist/execution/execution.js +132 -0
  31. package/dist/execution/index.d.ts +4 -0
  32. package/dist/execution/index.js +3 -0
  33. package/dist/graph/domain/binding.d.ts +2 -2
  34. package/dist/graph/domain/binding.js +24 -17
  35. package/dist/graph/domain/conformance.d.ts +12 -0
  36. package/dist/graph/domain/conformance.js +18 -0
  37. package/dist/graph/domain/index.d.ts +1 -0
  38. package/dist/graph/domain/index.js +1 -1
  39. package/dist/graph/domain/provenance.d.ts +0 -2
  40. package/dist/graph/domain/provenance.js +0 -4
  41. package/dist/graph/graph.d.ts +17 -3
  42. package/dist/graph/graph.js +4 -1
  43. package/dist/graph/index.d.ts +3 -3
  44. package/dist/graph/index.js +1 -1
  45. package/dist/graph/page-contract.d.ts +66 -1
  46. package/dist/graph/provenance.d.ts +4 -5
  47. package/dist/graph/provenance.js +5 -6
  48. package/dist/graph/query.d.ts +10 -4
  49. package/dist/graph/query.js +149 -7
  50. package/dist/graph/query.typecheck.d.ts +79 -0
  51. package/dist/graph/query.typecheck.js +34 -0
  52. package/dist/pacer/index.d.ts +1 -1
  53. package/dist/pacer/index.js +1 -1
  54. package/dist/pacer/retry/index.d.ts +1 -0
  55. package/dist/pacer/retry/index.js +1 -0
  56. package/dist/pacer/retry/refusals.d.ts +7 -0
  57. package/dist/pacer/retry/refusals.js +10 -0
  58. package/dist/pacer/retry/retry.d.ts +2 -0
  59. package/dist/pacer/retry/retry.js +33 -1
  60. package/dist/schema/contracts.d.ts +77 -9
  61. package/dist/schema/index.d.ts +3 -2
  62. package/dist/schema/index.js +1 -1
  63. package/dist/schema/installation.d.ts +76 -0
  64. package/dist/schema/installation.js +82 -0
  65. package/dist/schema/schema.d.ts +22 -13
  66. package/dist/schema/schema.js +67 -48
  67. package/dist/session/admission.js +10 -3
  68. package/dist/session/bound.d.ts +12 -3
  69. package/dist/session/bound.js +66 -20
  70. package/dist/session/content/api.d.ts +20 -2
  71. package/dist/session/content/api.js +17 -7
  72. package/dist/session/content/files.d.ts +12 -0
  73. package/dist/session/content/files.js +47 -0
  74. package/dist/session/content/index.d.ts +1 -1
  75. package/dist/session/content/input.d.ts +13 -2
  76. package/dist/session/content/input.js +25 -4
  77. package/dist/session/content/retry.d.ts +5 -1
  78. package/dist/session/content/retry.js +21 -31
  79. package/dist/session/context/index.d.ts +1 -1
  80. package/dist/session/context/index.js +1 -1
  81. package/dist/session/context/operation.d.ts +2 -0
  82. package/dist/session/context/operation.js +5 -0
  83. package/dist/session/credential/credentials.js +43 -28
  84. package/dist/session/discovery/bundle.d.ts +9 -4
  85. package/dist/session/discovery/bundle.js +28 -35
  86. package/dist/session/discovery/discovery.d.ts +1 -1
  87. package/dist/session/discovery/discovery.js +8 -11
  88. package/dist/session/discovery/release.js +18 -28
  89. package/dist/session/discovery/snapshot.d.ts +5 -1
  90. package/dist/session/discovery/transport.d.ts +35 -3
  91. package/dist/session/discovery/transport.js +40 -1
  92. package/dist/session/durable.d.ts +13 -0
  93. package/dist/session/durable.js +39 -0
  94. package/dist/session/exchange/exchange.d.ts +3 -2
  95. package/dist/session/exchange/exchange.js +10 -7
  96. package/dist/session/exchange/index.d.ts +1 -1
  97. package/dist/session/exchange/index.js +1 -1
  98. package/dist/session/exchange/presentation.js +28 -10
  99. package/dist/session/exchange/source.d.ts +7 -7
  100. package/dist/session/exchange/source.js +26 -27
  101. package/dist/session/index.d.ts +5 -4
  102. package/dist/session/index.js +0 -1
  103. package/dist/session/options.d.ts +0 -2
  104. package/dist/session/options.js +0 -3
  105. package/dist/session/reference.d.ts +27 -6
  106. package/dist/session/reference.js +18 -3
  107. package/dist/session/retry.d.ts +12 -0
  108. package/dist/session/retry.js +23 -0
  109. package/dist/session/routing/auth.d.ts +9 -3
  110. package/dist/session/routing/auth.js +4 -3
  111. package/dist/session/routing/cache.d.ts +12 -5
  112. package/dist/session/routing/cache.js +56 -48
  113. package/dist/session/routing/dispatch.d.ts +26 -3
  114. package/dist/session/routing/dispatch.js +112 -67
  115. package/dist/session/routing/envelope.d.ts +3 -3
  116. package/dist/session/routing/index.d.ts +1 -1
  117. package/dist/session/routing/legacy/release-route.d.ts +12 -0
  118. package/dist/session/routing/legacy/release-route.js +6 -0
  119. package/dist/session/routing/recovery.d.ts +3 -6
  120. package/dist/session/routing/recovery.js +3 -14
  121. package/dist/session/routing/request.d.ts +6 -1
  122. package/dist/session/routing/request.js +13 -2
  123. package/dist/session/routing/result.d.ts +1 -1
  124. package/dist/session/routing/result.js +5 -1
  125. package/dist/session/routing/store.js +4 -5
  126. package/dist/session/session.d.ts +31 -5
  127. package/dist/session/session.js +67 -20
  128. package/dist/session/session.typecheck.js +20 -6
  129. package/dist/session/views/index.d.ts +2 -2
  130. package/dist/session/views/index.js +2 -2
  131. package/dist/session/views/kernel.d.ts +31 -0
  132. package/dist/session/views/kernel.js +30 -0
  133. package/dist/session/views/legacy/catalog.d.ts +21 -0
  134. package/dist/session/views/{catalog.js → legacy/catalog.js} +7 -31
  135. package/dist/session/views/legacy/index.d.ts +14 -0
  136. package/dist/session/views/legacy/index.js +14 -0
  137. package/dist/session/views/legacy/resolve.d.ts +46 -0
  138. package/dist/session/views/legacy/resolve.js +83 -0
  139. package/dist/session/views/legacy/unplaced.d.ts +22 -0
  140. package/dist/session/views/legacy/unplaced.js +26 -0
  141. package/dist/session/views/read.d.ts +49 -0
  142. package/dist/session/views/read.js +148 -0
  143. package/dist/session/views/view.d.ts +48 -0
  144. package/dist/session/views/view.js +74 -0
  145. package/dist/store/memory.d.ts +1 -3
  146. package/dist/store/memory.js +73 -105
  147. package/dist/store/shape.d.ts +33 -12
  148. package/dist/store/shape.js +125 -16
  149. package/dist/store/store.d.ts +6 -6
  150. package/dist/store/store.js +1 -1
  151. package/dist/transport/auto/decision.d.ts +26 -14
  152. package/dist/transport/auto/decision.js +35 -32
  153. package/dist/transport/auto/fallback.d.ts +1 -1
  154. package/dist/transport/auto/fallback.js +5 -10
  155. package/dist/transport/auto/transport.d.ts +0 -2
  156. package/dist/transport/auto/transport.js +24 -32
  157. package/dist/transport/deadline.d.ts +2 -0
  158. package/dist/transport/deadline.js +18 -0
  159. package/dist/transport/failure.d.ts +28 -1
  160. package/dist/transport/failure.js +63 -1
  161. package/dist/transport/http/admission.js +7 -34
  162. package/dist/transport/http/binary-request.d.ts +44 -0
  163. package/dist/transport/http/binary-request.js +110 -0
  164. package/dist/transport/http/binary.js +3 -3
  165. package/dist/transport/http/byte-stream.d.ts +2 -1
  166. package/dist/transport/http/byte-stream.js +12 -8
  167. package/dist/transport/http/index.d.ts +1 -1
  168. package/dist/transport/http/index.js +1 -1
  169. package/dist/transport/http/response.d.ts +3 -1
  170. package/dist/transport/http/response.js +7 -9
  171. package/dist/transport/http/stream.d.ts +8 -13
  172. package/dist/transport/http/stream.js +34 -81
  173. package/dist/transport/http/transport.d.ts +10 -0
  174. package/dist/transport/http/transport.js +87 -32
  175. package/dist/transport/http/unprocessed.d.ts +5 -0
  176. package/dist/transport/http/unprocessed.js +28 -0
  177. package/dist/transport/index.d.ts +1 -1
  178. package/dist/transport/index.js +1 -1
  179. package/dist/transport/stream.d.ts +52 -0
  180. package/dist/transport/stream.js +101 -0
  181. package/dist/transport/transport.d.ts +10 -0
  182. package/dist/transport/unary.d.ts +8 -0
  183. package/dist/transport/unary.js +34 -0
  184. package/dist/transport/websocket/connection.d.ts +7 -3
  185. package/dist/transport/websocket/connection.js +103 -92
  186. package/dist/transport/websocket/exchange.d.ts +12 -19
  187. package/dist/transport/websocket/exchange.js +86 -184
  188. package/dist/transport/websocket/transport.js +11 -19
  189. package/package.json +9 -7
  190. package/dist/session/execution.d.ts +0 -65
  191. package/dist/session/execution.js +0 -34
  192. package/dist/session/views/catalog.d.ts +0 -19
  193. package/dist/session/views/resolve.d.ts +0 -13
  194. package/dist/session/views/resolve.js +0 -21
@@ -4,6 +4,10 @@ import { retainDerivedQueryProvenance } from '../graph/provenance.js';
4
4
  import { EntityIndex } from './entity-index.js';
5
5
  import { OptimisticOverlay } from './optimistic.js';
6
6
  import { COLD_SNAPSHOT, QueryShape } from './shape.js';
7
+ const NO_REFERENCES = Object.freeze({
8
+ nodes: Object.freeze([]),
9
+ edges: Object.freeze([]),
10
+ });
7
11
  export class GraphMemory {
8
12
  options;
9
13
  entities = new EntityIndex();
@@ -18,7 +22,7 @@ export class GraphMemory {
18
22
  const existing = this.shapes.get(key);
19
23
  const now = this.options.now();
20
24
  if (existing !== undefined) {
21
- this.touch(existing, now);
25
+ existing.touch(now);
22
26
  return existing;
23
27
  }
24
28
  this.releaseExpired(now);
@@ -27,76 +31,83 @@ export class GraphMemory {
27
31
  if (this.shapes.size >= this.options.maximumShapes) {
28
32
  throw new ClientError('GraphStore shape capacity is exhausted by observed or in-flight queries.');
29
33
  }
30
- const shape = new QueryShape(key, ast, page, expected, now);
31
- shape.releasedAt = now;
34
+ const shape = new QueryShape(key, ast, page, expected, now, (error) => this.report(error));
32
35
  this.shapes.set(key, shape);
33
36
  return shape;
34
37
  }
35
38
  get(key) {
36
39
  const shape = this.shapes.get(key);
37
40
  if (shape !== undefined)
38
- this.touch(shape, this.options.now());
41
+ shape.touch(this.options.now());
39
42
  return shape;
40
43
  }
41
44
  async read(shape, options) {
42
45
  this.options.requireOpen();
43
46
  requireReadLive(options.signal, this.options.lifecycle);
44
- const reusable = shape.response !== undefined &&
47
+ const prior = shape.answer;
48
+ const reusable = prior !== undefined &&
45
49
  shape.state === 'ready' &&
46
- shape.refreshedAt !== undefined &&
47
- this.options.now() - shape.refreshedAt < this.options.ttlMs;
50
+ this.options.now() - prior.refreshedAt < this.options.ttlMs;
48
51
  if (options.freshness !== 'refresh' && reusable)
49
- return shape.response;
52
+ return prior.response;
50
53
  if (shape.inflight !== undefined) {
51
54
  return observeRead(shape.inflight, options, this.options.lifecycle);
52
55
  }
53
- const prior = shape.response;
54
56
  const invalidation = shape.invalidation;
55
- this.publish(shape, prior === undefined ? 'loading' : this.overlay.hasAny() ? 'optimistic' : 'refreshing');
57
+ shape.publish(prior === undefined ? 'loading' : this.overlay.hasAny() ? 'optimistic' : 'refreshing');
56
58
  const inflight = this.schedule(async () => {
57
59
  const result = await this.options.graph.query(shape.ast, queryOptions(shape.page, this.options.lifecycle, shape.expected));
58
60
  this.options.requireOpen();
59
61
  if (shape.invalidation !== invalidation) {
60
- this.publish(shape, prior === undefined ? 'cold' : this.overlay.hasAny() ? 'optimistic' : 'stale');
62
+ shape.publish(prior === undefined ? 'cold' : this.overlay.hasAny() ? 'optimistic' : 'stale');
61
63
  return result;
62
64
  }
65
+ // An uncertified read may hold facts the active schema refutes: it stays raw on its own
66
+ // shape and never enters, or is derived from, the shared entity cache.
67
+ if (result.certification === 'uncertified') {
68
+ const answer = {
69
+ confirmed: result,
70
+ references: NO_REFERENCES,
71
+ response: result,
72
+ refreshedAt: this.options.now(),
73
+ };
74
+ shape.settle(answer, 'ready');
75
+ return answer.response;
76
+ }
63
77
  const integration = this.entities.integrate(result.result);
64
- shape.references = integration.references;
65
- shape.confirmed = result;
66
- shape.response = this.materialize(result, integration.references);
67
- shape.refreshedAt = this.options.now();
68
- this.touch(shape, shape.refreshedAt);
69
- this.publish(shape, this.overlay.hasAny() ? 'optimistic' : 'ready');
78
+ const answer = {
79
+ confirmed: result,
80
+ references: integration.references,
81
+ response: this.materialize(result, integration.references),
82
+ refreshedAt: this.options.now(),
83
+ };
84
+ shape.settle(answer, this.overlay.hasAny() ? 'optimistic' : 'ready');
70
85
  for (const other of this.shapes.values()) {
71
- if (other === shape || other.response === undefined)
86
+ if (other === shape)
72
87
  continue;
73
88
  if (other.referencesAny(integration.changedNodes, integration.changedEdges)) {
74
- other.response = this.materialize(other.confirmed, other.references);
75
- this.publish(other, other.state);
89
+ this.rematerialize(other, other.state);
76
90
  }
77
91
  }
78
- return shape.response;
92
+ return answer.response;
79
93
  })
80
94
  .catch((error) => {
81
95
  if (prior === undefined)
82
- this.publish(shape, 'failed', error);
83
- else {
84
- shape.response = prior;
85
- this.publish(shape, this.overlay.hasAny() ? 'optimistic' : 'stale');
86
- }
96
+ shape.publish('failed', error);
97
+ else
98
+ shape.show(prior, this.overlay.hasAny() ? 'optimistic' : 'stale');
87
99
  throw error;
88
100
  })
89
101
  .finally(() => {
90
- if (shape.inflight !== inflight)
102
+ if (!shape.finish(inflight))
91
103
  return;
92
- shape.inflight = undefined;
93
104
  if (shape.invalidation !== invalidation &&
94
105
  shape.observed &&
95
106
  !this.options.lifecycle.aborted) {
96
107
  void this.read(shape, { page: shape.page }).catch((error) => this.report(error));
97
108
  }
98
109
  });
99
- shape.inflight = inflight;
110
+ shape.begin(inflight);
100
111
  return observeRead(inflight, options, this.options.lifecycle);
101
112
  }
102
113
  snapshot(shape) {
@@ -104,22 +115,20 @@ export class GraphMemory {
104
115
  }
105
116
  subscribe(shape, listener) {
106
117
  this.options.requireOpen();
107
- shape.listeners.add(listener);
108
- shape.accessedAt = this.options.now();
109
- shape.releasedAt = undefined;
118
+ shape.listen(listener, this.options.now());
110
119
  return () => {
111
- shape.listeners.delete(listener);
120
+ shape.unlisten(listener);
112
121
  if (!shape.observed)
113
- shape.releasedAt = this.options.now();
122
+ shape.release(this.options.now());
114
123
  };
115
124
  }
116
125
  invalidate(shape) {
117
126
  this.options.requireOpen();
118
127
  for (const candidate of shape === undefined ? this.shapes.values() : [shape]) {
119
- candidate.invalidation += 1;
120
- if (candidate.response === undefined)
128
+ candidate.invalidate();
129
+ if (candidate.answer === undefined)
121
130
  continue;
122
- this.publish(candidate, this.overlay.hasAny() ? 'optimistic' : 'stale');
131
+ candidate.publish(this.overlay.hasAny() ? 'optimistic' : 'stale');
123
132
  if (candidate.observed && candidate.inflight === undefined) {
124
133
  void this.read(candidate, { page: candidate.page }).catch((error) => this.report(error));
125
134
  }
@@ -127,27 +136,31 @@ export class GraphMemory {
127
136
  }
128
137
  optimistic() {
129
138
  for (const shape of this.shapes.values()) {
130
- if (shape.response === undefined)
139
+ if (shape.answer?.confirmed.certification === 'uncertified')
131
140
  continue;
132
- shape.response = this.materialize(shape.confirmed, shape.references);
133
- this.publish(shape, 'optimistic');
141
+ this.rematerialize(shape, 'optimistic');
134
142
  }
135
143
  }
136
144
  settleOptimistic() {
137
145
  for (const shape of this.shapes.values()) {
138
- if (shape.response === undefined)
139
- continue;
140
- shape.response = this.materialize(shape.confirmed, shape.references);
141
- this.publish(shape, this.overlay.hasAny() ? 'optimistic' : 'stale');
146
+ this.rematerialize(shape, this.overlay.hasAny() ? 'optimistic' : 'stale');
142
147
  }
143
148
  }
144
149
  sweep() {
145
150
  this.options.requireOpen();
146
151
  this.releaseExpired(this.options.now());
147
152
  }
153
+ rematerialize(shape, state) {
154
+ const answer = shape.answer;
155
+ if (answer === undefined)
156
+ return;
157
+ const response = this.materialize(answer.confirmed, answer.references);
158
+ shape.show({ ...answer, response }, state);
159
+ }
148
160
  releaseExpired(now) {
149
161
  for (const [key, shape] of this.shapes) {
150
- if (this.releasable(shape) && now - shape.releasedAt >= this.options.ttlMs) {
162
+ const idleSince = shape.idleSince();
163
+ if (idleSince !== undefined && now - idleSince >= this.options.ttlMs) {
151
164
  this.shapes.delete(key);
152
165
  }
153
166
  }
@@ -156,7 +169,7 @@ export class GraphMemory {
156
169
  evictLeastRecentlyUsed() {
157
170
  let oldest;
158
171
  for (const shape of this.shapes.values()) {
159
- if (!this.releasable(shape))
172
+ if (shape.idleSince() === undefined)
160
173
  continue;
161
174
  if (oldest === undefined || shape.accessedAt < oldest.accessedAt)
162
175
  oldest = shape;
@@ -166,28 +179,18 @@ export class GraphMemory {
166
179
  this.sweepEntities();
167
180
  }
168
181
  }
169
- releasable(shape) {
170
- return !shape.observed && shape.inflight === undefined && shape.releasedAt !== undefined;
171
- }
172
- touch(shape, now) {
173
- shape.accessedAt = now;
174
- if (!shape.observed)
175
- shape.releasedAt = now;
176
- }
177
182
  sweepEntities() {
178
183
  const nodes = new Set();
179
184
  const edges = new Set();
180
185
  for (const shape of this.shapes.values()) {
181
- shape.references?.nodes.forEach((id) => nodes.add(id));
182
- shape.references?.edges.forEach((key) => edges.add(key));
186
+ shape.answer?.references.nodes.forEach((id) => nodes.add(id));
187
+ shape.answer?.references.edges.forEach((key) => edges.add(key));
183
188
  }
184
189
  this.entities.sweep(nodes, edges);
185
190
  }
186
191
  close(error) {
187
- for (const shape of this.shapes.values()) {
188
- shape.listeners.clear();
189
- shape.inflight?.catch(() => undefined);
190
- }
192
+ for (const shape of this.shapes.values())
193
+ shape.close();
191
194
  this.shapes.clear();
192
195
  this.entities.clear();
193
196
  this.overlay.clear();
@@ -195,9 +198,13 @@ export class GraphMemory {
195
198
  this.pending.shift()?.reject(error);
196
199
  }
197
200
  materialize(template, references) {
201
+ // Optimistic overlays and cache refreshes never rewrite an uncertified read.
202
+ if (template.certification === 'uncertified')
203
+ return template;
198
204
  const response = Object.freeze({
199
205
  result: this.materializeResult(template.result, references),
200
206
  page: template.page,
207
+ ...(template.certification === undefined ? {} : { certification: template.certification }),
201
208
  });
202
209
  return retainDerivedQueryProvenance(template, response);
203
210
  }
@@ -270,21 +277,6 @@ export class GraphMemory {
270
277
  ? Object.freeze({ kind: 'graph', graph, selection })
271
278
  : Object.freeze({ kind: 'graph', graph, selection });
272
279
  }
273
- publish(shape, state, error) {
274
- const next = snapshotFor(state, shape.response, error);
275
- if (sameSnapshot(shape.snapshot, next))
276
- return;
277
- shape.state = state;
278
- shape.snapshot = next;
279
- for (const listener of Array.from(shape.listeners)) {
280
- try {
281
- listener();
282
- }
283
- catch (error) {
284
- this.report(error);
285
- }
286
- }
287
- }
288
280
  schedule(task) {
289
281
  return new Promise((resolve, reject) => {
290
282
  const run = () => {
@@ -301,8 +293,11 @@ export class GraphMemory {
301
293
  });
302
294
  }
303
295
  drain() {
304
- while (this.active < this.options.concurrency && this.pending.length > 0) {
305
- this.pending.shift().run();
296
+ while (this.active < this.options.concurrency) {
297
+ const task = this.pending.shift();
298
+ if (task === undefined)
299
+ return;
300
+ task.run();
306
301
  }
307
302
  }
308
303
  report(error) {
@@ -349,7 +344,7 @@ function observeRead(pending, options, lifecycle) {
349
344
  }
350
345
  return new Promise((resolve, reject) => {
351
346
  let timeout;
352
- const callerCancelled = () => settleReject(cancellation(options.signal.reason));
347
+ const callerCancelled = () => settleReject(cancellation(options.signal?.reason));
353
348
  const storeClosed = () => settleReject(cancellation(lifecycle.reason));
354
349
  let settled = false;
355
350
  const cleanup = () => {
@@ -391,30 +386,3 @@ function cancellation(reason) {
391
386
  ? reason
392
387
  : new ClientError('Store read was cancelled.', { cause: reason });
393
388
  }
394
- function snapshotFor(state, response, error) {
395
- switch (state) {
396
- case 'cold':
397
- return Object.freeze({ state });
398
- case 'loading':
399
- return Object.freeze({ state });
400
- case 'failed':
401
- return Object.freeze({ state, error, ...(response === undefined ? {} : { response }) });
402
- case 'ready':
403
- case 'stale':
404
- case 'refreshing':
405
- case 'optimistic':
406
- if (response === undefined)
407
- throw new TypeError(`Store ${state} snapshot requires a response.`);
408
- return Object.freeze({ state, response });
409
- }
410
- }
411
- function sameSnapshot(left, right) {
412
- if (left.state !== right.state)
413
- return false;
414
- if ('response' in left || 'response' in right) {
415
- if (!('response' in left) || !('response' in right) || left.response !== right.response) {
416
- return false;
417
- }
418
- }
419
- return left.state !== 'failed' || right.state !== 'failed' || left.error === right.error;
420
- }
@@ -5,25 +5,46 @@ import type { QueryPageRequest } from '../graph/index.js';
5
5
  import type { FactReferences } from './entity-index.js';
6
6
  import type { StoreQueryResponse, StoreSnapshot } from './store.js';
7
7
  type ShapeState = StoreSnapshot['state'];
8
+ /** A confirmed server response with the facts it references and the response displayed from it. */
9
+ export interface QueryAnswer {
10
+ readonly confirmed: StoreQueryResponse;
11
+ readonly references: FactReferences;
12
+ readonly response: StoreQueryResponse;
13
+ readonly refreshedAt: number;
14
+ }
15
+ /** One cached query: its answer, snapshot, observers, in-flight read and retention clock. */
8
16
  export declare class QueryShape {
17
+ #private;
9
18
  readonly key: string;
10
19
  readonly ast: QueryAST;
11
20
  readonly page: QueryPageRequest;
12
21
  readonly expected: readonly DomainDependency[] | undefined;
13
- readonly listeners: Set<() => void>;
14
- state: ShapeState;
15
- confirmed?: StoreQueryResponse;
16
- response?: StoreQueryResponse;
17
- references?: FactReferences;
18
- inflight?: Promise<StoreQueryResponse>;
19
- invalidation: number;
20
- releasedAt?: number;
21
- accessedAt: number;
22
- refreshedAt?: number;
23
- snapshot: StoreSnapshot;
24
- constructor(key: string, ast: QueryAST, page: QueryPageRequest, expected: readonly DomainDependency[] | undefined, now: number);
22
+ constructor(key: string, ast: QueryAST, page: QueryPageRequest, expected: readonly DomainDependency[] | undefined, now: number, report: (error: unknown) => void);
23
+ get snapshot(): StoreSnapshot;
24
+ get state(): ShapeState;
25
+ get answer(): QueryAnswer | undefined;
26
+ get inflight(): Promise<StoreQueryResponse> | undefined;
27
+ get invalidation(): number;
28
+ get accessedAt(): number;
25
29
  get observed(): boolean;
30
+ /** When the shape became unobserved and idle, or undefined while it is observed or reading. */
31
+ idleSince(): number | undefined;
26
32
  referencesAny(nodes: ReadonlySet<NodeId>, edges: ReadonlySet<string>): boolean;
33
+ touch(now: number): void;
34
+ listen(listener: () => void, now: number): void;
35
+ /** Removes a listener; the caller releases the shape once it is no longer observed. */
36
+ unlisten(listener: () => void): void;
37
+ release(now: number): void;
38
+ begin(read: Promise<StoreQueryResponse>): void;
39
+ /** Ends `read` if it is still the shape's read and reports whether it was. */
40
+ finish(read: Promise<StoreQueryResponse>): boolean;
41
+ invalidate(): void;
42
+ /** Records a newly confirmed answer, then publishes `state`. */
43
+ settle(answer: QueryAnswer, state: ShapeState): void;
44
+ /** Replaces the displayed answer without a new server confirmation, then publishes `state`. */
45
+ show(answer: QueryAnswer, state: ShapeState): void;
46
+ publish(state: ShapeState, error?: unknown): void;
47
+ close(): void;
27
48
  }
28
49
  export declare const COLD_SNAPSHOT: StoreSnapshot;
29
50
  export {};
@@ -1,32 +1,141 @@
1
+ /** One cached query: its answer, snapshot, observers, in-flight read and retention clock. */
1
2
  export class QueryShape {
2
3
  key;
3
4
  ast;
4
5
  page;
5
6
  expected;
6
- listeners = new Set();
7
- state = 'cold';
8
- confirmed;
9
- response;
10
- references;
11
- inflight;
12
- invalidation = 0;
13
- releasedAt;
14
- accessedAt;
15
- refreshedAt;
16
- snapshot = COLD_SNAPSHOT;
17
- constructor(key, ast, page, expected, now) {
7
+ #listeners = new Set();
8
+ #report;
9
+ #snapshot = COLD_SNAPSHOT;
10
+ #answer;
11
+ #inflight;
12
+ #invalidation = 0;
13
+ #accessedAt;
14
+ #releasedAt;
15
+ constructor(key, ast, page, expected, now, report) {
18
16
  this.key = key;
19
17
  this.ast = ast;
20
18
  this.page = page;
21
19
  this.expected = expected;
22
- this.accessedAt = now;
20
+ this.#accessedAt = now;
21
+ this.#releasedAt = now;
22
+ this.#report = report;
23
+ }
24
+ get snapshot() {
25
+ return this.#snapshot;
26
+ }
27
+ get state() {
28
+ return this.#snapshot.state;
29
+ }
30
+ get answer() {
31
+ return this.#answer;
32
+ }
33
+ get inflight() {
34
+ return this.#inflight;
35
+ }
36
+ get invalidation() {
37
+ return this.#invalidation;
38
+ }
39
+ get accessedAt() {
40
+ return this.#accessedAt;
23
41
  }
24
42
  get observed() {
25
- return this.listeners.size > 0;
43
+ return this.#listeners.size > 0;
44
+ }
45
+ /** When the shape became unobserved and idle, or undefined while it is observed or reading. */
46
+ idleSince() {
47
+ return this.observed || this.#inflight !== undefined ? undefined : this.#releasedAt;
26
48
  }
27
49
  referencesAny(nodes, edges) {
28
- return (this.references?.nodes.some((id) => nodes.has(id)) === true ||
29
- this.references?.edges.some((key) => edges.has(key)) === true);
50
+ const references = this.#answer?.references;
51
+ return (references?.nodes.some((id) => nodes.has(id)) === true ||
52
+ references?.edges.some((key) => edges.has(key)) === true);
53
+ }
54
+ touch(now) {
55
+ this.#accessedAt = now;
56
+ if (!this.observed)
57
+ this.#releasedAt = now;
58
+ }
59
+ listen(listener, now) {
60
+ this.#listeners.add(listener);
61
+ this.#accessedAt = now;
62
+ this.#releasedAt = undefined;
63
+ }
64
+ /** Removes a listener; the caller releases the shape once it is no longer observed. */
65
+ unlisten(listener) {
66
+ this.#listeners.delete(listener);
67
+ }
68
+ release(now) {
69
+ this.#releasedAt = now;
70
+ }
71
+ begin(read) {
72
+ this.#inflight = read;
73
+ }
74
+ /** Ends `read` if it is still the shape's read and reports whether it was. */
75
+ finish(read) {
76
+ if (this.#inflight !== read)
77
+ return false;
78
+ this.#inflight = undefined;
79
+ return true;
80
+ }
81
+ invalidate() {
82
+ this.#invalidation += 1;
83
+ }
84
+ /** Records a newly confirmed answer, then publishes `state`. */
85
+ settle(answer, state) {
86
+ this.#answer = answer;
87
+ this.touch(answer.refreshedAt);
88
+ this.publish(state);
89
+ }
90
+ /** Replaces the displayed answer without a new server confirmation, then publishes `state`. */
91
+ show(answer, state) {
92
+ this.#answer = answer;
93
+ this.publish(state);
94
+ }
95
+ publish(state, error) {
96
+ const next = snapshotFor(state, this.#answer?.response, error);
97
+ if (sameSnapshot(this.#snapshot, next))
98
+ return;
99
+ this.#snapshot = next;
100
+ for (const listener of Array.from(this.#listeners)) {
101
+ try {
102
+ listener();
103
+ }
104
+ catch (error) {
105
+ this.#report(error);
106
+ }
107
+ }
108
+ }
109
+ close() {
110
+ this.#listeners.clear();
111
+ this.#inflight?.catch(() => undefined);
30
112
  }
31
113
  }
32
114
  export const COLD_SNAPSHOT = Object.freeze({ state: 'cold' });
115
+ function snapshotFor(state, response, error) {
116
+ switch (state) {
117
+ case 'cold':
118
+ return Object.freeze({ state });
119
+ case 'loading':
120
+ return Object.freeze({ state });
121
+ case 'failed':
122
+ return Object.freeze({ state, error, ...(response === undefined ? {} : { response }) });
123
+ case 'ready':
124
+ case 'stale':
125
+ case 'refreshing':
126
+ case 'optimistic':
127
+ if (response === undefined)
128
+ throw new TypeError(`Store ${state} snapshot requires a response.`);
129
+ return Object.freeze({ state, response });
130
+ }
131
+ }
132
+ function sameSnapshot(left, right) {
133
+ if (left.state !== right.state)
134
+ return false;
135
+ if ('response' in left || 'response' in right) {
136
+ if (!('response' in left) || !('response' in right) || left.response !== right.response) {
137
+ return false;
138
+ }
139
+ }
140
+ return left.state !== 'failed' || right.state !== 'failed' || left.error === right.error;
141
+ }
@@ -1,10 +1,10 @@
1
1
  import type { MutationAST, MutationInput, MutationResult } from '@astrale-os/kernel-core/graph/mutate';
2
2
  import type { QueryAST, QueryResultFor } from '@astrale-os/kernel-core/graph/query';
3
3
  import type { QueryResponse } from '../graph/index.js';
4
- import type { GraphApi, GraphMutationOptions, GraphQueryOptions, MutationAuthor } from '../graph/index.js';
4
+ import type { GraphApi, GraphMutationOptions, GraphQueryV6Options, MutationAuthor } from '../graph/index.js';
5
5
  export type StoreQuery = QueryAST;
6
6
  export type StoreQueryResponse<Query extends StoreQuery = StoreQuery> = QueryResponse<QueryResultFor<Query>>;
7
- export interface StoreReadOptions extends GraphQueryOptions {
7
+ export interface StoreReadOptions extends GraphQueryV6Options {
8
8
  readonly freshness?: 'cached' | 'refresh';
9
9
  }
10
10
  export interface StoreMutationOptions extends GraphMutationOptions {
@@ -50,15 +50,15 @@ export declare class GraphStore {
50
50
  private closed;
51
51
  constructor(options: StoreOptions);
52
52
  read<Query extends StoreQuery>(query: Query, options: StoreReadOptions): Promise<StoreQueryResponse<Query>>;
53
- peek<Query extends StoreQuery>(query: Query, options: GraphQueryOptions): StoreQueryResponse<Query> | undefined;
53
+ peek<Query extends StoreQuery>(query: Query, options: GraphQueryV6Options): StoreQueryResponse<Query> | undefined;
54
54
  preload<Query extends StoreQuery>(query: Query, options: StoreReadOptions): Promise<void>;
55
- subscribe<Query extends StoreQuery>(query: Query, listener: () => void, options: GraphQueryOptions): () => void;
56
- getSnapshot<Query extends StoreQuery>(query: Query, options: GraphQueryOptions): StoreSnapshot<Query>;
55
+ subscribe<Query extends StoreQuery>(query: Query, listener: () => void, options: GraphQueryV6Options): () => void;
56
+ getSnapshot<Query extends StoreQuery>(query: Query, options: GraphQueryV6Options): StoreSnapshot<Query>;
57
57
  observe<Query extends StoreQuery>(query: Query, listener: (snapshot: StoreSnapshot<Query>) => void, options: StoreReadOptions): () => void;
58
58
  mutate(author: MutationAuthor, options?: StoreMutationOptions): Promise<MutationResult>;
59
59
  mutate(input: MutationAST | MutationInput, options?: StoreMutationOptions): Promise<MutationResult>;
60
60
  invalidate(): void;
61
- invalidate(query: StoreQuery, options: GraphQueryOptions): void;
61
+ invalidate(query: StoreQuery, options: GraphQueryV6Options): void;
62
62
  sweep(): void;
63
63
  close(): void;
64
64
  private admit;
@@ -56,7 +56,7 @@ export class GraphStore {
56
56
  }
57
57
  peek(query, options) {
58
58
  const { key } = this.identity(query, options);
59
- return this.memory.get(key)?.response;
59
+ return this.memory.get(key)?.answer?.response;
60
60
  }
61
61
  async preload(query, options) {
62
62
  await this.read(query, options);
@@ -1,23 +1,35 @@
1
- type AutoSelection = 'websocket' | 'http';
2
- type AutoDecisionTicket = {
3
- readonly kind: 'selected';
4
- readonly selection: AutoSelection;
1
+ export type AutoVia = 'websocket' | 'http';
2
+ /** A state the Auto transport choice can rest in. */
3
+ export type AutoSettled = {
4
+ readonly kind: 'undecided';
5
5
  } | {
6
- readonly kind: 'wait';
7
- readonly selection: Promise<AutoSelection | undefined>;
6
+ readonly kind: 'chosen';
7
+ readonly via: AutoVia;
8
8
  } | {
9
- readonly kind: 'probe';
9
+ readonly kind: 'closed';
10
10
  };
11
+ /** What `begin` grants: the probe itself, or the state the caller must follow. */
12
+ export type AutoTicket = {
13
+ readonly kind: 'probe';
14
+ } | {
15
+ readonly kind: 'probing';
16
+ readonly settled: Promise<AutoSettled>;
17
+ } | Exclude<AutoSettled, {
18
+ kind: 'undecided';
19
+ }>;
11
20
  /** Serializes the one initial transport decision without retaining invocation values. */
12
21
  export declare class AutoDecision {
13
- private selected;
14
- private pending;
15
- private closed;
16
- begin(): AutoDecisionTicket;
17
- select(selection: AutoSelection): void;
22
+ private state;
23
+ get closed(): boolean;
24
+ /** Claim the probe when undecided; otherwise report the state to follow. */
25
+ begin(): AutoTicket;
26
+ /** The probe proved WebSocket usable. */
27
+ selectWebSocket(): void;
18
28
  /** Permanently degrade a prior or pending WebSocket selection to HTTP. */
19
29
  demote(): void;
30
+ /** The probe proved nothing; the next caller probes again. */
20
31
  release(): void;
21
- close(): void;
32
+ /** Returns whether this call closed the decision. */
33
+ close(): boolean;
34
+ private move;
22
35
  }
23
- export {};