Skip to content

Commit d9c0920

Browse files
fix(server): malformed legacy timestamps no longer block startup (pingdotgg#30823)
1 parent 6c7d23d commit d9c0920

2 files changed

Lines changed: 91 additions & 30 deletions

File tree

‎apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.ts‎

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,16 @@ it.effect(
155155
const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
156156
const projections = yield* ProjectionStore.ProjectionStoreV2;
157157
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-
`;
162158

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+
);
164168

165169
const later = yield* projections.getThreadProjection(ThreadId.make("thread:later"));
166170
assert.deepStrictEqual(
@@ -219,6 +223,53 @@ it.effect("propagates interruption from transcript reads and retry-marker writes
219223
}).pipe(Effect.provide(layerTest)),
220224
);
221225

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+
222273
it.effect("caches the absent legacy transcript for a native V2 thread", () =>
223274
Effect.gen(function* () {
224275
const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;

‎apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.ts‎

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,19 @@ function settledOverrideFor(value: string | null): OrchestrationV2AppThread["set
186186
return value === "settled" || value === "active" ? value : null;
187187
}
188188

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);
191192
}
192193

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);
195197
}
196198

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);
198202
const threadId = ThreadId.make(row.thread_id);
199203
const modelSelection = modelSelectionFor(row);
200204
const branch = row.branch?.trim() || null;
@@ -234,27 +238,30 @@ function importedThread(row: LegacyThreadRow): OrchestrationV2AppThread {
234238
rootThreadId: threadId,
235239
},
236240
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),
240244
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),
247251
pinOrderKey: row.pin_order_key?.trim() || null,
248252
lastVisitedAt: null,
249-
deletedAt: nullableDateTime(row.deleted_at),
253+
deletedAt: nullableDateTime(row.deleted_at, updatedAt),
250254
};
251255
}
252256

253-
function messageEvents(row: LegacyMessageRow): ReadonlyArray<OrchestrationV2DomainEvent> {
257+
function messageEvents(
258+
row: LegacyMessageRow,
259+
importedAt: DateTime.Utc,
260+
): ReadonlyArray<OrchestrationV2DomainEvent> {
254261
const threadId = ThreadId.make(row.thread_id);
255262
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);
258265
const attachments = attachmentsFor(row);
259266
const context = contextFor(row);
260267
const message: OrchestrationV2ConversationMessage = {
@@ -433,7 +440,7 @@ const make = Effect.gen(function* () {
433440
});
434441

435442
const reconcileShellsBase = Effect.gen(function* () {
436-
const now = DateTime.formatIso(yield* DateTime.now);
443+
const now = yield* DateTime.now;
437444
const repairRows = yield* sql<LegacyRepairRow>`
438445
SELECT
439446
thread.thread_id,
@@ -482,7 +489,7 @@ const make = Effect.gen(function* () {
482489
const decoded = decodeStoredThread(row.payload_json);
483490
if (Option.isNone(decoded)) continue;
484491
const current = decoded.value;
485-
const legacy = importedThread(row);
492+
const legacy = importedThread(row, now);
486493
const legacyPullRequests = legacy.pullRequests ?? [];
487494
const repaired: OrchestrationV2AppThread = {
488495
...current,
@@ -531,7 +538,7 @@ const make = Effect.gen(function* () {
531538
type: "thread.metadata-updated",
532539
threadId: repaired.id,
533540
providerInstanceId: repaired.providerInstanceId,
534-
occurredAt: dateTime(now),
541+
occurredAt: now,
535542
payload: repaired,
536543
},
537544
],
@@ -578,7 +585,7 @@ const make = Effect.gen(function* () {
578585
let importedThreadCount = repairedThreadCount;
579586
let importedMessageCount = 0;
580587
for (const row of rows) {
581-
const thread = importedThread(row);
588+
const thread = importedThread(row, now);
582589
const previews = yield* listShellMessages(thread.id);
583590
const events: Array<OrchestrationV2DomainEvent> = [
584591
{
@@ -589,7 +596,7 @@ const make = Effect.gen(function* () {
589596
occurredAt: thread.createdAt,
590597
payload: thread,
591598
},
592-
...previews.flatMap(messageEvents),
599+
...previews.flatMap((message) => messageEvents(message, now)),
593600
{
594601
id: EventId.make(`${IMPORT_EVENT_PREFIX}:thread:${row.thread_id}:shell`),
595602
type: "thread.metadata-updated",
@@ -632,7 +639,7 @@ const make = Effect.gen(function* () {
632639
VALUES (
633640
${thread.id},
634641
${row.updated_at},
635-
${now},
642+
${DateTime.formatIso(now)},
636643
NULL,
637644
${previews.length},
638645
NULL
@@ -695,6 +702,7 @@ const make = Effect.gen(function* () {
695702
confirmedTranscriptThreadIds.add(threadId);
696703
return { importedThreadCount: 0, importedMessageCount: 0 };
697704
}
705+
const importedAt = yield* DateTime.now;
698706
const messages = yield* listMessages(threadId);
699707
// Hint the index to skip V1 events without scanning their payloads.
700708
const existingRows = yield* sql<{ readonly event_id: string }>`
@@ -728,7 +736,9 @@ const make = Effect.gen(function* () {
728736
`,
729737
{ discard: true },
730738
);
731-
yield* eventSink.write({ events: batch.flatMap(messageEvents) });
739+
yield* eventSink.write({
740+
events: batch.flatMap((message) => messageEvents(message, importedAt)),
741+
});
732742
yield* Effect.yieldNow;
733743
}
734744
const now = DateTime.formatIso(yield* DateTime.now);

0 commit comments

Comments
 (0)