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

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
@@ -0,0 +1,18 @@
1
+ import { ClassKey } from '@astrale-os/kernel-dsl/v1/addressing';
2
+ /**
3
+ * The Class `domain` defines under `key`, or `undefined` when that Class was added after `domain`
4
+ * was built. The Kernel answers in its active Schema, so a result may hold values of such a Class.
5
+ */
6
+ export function definedClass(domain, key) {
7
+ const definition = domain.definition(ClassKey.ref(ClassKey(key)));
8
+ return definition?.ref.kind === 'class' ? definition : undefined;
9
+ }
10
+ /**
11
+ * What in `props` breaks `exact`: a Property it defines holding an invalid value, or a required one
12
+ * absent. A Property `exact` does not define passes: the active Schema may add it.
13
+ */
14
+ export function validateDefinedProperties(exact, props) {
15
+ return exact
16
+ .validateProperties(props)
17
+ .filter((diagnostic) => diagnostic.code !== 'DOMAIN_PROPERTY_UNKNOWN');
18
+ }
@@ -1,3 +1,4 @@
1
1
  export type { DomainBinding, DomainQueryOptions } from './binding.js';
2
+ export { definedClass, validateDefinedProperties } from './conformance.js';
2
3
  export type { BoundEdge, BoundGraphValue, BoundNode, BoundNodeFor } from './model.js';
3
4
  export type { BoundEdgePage, BoundNodePage, BoundQueryPage } from './page.js';
@@ -1 +1 @@
1
- export {};
1
+ export { definedClass, validateDefinedProperties } from './conformance.js';
@@ -1,6 +1,4 @@
1
- import type { Domain } from '@astrale-os/kernel-dsl/v1/domain';
2
1
  import type { QueryProvenance } from '../provenance.js';
3
2
  export declare function retainProjectedProvenance(value: object, provenance: QueryProvenance): void;
4
3
  export declare function provenanceOfProjectedValue(value: object): QueryProvenance | undefined;
5
- export declare function sameClosure(left: Domain['closure'], right: Domain['closure']): boolean;
6
4
  export type { QueryProvenance };
@@ -5,7 +5,3 @@ export function retainProjectedProvenance(value, provenance) {
5
5
  export function provenanceOfProjectedValue(value) {
6
6
  return projectedValues.get(value);
7
7
  }
8
- export function sameClosure(left, right) {
9
- return (left.length === right.length &&
10
- left.every((identity, index) => identity.origin === right[index]?.origin && identity.revision === right[index]?.revision));
11
- }
@@ -1,15 +1,27 @@
1
1
  import type { GraphSpec } from '@astrale-os/kernel-core/graph/graph';
2
2
  import type { MutationAST as MutationASTValue, MutationBuilder, MutationInput, MutationResult as MutationResultValue } from '@astrale-os/kernel-core/graph/mutate';
3
3
  import type { Node } from '@astrale-os/kernel-core/graph/node';
4
- import type { QueryAST as QueryASTValue, QueryEdgeClassInput, QueryResultFor } from '@astrale-os/kernel-core/graph/query';
4
+ import type { QueryAST as QueryASTValue, QueryEdgeClassInput, QueryResultFor, wire } from '@astrale-os/kernel-core/graph/query';
5
5
  import type { NodeTarget } from '@astrale-os/kernel-core/path';
6
6
  import type { DomainDependency } from '@astrale-os/kernel-dsl/v1/schema';
7
7
  import type { Call, Input } from '../client/index.js';
8
8
  import type { RequestOptions } from '../transport/index.js';
9
- import type { QueryPageRequest, QueryResponse } from './page-contract.js';
9
+ import type { QueryAnswerFor, QueryPageRequest, QueryResponse } from './page-contract.js';
10
10
  import type { NeighborsOptions, NodePage } from './page/index.js';
11
11
  export type GraphCall = (call: Call, options?: RequestOptions) => Promise<Input>;
12
+ /**
13
+ * The delivery of one Query: its page sizes and cursor (`paging`, never meaning, W1), the exact
14
+ * Domain revisions it is planned against, and its transport options.
15
+ */
12
16
  export interface GraphQueryOptions extends RequestOptions {
17
+ readonly paging?: wire.Paging;
18
+ readonly expected?: readonly DomainDependency[];
19
+ }
20
+ /**
21
+ * The options of a V6 QueryAST: its page and the exact Domain revisions it is planned against.
22
+ * @deprecated Query V6 is the legacy language: send a Query with `GraphQueryOptions`.
23
+ */
24
+ export interface GraphQueryV6Options extends RequestOptions {
13
25
  readonly expected?: readonly DomainDependency[];
14
26
  readonly page: QueryPageRequest;
15
27
  }
@@ -19,7 +31,9 @@ export interface GraphMutationOptions extends RequestOptions {
19
31
  }
20
32
  export type MutationAuthor = (mutation: MutationBuilder) => undefined;
21
33
  export interface GraphApi {
22
- query<Query extends QueryASTValue>(ast: Query, options: GraphQueryOptions): Promise<QueryResponse<QueryResultFor<Query>>>;
34
+ query<Query extends wire.Document>(query: Query, options?: GraphQueryOptions): Promise<QueryAnswerFor<Query>>;
35
+ /** The legacy V6 path; it stays the last overload, which `Parameters` and `ReturnType` read. */
36
+ query<Query extends QueryASTValue>(ast: Query, options: GraphQueryV6Options): Promise<QueryResponse<QueryResultFor<Query>>>;
23
37
  mutate(author: MutationAuthor, options?: GraphMutationOptions): Promise<MutationResultValue>;
24
38
  mutate(ast: MutationASTValue | MutationInput | GraphSpec, options?: GraphMutationOptions): Promise<MutationResultValue>;
25
39
  get(target: NodeTarget, options?: RequestOptions): Promise<Node | null>;
@@ -6,7 +6,10 @@ import { getNode, requireNode } from './read.js';
6
6
  export function createGraph(call) {
7
7
  if (typeof call !== 'function')
8
8
  throw new TypeError('Graph call capability is invalid.');
9
- const query = (ast, options) => queryGraph(call, ast, options);
9
+ function query(input, options) {
10
+ // The overloads above type the call; `queryGraph` dispatches on the input itself.
11
+ return queryGraph(call, input, options);
12
+ }
10
13
  const mutate = (input, options) => mutateGraph(call, input, options);
11
14
  return Object.freeze({
12
15
  query,
@@ -1,6 +1,6 @@
1
- export { createGraph, type GraphApi, type GraphCall, type GraphMutationOptions, type GraphQueryOptions, type MutationAuthor, } from './graph.js';
2
- export type { QueryPageRequest, QueryResponse } from './page-contract.js';
3
- export { type BoundEdge, type BoundEdgePage, type BoundGraphValue, type BoundNode, type BoundNodeFor, type BoundNodePage, type BoundQueryPage, type DomainBinding, type DomainQueryOptions, } from './domain/index.js';
1
+ export { createGraph, type GraphApi, type GraphCall, type GraphQueryOptions, type GraphMutationOptions, type GraphQueryV6Options, type MutationAuthor, } from './graph.js';
2
+ export type { QueryAnswer, QueryAnswerFor, QueryItem, QueryPage, QueryPageRequest, QueryResponse, } from './page-contract.js';
3
+ export { type BoundEdge, type BoundEdgePage, type BoundGraphValue, type BoundNode, type BoundNodeFor, type BoundNodePage, type BoundQueryPage, type DomainBinding, type DomainQueryOptions, definedClass, validateDefinedProperties, } from './domain/index.js';
4
4
  export { NodeUnavailableError } from './errors.js';
5
5
  export type { NeighborsOptions, NodePage } from './page/index.js';
6
6
  export { LocalAlias, type GraphSpec } from '@astrale-os/kernel-core/graph/graph';
@@ -1,5 +1,5 @@
1
1
  export { createGraph, } from './graph.js';
2
- export {} from './domain/index.js';
2
+ export { definedClass, validateDefinedProperties, } from './domain/index.js';
3
3
  export { NodeUnavailableError } from './errors.js';
4
4
  export { LocalAlias } from '@astrale-os/kernel-core/graph/graph';
5
5
  export { MutationAST, MutationResult, } from '@astrale-os/kernel-core/graph/mutate';
@@ -1,4 +1,7 @@
1
- import type { QueryResult } from '@astrale-os/kernel-core/graph/query';
1
+ import type { EdgeKey, EdgeSlug } from '@astrale-os/kernel-core/graph/edge';
2
+ import type { FactId } from '@astrale-os/kernel-core/graph/formula';
3
+ import type { Answer, QueryResult, Undecided, wire } from '@astrale-os/kernel-core/graph/query';
4
+ import type { ClassKey } from '@astrale-os/kernel-dsl/v1/addressing';
2
5
  /** Caller-selected bounds and continuation for one graph query page. */
3
6
  export interface QueryPageRequest {
4
7
  readonly size: number;
@@ -10,4 +13,66 @@ export interface QueryResponse<Result extends QueryResult = QueryResult> {
10
13
  readonly page: {
11
14
  readonly next?: string;
12
15
  };
16
+ /**
17
+ * Present only for a repair-session read made without conformance admission: its facts may
18
+ * violate the active schema, so it is raw Core data only. The Store keeps it out of its shared
19
+ * cache and a Domain binding refuses to type it.
20
+ */
21
+ readonly certification?: 'uncertified';
13
22
  }
23
+ /**
24
+ * One Page of a Query's answer (page "Fondations Query v7", § Résultats): the exact prefix of its
25
+ * list, and the opaque cursor that continues it when an item follows (W2, W3).
26
+ */
27
+ export type QueryPage = {
28
+ readonly items: readonly QueryItem[];
29
+ readonly undecided?: Undecided;
30
+ } & ({
31
+ readonly complete: true;
32
+ } | {
33
+ readonly complete: false;
34
+ readonly cursor: string;
35
+ });
36
+ /**
37
+ * One delivered item: its fact, its Class when the fact is a Node, the Edge its Branch followed,
38
+ * the ends it renders, its Branches.
39
+ */
40
+ export interface QueryItem {
41
+ readonly fact: FactId;
42
+ readonly class?: ClassKey;
43
+ readonly edge?: EdgeKey;
44
+ readonly source?: QueryItem;
45
+ readonly target?: QueryItem;
46
+ readonly branches: Readonly<Record<string, QueryPage | Exclude<Answer, {
47
+ readonly list: unknown;
48
+ }>>>;
49
+ }
50
+ /**
51
+ * What a Query answers, by its family: a list's Page, the table of the facts it renders as values
52
+ * (an Edge's entry with the slug the Edge stores, when it has one), the revision it was read at and
53
+ * whether its cursor came from another (W6); an `exists`; or a `count`.
54
+ */
55
+ export type QueryAnswer = {
56
+ readonly list: QueryPage;
57
+ readonly facts: readonly (Extract<Answer, {
58
+ readonly list: unknown;
59
+ }>['facts'][number] & {
60
+ readonly slug?: EdgeSlug;
61
+ })[];
62
+ readonly revision: string;
63
+ readonly revised?: true;
64
+ } | Exclude<Answer, {
65
+ readonly list: unknown;
66
+ }>;
67
+ /** The answer of the family a Query names: `list`, `exists` or `count`. */
68
+ export type QueryAnswerFor<Query extends wire.Document> = Query extends {
69
+ readonly list: unknown;
70
+ } ? Extract<QueryAnswer, {
71
+ readonly list: unknown;
72
+ }> : Query extends {
73
+ readonly exists: unknown;
74
+ } ? Extract<QueryAnswer, {
75
+ readonly exists: unknown;
76
+ }> : Extract<QueryAnswer, {
77
+ readonly count: unknown;
78
+ }>;
@@ -1,14 +1,13 @@
1
1
  import type { Edge } from '@astrale-os/kernel-core/graph/edge';
2
2
  import type { Node } from '@astrale-os/kernel-core/graph/node';
3
- import type { DomainDependency } from '@astrale-os/kernel-dsl/v1/schema';
4
3
  import type { QueryResponse } from './page-contract.js';
4
+ /** A certified Query response this client received from the Kernel; `page` identifies it. */
5
5
  export interface QueryProvenance {
6
- readonly closure: readonly DomainDependency[];
7
6
  readonly page: object;
8
7
  }
9
- /** Retain authenticated query provenance without adding serialized response metadata. */
10
- export declare function retainQueryProvenance(response: QueryResponse, closure: readonly DomainDependency[]): QueryResponse;
11
- /** Reattach the exact authenticated closure when Store materialization replaces response objects. */
8
+ /** Retain the provenance of a certified Query response without adding serialized metadata. */
9
+ export declare function retainQueryProvenance(response: QueryResponse): QueryResponse;
10
+ /** Reattach provenance when Store materialization replaces response objects. */
12
11
  export declare function retainDerivedQueryProvenance(source: QueryResponse, derived: QueryResponse): QueryResponse;
13
12
  export declare function provenanceOfGraphValue(value: Node | Edge): QueryProvenance | undefined;
14
13
  export declare function provenanceOfResponse(value: QueryResponse): QueryProvenance | undefined;
@@ -1,8 +1,8 @@
1
1
  const graphValues = new WeakMap();
2
2
  const responses = new WeakMap();
3
- /** Retain authenticated query provenance without adding serialized response metadata. */
4
- export function retainQueryProvenance(response, closure) {
5
- const provenance = Object.freeze({ closure, page: Object.freeze({}) });
3
+ /** Retain the provenance of a certified Query response without adding serialized metadata. */
4
+ export function retainQueryProvenance(response) {
5
+ const provenance = Object.freeze({ page: Object.freeze({}) });
6
6
  responses.set(response, provenance);
7
7
  const { result } = response;
8
8
  if (result.kind === 'graph') {
@@ -29,10 +29,9 @@ export function retainQueryProvenance(response, closure) {
29
29
  }
30
30
  return response;
31
31
  }
32
- /** Reattach the exact authenticated closure when Store materialization replaces response objects. */
32
+ /** Reattach provenance when Store materialization replaces response objects. */
33
33
  export function retainDerivedQueryProvenance(source, derived) {
34
- const provenance = responses.get(source);
35
- return provenance === undefined ? derived : retainQueryProvenance(derived, provenance.closure);
34
+ return responses.has(source) ? retainQueryProvenance(derived) : derived;
36
35
  }
37
36
  export function provenanceOfGraphValue(value) {
38
37
  return graphValues.get(value);
@@ -1,7 +1,13 @@
1
- import type { QueryAST as QueryASTValue, QueryResultFor } from '@astrale-os/kernel-core/graph/query';
1
+ import type { QueryAST as QueryASTValue, QueryResultFor, wire } from '@astrale-os/kernel-core/graph/query';
2
2
  import { Path, type NodeTarget } from '@astrale-os/kernel-core/path';
3
- import type { GraphCall, GraphQueryOptions } from './graph.js';
4
- import type { QueryResponse } from './page-contract.js';
3
+ import type { GraphCall, GraphQueryOptions, GraphQueryV6Options } from './graph.js';
4
+ import type { QueryAnswerFor, QueryResponse } from './page-contract.js';
5
5
  /** Point/neighbor reads are exact targets, unlike Query's overloaded Class sources. */
6
6
  export declare function queryNodePath(target: NodeTarget): Path;
7
- export declare function queryGraph<Query extends QueryASTValue>(call: GraphCall, input: Query, options: GraphQueryOptions): Promise<QueryResponse<QueryResultFor<Query>>>;
7
+ /**
8
+ * Query the graph. A Query names its version in `query` (`"astrale.query/7"`); anything else
9
+ * takes the legacy V6 path, whose codec admits a QueryAST by its `format` and `version` and
10
+ * refuses the rest as it always has.
11
+ */
12
+ export declare function queryGraph<Query extends wire.Document>(call: GraphCall, query: Query, options?: GraphQueryOptions): Promise<QueryAnswerFor<Query>>;
13
+ export declare function queryGraph<Query extends QueryASTValue>(call: GraphCall, ast: Query, options: GraphQueryV6Options): Promise<QueryResponse<QueryResultFor<Query>>>;
@@ -1,6 +1,9 @@
1
- import { QueryAST, QueryASTDecodeError, QueryResult } from '@astrale-os/kernel-core/graph/query';
1
+ import { QUERY, QueryAST, QueryASTDecodeError, QueryResult, } from '@astrale-os/kernel-core/graph/query';
2
2
  import { Path, PathError } from '@astrale-os/kernel-core/path';
3
+ import { queryOutput } from '@astrale-os/kernel-core/schema/kernel/syscalls/contracts';
3
4
  import { query } from '@astrale-os/kernel-core/schema/kernel/syscalls/references';
5
+ import * as portable from '@astrale-os/kernel-dsl/value';
6
+ import { ENVELOPE_LIMITS } from '@astrale-os/kernel-protocol/codecs';
4
7
  import { call as invocation } from '../client/index.js';
5
8
  import { captureExpected } from './expectation.js';
6
9
  import { retainQueryProvenance } from './provenance.js';
@@ -17,7 +20,137 @@ export function queryNodePath(target) {
17
20
  throw new QueryASTDecodeError('/source/nodes/0', 'Invalid Node target', { cause });
18
21
  }
19
22
  }
20
- export async function queryGraph(call, input, options) {
23
+ export function queryGraph(call, input, options) {
24
+ return isQuery(input)
25
+ ? queryV7(call, input, options)
26
+ : queryLegacyAST(call, input, options);
27
+ }
28
+ /** A Query: an object naming `"query": "astrale.query/7"`. */
29
+ function isQuery(input) {
30
+ return isRecord(input) && input.query === QUERY;
31
+ }
32
+ // ─── Query v7 ───────────────────────────────────────────────────────────────────────────────
33
+ /**
34
+ * Send one Query (page "Fondations Query v7", § Wire v7) as the operation's request `{document,
35
+ * paging?, expected?}`, and admit the answer of its family. The Kernel decodes the Query against
36
+ * its Registry; the Client admits its structure only, before any I/O. A continuation is the same
37
+ * Query with `paging.after` set to a Page's cursor.
38
+ */
39
+ async function queryV7(call, input, options) {
40
+ const admitted = admitQuery(input);
41
+ const { expected, paging, ...request } = options ?? {};
42
+ const admittedPaging = admitPaging(paging);
43
+ const capturedExpected = captureExpected(expected);
44
+ const output = await call(invocation(KERNEL_QUERY_PATH, Object.freeze({
45
+ document: admitted,
46
+ ...(admittedPaging === undefined ? {} : { paging: admittedPaging }),
47
+ ...(capturedExpected === undefined ? {} : { expected: capturedExpected }),
48
+ })), options === undefined ? undefined : request);
49
+ return decodeAnswer(output, familyOf(admitted));
50
+ }
51
+ /** A detached, frozen Query of exactly one family; its names are the Kernel's to decode. */
52
+ function admitQuery(input) {
53
+ const query = snapshot(input, 'query');
54
+ if (!isRecord(query) || familyOf(query) === undefined) {
55
+ return invalid('query', 'it holds exactly one of list, exists, count');
56
+ }
57
+ return query;
58
+ }
59
+ /** A detached, frozen paging: sizes per list path, each at least 1, and a Page's cursor. */
60
+ function admitPaging(input) {
61
+ if (input === undefined)
62
+ return undefined;
63
+ const paging = snapshot(input, 'paging');
64
+ if (!isRecord(paging) || !hasOnlyKeys(paging, ['sizes', 'after'])) {
65
+ return invalid('paging', 'it is {sizes?, after?}');
66
+ }
67
+ const { sizes, after } = paging;
68
+ if (sizes !== undefined &&
69
+ (!Array.isArray(sizes) ||
70
+ !sizes.every((entry) => isRecord(entry) &&
71
+ hasExactKeys(entry, ['at', 'size']) &&
72
+ Array.isArray(entry.at) &&
73
+ entry.at.every((name) => typeof name === 'string') &&
74
+ Number.isSafeInteger(entry.size) &&
75
+ entry.size >= 1))) {
76
+ return invalid('paging', 'each size is {at: [Branch names], size: an integer of at least 1}');
77
+ }
78
+ if (after !== undefined && (typeof after !== 'string' || after.length === 0)) {
79
+ return invalid('paging', 'after is the cursor of a Page');
80
+ }
81
+ return paging;
82
+ }
83
+ /** A detached, frozen portable snapshot of one caller value. */
84
+ function snapshot(input, what) {
85
+ try {
86
+ // As deep as an envelope carries it: the Kernel refuses a document past its nesting bound.
87
+ return portable.acceptImmutable(input, { limits: ENVELOPE_LIMITS });
88
+ }
89
+ catch (cause) {
90
+ if (!(cause instanceof portable.BoundaryError))
91
+ throw cause;
92
+ return invalid(what, 'it is not a portable JSON value', cause);
93
+ }
94
+ }
95
+ function invalid(what, detail, cause) {
96
+ const subject = what === 'query' ? 'Graph query' : 'Graph query paging';
97
+ throw new TypeError(`${subject} is invalid: ${detail}.`, { cause });
98
+ }
99
+ /** The family a Query names (`list`, `exists` or `count`); undefined unless it names one. */
100
+ function familyOf(query) {
101
+ const families = ['list', 'exists', 'count'].filter((family) => family in query);
102
+ return families.length === 1 ? families[0] : undefined;
103
+ }
104
+ /**
105
+ * The Kernel's answer, admitted by the query operation's contract as a detached frozen value, of
106
+ * the family the Query asked for.
107
+ */
108
+ function decodeAnswer(input, family) {
109
+ const refuse = (cause) => {
110
+ throw new TypeError('Graph query answer is invalid.', { cause });
111
+ };
112
+ let value;
113
+ try {
114
+ value = portable.acceptImmutable(input);
115
+ }
116
+ catch (cause) {
117
+ if (!(cause instanceof portable.BoundaryError))
118
+ throw cause;
119
+ return refuse(cause);
120
+ }
121
+ if (!isRecord(value) || !(family in value) || !queryOutput.safeParse(value).success) {
122
+ return refuse();
123
+ }
124
+ const answer = value;
125
+ if (!('list' in answer))
126
+ return answer;
127
+ // Only an Edge carries a slug.
128
+ const slugs = answer.facts.every(({ fact, slug }) => slug === undefined || typeof fact !== 'string');
129
+ return slugs && pagesClosed(answer.list) ? answer : refuse();
130
+ }
131
+ /**
132
+ * Every Page, its Branches' included, carries a cursor exactly when an item follows it (W3), and
133
+ * only a Node item carries a Class.
134
+ */
135
+ function pagesClosed(page) {
136
+ const continued = 'cursor' in page;
137
+ return page.complete !== continued && page.items.every(itemClosed);
138
+ }
139
+ function itemClosed(item) {
140
+ return ((!('class' in item) || typeof item.fact === 'string') &&
141
+ (item.source === undefined || itemClosed(item.source)) &&
142
+ (item.target === undefined || itemClosed(item.target)) &&
143
+ Object.values(item.branches).every((branch) => !('items' in branch) || pagesClosed(branch)));
144
+ }
145
+ function isRecord(input) {
146
+ return input !== null && typeof input === 'object' && !Array.isArray(input);
147
+ }
148
+ function hasOnlyKeys(input, allowed) {
149
+ return Object.keys(input).every((key) => allowed.includes(key));
150
+ }
151
+ // ─── Legacy: V6 QueryAST ────────────────────────────────────────────────────────────────────
152
+ /** The legacy path: one V6 QueryAST and its page, answered `{result, page}`. */
153
+ async function queryLegacyAST(call, input, options) {
21
154
  const ast = QueryAST.decode(input);
22
155
  const { expected, page, ...request } = options;
23
156
  const admittedPage = admitPage(page);
@@ -28,9 +161,10 @@ export async function queryGraph(call, input, options) {
28
161
  page: admittedPage,
29
162
  })), request);
30
163
  const response = decodeResponse(output);
31
- return capturedExpected === undefined
32
- ? response
33
- : retainQueryProvenance(response, capturedExpected);
164
+ // A repair-session read may hold facts the Domain refutes: it carries no provenance.
165
+ return response.certification === undefined
166
+ ? retainQueryProvenance(response)
167
+ : response;
34
168
  }
35
169
  function admitPage(input) {
36
170
  if (input === null || typeof input !== 'object' || Array.isArray(input)) {
@@ -52,9 +186,13 @@ function decodeResponse(input) {
52
186
  throw new TypeError('Graph query response is invalid.');
53
187
  }
54
188
  const value = input;
55
- if (!hasExactKeys(value, ['result', 'page'])) {
189
+ const uncertified = value.certification !== undefined;
190
+ if (!hasExactKeys(value, uncertified ? ['result', 'page', 'certification'] : ['result', 'page'])) {
56
191
  throw new TypeError('Graph query response is invalid.');
57
192
  }
193
+ if (uncertified && value.certification !== 'uncertified') {
194
+ throw new TypeError('Graph query response certification is invalid.');
195
+ }
58
196
  const pageInput = value.page;
59
197
  if (pageInput === null || typeof pageInput !== 'object' || Array.isArray(pageInput)) {
60
198
  throw new TypeError('Graph query response page is invalid.');
@@ -69,7 +207,11 @@ function decodeResponse(input) {
69
207
  }
70
208
  const result = QueryResult.decode(value.result);
71
209
  const page = Object.freeze(pageValue.next === undefined ? {} : { next: pageValue.next });
72
- return Object.freeze({ result, page });
210
+ return Object.freeze({
211
+ result,
212
+ page,
213
+ ...(uncertified ? { certification: 'uncertified' } : {}),
214
+ });
73
215
  }
74
216
  function hasExactKeys(input, expected) {
75
217
  const keys = Object.keys(input);
@@ -0,0 +1,79 @@
1
+ import type { QueryAST, QueryResultFor } from '@astrale-os/kernel-core/graph/query';
2
+ import type { GraphApi, GraphQueryV6Options } from './graph.js';
3
+ import type { QueryAnswer, QueryAnswerFor, QueryResponse } from './index.js';
4
+ type Equal<Left, Right> = (<Value>() => Value extends Left ? 1 : 2) extends <Value>() => (Value extends Right ? 1 : 2) ? true : false;
5
+ type Assert<Condition extends true> = Condition;
6
+ type ListAnswer = Extract<QueryAnswer, {
7
+ readonly list: unknown;
8
+ }>;
9
+ type ExistsAnswer = Extract<QueryAnswer, {
10
+ readonly exists: unknown;
11
+ }>;
12
+ type CountAnswer = Extract<QueryAnswer, {
13
+ readonly count: unknown;
14
+ }>;
15
+ declare const ast: QueryAST<import("@astrale-os/kernel-core/graph/query").QueryNodeSelect<{
16
+ kind: "value";
17
+ }>, never>;
18
+ declare const list: {
19
+ readonly query: "astrale.query/7";
20
+ readonly list: {
21
+ readonly nodes: readonly [`${string}:class.${string}`];
22
+ readonly orderBy: readonly [{
23
+ readonly property: `${string}:class.${string}.property.${string}`;
24
+ readonly direction: 'asc';
25
+ readonly missing: 'last';
26
+ }];
27
+ };
28
+ };
29
+ declare const exists: {
30
+ readonly query: "astrale.query/7";
31
+ readonly exists: {
32
+ readonly nodes: readonly [`${string}:class.${string}`];
33
+ };
34
+ };
35
+ declare const count: {
36
+ readonly query: "astrale.query/7";
37
+ readonly count: {
38
+ readonly nodes: readonly [`${string}:class.${string}`];
39
+ };
40
+ readonly upTo: 10;
41
+ };
42
+ declare function overloads(): Promise<readonly [QueryResponse<import("@astrale-os/kernel-core/graph/query").QueryNodeResult<{
43
+ kind: "value";
44
+ }>>, {
45
+ readonly list: import("./page-contract.js").QueryPage;
46
+ readonly facts: readonly (Extract<import("@astrale-os/kernel-core/graph/query").Answer, {
47
+ readonly list: unknown;
48
+ }>['facts'][number] & {
49
+ readonly slug?: import("@astrale-os/kernel-core/graph/edge").EdgeSlug;
50
+ })[];
51
+ readonly revision: string;
52
+ readonly revised?: true;
53
+ }, {
54
+ readonly exists: boolean;
55
+ readonly undecided?: boolean;
56
+ }, {
57
+ readonly count: number;
58
+ readonly more: boolean;
59
+ readonly undecided?: import("@astrale-os/kernel-core/graph/query").Undecided;
60
+ }]>;
61
+ export type pickedByInput = Assert<Equal<Awaited<ReturnType<typeof overloads>>, readonly [QueryResponse<QueryResultFor<typeof ast>>, ListAnswer, ExistsAnswer, CountAnswer]>>;
62
+ export type answerByFamily = Assert<Equal<[
63
+ QueryAnswerFor<typeof list>,
64
+ QueryAnswerFor<typeof exists>,
65
+ QueryAnswerFor<typeof count>
66
+ ], [
67
+ ListAnswer,
68
+ ExistsAnswer,
69
+ CountAnswer
70
+ ]>>;
71
+ /** The legacy overload stays last: `Parameters` and `ReturnType` keep reading V6. */
72
+ export type legacyLast = Assert<Equal<[
73
+ Parameters<GraphApi['query']>,
74
+ Awaited<ReturnType<GraphApi['query']>>
75
+ ], [
76
+ [QueryAST, GraphQueryV6Options],
77
+ QueryResponse<QueryResultFor<QueryAST>>
78
+ ]>>;
79
+ export {};
@@ -0,0 +1,34 @@
1
+ import { Query, QUERY } from '@astrale-os/kernel-core/graph/query';
2
+ const ast = Query.from({ nodes: [task] }).select({ kind: 'nodes', projection: { kind: 'value' } });
3
+ const list = {
4
+ query: QUERY,
5
+ list: { nodes: [Task], orderBy: [{ property: title, direction: 'asc', missing: 'last' }] },
6
+ };
7
+ const exists = { query: QUERY, exists: { nodes: [Task] } };
8
+ const count = { query: QUERY, count: { nodes: [Task] }, upTo: 10 };
9
+ async function overloads() {
10
+ // A V6 QueryAST picks the legacy overload and its result; a Query the answer of its family.
11
+ const legacy = await graph.query(ast, { page: { size: 10 } });
12
+ const listed = await graph.query(list, { paging: { sizes: [{ at: [], size: 10 }] } });
13
+ const decided = await graph.query(exists);
14
+ const counted = await graph.query(count, { expected: [] });
15
+ return [legacy, listed, decided, counted];
16
+ }
17
+ async function refusals() {
18
+ // @ts-expect-error A Query takes its paging in `paging`, not V6's `page`.
19
+ await graph.query(list, { page: { size: 10 } });
20
+ // @ts-expect-error A V6 QueryAST needs its `page`; v7 `paging` is not one.
21
+ await graph.query(ast, { paging: { sizes: [{ at: [], size: 10 }] } });
22
+ // @ts-expect-error A V6 QueryAST has no default page.
23
+ await graph.query(ast);
24
+ // @ts-expect-error A Query names the v7 version.
25
+ await graph.query({ query: 'astrale.query/6', list: { nodes: [Task] } });
26
+ // @ts-expect-error A Query is a list, an exists or a count.
27
+ await graph.query({ query: QUERY, nodes: [Task] });
28
+ // @ts-expect-error A count needs its bound.
29
+ await graph.query({ query: QUERY, count: { nodes: [Task] } });
30
+ // @ts-expect-error An input is a Query or a V6 QueryAST.
31
+ await graph.query({ ...ast, query: QUERY });
32
+ }
33
+ void overloads;
34
+ void refusals;
@@ -1,4 +1,4 @@
1
- export { retry, type RetryOptions } from './retry/index.js';
1
+ export { RETRY_REFUSALS, retry, type RetryOptions } from './retry/index.js';
2
2
  export { createQueue, type QueueOptions } from './queue/index.js';
3
3
  export { createBatcher, type BatchOptions } from './batcher/index.js';
4
4
  export { createRateLimiter, type RateLimitOptions } from './limiter/index.js';
@@ -1,4 +1,4 @@
1
- export { retry } from './retry/index.js';
1
+ export { RETRY_REFUSALS, retry } from './retry/index.js';
2
2
  export { createQueue } from './queue/index.js';
3
3
  export { createBatcher } from './batcher/index.js';
4
4
  export { createRateLimiter } from './limiter/index.js';
@@ -1 +1,2 @@
1
+ export { RETRY_REFUSALS } from './refusals.js';
1
2
  export { retry, type RetryOptions } from './retry.js';
@@ -1 +1,2 @@
1
+ export { RETRY_REFUSALS } from './refusals.js';
1
2
  export { retry } from './retry.js';
@@ -0,0 +1,7 @@
1
+ import type { RetryOptions } from './retry.js';
2
+ /**
3
+ * Opt-in policy for refusals proven to have run nothing. A back-pressure, draining or maintenance
4
+ * refusal is resent no earlier than its prescribed delay; a stale-snapshot conflict is resent after
5
+ * ordinary backoff. Every other failure ends the retry.
6
+ */
7
+ export declare const RETRY_REFUSALS: Readonly<Required<Pick<RetryOptions, 'shouldRetry' | 'retryAfter'>>>;
@@ -0,0 +1,10 @@
1
+ import { isSnapshotStale, retryAfterOf } from '../../errors/index.js';
2
+ /**
3
+ * Opt-in policy for refusals proven to have run nothing. A back-pressure, draining or maintenance
4
+ * refusal is resent no earlier than its prescribed delay; a stale-snapshot conflict is resent after
5
+ * ordinary backoff. Every other failure ends the retry.
6
+ */
7
+ export const RETRY_REFUSALS = Object.freeze({
8
+ shouldRetry: (error) => retryAfterOf(error) !== undefined || isSnapshotStale(error),
9
+ retryAfter: (error) => retryAfterOf(error),
10
+ });
@@ -4,6 +4,8 @@ export interface RetryOptions {
4
4
  readonly maxWait?: number;
5
5
  readonly jitter?: number;
6
6
  readonly shouldRetry?: (error: Error, attempt: number) => boolean;
7
+ /** Least delay in milliseconds a failure prescribes before the next attempt; backoff still applies. */
8
+ readonly retryAfter?: (error: Error, attempt: number) => number | undefined;
7
9
  }
8
10
  /** Retry with bounded exponential backoff and never swallow the final failure. */
9
11
  export declare function retry<Result>(operation: () => Promise<Result>, options?: RetryOptions): Promise<Result>;