Files
PaperClipAI/server
DottaandClaude Fable 5.2 e3faf579a1 test(recovery): drain background runs before the fresh-continuation test cleanup (#21375)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The server continues an interrupted task in a new run from saved
work
> - `fresh-continuation-fallback.test.ts` tests that behavior against a
real PostgreSQL database
> - Some tests start runs that continue in the background after the test
body returns
> - The cleanup truncated its tables while those runs still held locks,
and PostgreSQL sometimes reported a deadlock
> - The failure appeared on unrelated pull requests in server shard 9/12
> - This pull request makes the cleanup wait for in-flight runs and
retry the truncate on a deadlock
> - The benefit is that this file no longer fails CI at random

## Linked Issues or Issue Description

**What happened?**

The `afterEach` in
`server/src/services/fresh-continuation-fallback.test.ts` sometimes
fails with `PostgresError: deadlock detected` (code 40P01). The truncate
waits for an `AccessExclusiveLock`. A background run that a test started
holds an `AccessShareLock` and waits for another table. No assertion
fails. The test that receives the failure changes from run to run.

**Expected behavior**

The cleanup completes after each test. The file passes on each run.

**Steps to reproduce**

1. Check out master at 18fb6ee571.
2. Run `npx vitest run src/services/fresh-continuation-fallback.test.ts`
in `server/` on a loaded machine, several times.
3. One test fails in `afterEach` with the deadlock error in some runs.

**Relevant logs or output**

`truncate table issue_recovery_actions, ... heartbeat_runs, issues
cascade` → `PostgresError: deadlock detected (40P01)`

Refs #21348, #21135.

## What Changed

- The `afterEach` awaits
`heartbeatService(db).drainActiveRunExecutions()` before the truncate.
The registry of in-flight runs is shared by all service instances.
- The `afterEach` truncates through the existing
`truncateTablesWithDeadlockRetry` helper.
- No product code changed.

## Verification

- `npx vitest run src/services/fresh-continuation-fallback.test.ts` in
`server/`: 72 of 72 passed, two times in sequence.
- `npx tsc --noEmit -p .` in `server/`: no errors.
- The deadlock is not deterministic. Two passing runs do not prove that
it cannot occur. The retry helper covers a late writer that the drain
cannot see.

## Risks

Low risk. The change is in one test file. The drain adds a short wait to
each test cleanup.

## Model Used

Claude Fable 5.2 (`claude-fable-5-2`), in Claude Code, with tool use and
code execution.

## 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
- [ ] All Paperclip CI gates are green
- [ ] 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 Fable 5.2 <noreply@anthropic.com>
2026-11-13 06:46:18 -06:00
..