@astrale-os/kernel-client 1.0.0-beta.3 → 1.0.0-beta.30

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 (192) 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 +19 -0
  18. package/dist/errors/refusal.js +63 -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 +64 -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 +145 -7
  50. package/dist/graph/query.typecheck.d.ts +77 -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 +10 -2
  71. package/dist/session/content/api.js +13 -4
  72. package/dist/session/content/index.d.ts +1 -1
  73. package/dist/session/content/input.d.ts +6 -1
  74. package/dist/session/content/input.js +12 -4
  75. package/dist/session/content/retry.d.ts +5 -1
  76. package/dist/session/content/retry.js +21 -31
  77. package/dist/session/context/index.d.ts +1 -1
  78. package/dist/session/context/index.js +1 -1
  79. package/dist/session/context/operation.d.ts +2 -0
  80. package/dist/session/context/operation.js +5 -0
  81. package/dist/session/credential/credentials.js +43 -28
  82. package/dist/session/discovery/bundle.d.ts +9 -4
  83. package/dist/session/discovery/bundle.js +28 -35
  84. package/dist/session/discovery/discovery.d.ts +1 -1
  85. package/dist/session/discovery/discovery.js +8 -11
  86. package/dist/session/discovery/release.js +18 -28
  87. package/dist/session/discovery/snapshot.d.ts +5 -1
  88. package/dist/session/discovery/transport.d.ts +35 -3
  89. package/dist/session/discovery/transport.js +40 -1
  90. package/dist/session/durable.d.ts +13 -0
  91. package/dist/session/durable.js +39 -0
  92. package/dist/session/exchange/exchange.d.ts +3 -2
  93. package/dist/session/exchange/exchange.js +10 -7
  94. package/dist/session/exchange/index.d.ts +1 -1
  95. package/dist/session/exchange/index.js +1 -1
  96. package/dist/session/exchange/presentation.js +28 -10
  97. package/dist/session/exchange/source.d.ts +7 -7
  98. package/dist/session/exchange/source.js +26 -27
  99. package/dist/session/index.d.ts +4 -3
  100. package/dist/session/index.js +0 -1
  101. package/dist/session/options.d.ts +0 -2
  102. package/dist/session/options.js +0 -3
  103. package/dist/session/reference.d.ts +9 -5
  104. package/dist/session/reference.js +7 -2
  105. package/dist/session/retry.d.ts +12 -0
  106. package/dist/session/retry.js +23 -0
  107. package/dist/session/routing/auth.d.ts +9 -3
  108. package/dist/session/routing/auth.js +4 -3
  109. package/dist/session/routing/cache.d.ts +12 -5
  110. package/dist/session/routing/cache.js +56 -48
  111. package/dist/session/routing/dispatch.d.ts +26 -3
  112. package/dist/session/routing/dispatch.js +112 -67
  113. package/dist/session/routing/envelope.d.ts +3 -3
  114. package/dist/session/routing/index.d.ts +1 -1
  115. package/dist/session/routing/legacy/release-route.d.ts +12 -0
  116. package/dist/session/routing/legacy/release-route.js +6 -0
  117. package/dist/session/routing/recovery.d.ts +3 -6
  118. package/dist/session/routing/recovery.js +3 -14
  119. package/dist/session/routing/request.d.ts +6 -1
  120. package/dist/session/routing/request.js +13 -2
  121. package/dist/session/routing/result.d.ts +1 -1
  122. package/dist/session/routing/result.js +5 -1
  123. package/dist/session/routing/store.js +4 -5
  124. package/dist/session/session.d.ts +31 -5
  125. package/dist/session/session.js +60 -19
  126. package/dist/session/session.typecheck.js +20 -6
  127. package/dist/session/views/index.d.ts +2 -2
  128. package/dist/session/views/index.js +2 -2
  129. package/dist/session/views/kernel.d.ts +31 -0
  130. package/dist/session/views/kernel.js +30 -0
  131. package/dist/session/views/legacy/catalog.d.ts +21 -0
  132. package/dist/session/views/{catalog.js → legacy/catalog.js} +7 -31
  133. package/dist/session/views/legacy/index.d.ts +14 -0
  134. package/dist/session/views/legacy/index.js +14 -0
  135. package/dist/session/views/legacy/resolve.d.ts +46 -0
  136. package/dist/session/views/legacy/resolve.js +83 -0
  137. package/dist/session/views/legacy/unplaced.d.ts +22 -0
  138. package/dist/session/views/legacy/unplaced.js +26 -0
  139. package/dist/session/views/read.d.ts +49 -0
  140. package/dist/session/views/read.js +148 -0
  141. package/dist/session/views/view.d.ts +48 -0
  142. package/dist/session/views/view.js +74 -0
  143. package/dist/store/memory.d.ts +1 -3
  144. package/dist/store/memory.js +73 -105
  145. package/dist/store/shape.d.ts +33 -12
  146. package/dist/store/shape.js +125 -16
  147. package/dist/store/store.d.ts +6 -6
  148. package/dist/store/store.js +1 -1
  149. package/dist/transport/auto/decision.d.ts +26 -14
  150. package/dist/transport/auto/decision.js +35 -32
  151. package/dist/transport/auto/fallback.d.ts +1 -1
  152. package/dist/transport/auto/fallback.js +5 -10
  153. package/dist/transport/auto/transport.d.ts +0 -2
  154. package/dist/transport/auto/transport.js +24 -32
  155. package/dist/transport/deadline.d.ts +2 -0
  156. package/dist/transport/deadline.js +18 -0
  157. package/dist/transport/failure.d.ts +28 -1
  158. package/dist/transport/failure.js +63 -1
  159. package/dist/transport/http/admission.js +7 -34
  160. package/dist/transport/http/binary-request.d.ts +44 -0
  161. package/dist/transport/http/binary-request.js +110 -0
  162. package/dist/transport/http/binary.js +3 -3
  163. package/dist/transport/http/byte-stream.d.ts +2 -1
  164. package/dist/transport/http/byte-stream.js +12 -8
  165. package/dist/transport/http/index.d.ts +1 -1
  166. package/dist/transport/http/index.js +1 -1
  167. package/dist/transport/http/response.d.ts +3 -1
  168. package/dist/transport/http/response.js +7 -9
  169. package/dist/transport/http/stream.d.ts +8 -13
  170. package/dist/transport/http/stream.js +34 -81
  171. package/dist/transport/http/transport.d.ts +10 -0
  172. package/dist/transport/http/transport.js +87 -32
  173. package/dist/transport/http/unprocessed.d.ts +5 -0
  174. package/dist/transport/http/unprocessed.js +28 -0
  175. package/dist/transport/index.d.ts +1 -1
  176. package/dist/transport/index.js +1 -1
  177. package/dist/transport/stream.d.ts +52 -0
  178. package/dist/transport/stream.js +101 -0
  179. package/dist/transport/transport.d.ts +10 -0
  180. package/dist/transport/unary.d.ts +8 -0
  181. package/dist/transport/unary.js +34 -0
  182. package/dist/transport/websocket/connection.d.ts +7 -3
  183. package/dist/transport/websocket/connection.js +103 -92
  184. package/dist/transport/websocket/exchange.d.ts +12 -19
  185. package/dist/transport/websocket/exchange.js +86 -184
  186. package/dist/transport/websocket/transport.js +11 -19
  187. package/package.json +7 -5
  188. package/dist/session/execution.d.ts +0 -65
  189. package/dist/session/execution.js +0 -34
  190. package/dist/session/views/catalog.d.ts +0 -19
  191. package/dist/session/views/resolve.d.ts +0 -13
  192. package/dist/session/views/resolve.js +0 -21
@@ -0,0 +1,19 @@
1
+ import { ResponseError } from './response-error.js';
2
+ /** Return the delay a refused invocation prescribes before it is sent again, and nothing else. */
3
+ export declare function retryAfterOf(error: unknown): number | undefined;
4
+ /** Whether the Instance refused the invocation because a maintenance session admits only root. */
5
+ export declare function isMaintenance(error: unknown): error is ResponseError;
6
+ /**
7
+ * Whether the Kernel refused a request whose maintenance guard names a session that does not own
8
+ * the Instance: it ended, or another one owns it. Nothing ran; resending it unchanged cannot succeed.
9
+ */
10
+ export declare function isMaintenanceSessionStale(error: unknown): error is ResponseError;
11
+ /** Whether a sealed conflict ran nothing because the graph snapshot advanced; resend it at once. */
12
+ export declare function isSnapshotStale(error: unknown): error is ResponseError;
13
+ /**
14
+ * Whether the Kernel refused a request because the client was built against a Domain meaning this
15
+ * Instance does not serve: the revision a Query or a Mutation expects of a Domain, or the meaning
16
+ * of a callable. Nothing ran; resending it unchanged cannot succeed, so the client is rebuilt or
17
+ * reloaded against what the Instance serves.
18
+ */
19
+ export declare function isExpectationMismatch(error: unknown): error is ResponseError;
@@ -0,0 +1,63 @@
1
+ import * as errors from '@astrale-os/kernel-protocol/errors';
2
+ import { CALLABLE_FINGERPRINT_MISMATCH } from '@astrale-os/kernel-protocol/invocation';
3
+ import { ResponseError } from './response-error.js';
4
+ /**
5
+ * Sealed conflicts whose plan met a checkpoint advanced past its pinned snapshot, an Authority
6
+ * evaluation's included (POLICY_SNAPSHOT_MISMATCH).
7
+ */
8
+ const SNAPSHOT_STALE_REASONS = new Set([
9
+ 'SNAPSHOT_STALE',
10
+ 'QUERY_SNAPSHOT_STALE',
11
+ 'MUTATION_SNAPSHOT_STALE',
12
+ 'POLICY_SNAPSHOT_MISMATCH',
13
+ ]);
14
+ /** Conflicts whose request expects a Domain meaning this Instance does not serve. */
15
+ const EXPECTATION_MISMATCH_REASONS = new Set([
16
+ 'QUERY_EXPECTATION_MISMATCH',
17
+ 'MUTATION_EXPECTATION_MISMATCH',
18
+ CALLABLE_FINGERPRINT_MISMATCH,
19
+ ]);
20
+ /** Return the delay a refused invocation prescribes before it is sent again, and nothing else. */
21
+ export function retryAfterOf(error) {
22
+ if (!(error instanceof ResponseError))
23
+ return undefined;
24
+ return errors.retryAfterOf({
25
+ code: error.code,
26
+ message: error.message,
27
+ ...(error.data === undefined ? {} : { data: error.data }),
28
+ });
29
+ }
30
+ /** Whether the Instance refused the invocation because a maintenance session admits only root. */
31
+ export function isMaintenance(error) {
32
+ return (error instanceof ResponseError &&
33
+ error.reason?.code === errors.RETRY_REASONS.INSTANCE_MAINTENANCE &&
34
+ retryAfterOf(error) !== undefined);
35
+ }
36
+ /**
37
+ * Whether the Kernel refused a request whose maintenance guard names a session that does not own
38
+ * the Instance: it ended, or another one owns it. Nothing ran; resending it unchanged cannot succeed.
39
+ */
40
+ export function isMaintenanceSessionStale(error) {
41
+ return (error instanceof ResponseError &&
42
+ error.code === errors.CODES.CONFLICT &&
43
+ error.reason?.code === 'MAINTENANCE_SESSION_STALE');
44
+ }
45
+ /** Whether a sealed conflict ran nothing because the graph snapshot advanced; resend it at once. */
46
+ export function isSnapshotStale(error) {
47
+ return (error instanceof ResponseError &&
48
+ error.code === errors.CODES.CONFLICT &&
49
+ error.reason !== undefined &&
50
+ SNAPSHOT_STALE_REASONS.has(error.reason.code));
51
+ }
52
+ /**
53
+ * Whether the Kernel refused a request because the client was built against a Domain meaning this
54
+ * Instance does not serve: the revision a Query or a Mutation expects of a Domain, or the meaning
55
+ * of a callable. Nothing ran; resending it unchanged cannot succeed, so the client is rebuilt or
56
+ * reloaded against what the Instance serves.
57
+ */
58
+ export function isExpectationMismatch(error) {
59
+ return (error instanceof ResponseError &&
60
+ error.code === errors.CODES.CONFLICT &&
61
+ error.reason !== undefined &&
62
+ EXPECTATION_MISMATCH_REASONS.has(error.reason.code));
63
+ }
@@ -0,0 +1,11 @@
1
+ import { ProtocolError, type ProtocolErrorOptions } from './protocol-error.js';
2
+ /**
3
+ * The answer to one invocation, a body, a streamed binary or one frame of a stream, is larger than
4
+ * the client's `maximumResponseBytes`, and the client stopped reading it. A WebSocket frame past
5
+ * the bound names no request the client could read, so it ends its connection instead.
6
+ */
7
+ export declare class ResponseTooLargeError extends ProtocolError {
8
+ /** The bound the answer passed, in bytes. */
9
+ readonly maximumResponseBytes: number;
10
+ constructor(message: string, maximumResponseBytes: number, options?: ProtocolErrorOptions);
11
+ }
@@ -0,0 +1,15 @@
1
+ import { ProtocolError } from './protocol-error.js';
2
+ /**
3
+ * The answer to one invocation, a body, a streamed binary or one frame of a stream, is larger than
4
+ * the client's `maximumResponseBytes`, and the client stopped reading it. A WebSocket frame past
5
+ * the bound names no request the client could read, so it ends its connection instead.
6
+ */
7
+ export class ResponseTooLargeError extends ProtocolError {
8
+ /** The bound the answer passed, in bytes. */
9
+ maximumResponseBytes;
10
+ constructor(message, maximumResponseBytes, options) {
11
+ super(message, options);
12
+ this.name = 'ResponseTooLargeError';
13
+ this.maximumResponseBytes = maximumResponseBytes;
14
+ }
15
+ }
@@ -1,6 +1,6 @@
1
1
  import type { invocation as protocol, release } from '@astrale-os/kernel-protocol';
2
2
  import { ClientError } from './client-error.js';
3
- /** Client-visible stale-route error shape; public instances are not routing-control witnesses. */
3
+ /** Client-visible stale-route error the Client raises for a decoded Stale control. */
4
4
  export declare class StaleRouteError extends ClientError {
5
5
  readonly invocation: protocol.InvocationId;
6
6
  readonly name = "StaleRouteError";
@@ -1,5 +1,5 @@
1
1
  import { ClientError } from './client-error.js';
2
- /** Client-visible stale-route error shape; public instances are not routing-control witnesses. */
2
+ /** Client-visible stale-route error the Client raises for a decoded Stale control. */
3
3
  export class StaleRouteError extends ClientError {
4
4
  invocation;
5
5
  name = 'StaleRouteError';
@@ -7,6 +7,6 @@ interface OutputContract {
7
7
  readonly success: false;
8
8
  };
9
9
  }
10
- /** Admit one value result through its Core-owned syscall contract. */
11
- export declare function acceptOutput<T>(contract: OutputContract, input: unknown): T;
10
+ /** Admit one value an Execution Method answered through its Core-owned contract. */
11
+ export declare function acceptOutput<T>(contract: OutputContract, input: unknown, what: string): T;
12
12
  export {};
@@ -1,10 +1,10 @@
1
1
  import * as portable from '@astrale-os/kernel-dsl/value';
2
- /** Admit one value result through its Core-owned syscall contract. */
3
- export function acceptOutput(contract, input) {
2
+ /** Admit one value an Execution Method answered through its Core-owned contract. */
3
+ export function acceptOutput(contract, input, what) {
4
4
  const value = portable.accept(input);
5
5
  const parsed = contract.safeParse(value);
6
6
  if (!parsed.success)
7
- throw new TypeError('Kernel returned an invalid Schema result.');
7
+ throw new TypeError(`Kernel returned an invalid ${what}.`);
8
8
  return freeze(parsed.data);
9
9
  }
10
10
  function freeze(input) {
@@ -0,0 +1,76 @@
1
+ import type { NodeId } from '@astrale-os/kernel-core/graph/node';
2
+ import type { SystemExecutionDetail } from '@astrale-os/kernel-core/schema/kernel/syscalls/contracts';
3
+ import type { Value } from '@astrale-os/kernel-dsl/value';
4
+ declare const EXECUTION_OUTPUT: unique symbol;
5
+ /**
6
+ * Immediate value of a durable call: the `K.Execution` it created or recovered. `Output` is the
7
+ * handler output of the called declaration, carried only by the type.
8
+ */
9
+ export interface ExecutionRef<Output = unknown> {
10
+ readonly id: NodeId;
11
+ /**
12
+ * Present on the reference a durable call answered when its Host says: false when that call
13
+ * created the Execution, true when its idempotency key already named it.
14
+ */
15
+ readonly replayed?: boolean;
16
+ readonly [EXECUTION_OUTPUT]?: Output;
17
+ }
18
+ /** An Execution, by its reference or by its NodeId. */
19
+ export type ExecutionTarget<Output = unknown> = ExecutionRef<Output> | NodeId;
20
+ /** The public failure an Execution ended with, such as `DATA_MIGRATION_REQUIRED`. */
21
+ export interface ExecutionFailure {
22
+ readonly code: string;
23
+ readonly message: string;
24
+ readonly family?: string;
25
+ readonly details?: Value;
26
+ }
27
+ /** A terminal Execution: its completed value, its failure, or its cancellation. */
28
+ export type ExecutionOutcome<Output = unknown> = {
29
+ readonly kind: 'completed';
30
+ readonly value: Output;
31
+ } | {
32
+ readonly kind: 'failed';
33
+ readonly error: ExecutionFailure;
34
+ } | {
35
+ readonly kind: 'cancelled';
36
+ };
37
+ export type ExecutionState = 'pending' | 'running' | 'waiting' | 'paused' | 'completed' | 'failed' | 'cancelled' | 'expired';
38
+ /**
39
+ * The status of an Execution: its node's, or what Root reads of a system execution whose node was
40
+ * deleted.
41
+ */
42
+ export type ExecutionStatus<Output = unknown> = ExecutionNodeStatus<Output> | DeletedExecutionStatus<Output>;
43
+ /** The status of an Execution whose node exists, with the engine state of its run. */
44
+ export interface ExecutionNodeStatus<Output = unknown> {
45
+ readonly id: NodeId;
46
+ readonly function: NodeId;
47
+ readonly version: `sha256:${string}`;
48
+ readonly state: ExecutionState;
49
+ readonly createdAt: string;
50
+ /** A system execution's step, progress and session state, as Core shapes it. */
51
+ readonly detail?: SystemExecutionDetail;
52
+ /** Present exactly once the Execution is terminal. */
53
+ readonly outcome?: ExecutionOutcome<Output>;
54
+ }
55
+ /**
56
+ * What Root alone reads of a system execution whose node was deleted: the outcome its lifecycle
57
+ * record keeps, under the explicit state `deleted`, never a live state.
58
+ */
59
+ export interface DeletedExecutionStatus<Output = unknown> {
60
+ readonly id: NodeId;
61
+ readonly state: 'deleted';
62
+ readonly outcome: ExecutionOutcome<Output>;
63
+ /** Absent: the node that recorded them is deleted. */
64
+ readonly function?: never;
65
+ readonly version?: never;
66
+ readonly createdAt?: never;
67
+ readonly detail?: never;
68
+ }
69
+ /**
70
+ * How long a call waits for the terminal outcome, in total, across as many `Execution.result`
71
+ * requests as it takes. Absent or `0`, the call answers what the Execution holds now.
72
+ */
73
+ export interface ExecutionWait {
74
+ readonly waitMs?: number;
75
+ }
76
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ import type { RequestOptions } from '../transport/index.js';
2
+ /**
3
+ * The time a wait leaves its answer to come back. The Kernel bounds a wait by its own request
4
+ * deadline, never by this call's: the client ends each wait this long before its request times out.
5
+ */
6
+ export declare const ANSWER_MARGIN_MS = 2000;
7
+ /**
8
+ * The absolute deadline of one Client call, whatever number of Kernel requests it sends: its
9
+ * `timeoutMs` from now when given, otherwise none (`Infinity`), each request then taking its default.
10
+ */
11
+ export declare function deadlineOf(options: RequestOptions | undefined): number;
12
+ /**
13
+ * When a wait of `waitMs` from now ends: then, or the answer margin before the call's `deadline`
14
+ * if that comes first, so that its answer comes back in time.
15
+ */
16
+ export declare function waitEnd(deadline: number, waitMs: number): number;
17
+ /** The options of the call's next request: the time left before its deadline, at least 1 ms. */
18
+ export declare function within(deadline: number, options: RequestOptions | undefined): RequestOptions | undefined;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * The time a wait leaves its answer to come back. The Kernel bounds a wait by its own request
3
+ * deadline, never by this call's: the client ends each wait this long before its request times out.
4
+ */
5
+ export const ANSWER_MARGIN_MS = 2_000;
6
+ /**
7
+ * The absolute deadline of one Client call, whatever number of Kernel requests it sends: its
8
+ * `timeoutMs` from now when given, otherwise none (`Infinity`), each request then taking its default.
9
+ */
10
+ export function deadlineOf(options) {
11
+ return options?.timeoutMs === undefined ? Infinity : Date.now() + options.timeoutMs;
12
+ }
13
+ /**
14
+ * When a wait of `waitMs` from now ends: then, or the answer margin before the call's `deadline`
15
+ * if that comes first, so that its answer comes back in time.
16
+ */
17
+ export function waitEnd(deadline, waitMs) {
18
+ return Math.min(Date.now() + waitMs, deadline - ANSWER_MARGIN_MS);
19
+ }
20
+ /** The options of the call's next request: the time left before its deadline, at least 1 ms. */
21
+ export function within(deadline, options) {
22
+ if (deadline === Infinity)
23
+ return options;
24
+ return { ...options, timeoutMs: Math.max(1, deadline - Date.now()) };
25
+ }
@@ -0,0 +1,61 @@
1
+ import type { Value } from '@astrale-os/kernel-dsl/value';
2
+ import type { Call, Result } from '../client/index.js';
3
+ import type { RequestOptions } from '../transport/index.js';
4
+ import type { ExecutionOutcome, ExecutionStatus, ExecutionTarget, ExecutionWait } from './contracts.js';
5
+ /** The value-call capability the `K.Execution` Methods are sent through. */
6
+ export type ExecutionCall = (call: Call, options?: RequestOptions) => Promise<Result>;
7
+ /**
8
+ * The `K.Execution` Methods of any Execution. Observing and a cooperative cancel are the Domain's
9
+ * or the initiator's; signals, a forced cancel, resume and delete are the owner Domain's (Root's
10
+ * for an install). A completed value is typed by the reference alone.
11
+ */
12
+ export interface ExecutionApi {
13
+ status<Output>(execution: ExecutionTarget<Output>, options?: RequestOptions): Promise<ExecutionStatus<Output>>;
14
+ /**
15
+ * The terminal outcome, waiting at most `waitMs` for it in total; the Kernel's
16
+ * `EXECUTION_PENDING` once that wait lapses. Ending the wait never ends the Execution.
17
+ */
18
+ result<Output>(execution: ExecutionTarget<Output>, wait?: ExecutionWait, options?: RequestOptions): Promise<ExecutionOutcome<Output>>;
19
+ /** Resolve the named durable promise once. */
20
+ signal(execution: ExecutionTarget, signal: {
21
+ readonly name: string;
22
+ readonly value: Value;
23
+ }, options?: RequestOptions): Promise<void>;
24
+ /**
25
+ * A cooperative cancel: the run sees it at its next await and may compensate; one that cannot
26
+ * reach an await ends `cancelled` without compensation. `force` kills the run without
27
+ * compensation.
28
+ */
29
+ cancel<Output>(execution: ExecutionTarget<Output>, input?: {
30
+ readonly force?: boolean;
31
+ }, options?: RequestOptions): Promise<ExecutionStatus<Output>>;
32
+ /** Resume a paused Execution; a running one is unchanged. */
33
+ resume<Output>(execution: ExecutionTarget<Output>, options?: RequestOptions): Promise<ExecutionStatus<Output>>;
34
+ /** Delete the Execution, then kill and purge its run. */
35
+ delete(execution: ExecutionTarget, options?: RequestOptions): Promise<void>;
36
+ }
37
+ /** The `K.Execution` Methods of one kind of Execution, its completed value admitted as `Completed`. */
38
+ export interface ExecutionMethods<Completed> {
39
+ status(execution: ExecutionTarget, options?: RequestOptions): Promise<ExecutionStatus<Completed>>;
40
+ result(execution: ExecutionTarget, wait?: ExecutionWait, options?: RequestOptions): Promise<ExecutionOutcome<Completed>>;
41
+ signal(execution: ExecutionTarget, signal: {
42
+ readonly name: string;
43
+ readonly value: unknown;
44
+ }, options?: RequestOptions): Promise<void>;
45
+ cancel(execution: ExecutionTarget, input?: {
46
+ readonly force?: boolean;
47
+ }, options?: RequestOptions): Promise<ExecutionStatus<Completed>>;
48
+ resume(execution: ExecutionTarget, options?: RequestOptions): Promise<ExecutionStatus<Completed>>;
49
+ delete(execution: ExecutionTarget, options?: RequestOptions): Promise<void>;
50
+ }
51
+ /** Bind the `K.Execution` Methods to one call capability. */
52
+ export declare function createExecution(call: ExecutionCall): ExecutionApi;
53
+ /**
54
+ * The `K.Execution` Methods over one call capability, a completed value admitted by `accept`.
55
+ * Status and outcome are admitted against their Core contracts. The effect-free reads, `status`
56
+ * and `result`, are sent again through the pacer after a TransportError or a refusal that carries
57
+ * a retry delay, within the call's deadline; the other Methods are sent once.
58
+ */
59
+ export declare function executionMethods<Completed>(call: ExecutionCall, accept: (value: unknown) => Completed): ExecutionMethods<Completed>;
60
+ /** The wait an `ExecutionWait` asks for, in milliseconds. */
61
+ export declare function acceptWait(wait: ExecutionWait | undefined): number;
@@ -0,0 +1,132 @@
1
+ import { Path } from '@astrale-os/kernel-core/path';
2
+ import { executionOutcome, executionStatus, } from '@astrale-os/kernel-core/schema/kernel/syscalls/contracts';
3
+ import { execution as methods } from '@astrale-os/kernel-core/schema/kernel/syscalls/references';
4
+ import { MethodKey } from '@astrale-os/kernel-dsl/v1/addressing';
5
+ import { call as invocation } from '../client/index.js';
6
+ import { ResponseError, TransportError } from '../errors/index.js';
7
+ import { RETRY_REFUSALS, retry } from '../pacer/index.js';
8
+ import { acceptOutput } from './admission.js';
9
+ import { ANSWER_MARGIN_MS, deadlineOf, waitEnd, within } from './deadline.js';
10
+ /** Attempts of one effect-free Execution read; the call's deadline bounds them too. */
11
+ const READ_ATTEMPTS = 8;
12
+ /** A pending answer sooner than this did not wait: the Kernel answers now, so waiting ends. */
13
+ const LAPSED_WAIT_MS = 1_000;
14
+ /** No wait is sent again with less time left than this. */
15
+ const MINIMUM_WAIT_MS = 1_000;
16
+ /** Bind the `K.Execution` Methods to one call capability. */
17
+ export function createExecution(call) {
18
+ // A completed value is the handler output, which only the reference types.
19
+ return executionMethods(call, (value) => value);
20
+ }
21
+ /**
22
+ * The `K.Execution` Methods over one call capability, a completed value admitted by `accept`.
23
+ * Status and outcome are admitted against their Core contracts. The effect-free reads, `status`
24
+ * and `result`, are sent again through the pacer after a TransportError or a refusal that carries
25
+ * a retry delay, within the call's deadline; the other Methods are sent once.
26
+ */
27
+ export function executionMethods(call, accept) {
28
+ const send = async (id, method, input, options) => {
29
+ const target = Path.instanceMethod(Path.id(id), MethodKey.of(method.owner, method.name));
30
+ const result = await call(invocation(target, input), options);
31
+ if (result.kind !== 'value')
32
+ throw new TypeError('Execution Method returned a non-value result.');
33
+ return result.value;
34
+ };
35
+ const read = (deadline, options, work) => retry(() => work(within(deadline, options)), {
36
+ maxAttempts: READ_ATTEMPTS,
37
+ shouldRetry: (error, attempt) => Date.now() < deadline &&
38
+ (error instanceof TransportError || RETRY_REFUSALS.shouldRetry(error, attempt)),
39
+ retryAfter: RETRY_REFUSALS.retryAfter,
40
+ });
41
+ const outcomeOf = (input) => {
42
+ const outcome = acceptOutput(executionOutcome, input, 'Execution outcome');
43
+ if (outcome.kind !== 'completed')
44
+ return outcome;
45
+ return Object.freeze({ kind: 'completed', value: accept(outcome.value) });
46
+ };
47
+ const statusOf = (id, input) => {
48
+ const admitted = acceptOutput(executionStatus, input, 'Execution status');
49
+ if (admitted.id !== id)
50
+ throw new TypeError('Kernel returned the status of another Execution.');
51
+ const outcome = admitted.outcome === undefined ? undefined : outcomeOf(admitted.outcome);
52
+ // A deleted Execution carries the outcome its lifecycle record keeps, whatever its kind.
53
+ if (admitted.state === 'deleted')
54
+ return Object.freeze({ id, state: 'deleted', outcome: outcome });
55
+ if ((admitted.state === 'completed') !== (outcome?.kind === 'completed')) {
56
+ throw new TypeError('Execution status and outcome disagree.');
57
+ }
58
+ const { outcome: _outcome, ...rest } = admitted;
59
+ return Object.freeze({ ...rest, ...(outcome === undefined ? {} : { outcome }) });
60
+ };
61
+ return Object.freeze({
62
+ async status(target, options) {
63
+ const id = idOf(target);
64
+ return read(deadlineOf(options), options, async (attempt) => statusOf(id, await send(id, methods.status, {}, attempt)));
65
+ },
66
+ /**
67
+ * Each request waits for the time left, ending early enough for its answer to come back before
68
+ * the call's deadline, and lives as long as its wait and that answer. A wait that lapsed after
69
+ * waiting is sent again while time is left; any other end of the wait is the caller's.
70
+ */
71
+ async result(target, wait, options) {
72
+ const id = idOf(target);
73
+ const deadline = deadlineOf(options);
74
+ const end = waitEnd(deadline, acceptWait(wait));
75
+ for (;;) {
76
+ const sent = Date.now();
77
+ try {
78
+ return await read(deadline, options, async (attempt) => {
79
+ const waitMs = Math.max(0, end - Date.now());
80
+ return outcomeOf(await send(id, methods.result, { waitMs }, waitRequest(waitMs, attempt)));
81
+ });
82
+ }
83
+ catch (error) {
84
+ const lapsed = isPending(error) && Date.now() - sent >= LAPSED_WAIT_MS;
85
+ if (!lapsed || end - Date.now() < MINIMUM_WAIT_MS)
86
+ throw error;
87
+ }
88
+ }
89
+ },
90
+ async signal(target, signal, options) {
91
+ // The call admits the value as portable before it is sent.
92
+ const input = { name: signal.name, value: signal.value };
93
+ await send(idOf(target), methods.signal, input, options);
94
+ },
95
+ async cancel(target, input = {}, options) {
96
+ const id = idOf(target);
97
+ return statusOf(id, await send(id, methods.cancel, { ...input }, options));
98
+ },
99
+ async resume(target, options) {
100
+ const id = idOf(target);
101
+ return statusOf(id, await send(id, methods.resume, {}, options));
102
+ },
103
+ async delete(target, options) {
104
+ await send(idOf(target), methods.delete, {}, options);
105
+ },
106
+ });
107
+ }
108
+ /**
109
+ * The request of one wait: it lives as long as the wait and the time its answer takes to come
110
+ * back, which a wait ending that long before the call's deadline keeps within it. A request that
111
+ * does not wait keeps the call's options.
112
+ */
113
+ function waitRequest(waitMs, options) {
114
+ return waitMs === 0 ? options : { ...options, timeoutMs: waitMs + ANSWER_MARGIN_MS };
115
+ }
116
+ /** The wait an `ExecutionWait` asks for, in milliseconds. */
117
+ export function acceptWait(wait) {
118
+ const waitMs = wait?.waitMs ?? 0;
119
+ if (!Number.isSafeInteger(waitMs) || waitMs < 0) {
120
+ throw new TypeError('An Execution wait is a nonnegative integer of milliseconds.');
121
+ }
122
+ return waitMs;
123
+ }
124
+ function idOf(target) {
125
+ const id = typeof target === 'string' ? target : target?.id;
126
+ if (typeof id !== 'string')
127
+ throw new TypeError('An Execution is named by its reference or NodeId.');
128
+ return id;
129
+ }
130
+ function isPending(error) {
131
+ return error instanceof ResponseError && error.reason?.code === 'EXECUTION_PENDING';
132
+ }
@@ -0,0 +1,4 @@
1
+ export type { DeletedExecutionStatus, ExecutionFailure, ExecutionNodeStatus, ExecutionOutcome, ExecutionRef, ExecutionState, ExecutionStatus, ExecutionTarget, ExecutionWait, } from './contracts.js';
2
+ export { acceptWait, createExecution, executionMethods, type ExecutionApi, type ExecutionCall, type ExecutionMethods, } from './execution.js';
3
+ export { acceptOutput } from './admission.js';
4
+ export { deadlineOf, waitEnd, within } from './deadline.js';
@@ -0,0 +1,3 @@
1
+ export { acceptWait, createExecution, executionMethods, } from './execution.js';
2
+ export { acceptOutput } from './admission.js';
3
+ export { deadlineOf, waitEnd, within } from './deadline.js';
@@ -3,11 +3,11 @@ import type { Node } from '@astrale-os/kernel-core/graph/node';
3
3
  import type { QueryAST, QuerySingleSourceClassOf } from '@astrale-os/kernel-core/graph/query';
4
4
  import type { Domain } from '@astrale-os/kernel-dsl/v1/domain';
5
5
  import type { AnyResolvedClass, ResolvedClass } from '@astrale-os/kernel-dsl/v1/language';
6
- import type { GraphApi, GraphQueryOptions } from '../graph.js';
6
+ import type { GraphApi, GraphQueryV6Options } from '../graph.js';
7
7
  import type { BoundEdge, BoundNode } from './model.js';
8
8
  import type { BoundQueryPage } from './page.js';
9
9
  import type { MethodCall } from './project.js';
10
- export type DomainQueryOptions = Omit<GraphQueryOptions, 'expected'>;
10
+ export type DomainQueryOptions = Omit<GraphQueryV6Options, 'expected'>;
11
11
  type AdmittedSourceClasses<Query extends QueryAST> = [QuerySingleSourceClassOf<Query>] extends [
12
12
  never
13
13
  ] ? never : QuerySingleSourceClassOf<Query> extends AnyResolvedClass ? unknown : never;
@@ -1,31 +1,29 @@
1
- import { ClassKey } from '@astrale-os/kernel-dsl/v1/addressing';
2
1
  import { isDomain } from '@astrale-os/kernel-dsl/v1/domain';
3
2
  import { provenanceOfGraphValue, provenanceOfResponse } from '../provenance.js';
3
+ import { definedClass, validateDefinedProperties } from './conformance.js';
4
4
  import { selectedEdges, selectedNodes } from './page.js';
5
5
  import { projectEdge, projectNode } from './project.js';
6
- import { provenanceOfProjectedValue, sameClosure } from './provenance.js';
6
+ import { provenanceOfProjectedValue } from './provenance.js';
7
7
  export function createDomainBinding(domain, graph, call) {
8
8
  if (!isDomain(domain))
9
9
  throw new TypeError('Domain binding requires an admitted DSL Domain.');
10
10
  const wrapNode = (selected, node) => {
11
11
  requireOwnedClass(domain, selected);
12
- const provenance = requireProvenance(domain, node);
12
+ const provenance = requireProvenance(node);
13
13
  const exact = requireExactClass(domain, node.class, 'node');
14
14
  if (!exact.satisfies(selected))
15
15
  throw new TypeError(`${exact.key} does not satisfy ${selected.key}.`);
16
- const diagnostics = exact.validateProperties(node.props);
17
- if (diagnostics.length > 0)
16
+ if (validateDefinedProperties(exact, node.props).length > 0)
18
17
  throw new TypeError(`Node ${node.id} has invalid exact Properties.`);
19
18
  return projectNode(selected, node, provenance, call);
20
19
  };
21
20
  const wrapEdge = (selected, edge) => {
22
21
  requireOwnedClass(domain, selected);
23
- const provenance = requireProvenance(domain, edge);
22
+ const provenance = requireProvenance(edge);
24
23
  const exact = requireExactClass(domain, edge.class, 'edge');
25
24
  if (!exact.satisfies(selected))
26
25
  throw new TypeError(`${exact.key} does not satisfy ${selected.key}.`);
27
- const diagnostics = exact.validateProperties(edge.props);
28
- if (diagnostics.length > 0)
26
+ if (validateDefinedProperties(exact, edge.props).length > 0)
29
27
  throw new TypeError('Edge has invalid exact Properties.');
30
28
  return projectEdge(selected, edge, provenance);
31
29
  };
@@ -34,9 +32,14 @@ export function createDomainBinding(domain, graph, call) {
34
32
  graph,
35
33
  async query(query, options) {
36
34
  const selected = selectedSourceClass(domain, query);
37
- const response = await graph.query(query, { ...options, expected: domain.closure });
35
+ // The Kernel answers in its active Schema; projection checks each value against `domain`.
36
+ const response = await graph.query(query, options);
37
+ // A repair-session read may hold facts the bound Domain refutes: it is never typed by it.
38
+ if (response.certification === 'uncertified') {
39
+ throw new TypeError('An uncertified Query response cannot be bound to its Domain.');
40
+ }
38
41
  const provenance = provenanceOfResponse(response);
39
- if (provenance === undefined || !sameClosure(provenance.closure, domain.closure)) {
42
+ if (provenance === undefined) {
40
43
  throw new TypeError('Authenticated query provenance was not retained.');
41
44
  }
42
45
  const next = response.page.next === undefined
@@ -46,7 +49,9 @@ export function createDomainBinding(domain, graph, call) {
46
49
  page: { ...options.page, after: response.page.next },
47
50
  });
48
51
  if (selected.kind === 'node') {
49
- const nodes = selectedNodes(response).map((node) => wrapNode(selected, node));
52
+ const nodes = selectedNodes(response)
53
+ .filter((node) => definedClass(domain, node.class) !== undefined)
54
+ .map((node) => wrapNode(selected, node));
50
55
  return Object.freeze({
51
56
  nodes: Object.freeze(nodes),
52
57
  raw: response,
@@ -54,7 +59,9 @@ export function createDomainBinding(domain, graph, call) {
54
59
  refine: (value, target) => refineNode(domain, call, value, target, provenance),
55
60
  });
56
61
  }
57
- const edges = selectedEdges(response).map((edge) => wrapEdge(selected, edge));
62
+ const edges = selectedEdges(response)
63
+ .filter((edge) => definedClass(domain, edge.class) !== undefined)
64
+ .map((edge) => wrapEdge(selected, edge));
58
65
  return Object.freeze({
59
66
  edges: Object.freeze(edges),
60
67
  raw: response,
@@ -107,16 +114,16 @@ function requireOwnedClass(domain, selected) {
107
114
  throw new TypeError('Selected Class does not belong to the bound Domain closure.');
108
115
  }
109
116
  }
110
- function requireProvenance(domain, value) {
117
+ function requireProvenance(value) {
111
118
  const provenance = provenanceOfGraphValue(value);
112
- if (provenance === undefined || !sameClosure(provenance.closure, domain.closure)) {
113
- throw new TypeError('Graph value lacks matching authenticated query provenance.');
119
+ if (provenance === undefined) {
120
+ throw new TypeError('Graph value lacks authenticated query provenance.');
114
121
  }
115
122
  return provenance;
116
123
  }
117
124
  function requireExactClass(domain, key, kind) {
118
- const exact = domain.definition(ClassKey.ref(ClassKey(key)));
119
- if (exact?.ref.kind !== 'class' || exact.kind !== kind || exact.abstract) {
125
+ const exact = definedClass(domain, key);
126
+ if (exact?.kind !== kind || exact.abstract) {
120
127
  throw new TypeError(`Exact ${kind} Class ${key} is not active and concrete.`);
121
128
  }
122
129
  return exact;
@@ -0,0 +1,12 @@
1
+ import type { Domain } from '@astrale-os/kernel-dsl/v1/domain';
2
+ import type { AnyResolvedClass, Diagnostic } from '@astrale-os/kernel-dsl/v1/language';
3
+ /**
4
+ * The Class `domain` defines under `key`, or `undefined` when that Class was added after `domain`
5
+ * was built. The Kernel answers in its active Schema, so a result may hold values of such a Class.
6
+ */
7
+ export declare function definedClass(domain: Domain, key: string): AnyResolvedClass | undefined;
8
+ /**
9
+ * What in `props` breaks `exact`: a Property it defines holding an invalid value, or a required one
10
+ * absent. A Property `exact` does not define passes: the active Schema may add it.
11
+ */
12
+ export declare function validateDefinedProperties(exact: AnyResolvedClass, props: unknown): readonly Diagnostic[];
@@ -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';