@foldrun/core 24.9.1 → 24.11.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.
- package/dist/src/engine-common.d.ts +4 -0
- package/dist/src/engine-common.js +1 -0
- package/dist/src/engine-loop.d.ts +5 -0
- package/dist/src/engine-loop.js +31 -1
- package/dist/src/runner.d.ts +7 -1
- package/dist/src/runner.js +27 -4
- package/package.json +1 -1
- package/src/engine-common.ts +5 -0
- package/src/engine-loop.ts +31 -1
- package/src/runner.ts +27 -6
|
@@ -65,6 +65,10 @@ export interface LoopOptions {
|
|
|
65
65
|
reserveTokens: number;
|
|
66
66
|
keepRecentTokens: number;
|
|
67
67
|
} | false;
|
|
68
|
+
/** Steering (FOL-213): what a person sent while the turn runs, taken
|
|
69
|
+
* (and so removed) each time it is called. The model reads it after the
|
|
70
|
+
* calls it is making, or, about to finish, reads it and goes on. */
|
|
71
|
+
steer: (() => unknown[]) | null;
|
|
68
72
|
}
|
|
69
73
|
export declare function hookFns(hooks: unknown, event: string): HookFn[];
|
|
70
74
|
/** The SDK's options, as this engine reads them. What it cannot honour it
|
|
@@ -98,6 +98,7 @@ export function readOptions(options, engine = "foldrun") {
|
|
|
98
98
|
history: readHistory(options.history),
|
|
99
99
|
mcpServers,
|
|
100
100
|
mcpGranted: mcpTools,
|
|
101
|
+
steer: typeof options.steer === "function" ? options.steer : null,
|
|
101
102
|
compaction: options.compaction === false ? false : compactionFor(typeof options.contextWindow === "number" && Number.isFinite(options.contextWindow) && options.contextWindow >= MIN_CONTEXT_WINDOW ? Math.floor(options.contextWindow) : COMPACTION_DEFAULTS.contextWindow),
|
|
102
103
|
};
|
|
103
104
|
}
|
|
@@ -12,6 +12,11 @@ export type Engine = "sdk" | "foldrun";
|
|
|
12
12
|
* included, so a half-written answer passed as a finished step (FOL-211).
|
|
13
13
|
* The partial reply is still the result's text; `errors` says why it is not
|
|
14
14
|
* a finish, the way the SDK's error results do. */
|
|
15
|
+
/** What this engine can do that a caller may ask about before relying on
|
|
16
|
+
* it: `steer` — messages sent while a turn runs (FOL-213). */
|
|
17
|
+
export declare const ENGINE_FEATURES: {
|
|
18
|
+
readonly steer: true;
|
|
19
|
+
};
|
|
15
20
|
export declare function endOf(stopReason: string | null, calls: number): {
|
|
16
21
|
subtype: string;
|
|
17
22
|
error?: string;
|
package/dist/src/engine-loop.js
CHANGED
|
@@ -65,6 +65,9 @@ function requestHeaders(env) {
|
|
|
65
65
|
* included, so a half-written answer passed as a finished step (FOL-211).
|
|
66
66
|
* The partial reply is still the result's text; `errors` says why it is not
|
|
67
67
|
* a finish, the way the SDK's error results do. */
|
|
68
|
+
/** What this engine can do that a caller may ask about before relying on
|
|
69
|
+
* it: `steer` — messages sent while a turn runs (FOL-213). */
|
|
70
|
+
export const ENGINE_FEATURES = { steer: true };
|
|
68
71
|
export function endOf(stopReason, calls) {
|
|
69
72
|
switch (stopReason) {
|
|
70
73
|
case "end_turn":
|
|
@@ -270,6 +273,19 @@ async function* run(prompt, o, signal, interrupted) {
|
|
|
270
273
|
const sessionId = crypto.randomUUID();
|
|
271
274
|
const startedAt = Date.now();
|
|
272
275
|
const byName = new Map(o.tools.map((t) => [t.name, t]));
|
|
276
|
+
/** What the person sent meanwhile, as text blocks: each a string with
|
|
277
|
+
* words in it, held to a sane size; anything else is dropped. */
|
|
278
|
+
// Taken only when the model will get it: not once the turn is stopped,
|
|
279
|
+
// nor when no request is left under maxTurns (Codex) — it stays with the
|
|
280
|
+
// caller, unsent.
|
|
281
|
+
const steered = () => {
|
|
282
|
+
if (!o.steer || interrupted() || signal.aborted || (o.maxTurns !== null && turns >= o.maxTurns))
|
|
283
|
+
return [];
|
|
284
|
+
const taken = o.steer();
|
|
285
|
+
return (Array.isArray(taken) ? taken : [])
|
|
286
|
+
.filter((t) => typeof t === "string" && t.trim() !== "")
|
|
287
|
+
.map((t) => ({ type: "text", text: t.slice(0, 20_000) }));
|
|
288
|
+
};
|
|
273
289
|
const ctx = { cwd: o.cwd, env: o.shellEnv, signal, reads: new Map(), groups: new Set() };
|
|
274
290
|
// An earlier conversation comes first (foldrun code's local turns carry
|
|
275
291
|
// theirs, 19 Nov 2026); the prompt is always the newest user turn.
|
|
@@ -503,6 +519,19 @@ async function* run(prompt, o, signal, interrupted) {
|
|
|
503
519
|
continue;
|
|
504
520
|
if (turn.stopReason !== "tool_use" || !calls.length) {
|
|
505
521
|
const end = endOf(turn.stopReason, calls.length);
|
|
522
|
+
// About to finish with a message from the person waiting: it is the
|
|
523
|
+
// next user turn, and the model goes on. Not after a turn that failed
|
|
524
|
+
// (it stays with the caller, unsent).
|
|
525
|
+
if (end.subtype === "success") {
|
|
526
|
+
const more = steered();
|
|
527
|
+
if (more.length) {
|
|
528
|
+
// A new question: the answer to come is its own, not this one (Codex).
|
|
529
|
+
lastText = "";
|
|
530
|
+
messages.push({ role: "user", content: more });
|
|
531
|
+
yield { type: "user", message: { role: "user", content: more }, parent_tool_use_id: null, session_id: sessionId };
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
506
535
|
// A turn that did not finish carries ITS text, empty included — not an
|
|
507
536
|
// earlier turn's "I'll check" passed off as a refusal's words (Codex).
|
|
508
537
|
yield end.error ? { ...result(end.subtype, turn.stopReason), result: text, errors: [end.error] } : result(end.subtype, turn.stopReason);
|
|
@@ -572,7 +601,8 @@ async function* run(prompt, o, signal, interrupted) {
|
|
|
572
601
|
return { type: "tool_result", tool_use_id: String(p.block.id), content, ...(isError ? { is_error: true } : {}) };
|
|
573
602
|
});
|
|
574
603
|
const context = done.flatMap((d) => d.context);
|
|
575
|
-
|
|
604
|
+
// After the results: what hooks add, then what the person sent meanwhile.
|
|
605
|
+
const content = [...results, ...context.map((c) => ({ type: "text", text: c })), ...steered()];
|
|
576
606
|
messages.push({ role: "user", content });
|
|
577
607
|
yield { type: "user", message: { role: "user", content }, parent_tool_use_id: null, session_id: sessionId };
|
|
578
608
|
}
|
package/dist/src/runner.d.ts
CHANGED
|
@@ -137,11 +137,17 @@ export declare function readAgentsMd(dir: string): {
|
|
|
137
137
|
* the two can never disagree about what an agent needs.
|
|
138
138
|
*/
|
|
139
139
|
export declare function agentRuntimeSpec(tenant: string, workspace: string, agentDir: string): RuntimeSpec | null;
|
|
140
|
-
/**
|
|
140
|
+
/** The node disk one agent's steps may write: its `size:` class's, or its
|
|
141
|
+
* own `resources: disk` over it — the figure the runner gives its pod. */
|
|
142
|
+
export declare function agentDisk(agentDir: string): string;
|
|
143
|
+
/** Every distinct runtime a workspace's agents need, with who needs it and
|
|
144
|
+
* `disk`, the most node disk any of those agents' steps may write (a
|
|
145
|
+
* runtime built for them has at least that much room to be built in). */
|
|
141
146
|
export declare function workspaceRuntimePlan(tenant: string, workspace: string): {
|
|
142
147
|
fingerprint: string;
|
|
143
148
|
spec: RuntimeSpec;
|
|
144
149
|
agents: string[];
|
|
150
|
+
disk: string;
|
|
145
151
|
}[];
|
|
146
152
|
/**
|
|
147
153
|
* The calendar a set of frontmatter asks for, with no cascade around it —
|
package/dist/src/runner.js
CHANGED
|
@@ -20,7 +20,7 @@ import { resolveAgentPath } from "./confine.js";
|
|
|
20
20
|
import { browserPodLine, podLossDecision, RERAN_ON_FULL } from "./browser-pod.js";
|
|
21
21
|
import { eventUrl } from "./webhook.js";
|
|
22
22
|
import { runStepInContainer, resolveResources, memoryGiB, heldCores, killRunSandboxes, classDisk } from "./run-container.js";
|
|
23
|
-
import { readResources, hasOverride } from "./resources.js";
|
|
23
|
+
import { readResources, hasOverride, anyMemoryMi } from "./resources.js";
|
|
24
24
|
import { hostSafeEnv, platformApiUrl } from "./host-env.js";
|
|
25
25
|
import { resolveModelCredential, credentialLine, accountModelBlock, isClaudeLoginToken, isModelEnvName, DECLARED_ENV_PREFIX } from "./model-credential.js";
|
|
26
26
|
import { parseJudge } from "./judge-verdict.js";
|
|
@@ -484,19 +484,42 @@ export function agentRuntimeSpec(tenant, workspace, agentDir) {
|
|
|
484
484
|
.map(toolRuntime);
|
|
485
485
|
return mergeRuntimes(ownRuntime(front, agentDir, tenant), ...tools);
|
|
486
486
|
}
|
|
487
|
-
/**
|
|
487
|
+
/** The node disk one agent's steps may write: its `size:` class's, or its
|
|
488
|
+
* own `resources: disk` over it — the figure the runner gives its pod. */
|
|
489
|
+
export function agentDisk(agentDir) {
|
|
490
|
+
const file = path.join(agentDir, "agent.md");
|
|
491
|
+
let front = {};
|
|
492
|
+
try {
|
|
493
|
+
// The step's own parser: it lifts `guardrail: { resources: … }` to the
|
|
494
|
+
// top, so both spellings of a disk override count here as they do there.
|
|
495
|
+
if (fs.existsSync(file))
|
|
496
|
+
front = configMatter(fs.readFileSync(file, "utf8")).data;
|
|
497
|
+
}
|
|
498
|
+
catch {
|
|
499
|
+
/* frontmatter that does not parse: the class default, as `check` reports it */
|
|
500
|
+
}
|
|
501
|
+
const size = front.size === "small" ? "small" : front.size === "heavy" ? "heavy" : "large";
|
|
502
|
+
return resolveResources(size, readResources(front.resources).value).disk ?? classDisk(size);
|
|
503
|
+
}
|
|
504
|
+
/** Every distinct runtime a workspace's agents need, with who needs it and
|
|
505
|
+
* `disk`, the most node disk any of those agents' steps may write (a
|
|
506
|
+
* runtime built for them has at least that much room to be built in). */
|
|
488
507
|
export function workspaceRuntimePlan(tenant, workspace) {
|
|
489
508
|
const byFp = new Map();
|
|
490
509
|
const agentsRoot = path.join(workspaceDir(tenant, workspace), "agents");
|
|
491
510
|
if (!fs.existsSync(agentsRoot))
|
|
492
511
|
return [];
|
|
493
512
|
for (const name of fs.readdirSync(agentsRoot).sort()) {
|
|
494
|
-
const
|
|
513
|
+
const dir = path.join(agentsRoot, name);
|
|
514
|
+
const spec = agentRuntimeSpec(tenant, workspace, dir);
|
|
495
515
|
if (!spec)
|
|
496
516
|
continue;
|
|
497
517
|
const fp = fingerprint(spec);
|
|
498
|
-
const
|
|
518
|
+
const disk = agentDisk(dir);
|
|
519
|
+
const entry = byFp.get(fp) ?? { fingerprint: fp, spec, agents: [], disk };
|
|
499
520
|
entry.agents.push(name);
|
|
521
|
+
if (anyMemoryMi(disk) > anyMemoryMi(entry.disk))
|
|
522
|
+
entry.disk = disk;
|
|
500
523
|
byFp.set(fp, entry);
|
|
501
524
|
}
|
|
502
525
|
return [...byFp.values()];
|
package/package.json
CHANGED
package/src/engine-common.ts
CHANGED
|
@@ -70,6 +70,10 @@ export interface LoopOptions {
|
|
|
70
70
|
/** Compaction (engine-compact.ts): the model's context window, and false
|
|
71
71
|
* when the caller turned it off (`compaction: false`). */
|
|
72
72
|
compaction: { contextWindow: number; reserveTokens: number; keepRecentTokens: number } | false;
|
|
73
|
+
/** Steering (FOL-213): what a person sent while the turn runs, taken
|
|
74
|
+
* (and so removed) each time it is called. The model reads it after the
|
|
75
|
+
* calls it is making, or, about to finish, reads it and goes on. */
|
|
76
|
+
steer: (() => unknown[]) | null;
|
|
73
77
|
}
|
|
74
78
|
|
|
75
79
|
export function hookFns(hooks: unknown, event: string): HookFn[] {
|
|
@@ -140,6 +144,7 @@ export function readOptions(options: Json, engine = "foldrun"): LoopOptions {
|
|
|
140
144
|
history: readHistory(options.history),
|
|
141
145
|
mcpServers,
|
|
142
146
|
mcpGranted: mcpTools,
|
|
147
|
+
steer: typeof options.steer === "function" ? (options.steer as () => unknown[]) : null,
|
|
143
148
|
compaction: options.compaction === false ? false : compactionFor(
|
|
144
149
|
typeof options.contextWindow === "number" && Number.isFinite(options.contextWindow) && options.contextWindow >= MIN_CONTEXT_WINDOW ? Math.floor(options.contextWindow) : COMPACTION_DEFAULTS.contextWindow,
|
|
145
150
|
),
|
package/src/engine-loop.ts
CHANGED
|
@@ -70,6 +70,10 @@ function requestHeaders(env: Record<string, string | undefined>): Record<string,
|
|
|
70
70
|
* included, so a half-written answer passed as a finished step (FOL-211).
|
|
71
71
|
* The partial reply is still the result's text; `errors` says why it is not
|
|
72
72
|
* a finish, the way the SDK's error results do. */
|
|
73
|
+
/** What this engine can do that a caller may ask about before relying on
|
|
74
|
+
* it: `steer` — messages sent while a turn runs (FOL-213). */
|
|
75
|
+
export const ENGINE_FEATURES = { steer: true } as const;
|
|
76
|
+
|
|
73
77
|
export function endOf(stopReason: string | null, calls: number): { subtype: string; error?: string } {
|
|
74
78
|
switch (stopReason) {
|
|
75
79
|
case "end_turn":
|
|
@@ -258,6 +262,18 @@ async function* run(prompt: string, o: LoopOptions, signal: AbortSignal, interru
|
|
|
258
262
|
const sessionId = crypto.randomUUID();
|
|
259
263
|
const startedAt = Date.now();
|
|
260
264
|
const byName = new Map(o.tools.map((t) => [t.name, t]));
|
|
265
|
+
/** What the person sent meanwhile, as text blocks: each a string with
|
|
266
|
+
* words in it, held to a sane size; anything else is dropped. */
|
|
267
|
+
// Taken only when the model will get it: not once the turn is stopped,
|
|
268
|
+
// nor when no request is left under maxTurns (Codex) — it stays with the
|
|
269
|
+
// caller, unsent.
|
|
270
|
+
const steered = (): Block[] => {
|
|
271
|
+
if (!o.steer || interrupted() || signal.aborted || (o.maxTurns !== null && turns >= o.maxTurns)) return [];
|
|
272
|
+
const taken = o.steer();
|
|
273
|
+
return (Array.isArray(taken) ? taken : [])
|
|
274
|
+
.filter((t): t is string => typeof t === "string" && t.trim() !== "")
|
|
275
|
+
.map((t) => ({ type: "text", text: t.slice(0, 20_000) }));
|
|
276
|
+
};
|
|
261
277
|
const ctx: ToolContext = { cwd: o.cwd, env: o.shellEnv, signal, reads: new Map(), groups: new Set() };
|
|
262
278
|
// An earlier conversation comes first (foldrun code's local turns carry
|
|
263
279
|
// theirs, 19 Nov 2026); the prompt is always the newest user turn.
|
|
@@ -466,6 +482,19 @@ async function* run(prompt: string, o: LoopOptions, signal: AbortSignal, interru
|
|
|
466
482
|
if (turn.stopReason === "pause_turn" && !calls.length) continue;
|
|
467
483
|
if (turn.stopReason !== "tool_use" || !calls.length) {
|
|
468
484
|
const end = endOf(turn.stopReason, calls.length);
|
|
485
|
+
// About to finish with a message from the person waiting: it is the
|
|
486
|
+
// next user turn, and the model goes on. Not after a turn that failed
|
|
487
|
+
// (it stays with the caller, unsent).
|
|
488
|
+
if (end.subtype === "success") {
|
|
489
|
+
const more = steered();
|
|
490
|
+
if (more.length) {
|
|
491
|
+
// A new question: the answer to come is its own, not this one (Codex).
|
|
492
|
+
lastText = "";
|
|
493
|
+
messages.push({ role: "user", content: more });
|
|
494
|
+
yield { type: "user", message: { role: "user", content: more }, parent_tool_use_id: null, session_id: sessionId };
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
469
498
|
// A turn that did not finish carries ITS text, empty included — not an
|
|
470
499
|
// earlier turn's "I'll check" passed off as a refusal's words (Codex).
|
|
471
500
|
yield end.error ? { ...result(end.subtype, turn.stopReason), result: text, errors: [end.error] } : result(end.subtype, turn.stopReason);
|
|
@@ -533,7 +562,8 @@ async function* run(prompt: string, o: LoopOptions, signal: AbortSignal, interru
|
|
|
533
562
|
return { type: "tool_result", tool_use_id: String(p.block.id), content, ...(isError ? { is_error: true } : {}) };
|
|
534
563
|
});
|
|
535
564
|
const context = done.flatMap((d) => d.context);
|
|
536
|
-
|
|
565
|
+
// After the results: what hooks add, then what the person sent meanwhile.
|
|
566
|
+
const content: Block[] = [...results, ...context.map((c) => ({ type: "text", text: c })), ...steered()];
|
|
537
567
|
messages.push({ role: "user", content });
|
|
538
568
|
yield { type: "user", message: { role: "user", content }, parent_tool_use_id: null, session_id: sessionId };
|
|
539
569
|
}
|
package/src/runner.ts
CHANGED
|
@@ -22,7 +22,7 @@ import { resolveAgentPath } from "./confine.ts";
|
|
|
22
22
|
import { browserPodLine, podLossDecision, RERAN_ON_FULL } from "./browser-pod.ts";
|
|
23
23
|
import { eventUrl } from "./webhook.ts";
|
|
24
24
|
import { runStepInContainer, resolveResources, memoryGiB, heldCores, killRunSandboxes, classDisk, type StepTiming } from "./run-container.ts";
|
|
25
|
-
import { readResources, hasOverride } from "./resources.ts";
|
|
25
|
+
import { readResources, hasOverride, anyMemoryMi } from "./resources.ts";
|
|
26
26
|
import { hostSafeEnv, platformApiUrl } from "./host-env.ts";
|
|
27
27
|
import { resolveModelCredential, credentialLine, accountModelBlock, isClaudeLoginToken, isModelEnvName, DECLARED_ENV_PREFIX } from "./model-credential.ts";
|
|
28
28
|
import { parseJudge } from "./judge-verdict.ts";
|
|
@@ -567,20 +567,41 @@ export function agentRuntimeSpec(tenant: string, workspace: string, agentDir: st
|
|
|
567
567
|
return mergeRuntimes(ownRuntime(front, agentDir, tenant), ...tools);
|
|
568
568
|
}
|
|
569
569
|
|
|
570
|
-
/**
|
|
570
|
+
/** The node disk one agent's steps may write: its `size:` class's, or its
|
|
571
|
+
* own `resources: disk` over it — the figure the runner gives its pod. */
|
|
572
|
+
export function agentDisk(agentDir: string): string {
|
|
573
|
+
const file = path.join(agentDir, "agent.md");
|
|
574
|
+
let front: Record<string, unknown> = {};
|
|
575
|
+
try {
|
|
576
|
+
// The step's own parser: it lifts `guardrail: { resources: … }` to the
|
|
577
|
+
// top, so both spellings of a disk override count here as they do there.
|
|
578
|
+
if (fs.existsSync(file)) front = configMatter(fs.readFileSync(file, "utf8")).data as Record<string, unknown>;
|
|
579
|
+
} catch {
|
|
580
|
+
/* frontmatter that does not parse: the class default, as `check` reports it */
|
|
581
|
+
}
|
|
582
|
+
const size: "small" | "large" | "heavy" = front.size === "small" ? "small" : front.size === "heavy" ? "heavy" : "large";
|
|
583
|
+
return resolveResources(size, readResources(front.resources).value).disk ?? classDisk(size);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/** Every distinct runtime a workspace's agents need, with who needs it and
|
|
587
|
+
* `disk`, the most node disk any of those agents' steps may write (a
|
|
588
|
+
* runtime built for them has at least that much room to be built in). */
|
|
571
589
|
export function workspaceRuntimePlan(
|
|
572
590
|
tenant: string,
|
|
573
591
|
workspace: string,
|
|
574
|
-
): { fingerprint: string; spec: RuntimeSpec; agents: string[] }[] {
|
|
575
|
-
const byFp = new Map<string, { fingerprint: string; spec: RuntimeSpec; agents: string[] }>();
|
|
592
|
+
): { fingerprint: string; spec: RuntimeSpec; agents: string[]; disk: string }[] {
|
|
593
|
+
const byFp = new Map<string, { fingerprint: string; spec: RuntimeSpec; agents: string[]; disk: string }>();
|
|
576
594
|
const agentsRoot = path.join(workspaceDir(tenant, workspace), "agents");
|
|
577
595
|
if (!fs.existsSync(agentsRoot)) return [];
|
|
578
596
|
for (const name of fs.readdirSync(agentsRoot).sort()) {
|
|
579
|
-
const
|
|
597
|
+
const dir = path.join(agentsRoot, name);
|
|
598
|
+
const spec = agentRuntimeSpec(tenant, workspace, dir);
|
|
580
599
|
if (!spec) continue;
|
|
581
600
|
const fp = fingerprint(spec);
|
|
582
|
-
const
|
|
601
|
+
const disk = agentDisk(dir);
|
|
602
|
+
const entry = byFp.get(fp) ?? { fingerprint: fp, spec, agents: [], disk };
|
|
583
603
|
entry.agents.push(name);
|
|
604
|
+
if (anyMemoryMi(disk) > anyMemoryMi(entry.disk)) entry.disk = disk;
|
|
584
605
|
byFp.set(fp, entry);
|
|
585
606
|
}
|
|
586
607
|
return [...byFp.values()];
|