The best way to control Claude Code, Codex and more from any computer or phone
fix(server): malformed legacy timestamps no longer block startup (#30823)
Julius Marminge committed
Nov 11, 2026 at 15:28 UTC
d9c09204491070d3ece9f993d5539f3b6336d0d9
2 files changed
+91
-30
apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.ts
+56
-5
@@ -155,12 +155,16 @@ it.effect(
155
const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
156
const projections = yield* ProjectionStore.ProjectionStoreV2;
157
yield* seedPendingTranscripts;
158
- yield* sql`
159
- UPDATE projection_thread_messages SET created_at = '0000-not-a-date'
160
- WHERE message_id = 'thread:first:old'
161
- `;
158
163
- yield* importer.importPendingTranscripts;
159
+ yield* importer.importPendingTranscripts.pipe(
160
+ Effect.provideService(Statement.CurrentTransformer, (statement) => {
161
+ const [query, params] = statement.compile();
162
+ return query.includes("FROM projection_thread_messages") &&
163
+ params.includes("thread:first")
164
+ ? Effect.die("transcript read failed")
165
+ : Effect.succeed(statement);
166
+ }),
167
+ );
168
169
const later = yield* projections.getThreadProjection(ThreadId.make("thread:later"));
170
assert.deepStrictEqual(
@@ -219,6 +223,53 @@ it.effect("propagates interruption from transcript reads and retry-marker writes
223
}).pipe(Effect.provide(layerTest)),
224
);
225
226
+// regression: #30813 a malformed V1 timestamp stopped the server from starting
227
+it.effect("imports legacy rows with malformed timestamps and keeps the archive marker", () =>
228
+ Effect.gen(function* () {
229
+ const sql = yield* SqlClient.SqlClient;
230
+ const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
231
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
232
+ const malformed = "2026-07-13T14:13:00.3NZ";
233
+ yield* sql`
234
+ INSERT INTO projection_projects (
235
+ project_id, title, workspace_root, scripts_json, created_at, updated_at
236
+ ) VALUES (
237
+ 'project:malformed', 'Legacy project', '/tmp/legacy-malformed', '[]',
238
+ '2026-01-01T00:00:00.000Z', '2026-01-01T00:00:00.000Z'
239
+ )
240
+ `;
241
+ yield* sql`
242
+ INSERT INTO projection_threads (
243
+ thread_id, project_id, title, model_selection_json, runtime_mode,
244
+ interaction_mode, created_at, updated_at, archived_at
245
+ ) VALUES (
246
+ 'thread:malformed', 'project:malformed', 'Legacy thread',
247
+ '{"instanceId":"codex","model":"gpt-5.4"}', 'full-access', 'default',
248
+ '2026-07-13T14:00:00.000Z', ${malformed}, ${malformed}
249
+ )
250
+ `;
251
+ yield* sql`
252
+ INSERT INTO projection_thread_messages (
253
+ message_id, thread_id, role, text, is_streaming, created_at, updated_at
254
+ ) VALUES (
255
+ 'message:malformed', 'thread:malformed', 'user', 'Question', 0,
256
+ ${malformed}, '2026-07-13T14:13:00.000Z'
257
+ )
258
+ `;
259
+
260
+ assert.deepStrictEqual(yield* importer.reconcileShells, {
261
+ importedThreadCount: 1,
262
+ importedMessageCount: 1,
263
+ });
264
+ const projection = yield* projections.getThreadProjection(ThreadId.make("thread:malformed"));
265
+ assert.isNotNull(projection.thread.archivedAt);
266
+ assert.deepStrictEqual(
267
+ projection.messages.map((message) => message.text),
268
+ ["Question"],
269
+ );
270
+ }).pipe(Effect.provide(layerTest)),
271
+);
272
+
273
it.effect("caches the absent legacy transcript for a native V2 thread", () =>
274
Effect.gen(function* () {
275
const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.ts
+35
-25
@@ -186,15 +186,19 @@ function settledOverrideFor(value: string | null): OrchestrationV2AppThread["set
186
return value === "settled" || value === "active" ? value : null;
187
}
188
189
-function dateTime(value: string): DateTime.Utc {
190
- return DateTime.makeUnsafe(value);
189
+// V1 rows can hold unparseable timestamps; fall back to another time rather than fail the import.
190
+function dateTimeOr(value: string | null, fallback: DateTime.Utc): DateTime.Utc {
191
+ return value === null ? fallback : Option.getOrElse(DateTime.make(value), () => fallback);
192
}
193
193
-function nullableDateTime(value: string | null): DateTime.Utc | null {
194
- return value === null ? null : dateTime(value);
194
+// A malformed marker stays set so archived or deleted threads keep that state.
195
+function nullableDateTime(value: string | null, fallback: DateTime.Utc): DateTime.Utc | null {
196
+ return value === null ? null : dateTimeOr(value, fallback);
197
}
198
197
-function importedThread(row: LegacyThreadRow): OrchestrationV2AppThread {
199
+function importedThread(row: LegacyThreadRow, importedAt: DateTime.Utc): OrchestrationV2AppThread {
200
+ const createdAt = dateTimeOr(row.created_at, dateTimeOr(row.updated_at, importedAt));
201
+ const updatedAt = dateTimeOr(row.updated_at, createdAt);
202
const threadId = ThreadId.make(row.thread_id);
203
const modelSelection = modelSelectionFor(row);
204
const branch = row.branch?.trim() || null;
@@ -234,27 +238,30 @@ function importedThread(row: LegacyThreadRow): OrchestrationV2AppThread {
238
rootThreadId: threadId,
239
},
240
forkedFrom: null,
237
- createdAt: dateTime(row.created_at),
238
- updatedAt: dateTime(row.updated_at),
239
- archivedAt: nullableDateTime(row.archived_at),
241
+ createdAt,
242
+ updatedAt,
243
+ archivedAt: nullableDateTime(row.archived_at, updatedAt),
244
settledOverride: settledOverrideFor(row.settled_override),
241
- settledAt: nullableDateTime(row.settled_at),
242
- unsettledAt: nullableDateTime(row.unsettled_at),
243
- snoozedUntil: nullableDateTime(row.snoozed_until),
244
- snoozedAt: nullableDateTime(row.snoozed_at),
245
- pinnedAt: nullableDateTime(row.pinned_at),
246
- autoSettleDisabledAt: nullableDateTime(row.auto_settle_disabled_at),
245
+ settledAt: nullableDateTime(row.settled_at, updatedAt),
246
+ unsettledAt: nullableDateTime(row.unsettled_at, updatedAt),
247
+ snoozedUntil: nullableDateTime(row.snoozed_until, updatedAt),
248
+ snoozedAt: nullableDateTime(row.snoozed_at, updatedAt),
249
+ pinnedAt: nullableDateTime(row.pinned_at, updatedAt),
250
+ autoSettleDisabledAt: nullableDateTime(row.auto_settle_disabled_at, updatedAt),
251
pinOrderKey: row.pin_order_key?.trim() || null,
252
lastVisitedAt: null,
249
- deletedAt: nullableDateTime(row.deleted_at),
253
+ deletedAt: nullableDateTime(row.deleted_at, updatedAt),
254
};
255
}
256
253
-function messageEvents(row: LegacyMessageRow): ReadonlyArray<OrchestrationV2DomainEvent> {
257
+function messageEvents(
258
+ row: LegacyMessageRow,
259
+ importedAt: DateTime.Utc,
260
+): ReadonlyArray<OrchestrationV2DomainEvent> {
261
const threadId = ThreadId.make(row.thread_id);
262
const messageId = MessageId.make(row.message_id);
256
- const createdAt = dateTime(row.created_at);
257
- const updatedAt = dateTime(row.updated_at);
263
+ const createdAt = dateTimeOr(row.created_at, dateTimeOr(row.updated_at, importedAt));
264
+ const updatedAt = dateTimeOr(row.updated_at, createdAt);
265
const attachments = attachmentsFor(row);
266
const context = contextFor(row);
267
const message: OrchestrationV2ConversationMessage = {
@@ -433,7 +440,7 @@ const make = Effect.gen(function* () {
440
});
441
442
const reconcileShellsBase = Effect.gen(function* () {
436
- const now = DateTime.formatIso(yield* DateTime.now);
443
+ const now = yield* DateTime.now;
444
const repairRows = yield* sql<LegacyRepairRow>`
445
SELECT
446
thread.thread_id,
@@ -482,7 +489,7 @@ const make = Effect.gen(function* () {
489
const decoded = decodeStoredThread(row.payload_json);
490
if (Option.isNone(decoded)) continue;
491
const current = decoded.value;
485
- const legacy = importedThread(row);
492
+ const legacy = importedThread(row, now);
493
const legacyPullRequests = legacy.pullRequests ?? [];
494
const repaired: OrchestrationV2AppThread = {
495
...current,
@@ -531,7 +538,7 @@ const make = Effect.gen(function* () {
538
type: "thread.metadata-updated",
539
threadId: repaired.id,
540
providerInstanceId: repaired.providerInstanceId,
534
- occurredAt: dateTime(now),
541
+ occurredAt: now,
542
payload: repaired,
543
},
544
],
@@ -578,7 +585,7 @@ const make = Effect.gen(function* () {
585
let importedThreadCount = repairedThreadCount;
586
let importedMessageCount = 0;
587
for (const row of rows) {
581
- const thread = importedThread(row);
588
+ const thread = importedThread(row, now);
589
const previews = yield* listShellMessages(thread.id);
590
const events: Array<OrchestrationV2DomainEvent> = [
591
{
@@ -589,7 +596,7 @@ const make = Effect.gen(function* () {
596
occurredAt: thread.createdAt,
597
payload: thread,
598
},
592
- ...previews.flatMap(messageEvents),
599
+ ...previews.flatMap((message) => messageEvents(message, now)),
600
{
601
id: EventId.make(`${IMPORT_EVENT_PREFIX}:thread:${row.thread_id}:shell`),
602
type: "thread.metadata-updated",
@@ -632,7 +639,7 @@ const make = Effect.gen(function* () {
639
VALUES (
640
${thread.id},
641
${row.updated_at},
635
- ${now},
642
+ ${DateTime.formatIso(now)},
643
NULL,
644
${previews.length},
645
NULL
@@ -695,6 +702,7 @@ const make = Effect.gen(function* () {
702
confirmedTranscriptThreadIds.add(threadId);
703
return { importedThreadCount: 0, importedMessageCount: 0 };
704
}
705
+ const importedAt = yield* DateTime.now;
706
const messages = yield* listMessages(threadId);
707
// Hint the index to skip V1 events without scanning their payloads.
708
const existingRows = yield* sql<{ readonly event_id: string }>`
@@ -728,7 +736,9 @@ const make = Effect.gen(function* () {
736
`,
737
{ discard: true },
738
);
731
- yield* eventSink.write({ events: batch.flatMap(messageEvents) });
739
+ yield* eventSink.write({
740
+ events: batch.flatMap((message) => messageEvents(message, importedAt)),
741
+ });
742
yield* Effect.yieldNow;
743
}
744
const now = DateTime.formatIso(yield* DateTime.now);