Files
PaperClipAI/packages
Pavel StaykovandClaude Opus 5.5 e35d1819fc 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 `.<pid>.<ms>.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 '<home>/companies/<company>/acp-engine/agents/<agent>/sessions/paperclip%3A<company>%3A<agent>%3A__heartbeat__%3A<16-hex>.json.<pid>.<ms>.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, `<key>.stream.1024.ndjson` | 141 |
| UUID task key, `<key>.json` | 150 |
| UUID task key, `<key>.stream.1024.ndjson` | 164 |
| Summary-slot task key, `<key>.json` | 235 |
| Summary-slot task key, `<key>.stream.1024.ndjson` | 249 |

## What Changed

- `buildSessionKey` measures the URI-encoded key. If the longest session
file name (`<key>.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 <noreply@anthropic.com>
2026-12-05 04:22:02 -08:00
..