@foldrun/core 24.9.1 → 24.10.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.
@@ -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;
@@ -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
- const content = [...results, ...context.map((c) => ({ type: "text", text: c }))];
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foldrun/core",
3
- "version": "24.9.1",
3
+ "version": "24.10.0",
4
4
  "description": "Agents are just folders. The format, the runner, the checks.",
5
5
  "license": "FSL-1.1-ALv2",
6
6
  "type": "module",
@@ -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
  ),
@@ -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
- const content: Block[] = [...results, ...context.map((c) => ({ type: "text", text: c }))];
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
  }