Skip to content
TypeScript

fix(contracts): old message-sent events without turnId no longer stop the server from starting (#12763)

Igor Makowski80d9c181da41
diff --git a/packages/contracts/src/orchestration.test.ts b/packages/contracts/src/orchestration.test.ts
index d89e1fb2957..7c7bade4502 100644
--- a/packages/contracts/src/orchestration.test.ts
+++ b/packages/contracts/src/orchestration.test.ts
@@ -812,6 +812,37 @@ it.effect("decodes thread pull request links with snapshot and stack", () =>
}),
);
+// A stored event that fails to decode stops the event store read, and with it
+// server startup, so rows written before `turnId` existed must still load.
+it.effect("decodes a legacy message-sent event persisted without turnId", () =>
+ Effect.gen(function* () {
+ const event = yield* decodeOrchestrationEvent({
+ sequence: 539,
+ eventId: "event-message-legacy-1",
+ aggregateKind: "thread",
+ aggregateId: "thread-1",
+ type: "thread.message-sent",
+ occurredAt: "2026-01-01T00:00:00.000Z",
+ commandId: "cmd-message-legacy-1",
+ causationEventId: null,
+ correlationId: "cmd-message-legacy-1",
+ metadata: {},
+ payload: {
+ threadId: "thread-1",
+ messageId: "message-1",
+ role: "user",
+ text: "written before turn ids were recorded",
+ streaming: false,
+ createdAt: "2026-01-01T00:00:00.000Z",
+ updatedAt: "2026-01-01T00:00:00.000Z",
+ },
+ });
+ assert.strictEqual(event.type, "thread.message-sent");
+ if (event.type !== "thread.message-sent") return;
+ assert.strictEqual(event.payload.turnId, null);
+ }),
+);
+
it.effect("decodes thread archived and unarchived events", () =>
Effect.gen(function* () {
const archived = yield* decodeOrchestrationEvent({
diff --git a/packages/contracts/src/orchestration.ts b/packages/contracts/src/orchestration.ts
index 9a9ed05d4e6..3e323e4964d 100644
--- a/packages/contracts/src/orchestration.ts
+++ b/packages/contracts/src/orchestration.ts
@@ -1893,7 +1893,8 @@ export const ThreadMessageSentPayload = Schema.Struct({
text: Schema.String,
attachments: Schema.optional(Schema.Array(ChatAttachment)),
context: Schema.optional(OrchestrationMessageContext),
- turnId: Schema.NullOr(TurnId),
+ // Events persisted before the field existed carry no key at all.
+ turnId: Schema.NullOr(TurnId).pipe(Schema.withDecodingDefault(Effect.succeed(null))),
streaming: Schema.Boolean,
createdAt: IsoDateTime,
updatedAt: IsoDateTime,