@devries-cameron20 / t3code-1 / commits / 65aaf972ae
Fork 0

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

fix(server): avoid reparsing streamed replies for timestamps (#25252)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 2, 2026 at 18:00 UTC 65aaf972aef7de82721f52494ac224f803fd7be9
2 files changed +98 -6
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+90 -1
@@ -1,4 +1,4 @@
1 -import { assert, it } from "@effect/vitest";
1 +import { assert, it, vi } from "@effect/vitest";
2 import {
3 EnvironmentId,
4 EventId,
@@ -1954,6 +1954,95 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
1954 }),
1955 );
1956
1957 + // regression: #25157 streamed payloads are not re-parsed to read their indexed timestamps
1958 + it.effect("indexes streamed payload timestamps without parsing the payload JSON", () =>
1959 + Effect.gen(function* () {
1960 + const store = yield* ProjectionStore.ProjectionStoreV2;
1961 + const sql = yield* SqlClient.SqlClient;
1962 + const threadId = yield* addRolledBackRecoveryCandidate("streamed-payload-parse");
1963 + const runId = RunId.make("run:streamed-payload-parse:rolled-back");
1964 + const createdAt = DateTime.makeUnsafe("2026-01-02T03:04:05.006Z");
1965 + const updatedAt = DateTime.makeUnsafe("2026-01-02T03:04:06.007Z");
1966 + const text = "x".repeat(128 * 1024);
1967 + const messageId = MessageId.make("message:streamed-payload-parse");
1968 + const itemId = TurnItemId.make("item:streamed-payload-parse");
1969 +
1970 + const parsedPayloads = yield* Effect.acquireUseRelease(
1971 + Effect.sync(() => vi.spyOn(JSON, "parse")),
1972 + (parse) =>
1973 + Effect.gen(function* () {
1974 + yield* store.apply({
1975 + id: EventId.make("event:streamed-payload-parse:message"),
1976 + type: "message.updated",
1977 + threadId,
1978 + occurredAt: updatedAt,
1979 + payload: {
1980 + createdBy: "agent",
1981 + creationSource: "server",
1982 + id: messageId,
1983 + threadId,
1984 + runId,
1985 + nodeId: null,
1986 + role: "assistant",
1987 + text,
1988 + attachments: [],
1989 + streaming: true,
1990 + createdAt,
1991 + updatedAt,
1992 + },
1993 + });
1994 + yield* store.apply({
1995 + id: EventId.make("event:streamed-payload-parse:item"),
1996 + type: "turn-item.updated",
1997 + threadId,
1998 + runId,
1999 + occurredAt: updatedAt,
2000 + payload: {
2001 + id: itemId,
2002 + threadId,
2003 + runId,
2004 + nodeId: null,
2005 + providerThreadId: null,
2006 + providerTurnId: null,
2007 + nativeItemRef: null,
2008 + parentItemId: null,
2009 + ordinal: 1,
2010 + type: "assistant_message",
2011 + status: "running",
2012 + title: "Assistant",
2013 + messageId,
2014 + text,
2015 + streaming: true,
2016 + startedAt: createdAt,
2017 + completedAt: null,
2018 + updatedAt,
2019 + },
2020 + });
2021 + return parse.mock.calls
2022 + .filter(([json]) => typeof json === "string" && json.includes(text))
2023 + .map(([json]) => (JSON.parse(json) as { readonly id: string }).id);
2024 + }),
2025 + (parse) => Effect.sync(() => parse.mockRestore()),
2026 + );
2027 +
2028 + assert.deepStrictEqual(parsedPayloads, []);
2029 + assert.deepStrictEqual(
2030 + yield* sql`
2031 + SELECT created_at, updated_at FROM orchestration_v2_projection_messages
2032 + WHERE message_id = ${messageId}
2033 + `,
2034 + [{ created_at: DateTime.formatIso(createdAt), updated_at: DateTime.formatIso(updatedAt) }],
2035 + );
2036 + assert.deepStrictEqual(
2037 + yield* sql`
2038 + SELECT updated_at FROM orchestration_v2_projection_turn_items
2039 + WHERE turn_item_id = ${itemId}
2040 + `,
2041 + [{ updated_at: DateTime.formatIso(updatedAt) }],
2042 + );
2043 + }),
2044 + );
2045 +
2046 it.effect(
2047 "SQL recovery selects unfinished runless native subagents",
2048 () => runlessNativeSubagentNeedsRecovery,
apps/server/src/orchestration-v2/ProjectionStore.ts
+8 -5
@@ -1081,6 +1081,8 @@ const encodePlanPayload = Schema.encodeEffect(
1081 const encodeTurnItemPayload = Schema.encodeEffect(
1082 Schema.fromJsonString(OrchestrationV2TurnItemJsonSchema),
1083 );
1084 +// The codec every *Json payload schema uses for timestamps, so index columns match payload_json.
1085 +const encodePayloadTimestamp = Schema.encodeEffect(Schema.DateTimeUtcFromString);
1086 const encodeCheckpointScopePayload = Schema.encodeEffect(
1087 Schema.fromJsonString(OrchestrationV2CheckpointScopeJsonSchema),
1088 );
@@ -2453,7 +2455,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2455 }
2456 case "message.updated": {
2457 const payloadJson = yield* encodeMessagePayload(event.payload);
2456 - const payload = parseEncodedPayload(payloadJson);
2458 + const createdAt = yield* encodePayloadTimestamp(event.payload.createdAt);
2459 + const updatedAt = yield* encodePayloadTimestamp(event.payload.updatedAt);
2460 yield* sql`
2461 INSERT INTO orchestration_v2_projection_messages (
2462 message_id,
@@ -2473,8 +2476,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2476 ${event.payload.nodeId},
2477 ${event.payload.role},
2478 ${booleanInt(event.payload.streaming)},
2476 - ${stringField(payload, "createdAt")},
2477 - ${stringField(payload, "updatedAt")},
2479 + ${createdAt},
2480 + ${updatedAt},
2481 ${payloadJson}
2482 )
2483 ON CONFLICT(message_id)
@@ -2524,7 +2527,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2527 }
2528 case "turn-item.updated": {
2529 const payloadJson = yield* encodeTurnItemPayload(event.payload);
2527 - const payload = parseEncodedPayload(payloadJson);
2530 + const updatedAt = yield* encodePayloadTimestamp(event.payload.updatedAt);
2531 yield* sql`
2532 INSERT INTO orchestration_v2_projection_turn_items (
2533 turn_item_id,
@@ -2551,7 +2554,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2554 ${event.payload.ordinal},
2555 ${event.payload.type},
2556 ${event.payload.status},
2554 - ${stringField(payload, "updatedAt")},
2557 + ${updatedAt},
2558 ${payloadJson}
2559 )
2560 ON CONFLICT(turn_item_id)