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

@@ -202,7 +202,8 @@ describeEmbeddedPostgres("wake-queue postgres adapter", () => {
const blocker = vi.spyOn(executionBlocker, "getExecutionBlocker");
const admit = createPostgresIssueWakeAdmission(db);
try {
await admit({ companyId, issueId }, async (tx, { fresh }) => {
await admit({ companyId, issueId }, async (tx, { fresh, lockedIssue }) => {
expect(lockedIssue).toMatchObject({ id: issueId, companyId, assigneeAgentId: agentId });
await expect(db.execute(sql`select id from issues where id = ${issueId} for update nowait`))
.rejects.toMatchObject({ cause: { code: "55P03" } });
const input = { issueId, agentId, handoffRunId, checkExecutionBlocker: true };
@@ -218,6 +219,16 @@ describeEmbeddedPostgres("wake-queue postgres adapter", () => {
await expect(fresh.readCommentCandidates({ ...input, receiptScope, checkExecutionBlocker: false }))
.resolves.toMatchObject({ handoffAgentId: formerAgentId, candidates: [{ id: wakeId }] });
expect(blocker).toHaveBeenCalledTimes(1);
const select = vi.spyOn(tx, "select");
try {
expect(await executionBlocker.getExecutionBlocker(tx as unknown as Db, companyId, issueId, {
lockedIssue: lockedIssue!,
})).toMatchObject({ cause: "legacy_execution_requires_reconciliation" });
expect(select).not.toHaveBeenCalledWith(expect.objectContaining({ conversationAgentId: issues.conversationAgentId }));
} finally {
select.mockRestore();
}
return { kind: "skipped" };
});
} finally {
@@ -225,6 +236,21 @@ describeEmbeddedPostgres("wake-queue postgres adapter", () => {
}
});
it("locks an issue added after the initial admission lock missed", async () => {
const companyId = await seedCompany();
const issueId = randomUUID();
const admit = createPostgresIssueWakeAdmission(db);
await admit({ companyId, issueId }, async (_tx, { lockedIssue, lockIssue }) => {
expect(lockedIssue).toBeNull();
await seedIssue({ companyId, issueId });
expect(await lockIssue()).toMatchObject({ id: issueId, companyId });
await expect(db.execute(sql`select id from issues where id = ${issueId} for update nowait`))
.rejects.toMatchObject({ cause: { code: "55P03" } });
return { kind: "skipped" };
});
});
function recordReleaseRunReads() {
const rows: unknown[][] = [];
function recordSelects<T extends Pick<Db, "select">>(executor: T): T {