The open-source app everyone uses to manage agents at work
TypeScript acpxagent-orchestrationagent-skillsagentic-aiai-agentsai-engineeringai-governanceai-toolsautonomous-agentsclaudeclaude-codeclaude-skillscodexhermesllmmcpmulti-agentopenclawproject-managementworkflow-automation
import { createHash } from "node:crypto";
import type { IssueReviewAttention } from "@paperclipai/shared";
import { boundExternalChatProvider } from "../native-runtime/external-chat-provider.js";
import { extractWakeCommentIds } from "../../modules/run-dispatch/index.js";
import { withRecoveryContext } from "./status-only-context.js";
export const ISSUE_REVIEW_PATH_LOST_WAKE_REASON = "issue_review_path_lost";
export const REVIEW_PATH_RECOVERY_INSTRUCTION =
"This issue is still in review but its last maintained review path was consumed. Restore a reviewer, interaction, approval, monitor, or other durable waiting path, or choose an explicit disposition. If an async check is still pending, persist a one-shot issue monitor before ending this run and report its scheduled check time. A promise to check later or a background watcher is not a maintained review path. This is the only automatic review-path recovery wake for this fingerprint; ending without a maintained path will require a board decision.";
const REVIEW_PATH_RECOVERY_IDEMPOTENCY_INDEX = "agent_wakeup_requests_review_path_recovery_idempotency_uq";
function readNonEmptyString(value: unknown) {
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
}
export function buildReviewPathRecoveryDisposition(context: Record<string, unknown>) {
if (!isIssueReviewPathRecoveryRun(context)) return null;
return {
attempt: 1,
maxAttempts: 1,
sourceRunId: readNonEmptyString(context.sourceRunId),
instruction: REVIEW_PATH_RECOVERY_INSTRUCTION,
};
}
export function isIssueReviewPathRecoveryRun(contextSnapshot: Record<string, unknown> | null | undefined) {
return readNonEmptyString(contextSnapshot?.wakeReason) === ISSUE_REVIEW_PATH_LOST_WAKE_REASON
|| contextSnapshot?.reviewPathRecoveryAttempt === 1;
}
export function reviewPathConsumedRefFromRun(input: {
runId: string;
issueId: string;
contextSnapshot: Record<string, unknown> | null | undefined;
}) {
const context = input.contextSnapshot ?? {};
return readNonEmptyString(context.reviewPathConsumedRef)
?? readNonEmptyString(context.interactionId)
?? readNonEmptyString(context.approvalId)
?? (readNonEmptyString(context.wakeReason)?.startsWith("issue_monitor")
? `monitor:${input.issueId}:${readNonEmptyString(context.clearReason) ?? "cleared"}:${String(context.monitorAttemptCount ?? "unknown")}`
: null)
?? input.runId;
}
export function buildIssueReviewPathLostIdempotencyKey(input: {
issueId: string;
consumedPathRef: string;
}) {
const fingerprint = createHash("sha256").update(input.consumedPathRef).digest("hex").slice(0, 24);
return `${ISSUE_REVIEW_PATH_LOST_WAKE_REASON}:${input.issueId}:${fingerprint}`;
}
export function isReviewPathRecoveryIdempotencyConflict(error: unknown): boolean {
let current: unknown = error;
for (let depth = 0; depth < 4 && current && typeof current === "object"; depth += 1) {
const candidate = current as {
code?: unknown;
constraint?: unknown;
constraint_name?: unknown;
message?: unknown;
cause?: unknown;
};
const constraint = candidate.constraint ?? candidate.constraint_name;
if (
candidate.code === "23505"
&& (
constraint === REVIEW_PATH_RECOVERY_IDEMPOTENCY_INDEX
|| (typeof candidate.message === "string" && candidate.message.includes(REVIEW_PATH_RECOVERY_IDEMPOTENCY_INDEX))
)
) {
return true;
}
current = candidate.cause;
}
return false;
}
export type IssueReviewPathRecoveryDecision =
| {
kind: "enqueue";
idempotencyKey: string;
payload: Record<string, unknown>;
contextSnapshot: Record<string, unknown>;
}
| { kind: "exhausted" }
| { kind: "skip"; reason: string };
export function decideIssueReviewPathRecovery(input: {
issueId: string;
sourceRunId: string | null;
assigneeAgentId: string | null;
contextSnapshot: Record<string, unknown> | null | undefined;
reviewAttention: IssueReviewAttention;
existingWake: boolean;
}): IssueReviewPathRecoveryDecision {
if (!input.assigneeAgentId) return { kind: "skip", reason: "review issue has no agent assignee" };
if (input.reviewAttention.state !== "stalled") {
return { kind: "skip", reason: "review issue still has a maintained path" };
}
const context = input.contextSnapshot ?? {};
if (isIssueReviewPathRecoveryRun(context)) {
return { kind: "exhausted" };
}
const consumedPathRef = reviewPathConsumedRefFromRun({
runId: input.sourceRunId ?? input.issueId,
issueId: input.issueId,
contextSnapshot: context,
});
const idempotencyKey = buildIssueReviewPathLostIdempotencyKey({
issueId: input.issueId,
consumedPathRef,
});
if (input.existingWake) return { kind: "skip", reason: "review-path recovery wake already exists" };
const source = readNonEmptyString(context.source) ?? "heartbeat.review_path_disposition";
const chatCommentIds = boundExternalChatProvider(source) ? extractWakeCommentIds(context) : [];
const payload = withRecoveryContext({
issueId: input.issueId,
taskId: input.issueId,
sourceIssueId: input.issueId,
...(input.sourceRunId ? { sourceRunId: input.sourceRunId } : {}),
reviewPathLost: true,
reviewPathConsumedRef: consumedPathRef,
reviewPathRecoveryAttempt: 1,
maxReviewPathRecoveryAttempts: 1,
instruction: REVIEW_PATH_RECOVERY_INSTRUCTION,
}, "normal_model");
return {
kind: "enqueue",
idempotencyKey,
payload,
contextSnapshot: withRecoveryContext({
...payload,
wakeReason: ISSUE_REVIEW_PATH_LOST_WAKE_REASON,
source,
// Keep the admitted message references, not the source run's authority.
// Dispatch must prove the new run owns this task and recheck current
// endpoint, conversation, and principal access for the entire batch.
...(chatCommentIds.length > 0 ? {
wakeCommentIds: chatCommentIds,
wakeCommentId: chatCommentIds.at(-1),
commentId: chatCommentIds.at(-1),
} : {}),
}, "normal_model"),
};
}