#968·openwiki

[Bug]: Parallel tool calls on the same page corrupt it silently (leftover tails, lost edits, frontmatter-only pages)

Author: fredmnyCreated Oct 1, 2026Updated Oct 1, 2026

Description

When a page worker's model turn contains more than one tool call that touches the same page (two edit_file calls, or edit_file plus submit_page), the calls run concurrently and the writes interleave. Nothing serializes them, and the backend's write/edit are not atomic. The run still ends complete with every page marked done, so the damage reaches the wiki unnoticed.

We see three symptoms in real --update runs, on both 0.5.2 and 0.6.1:

  1. Leftover tail. A page ends with fragments of its own last paragraph, each a suffix of an earlier line, as if a shorter version was written over a longer one without truncating.
  2. Lost edit. One of two edits to the same page disappears with no error.
  3. Frontmatter-only page. A page comes back with frontmatter and no body (type, a title derived from the filename, openwiki_generated: true), while .page-manifest.json marks it complete.

In our repositories, 0.6.1 --update runs left 6 of 34 pages damaged in one wiki (one of them frontmatter-only) and 15 of 38 in another, and a clean rerun damaged pages again.

Cause. Three things combine:

  • Tool calls run concurrently. LangChain dispatches each tool call of a turn as its own task (langchain/dist/agents/ReactAgent.js:394, :469) and ToolNode awaits them with Promise.all (langchain/dist/agents/nodes/ToolNode.js:333). The page worker adds no per-path locking.
  • Writes truncate in place. OpenWikiLocalShellBackend (dist/agent/docs-only-backend.js) delegates to deepagents FilesystemBackend. In deepagents 1.13.2 (dist/src-CKk0apeE.js):
    • write opens with O_WRONLY|O_CREAT|O_TRUNC|O_NOFOLLOW and then writes (:797).
    • edit reads the whole file, applies the replacement, then opens with O_WRONLY|O_TRUNC and writes (:861-890).
    • There is no temp file plus rename and no lock. Two overlapping truncate-then-write sequences leave the tail of the longer content (symptom 1), or one read-modify-write overwrites the other (symptom 2).
  • submit_page accepts an empty read.
    • dist/generation/repository-run.js:713-714 treats the page as readable when content !== undefined, so "" passes. A read that lands between another call's truncate and write gets "".
    • repairPersistedFile (dist/okf/frontmatter.js:216, :222) then falls into rebuildMinimalConcept (:526). That builds renderFrontmatter(...) + body with an empty body (:584) and writes it back (symptom 3).
    • Claims finalize then stamps it and the manifest hashes it as complete. Nothing checks for an empty body.

The model can't avoid this on its own: Claude routinely emits several tool calls per turn, and there is no setting to turn that off for the Anthropic provider. parallel_tool_calls = false exists only for openai-chatgpt (dist/agent/openai-chatgpt-oauth.js:89). OPENWIKI_PAGE_CONCURRENCY controls workers, not calls within one turn, and the race happens at its default of 1.

Steps to Reproduce

No model calls are needed. Save this as race.mjs and run OW=$(npm root -g)/openwiki node race.mjs:

javascript
import { mkdtemp, mkdir, writeFile, readFile } from "node:fs/promises";
import os from "node:os"; import path from "node:path";
const OW = process.env.OW;
const { OpenWikiLocalShellBackend } = await import(`${OW}/dist/agent/docs-only-backend.js`);
const { OpenWikiIgnore } = await import(`${OW}/dist/agent/openwiki-ignore.js`);
const page = "/openwiki/guide.md";
const lines = ["---", "type: Reference", "title: Guide", "---", "", "# Guide", ""];
for (let i = 0; i < 200; i++) lines.push(`- item ${i}: see [Quickstart](/openwiki/quickstart.md) for the long explanation of thing ${i}.`);
lines.push("", "Final paragraph of the page.", "");
const original = lines.join("\n");
const a = ["- item 3: see [Quickstart](/openwiki/quickstart.md)", "- item 3: see [Quickstart](quickstart.md)"];
const b = ["- item 150: see [Quickstart](/openwiki/quickstart.md) for the long explanation of thing 150.", "- item 150: see [Quickstart](quickstart.md)."];
const expected = original.replace(a[0], a[1]).replace(b[0], b[1]);
const stats = { ok: 0, tail: 0, lost: 0 };
for (let t = 0; t < 200; t++) {
  const root = await mkdtemp(path.join(os.tmpdir(), "ow-race-"));
  await mkdir(path.join(root, "openwiki"), { recursive: true });
  await writeFile(path.join(root, "openwiki/guide.md"), original);
  const backend = new OpenWikiLocalShellBackend({ docsOnly: true, writableWikiPages: [page],
    openWikiIgnore: new OpenWikiIgnore([]), maxOutputBytes: 100000, outputMode: "repository",
    rootDir: root, timeout: 120, virtualMode: true });
  // What two edit_file calls in one model turn do: run concurrently on the same page.
  await Promise.all([backend.edit(page, ...a), backend.edit(page, ...b)]);
  const final = await readFile(path.join(root, "openwiki/guide.md"), "utf8");
  if (final === expected) stats.ok++;
  else if ((final.match(/Final paragraph of the page\./g) ?? []).length !== 1 || !final.endsWith("Final paragraph of the page.\n")) stats.tail++;
  else stats.lost++;
}
console.log(stats);

For the frontmatter-only page, run backend.edit(page, ...) concurrently with repairPersistedFile(backend, page, "Reference") from dist/okf/frontmatter.js in a loop. That is what an edit_file plus submit_page turn does. It produces a frontmatter-only page in about 2% of trials.

Expected Behavior

Concurrent tool calls on the same page should apply in some order and leave a valid page, with both edits applied, or one edit failing cleanly with "string not found". submit_page should never accept an empty page or rebuild it frontmatter-only.

Actual Behavior

With the script above, 200 trials on 0.6.1 gave { ok: 0, tail: 190, lost: 10 }. A close variant gave { ok: 2, tail: 152, lost: 46 }. Almost every trial corrupts the page, and almost none apply both edits.

In real runs these pages are committed and marked complete. With an automated update workflow that auto-merges the update PR, the corruption lands on the default branch with no human review.

Environment

  • OS: macOS (Darwin 27); the corrupted pages also come from CI runs on Linux (GitHub Actions, self-hosted runners), and the script below also reproduces in a node:22 linux/amd64 container
  • Node.js version: v26.7.0
  • OpenWiki version: 0.6.1 (also seen in output from 0.5.2); deepagents 1.13.2

Additional Context

Suggested fixes, most complete first:

  1. Per-path async lock in OpenWikiLocalShellBackend around write, edit, delete and readRaw, keyed by resolved path and shared by every writer, so post-processing such as repairPersistedFile is covered too. This is the only option that also prevents lost edits. Compute the lock key synchronously and join the queue before the first await: with an async realpath for the key, two calls from one turn can take the lock in the opposite order, and a write_file followed by an edit_file then loses the edit silently (up to about 1% of trials in our test).
  2. Atomic writes (temp file plus rename) in deepagents FilesystemBackend.write/edit. This fixes tails and empty reads, but not lost edits.
  3. Turn off parallel tool calls for page workers (Anthropic tool_choice.disable_parallel_tool_use, OpenAI parallel_tool_calls: false).
  4. Make submit_page reject an empty or whitespace-only page (repository-run.js:713), and have rebuildMinimalConcept refuse to produce an empty body.

Related: #839 / #840 (atomic .last-update.json), #719 and #542 (concurrent writes clobbering state). We are running a local patch along the lines of fixes 1 and 4 as a stopgap, and we're happy to test a fix or open a PR.