@nylorun/runtime 0.10.0-beta → 0.12.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 (194) hide show
  1. package/CHANGELOG.md +86 -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 +13 -4
  13. package/dist/core/sandbox-routes.js +14 -12
  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 +51 -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 +645 -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/002_session_owner.d.ts +6 -0
  67. package/dist/store/postgres/migrations/002_session_owner.js +13 -0
  68. package/dist/store/postgres/migrations/index.d.ts +49 -0
  69. package/dist/store/postgres/migrations/index.js +76 -0
  70. package/dist/store/postgres/names.d.ts +11 -0
  71. package/dist/store/postgres/names.js +42 -0
  72. package/dist/store/postgres/store.d.ts +11 -0
  73. package/dist/store/postgres/store.js +797 -0
  74. package/dist/store/postgres/tenants.d.ts +90 -0
  75. package/dist/store/postgres/tenants.js +214 -0
  76. package/dist/store/types.d.ts +442 -0
  77. package/dist/store/types.js +10 -0
  78. package/dist/streams/basin.d.ts +23 -0
  79. package/dist/streams/basin.js +51 -0
  80. package/dist/streams/memory.d.ts +25 -0
  81. package/dist/streams/memory.js +124 -0
  82. package/dist/streams/relay.d.ts +37 -0
  83. package/dist/streams/relay.js +254 -0
  84. package/dist/streams/types.d.ts +115 -0
  85. package/dist/streams/types.js +86 -0
  86. package/dist/tenant/actions.d.ts +153 -0
  87. package/dist/tenant/actions.js +191 -0
  88. package/dist/tenant/advance.d.ts +15 -0
  89. package/dist/tenant/advance.js +439 -0
  90. package/dist/tenant/auth.d.ts +32 -0
  91. package/dist/tenant/auth.js +169 -0
  92. package/dist/tenant/commands.d.ts +3 -0
  93. package/dist/tenant/commands.js +393 -0
  94. package/dist/tenant/context.d.ts +147 -0
  95. package/dist/tenant/context.js +48 -0
  96. package/dist/tenant/effects.d.ts +20 -0
  97. package/dist/tenant/effects.js +564 -0
  98. package/dist/tenant/envelope.d.ts +0 -3
  99. package/dist/tenant/envelope.js +2 -34
  100. package/dist/tenant/ephemeral.d.ts +15 -4
  101. package/dist/tenant/ephemeral.js +106 -38
  102. package/dist/tenant/http.d.ts +38 -0
  103. package/dist/tenant/http.js +53 -0
  104. package/dist/tenant/live.d.ts +92 -0
  105. package/dist/tenant/live.js +290 -0
  106. package/dist/tenant/model-setting.d.ts +16 -0
  107. package/dist/tenant/model-setting.js +12 -0
  108. package/dist/tenant/module.d.ts +29 -8
  109. package/dist/tenant/module.js +165 -158
  110. package/dist/tenant/paths.d.ts +12 -8
  111. package/dist/tenant/paths.js +38 -12
  112. package/dist/tenant/principals.d.ts +12 -13
  113. package/dist/tenant/principals.js +50 -38
  114. package/dist/tenant/quarantine-error.d.ts +1 -1
  115. package/dist/tenant/quarantine-error.js +2 -8
  116. package/dist/tenant/quarantine.d.ts +0 -8
  117. package/dist/tenant/quarantine.js +8 -30
  118. package/dist/tenant/reset.d.ts +7 -6
  119. package/dist/tenant/reset.js +7 -51
  120. package/dist/tenant/routes-tenant.d.ts +12 -0
  121. package/dist/tenant/routes-tenant.js +137 -0
  122. package/dist/tenant/routes.d.ts +9 -0
  123. package/dist/tenant/routes.js +151 -0
  124. package/dist/tenant/runtime.d.ts +59 -58
  125. package/dist/tenant/runtime.js +243 -2201
  126. package/dist/tenant/scheduler.d.ts +25 -0
  127. package/dist/tenant/scheduler.js +84 -0
  128. package/dist/tenant/session.d.ts +60 -0
  129. package/dist/tenant/session.js +94 -0
  130. package/dist/tenant/sessions.d.ts +40 -0
  131. package/dist/tenant/sessions.js +163 -0
  132. package/dist/tenant/status.d.ts +13 -4
  133. package/dist/tenant/status.js +76 -41
  134. package/dist/tenant/store-memory.d.ts +9 -12
  135. package/dist/tenant/store-memory.js +79 -68
  136. package/dist/tenant/store-pg.d.ts +15 -0
  137. package/dist/tenant/store-pg.js +140 -0
  138. package/dist/tenant/streams.d.ts +126 -0
  139. package/dist/tenant/streams.js +419 -0
  140. package/dist/tenant/sweep.d.ts +15 -0
  141. package/dist/tenant/sweep.js +122 -0
  142. package/dist/tenant/transcript.d.ts +104 -0
  143. package/dist/tenant/transcript.js +58 -0
  144. package/dist/tenant/types.d.ts +101 -15
  145. package/dist/tenant/types.js +23 -1
  146. package/dist/tenant/worker.d.ts +84 -0
  147. package/dist/tenant/worker.js +189 -0
  148. package/dist/vault/service.d.ts +44 -31
  149. package/dist/vault/service.js +454 -360
  150. package/dist/version.d.ts +1 -1
  151. package/dist/version.js +1 -1
  152. package/package.json +15 -14
  153. package/dist/adapters/sandbox/microsandbox.d.ts +0 -2
  154. package/dist/adapters/sandbox/microsandbox.js +0 -238
  155. package/dist/core/store.d.ts +0 -32
  156. package/dist/core/store.js +0 -143
  157. package/dist/launcher/commands.d.ts +0 -24
  158. package/dist/launcher/commands.js +0 -260
  159. package/dist/launcher/ensure-config.d.ts +0 -16
  160. package/dist/launcher/ensure-config.js +0 -79
  161. package/dist/launcher/errors.d.ts +0 -12
  162. package/dist/launcher/errors.js +0 -22
  163. package/dist/launcher/host-config.d.ts +0 -17
  164. package/dist/launcher/host-config.js +0 -64
  165. package/dist/launcher/host-state.d.ts +0 -17
  166. package/dist/launcher/host-state.js +0 -61
  167. package/dist/launcher/lifecycle.d.ts +0 -66
  168. package/dist/launcher/lifecycle.js +0 -407
  169. package/dist/launcher/locks.d.ts +0 -26
  170. package/dist/launcher/locks.js +0 -117
  171. package/dist/launcher/logs.d.ts +0 -18
  172. package/dist/launcher/logs.js +0 -117
  173. package/dist/launcher/main.d.ts +0 -2
  174. package/dist/launcher/main.js +0 -66
  175. package/dist/launcher/output.d.ts +0 -11
  176. package/dist/launcher/output.js +0 -68
  177. package/dist/launcher/paths.d.ts +0 -20
  178. package/dist/launcher/paths.js +0 -36
  179. package/dist/launcher/probe.d.ts +0 -23
  180. package/dist/launcher/probe.js +0 -72
  181. package/dist/launcher/protocol.d.ts +0 -62
  182. package/dist/launcher/protocol.js +0 -1
  183. package/dist/launcher/schema-guard.d.ts +0 -6
  184. package/dist/launcher/schema-guard.js +0 -36
  185. package/dist/launcher/spawn.d.ts +0 -22
  186. package/dist/launcher/spawn.js +0 -85
  187. package/dist/launcher/status.d.ts +0 -6
  188. package/dist/launcher/status.js +0 -65
  189. package/dist/tenant/migration.d.ts +0 -16
  190. package/dist/tenant/migration.js +0 -116
  191. package/dist/tenant/schema.d.ts +0 -23
  192. package/dist/tenant/schema.js +0 -151
  193. package/dist/tenant/store-fs.d.ts +0 -16
  194. package/dist/tenant/store-fs.js +0 -205
@@ -0,0 +1,147 @@
1
+ /**
2
+ * The shared Tenant context: every Tenant module function takes a `TenantContext` instead of
3
+ * calling methods on the composition root. It carries the stores and services and the
4
+ * in-process work and live-stream state.
5
+ *
6
+ * Business code changes state only inside `ctx.store.tx(async (t) => …)` and follows the
7
+ * seam rules: events through `t.event(...)` (the relay appends them to Durable Streams after
8
+ * commit), executor wakes through `t.signalWork()`, and advances through
9
+ * `t.afterCommit(() => ctx.wake(id, { reason, dedupeKey }))`. It never publishes or notifies
10
+ * itself; `runtime.ts` wires the streams with `wireStreams()`. No external I/O runs inside a
11
+ * tx.
12
+ *
13
+ * `wake` goes to `DurableExecution.wake`, which calls the Tenant's `advance` (`advance.ts`)
14
+ * under ownership (§10.6); `abortLocal` aborts an advance running on this process.
15
+ */
16
+ import type { SubjectScope, TenantEnvelope } from "@nylorun/core/contracts";
17
+ import type { DurableCheckpoint, FlowCheckpoint } from "@nylorun/harness/run";
18
+ import type { AgentManifest, JsonValue } from "@nylorun/core/define";
19
+ import type { CredentialSelection } from "@nylorun/core/contracts";
20
+ import type { SessionStore, Tx } from "../store/types.js";
21
+ import type { ExecutorRecord, ExecutorRegistry } from "../core/executors.js";
22
+ import type { FlowLimits } from "../core/limits.js";
23
+ import type { ModelProvider } from "../core/provider.js";
24
+ import type { VaultService } from "../vault/service.js";
25
+ import type { McpPool } from "../mcp/pool.js";
26
+ import type { McpDiagnostic, McpSnapshot } from "../mcp/snapshot.js";
27
+ import type { SandboxManager } from "../sandbox/manager.js";
28
+ import type { TenantConfig } from "./types.js";
29
+ import type { LiveHub } from "./live.js";
30
+ import type { StuckInvocation, Wake } from "../execution/types.js";
31
+ import type { WorkState } from "./scheduler.js";
32
+ export interface Session {
33
+ id: string;
34
+ agentId: string;
35
+ ownerUserId: string;
36
+ /** Session pin — the manifest the session was created with. */
37
+ manifest: any;
38
+ manifestHash: string;
39
+ /** Validated turn-manifest variants, keyed by hash (loops.md §4.2). */
40
+ variants?: Record<string, AgentManifest>;
41
+ implementationVersion: string;
42
+ info?: any;
43
+ status: string;
44
+ activeTurnId: string | null;
45
+ checkpoint?: DurableCheckpoint | FlowCheckpoint;
46
+ state?: any;
47
+ turnStartState?: any;
48
+ waits?: unknown;
49
+ error?: string;
50
+ /** Last completed turn output (for linked agent → workflow settle). */
51
+ lastOutput?: JsonValue;
52
+ /**
53
+ * The last turn that ended (completed, failed or cancelled): the turn `status`, `lastOutput`
54
+ * and `error` describe while no turn is active. A linked `agent` effect settles only from the
55
+ * turn it started (`linkedTurnEnd` in `core/flow-host.ts`).
56
+ */
57
+ lastTurnId?: string;
58
+ creation: unknown;
59
+ vaultIds?: readonly string[];
60
+ credentialSelections?: readonly CredentialSelection[];
61
+ pluginRoots?: Readonly<Record<string, string>>;
62
+ mcpSnapshot?: McpSnapshot;
63
+ mcpDiagnostics?: readonly McpDiagnostic[];
64
+ /** Session id that keys the shared sandbox; absent means this session owns it. */
65
+ sandboxOwnerId?: string;
66
+ /**
67
+ * The incarnation naming this session's event stream (`sessions/<id>/<incarnation>`), set at
68
+ * creation and never changed. Absent only on sessions created before incarnations.
69
+ */
70
+ streamIncarnation?: string;
71
+ }
72
+ export type AuthScope = {
73
+ kind: "application";
74
+ principalId: string;
75
+ }
76
+ /** An application principal acting for `subject` (`Nylorun-Subject`), narrowed to `scopes`. */
77
+ | {
78
+ kind: "subject";
79
+ principalId: string;
80
+ subject: string;
81
+ scopes: ReadonlySet<SubjectScope>;
82
+ } | {
83
+ kind: "executor";
84
+ executor: ExecutorRecord;
85
+ };
86
+ export interface TenantContext {
87
+ readonly config: TenantConfig;
88
+ readonly envelope: TenantEnvelope;
89
+ readonly store: SessionStore;
90
+ readonly vault: VaultService;
91
+ readonly registry: ExecutorRegistry;
92
+ readonly mcp: McpPool;
93
+ readonly sandbox: SandboxManager;
94
+ readonly flowLimits: FlowLimits;
95
+ readonly modelProvider: ModelProvider;
96
+ /** True when the model comes from the Tenant vault selection (pi-ai adapter). */
97
+ readonly useVaultModel: boolean;
98
+ /** Set by drain/close; reset clears it again. Stops new advances. */
99
+ closing: boolean;
100
+ /** Set once close has finished releasing resources. */
101
+ closed: boolean;
102
+ /** Advances running on this process, for `abortLocal`, drain and close. */
103
+ readonly work: WorkState;
104
+ /** Live delivery over Durable Streams: session feeds, executor streams, the streams wiring. */
105
+ readonly live: LiveHub;
106
+ /** The Worker id this process writes as session `owner` (§10.6). */
107
+ readonly workerId: string;
108
+ /** How long an advance's ownership lease lasts; the heartbeat renews it. */
109
+ readonly ownerLeaseMs: number;
110
+ /**
111
+ * Seam: request an advance of a session (`DurableExecution.wake`). Call it from
112
+ * `t.afterCommit`, never inside a tx. Dropped while the Tenant is closing; the sweep
113
+ * re-wakes anything left runnable.
114
+ */
115
+ wake(sessionId: string, wake: Wake): Promise<void>;
116
+ /** Seam: abort the advance of a session running in this process, if any. */
117
+ abortLocal(sessionId: string): void;
118
+ /** This Tenant's execution invocations that need an operator, for Tenant status. */
119
+ readonly stuckInvocations?: () => Promise<StuckInvocation[]>;
120
+ /** Callbacks the Tenant sweep runs after its own steps (`sweep.ts`). */
121
+ readonly sweepHooks: ReadonlySet<() => Promise<void>>;
122
+ /** Adds a sweep callback (the outbox drain, Wave 2 / Y). Returns a function that removes it. */
123
+ onSweep(hook: () => Promise<void>): () => void;
124
+ }
125
+ /** The session, or a 404. Reads without locking. */
126
+ export declare function sessionOf(t: Tx, id: string, owner?: string): Promise<Session>;
127
+ /** Locks the session for the rest of the transaction and returns it, or a 404. */
128
+ export declare function lockedSession(t: Tx, id: string, owner?: string): Promise<Session>;
129
+ /** An advance's hold on its session (§10.6): every write the advance makes presents `epoch`. */
130
+ export interface Lease {
131
+ readonly sessionId: string;
132
+ readonly owner: string;
133
+ readonly epoch: number;
134
+ }
135
+ /**
136
+ * Locks `id` for the advance holding `lease`. The advance's own session is epoch-checked: a
137
+ * mismatch (or a deleted session) throws `ownership.lost` before anything is written. Any
138
+ * other session is only locked (or a 404).
139
+ */
140
+ export declare function ownedSession(t: Tx, lease: Lease, id: string): Promise<Session>;
141
+ /** The session read in its own transaction, or a 404. */
142
+ export declare function loadSession(ctx: TenantContext, id: string, owner?: string): Promise<Session>;
143
+ /**
144
+ * A synchronous lookup over `start` and every session its `sandboxOwnerId` chain names, for
145
+ * the sandbox-sharing helpers, which walk that chain synchronously.
146
+ */
147
+ export declare function sandboxLookup(t: Tx, start: string | undefined): Promise<(id: string) => Session | undefined>;
@@ -0,0 +1,48 @@
1
+ import { fail } from "./http.js";
2
+ /**
3
+ * `owner`, when set, is the subject the request acts for: another owner's session is the same
4
+ * 404 as a missing one, so a subject cannot learn which session ids exist.
5
+ */
6
+ function owned(session, owner) {
7
+ if (!session || (owner !== undefined && session.ownerUserId !== owner))
8
+ return fail(404, "Session not found");
9
+ return session;
10
+ }
11
+ /** The session, or a 404. Reads without locking. */
12
+ export async function sessionOf(t, id, owner) {
13
+ return owned(await t.get("sessions", id), owner);
14
+ }
15
+ /** Locks the session for the rest of the transaction and returns it, or a 404. */
16
+ export async function lockedSession(t, id, owner) {
17
+ return owned(await t.lockSession(id), owner);
18
+ }
19
+ /**
20
+ * Locks `id` for the advance holding `lease`. The advance's own session is epoch-checked: a
21
+ * mismatch (or a deleted session) throws `ownership.lost` before anything is written. Any
22
+ * other session is only locked (or a 404).
23
+ */
24
+ export async function ownedSession(t, lease, id) {
25
+ if (id === lease.sessionId)
26
+ return t.assertEpoch(id, lease.epoch);
27
+ return lockedSession(t, id);
28
+ }
29
+ /** The session read in its own transaction, or a 404. */
30
+ export function loadSession(ctx, id, owner) {
31
+ return ctx.store.tx((t) => sessionOf(t, id, owner));
32
+ }
33
+ /**
34
+ * A synchronous lookup over `start` and every session its `sandboxOwnerId` chain names, for
35
+ * the sandbox-sharing helpers, which walk that chain synchronously.
36
+ */
37
+ export async function sandboxLookup(t, start) {
38
+ const found = new Map();
39
+ let id = start;
40
+ while (id && !found.has(id)) {
41
+ const session = await t.get("sessions", id);
42
+ if (!session)
43
+ break;
44
+ found.set(id, session);
45
+ id = session.sandboxOwnerId;
46
+ }
47
+ return (sid) => found.get(sid);
48
+ }
@@ -0,0 +1,20 @@
1
+ import type { EffectResolution, HostEffect } from "@nylorun/harness/run";
2
+ import type { AuthorizeResult } from "../vault/service.js";
3
+ import { type Lease, type TenantContext } from "./context.js";
4
+ import type { ModelProvider } from "../core/provider.js";
5
+ /** What one advance's segment decides for all its effects. */
6
+ export interface SegmentOptions {
7
+ /** Overrides the Tenant's model (the fixture-model Tenant setting, `model-setting.ts`). */
8
+ model?: ModelProvider;
9
+ }
10
+ export declare function invokeModel(ctx: TenantContext, request: HostEffect, signal: AbortSignal, model?: ModelProvider): Promise<unknown>;
11
+ export declare function resolveEffect(ctx: TenantContext, request: HostEffect, signal: AbortSignal, lease: Lease, segment?: SegmentOptions): Promise<EffectResolution>;
12
+ /** Journal and dispatch a new flow effect (agent / tool node / fn / verify). */
13
+ export declare function resolveNewFlowEffect(ctx: TenantContext, request: HostEffect, signal: AbortSignal, lease: Lease): Promise<EffectResolution>;
14
+ /** Discover the session's MCP tools once, or reconnect and refresh diagnostics. */
15
+ export declare function prepareMcp(ctx: TenantContext, lease: Lease, signal: AbortSignal): Promise<void>;
16
+ /** Vault authorization for an MCP server request made on behalf of a session. */
17
+ export declare function authorize(ctx: TenantContext, sessionId: string, request: {
18
+ url: string;
19
+ serverName?: string;
20
+ }): Promise<AuthorizeResult>;