mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-25 16:14:26 +01:00
fix(heartbeat): a provider usage-limit hold applies only to the account that reported it (#22800)
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - Agents run on an AI connection: a connected provider account (a
connection grant), or the host's own account
> - When a provider reports an exhausted allowance, Paperclip holds new
work for that agent until the reported reset (`provider_limit_wait`),
and schedules one recheck at the reset
> - The hold was keyed by agent only. After a user connected a
different, working account and the agent moved to it, every new run was
still cancelled with the old account's reset time. A reply on the task
did not recheck the wait. Only a manual "Check now" got work moving
> - A usage limit belongs to the account that reported it. When that
account is gone or replaced, the hold must be gone too, and waiting work
must start without manual repair
> - The banner's "Check now" button also failed here. It answered 409
"Issue has no scheduled monitor", because the button calls the
issue-monitor route and this task has a scheduled retry, not a monitor
> - This pull request scopes the hold to the credential, releases
waiting work when the account changes or is revoked, makes a reply
recheck the wait, and makes "Check now" recheck a waiting retry
> - The benefit is that fixing the account fixes the task. The same
account stays held, so an exhausted account is not called again
## Linked Issues or Issue Description
No public issue exists. Bug report:
**What happened?**
An agent's connected provider account ran out of quota, with a reset
about six days away. Paperclip held the agent's work ("Pool exhausted",
runs cancelled with `provider_limit_wait`, one recheck scheduled 24
hours later). The user then connected a different, paid account. The old
account became revoked, and the agent was moved to the new account. The
agent's connection test passed. But every new run was still cancelled at
once with the old account's reset time. A new comment on the task did
not start a run, although the composer said "Sending a reply wakes the
agent now". The task stayed in `todo`. Pressing "Check now" on the "Pool
exhausted" banner did nothing: `POST /issues/:id/monitor/check-now`
answered 409, because the task has no issue monitor
(`triggerIssueMonitor` throws "Issue has no scheduled monitor").
**Expected behavior**
A limit reported on account A must not hold a run that uses account B.
When account A is revoked, removed or replaced on the agent, the hold
stops. Work that waited on it starts promptly. While the agent still
uses account A, the hold stays, with the plain message. A reply does
what the composer says.
**Steps to reproduce**
1. Bind an agent to connected account A.
2. Let a run fail with `quota_exhausted` and a reported reset days away.
The next wake is cancelled with `provider_limit_wait`, and a recheck is
scheduled about 24 hours later.
3. Connect account B and move the agent to it, or revoke account A.
4. Wake the agent on the task, or reply on the task. Before this change,
the run is cancelled with `provider_limit_wait` again, and the reply
does not recheck.
## What Changed
- `server/src/services/native-runtime/native-startup-retry.ts`: limit
evidence now comes only from runs on the checked credential. A run
records its grant in `contextSnapshot.aiConnection.grantId`. A run with
no recorded grant used the host's own account. Evidence from a revoked,
expired or removed grant never applies. A later success on another
account no longer clears this account's limit. New helpers resolve the
credential of an agent's next run from its saved binding
(`shared`/`delegated`: the bound grant; `responsible_user`: that user's
default grants for the provider).
- `server/src/services/heartbeat/provider-limit-wait.ts`: the
pre-dispatch hold checks the agent's current credential, and records the
limited grant on `providerLimitWait.grantId`. New
`releaseProviderLimitWaits` makes waiting usage-limit retries due now,
notifies retry dispatch in the same transaction, and logs
`issue.scheduled_retry_retry_now` activity with `source:
provider_limit_recheck`. The recheck goes through normal dispatch. It
reads saved evidence only, so a still-limited account is held again with
no provider work.
- `server/src/services/agent-configuration.ts`: a change to an agent's
`runtimeConfig.aiConnection` releases that agent's waits, in the same
transaction as the change.
- `server/src/services/tool-access/connections/grants.ts` and
`removal.ts`: revoking or removing an account releases the waits whose
limit was reported on it (after commit, best effort; the evidence rule
alone already stops the hold).
- `server/src/services/heartbeat/queue.ts`: a user's wake that is folded
into a usage wait (comment or other user wake) rechecks it at once.
Agent and system wakes keep the scheduled time, so they cannot loop.
- `server/src/routes/issues.ts`: `POST /issues/:id/monitor/check-now` on
a task with no monitor and a waiting scheduled retry now starts that
retry (the same path as "retry now"). The retry rechecks availability
before any work. It logs `issue.scheduled_retry_retry_now` with `source:
monitor_check_now`. If the check cannot start, the 409 has a plain
reason. Tasks with a monitor keep the old behavior.
- `ui/src/components/IssueMonitorBanner.tsx`: for a usage wait, the
composer now says "Sending a reply checks usage again now. The agent
starts as soon as usage is available." The banner says work also resumes
when the agent uses a different account.
- Upgrade safety: holds are derived from saved run evidence, so old
holds resolve by the new rule after deploy. A wait saved before this
change has no `grantId`; the release finds its account from the run that
reported the limit (`providerLimitWait.sourceRunId`).
- `queue.ts` was already far above 1,000 lines. This change adds one
small helper beside the two coalescing paths that need it, rather than a
new module for one call.
## Verification
- Red first: the new tests ran against the `origin/master` sources and 6
of them failed (hold on account A blocked a run on account B; revoked
account still held; rebind and reply did not release the 24-hour retry;
old-shape release missing). The shared-account test passed on master
too, as intended: it guards that the hold stays.
- Check now: a route test covers a `todo` task with no monitor and a
waiting retry (200, the retry starts, the monitor path is not called,
activity is logged) and a refused check (409 with a plain reason). An
integration test covers the exact saved state: `todo` task, cancelled
`provider_limit_wait` run, `scheduled_retry` successor, agent already
moved to another account. "Check now" starts the retry, and it is not
held again.
- Green: `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/heartbeat-provider-limit-wait.test.ts
src/services/native-runtime/native-provider-limit-cooldown.integration.test.ts
src/services/native-runtime/native-startup-retry.integration.test.ts`
passes (58 tests), and
`src/__tests__/issue-execution-policy-routes.test.ts`.
- UI: `ui/src/components/IssueMonitorBanner.test.tsx` passes (2 tests).
- `npx tsc --noEmit -p server` passes.
- `pnpm verify` exited 2 ("broader verification required"; its graph
worker was killed under machine load). I ran the focused checks above
and rely on required CI for broad coverage.
- New tests cover: same account still held with the plain message; agent
moved to another connected account starts; revoked account stops
holding; rebind releases the pending retry and logs activity; a wait
saved in the old shape is released when its account is revoked, and not
when another account is revoked; two agents on one account both stay
held when one agent moves away; a user reply rechecks at once and holds
again (keeping the reply) while the account is still limited, and an
agent's comment does not.
- Not run live: I did not reproduce this on a running instance. I read
the evidence from a local instance where the bug happened, and I did not
change that instance. Live confirmation of the release after deploy is
still to do.
## Risks
- Behavior change: a limit no longer carries over to a different
account. If two accounts share one provider-side quota, the agent can
make one call on the new account before that account gets its own hold.
- A `responsible_user` agent whose user has no saved default cannot
resolve its account before the run. In that case only the revoked-grant
rule applies, as before.
- A user reply on a still-limited account makes one cancelled recheck
run and a new scheduled wait. This is cheap (no workspace or provider
work) and is bounded by the number of user replies.
- No schema change and no new background worker.
## Model Used
- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, agentic coding with
tool use and code execution in Claude Code.
## 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
- [ ] 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>
This commit is contained in:
1 parent
4401962e69
commit
137bd29531
18 files changed
+911
-30
No files matched your search
@@ -224,6 +224,22 @@ Saves a Retry request for the task's latest stopped run. The same `idempotencyKe
|
||||
|
||||
Every answer includes `presentation`, the task's current interruption state.
|
||||
|
||||
## Check Now
|
||||
|
||||
```
|
||||
POST /api/issues/{issueId}/monitor/check-now
|
||||
```
|
||||
|
||||
Starts the task's next check now. This is the "Check now" button on the task banner.
|
||||
|
||||
- When the task has a scheduled monitor, the monitor check starts. The answer is `200` with `{ "ok": true }`.
|
||||
- When the task has no monitor but its run waits in a scheduled retry (for example, a wait for provider usage), the retry starts now. Before any work, the retry checks again whether the agent's current AI account has usage. If that account is still limited, the run waits again with a plain message. The caller must be a board user who can read the task and has `issue:mutate` access to it, the same access as Retry now. The answer is `200` with `{ "ok": true, "outcome": "promoted" | "already_promoted" | "waiting", "message": "..." }`.
|
||||
|
||||
Refusals:
|
||||
|
||||
- `403` or `404`: the caller cannot start work on this task. A private task that the caller cannot see answers `404`.
|
||||
- `409` with `details.code: "issue_monitor_check_deferred"`: the check did not start. `error` gives the reason in plain words, for example that nothing waits to be checked anymore or that the task changed. Refresh the task.
|
||||
|
||||
## Comments
|
||||
|
||||
### List Comments
|
||||
|
||||
Reference in new issue
Block a user