Skip to content

Commit 6cee913

Browse files
committed
fix(server): cache absent legacy transcripts for V2 threads
1 parent 8045537 commit 6cee913

2 files changed

Lines changed: 67 additions & 4 deletions

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { assert, it } from "@effect/vitest";
22
import {
33
ComposerContextId,
44
EventId,
5+
ProjectId,
6+
ProviderInstanceId,
57
type OrchestrationMessageContext,
68
ThreadId,
79
} from "@t3tools/contracts";
@@ -150,6 +152,69 @@ it.effect("propagates interruption from transcript reads and retry-marker writes
150152
}).pipe(Effect.provide(layerTest)),
151153
);
152154

155+
it.effect("caches the absent legacy transcript for a native V2 thread", () =>
156+
Effect.gen(function* () {
157+
const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
158+
const eventSink = yield* EventSink.EventSinkV2;
159+
const projections = yield* ProjectionStore.ProjectionStoreV2;
160+
const threadId = ThreadId.make("thread:v2-born");
161+
const providerInstanceId = ProviderInstanceId.make("codex");
162+
const now = DateTime.makeUnsafe("2026-01-01T00:00:00.000Z");
163+
yield* eventSink.write({
164+
events: [
165+
{
166+
id: EventId.make("event:v2-born"),
167+
type: "thread.created",
168+
threadId,
169+
occurredAt: now,
170+
payload: {
171+
id: threadId,
172+
projectId: ProjectId.make("project:v2-born"),
173+
title: "Native V2 thread",
174+
providerInstanceId,
175+
modelSelection: { instanceId: providerInstanceId, model: "gpt-6" },
176+
createdBy: "user",
177+
creationSource: "web",
178+
runtimeMode: "full-access",
179+
interactionMode: "default",
180+
branch: null,
181+
worktreePath: null,
182+
activeProviderThreadId: null,
183+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
184+
forkedFrom: null,
185+
createdAt: now,
186+
updatedAt: now,
187+
archivedAt: null,
188+
settledOverride: null,
189+
settledAt: null,
190+
lastVisitedAt: null,
191+
deletedAt: null,
192+
},
193+
},
194+
],
195+
});
196+
const before = yield* projections.getThreadProjection(threadId);
197+
let legacyLookups = 0;
198+
for (let read = 0; read < 3; read++) {
199+
const summary = yield* importer.ensureTranscript(threadId).pipe(
200+
Effect.provideService(Statement.CurrentTransformer, (statement) => {
201+
const [query, parameters] = statement.compile();
202+
if (
203+
query.includes("FROM orchestration_v2_legacy_imports") &&
204+
parameters[0] === threadId
205+
) {
206+
legacyLookups++;
207+
}
208+
return Effect.succeed(statement);
209+
}),
210+
);
211+
assert.deepStrictEqual(summary, { importedThreadCount: 0, importedMessageCount: 0 });
212+
}
213+
assert.equal(legacyLookups, 1);
214+
assert.deepStrictEqual(yield* projections.getThreadProjection(threadId), before);
215+
}).pipe(Effect.provide(layerTest)),
216+
);
217+
153218
it.layer(layerTest)("LegacyV1ThreadImporter", (it) => {
154219
it.effect("imports lightweight shells, hydrates transcripts, and remains idempotent", () =>
155220
Effect.gen(function* () {

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ const make = Effect.gen(function* () {
677677
),
678678
);
679679

680-
// transcript_imported_at never resets; cache confirmed imports for the process lifetime to skip repeated locks and reads.
680+
// Both results are final for this process: transcript_imported_at never resets, and only startup shell reconciliation creates legacy rows.
681681
const confirmedTranscriptThreadIds = new Set<ThreadId>();
682682

683683
const ensureTranscriptBase = (threadId: ThreadId) =>
@@ -692,9 +692,7 @@ const make = Effect.gen(function* () {
692692
`.pipe(NodeSqliteClient.readOnly);
693693
const imported = imports[0];
694694
if (imported === undefined || imported.transcript_imported_at !== null) {
695-
if (imported !== undefined) {
696-
confirmedTranscriptThreadIds.add(threadId);
697-
}
695+
confirmedTranscriptThreadIds.add(threadId);
698696
return { importedThreadCount: 0, importedMessageCount: 0 };
699697
}
700698
const messages = yield* listMessages(threadId);

0 commit comments

Comments
 (0)