Files
PaperClipAI/server/src/__tests__/question-response-delivery.test.ts
T
Dotta bc9ba7cd26 feat(runner): project native runs into task threads (#12321)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - The experimental Paperclip Runner can execute a guarded Codex run
and persist provider-neutral events.
> - The task page still reads direct-adapter transcripts and cannot
present those native events.
> - Structured runner questions must also use the existing task
interaction experience.
> - Runtime selection must use the persisted run mode, not an adapter
name or a current feature flag.
> - This pull request projects native events and questions into the
existing task thread.
> - Direct adapters keep their existing transcript, composer,
interaction, and finalization paths.
> - The benefit is a complete native Codex task thread without a
behavior change for existing adapters.

## Linked Issues or Issue Description

Refs #12202. This pull request replaces that stale implementation on
current `master`.

**What happened?**

The server persists native runner events and structured input requests.
The task page only consumes direct-adapter transcripts. A native run
therefore cannot present a complete transcript, usage, or question flow
through the normal task experience.

**Expected behavior**

Native runs project persisted provider-neutral events into the existing
task thread. Native structured questions use the existing interaction
card. Direct adapters retain their current behavior.

**Steps to reproduce**

1. Enable the experimental runner.
2. Start a native Codex run that emits progress, usage, a structured
question, and a final reply.
3. Open the task page.
4. Observe that the direct-adapter transcript path cannot project the
native event records.

**Paperclip version or commit**

`master` at `67f9867bc`.

## What Changed

- Add the canonical structured-question validator and shared contract
exports.
- Materialize native input requests as existing task interactions.
- Validate native answers and deliver them through the durable
question-response receipt.
- Resume the original PRP request with an idempotent `request.resolve`
command.
- Project native messages, tool activity, cumulative usage, and final
replies into the existing transcript model.
- Propagate persisted `runtimeMode` to the task page and select native
handling only for `runtimeMode: "native"`.
- Expire pending interactions through the shared issue service on every
terminal transition, including decisions, stalled reviews, tree control,
and pipeline retry cleanup.
- Queue native run cancellation while a transaction is open and execute
it only after the owning transaction commits.
- Keep nonterminal and non-runner issue paths on their existing service
call shapes and behavior.

## Verification

- `pnpm --filter @paperclipai/server typecheck` — passed, including the
Rust runner release build and protocol/catalog drift gates.
- Focused native-thread and lifecycle suites — 18 files and 481 tests
passed during review.
- `issue-execution-policy-routes.test.ts` — 19/19 passed after the final
transactional-queue expectation update.
- `issue-agent-mutation-ownership-routes.test.ts` — 87/87 passed in the
final isolated compatibility rerun.
- GitHub Actions — policy, build, canary, typecheck/release registry, 5
serialized server shards, 8 general-test shards, 3 browser shards, and
both aggregate gates passed on `7793f3193`.
- Security — Snyk, Socket Project Report, Socket PR Alerts, and
Superagent passed.
- Greptile — 5/5 on `7793f3193`; all actionable review threads resolved.
- `git diff --check` — passed.
- Diff against `master`: 44 files.

## Compatibility Boundary

- Native transcript polling only runs when the persisted run reports
`runtimeMode: "native"`.
- Missing or legacy runtime modes continue through
`useLiveRunTranscripts`.
- Legacy questions keep the existing optional free-text choice.
- Native closed select sets can suppress that legacy fallback.
- Terminal cleanup uses the same issue service for native and legacy
interactions; only a bound native question schedules a native run
cancellation.
- Native cancellation happens after transaction commit, so failed or
rolled-back writes do not cancel a still-valid run.
- The durable delivery service checks the original native request before
it considers a continuation run.
- This pull request adds no migration, dependency, workflow, manifest,
or lockfile change.

## Risks

The main risk is routing a direct-adapter task through native handling
or changing terminal issue behavior. The implementation selects the
native path only from persisted runtime facts, retains the existing
nonterminal call shape, and schedules native cancellation only for a
validated bound native question after commit. Focused and
repository-wide tests cover both paths. Native requests remain bound to
the company, issue, run, and agent; answers are validated, durable, and
idempotent across reconnects.

## Model Used

OpenAI Codex, GPT-5 family. The client does not expose the exact
deployment ID or context window. Agentic 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 and contains no internal
Paperclip ticket id or instance-derived details
- [x] I have run the affected local tests and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated the compatibility notes for this change
- [x] I have considered and documented risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I addressed all Greptile and reviewer comments before requesting
merge
2026-08-29 19:26:20 -05:00

811 lines
30 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { eq } from "drizzle-orm";
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import {
activityLog,
agentWakeupRequests,
agents,
companies,
createDb,
goals,
heartbeatRuns,
issueQuestionResponseDeliveries,
issueThreadInteractions,
issues,
} from "@paperclipai/db";
import {
getEmbeddedPostgresTestSupport,
startEmbeddedPostgresTestDatabase,
} from "./helpers/embedded-postgres.js";
import { issueThreadInteractionService } from "../services/issue-thread-interactions.js";
import {
buildQuestionResponseDeliveryEnvelope,
formatQuestionResponseSteeringMessage,
questionResponseDeliveryService,
} from "../services/question-response-delivery.js";
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
const DIRECT_ADAPTER_TYPES = [
"acpx_local",
"claude_local",
"codex_local",
"cursor_cloud",
"cursor",
"gemini_local",
"grok_local",
"hermes_gateway",
"hermes_local",
"kimi_local",
"openclaw_gateway",
"opencode_local",
"pi_local",
"process",
"http",
"external_test_adapter",
] as const;
describeEmbeddedPostgres("question response delivery", () => {
let db!: ReturnType<typeof createDb>;
let tempDb: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | null = null;
beforeAll(async () => {
tempDb = await startEmbeddedPostgresTestDatabase("paperclip-question-delivery-");
db = createDb(tempDb.connectionString);
}, 20_000);
afterEach(async () => {
await db.delete(issueQuestionResponseDeliveries);
await db.delete(issueThreadInteractions);
await db.delete(activityLog);
await db.delete(heartbeatRuns);
await db.delete(agentWakeupRequests);
await db.delete(issues);
await db.delete(goals);
await db.delete(agents);
await db.delete(companies);
});
afterAll(async () => {
await tempDb?.cleanup();
});
async function seed(args: {
adapterType?: string;
runtimeMode?: "legacy" | "native";
sourceStatus?: string;
successorStatus?: "queued" | "running";
} = {}) {
const companyId = randomUUID();
const agentId = randomUUID();
const goalId = randomUUID();
const issueId = randomUUID();
const sourceRunId = randomUUID();
const successorRunId = args.successorStatus ? randomUUID() : null;
await db.insert(companies).values({
id: companyId,
name: "Question delivery",
issuePrefix: `Q${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
requireBoardApprovalForNewAgents: false,
});
await db.insert(agents).values({
id: agentId,
companyId,
name: "Runner",
role: "engineer",
status: "active",
adapterType: args.adapterType ?? "codex_local",
adapterConfig: {},
runtimeConfig: {},
permissions: {},
});
await db.insert(goals).values({ id: goalId, companyId, title: "Test", level: "task", status: "active" });
await db.insert(issues).values({
id: issueId,
companyId,
goalId,
title: "Deliver answers",
status: "in_progress",
priority: "medium",
assigneeAgentId: agentId,
});
await db.insert(heartbeatRuns).values({
id: sourceRunId,
companyId,
agentId,
invocationSource: "manual",
status: args.sourceStatus ?? "succeeded",
runtimeMode: args.runtimeMode ?? "native",
driverKind: "codex",
contextSnapshot: { issueId },
...(args.sourceStatus === "running" ? { startedAt: new Date() } : { finishedAt: new Date() }),
});
if (successorRunId && args.successorStatus) {
await db.insert(heartbeatRuns).values({
id: successorRunId,
companyId,
agentId,
invocationSource: "manual",
status: args.successorStatus,
runtimeMode: args.runtimeMode ?? "native",
driverKind: "codex",
contextSnapshot: { issueId },
...(args.successorStatus === "running" ? { startedAt: new Date() } : {}),
});
}
const interactionSvc = issueThreadInteractionService(db);
const interaction = await interactionSvc.create(
{ id: issueId, companyId },
{
kind: "ask_user_questions",
continuationPolicy: "wake_assignee",
sourceRunId,
payload: {
version: 1,
title: "Server choices",
questions: [
{ id: "purpose", prompt: "What is it for?", selectionMode: "single", required: true, options: [{ id: "custom", label: "Write an answer", freeText: true }] },
{ id: "runtime", prompt: "Which runtime?", selectionMode: "single", required: true, options: [{ id: "node", label: "Node.js" }, { id: "bun", label: "Bun" }] },
{ id: "features", prompt: "Which features?", selectionMode: "multi", options: [{ id: "health", label: "Health check" }, { id: "logs", label: "Request logs" }] },
],
questionSet: {
schema: "paperclip.question_set.v1",
title: "Server choices",
questions: [
{ id: "purpose", header: "Purpose", prompt: "What is it for?", required: true, answerMode: "text" },
{ id: "runtime", header: "Runtime", prompt: "Which runtime?", required: true, answerMode: "single_select", options: [{ id: "node", label: "Node.js" }, { id: "bun", label: "Bun" }] },
{ id: "features", header: "Features", prompt: "Which features?", required: false, answerMode: "multi_select", options: [{ id: "health", label: "Health check" }, { id: "logs", label: "Request logs" }], customAnswer: { enabled: true, label: "Other" } },
],
},
},
},
{ agentId, runId: sourceRunId },
);
const answered = await interactionSvc.answerQuestions(
{ id: issueId, companyId, status: "in_progress" },
interaction.id,
{ answers: [
{ questionId: "purpose", optionIds: [], otherText: "Internal API" },
{ questionId: "runtime", optionIds: ["node"] },
{ questionId: "features", optionIds: ["health", "logs"], otherText: "Metrics" },
] },
{ userId: "board-user" },
);
return { companyId, agentId, issueId, sourceRunId, successorRunId, interaction: answered };
}
it("persists the receipt atomically and steers exactly once into a running successor", async () => {
const seeded = await seed({ successorStatus: "running" });
const newerRunId = randomUUID();
await db.insert(heartbeatRuns).values({
id: newerRunId,
companyId: seeded.companyId,
agentId: seeded.agentId,
invocationSource: "manual",
status: "running",
runtimeMode: "native",
driverKind: "codex",
contextSnapshot: { issueId: seeded.issueId },
startedAt: new Date(),
});
await db.update(issues).set({ executionRunId: seeded.successorRunId })
.where(eq(issues.id, seeded.issueId));
const persistedBeforeDelivery = await db.select().from(issueQuestionResponseDeliveries)
.where(eq(issueQuestionResponseDeliveries.interactionId, seeded.interaction.id))
.then((rows) => rows[0]);
expect(persistedBeforeDelivery).toMatchObject({
status: "pending",
correlationId: `question-response:${seeded.interaction.id}`,
sourceRunId: seeded.sourceRunId,
});
const steer = vi.fn().mockResolvedValue({ turnId: "turn-successor" });
const wakeup = vi.fn();
const service = questionResponseDeliveryService(db, {
heartbeat: { wakeup } as never,
steer,
});
const first = await service.deliver(seeded.interaction.id);
const second = await service.deliver(seeded.interaction.id);
expect(first).toMatchObject({
status: "delivered",
mode: "steered",
targetRunId: seeded.successorRunId,
targetTurnId: "turn-successor",
duplicate: false,
});
expect(second).toMatchObject({ mode: "steered", duplicate: true });
expect(steer).toHaveBeenCalledTimes(1);
expect(steer).toHaveBeenCalledWith(expect.objectContaining({
runId: seeded.successorRunId,
correlationId: `question-response:${seeded.interaction.id}`,
message: expect.stringContaining("- Runtime — Which runtime?: Node.js"),
}));
expect(wakeup).not.toHaveBeenCalled();
const [delivery] = await db.select().from(issueQuestionResponseDeliveries);
expect(delivery).toMatchObject({
status: "delivered",
deliveryMode: "steered",
targetRunId: seeded.successorRunId,
targetTurnId: "turn-successor",
attemptCount: 1,
});
const deliveryEvents = await db.select().from(activityLog)
.where(eq(activityLog.action, "issue.question_response_delivered"));
expect(deliveryEvents).toHaveLength(1);
expect(JSON.stringify(deliveryEvents[0]?.details)).not.toContain("Internal API");
expect(JSON.stringify(deliveryEvents[0]?.details)).not.toContain("Node.js");
});
it("resolves an in-flight native input request before creating a continuation", async () => {
const seeded = await seed({
adapterType: "paperclip_runner",
runtimeMode: "native",
sourceStatus: "running",
});
const wakeup = vi.fn();
const resolveNativeQuestion = vi.fn().mockResolvedValue("queued" as const);
const outcome = await questionResponseDeliveryService(db, {
heartbeat: { wakeup } as never,
resolveNativeQuestion,
}).deliver(seeded.interaction.id);
expect(outcome).toMatchObject({
status: "delivered",
mode: "steered",
targetRunId: seeded.sourceRunId,
});
expect(resolveNativeQuestion).toHaveBeenCalledWith(expect.objectContaining({
id: seeded.interaction.id,
status: "answered",
}));
expect(wakeup).not.toHaveBeenCalled();
});
it("keeps native input delivery pending while its PRP session is unavailable", async () => {
const seeded = await seed({
adapterType: "paperclip_runner",
runtimeMode: "native",
sourceStatus: "running",
});
const wakeup = vi.fn();
const outcome = await questionResponseDeliveryService(db, {
heartbeat: { wakeup } as never,
resolveNativeQuestion: vi.fn().mockResolvedValue("pending" as const),
}).deliver(seeded.interaction.id);
expect(outcome).toBeNull();
expect(wakeup).not.toHaveBeenCalled();
const [delivery] = await db.select().from(issueQuestionResponseDeliveries);
expect(delivery).toMatchObject({
status: "pending",
attemptCount: 1,
errorCount: 0,
lastErrorCode: "native_question_session_unavailable",
});
});
it("coalesces into a queued successor without creating another wake", async () => {
const seeded = await seed({ successorStatus: "queued" });
const successor = await db.select().from(heartbeatRuns)
.where(eq(heartbeatRuns.id, seeded.successorRunId!))
.then((rows) => rows[0]!);
const wakeup = vi.fn().mockResolvedValue(successor);
const steer = vi.fn();
const outcome = await questionResponseDeliveryService(db, {
heartbeat: { wakeup } as never,
steer,
}).deliver(seeded.interaction.id);
expect(outcome).toMatchObject({ status: "delivered", mode: "coalesced", targetRunId: successor.id });
expect(steer).not.toHaveBeenCalled();
expect(wakeup).toHaveBeenCalledTimes(1);
expect(wakeup.mock.calls[0]?.[1]).toMatchObject({
idempotencyKey: `question-response:${seeded.interaction.id}`,
contextSnapshot: {
interactionId: seeded.interaction.id,
interactionStatus: "answered",
},
});
});
it("never steers into the source run and keeps a skipped wake retryable", async () => {
const seeded = await seed({ sourceStatus: "running" });
const wakeup = vi.fn().mockResolvedValue(null);
const steer = vi.fn();
const outcome = await questionResponseDeliveryService(db, {
heartbeat: { wakeup } as never,
steer,
}).deliver(seeded.interaction.id);
expect(outcome).toBeNull();
expect(steer).not.toHaveBeenCalled();
expect(wakeup).toHaveBeenCalledTimes(1);
expect(JSON.stringify(wakeup.mock.calls[0])).not.toContain("Internal API");
expect(JSON.stringify(wakeup.mock.calls[0])).not.toContain("Node.js");
const [delivery] = await db.select().from(issueQuestionResponseDeliveries);
expect(delivery).toMatchObject({
status: "pending",
attemptCount: 1,
lastErrorCode: "question_response_wake_skipped",
});
});
it("delivers after wake suppression outlasts the bounded error retry limit", async () => {
const seeded = await seed({ sourceStatus: "running" });
const fallbackRunId = randomUUID();
let wakeAttempts = 0;
const wakeup = vi.fn().mockImplementation(async () => {
wakeAttempts += 1;
if (wakeAttempts <= 5) return null;
return db.insert(heartbeatRuns).values({
id: fallbackRunId,
companyId: seeded.companyId,
agentId: seeded.agentId,
invocationSource: "automation",
status: "queued",
runtimeMode: "legacy",
driverKind: "codex",
contextSnapshot: { issueId: seeded.issueId },
}).returning().then((rows) => rows[0]!);
});
const service = questionResponseDeliveryService(db, {
heartbeat: { wakeup } as never,
steer: vi.fn(),
});
for (let attempt = 0; attempt < 5; attempt += 1) {
await expect(service.deliver(seeded.interaction.id)).resolves.toBeNull();
}
const delivered = await service.deliver(seeded.interaction.id);
expect(delivered).toMatchObject({
status: "fallback_queued",
mode: "wake_fallback",
targetRunId: fallbackRunId,
});
const [delivery] = await db.select().from(issueQuestionResponseDeliveries);
expect(delivery).toMatchObject({
status: "fallback_queued",
attemptCount: 6,
errorCount: 0,
targetRunId: fallbackRunId,
});
});
it("preserves the actual-error retry budget after prolonged wake suppression", async () => {
const seeded = await seed({ sourceStatus: "running" });
const fallbackRunId = randomUUID();
let wakeAttempts = 0;
const wakeup = vi.fn().mockImplementation(async () => {
wakeAttempts += 1;
if (wakeAttempts <= 5) return null;
if (wakeAttempts === 6) throw new Error("scheduler temporarily unavailable");
return db.insert(heartbeatRuns).values({
id: fallbackRunId,
companyId: seeded.companyId,
agentId: seeded.agentId,
invocationSource: "automation",
status: "queued",
runtimeMode: "legacy",
driverKind: "codex",
contextSnapshot: { issueId: seeded.issueId },
}).returning().then((rows) => rows[0]!);
});
const service = questionResponseDeliveryService(db, {
heartbeat: { wakeup } as never,
steer: vi.fn(),
});
for (let attempt = 0; attempt < 5; attempt += 1) {
await expect(service.deliver(seeded.interaction.id)).resolves.toBeNull();
}
await expect(service.deliver(seeded.interaction.id)).resolves.toBeNull();
const [afterError] = await db.select().from(issueQuestionResponseDeliveries);
expect(afterError).toMatchObject({
status: "pending",
attemptCount: 6,
errorCount: 1,
lastErrorCode: "scheduler temporarily unavailable",
});
await expect(service.deliver(seeded.interaction.id)).resolves.toMatchObject({
status: "fallback_queued",
targetRunId: fallbackRunId,
});
const [delivered] = await db.select().from(issueQuestionResponseDeliveries);
expect(delivered).toMatchObject({
status: "fallback_queued",
attemptCount: 7,
errorCount: 1,
targetRunId: fallbackRunId,
});
});
it("enforces one durable wake per question-response idempotency key", async () => {
const seeded = await seed({ sourceStatus: "running" });
const idempotencyKey = `question-response:${seeded.interaction.id}`;
const request = {
companyId: seeded.companyId,
agentId: seeded.agentId,
source: "automation",
triggerDetail: "system",
reason: "issue_commented",
idempotencyKey,
} as const;
await db.insert(agentWakeupRequests).values({ ...request, status: "queued" });
await expect(db.insert(agentWakeupRequests).values({
...request,
status: "coalesced",
})).rejects.toMatchObject({ cause: { code: "23505" } });
// Suppression receipts are intentionally outside the fence so the outbox
// can retry after scheduling is enabled again.
await expect(db.insert(agentWakeupRequests).values({
...request,
status: "skipped",
finishedAt: new Date(),
})).resolves.toBeDefined();
});
it("reuses the winning wake when a concurrent insert hits the idempotency fence", async () => {
const seeded = await seed({ sourceStatus: "running" });
const fallbackRunId = randomUUID();
const wakeup = vi.fn().mockImplementation(async (
_agentId: string,
options: { idempotencyKey?: string | null },
) => {
const request = {
companyId: seeded.companyId,
agentId: seeded.agentId,
source: "automation",
triggerDetail: "system",
reason: "issue_commented",
idempotencyKey: options.idempotencyKey,
} as const;
const [winner] = await db.insert(agentWakeupRequests).values({
...request,
status: "queued",
}).returning();
await db.insert(heartbeatRuns).values({
id: fallbackRunId,
companyId: seeded.companyId,
agentId: seeded.agentId,
invocationSource: "automation",
status: "queued",
runtimeMode: "legacy",
driverKind: "codex",
wakeupRequestId: winner!.id,
contextSnapshot: { issueId: seeded.issueId },
});
await db.update(agentWakeupRequests).set({ runId: fallbackRunId })
.where(eq(agentWakeupRequests.id, winner!.id));
// Model the losing claimant reaching the same transactional insert after
// the winner commits. The service must recover the winner's receipt.
await db.insert(agentWakeupRequests).values({
...request,
status: "coalesced",
});
throw new Error("unreachable");
});
const outcome = await questionResponseDeliveryService(db, {
heartbeat: { wakeup } as never,
steer: vi.fn(),
}).deliver(seeded.interaction.id);
expect(outcome).toMatchObject({
status: "fallback_queued",
mode: "wake_fallback",
targetRunId: fallbackRunId,
});
expect(wakeup).toHaveBeenCalledTimes(1);
const [delivery] = await db.select().from(issueQuestionResponseDeliveries);
expect(delivery).toMatchObject({
status: "fallback_queued",
attemptCount: 1,
errorCount: 0,
targetRunId: fallbackRunId,
});
});
it("reuses a durable wake receipt instead of issuing a duplicate continuation", async () => {
const seeded = await seed({ sourceStatus: "running" });
const [wakeRequest] = await db.insert(agentWakeupRequests).values({
companyId: seeded.companyId,
agentId: seeded.agentId,
source: "automation",
triggerDetail: "system",
reason: "issue_commented",
status: "queued",
idempotencyKey: `question-response:${seeded.interaction.id}`,
}).returning();
const [wakeRun] = await db.insert(heartbeatRuns).values({
companyId: seeded.companyId,
agentId: seeded.agentId,
invocationSource: "automation",
status: "queued",
runtimeMode: "legacy",
wakeupRequestId: wakeRequest!.id,
contextSnapshot: { issueId: seeded.issueId },
}).returning();
await db.update(agentWakeupRequests).set({ runId: wakeRun!.id })
.where(eq(agentWakeupRequests.id, wakeRequest!.id));
const wakeup = vi.fn();
const outcome = await questionResponseDeliveryService(db, {
heartbeat: { wakeup } as never,
steer: vi.fn(),
}).deliver(seeded.interaction.id);
expect(outcome).toMatchObject({
status: "delivered",
mode: "coalesced",
targetRunId: wakeRun!.id,
});
expect(wakeup).not.toHaveBeenCalled();
});
it("recovers a completed wake when receipt finalization was interrupted", async () => {
const seeded = await seed({ sourceStatus: "running" });
const [wakeRequest] = await db.insert(agentWakeupRequests).values({
companyId: seeded.companyId,
agentId: seeded.agentId,
source: "automation",
triggerDetail: "system",
reason: "issue_commented",
status: "completed",
idempotencyKey: `question-response:${seeded.interaction.id}`,
finishedAt: new Date(),
}).returning();
const [wakeRun] = await db.insert(heartbeatRuns).values({
companyId: seeded.companyId,
agentId: seeded.agentId,
invocationSource: "automation",
status: "succeeded",
runtimeMode: "legacy",
wakeupRequestId: wakeRequest!.id,
contextSnapshot: { issueId: seeded.issueId },
finishedAt: new Date(),
}).returning();
await db.update(agentWakeupRequests).set({ runId: wakeRun!.id })
.where(eq(agentWakeupRequests.id, wakeRequest!.id));
const wakeup = vi.fn();
const outcome = await questionResponseDeliveryService(db, {
heartbeat: { wakeup } as never,
steer: vi.fn(),
}).deliver(seeded.interaction.id);
expect(outcome).toMatchObject({
status: "fallback_queued",
mode: "wake_fallback",
targetRunId: wakeRun!.id,
});
expect(wakeup).not.toHaveBeenCalled();
const [delivery] = await db.select().from(issueQuestionResponseDeliveries);
expect(delivery).toMatchObject({
status: "fallback_queued",
errorCount: 0,
targetRunId: wakeRun!.id,
});
});
it("keeps a long wake claim leased while the side effect is active", async () => {
const seeded = await seed({ sourceStatus: "running" });
// Each call gets its own resolver. A second, unexpected call fails at
// once instead of sharing one resolver with the first call and hanging.
const wakeResolvers: Array<(value: null) => void> = [];
const wakeup = vi.fn(() => {
if (wakeResolvers.length > 0) {
throw new Error(
"wakeup was invoked a second time for the same claim; a re-entrant delivery must fail at once, not hang",
);
}
return new Promise<null>((resolve) => {
wakeResolvers.push(resolve);
});
});
// The renewal timer and the sweep both read time from this injected
// clock. The test moves the clock by hand, so the assertions below do
// not depend on the speed of a database round trip or a timer callback.
let clock = new Date("2026-01-01T00:00:00.000Z");
const service = questionResponseDeliveryService(db, {
heartbeat: { wakeup } as never,
steer: vi.fn(),
now: () => clock,
claimStaleMs: 40,
claimRefreshMs: 5,
});
const deliveryPromise = service.deliver(seeded.interaction.id);
await vi.waitFor(() => expect(wakeup).toHaveBeenCalledTimes(1));
const [claimedRow] = await db.select().from(issueQuestionResponseDeliveries)
.where(eq(issueQuestionResponseDeliveries.interactionId, seeded.interaction.id));
const claimedAt = claimedRow!.lastAttemptAt!.getTime();
// Move the clock past claimStaleMs, then wait for a real renewal tick to
// pick it up. This proves the renewal ran. It does not only assert that
// a fixed real-time sleep was long enough.
clock = new Date(clock.getTime() + 1000);
await vi.waitFor(async () => {
const [row] = await db.select().from(issueQuestionResponseDeliveries)
.where(eq(issueQuestionResponseDeliveries.interactionId, seeded.interaction.id));
expect(row?.lastAttemptAt?.getTime()).toBeGreaterThan(claimedAt);
});
await expect(service.sweepPending()).resolves.toMatchObject({ scanned: 0 });
wakeResolvers[0]!(null);
await expect(deliveryPromise).resolves.toBeNull();
expect(wakeup).toHaveBeenCalledTimes(1);
const [delivery] = await db.select().from(issueQuestionResponseDeliveries)
.where(eq(issueQuestionResponseDeliveries.interactionId, seeded.interaction.id));
expect(delivery).toMatchObject({ status: "pending", attemptCount: 1 });
});
it("fences a stale worker after a newer claim generation takes ownership", async () => {
const seeded = await seed({ sourceStatus: "running" });
let releaseFirstWake!: (value: { id: string; driverKind: string }) => void;
const firstWakeup = vi.fn(() => new Promise<{ id: string; driverKind: string }>((resolve) => {
releaseFirstWake = resolve;
}));
const firstService = questionResponseDeliveryService(db, {
heartbeat: { wakeup: firstWakeup } as never,
steer: vi.fn(),
claimStaleMs: 40,
claimRefreshMs: 5,
});
const firstDelivery = firstService.deliver(seeded.interaction.id);
await vi.waitFor(() => expect(firstWakeup).toHaveBeenCalledTimes(1));
// Simulate recovery after the first worker stopped renewing. The next
// claim increments attemptCount, which is the fencing generation.
await db.update(issueQuestionResponseDeliveries).set({
status: "pending",
lastAttemptAt: new Date(0),
}).where(eq(issueQuestionResponseDeliveries.interactionId, seeded.interaction.id));
const secondRunId = randomUUID();
const secondWakeup = vi.fn().mockImplementation(async () => db.insert(heartbeatRuns).values({
id: secondRunId,
companyId: seeded.companyId,
agentId: seeded.agentId,
invocationSource: "automation",
status: "queued",
runtimeMode: "legacy",
driverKind: "codex",
contextSnapshot: { issueId: seeded.issueId },
}).returning().then((rows) => rows[0]!));
const secondOutcome = await questionResponseDeliveryService(db, {
heartbeat: { wakeup: secondWakeup } as never,
steer: vi.fn(),
}).deliver(seeded.interaction.id);
expect(secondOutcome).toMatchObject({
status: "fallback_queued",
targetRunId: secondRunId,
duplicate: false,
});
releaseFirstWake({ id: randomUUID(), driverKind: "codex" });
await expect(firstDelivery).resolves.toMatchObject({
status: "fallback_queued",
targetRunId: secondRunId,
duplicate: true,
});
const [delivery] = await db.select().from(issueQuestionResponseDeliveries);
expect(delivery).toMatchObject({
status: "fallback_queued",
targetRunId: secondRunId,
attemptCount: 2,
});
const deliveryEvents = await db.select().from(activityLog)
.where(eq(activityLog.action, "issue.question_response_delivered"));
expect(deliveryEvents).toHaveLength(1);
});
it.each(DIRECT_ADAPTER_TYPES)(
"keeps %s on the existing wake path without invoking native steering",
async (adapterType) => {
const seeded = await seed({
adapterType,
runtimeMode: "legacy",
successorStatus: "running",
});
const fallbackRunId = randomUUID();
const wakeup = vi.fn().mockImplementation(async () => db.insert(heartbeatRuns).values({
id: fallbackRunId,
companyId: seeded.companyId,
agentId: seeded.agentId,
invocationSource: "automation",
status: "queued",
runtimeMode: "legacy",
contextSnapshot: { issueId: seeded.issueId },
}).returning().then((rows) => rows[0]!));
const steer = vi.fn();
const outcome = await questionResponseDeliveryService(db, {
heartbeat: { wakeup } as never,
steer,
}).deliver(seeded.interaction.id);
expect(outcome).toMatchObject({
status: "fallback_queued",
mode: "wake_fallback",
targetRunId: fallbackRunId,
});
expect(steer).not.toHaveBeenCalled();
expect(wakeup).toHaveBeenCalledTimes(1);
expect(wakeup.mock.calls[0]?.[1]).toMatchObject({
idempotencyKey: `question-response:${seeded.interaction.id}`,
contextSnapshot: {
issueId: seeded.issueId,
interactionId: seeded.interaction.id,
interactionStatus: "answered",
},
});
},
);
it("falls back once when successor steering is unsupported", async () => {
const seeded = await seed({ successorStatus: "running" });
const fallbackRunId = randomUUID();
const steer = vi.fn().mockRejectedValue(
Object.assign(new Error("unsupported"), {
code: "steering_unsupported",
}),
);
const wakeup = vi.fn().mockImplementation(async () => db.insert(heartbeatRuns).values({
id: fallbackRunId,
companyId: seeded.companyId,
agentId: seeded.agentId,
invocationSource: "automation",
status: "queued",
driverKind: "codex",
contextSnapshot: { issueId: seeded.issueId },
}).returning().then((rows) => rows[0]!));
const service = questionResponseDeliveryService(db, {
heartbeat: { wakeup } as never,
steer,
});
const first = await service.deliver(seeded.interaction.id);
const second = await service.deliver(seeded.interaction.id);
expect(first).toMatchObject({ status: "fallback_queued", mode: "wake_fallback", targetRunId: fallbackRunId });
expect(second?.duplicate).toBe(true);
expect(steer).toHaveBeenCalledTimes(1);
expect(wakeup).toHaveBeenCalledTimes(1);
});
it("formats text, select labels, multi-select, and custom answers in order", async () => {
const seeded = await seed();
const envelope = buildQuestionResponseDeliveryEnvelope(seeded.interaction);
expect(envelope.response).toEqual({
schema: "paperclip.question_response.v1",
answers: {
purpose: { text: "Internal API" },
runtime: { selectedOptionIds: ["node"] },
features: { selectedOptionIds: ["health", "logs"], customText: "Metrics" },
},
});
expect(formatQuestionResponseSteeringMessage(envelope)).toBe([
"Answered questions",
"",
"- Purpose — What is it for?: Internal API",
"- Runtime — Which runtime?: Node.js",
"- Features — Which features?: Health check, Request logs, Metrics",
].join("\n"));
});
});