@foldrun/core 24.4.1 → 24.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.
@@ -31,6 +31,12 @@ interface LoopOptions {
31
31
  post: HookFn[];
32
32
  canUseTool: CanUseTool | null;
33
33
  outer: AbortSignal | null;
34
+ /** The conversation so far, before this prompt: earlier user turns, the
35
+ * model's replies and tool results, as the loop yielded them. */
36
+ history: {
37
+ role: string;
38
+ content: Block[];
39
+ }[];
34
40
  }
35
41
  /** The request body for one turn. Cache marks go on the three places a
36
42
  * prefix ends: the tools, the system prompt, and the newest message — so
@@ -87,8 +87,27 @@ function readOptions(options) {
87
87
  post: hookFns(options.hooks, "PostToolUse"),
88
88
  canUseTool: typeof options.canUseTool === "function" ? options.canUseTool : null,
89
89
  outer: options.abortController?.signal ?? null,
90
+ history: readHistory(options.history),
90
91
  };
91
92
  }
93
+ /** An earlier conversation, checked: user and assistant turns that
94
+ * alternate, starting with the user's, each a list of content blocks. A
95
+ * turn that is not one is refused, not sent half-right to the model. */
96
+ function readHistory(raw) {
97
+ if (raw == null)
98
+ return [];
99
+ if (!Array.isArray(raw))
100
+ throw new Error("history must be a list of turns");
101
+ return raw.map((t, i) => {
102
+ const role = t?.role;
103
+ const content = t?.content;
104
+ if (role !== (i % 2 === 0 ? "user" : "assistant"))
105
+ throw new Error(`history turn ${i + 1} must be the ${i % 2 === 0 ? "user's" : "model's"}`);
106
+ if (!Array.isArray(content) || !content.every((b) => b && typeof b.type === "string"))
107
+ throw new Error(`history turn ${i + 1} has no content blocks`);
108
+ return { role: String(role), content: content };
109
+ });
110
+ }
92
111
  function requestHeaders(env) {
93
112
  const headers = { "content-type": "application/json", "anthropic-version": API_VERSION };
94
113
  if (env.ANTHROPIC_API_KEY)
@@ -327,7 +346,11 @@ async function* run(prompt, o, signal, interrupted) {
327
346
  const startedAt = Date.now();
328
347
  const byName = new Map(o.tools.map((t) => [t.name, t]));
329
348
  const ctx = { cwd: o.cwd, env: o.env, signal, reads: new Map(), groups: new Set() };
330
- const messages = [{ role: "user", content: [{ type: "text", text: prompt }] }];
349
+ // An earlier conversation comes first (foldrun code's local turns carry
350
+ // theirs, 19 Nov 2026); the prompt is always the newest user turn.
351
+ if (o.history.length % 2 !== 0)
352
+ throw new Error("history must end with the model's turn, so the prompt is the next user turn");
353
+ const messages = [...o.history, { role: "user", content: [{ type: "text", text: prompt }] }];
331
354
  const total = emptyUsage();
332
355
  let turns = 0;
333
356
  let lastText = "";
@@ -442,7 +442,9 @@ const workspaceRootOf = (agentDir) => path.join(agentDir, "..", "..");
442
442
  const SNAPSHOT_NOTE = "The folder you were sent to work on is in snapshot/ (inside your working directory). Make your changes there, as files: " +
443
443
  "when you finish, foldrun turns what changed in snapshot/ into a patch the person applies. " +
444
444
  "Only snapshot/ (as that patch) and your outputs/ come back from this step; anything you write elsewhere is not kept. " +
445
- "Do not create links or a .git folder in snapshot/ — links make the patch refused, and .git is left out.";
445
+ "Do not create links or a .git folder in snapshot/ — links make the patch refused, and .git is left out. " +
446
+ "snapshot/ is the person's own folder: when you write to them, name paths from its top (site/index.html, not snapshot/site/index.html), " +
447
+ "and do not mention snapshot/, outputs/, the sandbox or the patch — to them it is just their folder.";
446
448
  /**
447
449
  * Configuration from AGENTS.md, nearest-wins: the workspace's overrides the
448
450
  * account's, key by key.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foldrun/core",
3
- "version": "24.4.1",
3
+ "version": "24.5.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",
@@ -34,7 +34,8 @@
34
34
  "./subagents",
35
35
  "./tool-names",
36
36
  "./scan",
37
- "./snapshot-archive"
37
+ "./snapshot-archive",
38
+ "./engine-loop"
38
39
  ]
39
40
  },
40
41
  "main": "./dist/index.js",
@@ -80,6 +80,9 @@ interface LoopOptions {
80
80
  post: HookFn[];
81
81
  canUseTool: CanUseTool | null;
82
82
  outer: AbortSignal | null;
83
+ /** The conversation so far, before this prompt: earlier user turns, the
84
+ * model's replies and tool results, as the loop yielded them. */
85
+ history: { role: string; content: Block[] }[];
83
86
  }
84
87
 
85
88
  function hookFns(hooks: unknown, event: string): HookFn[] {
@@ -122,9 +125,25 @@ function readOptions(options: Json): LoopOptions {
122
125
  post: hookFns(options.hooks, "PostToolUse"),
123
126
  canUseTool: typeof options.canUseTool === "function" ? (options.canUseTool as CanUseTool) : null,
124
127
  outer: (options.abortController as AbortController | undefined)?.signal ?? null,
128
+ history: readHistory(options.history),
125
129
  };
126
130
  }
127
131
 
132
+ /** An earlier conversation, checked: user and assistant turns that
133
+ * alternate, starting with the user's, each a list of content blocks. A
134
+ * turn that is not one is refused, not sent half-right to the model. */
135
+ function readHistory(raw: unknown): { role: string; content: Block[] }[] {
136
+ if (raw == null) return [];
137
+ if (!Array.isArray(raw)) throw new Error("history must be a list of turns");
138
+ return raw.map((t, i) => {
139
+ const role = (t as Json)?.role;
140
+ const content = (t as Json)?.content;
141
+ if (role !== (i % 2 === 0 ? "user" : "assistant")) throw new Error(`history turn ${i + 1} must be the ${i % 2 === 0 ? "user's" : "model's"}`);
142
+ if (!Array.isArray(content) || !content.every((b) => b && typeof (b as Json).type === "string")) throw new Error(`history turn ${i + 1} has no content blocks`);
143
+ return { role: String(role), content: content as Block[] };
144
+ });
145
+ }
146
+
128
147
  function requestHeaders(env: Record<string, string | undefined>): Record<string, string> {
129
148
  const headers: Record<string, string> = { "content-type": "application/json", "anthropic-version": API_VERSION };
130
149
  if (env.ANTHROPIC_API_KEY) headers["x-api-key"] = env.ANTHROPIC_API_KEY;
@@ -346,7 +365,10 @@ async function* run(prompt: string, o: LoopOptions, signal: AbortSignal, interru
346
365
  const startedAt = Date.now();
347
366
  const byName = new Map(o.tools.map((t) => [t.name, t]));
348
367
  const ctx: ToolContext = { cwd: o.cwd, env: o.env, signal, reads: new Map(), groups: new Set() };
349
- const messages: { role: string; content: Block[] }[] = [{ role: "user", content: [{ type: "text", text: prompt }] }];
368
+ // An earlier conversation comes first (foldrun code's local turns carry
369
+ // theirs, 19 Nov 2026); the prompt is always the newest user turn.
370
+ if (o.history.length % 2 !== 0) throw new Error("history must end with the model's turn, so the prompt is the next user turn");
371
+ const messages: { role: string; content: Block[] }[] = [...o.history, { role: "user", content: [{ type: "text", text: prompt }] }];
350
372
  const total = emptyUsage();
351
373
  let turns = 0;
352
374
  let lastText = "";
package/src/runner.ts CHANGED
@@ -522,7 +522,9 @@ const SNAPSHOT_NOTE =
522
522
  "The folder you were sent to work on is in snapshot/ (inside your working directory). Make your changes there, as files: " +
523
523
  "when you finish, foldrun turns what changed in snapshot/ into a patch the person applies. " +
524
524
  "Only snapshot/ (as that patch) and your outputs/ come back from this step; anything you write elsewhere is not kept. " +
525
- "Do not create links or a .git folder in snapshot/ — links make the patch refused, and .git is left out.";
525
+ "Do not create links or a .git folder in snapshot/ — links make the patch refused, and .git is left out. " +
526
+ "snapshot/ is the person's own folder: when you write to them, name paths from its top (site/index.html, not snapshot/site/index.html), " +
527
+ "and do not mention snapshot/, outputs/, the sandbox or the patch — to them it is just their folder.";
526
528
 
527
529
  /**
528
530
  * Configuration from AGENTS.md, nearest-wins: the workspace's overrides the