Files
PaperClipAI/packages/adapter-utils/src/acpx-engine/ephemeral-session-environment.test.ts
T
commitperclip[bot] 2d72f2c4c0 fix: Serialize ACPX session record saves per record to stop overlapping renames (EPERM on Windows) (#16925)
> [!NOTE]
> Opened by Prospector's automated issue-fix pipeline. A maintainer
reviews and decides; nothing here merges on its own.

## Thinking Path

- Issue #15346 reports a defect in paperclipai/paperclip.
- Root cause, as the fixing agent read it: ACPX saves one session record
from several writers (live checkpoint timer, turn end, finalize, close)
with no coordination; each save renames a temp file onto the same record
file, and on Windows overlapping renames onto one file fail with EPERM.
All of these saves go through Paperclip's session-store wrapper, which
forwarded them concurrently.
- This pull request: Serialize ACPX session record saves per record to
stop overlapping renames (EPERM on Windows)

## What Changed

-
`packages/adapter-utils/src/acpx-engine/ephemeral-session-environment.ts`:
Every ACPX save in Paperclip passes through this wrapper, so it now puts
all saves of one record id in a single queue (a promise chain shared by
all wrappers in the process). Saves of one record never overlap and keep
their order; a failed save still returns its own error and does not
block the next one; saves of different records still run in parallel.

## Verification

- Reproducing test(s), added in this PR:
`packages/adapter-utils/src/acpx-engine/ephemeral-session-environment.test.ts`
- They fail on the base commit and pass with the fix, each twice in a
fresh sandbox.
- 3 independent attempts wrote 3 reproductions; this fix passes every
one of them.
- The repository's compile command passes with the fix applied.
- 3 existing test file(s) near the change pass.
- The full test suite passes except for the 246 file(s) that already
fail on the base commit.

## Risks

- Written by an automated pipeline, not a person: review it as you would
an outside contribution.
- Risk tier of the touched paths: 2.
- The scope reviewer found it also changes, beyond the report: save()
called when no earlier save of that record is pending
- The scope reviewer found it also changes, beyond the report:
persistedStore.save throwing synchronously
- The scope reviewer found it also changes, beyond the report: Records
with missing/undefined acpxRecordId
- The scope reviewer found it also changes, beyond the report: save()
for different record ids

## Model Used

- opus, sonnet (Anthropic Claude, via Claude Code), writing and checking
the change in an isolated sandbox.

## Linked Issues

Fixes #15346

Other pull requests that name #15346:
- #15370 (closed): fix(acpx): serialize session file saves and clean
failed temporary writes

## 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)
- [ ] 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)
- [ ] 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
- [ ] 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
- [ ] I will address all Greptile and reviewer comments before
requesting merge

<!-- prospector:issue-fix v1 issue=15346 base=1c07b5903b1b
report=4fe3fefc8864e00f -->

Co-authored-by: commitperclip[bot] <284812584+commitperclip[bot]@users.noreply.github.com>
2026-10-26 20:55:37 -07:00

207 lines
12 KiB
TypeScript

import { mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createRuntimeStore, type AcpSessionRecord, type AcpSessionStore } from "acpx/runtime";
import { afterEach, describe, expect, it, vi } from "vitest";
import { createEphemeralSessionEnvironmentStore } from "./ephemeral-session-environment.js";
const currentEnvironment = Object.freeze({
PAPERCLIP_API_KEY: "current-credential-fixture",
PAPERCLIP_RUN_ID: "current-run-fixture",
PATH: "/fixture/current-bin",
});
const oldEnvironment = { PAPERCLIP_API_KEY: "old-credential-fixture", OLD_RUN_PATH: "/fixture/old-run" };
function record(): AcpSessionRecord {
return {
schema: "acpx.session.v1",
acpxRecordId: "fixture-record",
acpSessionId: "fixture-session",
agentSessionId: "fixture-agent-session",
agentCommand: "fixture-provider",
cwd: "/fixture/project",
name: "fixture-name",
createdAt: "2026-10-03T00:00:00.000Z",
lastUsedAt: "2026-10-03T00:00:01.000Z",
lastSeq: 3,
eventLog: { active_path: "/fixture/events.jsonl", segment_count: 1, max_segment_bytes: 1024, max_segments: 2 },
messages: [{ User: { id: "message-1", content: [{ Text: "Retained conversation" }] } }],
updated_at: "2026-10-03T00:00:01.000Z",
cumulative_token_usage: { input_tokens: 11, output_tokens: 7 },
request_token_usage: { request_1: { input_tokens: 11 } },
importedFrom: { recordId: "origin-record", cwdOriginal: "/fixture/origin", exportedBy: "fixture-caller", exportedAt: "2026-10-03T00:00:00.000Z" },
acpx: {
current_mode_id: "code",
desired_config_options: { effort: "high" },
session_options: { model: "fixture-model", allowed_tools: ["Read", "Write"], max_turns: 4, system_prompt: { append: "Retained caller option" }, env: { ...currentEnvironment } },
},
};
}
function freeze<T extends object>(value: T): T {
for (const child of Object.values(value)) if (typeof child === "object" && child !== null) freeze(child);
return Object.freeze(value);
}
function stubStore(loaded: AcpSessionRecord | undefined) {
const load = vi.fn<AcpSessionStore["load"]>().mockResolvedValue(loaded);
const save = vi.fn<AcpSessionStore["save"]>().mockResolvedValue(undefined);
return { load, save };
}
const directories: string[] = [];
afterEach(async () => { await Promise.all(directories.splice(0).map(directory => rm(directory, { recursive: true, force: true }))); });
async function diskStore() {
const stateDir = await mkdtemp(join(tmpdir(), "paperclip-session-env-")); directories.push(stateDir);
return { store: createRuntimeStore({ stateDir }), file: join(stateDir, "sessions", "fixture-record.json") };
}
describe("ephemeral ACPX session environment", () => {
it("saves a fresh session to the actual file store without persisting its live environment", async () => {
const disk = await diskStore(), input = freeze(record());
const wrapper = createEphemeralSessionEnvironmentStore(disk.store, currentEnvironment);
await wrapper.save(input);
const text = await readFile(disk.file, "utf8"), saved = JSON.parse(text);
expect(saved.acpx.session_options).not.toHaveProperty("env");
for (const value of Object.values(currentEnvironment)) expect(text).not.toContain(value);
expect(input.acpx?.session_options?.env).toEqual(currentEnvironment);
expect(saved.acpx.current_mode_id).toBe("code");
expect(saved.acpx.desired_config_options).toEqual(input.acpx?.desired_config_options);
expect(saved.acpx.session_options).toEqual({ model: "fixture-model", allowed_tools: ["Read", "Write"], max_turns: 4, system_prompt: { append: "Retained caller option" } });
expect(saved.messages).toEqual(input.messages);
expect(saved.imported_from.record_id).toBe("origin-record");
const loaded = await wrapper.load(input.acpxRecordId);
expect(loaded?.acpx?.session_options?.env).toEqual(currentEnvironment);
expect(loaded?.messages).toEqual(input.messages);
});
it("loads an old env-bearing file with rotated credentials without writing, then saves neither environment", async () => {
const disk = await diskStore(), legacy = record(); legacy.acpx!.session_options!.env = { ...oldEnvironment };
await disk.store.save(legacy);
const before = await readFile(disk.file, "utf8");
const wrapper = createEphemeralSessionEnvironmentStore(disk.store, currentEnvironment);
const loaded = await wrapper.load(legacy.acpxRecordId);
expect(loaded?.acpx?.session_options?.env).toEqual(currentEnvironment);
expect(loaded?.acpx?.session_options?.env).not.toHaveProperty("OLD_RUN_PATH");
expect(await readFile(disk.file, "utf8")).toBe(before);
expect(legacy.acpx?.session_options?.env).toEqual(oldEnvironment);
await wrapper.save(loaded!);
const after = await readFile(disk.file, "utf8");
for (const value of [...Object.values(oldEnvironment), ...Object.values(currentEnvironment)]) expect(after).not.toContain(value);
expect(JSON.parse(after).acpx.session_options).not.toHaveProperty("env");
expect(loaded?.acpx?.session_options?.env).toEqual(currentEnvironment);
});
it("loads an env-free persisted session into the current environment without writing", async () => {
const disk = await diskStore(), input = record(); delete input.acpx!.session_options!.env;
await disk.store.save(input);
const before = await readFile(disk.file, "utf8");
const loaded = await createEphemeralSessionEnvironmentStore(disk.store, currentEnvironment).load(input.acpxRecordId);
expect(loaded?.acpx?.session_options?.env).toEqual(currentEnvironment);
expect(loaded?.acpx?.session_options?.model).toBe("fixture-model");
expect(await readFile(disk.file, "utf8")).toBe(before);
});
it("copies loaded records, options and current env without mutating stored or caller-owned objects", async () => {
const stored = record(); stored.acpx!.session_options!.env = { ...oldEnvironment }; freeze(stored);
const backing = stubStore(stored), wrapper = createEphemeralSessionEnvironmentStore(backing, currentEnvironment);
const loaded = await wrapper.load("fixture-record");
expect(backing.load).toHaveBeenCalledWith("fixture-record"); expect(backing.save).not.toHaveBeenCalled();
expect(loaded).not.toBe(stored); expect(loaded?.acpx).not.toBe(stored.acpx);
expect(loaded?.acpx?.session_options).not.toBe(stored.acpx?.session_options);
expect(loaded?.acpx?.session_options?.env).not.toBe(currentEnvironment);
expect(stored.acpx?.session_options?.env).toEqual(oldEnvironment);
loaded!.acpx!.session_options!.env!.PAPERCLIP_API_KEY = "isolated-loaded-fixture";
expect(currentEnvironment.PAPERCLIP_API_KEY).toBe("current-credential-fixture");
});
it("saves only a copied env-free projection, preserving unrelated and caller metadata", async () => {
const input = freeze(Object.assign(record(), { caller_metadata: { keep: "caller-state", env: { preserve: "unrelated caller metadata" } } }));
const backing = stubStore(undefined), wrapper = createEphemeralSessionEnvironmentStore(backing, currentEnvironment);
await wrapper.save(input);
const saved = backing.save.mock.calls[0]![0];
const { env: _environment, ...expectedOptions } = input.acpx!.session_options!;
expect(saved).toEqual({ ...input, acpx: { ...input.acpx, session_options: expectedOptions } });
expect(saved).not.toBe(input); expect(saved.acpx).not.toBe(input.acpx);
expect(saved.acpx?.session_options).not.toBe(input.acpx?.session_options);
expect(saved.acpx?.session_options).not.toHaveProperty("env");
expect(input.acpx?.session_options?.env).toEqual(currentEnvironment);
});
for (const [name, acpx] of [
["absent acpx", undefined],
["explicit undefined acpx", undefined],
["absent session options", { current_mode_id: "code" }],
["undefined session options", { session_options: undefined }],
["empty session options", { session_options: {} }],
["undefined env", { session_options: { env: undefined, model: "fixture-model" } }],
] as const) it(`preserves ${name} when saving and retains existing load injection semantics`, async () => {
const input = record();
if (name === "absent acpx") delete input.acpx; else input.acpx = acpx;
freeze(input);
const backing = stubStore(input), wrapper = createEphemeralSessionEnvironmentStore(backing, currentEnvironment);
await wrapper.save(input);
const saved = backing.save.mock.calls[0]![0];
if (name === "undefined env") expect(saved.acpx?.session_options).toEqual({ model: "fixture-model" });
else expect(saved).toEqual(input);
expect(Object.hasOwn(saved, "acpx")).toBe(Object.hasOwn(input, "acpx"));
if (saved.acpx && input.acpx) expect(Object.hasOwn(saved.acpx, "session_options")).toBe(Object.hasOwn(input.acpx, "session_options"));
const loaded = await wrapper.load(input.acpxRecordId);
expect(loaded?.acpx?.session_options?.env).toEqual(currentEnvironment);
expect(backing.save).toHaveBeenCalledTimes(1);
});
it("returns a missing record without saving or creating conversation state", async () => {
const backing = stubStore(undefined);
expect(await createEphemeralSessionEnvironmentStore(backing, currentEnvironment).load("missing")).toBeUndefined();
expect(backing.save).not.toHaveBeenCalled();
});
it("propagates load and save errors unchanged", async () => {
const failure = new Error("fixture store failure"), backing = stubStore(undefined);
backing.load.mockRejectedValue(failure); backing.save.mockRejectedValue(failure);
const wrapper = createEphemeralSessionEnvironmentStore(backing, currentEnvironment);
await expect(wrapper.load("fixture-record")).rejects.toBe(failure);
const input = freeze(record()); await expect(wrapper.save(input)).rejects.toBe(failure);
expect(input.acpx?.session_options?.env).toEqual(currentEnvironment);
});
it("never overlaps two saves of one record, even across wrappers, and keeps their order", async () => {
let active = 0, maxActive = 0;
const order: string[] = [];
const failure = new Error("fixture rename failure");
const backing = stubStore(undefined);
backing.save.mockImplementation(async (saved) => {
active += 1; maxActive = Math.max(maxActive, active);
await new Promise(resolve => setTimeout(resolve, 5));
order.push(saved.lastUsedAt);
active -= 1;
if (saved.lastUsedAt === "checkpoint") throw failure;
});
const turnStore = createEphemeralSessionEnvironmentStore(backing, currentEnvironment);
const closeStore = createEphemeralSessionEnvironmentStore(backing, currentEnvironment);
const results = await Promise.allSettled([
turnStore.save({ ...record(), lastUsedAt: "checkpoint" }),
turnStore.save({ ...record(), lastUsedAt: "turn-end" }),
closeStore.save({ ...record(), lastUsedAt: "close" }),
]);
expect(maxActive).toBe(1);
expect(order).toEqual(["checkpoint", "turn-end", "close"]);
expect(results.map(result => result.status)).toEqual(["rejected", "fulfilled", "fulfilled"]);
expect((results[0] as PromiseRejectedResult).reason).toBe(failure);
});
it("does not serialize saves of different records", async () => {
let active = 0, maxActive = 0;
const backing = stubStore(undefined);
backing.save.mockImplementation(async () => {
active += 1; maxActive = Math.max(maxActive, active);
await new Promise(resolve => setTimeout(resolve, 5));
active -= 1;
});
const wrapper = createEphemeralSessionEnvironmentStore(backing, currentEnvironment);
await Promise.all([
wrapper.save({ ...record(), acpxRecordId: "fixture-record-a" }),
wrapper.save({ ...record(), acpxRecordId: "fixture-record-b" }),
]);
expect(maxActive).toBe(2);
});
});