TypeScript
fix(server): reject unusable user-input answers before the request resolves (#14884)
Jonas B.128846e399e6
diff --git a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.tsindex 7237c16a277..eda76857a9e 100644--- a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts+++ b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts@@ -28,6 +28,7 @@ import * as SqlClient from "effect/sql/SqlClient";import * as SqlitePersistence from "../persistence/Sqlite.ts";import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";import * as DelegatedTaskQuestionReactor from "./DelegatedTaskQuestionReactor.ts";+import * as EffectOutbox from "./EffectOutbox.ts";import * as EventSink from "./EventSink.ts";import * as Orchestrator from "./Orchestrator.ts";import * as ProjectionStore from "./ProjectionStore.ts";@@ -48,6 +49,7 @@ const layerDatabase = SqlitePersistence.layerMemory;const layerTest = Layer.mergeAll(layerDatabase,ProjectionStore.layer.pipe(Layer.provide(layerDatabase)),+ EffectOutbox.layer.pipe(Layer.provide(layerDatabase)),ProviderReplayHarness.layerWithRegistry({ name: "control-reads" },ProviderAdapterRegistry.layerFromAdapters([adapter]),@@ -124,6 +126,7 @@ it.effect(const orchestrator = yield* Orchestrator.OrchestratorV2;const projections = yield* ProjectionStore.ProjectionStoreV2;const sql = yield* SqlClient.SqlClient;+ const outbox = yield* EffectOutbox.EffectOutboxV2;const threadId = ThreadId.make("thread:control-dispatch");const now = yield* DateTime.now;yield* orchestrator.dispatch({@@ -270,6 +273,37 @@ it.effect(questions: [],},});+ const answers = { text: "Blue", list: ["Red"], codex: { answers: ["Green"] } };+ const normalizedAnswers = { ...answers, codex: ["Green"] };+ if (mode === "live") {+ const pending = yield* projections.getRuntimeResponseContext(threadId, requestId);+ const sequence = yield* orchestrator.getThreadEventSequence(threadId);+ for (const [index, value] of [+ null,+ 42,+ { choice: "Blue" },+ ["Blue", 42],+ { answers: [42] },+ ].entries()) {+ const commandId = CommandId.make(`invalid-answer:${index}`);+ const result = yield* orchestrator+ .dispatch({+ type: "runtime-request.respond",+ commandId,+ threadId,+ requestId,+ answers: { ...answers, invalid: value },+ })+ .pipe(Effect.result);+ assert.isTrue(Result.isFailure(result));+ assert.deepEqual(+ yield* projections.getRuntimeResponseContext(threadId, requestId),+ pending,+ );+ assert.equal(yield* orchestrator.getThreadEventSequence(threadId), sequence);+ assert.deepEqual(yield* outbox.listByCommandId(commandId), []);+ }+ }yield* orchestrator.dispatch(mode === "live"? {@@ -277,7 +311,7 @@ it.effect(commandId: CommandId.make(`respond:${mode}`),threadId,requestId,- decision: "accept",+ answers,}: {type: "thread.user-input.dismiss",@@ -293,6 +327,25 @@ it.effect(const response = yield* projections.getRuntimeResponseContext(threadId, requestId);assert.equal(response.node?.status, mode === "live" ? "completed" : "cancelled");assert.equal(response.item?.status, mode === "live" ? "completed" : "cancelled");+ if (mode === "live") {+ assert.deepEqual(response.request?.answers, normalizedAnswers);+ assert.equal(response.item?.type, "user_input_request");+ if (response.item?.type === "user_input_request") {+ assert.deepEqual(response.item.questionAnswer?.answers, normalizedAnswers);+ }+ const effects = yield* outbox.listByCommandId(CommandId.make(`respond:${mode}`));+ assert.deepEqual(+ effects.map((effect) => effect.request),+ [+ {+ type: "runtime-request.respond",+ providerSessionId: sessionId,+ requestId,+ answers: normalizedAnswers,+ },+ ],+ );+ }}yield* orchestrator.dispatch({type: "thread.metadata.update",diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.tsindex 5024127de33..08048e1328f 100644--- a/apps/server/src/orchestration-v2/Orchestrator.ts+++ b/apps/server/src/orchestration-v2/Orchestrator.ts@@ -498,6 +498,18 @@ function commandThreadId(command: OrchestrationV2ServerCommand): ThreadId {}}+// Validate before resolving the request: adapters may otherwise coerce unusable answers.+const decodeUserInputAnswers = Schema.decodeUnknownEffect(+ Schema.Record(+ Schema.String,+ Schema.Union([+ Schema.String,+ Schema.Array(Schema.String),+ Schema.Struct({ answers: Schema.Array(Schema.String) }),+ ]),+ ),+);+function pendingThreadTitleGenerationEffect(commandId: CommandId,threadId: ThreadId,@@ -7514,6 +7526,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functiocause: `Provider session ${providerSessionId} was not found.`,});}+ let answers = command.answers;+ if (runtimeRequest.kind === "user_input" && answers !== undefined) {+ const decoded = yield* decodeUserInputAnswers(answers).pipe(mapDispatchError(command));+ answers = Object.fromEntries(+ Object.entries(decoded).map(([questionId, answer]) => [+ questionId,+ typeof answer === "object" && "answers" in answer ? answer.answers : answer,+ ]),+ );+ }const now = yield* DateTime.now;const resolvedRequest = {@@ -7521,7 +7543,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functiostatus: "resolved" as const,resolvedAt: now,...(command.decision === undefined ? {} : { decision: command.decision }),- ...(command.answers === undefined ? {} : { answers: command.answers }),+ ...(answers === undefined ? {} : { answers }),};const emitEvent = emit(events, command);const requestNode = context.node;@@ -7580,11 +7602,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functiooccurredAt: now,payload: {...approvalTurnItem,- ...(approvalTurnItem.type === "user_input_request" && command.answers !== undefined+ ...(approvalTurnItem.type === "user_input_request" && answers !== undefined? {questionAnswer: {requestId: command.requestId,- answers: command.answers ?? {},+ answers,attachmentsByQuestionId: command.attachmentsByQuestionId ?? {},questionTextById: Object.fromEntries(approvalTurnItem.questions.map((question) => [@@ -7612,7 +7634,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio}const replies: string[] = [];for (const question of approvalTurnItem.questions) {- const answer = command.answers?.[question.id];+ const answer = answers?.[question.id];if (typeof answer !== "string" || answer.trim().length === 0) {if (question.required === false) continue;return yield* new OrchestratorDispatchError({@@ -7691,7 +7713,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functioproviderSessionId,requestId: command.requestId,...(command.decision === undefined ? {} : { decision: command.decision }),- ...(command.answers === undefined ? {} : { answers: command.answers }),+ ...(answers === undefined ? {} : { answers }),},} satisfies PendingOrchestrationEffectV2,]);