@nylorun/runtime 0.10.0-beta → 0.11.0-beta

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 (190) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/README.md +53 -51
  3. package/dist/adapters/execution/restate.d.ts +130 -0
  4. package/dist/adapters/execution/restate.js +442 -0
  5. package/dist/adapters/sandbox/virtual.js +1 -1
  6. package/dist/adapters/streams/s2.d.ts +18 -0
  7. package/dist/adapters/streams/s2.js +361 -0
  8. package/dist/core/executors.d.ts +4 -1
  9. package/dist/core/executors.js +20 -1
  10. package/dist/core/flow-host.d.ts +93 -132
  11. package/dist/core/flow-host.js +282 -547
  12. package/dist/core/sandbox-routes.d.ts +8 -3
  13. package/dist/core/sandbox-routes.js +10 -10
  14. package/dist/execution/memory.d.ts +54 -0
  15. package/dist/execution/memory.js +217 -0
  16. package/dist/execution/types.d.ts +125 -0
  17. package/dist/execution/types.js +52 -0
  18. package/dist/host/config-for.d.ts +2 -1
  19. package/dist/host/config-for.js +3 -21
  20. package/dist/host/config.d.ts +0 -8
  21. package/dist/host/create-host.d.ts +36 -1
  22. package/dist/host/create-host.js +48 -24
  23. package/dist/host/execution.d.ts +61 -0
  24. package/dist/host/execution.js +69 -0
  25. package/dist/host/http.d.ts +4 -0
  26. package/dist/host/http.js +6 -0
  27. package/dist/host/index.d.ts +3 -1
  28. package/dist/host/index.js +2 -0
  29. package/dist/host/main.js +106 -47
  30. package/dist/host/stack-config.d.ts +72 -0
  31. package/dist/host/stack-config.js +193 -0
  32. package/dist/index.d.ts +1 -1
  33. package/dist/infra/database.d.ts +16 -0
  34. package/dist/infra/database.js +31 -0
  35. package/dist/infra/execution.d.ts +39 -0
  36. package/dist/infra/execution.js +68 -0
  37. package/dist/infra/index.d.ts +42 -0
  38. package/dist/infra/index.js +56 -0
  39. package/dist/infra/readiness.d.ts +33 -0
  40. package/dist/infra/readiness.js +53 -0
  41. package/dist/infra/streams.d.ts +17 -0
  42. package/dist/infra/streams.js +33 -0
  43. package/dist/infra/workers.d.ts +39 -0
  44. package/dist/infra/workers.js +31 -0
  45. package/dist/model/pi-model.d.ts +2 -2
  46. package/dist/model/pi-model.js +5 -5
  47. package/dist/sandbox/manager.d.ts +26 -8
  48. package/dist/sandbox/manager.js +95 -47
  49. package/dist/sandbox/select.d.ts +1 -1
  50. package/dist/sandbox/select.js +4 -5
  51. package/dist/sandbox/types.d.ts +2 -2
  52. package/dist/store/canonical.d.ts +5 -0
  53. package/dist/store/canonical.js +17 -0
  54. package/dist/store/cursor.d.ts +9 -0
  55. package/dist/store/cursor.js +18 -0
  56. package/dist/store/memory.d.ts +57 -0
  57. package/dist/store/memory.js +643 -0
  58. package/dist/store/ownership.d.ts +19 -0
  59. package/dist/store/ownership.js +33 -0
  60. package/dist/store/postgres/connect.d.ts +25 -0
  61. package/dist/store/postgres/connect.js +25 -0
  62. package/dist/store/postgres/locking.d.ts +16 -0
  63. package/dist/store/postgres/locking.js +22 -0
  64. package/dist/store/postgres/migrations/001_initial.d.ts +27 -0
  65. package/dist/store/postgres/migrations/001_initial.js +196 -0
  66. package/dist/store/postgres/migrations/index.d.ts +49 -0
  67. package/dist/store/postgres/migrations/index.js +75 -0
  68. package/dist/store/postgres/names.d.ts +11 -0
  69. package/dist/store/postgres/names.js +42 -0
  70. package/dist/store/postgres/store.d.ts +11 -0
  71. package/dist/store/postgres/store.js +790 -0
  72. package/dist/store/postgres/tenants.d.ts +90 -0
  73. package/dist/store/postgres/tenants.js +207 -0
  74. package/dist/store/types.d.ts +440 -0
  75. package/dist/store/types.js +10 -0
  76. package/dist/streams/basin.d.ts +23 -0
  77. package/dist/streams/basin.js +51 -0
  78. package/dist/streams/memory.d.ts +25 -0
  79. package/dist/streams/memory.js +124 -0
  80. package/dist/streams/relay.d.ts +37 -0
  81. package/dist/streams/relay.js +254 -0
  82. package/dist/streams/types.d.ts +115 -0
  83. package/dist/streams/types.js +86 -0
  84. package/dist/tenant/actions.d.ts +153 -0
  85. package/dist/tenant/actions.js +191 -0
  86. package/dist/tenant/advance.d.ts +15 -0
  87. package/dist/tenant/advance.js +439 -0
  88. package/dist/tenant/auth.d.ts +11 -0
  89. package/dist/tenant/auth.js +34 -0
  90. package/dist/tenant/commands.d.ts +3 -0
  91. package/dist/tenant/commands.js +384 -0
  92. package/dist/tenant/context.d.ts +140 -0
  93. package/dist/tenant/context.js +40 -0
  94. package/dist/tenant/effects.d.ts +20 -0
  95. package/dist/tenant/effects.js +539 -0
  96. package/dist/tenant/envelope.d.ts +0 -3
  97. package/dist/tenant/envelope.js +2 -34
  98. package/dist/tenant/ephemeral.d.ts +15 -4
  99. package/dist/tenant/ephemeral.js +106 -38
  100. package/dist/tenant/http.d.ts +32 -0
  101. package/dist/tenant/http.js +51 -0
  102. package/dist/tenant/live.d.ts +92 -0
  103. package/dist/tenant/live.js +290 -0
  104. package/dist/tenant/model-setting.d.ts +16 -0
  105. package/dist/tenant/model-setting.js +12 -0
  106. package/dist/tenant/module.d.ts +29 -8
  107. package/dist/tenant/module.js +162 -158
  108. package/dist/tenant/paths.d.ts +12 -8
  109. package/dist/tenant/paths.js +38 -12
  110. package/dist/tenant/principals.d.ts +9 -13
  111. package/dist/tenant/principals.js +31 -38
  112. package/dist/tenant/quarantine-error.d.ts +1 -1
  113. package/dist/tenant/quarantine-error.js +2 -8
  114. package/dist/tenant/quarantine.d.ts +0 -8
  115. package/dist/tenant/quarantine.js +8 -30
  116. package/dist/tenant/reset.d.ts +7 -6
  117. package/dist/tenant/reset.js +7 -51
  118. package/dist/tenant/routes-tenant.d.ts +12 -0
  119. package/dist/tenant/routes-tenant.js +126 -0
  120. package/dist/tenant/routes.d.ts +9 -0
  121. package/dist/tenant/routes.js +142 -0
  122. package/dist/tenant/runtime.d.ts +59 -58
  123. package/dist/tenant/runtime.js +243 -2201
  124. package/dist/tenant/scheduler.d.ts +25 -0
  125. package/dist/tenant/scheduler.js +84 -0
  126. package/dist/tenant/session.d.ts +60 -0
  127. package/dist/tenant/session.js +94 -0
  128. package/dist/tenant/sessions.d.ts +34 -0
  129. package/dist/tenant/sessions.js +149 -0
  130. package/dist/tenant/status.d.ts +13 -4
  131. package/dist/tenant/status.js +76 -41
  132. package/dist/tenant/store-memory.d.ts +9 -12
  133. package/dist/tenant/store-memory.js +79 -68
  134. package/dist/tenant/store-pg.d.ts +15 -0
  135. package/dist/tenant/store-pg.js +140 -0
  136. package/dist/tenant/streams.d.ts +126 -0
  137. package/dist/tenant/streams.js +419 -0
  138. package/dist/tenant/sweep.d.ts +15 -0
  139. package/dist/tenant/sweep.js +122 -0
  140. package/dist/tenant/types.d.ts +95 -15
  141. package/dist/tenant/types.js +23 -1
  142. package/dist/tenant/worker.d.ts +84 -0
  143. package/dist/tenant/worker.js +189 -0
  144. package/dist/vault/service.d.ts +37 -31
  145. package/dist/vault/service.js +439 -359
  146. package/dist/version.d.ts +1 -1
  147. package/dist/version.js +1 -1
  148. package/package.json +13 -13
  149. package/dist/adapters/sandbox/microsandbox.d.ts +0 -2
  150. package/dist/adapters/sandbox/microsandbox.js +0 -238
  151. package/dist/core/store.d.ts +0 -32
  152. package/dist/core/store.js +0 -143
  153. package/dist/launcher/commands.d.ts +0 -24
  154. package/dist/launcher/commands.js +0 -260
  155. package/dist/launcher/ensure-config.d.ts +0 -16
  156. package/dist/launcher/ensure-config.js +0 -79
  157. package/dist/launcher/errors.d.ts +0 -12
  158. package/dist/launcher/errors.js +0 -22
  159. package/dist/launcher/host-config.d.ts +0 -17
  160. package/dist/launcher/host-config.js +0 -64
  161. package/dist/launcher/host-state.d.ts +0 -17
  162. package/dist/launcher/host-state.js +0 -61
  163. package/dist/launcher/lifecycle.d.ts +0 -66
  164. package/dist/launcher/lifecycle.js +0 -407
  165. package/dist/launcher/locks.d.ts +0 -26
  166. package/dist/launcher/locks.js +0 -117
  167. package/dist/launcher/logs.d.ts +0 -18
  168. package/dist/launcher/logs.js +0 -117
  169. package/dist/launcher/main.d.ts +0 -2
  170. package/dist/launcher/main.js +0 -66
  171. package/dist/launcher/output.d.ts +0 -11
  172. package/dist/launcher/output.js +0 -68
  173. package/dist/launcher/paths.d.ts +0 -20
  174. package/dist/launcher/paths.js +0 -36
  175. package/dist/launcher/probe.d.ts +0 -23
  176. package/dist/launcher/probe.js +0 -72
  177. package/dist/launcher/protocol.d.ts +0 -62
  178. package/dist/launcher/protocol.js +0 -1
  179. package/dist/launcher/schema-guard.d.ts +0 -6
  180. package/dist/launcher/schema-guard.js +0 -36
  181. package/dist/launcher/spawn.d.ts +0 -22
  182. package/dist/launcher/spawn.js +0 -85
  183. package/dist/launcher/status.d.ts +0 -6
  184. package/dist/launcher/status.js +0 -65
  185. package/dist/tenant/migration.d.ts +0 -16
  186. package/dist/tenant/migration.js +0 -116
  187. package/dist/tenant/schema.d.ts +0 -23
  188. package/dist/tenant/schema.js +0 -151
  189. package/dist/tenant/store-fs.d.ts +0 -16
  190. package/dist/tenant/store-fs.js +0 -205
@@ -1,80 +1,81 @@
1
- import { type ServerResponse, type IncomingMessage } from "node:http";
2
- import { type ModelProvider } from "../core/provider.js";
3
- import type { TenantConfig, TenantHandle, TenantSummary } from "./types.js";
1
+ /**
2
+ * Composition root of an open Tenant: validates the config, opens the Session Store and the
3
+ * vault key, builds the services and the `TenantContext`, wires the seams, registers the
4
+ * Tenant's Worker handlers and arms its sweep, and implements `TenantHandle` (handle,
5
+ * summary, drain, close) by delegating to the Tenant modules.
6
+ *
7
+ * Seams wired here: `wireStreams()` connects the store's commits to Durable Streams (the
8
+ * relay, and the history, SSE, work and control readers); `wake` goes to the
9
+ * `DurableExecution`, whose handlers (`worker.ts`) call `advance` and `sweep`; `abortLocal`
10
+ * aborts an advance running on this process. The sweep also drains the outbox.
11
+ *
12
+ * Execution: the Host passes one `TenantExecution` for every Tenant it opens
13
+ * (`TenantOpenHooks.execution`); without one, the Tenant runs its own in-process
14
+ * `MemoryExecution`. No lock file: several processes may open a Tenant, and ownership of each
15
+ * session (§10.6) keeps its advances apart.
16
+ */
17
+ import type { ServerResponse, IncomingMessage } from "node:http";
4
18
  import type { TenantEnvelope } from "@nylorun/core/contracts";
19
+ import { type ModelProvider } from "../core/provider.js";
5
20
  import { type AuthorizeResult } from "../vault/service.js";
21
+ import type { DurableStreams } from "../streams/types.js";
22
+ import type { OpenedTenant, TenantConfig, TenantHandle, TenantSummary } from "./types.js";
23
+ import { type StreamsStatus } from "./streams.js";
24
+ import { type TenantExecution, type TenantWorker } from "./worker.js";
6
25
  /** TENANTS-CCR: test/injection hooks until TenantConfig gains them. */
7
26
  export type TenantOpenHooks = {
8
27
  modelProvider?: ModelProvider;
9
28
  vaultKek?: Buffer | string | null;
10
29
  /** When true, create the KEK file on first vault write (tests / new Tenants). */
11
30
  createKekIfMissing?: boolean;
12
- };
31
+ /**
32
+ * The Host's Durable Session Execution and the registry its handlers dispatch through.
33
+ * The Host starts it; the Tenant registers its worker and arms its sweep. Without one,
34
+ * the Tenant starts and stops its own in-process `MemoryExecution`.
35
+ */
36
+ execution?: TenantExecution;
37
+ /** The Worker id written as session owner. Defaults to this process's `WORKER_ID`. */
38
+ workerId?: string;
39
+ /**
40
+ * Durable Streams for this Tenant, owned by the caller (the Host's S2 streams). Without
41
+ * them the Tenant creates its own in-process `MemoryStreams`, whose history does not
42
+ * survive a restart (tests, and a Host without S2).
43
+ */
44
+ streams?: DurableStreams;
45
+ } & OpenedTenant;
13
46
  export declare class TenantRuntime implements TenantHandle {
14
- private readonly config;
15
- private readonly store;
16
- private readonly vault;
17
- private kek;
18
- private readonly kekPath;
19
- private readonly running;
20
- private readonly pending;
21
- private readonly observers;
22
- private readonly executorStreams;
23
- private readonly registry;
24
- private readonly mcp;
25
- private readonly sandbox;
26
- private closing;
27
- private closed;
28
- private readonly lockPath?;
29
- private readonly timer;
30
- private readonly modelProvider;
31
- private readonly useVaultModel;
32
- private readonly createKekIfMissing;
33
- private readonly flowLimits;
47
+ private readonly ctx;
34
48
  readonly envelope: TenantEnvelope;
49
+ /** This Tenant's Worker handlers, as registered with the execution. */
50
+ readonly worker: TenantWorker;
51
+ private readonly detach;
52
+ /** How long close waits for running advances: the execution's advance grace period. */
53
+ private readonly closeGraceMs;
35
54
  private constructor();
36
- static open(config: TenantConfig, hooks?: TenantOpenHooks): Promise<TenantRuntime>;
37
- private session;
38
- private publish;
39
- private send;
40
- private notify;
41
- /**
42
- * Manifest v4 replaced beforeModelCall/afterModelCall with scoped hooks. Turns started under
43
- * an older manifest cannot continue, and their hook actions no longer parse, so end them.
44
- */
45
- private retireLegacyHooks;
46
- private expireClaims;
47
- private schedule;
48
- private execute;
49
- private invokeModel;
50
- private resolveEffect;
51
- /** Journal and dispatch a new flow effect (agent / tool node / fn / verify). */
52
- private resolveNewFlowEffect;
53
- private scope;
54
- private scoped;
55
- private requireApplication;
56
- private sandboxRouteDeps;
57
- private body;
58
- private command;
59
- private sse;
60
- handle(request: IncomingMessage, response: ServerResponse, _url?: URL): Promise<void>;
61
- private view;
62
- private prepareMcp;
63
- private callMcpTool;
64
- private callSandboxTool;
55
+ static open(config: TenantConfig, hooks: TenantOpenHooks): Promise<TenantRuntime>;
56
+ handle(request: IncomingMessage, response: ServerResponse, url?: URL): Promise<void>;
65
57
  authorize(sessionId: string, request: {
66
58
  url: string;
67
59
  serverName?: string;
68
60
  }): Promise<AuthorizeResult>;
69
- private ensureKek;
70
- private dispatchTenant;
71
- private dispatchVault;
72
- summary(): TenantSummary;
61
+ summary(): Promise<TenantSummary>;
73
62
  drain(activeWork: "drain" | "cancel", timeoutMs?: number): Promise<void>;
63
+ /**
64
+ * Aborts the advance of `sessionId` if it runs on this process. Cancel calls it after
65
+ * committing `cancelled`; the control stream calls it for cancels made elsewhere.
66
+ */
67
+ abortLocal(sessionId: string): void;
68
+ /**
69
+ * The Durable Streams seam's status for Tenant status and readiness: S2 reachability, the
70
+ * basin, outbox depth and relay lag (`streamsStatus` in `tenant/streams.ts`).
71
+ */
72
+ streamsStatus(): Promise<StreamsStatus>;
73
+ /** Adds a callback to the Tenant sweep. Returns a function that removes it. */
74
+ onSweep(hook: () => Promise<void>): () => void;
74
75
  close(): Promise<void>;
75
76
  }
76
77
  /**
77
78
  * Open a Tenant Runtime from an explicit `TenantConfig`. No listen() (A1).
78
79
  * Implements `OpenTenantRuntime`.
79
80
  */
80
- export declare function openTenantRuntime(config: TenantConfig, hooks?: TenantOpenHooks): Promise<TenantHandle>;
81
+ export declare function openTenantRuntime(config: TenantConfig, hooks: TenantOpenHooks): Promise<TenantHandle>;