Files
PaperClipAI/server/src/services/recovery/blocker-stall-wakeups.ts
DottaandClaude Opus 5.5 de80299d2c perf(recovery): check only blocked tasks whose blockers are done (#21517)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The server runs issue recovery queues in the background. One queue,
`resolved-dependencies`, wakes a blocked task when all of its blockers
are done.
> - The queue reloads every blocked task of a company when the company
gets a recovery hint. Every run dispatch and run state change sends that
hint.
> - Each reloaded task gets a dependency check of about five statements,
also when its blockers are still open and nothing can happen.
> - On a hosted instance with 577 blocked tasks and 5 live runs, this
loop ran all the time: five statements at 7.3 per second each.
> - This pull request loads only the blocked tasks whose every blocker
is done. These are the only tasks the check can wake.
> - The benefit is that the cost of this queue no longer grows with the
number of blocked tasks that wait on open work.

## Linked Issues or Issue Description

No issue exists. This describes the problem with the fields of
`.github/ISSUE_TEMPLATE/enhancement.yml`.

**What existing behavior does this improve?**

The background check that wakes blocked tasks when their blockers are
done (`resolved-dependencies` in
`server/src/services/recovery/work.ts`).

**Current behavior**

A company recovery hint reloads every blocked, agent-assigned task of
the company. Run dispatch and every run state change send this hint, so
with live runs it comes all the time. Each task then gets
`reconcileResolvedDependencyWakeBackstop` with its issue id. That reads
the task, reads the dependency readiness, and then reads the stall wakes
of the task (children, blockers, recovery actions). For a task with open
blockers this finds nothing to do. The queue reloads at most every 30
seconds per hint and works 25 tasks per pass with 1 second between
passes, so with hundreds of blocked tasks it never becomes idle.

**Proposed behavior**

The queue loads only blocked tasks that have at least one blocker and no
blocker that is not done. The per-task check is unchanged, so the
workspace-finalize barrier and the existing-wake dedup still apply. A
blocked task enters the queue when its last blocker becomes done,
because that change sends the company hint and the next reload now finds
it.

Stall wakes for tasks with open blockers come from the company pass of
the `active-recovery-actions` queue (`reconcileActiveRecoveryActions`).
That pass reads every stall of the company with the same predicate, and
it gets the same company hint.

**Reason and benefit**

Background work must not grow with the backlog of open tasks. With 300
blocked tasks behind one open blocker, a company hint now checks 0 of
them instead of 300.

## What Changed

- `server/src/services/recovery/work.ts`: the shared `register` helper
takes an optional extra SQL condition. The `resolved-dependencies` queue
gives a condition that keeps only blocked tasks with at least one
`blocks` relation and only `done` blockers.
- `server/src/__tests__/issue-recovery-deadlines.test.ts`: a new test
with 300 blocked tasks behind an open blocker and 1 blocked task behind
a done blocker. It runs the real coordinator, notification and SQL.

## Verification

- `npx vitest run src/__tests__/issue-recovery-deadlines.test.ts -t
"blockers are done"` (in `server/`). Before the change it fails: the
first pass checks all 301 tasks instead of the 1 ready task. After the
change it passes. The test also shows that the 300 tasks are checked as
soon as their blocker becomes done.
- `npx vitest run src/__tests__/issue-recovery-deadlines.test.ts
src/__tests__/issue-recovery-work-lifecycle.test.ts
src/__tests__/issue-blocker-stall-wakeups.test.ts` pass. The stall test
file covers the stall wakes through the company pass.
- `pnpm check:module-boundaries` passes.
- Local benchmark: the real server against PostgreSQL with statement
logging, one company, 60 agents, about 1,440 open tasks (780 in review,
580 blocked with blocker relations, 50 in progress, 30 to do), with run
churn from the recovery queues. Results are in the table below.

The benchmark table follows in an update of this description.

## Risks

- Low risk. The check itself is unchanged. The change only removes tasks
from the queue that the check skipped as not ready.
- A blocked task whose only blocker is cancelled stays out of the queue.
The check also treated it as not ready, because only a done blocker
resolves a dependent.
- Stall wakes for blocked tasks with open blockers now come only from
the company recovery-actions pass. That pass uses a superset of the
stall predicate.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, 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>
2026-11-13 17:52:03 -06:00

456 lines
27 KiB
TypeScript

/**
* Wake the issues waiting on a stalled one (`issue_blocker_stalled`).
*
* A parent hears about a child only when the child completes
* (`issue_children_completed`), and a dependent hears about a blocker only when
* every blocker is done (`issue_blockers_resolved`). When a child or blocker
* instead stalls on a recovery action that needs the board (for example a run
* whose retries are exhausted), nothing told the waiting agent: restart chaos
* w-20261108T181313-s9701 t05 left a parent silently blocked for 50+ minutes.
*
* A stall is the issue's latest recovery action needing the board, on an
* issue that is not done or cancelled:
* - open (`active` or `escalated`) and board-owned or escalated; or
* - board-owned and resolved with outcome `blocked` while the issue is still
* `blocked`. Recovery resolves its action this way when it gives the issue
* to the board (t05: created and resolved within six seconds), so an open
* action alone misses the common case.
* Each open, agent-assigned waiter (the parent, and every issue the stalled
* one `blocks`) is woken once per stall. The idempotency key names the
* recovery action, so a repeated sweep never wakes twice, and a later stall
* (a new action after a Retry or another failure) wakes again.
*
* A wake that did not reach the agent is retried, boundedly. A refusal
* (wake-on-demand off, agent paused) is retried after ten minutes. A stall whose
* runs failed (the keyed wake's run or a resource-wait retry carrying it) is
* retried with a doubling wait and given up after MAX_FAILED_STALL_DELIVERIES
* failed runs; the stall stays listed on the waiter's page. A waiter whose last
* run failed setup on its bound remote workspace's hold is not woken again while
* reservation would still refuse that hold (remoteWorkspaceHoldRefusesStart):
* its run could only fail the same way. It is woken once the hold settles.
*/
import { and, desc, eq, gt, inArray, isNull, ne, notInArray, or, sql, type Placeholder } from "drizzle-orm";
import { agents, agentWakeupRequests, executionWorkspaces, heartbeatRuns, issueRecoveryActions, issueRelations, issues, type Db } from "@paperclipai/db";
import { ISSUE_BLOCKER_STALLED_WAKE_REASON, readRemoteWorkspaceAuthority, type IssueBlockerStalledWakeContext,
type IssueBlockerStallRelation } from "@paperclipai/shared";
import { logActivity } from "../activity-log.js";
import { parseHeartbeatPolicy } from "../heartbeat/timer-policy.js";
import { authorizationService } from "../authorization.js";
import { preparedStatements } from "../prepared-statements.js";
import { readRemoteWorkspaceWriterHold, REMOTE_WORKSPACE_WRITER_PENDING_MESSAGE,
remoteWorkspaceHoldRefusesStart } from "../remote-workspace-writer-hold.js";
const TERMINAL_ISSUE_STATUSES = ["done", "cancelled"];
/** Waiters that may be woken: not finished, and not parked in the backlog by the board. */
const WAKEABLE_WAITER_STATUSES = ["todo", "in_progress", "in_review", "blocked"];
const MAX_STALLS_PER_PASS = 200;
/** A wake row with one of these statuses was delivered or is still pending. */
const DELIVERED_OR_PENDING_WAKE_STATUSES = ["queued", "deferred_issue_execution", "claimed", "coalesced", "completed"];
/** Waiters of one stall read per page, and scanned per pass before a cursor resumes the rest. */
const MAX_WAITERS_PER_STALL = 100;
const MAX_WAITERS_SCANNED_PER_STALL = 500;
const waiterCursors = new Map<string, string>();
/** Runs that still hold, or already delivered, a stall prompt. A failed setup never did. */
const DELIVERING_RUN_STATUSES = ["queued", "scheduled_retry", "running", "succeeded"];
/** A refused wake (skipped, failed, cancelled) is retried, but not more often than this. */
const REFUSED_WAKE_RETRY_MS = 10 * 60_000;
/** Failed runs of one stall's wake before it is no longer retried; each retry waits twice as long. */
const MAX_FAILED_STALL_DELIVERIES = 3;
/** Re-check for a waiter whose workspace hold refuses a run; settling the hold is what lets it run. */
const HELD_WORKSPACE_RECHECK_MS = 60 * 60_000;
const NON_INVOKABLE_AGENT_STATUSES = ["paused", "terminated", "pending_approval"];
/** Company-wide passes resume after the last stall a capped pass reached, so every stall is visited. */
const companyCursors = new Map<string, string>();
export type BlockerStallWakeup = (agentId: string, input: {
source: "automation";
triggerDetail: "system";
reason: string;
payload: Record<string, unknown>;
idempotencyKey: string;
requestedByActorType: "system";
requestedByActorId: string;
contextSnapshot: Record<string, unknown>;
allowRunCoalescing: false;
issueStateGuard: { statuses: string[]; assigneeAgentId: string };
}) => Promise<unknown>;
export function issueBlockerStalledWakeKey(input: { waiterIssueId: string; stalledIssueId: string; recoveryActionId: string }) {
return [ISSUE_BLOCKER_STALLED_WAKE_REASON, input.waiterIssueId, input.stalledIssueId, input.recoveryActionId].join(":");
}
/**
* Recovery-action rows that keep a company in the recovery-action sweep: open
* actions, and a board action resolved as "blocked" on an issue that is still
* blocked (a stall with no open action left). Without the second case, an
* unassigned stalled blocker with a todo waiter is visited by no sweep.
*/
export function recoveryActionSweepCondition() {
return or(
inArray(issueRecoveryActions.status, ["active", "escalated"]),
and(eq(issueRecoveryActions.status, "resolved"), eq(issueRecoveryActions.outcome, "blocked"),
eq(issueRecoveryActions.ownerType, "board"),
sql`exists (select 1 from ${issues} stalled where stalled.id = ${issueRecoveryActions.sourceIssueId}
and stalled.company_id = ${issueRecoveryActions.companyId} and stalled.status = 'blocked')`),
// A legacy subtask moved to its own workspace whose wake is still owed.
// The pass returns its retry time, which schedules the company again.
and(eq(issueRecoveryActions.status, "resolved"),
sql`${issueRecoveryActions.evidence}#>>'{legacyWorkspaceRehome,wake}' = 'pending'`),
// Likewise a task continued once its workspace's writer let go.
and(eq(issueRecoveryActions.status, "resolved"),
sql`${issueRecoveryActions.evidence}#>>'{releasedWorkspaceWriter,wake}' = 'pending'`),
)!;
}
type StallFilter = { companyId?: string | null; issueId?: string | null; issueIds?: readonly string[]; afterActionId?: string | null };
/** The stalled issues matching `filter` and the recovery action each one waits on. */
async function readStalls(db: Db, filter: StallFilter, limit = MAX_STALLS_PER_PASS) {
if (filter.issueIds && filter.issueIds.length === 0) return [];
return db.select({
actionId: issueRecoveryActions.id, companyId: issueRecoveryActions.companyId,
kind: issueRecoveryActions.kind, cause: issueRecoveryActions.cause, status: issueRecoveryActions.status,
ownerType: issueRecoveryActions.ownerType, nextAction: issueRecoveryActions.nextAction,
issueId: issues.id, identifier: issues.identifier, title: issues.title, issueStatus: issues.status, parentId: issues.parentId,
projectId: issues.projectId, assigneeAgentId: issues.assigneeAgentId, assigneeUserId: issues.assigneeUserId,
}).from(issueRecoveryActions)
.innerJoin(issues, and(eq(issues.id, issueRecoveryActions.sourceIssueId), eq(issues.companyId, issueRecoveryActions.companyId)))
.where(and(
filter.companyId ? eq(issueRecoveryActions.companyId, filter.companyId) : undefined,
filter.issueId ? eq(issueRecoveryActions.sourceIssueId, filter.issueId) : undefined,
filter.issueIds ? inArray(issueRecoveryActions.sourceIssueId, [...filter.issueIds]) : undefined,
filter.afterActionId ? gt(issueRecoveryActions.id, filter.afterActionId) : undefined,
notInArray(issues.status, TERMINAL_ISSUE_STATUSES),
or(
and(inArray(issueRecoveryActions.status, ["active", "escalated"]),
or(eq(issueRecoveryActions.ownerType, "board"), eq(issueRecoveryActions.status, "escalated"))),
and(eq(issueRecoveryActions.status, "resolved"), eq(issueRecoveryActions.outcome, "blocked"),
eq(issueRecoveryActions.ownerType, "board"), eq(issues.status, "blocked")),
),
// Only the issue's latest action describes its current state.
sql`not exists (select 1 from ${issueRecoveryActions} newer
where newer.company_id = ${issueRecoveryActions.companyId}
and newer.source_issue_id = ${issueRecoveryActions.sourceIssueId}
and newer.created_at > ${issueRecoveryActions.createdAt})`,
))
.orderBy(issueRecoveryActions.id)
.limit(limit);
}
type StallRow = Awaited<ReturnType<typeof readStalls>>[number];
type Bound<Value> = Value | Placeholder;
export type StallWaiterPageBinding = {
companyId: Bound<string>;
stalledIssueId: Bound<string>;
parentId: Bound<string | null>;
hasParent: boolean;
onlyIssueId?: Bound<string>;
after?: Bound<string>;
wakeKeySuffix: Bound<string>;
};
export function stallWaiterPageQuery(db: Db, bind: StallWaiterPageBinding) {
return db.select({ id: issues.id, identifier: issues.identifier, assigneeAgentId: issues.assigneeAgentId,
status: issues.status, responsibleUserId: issues.responsibleUserId, executionWorkspaceId: issues.executionWorkspaceId,
relation: sql<"child" | "blocker">`case when ${issues.id} = ${bind.parentId} then 'child' else 'blocker' end` })
.from(issues)
.where(and(
eq(issues.companyId, bind.companyId),
ne(issues.id, bind.stalledIssueId),
inArray(issues.status, WAKEABLE_WAITER_STATUSES),
isNull(issues.hiddenAt),
sql`${issues.assigneeAgentId} is not null`,
bind.onlyIssueId !== undefined ? eq(issues.id, bind.onlyIssueId) : undefined,
bind.after !== undefined ? gt(issues.id, bind.after) : undefined,
sql`not exists (select 1 from ${agentWakeupRequests} w where w.company_id = ${bind.companyId}
and w.idempotency_key = 'issue_blocker_stalled:' || ${issues.id}::text || ${bind.wakeKeySuffix}
and w.status in (${sql.join(DELIVERED_OR_PENDING_WAKE_STATUSES.map(status => sql`${status}`), sql`, `)}))`,
or(
bind.hasParent ? eq(issues.id, bind.parentId as Bound<string>) : sql`false`,
sql`exists (select 1 from ${issueRelations} r where r.company_id = ${bind.companyId}
and r.type = 'blocks' and r.issue_id = ${bind.stalledIssueId} and r.related_issue_id = ${issues.id})`,
),
)).orderBy(issues.id).limit(MAX_WAITERS_PER_STALL);
}
type StallWaiterPageShape = `${"child" | "blocker"}:${"all" | "one"}:${"first" | "after"}`;
const stallWaiterPageStatements = preparedStatements((db, shape: StallWaiterPageShape) => {
const [parent, waiters, page] = shape.split(":");
return stallWaiterPageQuery(db, {
companyId: sql.placeholder("companyId"), stalledIssueId: sql.placeholder("stalledIssueId"),
parentId: sql.placeholder("parentId"), hasParent: parent === "child",
onlyIssueId: waiters === "one" ? sql.placeholder("onlyIssueId") : undefined,
after: page === "after" ? sql.placeholder("after") : undefined,
wakeKeySuffix: sql.placeholder("wakeKeySuffix"),
}).prepare(`issue_blocker_stall_waiter_page_${shape.replace(/:/g, "_")}`);
});
type StallWaiterPageInput = {
stall: Pick<StallRow, "companyId" | "issueId" | "actionId" | "parentId">;
onlyIssueId: string | null;
after: string | null;
};
export function stallWaiterPageStatement(db: Db, input: StallWaiterPageInput) {
const { stall } = input;
return {
statement: stallWaiterPageStatements(db,
`${stall.parentId ? "child" : "blocker"}:${input.onlyIssueId ? "one" : "all"}:${input.after ? "after" : "first"}`),
values: {
companyId: stall.companyId, stalledIssueId: stall.issueId, parentId: stall.parentId ?? null,
wakeKeySuffix: `:${stall.issueId}:${stall.actionId}`,
...(input.onlyIssueId ? { onlyIssueId: input.onlyIssueId } : {}),
...(input.after ? { after: input.after } : {}),
},
};
}
/**
* Whether a run of the waiter's assignee could only fail setup on its bound
* remote workspace's hold. The evidence is the assignee's own latest run on the
* task: it failed setup on that hold, so this agent uses the workspace and
* reservation already tried, and refused, to release the writer. The hold must
* still be one reservation refuses outright (an ended writer past the holder
* cutoff with its save unsettled, or a writer that cannot be named). Another
* assignee, a later run, or a settled hold lets the wake through.
*/
async function waiterWorkspaceRefusesStart(db: Db, companyId: string,
waiter: { id: string; assigneeAgentId: string | null; executionWorkspaceId: string | null }) {
if (!waiter.executionWorkspaceId || !waiter.assigneeAgentId) return false;
const [latest] = await db.select({ status: heartbeatRuns.status, error: heartbeatRuns.error }).from(heartbeatRuns)
.where(and(eq(heartbeatRuns.companyId, companyId), eq(heartbeatRuns.agentId, waiter.assigneeAgentId),
sql`${heartbeatRuns.contextSnapshot}->>'issueId' = ${waiter.id}`))
.orderBy(desc(heartbeatRuns.createdAt)).limit(1);
if (latest?.status !== "failed" || latest.error !== REMOTE_WORKSPACE_WRITER_PENDING_MESSAGE) return false;
const [workspace] = await db.select({ metadata: executionWorkspaces.metadata }).from(executionWorkspaces)
.where(and(eq(executionWorkspaces.companyId, companyId), eq(executionWorkspaces.id, waiter.executionWorkspaceId)));
const authority = readRemoteWorkspaceAuthority(workspace?.metadata);
if (!authority?.writerRunId) return false;
return remoteWorkspaceHoldRefusesStart(await readRemoteWorkspaceWriterHold(db, { companyId, authority }));
}
/** What a waiter's agent may see of a stalled issue it cannot read: that it is stalled, nothing more. */
function redactedStallContext(stall: StallRow, relation: IssueBlockerStallRelation): IssueBlockerStalledWakeContext {
return {
stalledIssueId: stall.issueId, stalledIssueIdentifier: null, stalledIssueTitle: "A task you cannot read",
stalledIssueStatus: stall.issueStatus, relation,
recoveryAction: { id: stall.actionId, kind: "", cause: "", status: stall.status, ownerType: stall.ownerType,
nextAction: "Ask the board to act on it; its details are not available to you." },
};
}
function stallContext(stall: StallRow, relation: IssueBlockerStallRelation): IssueBlockerStalledWakeContext {
return {
stalledIssueId: stall.issueId,
stalledIssueIdentifier: stall.identifier,
stalledIssueTitle: stall.title,
stalledIssueStatus: stall.issueStatus,
relation,
recoveryAction: { id: stall.actionId, kind: stall.kind, cause: stall.cause, status: stall.status,
ownerType: stall.ownerType, nextAction: stall.nextAction },
};
}
/**
* The children and blockers of `issueId` that are stalled on the board, for
* the waiting issue's own page: the board sees them without opening each one.
*/
export async function listStalledDependencies(db: Db, input: { companyId: string; issueId: string }): Promise<IssueBlockerStalledWakeContext[]> {
const children = await db.select({ id: issues.id }).from(issues)
.where(and(eq(issues.companyId, input.companyId), eq(issues.parentId, input.issueId), isNull(issues.hiddenAt)));
const blockers = await db.select({ id: issueRelations.issueId }).from(issueRelations)
.where(and(eq(issueRelations.companyId, input.companyId), eq(issueRelations.type, "blocks"),
eq(issueRelations.relatedIssueId, input.issueId)));
const childIds = new Set(children.map(row => row.id));
const stalls = await readStalls(db, { companyId: input.companyId, issueIds: [...new Set([...childIds, ...blockers.map(row => row.id)])] }, 50);
return stalls.map(stall => stallContext(stall, childIds.has(stall.issueId) ? "child" : "blocker"));
}
export async function wakeWaitersOfStalledIssues(db: Db, enqueueWakeup: BlockerStallWakeup,
opts: { companyId?: string | null; issueId?: string | null; maxStalls?: number } = {}) {
const result = { stalls: 0, woken: 0, alreadyWoken: 0, deferred: 0, failed: 0, retryAt: null as number | null };
const retryAt = (at: number) => { result.retryAt = result.retryAt === null ? at : Math.min(result.retryAt, at); };
const maxStalls = opts.maxStalls ?? MAX_STALLS_PER_PASS;
let stalled: StallRow[];
if (opts.issueId) {
// The per-issue sweep checks the issue itself and its own children and
// blockers, so a stalled dependency that this sweep never visits (for
// example an unassigned blocker) still reaches the waiting issue.
const scope = opts.companyId ? [eq(issues.companyId, opts.companyId)] : [];
const children = await db.select({ id: issues.id }).from(issues).where(and(...scope, eq(issues.parentId, opts.issueId)));
const blockers = await db.select({ id: issueRelations.issueId }).from(issueRelations)
.where(and(eq(issueRelations.type, "blocks"), eq(issueRelations.relatedIssueId, opts.issueId),
opts.companyId ? eq(issueRelations.companyId, opts.companyId) : undefined));
stalled = await readStalls(db, { companyId: opts.companyId,
issueIds: [...new Set([opts.issueId, ...children.map(row => row.id), ...blockers.map(row => row.id)])] }, maxStalls);
} else {
const cursorKey = opts.companyId ?? "*";
stalled = await readStalls(db, { companyId: opts.companyId, afterActionId: companyCursors.get(cursorKey) ?? null }, maxStalls);
if (stalled.length >= maxStalls) {
companyCursors.set(cursorKey, stalled.at(-1)!.actionId);
retryAt(Date.now());
} else companyCursors.delete(cursorKey);
}
const access = authorizationService(db);
const agentCanRead = async (agentId: string, onBehalfOfUserId: string | null, stall: StallRow) => (await access.decide({
actor: { type: "agent", agentId, companyId: stall.companyId, onBehalfOfUserId },
action: "issue:read",
resource: { type: "issue", companyId: stall.companyId, issueId: stall.issueId, projectId: stall.projectId,
parentIssueId: stall.parentId, assigneeAgentId: stall.assigneeAgentId, assigneeUserId: stall.assigneeUserId,
status: stall.issueStatus },
})).allowed;
const agentAcceptsWakes = new Map<string, boolean>();
const acceptsWakes = async (agentId: string) => {
if (!agentAcceptsWakes.has(agentId)) {
const [agent] = await db.select().from(agents).where(eq(agents.id, agentId));
agentAcceptsWakes.set(agentId, Boolean(agent) && !NON_INVOKABLE_AGENT_STATUSES.includes(agent!.status)
&& parseHeartbeatPolicy(agent!).wakeOnDemand);
}
return agentAcceptsWakes.get(agentId)!;
};
for (const stall of stalled) {
result.stalls += 1;
const cursorKey = `${stall.actionId}:${opts.issueId ?? "*"}`;
let after = waiterCursors.get(cursorKey) ?? null;
let scanned = 0;
for (;;) {
const page = stallWaiterPageStatement(db, {
stall, after, onlyIssueId:
// A waiter's own sweep wakes only that waiter for its children and
// blockers; the stalled issue's own pass covers all of its waiters.
opts.issueId && stall.issueId !== opts.issueId ? opts.issueId : null,
});
const waiters = await page.statement.execute(page.values);
for (const waiter of waiters) {
const idempotencyKey = issueBlockerStalledWakeKey({ waiterIssueId: waiter.id, stalledIssueId: stall.issueId, recoveryActionId: stall.actionId });
// (company_id, payload->>'issueId') is indexed; the key narrows to this stall.
// A delivered or pending wake ends it. A refused one (wake-on-demand off,
// agent paused) is retried once the refusal is old, so re-enabling wakes
// still delivers the stall. Failed runs carrying this stall (the keyed
// wake's run, or a resource-wait retry that keeps its context) are
// retried after a doubling wait, and not after MAX_FAILED_STALL_DELIVERIES.
const [prior] = await db.select({
delivered: sql<boolean>`coalesce(bool_or(${inArray(agentWakeupRequests.status, DELIVERED_OR_PENDING_WAKE_STATUSES)}), false)`,
lastRequestedAt: sql<string | null>`max(${agentWakeupRequests.requestedAt})`,
}).from(agentWakeupRequests)
.where(and(eq(agentWakeupRequests.companyId, stall.companyId),
sql`${agentWakeupRequests.payload}->>'issueId' = ${waiter.id}`,
eq(agentWakeupRequests.idempotencyKey, idempotencyKey)));
if (prior?.delivered) { result.alreadyWoken += 1; continue; }
const [failed] = await db.select({
runs: sql<number>`count(*)::int`,
lastAt: sql<string | null>`max(coalesce(${heartbeatRuns.finishedAt}, ${heartbeatRuns.createdAt}))`,
}).from(heartbeatRuns)
.where(and(eq(heartbeatRuns.companyId, stall.companyId), eq(heartbeatRuns.status, "failed"),
sql`${heartbeatRuns.contextSnapshot}->>'issueId' = ${waiter.id}`,
sql`${heartbeatRuns.contextSnapshot}#>>'{blockerStall,stalledIssueId}' = ${stall.issueId}`,
sql`${heartbeatRuns.contextSnapshot}#>>'{blockerStall,recoveryAction,id}' = ${stall.actionId}`));
const failedRuns = Number(failed?.runs ?? 0);
if (failedRuns >= MAX_FAILED_STALL_DELIVERIES) { result.alreadyWoken += 1; continue; }
const lastAttemptAt = Math.max(prior?.lastRequestedAt ? new Date(prior.lastRequestedAt).getTime() : 0,
failed?.lastAt ? new Date(failed.lastAt).getTime() : 0);
if (lastAttemptAt > 0) {
const dueAt = lastAttemptAt + REFUSED_WAKE_RETRY_MS * 2 ** Math.max(0, failedRuns - 1);
if (dueAt > Date.now()) {
result.alreadyWoken += 1;
retryAt(dueAt);
continue;
}
}
// A resource wait cancels the stall wake's run and schedules a retry
// with no idempotency key; the retry keeps the stall in its context.
const [carried] = await db.select({ id: heartbeatRuns.id }).from(heartbeatRuns)
.where(and(eq(heartbeatRuns.companyId, stall.companyId), eq(heartbeatRuns.agentId, waiter.assigneeAgentId!),
inArray(heartbeatRuns.status, DELIVERING_RUN_STATUSES),
sql`${heartbeatRuns.contextSnapshot}->>'issueId' = ${waiter.id}`,
sql`${heartbeatRuns.contextSnapshot}#>>'{blockerStall,stalledIssueId}' = ${stall.issueId}`,
sql`${heartbeatRuns.contextSnapshot}#>>'{blockerStall,recoveryAction,id}' = ${stall.actionId}`)).limit(1);
if (carried) { result.alreadyWoken += 1; continue; }
if (await waiterWorkspaceRefusesStart(db, stall.companyId, waiter)) {
result.deferred += 1;
retryAt(Date.now() + HELD_WORKSPACE_RECHECK_MS);
continue;
}
if (!(await acceptsWakes(waiter.assigneeAgentId!))) {
result.deferred += 1;
retryAt(Date.now() + REFUSED_WAKE_RETRY_MS);
continue;
}
// A private stalled issue's details never reach an agent that cannot read it.
const readable = await agentCanRead(waiter.assigneeAgentId!, waiter.responsibleUserId, stall);
const context = readable ? stallContext(stall, waiter.relation) : redactedStallContext(stall, waiter.relation);
try {
const wake = await enqueueWakeup(waiter.assigneeAgentId!, {
source: "automation", triggerDetail: "system", reason: ISSUE_BLOCKER_STALLED_WAKE_REASON,
payload: { issueId: waiter.id, ...context }, idempotencyKey,
requestedByActorType: "system", requestedByActorId: "issue-blocker-stall",
contextSnapshot: { issueId: waiter.id, taskId: waiter.id, wakeReason: ISSUE_BLOCKER_STALLED_WAKE_REASON,
source: "issue.blocker_stalled", blockerStall: context },
// One stall per run: coalescing would keep only the last blockerStall
// context while marking every merged stall as delivered.
allowRunCoalescing: false,
issueStateGuard: { statuses: WAKEABLE_WAITER_STATUSES, assigneeAgentId: waiter.assigneeAgentId! },
});
// Null is a normal deferral (for example wake-on-demand disabled): no
// wake row exists yet, so a later sweep tries again.
if (!wake) { result.deferred += 1; continue; }
await logActivity(db, {
companyId: stall.companyId, actorType: "system", actorId: "issue-blocker-stall",
action: "issue.blocker_stalled_wake", entityType: "issue", entityId: waiter.id,
agentId: waiter.assigneeAgentId, issueId: waiter.id,
details: { stalledIssueId: stall.issueId, stalledIssueIdentifier: readable ? stall.identifier : null,
relation: waiter.relation, recoveryActionId: stall.actionId,
recoveryActionKind: readable ? stall.kind : null, recoveryActionStatus: stall.status, detailsWithheld: !readable },
});
result.woken += 1;
} catch {
// The next sweep retries: no wake row with this key exists yet.
result.failed += 1;
}
}
scanned += waiters.length;
if (waiters.length < MAX_WAITERS_PER_STALL) { waiterCursors.delete(cursorKey); break; }
after = waiters.at(-1)!.id;
// Skipped waiters (agents taking no wakes) never pin later ones: the next pass resumes here.
if (scanned >= MAX_WAITERS_SCANNED_PER_STALL) { waiterCursors.set(cursorKey, after); retryAt(Date.now()); break; }
}
}
return result;
}
/**
* Whether a wake's `issue_blocker_stalled` context is real, so it may run past
* the waiter's unresolved blockers. The reason string alone is not trusted:
* the wake must come from the system, name a recovery action that is the
* stalled issue's current stall, and that issue must be a child or blocker of
* the waking issue. Anything else falls back to the ordinary dependency gate.
*/
export async function verifyBlockerStallWake(db: Db, input: {
companyId: string;
issueId: string;
contextSnapshot: Record<string, unknown> | null | undefined;
requestedByActorType: string | null | undefined;
}): Promise<boolean> {
const context = input.contextSnapshot;
// A resource wait (AI connection busy, workspace busy) replaces the run with
// a server-created retry that keeps the context but not the wake reason or
// request. It carries the stall that admitted the original run.
const serverRetry = typeof context?.retryOfRunId === "string" && context.retryOfRunId.length > 0;
if (!context || (context.wakeReason !== ISSUE_BLOCKER_STALLED_WAKE_REASON && !serverRetry)) return false;
if (!serverRetry && input.requestedByActorType !== "system") return false;
const stall = context.blockerStall as Partial<IssueBlockerStalledWakeContext> | undefined;
const stalledIssueId = typeof stall?.stalledIssueId === "string" ? stall.stalledIssueId : null;
const actionId = typeof stall?.recoveryAction?.id === "string" ? stall.recoveryAction.id : null;
if (!stalledIssueId || !actionId || stalledIssueId === input.issueId) return false;
const [current] = await readStalls(db, { companyId: input.companyId, issueIds: [stalledIssueId] }, 1);
if (!current || current.actionId !== actionId) return false;
if (current.parentId === input.issueId) return true;
const [edge] = await db.select({ id: issueRelations.id }).from(issueRelations)
.where(and(eq(issueRelations.companyId, input.companyId), eq(issueRelations.type, "blocks"),
eq(issueRelations.issueId, stalledIssueId), eq(issueRelations.relatedIssueId, input.issueId))).limit(1);
return Boolean(edge);
}