From e35d1819fc6928f870d35e4a07ebe42338683cfa Mon Sep 17 00:00:00 2001 From: Pavel Staykov <73100735+Pavalunkata@users.noreply.github.com> Date: Sat, 5 Dec 2026 14:22:02 +0200 Subject: [PATCH] fix(acpx): bound session keys so session file names fit NAME_MAX (#22115) ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The shared ACPX engine keeps each agent session in files that ACPX names after the session key > - The session key holds two UUIDs, the task key and a fingerprint, so its length is not bounded > - When a file name is longer than the NAME_MAX of the file system, the session cannot start (`acpx_session_init_failed`, `ENAMETOOLONG`) > - With ACPX 0.12.0, summary-slot task keys exceeded 255 bytes and failed on all Linux file systems (#17785). With ACPX 0.22.0 they fit, but only with 6 bytes to spare > - On eCryptfs (NAME_MAX 143), a UUID task key also fails. Synology encrypted shared folders use eCryptfs, and self-hosted users often put the Paperclip home on an NFS export of such a folder > - This pull request replaces a key that is too long with a fixed-length digest of that key > - The benefit is that every session file name fits in 143 bytes, and keys that fit now stay the same ## Linked Issues or Issue Description Refs #17785 The ACPX 0.22.0 upgrade (#20770) removed the `...tmp` suffix that made the summary-slot names in #17785 longer than 255 bytes. This change removes the remaining risk: no session file name can be longer than 143 bytes, whatever the task key is. The same failure also occurs on eCryptfs with shorter keys. A self-hosted instance on Kubernetes has `PAPERCLIP_HOME` on an NFS export of a Synology encrypted shared folder (`namlen=143` in the mount options). On release `v2026.1106.0` (ACPX 0.12.0), a heartbeat run failed in `ensure_session`: ``` ENAMETOOLONG: name too long, open '/companies//acp-engine/agents//sessions/paperclip%3A%3A%3A__heartbeat__%3A<16-hex>.json...tmp' (acpx_session_init_failed) ``` That name is 147 bytes. ACPX 0.22.0 (on `master` since #20770) writes the temporary file in a subdirectory, so the heartbeat name now fits. But other names are still too long: | Session file name | Bytes | |---|---| | Heartbeat key, `.stream.1024.ndjson` | 141 | | UUID task key, `.json` | 150 | | UUID task key, `.stream.1024.ndjson` | 164 | | Summary-slot task key, `.json` | 235 | | Summary-slot task key, `.stream.1024.ndjson` | 249 | ## What Changed - `buildSessionKey` measures the URI-encoded key. If the longest session file name (`.stream.1024.ndjson`) would be longer than 143 bytes, it returns `paperclip:sha256:<32 hex>`. The digest is the SHA-256 of the full key, so a digested key has the same scope as the full key. - A key that fits is not changed. Thus saved heartbeat and `default` sessions still resume after an upgrade. - When a resumed run's session key differs from the saved key, the run result now reports `sessionReset` with the named code `acpx_session_identity_changed`. Before, it reported the reset only when `configFingerprint` changed. `isCompatibleSession` already refuses to resume when the keys differ, so the notice now covers every key change. This includes the one-time change from a long key to a digest. - Added `SESSION_KEY_MAX_ENCODED_LENGTH` and tests in `execute-identity.test.ts`. The tests check that a heartbeat key does not change, that issue and summary-slot keys become digests that fit in 143 bytes, and that a digest changes when the company, agent, task or fingerprint changes. - Added an old-saved/new-resumed test in `execute.test.ts`. It saves session params with a long key, as the previous release wrote them, and resumes them with the new code. The run gets the digest key and reports `acpx_session_identity_changed`. The next run resumes without a reset. ## Verification - `pnpm --filter @paperclipai/adapter-utils exec vitest run src/acpx-engine/`: 22 files and 477 tests pass. - `pnpm --filter @paperclipai/adapter-utils typecheck` passes. - Without the `buildSessionKey` change, the new test "digests a key whose file names would exceed the eCryptfs NAME_MAX" fails. - Without the `sessionReset` change, the new old-saved/new-resumed test fails. ## Risks - Previous-release story: saved session params with a key that fits carry forward unchanged and resume. Saved params with a key that becomes a digest are reset on purpose: `isCompatibleSession` compares the saved `sessionKey` with the new key, so the next run starts a new session and reports `acpx_session_identity_changed`. The saved files under the old name are not deleted. On a file system with a small NAME_MAX, these keys could not write their session files before this change, so nothing is lost. On ext4, a session with a UUID or summary-slot task key resumed before this change and starts again one time after it. - A digested key does not show the company, agent or task in logs. The session directory path and the run context still show the company and the agent. - Low risk in other areas. The key format of short keys does not change, and the code does not parse session keys. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use (shell, file edits, test runs). A human reviewed the change. ## 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 - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Claude Opus 5.5 --- .../src/acpx-engine/execute-identity.test.ts | 38 +++++++++++++++++++ .../src/acpx-engine/execute.test.ts | 27 +++++++++++++ .../adapter-utils/src/acpx-engine/execute.ts | 11 +++++- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/packages/adapter-utils/src/acpx-engine/execute-identity.test.ts b/packages/adapter-utils/src/acpx-engine/execute-identity.test.ts index 365f49cff78..fe8b2566ee9 100644 --- a/packages/adapter-utils/src/acpx-engine/execute-identity.test.ts +++ b/packages/adapter-utils/src/acpx-engine/execute-identity.test.ts @@ -9,8 +9,10 @@ import { describe, expect, it } from "vitest"; import { buildSessionFingerprint, + buildSessionKey, finalizeLaunchEnvironment, projectAcpxInheritedHostEnvironment, + SESSION_KEY_MAX_ENCODED_LENGTH, } from "./execute.js"; import type { LaunchEnvironment, @@ -261,3 +263,39 @@ describe("acpx identity split and launch environment", () => { }); }); }); + +describe("acpx session key length", () => { + const COMPANY_ID = "11111111-2222-4333-8444-555555555555"; + const AGENT_ID = "66666666-7777-4888-9999-aaaaaaaaaaaa"; + const FINGERPRINT = "0123456789abcdef"; + const longestFileName = (key: string) => `${encodeURIComponent(key)}.stream.1024.ndjson`; + + it("keeps a key that fits unchanged so its saved session still resumes", () => { + const key = buildSessionKey( + { companyId: COMPANY_ID, agentId: AGENT_ID, taskKey: "__heartbeat__" }, + FINGERPRINT, + ); + expect(key).toBe(`paperclip:${COMPANY_ID}:${AGENT_ID}:__heartbeat__:${FINGERPRINT}`); + expect(Buffer.byteLength(longestFileName(key))).toBeLessThanOrEqual(143); + }); + + it("digests a key whose file names would exceed the eCryptfs NAME_MAX", () => { + const issueTaskKey = "9b1f1c52-3f43-4f7a-9d55-0a6a1c2b7e10"; + const summarySlotTaskKey = `summary-slot:${COMPANY_ID}:execution_workspace:${issueTaskKey}:header`; + for (const taskKey of [issueTaskKey, summarySlotTaskKey]) { + const key = buildSessionKey({ companyId: COMPANY_ID, agentId: AGENT_ID, taskKey }, FINGERPRINT); + expect(key).toMatch(/^paperclip:sha256:[0-9a-f]{32}$/); + expect(encodeURIComponent(key).length).toBeLessThanOrEqual(SESSION_KEY_MAX_ENCODED_LENGTH); + expect(Buffer.byteLength(longestFileName(key))).toBeLessThanOrEqual(143); + } + }); + + it("gives a digested key the same scoping as the full key", () => { + const identity = { companyId: COMPANY_ID, agentId: AGENT_ID, taskKey: "9b1f1c52-3f43-4f7a-9d55-0a6a1c2b7e10" }; + const key = buildSessionKey(identity, FINGERPRINT); + expect(buildSessionKey({ ...identity }, FINGERPRINT)).toBe(key); + expect(buildSessionKey({ ...identity, taskKey: "0d6c7e4a-6b0e-4f0b-8a51-2f7d5c9e3a11" }, FINGERPRINT)).not.toBe(key); + expect(buildSessionKey({ ...identity, agentId: "5f0e8a2b-7c1d-4e3f-9a6b-8d2c4e1f0a93" }, FINGERPRINT)).not.toBe(key); + expect(buildSessionKey(identity, "ffffffffffffffff")).not.toBe(key); + }); +}); diff --git a/packages/adapter-utils/src/acpx-engine/execute.test.ts b/packages/adapter-utils/src/acpx-engine/execute.test.ts index b84b06ec7ee..dfc0ae27752 100644 --- a/packages/adapter-utils/src/acpx-engine/execute.test.ts +++ b/packages/adapter-utils/src/acpx-engine/execute.test.ts @@ -2232,6 +2232,33 @@ describe("shared ACPX engine runtime behavior", () => { expect(sameEnvNewWake.result.resultJson?.sessionReset).toBeUndefined(); }); + it("reports a named reset when a session saved under a long key resumes as a digest key", async () => { + const root = await makeTempRoot(); + const config = { agentCommand: "node ./fake-acp.js", stateDir: path.join(root, "state") }; + const taskId = `summary-slot:company-1:execution_workspace:${"a".repeat(36)}:header`; + + const current = await runExecutor(config, { context: { taskId, wakeReason: "issue_assigned" } }); + const savedParams = current.result.sessionParams as Record; + expect(savedParams.sessionKey).toMatch(/^paperclip:sha256:[0-9a-f]{32}$/); + + const previousReleaseParams = { + ...savedParams, + sessionKey: `paperclip:company-1:agent-1:${taskId}:${String(savedParams.configFingerprint)}`, + }; + const upgraded = await runExecutor(config, { + context: { taskId, wakeReason: "comment", wakeCommentId: "c-1" }, + runtime: { sessionParams: previousReleaseParams }, + }); + expect((upgraded.result.sessionParams as Record).sessionKey).toBe(savedParams.sessionKey); + expect(upgraded.result.resultJson?.sessionReset).toMatchObject({ code: "acpx_session_identity_changed" }); + + const resumed = await runExecutor(config, { + context: { taskId, wakeReason: "comment", wakeCommentId: "c-2" }, + runtime: { sessionParams: upgraded.result.sessionParams }, + }); + expect(resumed.result.resultJson?.sessionReset).toBeUndefined(); + }); + it("keeps rotated run scratch paths out of session identity while retaining user temp overrides", async () => { const root = await makeTempRoot(); const config = { agentCommand: "node ./fake-acp.js", stateDir: path.join(root, "state") }; diff --git a/packages/adapter-utils/src/acpx-engine/execute.ts b/packages/adapter-utils/src/acpx-engine/execute.ts index 2a873f1bde9..299783d25a7 100644 --- a/packages/adapter-utils/src/acpx-engine/execute.ts +++ b/packages/adapter-utils/src/acpx-engine/execute.ts @@ -578,12 +578,18 @@ export function buildSessionFingerprint(identity: SessionFingerprintIdentity): s return shortHash(identity); } +const SESSION_FILE_NAME_MAX_BYTES = 143; +const LONGEST_SESSION_FILE_SUFFIX = ".stream.1024.ndjson"; +export const SESSION_KEY_MAX_ENCODED_LENGTH = SESSION_FILE_NAME_MAX_BYTES - LONGEST_SESSION_FILE_SUFFIX.length; + /** * Build the session key from the fingerprint and the outer-key identity. The key * form is `paperclip:companyId:agentId:taskKey:fingerprint`. */ export function buildSessionKey(identity: SessionKeyIdentity, fingerprint: string): string { - return `paperclip:${identity.companyId}:${identity.agentId}:${identity.taskKey}:${fingerprint}`; + const key = `paperclip:${identity.companyId}:${identity.agentId}:${identity.taskKey}:${fingerprint}`; + if (encodeURIComponent(key).length <= SESSION_KEY_MAX_ENCODED_LENGTH) return key; + return `paperclip:sha256:${createHash("sha256").update(key).digest("hex").slice(0, 32)}`; } // ACPX runs inside the long-lived Paperclip server process. A local child needs @@ -6087,7 +6093,8 @@ export function createAcpxEngineExecutor(deps: AcpxEngineExecutorOptions = {}) { const result = await runAttempt(plan); const previousSession = parseObject(ctx.runtime.sessionParams); if (asString(previousSession.acpSessionId, "") && result.sessionParams?.configFingerprint - && previousSession.configFingerprint !== result.sessionParams.configFingerprint) { + && (previousSession.configFingerprint !== result.sessionParams.configFingerprint + || previousSession.sessionKey !== result.sessionParams.sessionKey)) { return { ...result, resultJson: { ...result.resultJson, sessionReset: { code: "acpx_session_identity_changed", reason: "The agent configuration or execution identity changed." }, } };