Files
PaperClipAI/server/src/services/issue-recovery-actions.ts
T
Dotta 9bb4789c57 fix(heartbeat): park deferred issue monitors and preserve updated_at (#22851)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The heartbeat scheduler claims due issue monitors and dispatches
wakes when their scheduled check time arrives
> - When admission defers a scheduled monitor behind an unsettled
execution-reconciliation hold or another temporary block, the scheduler
wrote `issues.updated_at` on every claim and release and immediately
reset `monitor_wake_requested_at` to `null`
> - That left the past `monitor_next_check_at` eligible on every
subsequent tick, flooding old held tasks to the top of the issues list,
generating recurring database writes, and emitting a new
`issue.monitor_deferred` activity row every five minutes forever
> - This pull request stops updating `issues.updated_at` on sterile
monitor claims and deferred releases, parks monitors held behind an
unchanged effective recovery hold until that hold resolves or changes
(with event-driven wake on resolution), and applies capped exponential
backoff to non-hold deferrals without consuming `monitor_attempt_count`
> - The benefit is zero idle polling, zero `updated_at` churn, and zero
unbounded `activity_log` spam on held tasks while legitimate monitors
still fire immediately once reconciliation clears and still respect
`timeoutAt` deadlines

## Linked Issues or Issue Description

Fixes #20174
Refs #20114
Refs #18973
Refs #18979

Related open PRs:
- `#20175` and `#20296` remove `updated_at` writes during
claims/releases and retain the five-minute stale claim, but deferred
monitors still re-fire and emit a new `issue.monitor_deferred` activity
row every five minutes indefinitely.
- `#21714` adds backoff by mutating `monitor_next_check_at` and
incrementing `monitor_attempt_count`, but still bumps
`issues.updated_at` on claims and releases, spends attempt budget on
rejected admissions, and loops every 30 minutes on
`monitor_recovery_deferred` when default `recoveryPolicy: "wake_owner"`
hits an active `execution_reconciliation_required` hold.

## What Changed

- Added `server/src/services/heartbeat/issue-monitor-deferral.ts`
(keeping `server/src/services/heartbeat/scheduling.ts` lean and
cohesive) with:
- Server-internal deferral state stored under
`execution_state.monitorDeferral`, bound to `monitor_next_check_at` so
any schedule replacement auto-invalidates prior deferral state without
any database migration or `packages/shared` contract change.
- Shared `parkableRecoveryHoldCondition()` (`status in ('active',
'escalated')` OR `executionBlockerPredicate()`), parking monitors behind
both open holds and still-effective resolved holds (such as
`automaticRecovery.replay = 'blocked'` and unreconciled native exports).
- `resolveScheduledMonitorDeferralUpdate(...)` encapsulating
recovery-hold lookup, deferral state construction, and patch assembly
outside `scheduling.ts`.
- Capped exponential backoff (`30s` -> `60m`, clamped to `timeoutAt`)
with scaled diagnostic quiet windows for non-hold deferrals.
- Updated `server/src/services/heartbeat/scheduling.ts`:
- Removed `updatedAt` updates from monitor claims
(`triggerIssueMonitor`, `tickDueIssueMonitors`) and deferred releases
(`releaseIssueMonitorWithDiagnostic`), so only actual monitor dispatch
or clear advances task activity.
- Parked scheduled deferrals (including `wake_owner` recovery deferrals)
on unchanged effective `issue_recovery_actions` holds with a
microsecond-exact UTC `updated_at` fence, yielding `nextIssueMonitorAt()
=== null` while the hold stays unchanged.
- Preserved `timeoutAt` deadlines for `escalate_to_board` and
`create_recovery_issue` policies even while a recovery hold is active.
- Stripped `monitorDeferral` atomically on monitor trigger or clear
without consuming `monitor_attempt_count` on rejected admissions or
starving ready monitors behind tied deferred rows.
- Updated `server/src/services/heartbeat/run-preparation.ts` and
`server/src/services/heartbeat/queue.ts` to pass optional
`executionBlocker.recoveryActionId ?? null` to `onExecutionDeferred`.
- Updated `server/src/services/issue-recovery-actions.ts`
(`resolveActiveForIssue`, `upsertSourceScoped`) and
`server/src/routes/issues.ts` (direct workspace-repair resolution) to
notify `DELIVERY_QUEUES.issueMonitors` when mutating or resolving a hold
on an issue with a scheduled monitor.
- Updated `server/src/services/issue-execution-policy.ts` to preserve
`monitorDeferral` across unrelated `execution_state` rewrites for the
same schedule and drop it when a monitor is replaced or cleared.
- Added red-green behavioral regressions and unit tests in
`server/src/__tests__/issue-monitor-deferral-parking.test.ts` (keeping
`issue-monitor-scheduler.test.ts` at 817 lines),
`server/src/services/heartbeat/scheduling.test.ts`, and
`server/src/services/heartbeat/issue-monitor-deferral.test.ts`.

## Verification

- **Red -> Green proof**:
- Before production edits, all 5 new behavioral regressions failed on
unfixed `master`: parking regressions failed on `issues.updated_at`
bumping during deferred claims and `nextIssueMonitorAt()` staying
immediately due instead of parking, and `scheduling.test.ts` regressions
failed on `issues.updated_at` bumping and re-selecting 49 deferred tied
rows.
  - After production edits and review fixes (commit `db0cdf8cbf`):
    - `npx tsc --noEmit -p server/tsconfig.json`: passed (0 errors)
- `pnpm --filter @paperclipai/server exec vitest run
src/services/heartbeat/issue-monitor-deferral.test.ts`: **4/4 passed**
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/issue-monitor-deferral-parking.test.ts`: **4/4 passed**
(covers `provider_failure_meaning_unverified`,
`legacy_execution_requires_reconciliation`, resolved
`automaticRecovery.replay = 'blocked'` no-replay holds,
`DELIVERY_QUEUES.issueMonitors` wake on resolution, and `timeoutAt` for
`escalate_to_board` vs `wake_owner`)
- `pnpm --filter @paperclipai/server exec vitest run
src/services/heartbeat/scheduling.test.ts`: **58/58 passed** (includes
exponential backoff, replacement invalidation, 50-deferred + 1-ready
non-starvation, and all 18 deadline lifecycle tests)
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/issue-execution-policy.test.ts
src/__tests__/issue-monitor-scheduler.test.ts
src/services/native-runtime/native-failed-run-owner-release.test.ts
src/__tests__/scheduled-work-postgres.test.ts`: **134/134 passed**
- `pnpm verify --explain`: selects broad transitive server suites;
required CI covers the full suite.

## Risks

- Low risk; no schema migration and no `packages/shared` public contract
changes.
- `monitorDeferral` lives inside `issues.execution_state` as
server-internal state stripped by `parseIssueExecutionState` on public
reads and bound to `monitor_next_check_at` so raw or API schedule
replacements invalidate it immediately.
- Non-hold scheduled deferrals now wait at least 30 seconds (growing
exponentially to 60 minutes, clamped to `timeoutAt`) before retrying
instead of re-selecting immediately on the next tick. Manual board
checks (`triggerIssueMonitor`) bypass scheduled backoff and run on
demand.
- A monitor parked on an effective `issue_recovery_actions` hold wakes
immediately when that hold clears or its `updated_at` advances, and
recovery-action mutations emit `DELIVERY_QUEUES.issueMonitors` so
deadline workers wake without polling. `readIdleIssueSchedule` remains
intentionally conservative about open holds.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Google Gemini (`gemini-2.5-pro` / Antigravity agent runtime with
reasoning, tool use, and local code execution)

## 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
- [ ] I have updated relevant documentation to reflect my changes (no
user-facing docs cover internal scheduler deferral bookkeeping)
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green (in progress on PR head
`db0cdf8cbf`)
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
(in progress on PR head `db0cdf8cbf`)
- [x] I will address all Greptile and reviewer comments before
requesting merge
2026-11-25 04:37:02 -06:00

721 lines
32 KiB
TypeScript

import { notifyAttentionRetentionWork } from "./retention-work-notifications.js";
import { notifyIssueGraphRecovery } from "./issue-recovery-notifications.js";
import { notifyNativeRecoveryNotices } from "./native-recovery-notice-notifications.js";
import { notifyUnsafeWorkspaceExportRecovery } from "./unsafe-workspace-export-notifications.js";
import { writeExecutionControl } from "./execution-control-notifications.js";
import { DELIVERY_QUEUES, notifyDeliveryWork } from "./delivery-work-notifications.js";
import { and, desc, eq, inArray, isNull, isNotNull, or, sql } from "drizzle-orm";
import type { Db } from "@paperclipai/db";
import { heartbeatRuns, issueRecoveryActions, issues, nativeRunFinalizations, workspaceOperations } from "@paperclipai/db";
import { isUuidLike } from "@paperclipai/shared";
import type {
IssueRecoveryAction,
IssueRecoveryActionKind,
IssueRecoveryActionOwnerType,
IssueRecoveryActionOutcome,
IssueRecoveryActionStatus,
} from "@paperclipai/shared";
import { isNativeWorkspaceFinalizationOperationActive } from "./workspace-operations.js";
import { exhaustedSystemRecoveryAction, readSystemRecovery } from "./system-recovery-state.js";
const ACTIVE_RECOVERY_ACTION_STATUSES = ["active", "escalated"] as const satisfies readonly IssueRecoveryActionStatus[];
const MAX_UPSERT_RETRIES = 3;
type IssueRecoveryActionRow = typeof issueRecoveryActions.$inferSelect;
type DbTransaction = Parameters<Parameters<Db["transaction"]>[0]>[0];
type DbOrTransaction = Db | DbTransaction;
function asDatabaseDate(value: string | Date | null) {
return typeof value === "string" ? new Date(value) : value;
}
function isRecoveryBudgetExhausted(evidence: Record<string, unknown>) {
const budget = evidence.recoveryBudget;
return Boolean(
budget &&
typeof budget === "object" &&
!Array.isArray(budget) &&
(budget as Record<string, unknown>).state === "exhausted",
);
}
export type UpsertIssueRecoveryActionInput = {
companyId: string;
sourceIssueId: string;
recoveryIssueId?: string | null;
kind: IssueRecoveryActionKind;
ownerType?: IssueRecoveryActionOwnerType;
ownerAgentId?: string | null;
ownerUserId?: string | null;
previousOwnerAgentId?: string | null;
returnOwnerAgentId?: string | null;
cause: string;
fingerprint: string;
evidence?: Record<string, unknown>;
/** Evidence written only when this upsert creates a new action row. */
evidenceOnCreate?: Record<string, unknown>;
nextAction: string;
wakePolicy?: Record<string, unknown> | null;
monitorPolicy?: Record<string, unknown> | null;
maxAttempts?: number | null;
timeoutAt?: Date | null;
lastAttemptAt?: Date | null;
attemptCount?: number;
// When true, a change of (cause, fingerprint) does not overwrite the active
// action in place. The service resolves the prior action and inserts a new
// one. The new failure then gets a distinct recovery identity and a fresh
// operator notice, and the prior identity stays as a resolved record.
supersedeOnIdentityChange?: boolean;
// When true, an existing active action is returned unchanged. A concurrent
// writer that wins the insert keeps its owner, cause, and evidence.
createOnly?: boolean;
// Rollout compatibility for active pre-policy actions. Refresh their
// evidence/attempt metadata without silently changing the recorded owner or
// the wake/monitor contract that made that owner authoritative.
preserveExistingOwner?: boolean;
};
export type ResolveIssueRecoveryActionInput = {
companyId: string;
sourceIssueId: string;
actionId?: string | null;
kind?: IssueRecoveryActionKind | null;
cause?: string | null;
fingerprint?: string | null;
status: Extract<IssueRecoveryActionStatus, "resolved" | "cancelled">;
outcome: IssueRecoveryActionOutcome;
resolutionNote?: string | null;
sourceTerminal?: boolean;
};
export const SOURCE_TERMINAL_RESOLUTION = "source_terminal";
/** A spent saved-work recovery is described from its current state. Text saved
* by an older release could ask a person to inspect the run's environment. */
function readModelNextAction(row: IssueRecoveryActionRow): string {
const system = readSystemRecovery(row.evidence);
return system?.state === "exhausted" && row.evidence?.workspaceRecovery
? exhaustedSystemRecoveryAction(system, row.evidence) : row.nextAction;
}
function toReadModel(row: IssueRecoveryActionRow): IssueRecoveryAction {
return {
id: row.id,
companyId: row.companyId,
sourceIssueId: row.sourceIssueId,
recoveryIssueId: row.recoveryIssueId,
kind: row.kind as IssueRecoveryAction["kind"],
status: row.status as IssueRecoveryAction["status"],
ownerType: row.ownerType as IssueRecoveryAction["ownerType"],
ownerAgentId: row.ownerAgentId,
ownerUserId: row.ownerUserId,
previousOwnerAgentId: row.previousOwnerAgentId,
returnOwnerAgentId: row.returnOwnerAgentId,
cause: row.cause,
fingerprint: row.fingerprint,
evidence: row.evidence,
nextAction: readModelNextAction(row),
wakePolicy: row.wakePolicy,
monitorPolicy: row.monitorPolicy,
attemptCount: row.attemptCount,
maxAttempts: row.maxAttempts,
timeoutAt: row.timeoutAt,
lastAttemptAt: row.lastAttemptAt,
outcome: row.outcome as IssueRecoveryAction["outcome"],
resolutionNote: row.resolutionNote,
resolvedAt: row.resolvedAt,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
};
}
function isUniqueRecoveryActionConflict(error: unknown) {
const seen = new Set<unknown>();
let candidate = error;
// Drizzle wraps the driver error in cause. Only this specific uniqueness
// conflict can reuse the existing bounded source-scoped upsert retry.
for (let depth = 0; depth < 8 && candidate && typeof candidate === "object"; depth++) {
if (seen.has(candidate)) break;
seen.add(candidate);
const maybe = candidate as { code?: string; constraint?: string; constraint_name?: string; message?: string; cause?: unknown };
if (maybe.code === "23505" && (
maybe.constraint === "issue_recovery_actions_active_source_uq" ||
maybe.constraint === "issue_recovery_actions_active_fingerprint_uq" ||
maybe.constraint_name === "issue_recovery_actions_active_source_uq" ||
maybe.constraint_name === "issue_recovery_actions_active_fingerprint_uq" ||
typeof maybe.message === "string" && (
maybe.message.includes("issue_recovery_actions_active_source_uq") ||
maybe.message.includes("issue_recovery_actions_active_fingerprint_uq")
)
)) return true;
candidate = maybe.cause;
}
return false;
}
export function issueRecoveryActionService(db: DbOrTransaction) {
const upsertQueues = new Map<string, Promise<void>>();
async function runExclusiveUpsert<T>(
input: UpsertIssueRecoveryActionInput,
task: () => Promise<T>,
): Promise<T> {
const key = `${input.companyId}:${input.sourceIssueId}`;
const previous = upsertQueues.get(key) ?? Promise.resolve();
let release: () => void = () => {};
const current = new Promise<void>((resolve) => {
release = resolve;
});
const next = previous.catch(() => undefined).then(() => current);
upsertQueues.set(key, next);
await previous.catch(() => undefined);
try {
return await task();
} finally {
release();
if (upsertQueues.get(key) === next) {
upsertQueues.delete(key);
}
}
}
// Coordinator retries reuse the original native run. They are not legacy
// scheduled retries, and workspace export can run while the heartbeat still
// records its earlier failure. Project their actual activity for every reader.
async function projectNativeRunActivity(
companyId: string,
actions: IssueRecoveryAction[],
dbOrTx: DbOrTransaction,
) {
const nativeActions = actions.filter((action) => action.wakePolicy?.kind === "resume_native_run");
for (const action of nativeActions) action.nativeRunActivity = null;
const runIds = [...new Set(nativeActions.flatMap((action) => {
const runId = action.wakePolicy?.runId;
return typeof runId === "string" && isUuidLike(runId) ? [runId] : [];
}))];
if (runIds.length === 0) return;
const activity = await dbOrTx.select({
runId: heartbeatRuns.id,
issueId: heartbeatRuns.nativeIssueId,
status: heartbeatRuns.status,
finishedAt: heartbeatRuns.finishedAt,
workspaceOperationId: workspaceOperations.id,
}).from(heartbeatRuns).leftJoin(workspaceOperations, and(
eq(workspaceOperations.companyId, heartbeatRuns.companyId),
eq(workspaceOperations.heartbeatRunId, heartbeatRuns.id),
eq(workspaceOperations.issueId, heartbeatRuns.nativeIssueId),
eq(workspaceOperations.phase, "workspace_finalize"),
eq(workspaceOperations.status, "running"),
isNull(workspaceOperations.finishedAt),
sql`${workspaceOperations.metadata}->>'owningService' = 'native_workspace_finalizer'`,
)).where(and(
eq(heartbeatRuns.companyId, companyId),
eq(heartbeatRuns.runtimeMode, "native"),
inArray(heartbeatRuns.id, runIds),
or(
and(inArray(heartbeatRuns.status, ["queued", "running"]), isNull(heartbeatRuns.finishedAt)),
isNotNull(workspaceOperations.id),
),
)).orderBy(desc(workspaceOperations.startedAt));
const activityByRun = new Map<string, (typeof activity)[number]>();
for (const row of activity) {
const liveOperation = row.workspaceOperationId && row.issueId
&& isNativeWorkspaceFinalizationOperationActive({
operationId: row.workspaceOperationId, companyId, runId: row.runId, issueId: row.issueId,
});
const liveHeartbeat = ["queued", "running"].includes(row.status) && row.finishedAt === null;
if (!liveHeartbeat && !liveOperation) continue;
const projected = { ...row, workspaceOperationId: liveOperation ? row.workspaceOperationId : null };
if (!activityByRun.has(row.runId) || (!activityByRun.get(row.runId)?.workspaceOperationId && liveOperation)) {
activityByRun.set(row.runId, projected);
}
}
for (const action of nativeActions) {
const row = activityByRun.get(String(action.wakePolicy?.runId).toLowerCase());
if (!row || row.issueId !== action.sourceIssueId) continue;
action.nativeRunActivity = {
runId: row.runId,
status: row.workspaceOperationId || row.status === "running" ? "running" : "queued",
workspaceOperationId: row.workspaceOperationId,
};
}
}
async function getActiveForIssue(
companyId: string,
sourceIssueId: string,
dbOrTx: DbOrTransaction = db,
): Promise<IssueRecoveryAction | null> {
const row = await dbOrTx
.select()
.from(issueRecoveryActions)
.where(
and(
eq(issueRecoveryActions.companyId, companyId),
eq(issueRecoveryActions.sourceIssueId, sourceIssueId),
inArray(issueRecoveryActions.status, [...ACTIVE_RECOVERY_ACTION_STATUSES]),
),
)
.orderBy(desc(issueRecoveryActions.updatedAt))
.limit(1)
.then((rows) => rows[0] ?? null);
if (!row) return null;
const action = toReadModel(row);
await projectNativeRunActivity(companyId, [action], dbOrTx);
return action;
}
async function listActiveForIssues(companyId: string, sourceIssueIds: string[]) {
if (sourceIssueIds.length === 0) return new Map<string, IssueRecoveryAction>();
const rows = await db
.select()
.from(issueRecoveryActions)
.where(
and(
eq(issueRecoveryActions.companyId, companyId),
inArray(issueRecoveryActions.sourceIssueId, [...new Set(sourceIssueIds)]),
inArray(issueRecoveryActions.status, [...ACTIVE_RECOVERY_ACTION_STATUSES]),
),
)
.orderBy(desc(issueRecoveryActions.updatedAt));
const result = new Map<string, IssueRecoveryAction>();
for (const row of rows) {
if (!result.has(row.sourceIssueId)) result.set(row.sourceIssueId, toReadModel(row));
}
await projectNativeRunActivity(companyId, [...result.values()], db);
return result;
}
async function retryUpsertSourceScoped(
input: UpsertIssueRecoveryActionInput,
retryCount: number,
error?: unknown,
): Promise<IssueRecoveryAction> {
if (retryCount >= MAX_UPSERT_RETRIES) {
if (error) throw error;
throw new Error(
`Failed to upsert active recovery action for issue ${input.sourceIssueId} after ${MAX_UPSERT_RETRIES} retries`,
);
}
return upsertSourceScopedUnlocked(input, retryCount + 1);
}
function buildInsertValues(
input: UpsertIssueRecoveryActionInput,
ownerType: IssueRecoveryActionOwnerType,
now: Date,
) {
return {
companyId: input.companyId,
sourceIssueId: input.sourceIssueId,
recoveryIssueId: input.recoveryIssueId ?? null,
kind: input.kind,
status: "active" as const,
ownerType,
ownerAgentId: input.ownerAgentId ?? null,
ownerUserId: input.ownerUserId ?? null,
previousOwnerAgentId: input.previousOwnerAgentId ?? null,
returnOwnerAgentId: input.returnOwnerAgentId ?? null,
cause: input.cause,
fingerprint: input.fingerprint,
evidence: {
...(input.evidence ?? {}),
...(input.evidenceOnCreate ?? {}),
},
nextAction: input.nextAction,
wakePolicy: input.wakePolicy ?? null,
monitorPolicy: input.monitorPolicy ?? null,
attemptCount: input.attemptCount ?? 1,
maxAttempts: input.maxAttempts ?? null,
timeoutAt: input.timeoutAt ?? null,
lastAttemptAt: input.lastAttemptAt ?? now,
};
}
// Resolve the prior active action, then insert a new one in one transaction.
// The prior identity stays as a cancelled record and the new failure gets a
// fresh action row with its own id. The partial unique index on the active
// status stays satisfied because only the new row is active at commit.
async function supersedePriorAndInsert(
input: UpsertIssueRecoveryActionInput,
priorActionId: string,
ownerType: IssueRecoveryActionOwnerType,
now: Date,
retryCount: number,
): Promise<IssueRecoveryAction> {
try {
const created = await db.transaction(async (tx) => {
await notifyAttentionRetentionWork(tx, { companyId: input.companyId });
await notifyIssueGraphRecovery(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
await notifyNativeRecoveryNotices(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
await notifyDeliveryWork(tx, DELIVERY_QUEUES.executionDisposition);
await notifyDeliveryWork(tx, DELIVERY_QUEUES.executionContinuation);
await notifyUnsafeWorkspaceExportRecovery(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
const [superseded] = await tx
.update(issueRecoveryActions)
.set({
status: "cancelled",
outcome: "cancelled",
resolutionNote: "A new failure with a different identity superseded this recovery action.",
resolvedAt: now,
updatedAt: now,
})
.where(
and(
eq(issueRecoveryActions.id, priorActionId),
inArray(issueRecoveryActions.status, [...ACTIVE_RECOVERY_ACTION_STATUSES]),
),
)
.returning();
// Another writer resolved the prior action first. Abort and retry the
// whole upsert so the retry reads the current active state.
if (!superseded) return null;
const [row] = await tx
.insert(issueRecoveryActions)
.values(buildInsertValues(input, ownerType, now))
.returning();
return row ?? null;
});
if (!created) return retryUpsertSourceScoped(input, retryCount);
return toReadModel(created);
} catch (error) {
if (!isUniqueRecoveryActionConflict(error)) throw error;
return retryUpsertSourceScoped(input, retryCount, error);
}
}
async function upsertSourceScopedUnlocked(
input: UpsertIssueRecoveryActionInput,
retryCount = 0,
): Promise<IssueRecoveryAction> {
const existing = await getActiveForIssue(input.companyId, input.sourceIssueId);
const now = new Date();
if (existing && input.createOnly) return existing;
if (!existing) {
const [source] = await db.select({ status: issues.status }).from(issues)
.where(and(eq(issues.companyId, input.companyId), eq(issues.id, input.sourceIssueId))).limit(1);
if (source && (source.status === "done" || source.status === "cancelled")) {
const [settled] = await db.select().from(issueRecoveryActions).where(and(
eq(issueRecoveryActions.companyId, input.companyId),
eq(issueRecoveryActions.sourceIssueId, input.sourceIssueId),
eq(issueRecoveryActions.status, "resolved"),
eq(issueRecoveryActions.cause, input.cause),
eq(issueRecoveryActions.fingerprint, input.fingerprint),
eq(issueRecoveryActions.resolutionNote, SOURCE_TERMINAL_RESOLUTION),
)).limit(1);
if (settled) return toReadModel(settled);
}
}
const ownerType = input.ownerType ?? (input.ownerAgentId ? "agent" : "board");
if (existing) {
// A distinct failure identity must not overwrite the active action of a
// prior identity. Resolve the prior action and insert a new one, so the
// operator gets a new notice for the new failure.
if (
input.supersedeOnIdentityChange &&
(existing.cause !== input.cause || existing.fingerprint !== input.fingerprint)
) {
return supersedePriorAndInsert(input, existing.id, ownerType, now, retryCount);
}
// `maxAttempts` is an execution budget, not display metadata. Once the
// same recovery identity consumes it, retain one inspectable board-owned
// action but remove every automatic wake/monitor path. Repeated sweep or
// finalizer writes then become idempotent instead of silently advancing
// beyond the advertised cap. A distinct identity can still supersede the
// exhausted action through the branch above.
if (isRecoveryBudgetExhausted(existing.evidence ?? {})) {
// Older sweeps replaced this operator-only hold with instructions to
// start another run. Restore the exact copyback recovery instructions
// without reopening its budget or erasing a recorded stop confirmation.
if (existing.cause === "native_workspace_finalization_owner_unverified"
&& input.cause === existing.cause && input.fingerprint === existing.fingerprint
&& !existing.evidence?.workspaceOwnerStop && existing.nextAction !== input.nextAction) {
const [updated] = await writeExecutionControl(db, [DELIVERY_QUEUES.executionDisposition, DELIVERY_QUEUES.executionContinuation], async tx => {
const [monitored] = await tx.select({ id: issues.id }).from(issues).where(and(
eq(issues.companyId, input.companyId),
eq(issues.id, input.sourceIssueId),
isNotNull(issues.monitorNextCheckAt),
)).limit(1);
if (monitored) await notifyDeliveryWork(tx, DELIVERY_QUEUES.issueMonitors);
return tx.update(issueRecoveryActions).set({ nextAction: input.nextAction, updatedAt: now })
.where(and(eq(issueRecoveryActions.id, existing.id),
inArray(issueRecoveryActions.status, [...ACTIVE_RECOVERY_ACTION_STATUSES]),
sql`${issueRecoveryActions.evidence}->'workspaceOwnerStop' is null`)).returning();
}, { companyId: input.companyId, issueId: input.sourceIssueId });
if (updated) return toReadModel(updated);
}
return existing;
}
const nextAttemptCount =
input.attemptCount ?? existing.attemptCount + 1;
const effectiveMaxAttempts = input.preserveExistingOwner
? existing.maxAttempts
: input.maxAttempts === undefined
? existing.maxAttempts
: input.maxAttempts;
if (
effectiveMaxAttempts !== null &&
nextAttemptCount >= effectiveMaxAttempts
) {
const attemptsUsed = Math.max(
existing.attemptCount,
Math.min(nextAttemptCount, effectiveMaxAttempts),
);
const [exhausted] = await writeExecutionControl(db, [DELIVERY_QUEUES.executionDisposition, DELIVERY_QUEUES.executionContinuation], async tx => {
await notifyIssueGraphRecovery(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
await notifyNativeRecoveryNotices(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
if ((!input.preserveExistingOwner && existing.cause !== input.cause)
|| (input.evidence?.runId !== undefined && input.evidence.runId !== existing.evidence?.runId)) {
await notifyUnsafeWorkspaceExportRecovery(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
}
return tx
.update(issueRecoveryActions)
.set({
status: "escalated",
ownerType: existing.ownerType === "system" || input.ownerType === "system" ? "system" : "board",
ownerAgentId: null,
ownerUserId: null,
previousOwnerAgentId:
existing.ownerAgentId ?? existing.previousOwnerAgentId,
returnOwnerAgentId:
input.returnOwnerAgentId ??
existing.returnOwnerAgentId ??
existing.ownerAgentId,
evidence: {
...(existing.evidence ?? {}),
...(input.evidence ?? {}),
recoveryBudget: {
state: "exhausted",
attemptsUsed,
maxAttempts: effectiveMaxAttempts,
exhaustedAt: now.toISOString(),
cause: existing.cause,
fingerprint: existing.fingerprint,
},
},
nextAction: existing.cause === "native_workspace_finalization_owner_unverified"
? input.nextAction
: `Automatic recovery exhausted after ${attemptsUsed}/${effectiveMaxAttempts} attempts. ` +
"Review the infrastructure failure and explicitly choose a replacement run or provider configuration.",
wakePolicy: null,
monitorPolicy: null,
attemptCount: attemptsUsed,
maxAttempts: effectiveMaxAttempts,
timeoutAt: null,
lastAttemptAt: input.lastAttemptAt ?? now,
outcome: "escalated",
resolutionNote: null,
resolvedAt: null,
updatedAt: now,
})
.where(
and(
eq(issueRecoveryActions.id, existing.id),
inArray(issueRecoveryActions.status, [
...ACTIVE_RECOVERY_ACTION_STATUSES,
]),
),
)
.returning();
}, { companyId: input.companyId, issueId: input.sourceIssueId });
if (!exhausted) {
return retryUpsertSourceScoped(input, retryCount);
}
return toReadModel(exhausted);
}
const [updated] = await writeExecutionControl(db, [DELIVERY_QUEUES.executionDisposition, DELIVERY_QUEUES.executionContinuation], async tx => {
await notifyIssueGraphRecovery(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
await notifyNativeRecoveryNotices(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
if ((!input.preserveExistingOwner && existing.cause !== input.cause)
|| (input.evidence?.runId !== undefined && input.evidence.runId !== existing.evidence?.runId)) {
await notifyUnsafeWorkspaceExportRecovery(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
}
return tx
.update(issueRecoveryActions)
.set({
recoveryIssueId: input.preserveExistingOwner
? existing.recoveryIssueId
: input.recoveryIssueId ?? null,
kind: input.preserveExistingOwner ? existing.kind : input.kind,
status: input.preserveExistingOwner ? existing.status : "active",
ownerType: input.preserveExistingOwner ? existing.ownerType : ownerType,
ownerAgentId: input.preserveExistingOwner
? existing.ownerAgentId
: input.ownerAgentId ?? null,
ownerUserId: input.preserveExistingOwner
? existing.ownerUserId
: input.ownerUserId ?? null,
previousOwnerAgentId: input.preserveExistingOwner
? existing.previousOwnerAgentId
: input.previousOwnerAgentId ?? existing.previousOwnerAgentId,
returnOwnerAgentId: input.preserveExistingOwner
? existing.returnOwnerAgentId
: input.returnOwnerAgentId ?? existing.returnOwnerAgentId,
cause: input.preserveExistingOwner ? existing.cause : input.cause,
fingerprint: input.preserveExistingOwner ? existing.fingerprint : input.fingerprint,
evidence: input.preserveExistingOwner
? {
...(existing.evidence ?? {}),
...(input.evidence ?? {}),
}
: input.evidence ?? existing.evidence,
nextAction: input.preserveExistingOwner ? existing.nextAction : input.nextAction,
wakePolicy: input.preserveExistingOwner
? existing.wakePolicy
: input.wakePolicy ?? null,
monitorPolicy: input.preserveExistingOwner
? existing.monitorPolicy
: input.monitorPolicy ?? null,
attemptCount: nextAttemptCount,
maxAttempts: input.preserveExistingOwner
? existing.maxAttempts
: input.maxAttempts === undefined
? existing.maxAttempts
: input.maxAttempts,
timeoutAt: input.preserveExistingOwner
? asDatabaseDate(existing.timeoutAt)
: input.timeoutAt ?? null,
lastAttemptAt: input.preserveExistingOwner
? asDatabaseDate(existing.lastAttemptAt)
: input.lastAttemptAt ?? now,
outcome: input.preserveExistingOwner ? existing.outcome : null,
resolutionNote: input.preserveExistingOwner ? existing.resolutionNote : null,
resolvedAt: null,
updatedAt: now,
})
.where(
and(
eq(issueRecoveryActions.id, existing.id),
inArray(issueRecoveryActions.status, [...ACTIVE_RECOVERY_ACTION_STATUSES]),
),
)
.returning();
}, { companyId: input.companyId, issueId: input.sourceIssueId });
if (!updated) {
return retryUpsertSourceScoped(input, retryCount);
}
return toReadModel(updated!);
}
try {
const [created] = await writeExecutionControl(db, [DELIVERY_QUEUES.executionDisposition, DELIVERY_QUEUES.executionContinuation], async tx => {
await notifyIssueGraphRecovery(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
await notifyNativeRecoveryNotices(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
await notifyUnsafeWorkspaceExportRecovery(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
return tx
.insert(issueRecoveryActions)
.values(buildInsertValues(input, ownerType, now))
.returning();
}, { companyId: input.companyId, issueId: input.sourceIssueId });
return toReadModel(created!);
} catch (error) {
if (!isUniqueRecoveryActionConflict(error)) throw error;
return retryUpsertSourceScoped(input, retryCount, error);
}
}
async function upsertSourceScoped(
input: UpsertIssueRecoveryActionInput,
): Promise<IssueRecoveryAction> {
return runExclusiveUpsert(input, () => upsertSourceScopedUnlocked(input));
}
async function resolveActiveForIssue(
input: ResolveIssueRecoveryActionInput,
dbOrTx: DbOrTransaction = db,
): Promise<IssueRecoveryAction | null> {
const now = new Date();
const predicates = [
eq(issueRecoveryActions.companyId, input.companyId),
eq(issueRecoveryActions.sourceIssueId, input.sourceIssueId),
inArray(issueRecoveryActions.status, [...ACTIVE_RECOVERY_ACTION_STATUSES]),
];
if (input.actionId) {
predicates.push(eq(issueRecoveryActions.id, input.actionId));
}
if (input.kind) {
predicates.push(eq(issueRecoveryActions.kind, input.kind));
}
if (input.cause) {
predicates.push(eq(issueRecoveryActions.cause, input.cause));
}
if (input.fingerprint) {
predicates.push(eq(issueRecoveryActions.fingerprint, input.fingerprint));
}
const [candidate] = await dbOrTx.select({ id: issueRecoveryActions.id }).from(issueRecoveryActions)
.where(and(...predicates)).limit(1);
if (!candidate) return null;
const releaseWaiter = async (tx: DbOrTransaction) => {
const [waiter] = await tx.select({ id: nativeRunFinalizations.runId }).from(nativeRunFinalizations)
.innerJoin(heartbeatRuns, eq(heartbeatRuns.id, nativeRunFinalizations.runId)).where(and(
eq(nativeRunFinalizations.companyId, input.companyId), eq(nativeRunFinalizations.issueId, input.sourceIssueId),
eq(nativeRunFinalizations.phase, "observed"), isNull(nativeRunFinalizations.resultId),
isNotNull(nativeRunFinalizations.leaseOwner), inArray(heartbeatRuns.status, ["failed", "timed_out", "interrupted"]),
)).limit(1);
if (waiter) await notifyDeliveryWork(tx, DELIVERY_QUEUES.executionFinalization);
};
const releaseParkedMonitor = async (tx: DbOrTransaction) => {
const [monitored] = await tx.select({ id: issues.id }).from(issues).where(and(
eq(issues.companyId, input.companyId),
eq(issues.id, input.sourceIssueId),
isNotNull(issues.monitorNextCheckAt),
)).limit(1);
if (monitored) await notifyDeliveryWork(tx, DELIVERY_QUEUES.issueMonitors);
};
if (input.sourceTerminal) {
const [settled] = await dbOrTx.transaction(async tx => {
await releaseWaiter(tx as unknown as DbOrTransaction);
await notifyNativeRecoveryNotices(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
await notifyUnsafeWorkspaceExportRecovery(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
return tx.update(issueRecoveryActions)
.set({ status: input.status, outcome: input.outcome, resolutionNote: input.resolutionNote ?? null, resolvedAt: now, updatedAt: now })
.where(and(...predicates, sql`exists (select 1 from ${issues} where ${issues.id} = ${issueRecoveryActions.sourceIssueId}
and ${issues.companyId} = ${issueRecoveryActions.companyId} and ${issues.status} in ('done', 'cancelled'))`))
.returning();
});
return settled ? toReadModel(settled) : null;
}
const [updated] = await writeExecutionControl(dbOrTx, [DELIVERY_QUEUES.executionDisposition, DELIVERY_QUEUES.executionContinuation], async tx => {
const pending = await tx.select({ id: issueRecoveryActions.id }).from(issueRecoveryActions)
.where(and(...predicates)).limit(1);
if (!pending.length) return [];
// Resolving another attention releases this issue's preserved Native
// waiter to one normal inspection. No recurring monitor or new turn.
await releaseWaiter(tx as unknown as DbOrTransaction);
await releaseParkedMonitor(tx as unknown as DbOrTransaction);
await notifyIssueGraphRecovery(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
await notifyNativeRecoveryNotices(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
await notifyUnsafeWorkspaceExportRecovery(tx, { companyId: input.companyId, issueId: input.sourceIssueId });
return tx
.update(issueRecoveryActions)
.set({
status: input.status,
outcome: input.outcome,
resolutionNote: input.resolutionNote ?? null,
resolvedAt: now,
updatedAt: now,
})
.where(and(...predicates))
.returning();
}, { companyId: input.companyId, issueId: input.sourceIssueId });
return updated ? toReadModel(updated) : null;
}
return {
getActiveForIssue,
listActiveForIssues,
resolveActiveForIssue,
upsertSourceScoped,
};
}
export { toReadModel as issueRecoveryActionReadModel };