perf(wake-queue): reuse the task row locked by admission (#22280)

## Thinking Path

> - Paperclip schedules agent work for a task.
> - Admission locks the task before it decides what to run.
> - The lock query returned only the identifier. The policy and recovery
check then read the same task again.
> - Return the fields those decisions need from the lock query.
> - Refresh the task after a callback that can change it. Keep recovery
and workspace authority current.

## 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**

Admission reads the task again while it holds the row lock. A comment
also queries the bound workspace when the locked task has none.

**Proposed behavior**

Use the company-scoped locked row. Check workspace preservation when the
task has a bound workspace.

**Additional context**

Builds on #22272 and #22279. Related work in #21954 changes workspace
binding.

## What Changed

- Return the admission projection from the row lock. Repeat the locking
read after durable authorization or an initial miss.
- Use the task projection only for the same company and task. Keep every
recovery-action, ownership and lease read.
- Cover changes inside durable authorization and a concurrent insert
after the initial lock missed.

## Verification

All 261 tests passed in the wake-queue Postgres adapter,
execution-blocker-read and heartbeat queue suites. On 64ca436676 a local
PostgreSQL benchmark retained every saved input. Warm median statements
for assignment, one comment and two separately posted comments fell from
38/40/82 on its parent to 36/37/76. Server declaration and
colocated-test typechecks and the module-boundary check passed.

## Risks

A stale task can authorize work after the task changes. The tests use a
callback that writes through the transaction, plus a competing
connection that attempts to acquire the issue lock.

## 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
This commit is contained in:
Nicky Leach authored and GitHub committed 2026-11-18 20:05:16 -08:00
1 parent 5506359bbb
commit 3dc6ccd8d8
6 files changed
+128 -46

No files matched your search

@@ -1278,10 +1278,39 @@ export function createWakeAdmissionWriter(rawTx: IssueAdmissionDbTransaction, ad
};
}
const issueAdmissionFields = {
id: issues.id,
companyId: issues.companyId,
identifier: issues.identifier,
conversationBoundaryCommentId: issues.conversationBoundaryCommentId,
conversationAgentId: issues.conversationAgentId,
conversationUserId: issues.conversationUserId,
conversationState: issues.conversationState,
status: issues.status,
statusVersion: issues.statusVersion,
projectId: issues.projectId,
projectWorkspaceId: issues.projectWorkspaceId,
executionWorkspaceId: issues.executionWorkspaceId,
executionWorkspacePreference: issues.executionWorkspacePreference,
executionWorkspaceSettings: issues.executionWorkspaceSettings,
assigneeAgentId: issues.assigneeAgentId,
assigneeUserId: issues.assigneeUserId,
monitorNextCheckAt: issues.monitorNextCheckAt,
monitorWakeRequestedAt: issues.monitorWakeRequestedAt,
checkoutRunId: issues.checkoutRunId,
executionRunId: issues.executionRunId,
executionAgentNameKey: issues.executionAgentNameKey,
createdAt: issues.createdAt,
};
export type IssueAdmissionRow = Pick<IssueRow, keyof typeof issueAdmissionFields>;
export function createPostgresIssueWakeAdmission(db: Db) {
return async function withIssueAdmission(
{ companyId, issueId, durableChatRequest }: IssueAdmissionTarget,
evaluate: (tx: IssueAdmissionDbTransaction, stores: {
lockedIssue: IssueAdmissionRow | null;
lockIssue: () => Promise<IssueAdmissionRow | null>;
reader: WakeAdmissionReader;
writer: WakeAdmissionWriter;
fresh: FreshIssueRunStore<HeartbeatRunRow>;
@@ -1290,8 +1319,15 @@ export function createPostgresIssueWakeAdmission(db: Db) {
try {
return await db.transaction(withDurableChatAdmission(durableChatRequest, async (tx) => {
// Mentions may target another agent, but every admission must share the issue lock.
await tx.execute(sql`select id from issues where id = ${issueId} and company_id = ${companyId} for update`);
const lockIssue = async () => {
const [issue] = await tx.select(issueAdmissionFields).from(issues)
.where(and(eq(issues.id, issueId), eq(issues.companyId, companyId))).for("update");
return issue ?? null;
};
const lockedIssue = await lockIssue();
return evaluate(tx, {
lockedIssue,
lockIssue,
reader: createWakeAdmissionReader(tx, companyId),
writer: createWakeAdmissionWriter(tx, companyId),
fresh: createFreshIssueRunStore(tx, companyId),