@astrale-os/kernel-client 1.0.0-beta.23 → 1.0.0-beta.24
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.
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +1 -0
- package/dist/errors/query-input.d.ts +47 -0
- package/dist/errors/query-input.js +84 -0
- package/dist/execution/deadline.d.ts +10 -0
- package/dist/execution/deadline.js +12 -0
- package/dist/execution/execution.js +2 -7
- package/dist/execution/index.d.ts +1 -1
- package/dist/execution/index.js +1 -1
- package/dist/schema/contracts.d.ts +21 -10
- package/dist/schema/index.d.ts +1 -1
- package/dist/schema/installation.d.ts +11 -1
- package/dist/schema/installation.js +45 -1
- package/dist/schema/schema.d.ts +4 -4
- package/dist/schema/schema.js +34 -24
- package/dist/session/bound.js +2 -0
- package/dist/session/context/operation.js +2 -16
- package/dist/transport/deadline.d.ts +2 -0
- package/dist/transport/deadline.js +18 -0
- package/dist/transport/index.d.ts +1 -1
- package/dist/transport/index.js +1 -1
- package/package.json +5 -5
package/dist/errors/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from './client-error.js';
|
|
|
2
2
|
export * from './exchange-error.js';
|
|
3
3
|
export * from './miss-error.js';
|
|
4
4
|
export * from './protocol-error.js';
|
|
5
|
+
export * from './query-input.js';
|
|
5
6
|
export * from './refusal.js';
|
|
6
7
|
export * from './response-error.js';
|
|
7
8
|
export * from './session-error.js';
|
package/dist/errors/index.js
CHANGED
|
@@ -2,6 +2,7 @@ export * from './client-error.js';
|
|
|
2
2
|
export * from './exchange-error.js';
|
|
3
3
|
export * from './miss-error.js';
|
|
4
4
|
export * from './protocol-error.js';
|
|
5
|
+
export * from './query-input.js';
|
|
5
6
|
export * from './refusal.js';
|
|
6
7
|
export * from './response-error.js';
|
|
7
8
|
export * from './session-error.js';
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Refusal } from '@astrale-os/kernel-core/graph/query';
|
|
2
|
+
import * as errors from '@astrale-os/kernel-protocol/errors';
|
|
3
|
+
import { ResponseError } from './response-error.js';
|
|
4
|
+
/**
|
|
5
|
+
* Why the Kernel refused a Query as input (`QUERY_INPUT_INVALID`), by the phase that refused it.
|
|
6
|
+
* Nothing was read, and the same request is refused again.
|
|
7
|
+
*/
|
|
8
|
+
export type QueryInputDetails =
|
|
9
|
+
/** A v7 Query the wire refused whole: each fault, where it is and how to fix it (§ Wire v7). */
|
|
10
|
+
{
|
|
11
|
+
readonly phase: 'decode';
|
|
12
|
+
readonly refusals: readonly Readonly<Refusal>[];
|
|
13
|
+
}
|
|
14
|
+
/** A Query its planning refused: the issue, and where in the request when it says. */
|
|
15
|
+
| {
|
|
16
|
+
readonly phase: 'plan';
|
|
17
|
+
readonly issue: string;
|
|
18
|
+
readonly path?: string;
|
|
19
|
+
}
|
|
20
|
+
/** A Query past a limit the Kernel sets for every caller: which, its maximum, what it needs. */
|
|
21
|
+
| {
|
|
22
|
+
readonly phase: 'plan';
|
|
23
|
+
readonly issue: 'QUERY_PLAN_LIMIT_EXCEEDED';
|
|
24
|
+
readonly limit: string;
|
|
25
|
+
readonly maximum: number;
|
|
26
|
+
readonly actual: number;
|
|
27
|
+
readonly path?: string;
|
|
28
|
+
}
|
|
29
|
+
/** An `expected` closure the Kernel cannot read, or a V6 page size: where. */
|
|
30
|
+
| {
|
|
31
|
+
readonly phase: 'input';
|
|
32
|
+
readonly path: string;
|
|
33
|
+
}
|
|
34
|
+
/** A V6 QueryAST the Kernel could not decode: where. */
|
|
35
|
+
| {
|
|
36
|
+
readonly phase: 'decode';
|
|
37
|
+
readonly path: string;
|
|
38
|
+
};
|
|
39
|
+
/** The reason of a Query the Kernel refused as input. */
|
|
40
|
+
export type QueryInputReason = errors.ErrorReason<'QUERY_INPUT_INVALID', QueryInputDetails>;
|
|
41
|
+
/**
|
|
42
|
+
* Whether the Kernel refused a Query as input: nothing was read, and resending it unchanged is
|
|
43
|
+
* refused again. Its `reason.details` then say where and why, typed by phase.
|
|
44
|
+
*/
|
|
45
|
+
export declare function isQueryInputInvalid(error: unknown): error is Omit<ResponseError, 'reason'> & {
|
|
46
|
+
readonly reason: QueryInputReason;
|
|
47
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as errors from '@astrale-os/kernel-protocol/errors';
|
|
2
|
+
import { ResponseError } from './response-error.js';
|
|
3
|
+
/**
|
|
4
|
+
* Whether the Kernel refused a Query as input: nothing was read, and resending it unchanged is
|
|
5
|
+
* refused again. Its `reason.details` then say where and why, typed by phase.
|
|
6
|
+
*/
|
|
7
|
+
export function isQueryInputInvalid(error) {
|
|
8
|
+
return (error instanceof ResponseError &&
|
|
9
|
+
error.code === errors.CODES.VALIDATION_ERROR &&
|
|
10
|
+
error.reason?.code === 'QUERY_INPUT_INVALID' &&
|
|
11
|
+
isQueryInputDetails(error.reason.details));
|
|
12
|
+
}
|
|
13
|
+
const REFUSAL_FIELDS = new Set([
|
|
14
|
+
'code',
|
|
15
|
+
'path',
|
|
16
|
+
'expected',
|
|
17
|
+
'fix',
|
|
18
|
+
'didYouMean',
|
|
19
|
+
'tier',
|
|
20
|
+
]);
|
|
21
|
+
const ISSUE = /^[A-Z][A-Z0-9_]{0,127}$/u;
|
|
22
|
+
/** The details the Kernel gives a refused Query, each phase with exactly its own fields. */
|
|
23
|
+
function isQueryInputDetails(details) {
|
|
24
|
+
const path = details.path === undefined ? [] : ['path'];
|
|
25
|
+
switch (details.phase) {
|
|
26
|
+
case 'decode':
|
|
27
|
+
return Object.hasOwn(details, 'refusals')
|
|
28
|
+
? exact(details, ['phase', 'refusals']) &&
|
|
29
|
+
Array.isArray(details.refusals) &&
|
|
30
|
+
details.refusals.length > 0 &&
|
|
31
|
+
details.refusals.every(isRefusal)
|
|
32
|
+
: exact(details, ['phase', 'path']) && isPointer(details.path);
|
|
33
|
+
case 'input':
|
|
34
|
+
return exact(details, ['phase', 'path']) && isPointer(details.path);
|
|
35
|
+
case 'plan':
|
|
36
|
+
if (typeof details.issue !== 'string' ||
|
|
37
|
+
!ISSUE.test(details.issue) ||
|
|
38
|
+
(details.path !== undefined && !isPointer(details.path))) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
if (!Object.hasOwn(details, 'limit'))
|
|
42
|
+
return exact(details, ['phase', 'issue', ...path]);
|
|
43
|
+
return (details.issue === 'QUERY_PLAN_LIMIT_EXCEEDED' &&
|
|
44
|
+
exact(details, ['phase', 'issue', 'limit', 'maximum', 'actual', ...path]) &&
|
|
45
|
+
typeof details.limit === 'string' &&
|
|
46
|
+
details.limit.length > 0 &&
|
|
47
|
+
isCount(details.maximum) &&
|
|
48
|
+
isCount(details.actual) &&
|
|
49
|
+
details.actual > details.maximum);
|
|
50
|
+
default:
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** One refusal of the v7 wire, as the Kernel carries it: `{code, path, expected, fix, …}`. */
|
|
55
|
+
function isRefusal(input) {
|
|
56
|
+
return (isObject(input) &&
|
|
57
|
+
Object.keys(input).every((field) => REFUSAL_FIELDS.has(field)) &&
|
|
58
|
+
typeof input.code === 'string' &&
|
|
59
|
+
/^QUERY_[A-Z_]{1,64}$/u.test(input.code) &&
|
|
60
|
+
isPointer(input.path) &&
|
|
61
|
+
typeof input.expected === 'string' &&
|
|
62
|
+
typeof input.fix === 'string' &&
|
|
63
|
+
(input.didYouMean === undefined ||
|
|
64
|
+
(Array.isArray(input.didYouMean) &&
|
|
65
|
+
input.didYouMean.every((name) => typeof name === 'string'))) &&
|
|
66
|
+
(input.tier === undefined || typeof input.tier === 'string'));
|
|
67
|
+
}
|
|
68
|
+
/** An RFC 6901 pointer into the request, as the Kernel bounds it. */
|
|
69
|
+
function isPointer(input) {
|
|
70
|
+
return (typeof input === 'string' &&
|
|
71
|
+
input.length <= 1_024 &&
|
|
72
|
+
input.normalize('NFC') === input &&
|
|
73
|
+
(input === '' || input.startsWith('/')));
|
|
74
|
+
}
|
|
75
|
+
function isCount(input) {
|
|
76
|
+
return typeof input === 'number' && Number.isSafeInteger(input) && input >= 0;
|
|
77
|
+
}
|
|
78
|
+
function isObject(input) {
|
|
79
|
+
return input !== null && typeof input === 'object' && !Array.isArray(input);
|
|
80
|
+
}
|
|
81
|
+
function exact(input, fields) {
|
|
82
|
+
return (Object.keys(input).length === fields.length &&
|
|
83
|
+
fields.every((field) => Object.hasOwn(input, field)));
|
|
84
|
+
}
|
|
@@ -1,8 +1,18 @@
|
|
|
1
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;
|
|
2
7
|
/**
|
|
3
8
|
* The absolute deadline of one Client call, whatever number of Kernel requests it sends: its
|
|
4
9
|
* `timeoutMs` from now when given, otherwise none (`Infinity`), each request then taking its default.
|
|
5
10
|
*/
|
|
6
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;
|
|
7
17
|
/** The options of the call's next request: the time left before its deadline, at least 1 ms. */
|
|
8
18
|
export declare function within(deadline: number, options: RequestOptions | undefined): RequestOptions | undefined;
|
|
@@ -1,3 +1,8 @@
|
|
|
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;
|
|
1
6
|
/**
|
|
2
7
|
* The absolute deadline of one Client call, whatever number of Kernel requests it sends: its
|
|
3
8
|
* `timeoutMs` from now when given, otherwise none (`Infinity`), each request then taking its default.
|
|
@@ -5,6 +10,13 @@
|
|
|
5
10
|
export function deadlineOf(options) {
|
|
6
11
|
return options?.timeoutMs === undefined ? Infinity : Date.now() + options.timeoutMs;
|
|
7
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
|
+
}
|
|
8
20
|
/** The options of the call's next request: the time left before its deadline, at least 1 ms. */
|
|
9
21
|
export function within(deadline, options) {
|
|
10
22
|
if (deadline === Infinity)
|
|
@@ -6,18 +6,13 @@ import { call as invocation } from '../client/index.js';
|
|
|
6
6
|
import { ResponseError, TransportError } from '../errors/index.js';
|
|
7
7
|
import { RETRY_REFUSALS, retry } from '../pacer/index.js';
|
|
8
8
|
import { acceptOutput } from './admission.js';
|
|
9
|
-
import { deadlineOf, within } from './deadline.js';
|
|
9
|
+
import { ANSWER_MARGIN_MS, deadlineOf, waitEnd, within } from './deadline.js';
|
|
10
10
|
/** Attempts of one effect-free Execution read; the call's deadline bounds them too. */
|
|
11
11
|
const READ_ATTEMPTS = 8;
|
|
12
12
|
/** A pending answer sooner than this did not wait: the Kernel answers now, so waiting ends. */
|
|
13
13
|
const LAPSED_WAIT_MS = 1_000;
|
|
14
14
|
/** No wait is sent again with less time left than this. */
|
|
15
15
|
const MINIMUM_WAIT_MS = 1_000;
|
|
16
|
-
/**
|
|
17
|
-
* The time a wait leaves its answer to come back. The Kernel bounds a wait by its own request
|
|
18
|
-
* deadline, never by this call's: the client ends each wait this long before its request times out.
|
|
19
|
-
*/
|
|
20
|
-
const ANSWER_MARGIN_MS = 2_000;
|
|
21
16
|
/** Bind the `K.Execution` Methods to one call capability. */
|
|
22
17
|
export function createExecution(call) {
|
|
23
18
|
// A completed value is the handler output, which only the reference types.
|
|
@@ -76,7 +71,7 @@ export function executionMethods(call, accept) {
|
|
|
76
71
|
async result(target, wait, options) {
|
|
77
72
|
const id = idOf(target);
|
|
78
73
|
const deadline = deadlineOf(options);
|
|
79
|
-
const end =
|
|
74
|
+
const end = waitEnd(deadline, acceptWait(wait));
|
|
80
75
|
for (;;) {
|
|
81
76
|
const sent = Date.now();
|
|
82
77
|
try {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export type { DeletedExecutionStatus, ExecutionFailure, ExecutionNodeStatus, ExecutionOutcome, ExecutionRef, ExecutionState, ExecutionStatus, ExecutionTarget, ExecutionWait, } from './contracts.js';
|
|
2
2
|
export { acceptWait, createExecution, executionMethods, type ExecutionApi, type ExecutionCall, type ExecutionMethods, } from './execution.js';
|
|
3
3
|
export { acceptOutput } from './admission.js';
|
|
4
|
-
export { deadlineOf, within } from './deadline.js';
|
|
4
|
+
export { deadlineOf, waitEnd, within } from './deadline.js';
|
package/dist/execution/index.js
CHANGED
|
@@ -2,16 +2,23 @@ import type { NodeId } from '@astrale-os/kernel-core/graph/node';
|
|
|
2
2
|
import type { installation } from '@astrale-os/kernel-core/schema';
|
|
3
3
|
import type { AttemptReport, DomainRequest as CoreDomainRequest, InstallClass as CoreInstallClass, InstallInput, InstallMode as CoreInstallMode, InstallReceipt as CoreInstallReceipt, SystemExecutionDetail, UninstallInput, UninstallOutput, ValidationOptions } from '@astrale-os/kernel-core/schema/kernel/syscalls/contracts';
|
|
4
4
|
import type { schema as protocolSchema } from '@astrale-os/kernel-protocol';
|
|
5
|
-
import type { ExecutionOutcome, ExecutionStatus
|
|
5
|
+
import type { ExecutionOutcome, ExecutionStatus } from '../execution/index.js';
|
|
6
6
|
export type OperationId = installation.OperationId;
|
|
7
7
|
/** Canonical caller input accepted by the Kernel install syscall. */
|
|
8
8
|
export type DomainRequest = CoreDomainRequest;
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
* by default; `0`
|
|
12
|
-
* part of the operation's intent.
|
|
10
|
+
* How long an install, an uninstall or a watch of an install waits at most, `waitMs`, in
|
|
11
|
+
* milliseconds: 110 s by default; `0` reads the status once and answers it. The wait is the
|
|
12
|
+
* Client's own: it is never sent, so it is no part of the operation's intent.
|
|
13
13
|
*/
|
|
14
|
-
export
|
|
14
|
+
export interface LifecycleWait {
|
|
15
|
+
readonly waitMs?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* One install: its Kernel input, plus how long the Client waits for its outcome, or, in mode
|
|
19
|
+
* `maintenance`, until its repair session awaits Root's first command.
|
|
20
|
+
*/
|
|
21
|
+
export type InstallRequest = InstallInput & LifecycleWait;
|
|
15
22
|
/** Admitted terminal install receipt returned by the Kernel. */
|
|
16
23
|
export type InstallReceipt = CoreInstallReceipt;
|
|
17
24
|
/** The committed result of an install: its receipt, or the generations it proved unchanged. */
|
|
@@ -33,6 +40,13 @@ export type InstallOutcome = ExecutionOutcome<InstallResult>;
|
|
|
33
40
|
* attempt report).
|
|
34
41
|
*/
|
|
35
42
|
export type InstallStatus = ExecutionStatus<InstallResult>;
|
|
43
|
+
/**
|
|
44
|
+
* How long a watch of an install lasts at most, and, for a repair session, the index the command
|
|
45
|
+
* it awaits must pass, `after` (`0` by default: any).
|
|
46
|
+
*/
|
|
47
|
+
export interface InstallationWatch extends LifecycleWait {
|
|
48
|
+
readonly after?: number;
|
|
49
|
+
}
|
|
36
50
|
/** One admitted install answer: its Execution, its status, and its result once completed. */
|
|
37
51
|
export interface Installation {
|
|
38
52
|
readonly execution: NodeId;
|
|
@@ -45,11 +59,8 @@ export interface Installation {
|
|
|
45
59
|
/** The committed result, present exactly when the status is completed. */
|
|
46
60
|
readonly result?: InstallResult;
|
|
47
61
|
}
|
|
48
|
-
/**
|
|
49
|
-
|
|
50
|
-
* for an install, and never sent.
|
|
51
|
-
*/
|
|
52
|
-
export type UninstallRequest = UninstallInput & ExecutionWait;
|
|
62
|
+
/** One uninstall: its Kernel input, plus how long the Client waits for its outcome. */
|
|
63
|
+
export type UninstallRequest = UninstallInput & LifecycleWait;
|
|
53
64
|
/** Admitted terminal uninstall receipt returned by the Kernel. */
|
|
54
65
|
export type UninstallReceipt = UninstallOutput;
|
|
55
66
|
/** A terminal uninstall: its committed receipt, its rejection, or its cancellation. */
|
package/dist/schema/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { createSchema, type SchemaApi, type SchemaCall, type SchemaOptions, type DomainBundle, type DomainDependencies, type DomainInfo, type InstalledPin, type InstalledRelease, } from './schema.js';
|
|
2
2
|
export { acceptOperationId, createOperationId } from './operation.js';
|
|
3
3
|
export type { InstallationApi, UninstallationApi } from './installation.js';
|
|
4
|
-
export type { DomainRequest, InstallAttemptReport, InstallClass, InstallDetail, InstallMode, InstallOutcome, InstallResult, InstallStatus, InstallValidationOptions, Installation, InstallReceipt, InstallRequest, OperationId, Uninstallation, UninstallOutcome, UninstallReceipt, UninstallRequest, UninstallStatus, } from './contracts.js';
|
|
4
|
+
export type { DomainRequest, InstallAttemptReport, InstallClass, InstallDetail, InstallMode, InstallOutcome, InstallResult, InstallStatus, InstallValidationOptions, Installation, InstallationWatch, InstallReceipt, InstallRequest, LifecycleWait, OperationId, Uninstallation, UninstallOutcome, UninstallReceipt, UninstallRequest, UninstallStatus, } from './contracts.js';
|
|
@@ -2,7 +2,7 @@ import type { NodeId } from '@astrale-os/kernel-core/graph/node';
|
|
|
2
2
|
import type { InstallValidation } from '@astrale-os/kernel-core/schema/kernel/syscalls/contracts';
|
|
3
3
|
import type { ExecutionCall, ExecutionWait } from '../execution/index.js';
|
|
4
4
|
import type { RequestOptions } from '../transport/index.js';
|
|
5
|
-
import type { InstallOutcome, InstallStatus, UninstallOutcome, UninstallStatus } from './contracts.js';
|
|
5
|
+
import type { InstallationWatch, InstallOutcome, InstallStatus, LifecycleWait, UninstallOutcome, UninstallStatus } from './contracts.js';
|
|
6
6
|
/**
|
|
7
7
|
* The install executions of one Instance, through the `K.Execution` Methods of each. Observing
|
|
8
8
|
* and a cooperative cancel are Root's or the initiator's; signals, a forced cancel, resume and
|
|
@@ -13,6 +13,14 @@ export interface InstallationApi {
|
|
|
13
13
|
status(execution: NodeId, options?: RequestOptions): Promise<InstallStatus>;
|
|
14
14
|
/** The terminal outcome, waiting at most `waitMs` in total; `EXECUTION_PENDING` once it lapses. */
|
|
15
15
|
result(execution: NodeId, wait?: ExecutionWait, options?: RequestOptions): Promise<InstallOutcome>;
|
|
16
|
+
/**
|
|
17
|
+
* Read the status until the repair session awaits a command whose index passes `after`, or the
|
|
18
|
+
* install ends, pauses or expires, or `waitMs` lapses, or the deadline `timeoutMs` sets is within
|
|
19
|
+
* the answer margin (2 s), and answer the last status read. Each read
|
|
20
|
+
* follows the last one after a quarter of the time the watch has lasted, at most one second and
|
|
21
|
+
* never less than that read took: a change is seen within a quarter of the time it took to come.
|
|
22
|
+
*/
|
|
23
|
+
watch(execution: NodeId, watch?: InstallationWatch, options?: RequestOptions): Promise<InstallStatus>;
|
|
16
24
|
/**
|
|
17
25
|
* A waiting repair session attempts its target: the `command:<index>` signal, `index` being
|
|
18
26
|
* the `detail.command` its status announces. A signal already resolved is refused.
|
|
@@ -64,3 +72,5 @@ export interface UninstallationApi {
|
|
|
64
72
|
export declare function createInstallation(call: ExecutionCall): InstallationApi;
|
|
65
73
|
/** The uninstall Methods: a completed outcome carries the receipt Core's contract admits. */
|
|
66
74
|
export declare function createUninstallation(call: ExecutionCall): UninstallationApi;
|
|
75
|
+
/** The wait `wait` asks for, in milliseconds: its `waitMs`, or the default. */
|
|
76
|
+
export declare function lifecycleWait(wait: LifecycleWait | undefined): number;
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import { abortSignal, commandSignal, uninstallOutput, } from '@astrale-os/kernel-core/schema/kernel/syscalls/contracts';
|
|
2
2
|
import * as protocolSchema from '@astrale-os/kernel-protocol/schema';
|
|
3
|
-
import { acceptOutput, executionMethods } from '../execution/index.js';
|
|
3
|
+
import { acceptOutput, acceptWait, deadlineOf, executionMethods, waitEnd, within, } from '../execution/index.js';
|
|
4
|
+
import { pause } from '../transport/index.js';
|
|
5
|
+
/** How long an install or uninstall waits for what it waits for unless its request says otherwise. */
|
|
6
|
+
const DEFAULT_LIFECYCLE_WAIT_MS = 110_000;
|
|
7
|
+
/** The longest pause between two reads of a watch: a long watch reads once a second. */
|
|
8
|
+
const WATCH_PAUSE_MS = 1_000;
|
|
4
9
|
/** The install Methods: a completed outcome carries the result its Protocol owner admits. */
|
|
5
10
|
export function createInstallation(call) {
|
|
6
11
|
const { signal, ...methods } = executionMethods(call, acceptInstallResult);
|
|
7
12
|
return Object.freeze({
|
|
8
13
|
...methods,
|
|
14
|
+
watch: (execution, watch, options) => watchInstall(methods.status, execution, watch, options),
|
|
9
15
|
apply: async (execution, command, options) => signal(execution, {
|
|
10
16
|
name: commandSignal(ordinal(command.index)),
|
|
11
17
|
value: Object.freeze({
|
|
@@ -21,6 +27,44 @@ export function createUninstallation(call) {
|
|
|
21
27
|
const { signal: _signal, ...methods } = executionMethods(call, acceptUninstallReceipt);
|
|
22
28
|
return Object.freeze(methods);
|
|
23
29
|
}
|
|
30
|
+
/** The watch `InstallationApi.watch` describes, over the `status` Method. */
|
|
31
|
+
async function watchInstall(status, execution, watch, options) {
|
|
32
|
+
const waitMs = lifecycleWait(watch);
|
|
33
|
+
const after = watch?.after ?? 0;
|
|
34
|
+
if (!Number.isSafeInteger(after) || after < 0) {
|
|
35
|
+
throw new TypeError('A watched command index is a nonnegative integer.');
|
|
36
|
+
}
|
|
37
|
+
const deadline = deadlineOf(options);
|
|
38
|
+
const started = Date.now();
|
|
39
|
+
// Each read after the first has the answer margin left before the call's deadline to come back.
|
|
40
|
+
const ends = waitEnd(deadline, waitMs);
|
|
41
|
+
for (;;) {
|
|
42
|
+
const read = Date.now();
|
|
43
|
+
const current = await status(execution, within(deadline, options));
|
|
44
|
+
const now = Date.now();
|
|
45
|
+
if (settled(current, after) || now >= ends)
|
|
46
|
+
return current;
|
|
47
|
+
// In whole milliseconds, a timer's own resolution: an instant read never reads again at once.
|
|
48
|
+
await pause(Math.min(WATCH_PAUSE_MS, Math.max(1, now - read, Math.ceil((now - started) / 4)), ends - now), options?.signal);
|
|
49
|
+
// A read starts by the end of the watch at the latest: a timer that woke past it reads no more.
|
|
50
|
+
if (Date.now() > ends)
|
|
51
|
+
return current;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Whether a watch ends at `status`: its session awaits a command past `after`, or nothing changes
|
|
56
|
+
* it any more without a command: it ended, paused or expired.
|
|
57
|
+
*/
|
|
58
|
+
function settled(status, after) {
|
|
59
|
+
if (status.outcome !== undefined || status.state === 'paused' || status.state === 'expired')
|
|
60
|
+
return true;
|
|
61
|
+
const detail = status.detail;
|
|
62
|
+
return detail?.phase === 'awaiting' && (detail.command ?? 0) > after;
|
|
63
|
+
}
|
|
64
|
+
/** The wait `wait` asks for, in milliseconds: its `waitMs`, or the default. */
|
|
65
|
+
export function lifecycleWait(wait) {
|
|
66
|
+
return acceptWait({ waitMs: wait?.waitMs ?? DEFAULT_LIFECYCLE_WAIT_MS });
|
|
67
|
+
}
|
|
24
68
|
function acceptInstallResult(input) {
|
|
25
69
|
if (uninstallOutput.safeParse(input).success) {
|
|
26
70
|
throw new TypeError('The Execution is an uninstall: read it through SchemaApi.uninstallation.');
|
package/dist/schema/schema.d.ts
CHANGED
|
@@ -15,10 +15,10 @@ export type InstalledPin = protocolSchema.installation.introspection.InstalledPi
|
|
|
15
15
|
export interface SchemaApi {
|
|
16
16
|
/**
|
|
17
17
|
* Install Domains: start the durable Execution the install is, wait at most `waitMs` (110 s by
|
|
18
|
-
* default) for its outcome
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* call.
|
|
18
|
+
* default) for its outcome, or, in mode `maintenance`, until its repair session awaits Root's
|
|
19
|
+
* first command (`installation.watch`), and answer its status. `timeoutMs`, when given, bounds the
|
|
20
|
+
* whole call. The answer names the Execution with its status; `result` is present once it
|
|
21
|
+
* completed. A rejected install is a terminal `failed` status, never a failed call.
|
|
22
22
|
*/
|
|
23
23
|
install(request: InstallRequest, options?: RequestOptions): Promise<Installation>;
|
|
24
24
|
/** The install executions and repair sessions, through their `K.Execution` Methods. */
|
package/dist/schema/schema.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { Path } from '@astrale-os/kernel-core/path';
|
|
2
2
|
import { install, uninstall } from '@astrale-os/kernel-core/schema/kernel/syscalls/references';
|
|
3
3
|
import { call as invocation } from '../client/index.js';
|
|
4
|
-
import {
|
|
5
|
-
import { createInstallation, createUninstallation, } from './installation.js';
|
|
4
|
+
import { deadlineOf, within } from '../execution/index.js';
|
|
5
|
+
import { createInstallation, createUninstallation, lifecycleWait, } from './installation.js';
|
|
6
6
|
import { introspect } from './introspection.js';
|
|
7
7
|
const INSTALL_PATH = Path.project(install).raw;
|
|
8
8
|
const UNINSTALL_PATH = Path.project(uninstall).raw;
|
|
9
|
-
/** How long an install or uninstall waits for its outcome unless its request says otherwise. */
|
|
10
|
-
const DEFAULT_LIFECYCLE_WAIT_MS = 110_000;
|
|
11
9
|
/** Bind the canonical Schema lifecycle syscalls to one caller-scoped value-call capability. */
|
|
12
10
|
export function createSchema(call, limits) {
|
|
13
11
|
if (typeof call !== 'function')
|
|
@@ -17,17 +15,19 @@ export function createSchema(call, limits) {
|
|
|
17
15
|
const uninstallation = createUninstallation(call);
|
|
18
16
|
return Object.freeze({
|
|
19
17
|
async install(request, options) {
|
|
20
|
-
const { waitMs, ...input } = request;
|
|
21
|
-
const
|
|
22
|
-
// A maintenance session
|
|
23
|
-
const
|
|
24
|
-
|
|
18
|
+
const { waitMs: _waitMs, ...input } = request;
|
|
19
|
+
const waitMs = lifecycleWait(request);
|
|
20
|
+
// A maintenance session's outcome is Root's to decide: the call waits until it awaits Root.
|
|
21
|
+
const wait = input.mode === 'maintenance'
|
|
22
|
+
? sessionWait(installation, waitMs)
|
|
23
|
+
: outcomeWait(installation, waitMs);
|
|
24
|
+
return admitInstallation(request, await lifecycle(call, INSTALL_PATH, input, wait, options));
|
|
25
25
|
},
|
|
26
26
|
installation,
|
|
27
27
|
async uninstall(request, options) {
|
|
28
|
-
const wait = lifecycleWait(request
|
|
28
|
+
const wait = outcomeWait(uninstallation, lifecycleWait(request));
|
|
29
29
|
const snapshot = snapshotUninstallRequest(request);
|
|
30
|
-
const started = await lifecycle(call,
|
|
30
|
+
const started = await lifecycle(call, UNINSTALL_PATH, snapshot, wait, options);
|
|
31
31
|
const answer = { execution: started.status.id, ...started };
|
|
32
32
|
const { status } = started;
|
|
33
33
|
if (status.outcome?.kind !== 'completed')
|
|
@@ -50,28 +50,38 @@ export function createSchema(call, limits) {
|
|
|
50
50
|
},
|
|
51
51
|
});
|
|
52
52
|
}
|
|
53
|
+
/** Wait at most `waitMs` for the outcome, however that wait ends, then read the status. */
|
|
54
|
+
function outcomeWait(executions, waitMs) {
|
|
55
|
+
return async (execution, deadline, options) => {
|
|
56
|
+
if (waitMs > 0)
|
|
57
|
+
await executions
|
|
58
|
+
.result(execution, { waitMs }, within(deadline, options))
|
|
59
|
+
.catch(() => undefined);
|
|
60
|
+
return executions.status(execution, within(deadline, options));
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Watch a repair session at most `waitMs`, until it awaits Root's first command, and answer the
|
|
65
|
+
* last status read; a watch that fails reads the status once.
|
|
66
|
+
*/
|
|
67
|
+
function sessionWait(installation, waitMs) {
|
|
68
|
+
return (execution, deadline, options) => installation
|
|
69
|
+
.watch(execution, { waitMs }, within(deadline, options))
|
|
70
|
+
.catch(() => installation.status(execution, within(deadline, options)));
|
|
71
|
+
}
|
|
53
72
|
/**
|
|
54
|
-
* One lifecycle syscall, before the call's deadline: start the durable Execution it is
|
|
55
|
-
*
|
|
56
|
-
* where the Execution is, so the caller never loses it.
|
|
73
|
+
* One lifecycle syscall, before the call's deadline: start the durable Execution it is and answer
|
|
74
|
+
* the status `wait` ends on.
|
|
57
75
|
*/
|
|
58
|
-
async function lifecycle(call,
|
|
76
|
+
async function lifecycle(call, path, input, wait, options) {
|
|
59
77
|
const deadline = deadlineOf(options);
|
|
60
78
|
const started = await call(invocation(path, input), within(deadline, options));
|
|
61
79
|
if (started.kind !== 'execution') {
|
|
62
80
|
throw new TypeError('Schema syscall did not answer with an Execution.');
|
|
63
81
|
}
|
|
64
|
-
|
|
65
|
-
await executions
|
|
66
|
-
.result(started.execution, { waitMs }, within(deadline, options))
|
|
67
|
-
.catch(() => undefined);
|
|
68
|
-
}
|
|
69
|
-
const status = await executions.status(started.execution, within(deadline, options));
|
|
82
|
+
const status = await wait(started.execution, deadline, options);
|
|
70
83
|
return started.replayed === undefined ? { status } : { status, replayed: started.replayed };
|
|
71
84
|
}
|
|
72
|
-
function lifecycleWait(waitMs) {
|
|
73
|
-
return acceptWait({ waitMs: waitMs ?? DEFAULT_LIFECYCLE_WAIT_MS });
|
|
74
|
-
}
|
|
75
85
|
function snapshotUninstallRequest(request) {
|
|
76
86
|
return Object.freeze({
|
|
77
87
|
operation: request.operation,
|
package/dist/session/bound.js
CHANGED
|
@@ -41,6 +41,7 @@ export function bindSession(source, controls) {
|
|
|
41
41
|
const installation = Object.freeze({
|
|
42
42
|
status: (execution, options) => installs.status(execution, mergeOptions(options, admittedControls)),
|
|
43
43
|
result: (execution, wait, options) => installs.result(execution, wait, mergeOptions(options, admittedControls)),
|
|
44
|
+
watch: (execution, watch, options) => installs.watch(execution, watch, mergeOptions(options, admittedControls)),
|
|
44
45
|
apply: (execution, command, options) => installs.apply(execution, command, mergeOptions(options, admittedControls)),
|
|
45
46
|
abort: (execution, attempt, options) => installs.abort(execution, attempt, mergeOptions(options, admittedControls)),
|
|
46
47
|
cancel: (execution, input, options) => installs.cancel(execution, input, mergeOptions(options, admittedControls)),
|
|
@@ -134,6 +135,7 @@ function captureSchema(input) {
|
|
|
134
135
|
installation: Object.freeze({
|
|
135
136
|
status: capture(installation, 'status'),
|
|
136
137
|
result: capture(installation, 'result'),
|
|
138
|
+
watch: capture(installation, 'watch'),
|
|
137
139
|
apply: capture(installation, 'apply'),
|
|
138
140
|
abort: capture(installation, 'abort'),
|
|
139
141
|
cancel: capture(installation, 'cancel'),
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { SessionError } from '../../errors/index.js';
|
|
2
|
+
import { pause } from '../../transport/index.js';
|
|
2
3
|
/** Create one total wall-clock budget for a complete public Client Session operation. */
|
|
3
4
|
export function createSessionOperation(lifecycle, input, defaultTimeoutMs) {
|
|
4
5
|
const timeoutMs = input?.timeoutMs ?? defaultTimeoutMs;
|
|
@@ -56,22 +57,7 @@ export async function runSessionOperation(operation, task) {
|
|
|
56
57
|
}
|
|
57
58
|
/** Wait `ms` before another attempt, ending early with the operation's interruption. */
|
|
58
59
|
export function pauseSessionOperation(operation, ms) {
|
|
59
|
-
return runSessionOperation(operation, (signal) =>
|
|
60
|
-
}
|
|
61
|
-
function delay(ms, signal) {
|
|
62
|
-
return new Promise((resolve, reject) => {
|
|
63
|
-
if (signal.aborted)
|
|
64
|
-
return reject(signal.reason);
|
|
65
|
-
const timer = setTimeout(() => {
|
|
66
|
-
signal.removeEventListener('abort', abort);
|
|
67
|
-
resolve();
|
|
68
|
-
}, ms);
|
|
69
|
-
const abort = () => {
|
|
70
|
-
clearTimeout(timer);
|
|
71
|
-
reject(signal.reason);
|
|
72
|
-
};
|
|
73
|
-
signal.addEventListener('abort', abort, { once: true });
|
|
74
|
-
});
|
|
60
|
+
return runSessionOperation(operation, (signal) => pause(ms, signal));
|
|
75
61
|
}
|
|
76
62
|
function requireSessionOperation(operation) {
|
|
77
63
|
if (!operation.signal.aborted)
|
|
@@ -4,6 +4,8 @@ export interface ControlledSignal {
|
|
|
4
4
|
cancel(reason?: unknown): void;
|
|
5
5
|
close(): void;
|
|
6
6
|
}
|
|
7
|
+
/** Wait `ms`, or until `signal` aborts, which rejects with its reason. */
|
|
8
|
+
export declare function pause(ms: number, signal?: AbortSignal): Promise<void>;
|
|
7
9
|
/** One caller signal plus one transport-owned lifetime deadline. */
|
|
8
10
|
export declare function controlledSignal(parent: AbortSignal | undefined, timeoutMs: number): ControlledSignal;
|
|
9
11
|
export declare function positiveInteger(input: number, label: string): number;
|
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
/** Wait `ms`, or until `signal` aborts, which rejects with its reason. */
|
|
2
|
+
export function pause(ms, signal) {
|
|
3
|
+
return new Promise((resolve, reject) => {
|
|
4
|
+
if (signal?.aborted) {
|
|
5
|
+
reject(signal.reason);
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
const abort = () => {
|
|
9
|
+
clearTimeout(timer);
|
|
10
|
+
reject(signal.reason);
|
|
11
|
+
};
|
|
12
|
+
const timer = setTimeout(() => {
|
|
13
|
+
signal?.removeEventListener('abort', abort);
|
|
14
|
+
resolve();
|
|
15
|
+
}, ms);
|
|
16
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
17
|
+
});
|
|
18
|
+
}
|
|
1
19
|
/** One caller signal plus one transport-owned lifetime deadline. */
|
|
2
20
|
export function controlledSignal(parent, timeoutMs) {
|
|
3
21
|
const controller = new AbortController();
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export type { RequestOptions, DeploymentRetired, MissControl, StaleControl, Transport, TransportRequest, TransportResult, } from './transport.js';
|
|
2
|
-
export { asError, controlledSignal, positiveInteger, requireLive, type ControlledSignal, } from './deadline.js';
|
|
2
|
+
export { asError, controlledSignal, pause, positiveInteger, requireLive, type ControlledSignal, } from './deadline.js';
|
package/dist/transport/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { asError, controlledSignal, positiveInteger, requireLive, } from './deadline.js';
|
|
1
|
+
export { asError, controlledSignal, pause, positiveInteger, requireLive, } from './deadline.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrale-os/kernel-client",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.24",
|
|
4
4
|
"description": "Universal Client — protocol-first, transport-agnostic",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astrale",
|
|
@@ -65,13 +65,13 @@
|
|
|
65
65
|
"access": "public"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"@astrale-os/kernel-core": "1.0.0-beta.
|
|
69
|
-
"@astrale-os/kernel-dsl": "1.0.0-beta.
|
|
70
|
-
"@astrale-os/kernel-protocol": "1.0.0-beta.
|
|
68
|
+
"@astrale-os/kernel-core": "1.0.0-beta.21",
|
|
69
|
+
"@astrale-os/kernel-dsl": "1.0.0-beta.14",
|
|
70
|
+
"@astrale-os/kernel-protocol": "1.0.0-beta.21",
|
|
71
71
|
"@tanstack/pacer": "0.20.1"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
|
-
"@astrale-os/kernel-server": "1.0.0-beta.
|
|
74
|
+
"@astrale-os/kernel-server": "1.0.0-beta.21",
|
|
75
75
|
"@astrale/typescript-config": "npm:@jsr/astrale__typescript-config@~1.1.0",
|
|
76
76
|
"@types/node": "^22.0.0",
|
|
77
77
|
"typescript": "7.0.2",
|