@parcha/agentrun-serve 0.1.0-beta.29 → 0.1.0-beta.31

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 (172) hide show
  1. package/CHANGELOG.md +238 -0
  2. package/README.md +739 -116
  3. package/dist/adapters.d.ts +35 -6
  4. package/dist/adapters.js +152 -26
  5. package/dist/agents.d.ts +18 -7
  6. package/dist/agents.js +19 -10
  7. package/dist/app.js +101 -26
  8. package/dist/background.d.ts +29 -0
  9. package/dist/background.js +397 -0
  10. package/dist/box-egress-vm.d.ts +125 -0
  11. package/dist/box-egress-vm.js +350 -0
  12. package/dist/box-egress.d.ts +99 -0
  13. package/dist/box-egress.js +259 -0
  14. package/dist/box-image.d.ts +1 -1
  15. package/dist/box-image.js +9 -7
  16. package/dist/box-relay-vm.d.ts +84 -0
  17. package/dist/box-relay-vm.js +410 -0
  18. package/dist/box-relay.d.ts +8 -5
  19. package/dist/box-relay.js +14 -9
  20. package/dist/boxes.d.ts +28 -5
  21. package/dist/boxes.js +44 -18
  22. package/dist/browser-cost.d.ts +41 -0
  23. package/dist/browser-cost.js +158 -0
  24. package/dist/cap.d.ts +130 -21
  25. package/dist/cap.js +339 -67
  26. package/dist/cli.js +30 -4
  27. package/dist/coding-tools.d.ts +7 -3
  28. package/dist/coding-tools.js +16 -10
  29. package/dist/console/THIRD-PARTY-NOTICES.txt +580 -0
  30. package/dist/console/assets/index-CnJBf7Lc.js +486 -0
  31. package/dist/console/assets/index-DUHbWcIP.css +1 -0
  32. package/dist/console/index.html +2 -2
  33. package/dist/content-filter.d.ts +7 -0
  34. package/dist/content-filter.js +23 -0
  35. package/dist/decision-rows.d.ts +11 -3
  36. package/dist/decision-rows.js +18 -11
  37. package/dist/decisions.d.ts +3 -3
  38. package/dist/decisions.js +4 -3
  39. package/dist/errors.d.ts +2 -0
  40. package/dist/errors.js +5 -2
  41. package/dist/files.d.ts +9 -1
  42. package/dist/files.js +10 -2
  43. package/dist/grade.d.ts +11 -11
  44. package/dist/grade.js +43 -55
  45. package/dist/graph-served.d.ts +23 -0
  46. package/dist/graph-served.js +75 -0
  47. package/dist/graph-view.d.ts +7 -5
  48. package/dist/graph-view.js +10 -3
  49. package/dist/harness-app.d.ts +4 -2
  50. package/dist/harness-app.js +110 -19
  51. package/dist/harness-opencode/adapter.d.ts +45 -0
  52. package/dist/harness-opencode/adapter.js +272 -0
  53. package/dist/harness-opencode/binary.d.ts +18 -0
  54. package/dist/harness-opencode/binary.js +87 -0
  55. package/dist/harness-opencode/config.d.ts +24 -0
  56. package/dist/harness-opencode/config.js +61 -0
  57. package/dist/harness-opencode/harness.d.ts +77 -0
  58. package/dist/harness-opencode/harness.js +620 -0
  59. package/dist/harness-opencode/host/mcp.d.ts +40 -0
  60. package/dist/harness-opencode/host/mcp.js +208 -0
  61. package/dist/harness-opencode/host/policy.d.ts +71 -0
  62. package/dist/harness-opencode/host/policy.js +419 -0
  63. package/dist/harness-opencode/host/server.d.ts +55 -0
  64. package/dist/harness-opencode/host/server.js +165 -0
  65. package/dist/harness-opencode/index.d.ts +9 -0
  66. package/dist/harness-opencode/index.js +6 -0
  67. package/dist/harness-opencode/verify.d.ts +32 -0
  68. package/dist/harness-opencode/verify.js +75 -0
  69. package/dist/harness-opencode/versions.d.ts +33 -0
  70. package/dist/harness-opencode/versions.js +64 -0
  71. package/dist/harness.d.ts +4 -1
  72. package/dist/harness.js +5 -2
  73. package/dist/host-check.d.ts +12 -0
  74. package/dist/host-check.js +34 -0
  75. package/dist/host-tools.d.ts +12 -2
  76. package/dist/host-tools.js +26 -12
  77. package/dist/host.js +6 -2
  78. package/dist/hosted-search.js +5 -1
  79. package/dist/import.d.ts +2 -2
  80. package/dist/import.js +4 -3
  81. package/dist/key-check.d.ts +3 -2
  82. package/dist/key-check.js +7 -3
  83. package/dist/key-refusal.js +8 -4
  84. package/dist/key-words.d.ts +37 -0
  85. package/dist/key-words.js +40 -0
  86. package/dist/learn/check.d.ts +10 -1
  87. package/dist/learn/check.js +19 -1
  88. package/dist/learn/grade.d.ts +6 -2
  89. package/dist/learn/grade.js +28 -14
  90. package/dist/learn/index.d.ts +3 -3
  91. package/dist/learn/index.js +3 -3
  92. package/dist/learn/notes.js +2 -1
  93. package/dist/learn/reflect.d.ts +2 -2
  94. package/dist/learn/reflect.js +53 -5
  95. package/dist/learn/secrets.js +28 -8
  96. package/dist/learn/skills.d.ts +19 -4
  97. package/dist/learn/skills.js +95 -6
  98. package/dist/learned-workflow.d.ts +59 -0
  99. package/dist/learned-workflow.js +191 -0
  100. package/dist/learning-routes.d.ts +30 -1
  101. package/dist/learning-routes.js +173 -19
  102. package/dist/learning-run.d.ts +8 -6
  103. package/dist/learning-run.js +70 -27
  104. package/dist/learning-words.d.ts +12 -5
  105. package/dist/learning-words.js +34 -13
  106. package/dist/learning.d.ts +52 -3
  107. package/dist/learning.js +204 -4
  108. package/dist/mcp-gateway.d.ts +96 -0
  109. package/dist/mcp-gateway.js +408 -0
  110. package/dist/mcp-meta.d.ts +17 -0
  111. package/dist/mcp-meta.js +38 -0
  112. package/dist/mcp-servers.d.ts +33 -9
  113. package/dist/mcp-servers.js +58 -10
  114. package/dist/mcp.js +5 -5
  115. package/dist/model.d.ts +13 -11
  116. package/dist/model.js +11 -10
  117. package/dist/openapi/agentrun-v2.openapi.json +1 -0
  118. package/dist/openapi.js +407 -54
  119. package/dist/page.d.ts +2 -0
  120. package/dist/page.js +3 -0
  121. package/dist/paid-calls.d.ts +5 -1
  122. package/dist/paid-calls.js +1 -1
  123. package/dist/pause.d.ts +2 -0
  124. package/dist/place.d.ts +29 -5
  125. package/dist/place.js +51 -11
  126. package/dist/print.d.ts +7 -0
  127. package/dist/print.js +13 -0
  128. package/dist/propose.d.ts +2 -0
  129. package/dist/propose.js +38 -8
  130. package/dist/request-start.d.ts +12 -4
  131. package/dist/request-start.js +139 -15
  132. package/dist/run-box.d.ts +56 -4
  133. package/dist/run-box.js +183 -23
  134. package/dist/runnable.d.ts +13 -3
  135. package/dist/runnable.js +63 -6
  136. package/dist/runs.d.ts +266 -37
  137. package/dist/runs.js +884 -90
  138. package/dist/secrets.d.ts +27 -0
  139. package/dist/secrets.js +38 -2
  140. package/dist/server.d.ts +141 -36
  141. package/dist/server.js +562 -118
  142. package/dist/sign-ins.d.ts +99 -0
  143. package/dist/sign-ins.js +288 -0
  144. package/dist/skills.d.ts +16 -4
  145. package/dist/skills.js +35 -7
  146. package/dist/stats.d.ts +7 -2
  147. package/dist/stats.js +28 -7
  148. package/dist/tool-gate.d.ts +281 -0
  149. package/dist/tool-gate.js +585 -0
  150. package/dist/tool-keys.d.ts +48 -11
  151. package/dist/tool-keys.js +46 -8
  152. package/dist/tool-plane.d.ts +116 -27
  153. package/dist/tool-plane.js +232 -50
  154. package/dist/tools.d.ts +69 -14
  155. package/dist/tools.js +148 -26
  156. package/dist/trigger-secrets.d.ts +32 -0
  157. package/dist/trigger-secrets.js +143 -0
  158. package/dist/triggers.d.ts +439 -0
  159. package/dist/triggers.js +863 -0
  160. package/dist/turn-end.d.ts +3 -2
  161. package/dist/turn-end.js +3 -2
  162. package/dist/usage-lines.d.ts +26 -0
  163. package/dist/usage-lines.js +28 -0
  164. package/dist/validate.d.ts +2 -0
  165. package/dist/validate.js +11 -2
  166. package/dist/web-fetch.js +3 -3
  167. package/dist/webhooks.d.ts +11 -4
  168. package/dist/webhooks.js +17 -1
  169. package/launcher/agentrun-serve +1 -1
  170. package/package.json +16 -14
  171. package/dist/console/assets/index-BN7oVcO4.js +0 -445
  172. package/dist/console/assets/index-By-jkqki.css +0 -1
package/dist/app.js CHANGED
@@ -8,11 +8,11 @@
8
8
  // workflow ends the app writes the run's final status, with the outcome, and releases the run. The server carries
9
9
  // it back once this process has exited: this process holds the run's store until then.
10
10
  // First: the secrets serve hands this process over a pipe, read and the pipe closed before anything else runs.
11
- import { authContextOf, withSecrets } from "./secrets.js";
11
+ import { authContextOf, mcpCredentials, withSecrets } from "./secrets.js";
12
12
  import { mkdir } from "node:fs/promises";
13
13
  import { BACKGROUND_CONTEXT as BACKGROUND } from "@earendil-works/chord/context";
14
14
  import { basename, join, resolve } from "node:path";
15
- import { createRegistry, ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
15
+ import { createRegistry, defineExtension, ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
16
16
  import { openStoreReader, RUN_STORE_FILE } from "@parcha/pi-durable-agent";
17
17
  import { RUN_RESUMED } from "@parcha/pi-durable-agent/timeline";
18
18
  import { settleIn } from "./paid-calls.js";
@@ -21,10 +21,12 @@ import { codingSource } from "./coding-tools.js";
21
21
  import { cancelledIn, markRequests } from "./request-start.js";
22
22
  import { decisionProvidersOf } from "./decisions.js";
23
23
  import { FOLLOW_UP_OPEN_ENV, readJson, RUN_META } from "./files.js";
24
- import { adapterFor, claudeModel, claudePriceModel, claudeSkillCheck, codexModel, openRouterOnly, thinkingFor } from "./adapters.js";
24
+ import { adapterFor, claudeModel, claudePriceModel, claudeSkillCheck, codexModel, learnedRoute, learnedWorkflowsOff, openRouterOnly, thinkingFor } from "./adapters.js";
25
25
  import { DEFAULT_HARNESS, FAUX_HARNESS_ENV } from "./harness.js";
26
26
  import { adapterRun } from "./harness-app.js";
27
- import { withLearned } from "./learning.js";
27
+ import { MCP_PIN, mcpGateway, mcpNameOf, mcpRegistrations } from "./mcp-gateway.js";
28
+ import { pinnedWorkflows, readPin, withLearned } from "./learning.js";
29
+ import { LEARNED_WORKFLOW_TOOL, learnedWorkflowTool } from "./learned-workflow.js";
28
30
  import { completer } from "./llm.js";
29
31
  import { searchOn } from "./hosted-search.js";
30
32
  import { imageLoader } from "./pdf-pages.js";
@@ -32,12 +34,14 @@ import { EFFORT_ENV, llmTiers, nodeTiers, runModels, thinkingOf, workflowNodes }
32
34
  import { gated, liveView, nodeLog, readLiveView } from "./pause.js";
33
35
  import { remotePlace, storedPlace } from "./place.js";
34
36
  import { runBox, runBoxProvider } from "./run-box.js";
37
+ import { planeOf } from "./tool-plane.js";
35
38
  import { ASK_TOOL, toolOf, workflowTools } from "./tools.js";
36
- import { promptOf, systemOf } from "./skills.js";
39
+ import { promptOf, readsFiles, systemOf } from "./skills.js";
37
40
  import { EnvironmentLine, environmentLine } from "./environment.js";
38
41
  import { effortGuidance } from "./effort-guidance.js";
39
42
  import { isObject } from "./validate.js";
40
43
  import { mirrorWrites } from "./workspace.js";
44
+ import { deniedText, runGate } from "./tool-gate.js";
41
45
  /** The workflow adapters this host gives every run: model nodes, and calls `via: "tool"` to the table's tools. Typed
42
46
  * questions (`runJudge`) run too where the host sets a decision provider's key (decisions.ts). */
43
47
  export const WORKFLOW_ADAPTERS = ["runNode", "runEffect"];
@@ -72,12 +76,12 @@ async function claudeModels(env, ref) {
72
76
  const { builtinModels } = await import("@earendil-works/pi-ai/providers/all");
73
77
  return { models: builtinModels({ authContext: authContextOf(env) }), model: choice.model, byTier: new Map([["default", choice.model]]), unpriced: new Set() };
74
78
  }
75
- /** codex's model and pi-ai's catalog that prices it, where codex reaches OpenAI only through OpenRouter (codexModel: no
79
+ /** codex's model (or opencode's, `harness`, on codex's route) and pi-ai's catalog that prices it, where it reaches OpenAI only through OpenRouter (codexModel: no
76
80
  * OpenAI key needed); anywhere else, the run's usual models (runModels). */
77
- async function codexModels(env, ref) {
78
- if (env[FAUX_HARNESS_ENV] || !openRouterOnly("codex", env))
79
- return runModels(env, ref, ["default"], "codex");
80
- const choice = await codexModel(env, ref);
81
+ async function codexModels(env, ref, harness = "codex") {
82
+ if (env[FAUX_HARNESS_ENV] || !openRouterOnly(harness, env))
83
+ return runModels(env, ref, ["default"], harness);
84
+ const choice = await codexModel(env, ref, harness);
81
85
  if ("why" in choice)
82
86
  throw new Error(choice.why);
83
87
  const { builtinModels } = await import("@earendil-works/pi-ai/providers/all");
@@ -103,14 +107,21 @@ export default async function app(where) {
103
107
  const env = withSecrets(process.env);
104
108
  const workflowNodeList = revision.kind === "workflow" ? workflowNodes(revision.body.workflow, (await import("@parcha/agentrun-dsl")).inspectWorkflow(revision.body.workflow)) : [];
105
109
  // codex's model as a save resolves it (codexModel): an OpenAI model it reaches through OpenRouter alone needs no OpenAI key.
106
- const chosen = revision.kind === "agentic" && revision.body.harness === "claude-agent" && !env[FAUX_HARNESS_ENV] ? await claudeModels(env, revision.body.model) : revision.kind === "agentic" && revision.body.harness === "codex" ? await codexModels(env, revision.body.model) : revision.kind === "agentic" ? await runModels(env, revision.body.model, ["default"], revision.body.harness) : await runModels(env, undefined, llmTiers(workflowNodeList, nodeTiers(workflowNodeList)));
110
+ const chosen = revision.kind === "agentic" && revision.body.harness === "claude-agent" && !env[FAUX_HARNESS_ENV] ? await claudeModels(env, revision.body.model) : revision.kind === "agentic" && (revision.body.harness === "codex" || revision.body.harness === "opencode") ? await codexModels(env, revision.body.model, revision.body.harness) : revision.kind === "agentic" ? await runModels(env, revision.body.model, ["default"], revision.body.harness) : await runModels(env, undefined, llmTiers(workflowNodeList, nodeTiers(workflowNodeList)));
107
111
  const { model, byTier } = chosen;
108
112
  // Read before the run opens its store: what it spent in earlier processes counts against its cap.
109
113
  const cap = capOf(process.env);
110
114
  // Each request is marked as it leaves (request-start.ts), inside the cap: a request the cap refuses never leaves, and
111
115
  // gets a start mark of its own, which its error entry answers.
112
116
  const marked = await markRequests(chosen.models, cancelledIn(where.root));
113
- const models = cap === undefined ? marked.models : await capModels(marked.models, cap, spentIn(join(where.store, RUN_STORE_FILE)), { unpriced: chosen.unpriced, refused: marked.refused });
117
+ // On another harness these models carry only the run's own requests (a learned workflow's, a hosted search's), under
118
+ // the run's one total, which the harness keeps (cap.ts capModels' `joined`): what they cost in earlier processes is
119
+ // read here, as the harness's booking does not hold it.
120
+ const joined = revision.kind === "agentic" && Boolean(revision.body.harness) && revision.body.harness !== DEFAULT_HARNESS;
121
+ const models = cap === undefined ? marked.models : await capModels(marked.models, cap, spentIn(join(where.store, RUN_STORE_FILE), { sides: joined }), { unpriced: chosen.unpriced, refused: marked.refused, joined });
122
+ // web_search's own request to the model provider, when it resolves native, goes through the run's models (hosted-search.ts):
123
+ // on another harness too, for a learned workflow's call node.
124
+ searchOn(models);
114
125
  const thinkingLevel = thinkingOf(process.env, revision.kind === "agentic" ? revision.body.thinking : undefined);
115
126
  // The run's place, as its start resolved and kept it (serve.json): in a box, its coding tools run there (run-box.ts).
116
127
  const meta = await readJson(join(where.root, RUN_META));
@@ -122,6 +133,17 @@ export default async function app(where) {
122
133
  if (box?.mirror)
123
134
  mirrorWrites(where.work, box.mirror);
124
135
  const coding = codingSource(place, box);
136
+ // Every tool call the run's model makes passes the run's tool gate (tool-gate.ts): pi's tools wrapped by name where the
137
+ // registry composes each agent's, another harness's host tools in their call. An approved call runs on this coding
138
+ // source, as the run's own would.
139
+ const toolGate = runGate(where, run.id, revision);
140
+ // Its remote MCP servers, as its first launch pinned them, with the credentials this launch handed it (mcp-gateway.ts).
141
+ const pinned = (await readJson(join(where.root, MCP_PIN)));
142
+ // Its records (a held ask, a priced call's mark and charge, an unpriced call's "not metered") go on the run's root, once
143
+ // the run is open.
144
+ let runHarness;
145
+ const commit = () => runHarness && ((change) => runHarness.commit(change, BACKGROUND));
146
+ const gateway = pinned && Object.keys(pinned).length ? mcpGateway({ pin: pinned, credentials: mcpCredentials(), work: () => where.work, commit, gate: toolGate }) : undefined;
125
147
  const registry = createRegistry();
126
148
  // An agentic run's system prompt starts with the date and the workspace (environment.ts). On pi it is a section,
127
149
  // installed first so it comes before every other extension's and the instructions.
@@ -131,20 +153,40 @@ export default async function app(where) {
131
153
  // What the run's agent learned goes into its instructions, and the skills it wrote join the revision's, the one path for
132
154
  // every harness (learning.ts).
133
155
  const learned = revision.kind === "agentic" ? await withLearned(where.root, where.work, revision.body.instructions, Object.keys(revision.body.skills)) : { instructions: "", skills: [] };
156
+ // The workflows the agent learned that its run may run, as one tool (learned-workflow.ts), when its pin names any: on
157
+ // the run's models, as side requests under its cap, each model node and `llm` call on the run's model of its tier,
158
+ // else the run's own (the only one the run was made with, and handed the key of); the `llm` tool's completion for a
159
+ // call node that names it; its call nodes on the agent's tools its run's plane resolves (the plane its harness and
160
+ // its tools read).
161
+ const flows = revision.kind === "agentic" ? await pinnedWorkflows(where.root, await readPin(where.root)) : [];
162
+ // On claude-agent and codex, on the route the harness's own requests take (learnedRoute): on OpenRouter alone,
163
+ // OpenRouter's name for the run's model, whose key the run holds, never the vendor's provider.
164
+ const learnedHarness = revision.kind === "agentic" ? revision.body.harness : undefined;
165
+ const learnedModel = (tier) => {
166
+ const ref = byTier.get(tier ?? "default") ?? byTier.get("default");
167
+ return ref && learnedHarness && learnedHarness !== DEFAULT_HARNESS ? learnedRoute(learnedHarness, env, ref) : ref;
168
+ };
169
+ // A second guard beside the pin's (learning-run.ts): a run whose harness's requests go where a learned workflow's could
170
+ // not follow gets no tool, whatever its pin says.
171
+ const learnedTool = flows.length && revision.kind === "agentic" && !(await learnedWorkflowsOff(revision.body.harness, env, revision.body.model))
172
+ ? learnedWorkflowTool({ workflows: flows, granted: revision.body.tools, plane: planeOf(process.env), policy: toolGate.policy, env, work: where.work, models: models, modelOf: learnedModel, complete: completer(models, learnedModel, thinkingLevel) })
173
+ : undefined;
134
174
  // The run's effort, as its thinking level reads it: its note on how to work follows the instructions on every harness
135
175
  // (effort-guidance.ts). None without an effort.
136
176
  const guidance = effortGuidance(process.env[EFFORT_ENV]);
137
177
  if (revision.kind === "agentic" && revision.body.harness && revision.body.harness !== DEFAULT_HARNESS) {
138
178
  const harness = revision.body.harness;
139
179
  // claude-agent loads the skills its adapter takes itself (its Skill tool), and its prompt keeps only the ones it would
140
- // refuse; codex's prompt, and a faux harness's, keep every skill (skills.ts).
180
+ // refuse; codex's prompt, and a faux harness's, name each skill and its file when the run reads its files (it reads
181
+ // the SKILL.md when it fits), else keep every skill whole (skills.ts).
141
182
  const check = harness === "claude-agent" && !env[FAUX_HARNESS_ENV] ? await claudeSkillCheck() : undefined;
142
- const { system: instructions, native } = await systemOf(learned.instructions, learned.skills, where.work, check);
183
+ const { system: instructions, native } = await systemOf(learned.instructions, learned.skills, where.work, check, { onDemand: readsFiles(revision.body.tools) });
143
184
  // The session's system prompt starts with the line, made as this process opens it: a run resumed on a later day is
144
185
  // opened with that day.
145
- const spec = { key: `run:${run.id}`, label: run.agent, system: [environmentLine(), instructions, guidance].filter((text) => Boolean(text)), context: "fresh", thinking: thinkingFor(harness, thinkingLevel) };
146
- // ask_person stays pi's (runnable.ts refuses a run that names it here); the file tools are pi's own.
147
- const named = revision.body.tools.filter((name) => name !== ASK_TOOL);
186
+ const spec = { key: `run:${run.id}`, label: run.agent, system: [environmentLine(), instructions, guidance].filter((text) => Boolean(text)), context: "fresh", thinking: thinkingFor(harness, thinkingLevel), ...(gateway ? { mcp: gateway } : {}) };
187
+ // ask_person stays pi's (runnable.ts refuses a run that names it here); the file tools are pi's own; the MCP tools
188
+ // are the harness's own client's, at the session's proxy.
189
+ const named = revision.body.tools.filter((name) => name !== ASK_TOOL && !mcpNameOf(name));
148
190
  // The harness's own files (its homes, its state) go in a private directory beside the workspace, never in it. It works
149
191
  // in the workspace itself (where.work), the directory the run's file tools confine to: a path the model builds from
150
192
  // its working directory is then one they take.
@@ -159,12 +201,11 @@ export default async function app(where) {
159
201
  // The cancel mark serve writes is read here too, as pi's model wrapper reads it (request-start.ts).
160
202
  // The run's stop record (serve.json, as serve saved it before a stop's abort): a turn it names is not resumed.
161
203
  const stopRecord = { ...(typeof meta?.stopped_at === "string" ? { stopped_at: meta.stopped_at } : {}), ...(typeof meta?.stopped_after === "number" ? { stopped_after: meta.stopped_after } : {}) };
162
- const { door, onOpen } = adapterRun(make, harness, spec, BACKGROUND, cap, named, dir, guard, cancelledIn(where.root), where.work, coding.tools, stopRecord);
204
+ const { door, onOpen } = adapterRun(make, harness, spec, BACKGROUND, cap, learnedTool ? [...named, LEARNED_WORKFLOW_TOOL] : named, dir, guard, cancelledIn(where.root), where.work, coding.tools, stopRecord, toolGate, learnedTool ? [learnedTool] : []);
163
205
  // A paid host tool's settle goes to the run's root, as on pi (paid-calls.ts settleIn).
164
- return { models: {}, registry: createRegistry(), door, onOpen: async (durable) => (settleIn(rootRecordOf(durable.harness)), onOpen(durable)), beforeResume: markResumed };
206
+ // A learned workflow's model requests go through the run's models, marked on its store once it opens (markRequests).
207
+ return { models: {}, registry: createRegistry(), door, onOpen: async (durable) => ((runHarness = durable.harness), settleIn(rootRecordOf(durable.harness)), learnedTool && marked.open(durable), onOpen(durable)), beforeResume: markResumed };
165
208
  }
166
- // web_search's own request to the model provider, when it resolves native, goes through the run's models (hosted-search.ts).
167
- searchOn(models);
168
209
  // Each tool the revision names is bound once, from the table: its extensions installed here, its documents written in
169
210
  // each conversation's creating commit, what it settles done before anything resumes, and what it lets go done before
170
211
  // the run is released.
@@ -200,9 +241,29 @@ export default async function app(where) {
200
241
  await durable.setStatus("sleeping", { reason: "drained" }, { wakeAt: at === null ? null : new Date(at).toISOString() });
201
242
  };
202
243
  if (revision.kind === "agentic") {
203
- const tools = (await Promise.all(revision.body.tools.map((name) => bind(name)))).flatMap((binding) => binding.tools);
204
- const instructions = [await promptOf(learned.instructions, learned.skills, where.work), guidance].filter(Boolean).join("\n\n");
205
- return { models, registry, ...hooks, ...(stops.length ? { wake } : {}), root: { agent: { model, thinkingLevel, instructions, tools } }, onOpen: async (durable) => (settleIn(rootRecordOf(durable.harness)), marked.open(durable)) };
244
+ const tools = (await Promise.all(revision.body.tools.filter((name) => !mcpNameOf(name)).map((name) => bind(name)))).flatMap((binding) => binding.tools);
245
+ // The MCP tools pass the gate in the gateway (mcp-gateway.ts) and carry its mark (GATED_BY_GATEWAY), so the gate's
246
+ // extension leaves exactly those; every other tool, any mcp__ name included, it wraps.
247
+ const mcp = gateway ? mcpRegistrations(gateway) : [];
248
+ if (mcp.length)
249
+ registry.install(defineExtension({ name: "mcp", tools: mcp }));
250
+ tools.push(...mcp);
251
+ if (learnedTool) {
252
+ registry.install(defineExtension({ name: "learned-workflow", tools: [learnedTool] }));
253
+ tools.push(learnedTool);
254
+ }
255
+ registry.install(toolGate.extension(tools));
256
+ // Each skill named with its file when the run reads its files (it reads the SKILL.md when it fits), else whole.
257
+ const instructions = [await promptOf(learned.instructions, learned.skills, where.work, { onDemand: readsFiles(revision.body.tools) }), guidance].filter(Boolean).join("\n\n");
258
+ // The calls that wait for a person's approval are read as the run opens, and their answers acted on from then.
259
+ const onOpen = async (durable) => {
260
+ runHarness = durable.harness;
261
+ settleIn(rootRecordOf(durable.harness));
262
+ marked.open(durable);
263
+ await toolGate.load(durable, BACKGROUND, coding.tools, learnedTool ? [learnedTool] : []);
264
+ toolGate.start();
265
+ };
266
+ return { models, registry, ...hooks, ...(stops.length ? { wake } : {}), root: { agent: { model, thinkingLevel, instructions, tools } }, onOpen };
206
267
  }
207
268
  const input = isObject(meta?.input) ? meta.input : {};
208
269
  const [{ BACKGROUND_CONTEXT }, { createWorkflowExtension, decisionRecovery, durableDecisions, nodeRunner, taskScope }, { EffectFailure, inspectWorkflow, judgeFromProviders }, { withRecovery, workspaceFiles }, { startWorkflowRevision }, decisionProviders] = await Promise.all([
@@ -217,6 +278,11 @@ export default async function app(where) {
217
278
  const nodeTools = new Map();
218
279
  for (const name of workflowTools(workflowNodeList).nodes)
219
280
  nodeTools.set(name, await bind(name, true));
281
+ // A model node's tools pass the gate too; one set to ask is refused there, as no node can wait for a person. A call
282
+ // node is the author's own step, which cannot wait for an approval: runnable.ts refuses a run whose call node calls a tool
283
+ // set to deny or ask, and a tool set to either since is refused as its call node runs (runEffect).
284
+ const gatedTools = toolGate.extension([...nodeTools.values()].flatMap((binding) => binding.tools));
285
+ registry.install(gatedTools);
220
286
  // A decision node's images are files of the run's work directory (pdf_pages draws them there).
221
287
  const loadImage = imageLoader(where.work);
222
288
  const log = nodeLog(where.root);
@@ -244,6 +310,14 @@ export default async function app(where) {
244
310
  const at = { path: executionPath ?? null, item: item?.index ?? null, tool: node.tool, label: node.label };
245
311
  log({ t: "effect.start", ...at, input, at: new Date().toISOString() });
246
312
  try {
313
+ // Its tool's permission now, the gate's one rule read as each call node runs (tool-gate.ts policy): a tool set to
314
+ // deny since the run started is refused in the gate's words, and one set to ask too, since a call node cannot wait
315
+ // for an approval. Neither runs.
316
+ const permission = toolGate.policy(node.tool);
317
+ if (permission === "deny")
318
+ throw new Error(deniedText(node.tool));
319
+ if (permission === "ask")
320
+ throw new Error(`This ${node.tool} call needs a person's approval, which a workflow's call node cannot wait for; it was not run.`);
247
321
  const durable = opened;
248
322
  const ledger = durable ? { call: `${node.label}@${executionPath ?? ""}${item ? `#${item.index}` : ""}`, record: rootRecordOf(durable.harness) } : undefined;
249
323
  const output = await effect(input, { signal, env, work: where.work, complete, ...(ledger ? { ledger } : {}) });
@@ -252,7 +326,8 @@ export default async function app(where) {
252
326
  }
253
327
  catch (error) {
254
328
  const message = String(error?.message ?? error);
255
- log({ t: "effect.end", ...at, status: "failed", error: message.slice(0, 2000), at: new Date().toISOString() });
329
+ // Whole up to 64 KB, so the server redacts it before it cuts it for the graph (graph-view.ts); a longer one is marked cut.
330
+ log({ t: "effect.end", ...at, status: "failed", error: message.slice(0, 65_536), ...(message.length > 65_536 ? { error_truncated: true } : {}), at: new Date().toISOString() });
256
331
  // An abort (a cancel, the node's deadline) stays a throw: the driver keeps its effect unknown. A tool that failed on
257
332
  // its own (an unreadable PDF, a model's refusal) answered: its failure is the effect's outcome, completed, and is
258
333
  // thrown past the driver as the call node's typed failure (`failing`), which a collecting map ends that item on.
@@ -272,7 +347,7 @@ export default async function app(where) {
272
347
  /** A model node's agent: its tier's model, the tools it names, and the extensions they come from. */
273
348
  const nodeAgent = (node) => {
274
349
  const bindings = (node.tools ?? []).flatMap((name) => nodeTools.get(name) ?? []);
275
- return { model: byTier.get(node.tier ?? "default"), thinkingLevel, extensions: [...new Set([coding.extension, ...bindings.flatMap((b) => b.extensions)])], tools: bindings.flatMap((b) => b.tools) };
350
+ return { model: byTier.get(node.tier ?? "default"), thinkingLevel, extensions: [...new Set([coding.extension, ...bindings.flatMap((b) => b.extensions), gatedTools])], tools: bindings.flatMap((b) => b.tools) };
276
351
  };
277
352
  const nodes = nodeRunner({ agent: (node) => nodeAgent(node) });
278
353
  /** The node log's starts and ends, from the workflow's events: what a graph shows of a node's timing. */
@@ -0,0 +1,29 @@
1
+ import type { Context } from "@earendil-works/chord";
2
+ import { type ToolRegistration } from "@earendil-works/pi-durable";
3
+ import type { BoxProvider } from "@parcha/pi-transport";
4
+ /** The most background processes that run at once in one run's box. */
5
+ export declare const BACKGROUND_MAX = 8;
6
+ /** The most bytes of output one background process's log keeps: later output is dropped, and the process runs on. */
7
+ export declare const BACKGROUND_LOG_BYTES: number;
8
+ /** The most bytes of output one `bash_output` call answers, and its default. */
9
+ export declare const BACKGROUND_PAGE_BYTES: number;
10
+ /** The most bytes one `bash_input` call writes. */
11
+ export declare const BACKGROUND_INPUT_BYTES: number;
12
+ /** The most processes `bash_list` names: every running one, then the newest that ended. */
13
+ export declare const BACKGROUND_LIST_MAX = 40;
14
+ /** Where a background process's log and state live, under the run's work/. */
15
+ export declare const BACKGROUND_DIR = "background";
16
+ /** The signals `bash_signal` sends. */
17
+ export declare const BACKGROUND_SIGNALS: readonly ["TERM", "INT", "KILL"];
18
+ /** The tools, by name. */
19
+ export declare const BACKGROUND_TOOLS: readonly ["bash_background", "bash_output", "bash_input", "bash_signal", "bash_list"];
20
+ /** The tools that run a command in the box, never on this server: bash, and the background tools. The tool gate holds
21
+ * each to bash's setting (tool-gate.ts SHELL_FAMILY); under bash: ask, bash_output, bash_list and bash_signal run unasked
22
+ * (tool-gate.ts SHELL_WATCHERS), since an id reaches only this run's own processes (idOf, noSuch). */
23
+ export declare const SHELL_TOOLS: readonly string[];
24
+ /** The background tools, each run on the environment its call gives (the box's, coding-tools.ts). */
25
+ export declare const backgroundTools: () => ToolRegistration[];
26
+ /** The run's end: every background process still running in run `runId`'s box (found by `label`) is sent TERM, then
27
+ * KILL after 3 s, and marked as ended at the run's end, so its runner writes its exit code and its log is whole before
28
+ * the box is pulled and destroyed. How many it ended; a run with no box ends none. */
29
+ export declare function endBackground(boxes: BoxProvider, label: Record<string, string>, context: Context): Promise<number>;