mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 02:30:43 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The recovery service restores execution when a task loses its live path. > - The service retries the original agent for a limited number of attempts. > - The old fallback could select a manager or an executive and wake that agent. > - That fallback changed the effective recovery owner without a board decision. > - This pull request keeps the source owner and gives the exhausted recovery decision to the board. > - The benefit is a clear ownership rule with no automatic task takeover. ## Linked Issues or Issue Description Refs: #11807 Refs: #11817 **What existing behavior does this improve?** This improves stranded-task recovery in the server and the recovery action card in the board UI. **Subsystem affected** Cross-cutting: server recovery orchestration, recovery observability, board UI, and execution documentation. **Current behavior** Paperclip retries the original agent for a limited number of attempts. After the retry limit, it can select a manager, task creator, CTO, or CEO as a recovery owner. It can then wake that substitute agent. The source task keeps its assignee, but the automatic substitute wake creates an implicit takeover path. **Proposed behavior** Paperclip keeps the limited retry path for the original agent. If recovery is exhausted or unsafe, Paperclip creates one board-owned source recovery action. It keeps both source assignee fields. It does not wake a substitute agent. The board can repair, retry the original owner, explicitly reassign, or resolve the task. **Reason and benefit** Source task ownership must remain stable until a person or an approved policy changes it. The new rule removes implicit manager and executive takeover. It also gives operators clear evidence through the `board_escalation_no_takeover_v1` routing marker. **Breaking changes** Automatic recovery no longer wakes a manager or executive after the original-agent retry limit. Existing active agent-owned recovery actions remain visible and can resolve. Paperclip does not schedule a new takeover wake for those legacy actions. ## What Changed - Route exhausted and unsafe stranded recovery to a board-owned source action. - Preserve agent and user assignee fields during automatic escalation. - Keep limited same-agent continuity repair and provider quota monitoring. - Stop new manager, creator, CTO, and CEO recovery wakes. - Keep legacy agent-owned recovery actions readable and resolvable. - Add the routing marker to new board escalation evidence and observability. - Update recovery notices, the board UI card, tests, and execution documentation. ## Verification - Run `pnpm -r typecheck`. - Run `pnpm build`. - Run `pnpm check:token-gates`. - Run `pnpm --filter @paperclipai/server exec vitest run src/__tests__/heartbeat-process-recovery.test.ts`. - Run `pnpm --filter @paperclipai/server exec vitest run src/__tests__/heartbeat-workspace-branch-containment.test.ts`. - Run the focused recovery and UI Vitest files changed by this pull request. - Confirm that a paused or over-budget source owner creates one board action, keeps the source assignee, and creates no substitute wake. ## Risks - Operators must now make the final recovery decision after the original-agent limit. - Legacy agent-owned actions use their stored contract. This avoids a rollout-time ownership rewrite. - No database migration or API response shape changes are included. - The tests cover concurrent escalation, paused and over-budget owners, legacy actions, provider quota monitoring, and UI presentation. > 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 OpenAI Codex with GPT-5. The hosted exact model revision and context window are not exposed. Reasoning, tool use, and code execution were enabled. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge Co-authored-by: Paperclip <noreply@paperclip.ing>
374 lines
13 KiB
TypeScript
374 lines
13 KiB
TypeScript
import { and, eq, gte, sql } from "drizzle-orm";
|
|
import type { Db } from "@paperclipai/db";
|
|
import { heartbeatRuns, issueRecoveryActions, issues } from "@paperclipai/db";
|
|
|
|
// Default alert threshold: the recovery rate that a regression like the 07-06
|
|
// week (3.26% of runs) blew past while nobody noticed by feel. See the plan on
|
|
// PAP-14080 (Phase 3) and the retrospective on PAP-14098.
|
|
export const DEFAULT_RECOVERY_RATE_THRESHOLD_PERCENT = 2;
|
|
const DEFAULT_WINDOW_WEEKS = 8;
|
|
// Upper bound on the reporting window. Caps the per-week array allocation so an
|
|
// attacker-supplied `weeks` query param can't trigger an unbounded allocation
|
|
// (~2 years of weekly buckets is far beyond any real dashboard use).
|
|
export const MAX_WINDOW_WEEKS = 104;
|
|
|
|
// Weeks are ISO/Monday-anchored to match the retrospective table (2026-06-01 is
|
|
// a Monday). Postgres `date_trunc('week', ...)` is Monday-based, so this keeps
|
|
// the live report aligned with the numbers in the plan document.
|
|
|
|
export type WeeklyRecoveryRate = {
|
|
/** Monday (UTC) of the week, `YYYY-MM-DD`. */
|
|
weekStart: string;
|
|
runs: number;
|
|
recoveryActions: number;
|
|
/** recoveryActions / runs, as a percentage (0 when there are no runs). */
|
|
ratePercent: number;
|
|
};
|
|
|
|
export type RecoveryRateAlert = {
|
|
thresholdPercent: number;
|
|
/** True when any complete week in the window exceeded the threshold. */
|
|
breached: boolean;
|
|
breachedWeeks: WeeklyRecoveryRate[];
|
|
latestWeek: WeeklyRecoveryRate | null;
|
|
latestWeekBreached: boolean;
|
|
};
|
|
|
|
export type RecoveryCauseGroup = {
|
|
cause: string;
|
|
latestRunErrorCode: string;
|
|
count: number;
|
|
activeCount: number;
|
|
resolvedCount: number;
|
|
cancelledCount: number;
|
|
};
|
|
|
|
export type RecoveryHandoffSummary = {
|
|
/** Historical agent takeovers (recovery owner != original assignee) that resolved. */
|
|
resolvedTakeovers: number;
|
|
handedBack: number;
|
|
ownerCompleted: number;
|
|
otherTakeover: number;
|
|
/** Original agent recovered its own issue (owner == original assignee). */
|
|
selfRecovery: number;
|
|
boardOwned: number;
|
|
activeTakeovers: number;
|
|
/** handedBack / (handedBack + ownerCompleted); null when no resolved takeovers. */
|
|
handedBackRatio: number | null;
|
|
ownerCompletedRatio: number | null;
|
|
};
|
|
|
|
export type RecoveryCauseRouting = {
|
|
cause: string;
|
|
total: number;
|
|
active: number;
|
|
/** Original agent recovered its own issue and resolved it. */
|
|
retriedByOriginalSucceeded: number;
|
|
handedBack: number;
|
|
ownerCompleted: number;
|
|
escalated: number;
|
|
falsePositive: number;
|
|
cancelled: number;
|
|
};
|
|
|
|
export type RecoveryObservabilityReport = {
|
|
companyId: string;
|
|
generatedAt: string;
|
|
window: { weeks: number; since: string };
|
|
thresholdPercent: number;
|
|
weekly: WeeklyRecoveryRate[];
|
|
alert: RecoveryRateAlert;
|
|
byCause: RecoveryCauseGroup[];
|
|
handoff: RecoveryHandoffSummary;
|
|
perCauseRouting: RecoveryCauseRouting[];
|
|
};
|
|
|
|
export type HandoffClass =
|
|
| "self_recovery"
|
|
| "handed_back"
|
|
| "owner_completed"
|
|
| "board_owned"
|
|
| "active"
|
|
| "other";
|
|
|
|
type RecoveryActionFacts = {
|
|
status: string;
|
|
outcome: string | null;
|
|
ownerAgentId: string | null;
|
|
returnOwnerAgentId: string | null;
|
|
finalAssigneeAgentId: string | null;
|
|
finalIssueStatus: string | null;
|
|
};
|
|
|
|
const ACTIVE_STATUSES = new Set(["active", "escalated"]);
|
|
const TERMINAL_ISSUE_STATUSES = new Set(["done"]);
|
|
|
|
/**
|
|
* Classify a recovery action by who ended up owning the deliverable work.
|
|
*
|
|
* The plan's `handed_back` vs `owner_completed` outcomes were never added to the
|
|
* outcome vocabulary (recovery track 1 kept `restored`/`cancelled`/…), so we
|
|
* derive the distinction from the durable relationship between the recovery
|
|
* owner, the original assignee (`returnOwnerAgentId`), and where the source
|
|
* issue actually landed. This directly measures the product goal — "managers
|
|
* doing the work becomes rare".
|
|
*/
|
|
export function classifyRecoveryHandoff(facts: RecoveryActionFacts): HandoffClass {
|
|
if (ACTIVE_STATUSES.has(facts.status)) return "active";
|
|
if (!facts.ownerAgentId) return "board_owned";
|
|
// Original agent recovered its own issue — the ideal per-cause routing, not a takeover.
|
|
if (facts.ownerAgentId === facts.returnOwnerAgentId) return "self_recovery";
|
|
// Genuine takeover: recovery owner differs from the original assignee.
|
|
const landedElsewhere =
|
|
facts.finalAssigneeAgentId != null && facts.finalAssigneeAgentId !== facts.ownerAgentId;
|
|
if (landedElsewhere) return "handed_back";
|
|
const ownerKept = facts.finalAssigneeAgentId === facts.ownerAgentId;
|
|
if (ownerKept && facts.finalIssueStatus && TERMINAL_ISSUE_STATUSES.has(facts.finalIssueStatus)) {
|
|
return "owner_completed";
|
|
}
|
|
return "other";
|
|
}
|
|
|
|
/**
|
|
* Pure alert evaluation over weekly rates. Kept independent of the database so
|
|
* the 2%-crossing regression case can be tested with synthetic data.
|
|
*/
|
|
export function evaluateRecoveryRateAlert(
|
|
weekly: WeeklyRecoveryRate[],
|
|
thresholdPercent: number = DEFAULT_RECOVERY_RATE_THRESHOLD_PERCENT,
|
|
): RecoveryRateAlert {
|
|
const breachedWeeks = weekly.filter((week) => week.ratePercent > thresholdPercent);
|
|
const latestWeek = weekly.length > 0 ? weekly[weekly.length - 1]! : null;
|
|
return {
|
|
thresholdPercent,
|
|
breached: breachedWeeks.length > 0,
|
|
breachedWeeks,
|
|
latestWeek,
|
|
latestWeekBreached: latestWeek != null && latestWeek.ratePercent > thresholdPercent,
|
|
};
|
|
}
|
|
|
|
function round2(value: number): number {
|
|
return Math.round(value * 100) / 100;
|
|
}
|
|
|
|
function utcWeekStart(now: Date, weeksAgo: number): Date {
|
|
const utcMidnight = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate());
|
|
const dayOfWeek = new Date(utcMidnight).getUTCDay(); // 0 = Sunday
|
|
const mondayOffset = (dayOfWeek + 6) % 7; // days since Monday
|
|
const thisMonday = utcMidnight - mondayOffset * 24 * 60 * 60 * 1000;
|
|
return new Date(thisMonday - weeksAgo * 7 * 24 * 60 * 60 * 1000);
|
|
}
|
|
|
|
export function recoveryObservabilityService(db: Db) {
|
|
async function report(
|
|
companyId: string,
|
|
opts?: { now?: Date; weeks?: number; thresholdPercent?: number },
|
|
): Promise<RecoveryObservabilityReport> {
|
|
const now = opts?.now ?? new Date();
|
|
const weeks = Math.min(
|
|
MAX_WINDOW_WEEKS,
|
|
Math.max(1, Math.floor(opts?.weeks ?? DEFAULT_WINDOW_WEEKS)),
|
|
);
|
|
const thresholdPercent = opts?.thresholdPercent ?? DEFAULT_RECOVERY_RATE_THRESHOLD_PERCENT;
|
|
const since = utcWeekStart(now, weeks - 1);
|
|
const sinceIso = since.toISOString();
|
|
|
|
// Weekly run volume (denominator) — normalizes the recovery rate per run.
|
|
const runRows = (await db.execute(sql`
|
|
SELECT
|
|
to_char(date_trunc('week', ${heartbeatRuns.createdAt} AT TIME ZONE 'UTC'), 'YYYY-MM-DD') AS week_start,
|
|
count(*)::int AS runs
|
|
FROM ${heartbeatRuns}
|
|
WHERE ${heartbeatRuns.companyId} = ${companyId}
|
|
AND ${heartbeatRuns.createdAt} >= ${sinceIso}::timestamptz
|
|
GROUP BY week_start
|
|
`)) as unknown as Iterable<{ week_start: string; runs: number | string }>;
|
|
|
|
// Weekly recovery-action volume (numerator).
|
|
const actionRows = (await db.execute(sql`
|
|
SELECT
|
|
to_char(date_trunc('week', ${issueRecoveryActions.createdAt} AT TIME ZONE 'UTC'), 'YYYY-MM-DD') AS week_start,
|
|
count(*)::int AS actions
|
|
FROM ${issueRecoveryActions}
|
|
WHERE ${issueRecoveryActions.companyId} = ${companyId}
|
|
AND ${issueRecoveryActions.createdAt} >= ${sinceIso}::timestamptz
|
|
GROUP BY week_start
|
|
`)) as unknown as Iterable<{ week_start: string; actions: number | string }>;
|
|
|
|
const runsByWeek = new Map<string, number>();
|
|
for (const row of runRows) runsByWeek.set(String(row.week_start), Number(row.runs));
|
|
const actionsByWeek = new Map<string, number>();
|
|
for (const row of actionRows) actionsByWeek.set(String(row.week_start), Number(row.actions));
|
|
|
|
const weekly: WeeklyRecoveryRate[] = Array.from({ length: weeks }, (_, index) => {
|
|
const weekStart = utcWeekStart(now, weeks - 1 - index).toISOString().slice(0, 10);
|
|
const runs = runsByWeek.get(weekStart) ?? 0;
|
|
const recoveryActions = actionsByWeek.get(weekStart) ?? 0;
|
|
return {
|
|
weekStart,
|
|
runs,
|
|
recoveryActions,
|
|
ratePercent: runs > 0 ? round2((recoveryActions / runs) * 100) : 0,
|
|
};
|
|
});
|
|
|
|
const alert = evaluateRecoveryRateAlert(weekly, thresholdPercent);
|
|
|
|
// Cause + latestRunErrorCode taxonomy (from the action's evidence snapshot).
|
|
const causeRows = (await db.execute(sql`
|
|
SELECT
|
|
${issueRecoveryActions.cause} AS cause,
|
|
coalesce(${issueRecoveryActions.evidence} ->> 'latestRunErrorCode', '(none)') AS error_code,
|
|
count(*)::int AS count,
|
|
count(*) FILTER (WHERE ${issueRecoveryActions.status} IN ('active', 'escalated'))::int AS active_count,
|
|
count(*) FILTER (WHERE ${issueRecoveryActions.status} = 'resolved')::int AS resolved_count,
|
|
count(*) FILTER (WHERE ${issueRecoveryActions.status} = 'cancelled')::int AS cancelled_count
|
|
FROM ${issueRecoveryActions}
|
|
WHERE ${issueRecoveryActions.companyId} = ${companyId}
|
|
AND ${issueRecoveryActions.createdAt} >= ${sinceIso}::timestamptz
|
|
GROUP BY cause, error_code
|
|
ORDER BY count DESC
|
|
`)) as unknown as Iterable<{
|
|
cause: string;
|
|
error_code: string;
|
|
count: number | string;
|
|
active_count: number | string;
|
|
resolved_count: number | string;
|
|
cancelled_count: number | string;
|
|
}>;
|
|
|
|
const byCause: RecoveryCauseGroup[] = Array.from(causeRows).map((row) => ({
|
|
cause: String(row.cause),
|
|
latestRunErrorCode: String(row.error_code),
|
|
count: Number(row.count),
|
|
activeCount: Number(row.active_count),
|
|
resolvedCount: Number(row.resolved_count),
|
|
cancelledCount: Number(row.cancelled_count),
|
|
}));
|
|
|
|
// Hand-back accounting + per-cause routing. Recovery actions are inherently
|
|
// low-volume (they are the exception path), so we join to the source issue
|
|
// and classify each action in TS rather than encoding the derivation in SQL.
|
|
const facts = await db
|
|
.select({
|
|
cause: issueRecoveryActions.cause,
|
|
status: issueRecoveryActions.status,
|
|
outcome: issueRecoveryActions.outcome,
|
|
ownerAgentId: issueRecoveryActions.ownerAgentId,
|
|
returnOwnerAgentId: issueRecoveryActions.returnOwnerAgentId,
|
|
finalAssigneeAgentId: issues.assigneeAgentId,
|
|
finalIssueStatus: issues.status,
|
|
})
|
|
.from(issueRecoveryActions)
|
|
.innerJoin(issues, eq(issues.id, issueRecoveryActions.sourceIssueId))
|
|
.where(
|
|
and(
|
|
eq(issueRecoveryActions.companyId, companyId),
|
|
gte(issueRecoveryActions.createdAt, since),
|
|
),
|
|
);
|
|
|
|
const handoff: RecoveryHandoffSummary = {
|
|
resolvedTakeovers: 0,
|
|
handedBack: 0,
|
|
ownerCompleted: 0,
|
|
otherTakeover: 0,
|
|
selfRecovery: 0,
|
|
boardOwned: 0,
|
|
activeTakeovers: 0,
|
|
handedBackRatio: null,
|
|
ownerCompletedRatio: null,
|
|
};
|
|
|
|
const routingByCause = new Map<string, RecoveryCauseRouting>();
|
|
const routingFor = (cause: string): RecoveryCauseRouting => {
|
|
let entry = routingByCause.get(cause);
|
|
if (!entry) {
|
|
entry = {
|
|
cause,
|
|
total: 0,
|
|
active: 0,
|
|
retriedByOriginalSucceeded: 0,
|
|
handedBack: 0,
|
|
ownerCompleted: 0,
|
|
escalated: 0,
|
|
falsePositive: 0,
|
|
cancelled: 0,
|
|
};
|
|
routingByCause.set(cause, entry);
|
|
}
|
|
return entry;
|
|
};
|
|
|
|
for (const row of facts) {
|
|
const klass = classifyRecoveryHandoff(row);
|
|
const routing = routingFor(String(row.cause));
|
|
routing.total += 1;
|
|
|
|
if (klass === "active") {
|
|
routing.active += 1;
|
|
if (row.status === "escalated") routing.escalated += 1;
|
|
if (!row.ownerAgentId) {
|
|
handoff.boardOwned += 1;
|
|
} else if (row.ownerAgentId !== row.returnOwnerAgentId) {
|
|
handoff.activeTakeovers += 1;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
// Routing verification counters (resolved actions).
|
|
if (row.status === "escalated") routing.escalated += 1;
|
|
if (row.outcome === "false_positive") routing.falsePositive += 1;
|
|
if (row.status === "cancelled" && row.outcome !== "false_positive") routing.cancelled += 1;
|
|
|
|
switch (klass) {
|
|
case "self_recovery":
|
|
handoff.selfRecovery += 1;
|
|
if (row.status === "resolved") routing.retriedByOriginalSucceeded += 1;
|
|
break;
|
|
case "handed_back":
|
|
handoff.handedBack += 1;
|
|
handoff.resolvedTakeovers += 1;
|
|
routing.handedBack += 1;
|
|
break;
|
|
case "owner_completed":
|
|
handoff.ownerCompleted += 1;
|
|
handoff.resolvedTakeovers += 1;
|
|
routing.ownerCompleted += 1;
|
|
break;
|
|
case "board_owned":
|
|
handoff.boardOwned += 1;
|
|
break;
|
|
default:
|
|
if (row.ownerAgentId && row.ownerAgentId !== row.returnOwnerAgentId) {
|
|
handoff.otherTakeover += 1;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
const decided = handoff.handedBack + handoff.ownerCompleted;
|
|
if (decided > 0) {
|
|
handoff.handedBackRatio = round2((handoff.handedBack / decided) * 100);
|
|
handoff.ownerCompletedRatio = round2((handoff.ownerCompleted / decided) * 100);
|
|
}
|
|
|
|
const perCauseRouting = Array.from(routingByCause.values()).sort((a, b) => b.total - a.total);
|
|
|
|
return {
|
|
companyId,
|
|
generatedAt: now.toISOString(),
|
|
window: { weeks, since: sinceIso.slice(0, 10) },
|
|
thresholdPercent,
|
|
weekly,
|
|
alert,
|
|
byCause,
|
|
handoff,
|
|
perCauseRouting,
|
|
};
|
|
}
|
|
|
|
return { report };
|
|
}
|