Preserve cancelled native questions and their Stop evidence

Use the canonical question converter for durable fallbacks. Retire pending native input only on its confirmed cancelled turn. Pass cancelled status to shared account and billing checks and retain the original browser acknowledgement before polling.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-08 12:28:13 -05:00
1 parent f198406e80
commit b7ea45ae9c
10 files changed
+146 -62

No files matched your search

@@ -1,3 +1,4 @@
import { createIssueThreadInteractionSchema } from "@paperclipai/shared";
import { configuredEnvironmentProjection } from "../../vendor/paperclip-runner/index.js";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { readFileSync } from "node:fs";
@@ -2555,6 +2556,33 @@ describe("runtime question fallback", () => {
],
};
it.each(["provider_process_lost", "durable_handoff"])("preserves a complete custom-answer batch after %s", reason => {
const form = { schema: "paperclip.question_set.v1" as const, questions: [
{ id: "color", prompt: "Choose color", required: true, answerMode: "single_select" as const,
options: [{ id: "cobalt", label: "Cobalt" }], customAnswer: { enabled: true, label: "Another color" } },
{ id: "targets", prompt: "Choose targets", required: true, answerMode: "multi_select" as const,
options: [{ id: "paperclip_custom_answer", label: "Linux" }], customAnswer: { enabled: true, placeholder: "Name a target" } },
{ id: "notes", prompt: "Describe constraint", required: true, answerMode: "text" as const },
] };
const fallback = runtimeQuestionFallbackFromEvent({ eventType: "runtime_request.expired", runId: "00000000-0000-4000-8000-000000000001",
payload: { requestId: "native-batch", requestKind: "runtime", requestType: "input", reason, replayAllowed: false,
request: { schema: "paperclip.runtime_request.v2", requestKind: "runtime", type: "input", status: "pending",
requestId: "native-batch", turnId: "turn", itemId: "item", input: form } } });
expect(fallback).not.toBeNull();
expect(createIssueThreadInteractionSchema.safeParse(fallback).success).toBe(true);
expect(fallback!.payload.questionSet).toEqual(form);
expect(fallback!.payload.questions[0]!.options).toEqual([{ id: "cobalt", label: "Cobalt" },
{ id: "paperclip_custom_answer", label: "Another color", freeText: true }]);
expect(fallback!.payload.questions[1]!.options).toEqual([{ id: "paperclip_custom_answer", label: "Linux" },
{ id: "paperclip_custom_answer_2", label: "Other", description: "Name a target", freeText: true }]);
expect(fallback!.payload.questions[1]!.selectionMode).toBe("multi");
expect(fallback!.payload.questions[2]!.options[0]!.freeText).toBe(true);
const incomplete = structuredClone(fallback!);
incomplete.payload.questions[1]!.options.pop();
incomplete.payload.questions[1]!.allowOther = false;
expect(createIssueThreadInteractionSchema.safeParse(incomplete).success).toBe(false);
});
it.each(["provider_process_lost", "durable_handoff"])(
"materializes one idempotent durable interaction after %s",
(reason) => {
@@ -2602,7 +2630,7 @@ describe("runtime question fallback", () => {
{
id: "replicas",
selectionMode: "single",
options: [{ id: "__paperclip_text__", freeText: true }],
options: [{ id: "paperclip_text_answer", freeText: true }],
},
],
},
@@ -77,7 +77,7 @@ import type {
AdapterExecutionResult,
AdapterRuntimeEvent,
} from "../../adapters/index.js";
import { usdToUnits, type NativeFinalizationResult } from "@paperclipai/shared";
import { usdToUnits, questionSetToAskUserQuestionsPayload, type AskUserQuestionsPayload, type NativeFinalizationResult } from "@paperclipai/shared";
import type {
HarnessRuntimeRequestResolution,
NativeExecutionInput,
@@ -722,24 +722,12 @@ type RuntimeQuestionFallback = {
continuationPolicy: "wake_assignee";
payload: {
version: 1;
title?: string;
submitLabel?: string;
title?: string | null;
submitLabel?: string | null;
supersedeOnUserComment: false;
runtimeRequestId: string;
questionSet: PaperclipQuestionSet;
questions: Array<{
id: string;
prompt: string;
helpText?: string;
selectionMode: "single" | "multi";
required: boolean;
options: Array<{
id: string;
label: string;
description?: string;
freeText?: boolean;
}>;
}>;
questions: AskUserQuestionsPayload["questions"];
};
};
@@ -775,35 +763,7 @@ export function runtimeQuestionFallbackFromEvent(
} catch {
return null;
}
const questions = questionSet.questions.map((question) => ({
id: question.id,
prompt: question.prompt,
...(question.helpText ? { helpText: question.helpText } : {}),
selectionMode:
question.answerMode === "multi_select"
? ("multi" as const)
: ("single" as const),
required: question.required,
options:
question.answerMode === "text"
? [
{
id: "__paperclip_text__",
label:
question.textValidation?.inputType === "integer"
? "Enter an integer"
: question.textValidation?.inputType === "number"
? "Enter a number"
: "Enter your answer",
freeText: true,
},
]
: (question.options ?? []).map((option) => ({
id: option.id,
label: option.label,
...(option.description ? { description: option.description } : {}),
})),
}));
const storagePayload = questionSetToAskUserQuestionsPayload(questionSet);
return {
kind: "ask_user_questions",
idempotencyKey: `runtime-input-durable:v1:${event.runId}:${request.requestId}`,
@@ -812,15 +772,11 @@ export function runtimeQuestionFallbackFromEvent(
summary: questionSet.description?.slice(0, 1000) ?? null,
continuationPolicy: "wake_assignee",
payload: {
version: 1,
...(questionSet.title ? { title: questionSet.title.slice(0, 240) } : {}),
...(questionSet.submitLabel
? { submitLabel: questionSet.submitLabel.slice(0, 120) }
: {}),
...storagePayload,
supersedeOnUserComment: false,
runtimeRequestId: request.requestId,
questionSet,
questions,
questions: storagePayload.questions,
},
};
}