Files
PaperClipAI/server/src/services/governed-wait-runner-loss.test.ts
T
Devin FoleyandPaperclip c41145e72a fix: wait on pending questions and quiet provider holds (#22363)
## Thinking Path

> - Paperclip manages agents and the tasks they work on.
> - Recovery should continue unfinished work when it is eligible.
> - An active unanswered question is a persisted reason to wait for a
person.
> - Treating a later Retry as an answer makes the recovery sweep
repeatedly request continuations that admission refuses.
> - Provider-limit holds also produce one cancellation toast per task,
even though provider work never started.
> - This PR aligns recovery with persisted interaction status and
presents these waits accurately.

## Linked Issues or Issue Description

Refs: #20466. This addresses the question blocker presentation and
automatic recovery loop; it preserves the existing
interaction-resolution policy.

Related work: #19420 schedules provider quota retries, and #21570
handles notices and reply ownership in agent chats. This change concerns
assigned-task reconciliation, task/inbox status, and per-run
cancellation toasts.

**What happened?**

The assigned-work sweep ignored pending interactions whenever a newer
run carried `explicitUserContinuation`. Continuation admission still
refused the pending question, so repeated sweeps recorded unsuccessful
continuation attempts. The task could meanwhile show an older provider
error as its current blocker. Separately, each pre-dispatch
provider-limit hold generated a cancellation toast, and the one-hour
fallback was described as a known reset and promised start time.

**Expected behavior**

Wait quietly while the question remains active under the shared
interaction eligibility rules, then react to the existing resolution
notification. Dismissed or historical ordinary questions remain
answerable without blocking work; governed requests keep their gates.
Show the current question on task and inbox surfaces. Keep provider
failure history, suppress cancellation toasts for expected provider
holds, and describe scheduled availability checks without promising the
provider will accept work.

**Steps to reproduce**

1. Leave an `ask_user_questions` interaction pending with
`supersedeOnUserComment: false`.
2. Create a later failed/interrupted explicit Retry and run
assigned-work reconciliation repeatedly.
3. Observe that the question remains pending while the sweep attempts
fresh continuations.
4. Separately, let an agent with recent provider rate-limit evidence
receive automatic task wakes without a reported reset time.

## What Changed

- Remove inferred question supersession from both assigned-work fact
loading and candidate selection. Use the existing active-interaction
predicate consistently in those reads, fresh-continuation admission, and
the task projection. Cover dismissed/historical ordinary questions,
retained governed gates, repeated checks, idle workers, and the existing
resolution notification.
- Add the `question` interruption action, load active pending question
IDs alongside approvals in one company-scoped query, and show that
blocker on tasks and inbox rows. Preserve a supported manual Retry after
a failed attempt, suppress duplicate actions for saved input, and keep
other current user gates authoritative. This preserves the existing
issue-detail SQL statement budget. Historical run failures remain
intact. Refresh the shared source SDK input pin.
- Suppress cancellation toasts specifically for cancelled
`provider_limit_wait` runs. Label provider retry deadlines as
availability rechecks, retain relative-time formatting, and record
successful scheduling as an informational lifecycle event.
- Document the wait contract in the continuation architecture guide.

## Verification

- Demonstrated failing regressions before the implementation:
continuation loop, task projection, task notice, cancellation toast,
provider deadline wording, and deadline formatting.
- Initial focused Vitest qualification: 642 tests passed across 14
files, using:

  ```sh
  pnpm exec vitest run \

server/src/__tests__/{assigned-work-invariant,assigned-work-recheck-idle,heartbeat-provider-limit-wait,task-interruption-query}.test.ts
\

server/src/services/{task-interruption-presentation,execution-projection,fresh-continuation-fallback,issue-thread-interactions}.test.ts
\
    server/src/services/recovery/assigned-work-invariant.test.ts \

ui/src/{components/ExecutionBlockerNotice.test.tsx,context/LiveUpdatesProvider.test.ts,context/LiveUpdatesProvider.hook.test.tsx,lib/provider-limit-message.test.ts,pages/Inbox.test.tsx}
  ```
- `pnpm --filter @paperclipai/shared typecheck` — passed.
- Review follow-up: 157 tests passed in `assigned-work-invariant`,
`task-interruption-query`, and `fresh-continuation-fallback`; 575 passed
in `task-interruption-query`, `assigned-work-recheck-idle`,
`task-interruption-presentation`, `issue-question-context`,
`issue-thread-interactions`, and `explicit-native-continuation` (the
query file overlaps). The strengthened final
dismissed/historical/governed projection regression also passed
separately.
- Final query-consolidation verification: 157 tests passed across five
files, including the unchanged 64-statement issue-detail census and
mixed approval/question batches:

  ```sh
  pnpm exec vitest run \

server/src/__tests__/{issue-detail-statements,task-interruption-query,assigned-work-invariant,assigned-work-recheck-idle}.test.ts
\
    server/src/services/task-interruption-presentation.test.ts
  ```
- `pnpm --filter @paperclipai/server typecheck` — passed, including test
layers.
- Manual-recovery follow-up: 204 tests passed across seven
projection/recovery/notice suites, and all 104 Inbox tests passed. The
lost-runner integration still starts exactly one run from a manual Retry
and leaves the question unanswered. The saved-decision census verifies
one decision batch query for a 41-task list, with at most three
statements referencing interactions; the overall 64-statement
issue-detail limit remains unchanged.
- `pnpm --filter @paperclipai/ui typecheck` — passed.
- `pnpm --filter @paperclipai/ui build` — passed.
- `node tests/runner-e2e/source-sdk-public-inputs.mjs $(git rev-parse
HEAD) --check` — passed.
- `pnpm verify` — exit 2: broader CI is required for changed global
inputs. Focused tests and package checks supply local evidence; broad CI
remains required.
- `git diff --check` and a local secret/instance-data scan — passed.
- Final CI on `1c16fe7c0d8`: [trusted CI
workflow](https://github.com/paperclipai/paperclip/actions/runs/44650155842)
passed, including all server shards, native-server verification, build,
canary, e2e, release registry, and serialized suites. All PR checks are
complete, with no failures; the branch is conflict-free.
- Greptile reviewed the latest revision, `1c16fe7c0d8`, at 5/5 with no
actionable findings or unresolved review threads.

The focused tests cover genuine provider failures, live work and
terminal tasks, unchanged task locks/retry delivery, preservation of
historical failures, and question resolution waking the affected task
while other active pending questions remain quiet. Review regressions
also cover dismissed/historical ordinary questions retaining Retry and
governed requests retaining their gates. No live task was retried or
answered for validation.

## Risks

- An active pending question now consistently blocks automatic
assigned-work recovery. Shared interaction eligibility determines when
ordinary conversation input becomes deferred or historical; governed
requests retain their authority. Automatic recovery no longer infers
that a newer Retry resolved an active question.
- Manual Retry keeps its existing admission policy and recovery checks.
The question notice preserves that supported action after a failed
attempt without changing approval or credential authority; pending saved
input prevents duplicate actions.
- The shared interruption action gains `question`; the task and inbox
consumers are updated together. The pending-question query returns only
task IDs and uses the existing company/task/status index.
- Provider cooldown policy, quotas, credentials, routing, cancellation
history, and durable retry timing are unchanged. The actual provider can
still return a rate limit at the recheck.
- No migration is required. Existing large owner modules receive small
localized changes; no code file crosses 1,000 lines.

## Model Used

OpenAI `gpt-6.1-sol` through the Codex harness in T3 Code, xhigh
reasoning effort, with code execution and tool use.

## 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 linked existing issues or described the issue in-PR
following the relevant issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links
- [x] My branch name describes the change and contains no internal
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>
2026-11-19 19:54:15 -08:00

333 lines
24 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { readFile } from "node:fs/promises";
import path from "node:path";
import { and, eq } from "drizzle-orm";
import { describe, expect, it, vi } from "vitest";
import type { AdapterSandboxExecutionTarget } from "@paperclipai/adapter-utils/execution-target";
import { activityLog, agentWakeupRequests, heartbeatRuns, issueComments, issueRecoveryActions, issueThreadInteractions, issues } from "@paperclipai/db";
import { appendHeartbeatRunEvent } from "./heartbeat-run-events.js";
import { heartbeatService } from "./heartbeat.js";
import { requestInterruptedNativeTurnRetry } from "./heartbeat/restart-interrupted-turn.js";
import { getExecutionBlocker } from "./execution-blocker.js";
import { taskInterruptionPresentation, taskInterruptionPresentations } from "./task-interruption-presentation.js";
import { pendingContinuationDecisionWaits } from "./explicit-native-continuation.js";
import { readSystemRecovery } from "./system-recovery-state.js";
import { reconcileRetainedWorkspaces } from "./workspace-recovery.js";
import { readTaskRetryIntent, requestTaskRetry } from "./task-retry.js";
import { buildExecutionContinuation } from "./execution-continuation.js";
import { issueThreadInteractionService } from "./issue-thread-interactions.js";
import { useWorkspaceRecoveryFixture, action, db, run, seed } from "./__tests__/workspace-recovery-fixture.js";
const targets = vi.hoisted(() => new Map<string, AdapterSandboxExecutionTarget>());
vi.mock("./environment-execution-target.js", async importOriginal => {
const actual = await importOriginal<typeof import("./environment-execution-target.js")>();
return { ...actual, resolveEnvironmentExecutionTarget: async (input: Parameters<typeof actual.resolveEnvironmentExecutionTarget>[0]) =>
(input.leaseId ? targets.get(input.leaseId) : undefined) ?? actual.resolveEnvironmentExecutionTarget(input) };
});
describe("a held task with a pending decision after its remote runner was lost", () => {
useWorkspaceRecoveryFixture(targets);
const shapes = {
parkedQuestion: { wakeReason: "issue_assigned", turnEvents: ["turn.started", "turn.accepted"],
kind: "ask_user_questions", fromFailedRun: true },
olderConnectionRequest: { wakeReason: "issue_monitor_due", turnEvents: [],
kind: "connection_intent", fromFailedRun: false },
} as const;
async function lostRunnerWithPendingDecision(shape: keyof typeof shapes) {
const s = shapes[shape];
const f = await seed("native");
const original = await run(f.runId);
const [wake] = await db.insert(agentWakeupRequests).values({ companyId: f.companyId, agentId: original.agentId,
source: "automation", triggerDetail: "system", reason: s.wakeReason, status: "failed", runId: f.runId,
finishedAt: new Date(), payload: { issueId: f.issueId } }).returning();
await db.update(heartbeatRuns).set({ errorCode: "native_continuation_requires_reconciliation",
error: "runner_remote_process_identity_unavailable", wakeupRequestId: wake.id, responsibleUserId: "local-board",
startedAt: new Date(Date.now() - 20 * 60_000), contextSnapshot: { issueId: f.issueId, wakeReason: s.wakeReason },
}).where(eq(heartbeatRuns.id, f.runId));
for (const eventType of s.turnEvents)
await appendHeartbeatRunEvent(db, { companyId: f.companyId, runId: f.runId, agentId: original.agentId,
eventType, stream: "system", level: "info", message: eventType });
const [older] = s.fromFailedRun ? [] : await db.insert(heartbeatRuns).values({ companyId: f.companyId,
agentId: original.agentId, issueId: f.issueId, status: "succeeded", createdAt: new Date(Date.now() - 2 * 86_400_000),
finishedAt: new Date(Date.now() - 86_400_000), contextSnapshot: { issueId: f.issueId } }).returning();
const [decision] = await db.insert(issueThreadInteractions).values({ companyId: f.companyId, issueId: f.issueId,
kind: s.kind, status: "pending", continuationPolicy: "wake_assignee", sourceRunId: older?.id ?? f.runId,
createdByAgentId: original.agentId, title: "Which default should new agents get?",
payload: { version: 1, questions: [{ id: "q1", prompt: "Which default?", selectionMode: "single",
options: [{ id: "a", label: "Read only" }, { id: "b", label: "Full" }] }] } as never }).returning();
const service = heartbeatService(db, { environmentRuntime: f.environmentRuntime });
await db.insert(heartbeatRuns).values({ companyId: f.companyId, agentId: original.agentId, status: "running" });
expect(await requestInterruptedNativeTurnRetry(db, f.runId)).toMatchObject({ requested: false });
await vi.waitFor(async () => {
await Promise.all(await reconcileRetainedWorkspaces(db, f.environmentRuntime, current => service.resumeRemoteStopComments(current)) ?? []);
expect(readSystemRecovery((await action(f.actionId)).evidence)?.state).toBe("complete");
}, { timeout: 10_000, interval: 10 });
expect(await readFile(path.join(f.host, "saved.txt"), "utf8")).toBe("finished work\n");
return { ...f, agentId: original.agentId, decision, service };
}
const queuedSuccessors = async (companyId: string) => db.select().from(heartbeatRuns)
.where(and(eq(heartbeatRuns.companyId, companyId), eq(heartbeatRuns.status, "queued")));
const deferredWaits = async (companyId: string) => (await db.select().from(agentWakeupRequests).where(and(
eq(agentWakeupRequests.companyId, companyId), eq(agentWakeupRequests.status, "deferred_issue_execution"))))
.map(wake => (wake.payload?.executionWait as { reason?: string } | undefined)?.reason);
type Decision = typeof issueThreadInteractions.$inferSelect;
const resolve = (decision: Decision, resolvedBy: string) => db.update(issueThreadInteractions).set({ status: "answered",
resolvedByUserId: resolvedBy, resolvedAt: new Date(), result: { version: 1, answers: [{ questionId: "q1", optionIds: ["a"] }] } as never })
.where(eq(issueThreadInteractions.id, decision.id));
const deliverAnswer = (f: Awaited<ReturnType<typeof lostRunnerWithPendingDecision>>, decision: Decision, userId: string) =>
f.service.wakeup(f.agentId, {
source: "automation", triggerDetail: "system", reason: "issue_commented",
payload: { issueId: f.issueId, interactionId: decision.id, interactionKind: decision.kind,
interactionStatus: "answered", sourceCommentId: null, sourceRunId: decision.sourceRunId, mutation: "interaction" },
idempotencyKey: `interaction:${decision.id}:answered`,
requestedByActorType: "user", requestedByActorId: userId,
contextSnapshot: { issueId: f.issueId, taskId: f.issueId, interactionId: decision.id,
interactionKind: decision.kind, interactionStatus: "answered", sourceCommentId: null,
sourceRunId: decision.sourceRunId, wakeReason: "issue_commented", source: "issue.interaction.resolve" },
});
const answer = async (f: Awaited<ReturnType<typeof lostRunnerWithPendingDecision>>, userId: string, resolvedBy = userId) => {
await resolve(f.decision, resolvedBy);
await deliverAnswer(f, f.decision, userId);
};
const dispatchContinuation = (f: Awaited<ReturnType<typeof lostRunnerWithPendingDecision>>, successor: typeof heartbeatRuns.$inferSelect) =>
buildExecutionContinuation({ db, companyId: f.companyId, issueId: f.issueId, agentId: f.agentId,
runId: successor.id, context: successor.contextSnapshot ?? {}, summary: null, exposeLowTrustRaw: false });
it.each(Object.keys(shapes) as Array<keyof typeof shapes>)("%s: Retry starts exactly one run after the sandbox work is saved", async shape => {
const f = await lostRunnerWithPendingDecision(shape);
const issue = (await db.select().from(issues).where(eq(issues.id, f.issueId)))[0];
expect(await getExecutionBlocker(db, f.companyId, f.issueId)).toMatchObject({ runId: f.runId, canRetry: true,
runError: "runner_remote_process_identity_unavailable", workspaceRecovery: { status: "recovered" } });
expect(await taskInterruptionPresentation(db, issue)).toMatchObject({ state: shape === "parkedQuestion" ? "waiting_for_user" : "failed",
userAction: shape === "parkedQuestion" ? "question" : null, canRetry: true, observedRunId: f.runId });
const receipt = await requestTaskRetry({ db, companyId: f.companyId, issueId: f.issueId, actorId: "local-board",
actorSource: "local_implicit", idempotencyKey: randomUUID(), observedRunId: f.runId });
expect(receipt).toMatchObject({ accepted: true });
await f.service.resumeTaskRetry(f.companyId, receipt.requestId!);
expect({ successors: (await queuedSuccessors(f.companyId)).length, deferred: await deferredWaits(f.companyId) })
.toEqual({ successors: 1, deferred: [] });
const [successor] = await queuedSuccessors(f.companyId);
expect(successor.contextSnapshot).toMatchObject({ previousRunId: f.runId, taskRetryRequestId: receipt.requestId, forceFreshSession: true });
expect((await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, f.decision.id)))[0].status).toBe("pending");
}, 60_000);
it.each(Object.keys(shapes) as Array<keyof typeof shapes>)("%s: the person's answer to the pending decision continues the task", async shape => {
const f = await lostRunnerWithPendingDecision(shape);
await answer(f, "local-board");
expect({ successors: (await queuedSuccessors(f.companyId)).length, deferred: await deferredWaits(f.companyId) })
.toEqual({ successors: 1, deferred: [] });
const [successor] = await queuedSuccessors(f.companyId);
expect(successor.contextSnapshot).toMatchObject({ interactionId: f.decision.id, previousRunId: f.runId, forceFreshSession: true,
explicitUserContinuation: { previousRunId: f.runId, commentId: null } });
expect(await action(f.actionId)).toMatchObject({ status: "resolved",
evidence: { explicitUserContinuation: { actorId: "local-board", interactionId: f.decision.id, previousRunId: f.runId } } });
expect(await db.select().from(activityLog).where(and(eq(activityLog.companyId, f.companyId),
eq(activityLog.action, "issue.execution_recovery_settled"), eq(activityLog.actorId, "local-board")))).toHaveLength(1);
const envelope = await dispatchContinuation(f, successor);
expect(envelope).toMatchObject({ trigger: { interactionId: f.decision.id }, interruptedRunId: f.runId });
expect(envelope.interactionOutcomes).toEqual(expect.arrayContaining([expect.objectContaining({ id: f.decision.id })]));
}, 60_000);
it("dispatches the first answer's successor after a second answer coalesces into it", async () => {
const f = await lostRunnerWithPendingDecision("parkedQuestion");
const [second] = await db.insert(issueThreadInteractions).values({ companyId: f.companyId, issueId: f.issueId,
kind: "ask_user_questions", status: "pending", continuationPolicy: "wake_assignee", sourceRunId: f.runId,
createdByAgentId: f.agentId, title: "Which audit level?", payload: f.decision.payload }).returning();
await resolve(f.decision, "local-board");
await resolve(second, "local-board");
await deliverAnswer(f, f.decision, "local-board");
await deliverAnswer(f, second, "local-board");
const successors = await queuedSuccessors(f.companyId);
expect(successors).toHaveLength(1);
const [secondWake] = await db.select().from(agentWakeupRequests).where(and(eq(agentWakeupRequests.companyId, f.companyId),
eq(agentWakeupRequests.idempotencyKey, `interaction:${second.id}:answered`)));
expect(secondWake).toMatchObject({ status: "deferred_issue_execution", runId: null });
for (const context of [successors[0].contextSnapshot ?? {},
{ ...successors[0].contextSnapshot, interactionId: second.id, interactionStatus: "answered" }]) {
const envelope = await dispatchContinuation(f, { ...successors[0], contextSnapshot: context });
expect(envelope.interactionOutcomes).toEqual(expect.arrayContaining([
expect.objectContaining({ id: f.decision.id }), expect.objectContaining({ id: second.id })]));
}
}, 60_000);
it.each([
["a plan review", "request_confirmation", { version: 1, prompt: "Approve the plan?", target: { type: "issue_document", key: "plan", revisionId: randomUUID() } }],
["a governed tool action", "request_confirmation", { version: 1, prompt: "Run it?", toolAction: { tool: "deploy" } }],
["a secret proposal", "request_confirmation", { version: 1, prompt: "Store it?", secretProposal: { name: "TOKEN" } }],
["a connection authorization", "connection_intent", { version: 1, serviceName: "Sentry", connectionAuthorization: { scope: "read" } }],
])("keeps Retry on hold while %s is pending", async (_label, kind, payload) => {
const f = await lostRunnerWithPendingDecision("parkedQuestion");
await db.update(issueThreadInteractions).set({ kind, payload: payload as never })
.where(eq(issueThreadInteractions.id, f.decision.id));
const receipt = await requestTaskRetry({ db, companyId: f.companyId, issueId: f.issueId, actorId: "local-board",
actorSource: "local_implicit", idempotencyKey: randomUUID(), observedRunId: f.runId });
await f.service.resumeTaskRetry(f.companyId, receipt.requestId!);
expect({ successors: (await queuedSuccessors(f.companyId)).length, deferred: await deferredWaits(f.companyId) })
.toEqual({ successors: 0, deferred: ["decision_pending"] });
}, 60_000);
it("lets a new message supersede a plain question and continue the task", async () => {
const f = await lostRunnerWithPendingDecision("parkedQuestion");
const [comment] = await db.insert(issueComments).values({ companyId: f.companyId, issueId: f.issueId,
authorType: "user", authorUserId: "local-board", body: "Please continue." }).returning();
await f.service.wakeup(f.agentId, {
source: "automation", triggerDetail: "system", reason: "issue_commented",
payload: { issueId: f.issueId, commentId: comment.id, mutation: "comment" },
requestedByActorType: "user", requestedByActorId: "local-board",
contextSnapshot: { issueId: f.issueId, taskId: f.issueId, wakeReason: "issue_commented", wakeCommentId: comment.id },
});
expect({ successors: (await queuedSuccessors(f.companyId)).length, deferred: await deferredWaits(f.companyId) })
.toEqual({ successors: 1, deferred: [] });
}, 60_000);
it("keeps a new message waiting while a plan review is pending", async () => {
const f = await lostRunnerWithPendingDecision("parkedQuestion");
await db.update(issueThreadInteractions).set({ kind: "request_confirmation",
payload: { version: 1, prompt: "Approve the plan?", target: { type: "issue_document", key: "plan", revisionId: randomUUID() } } as never })
.where(eq(issueThreadInteractions.id, f.decision.id));
const [comment] = await db.insert(issueComments).values({ companyId: f.companyId, issueId: f.issueId,
authorType: "user", authorUserId: "local-board", body: "Please continue." }).returning();
await f.service.wakeup(f.agentId, {
source: "automation", triggerDetail: "system", reason: "issue_commented",
payload: { issueId: f.issueId, commentId: comment.id, mutation: "comment" },
requestedByActorType: "user", requestedByActorId: "local-board",
contextSnapshot: { issueId: f.issueId, taskId: f.issueId, wakeReason: "issue_commented", wakeCommentId: comment.id },
});
expect({ successors: (await queuedSuccessors(f.companyId)).length, deferred: await deferredWaits(f.companyId) })
.toEqual({ successors: 0, deferred: ["decision_pending"] });
}, 60_000);
it("keeps the hold when the wake's user did not give the answer", async () => {
const f = await lostRunnerWithPendingDecision("parkedQuestion");
await answer(f, "other-user", "local-board");
expect(await queuedSuccessors(f.companyId)).toHaveLength(0);
expect(await deferredWaits(f.companyId)).toEqual(["execution_recovery"]);
expect((await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.id, f.actionId)))[0].evidence)
.not.toHaveProperty("explicitUserContinuation");
}, 60_000);
describe("a Retry held on a confirmation the person resolves", () => {
const heldOnConfirmation = async () => {
const f = await lostRunnerWithPendingDecision("parkedQuestion");
await db.update(issueThreadInteractions).set({ kind: "request_confirmation",
payload: { version: 1, prompt: "Review sidebar subtask screenshots" } as never })
.where(eq(issueThreadInteractions.id, f.decision.id));
const receipt = await requestTaskRetry({ db, companyId: f.companyId, issueId: f.issueId, actorId: "local-board",
actorSource: "local_implicit", idempotencyKey: randomUUID(), observedRunId: f.runId });
await f.service.resumeTaskRetry(f.companyId, receipt.requestId!);
expect({ successors: (await queuedSuccessors(f.companyId)).length, deferred: await deferredWaits(f.companyId) })
.toEqual({ successors: 0, deferred: ["decision_pending"] });
return { ...f, requestId: receipt.requestId! };
};
const savedResolution = (f: Awaited<ReturnType<typeof heldOnConfirmation>>) =>
db.update(issueThreadInteractions).set({ status: "rejected", resolvedByUserId: "local-board", resolvedAt: new Date(),
result: { version: 1, outcome: "rejected", reason: "what" } as never })
.where(eq(issueThreadInteractions.id, f.decision.id));
const currentIssue = async (issueId: string) => (await db.select().from(issues).where(eq(issues.id, issueId)))[0];
const retryReceipt = async (id: string) => (await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, id)))[0];
it.each(["accept", "reject"] as const)("releases the held Retry as soon as the person chooses %s", async choice => {
const f = await heldOnConfirmation();
const interactions = issueThreadInteractionService(db);
const issue = await currentIssue(f.issueId);
if (choice === "accept") await interactions.acceptInteraction(issue, f.decision.id, {}, { userId: "local-board" });
else await interactions.rejectInteraction(issue, f.decision.id, { reason: "what" }, { userId: "local-board" });
expect((await retryReceipt(f.requestId)).payload).not.toHaveProperty("executionWait");
expect(await deferredWaits(f.companyId)).not.toContain("decision_pending");
await f.service.resumeTaskRetry(f.companyId, f.requestId);
const [successor] = await queuedSuccessors(f.companyId);
expect(successor?.contextSnapshot).toMatchObject({ previousRunId: f.runId, taskRetryRequestId: f.requestId });
}, 60_000);
it("accepts the confirmation while the held Retry is admitted, without either aborting", async () => {
const f = await heldOnConfirmation();
const issue = await currentIssue(f.issueId);
const outcomes = await Promise.allSettled([
issueThreadInteractionService(db).acceptInteraction(issue, f.decision.id, {}, { userId: "local-board" }),
f.service.resumeTaskRetry(f.companyId, f.requestId),
f.service.resumeTaskRetry(f.companyId, f.requestId),
]);
expect(outcomes.filter(outcome => outcome.status === "rejected")).toEqual([]);
await f.service.resumeTaskRetry(f.companyId, f.requestId);
expect({ successors: (await queuedSuccessors(f.companyId)).length, deferred: await deferredWaits(f.companyId) })
.toEqual({ successors: 1, deferred: [] });
}, 60_000);
it("never shows a resolved confirmation as the Retry's wait, and both projections offer the same Retry", async () => {
const f = await heldOnConfirmation();
await savedResolution(f);
await db.update(agentWakeupRequests).set({ requestedAt: new Date(Date.now() - 5 * 60_000) })
.where(eq(agentWakeupRequests.id, f.requestId));
const interruption = await taskInterruptionPresentation(db, await currentIssue(f.issueId));
const blocker = await getExecutionBlocker(db, f.companyId, f.issueId);
expect(interruption).toMatchObject({ state: "failed", canRetry: true });
expect(interruption?.retryWait).toBeUndefined();
expect(blocker?.canRetry).toBe(interruption?.canRetry);
}, 60_000);
it("a person's comment starts work and settles the held Retry once nothing is left to answer", async () => {
const f = await heldOnConfirmation();
await savedResolution(f);
const [comment] = await db.insert(issueComments).values({ companyId: f.companyId, issueId: f.issueId,
authorType: "user", authorUserId: "local-board", body: "answer me" }).returning();
await f.service.wakeup(f.agentId, {
source: "automation", triggerDetail: "system", reason: "issue_commented",
payload: { issueId: f.issueId, commentId: comment.id, mutation: "comment" },
requestedByActorType: "user", requestedByActorId: "local-board",
contextSnapshot: { issueId: f.issueId, taskId: f.issueId, wakeReason: "issue_commented", wakeCommentId: comment.id },
});
expect({ successors: (await queuedSuccessors(f.companyId)).length, deferred: await deferredWaits(f.companyId) })
.toEqual({ successors: 1, deferred: [] });
expect(await retryReceipt(f.requestId)).toMatchObject({ status: "skipped", error: "task_retry_superseded" });
}, 60_000);
it("starts a Retry at once when the confirmation was already open before the failed turn started", async () => {
const f = await lostRunnerWithPendingDecision("parkedQuestion");
const failed = await run(f.runId);
await db.update(issueThreadInteractions).set({ kind: "request_confirmation",
payload: { version: 1, prompt: "Review sidebar subtask screenshots" } as never,
createdAt: new Date(failed.createdAt.getTime() - 6 * 60_000) })
.where(eq(issueThreadInteractions.id, f.decision.id));
const receipt = await requestTaskRetry({ db, companyId: f.companyId, issueId: f.issueId, actorId: "local-board",
actorSource: "local_implicit", idempotencyKey: randomUUID(), observedRunId: f.runId });
await f.service.resumeTaskRetry(f.companyId, receipt.requestId!);
expect({ successors: (await queuedSuccessors(f.companyId)).length, deferred: await deferredWaits(f.companyId) })
.toEqual({ successors: 1, deferred: [] });
}, 60_000);
it("keeps question and saved decision reads bounded on a task list", async () => {
const f = await heldOnConfirmation();
const source = await currentIssue(f.issueId);
const listed = await db.insert(issues).values(Array.from({ length: 40 }, (_, index) => ({
...source, id: randomUUID(), identifier: `${source.identifier}-${index}`, issueNumber: (source.issueNumber ?? 0) + 1000 + index,
executionRunId: null, checkoutRunId: null,
}))).returning();
await db.insert(agentWakeupRequests).values(listed.map(issue => ({ companyId: f.companyId, agentId: f.agentId,
source: "automation", triggerDetail: "system", reason: "issue_commented", status: "deferred_issue_execution",
requestedByActorType: "user", requestedByActorId: "local-board",
payload: { issueId: issue.id, executionWait: { reason: "decision_pending", message: "held" } } })));
const open = listed.slice(0, 20);
await db.insert(issueThreadInteractions).values(open.map(issue => ({ companyId: f.companyId, issueId: issue.id,
kind: "request_confirmation", status: "pending", continuationPolicy: "wake_assignee", createdByAgentId: f.agentId,
payload: { version: 1, prompt: "Review it?" } as never })));
const statements: string[] = [];
const client = db.$client.options;
const previous = client.debug;
client.debug = (_connection, query) => { if (query.includes("issue_thread_interactions")) statements.push(query); };
try {
await taskInterruptionPresentations(db, f.companyId, [source, ...listed]);
} finally { client.debug = previous; }
expect(statements.length).toBeLessThanOrEqual(3);
expect(statements.filter(query => /\bas held\s*\(/.test(query))).toHaveLength(1);
const waits = await db.select().from(agentWakeupRequests).where(and(eq(agentWakeupRequests.companyId, f.companyId),
eq(agentWakeupRequests.status, "deferred_issue_execution")));
const held = await pendingContinuationDecisionWaits(db, f.companyId, waits.map(wait => ({ id: wait.id,
issueId: String(wait.payload?.issueId), actorId: wait.requestedByActorId!,
taskRetrySourceRunId: readTaskRetryIntent(wait.payload)?.sourceRunId ?? null })));
const heldIssues = new Set(waits.filter(wait => held.has(wait.id)).map(wait => String(wait.payload?.issueId)));
expect(heldIssues).toEqual(new Set([f.issueId, ...open.map(issue => issue.id)]));
}, 60_000);
});
});