@foldrun/core 2.2.0 → 2.4.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.
@@ -23,6 +23,7 @@ import { runInContainer } from "./container.ts";
23
23
  import { hostSafeEnv } from "./host-env.ts";
24
24
  import { workspaceLinkEnv } from "./confine.ts";
25
25
  import { platform } from "./platform.ts";
26
+ import { previewOrKeep, RESULT_KEEP_CHARS } from "./tool-output.ts";
26
27
 
27
28
  // A script's only clock is the `timeout:` its own tool.md declares. There
28
29
  // was a two-minute default and a ten-minute cap here; both were the
@@ -30,7 +31,9 @@ import { platform } from "./platform.ts";
30
31
  // deploy watcher had to be taught to call its tool four times to outlast the
31
32
  // cap. The platform orchestrates; it does not decide that.
32
33
  const timeoutFor = (spec: ScriptSpec) => (spec.timeout ? spec.timeout * 1000 : undefined);
33
- const MAX_OUTPUT = 20_000;
34
+ // Collected up to this, then previewed and kept (tool-output.ts): the model
35
+ // sees the head and where the rest is, instead of a cut with the rest gone.
36
+ const MAX_OUTPUT = RESULT_KEEP_CHARS;
34
37
 
35
38
  export interface ScriptSpec {
36
39
  name: string;
@@ -419,6 +422,9 @@ export function buildScriptTools(
419
422
  libraryScripts = "",
420
423
  interpreters: Record<string, string> = {},
421
424
  exec: ExecutionContext | null = null,
425
+ /** Whether the agent can read files back (Read or Grep): only then is a
426
+ * long output kept in a file and previewed (tool-output.ts). */
427
+ readable: () => boolean = () => false,
422
428
  ): ScriptToolResult {
423
429
  if (scripts.length === 0) {
424
430
  return { server: null, toolNames: [], promptLines: [], drainLog: () => [] };
@@ -453,7 +459,7 @@ export function buildScriptTools(
453
459
  `(${Date.now() - started}ms, ${exec?.executor ?? "sandbox"})`,
454
460
  );
455
461
  return {
456
- content: [{ type: "text" as const, text: `exit ${code ?? "error"}\n\n${out}` }],
462
+ content: [{ type: "text" as const, text: `exit ${code ?? "error"}\n\n${previewOrKeep(out, { dir: agentDir, readable }, spec.name)}` }],
457
463
  isError: code !== 0,
458
464
  };
459
465
  },
package/src/step-exec.ts CHANGED
@@ -76,6 +76,9 @@ export interface ExecOutcome {
76
76
  * end_turn, max_tokens, stop_sequence, tool_use, pause_turn, refusal or
77
77
  * model_context_window_exceeded. Null when no result arrived. */
78
78
  stopReason?: string | null;
79
+ /** Model turns the step took (the SDK's num_turns; counted from the
80
+ * turns seen when no closing result arrived). */
81
+ turns?: number;
79
82
  /** Set when the step browsed through the account's browser pod from the
80
83
  * slim image (ExecOptions.browserPod): its reconnects, and — when the pod
81
84
  * was lost — why, and what the step had written by then. */
@@ -192,7 +195,7 @@ export type QueryLike = AsyncIterable<unknown> & { interrupt(): Promise<unknown>
192
195
  export type QueryFn = (args: { prompt: string; options: Record<string, unknown> }) => QueryLike;
193
196
 
194
197
  /** The pairing fields on a tool event — see RunEvent in store.ts. */
195
- export type EventExtra = { call?: string; ms?: number; err?: boolean; effect?: TestEffect; operator?: OperatorEvent; subagent?: string; check?: boolean; hook?: HookRecord };
198
+ export type EventExtra = { call?: string; ms?: number; err?: boolean; bytes?: number; effect?: TestEffect; operator?: OperatorEvent; subagent?: string; check?: boolean; hook?: HookRecord };
196
199
 
197
200
  /** The tag on every event a failed check writes — `verify:`, `output: json`,
198
201
  * a schema — headline and detail alike. The runner's no-retry rule for
@@ -312,6 +315,7 @@ async function executeStepInner(opts: ExecOptions, runQuery: QueryFn): Promise<E
312
315
  let costUsd: number | null = null;
313
316
  let usage: ExecOutcome["usage"] = null;
314
317
  let stopReason: string | null = null;
318
+ let numTurns: number | null = null;
315
319
  const texts: string[] = [];
316
320
 
317
321
  fs.mkdirSync(path.join(agentDir, "outputs"), { recursive: true });
@@ -749,6 +753,9 @@ async function executeStepInner(opts: ExecOptions, runQuery: QueryFn): Promise<E
749
753
  emit("tool", open.name, {
750
754
  call: block.tool_use_id,
751
755
  ms: Date.now() - open.at,
756
+ // How much the result put into the conversation — sent again on
757
+ // every later turn, so the size is what a token report needs.
758
+ bytes: toolResultText(block.content).length,
752
759
  ...(block.is_error ? { err: true } : {}),
753
760
  ...(open.subagent ? { subagent: open.subagent } : {}),
754
761
  });
@@ -774,6 +781,7 @@ async function executeStepInner(opts: ExecOptions, runQuery: QueryFn): Promise<E
774
781
  }
775
782
  costUsd = "total_cost_usd" in message ? (message.total_cost_usd ?? null) : null;
776
783
  stopReason = "stop_reason" in message && typeof message.stop_reason === "string" ? message.stop_reason : null;
784
+ if ("num_turns" in message && typeof message.num_turns === "number") numTurns = message.num_turns;
777
785
  if ("usage" in message && message.usage) {
778
786
  // Cache reads and writes counted beside the input total: with only
779
787
  // the total (until 18 Oct 2026) the hit rate could not be read and
@@ -941,6 +949,7 @@ async function executeStepInner(opts: ExecOptions, runQuery: QueryFn): Promise<E
941
949
  status, result, conclusion, ...(opts.output ? { data } : {}), costUsd, usage,
942
950
  ...(mixedModels() ? { turnsCostUsd: turnsTotal() } : {}),
943
951
  stopReason,
952
+ turns: numTurns ?? turns.size,
944
953
  ...(pod ? { browserPod: podState } : {}),
945
954
  };
946
955
  }
package/src/store.ts CHANGED
@@ -3046,6 +3046,10 @@ export interface RunEvent {
3046
3046
  call?: string;
3047
3047
  ms?: number;
3048
3048
  err?: boolean;
3049
+ /** On a tool completion: the result's length in characters — what it put
3050
+ * into the conversation, which every later turn sends again. Absent before
3051
+ * 18 Oct 2026. */
3052
+ bytes?: number;
3049
3053
  /** Set on a test run's event that reports what the platform did instead
3050
3054
  * of the real thing — a refused send, a redirected email, a diverted
3051
3055
  * state write, a withheld secret. The run page lists these; the text is
@@ -3103,6 +3107,8 @@ export interface StepAttempt {
3103
3107
  status: "completed" | "failed" | "lost";
3104
3108
  costUsd: number | null;
3105
3109
  tokens: TokenUsage | null;
3110
+ /** Model turns this attempt took; absent before 18 Oct 2026. */
3111
+ turns?: number | null;
3106
3112
  computeSecs: number | null;
3107
3113
  startedAt: string;
3108
3114
  finishedAt: string;
@@ -3118,85 +3124,21 @@ export interface StepAttempt {
3118
3124
  /** The step's totals across its attempts, from the rows. Null where no
3119
3125
  * attempt reported the figure — an in-process step rents no sandbox, and
3120
3126
  * billing reads null as zero, not as "unknown, charge something". */
3121
- export function sumAttempts(tries: StepAttempt[]): Pick<StepAttempt, "costUsd" | "tokens" | "computeSecs"> {
3127
+ export function sumAttempts(tries: StepAttempt[]): Pick<StepAttempt, "costUsd" | "tokens" | "computeSecs" | "turns"> {
3122
3128
  const costs = tries.map((t) => t.costUsd).filter((c): c is number => typeof c === "number");
3129
+ const turns = tries.map((t) => t.turns).filter((c): c is number => typeof c === "number");
3123
3130
  const toks = tries.map((t) => t.tokens).filter((t): t is TokenUsage => !!t);
3124
3131
  const secs = tries.map((t) => t.computeSecs).filter((c): c is number => typeof c === "number");
3125
3132
  return {
3126
3133
  costUsd: costs.length ? costs.reduce((a, b) => a + b, 0) : null,
3127
3134
  tokens: toks.length ? sumTokens(toks) : null,
3128
3135
  computeSecs: secs.length ? secs.reduce((a, b) => a + b, 0) : null,
3136
+ ...(turns.length ? { turns: turns.reduce((a, b) => a + b, 0) } : {}),
3129
3137
  };
3130
3138
  }
3131
3139
 
3132
- /**
3133
- * The token counts behind a step's cost. `input` is every input token the
3134
- * model saw; `cacheRead` and `cacheWrite` are the part of it the prompt
3135
- * cache served or stored — a read costs a tenth of fresh input, and a usage
3136
- * page wants to say how much of a step's context was re-read rather than
3137
- * re-sent. Steps recorded before 18 Oct 2026 have no cache fields: their
3138
- * `input` is the same total, but how much of it was cached is unknown.
3139
- */
3140
- export interface TokenUsage {
3141
- input: number;
3142
- output: number;
3143
- cacheRead?: number;
3144
- cacheWrite?: number;
3145
- }
3146
-
3147
- /** The record's shape from what a step reports (ExecOutcome.usage). */
3148
- export function tokensOf(usage: { inputTokens: number; outputTokens: number; cacheReadTokens?: number; cacheWriteTokens?: number } | null | undefined): TokenUsage | null {
3149
- if (!usage) return null;
3150
- return {
3151
- input: usage.inputTokens,
3152
- output: usage.outputTokens,
3153
- cacheRead: usage.cacheReadTokens ?? 0,
3154
- cacheWrite: usage.cacheWriteTokens ?? 0,
3155
- };
3156
- }
3157
-
3158
- /** Field-by-field sum; the cache fields are present in the result whenever
3159
- * any input had them, so a run of old and new steps still reads whole. */
3160
- export function sumTokens(list: readonly TokenUsage[]): TokenUsage {
3161
- const out: TokenUsage = { input: 0, output: 0 };
3162
- for (const t of list) {
3163
- out.input += t.input;
3164
- out.output += t.output;
3165
- if (t.cacheRead !== undefined || t.cacheWrite !== undefined) {
3166
- out.cacheRead = (out.cacheRead ?? 0) + (t.cacheRead ?? 0);
3167
- out.cacheWrite = (out.cacheWrite ?? 0) + (t.cacheWrite ?? 0);
3168
- }
3169
- }
3170
- return out;
3171
- }
3172
-
3173
- /** The share of all input that the prompt cache served, 0–1; null when
3174
- * there was no input, or the record predates the split and cannot say. */
3175
- export function cacheHitRate(t: TokenUsage | null | undefined): number | null {
3176
- if (!t || t.cacheRead === undefined) return null;
3177
- return t.input > 0 ? Math.min(1, t.cacheRead / t.input) : null;
3178
- }
3179
-
3180
- /** The cache share over several counts, taking only those that report the
3181
- * split: a step recorded before it holds its cache traffic inside `input`,
3182
- * and counting that as uncached would understate the rate. The rule the
3183
- * Usage page and the CLI's totals follow too. */
3184
- export function measuredHitRate(list: readonly (TokenUsage | null | undefined)[]): number | null {
3185
- let read = 0;
3186
- let all = 0;
3187
- for (const t of list) {
3188
- if (!t || t.cacheRead === undefined) continue;
3189
- read += t.cacheRead;
3190
- all += t.input;
3191
- }
3192
- return all > 0 ? read / all : null;
3193
- }
3194
-
3195
- /** A run's token counts, summed over its steps; null when no step reported any. */
3196
- export function runTokens(run: { steps: readonly { tokens?: TokenUsage | null }[] }): TokenUsage | null {
3197
- const toks = run.steps.map((s) => s.tokens).filter((t): t is TokenUsage => !!t);
3198
- return toks.length ? sumTokens(toks) : null;
3199
- }
3140
+ export { type TokenUsage, tokensOf, sumTokens, cacheHitRate, measuredHitRate, runTokens } from "./tokens.ts";
3141
+ import { sumTokens, type TokenUsage } from "./tokens.ts";
3200
3142
 
3201
3143
  export interface StepRecord {
3202
3144
  agent: string;
@@ -3377,6 +3319,14 @@ export interface StepRecord {
3377
3319
  * and not only "how much" — the price of a token changes, the count is
3378
3320
  * the fact. Absent on steps recorded before this existed. */
3379
3321
  tokens?: TokenUsage | null;
3322
+ /** Model turns, summed across attempts, and the system prompt's length in
3323
+ * characters — every turn sends it again. With the tool events' `bytes`,
3324
+ * what says where a step's input tokens went. Absent before 18 Oct 2026. */
3325
+ turns?: number | null;
3326
+ systemPromptChars?: number;
3327
+ /** Why the model's last turn ended (end_turn, max_tokens, tool_use…), as
3328
+ * the SDK's closing result says. */
3329
+ stopReason?: string | null;
3380
3330
  /** What the sandbox actually touched, against what it reserved: CPU
3381
3331
  * busy-seconds and peak memory from the pod's own cgroup, bytes on the
3382
3332
  * wire from /proc/net/dev. Null per-field where the kernel interface
package/src/tokens.ts ADDED
@@ -0,0 +1,75 @@
1
+ // Token counts and the prompt-cache share, as pure functions over values.
2
+ //
3
+ // No node imports: the dashboard's client components read these (the run
4
+ // page's step chips), and store.ts — fs, path, the tree reader — must never
5
+ // reach a browser bundle (web ae042d6 broke `next build` doing exactly that).
6
+ // store.ts re-exports everything here, so server code imports as before.
7
+
8
+ /**
9
+ * The token counts behind a step's cost. `input` is every input token the
10
+ * model saw; `cacheRead` and `cacheWrite` are the part of it the prompt
11
+ * cache served or stored — a read costs a tenth of fresh input, and a usage
12
+ * page wants to say how much of a step's context was re-read rather than
13
+ * re-sent. Steps recorded before 18 Oct 2026 have no cache fields: their
14
+ * `input` is the same total, but how much of it was cached is unknown.
15
+ */
16
+ export interface TokenUsage {
17
+ input: number;
18
+ output: number;
19
+ cacheRead?: number;
20
+ cacheWrite?: number;
21
+ }
22
+
23
+ /** The record's shape from what a step reports (ExecOutcome.usage). */
24
+ export function tokensOf(usage: { inputTokens: number; outputTokens: number; cacheReadTokens?: number; cacheWriteTokens?: number } | null | undefined): TokenUsage | null {
25
+ if (!usage) return null;
26
+ return {
27
+ input: usage.inputTokens,
28
+ output: usage.outputTokens,
29
+ cacheRead: usage.cacheReadTokens ?? 0,
30
+ cacheWrite: usage.cacheWriteTokens ?? 0,
31
+ };
32
+ }
33
+
34
+ /** Field-by-field sum; the cache fields are present in the result whenever
35
+ * any input had them, so a run of old and new steps still reads whole. */
36
+ export function sumTokens(list: readonly TokenUsage[]): TokenUsage {
37
+ const out: TokenUsage = { input: 0, output: 0 };
38
+ for (const t of list) {
39
+ out.input += t.input;
40
+ out.output += t.output;
41
+ if (t.cacheRead !== undefined || t.cacheWrite !== undefined) {
42
+ out.cacheRead = (out.cacheRead ?? 0) + (t.cacheRead ?? 0);
43
+ out.cacheWrite = (out.cacheWrite ?? 0) + (t.cacheWrite ?? 0);
44
+ }
45
+ }
46
+ return out;
47
+ }
48
+
49
+ /** The share of all input that the prompt cache served, 0–1; null when
50
+ * there was no input, or the record predates the split and cannot say. */
51
+ export function cacheHitRate(t: TokenUsage | null | undefined): number | null {
52
+ if (!t || t.cacheRead === undefined) return null;
53
+ return t.input > 0 ? Math.min(1, t.cacheRead / t.input) : null;
54
+ }
55
+
56
+ /** The cache share over several counts, taking only those that report the
57
+ * split: a step recorded before it holds its cache traffic inside `input`,
58
+ * and counting that as uncached would understate the rate. The rule the
59
+ * Usage page and the CLI's totals follow too. */
60
+ export function measuredHitRate(list: readonly (TokenUsage | null | undefined)[]): number | null {
61
+ let read = 0;
62
+ let all = 0;
63
+ for (const t of list) {
64
+ if (!t || t.cacheRead === undefined) continue;
65
+ read += t.cacheRead;
66
+ all += t.input;
67
+ }
68
+ return all > 0 ? read / all : null;
69
+ }
70
+
71
+ /** A run's token counts, summed over its steps; null when no step reported any. */
72
+ export function runTokens(run: { steps: readonly { tokens?: TokenUsage | null }[] }): TokenUsage | null {
73
+ const toks = run.steps.map((s) => s.tokens).filter((t): t is TokenUsage => !!t);
74
+ return toks.length ? sumTokens(toks) : null;
75
+ }
@@ -0,0 +1,82 @@
1
+ // A tool result too long to send whole: kept in a file, previewed inline.
2
+ //
3
+ // A tool result stays in the conversation and is sent again on every later
4
+ // turn of the step, so one 20,000-character answer costs its tokens over and
5
+ // over (FOL-28: ~470k input tokens on a 3-line answer). Script and API tools
6
+ // used to cut at 20,000 characters and drop the rest — the model saw a
7
+ // fragment and could not ask for more. Now the whole output is written under
8
+ // the agent's `.cache/results/`, the model gets the head and the path, and
9
+ // reads on with Read (offset/limit) or Grep only if it needs to.
10
+ //
11
+ // `.cache/` is inside the workspace, so the file tools reach it, and is never
12
+ // copied back from a run's sandbox (run-container.ts DENY_BACK_CACHE): a
13
+ // result is the step's working material, not workspace content.
14
+
15
+ import fs from "node:fs";
16
+ import path from "node:path";
17
+ import crypto from "node:crypto";
18
+
19
+ /** What the model sees inline, at most: about 3,000 tokens. */
20
+ export const RESULT_PREVIEW_CHARS = 12_000;
21
+
22
+ /** What a tool's output is collected up to before it is kept — enough for a
23
+ * big API page or a long script log, bounded so a runaway stream cannot
24
+ * fill the sandbox's memory. */
25
+ export const RESULT_KEEP_CHARS = 2_000_000;
26
+
27
+ /** The results folder, relative to the agent's working directory. */
28
+ export const RESULTS_DIR = ".cache/results";
29
+
30
+ /** What an agent that cannot read files gets: the whole result up to the
31
+ * old cut, as before 18 Oct 2026 — a pointer to a file it has no tool to
32
+ * open would only lose it the tail. */
33
+ export const UNREADABLE_CAP = 20_000;
34
+
35
+ /** Does a step granted these tools hold one that reads a kept result back? */
36
+ export function canReadBack(allowed: readonly string[]): boolean {
37
+ return allowed.includes("Read") || allowed.includes("Grep");
38
+ }
39
+
40
+ /** Where a long result may be kept: the agent's folder, and whether the agent
41
+ * holds a tool to read it back (Read or Grep). Asked per call, because the
42
+ * runner builds the tools before it settles the agent's grants. */
43
+ export interface KeepIn {
44
+ dir: string;
45
+ readable: () => boolean;
46
+ }
47
+
48
+ /**
49
+ * `text` as the model should see it: unchanged when short; otherwise its
50
+ * head — cut at a line break when one falls in the last fifth — and where
51
+ * the whole of it is. `label` names the file (the tool's name). An agent
52
+ * that cannot read files back, or no folder to keep it in: the whole result
53
+ * up to UNREADABLE_CAP, as it always was.
54
+ */
55
+ export function previewOrKeep(
56
+ text: string,
57
+ keep: KeepIn | null | undefined,
58
+ label: string,
59
+ cap: number = RESULT_PREVIEW_CHARS,
60
+ ): string {
61
+ const whole = () => (text.length > UNREADABLE_CAP ? `${text.slice(0, UNREADABLE_CAP)}\n…[truncated]` : text);
62
+ if (!keep || !keep.readable()) return whole();
63
+ const agentDir = keep.dir;
64
+ if (text.length <= cap) return text;
65
+ let cut = text.lastIndexOf("\n", cap);
66
+ if (cut < cap * 0.8) cut = cap;
67
+ const head = text.slice(0, cut);
68
+ const more = text.length - cut;
69
+ const name = `${label.replace(/[^A-Za-z0-9_.-]+/g, "_").slice(0, 60)}-${Date.now().toString(36)}-${crypto.randomBytes(3).toString("hex")}.txt`;
70
+ const rel = path.posix.join(RESULTS_DIR, name);
71
+ try {
72
+ fs.mkdirSync(path.join(agentDir, RESULTS_DIR), { recursive: true });
73
+ fs.writeFileSync(path.join(agentDir, rel), text);
74
+ } catch {
75
+ // Nowhere to keep it: what it would have had before, rather than a tail lost.
76
+ return whole();
77
+ }
78
+ return (
79
+ `${head}\n…(${more} more characters not shown — the whole result, ${text.length} characters, is in \`${rel}\`. ` +
80
+ `Read it with offset/limit, or Grep it, if you need what is past this point.)`
81
+ );
82
+ }