Files
PaperClipAI/server
DottaandPaperclip 848e659bc6 refactor(heartbeat): extract session preparation (#15966)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Heartbeat execution prepares a provider session before dispatch.
> - The same executor also owns workspace setup, admission, cleanup, and
recovery.
> - Session freshness and compaction form a separate responsibility with
explicit inputs and results.
> - This pull request moves those two session phases into one module and
keeps their order around workspace preparation.
> - The benefit is a shorter executor with unchanged session behavior.

## Linked Issues or Issue Description

**What existing behavior does this improve?**
Maintenance of heartbeat execution. This continues the extraction series
after #15872.

**Current behavior**
`heartbeat.ts` has 4,989 lines and 206,650 bytes. Session config
fingerprints, resume selection, credential compatibility, and compaction
are embedded in the executor.

**Proposed behavior**
The executor calls `prepareHeartbeatSession` before workspace
preparation and `resolveHeartbeatAdapterSession` after it.
`heartbeat.ts` has 4,740 lines and 195,258 bytes. The new cohesive
module has 385 lines and 19,518 bytes.

**Reason and benefit**
A reviewer can find session policy in one module. Runtime acquisition,
admission gates, and cleanup remain in the executor. Related open work
includes #15787 and #15142; this PR does not change either policy.

**Breaking changes**
None intended. Existing public helper exports remain available.

## What Changed

- Move 267 lines into
`server/src/services/heartbeat/session-preparation.ts` with typed task,
config, workspace, and session inputs.
- Keep config fingerprints and context mutations on the same objects.
Return compaction and adapter-session results to existing consumers.
- Add four real-PostgreSQL characterization tests through heartbeat
wakeup and adapter dispatch.
- Document the two session preparation phases.

## Verification

- Before extraction: four new characterization tests pass; 180 existing
workspace/session tests pass.
- After extraction: 205 tests pass across session preparation,
workspace/session policy, managed-account session compatibility, and
accepted-plan refresh. No skips.
- `pnpm -r typecheck` and `pnpm build` pass.
- Normalized syntax comparison: both moved blocks and the reconstructed
executor, including public exports, match the base.
- Isolated browser acceptance: 2/2 pass. A real process worker completes
after automatic recovery; persistent timeouts stop after three attempts.
Both terminal states survive reload.
- All hosted CI gates pass on `d33f8506a7`, including the complete test
shards, typecheck, build, and browser suites. The duplicate local `pnpm
test:run` was interrupted after hosted CI passed; it is not claimed as a
full local pass.
- Greptile: 5/5 on this head, with no review threads.

## Risks

- Session freshness can alter provider context if a dependency is
omitted. Typed inputs and passing-before/passing-after dispatch tests
cover unchanged resumes, model changes, tool refresh, and compaction.
- The module acquires no runtime resource. Workspace preparation still
occurs between the two phases.
- Live external providers are not exercised; browser acceptance uses a
real process fixture in a disposable app.

## Model Used

OpenAI Codex, based on GPT-6, assisted with reasoning, code edits, tool
execution, and verification. The exact deployment model ID and
context-window size are not exposed in this session.

## 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: Paperclip <noreply@paperclip.ing>
2026-10-12 09:40:09 -05:00
..