@devries-cameron20 / t3code-1 / commits / e80468ba25
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): report invalid provider events without a second schema failure (#23650)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 14, 2026 at 09:06 UTC e80468ba256a025c68dbf331c76a7cca46f71dfc
2 files changed +76 -3
apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts
+72
@@ -33,6 +33,7 @@ import * as Effect from "effect/Effect";
33 import * as Exit from "effect/Exit";
34 import * as FileSystem from "effect/FileSystem";
35 import * as Layer from "effect/Layer";
36 +import * as Schema from "effect/Schema";
37 import * as SqlClient from "effect/sql/SqlClient";
38 import * as SqlError from "effect/sql/SqlError";
39 import * as Stream from "effect/Stream";
@@ -169,6 +170,7 @@ const modelSelection = {
170 model: "gpt-5.4",
171 } satisfies ModelSelection;
172 const CODEX_DRIVER = ProviderDriverKind.make("codex");
173 +const isProviderEvent = Schema.is(ProviderAdapter.ProviderAdapterV2Event);
174
175 function threadCreatedEvent(
176 now: DateTime.Utc,
@@ -446,6 +448,76 @@ it.effect("records accepted billed turn usage once without billing the context w
448 }).pipe(Effect.provide(layerTest.pipe(Layer.provide(layerAnalytics))));
449 });
450
451 +it.effect(
452 + "reports invalid provider questions as typed errors and still ingests valid questions",
453 + () =>
454 + Effect.gen(function* () {
455 + const now = yield* DateTime.now;
456 + const eventSink = yield* EventSink.EventSinkV2;
457 + const eventStore = yield* EventStore.EventStoreV2;
458 + const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
459 + const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2;
460 + const idAllocator = yield* IdAllocator.IdAllocatorV2;
461 + const threadEvent = yield* threadCreatedEvent(now);
462 + const threadId = threadEvent.threadId;
463 + yield* eventSink.write({ events: [threadEvent] });
464 + const providerInstanceId = ProviderInstanceId.make("pi");
465 + const providerSessionId = yield* idAllocator.allocate.providerSession({
466 + providerInstanceId,
467 + threadId,
468 + });
469 + const question = { id: "input", header: "Enter a value", question: "", options: [] };
470 + const item = {
471 + id: TurnItemId.make(`${threadId}:question`),
472 + threadId,
473 + runId: null,
474 + nodeId: null,
475 + providerThreadId: null,
476 + providerTurnId: null,
477 + nativeItemRef: null,
478 + parentItemId: null,
479 + ordinal: 1,
480 + status: "waiting",
481 + title: "Enter a value",
482 + startedAt: now,
483 + completedAt: null,
484 + updatedAt: now,
485 + type: "user_input_request",
486 + requestId: RuntimeRequestId.make(`${threadId}:question`),
487 + questions: [question],
488 + } satisfies OrchestrationV2TurnItem;
489 + const event = {
490 + type: "turn_item.updated",
491 + driver: ProviderDriverKind.make("pi"),
492 + turnItem: item,
493 + } satisfies ProviderAdapter.ProviderAdapterV2Event;
494 + const input = { providerSessionId, providerInstanceId, threadId, event };
495 +
496 + assert.isFalse(isProviderEvent(event));
497 + const error = yield* ingestor.ingestNormalized(input).pipe(Effect.flip);
498 + assert.instanceOf(error, ProviderEventIngestor.ProviderEventNormalizeError);
499 + assert.equal(
500 + error.message,
501 + `Failed to normalize provider event turn_item.updated for thread ${threadId}.`,
502 + );
503 + assert.instanceOf(error.cause, Schema.SchemaError);
504 + assert.equal(yield* eventStore.latestSequence({ threadId }), 1);
505 + assert.isEmpty((yield* projectionStore.getThreadProjection(threadId)).turnItems);
506 +
507 + const validEvent = {
508 + ...event,
509 + turnItem: { ...item, questions: [{ ...question, question: "Enter a value" }] },
510 + };
511 + assert.isTrue(isProviderEvent(validEvent));
512 + const stored = yield* ingestor.ingestNormalized({ ...input, event: validEvent });
513 + assert.lengthOf(stored, 1);
514 + assert.equal(stored[0]?.event.type, "turn-item.updated");
515 + assert.deepEqual((yield* projectionStore.getThreadProjection(threadId)).turnItems, [
516 + validEvent.turnItem,
517 + ]);
518 + }).pipe(Effect.provide(layerTest)),
519 +);
520 +
521 layer("ProviderEventIngestorV2", (it) => {
522 it.effect("normalizes provider events through the real event log and projection store", () =>
523 Effect.gen(function* () {
apps/server/src/orchestration-v2/ProviderEventIngestor.ts
+4 -3
@@ -47,12 +47,13 @@ export class ProviderEventNormalizeError extends Schema.TaggedError<ProviderEven
47 {
48 providerSessionId: ProviderSessionId,
49 threadId: ThreadId,
50 - providerEvent: ProviderAdapter.ProviderAdapterV2Event,
50 + // The rejected payload cannot be validated again while reporting its normalization failure.
51 + eventType: Schema.String,
52 cause: Schema.optional(Schema.Defect()),
53 },
54 ) {
55 override get message(): string {
55 - return `Failed to normalize provider event ${this.providerEvent.type} for thread ${this.threadId}.`;
56 + return `Failed to normalize provider event ${this.eventType} for thread ${this.threadId}.`;
57 }
58 }
59
@@ -721,7 +722,7 @@ export const layer: Layer.Layer<
722 new ProviderEventNormalizeError({
723 providerSessionId: input.providerSessionId,
724 threadId: input.threadId,
724 - providerEvent: input.event,
725 + eventType: input.event.type,
726 cause,
727 }),
728 ),