feat(runner): preserve editable question defaults through explicit response

Carry bounded text-only initialText through ACP normalization, Rust durable validation, shared contracts and the question UI. Saved edits and explicit clears survive reload. Preserve canonical submitted text while retaining legacy custom-answer normalization and existing validation.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-09-29 12:06:48 -05:00
1 parent 0f04bf454d
commit 012aa52c77
23 files changed
+373 -9

No files matched your search

@@ -1,3 +1,4 @@
import { normalizeEscapedLineBreaks } from "@paperclipai/shared/validators/text";
import {
currentContinuationOrigins,
deliveredContinuationCommentIds,
@@ -1668,6 +1669,7 @@ function resolveRequestItemVerdictSubmissions(args: {
function normalizeQuestionAnswers(args: {
questions: AskUserQuestionsInteraction["payload"]["questions"];
textQuestionIds?: ReadonlySet<string>;
answers: RespondIssueThreadInteraction["answers"];
}) {
const questionById = new Map(
@@ -1702,7 +1704,11 @@ function normalizeQuestionAnswers(args: {
);
}
const otherText = answer.otherText?.trim() ?? "";
// Canonical text fields may contain code or intentional whitespace. The
// legacy custom-answer path keeps its historical newline/trim behavior.
const otherText = args.textQuestionIds?.has(answer.questionId)
? answer.otherText ?? ""
: normalizeEscapedLineBreaks(answer.otherText ?? "").trim();
answerByQuestionId.set(answer.questionId, {
questionId: answer.questionId,
optionIds: uniqueOptionIds,
@@ -1714,7 +1720,7 @@ function normalizeQuestionAnswers(args: {
const answer = answerByQuestionId.get(question.id);
if (
question.required &&
(!answer || (answer.optionIds.length === 0 && !answer.otherText))
(!answer || (answer.optionIds.length === 0 && !answer.otherText?.trim()))
) {
throw unprocessable(`Question ${question.id} requires an answer`);
}
@@ -4845,6 +4851,9 @@ export function issueThreadInteractionService(
) as AskUserQuestionsInteraction;
const normalizedAnswers = normalizeQuestionAnswers({
questions: interaction.payload.questions,
textQuestionIds: new Set((interaction.payload.questionSet?.questions ?? [])
.filter((question) => question.answerMode === "text")
.map((question) => question.id)),
answers: input.answers,
});
@@ -20,6 +20,7 @@ import {
nativeRunFinalizations,
} from "@paperclipai/db";
import type { PrpEvent } from "@paperclipai/paperclip-runner";
import { respondIssueThreadInteractionSchema } from "@paperclipai/shared";
import {
getEmbeddedPostgresTestSupport,
@@ -403,6 +404,54 @@ describeEmbeddedPostgres("native question bridge", () => {
release();
});
it.each(["legacy", "canonical_select"])("keeps %s custom-answer normalization and rejects blank public answers", async (mode) => {
await seed();
const event = runtimeRequestEvent();
const input = (event.payload.request as { input: { questions: Record<string, unknown>[] } }).input;
input.questions[0]!.customAnswer = { enabled: true };
const interaction = await projectNativeRuntimeRequest({ db, binding: binding(), event });
if (mode === "legacy") {
const payload = { ...interaction!.payload } as Record<string, unknown>;
delete payload.questionSet;
await db.update(issueThreadInteractions).set({ payload }).where(eq(issueThreadInteractions.id, interaction!.id));
}
const service = issueThreadInteractionService(db);
await expect(service.answerQuestions({ id: issueId, companyId, status: "in_progress" }, interaction!.id,
respondIssueThreadInteractionSchema.parse({ answers: [{ questionId: "color", optionIds: [], otherText: " \n " }] }), { userId: "operator-1" },
)).rejects.toThrow(/requires an answer/);
const answered = await service.answerQuestions({ id: issueId, companyId, status: "in_progress" }, interaction!.id,
respondIssueThreadInteractionSchema.parse({ answers: [{ questionId: "color", optionIds: [], otherText: " Extra\\nline " }] }), { userId: "operator-1" },
);
expect(answered.result).toMatchObject({ answers: [{ questionId: "color", otherText: "Extra\nline" }] });
});
it("persists initial text without resolving and delivers only an explicit edited answer", async () => {
await seed();
const initialText = " Editable draft\n漢字\n";
const event = runtimeRequestEvent();
const request = event.payload.request as Record<string, unknown>;
request.input = { schema: "paperclip.question_set.v1", questions: [{ id: "draft", prompt: "Edit", required: true, answerMode: "text", initialText }] };
const interaction = await projectNativeRuntimeRequest({ db, binding: binding(), event });
expect(interaction).toMatchObject({ status: "pending", payload: { questionSet: { questions: [{ initialText }] } } });
const [stored] = await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, interaction!.id));
expect(stored).toMatchObject({ status: "pending", result: null, payload: { questionSet: { questions: [{ initialText }] } } });
const queueCommand = vi.fn(() => ({ commandId: "edited", controllerSeq: 1 }));
const release = registerNativeQuestionCommandTarget({ binding: { companyId, issueId, runId, agentId }, queueCommand });
try {
await flushNativeQuestionResponses(db, runId);
expect(queueCommand).not.toHaveBeenCalled();
const edited = "\n Operator 漢字 edited\n\nLiteral \\n stays literal. \n";
await issueThreadInteractionService(db).answerQuestions(
{ id: issueId, companyId, status: "in_progress" }, interaction!.id,
respondIssueThreadInteractionSchema.parse({ answers: [{ questionId: "draft", optionIds: [], otherText: edited }] }), { userId: "operator-1" },
);
await flushNativeQuestionResponses(db, runId);
expect(queueCommand).toHaveBeenCalledExactlyOnceWith("request.resolve", {
requestId: "request-1", response: { schema: "paperclip.question_response.v1", answers: { draft: { text: edited } } },
}, `question_${interaction!.id}`);
} finally { release(); }
});
it("binds projection to the persisted native run and ignores legacy delivery", async () => {
await seed();
const mismatched = runtimeRequestEvent();