Files
PaperClipAI/server
Nicky LeachandClaude Opus 5.5 db73178bcf fix(heartbeat): keep saved sessions across config edits the provider thread cannot see (#21862)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - A heartbeat run resumes the agent's saved task session, which holds
the provider thread and its history.
> - Before resume, Paperclip compares a fingerprint of the effective run
configuration with the fingerprint that the session saved. A difference
discards the thread.
> - The fingerprint hashed the agent config revision row (id, changed
keys, time). Every saved agent edit therefore discarded the thread, also
an edit that set a value back to what it was.
> - The fingerprint also hashed settings that never reach the thread or
the native execution input, for example turn timeouts, wake scheduling,
and the environment row's update time.
> - This pull request removes those inputs from the fingerprint,
classifies every input with its reason, and adds backstop tests so that
a new input cannot join or leave the fingerprint without a decision.
> - The benefit is that agents keep their thread history across edits
that cannot change what the thread saw or may do, while real changes
still start a new thread.

## Linked Issues or Issue Description

Related: #9977 (semantic task-session fingerprints, open) and #8797
(added the effective run config fingerprint).

**What happened?**
An operator changed an agent's warm idle window from 60000 to unset, and
then back to 60000, between two turns on the same issue. The next run
logged `Skipping saved session resume for task "<issue>" because
effective run configuration changed: adapter.` Only the `adapter`
category changed, because the new config revision row was in it. The
adapter config itself was identical. The run started a new provider
thread. The environment lease was reused, so the run stopped the
retained remote task before it started a new one. That stop raced the
task's exit, and the run failed with `Plugin Runner termination is
unconfirmed; reconcile the saved task`. An automatic retry then
succeeded on a fresh environment.

**Expected behavior**
An edit that cannot change what the provider thread saw or may do, and
cannot change the native execution input, keeps the saved session. An
edit to the model, provider, active permission mode, lifecycle mode or
warm idle window, instructions, skills, environment, secrets, or another
thread input still starts a new session.

**Steps to reproduce**
1. Run an agent turn on an issue so that the task session saves its
fingerprint.
2. Save an agent config edit that does not change a thread input, for
example set `adapterConfig.idleTimeoutMs` to another value and back, or
rename the agent.
3. Wake the agent on the same issue. On master the run log shows
"effective run configuration changed: adapter" and the run starts a new
thread.

## What Changed

- New `server/src/services/heartbeat/session-fingerprint-inputs.ts`. It
classifies each fingerprint input as kept or excluded, with the reason:
  - agent config revision fields (`CONFIG_REVISION_FIELDS`);
  - `paperclip_runner` adapter config keys;
  - top-level environment driver config keys;
  - agent runtime config keys;
  - selected-environment and environment-selection fields.
- The session fingerprint no longer hashes these inputs:
  - the config revision row (id, changed keys, time);
- for `paperclip_runner`: `timeoutSec`, `graceSec`, the permission keys
of providers that are not selected, and `search` when the provider is
not Codex;
- `runtimeConfig.heartbeat` (wake scheduling, concurrency, daily caps,
max-turn continuation);
- the selected environment's `configRevisionAt` (row update time) and
the `selectionSource`;
- environment config `archiveOnRelease`, `streamRunLogs`, and
`timeoutMs` (sandbox RPC timeout).
- Unknown adapter and runtime config keys are still hashed. A missing
classification can only keep the old reset behavior; it cannot skip a
reset.
- Lifecycle inputs stay in the fingerprint: `lifecycleMode`,
`idleTimeoutMs`, and the environment's `runnerLifecycleMode`,
`runnerIdleTimeoutMs`, and `reuseLease`. They become
`session.lifecyclePolicy`, which the native session digest compares
before a follow-up turn attaches to its warm Runner. A kept session with
a different policy would be refused
(`warm_recovery_refused=execution_mismatch`).
- Session preparation accepts a saved fingerprint built with the
previous composition (`legacyComposition`) for the same inputs. The
upgrade therefore does not discard every saved session once. The run
then saves the new fingerprint.
- Backstop tests in `session-fingerprint-inputs.test.ts`:
- every agent revision field, runner config schema field, provider
permission key, and environment driver config key has a classification;
- the shape of each hashed category is pinned, so a new field cannot
reach the fingerprint without a decision;
- every runner key classified as excluded leaves the native provider
input and the lifecycle policy unchanged (this test fails if
`idleTimeoutMs` is reclassified as excluded);
- behavior tests for each exclusion and for the thread and lifecycle
inputs that must still reset;
- a pinned fingerprint proves that the legacy composition reproduces the
master fingerprint exactly.
- `ai-connection-session.test.ts`: two tests used a rename or a
`heartbeat` edit as the "unrelated change". They now use a model change
or an unclassified runtime setting, and two new tests pin that a rename
and a scheduling change keep the session.
- `doc/heartbeat-internals.md` (Config Freshness) describes the rule and
the backstop. The PR adds no code comments, as `CONTRIBUTING.md`
requires; the reasons live in the classification tables and the doc.

## Verification

- Red/green: on master, the exclusion cases, the category-shape pin, and
the legacy acceptance case fail. With the change, all pass.
- Backstop check: with `idleTimeoutMs` temporarily reclassified as
excluded, the native-input test and the idle-window reset test fail.
- Legacy bridge: I computed the fingerprint for one runner fixture and
one `codex_local` fixture with master's `workspaces.ts` and with
`legacyComposition: true` on this branch. The output files were
byte-identical, category fingerprints included. The pinned test uses
those values.
- `cd server && npx vitest run
src/services/heartbeat/session-fingerprint-inputs.test.ts
src/__tests__/ai-connection-session.test.ts
src/__tests__/heartbeat-workspace-session.test.ts
src/__tests__/heartbeat-execution-configuration.test.ts
src/__tests__/ai-connection-router.test.ts
src/__tests__/native-session-resumption.test.ts
src/__tests__/environment-service.test.ts`: 7 files, 397 tests, all
pass.
- `npx tsc --noEmit -p server`: no errors.
- `pnpm verify` exited 2 (broader coverage needed), so the focused
checks above and required CI give the coverage.

## Risks

- Behavior change: an agent rename, role, title, icon, appearance,
reports-to, capabilities, budget, or metadata edit no longer starts a
new thread. Agent tools read these fields live at call time
(`runner-semantic-authority.ts`, `paperclip-runner-tool-authority.ts`),
so the thread never holds a frozen copy.
- Behavior change: wake-scheduling and turn-timeout edits no longer
start a new thread. Neither value is part of the native execution input,
so warm Runner attachment is unaffected.
- A warm idle window edit still starts a new thread. Setting it and
setting it back between turns no longer does, because the revision row
is not hashed.
- The legacy bridge computes extra candidate fingerprints only when the
saved fingerprint differs. It accepts a legacy fingerprint only for the
same current inputs; it never widens what a reset protects.
- During the transition, a real change on a session that still holds a
legacy fingerprint can list extra categories in the reset reason. The
reset decision is correct.
- No schema or API change. The fingerprint version stays 1.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Anthropic Claude Opus 5.5 (`claude-opus-5-5`), used in Claude Code
with tool use (shell, file edit) and extended thinking.

## 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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 08:24:47 -08:00
..