From 0d3ed44550c91a5a5568f79f0d94c326a76caa0c Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Tue, 1 Dec 2026 22:51:30 +0000 Subject: [PATCH] fix(test): write observed.json atomically in acpx-provider-turn-host (#23424) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The Paperclip Runner protocol suite verifies ACPX sidecar provider-started turns using `packages/paperclip-runner/src/cli/acpx-runtime-sidecar.provider-turn.test.ts` and the test host `packages/paperclip-runner/src/test-support/acpx-provider-turn-host.mjs` > - `acpx-provider-turn-host.mjs` updated `observed.json` in place with `writeFileSync()`, which truncates the file to zero bytes before writing the JSON bytes > - When the parent test polls `observed()` concurrently during a write, `JSON.parse(readFileSync(...))` can observe the truncated empty file and throw `SyntaxError: Unexpected end of JSON input`, failing `Verify Paperclip Runner (protocol (1/4))` on `master` (for example, Cloud readiness run 46754145328) > - This pull request writes `observed.json` to a unique sibling temporary file and atomically replaces `observed.json` with `renameSync()` > - The benefit is atomic handoff between the sidecar test host and the polling test, eliminating this intermittent `master` CI failure ## Linked Issues or Issue Description **What happened?** `verify / Verify Paperclip Runner (protocol (1/4))` intermittently failed `ACPX sidecar provider-started Claude turns > leaves non-Claude agents without a provider-turn listener` on `master` (Cloud readiness run 46754145328) with `SyntaxError: Unexpected end of JSON input` at `src/cli/acpx-runtime-sidecar.provider-turn.test.ts:64`. **Expected behavior** Polling `observed.json` from the test process should always read a complete JSON document and never observe a truncated in-progress write. **Steps to reproduce** 1. Run `packages/paperclip-runner/src/cli/acpx-runtime-sidecar.provider-turn.test.ts`. 2. The child sidecar host calls `note()` in `packages/paperclip-runner/src/test-support/acpx-provider-turn-host.mjs`, which truncates and rewrites `observed.json` in place with `writeFileSync()`. 3. If the parent test reads `observed.json` during truncation, `JSON.parse()` throws `SyntaxError: Unexpected end of JSON input`. **Paperclip version or commit** `16614f4fdae31dbdd991cc41b879b4489561047a` / `3df59a1853752dea020524815737afec39882be2` **Deployment mode** Other (CI test suite) ## What Changed - Update `note()` in `packages/paperclip-runner/src/test-support/acpx-provider-turn-host.mjs` to write JSON to a unique sibling temp file and atomically replace `observed.json` via `renameSync()`. ## Verification - `pnpm --filter @paperclipai/paperclip-runner exec vitest run src/cli/acpx-runtime-sidecar.provider-turn.test.ts` — 14/14 passed. ## Risks - Low risk. Test-helper-only change scoped to `acpx-provider-turn-host.mjs`. ## Model Used - Google DeepMind Antigravity using Gemini 4 Neon (Medium) with reasoning, tool use, and local command execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --- .../src/cli/acpx-runtime-sidecar.provider-turn.test.ts | 6 ++++-- .../src/test-support/acpx-provider-turn-host.mjs | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.provider-turn.test.ts b/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.provider-turn.test.ts index aa12f651508..afbfd0cfef8 100644 --- a/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.provider-turn.test.ts +++ b/packages/paperclip-runner/src/cli/acpx-runtime-sidecar.provider-turn.test.ts @@ -1,4 +1,4 @@ -import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; @@ -366,7 +366,7 @@ describe("ACPX sidecar provider-started Claude turns", () => { }, 20_000); it("leaves non-Claude agents without a provider-turn listener", async () => { - const { sidecar, send, observed, cleanup } = await open({ agent: "codex", turns: [{ state: { live: 1 }, cycle: completedCycle }] }); + const { sidecar, root, send, observed, cleanup } = await open({ agent: "codex", turns: [{ state: { live: 1 }, cycle: completedCycle }] }); try { await send("turn.start", { turnId: "turn-1", message: "Start." }); const terminal = await sidecar.next(frame => frame.eventType === "runtime.turn_terminal"); @@ -374,6 +374,8 @@ describe("ACPX sidecar provider-started Claude turns", () => { await expect.poll(() => observed().offered, { timeout: 5_000 }).toBe(true); expect(observed().listenerRegistered).toBeUndefined(); expect(sidecar.pending(frame => frame.eventType === "runtime.turn_started")).toBe(0); + await sidecar.close(); + expect(readdirSync(root).filter(name => name.startsWith("observed.") && name.endsWith(".tmp"))).toEqual([]); } finally { await cleanup(); } }, 20_000); }); diff --git a/packages/paperclip-runner/src/test-support/acpx-provider-turn-host.mjs b/packages/paperclip-runner/src/test-support/acpx-provider-turn-host.mjs index 34ec76a6f1d..7ea0cebe815 100644 --- a/packages/paperclip-runner/src/test-support/acpx-provider-turn-host.mjs +++ b/packages/paperclip-runner/src/test-support/acpx-provider-turn-host.mjs @@ -1,15 +1,18 @@ -import { existsSync, readFileSync, writeFileSync } from "node:fs"; +import { existsSync, readFileSync, renameSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { AcpxRuntimeHost as Base } from "./acpx-blocked-output-host.mjs"; const root = () => process.env.PAPERCLIP_TEST_OUTPUT_ROOT; const script = () => JSON.parse(readFileSync(join(root(), "script.json"), "utf8")); +let noteSeq = 0; const note = (key, value) => { const path = join(root(), "observed.json"); + const tmp = join(root(), `observed.${process.pid}.${++noteSeq}.tmp`); const observed = existsSync(path) ? JSON.parse(readFileSync(path, "utf8")) : {}; observed[key] = value; - writeFileSync(path, JSON.stringify(observed)); + writeFileSync(tmp, JSON.stringify(observed)); + renameSync(tmp, path); }; const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); const gate = async (name) => { while (!existsSync(join(root(), name))) await sleep(5); };