Files
PaperClipAI/server
Dotta 4434d12e2f fix(setup-token): back off cleanup retries after failed sweeps (#19446)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - A setup-token login keeps a durable cleanup record. The setup-token
reaper releases its provider lease and removes the record
> - A sweep that fails keeps the record and retries on the next
30-second tick, for as long as the failure lasts
> - During a provider outage, that is a steady stream of provider calls
and log lines with no recovery benefit
> - This pull request backs off consecutive failed sweeps from 30
seconds to 30 minutes and resets after a successful sweep
> - The benefit is less load and log noise during outages, with the same
recovery once the provider is back

## Linked Issues or Issue Description

Refs #14777 (fixed by #15200 and #22667; this pull request adds the
retry backoff that the report also asked about).

This pull request was rebased onto `master` and reduced to the backoff.
#15200 already makes a blank lease lookup return null, and #22667
already logs the failure class and code and covers a restart with a
PostgreSQL test. The earlier redacted-message logging is removed,
because the reaper now logs no error message.

**What happened?**

When a setup-token cleanup sweep fails, for example because the sandbox
provider is down, the cleanup worker retries every 30 seconds until the
failure ends. Each retry calls the provider again and logs again.

**Expected behavior**

Repeated failures back off. A successful sweep returns to the normal
interval.

**Steps to reproduce**

1. Make `releaseById` fail for a reapable setup-token record.
2. Watch the setup-token cleanup worker. It retries the release every 30
seconds without a limit.

## What Changed

- `setup-token-reaper.ts`: count consecutive failed sweeps (a sweep with
a failed record, or a sweep that throws). `retryDelayMs()` returns 30 s,
60 s, 120 s and so on up to 30 min, and 0 after a successful sweep.
- `login-cleanup-work.ts` and `index.ts`: the setup-token cleanup duty
passes `retryDelayMs` to the shared scheduler. The next run waits for
the larger of the configured interval and the backoff delay.
- `setup-token-session.ts`: skip the provider call for a reapable record
with no lease id and remove the record directly.
- `index.ts`: the sweep log line says "cleared records", because a
record without a lease is cleared with no release.
- `setup-token-session.ts`: the failure log line says "a cleanup
failed", because a failure can now come from removing the record as well
as from the provider release.
- `doc/heartbeat-internals.md`: document the retry behavior.
- Tests: backoff to the cap and reset (`setup-token-reaper.test.ts`),
backoff after a thrown sweep, removal of records without a lease, and
the scheduler honoring the backoff across notifications for both a sweep
with failed records and a sweep that throws
(`login-cleanup-work.test.ts`).

## Verification

- `pnpm exec vitest run server/src/services/setup-token-reaper.test.ts
server/src/__tests__/login-cleanup-work.test.ts
server/src/services/setup-token-session.test.ts`: 3 files, 98 tests
passed.
- The seven new tests fail without the source changes.
- `pnpm --filter @paperclipai/server typecheck` passes.

## Risks

- The backoff applies to the whole setup-token cleanup worker. While one
record keeps failing, new cleanup can wait up to 30 minutes. Records
stay in the database, so nothing is lost.
- The failure count lives in memory and resets on restart. The retained
rows rebuild the cleanup work.

## Model Used

- Original change by the pull request author. Rebased and reduced by
Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with shell, file
edit, and code execution tools.

## 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
2026-11-23 13:15:08 -08:00
..