Skip to content

Commit 98a7a51

Browse files
committed
refactor(host-kit): keep one entered runtime-state store for the whole process (#3794)
Each module used to declare its own async-local store and expose a handle the daemon had to aggregate. Modules now declare slot keys on one store: runtimeSlot reads a piece of state from the store the current runtime entered, and createRuntimeStateHandle makes the one handle a runtime enters and disposes. The standalone fallback stays, so a client process or a unit test reads its own state without composing a runtime. request-cancel drops its handle factory; the runtime's handle owns the slot and its disposer.
1 parent b5f47e7 commit 98a7a51

3 files changed

Lines changed: 25 additions & 38 deletions

File tree

‎packages/host-kit/src/internal/request-cancel.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { AppError, createRequestCanceledError } from '@agent-device/kernel/errors';
2-
import { defineRuntimeSlots, type RuntimeStateHandle } from '../runtime-slots.ts';
2+
import { runtimeSlot } from '../runtime-slots.ts';
33

44
type RequestCancelState = {
55
canceledRequestIds: Set<string>;
66
requestAbortControllers: Map<string, AbortController>;
77
};
88

9-
const requestCancelRuntime = defineRuntimeSlots();
109
const REQUEST_CANCEL_SLOT = Symbol('requestCancel');
1110
const requestCancelState = (): RequestCancelState =>
12-
requestCancelRuntime.slot(
11+
runtimeSlot(
1312
REQUEST_CANCEL_SLOT,
1413
(): RequestCancelState => ({
1514
canceledRequestIds: new Set<string>(),
@@ -22,9 +21,6 @@ const requestCancelState = (): RequestCancelState =>
2221
const canceledRequestIds = () => requestCancelState().canceledRequestIds;
2322
const requestAbortControllers = () => requestCancelState().requestAbortControllers;
2423

25-
export const createRequestCancelRuntimeState = (): RuntimeStateHandle =>
26-
requestCancelRuntime.createHandle();
27-
2824
export type RequestAbortRegistration = {
2925
requestId: string;
3026
controller: AbortController;

‎packages/host-kit/src/request.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export {
22
cancelAllRequests,
33
clearRequestAbortRegistration,
4-
createRequestCancelRuntimeState,
54
clearRequestCanceled,
65
getRequestSignal,
76
isRequestCanceled,
Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
11
import { AsyncLocalStorage } from 'node:async_hooks';
22

33
/**
4-
* What one daemon runtime owns of a kind: a module (or a package) keeps its state in slots of an
5-
* async-local store the runtime enters, so two runtimes in one process never share it. Outside a
6-
* runtime (a client process, a unit test) the store's own standalone state answers.
4+
* What one daemon runtime owns: a handle over the process's one slot store. The runtime enters
5+
* it once (through {@link RuntimeStateHandle.run}) and disposes it once at close, so two runtimes
6+
* in one process never share per-runtime state. Outside a runtime (a client process, a unit
7+
* test) the store's own standalone slots answer.
78
*/
89
export type RuntimeStateHandle = Readonly<{
910
/** Runs `fn` as the runtime that owns this state. */
1011
run<T>(fn: () => T): T;
11-
/** Binds this state for the rest of the current async context, after the runtime is composed. */
12-
enter(): void;
1312
/** Releases what the state holds: each slot's disposer runs, then the slots are forgotten. */
1413
dispose(): void;
1514
}>;
1615

17-
export type RuntimeSlots = Readonly<{
18-
/** One piece of state the current runtime owns, created on first use. */
19-
slot<T>(key: symbol, create: () => T, dispose?: (value: T) => void): T;
20-
/** The state of one new runtime: enter it before anything reads a slot. */
21-
createHandle(): RuntimeStateHandle;
22-
}>;
23-
2416
type Slot = { value: unknown; dispose: ((value: never) => void) | undefined };
2517

26-
/** The single owner of the "entered store, else standalone store" pattern; callers declare slots. */
27-
export function defineRuntimeSlots(): RuntimeSlots {
28-
const storage = new AsyncLocalStorage<Map<symbol, Slot>>();
29-
const standalone = new Map<symbol, Slot>();
18+
// One store per process: modules declare only slot keys, and each runtime enters exactly one
19+
// handle of this store.
20+
const storage = new AsyncLocalStorage<Map<symbol, Slot>>();
21+
const standalone = new Map<symbol, Slot>();
22+
23+
/** One piece of state the current runtime owns, created on first use. */
24+
export function runtimeSlot<T>(key: symbol, create: () => T, dispose?: (value: T) => void): T {
25+
const slots = storage.getStore() ?? standalone;
26+
if (!slots.has(key)) slots.set(key, { value: create(), dispose });
27+
return slots.get(key)!.value as never;
28+
}
29+
30+
/** The state of one new runtime: enter it before anything reads a slot. */
31+
export function createRuntimeStateHandle(): RuntimeStateHandle {
32+
const owned = new Map<symbol, Slot>();
3033
return {
31-
slot: (key, create, dispose) => {
32-
const slots = storage.getStore() ?? standalone;
33-
if (!slots.has(key)) slots.set(key, { value: create(), dispose });
34-
return slots.get(key)!.value as never;
35-
},
36-
createHandle: () => {
37-
const owned = new Map<symbol, Slot>();
38-
return {
39-
run: (fn) => storage.run(owned, fn),
40-
enter: () => storage.enterWith(owned),
41-
dispose: () => {
42-
for (const { value, dispose } of owned.values()) dispose?.(value as never);
43-
owned.clear();
44-
},
45-
};
34+
run: (fn) => storage.run(owned, fn),
35+
dispose: () => {
36+
for (const { value, dispose } of owned.values()) dispose?.(value as never);
37+
owned.clear();
4638
},
4739
};
4840
}

0 commit comments

Comments
 (0)