@foldrun/core 0.3.0 → 0.5.0

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 (175) hide show
  1. package/README.md +27 -10
  2. package/dist/index.d.ts +3 -0
  3. package/dist/index.js +3 -0
  4. package/dist/src/account.d.ts +45 -0
  5. package/dist/src/account.js +130 -0
  6. package/dist/src/api-tools.d.ts +8 -0
  7. package/dist/src/api-tools.js +22 -4
  8. package/dist/src/approvals.d.ts +104 -1
  9. package/dist/src/approvals.js +192 -2
  10. package/dist/src/browser-pod.d.ts +129 -0
  11. package/dist/src/browser-pod.js +284 -0
  12. package/dist/src/budget.d.ts +32 -0
  13. package/dist/src/budget.js +177 -0
  14. package/dist/src/catalog.js +3 -1
  15. package/dist/src/clock.d.ts +49 -0
  16. package/dist/src/clock.js +121 -0
  17. package/dist/src/completions.js +17 -8
  18. package/dist/src/confine.d.ts +22 -0
  19. package/dist/src/confine.js +224 -4
  20. package/dist/src/deploy.d.ts +33 -0
  21. package/dist/src/deploy.js +145 -6
  22. package/dist/src/egress.d.ts +26 -2
  23. package/dist/src/egress.js +8 -1
  24. package/dist/src/eol.d.ts +2 -0
  25. package/dist/src/eol.js +7 -0
  26. package/dist/src/evals.d.ts +9 -0
  27. package/dist/src/evals.js +26 -13
  28. package/dist/src/flow-lint.d.ts +12 -0
  29. package/dist/src/flow-lint.js +331 -1
  30. package/dist/src/flow-patterns.d.ts +141 -0
  31. package/dist/src/flow-patterns.js +568 -0
  32. package/dist/src/gitrepo.d.ts +18 -0
  33. package/dist/src/gitrepo.js +49 -0
  34. package/dist/src/host-env.d.ts +8 -0
  35. package/dist/src/host-env.js +48 -0
  36. package/dist/src/json-schema.d.ts +12 -0
  37. package/dist/src/json-schema.js +202 -0
  38. package/dist/src/kinds.js +1 -1
  39. package/dist/src/language.d.ts +17 -0
  40. package/dist/src/language.js +55 -0
  41. package/dist/src/layout.d.ts +69 -0
  42. package/dist/src/layout.js +200 -0
  43. package/dist/src/library.d.ts +9 -0
  44. package/dist/src/library.js +43 -4
  45. package/dist/src/limits.d.ts +84 -0
  46. package/dist/src/limits.js +287 -0
  47. package/dist/src/locale.d.ts +47 -0
  48. package/dist/src/locale.js +177 -0
  49. package/dist/src/mail.d.ts +88 -0
  50. package/dist/src/mail.js +245 -0
  51. package/dist/src/model-probe.d.ts +3 -0
  52. package/dist/src/model-probe.js +1 -0
  53. package/dist/src/notify.d.ts +132 -8
  54. package/dist/src/notify.js +360 -58
  55. package/dist/src/openapi.js +5 -3
  56. package/dist/src/operator.d.ts +201 -0
  57. package/dist/src/operator.js +251 -0
  58. package/dist/src/paths.d.ts +33 -0
  59. package/dist/src/paths.js +87 -0
  60. package/dist/src/platform.d.ts +144 -4
  61. package/dist/src/platform.js +27 -3
  62. package/dist/src/provider-health.d.ts +55 -0
  63. package/dist/src/provider-health.js +233 -0
  64. package/dist/src/providers.d.ts +481 -0
  65. package/dist/src/providers.js +1105 -6
  66. package/dist/src/refusal.d.ts +83 -0
  67. package/dist/src/refusal.js +281 -0
  68. package/dist/src/run-container.d.ts +206 -17
  69. package/dist/src/run-container.js +547 -80
  70. package/dist/src/runner.d.ts +179 -10
  71. package/dist/src/runner.js +1423 -169
  72. package/dist/src/runtime.d.ts +52 -0
  73. package/dist/src/runtime.js +493 -58
  74. package/dist/src/script-tools.d.ts +15 -2
  75. package/dist/src/script-tools.js +44 -4
  76. package/dist/src/secret-files.d.ts +6 -5
  77. package/dist/src/secret-files.js +24 -11
  78. package/dist/src/secret-health.d.ts +83 -0
  79. package/dist/src/secret-health.js +177 -0
  80. package/dist/src/secrets.d.ts +38 -1
  81. package/dist/src/secrets.js +197 -96
  82. package/dist/src/starter.d.ts +9 -0
  83. package/dist/src/starter.js +95 -11
  84. package/dist/src/step-exec.d.ts +106 -1
  85. package/dist/src/step-exec.js +558 -85
  86. package/dist/src/step-removal.d.ts +15 -0
  87. package/dist/src/step-removal.js +49 -0
  88. package/dist/src/storage.d.ts +23 -4
  89. package/dist/src/storage.js +125 -23
  90. package/dist/src/store.d.ts +492 -38
  91. package/dist/src/store.js +882 -69
  92. package/dist/src/subagents.d.ts +75 -0
  93. package/dist/src/subagents.js +149 -0
  94. package/dist/src/tar.js +2 -1
  95. package/dist/src/test-mode.d.ts +111 -0
  96. package/dist/src/test-mode.js +285 -0
  97. package/dist/src/tool-names.d.ts +13 -1
  98. package/dist/src/tool-names.js +38 -11
  99. package/dist/src/tool-programs.d.ts +26 -0
  100. package/dist/src/tool-programs.js +172 -0
  101. package/dist/src/tool-test.js +152 -33
  102. package/dist/src/translator.d.ts +8 -2
  103. package/dist/src/translator.js +71 -24
  104. package/dist/src/trigger-log.d.ts +43 -0
  105. package/dist/src/trigger-log.js +122 -0
  106. package/dist/src/triggers.d.ts +23 -0
  107. package/dist/src/triggers.js +101 -12
  108. package/dist/src/untrusted-fetch.d.ts +11 -0
  109. package/dist/src/untrusted-fetch.js +77 -0
  110. package/dist/src/web-actions.d.ts +91 -0
  111. package/dist/src/web-actions.js +199 -0
  112. package/dist/src/webhook.d.ts +19 -3
  113. package/dist/src/webhook.js +50 -11
  114. package/index.ts +3 -0
  115. package/package.json +5 -5
  116. package/src/account.ts +149 -0
  117. package/src/api-tools.ts +32 -4
  118. package/src/approvals.ts +253 -2
  119. package/src/browser-pod.ts +313 -0
  120. package/src/budget.ts +178 -0
  121. package/src/catalog.ts +3 -1
  122. package/src/clock.ts +149 -0
  123. package/src/completions.ts +17 -8
  124. package/src/confine.ts +224 -4
  125. package/src/deploy.ts +137 -6
  126. package/src/egress.ts +32 -4
  127. package/src/eol.ts +7 -0
  128. package/src/evals.ts +36 -11
  129. package/src/flow-lint.ts +371 -1
  130. package/src/flow-patterns.ts +597 -0
  131. package/src/gitrepo.ts +48 -0
  132. package/src/host-env.ts +51 -0
  133. package/src/json-schema.ts +184 -0
  134. package/src/kinds.ts +1 -1
  135. package/src/language.ts +62 -0
  136. package/src/layout.ts +228 -0
  137. package/src/library.ts +50 -4
  138. package/src/limits.ts +290 -0
  139. package/src/locale.ts +217 -0
  140. package/src/mail.ts +316 -0
  141. package/src/model-probe.ts +4 -0
  142. package/src/notify.ts +391 -44
  143. package/src/openapi.ts +5 -3
  144. package/src/operator.ts +300 -0
  145. package/src/paths.ts +83 -0
  146. package/src/platform.ts +158 -5
  147. package/src/provider-health.ts +275 -0
  148. package/src/providers.ts +1368 -6
  149. package/src/refusal.ts +320 -0
  150. package/src/run-container.ts +611 -76
  151. package/src/runner.ts +1463 -165
  152. package/src/runtime.ts +487 -55
  153. package/src/script-tools.ts +55 -6
  154. package/src/secret-files.ts +22 -10
  155. package/src/secret-health.ts +230 -0
  156. package/src/secrets.ts +191 -95
  157. package/src/starter.ts +96 -11
  158. package/src/step-exec.ts +548 -41
  159. package/src/step-removal.ts +59 -0
  160. package/src/storage.ts +149 -24
  161. package/src/store.ts +1227 -74
  162. package/src/subagents.ts +189 -0
  163. package/src/tar.ts +2 -1
  164. package/src/test-mode.ts +311 -0
  165. package/src/tool-names.ts +40 -11
  166. package/src/tool-programs.ts +179 -0
  167. package/src/tool-test.ts +159 -32
  168. package/src/translator.ts +74 -23
  169. package/src/trigger-log.ts +160 -0
  170. package/src/triggers.ts +102 -12
  171. package/src/untrusted-fetch.ts +88 -0
  172. package/src/web-actions.ts +237 -0
  173. package/src/webhook.ts +58 -11
  174. package/templates/hello/agents/curator/agent.md +1 -1
  175. package/templates/hello/agents/notetaker/agent.md +1 -1
@@ -1,5 +1,10 @@
1
1
  import type { McpServerConfig } from "@anthropic-ai/claude-agent-sdk";
2
2
  import type { Effort } from "./store.ts";
3
+ import type { TestEffect } from "./test-mode.ts";
4
+ import type { OperatorEvent } from "./operator.ts";
5
+ import { type SubagentSpec } from "./subagents.ts";
6
+ import { type Limits } from "./limits.ts";
7
+ import { type BrowserPodOutcome, type ClassifyContext } from "./browser-pod.ts";
3
8
  export interface ExecOutcome {
4
9
  status: "completed" | "failed";
5
10
  result: string | null;
@@ -19,6 +24,20 @@ export interface ExecOutcome {
19
24
  inputTokens: number;
20
25
  outputTokens: number;
21
26
  } | null;
27
+ /** Set only when some turns ran on a different model from the step's — a
28
+ * sub-agent with its own `model:`. The sum of every turn, each priced at
29
+ * its own model. `usage` above is one lump and cannot be repriced at the
30
+ * step's model without charging the sub-agent's turns at the wrong rate;
31
+ * the runner reads this instead (repriced in runner.ts). */
32
+ turnsCostUsd?: number;
33
+ /** Why the model's last turn ended, as the SDK's result message says:
34
+ * end_turn, max_tokens, stop_sequence, tool_use, pause_turn, refusal or
35
+ * model_context_window_exceeded. Null when no result arrived. */
36
+ stopReason?: string | null;
37
+ /** Set when the step browsed through the account's browser pod from the
38
+ * slim image (ExecOptions.browserPod): its reconnects, and — when the pod
39
+ * was lost — why, and what the step had written by then. */
40
+ browserPod?: BrowserPodOutcome;
22
41
  }
23
42
  export interface ExecOptions {
24
43
  agentDir: string;
@@ -43,6 +62,9 @@ export interface ExecOptions {
43
62
  * The runner derives it from the flow's `budget:` and what the run has
44
63
  * already spent; absent means no ceiling on this step. */
45
64
  budgetUsd?: number | null;
65
+ /** Which line set the ceiling, for the error that names it — "budget: in
66
+ * the flow file" or "budget: on the <agent> agent". */
67
+ budgetNote?: string;
46
68
  /** Price per token for the model, when the catalogue knows it, so spend
47
69
  * can be counted turn by turn instead of learned at the end. Without it
48
70
  * the step prices its turns from the SDK's own running total. */
@@ -58,17 +80,77 @@ export interface ExecOptions {
58
80
  /** `output: json` — the reply must carry one JSON value; extracting it is
59
81
  * part of finishing the step, and failing to is failing the step. */
60
82
  output?: "json";
83
+ /** `schema:` — what that value must look like; a value that does not fit
84
+ * fails the step naming the field. See json-schema.ts. */
85
+ schema?: Record<string, unknown> | boolean;
86
+ /** `max_turns:` — the most model turns before the step is stopped. The
87
+ * SDK enforces it; the step reads the result's reason and says so. */
88
+ maxTurns?: number;
61
89
  /** false when the caller is already an isolation boundary (a run
62
90
  * container): the SDK's bash sandbox is then redundant and would block
63
91
  * declared network use. Default (undefined/true) keeps it on. */
64
92
  sandboxBash?: boolean;
93
+ /** Has a person asked for the run to stop? Polled every couple of
94
+ * seconds while the model loop runs; true interrupts it. The in-process
95
+ * runner answers from the run record (stopRun writes there from another
96
+ * process); a container has a sandbox that is destroyed instead. */
97
+ stopRequested?: () => boolean;
98
+ /** Reads the run's inbox (operator.ts inboxReader): what a person wrote
99
+ * in while the step runs, handed to the model after its next tool call.
100
+ * Absent where nobody can write in (no egress proxy). */
101
+ inbox?: () => Promise<string | null>;
102
+ /** `subagents:` — colleagues the model may delegate to, each with its own
103
+ * context and tools no wider than this step's (subagents.ts). Absent or
104
+ * empty: no Agent tool, as before. */
105
+ subagents?: SubagentSpec[];
106
+ /** `limits:` — the most calls this step may make, per tool and in all,
107
+ * resolved host-side (account, workspace, agent, step: nearest wins per
108
+ * key). Counted in the PreToolUse hook below; see limits.ts. */
109
+ limits?: Limits;
110
+ /** SDK tool name → the foldrun name it counts under, for the tools whose
111
+ * SDK name does not say (limits.ts toolOwners). */
112
+ toolOwners?: Record<string, string>;
113
+ /** The step runs on the slim image and browses through the account's
114
+ * browser pod (browser-pod.ts). `events` is the file the web tool writes
115
+ * its reconnects to; the classify context says which API operations and
116
+ * web actions are writes. A lost pod stops the step, and the outcome
117
+ * says what it had written, so the runner can re-run it or fail it. */
118
+ browserPod?: {
119
+ events: string;
120
+ } & ClassifyContext;
65
121
  emit: (type: "text" | "tool" | "info" | "error", text: string, extra?: EventExtra) => void;
66
122
  }
123
+ /** How often the stop flag is read while a step runs. */
124
+ export declare const STOP_POLL_MS = 2000;
125
+ /** The least a `verify:` gets when the step used up most of its `timeout:` —
126
+ * capped by the timeout itself, so the step and its check together never
127
+ * run past timeout + this. Under the isolated path's backstop (+60 s). */
128
+ export declare const VERIFY_FLOOR_MS = 30000;
129
+ /** How long a `verify:` may run: what is left of the step's `timeout:`,
130
+ * with a floor. Null when the flow set no timeout — `timeout:` is the only
131
+ * clock there is, and a stop still ends the check. */
132
+ export declare function verifyBudgetMs(timeoutSec: number | undefined, elapsedMs: number): number | null;
133
+ /** After an interrupt is asked for, how long the model loop gets to wind
134
+ * down on its own before the query is aborted outright. */
135
+ export declare const INTERRUPT_GRACE_MS = 10000;
136
+ /** The model loop, as executeStep needs it — the SDK's `query`, or a test's
137
+ * stand-in. Only the shape the loop reads. */
138
+ export type QueryLike = AsyncIterable<unknown> & {
139
+ interrupt(): Promise<unknown>;
140
+ };
141
+ export type QueryFn = (args: {
142
+ prompt: string;
143
+ options: Record<string, unknown>;
144
+ }) => QueryLike;
67
145
  /** The pairing fields on a tool event — see RunEvent in store.ts. */
68
146
  export type EventExtra = {
69
147
  call?: string;
70
148
  ms?: number;
71
149
  err?: boolean;
150
+ effect?: TestEffect;
151
+ operator?: OperatorEvent;
152
+ subagent?: string;
153
+ check?: boolean;
72
154
  };
73
155
  /** Conservative per-token rates for a model nothing else can price — an
74
156
  * unknown id on a gateway with no catalogue. Opus-class, so a ceiling
@@ -101,7 +183,27 @@ export declare function priceTurn(u: Record<string, number | undefined>, price:
101
183
  * over the cap however many steps it fans out to. No budget, no ceiling.
102
184
  */
103
185
  export declare function stepCeiling(budgetUsd: number | null | undefined, spentUsd: number, launching: number): number | null;
104
- export declare function executeStep(opts: ExecOptions): Promise<ExecOutcome>;
186
+ /**
187
+ * The ceiling a step actually runs under, and which line set it.
188
+ *
189
+ * Two caps can apply to one step: the flow's `budget:` gives it an equal
190
+ * share of what the run has left; the agent's own `budget:` is the most that
191
+ * agent may spend in one run, less what its steps in this run have cost or
192
+ * may still cost, shared across `sharing` — this step and the agent's steps
193
+ * launched with it that have not started yet. Without the share, twenty
194
+ * fan-out copies of one agent each got the whole remainder, because none of
195
+ * them has recorded a cent when the next one starts.
196
+ * The tighter one wins, and the note says which, because "over budget" with
197
+ * no line to go and raise is a message that sends someone to the wrong file.
198
+ */
199
+ export declare function stepCeilingFor(flowShareUsd: number | null, agentBudgetUsd: number | null | undefined, agentSpentUsd: number, agent: string, sharing?: number): {
200
+ ceilingUsd: number | null;
201
+ note: string;
202
+ };
203
+ export declare function executeStep(opts: ExecOptions,
204
+ /** The model loop. Injected so the clock and the stop can be tested
205
+ * without a model; production passes nothing and gets the SDK. */
206
+ runQuery?: QueryFn): Promise<ExecOutcome>;
105
207
  /**
106
208
  * The one JSON value a reply carries. A ```json fence wins — it is what the
107
209
  * step was asked to write — and the LAST one at that, because a model that
@@ -143,4 +245,7 @@ export declare function checkVerify(agentDir: string, verify: string, ctx: {
143
245
  /** The step's own model environment, for `judge:` — it grades on the
144
246
  * fast tier through the same credential the step rode. */
145
247
  modelEnv?: Record<string, string | undefined>;
248
+ /** Ends a shell check or a judge mid-way: the step's clock ran out, or a
249
+ * person asked the run to stop. */
250
+ signal?: AbortSignal;
146
251
  }): Promise<VerifyVerdict>;