diff --git a/apps/server/src/orchestration-v2/LegacyV1ThreadImporter.test.ts b/apps/server/src/orchestration-v2/LegacyV1ThreadImporter.test.ts index a2afd17616..bbb081f29a 100644 --- a/apps/server/src/orchestration-v2/LegacyV1ThreadImporter.test.ts +++ b/apps/server/src/orchestration-v2/LegacyV1ThreadImporter.test.ts @@ -157,10 +157,12 @@ it.layer(TestLayer)("LegacyV1ThreadImporter", (it) => { 1, '2026-01-04T01:00:00.000Z', '2026-01-04T01:00:00.000Z' - ) + ) `; + assert.equal(yield* importer.pendingThreadCount, 1); const shellImport = yield* importer.reconcileShells; + assert.equal(yield* importer.pendingThreadCount, 1); assert.deepStrictEqual(shellImport, { importedThreadCount: 1, importedMessageCount: 2, @@ -214,6 +216,7 @@ it.layer(TestLayer)("LegacyV1ThreadImporter", (it) => { }); const transcriptImport = yield* importer.ensureTranscript(threadId); + assert.equal(yield* importer.pendingThreadCount, 0); assert.deepStrictEqual(transcriptImport, { importedThreadCount: 1, importedMessageCount: 2, diff --git a/apps/server/src/orchestration-v2/LegacyV1ThreadImporter.ts b/apps/server/src/orchestration-v2/LegacyV1ThreadImporter.ts index 5c10bacb09..946d8c3561 100644 --- a/apps/server/src/orchestration-v2/LegacyV1ThreadImporter.ts +++ b/apps/server/src/orchestration-v2/LegacyV1ThreadImporter.ts @@ -83,6 +83,7 @@ export class LegacyV1ThreadImportError extends Schema.TaggedErrorClass; readonly reconcileShells: Effect.Effect; readonly ensureTranscript: ( threadId: ThreadId, @@ -461,6 +462,31 @@ const make = Effect.gen(function* () { Effect.mapError((cause) => new LegacyV1ThreadImportError({ operation: "import", cause })), ); + const pendingThreadCount = sql<{ readonly count: number }>` + SELECT COUNT(*) AS count + FROM ( + SELECT thread.thread_id + FROM projection_threads AS thread + WHERE NOT EXISTS ( + SELECT 1 + FROM orchestration_events AS event + WHERE event.application_event_version = 2 + AND event.aggregate_kind = 'thread' + AND event.stream_id = thread.thread_id + AND event.event_type = 'thread.created' + ) + UNION + SELECT legacy_import.thread_id + FROM orchestration_v2_legacy_imports AS legacy_import + WHERE legacy_import.transcript_imported_at IS NULL + ) + `.pipe( + Effect.map((rows) => rows[0]?.count ?? 0), + Effect.mapError( + (cause) => new LegacyV1ThreadImportError({ operation: "inspect pending", cause }), + ), + ); + const ensureTranscriptBase = (threadId: ThreadId) => transcriptImports.withLock( threadId, @@ -583,6 +609,7 @@ const make = Effect.gen(function* () { ); return LegacyV1ThreadImporter.of({ + pendingThreadCount, reconcileShells, ensureTranscript, importPendingTranscripts, diff --git a/apps/server/src/orchestration-v2/ThreadManagementService.ts b/apps/server/src/orchestration-v2/ThreadManagementService.ts index e8b4cd60b9..0214525d79 100644 --- a/apps/server/src/orchestration-v2/ThreadManagementService.ts +++ b/apps/server/src/orchestration-v2/ThreadManagementService.ts @@ -645,6 +645,7 @@ const make = Effect.gen(function* () { const legacyV1ThreadImporterNoopLayer = Layer.succeed( LegacyV1ThreadImporter, LegacyV1ThreadImporter.of({ + pendingThreadCount: Effect.succeed(0), reconcileShells: Effect.succeed({ importedThreadCount: 0, importedMessageCount: 0 }), ensureTranscript: () => Effect.succeed({ importedThreadCount: 0, importedMessageCount: 0 }), importPendingTranscripts: Effect.succeed({ importedThreadCount: 0, importedMessageCount: 0 }), diff --git a/apps/server/src/serverLifecycleEvents.test.ts b/apps/server/src/serverLifecycleEvents.test.ts index 4f7b75fb4b..28ba41125b 100644 --- a/apps/server/src/serverLifecycleEvents.test.ts +++ b/apps/server/src/serverLifecycleEvents.test.ts @@ -46,8 +46,31 @@ it.effect( assertTrue(Option.isSome(ready)); assert.equal(ready.value.sequence, 2); + yield* lifecycleEvents.publish({ + version: 1, + type: "legacyThreadMigration", + payload: { + status: "running", + totalThreadCount: 12, + }, + }); + yield* lifecycleEvents.publish({ + version: 1, + type: "legacyThreadMigration", + payload: { + status: "complete", + totalThreadCount: 12, + }, + }); + const snapshot = yield* lifecycleEvents.snapshot; - assert.equal(snapshot.sequence, 2); - assert.deepEqual(snapshot.events.map((event) => event.type).toSorted(), ["ready", "welcome"]); + assert.equal(snapshot.sequence, 4); + assert.deepEqual(snapshot.events.map((event) => event.type).toSorted(), [ + "legacyThreadMigration", + "ready", + "welcome", + ]); + const migration = snapshot.events.find((event) => event.type === "legacyThreadMigration"); + assert.equal(migration?.payload.status, "complete"); }).pipe(Effect.provide(ServerLifecycleEvents.layer)), ); diff --git a/apps/server/src/serverLifecycleEvents.ts b/apps/server/src/serverLifecycleEvents.ts index 855d03490e..7d394e4dc9 100644 --- a/apps/server/src/serverLifecycleEvents.ts +++ b/apps/server/src/serverLifecycleEvents.ts @@ -8,7 +8,8 @@ import * as Stream from "effect/Stream"; type LifecycleEventInput = | Omit, "sequence"> - | Omit, "sequence">; + | Omit, "sequence"> + | Omit, "sequence">; interface SnapshotState { readonly sequence: number; @@ -39,10 +40,10 @@ const make = Effect.gen(function* () { ...event, sequence: nextSequence, } satisfies ServerLifecycleStreamEvent; - const nextEvents = - nextEvent.type === "welcome" - ? [nextEvent, ...current.events.filter((entry) => entry.type !== "welcome")] - : [nextEvent, ...current.events.filter((entry) => entry.type !== "ready")]; + const nextEvents = [ + nextEvent, + ...current.events.filter((entry) => entry.type !== nextEvent.type), + ]; return [nextEvent, { sequence: nextSequence, events: nextEvents }] as const; }).pipe(Effect.tap((event) => PubSub.publish(pubsub, event))), snapshot: Ref.get(state), diff --git a/apps/server/src/serverRuntimeStartup.ts b/apps/server/src/serverRuntimeStartup.ts index 4bf93af9e9..13d97aa1d0 100644 --- a/apps/server/src/serverRuntimeStartup.ts +++ b/apps/server/src/serverRuntimeStartup.ts @@ -419,6 +419,17 @@ export const make = Effect.gen(function* () { const welcomeBase = yield* resolveWelcomeBase; const environment = yield* serverEnvironment.getDescriptor; + const legacyMigrationThreadCount = yield* legacyV1ThreadImporter.pendingThreadCount; + if (legacyMigrationThreadCount > 0) { + yield* lifecycleEvents.publish({ + version: 1, + type: "legacyThreadMigration", + payload: { + status: "running", + totalThreadCount: legacyMigrationThreadCount, + }, + }); + } const { recovery, bootstrap: bootstrapTargets } = yield* runOrderedV2StartupPhases({ importLegacyShells: runStartupPhase( "orchestration-v2.legacy-v1.import-shells", @@ -475,14 +486,29 @@ export const make = Effect.gen(function* () { yield* Effect.logDebug("Accepting commands"); yield* commandGate.signalCommandReady; - yield* legacyV1ThreadImporter.importPendingTranscripts.pipe( + const importPendingTranscripts = legacyV1ThreadImporter.importPendingTranscripts.pipe( Effect.tap((summary) => summary.importedThreadCount === 0 ? Effect.void : Effect.logInfo("Hydrated legacy v1 thread transcripts", summary), ), - Effect.forkScoped, ); + yield* ( + legacyMigrationThreadCount > 0 + ? importPendingTranscripts.pipe( + Effect.tap(() => + lifecycleEvents.publish({ + version: 1, + type: "legacyThreadMigration", + payload: { + status: "complete", + totalThreadCount: legacyMigrationThreadCount, + }, + }), + ), + ) + : importPendingTranscripts + ).pipe(Effect.forkScoped); yield* Effect.logDebug("startup phase: publishing welcome event", { environmentId: environment.environmentId, diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index acdf608c36..18c44a61bb 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -6378,9 +6378,6 @@ function ChatViewContent(props: ChatViewProps) { - {!isDraftHeroState ? ( -
- ) : null}
{activeThreadRef && activePreviewMiniPlayer ? ( diff --git a/apps/web/src/components/LegacyThreadMigrationToast.tsx b/apps/web/src/components/LegacyThreadMigrationToast.tsx new file mode 100644 index 0000000000..720e11c429 --- /dev/null +++ b/apps/web/src/components/LegacyThreadMigrationToast.tsx @@ -0,0 +1,45 @@ +import { useAtomValue } from "@effect/atom-react"; +import { useEffect, useRef } from "react"; + +import { primaryServerLegacyThreadMigrationAtom } from "../state/server"; +import { toastManager } from "./ui/toast"; + +type MigrationToastId = ReturnType; + +export function LegacyThreadMigrationToast() { + const migration = useAtomValue(primaryServerLegacyThreadMigrationAtom); + const toastIdRef = useRef(null); + + useEffect(() => { + if (migration?.status === "running") { + if (toastIdRef.current !== null) { + return; + } + toastIdRef.current = toastManager.add({ + type: "loading", + title: "Restoring your threads…", + description: `Migrating ${migration.totalThreadCount.toLocaleString()} ${ + migration.totalThreadCount === 1 ? "thread" : "threads" + } from the previous version. You can keep working while this finishes.`, + timeout: 0, + }); + return; + } + + if (toastIdRef.current !== null) { + toastManager.close(toastIdRef.current); + toastIdRef.current = null; + } + }, [migration]); + + useEffect( + () => () => { + if (toastIdRef.current !== null) { + toastManager.close(toastIdRef.current); + } + }, + [], + ); + + return null; +} diff --git a/apps/web/src/components/chat/ThreadDetailsPanel.tsx b/apps/web/src/components/chat/ThreadDetailsPanel.tsx index cb01abe28a..3fb7935810 100644 --- a/apps/web/src/components/chat/ThreadDetailsPanel.tsx +++ b/apps/web/src/components/chat/ThreadDetailsPanel.tsx @@ -109,6 +109,7 @@ export function ThreadDetailsPanel(props: ThreadDetailsPanelProps) {
+ {primaryEnvironmentAuthenticated ? : null} {primaryEnvironmentAuthenticated ? : null} {primaryEnvironmentAuthenticated ? : null} diff --git a/apps/web/src/state/server.ts b/apps/web/src/state/server.ts index 1071d8209d..2f484f507c 100644 --- a/apps/web/src/state/server.ts +++ b/apps/web/src/state/server.ts @@ -3,6 +3,7 @@ import { type EditorId, type ServerConfig, type ServerConfigStreamEvent, + type ServerLifecycleLegacyThreadMigrationPayload, type ServerLifecycleWelcomePayload, type ServerProvider, type ServerSettings, @@ -71,6 +72,18 @@ export const primaryServerWelcomeAtom = Atom.make( (get): ServerLifecycleWelcomePayload | null => get(primaryServerStateAtom).welcome, ).pipe(Atom.withLabel("web-primary-server-welcome")); +export const primaryServerLegacyThreadMigrationAtom = Atom.make( + (get): ServerLifecycleLegacyThreadMigrationPayload | null => { + const environmentId = get(primaryEnvironmentIdAtom); + if (environmentId === null) { + return null; + } + return Option.getOrNull( + AsyncResult.value(get(serverEnvironment.legacyThreadMigration({ environmentId, input: {} }))), + ); + }, +).pipe(Atom.withLabel("web-primary-server-legacy-thread-migration")); + export const primaryServerSettingsAtom = Atom.make( (get): ServerSettings => get(primaryServerConfigAtom)?.settings ?? DEFAULT_SERVER_SETTINGS, ).pipe(Atom.withLabel("web-primary-server-settings")); diff --git a/packages/client-runtime/src/state/server.ts b/packages/client-runtime/src/state/server.ts index a52845007c..e4414cac1f 100644 --- a/packages/client-runtime/src/state/server.ts +++ b/packages/client-runtime/src/state/server.ts @@ -428,7 +428,7 @@ export function serverConfigStateChanges(environmentId: EnvironmentId) { export function projectServerWelcome( current: Option.Option, event: { - readonly type: "welcome" | "ready"; + readonly type: "welcome" | "ready" | "legacyThreadMigration"; readonly payload: unknown; }, ): readonly [ @@ -718,6 +718,18 @@ export function createServerEnvironmentAtoms( Stream.mapAccum(Option.none, projectServerWelcome), ), }), + legacyThreadMigration: createEnvironmentRpcSubscriptionAtomFamily(runtime, { + label: "environment-data:server:legacy-thread-migration", + tag: WS_METHODS.subscribeServerLifecycle, + transform: (stream) => + stream.pipe( + Stream.filterMap((event) => + event.type === "legacyThreadMigration" + ? Result.succeed(event.payload) + : Result.failVoid, + ), + ), + }), refreshProviders: createEnvironmentRpcCommand(runtime, { label: "environment-data:server:refresh-providers", tag: WS_METHODS.serverRefreshProviders, diff --git a/packages/contracts/src/server.ts b/packages/contracts/src/server.ts index 9791a4f621..a24cd42194 100644 --- a/packages/contracts/src/server.ts +++ b/packages/contracts/src/server.ts @@ -565,6 +565,13 @@ export const ServerLifecycleWelcomePayload = Schema.Struct({ }); export type ServerLifecycleWelcomePayload = typeof ServerLifecycleWelcomePayload.Type; +export const ServerLifecycleLegacyThreadMigrationPayload = Schema.Struct({ + status: Schema.Union([Schema.Literal("running"), Schema.Literal("complete")]), + totalThreadCount: NonNegativeInt, +}); +export type ServerLifecycleLegacyThreadMigrationPayload = + typeof ServerLifecycleLegacyThreadMigrationPayload.Type; + export const ServerLifecycleStreamWelcomeEvent = Schema.Struct({ version: Schema.Literal(1), sequence: NonNegativeInt, @@ -581,9 +588,19 @@ export const ServerLifecycleStreamReadyEvent = Schema.Struct({ }); export type ServerLifecycleStreamReadyEvent = typeof ServerLifecycleStreamReadyEvent.Type; +export const ServerLifecycleStreamLegacyThreadMigrationEvent = Schema.Struct({ + version: Schema.Literal(1), + sequence: NonNegativeInt, + type: Schema.Literal("legacyThreadMigration"), + payload: ServerLifecycleLegacyThreadMigrationPayload, +}); +export type ServerLifecycleStreamLegacyThreadMigrationEvent = + typeof ServerLifecycleStreamLegacyThreadMigrationEvent.Type; + export const ServerLifecycleStreamEvent = Schema.Union([ ServerLifecycleStreamWelcomeEvent, ServerLifecycleStreamReadyEvent, + ServerLifecycleStreamLegacyThreadMigrationEvent, ]); export type ServerLifecycleStreamEvent = typeof ServerLifecycleStreamEvent.Type;