@syntropika/tembra-sdk 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +47 -2
- package/dist/authoring/declaration.d.ts +47 -0
- package/dist/authoring/hooks.d.ts +11 -0
- package/dist/authoring/orb.d.ts +3 -0
- package/dist/authoring/types.d.ts +166 -0
- package/dist/bridge/identifiers.d.ts +207 -0
- package/dist/bridge/kernel.d.ts +117 -0
- package/dist/bridge/protocol.d.ts +181 -0
- package/dist/browser.d.ts +21 -0
- package/dist/browser.js +3282 -0
- package/dist/codec.d.ts +1 -0
- package/dist/codecs/json.d.ts +13 -0
- package/dist/codecs/msgpack.d.ts +3 -0
- package/dist/context-key.d.ts +35 -0
- package/dist/coordinator.d.ts +1 -0
- package/dist/coordinator.js +543 -0
- package/dist/embedded.d.ts +1 -0
- package/dist/embedded.js +1 -0
- package/dist/error.d.ts +13 -0
- package/dist/errors.d.ts +122 -0
- package/dist/hooks.d.ts +1 -0
- package/dist/host.d.ts +96 -0
- package/dist/host.js +3225 -0
- package/dist/identifiers.d.ts +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +833 -0
- package/dist/infrastructure/coordinator.d.ts +13 -0
- package/dist/infrastructure/manager.d.ts +23 -0
- package/dist/infrastructure/rpc/channel.d.ts +52 -0
- package/dist/infrastructure/rpc/client.d.ts +20 -0
- package/dist/infrastructure/rpc/server.d.ts +10 -0
- package/dist/infrastructure/rpc/state.d.ts +16 -0
- package/dist/kernel.d.ts +1 -0
- package/dist/manager.d.ts +1 -0
- package/dist/manager.js +390 -0
- package/dist/msgpack.d.ts +1 -0
- package/dist/msgpack.js +635 -0
- package/dist/node.d.ts +14 -0
- package/dist/node.js +3283 -0
- package/dist/orb.d.ts +1 -0
- package/dist/protocol.d.ts +1 -0
- package/dist/rpc.d.ts +3 -0
- package/dist/rpc.js +1036 -0
- package/dist/runtime/activities.d.ts +30 -0
- package/dist/runtime/calls.d.ts +22 -0
- package/dist/runtime/context.d.ts +37 -0
- package/dist/runtime/declaration.d.ts +1 -0
- package/dist/runtime/lifecycle.d.ts +28 -0
- package/dist/runtime/orbs.d.ts +39 -0
- package/dist/runtime/programs.d.ts +31 -0
- package/dist/runtime/records.d.ts +107 -0
- package/dist/runtime/references.d.ts +38 -0
- package/dist/runtime/reload.d.ts +17 -0
- package/dist/runtime/resources.d.ts +24 -0
- package/dist/runtime/scopes.d.ts +35 -0
- package/dist/runtime/session.d.ts +69 -0
- package/dist/state/binding.d.ts +28 -0
- package/dist/state/selection.d.ts +21 -0
- package/dist/state/types.d.ts +35 -0
- package/dist/state.d.ts +2 -0
- package/dist/telemetry.d.ts +23 -0
- package/dist/telemetry.js +27 -0
- package/dist/tooling/build.d.ts +3 -0
- package/dist/tooling/index.d.ts +2 -0
- package/dist/tooling/index.js +181 -0
- package/dist/tooling/resolver.d.ts +5 -0
- package/dist/tooling/typecheck.d.ts +2 -0
- package/dist/tooling/types.d.ts +29 -0
- package/dist/types.d.ts +1 -0
- package/dist/values.d.ts +23 -0
- package/package.json +103 -4
- package/src/authoring/declaration.ts +220 -0
- package/src/authoring/hooks.ts +101 -0
- package/src/authoring/orb.ts +77 -0
- package/src/authoring/types.ts +212 -0
- package/src/bridge/identifiers.ts +212 -0
- package/src/bridge/kernel.ts +117 -0
- package/src/bridge/protocol.ts +193 -0
- package/src/browser.ts +59 -0
- package/src/codec.ts +1 -0
- package/src/codecs/json.ts +147 -0
- package/src/codecs/msgpack.ts +26 -0
- package/src/context-key.ts +94 -0
- package/src/coordinator.ts +1 -0
- package/src/embedded.ts +293 -0
- package/src/error.ts +50 -0
- package/src/errors.ts +237 -0
- package/src/hooks.ts +1 -0
- package/src/host.ts +142 -0
- package/src/identifiers.ts +1 -0
- package/src/index.ts +22 -0
- package/src/infrastructure/coordinator.ts +76 -0
- package/src/infrastructure/manager.ts +77 -0
- package/src/infrastructure/rpc/channel.ts +88 -0
- package/src/infrastructure/rpc/client.ts +161 -0
- package/src/infrastructure/rpc/server.ts +201 -0
- package/src/infrastructure/rpc/state.ts +70 -0
- package/src/kernel.ts +1 -0
- package/src/manager.ts +1 -0
- package/src/msgpack.ts +1 -0
- package/src/node.ts +53 -0
- package/src/orb.ts +1 -0
- package/src/protocol.ts +1 -0
- package/src/rpc.ts +3 -0
- package/src/runtime/activities.ts +197 -0
- package/src/runtime/calls.ts +165 -0
- package/src/runtime/context.ts +113 -0
- package/src/runtime/declaration.ts +1 -0
- package/src/runtime/lifecycle.ts +227 -0
- package/src/runtime/orbs.ts +292 -0
- package/src/runtime/programs.ts +274 -0
- package/src/runtime/records.ts +127 -0
- package/src/runtime/references.ts +398 -0
- package/src/runtime/reload.ts +161 -0
- package/src/runtime/resources.ts +157 -0
- package/src/runtime/scopes.ts +210 -0
- package/src/runtime/session.ts +181 -0
- package/src/state/binding.ts +157 -0
- package/src/state/selection.ts +122 -0
- package/src/state/types.ts +47 -0
- package/src/state.ts +2 -0
- package/src/telemetry.ts +45 -0
- package/src/tooling/build.ts +83 -0
- package/src/tooling/index.ts +8 -0
- package/src/tooling/resolver.ts +95 -0
- package/src/tooling/typecheck.ts +32 -0
- package/src/tooling/types.ts +32 -0
- package/src/types.ts +1 -0
- package/src/values.ts +85 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import type { Completion } from "../authoring/types.ts";
|
|
2
|
+
import { Code } from "../bridge/identifiers.ts";
|
|
3
|
+
import type { Invocation } from "../bridge/kernel.ts";
|
|
4
|
+
import { KernelOp, Outcome } from "../bridge/protocol.ts";
|
|
5
|
+
import { TembraError } from "../error.ts";
|
|
6
|
+
import { Errors } from "../errors.ts";
|
|
7
|
+
import { Context, context } from "./context.ts";
|
|
8
|
+
import type { Environment, ReferenceRecord } from "./records.ts";
|
|
9
|
+
import type { Session } from "./session.ts";
|
|
10
|
+
|
|
11
|
+
/** Execute JS calls. Rust controls admission and physical lifetime. */
|
|
12
|
+
export class Calls {
|
|
13
|
+
/** Connect JS callback execution to the session kernel. */
|
|
14
|
+
constructor(readonly session: Session) {}
|
|
15
|
+
|
|
16
|
+
/** Lazily mirror root cancellation into an AbortSignal. */
|
|
17
|
+
signal(root: number): AbortSignal {
|
|
18
|
+
const session = this.session;
|
|
19
|
+
let controller = session.controllers.get(root);
|
|
20
|
+
if (!controller) {
|
|
21
|
+
controller = new AbortController();
|
|
22
|
+
try {
|
|
23
|
+
session.kernel.command(KernelOp.CheckRoot, { root });
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (!(error instanceof TembraError) || error.code !== Code.Cancelled) throw error;
|
|
26
|
+
controller.abort(error);
|
|
27
|
+
}
|
|
28
|
+
session.controllers.set(root, controller);
|
|
29
|
+
}
|
|
30
|
+
return controller.signal;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Revoke claims and cancellation mirrors when either language ends a root. */
|
|
34
|
+
releaseRoot(root: number): void {
|
|
35
|
+
for (const reference of this.session.rootReferences.get(root) ?? [])
|
|
36
|
+
this.session.handles.release(reference);
|
|
37
|
+
this.session.rootReferences.delete(root);
|
|
38
|
+
this.session.controllers.delete(root);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Release root-owned references after the callback settles. */
|
|
42
|
+
async root<T>(
|
|
43
|
+
operation: (context: Context) => Completion<T>,
|
|
44
|
+
cleanup = false,
|
|
45
|
+
issuedRoot?: number,
|
|
46
|
+
principal = this.session.principal,
|
|
47
|
+
): Promise<T> {
|
|
48
|
+
const session = this.session;
|
|
49
|
+
const root =
|
|
50
|
+
issuedRoot ??
|
|
51
|
+
session.kernel.command<number>(cleanup ? KernelOp.CleanupRoot : KernelOp.Root, { principal });
|
|
52
|
+
const ctx = new Context(session, root);
|
|
53
|
+
try {
|
|
54
|
+
return await operation(ctx);
|
|
55
|
+
} finally {
|
|
56
|
+
this.releaseRoot(root);
|
|
57
|
+
session.options.native?.rootEnded?.(root);
|
|
58
|
+
if (!session.lifecycle.closedDiagnostics) session.kernel.command(KernelOp.EndRoot, { root });
|
|
59
|
+
session.controllers.delete(root);
|
|
60
|
+
session.lifecycle.flush();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Execute one admitted service call and retain physical ownership. */
|
|
65
|
+
async invoke(
|
|
66
|
+
reference: ReferenceRecord,
|
|
67
|
+
method: string,
|
|
68
|
+
arguments_: unknown[],
|
|
69
|
+
ctx: Context,
|
|
70
|
+
): Promise<unknown> {
|
|
71
|
+
const session = this.session;
|
|
72
|
+
context(ctx, session);
|
|
73
|
+
if (reference.released) throw new TembraError(Errors.Released.Reference);
|
|
74
|
+
await reference.ready;
|
|
75
|
+
const admission = session.options.native?.admit?.(reference.id, method, ctx);
|
|
76
|
+
let invocation: Invocation;
|
|
77
|
+
if (admission) invocation = admission.call;
|
|
78
|
+
else {
|
|
79
|
+
ctx.check();
|
|
80
|
+
invocation = session.kernel.command<Invocation>(KernelOp.BeginCall, {
|
|
81
|
+
reference: reference.id,
|
|
82
|
+
root: ctx.root,
|
|
83
|
+
context: ctx.frame || undefined,
|
|
84
|
+
method,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const entered = new Context(session, ctx.root, invocation, ctx.frame);
|
|
88
|
+
const physical = session.track(
|
|
89
|
+
(async () => {
|
|
90
|
+
const sampled = session.telemetry?.sample();
|
|
91
|
+
const started = sampled ? session.now() : 0;
|
|
92
|
+
let outcome: Outcome = Outcome.Ok;
|
|
93
|
+
try {
|
|
94
|
+
const native = session.options.native;
|
|
95
|
+
if (native && (admission?.native ?? native.hasOrb(invocation.orb)))
|
|
96
|
+
return await native.invoke(invocation, arguments_, entered);
|
|
97
|
+
const object = session.objects.get(invocation.object);
|
|
98
|
+
const environment = object?.versions.get(invocation.orb);
|
|
99
|
+
const handle =
|
|
100
|
+
environment?.orb.definition.services?.[reference.address.service]?.methods[method];
|
|
101
|
+
if (!environment || !handle) throw new TembraError(Errors.Gone.Implementation);
|
|
102
|
+
return await handle(environment.value, entered, ...arguments_);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
outcome = Outcome.Error;
|
|
105
|
+
throw error;
|
|
106
|
+
} finally {
|
|
107
|
+
if (sampled)
|
|
108
|
+
session.telemetry!.record({
|
|
109
|
+
service: reference.address.service,
|
|
110
|
+
method,
|
|
111
|
+
root: invocation.root,
|
|
112
|
+
codeVersion: invocation.codeVersion,
|
|
113
|
+
duration: session.now() - started,
|
|
114
|
+
outcome,
|
|
115
|
+
});
|
|
116
|
+
session.lifecycle.finishCall(invocation.id);
|
|
117
|
+
}
|
|
118
|
+
})(),
|
|
119
|
+
);
|
|
120
|
+
return this.observe(physical, ctx.signal);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Fresh callback roots inherit owner identity; supplied roots retain the caller's authority. */
|
|
124
|
+
async owned<T>(
|
|
125
|
+
environment: Environment,
|
|
126
|
+
operation: number,
|
|
127
|
+
handle: (ctx: Context) => Completion<T>,
|
|
128
|
+
root?: number | Context,
|
|
129
|
+
): Promise<T> {
|
|
130
|
+
const session = this.session;
|
|
131
|
+
const execute = async (ctx: Context) => {
|
|
132
|
+
const invocation = session.kernel.command<Invocation>(KernelOp.BeginOperation, {
|
|
133
|
+
orb: environment.orb.info.id,
|
|
134
|
+
root: ctx.root,
|
|
135
|
+
context: ctx.frame || undefined,
|
|
136
|
+
operation,
|
|
137
|
+
...(environment.object ? { object: environment.object.id } : {}),
|
|
138
|
+
});
|
|
139
|
+
try {
|
|
140
|
+
return await handle(new Context(session, ctx.root, invocation, ctx.frame));
|
|
141
|
+
} finally {
|
|
142
|
+
session.kernel.command(KernelOp.FinishCall, { call: invocation.id });
|
|
143
|
+
session.lifecycle.flush();
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
return root === undefined
|
|
147
|
+
? this.root(execute, false, undefined, session.principalFor(environment.object?.id ?? 0))
|
|
148
|
+
: execute(typeof root === "number" ? new Context(session, root) : root);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Stop waiting on cancellation while physical work remains tracked. */
|
|
152
|
+
async observe<T>(physical: Promise<T>, signal: AbortSignal): Promise<T> {
|
|
153
|
+
if (signal.aborted) throw signal.reason;
|
|
154
|
+
let abort!: () => void;
|
|
155
|
+
const cancelled = new Promise<never>((_, reject) => {
|
|
156
|
+
abort = () => reject(signal.reason ?? new TembraError(Errors.Cancelled.Root));
|
|
157
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
158
|
+
});
|
|
159
|
+
try {
|
|
160
|
+
return await Promise.race([physical, cancelled]);
|
|
161
|
+
} finally {
|
|
162
|
+
signal.removeEventListener("abort", abort);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { ContextKey, ContextKind, readContextValue } from "../context-key.ts";
|
|
2
|
+
import { KernelOp } from "../bridge/protocol.ts";
|
|
3
|
+
import { Errors } from "../errors.ts";
|
|
4
|
+
|
|
5
|
+
import type { AcquisitionOptions, DynamicReference } from "../authoring/types.ts";
|
|
6
|
+
import type { Invocation } from "../bridge/kernel.ts";
|
|
7
|
+
import { TembraError } from "../error.ts";
|
|
8
|
+
import type { Session } from "./session.ts";
|
|
9
|
+
|
|
10
|
+
const issued = new WeakMap<object, Context>();
|
|
11
|
+
|
|
12
|
+
/** An issued operation scope. Keeping this object does not extend its root or invocation. */
|
|
13
|
+
export class Context {
|
|
14
|
+
/** Issue a context for an existing root and optional admitted call. */
|
|
15
|
+
constructor(
|
|
16
|
+
readonly session: Session,
|
|
17
|
+
readonly root: number,
|
|
18
|
+
readonly invocation?: Invocation,
|
|
19
|
+
readonly frame: number = 0,
|
|
20
|
+
) {
|
|
21
|
+
issued.set(this, this);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Derive one binding without changing identity or lifetime. */
|
|
25
|
+
with<T>(key: ContextKey<T>, value: T): Context {
|
|
26
|
+
const source = key.source(value, this);
|
|
27
|
+
const frame = this.session.kernel.command<number>(KernelOp.ContextDerive, {
|
|
28
|
+
root: this.root,
|
|
29
|
+
context: this.frame,
|
|
30
|
+
call: this.invocation?.id,
|
|
31
|
+
key,
|
|
32
|
+
...(key.kind === ContextKind.Opaque ? { source } : { value }),
|
|
33
|
+
});
|
|
34
|
+
return new Context(this.session, this.root, this.invocation, frame);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Read the nearest binding of this typed key. */
|
|
38
|
+
value<T>(key: ContextKey<T>): T | undefined {
|
|
39
|
+
return readContextValue(this, key, this.frame);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Export only explicitly allowed scalar metadata. */
|
|
43
|
+
exportValues(keys: readonly ContextKey<unknown>[]): Record<string, unknown> {
|
|
44
|
+
const result: Record<string, unknown> = {};
|
|
45
|
+
for (const key of keys) {
|
|
46
|
+
if (key.kind === ContextKind.Opaque)
|
|
47
|
+
throw new TypeError("Local opaque references cannot be exported");
|
|
48
|
+
const value = this.value(key);
|
|
49
|
+
if (value !== undefined) result[`${key.name}@${key.version}`] = value;
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Read the root identity from the kernel. */
|
|
55
|
+
get principal(): string {
|
|
56
|
+
return this.session.kernel.command<string>(KernelOp.RootPrincipal, { root: this.root });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Read the shared cancellation signal for this root. */
|
|
60
|
+
get signal(): AbortSignal {
|
|
61
|
+
return this.session.calls.signal(this.root);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Reject a cancelled root or expired invocation. */
|
|
65
|
+
check(): void {
|
|
66
|
+
this.session.kernel.command(KernelOp.CheckContext, {
|
|
67
|
+
root: this.root,
|
|
68
|
+
call: this.invocation?.id,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Cancel the root and dispatch its cancellation actions. */
|
|
73
|
+
cancel(): void {
|
|
74
|
+
this.session.kernel.command(KernelOp.CancelRoot, { root: this.root });
|
|
75
|
+
this.session.lifecycle.flush();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Acquire a reference whose claim ends with this root. */
|
|
79
|
+
get(
|
|
80
|
+
service: string,
|
|
81
|
+
key: string | readonly string[],
|
|
82
|
+
options: AcquisitionOptions = {},
|
|
83
|
+
): Promise<DynamicReference> {
|
|
84
|
+
this.check();
|
|
85
|
+
return this.session.handles.get(service, key, options, this);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Read a named host resource while the context remains valid. */
|
|
89
|
+
resource<T>(name: string): import("../authoring/types.ts").Resource<T> {
|
|
90
|
+
this.check();
|
|
91
|
+
return this.session.scopes.hostResource<T>(name);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Require a context issued by the same host without checking cancellation or expiry. */
|
|
96
|
+
export function issuedContext(value: unknown, session: Session): Context {
|
|
97
|
+
if (typeof value !== "object" || value === null || issued.get(value)?.session !== session) {
|
|
98
|
+
throw new TembraError(Errors.Forbidden.Context, "An issued context from this host is required");
|
|
99
|
+
}
|
|
100
|
+
return issued.get(value)!;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Require a live context issued by the same host. */
|
|
104
|
+
export function context(value: unknown, session: Session): Context {
|
|
105
|
+
const result = issuedContext(value, session);
|
|
106
|
+
result.check();
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Recognize an issued context without extending its lifetime. */
|
|
111
|
+
export function isContext(value: unknown): value is Context {
|
|
112
|
+
return typeof value === "object" && value !== null && issued.has(value);
|
|
113
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../authoring/declaration.ts";
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import type { Action, Diagnostics } from "../bridge/kernel.ts";
|
|
2
|
+
import { ActionType, KernelOp, OwnerKind } from "../bridge/protocol.ts";
|
|
3
|
+
import { TembraError } from "../error.ts";
|
|
4
|
+
import { Errors } from "../errors.ts";
|
|
5
|
+
import type { Session } from "./session.ts";
|
|
6
|
+
|
|
7
|
+
/** Dispatch core actions and wait for physical shutdown. */
|
|
8
|
+
export class Lifecycle {
|
|
9
|
+
/** Connect kernel actions to JS cleanup and scheduling. */
|
|
10
|
+
constructor(readonly session: Session) {}
|
|
11
|
+
|
|
12
|
+
/** Read final counts after kernel storage has been released. */
|
|
13
|
+
get closedDiagnostics(): ReturnType<Lifecycle["diagnostics"]> | undefined {
|
|
14
|
+
return this.#finalDiagnostics;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
#closed?: Promise<void>;
|
|
18
|
+
#flushing = false;
|
|
19
|
+
#linger: ReturnType<typeof setTimeout> | undefined;
|
|
20
|
+
#lingerDeadline: number | undefined;
|
|
21
|
+
#finalDiagnostics: ReturnType<Lifecycle["diagnostics"]> | undefined;
|
|
22
|
+
|
|
23
|
+
/** Drain kernel actions without re-entering the dispatcher. */
|
|
24
|
+
flush(initial?: Action[]): void {
|
|
25
|
+
if (this.#flushing) return;
|
|
26
|
+
this.#flushing = true;
|
|
27
|
+
try {
|
|
28
|
+
for (;;) {
|
|
29
|
+
const actions = initial ?? this.session.kernel.command<Action[]>(KernelOp.Drain);
|
|
30
|
+
initial = undefined;
|
|
31
|
+
if (!actions.length) break;
|
|
32
|
+
for (const action of actions) {
|
|
33
|
+
try {
|
|
34
|
+
this.action(action);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
this.session.report(error);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
} finally {
|
|
41
|
+
this.#flushing = false;
|
|
42
|
+
this.scheduleLinger();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Compose kernel finish bookkeeping through the embedded adapter when available. */
|
|
47
|
+
finishCall(call: number): void {
|
|
48
|
+
const session = this.session;
|
|
49
|
+
if (!this.#flushing && session.options.native?.finish) {
|
|
50
|
+
const batch = session.options.native.finish(call, session.now());
|
|
51
|
+
if (!batch.actions.length) {
|
|
52
|
+
this.scheduleLinger(batch.deadline);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
this.flush(batch.actions);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
session.kernel.command(KernelOp.Tick, { now: session.now() });
|
|
59
|
+
session.kernel.command(KernelOp.FinishCall, { call });
|
|
60
|
+
this.flush();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Execute one action using its owning JS module. */
|
|
64
|
+
private action(action: Action): void {
|
|
65
|
+
const session = this.session;
|
|
66
|
+
const nativeHandled = session.options.native?.notify?.(action);
|
|
67
|
+
if (
|
|
68
|
+
nativeHandled &&
|
|
69
|
+
!(action.type === ActionType.ProgramComplete && session.programs.pending(action.id))
|
|
70
|
+
)
|
|
71
|
+
return;
|
|
72
|
+
switch (action.type) {
|
|
73
|
+
case ActionType.Execute:
|
|
74
|
+
case ActionType.CancelEffects:
|
|
75
|
+
case ActionType.ProgramPrepared:
|
|
76
|
+
case ActionType.ProgramComplete:
|
|
77
|
+
session.programs.dispatch(action);
|
|
78
|
+
break;
|
|
79
|
+
case ActionType.DropValue:
|
|
80
|
+
session.values.drop(action.id);
|
|
81
|
+
session.options.native?.dropValue?.(action.id);
|
|
82
|
+
break;
|
|
83
|
+
case ActionType.CancelRoot:
|
|
84
|
+
session.controllers.get(action.id)?.abort(new TembraError(Errors.Cancelled.Root));
|
|
85
|
+
break;
|
|
86
|
+
case ActionType.CancelTask:
|
|
87
|
+
session.activities.cancel(action.id);
|
|
88
|
+
break;
|
|
89
|
+
case ActionType.Deliver:
|
|
90
|
+
session.activities.deliver(action.id, action.payload!, action.operation!);
|
|
91
|
+
break;
|
|
92
|
+
case ActionType.DetachSubscription:
|
|
93
|
+
session.activities.detach(action.id);
|
|
94
|
+
break;
|
|
95
|
+
case ActionType.SubscriptionFailed:
|
|
96
|
+
session.activities.failed(action.id);
|
|
97
|
+
break;
|
|
98
|
+
case ActionType.Dispose:
|
|
99
|
+
session.track(this.dispose(action.id), true);
|
|
100
|
+
break;
|
|
101
|
+
case ActionType.CloseResource: {
|
|
102
|
+
// Resource callbacks execute only in response to the engine's ordered program effect.
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case ActionType.RetireOrb:
|
|
106
|
+
session.handles.retire(action.id);
|
|
107
|
+
session.orbs.retire(action.id);
|
|
108
|
+
break;
|
|
109
|
+
case ActionType.State: {
|
|
110
|
+
const object = session.objects.get(action.id);
|
|
111
|
+
if (!object?.binding) break;
|
|
112
|
+
const snapshot = session.snapshot(action.id);
|
|
113
|
+
// Local updates already staged their selectors. Foreign Rust updates enter JS only here.
|
|
114
|
+
if (!session.values.has(snapshot.value)) {
|
|
115
|
+
const candidate = session.value<any>(snapshot.value);
|
|
116
|
+
session.publish(object.binding.adopt(object.binding.prepare(candidate), candidate));
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Wait for object activation and cleanup before releasing its binding. */
|
|
124
|
+
private async dispose(id: number): Promise<void> {
|
|
125
|
+
const session = this.session;
|
|
126
|
+
const object = session.objects.get(id);
|
|
127
|
+
if (!object) {
|
|
128
|
+
// Native contexts can acquire objects before any JS reference mirrors them.
|
|
129
|
+
await session.options.native?.dispose(id);
|
|
130
|
+
session.kernel.command(KernelOp.Disposed, { object: id });
|
|
131
|
+
this.flush();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
object.disposing ??= (async () => {
|
|
135
|
+
const errors: unknown[] = [];
|
|
136
|
+
await object.activation.catch(() => {});
|
|
137
|
+
const scopes = session.kernel.command<number[]>(KernelOp.ScopesForOwner, {
|
|
138
|
+
owner: { kind: OwnerKind.Object, id },
|
|
139
|
+
});
|
|
140
|
+
try {
|
|
141
|
+
await session.programs.cleanupScopes(scopes);
|
|
142
|
+
} catch (error) {
|
|
143
|
+
errors.push(error);
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
if (session.options.native?.hasOrb(object.orb)) await session.options.native.dispose(id);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
errors.push(error);
|
|
149
|
+
}
|
|
150
|
+
object.binding?.close();
|
|
151
|
+
session.kernel.command(KernelOp.Disposed, { object: id });
|
|
152
|
+
session.objects.delete(id);
|
|
153
|
+
this.flush();
|
|
154
|
+
if (errors.length) throw new AggregateError(errors, "Object cleanup failed");
|
|
155
|
+
})();
|
|
156
|
+
return object.disposing;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Keep one timer for the kernel's earliest deadline. */
|
|
160
|
+
private scheduleLinger(known?: number | null): void {
|
|
161
|
+
const session = this.session;
|
|
162
|
+
if (session.closing) return;
|
|
163
|
+
const deadline =
|
|
164
|
+
(known === undefined
|
|
165
|
+
? session.kernel.command<number | null>(KernelOp.NextDeadline)
|
|
166
|
+
: known) ?? undefined;
|
|
167
|
+
if (deadline === this.#lingerDeadline) return;
|
|
168
|
+
if (this.#linger !== undefined) clearTimeout(this.#linger);
|
|
169
|
+
this.#linger = undefined;
|
|
170
|
+
this.#lingerDeadline = deadline;
|
|
171
|
+
if (deadline === undefined) return;
|
|
172
|
+
this.#linger = setTimeout(
|
|
173
|
+
() => {
|
|
174
|
+
if (this.#lingerDeadline !== deadline) return;
|
|
175
|
+
this.#linger = undefined;
|
|
176
|
+
this.#lingerDeadline = undefined;
|
|
177
|
+
session.kernel.command(KernelOp.Tick, { now: session.now() });
|
|
178
|
+
this.flush();
|
|
179
|
+
},
|
|
180
|
+
Math.max(0, deadline - session.now()),
|
|
181
|
+
);
|
|
182
|
+
this.#linger.unref?.();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Combine kernel counts with retained JS values and jobs. */
|
|
186
|
+
diagnostics(): Diagnostics & { values: number; pendingJobs: number } {
|
|
187
|
+
const session = this.session;
|
|
188
|
+
if (this.#finalDiagnostics) return this.#finalDiagnostics;
|
|
189
|
+
return {
|
|
190
|
+
...session.kernel.command<Diagnostics>(KernelOp.Diagnostics),
|
|
191
|
+
values: session.values.size,
|
|
192
|
+
pendingJobs: session.jobs.size,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Wait for physical cleanup before releasing the platform binding. */
|
|
197
|
+
close(): Promise<void> {
|
|
198
|
+
const session = this.session;
|
|
199
|
+
this.#closed ??= (async () => {
|
|
200
|
+
session.closing = true;
|
|
201
|
+
clearTimeout(this.#linger);
|
|
202
|
+
this.#linger = undefined;
|
|
203
|
+
this.#lingerDeadline = undefined;
|
|
204
|
+
session.kernel.command(KernelOp.Close);
|
|
205
|
+
await session.replacements.cancel();
|
|
206
|
+
this.flush();
|
|
207
|
+
for (;;) {
|
|
208
|
+
await session.options.native?.settled?.();
|
|
209
|
+
this.flush();
|
|
210
|
+
const pending = [...session.jobs];
|
|
211
|
+
if (!pending.length) break;
|
|
212
|
+
await Promise.allSettled(pending);
|
|
213
|
+
this.flush();
|
|
214
|
+
}
|
|
215
|
+
session.kernel.command(KernelOp.FinishClose);
|
|
216
|
+
session.values.clear();
|
|
217
|
+
session.handles.clear();
|
|
218
|
+
session.orbs.records.clear();
|
|
219
|
+
session.scopes.clear();
|
|
220
|
+
this.#finalDiagnostics = this.diagnostics();
|
|
221
|
+
session.kernel.port.free?.();
|
|
222
|
+
if (session.cleanupErrors.length)
|
|
223
|
+
throw new AggregateError(session.cleanupErrors, "Host cleanup failed");
|
|
224
|
+
})();
|
|
225
|
+
return this.#closed;
|
|
226
|
+
}
|
|
227
|
+
}
|