@parcha/agentrun-serve 0.1.0-beta.42 → 0.1.0-beta.44

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 (181) hide show
  1. package/CHANGELOG.md +126 -0
  2. package/README.md +221 -31
  3. package/dist/cli.js +42 -8
  4. package/dist/composio-preset.d.ts +4 -3
  5. package/dist/composio-preset.js +12 -4
  6. package/dist/composio-triggers.d.ts +20 -2
  7. package/dist/composio-triggers.js +183 -17
  8. package/dist/computer-lost.d.ts +11 -0
  9. package/dist/computer-lost.js +19 -0
  10. package/dist/computer-record.d.ts +10 -2
  11. package/dist/computer-record.js +50 -18
  12. package/dist/computer-setting.d.ts +1 -0
  13. package/dist/computer-setting.js +2 -2
  14. package/dist/console/assets/index-BUktRvS-.css +1 -0
  15. package/dist/console/assets/index-nB9kTfIN.js +711 -0
  16. package/dist/console/index.html +2 -2
  17. package/dist/decisions.js +0 -4
  18. package/dist/errors.d.ts +1 -0
  19. package/dist/errors.js +3 -0
  20. package/dist/harness-table.d.ts +51 -3
  21. package/dist/harness-table.js +101 -13
  22. package/dist/harness.d.ts +18 -1
  23. package/dist/harness.js +21 -9
  24. package/dist/host.d.ts +12 -10
  25. package/dist/host.js +18 -16
  26. package/dist/import-sources.js +25 -5
  27. package/dist/import-worker.d.ts +3 -0
  28. package/dist/import-worker.js +7 -5
  29. package/dist/import.d.ts +22 -1
  30. package/dist/import.js +141 -28
  31. package/dist/learn/audit.d.ts +123 -0
  32. package/dist/learn/audit.js +159 -0
  33. package/dist/learn/check.d.ts +3 -0
  34. package/dist/learn/check.js +1 -1
  35. package/dist/learn/compile.d.ts +50 -74
  36. package/dist/learn/compile.js +90 -124
  37. package/dist/learn/delete.d.ts +17 -0
  38. package/dist/learn/delete.js +44 -0
  39. package/dist/learn/folder.d.ts +9 -0
  40. package/dist/learn/folder.js +13 -2
  41. package/dist/learn/promote.d.ts +92 -200
  42. package/dist/learn/promote.js +65 -187
  43. package/dist/learn/replay-calls.d.ts +5 -0
  44. package/dist/learn/replay-calls.js +12 -1
  45. package/dist/learn/secrets.d.ts +8 -0
  46. package/dist/learn/secrets.js +12 -0
  47. package/dist/learn/skills.d.ts +4 -0
  48. package/dist/learn/skills.js +19 -5
  49. package/dist/learn/workflow-nodes.d.ts +23 -3
  50. package/dist/learn/workflow-nodes.js +30 -12
  51. package/dist/learned-nodes.d.ts +9 -34
  52. package/dist/learned-nodes.js +11 -23
  53. package/dist/learned-workflow.d.ts +6 -8
  54. package/dist/learned-workflow.js +18 -57
  55. package/dist/learning-batch.d.ts +13 -5
  56. package/dist/learning-batch.js +34 -8
  57. package/dist/learning-cost.d.ts +111 -4
  58. package/dist/learning-cost.js +120 -3
  59. package/dist/learning-evals.d.ts +2 -0
  60. package/dist/learning-evals.js +4 -3
  61. package/dist/learning-routes.d.ts +8 -0
  62. package/dist/learning-routes.js +18 -1
  63. package/dist/learning-setting.d.ts +16 -1
  64. package/dist/learning-setting.js +34 -2
  65. package/dist/learning.d.ts +1 -1
  66. package/dist/listen-first.d.ts +30 -0
  67. package/dist/listen-first.js +115 -0
  68. package/dist/llms.txt +2 -1
  69. package/dist/mcp-servers-store.d.ts +12 -12
  70. package/dist/model.d.ts +8 -4
  71. package/dist/model.js +16 -8
  72. package/dist/openapi.js +203 -87
  73. package/dist/owner.d.ts +8 -0
  74. package/dist/owner.js +11 -0
  75. package/dist/paid-calls.d.ts +6 -0
  76. package/dist/paid-calls.js +11 -1
  77. package/dist/place.d.ts +8 -3
  78. package/dist/place.js +13 -9
  79. package/dist/provider.js +15 -1
  80. package/dist/run-launch.d.ts +6 -3
  81. package/dist/run-launch.js +2 -0
  82. package/dist/run-learning-served.d.ts +1 -1
  83. package/dist/run-model-key.d.ts +64 -0
  84. package/dist/run-model-key.js +147 -0
  85. package/dist/runner/adapter-loop.d.ts +10 -0
  86. package/dist/runner/adapter-loop.js +26 -4
  87. package/dist/runner/adapter-run-spec.d.ts +6 -3
  88. package/dist/runner/adapter-run-spec.js +83 -25
  89. package/dist/runner/adapters/index.d.ts +4 -2
  90. package/dist/runner/adapters/index.js +13 -4
  91. package/dist/runner/audit.d.ts +68 -0
  92. package/dist/runner/audit.js +349 -0
  93. package/dist/runner/beside.d.ts +2 -1
  94. package/dist/runner/beside.js +6 -3
  95. package/dist/runner/claim-secrets.d.ts +49 -0
  96. package/dist/runner/claim-secrets.js +110 -1
  97. package/dist/runner/cli-run-spec.d.ts +2 -1
  98. package/dist/runner/cli-run-spec.js +44 -15
  99. package/dist/runner/commands.d.ts +3 -6
  100. package/dist/runner/feedback-round.d.ts +3 -2
  101. package/dist/runner/feedback-round.js +8 -7
  102. package/dist/runner/harness-loops.js +31 -23
  103. package/dist/runner/journal-kinds.d.ts +5 -7
  104. package/dist/runner/journal-kinds.js +0 -7
  105. package/dist/runner/learning-dispatch.d.ts +0 -16
  106. package/dist/runner/learning-dispatch.js +4 -47
  107. package/dist/runner/learning-end.d.ts +55 -13
  108. package/dist/runner/learning-end.js +128 -44
  109. package/dist/runner/learning-factory.d.ts +1 -1
  110. package/dist/runner/learning-factory.js +1 -4
  111. package/dist/runner/learning-loop.d.ts +2 -38
  112. package/dist/runner/learning-loop.js +8 -116
  113. package/dist/runner/loop-contract.d.ts +6 -0
  114. package/dist/runner/loop-contract.js +4 -0
  115. package/dist/runner/loop.js +6 -2
  116. package/dist/runner/main.d.ts +1 -0
  117. package/dist/runner/main.js +25 -8
  118. package/dist/runner/memory-claim.d.ts +9 -6
  119. package/dist/runner/memory-claim.js +12 -9
  120. package/dist/runner/promotion.d.ts +55 -54
  121. package/dist/runner/promotion.js +232 -585
  122. package/dist/runner/reflect-hook.d.ts +14 -0
  123. package/dist/runner/reflect-hook.js +28 -3
  124. package/dist/runner/runner.js +57 -4
  125. package/dist/runner/store-secrets.js +9 -5
  126. package/dist/runner/workflow-first.d.ts +6 -5
  127. package/dist/runner/workflow-first.js +17 -12
  128. package/dist/runner/workflow-spec.js +10 -2
  129. package/dist/runs-over-store.d.ts +5 -4
  130. package/dist/runs-over-store.js +13 -3
  131. package/dist/sdk/workflows.d.ts +1 -1
  132. package/dist/server.d.ts +64 -50
  133. package/dist/server.js +516 -134
  134. package/dist/store/index.d.ts +2 -2
  135. package/dist/store/index.js +1 -1
  136. package/dist/store/seal.d.ts +20 -2
  137. package/dist/store/seal.js +24 -8
  138. package/dist/store/sqlite/run-launch.d.ts +4 -1
  139. package/dist/store/sqlite/run-launch.js +12 -0
  140. package/dist/store/sqlite/store.d.ts +3 -2
  141. package/dist/store/sqlite/store.js +119 -16
  142. package/dist/store/store.d.ts +41 -3
  143. package/dist/store-reads/analytics.js +11 -1
  144. package/dist/store-reads/learning.d.ts +57 -7
  145. package/dist/store-reads/learning.js +194 -28
  146. package/dist/store-reads/loop.d.ts +4 -39
  147. package/dist/store-reads/loop.js +10 -79
  148. package/dist/store-reads/stats.js +13 -3
  149. package/dist/store-view.d.ts +5 -1
  150. package/dist/store-view.js +12 -1
  151. package/dist/telemetry.d.ts +59 -0
  152. package/dist/telemetry.js +153 -0
  153. package/dist/tool-keys-store.d.ts +1 -1
  154. package/dist/tool-keys.d.ts +2 -2
  155. package/dist/tool-plane.d.ts +12 -12
  156. package/dist/tools.d.ts +11 -11
  157. package/dist/tools.js +18 -5
  158. package/dist/trigger-adapter.d.ts +3 -0
  159. package/dist/trigger-adapters.d.ts +4 -0
  160. package/dist/trigger-adapters.js +13 -0
  161. package/dist/trigger-composio.d.ts +58 -1
  162. package/dist/trigger-composio.js +160 -6
  163. package/dist/triggers.d.ts +4 -0
  164. package/dist/triggers.js +22 -3
  165. package/dist/validate.d.ts +11 -7
  166. package/dist/validate.js +8 -7
  167. package/dist/workspace/computer-for-claim.d.ts +15 -5
  168. package/dist/workspace/computer-for-claim.js +228 -32
  169. package/dist/workspace/resolve-computer.d.ts +8 -2
  170. package/dist/workspace/resolve-computer.js +1 -1
  171. package/dist/workspace/run-tools.d.ts +1 -1
  172. package/dist/workspace/run-tools.js +14 -3
  173. package/package.json +10 -9
  174. package/dist/console/assets/index-C3gNPz58.css +0 -1
  175. package/dist/console/assets/index-DYJ_wj27.js +0 -711
  176. package/dist/learn/general.d.ts +0 -69
  177. package/dist/learn/general.js +0 -75
  178. package/dist/learn/refit.d.ts +0 -48
  179. package/dist/learn/refit.js +0 -264
  180. package/dist/runner/step-guard.d.ts +0 -62
  181. package/dist/runner/step-guard.js +0 -93
package/dist/owner.d.ts CHANGED
@@ -58,6 +58,14 @@ export declare class Owner {
58
58
  attach(baseUrl: string): void;
59
59
  /** The setup page's link: with its code when setup needs one, a new code printed (never answered) when none is good. */
60
60
  setupLink(): string;
61
+ /** A setup code to use, for whoever asks with no credential: the one that stands is kept (never voided here), and a
62
+ * new one is printed only when none is good (expired, or spent). Answers which, and when the code now good runs
63
+ * out; never the code. Null where there is none to give: an open setup, or a setup under way, whose spent code
64
+ * needs no successor. */
65
+ renewSetupCode(): {
66
+ printed: boolean;
67
+ until: number;
68
+ } | null;
61
69
  /** Whether `given` is the setup code. None given (`missing`) is no try: it never counts against the printed code. A
62
70
  * wrong one counts, and the fifth voids it and prints a new one at once (`new`), as an expired one does. */
63
71
  private spendCode;
package/dist/owner.js CHANGED
@@ -173,6 +173,17 @@ export class Owner {
173
173
  }
174
174
  return `${this.base}/setup#code=${this.setupCode.code}`;
175
175
  }
176
+ /** A setup code to use, for whoever asks with no credential: the one that stands is kept (never voided here), and a
177
+ * new one is printed only when none is good (expired, or spent). Answers which, and when the code now good runs
178
+ * out; never the code. Null where there is none to give: an open setup, or a setup under way, whose spent code
179
+ * needs no successor. */
180
+ renewSetupCode() {
181
+ if (this.openSetup || this.settingUp)
182
+ return null;
183
+ const before = this.setupCode;
184
+ this.setupLink();
185
+ return { printed: this.setupCode !== before, until: this.setupCode.until };
186
+ }
176
187
  /** Whether `given` is the setup code. None given (`missing`) is no try: it never counts against the printed code. A
177
188
  * wrong one counts, and the fifth voids it and prints a new one at once (`new`), as an expired one does. */
178
189
  spendCode(given) {
@@ -82,6 +82,9 @@ export declare const bookedIn: <T>(ledger: Ledger | undefined, work: () => Promi
82
82
  export declare function paidRequest(fee: Fee, send: () => Promise<Response>): Promise<Response>;
83
83
  /** paidStep's chargeOf for a step that charged nothing and is given back: its mark settled with no charge row. */
84
84
  export declare const GIVEN_BACK = "given back";
85
+ /** Whether the current work runs inside a call's ledger (bookedIn): a host tool call of the run's, or a workflow call
86
+ * node's tool. A lost sandbox is started again only for one (computer-for-claim.ts lostAware), never for the run's end. */
87
+ export declare const inToolCall: () => boolean;
85
88
  /** The run's one ledger the current call books on (Ledger.paid), when it has one: the runner's. */
86
89
  export declare const paidLedger: () => PaidBooking | undefined;
87
90
  /** Stops the run the current call books in, in `words`: handed to its pause (Ledger.pause) as a cap refusal no raise
@@ -89,6 +92,9 @@ export declare const paidLedger: () => PaidBooking | undefined;
89
92
  * run ends failed in them, past its cap too, as a cap refusal for good ends it. Its code is the cap's, so the refused
90
93
  * request reads as the cap's refusal (no model error row of its own). */
91
94
  export declare const stopRun: (words: string) => Promise<void>;
95
+ /** Stops the run whose pause is `pause` (its ledger's: the adapter loop's RunLedger.pause), in `words`, as stopRun does,
96
+ * from any caller: one outside a tool call (the owner's computer channel) too. */
97
+ export declare const stopRunWith: (pause: NonNullable<Ledger["pause"]>) => (words: string) => Promise<void>;
92
98
  /** One paid step on the run's one ledger `paid`, under call `call`: admitted at `holdUsd` (refused unsent: the cap's words,
93
99
  * or why its hold could not be kept), then `run`, then settled at `chargeOf` its outcome (null: at its hold; GIVEN_BACK:
94
100
  * given back, nothing charged, as a hold-only step's: a request its provider refused unrun leaves no charge row). One refused
@@ -101,6 +101,9 @@ export async function paidRequest(fee, send) {
101
101
  export const GIVEN_BACK = "given back";
102
102
  /** Each paid request of this process on the run's one ledger, numbered within its call (one call may make several). */
103
103
  let paidSeq = 0;
104
+ /** Whether the current work runs inside a call's ledger (bookedIn): a host tool call of the run's, or a workflow call
105
+ * node's tool. A lost sandbox is started again only for one (computer-for-claim.ts lostAware), never for the run's end. */
106
+ export const inToolCall = () => ledgers.getStore() !== undefined;
104
107
  /** The run's one ledger the current call books on (Ledger.paid), when it has one: the runner's. */
105
108
  export const paidLedger = () => ledgers.getStore()?.paid;
106
109
  /** Stops the run the current call books in, in `words`: handed to its pause (Ledger.pause) as a cap refusal no raise
@@ -108,7 +111,14 @@ export const paidLedger = () => ledgers.getStore()?.paid;
108
111
  * run ends failed in them, past its cap too, as a cap refusal for good ends it. Its code is the cap's, so the refused
109
112
  * request reads as the cap's refusal (no model error row of its own). */
110
113
  export const stopRun = async (words) => {
111
- await ledgers.getStore()?.pause?.({ refuse: words, code: SPEND_CAP_REACHED, forGood: true });
114
+ const pause = ledgers.getStore()?.pause;
115
+ if (pause)
116
+ await stopRunWith(pause)(words);
117
+ };
118
+ /** Stops the run whose pause is `pause` (its ledger's: the adapter loop's RunLedger.pause), in `words`, as stopRun does,
119
+ * from any caller: one outside a tool call (the owner's computer channel) too. */
120
+ export const stopRunWith = (pause) => async (words) => {
121
+ await pause({ refuse: words, code: SPEND_CAP_REACHED, forGood: true });
112
122
  };
113
123
  /** One paid step on the run's one ledger `paid`, under call `call`: admitted at `holdUsd` (refused unsent: the cap's words,
114
124
  * or why its hold could not be kept), then `run`, then settled at `chargeOf` its outcome (null: at its hold; GIVEN_BACK:
package/dist/place.d.ts CHANGED
@@ -22,9 +22,14 @@ export declare function newAgentPlace(registry: readonly BoxOffer[]): {
22
22
  why: string;
23
23
  };
24
24
  /** The place a revision that names none resolves to, at its create and at each run alike: this server's default place
25
- * (newAgentPlace) for an agentic revision; the runner for a workflow, which is never given a box, and for one that names
26
- * a tool the box refuses (`refused`: the browser, runner-only for now). */
27
- export declare function defaultPlaceOf(kind: "agentic" | "workflow", tools: readonly unknown[], registry: readonly BoxOffer[], refused: (tool: string) => boolean): Place;
25
+ * (newAgentPlace) for an agentic revision with a tool that runs only in a box (`flags`, tools.ts: bash and the
26
+ * background tools); the runner for a workflow, which is never given a box, for one whose tools need no shell (none, or
27
+ * only read and write: a container would only hold up its start), and for one that names a tool the box refuses (the
28
+ * browser, runner-only for now). */
29
+ export declare function defaultPlaceOf(kind: "agentic" | "workflow", tools: readonly unknown[], registry: readonly BoxOffer[], flags: (tool: string) => {
30
+ readonly sandboxOnly?: true;
31
+ readonly runnerOnly?: true;
32
+ } | undefined): Place;
28
33
  /** The tools a new agent in the default box is given when its definition names none: a shell, and the background tools
29
34
  * that start, read, feed, signal and list commands that outlive one call (background.ts SHELL_TOOLS, which a test holds
30
35
  * this list to; written out here, since background.ts reads this module). A definition that names its tools, an empty
package/dist/place.js CHANGED
@@ -7,11 +7,12 @@
7
7
  // of that provider (computer-setting.ts computerOfPlace), which the run's claim starts: Docker, Daytona or Modal. A harness's own tools in the box, when they come, are an option of sandbox, never a third
8
8
  // mode.
9
9
  //
10
- // The box is a new agent's default machine: an agentic definition that names no place is saved, at its create, with the
11
- // Docker box as its place wherever this server has docker, and with bash when it names no tools
12
- // (newAgentPlace). A revision that names no place resolves the same way at each run (defaultPlaceOf): this server's
13
- // default_place, so where docker is there now it runs in the Docker box, and the runner where it is not. A workflow, and a
14
- // revision with a tool the box refuses, run on the runner.
10
+ // The box is a new agent's default machine for a shell: an agentic definition that names no place and no tools is saved,
11
+ // at its create, with the Docker box as its place wherever this server has docker, and with bash (newAgentPlace). One that
12
+ // names its tools gets the box only when one of them needs a shell; else it keeps no place and runs on the runner, with
13
+ // no container to wait for. A revision that names no place resolves the same way at each run (defaultPlaceOf): with a
14
+ // shell tool, this server's default_place, so where docker is there now it runs in the Docker box, and the runner where it
15
+ // is not. A workflow, a revision whose tools need no shell, and one with a tool the box refuses run on the runner.
15
16
  import { BOX_IDS, BOXES_ENV } from "./place-offers.js";
16
17
  import { sentence } from "./errors.js";
17
18
  export const PLACE_MODES = ["runner", "sandbox"];
@@ -39,10 +40,13 @@ export function newAgentPlace(registry) {
39
40
  return { reason: `new agents run on the runner, because the docker box is unavailable here: ${why}`, why };
40
41
  }
41
42
  /** The place a revision that names none resolves to, at its create and at each run alike: this server's default place
42
- * (newAgentPlace) for an agentic revision; the runner for a workflow, which is never given a box, and for one that names
43
- * a tool the box refuses (`refused`: the browser, runner-only for now). */
44
- export function defaultPlaceOf(kind, tools, registry, refused) {
45
- if (kind === "workflow" || tools.some((tool) => typeof tool === "string" && refused(tool)))
43
+ * (newAgentPlace) for an agentic revision with a tool that runs only in a box (`flags`, tools.ts: bash and the
44
+ * background tools); the runner for a workflow, which is never given a box, for one whose tools need no shell (none, or
45
+ * only read and write: a container would only hold up its start), and for one that names a tool the box refuses (the
46
+ * browser, runner-only for now). */
47
+ export function defaultPlaceOf(kind, tools, registry, flags) {
48
+ const named = tools.flatMap((tool) => (typeof tool === "string" ? [flags(tool)] : []));
49
+ if (kind === "workflow" || named.some((tool) => tool?.runnerOnly) || !named.some((tool) => tool?.sandboxOnly))
46
50
  return { mode: "runner" };
47
51
  const fresh = newAgentPlace(registry);
48
52
  return "place" in fresh ? fresh.place : { mode: "runner" };
package/dist/provider.js CHANGED
@@ -59,6 +59,18 @@ export function gatewayBase(setting, value) {
59
59
  throw wrong(`is ${url.protocol.replace(/:$/, "")}, not https`);
60
60
  return `${url.origin}${url.pathname.replace(/\/+$/, "").replace(/\/v1$/, "")}`;
61
61
  }
62
+ /** Whether `base` (a gatewayBase answer) is on the origin of serve's own gateway (AGENT_MODEL_BASE_URL): there a run's own
63
+ * model key (run-model-key.ts) is sent, so the endpoint needs no standing key of the server's. */
64
+ function onServeGateway(env, base) {
65
+ if (!env.AGENT_MODEL_BASE_URL)
66
+ return false;
67
+ try {
68
+ return new URL(gatewayBase("AGENT_MODEL_BASE_URL", env.AGENT_MODEL_BASE_URL)).origin === new URL(base).origin;
69
+ }
70
+ catch {
71
+ return false;
72
+ }
73
+ }
62
74
  /**
63
75
  * Whether `wire` reaches its vendor's models here only through OpenRouter: OPENROUTER_API_KEY is set and none of the
64
76
  * wire's own settings is. Those win, as on a host with no OpenRouter key: the endpoint the host names (ANTHROPIC_BASE_URL
@@ -114,7 +126,9 @@ export function providerFor(wire, env, model, who) {
114
126
  // The endpoint first: one it refuses is the reason, and no key would fix it (no `keys`). An endpoint the host names
115
127
  // takes Anthropic's key alone: OpenRouter's never goes there.
116
128
  const origin = env.ANTHROPIC_BASE_URL ? gatewayBase("ANTHROPIC_BASE_URL", env.ANTHROPIC_BASE_URL) : ANTHROPIC_ORIGIN;
117
- if (!env.ANTHROPIC_API_KEY)
129
+ // An endpoint on serve's own gateway's origin takes each run's own key where the server holds none (run-model-key.ts):
130
+ // the harness runs here with no standing key, and a run with no key to spend is refused at its start.
131
+ if (!env.ANTHROPIC_API_KEY && !onServeGateway(env, origin))
118
132
  throw env.ANTHROPIC_BASE_URL ? new HarnessUnavailable(`${who} needs ANTHROPIC_API_KEY.`, undefined, ["ANTHROPIC_API_KEY"]) : new HarnessUnavailable(`${who} needs ANTHROPIC_API_KEY or OPENROUTER_API_KEY.`, undefined, ["ANTHROPIC_API_KEY", "OPENROUTER_API_KEY"]);
119
133
  return { origin, credential: () => env.ANTHROPIC_API_KEY ?? "", auth: "x-api-key", modelId: own, priceModel: { provider: "anthropic", modelId: own } };
120
134
  }
@@ -84,10 +84,13 @@ export interface RunLaunch {
84
84
  /** `false`: the run never starts on a learned workflow, even one that fits its input (RunCreate `workflow_first`,
85
85
  * runner/workflow-first.ts); its agent takes it from its first turn. Absent: a fitting one serves first. */
86
86
  readonly workflowFirst?: false;
87
- /** A replay run (runner/promotion.ts): learned workflow `workflow` at `digest`, run alone on graded run `of`'s input to
88
- * decide whether that revision serves first (learn/promote.ts). Its guards read record-only (learn/promote.ts recordOnly), a hand-back ends it
89
- * with no agent turn, and it asks nothing at its end. Absent: a run of its own. */
87
+ /** A replay run (runner/promotion.ts): learned workflow `workflow` at `digest`, run alone on the agent's own run `of`'s input to
88
+ * decide whether that revision serves first (learn/promote.ts). A hand-back ends it with no agent turn, and it asks nothing at its end. Absent: a run of its own. */
90
89
  readonly replay?: Replay;
90
+ /** A shadow run (runner/audit.ts): the audit of run `of`, which learned workflow `workflow` at `digest` served. The same
91
+ * agent on the same input with learning off: its answer is what the served answer is compared with (learn/audit.ts),
92
+ * it is no row of the run list, and it asks nothing at its end. Absent: a run of its own. */
93
+ readonly audit?: Replay;
91
94
  /** The trigger and firing that started the run, or none for a person's run. */
92
95
  readonly trigger?: RunTrigger;
93
96
  /** Who the first message is from, when not the person: kept only through the fail-safe below. */
@@ -70,6 +70,7 @@ export function launchBody(launch) {
70
70
  ...(launch.learning ? { learning: { given: launch.learning.given.map((note) => ({ file: note.file, name: note.name })), seed: launch.learning.seed, section: launch.learning.section, ...(launch.learning.seedNotes !== undefined ? { seedNotes: launch.learning.seedNotes } : {}), ...(launch.learning.notesLeftOut?.length ? { notesLeftOut: [...launch.learning.notesLeftOut] } : {}), ...(launch.learning.skills?.length ? { skills: [...launch.learning.skills] } : {}), ...(launch.learning.notesOnly ? { notesOnly: true } : {}), ...(launch.learning.workflows?.length ? { workflows: [...launch.learning.workflows] } : {}), ...(launch.learning.gradeModel ? { gradeModel: launch.learning.gradeModel } : {}), ...(launch.learning.seedFailed ? { seedFailed: launch.learning.seedFailed } : {}), ...(launch.learning.use ? { use: true } : {}) } } : {}),
71
71
  ...(launch.workflowFirst === false ? { workflow_first: false } : {}),
72
72
  ...(launch.replay ? { replay: { of: launch.replay.of, workflow: launch.replay.workflow, digest: launch.replay.digest } } : {}),
73
+ ...(launch.audit ? { audit: { of: launch.audit.of, workflow: launch.audit.workflow, digest: launch.audit.digest } } : {}),
73
74
  ...(launch.trigger ? { trigger: launch.trigger } : {}),
74
75
  ...(launch.inputOrigin ? { input_origin: launch.inputOrigin } : {}),
75
76
  ...(launch.followUp ? { follow_up: true } : {}),
@@ -124,6 +125,7 @@ export function readLaunchBody(stored) {
124
125
  ...(stored.learning !== undefined ? { learning: readLearning(stored.learning) ?? { given: [], seed: null, section: "" } } : {}),
125
126
  ...(stored.workflow_first === false ? { workflowFirst: false } : {}),
126
127
  ...(readReplay(stored.replay) ? { replay: readReplay(stored.replay) } : {}),
128
+ ...(readReplay(stored.audit) ? { audit: readReplay(stored.audit) } : {}),
127
129
  ...(trigger ? { trigger } : {}),
128
130
  ...(inputOrigin ? { inputOrigin } : {}),
129
131
  ...(stored.follow_up === true ? { followUp: true } : {}),
@@ -74,7 +74,7 @@ export declare function servedRunLearning(deps: {
74
74
  feedback_status: "running" | "ended" | "not_run" | null;
75
75
  source: "feedback" | "eval";
76
76
  graded_at: string;
77
- path: "agent" | "workflow" | "handed_back" | "replay";
77
+ path: "agent" | "workflow" | "replay" | "handed_back" | "audit";
78
78
  workflow_digest: string | null;
79
79
  feedback: "look_back" | null;
80
80
  run_id: string;
@@ -0,0 +1,64 @@
1
+ import { ApiError, type InputIssue } from "./errors.js";
2
+ /** The longest model key a run takes: a header value, never a document. */
3
+ export declare const MAX_MODEL_KEY_CHARS = 4096;
4
+ /** The origin of the model gateway a run's model key goes to (AGENT_MODEL_BASE_URL's, by the one base rule), or undefined
5
+ * when the server names none or one a key may not go to. */
6
+ export declare function runModelOrigin(env: NodeJS.ProcessEnv): string | undefined;
7
+ /** The variables a run's model key fills in a model route's env, for a gateway at `origin`: the gateway's key, and
8
+ * ANTHROPIC_API_KEY where ANTHROPIC_BASE_URL is that same gateway. */
9
+ export declare function runKeyNames(env: NodeJS.ProcessEnv, origin: string): string[];
10
+ /** A model key `body` hands, checked: text of 1 to MAX_MODEL_KEY_CHARS characters with no whitespace or control character
11
+ * (it goes in a header). Issues are pushed at `model_key`. */
12
+ export declare function modelKeyOf(value: unknown, issues: InputIssue[]): string | undefined;
13
+ /** 422 model_key_required: a run start, or a message that would start a turn, with no model key to spend: none handed,
14
+ * none the run holds, and none the server holds for its route. */
15
+ export declare const modelKeyRequired: (message: boolean) => ApiError;
16
+ /** What of a run says where its model requests go: its kind and its launch's pinned model. */
17
+ export type ModelKeyTarget = {
18
+ readonly kind: "agentic" | "workflow" | string;
19
+ readonly model?: {
20
+ readonly provider: string;
21
+ readonly modelId: string;
22
+ } | undefined;
23
+ readonly native?: boolean;
24
+ };
25
+ /** The variables a run's model route reads its key from when that route goes to the model gateway (a workflow's model
26
+ * nodes, pi's and codex's models, a CLI harness on Anthropic's wire where ANTHROPIC_BASE_URL is the gateway's origin);
27
+ * undefined for a route that goes elsewhere (a provider's own, the ChatGPT plan, the faux model) or a native run. */
28
+ export declare function gatewayKeyNames(env: NodeJS.ProcessEnv, run: ModelKeyTarget): string[] | undefined;
29
+ /** Whether a run of `run` with no key of its own has none to spend: its route goes to the gateway, and `env` (the
30
+ * server's, with the keys saved in Settings) holds no key it reads. */
31
+ export declare function serverKeyMissing(env: NodeJS.ProcessEnv, run: ModelKeyTarget): boolean;
32
+ /** Words for model spend tied to no run's key (`what`, a noun phrase) on a server that holds no gateway key (`env`, its
33
+ * keys saved in Settings among them): skipped or refused, never sent with no key. Undefined where the server holds one. */
34
+ export declare function noServerKeyFor(env: NodeJS.ProcessEnv, what: string): string | undefined;
35
+ /** The words of noServerKeyFor for `what`. */
36
+ export declare const noServerKeyWords: (what: string) => string;
37
+ /** Words for spend after the turn of a run that held its own model key (`what`): skipped, since that key is erased with
38
+ * the turn's end and revoked by its caller, and the server's key never pays for a run that brought its own. */
39
+ export declare const runKeyEndedFor: (what: string) => string;
40
+ /** Why a run's model key would never be used, so it is refused rather than kept unused; undefined where it applies: a
41
+ * workflow run (its model nodes go to the gateway), or an agentic run whose model goes to the gateway (pi's and codex's,
42
+ * MODEL_PROVIDER) or to Anthropic's endpoint on the gateway's origin (a CLI harness on Anthropic's wire). */
43
+ export declare function modelKeyMisfit(env: NodeJS.ProcessEnv, run: ModelKeyTarget): string | undefined;
44
+ /** A run's model key as a claim holds it: the gateway origin it was handed for, and the key now (a message's replaces it
45
+ * at the next command; none before the run's first key, and none once its launch no longer holds one). */
46
+ export interface HeldModelKey {
47
+ readonly origin: string;
48
+ key(): string | undefined;
49
+ }
50
+ /**
51
+ * `env` for a run's model routes when the run holds its own key: every property of `env` but each model key (the runner's
52
+ * own and any saved one never go), with the run's key read at each request (`held.key()`) in each variable it fills
53
+ * (runKeyNames). Those are getters, never enumerable: a spread of the result, a spawn given it or a tool's env made from
54
+ * it carries none of them. No ChatGPT sign-in either (a symbol, not copied): every model call tied to the run carries
55
+ * the run's key.
56
+ */
57
+ export declare function withRunModelKey(env: NodeJS.ProcessEnv, held: HeldModelKey): NodeJS.ProcessEnv;
58
+ /**
59
+ * `env` (a claim's model env on the server's keys, withModelKeys) for a run that holds no key of its own yet: each
60
+ * variable a run's key fills (runKeyNames) reads the run's key once a message hands one, and the server's value until
61
+ * then, at each request; every other variable as it is. So a run that started on the server's key spends a message's
62
+ * key from the request after that message on. Getters, never enumerable, as withRunModelKey's.
63
+ */
64
+ export declare function withRunModelKeyLater(env: NodeJS.ProcessEnv, held: HeldModelKey): NodeJS.ProcessEnv;
@@ -0,0 +1,147 @@
1
+ // A run's own model key: a key its caller hands with the run (`model_key` on POST /runs, POST /agents/{id}/runs and
2
+ // POST /runs/{id}/messages), such as a LiteLLM virtual key minted for that run, which every model request the run makes
3
+ // carries in place of the server's own key. AgentRun never mints, lists or revokes it: the caller owns its life.
4
+ //
5
+ // Where it goes: the model gateway at AGENT_MODEL_BASE_URL, which every pi model and every workflow model node is sent to,
6
+ // and Anthropic's Messages endpoint when ANTHROPIC_BASE_URL names that same gateway origin (a CLI harness on Anthropic's
7
+ // wire). It is sealed with the run, bound to that origin when handed (store/seal.ts RunModelKey), so a gateway moved since
8
+ // never receives it; it fills the gateway's key variable (and ANTHROPIC_API_KEY on that origin) in the model routes' env
9
+ // alone, read at each request, never enumerable and never on the env a tool, a harness or a sandbox runs under.
10
+ //
11
+ // No setting: what the server holds decides. A run with a model_key spends on it alone, outside its turn too (its
12
+ // subagents, learned nodes, end-of-run look-back), never on the server's key. A run without one spends on the server's
13
+ // key for its route, as before. A server that holds no key for a run's route (no AGENT_MODEL_API_KEY, nothing saved in
14
+ // Settings) is an operator saying "callers bring their own": a start or a message that would start a turn with no key
15
+ // to spend is 422 model_key_required, never a failure at the first model call. Spend after a run's turn ended (its key
16
+ // erased with that end, and revoked by its caller) or tied to no run, with no key to spend, is skipped or refused in words.
17
+ import { ApiError } from "./errors.js";
18
+ import { FAUX_ENV, GATEWAY_KEY_ENV, MODEL_PROVIDER, PROVIDER_KEYS } from "./model.js";
19
+ import { gatewayBase } from "./provider.js";
20
+ /** The longest model key a run takes: a header value, never a document. */
21
+ export const MAX_MODEL_KEY_CHARS = 4096;
22
+ /** The origin of the model gateway a run's model key goes to (AGENT_MODEL_BASE_URL's, by the one base rule), or undefined
23
+ * when the server names none or one a key may not go to. */
24
+ export function runModelOrigin(env) {
25
+ if (!env.AGENT_MODEL_BASE_URL)
26
+ return undefined;
27
+ try {
28
+ return new URL(gatewayBase("AGENT_MODEL_BASE_URL", env.AGENT_MODEL_BASE_URL)).origin;
29
+ }
30
+ catch {
31
+ return undefined;
32
+ }
33
+ }
34
+ /** Whether ANTHROPIC_BASE_URL names the gateway at `origin`: there a CLI harness on Anthropic's wire sends ANTHROPIC_API_KEY,
35
+ * which a run's key then fills. */
36
+ function anthropicOnGateway(env, origin) {
37
+ if (!env.ANTHROPIC_BASE_URL)
38
+ return false;
39
+ try {
40
+ return new URL(gatewayBase("ANTHROPIC_BASE_URL", env.ANTHROPIC_BASE_URL)).origin === origin;
41
+ }
42
+ catch {
43
+ return false;
44
+ }
45
+ }
46
+ /** The variables a run's model key fills in a model route's env, for a gateway at `origin`: the gateway's key, and
47
+ * ANTHROPIC_API_KEY where ANTHROPIC_BASE_URL is that same gateway. */
48
+ export function runKeyNames(env, origin) {
49
+ return [GATEWAY_KEY_ENV, ...(anthropicOnGateway(env, origin) ? ["ANTHROPIC_API_KEY"] : [])];
50
+ }
51
+ /** Every model key a model route reads by name: each of pi-ai's providers' and the gateway's. */
52
+ const MODEL_KEY_NAMES = new Set([...Object.values(PROVIDER_KEYS).flat(), GATEWAY_KEY_ENV]);
53
+ /** A model key `body` hands, checked: text of 1 to MAX_MODEL_KEY_CHARS characters with no whitespace or control character
54
+ * (it goes in a header). Issues are pushed at `model_key`. */
55
+ export function modelKeyOf(value, issues) {
56
+ if (typeof value !== "string")
57
+ return void issues.push({ field: "model_key", reason: "wrong_type", message: "model_key is the run's model key as text, such as a LiteLLM virtual key minted for this run." });
58
+ if (!value.length || value.length > MAX_MODEL_KEY_CHARS || /[\s\x00-\x1f\x7f]/.test(value))
59
+ return void issues.push({ field: "model_key", reason: "invalid", message: `model_key is 1 to ${MAX_MODEL_KEY_CHARS} characters with no space or control character.` });
60
+ return value;
61
+ }
62
+ /** 422 model_key_required: a run start, or a message that would start a turn, with no model key to spend: none handed,
63
+ * none the run holds, and none the server holds for its route. */
64
+ export const modelKeyRequired = (message) => new ApiError("model_key_required", `This server holds no model key for this run's model (AGENT_MODEL_API_KEY is not set and none is saved in Settings → Model keys), so each run's caller brings its own: send model_key with ${message ? "a message that starts a turn" : "the run"}, a key for the model gateway at AGENT_MODEL_BASE_URL (such as a LiteLLM virtual key) minted for ${message ? "that turn" : "this run"}.`, { fields: [{ field: "model_key", reason: "missing", message: "model_key is required here: the server holds no model key for this run's model." }] });
65
+ /** The variables a run's model route reads its key from when that route goes to the model gateway (a workflow's model
66
+ * nodes, pi's and codex's models, a CLI harness on Anthropic's wire where ANTHROPIC_BASE_URL is the gateway's origin);
67
+ * undefined for a route that goes elsewhere (a provider's own, the ChatGPT plan, the faux model) or a native run. */
68
+ export function gatewayKeyNames(env, run) {
69
+ const origin = runModelOrigin(env);
70
+ if (!origin || run.native || env[FAUX_ENV])
71
+ return undefined;
72
+ if (run.kind === "workflow" || run.model?.provider === MODEL_PROVIDER)
73
+ return [GATEWAY_KEY_ENV];
74
+ if (run.model?.provider === "anthropic" && anthropicOnGateway(env, origin))
75
+ return ["ANTHROPIC_API_KEY"];
76
+ return undefined;
77
+ }
78
+ /** Whether a run of `run` with no key of its own has none to spend: its route goes to the gateway, and `env` (the
79
+ * server's, with the keys saved in Settings) holds no key it reads. */
80
+ export function serverKeyMissing(env, run) {
81
+ const names = gatewayKeyNames(env, run);
82
+ return names !== undefined && !names.some((name) => env[name]);
83
+ }
84
+ /** Words for model spend tied to no run's key (`what`, a noun phrase) on a server that holds no gateway key (`env`, its
85
+ * keys saved in Settings among them): skipped or refused, never sent with no key. Undefined where the server holds one. */
86
+ export function noServerKeyFor(env, what) {
87
+ return runModelOrigin(env) && !env[GATEWAY_KEY_ENV] ? noServerKeyWords(what) : undefined;
88
+ }
89
+ /** The words of noServerKeyFor for `what`. */
90
+ export const noServerKeyWords = (what) => `${what} is skipped: it is tied to no run's model key, and this server holds none of its own (AGENT_MODEL_API_KEY is not set and none is saved in Settings → Model keys).`;
91
+ /** Words for spend after the turn of a run that held its own model key (`what`): skipped, since that key is erased with
92
+ * the turn's end and revoked by its caller, and the server's key never pays for a run that brought its own. */
93
+ export const runKeyEndedFor = (what) => `${what} is skipped: this run spent on its own model key, which is erased when its turn ends; the server's key never pays for a run that brought its own.`;
94
+ /** Why a run's model key would never be used, so it is refused rather than kept unused; undefined where it applies: a
95
+ * workflow run (its model nodes go to the gateway), or an agentic run whose model goes to the gateway (pi's and codex's,
96
+ * MODEL_PROVIDER) or to Anthropic's endpoint on the gateway's origin (a CLI harness on Anthropic's wire). */
97
+ export function modelKeyMisfit(env, run) {
98
+ const origin = runModelOrigin(env);
99
+ if (!origin)
100
+ return "This server sends no model through a gateway (AGENT_MODEL_BASE_URL is not set), so a run's model key has nowhere to go.";
101
+ if (run.native)
102
+ return "A native run's model runs on its own machine, never through this server's gateway, so a model key would never be used.";
103
+ if (run.kind === "workflow")
104
+ return undefined;
105
+ if (run.model?.provider === MODEL_PROVIDER)
106
+ return undefined;
107
+ if (run.model?.provider === "anthropic" && anthropicOnGateway(env, origin))
108
+ return undefined;
109
+ const named = run.model ? `${run.model.provider}/${run.model.modelId}` : "its harness's own model";
110
+ return `This run's model (${named}) is not sent through the gateway at AGENT_MODEL_BASE_URL, so its model key would never be used.`;
111
+ }
112
+ /**
113
+ * `env` for a run's model routes when the run holds its own key: every property of `env` but each model key (the runner's
114
+ * own and any saved one never go), with the run's key read at each request (`held.key()`) in each variable it fills
115
+ * (runKeyNames). Those are getters, never enumerable: a spread of the result, a spawn given it or a tool's env made from
116
+ * it carries none of them. No ChatGPT sign-in either (a symbol, not copied): every model call tied to the run carries
117
+ * the run's key.
118
+ */
119
+ export function withRunModelKey(env, held) {
120
+ const out = {};
121
+ for (const name of Object.getOwnPropertyNames(env)) {
122
+ if (!MODEL_KEY_NAMES.has(name))
123
+ Object.defineProperty(out, name, Object.getOwnPropertyDescriptor(env, name));
124
+ }
125
+ for (const name of runKeyNames(env, held.origin))
126
+ Object.defineProperty(out, name, { get: () => held.key(), enumerable: false, configurable: false });
127
+ return out;
128
+ }
129
+ /**
130
+ * `env` (a claim's model env on the server's keys, withModelKeys) for a run that holds no key of its own yet: each
131
+ * variable a run's key fills (runKeyNames) reads the run's key once a message hands one, and the server's value until
132
+ * then, at each request; every other variable as it is. So a run that started on the server's key spends a message's
133
+ * key from the request after that message on. Getters, never enumerable, as withRunModelKey's.
134
+ */
135
+ export function withRunModelKeyLater(env, held) {
136
+ const names = new Set(runKeyNames(env, held.origin));
137
+ const out = {};
138
+ for (const name of Object.getOwnPropertyNames(env)) {
139
+ if (!names.has(name))
140
+ Object.defineProperty(out, name, Object.getOwnPropertyDescriptor(env, name));
141
+ }
142
+ for (const name of names) {
143
+ const server = env[name];
144
+ Object.defineProperty(out, name, { get: () => held.key() ?? server, enumerable: false, configurable: false });
145
+ }
146
+ return out;
147
+ }
@@ -207,6 +207,10 @@ export interface AdapterRunSpec {
207
207
  * words the loop says so in on the run's timeline as it parks (computer-deps.ts computerKeptWords). Absent: a park lets
208
208
  * the computer go. */
209
209
  readonly keptWords?: string;
210
+ /** The run waits on no one (an audit's shadow run, runner/audit.ts AUDIT_PARKED): where it would park (an approval, a
211
+ * raise of its cap, a person's answer) it ends failed at once in these words, saying what it would wait on, every call
212
+ * still waiting withdrawn in the end's commit (parkClaim). Absent: it parks. */
213
+ readonly neverWaits?: string;
210
214
  /** What the spec's tool host bindings registered (tools.ts ToolHost's onConversation, beforeResume, beforeRelease),
211
215
  * run where main's per-run process runs them (app.ts): each agent conversation's creating commit, once the claim's
212
216
  * Harness is open and before anything in it resumes, and when the claim lets the run go (its end, a park, a
@@ -216,6 +220,12 @@ export interface AdapterRunSpec {
216
220
  * the session through the loop's gate, a command row makes them fresh again, and the pass's end lets them go. Absent:
217
221
  * the run grants no MCP tool. */
218
222
  readonly mcp?: ClaimMcp;
223
+ /** The run's model key holder (claim-secrets.ts claimModelKey), which its model routes read at each request: a command
224
+ * row opens it from the run's launch again before the command reaches the session, so a message's key (a replacement,
225
+ * or a run's first) is the one its next request carries. Absent: no gateway here, and the routes read the server's keys. */
226
+ readonly modelKey?: {
227
+ fresh(): Promise<void>;
228
+ };
219
229
  /** The JSON Schema the run's final answer's last json block meets (its revision's definition.output_schema). An answer
220
230
  * that misses it is asked again once, in the same session under the same cap (the output contract's retry); absent:
221
231
  * the run's answer is never checked here. */
@@ -73,6 +73,14 @@ function cardOf(tool, shown, args) {
73
73
  const COMMAND_REQUEST_PREFIX = commandRequest(0).replace(/0$/, "");
74
74
  /** What a CLI's request held at a cut turn (TurnCut) is refused with: the turn ended at the person's question. */
75
75
  const TURN_CUT_AT_QUESTION = "the turn ended at its question to the person";
76
+ /** What a run that waits on no one (AdapterRunSpec.neverWaits) would have waited on, by its park's cause: its end says so. */
77
+ const WAITS_ON = {
78
+ approval: "a person's approval of a tool call",
79
+ answer: "a person's answer to its question",
80
+ cap: "a raise of its cap",
81
+ refusal: "a raise of its cap",
82
+ workflow: "a person's approval of a tool call",
83
+ };
76
84
  function turnCut() {
77
85
  let armedAt;
78
86
  let ask;
@@ -342,7 +350,7 @@ async function openCliRecord(harness, journal, context, now, cwd, endsItsCalls =
342
350
  flushedAtRequest = atRequest;
343
351
  if (joined)
344
352
  return;
345
- const length = cut?.stopReason === "length" ? { stopReason: "length", ...(cut.outputTokens === undefined ? {} : { outputTokens: cut.outputTokens }) } : {};
353
+ const length = cut?.stopReason === "length" ? { stopReason: "length", ...(cut.outputTokens === undefined ? {} : { outputTokens: cut.outputTokens }), ...(cut.capCut === undefined ? {} : { capCut: { capUsd: cut.capCut.capUsd, raiseToUsd: cut.capCut.raiseToUsd } }) } : {};
346
354
  await append(KINDS.message, { role: "model", text: said, ...length });
347
355
  });
348
356
  };
@@ -3767,6 +3775,12 @@ function adapterLoop(specOf, options, deps) {
3767
3775
  * the park cuts (its call held at the tool bridge) may still be streaming its answer: closed under it, its booking
3768
3776
  * would be lost and the next claim would book the turn cut, at an unknown price. Past the bound, it is that cut. */
3769
3777
  async function parkClaim(on, theGate, cause) {
3778
+ // A run that waits on no one (AdapterRunSpec.neverWaits) ends here instead: its park's entry (the ask, the budget, the
3779
+ // question) landed with it parked, and this end, in the same claim, withdraws what waits (writeEnd), books what it
3780
+ // spent, and lets nothing go parked.
3781
+ const never = specNow?.neverWaits;
3782
+ if (never !== undefined && root)
3783
+ return writeEnd(on, root, theGate, { state: "failed", summary: `${never}: it would wait on ${WAITS_ON[cause]}` }, []);
3770
3784
  await releaseHost(on);
3771
3785
  await bookingsSettled();
3772
3786
  await keepAtPark(on, cause);
@@ -3876,8 +3890,9 @@ function adapterLoop(specOf, options, deps) {
3876
3890
  ...(data.metrics !== undefined ? { metrics: data.metrics } : {}),
3877
3891
  };
3878
3892
  // A person's stop or cancel withdraws every call still waiting for its answer, in the end's own commit: the approval
3879
- // card reads answered (withdrawn), never waiting for good on a final run.
3880
- const byPerson = data.state === "stopped" || stopReason?.kind === "command";
3893
+ // card reads answered (withdrawn), never waiting for good on a final run. So does the end of a run that waits on no one
3894
+ // (neverWaits): the ask its park wrote is withdrawn with it.
3895
+ const byPerson = data.state === "stopped" || stopReason?.kind === "command" || specNow?.neverWaits !== undefined;
3881
3896
  // A cancel's calls read cancelled with their run; a stop's, withdrawn.
3882
3897
  const withdrawn = byPerson ? withdrawnAnswers(await entriesOf(theGate.journal, conversation.id, context), isoNow(), stopReason?.kind === "command" && stopReason.cancel ? "canceled" : "withdrawn") : [];
3883
3898
  // A CLI's call on the record from its gate and never sent (parked, or its runner gone before its send): its record ends
@@ -3933,8 +3948,15 @@ function adapterLoop(specOf, options, deps) {
3933
3948
  async command(command) {
3934
3949
  // A command row resumes a loop that may have sat past a token's life: its MCP credentials are made fresh again
3935
3950
  // before its next model request.
3936
- if (command.kind === "steer" || command.kind === "approve" || command.kind === "deny" || command.kind === "raise")
3951
+ if (command.kind === "steer" || command.kind === "approve" || command.kind === "deny" || command.kind === "raise") {
3937
3952
  mcpStale = true;
3953
+ // The run's own model key, read again from its launch before the command reaches the session: the message's key
3954
+ // was written with its command row, so the next request's credential (which the model proxy reads before its
3955
+ // beforeRequest hook) is the message's, a first key or a replacement. Fail-closed: a read that keeps failing
3956
+ // throws here, before the command is taken, so the runner delivers it again at its next poll and the session
3957
+ // never runs the message on the key held before it.
3958
+ await specNow?.modelKey?.fresh();
3959
+ }
3938
3960
  if (command.kind === "steer")
3939
3961
  return steer(command);
3940
3962
  if (command.kind === "approve" || command.kind === "deny") {
@@ -16,6 +16,7 @@ import { type LearnedWorkflow } from "../learning.js";
16
16
  import { type ToolResults } from "../workspace/run-tools.js";
17
17
  import { type ClaimMcp } from "./mcp-run.js";
18
18
  import { type AdapterLoopOptions, type AdapterRun, type AdapterRunSpec, hostLifecycle } from "./adapter-loop.js";
19
+ import { type RunnerHarness } from "./adapters/index.js";
19
20
  import { type Claim, type Journal } from "./loop-contract.js";
20
21
  import type { ReplayCalls } from "../learn/replay-calls.js";
21
22
  import type { ModelRoute } from "./model-route.js";
@@ -221,9 +222,11 @@ export declare function adapterRunGate(store: Pick<Store, "settings">, claim: Pi
221
222
  /** The server's tool settings from the store now; a read that throws is unread (no value of it said). A workflow's call
222
223
  * and model nodes read theirs through it too (workflow-spec.ts callPolicyOf), so both gates fail closed alike. */
223
224
  export declare function storeSettings(store: Pick<Store, "settings">): Promise<Awaited<ReturnType<typeof loadToolSettings>>>;
224
- /** pi's spec for a claimed run. What it makes for the run (its Archil mount credential, its Workspace, its computer, its
225
- * tools) is held until the spec's release; a build that throws part-way lets go what it made by then (spec-holds.ts). */
226
- export declare function adapterRunSpecFromLaunch(launch: RunLaunch, run: AdapterRun, deps: AdapterSpecDeps, claim: Claim): Promise<AdapterRunSpec>;
225
+ /** pi's spec for a claimed run, on `runner`'s adapter (pi's when none is given; a harness module's runner for a harness it
226
+ * adds with no adapter facts), its visible record kept as that runner says (RunnerHarness.record). What it makes for the
227
+ * run (its Archil mount credential, its Workspace, its computer, its tools) is held until the spec's release; a build
228
+ * that throws part-way lets go what it made by then (spec-holds.ts). */
229
+ export declare function adapterRunSpecFromLaunch(launch: RunLaunch, run: AdapterRun, deps: AdapterSpecDeps, claim: Claim, runner?: RunnerHarness): Promise<AdapterRunSpec>;
227
230
  /** The host-tool ctx a non-hand tool is wrapped with (host-tools.ts hostToolOf): the run's own Harness (its commit,
228
231
  * snapshot and task reads), the results map, and the run's folder as the api env's cwd (a tool reads no
229
232
  * file environment, so an error names a path relative to it). Shared by the pi spec's bindNonHandTools and the CLI spec's