perf(wake-queue): preflight task admission without loading execution context (#22289)

## Thinking Path

> - Paperclip uses task admission to turn assignments and comments into
queued work.
> - Wake-queue owns the issue-admission transaction. Heartbeat
previously loaded full execution context to decide whether a
conversation wake could proceed.
> - After the conversation preflight, task state can change before
admission obtains its lock.
> - Wake-queue now exposes the preflight decision and obtains only the
fields it needs. It provides current task attribution from the locked
row.
> - Heartbeat uses the existing responsible-user and routine rules when
it creates a run.

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

Conversation preflight and fresh-run attribution load full task
execution context from heartbeat, including chat joins.

**Proposed behavior**

Give wake-queue ownership of conversation preflight and its PostgreSQL
read. Use the issue row already locked by wake-queue for fresh-run
attribution.

**Additional context**

Builds on #22280 and #22282. #17396 covers a separate workspace
inspection on task context reads.

## What Changed

- Give heartbeat a preflight decision. Keep storage and the five-field
query inside wake-queue.
- Reuse the conversation and retry predicates for all callers. Keep the
locked recheck after preflight, including durable chat authorization.
- Supply the task's responsible user, parent and routine origin from the
current locked row. A company-only admission supplies no issue.

## Verification

On `b89bed954a`, all 307 tests passed across the wake-queue preflight
and release application suites, heartbeat queue and run-preparation
suites, and Slack conversation lifecycle suite. The integration suites
used embedded PostgreSQL. Coverage includes the owner 403, a
conversation settling before admission locks the issue, saved-comment
attribution after reassignment, and routine ownership written inside the
admission transaction. The layered server typecheck and module
dependency check passed on the same revision.

In a repeated local comparison of base `90d985b8b9` and PR head
`b89bed954a`, we measured 34 samples per revision, scenario, and
recovery-history state, alternating the two revisions in ABBA order.
Server-side admission medians were:

| Recovery history | Assignment | One comment | Two serial comments |
| --- | ---: | ---: | ---: |
| Empty | 23.45 → 21.80 ms | 23.95 → 23.00 ms | 47.30 → 45.40 ms |
| Resolved | 23.35 → 21.30 ms | 23.70 → 22.05 ms | 46.70 → 45.40 ms |

Typical SQL statements fell from 36/37/74 to 35/36/73 for the three
scenarios. Lock statement counts were 2/2/6 for both revisions.
Throughput varied under local contention. These are local measurements.

Standard Greptile scored the exact head 5/5 with no review threads.
[Greptile
review](https://github.com/paperclipai/paperclip/pull/22289#issuecomment-6999578088)

GitHub CI passed on the exact head after one targeted retry of the
serialized server shard. The initial status-card maintenance case timed
out waiting for idle, passed when run locally on the same revision, and
passed in the retried shard. [CI workflow attempt
2](https://github.com/paperclipai/paperclip/actions/runs/44458613437/attempts/2)

## Risks

An early decision can go stale, and the caller must preserve
authenticated actor identity. The locked path retains its own
eligibility checks. A regression exercises the gap between the two
decisions. An attribution test inserts its evidence inside the
transaction to detect a read through a different connection.

## 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-19 07:23:02 -08:00
1 parent ffcd49f6aa
commit 503268b806
13 files changed
+262 -109

No files matched your search

+2 -32
View File
@@ -32,26 +32,8 @@ import { isAgentAwaitingSetup } from "../modules/agent-lifecycle/index.js";
import { HttpError } from "../errors.js";
import { ONBOARDING_CHAT_GREETING_REASON } from "./onboarding-chat-prompt.js";
export type ConversationIdentity = {
conversationAgentId?: string | null;
conversationUserId?: string | null;
conversationState?: string | null;
status?: string;
};
export function isConversation(
issue: ConversationIdentity | null | undefined,
): boolean {
return Boolean(issue?.conversationAgentId && issue.conversationUserId);
}
export function isWaitingConversation(
issue: ConversationIdentity | null | undefined,
): boolean {
return (
isConversation(issue) &&
issue?.conversationState === "waiting" &&
issue.status === "in_review"
);
}
import { isConversation, isWaitingConversation, type ConversationIdentity } from "../lib/conversation-wake-policy.js";
export { isConversation, isWaitingConversation, isConversationExecutionWake, type ConversationIdentity } from "../lib/conversation-wake-policy.js";
/** Recovery for an older turn must not replace a reset or an answered chat. */
export function isSupersededConversationRun(
@@ -76,18 +58,6 @@ export function isSupersededConversationRun(
* Apply before enqueue, including while a reply is still running: waiting until
* finalization is too late to prevent a deferred dependency follow-up.
*/
export function isConversationExecutionWake(
issue: ConversationIdentity | null | undefined,
reason: string | null | undefined,
): boolean {
return isConversation(issue) && (
reason === "issue_blockers_resolved" ||
reason === "issue_children_completed" ||
reason === "issue_blocker_stalled" ||
reason === "issue_unblock_requested"
);
}
export function isConversationReset(body: string): boolean {
return body.trim() === "/new";
}
+36 -11
View File
@@ -2,7 +2,7 @@ import { randomUUID } from "node:crypto";
import { contextCompactionService } from "../context-compaction.js";
import { claimNativeSessionResumptions } from "../native-runtime/native-finalization-reconciler.js";
import type { Db } from "@paperclipai/db";
import { authUsers, agents, agentTaskSessions, agentWakeupRequests, budgetIncidents, budgetPolicies, caseHandoffs, cases, chatCompletionDeliveries, companies, companyMemberships, costEvents, createDb, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, environmentLeases, heartbeatRuns, issueComments, issueThreadInteractions, issues, nativeRunFinalizations } from "@paperclipai/db";
import { authUsers, agents, agentTaskSessions, agentWakeupRequests, budgetIncidents, budgetPolicies, caseHandoffs, cases, chatCompletionDeliveries, companies, companyMemberships, costEvents, createDb, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, environmentLeases, heartbeatRuns, issueComments, issueThreadInteractions, issues, nativeRunFinalizations, routines, routineRuns } from "@paperclipai/db";
import { eq, inArray, sql } from "drizzle-orm";
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { resolveExternalTestDatabaseUrl } from "../../__tests__/helpers/external-test-database.js";
@@ -184,13 +184,7 @@ describe.skipIf(!support.supported)("heartbeat queue database wiring", () => {
// Admit through the normal transaction, but hold dispatch so no adapter runs.
deps.getSchedulingSuppression.mockResolvedValue({ suppressed: true, reason: "task_drain" });
deps.getSchedulingSuppression.mockResolvedValueOnce({ suppressed: false, reason: null });
const getIssueExecutionContext = deps.getIssueExecutionContext;
const outsideStates: Array<{ status: string; conversationState: string | null }> = [];
deps.getIssueExecutionContext = async (...args) => {
const current = await getIssueExecutionContext(...args);
if (current) outsideStates.push({ status: current.status, conversationState: current.conversationState });
return current;
};
const preflight = deps.wakeQueue.preflightIssueWake;
let reached!: () => void;
let release!: () => void;
const atBarrier = new Promise<void>(resolve => { reached = resolve; });
@@ -213,7 +207,8 @@ describe.skipIf(!support.supported)("heartbeat queue database wiring", () => {
});
try {
await atBarrier;
expect(outsideStates).toEqual([{ status: "in_progress", conversationState: "active" }]);
expect(preflight).toHaveBeenCalledOnce();
await expect(preflight.mock.results[0]?.value).resolves.toMatchObject({ kind: "eligible" });
expect(await settleConversationTurn(db, completed)).toBe(true);
expect((await db.select().from(issues).where(eq(issues.id, issue.id)))[0])
.toMatchObject({ status: "in_review", conversationState: "waiting" });
@@ -452,7 +447,7 @@ describe.skipIf(!support.supported)("heartbeat queue database wiring", () => {
{ actor: "foreign owner", userId: "another-conversation-owner", message: "Only the conversation owner can start a chat run" },
])("refuses waiting-conversation compaction from a $actor", async ({ userId, message }) => {
const f = await waitingCompactionFixture();
await expect(f.service.request({ companyId: f.company.id, issue: f.issue, userId })).rejects.toThrow(message);
await expect(f.service.request({ companyId: f.company.id, issue: f.issue, userId })).rejects.toMatchObject({ status: 403, message });
expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.agentId, f.agent.id))).toHaveLength(0);
expect(await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.agentId, f.agent.id))).toHaveLength(0);
expect(f.deps.executeRun).not.toHaveBeenCalled();
@@ -577,7 +572,7 @@ describe.skipIf(!support.supported)("heartbeat queue database wiring", () => {
}
it.each([true, false])("adopts only eligible saved comments from the exact reassignment source (%s)", async (matchingIssue) => {
const { user, company, agent, issue, deps, queue } = await waitingConversationFixture();
const { user, company, agent, issue, deps } = await waitingConversationFixture();
await db.update(issues).set({ conversationAgentId: null, conversationUserId: null,
conversationState: null, status: "todo" }).where(eq(issues.id, issue.id));
const [previousAgent] = await db.insert(agents).values({ companyId: company.id,
@@ -599,6 +594,8 @@ describe.skipIf(!support.supported)("heartbeat queue database wiring", () => {
payload: withQueuedCommentIdsInWakePayload({ issueId: issue.id, mutation }, [commentId]),
}))).returning();
const loadExecutionContext = vi.spyOn(deps, "getIssueExecutionContext");
const queue = createHeartbeatQueue(db, deps);
const admit = deps.wakeQueue.admitIssueWake.getMockImplementation()!;
deps.wakeQueue.admitIssueWake.mockImplementation((target, evaluate, effects) => admit(target, async (tx, admission) => {
await tx.insert(agentTaskSessions).values({ companyId: company.id, agentId: agent.id,
@@ -610,6 +607,7 @@ describe.skipIf(!support.supported)("heartbeat queue database wiring", () => {
payload: { issueId: issue.id }, contextSnapshot: { issueId: issue.id, interruptedRunId: interruptedRun.id },
});
expect(loadExecutionContext).not.toHaveBeenCalled();
const commentIds = matchingIssue ? [earlier.id, later.id] : [later.id];
expect(run).toMatchObject({ status: "queued", responsibleUserId: user.id, invocationSource: "assignment",
sessionIdBefore: "session-available-in-admission",
@@ -626,6 +624,33 @@ describe.skipIf(!support.supported)("heartbeat queue database wiring", () => {
expect(deps.executeRun).not.toHaveBeenCalled();
});
it("uses the routine run attribution visible in the admission transaction", async () => {
const { company, agent, issue, deps, queue } = await waitingConversationFixture();
const [routine] = await db.insert(routines).values({
companyId: company.id, title: "Routine", assigneeAgentId: agent.id, responsibleUserId: "current-routine-owner",
}).returning();
const [routineRun] = await db.insert(routineRuns).values({
companyId: company.id, routineId: routine.id, source: "schedule", responsibleUserId: "original-routine-owner",
}).returning();
await db.update(issues).set({
conversationAgentId: null, conversationUserId: null, conversationState: null, status: "todo",
originKind: "routine_execution", originId: routine.id,
}).where(eq(issues.id, issue.id));
const admit = deps.wakeQueue.admitIssueWake.getMockImplementation()!;
deps.wakeQueue.admitIssueWake.mockImplementation((target, evaluate, effects) => admit(target, async (tx, admission) => {
await tx.update(issues).set({ originRunId: routineRun.id }).where(eq(issues.id, issue.id));
return evaluate(tx, { ...admission, lockedIssue: await admission.lockIssue() });
}, effects));
const run = await queue.enqueueWakeup(agent.id, {
source: "assignment", triggerDetail: "system", reason: "issue_assigned", requestedByActorType: "system",
payload: { issueId: issue.id },
});
expect(run).toMatchObject({ status: "queued", responsibleUserId: "original-routine-owner" });
expect(deps.executeRun).not.toHaveBeenCalled();
});
it("only promotes a deferred chat receipt for its agent and binds receipts within its company", async () => {
const local = await fixture("queued");
const [foreignCompany] = await db.insert(companies).values({ name: "Other company", issuePrefix: "OC" }).returning();
+49 -57
View File
@@ -57,7 +57,6 @@ import { isVerifiedDependencyInteractionResponse, readQueuedInteractionResponse
import {
isConversation,
isConversationReset,
isConversationExecutionWake,
isWaitingConversation,
} from "../agent-conversations.js";
import { onboardingGreetingNeeded } from "../onboarding-chat.js";
@@ -219,7 +218,7 @@ import type { budgetService } from "../budgets.js";
import { isBoardOwnedBudgetBlock } from "../budget-block-ownership.js";
import type { instanceSettingsService } from "../instance-settings.js";
import type { createRunDispatch } from "../../modules/run-dispatch/index.js";
import { hasRetainedExternalChatRetry, type PostCommitEffect as WakeQueuePostCommitEffect, type createWakeQueue } from "../../modules/wake-queue/index.js";
import { hasRetainedExternalChatRetry, type IssueAttributionSnapshot, type PostCommitEffect as WakeQueuePostCommitEffect, type createWakeQueue } from "../../modules/wake-queue/index.js";
import { crashPoint } from "@paperclipai/shared/crash-points";
import { emitSystemTaskBlockedAfterCommit } from "../product-telemetry.js";
@@ -248,7 +247,7 @@ export interface HeartbeatQueueDependencies extends Pick<HeartbeatRetryDependenc
budgets: Pick<ReturnType<typeof budgetService>, "getInvocationBlock">;
instanceSettings: Pick<ReturnType<typeof instanceSettingsService>, "getExperimental">;
runDispatch: Pick<ReturnType<typeof createRunDispatch>, "cancelStaleQueuedRun">;
wakeQueue: Pick<ReturnType<typeof createWakeQueue>, "admitIssueWake">;
wakeQueue: Pick<ReturnType<typeof createWakeQueue>, "preflightIssueWake" | "admitIssueWake">;
activeRunExecutions: Set<string>;
activeRunExecutionPromises: Set<Promise<void>>;
activeWakeupPromises: Set<Promise<unknown>>;
@@ -2877,19 +2876,19 @@ export function createHeartbeatQueue(db: Db, dependencies: HeartbeatQueueDepende
// a run or deferred request; assignment and review have their own wakes.
if (reason === "issue_comment_mentioned" || enrichedContextSnapshot.wakeReason === "issue_comment_mentioned") return null;
if (issueId) {
const conversation = await getIssueExecutionContext(agent.companyId, issueId);
if (taskRetryActorId && conversation) assertTaskRetryConversationAllowed(conversation, taskRetryActorId);
if (reason === "issue_children_completed" && conversation?.originKind === "onboarding_first_task") enrichedContextSnapshot.onboardingCompletion = true;
if (isConversation(conversation)) {
if (reason === "onboarding_chat_greeting" && (conversation?.originKind !== "onboarding_chat"
|| conversation.conversationUserId !== opts.requestedByActorId)) return null;
if (opts.manualUserWake && conversation!.conversationUserId !== opts.requestedByActorId) {
throw new HttpError(403, "Only the conversation owner can start a chat run");
}
if (isConversationExecutionWake(conversation, reason ?? readNonEmptyString(enrichedContextSnapshot.wakeReason))) return null;
if (agent.id !== conversation!.conversationAgentId) return null;
if (isWaitingConversation(conversation) && !hasIndependentConversationWakeCause()) return null;
}
const preflight = await wakeQueue.preflightIssueWake({
companyId: agent.companyId,
issueId,
agentId: agent.id,
reason,
wakeReason: readNonEmptyString(enrichedContextSnapshot.wakeReason),
requestedByActorId: opts.requestedByActorId,
manualUserWake: opts.manualUserWake,
taskRetryActorId,
hasIndependentConversationWakeCause: hasIndependentConversationWakeCause(),
});
if (preflight.kind === "skipped") return null;
if (preflight.onboardingCompletion) enrichedContextSnapshot.onboardingCompletion = true;
}
if (agent.adapterType === "paperclip_runner") {
const oldConfig = parseObject(agent.adapterConfig);
@@ -3350,48 +3349,41 @@ export function createHeartbeatQueue(db: Db, dependencies: HeartbeatQueueDepende
? (await instanceSettings.getExperimental()).enableIsolatedWorkspaces
: false;
let operatorResponsibleUserId: string | null = opts.manualUserWake ? opts.requestedByActorId! : null;
let queuedResponsibleUserIdPromise: Promise<string> | null = null;
const resolveQueuedResponsibleUserId = () => {
if (operatorResponsibleUserId) return Promise.resolve(operatorResponsibleUserId);
queuedResponsibleUserIdPromise ??= (async () => {
const queuedIssueContext = issueId
? await getIssueExecutionContext(agent.companyId, issueId)
: null;
const queuedRoutineEnvContext = await getRoutineEnvForExecutionIssue(
agent.companyId,
queuedIssueContext,
);
const queuedResponsibleUserId =
await resolveResponsibleUserIdForRunSeed({
const resolveQueuedResponsibleUserId = async (issueContext: IssueAttributionSnapshot | null) => {
if (operatorResponsibleUserId) return operatorResponsibleUserId;
const queuedRoutineEnvContext = await getRoutineEnvForExecutionIssue(
agent.companyId,
issueContext,
);
const queuedResponsibleUserId =
await resolveResponsibleUserIdForRunSeed({
companyId: agent.companyId,
contextSnapshot: enrichedContextSnapshot,
issueContext,
routineEnvContext: queuedRoutineEnvContext,
requestedByActorType: opts.requestedByActorType ?? null,
requestedByActorId: opts.requestedByActorId ?? null,
source,
triggerDetail,
});
if (!queuedResponsibleUserId) {
throw new HttpError(
422,
"Unable to resolve responsible user for heartbeat run dispatch",
{
code: "responsible_user_unresolved",
agentId,
companyId: agent.companyId,
contextSnapshot: enrichedContextSnapshot,
issueContext: queuedIssueContext,
routineEnvContext: queuedRoutineEnvContext,
requestedByActorType: opts.requestedByActorType ?? null,
requestedByActorId: opts.requestedByActorId ?? null,
issueId: issueId ?? null,
source,
triggerDetail,
});
if (!queuedResponsibleUserId) {
throw new HttpError(
422,
"Unable to resolve responsible user for heartbeat run dispatch",
{
code: "responsible_user_unresolved",
agentId,
companyId: agent.companyId,
issueId: issueId ?? null,
source,
triggerDetail,
wakeReason: readNonEmptyString(
enrichedContextSnapshot.wakeReason,
),
},
);
}
return queuedResponsibleUserId;
})();
return queuedResponsibleUserIdPromise;
wakeReason: readNonEmptyString(
enrichedContextSnapshot.wakeReason,
),
},
);
}
return queuedResponsibleUserId;
};
const budgetBlock = await budgets.getInvocationBlock(
@@ -5068,7 +5060,7 @@ export function createHeartbeatQueue(db: Db, dependencies: HeartbeatQueueDepende
continuationAttempt,
reconciledSourceRunId,
reconciledRestoreRetryCount,
resolveResponsibleUserId: resolveQueuedResponsibleUserId,
resolveResponsibleUserId: () => resolveQueuedResponsibleUserId(issue),
async recordContinuationDelivery(runId) {
if (ownsReconstructionContinuation) {
await tx.update(issueRecoveryActions).set({ evidence: sql`${issueRecoveryActions.evidence} || ${JSON.stringify({
@@ -5296,7 +5288,7 @@ export function createHeartbeatQueue(db: Db, dependencies: HeartbeatQueueDepende
invocationSource: source,
triggerDetail,
status: "queued",
responsibleUserId: await resolveQueuedResponsibleUserId(),
responsibleUserId: await resolveQueuedResponsibleUserId(null),
wakeupRequestId: wakeupRequest.id,
contextSnapshot: enrichedContextSnapshot,
sessionIdBefore: await resolveSessionBefore(tx),
+2 -2
View File
@@ -1,3 +1,4 @@
import { canActorRetryConversation } from "../lib/conversation-wake-policy.js";
import { notifyStrandedAgentStatus } from "./live-run-recovery-notifications.js";
import { notifyExternalConversationSettlement } from "./external-conversation-settlement-notifications.js";
import { writeExecutionControl } from "./execution-control-notifications.js";
@@ -46,8 +47,7 @@ export function assertTaskRetryConversationAllowed(
issue: { conversationAgentId?: string | null; conversationUserId?: string | null },
actorId: string,
) {
if (!issue.conversationAgentId) return;
if (issue.conversationUserId !== actorId) throw forbidden("Only the conversation owner can retry a chat run");
if (!canActorRetryConversation(issue, actorId)) throw forbidden("Only the conversation owner can retry a chat run");
}
const taskRetryIntentSchema = z.object({