@nylorun/runtime 0.20.0-beta → 0.21.1-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 (109) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/README.md +19 -12
  3. package/dist/adapters/execution/restate.js +26 -1
  4. package/dist/api/components.d.ts +155 -36
  5. package/dist/api/components.js +3 -3
  6. package/dist/api/http/app.js +2 -0
  7. package/dist/api/http/routes/mcp.d.ts +9 -0
  8. package/dist/api/http/routes/mcp.js +32 -0
  9. package/dist/api/http/routes/vaults.d.ts +1 -9
  10. package/dist/api/http/routes/vaults.js +3 -103
  11. package/dist/api/openapi.js +6 -3
  12. package/dist/artifacts/media-types.d.ts +2 -0
  13. package/dist/artifacts/media-types.js +5 -0
  14. package/dist/core/flow-host.d.ts +2 -3
  15. package/dist/core/flow-host.js +10 -7
  16. package/dist/execution/types.d.ts +13 -5
  17. package/dist/execution/types.js +13 -5
  18. package/dist/gates/http-tool.d.ts +7 -0
  19. package/dist/gates/http-tool.js +34 -3
  20. package/dist/gates/mcp-handler.d.ts +4 -1
  21. package/dist/gates/mcp-handler.js +64 -8
  22. package/dist/gates/tenant-vaults.d.ts +9 -15
  23. package/dist/gates/tenant-vaults.js +9 -17
  24. package/dist/gates/tool-client.js +36 -17
  25. package/dist/gates/tool-contract.d.ts +19 -3
  26. package/dist/gates/tool-contract.js +9 -3
  27. package/dist/gates/tool-gate.d.ts +2 -2
  28. package/dist/harness/calls.d.ts +2 -0
  29. package/dist/harness/calls.js +13 -3
  30. package/dist/harness-api/record.d.ts +7 -6
  31. package/dist/harness-api/record.js +99 -17
  32. package/dist/harness-api/server.js +35 -27
  33. package/dist/harness-api/start.js +2 -1
  34. package/dist/host/config-for.d.ts +0 -2
  35. package/dist/host/config-for.js +0 -1
  36. package/dist/host/create-host.d.ts +1 -1
  37. package/dist/host/create-host.js +2 -1
  38. package/dist/host/gates.js +0 -1
  39. package/dist/host/http.d.ts +0 -1
  40. package/dist/host/http.js +0 -6
  41. package/dist/host/main.js +13 -3
  42. package/dist/host/stack-config.d.ts +7 -8
  43. package/dist/host/stack-config.js +50 -92
  44. package/dist/index.d.ts +0 -1
  45. package/dist/keys/client.js +1 -9
  46. package/dist/keys/keys.d.ts +11 -13
  47. package/dist/keys/keys.js +5 -7
  48. package/dist/management-openapi.json +315 -345
  49. package/dist/mcp/connect.d.ts +159 -13
  50. package/dist/mcp/connect.js +538 -55
  51. package/dist/mcp/pool.d.ts +11 -2
  52. package/dist/mcp/pool.js +74 -27
  53. package/dist/mcp/preview.d.ts +35 -0
  54. package/dist/mcp/preview.js +215 -0
  55. package/dist/mcp/snapshot.d.ts +79 -5
  56. package/dist/mcp/snapshot.js +145 -27
  57. package/dist/model/catalog.d.ts +14 -0
  58. package/dist/model/catalog.js +14 -1
  59. package/dist/model/pi-model.js +36 -2
  60. package/dist/openapi.json +362 -123
  61. package/dist/redact.d.ts +19 -1
  62. package/dist/redact.js +42 -13
  63. package/dist/sandbox/session-sandbox.d.ts +7 -0
  64. package/dist/sandbox/session-sandbox.js +89 -28
  65. package/dist/store/postgres/drizzle/0016_mcp_oauth_removed.sql +5 -0
  66. package/dist/store/postgres/drizzle/0017_wake_outbox.sql +9 -0
  67. package/dist/store/postgres/drizzle/meta/_journal.json +14 -0
  68. package/dist/store/postgres/schema.d.ts +105 -261
  69. package/dist/store/postgres/schema.js +18 -36
  70. package/dist/store/postgres/store.js +54 -21
  71. package/dist/store/postgres/tenant.d.ts +14 -0
  72. package/dist/store/postgres/tenant.js +22 -2
  73. package/dist/store/types.d.ts +48 -13
  74. package/dist/tenant/advance.js +1 -4
  75. package/dist/tenant/artifact-tool.d.ts +9 -1
  76. package/dist/tenant/artifact-tool.js +113 -9
  77. package/dist/tenant/commands.js +5 -5
  78. package/dist/tenant/context.d.ts +15 -13
  79. package/dist/tenant/effects.d.ts +2 -2
  80. package/dist/tenant/effects.js +20 -5
  81. package/dist/tenant/ephemeral.d.ts +0 -6
  82. package/dist/tenant/ephemeral.js +0 -1
  83. package/dist/tenant/outbound.d.ts +14 -5
  84. package/dist/tenant/outbound.js +21 -9
  85. package/dist/tenant/runtime.js +11 -15
  86. package/dist/tenant/sessions.js +20 -3
  87. package/dist/tenant/store-pg.js +9 -0
  88. package/dist/tenant/sweep.d.ts +14 -0
  89. package/dist/tenant/sweep.js +69 -10
  90. package/dist/tenant/tool-results.d.ts +57 -0
  91. package/dist/tenant/tool-results.js +298 -0
  92. package/dist/tenant/tool-search.d.ts +43 -0
  93. package/dist/tenant/tool-search.js +100 -0
  94. package/dist/tenant/transcript.d.ts +108 -0
  95. package/dist/tenant/transcript.js +13 -0
  96. package/dist/tenant/types.d.ts +0 -14
  97. package/dist/vault/crypto.d.ts +0 -7
  98. package/dist/vault/crypto.js +0 -16
  99. package/dist/vault/headers.d.ts +17 -0
  100. package/dist/vault/headers.js +61 -0
  101. package/dist/vault/service.d.ts +11 -68
  102. package/dist/vault/service.js +83 -438
  103. package/dist/vault/sources.d.ts +20 -48
  104. package/dist/vault/sources.js +25 -189
  105. package/dist/version.d.ts +1 -1
  106. package/dist/version.js +1 -1
  107. package/package.json +3 -3
  108. package/dist/vault/oauth.d.ts +0 -55
  109. package/dist/vault/oauth.js +0 -186
@@ -9,12 +9,12 @@
9
9
  import type { Credential } from "@earendil-works/pi-ai";
10
10
  import type { ModelFailureOutcome } from "@nylorun/core/define";
11
11
  import type { PostgresClient } from "../store/postgres/connect.js";
12
- import { type AuthorizeResult, type HostModelSecret } from "../vault/service.js";
13
- import { CredentialSources, type McpCredentialRequest, type ResolverConfig } from "../vault/sources.js";
12
+ import { VaultService, type AuthorizeResult, type HostModelSecret } from "../vault/service.js";
13
+ import { type McpCredentialRequest } from "../vault/sources.js";
14
14
  import type { SessionStore } from "../store/types.js";
15
+ import type { OutboundPolicy } from "../tenant/outbound.js";
15
16
  import type { Session } from "../tenant/context.js";
16
17
  import { type Keys } from "../keys/keys.js";
17
- import { type OutboundPolicy } from "../tenant/outbound.js";
18
18
  /** The Tenant, as a model call needs it. */
19
19
  export interface TenantVault {
20
20
  readonly tenantId: string;
@@ -29,8 +29,7 @@ export interface TenantVault {
29
29
  session(sessionId: string): Promise<Session | undefined>;
30
30
  /**
31
31
  * The authorization of one request to a session's remote MCP server or HTTP tool: its
32
- * credential from the session's attached vaults, refreshed when due (F4.1), else from the
33
- * operator's credential resolver (F9 C1).
32
+ * credential from the session's attached vaults.
34
33
  */
35
34
  authorizeMcp(sessionId: string, request: McpCredentialRequest): Promise<AuthorizeResult>;
36
35
  /** Vault writes and token signing with the Tenant's vault key (the keys service, F4.2). */
@@ -52,18 +51,13 @@ export interface TenantVaultsOptions {
52
51
  readonly sql: PostgresClient;
53
52
  /** The Host root; the gate reads `keys/vault-kek` and `tenant/home` under it. */
54
53
  readonly hostRoot: string;
55
- /** The operator's credential resolver (`NYLORUN_RESOLVER_*`, F9 C1). */
56
- readonly resolver?: ResolverConfig;
57
- /**
58
- * The Host's address policy (`NYLORUN_ENDPOINT_*`): OAuth refresh and MCP OAuth connect call
59
- * authorization servers through `guardedFetch` under it (F9 C2).
60
- */
54
+ /** How the keys service's tool preview reaches a server (`NYLORUN_ENDPOINT_*`, R2b C12). */
61
55
  readonly delivery?: OutboundPolicy;
62
56
  }
63
57
  export declare function createTenantVaults(options: TenantVaultsOptions): TenantVaults;
64
58
  /**
65
- * A request to a session's remote MCP server, authorized from the session's attached vaults,
66
- * else the operator's credential resolver: what the loop does in its own process
67
- * (`tenant/effects.ts` `authorize`). The owner and turn come from the session row.
59
+ * A request to a session's remote MCP server, authorized from the session's attached vaults:
60
+ * what the loop does in its own process (`tenant/effects.ts` `authorize`). The vaults come from
61
+ * the session row.
68
62
  */
69
- export declare function authorizeSessionMcp(sources: CredentialSources, session: (sessionId: string) => Promise<Session | undefined>, sessionId: string, request: McpCredentialRequest): Promise<AuthorizeResult>;
63
+ export declare function authorizeSessionMcp(vault: Pick<VaultService, "authorize">, session: (sessionId: string) => Promise<Session | undefined>, sessionId: string, request: McpCredentialRequest): Promise<AuthorizeResult>;
@@ -8,10 +8,9 @@ import { tenantPaths } from "../tenant/paths.js";
8
8
  import { readVaultKek } from "../vault/kek.js";
9
9
  import { HostModelVault } from "../vault/host-model.js";
10
10
  import { VaultService } from "../vault/service.js";
11
- import { CredentialSources } from "../vault/sources.js";
11
+ import { sessionCredentials } from "../vault/sources.js";
12
12
  import { inProcessKeys } from "../keys/keys.js";
13
13
  import { SigningKeys } from "../tenant/signing-keys.js";
14
- import { guardedFetch } from "../tenant/outbound.js";
15
14
  /** A call the gate refuses before the provider is called; it answers with `outcome`. */
16
15
  export class GateRefusal extends Error {
17
16
  outcome;
@@ -35,20 +34,13 @@ export function createTenantVaults(options) {
35
34
  return kek;
36
35
  };
37
36
  const vault = new HostModelVault({ store, kek: readKek });
38
- const credentials = new VaultService({
39
- store,
40
- kek: readKek,
41
- fetch: guardedFetch(options.delivery ?? {}),
42
- });
43
- const sources = new CredentialSources({
44
- vault: credentials,
45
- ...(options.resolver ? { resolver: options.resolver } : {}),
46
- });
37
+ const credentials = new VaultService({ store, kek: readKek });
47
38
  const keys = inProcessKeys({
48
39
  store,
49
40
  vault: credentials,
50
41
  signingKeys: new SigningKeys({ tenantId, kek: readKek }),
51
42
  kek: readKek,
43
+ ...(options.delivery ? { policy: options.delivery } : {}),
52
44
  });
53
45
  const session = (sessionId) => store.tx((t) => t.get("sessions", sessionId));
54
46
  return {
@@ -58,7 +50,7 @@ export function createTenantVaults(options) {
58
50
  readHostModel: () => vault.readHostModel(),
59
51
  writeHostCredential: (credential) => vault.updateHostCredential(credential),
60
52
  session,
61
- authorizeMcp: (sessionId, request) => authorizeSessionMcp(sources, session, sessionId, request),
53
+ authorizeMcp: (sessionId, request) => authorizeSessionMcp(credentials, session, sessionId, request),
62
54
  keys: () => keys,
63
55
  };
64
56
  }
@@ -87,13 +79,13 @@ export function createTenantVaults(options) {
87
79
  };
88
80
  }
89
81
  /**
90
- * A request to a session's remote MCP server, authorized from the session's attached vaults,
91
- * else the operator's credential resolver: what the loop does in its own process
92
- * (`tenant/effects.ts` `authorize`). The owner and turn come from the session row.
82
+ * A request to a session's remote MCP server, authorized from the session's attached vaults:
83
+ * what the loop does in its own process (`tenant/effects.ts` `authorize`). The vaults come from
84
+ * the session row.
93
85
  */
94
- export async function authorizeSessionMcp(sources, session, sessionId, request) {
86
+ export async function authorizeSessionMcp(vault, session, sessionId, request) {
95
87
  const found = await session(sessionId);
96
88
  if (!found)
97
89
  throw new Error(`Session ${sessionId} not found`);
98
- return sources.authorize({ ...found, id: sessionId }, request);
90
+ return sessionCredentials(vault, { ...found, id: sessionId }, request);
99
91
  }
@@ -6,9 +6,12 @@
6
6
  * has finished.
7
7
  *
8
8
  * Failures keep the meaning they had in the loop's own process:
9
- * - An MCP request that fails, at the server or on the hop, throws, so the pool reports a
10
- * diagnostic and `resolveEffect` marks a call `uncertain`, as before. An HTTP tool call
11
- * throws only for the hop, or a call lost with the gateway: its own failures are outcomes.
9
+ * - An MCP request that fails throws, so the pool reports a diagnostic. A tool call's coded
10
+ * failure (R2b C7) throws the `McpCallFailed` the loop's own call would have, which becomes a
11
+ * failed outcome the model sees. Only the hop's failures and a call lost with the gateway leave
12
+ * it `uncertain`.
13
+ * - An HTTP tool call throws only for the hop, or a call lost with the gateway: its own failures
14
+ * are outcomes.
12
15
  *
13
16
  * Credentials (F5): a session's MCP requests and keyed cancels carry its run token, read per
14
17
  * request from the advance's grant, and leave the session out of the body: the gate takes it
@@ -19,6 +22,7 @@
19
22
  import { randomUUID } from "node:crypto";
20
23
  import { request as httpRequest } from "node:http";
21
24
  import { request as httpsRequest } from "node:https";
25
+ import { McpCallFailed, } from "../mcp/connect.js";
22
26
  import { TENANT_HEADER } from "./contract.js";
23
27
  import { GATE_CLIENT_TIMEOUT_MS } from "./http-client.js";
24
28
  import { HTTP_CALLS_PATH, MCP_CLOSE_PATH, MCP_CONNECT_PATH, MCP_LIST_PATH, TOOL_CALLS_PATH, } from "./tool-contract.js";
@@ -106,10 +110,11 @@ export function httpToolGate(options) {
106
110
  return { body: body(unnamed), bearer: token };
107
111
  }
108
112
  /**
109
- * The result of an MCP or HTTP tool request, or a throw the pool and `resolveEffect` read as
110
- * before: the call's fate is unknown, and the effect becomes `uncertain`.
113
+ * The answer of an MCP or HTTP tool request, or a throw: a coded MCP failure (`McpCallFailed`),
114
+ * or one the pool and `resolveEffect` read as before, the call's fate unknown and the effect
115
+ * `uncertain`.
111
116
  */
112
- async function answered(path, scope, request) {
117
+ async function answer(path, scope, request) {
113
118
  const result = await exchange(path, scope.body, {
114
119
  ...request,
115
120
  timeoutMs,
@@ -123,9 +128,12 @@ export function httpToolGate(options) {
123
128
  if (result.status !== 200 || !parsed || !("ok" in parsed))
124
129
  throw new Error(gateRefusal(result.status, parsed));
125
130
  if (parsed.ok)
126
- return parsed.result;
131
+ return parsed;
127
132
  throw mcpError(parsed.error);
128
133
  }
134
+ async function answered(path, scope, request) {
135
+ return (await answer(path, scope, request)).result;
136
+ }
129
137
  async function fireAndForget(path, scope) {
130
138
  await exchange(path, scope.body, {
131
139
  timeoutMs: SHORT_TIMEOUT_MS,
@@ -136,24 +144,32 @@ export function httpToolGate(options) {
136
144
  recovers: true,
137
145
  async openMcp(server) {
138
146
  // The credential is read per request: the connection outlives the advance that opened it.
139
- await answered(MCP_CONNECT_PATH, scoped(server, (named) => ({ server: named })), {});
147
+ // A gate answers the server's instructions (R2b C10); an older one answers null.
148
+ const connected = await answered(MCP_CONNECT_PATH, scoped(server, (named) => ({ server: named })), {});
149
+ const instructions = typeof connected?.instructions === "string" && connected.instructions.length > 0
150
+ ? connected.instructions
151
+ : undefined;
140
152
  const client = {
141
153
  listTools: (params, request) => answered(MCP_LIST_PATH, scoped(server, (named) => ({
142
154
  server: named,
143
155
  ...(params?.cursor ? { cursor: params.cursor } : {}),
144
156
  })), request?.signal ? { signal: request.signal } : {}),
145
- callTool: (params, request) => answered(TOOL_CALLS_PATH, scoped(server, (named) => ({
146
- server: named,
147
- effectId: request?.key ?? randomUUID(),
148
- name: params.name,
149
- arguments: params.arguments,
150
- })), {
151
- ...(request?.signal ? { signal: request.signal } : {}),
152
- ...(request?.key ? { idempotencyKey: request.key } : {}),
153
- }),
157
+ async callTool(params, request) {
158
+ const called = await answer(TOOL_CALLS_PATH, scoped(server, (named) => ({
159
+ server: named,
160
+ effectId: request?.key ?? randomUUID(),
161
+ name: params.name,
162
+ arguments: params.arguments,
163
+ })), {
164
+ ...(request?.signal ? { signal: request.signal } : {}),
165
+ ...(request?.key ? { idempotencyKey: request.key } : {}),
166
+ });
167
+ return { result: called.result, ...(called.redacted ? { redacted: called.redacted } : {}) };
168
+ },
154
169
  };
155
170
  return {
156
171
  client,
172
+ ...(instructions === undefined ? {} : { instructions }),
157
173
  close: () => fireAndForget(MCP_CLOSE_PATH, scoped(server, (named) => ({ server: named }))),
158
174
  };
159
175
  },
@@ -174,6 +190,9 @@ export function httpToolGate(options) {
174
190
  }
175
191
  /** An MCP failure the gate reported, as the loop's own MCP code would have thrown it. */
176
192
  function mcpError(error) {
193
+ // Coded (R2b C7): the model sees it. A call lost with the gateway never is.
194
+ if (error.failure && !error.uncertain)
195
+ return McpCallFailed.from(error.failure);
177
196
  const thrown = new Error(error.uncertain ? `${error.message} (the call may have run; it is not sent again)` : error.message);
178
197
  if (error.code !== undefined)
179
198
  thrown.code = error.code;
@@ -11,7 +11,9 @@
11
11
  * Remote MCP. The gate holds the connection and the vault credential; the loop names the
12
12
  * server, never a URL or a credential, and the gate finds it in the session's pinned manifest.
13
13
  * - `POST /nylorun/v1/mcp/connect` `{server}`: opens the server's connection (vault
14
- * authorization included), or keeps the open one.
14
+ * authorization included), or keeps the open one. Its result is `{instructions}` when the
15
+ * server gave instructions at `initialize` (R2b C10, for the note on its deferred tools), else
16
+ * `null`.
15
17
  * - `POST /nylorun/v1/mcp/list` `{server, cursor?}`: one page of `tools/list`.
16
18
  * - `POST /nylorun/v1/tool-calls` `{server, effectId, name, arguments}`: one
17
19
  * `tools/call`. With an `Idempotency-Key` (the effect id) the call outlives its client and runs
@@ -20,8 +22,12 @@
20
22
  * gate_forbidden` for another session's call under a run token.
21
23
  * - `POST /nylorun/v1/mcp/close` `{server}`: closes the server's connection; `204`.
22
24
  * Each answers `200 {ok: true, result}` or `200 {ok: false, error}`: an MCP failure is an
23
- * answer, not a gate error. A keyed call whose earlier attempt was lost with the gateway answers
24
- * `{ok: false, error: {uncertain: true}}` and is never run again.
25
+ * answer, not a gate error. A tool call's failure is coded (`error.failure`, R2b C7), and neither
26
+ * a result nor a failure carries a credential value the gate sent (C8). An answer past 8 MiB is
27
+ * `mcp.too-large` (C11), as an HTTP tool's is `http.too-large`; core shapes what fits into a
28
+ * result the model can take (`tenant/tool-results.ts`). A keyed call whose
29
+ * earlier attempt was lost with the gateway answers `{ok: false, error: {uncertain: true}}` and
30
+ * is never run again.
25
31
  *
26
32
  * HTTP tools (R2 M3). `POST /nylorun/v1/http-calls` `{tool, effectId, turnId, input}`: one call
27
33
  * of an HTTP tool. The loop names the tool (`{sessionId?, agentId?, capabilityId, toolName}`), or a
@@ -33,6 +39,7 @@
33
39
  * model sees), or `{ok: false, error: {uncertain: true}}` for a call lost with the gateway.
34
40
  */
35
41
  import { z } from "zod";
42
+ import type { McpCallFailure } from "../mcp/connect.js";
36
43
  export declare const MCP_CONNECT_PATH = "/nylorun/v1/mcp/connect";
37
44
  export declare const MCP_LIST_PATH = "/nylorun/v1/mcp/list";
38
45
  export declare const MCP_CLOSE_PATH = "/nylorun/v1/mcp/close";
@@ -99,10 +106,19 @@ export interface McpGateError {
99
106
  readonly credentialIds?: readonly string[];
100
107
  /** The call may have run: its earlier attempt was lost with the gateway. Never re-run. */
101
108
  readonly uncertain?: boolean;
109
+ /**
110
+ * How the request failed, coded (R2b C1, C7): the loop's `McpCallFailed`, which a tool call
111
+ * turns into a failed outcome the model sees (`mcp.lost` only for a read-only or idempotent
112
+ * tool). `credential_rejected` for a `401`, with the server and the scope of the vault whose
113
+ * credential was sent. Its message has no credential value in it (C8).
114
+ */
115
+ readonly failure?: McpCallFailure;
102
116
  }
103
117
  export type McpAnswer<T> = {
104
118
  readonly ok: true;
105
119
  readonly result: T;
120
+ /** A `tools/call`'s: how many credential values its result echoed, replaced (R2b C8). */
121
+ readonly redacted?: number;
106
122
  } | {
107
123
  readonly ok: false;
108
124
  readonly error: McpGateError;
@@ -11,7 +11,9 @@
11
11
  * Remote MCP. The gate holds the connection and the vault credential; the loop names the
12
12
  * server, never a URL or a credential, and the gate finds it in the session's pinned manifest.
13
13
  * - `POST /nylorun/v1/mcp/connect` `{server}`: opens the server's connection (vault
14
- * authorization included), or keeps the open one.
14
+ * authorization included), or keeps the open one. Its result is `{instructions}` when the
15
+ * server gave instructions at `initialize` (R2b C10, for the note on its deferred tools), else
16
+ * `null`.
15
17
  * - `POST /nylorun/v1/mcp/list` `{server, cursor?}`: one page of `tools/list`.
16
18
  * - `POST /nylorun/v1/tool-calls` `{server, effectId, name, arguments}`: one
17
19
  * `tools/call`. With an `Idempotency-Key` (the effect id) the call outlives its client and runs
@@ -20,8 +22,12 @@
20
22
  * gate_forbidden` for another session's call under a run token.
21
23
  * - `POST /nylorun/v1/mcp/close` `{server}`: closes the server's connection; `204`.
22
24
  * Each answers `200 {ok: true, result}` or `200 {ok: false, error}`: an MCP failure is an
23
- * answer, not a gate error. A keyed call whose earlier attempt was lost with the gateway answers
24
- * `{ok: false, error: {uncertain: true}}` and is never run again.
25
+ * answer, not a gate error. A tool call's failure is coded (`error.failure`, R2b C7), and neither
26
+ * a result nor a failure carries a credential value the gate sent (C8). An answer past 8 MiB is
27
+ * `mcp.too-large` (C11), as an HTTP tool's is `http.too-large`; core shapes what fits into a
28
+ * result the model can take (`tenant/tool-results.ts`). A keyed call whose
29
+ * earlier attempt was lost with the gateway answers `{ok: false, error: {uncertain: true}}` and
30
+ * is never run again.
25
31
  *
26
32
  * HTTP tools (R2 M3). `POST /nylorun/v1/http-calls` `{tool, effectId, turnId, input}`: one call
27
33
  * of an HTTP tool. The loop names the tool (`{sessionId?, agentId?, capabilityId, toolName}`), or a
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * The Tool Gate seam (blueprint §12, F4.1). Every tool call that leaves the loop crosses one
3
3
  * `ToolGate`: a remote MCP server's connection and calls, and an HTTP tool's request (R2 M3).
4
- * The gate owns what those need that the loop must not hold: the MCP and HTTP tool credentials
5
- * and their OAuth refresh, and the outbound route.
4
+ * The gate owns what those need that the loop must not hold: the MCP and HTTP tool credentials,
5
+ * and the outbound route.
6
6
  *
7
7
  * Two implementations: `inProcessToolGate` (embedding, the ephemeral Runtime, tests), which
8
8
  * opens remote servers and calls HTTP tools in this process, and the HTTP client of the gates
@@ -46,6 +46,8 @@ export declare function invokeModel(route: ModelRoute, request: HostEffect, sign
46
46
  export declare function abortOn(signal: AbortSignal, kinds: readonly AbortReason[]): AbortSignal;
47
47
  /** True when `request` calls `save_artifact` of the `nylorun.artifacts` capability `manifest` has. */
48
48
  export declare function isSaveArtifactCall(manifest: AgentManifest | undefined, request: HostEffect): boolean;
49
+ /** True when `request` calls `read_artifact` (R2b C11) of the `nylorun.artifacts` capability. */
50
+ export declare function isReadArtifactCall(manifest: AgentManifest | undefined, request: HostEffect): boolean;
49
51
  /** True when `request` calls a tool of a declared MCP server (every one is remote). */
50
52
  export declare function isRemoteMcpCall(routing: Pick<ToolRouting, "rootManifest" | "mcpSnapshot">, request: HostEffect): boolean;
51
53
  export declare function callMcpTool(mcp: McpPool, routing: ToolRouting, request: HostEffect, signal: AbortSignal): Promise<unknown>;
@@ -3,7 +3,7 @@
3
3
  * Tenant's provider), the Tool Gate, the MCP pool, the SandboxManager. No store, no record: the
4
4
  * session's routing comes with the run (`TurnStart.routing`).
5
5
  */
6
- import { ARTIFACTS_CAPABILITY_ID, SAVE_ARTIFACT_TOOL } from "@nylorun/core/define";
6
+ import { ARTIFACTS_CAPABILITY_ID, READ_ARTIFACT_TOOL, SAVE_ARTIFACT_TOOL, } from "@nylorun/core/define";
7
7
  import { runAbortKind } from "@nylorun/harness/api";
8
8
  import { httpToolRefOf } from "../gates/http-tool.js";
9
9
  import { classifyThrown } from "../model/classify.js";
@@ -67,10 +67,18 @@ export function abortOn(signal, kinds) {
67
67
  }
68
68
  /** True when `request` calls `save_artifact` of the `nylorun.artifacts` capability `manifest` has. */
69
69
  export function isSaveArtifactCall(manifest, request) {
70
+ return isArtifactsToolCall(manifest, request, SAVE_ARTIFACT_TOOL);
71
+ }
72
+ /** True when `request` calls `read_artifact` (R2b C11) of the `nylorun.artifacts` capability. */
73
+ export function isReadArtifactCall(manifest, request) {
74
+ return isArtifactsToolCall(manifest, request, READ_ARTIFACT_TOOL);
75
+ }
76
+ function isArtifactsToolCall(manifest, request, toolName) {
70
77
  return (request.kind === "tool" &&
71
78
  request.capabilityId === ARTIFACTS_CAPABILITY_ID &&
72
- request.toolName === SAVE_ARTIFACT_TOOL &&
73
- manifest?.capabilities.some((capability) => capability.id === ARTIFACTS_CAPABILITY_ID) === true);
79
+ request.toolName === toolName &&
80
+ manifest?.capabilities.some((capability) => capability.id === ARTIFACTS_CAPABILITY_ID &&
81
+ capability.tools?.some((tool) => tool.name === toolName) === true) === true);
74
82
  }
75
83
  /** True when `request` calls a tool of a declared MCP server (every one is remote). */
76
84
  export function isRemoteMcpCall(routing, request) {
@@ -94,6 +102,8 @@ export async function callMcpTool(mcp, routing, request, signal) {
94
102
  args: request.input,
95
103
  manifest: routing.rootManifest,
96
104
  effectId: request.effectId,
105
+ // A read-only or idempotent tool's lost answer is the model's to retry (R2b C7, Q15).
106
+ retrySafe: tool.annotations?.readOnlyHint === true || tool.annotations?.idempotentHint === true,
97
107
  signal,
98
108
  });
99
109
  }
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * The Record seam of the Harness API: core's journal for the effects of a run. `recordIntent`
3
3
  * journals an effect before anything runs it, and either answers it from the journal, hands it
4
- * to core's own executors (flow work, delegation, `save_artifact` and the skill tools), tells
5
- * the harness to `execute` it (model calls; MCP, HTTP and sandbox tools; a flow's HTTP stages
6
- * and HTTP verifiers), or fails a tool the Runtime cannot run (one that would run the
7
- * developer's code, R2 M6). `recordOutcome`
8
- * records what the harness's call returned. Both run under the advance's lease: every write is
9
- * epoch-checked (`ownedSession`), and a lost epoch writes nothing.
4
+ * to core's own executors (flow work, delegation, `save_artifact`, `read_artifact`,
5
+ * `tool_search` and the skill tools), tells the harness to `execute` it (model calls; MCP, HTTP
6
+ * and sandbox tools, a deferred MCP tool's `tool_call` among them, whose effect names the tool;
7
+ * a flow's HTTP stages and HTTP verifiers), or fails a tool the Runtime cannot run (one that
8
+ * would run the developer's code, R2 M6). `recordOutcome` records what the harness's call returned, an
9
+ * agent's MCP or HTTP tool result first shaped to fit (R2b C11). Both run under the advance's
10
+ * lease: every write is epoch-checked (`ownedSession`), and a lost epoch writes nothing.
10
11
  *
11
12
  * Rows store the request's hash (`requestHash`) and, for a model call, the request without its
12
13
  * prompt: drift is a hash compare. Rows written before carry their full request, hashed on read.
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * The Record seam of the Harness API: core's journal for the effects of a run. `recordIntent`
3
3
  * journals an effect before anything runs it, and either answers it from the journal, hands it
4
- * to core's own executors (flow work, delegation, `save_artifact` and the skill tools), tells
5
- * the harness to `execute` it (model calls; MCP, HTTP and sandbox tools; a flow's HTTP stages
6
- * and HTTP verifiers), or fails a tool the Runtime cannot run (one that would run the
7
- * developer's code, R2 M6). `recordOutcome`
8
- * records what the harness's call returned. Both run under the advance's lease: every write is
9
- * epoch-checked (`ownedSession`), and a lost epoch writes nothing.
4
+ * to core's own executors (flow work, delegation, `save_artifact`, `read_artifact`,
5
+ * `tool_search` and the skill tools), tells the harness to `execute` it (model calls; MCP, HTTP
6
+ * and sandbox tools, a deferred MCP tool's `tool_call` among them, whose effect names the tool;
7
+ * a flow's HTTP stages and HTTP verifiers), or fails a tool the Runtime cannot run (one that
8
+ * would run the developer's code, R2 M6). `recordOutcome` records what the harness's call returned, an
9
+ * agent's MCP or HTTP tool result first shaped to fit (R2b C11). Both run under the advance's
10
+ * lease: every write is epoch-checked (`ownedSession`), and a lost epoch writes nothing.
10
11
  *
11
12
  * Rows store the request's hash (`requestHash`) and, for a model call, the request without its
12
13
  * prompt: drift is a hash compare. Rows written before carry their full request, hashed on read.
@@ -14,9 +15,12 @@
14
15
  import { HarnessApiError, effectRequestHash, } from "@nylorun/core/harness-api";
15
16
  import { isFlowEffect, isFlowToolEffect, recordVerdict, settleAgentEffect, } from "../core/flow-host.js";
16
17
  import { sandboxCapabilityOf } from "../sandbox/capability.js";
17
- import { isSaveArtifactCall } from "../harness/calls.js";
18
- import { callSaveArtifact } from "../tenant/artifact-tool.js";
18
+ import { abortOn, isReadArtifactCall, isSaveArtifactCall } from "../harness/calls.js";
19
+ import { callReadArtifact, callSaveArtifact } from "../tenant/artifact-tool.js";
20
+ import { mayNeedShaping, resultBytes, resultLimit, shapeToolOutcome, } from "../tenant/tool-results.js";
19
21
  import { callSkillTool, isSkillToolCall } from "../tenant/skill-tool.js";
22
+ import { callToolSearch, isToolSearchCall } from "../tenant/tool-search.js";
23
+ import { turnManifestOf } from "../tenant/session.js";
20
24
  import { isOwnershipLost } from "../store/ownership.js";
21
25
  import { manifestFor, mcpToolOf } from "../mcp/snapshot.js";
22
26
  import { isHttpToolCall } from "../gates/http-tool.js";
@@ -109,17 +113,24 @@ export async function recordIntent(scope, effect, requestHash) {
109
113
  throw new Error(`Agent '${request.agent?.id ?? ""}' is not used as a tool`);
110
114
  const executed = request.kind === "model" ||
111
115
  (request.kind === "tool" &&
112
- (mcpToolOf(s.mcpSnapshot, request) !== undefined ||
116
+ // An MCP tool the turn's manifest disables is not one (R2b C9).
117
+ (mcpToolOf(s.mcpSnapshot, request, turnManifestOf(s)) !== undefined ||
113
118
  // HTTP tools (R2 M3) cross the Tool Gate.
114
119
  isHttpToolCall(s.manifest, request) ||
115
120
  sandboxCapabilityOf(agentManifest, request.capabilityId, request.toolName) !== undefined ||
116
- // `save_artifact` (F8.1) runs beside the sandbox tools, and the skill tools (R2 M4).
121
+ // `save_artifact` (F8.1) runs beside the sandbox tools, as do `read_artifact` (R2b
122
+ // C11) and the skill tools (R2 M4).
117
123
  isSaveArtifactCall(agentManifest, request) ||
118
- isSkillToolCall(agentManifest, request)));
124
+ isReadArtifactCall(agentManifest, request) ||
125
+ isSkillToolCall(agentManifest, request) ||
126
+ // `tool_search` (R2b C10) reads the session's pinned MCP snapshot: core serves it.
127
+ isToolSearchCall(agentManifest, request)));
119
128
  if (!executed) {
120
129
  // A tool that would run the developer's code: refused at save (a turn's manifest only
121
- // removes tools), so this is a backstop. The model sees the failure; nothing runs.
122
- const outcome = { value: unrunnableTool(request) };
130
+ // removes tools), so this is a backstop, as is an MCP tool the turn disabled, which the
131
+ // model was never offered. The model sees the failure; nothing runs.
132
+ const disabled = request.kind === "tool" && mcpToolOf(s.mcpSnapshot, request) !== undefined;
133
+ const outcome = { value: disabled ? disabledTool(request) : unrunnableTool(request) };
123
134
  await t.put("effects", request.effectId, { request, requestHash, status: "completed", outcome });
124
135
  const transcript = toolCompleted(request, outcome.value);
125
136
  if (transcript)
@@ -135,13 +146,22 @@ export async function recordIntent(scope, effect, requestHash) {
135
146
  // workspace capability, wherever the sandbox is.
136
147
  if (request.kind === "tool" && isSaveArtifactCall(agentManifest, request))
137
148
  return "save";
149
+ // `read_artifact` reads the session's artifacts: core serves it, with or without a sandbox.
150
+ if (isReadArtifactCall(agentManifest, request))
151
+ return "read";
138
152
  // The skill tools read the agent's definition files: core serves them.
139
153
  if (isSkillToolCall(agentManifest, request))
140
154
  return { skill: agentManifest };
155
+ if (isToolSearchCall(agentManifest, request))
156
+ return "search";
141
157
  return { status: "execute" };
142
158
  });
143
159
  if (answer === "save")
144
160
  return runInCore(scope, request, () => callSaveArtifact(scope.ctx, request, scope.signal));
161
+ if (answer === "read")
162
+ return runInCore(scope, request, () => callReadArtifact(scope.ctx, request, scope.signal));
163
+ if (answer === "search")
164
+ return runInCore(scope, request, () => callToolSearch(scope.ctx, request));
145
165
  if (typeof answer === "object" && "skill" in answer)
146
166
  return runInCore(scope, request, () => callSkillTool(scope.ctx, answer.skill, request, scope.signal));
147
167
  if (answer !== "flow")
@@ -151,9 +171,10 @@ export async function recordIntent(scope, effect, requestHash) {
151
171
  return resolveNewFlowEffect(ctx, request, signal, lease);
152
172
  }
153
173
  /**
154
- * Runs a tool core serves (`save_artifact`, F8.1; the skill tools, R2 M4) for the run and records
155
- * its outcome, as a harness would record a call it ran: the run gets the outcome as the intent's
156
- * answer.
174
+ * Runs a tool core serves (`save_artifact`, F8.1; `read_artifact`, R2b C11; `tool_search`, R2b
175
+ * C10; the skill tools, R2 M4) for the run and records its outcome, as a harness would record a
176
+ * call it ran: the run gets
177
+ * the outcome as the intent's answer.
157
178
  */
158
179
  async function runInCore(scope, request, call) {
159
180
  let value;
@@ -169,6 +190,14 @@ async function runInCore(scope, request, call) {
169
190
  }
170
191
  return recordOutcome(scope, request.effectId, { value });
171
192
  }
193
+ /** The failed outcome of an MCP tool the turn's manifest disables (R2b C9). */
194
+ function disabledTool(request) {
195
+ return {
196
+ kind: "failed",
197
+ code: "tool.disabled",
198
+ message: `Tool '${request.toolName ?? ""}' is disabled by its MCP server's tools settings.`,
199
+ };
200
+ }
172
201
  /** The failed outcome of a tool the Runtime cannot run: it would run the developer's code. */
173
202
  function unrunnableTool(request) {
174
203
  return {
@@ -199,7 +228,7 @@ export async function recordOutcome(scope, effectId, result) {
199
228
  });
200
229
  return { status: "uncertain" };
201
230
  }
202
- const { value } = result;
231
+ const value = await shaped(scope, effectId, result.value);
203
232
  return ctx.store.tx(async (t) => {
204
233
  const s = await t.assertEpoch(lease.sessionId, lease.epoch);
205
234
  const effect = await t.get("effects", effectId);
@@ -234,6 +263,59 @@ export async function recordOutcome(scope, effectId, result) {
234
263
  return { status: "completed", outcome: effect.outcome };
235
264
  });
236
265
  }
266
+ /**
267
+ * The outcome of an agent's remote MCP or HTTP tool call, shaped to fit before anything records
268
+ * it (R2b C11, `tenant/tool-results.ts`): what does not fit becomes the session's artifacts. Any
269
+ * other outcome, and one a cancel will discard, as it is. Only a cancel stops the upload: after a
270
+ * shutdown or a deadline the outcome is still recorded, so the next advance replays it.
271
+ *
272
+ * Its cap is what its step's budget has left (`resultLimit`): a step's results are one transcript
273
+ * entry. Parallel results are counted under the session lock, each reserving its share on its
274
+ * row (`resultBytes`) before it is shaped, so two never spend the same room.
275
+ */
276
+ async function shaped(scope, effectId, value) {
277
+ const { ctx, lease, signal } = scope;
278
+ if (abortKind(signal) === "cancel" || !mayNeedShaping(value))
279
+ return value;
280
+ const call = await ctx.store.tx(async (t) => {
281
+ const s = await t.lockSession(lease.sessionId);
282
+ const effect = await t.get("effects", effectId);
283
+ if (!s || !effect || effect.status === "completed")
284
+ return undefined;
285
+ const request = effect.request;
286
+ // An agent's tool; a flow's HTTP stage or verifier names no capability, and its output is data.
287
+ if (request.kind !== "tool" || request.capabilityId === undefined)
288
+ return undefined;
289
+ const mcp = mcpToolOf(s.mcpSnapshot, request) !== undefined;
290
+ if (!mcp && !isHttpToolCall(s.manifest, request))
291
+ return undefined;
292
+ const step = stepOf(request);
293
+ let used = 0;
294
+ if (step !== undefined)
295
+ for (const other of await t.effectsForTurn(s.id, request.turnId, [
296
+ "invoking",
297
+ "completed",
298
+ ])) {
299
+ if (other.request.effectId === effectId || stepOf(other.request) !== step)
300
+ continue;
301
+ used +=
302
+ other.resultBytes ??
303
+ (other.status === "completed" ? resultBytes(other.outcome?.value) : 0);
304
+ }
305
+ const limit = resultLimit(used);
306
+ effect.resultBytes = Math.min(resultBytes(value), limit);
307
+ await t.put("effects", effectId, effect);
308
+ return { request, session: { id: s.id, ownerUserId: s.ownerUserId }, mcp, limit };
309
+ });
310
+ return call ? shapeToolOutcome(ctx, call, value, abortOn(signal, ["cancel"])) : value;
311
+ }
312
+ /** The model step a tool call answers, and whose agent: its results share one transcript entry. */
313
+ function stepOf(request) {
314
+ if (request.kind !== "tool")
315
+ return undefined;
316
+ const stepId = request.context?.stepId;
317
+ return typeof stepId === "string" ? JSON.stringify([request.agent?.id ?? null, stepId]) : undefined;
318
+ }
237
319
  /** What a journal row keeps of a request: a model call without its prompt. */
238
320
  function storedRequest(request) {
239
321
  if (request.kind !== "model" || request.input === undefined)