mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-25 09:41:04 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The native runner connects task messages, provider execution, and task outcomes. > - First-time user tests exposed gaps in recovery, completion permissions, message delivery, and Stop behavior. > - These gaps left usable output hidden, completed work waiting for bookkeeping, or safe work unable to continue. > - This pull request fixes the shared lifecycle and receipt paths while preserving process ownership and action checks. > - Users can continue work with accurate task state and durable messages. ## Linked Issues or Issue Description **What happened?** A stopped local Codex execution could remain blocked even after its processes had stopped and its complete transcript proved that no external action needed replay. Claude under Conservative permissions could fail to call task completion tools. Recovery could reuse an assistant item ID and overwrite prior output. A delivered comment could remain marked uncertain after navigation. Stop could look like Pause or a new recovery incident. Workspace contention could look like cancellation. A direct reply reopening Done could enter a clarification loop. **Expected behavior** Recover automatically only with verified termination and complete action receipts. Preserve answers and messages. Keep task completion available under Conservative permissions without broad tool access. Show crashes as Blocked, actual human decisions as In Review, and ordinary workspace contention as waiting. Stop the current response and allow a new direction. **Steps to reproduce** 1. Create ordinary response tasks with local Codex and Claude Code, then send follow-up messages through the task composer. 2. Interrupt a disposable local Codex runner during text-only work. Verify automatic continuation and retained output. 3. Stop a response, send a new request, answer a clarification, and reopen completed work with another message. 4. Navigate or reload while a comment submission is pending. Confirm the exact persisted request receipt settles it without removing newer draft text. 5. Run two tasks in a shared Daytona workspace. Confirm waiting does not appear as failure. **Paperclip version or commit** Initial acceptance baseline: `c9021c6721f91e2c74bd9fee9d3fd41c999d17b7`. Current integration base: `6cef9743c`. Both operator-interruption and workspace-waiting guards are preserved; native restart and legacy permission rules remain documented. **Deployment mode** An isolated source-built test-drive instance, with real local Codex and Claude Code providers and disposable Daytona environments. Related work: #13314, #13316, #13327, #13344, #13239, #13254, #13163. This PR addresses additional failures from ordinary task journeys, including controller restart handoff and repeated warm sandbox setup. Historical task status reconciliation is excluded. ## What Changed - Persist runner ownership immediately at spawn and resume an explicitly adopted runner even when the controller crashed before the first driver checkpoint. Detach the controller safely across graceful restarts, including session startup. Prevent an old finalizer from suspending or signaling an adopted runner. Checkpoint idle warm sessions before shutdown. Preserve the same run and queued follow-up messages. - Scope saved legacy queue successor checks to the queue owner while preserving ordinary task locks, operator identity, assignment gates, and exactly-once delivery. - Preserve managed Codex credential files when an old session is detached for restart; normal owned cleanup still copies refreshed auth back and removes the scoped copy. - Reuse the bound warm shared sandbox and fully verify an existing staged provider pack before using it. This avoids repeated uploads when the pack is already valid. - Add a narrow local Codex replacement path with stopped-process proof, a closed transcript inventory, exact completion receipts, and fresh-session lineage. Preserve no-replay holds when evidence is incomplete. Recovery may clear only the same run's recorded Blocked status version; manual re-blocking and dependency changes invalidate that receipt, while queued comments do not. Later blocks stop scheduled, queued, and final dispatch; queued/final checks re-read dependencies even when the task status stays In Progress. - Permit only task delivery and human-input tools through the isolated Claude runner's exact task bridge. - Scope assistant item identity to the provider turn and ignore only authority-free Codex skill-change notifications during startup. - Reconcile composer submissions by client request ID across response loss, navigation, and reload. Retain text typed during delivery. - Keep acknowledged run-only Stop neutral and show workspace contention as waiting. Project exhausted native failures as Blocked. - Restore the guarded task-page retry action for failed legacy runs, including the server-supported explicit new-attempt path for stopped conversation adapters. Preserve native/process recovery holds and avoid promising Retry while a decision or execution gate hides it. - Refresh delivered artifacts and handle direct user replies that reopen completed work without a clarification loop. - Check the embedded PostgreSQL PID, data directory, and actual port before connecting or migrating. - Document accepted behavior and add focused regressions at lifecycle, route, transcript, and UI boundaries. ## Verification - Final head `fece606ac2` passes the complete GitHub CI matrix: **34 green checks, two expected Storybook skips, no failures or pending checks**, including `ci / verify`, `ci / e2e`, full runner verification, typecheck, build, every server/workspace shard, and all browser shards. [CI run](https://github.com/paperclipai/paperclip/actions/runs/34727183287). Greptile is **5/5 with no open findings**. The final two commits only refine test fixtures; both affected suites pass 24/24 locally and in CI, with server typecheck green. - Complete local Vitest coverage uses the canonical groups/shards: all 635 general server suites, all 145 serialized suites, and all workspace packages. The aggregate began on `0a8001c18` while the final queue fix arrived: 23,903 passed, five failed, 87 skipped. The five port/socket/timing failures passed unchanged in follow-ups (60 tests in the exposure/file suites and 412 tests covering the serialized failures and unrun tails). The final queue/operator-identity suites separately passed 52/52. This is aggregate coverage plus explicit reruns, not a pristine single-command final-head run. - After integration with current master, queue/operator-identity/continuation suites passed 162/162 and affected UI suites passed 140/140. ACP Stop/continuation and legacy task/Inbox/message browser suites passed 9/9, including both task recovery Retry and thread Try again, automatic saved-message delivery, exactly one new run, Done, and retained output after reload. The default process Stop/Pause/Resume browser case passed (the native-provider case is opt-in and skipped by default). The complete Board attachment/receipt browser suite passed 11/11 on a disposable instance, covering both composers, exact receipts after lost responses, no replay, bound attachments, and newer drafts after reload. - Blocking-intent regressions cover pre-existing Blocked, a mismatched run/cause, an explicit manual re-block, changed dependencies, a queued comment after failure, and a block arriving between scheduling and provider dispatch. The negative cases reproduced before the fix. All 478 affected executor/recovery/dispatch tests passed; both database suites ran separately after availability-probe skips in the first combined command. The final late-dependency check passed all 143 affected recovery/dispatch tests (zero skips) after two new negative cases reproduced the bug. - Focused runtime regressions cover awaited runner ownership publication, authenticated adoption before the first checkpoint, old-finalizer detachment, idle and busy warm-session shutdown, rejected checkpoint propagation, provider-pack verification, and managed-Codex credential preservation. Four managed credential detachment cases reproduced the bug before the fix; normal owned cleanup still succeeds exactly once. - Live local Claude: SIGKILL 2.6 seconds into startup recovered the same run automatically in 53 seconds, then a normal follow-up completed in 24 seconds. SIGTERM 2.5 seconds into startup preserved the same run (54 seconds) and its queued follow-up (21 seconds). Answers remained visible and the task reached Done. - Live Claude Daytona: a warm follow-up retained its sandbox and fell from 121 seconds to 44 seconds. A separate cold turn took 127 seconds; after controller shutdown and checkpointing, its follow-up completed in 33 seconds with the same sandbox, workspace, native session, and runner. Both answers remained visible and the task was Done. - Other live journeys covered task completion and follow-up with local and Daytona Codex, local Codex crash recovery, Stop then new direction, clarification response, live artifact refresh, and shared-workspace waiting. - Validation limits: the opt-in native composer Stop/Pause→subtree Resume fixture exposes terminal/result ordering and subtree-cancellation attribution bugs that can leave a child task blocked; that new finding is assigned to a separate follow-up and is not claimed fixed here. Default CI skips this optional native-provider fixture. Managed-Codex credential handoff and the queue-agent integration use automated regression evidence. Cold custom provider-pack uploads still add startup latency. ## Risks - Automatic replacement remains deliberately narrow: local Codex, verified stopped identities, unchanged retained state, and a complete text/completion-only turn. Unknown actions, partial history, or changed ownership remain blocked. - Claude completion permission handling changes an upstream package patch. The exact isolated task bridge must remain pinned; unrelated tools keep their existing permissions. - New task failure projection changes user-visible status. No historical status backfill or database migration is included. - This is a broad lifecycle fix across server and UI. Live proof covers graceful local Claude restart during startup and idle Claude Daytona session recovery across controller shutdown. Live abrupt SIGKILL during local Claude startup also recovered the same run. Unknown ownership or missing action evidence still blocks reuse. Cold custom provider-pack uploads still add startup latency; this change avoids unnecessary repeat uploads. ## Model Used OpenAI GPT-6 (Codex), with reasoning, code execution, browser automation, and tool use. The exact hosted model ID and context window are not exposed in this task. ## 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>
1034 lines
48 KiB
TypeScript
1034 lines
48 KiB
TypeScript
import { createRunDispatch, deriveCommentId } from "../../modules/run-dispatch/index.js";
|
|
import { buildExecutionContinuation } from "../execution-continuation.js";
|
|
import { issueService } from "../issues.js";
|
|
import { activityService } from "../activity.js";
|
|
import { buildPaperclipWakePayload, heartbeatService } from "../heartbeat.js";
|
|
import { legacyExecutionNeedsReconciliation, terminalizeLegacyExecution } from "../legacy-execution-recovery.js";
|
|
import { deliverExecutionStatuses } from "../execution-status-delivery.js";
|
|
import { publishLiveEvent } from "../live-events.js";
|
|
import {
|
|
settleUnrecoverableExecutions,
|
|
validateExecutionReconciliation,
|
|
markExecutionReconciliation,
|
|
deliverReconciledExecutions,
|
|
} from "../execution-recovery-resolution.js";
|
|
import { randomUUID } from "node:crypto";
|
|
import { appendHeartbeatRunEvent } from "../heartbeat-run-events.js";
|
|
import { tmpdir } from "node:os";
|
|
import { and, eq, inArray } from "drizzle-orm";
|
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
import {
|
|
agents,
|
|
companies,
|
|
createDb,
|
|
heartbeatRunEvents,
|
|
heartbeatRuns,
|
|
issueRecoveryActions,
|
|
issueComments,
|
|
issues,
|
|
nativeRunFinalizations,
|
|
} from "@paperclipai/db";
|
|
import {
|
|
getEmbeddedPostgresTestSupport,
|
|
startEmbeddedPostgresTestDatabase,
|
|
} from "../../__tests__/helpers/embedded-postgres.js";
|
|
import { reconcileSafeNativeReplacements } from "./native-safe-replacement.js";
|
|
import { reconcileAbandonedExecutionControl } from "../execution-control-reconciliation.js";
|
|
const externalDatabaseUrl = process.env.PAPERCLIP_TEST_DATABASE_URL;
|
|
const support = externalDatabaseUrl
|
|
? { supported: true }
|
|
: await getEmbeddedPostgresTestSupport();
|
|
(support.supported ? describe : describe.skip)(
|
|
"durable replacement and control recovery",
|
|
() => {
|
|
let database: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
|
|
let db: ReturnType<typeof createDb>;
|
|
beforeAll(async () => {
|
|
if (externalDatabaseUrl) {
|
|
// The caller owns this fresh, already-migrated database.
|
|
db = createDb(externalDatabaseUrl);
|
|
return;
|
|
}
|
|
database = await startEmbeddedPostgresTestDatabase(
|
|
"paperclip-safe-replacement-",
|
|
);
|
|
db = createDb(database.connectionString);
|
|
}, 30_000);
|
|
afterAll(async () => {
|
|
if (externalDatabaseUrl) await db?.$client.end();
|
|
else await database?.cleanup();
|
|
});
|
|
async function seed(attempt = 1) {
|
|
const companyId = randomUUID(),
|
|
agentId = randomUUID(),
|
|
issueId = randomUUID(),
|
|
runId = randomUUID();
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Recovery",
|
|
issuePrefix: `R${companyId.slice(0, 6)}`,
|
|
});
|
|
await db.insert(agents).values({
|
|
id: agentId,
|
|
companyId,
|
|
name: "Executor",
|
|
role: "engineer",
|
|
adapterType: "paperclip_runner",
|
|
});
|
|
await db.insert(issues).values({
|
|
id: issueId,
|
|
companyId,
|
|
title: "Read fixture",
|
|
status: "in_progress",
|
|
assigneeAgentId: agentId,
|
|
});
|
|
await db.insert(heartbeatRuns).values({
|
|
id: runId,
|
|
companyId,
|
|
agentId,
|
|
nativeIssueId: issueId,
|
|
runtimeMode: "native",
|
|
status: "failed",
|
|
contextSnapshot: { issueId },
|
|
runnerProfileJson: {
|
|
recoveryEventInventoryVersion: 1,
|
|
nativeExecutionInput: {
|
|
provider: { kind: "codex" },
|
|
workspace: { cwd: tmpdir() },
|
|
},
|
|
},
|
|
});
|
|
await db.insert(nativeRunFinalizations).values({
|
|
runId,
|
|
companyId,
|
|
issueId,
|
|
phase: "terminal_failure",
|
|
attempt,
|
|
failureCode: "native_provider_terminal_failed",
|
|
failureDetail: { originalFailureCode: "fixture_checkpoint_unusable" },
|
|
});
|
|
return { companyId, agentId, issueId, runId };
|
|
}
|
|
it.each(["unproven", "changed", "verified"] as const)("requires stopped-session proof through commit (%s)", async (mode) => {
|
|
const source = await seed(2);
|
|
await db.update(nativeRunFinalizations).set({ failureCode: "native_session_cleanup_quarantined" }).where(eq(nativeRunFinalizations.runId, source.runId));
|
|
const [projected] = await db.update(issues).set({ status: "blocked" }).where(eq(issues.id, source.issueId)).returning();
|
|
await db.insert(issueRecoveryActions).values({ companyId: source.companyId, sourceIssueId: source.issueId,
|
|
kind: "active_run_watchdog", cause: "native_session_cleanup_quarantined", fingerprint: source.runId,
|
|
ownerType: "board", returnOwnerAgentId: source.agentId, status: "resolved", outcome: "blocked",
|
|
evidence: { runId: source.runId, nativeFailureBlock: { runId: source.runId, statusVersion: projected!.statusVersion }, automaticRecovery: { replay: "blocked" } }, nextAction: "Automatic recovery stopped.",
|
|
});
|
|
const retire = vi.fn(() => mode === "verified");
|
|
const verifyStoppedSession = vi.fn(async (run: typeof heartbeatRuns.$inferSelect) =>
|
|
run.id === source.runId && mode !== "unproven" ? { evidence: { completedTaskControlCallIds: ["completion"] }, retire } : null);
|
|
await appendHeartbeatRunEvent(db, { companyId: source.companyId, runId: source.runId, agentId: source.agentId,
|
|
eventType: "tool.execution.started", stream: "system",
|
|
payload: { name: "paperclip_finish", executionId: "completion", transport: "process" },
|
|
});
|
|
await reconcileSafeNativeReplacements(db, new Date(), { verifyStoppedSession });
|
|
await reconcileSafeNativeReplacements(db, new Date(), { verifyStoppedSession });
|
|
const successors = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, source.runId));
|
|
expect(successors).toHaveLength(mode === "verified" ? 1 : 0);
|
|
const [hold] = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, source.issueId));
|
|
if (mode === "verified") {
|
|
expect(retire).toHaveBeenCalledOnce();
|
|
expect(hold).toMatchObject({ status: "resolved", outcome: "handed_back", evidence: { automaticRecovery: { replay: "verified_safe_replacement" } } });
|
|
// Prove that the real dispatch gate accepts the successor, not only that a row exists.
|
|
const dispatch = createRunDispatch(db);
|
|
await db.update(heartbeatRuns).set({ status: "queued" }).where(eq(heartbeatRuns.id, successors[0]!.id));
|
|
const result = await dispatch.cancelStaleQueuedRun({ companyId: source.companyId, runId: successors[0]!.id, expectedStatus: "queued", now: new Date() });
|
|
expect(result.outcome).toBe("not_stale");
|
|
} else {
|
|
expect(hold!.evidence.automaticRecovery).toEqual({ replay: "blocked" });
|
|
}
|
|
});
|
|
it.each(["missing_receipt", "other_run", "other_cause", "manual_reblock", "dependency_edit", "queued_comment"] as const)("honors blocking intent before safe replacement (%s)", async (mode) => {
|
|
const source = await seed(2);
|
|
await db.update(nativeRunFinalizations).set({ failureCode: "native_session_cleanup_quarantined" }).where(eq(nativeRunFinalizations.runId, source.runId));
|
|
const projected = await issueService(db).update(source.issueId, { status: "blocked" });
|
|
const evidence = { runId: source.runId, ...(mode === "missing_receipt" ? {} : {
|
|
nativeFailureBlock: { runId: mode === "other_run" ? randomUUID() : source.runId, statusVersion: projected!.statusVersion },
|
|
}) };
|
|
const [hold] = await db.insert(issueRecoveryActions).values({ companyId: source.companyId, sourceIssueId: source.issueId,
|
|
kind: "active_run_watchdog", cause: mode === "other_cause" ? "native_event_replay_conflict" : "native_session_cleanup_quarantined",
|
|
fingerprint: source.runId, ownerType: "board", returnOwnerAgentId: source.agentId,
|
|
status: "resolved", outcome: "blocked", evidence, nextAction: "Preserve this hold.",
|
|
}).returning();
|
|
if (mode === "manual_reblock") await issueService(db).update(source.issueId, { status: "blocked", actorUserId: "operator" });
|
|
if (mode === "dependency_edit") {
|
|
const blockerId = randomUUID();
|
|
await db.insert(issues).values({ id: blockerId, companyId: source.companyId, title: "Human dependency", status: "todo" });
|
|
await issueService(db).update(source.issueId, { blockedByIssueIds: [blockerId], actorUserId: "operator" });
|
|
}
|
|
const comment = mode === "queued_comment" ? await issueService(db).addComment(source.issueId, "Also explain the result", { userId: "operator" }) : null;
|
|
const retire = vi.fn(() => true);
|
|
await reconcileSafeNativeReplacements(db, new Date(), { verifyStoppedSession: async run =>
|
|
run.id === source.runId ? { evidence: {}, retire } : null });
|
|
const successors = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, source.runId));
|
|
const [after] = await db.select().from(issues).where(eq(issues.id, source.issueId));
|
|
const [afterHold] = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.id, hold!.id));
|
|
if (mode === "queued_comment") {
|
|
expect(successors).toHaveLength(1);
|
|
expect(after!.status).toBe("in_progress");
|
|
expect(afterHold!.outcome).toBe("handed_back");
|
|
expect((await db.select().from(issueComments).where(eq(issueComments.id, comment!.id)))[0]!.body).toBe("Also explain the result");
|
|
} else {
|
|
expect(successors).toHaveLength(0);
|
|
expect(retire).not.toHaveBeenCalled();
|
|
expect(after!.status).toBe("blocked");
|
|
expect(afterHold!.evidence).toEqual(evidence);
|
|
expect(afterHold!.outcome).toBe("blocked");
|
|
}
|
|
});
|
|
it("automatically closes an exhausted incident once, preserves ownership, and records no replay", async () => {
|
|
const source = await seed(3);
|
|
await reconcileSafeNativeReplacements(db);
|
|
await Promise.all([settleUnrecoverableExecutions(db), settleUnrecoverableExecutions(db)]);
|
|
await settleUnrecoverableExecutions(db);
|
|
const [task] = await db.select().from(issues).where(eq(issues.id, source.issueId));
|
|
expect(task).toMatchObject({ status: "blocked", assigneeAgentId: source.agentId, executionRunId: null, checkoutRunId: null });
|
|
const actions = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, source.issueId));
|
|
expect(actions).toHaveLength(1);
|
|
expect(actions[0]).toMatchObject({ status: "resolved", outcome: "blocked", evidence: {
|
|
automaticRecovery: { policy: "preserve_without_replay_v1", actionOutcome: "unknown", replay: "blocked", runId: source.runId },
|
|
} });
|
|
const logs = await db.select().from(heartbeatRunEvents).where(eq(heartbeatRunEvents.runId, source.runId));
|
|
expect(logs.filter(log => log.payload?.automaticRecovery === "preserve_without_replay_v1")).toHaveLength(1);
|
|
const history = await activityService(db).runsForIssue(source.companyId, source.issueId);
|
|
expect(history.find(run => run.runId === source.runId)).toMatchObject({ execution: { phase: "recovery_needed", label: "Stopped" } });
|
|
|
|
expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, source.companyId))).toHaveLength(1);
|
|
});
|
|
it("does not let a full batch awaiting replacement starve an eligible disposition", async () => {
|
|
const sources: Awaited<ReturnType<typeof seed>>[] = [];
|
|
for (let index = 0; index < 26; index += 1) {
|
|
const source = await seed();
|
|
sources.push(source);
|
|
await db.insert(issueRecoveryActions).values({ companyId: source.companyId, sourceIssueId: source.issueId,
|
|
kind: "active_run_watchdog", ownerType: "board", returnOwnerAgentId: source.agentId,
|
|
cause: "native_provider_terminal_failed", fingerprint: source.runId, evidence: { runId: source.runId }, nextAction: "Checking recovery" });
|
|
if (index === 25) {
|
|
await db.update(nativeRunFinalizations).set({ failureDetail: { replacementDenied: "uncertain_external_action" } }).where(eq(nativeRunFinalizations.runId, source.runId));
|
|
await settleUnrecoverableExecutions(db);
|
|
const [task] = await db.select().from(issues).where(eq(issues.id, source.issueId));
|
|
expect(task.status).toBe("blocked");
|
|
}
|
|
}
|
|
await db.update(issueRecoveryActions).set({ status: "resolved" }).where(inArray(issueRecoveryActions.sourceIssueId, sources.map(source => source.issueId)));
|
|
await db.update(nativeRunFinalizations).set({ failureDetail: { replacementDenied: "fixture_closed" } }).where(inArray(nativeRunFinalizations.runId, sources.map(source => source.runId)));
|
|
});
|
|
it("rolls back a crashed automatic disposition and completes it on the next sweep", async () => {
|
|
const source = await seed(3);
|
|
await reconcileSafeNativeReplacements(db);
|
|
await expect(settleUnrecoverableExecutions(db, new Date(), { failpoint: () => { throw new Error("crash before commit"); } })).rejects.toThrow("crash before commit");
|
|
const [before] = await db.select().from(issues).where(eq(issues.id, source.issueId));
|
|
expect(before.status).toBe("in_progress");
|
|
const [pending] = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, source.issueId));
|
|
expect(pending.status).toBe("active");
|
|
await settleUnrecoverableExecutions(db);
|
|
const [after] = await db.select().from(issues).where(eq(issues.id, source.issueId));
|
|
expect(after.status).toBe("blocked");
|
|
const logs = await db.select().from(heartbeatRunEvents).where(eq(heartbeatRunEvents.runId, source.runId));
|
|
expect(logs.filter(log => log.payload?.automaticRecovery === "preserve_without_replay_v1")).toHaveLength(1);
|
|
});
|
|
it("does not let the automatic fallback preempt a safe replacement", async () => {
|
|
const source = await seed();
|
|
await db.insert(issueRecoveryActions).values({ companyId: source.companyId, sourceIssueId: source.issueId,
|
|
kind: "active_run_watchdog", ownerType: "board", returnOwnerAgentId: source.agentId,
|
|
cause: "native_provider_terminal_failed", fingerprint: source.runId, evidence: { runId: source.runId }, nextAction: "Checking recovery" });
|
|
await settleUnrecoverableExecutions(db);
|
|
const [task] = await db.select().from(issues).where(eq(issues.id, source.issueId));
|
|
expect(task.status).toBe("in_progress");
|
|
const [action] = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, source.issueId));
|
|
expect(action.status).toBe("active");
|
|
});
|
|
it.each(["closed", "reassigned", "new_execution"])("closes stale recovery after %s without changing task state or granting replay", async change => {
|
|
const source = await seed(3);
|
|
await reconcileSafeNativeReplacements(db);
|
|
const nextRun = randomUUID();
|
|
if (change === "new_execution") await db.insert(heartbeatRuns).values({ id: nextRun, companyId: source.companyId, agentId: source.agentId, status: "running" });
|
|
const patch = change === "closed" ? { status: "done" } : change === "reassigned" ? { assigneeAgentId: null } : { executionRunId: nextRun };
|
|
await db.update(issues).set(patch).where(eq(issues.id, source.issueId));
|
|
await settleUnrecoverableExecutions(db);
|
|
const [task] = await db.select().from(issues).where(eq(issues.id, source.issueId));
|
|
expect(task).toMatchObject(patch);
|
|
const [action] = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, source.issueId));
|
|
expect(action).toMatchObject({ status: "resolved", outcome: "cancelled", evidence: { automaticRecovery: { replay: "blocked" } } });
|
|
});
|
|
it("cancels a durable native retry even when its previous provider is already failed", async () => {
|
|
const source = await seed();
|
|
await db.update(nativeRunFinalizations).set({ phase: "retryable_failure", nextAttemptAt: new Date(Date.now() + 30_000) }).where(eq(nativeRunFinalizations.runId, source.runId));
|
|
await db.update(issues).set({ executionRunId: source.runId }).where(eq(issues.id, source.issueId));
|
|
const heartbeat = heartbeatService(db);
|
|
expect(await heartbeat.cancelRun(source.runId)).toMatchObject({ status: "cancelled" });
|
|
expect(await heartbeat.cancelRun(source.runId)).toMatchObject({ status: "cancelled" });
|
|
const [coordinator] = await db.select().from(nativeRunFinalizations).where(eq(nativeRunFinalizations.runId, source.runId));
|
|
expect(coordinator).toMatchObject({ phase: "terminal_failure", nextAttemptAt: null, leaseOwner: null });
|
|
expect(await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, source.runId))).toHaveLength(0);
|
|
});
|
|
it("atomically terminalizes unsupported legacy recovery and preserves its owner", async () => {
|
|
const source = await seed();
|
|
await db.update(heartbeatRuns).set({ runtimeMode: "legacy", status: "running" }).where(eq(heartbeatRuns.id, source.runId));
|
|
await db.update(issues).set({ executionRunId: source.runId, checkoutRunId: source.runId }).where(eq(issues.id, source.issueId));
|
|
const [run] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, source.runId));
|
|
const results = await Promise.all([1, 2].map(() => terminalizeLegacyExecution({ db, run, status: "failed", fromStatuses: ["running"], patch: { errorCode: "provider_quota", finishedAt: new Date() } })));
|
|
expect(results.filter(Boolean)).toHaveLength(1);
|
|
const [task] = await db.select().from(issues).where(eq(issues.id, source.issueId));
|
|
expect(task).toMatchObject({ assigneeAgentId: source.agentId, executionRunId: null, checkoutRunId: null });
|
|
const actions = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, source.issueId));
|
|
expect(actions).toHaveLength(1);
|
|
expect(actions[0]).toMatchObject({ ownerType: "board", cause: "legacy_execution_requires_reconciliation" });
|
|
await db.delete(nativeRunFinalizations).where(eq(nativeRunFinalizations.runId, source.runId));
|
|
await settleUnrecoverableExecutions(db);
|
|
const [settled] = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.id, actions[0]!.id));
|
|
expect(settled).toMatchObject({ status: "resolved", outcome: "blocked", evidence: { automaticRecovery: { replay: "blocked" } } });
|
|
|
|
expect(legacyExecutionNeedsReconciliation({ ...run, status: "failed", resultJson: { errorFamily: "provider_quota" } })).toBe(true);
|
|
expect(legacyExecutionNeedsReconciliation({ ...run, status: "failed", resultJson: { executionRecovery: { kind: "bootstrap", providerWorkStarted: false } } })).toBe(false);
|
|
expect(legacyExecutionNeedsReconciliation({ ...run, status: "failed", scheduledRetryAttempt: 2, resultJson: { executionRecovery: { kind: "bootstrap", providerWorkStarted: false } } })).toBe(true);
|
|
});
|
|
it("does not reopen a reconciled legacy run while continuation is pending", async () => {
|
|
const source = await seed();
|
|
const [run] = await db.update(heartbeatRuns).set({ runtimeMode: "legacy", status: "cancelled" }).where(eq(heartbeatRuns.id, source.runId)).returning();
|
|
await terminalizeLegacyExecution({ db, run, status: "cancelled" });
|
|
const [action] = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, source.issueId));
|
|
await markExecutionReconciliation(db, action!, { runId: source.runId, providerStopped: true, actionOutcome: "not_performed", outcomeEvidence: "The deterministic fixture has stopped and only printed output." }, "board");
|
|
await db.update(issueRecoveryActions).set({ status: "resolved" }).where(eq(issueRecoveryActions.id, action!.id));
|
|
await terminalizeLegacyExecution({ db, run, status: "cancelled" });
|
|
const actions = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, source.issueId));
|
|
expect(actions).toHaveLength(1);
|
|
expect(actions[0]).toMatchObject({ status: "resolved", evidence: { continuationDelivery: "pending", executionReconciliation: { runId: source.runId } } });
|
|
await db.update(issueRecoveryActions).set({ evidence: { ...actions[0]!.evidence, continuationDelivery: "invalidated" } }).where(eq(issueRecoveryActions.id, action!.id));
|
|
});
|
|
it("surfaces a failed current reviewer without transferring the original assignment", async () => {
|
|
const source = await seed();
|
|
const reviewerId = randomUUID();
|
|
await db.insert(agents).values({ id: reviewerId, companyId: source.companyId, name: "Reviewer", role: "engineer", adapterType: "process" });
|
|
await db.update(issues).set({ status: "in_review", executionState: {
|
|
status: "pending", currentStageId: randomUUID(), currentStageIndex: 0, currentStageType: "review",
|
|
currentParticipant: { type: "agent", agentId: reviewerId, userId: null },
|
|
returnAssignee: { type: "agent", agentId: source.agentId, userId: null },
|
|
completedStageIds: [], lastDecisionId: null, lastDecisionOutcome: null,
|
|
} }).where(eq(issues.id, source.issueId));
|
|
const [run] = await db.update(heartbeatRuns).set({ agentId: reviewerId, runtimeMode: "legacy", status: "running" }).where(eq(heartbeatRuns.id, source.runId)).returning();
|
|
await terminalizeLegacyExecution({ db, run, status: "failed", patch: { finishedAt: new Date() } });
|
|
const [action] = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, source.issueId));
|
|
expect(action).toMatchObject({ ownerType: "board", returnOwnerAgentId: source.agentId, evidence: { reviewParticipantAgentId: reviewerId } });
|
|
const input = { db, companyId: source.companyId, issueId: source.issueId, agentId: source.agentId, sourceRunId: source.runId,
|
|
decision: { runId: source.runId, providerStopped: true as const, actionOutcome: "not_performed" as const, outcomeEvidence: "Inspected reviewer process and verified no actions occurred." } };
|
|
await expect(validateExecutionReconciliation(input)).resolves.toMatchObject({ id: source.runId });
|
|
await db.update(issues).set({ executionState: null }).where(eq(issues.id, source.issueId));
|
|
await expect(validateExecutionReconciliation(input)).rejects.toThrow("source or task owner changed");
|
|
});
|
|
it("does not turn an already reconciled continuation into a recovery assignment", async () => {
|
|
const source = await seed();
|
|
const [action] = await db.insert(issueRecoveryActions).values({ companyId: source.companyId, sourceIssueId: source.issueId, kind: "active_run_watchdog", status: "resolved", ownerType: "board", returnOwnerAgentId: source.agentId, cause: "native_event_replay_conflict", fingerprint: source.runId, nextAction: "Old recovery instruction", evidence: { executionReconciliation: { runId: source.runId } } }).returning();
|
|
const wake = await buildPaperclipWakePayload({ db, companyId: source.companyId, contextSnapshot: { issueId: source.issueId, recoveryActionId: action.id, wakeReason: "issue_recovery_action_restored" } });
|
|
expect(wake?.recovery).toBeNull();
|
|
expect(wake?.reason).toBe("issue_recovery_action_restored");
|
|
});
|
|
it("keeps an unclassified transport terminal operator-owned", async () => {
|
|
const source = await seed();
|
|
await db
|
|
.update(nativeRunFinalizations)
|
|
.set({
|
|
failureDetail: {
|
|
originalFailureCode: "notification_transport_failed",
|
|
},
|
|
})
|
|
.where(eq(nativeRunFinalizations.runId, source.runId));
|
|
await reconcileSafeNativeReplacements(db);
|
|
expect(
|
|
await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.retryOfRunId, source.runId)),
|
|
).toHaveLength(0);
|
|
const [coordinator] = await db
|
|
.select()
|
|
.from(nativeRunFinalizations)
|
|
.where(eq(nativeRunFinalizations.runId, source.runId));
|
|
expect(coordinator?.failureDetail?.replacementDenied).toBe(
|
|
"provider_failure_meaning_unverified",
|
|
);
|
|
});
|
|
it.each(["done", "cancelled", "review"])("does not reuse consumed wake authority after a later %s transition", async transition => {
|
|
const source = await seed();
|
|
const previousRunId = randomUUID(), commentId = randomUUID();
|
|
await db.insert(heartbeatRuns).values({ id: previousRunId, companyId: source.companyId,
|
|
agentId: source.agentId, runtimeMode: "native", status: "failed", contextSnapshot: { issueId: source.issueId } });
|
|
await db.insert(issueComments).values({ id: commentId, companyId: source.companyId,
|
|
issueId: source.issueId, authorType: "user", authorUserId: "board", body: "Continue" });
|
|
await db.insert(issueRecoveryActions).values({ companyId: source.companyId, sourceIssueId: source.issueId,
|
|
kind: "active_run_watchdog", cause: "native_provider_terminal_failed", fingerprint: previousRunId,
|
|
status: "resolved", outcome: "cancelled", nextAction: "User continued", evidence: {
|
|
explicitUserContinuation: { previousRunId, commentId, actorId: "board", runId: source.runId },
|
|
} });
|
|
await db.update(heartbeatRuns).set({ contextSnapshot: {
|
|
issueId: source.issueId, previousRunId,
|
|
wakeCommentId: commentId, wakeCommentIds: [commentId], commentId,
|
|
resumeIntent: true, followUpRequested: true,
|
|
explicitUserContinuation: { previousRunId, commentId },
|
|
} }).where(eq(heartbeatRuns.id, source.runId));
|
|
await reconcileSafeNativeReplacements(db);
|
|
const [successor] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, source.runId));
|
|
expect(successor.contextSnapshot?.explicitUserContinuation).toBeUndefined();
|
|
expect(deriveCommentId(successor.contextSnapshot)).toBeNull();
|
|
expect(successor.contextSnapshot?.resumeIntent).toBeUndefined();
|
|
expect(successor.contextSnapshot?.followUpRequested).toBeUndefined();
|
|
const envelope = await buildExecutionContinuation({ db, companyId: source.companyId,
|
|
issueId: source.issueId, agentId: source.agentId, context: successor.contextSnapshot!,
|
|
summary: null, exposeLowTrustRaw: false });
|
|
expect(envelope.trigger.sourceRunId).toBe(source.runId);
|
|
expect(envelope.objective).toBe("Continue");
|
|
if (transition === "review") {
|
|
await db.update(issues).set({ status: "in_review", executionState: {
|
|
status: "pending", currentStageId: randomUUID(), currentStageIndex: 0, currentStageType: "review",
|
|
currentParticipant: { type: "agent", agentId: randomUUID(), userId: null },
|
|
returnAssignee: { type: "agent", agentId: source.agentId, userId: null },
|
|
reviewRequest: null, completedStageIds: [], lastDecisionId: null, lastDecisionOutcome: null,
|
|
} }).where(eq(issues.id, source.issueId));
|
|
} else {
|
|
await db.update(issues).set({ status: transition }).where(eq(issues.id, source.issueId));
|
|
}
|
|
await db.update(heartbeatRuns).set({ status: "queued" }).where(eq(heartbeatRuns.id, successor.id));
|
|
expect(await createRunDispatch(db).cancelStaleQueuedRun({ companyId: source.companyId,
|
|
runId: successor.id, expectedStatus: "queued" })).toMatchObject({ outcome: "cancelled",
|
|
errorCode: transition === "review" ? "issue_review_participant_changed" : "issue_terminal_status" });
|
|
});
|
|
it("persists exactly one linked successor under competing sweepers and restarts", async () => {
|
|
const source = await seed(2);
|
|
const now = new Date();
|
|
await Promise.all([
|
|
reconcileSafeNativeReplacements(db, now),
|
|
reconcileSafeNativeReplacements(db, now),
|
|
]);
|
|
await reconcileSafeNativeReplacements(db, now);
|
|
const children = await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.retryOfRunId, source.runId));
|
|
expect(children).toHaveLength(1);
|
|
expect(children[0]).toMatchObject({
|
|
agentId: source.agentId,
|
|
status: "scheduled_retry",
|
|
scheduledRetryAttempt: 2,
|
|
contextSnapshot: {
|
|
forceFreshSession: true,
|
|
recoveryIncidentRootRunId: source.runId,
|
|
},
|
|
});
|
|
expect(children[0]!.scheduledRetryAt!.getTime()).toBe(
|
|
now.getTime() + 30_000,
|
|
);
|
|
expect(
|
|
(
|
|
await db
|
|
.select()
|
|
.from(nativeRunFinalizations)
|
|
.where(eq(nativeRunFinalizations.runId, source.runId))
|
|
)[0]?.failureDetail?.successorRunId,
|
|
).toBe(children[0]!.id);
|
|
});
|
|
it("retries status publication after a crash without dispatching provider work", async () => {
|
|
await deliverExecutionStatuses(db);
|
|
const source = await seed();
|
|
const deliveryId = randomUUID();
|
|
await db
|
|
.update(heartbeatRuns)
|
|
.set({
|
|
executionStatusDeliveryId: deliveryId,
|
|
error: "credential-in-provider-error",
|
|
errorCode: "credential-in-provider-code",
|
|
triggerDetail: "credential-in-trigger-detail",
|
|
contextSnapshot: {
|
|
issueId: source.issueId,
|
|
secret: "credential-in-context",
|
|
nested: { provider: "credential-in-nested-context" },
|
|
},
|
|
resultJson: {
|
|
summary: "credential-in-provider-summary",
|
|
toolResult: "credential-in-tool-result",
|
|
},
|
|
})
|
|
.where(eq(heartbeatRuns.id, source.runId));
|
|
await deliverExecutionStatuses(db, {
|
|
publish: () => {
|
|
throw new Error("publication unavailable");
|
|
},
|
|
});
|
|
expect(
|
|
(
|
|
await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, source.runId))
|
|
)[0]?.executionStatusDeliveryId,
|
|
).toBe(deliveryId);
|
|
const observed: unknown[] = [];
|
|
const publish: typeof publishLiveEvent = (event) => {
|
|
observed.push(event);
|
|
return publishLiveEvent(event);
|
|
};
|
|
await expect(
|
|
deliverExecutionStatuses(db, {
|
|
publish,
|
|
failpoint: () => {
|
|
throw new Error("crash after publication");
|
|
},
|
|
}),
|
|
).rejects.toThrow("crash after publication");
|
|
await deliverExecutionStatuses(db, { publish });
|
|
expect(JSON.stringify(observed)).not.toContain("credential-in-");
|
|
expect(
|
|
Object.keys(
|
|
(observed[0] as { payload: Record<string, unknown> }).payload,
|
|
).sort(),
|
|
).toEqual(
|
|
[
|
|
"runId",
|
|
"agentId",
|
|
"issueId",
|
|
"status",
|
|
"startedAt",
|
|
"finishedAt",
|
|
"deliveryId",
|
|
].sort(),
|
|
);
|
|
expect(observed).toEqual([
|
|
expect.objectContaining({
|
|
companyId: source.companyId,
|
|
payload: expect.objectContaining({
|
|
runId: source.runId,
|
|
issueId: source.issueId,
|
|
deliveryId,
|
|
status: "failed",
|
|
}),
|
|
}),
|
|
expect.objectContaining({
|
|
companyId: source.companyId,
|
|
payload: expect.objectContaining({
|
|
runId: source.runId,
|
|
issueId: source.issueId,
|
|
deliveryId,
|
|
status: "failed",
|
|
}),
|
|
}),
|
|
]);
|
|
expect(
|
|
(
|
|
await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, source.runId))
|
|
)[0]?.executionStatusDeliveryId,
|
|
).toBeNull();
|
|
expect(
|
|
await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.retryOfRunId, source.runId)),
|
|
).toHaveLength(0);
|
|
});
|
|
it.each([
|
|
"native",
|
|
"legacy",
|
|
"native_precedes_context",
|
|
"missing",
|
|
"nonexistent",
|
|
"deleted",
|
|
"foreign",
|
|
"malformed",
|
|
"object",
|
|
"array",
|
|
"number",
|
|
"json_null",
|
|
"native_foreign",
|
|
"native_nonexistent",
|
|
] as const)(
|
|
"routes status delivery only through a proven same-company task: %s",
|
|
async (association) => {
|
|
await deliverExecutionStatuses(db);
|
|
const source = await seed();
|
|
const otherIssueId = randomUUID();
|
|
await db.insert(issues).values({
|
|
id: otherIssueId,
|
|
companyId: source.companyId,
|
|
title: "Other task",
|
|
status: "backlog",
|
|
});
|
|
const foreign =
|
|
association === "foreign" || association === "native_foreign"
|
|
? await seed()
|
|
: null;
|
|
const runId = randomUUID();
|
|
const deliveryId = randomUUID();
|
|
let nativeIssueId: string | null = null;
|
|
let contextSnapshot: Record<string, unknown> = {
|
|
issueId: source.issueId,
|
|
};
|
|
let expectedIssueId: string | null = null;
|
|
switch (association) {
|
|
case "native":
|
|
nativeIssueId = source.issueId;
|
|
contextSnapshot = {};
|
|
expectedIssueId = source.issueId;
|
|
break;
|
|
case "legacy":
|
|
expectedIssueId = source.issueId;
|
|
break;
|
|
case "native_precedes_context":
|
|
nativeIssueId = source.issueId;
|
|
contextSnapshot = { issueId: otherIssueId };
|
|
expectedIssueId = source.issueId;
|
|
break;
|
|
case "missing":
|
|
contextSnapshot = {};
|
|
break;
|
|
case "nonexistent":
|
|
contextSnapshot = { issueId: randomUUID() };
|
|
break;
|
|
case "deleted":
|
|
contextSnapshot = { issueId: otherIssueId };
|
|
await db.delete(issues).where(eq(issues.id, otherIssueId));
|
|
break;
|
|
case "foreign":
|
|
contextSnapshot = { issueId: foreign!.issueId };
|
|
break;
|
|
case "malformed":
|
|
contextSnapshot = { issueId: "credential-in-invalid-issue-id" };
|
|
break;
|
|
case "object":
|
|
contextSnapshot = { issueId: { secret: "credential-in-object" } };
|
|
break;
|
|
case "array":
|
|
contextSnapshot = {
|
|
issueId: [source.issueId, "credential-in-array"],
|
|
};
|
|
break;
|
|
case "number":
|
|
contextSnapshot = { issueId: 42 };
|
|
break;
|
|
case "json_null":
|
|
contextSnapshot = { issueId: null };
|
|
break;
|
|
case "native_foreign":
|
|
nativeIssueId = foreign!.issueId;
|
|
break;
|
|
case "native_nonexistent":
|
|
nativeIssueId = randomUUID();
|
|
break;
|
|
}
|
|
await db.insert(heartbeatRuns).values({
|
|
id: runId,
|
|
companyId: source.companyId,
|
|
agentId: source.agentId,
|
|
runtimeMode: nativeIssueId ? "native" : "legacy",
|
|
nativeIssueId,
|
|
contextSnapshot: {
|
|
...contextSnapshot,
|
|
secret: "credential-in-context",
|
|
},
|
|
status: "cancelled",
|
|
executionStatusDeliveryId: deliveryId,
|
|
error: "credential-in-error",
|
|
resultJson: { output: "credential-in-output" },
|
|
});
|
|
const beforeRunIds = (
|
|
await db
|
|
.select({ id: heartbeatRuns.id })
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.companyId, source.companyId))
|
|
)
|
|
.map((row) => row.id)
|
|
.sort();
|
|
const observed: Parameters<typeof publishLiveEvent>[0][] = [];
|
|
await deliverExecutionStatuses(db, {
|
|
publish: (event) => {
|
|
observed.push(event);
|
|
return publishLiveEvent(event);
|
|
},
|
|
});
|
|
expect(observed).toEqual([
|
|
{
|
|
companyId: source.companyId,
|
|
type: "heartbeat.run.status",
|
|
payload: {
|
|
runId,
|
|
agentId: source.agentId,
|
|
issueId: expectedIssueId,
|
|
status: "cancelled",
|
|
startedAt: null,
|
|
finishedAt: null,
|
|
deliveryId,
|
|
},
|
|
},
|
|
]);
|
|
expect(JSON.stringify(observed)).not.toContain("credential-in-");
|
|
if (foreign)
|
|
expect(JSON.stringify(observed)).not.toContain(foreign.issueId);
|
|
expect(
|
|
(
|
|
await db
|
|
.select({ id: heartbeatRuns.id })
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.companyId, source.companyId))
|
|
)
|
|
.map((row) => row.id)
|
|
.sort(),
|
|
).toEqual(beforeRunIds);
|
|
expect(
|
|
(
|
|
await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, runId))
|
|
)[0],
|
|
).toMatchObject({
|
|
status: "cancelled",
|
|
executionStatusDeliveryId: null,
|
|
processPid: null,
|
|
});
|
|
},
|
|
);
|
|
|
|
it("never resets an exhausted incident by assigning another run id", async () => {
|
|
const source = await seed(3);
|
|
await reconcileSafeNativeReplacements(db);
|
|
expect(
|
|
await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.retryOfRunId, source.runId)),
|
|
).toHaveLength(0);
|
|
expect(
|
|
(
|
|
await db
|
|
.select()
|
|
.from(issueRecoveryActions)
|
|
.where(eq(issueRecoveryActions.sourceIssueId, source.issueId))
|
|
)[0],
|
|
).toMatchObject({
|
|
ownerType: "board",
|
|
cause: "execution_recovery_budget_exhausted",
|
|
});
|
|
});
|
|
it.each(["successor_inserted", "lineage_committed"] as const)(
|
|
"recovers a crash at %s without duplicate successors",
|
|
async (phase) => {
|
|
const source = await seed();
|
|
await expect(
|
|
reconcileSafeNativeReplacements(db, new Date(), {
|
|
failpoint: (point) => {
|
|
if (point === phase)
|
|
throw new Error("simulated coordinator crash");
|
|
},
|
|
}),
|
|
).rejects.toThrow("simulated coordinator crash");
|
|
await reconcileSafeNativeReplacements(db);
|
|
expect(
|
|
await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.retryOfRunId, source.runId)),
|
|
).toHaveLength(1);
|
|
},
|
|
);
|
|
it.each([false, true])(
|
|
"does not replay a provider-native command with unknown effects (PRP envelope: %s)",
|
|
async (wrapped) => {
|
|
const source = await seed();
|
|
await db.insert(heartbeatRunEvents).values({
|
|
companyId: source.companyId,
|
|
runId: source.runId,
|
|
agentId: source.agentId,
|
|
seq: 1,
|
|
eventType: "tool.execution.started",
|
|
stream: "system",
|
|
payload: wrapped
|
|
? {
|
|
prpEvent: {
|
|
payload: {
|
|
transport: "process",
|
|
executionId: "shell-write-1",
|
|
name: "send_email",
|
|
},
|
|
},
|
|
}
|
|
: {
|
|
transport: "process",
|
|
executionId: "shell-write-1",
|
|
name: "send_email",
|
|
},
|
|
});
|
|
await reconcileSafeNativeReplacements(db);
|
|
expect(
|
|
await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.retryOfRunId, source.runId)),
|
|
).toHaveLength(0);
|
|
expect(
|
|
(
|
|
await db
|
|
.select()
|
|
.from(issueRecoveryActions)
|
|
.where(eq(issueRecoveryActions.sourceIssueId, source.issueId))
|
|
)[0]?.nextAction,
|
|
).toContain("shell-write-1");
|
|
},
|
|
);
|
|
it("surfaces a reviewer's abandoned control transition for the operator", async () => {
|
|
const source = await seed();
|
|
const reviewerId = randomUUID();
|
|
await db.insert(agents).values({ id: reviewerId, companyId: source.companyId, name: "Reviewer", role: "engineer", adapterType: "process" });
|
|
await db.update(issues).set({ status: "in_review", executionRunId: source.runId, executionState: {
|
|
status: "pending", currentStageId: randomUUID(), currentStageIndex: 0, currentStageType: "review",
|
|
currentParticipant: { type: "agent", agentId: reviewerId, userId: null },
|
|
returnAssignee: { type: "agent", agentId: source.agentId, userId: null },
|
|
completedStageIds: [], lastDecisionId: null, lastDecisionOutcome: null,
|
|
} }).where(eq(issues.id, source.issueId));
|
|
await db.update(heartbeatRuns).set({ agentId: reviewerId, status: "running", executionControlDeadlineAt: new Date(Date.now() - 1_000) }).where(eq(heartbeatRuns.id, source.runId));
|
|
await reconcileAbandonedExecutionControl(db);
|
|
expect(await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, source.issueId))).toMatchObject([
|
|
{ ownerType: "board", returnOwnerAgentId: source.agentId, evidence: { reviewParticipantAgentId: reviewerId } },
|
|
]);
|
|
expect((await db.select().from(issues).where(eq(issues.id, source.issueId)))[0]).toMatchObject({ assigneeAgentId: source.agentId, executionRunId: null });
|
|
});
|
|
it("fences a stranded finalization and surfaces one operator action after its deadline", async () => {
|
|
const source = await seed();
|
|
const deadline = new Date("2026-09-08T10:00:00Z");
|
|
await db
|
|
.update(heartbeatRuns)
|
|
.set({ status: "running" })
|
|
.where(eq(heartbeatRuns.id, source.runId));
|
|
await db
|
|
.update(nativeRunFinalizations)
|
|
.set({
|
|
phase: "observed",
|
|
leaseOwner: "dead-controller",
|
|
controlDeadlineAt: deadline,
|
|
})
|
|
.where(eq(nativeRunFinalizations.runId, source.runId));
|
|
await reconcileAbandonedExecutionControl(
|
|
db,
|
|
new Date(deadline.getTime() + 15_000),
|
|
);
|
|
await reconcileAbandonedExecutionControl(
|
|
db,
|
|
new Date(deadline.getTime() + 30_000),
|
|
);
|
|
expect(
|
|
(
|
|
await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, source.runId))
|
|
)[0],
|
|
).toMatchObject({
|
|
status: "failed",
|
|
errorCode: "execution_finalization_deadline_exceeded",
|
|
});
|
|
expect(
|
|
(
|
|
await db
|
|
.select()
|
|
.from(nativeRunFinalizations)
|
|
.where(eq(nativeRunFinalizations.runId, source.runId))
|
|
)[0],
|
|
).toMatchObject({
|
|
leaseOwner: null,
|
|
controlDeadlineAt: null,
|
|
phase: "terminal_failure",
|
|
});
|
|
expect(
|
|
await db
|
|
.select()
|
|
.from(issueRecoveryActions)
|
|
.where(
|
|
and(
|
|
eq(issueRecoveryActions.sourceIssueId, source.issueId),
|
|
eq(
|
|
issueRecoveryActions.cause,
|
|
"execution_finalization_deadline_exceeded",
|
|
),
|
|
),
|
|
),
|
|
).toHaveLength(1);
|
|
});
|
|
it("bounds legacy finalization without adding a deadline to healthy provider execution", async () => {
|
|
const source = await seed();
|
|
await db
|
|
.delete(nativeRunFinalizations)
|
|
.where(eq(nativeRunFinalizations.runId, source.runId));
|
|
await db
|
|
.update(heartbeatRuns)
|
|
.set({
|
|
runtimeMode: "legacy",
|
|
status: "running",
|
|
executionControlDeadlineAt: null,
|
|
})
|
|
.where(eq(heartbeatRuns.id, source.runId));
|
|
await reconcileAbandonedExecutionControl(db);
|
|
expect(
|
|
(
|
|
await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, source.runId))
|
|
)[0]?.status,
|
|
).toBe("running");
|
|
await db
|
|
.update(heartbeatRuns)
|
|
.set({ executionControlDeadlineAt: new Date(Date.now() - 1000) })
|
|
.where(eq(heartbeatRuns.id, source.runId));
|
|
await reconcileAbandonedExecutionControl(db);
|
|
expect(
|
|
(
|
|
await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.id, source.runId))
|
|
)[0],
|
|
).toMatchObject({ status: "failed", executionControlDeadlineAt: null });
|
|
expect(
|
|
(
|
|
await db
|
|
.select()
|
|
.from(issueRecoveryActions)
|
|
.where(eq(issueRecoveryActions.sourceIssueId, source.issueId))
|
|
)[0]?.ownerType,
|
|
).toBe("board");
|
|
});
|
|
it("does not release another run's checkout or replace a reassigned task", async () => {
|
|
const source = await seed();
|
|
const otherRunId = randomUUID();
|
|
await db.insert(heartbeatRuns).values({
|
|
id: otherRunId,
|
|
companyId: source.companyId,
|
|
agentId: source.agentId,
|
|
status: "running",
|
|
});
|
|
await db
|
|
.update(issues)
|
|
.set({ checkoutRunId: otherRunId })
|
|
.where(eq(issues.id, source.issueId));
|
|
await reconcileSafeNativeReplacements(db);
|
|
expect(
|
|
await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.retryOfRunId, source.runId)),
|
|
).toHaveLength(0);
|
|
await db
|
|
.update(issues)
|
|
.set({ assigneeAgentId: null })
|
|
.where(eq(issues.id, source.issueId));
|
|
await reconcileSafeNativeReplacements(db);
|
|
expect(
|
|
await db
|
|
.select()
|
|
.from(heartbeatRuns)
|
|
.where(eq(heartbeatRuns.retryOfRunId, source.runId)),
|
|
).toHaveLength(0);
|
|
});
|
|
it("requires explicit reconciled outcomes and rejects a still-running provider", async () => {
|
|
const source = await seed();
|
|
const input = {
|
|
db,
|
|
companyId: source.companyId,
|
|
issueId: source.issueId,
|
|
agentId: source.agentId,
|
|
sourceRunId: source.runId,
|
|
decision: undefined,
|
|
};
|
|
await expect(validateExecutionReconciliation(input)).rejects.toThrow(
|
|
"Reconcile the recorded execution",
|
|
);
|
|
const decision = {
|
|
runId: source.runId,
|
|
providerStopped: true as const,
|
|
actionOutcome: "not_performed" as const,
|
|
outcomeEvidence:
|
|
"Verified that the fixture emitted an event only; no external action happened.",
|
|
};
|
|
await db
|
|
.update(heartbeatRuns)
|
|
.set({ processPid: process.pid })
|
|
.where(eq(heartbeatRuns.id, source.runId));
|
|
await expect(
|
|
validateExecutionReconciliation({ ...input, decision }),
|
|
).rejects.toThrow("still running");
|
|
await db
|
|
.update(heartbeatRuns)
|
|
.set({ processPid: null })
|
|
.where(eq(heartbeatRuns.id, source.runId));
|
|
await expect(
|
|
validateExecutionReconciliation({ ...input, decision }),
|
|
).resolves.toMatchObject({ id: source.runId });
|
|
await expect(
|
|
validateExecutionReconciliation({
|
|
...input,
|
|
companyId: randomUUID(),
|
|
decision,
|
|
}),
|
|
).rejects.toThrow("source or task owner changed");
|
|
});
|
|
it("retains a reconciliation delivery across dispatch failure and invalidates stale ownership", async () => {
|
|
const source = await seed();
|
|
const [action] = await db
|
|
.insert(issueRecoveryActions)
|
|
.values({
|
|
companyId: source.companyId,
|
|
sourceIssueId: source.issueId,
|
|
kind: "active_run_watchdog",
|
|
status: "resolved",
|
|
ownerType: "board",
|
|
returnOwnerAgentId: source.agentId,
|
|
cause: "uncertain_provider_action",
|
|
fingerprint: source.runId,
|
|
evidence: { runId: source.runId },
|
|
nextAction: "Reconcile fixture action",
|
|
})
|
|
.returning();
|
|
const decision = {
|
|
runId: source.runId,
|
|
providerStopped: true as const,
|
|
actionOutcome: "not_performed" as const,
|
|
outcomeEvidence:
|
|
"The fixture was inspected and no command was executed.",
|
|
};
|
|
await markExecutionReconciliation(db, action!, decision, "operator");
|
|
await deliverReconciledExecutions(db, async () => {
|
|
throw new Error("dispatch unavailable");
|
|
});
|
|
expect(
|
|
(
|
|
await db
|
|
.select()
|
|
.from(issueRecoveryActions)
|
|
.where(eq(issueRecoveryActions.id, action!.id))
|
|
)[0]?.evidence.continuationDelivery,
|
|
).toBe("pending");
|
|
await db
|
|
.update(issues)
|
|
.set({ assigneeAgentId: null })
|
|
.where(eq(issues.id, source.issueId));
|
|
let woke = false;
|
|
await deliverReconciledExecutions(db, async () => {
|
|
woke = true;
|
|
return null;
|
|
});
|
|
expect(woke).toBe(false);
|
|
expect(
|
|
(
|
|
await db
|
|
.select()
|
|
.from(issueRecoveryActions)
|
|
.where(eq(issueRecoveryActions.id, action!.id))
|
|
)[0]?.evidence.continuationDelivery,
|
|
).toBe("invalidated");
|
|
});
|
|
},
|
|
);
|