mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 23:56:43 +01:00
Add dependency wake reconciliation backstop (#8943)
This commit is contained in:
1 parent
bcac517f3b
commit
7bfaaadcb8
8 files changed
+959
-51
No files matched your search
@@ -109,6 +109,11 @@ import {
|
||||
sanitizeRuntimeServiceBaseEnv,
|
||||
} from "./workspace-runtime.js";
|
||||
import { issueService } from "./issues.js";
|
||||
import {
|
||||
ISSUE_BLOCKERS_RESOLVED_WAKE_REASON,
|
||||
buildIssueBlockersResolvedWakeIdempotencyKey,
|
||||
findExistingIssueBlockersResolvedWake,
|
||||
} from "./issue-dependency-wakeups.js";
|
||||
import {
|
||||
buildIssueMonitorClearedPatch,
|
||||
buildIssueMonitorTriggeredPatch,
|
||||
@@ -417,7 +422,10 @@ function mergeAdapterRecoveryMetadata(input: {
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
const RUNNING_ISSUE_WAKE_REASONS_REQUIRING_FOLLOWUP = new Set(["approval_approved"]);
|
||||
const RUNNING_ISSUE_WAKE_REASONS_REQUIRING_FOLLOWUP = new Set([
|
||||
"approval_approved",
|
||||
ISSUE_BLOCKERS_RESOLVED_WAKE_REASON,
|
||||
]);
|
||||
const SESSIONED_LOCAL_ADAPTERS = new Set([
|
||||
"claude_local",
|
||||
"codex_local",
|
||||
@@ -11439,11 +11447,14 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
|
||||
agent,
|
||||
);
|
||||
|
||||
// Workspace-finalize wake re-fire: if this run's issue was marked done
|
||||
// mid-run (so the original `issue_blockers_resolved` wake was gated by
|
||||
// the readiness check waiting for workspace_finalize), the finalize
|
||||
// row we just recorded now lets dependents proceed. Fire wakes here.
|
||||
if (issueId && adapterFinalizeOutcome === "succeeded") {
|
||||
// Dependency wake re-check: if this run's issue was marked done mid-run,
|
||||
// the route-time `issue_blockers_resolved` wake may have been gated by
|
||||
// workspace finalization or merged into this run. Re-evaluate after any
|
||||
// run completion, including failed adapter outcomes; this is safe because
|
||||
// `listWakeableBlockedDependents` delegates to dependency readiness, which
|
||||
// only returns dependents whose done blockers have crossed the successful
|
||||
// `workspace_finalize` barrier.
|
||||
if (issueId && finalizedRun) {
|
||||
try {
|
||||
const blockerIssueStatus = await db
|
||||
.select({ status: issues.status })
|
||||
@@ -11453,25 +11464,55 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
|
||||
if (blockerIssueStatus === "done") {
|
||||
const dependents = await issuesSvc.listWakeableBlockedDependents(issueId);
|
||||
for (const dependent of dependents) {
|
||||
const idempotencyKey = buildIssueBlockersResolvedWakeIdempotencyKey({
|
||||
dependentIssueId: dependent.id,
|
||||
resolvedBlockerIssueId: issueId,
|
||||
});
|
||||
const existingWake = await findExistingIssueBlockersResolvedWake(db, {
|
||||
companyId: finalizedRun.companyId,
|
||||
idempotencyKey,
|
||||
});
|
||||
if (existingWake) continue;
|
||||
await enqueueWakeup(dependent.assigneeAgentId, {
|
||||
source: "automation",
|
||||
triggerDetail: "system",
|
||||
reason: "issue_blockers_resolved",
|
||||
reason: ISSUE_BLOCKERS_RESOLVED_WAKE_REASON,
|
||||
payload: {
|
||||
issueId: dependent.id,
|
||||
resolvedBlockerIssueId: issueId,
|
||||
blockerIssueIds: dependent.blockerIssueIds,
|
||||
deferredFor: "workspace_finalize",
|
||||
},
|
||||
idempotencyKey,
|
||||
requestedByActorType: "system",
|
||||
requestedByActorId: "heartbeat_finalize",
|
||||
contextSnapshot: {
|
||||
issueId: dependent.id,
|
||||
taskId: dependent.id,
|
||||
wakeReason: "issue_blockers_resolved",
|
||||
wakeReason: ISSUE_BLOCKERS_RESOLVED_WAKE_REASON,
|
||||
source: "workspace.finalize",
|
||||
resolvedBlockerIssueId: issueId,
|
||||
blockerIssueIds: dependent.blockerIssueIds,
|
||||
},
|
||||
}).catch((wakeErr) => {
|
||||
}).then((wakeRun) =>
|
||||
logActivity(db, {
|
||||
companyId: finalizedRun.companyId,
|
||||
actorType: "system",
|
||||
actorId: "heartbeat_finalize",
|
||||
agentId: dependent.assigneeAgentId,
|
||||
runId: finalizedRun.id,
|
||||
action: "issue.blockers_resolved_wake_emitted",
|
||||
entityType: "issue",
|
||||
entityId: dependent.id,
|
||||
details: {
|
||||
source: "workspace.finalize",
|
||||
wakeupRunId: wakeRun?.id ?? null,
|
||||
idempotencyKey,
|
||||
resolvedBlockerIssueId: issueId,
|
||||
blockerIssueIds: dependent.blockerIssueIds,
|
||||
},
|
||||
})
|
||||
).catch((wakeErr) => {
|
||||
logger.warn(
|
||||
{ err: wakeErr, issueId, dependentIssueId: dependent.id, agentId: dependent.assigneeAgentId },
|
||||
"failed to fire deferred dependent wake after workspace_finalize",
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { agentWakeupRequests } from "@paperclipai/db";
|
||||
|
||||
export const ISSUE_BLOCKERS_RESOLVED_WAKE_REASON = "issue_blockers_resolved";
|
||||
|
||||
const IDEMPOTENT_DEPENDENCY_WAKE_STATUSES = [
|
||||
"queued",
|
||||
"deferred_issue_execution",
|
||||
"claimed",
|
||||
"completed",
|
||||
] as const;
|
||||
|
||||
export function buildIssueBlockersResolvedWakeIdempotencyKey(input: {
|
||||
dependentIssueId: string;
|
||||
resolvedBlockerIssueId: string;
|
||||
}) {
|
||||
return [
|
||||
ISSUE_BLOCKERS_RESOLVED_WAKE_REASON,
|
||||
input.dependentIssueId,
|
||||
input.resolvedBlockerIssueId,
|
||||
].join(":");
|
||||
}
|
||||
|
||||
export async function findExistingIssueBlockersResolvedWake(
|
||||
db: Db,
|
||||
input: {
|
||||
companyId: string;
|
||||
idempotencyKey: string;
|
||||
},
|
||||
) {
|
||||
return db
|
||||
.select({ id: agentWakeupRequests.id, status: agentWakeupRequests.status })
|
||||
.from(agentWakeupRequests)
|
||||
.where(
|
||||
and(
|
||||
eq(agentWakeupRequests.companyId, input.companyId),
|
||||
eq(agentWakeupRequests.idempotencyKey, input.idempotencyKey),
|
||||
inArray(agentWakeupRequests.status, [...IDEMPOTENT_DEPENDENCY_WAKE_STATUSES]),
|
||||
),
|
||||
)
|
||||
.limit(1)
|
||||
.then((rows) => rows[0] ?? null);
|
||||
}
|
||||
|
||||
export async function findExistingIssueBlockersResolvedWakeForAnyKey(
|
||||
db: Db,
|
||||
input: {
|
||||
companyId: string;
|
||||
idempotencyKeys: string[];
|
||||
},
|
||||
) {
|
||||
const idempotencyKeys = [...new Set(input.idempotencyKeys.filter(Boolean))];
|
||||
if (idempotencyKeys.length === 0) return null;
|
||||
|
||||
return db
|
||||
.select({
|
||||
id: agentWakeupRequests.id,
|
||||
status: agentWakeupRequests.status,
|
||||
idempotencyKey: agentWakeupRequests.idempotencyKey,
|
||||
})
|
||||
.from(agentWakeupRequests)
|
||||
.where(
|
||||
and(
|
||||
eq(agentWakeupRequests.companyId, input.companyId),
|
||||
inArray(agentWakeupRequests.idempotencyKey, idempotencyKeys),
|
||||
inArray(agentWakeupRequests.status, [...IDEMPOTENT_DEPENDENCY_WAKE_STATUSES]),
|
||||
),
|
||||
)
|
||||
.limit(1)
|
||||
.then((rows) => rows[0] ?? null);
|
||||
}
|
||||
@@ -37,6 +37,11 @@ import { instanceSettingsService } from "../instance-settings.js";
|
||||
import { issueRecoveryActionService } from "../issue-recovery-actions.js";
|
||||
import { issueTreeControlService } from "../issue-tree-control.js";
|
||||
import { TERMINAL_HEARTBEAT_RUN_STATUSES, issueService } from "../issues.js";
|
||||
import {
|
||||
ISSUE_BLOCKERS_RESOLVED_WAKE_REASON,
|
||||
buildIssueBlockersResolvedWakeIdempotencyKey,
|
||||
findExistingIssueBlockersResolvedWakeForAnyKey,
|
||||
} from "../issue-dependency-wakeups.js";
|
||||
import { parseIssueExecutionState } from "../issue-execution-policy.js";
|
||||
import { evaluateAgentInvokabilityFromDb } from "../agent-invokability.js";
|
||||
import { getRunLogStore } from "../run-log-store.js";
|
||||
@@ -74,6 +79,7 @@ const STRANDED_ISSUE_RECOVERY_ORIGIN_KIND = RECOVERY_ORIGIN_KINDS.strandedIssueR
|
||||
const STALE_ACTIVE_RUN_EVALUATION_ORIGIN_KIND = RECOVERY_ORIGIN_KINDS.staleActiveRunEvaluation;
|
||||
const DEFERRED_WAKE_CONTEXT_KEY = "_paperclipWakeContext";
|
||||
const EXECUTION_REVIEW_PARTICIPANT_RECOVERY_REASON = "execution_review_participant_recovery";
|
||||
const RESOLVED_DEPENDENCY_WAKE_BACKSTOP_CANDIDATE_LIMIT = 500;
|
||||
const SESSIONED_LOCAL_ADAPTERS = new Set([
|
||||
"claude_local",
|
||||
"codex_local",
|
||||
@@ -516,6 +522,7 @@ export function recoveryService(db: Db, deps: { enqueueWakeup: RecoveryWakeup })
|
||||
const budgets = budgetService(db);
|
||||
const instanceSettings = instanceSettingsService(db);
|
||||
const runLogStore = getRunLogStore();
|
||||
let resolvedDependencyWakeBackstopCandidateCursor: string | null = null;
|
||||
|
||||
const getCurrentUserRedactionOptions = async () => ({
|
||||
enabled: (await instanceSettings.getGeneral()).censorUsernameInLogs,
|
||||
@@ -4066,6 +4073,205 @@ export function recoveryService(db: Db, deps: { enqueueWakeup: RecoveryWakeup })
|
||||
return { kind: "created" as const, escalationIssueId: escalation.id };
|
||||
}
|
||||
|
||||
async function reconcileResolvedDependencyWakeBackstop(opts?: { runId?: string | null }) {
|
||||
const result = {
|
||||
checked: 0,
|
||||
healed: 0,
|
||||
existingWakeSkipped: 0,
|
||||
livePathSkipped: 0,
|
||||
interactionSkipped: 0,
|
||||
pauseHoldSkipped: 0,
|
||||
notReadySkipped: 0,
|
||||
candidateLimitSkipped: 0,
|
||||
deferredOrFailed: 0,
|
||||
enqueueFailed: 0,
|
||||
issueIds: [] as string[],
|
||||
};
|
||||
|
||||
const queryCandidates = (afterIssueId: string | null) => {
|
||||
const filters = [
|
||||
eq(issues.status, "blocked"),
|
||||
isNull(issues.hiddenAt),
|
||||
sql`${issues.assigneeAgentId} is not null`,
|
||||
];
|
||||
if (afterIssueId) filters.push(gt(issues.id, afterIssueId));
|
||||
|
||||
return db
|
||||
.select({
|
||||
id: issues.id,
|
||||
companyId: issues.companyId,
|
||||
identifier: issues.identifier,
|
||||
assigneeAgentId: issues.assigneeAgentId,
|
||||
totalCount: sql<number>`count(*) over()::int`,
|
||||
})
|
||||
.from(issues)
|
||||
.where(and(...filters))
|
||||
.orderBy(asc(issues.id))
|
||||
.limit(RESOLVED_DEPENDENCY_WAKE_BACKSTOP_CANDIDATE_LIMIT);
|
||||
};
|
||||
|
||||
let candidateRows = await queryCandidates(resolvedDependencyWakeBackstopCandidateCursor);
|
||||
if (candidateRows.length === 0 && resolvedDependencyWakeBackstopCandidateCursor) {
|
||||
resolvedDependencyWakeBackstopCandidateCursor = null;
|
||||
candidateRows = await queryCandidates(null);
|
||||
}
|
||||
const totalCandidateCount = candidateRows[0]?.totalCount ?? 0;
|
||||
const candidates = candidateRows.map(({ totalCount: _totalCount, ...candidate }) => candidate);
|
||||
result.checked = candidates.length;
|
||||
result.candidateLimitSkipped = Math.max(0, totalCandidateCount - candidates.length);
|
||||
const lastCandidate = candidates[candidates.length - 1] ?? null;
|
||||
resolvedDependencyWakeBackstopCandidateCursor =
|
||||
result.candidateLimitSkipped > 0 && lastCandidate ? lastCandidate.id : null;
|
||||
if (result.candidateLimitSkipped > 0) {
|
||||
logger.warn(
|
||||
{
|
||||
processed: candidates.length,
|
||||
skipped: result.candidateLimitSkipped,
|
||||
limit: RESOLVED_DEPENDENCY_WAKE_BACKSTOP_CANDIDATE_LIMIT,
|
||||
nextCursor: resolvedDependencyWakeBackstopCandidateCursor,
|
||||
},
|
||||
"issue graph liveness backstop deferred resolved dependency wake candidates past page limit",
|
||||
);
|
||||
}
|
||||
|
||||
const candidatesByCompany = new Map<string, typeof candidates>();
|
||||
for (const candidate of candidates) {
|
||||
const companyCandidates = candidatesByCompany.get(candidate.companyId) ?? [];
|
||||
companyCandidates.push(candidate);
|
||||
candidatesByCompany.set(candidate.companyId, companyCandidates);
|
||||
}
|
||||
|
||||
for (const [companyId, companyCandidates] of candidatesByCompany.entries()) {
|
||||
const readinessMap = await issuesSvc.listDependencyReadiness(
|
||||
companyId,
|
||||
companyCandidates.map((candidate) => candidate.id),
|
||||
);
|
||||
|
||||
for (const candidate of companyCandidates) {
|
||||
const agentId = candidate.assigneeAgentId;
|
||||
if (!agentId) continue;
|
||||
|
||||
const readiness = readinessMap.get(candidate.id);
|
||||
const resolvedBlockerIssueId = readiness?.blockerIssueIds[0] ?? null;
|
||||
if (
|
||||
!readiness ||
|
||||
!readiness.isDependencyReady ||
|
||||
readiness.blockerIssueIds.length === 0 ||
|
||||
!resolvedBlockerIssueId
|
||||
) {
|
||||
result.notReadySkipped += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
const idempotencyKeys = readiness.blockerIssueIds.map((blockerIssueId) =>
|
||||
buildIssueBlockersResolvedWakeIdempotencyKey({
|
||||
dependentIssueId: candidate.id,
|
||||
resolvedBlockerIssueId: blockerIssueId,
|
||||
})
|
||||
);
|
||||
const idempotencyKey = buildIssueBlockersResolvedWakeIdempotencyKey({
|
||||
dependentIssueId: candidate.id,
|
||||
resolvedBlockerIssueId,
|
||||
});
|
||||
const existingWake = await findExistingIssueBlockersResolvedWakeForAnyKey(db, {
|
||||
companyId,
|
||||
idempotencyKeys,
|
||||
});
|
||||
if (existingWake) {
|
||||
result.existingWakeSkipped += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
await hasActiveExecutionPath(companyId, candidate.id, agentId) ||
|
||||
await hasQueuedIssueWake(companyId, candidate.id, agentId)
|
||||
) {
|
||||
result.livePathSkipped += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (await hasPendingWakeInteraction(companyId, candidate.id)) {
|
||||
result.interactionSkipped += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (await isAutomaticRecoverySuppressedByPauseHold(db, companyId, candidate.id, treeControlSvc)) {
|
||||
result.pauseHoldSkipped += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const wake = await deps.enqueueWakeup(agentId, {
|
||||
source: "automation",
|
||||
triggerDetail: "system",
|
||||
reason: ISSUE_BLOCKERS_RESOLVED_WAKE_REASON,
|
||||
payload: {
|
||||
issueId: candidate.id,
|
||||
resolvedBlockerIssueId,
|
||||
blockerIssueIds: readiness.blockerIssueIds,
|
||||
backstop: "issue_graph_liveness_reconciliation",
|
||||
},
|
||||
idempotencyKey,
|
||||
requestedByActorType: "system",
|
||||
requestedByActorId: "issue_graph_liveness_backstop",
|
||||
contextSnapshot: {
|
||||
issueId: candidate.id,
|
||||
taskId: candidate.id,
|
||||
wakeReason: ISSUE_BLOCKERS_RESOLVED_WAKE_REASON,
|
||||
source: "issue_graph_liveness.backstop",
|
||||
resolvedBlockerIssueId,
|
||||
blockerIssueIds: readiness.blockerIssueIds,
|
||||
},
|
||||
});
|
||||
if (!wake) {
|
||||
// enqueueWakeup returns null for normal deferred/skipped paths
|
||||
// such as disabled wake-on-demand or concurrency gating. That is
|
||||
// not an enqueue error, but the backstop still did not heal now.
|
||||
result.deferredOrFailed += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
result.healed += 1;
|
||||
result.issueIds.push(candidate.id);
|
||||
|
||||
await logActivity(db, {
|
||||
companyId,
|
||||
actorType: "system",
|
||||
actorId: "issue_graph_liveness_backstop",
|
||||
agentId,
|
||||
runId: opts?.runId ?? null,
|
||||
action: "issue.blockers_resolved_wake_emitted",
|
||||
entityType: "issue",
|
||||
entityId: candidate.id,
|
||||
details: {
|
||||
source: "issue_graph_liveness.backstop",
|
||||
wakeupRunId: wake.id,
|
||||
idempotencyKey,
|
||||
resolvedBlockerIssueId,
|
||||
blockerIssueIds: readiness.blockerIssueIds,
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
result.deferredOrFailed += 1;
|
||||
result.enqueueFailed += 1;
|
||||
logger.warn(
|
||||
{ err, issueId: candidate.id, agentId, idempotencyKey },
|
||||
"failed to enqueue dependency wake from issue graph liveness backstop",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result.healed > 0) {
|
||||
logger.warn(
|
||||
{ healed: result.healed, issueIds: result.issueIds },
|
||||
"issue graph liveness backstop healed resolved blocked dependency wakes",
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async function reconcileIssueGraphLiveness(opts?: {
|
||||
runId?: string | null;
|
||||
force?: boolean;
|
||||
@@ -4099,6 +4305,17 @@ export function recoveryService(db: Db, deps: { enqueueWakeup: RecoveryWakeup })
|
||||
obsoleteRecoveriesActiveSkipped: obsoleteRecoveryCleanup.activeSkipped,
|
||||
obsoleteRecoveryBlockerRelationsRemoved: obsoleteRecoveryCleanup.blockerRelationsRemoved,
|
||||
doneRecoveryBlockerRelationsRemoved: doneRecoveryBlockerCleanup.blockerRelationsRemoved,
|
||||
dependencyWakeBackstopChecked: 0,
|
||||
dependencyWakesHealed: 0,
|
||||
dependencyWakeExistingSkipped: 0,
|
||||
dependencyWakeLivePathSkipped: 0,
|
||||
dependencyWakeInteractionSkipped: 0,
|
||||
dependencyWakePauseHoldSkipped: 0,
|
||||
dependencyWakeNotReadySkipped: 0,
|
||||
dependencyWakeCandidateLimitSkipped: 0,
|
||||
dependencyWakeDeferredOrFailed: 0,
|
||||
dependencyWakeEnqueueFailed: 0,
|
||||
dependencyWakeIssueIds: [] as string[],
|
||||
issueIds: [] as string[],
|
||||
escalationIssueIds: [] as string[],
|
||||
retiredRecoveryIssueIds: obsoleteRecoveryCleanup.retiredIssueIds,
|
||||
@@ -4109,6 +4326,21 @@ export function recoveryService(db: Db, deps: { enqueueWakeup: RecoveryWakeup })
|
||||
return result;
|
||||
}
|
||||
|
||||
const dependencyWakeBackstop = await reconcileResolvedDependencyWakeBackstop({
|
||||
runId: opts?.runId ?? null,
|
||||
});
|
||||
result.dependencyWakeBackstopChecked = dependencyWakeBackstop.checked;
|
||||
result.dependencyWakesHealed = dependencyWakeBackstop.healed;
|
||||
result.dependencyWakeExistingSkipped = dependencyWakeBackstop.existingWakeSkipped;
|
||||
result.dependencyWakeLivePathSkipped = dependencyWakeBackstop.livePathSkipped;
|
||||
result.dependencyWakeInteractionSkipped = dependencyWakeBackstop.interactionSkipped;
|
||||
result.dependencyWakePauseHoldSkipped = dependencyWakeBackstop.pauseHoldSkipped;
|
||||
result.dependencyWakeNotReadySkipped = dependencyWakeBackstop.notReadySkipped;
|
||||
result.dependencyWakeCandidateLimitSkipped = dependencyWakeBackstop.candidateLimitSkipped;
|
||||
result.dependencyWakeDeferredOrFailed = dependencyWakeBackstop.deferredOrFailed;
|
||||
result.dependencyWakeEnqueueFailed = dependencyWakeBackstop.enqueueFailed;
|
||||
result.dependencyWakeIssueIds = dependencyWakeBackstop.issueIds;
|
||||
|
||||
for (const finding of findings) {
|
||||
if (!isLivenessFindingInsideAutoRecoveryLookback(finding, cutoff, updatedAtByIssueKey)) {
|
||||
result.skippedOutsideLookback += 1;
|
||||
|
||||
Reference in new issue
Block a user