Files
PaperClipAI/server
Nicky Leach 90d985b8b9 perf(heartbeat): skip unused session and holder reads for queued input (#22282)
## Thinking Path

> - Paperclip saves comments on a task while an agent is busy.
> - A second comment can join an existing queued run.
> - Only a new run records the starting session, yet admission read it
before choosing a run.
> - Admission can also bind an existing run and then use the previous
holder's agent name.
> - Read the session at insertion. Use the selected holder's key to
decide whether the existing run can accept the comment.

## Linked Issues or Issue Description

**What existing behavior does this improve?**

Admitting board assignments and saved task comments.

**Subsystem affected**

Server task admission.

**Current behavior**

Adding a comment to queued work reads a task session it does not use. A
replaced execution holder can leave its old key in this admission
decision.

**Proposed behavior**

Read the default session in the transaction that creates a run. Use the
selected run's execution-agent key when evaluating queued input.

**Additional context**

Builds on #22268, #22279 and #22280.

## What Changed

- Keep explicit resume lookup early because it determines task scope.
- Use the stored execution key if present. Scope the fallback agent
lookup to the company.
- When binding an existing run, use the same normalized key for the
issue write and the coalescing decision.

## Verification

All 280 tests passed across the PostgreSQL wake-queue adapter, heartbeat
queue, heartbeat run-state and responsible-user suites at a4d75b2d3e.
Three explicit-continuation tests and server typecheck also passed. The
benchmark retained one run, two receipts and both input IDs. Warm median
statements for two comments fell from 76 to 74. Single assignment and
single comment stayed at 36 and 37. The missing and stale
execution-holder regressions cover both process and Runner.

Across the stack, a separate local PostgreSQL run compared f0caee6f95
and a4d75b2d3e in base/tip/tip/base order. Each revision had 34 warm
measurements per case. Median admission times fell from 41.8 to 25.1 ms
for assignment, 42.6 to 25.5 ms for one comment, and 70.7 to 49.7 ms for
two comments. The agent slot was occupied. Counts exclude HTTP task and
comment creation and run execution.

## Risks

The default session is read later, inside the admission transaction. A
fresh run can now observe session changes committed before that read.
Explicit resume and reconstruction keep their identity rules. When
replacing a stale holder, a matching queued run now receives the comment
instead of deferring it under the previous holder's key.

## Model Used

OpenAI GPT-6.2 Sol (gpt-6.2-sol) implemented the change. OpenAI Codex
reviewed and qualified it.

## 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
2026-11-18 20:05:16 -08:00
..