@devries-cameron20 / t3code Mirror · read-only / commits / 2e741fc089
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 18:30 UTC

fix(desktop): wait for the server's database upgrade instead of reporting a failed start (#35056)

Julius Marminge committed Nov 12, 2026 at 16:00 UTC 2e741fc089e174818728e301ae11d7daf42d6b0f
5 files changed +74 -5
apps/desktop/src/backend/DesktopBackendManager.test.ts
+40 -3
@@ -5,6 +5,11 @@ import {
5 type DesktopBackendBootstrap as DesktopBackendBootstrapValue,
6 DesktopTelemetryControlMessage,
7 } from "@t3tools/contracts";
8 +import {
9 + DATABASE_MIGRATION_PROGRESS_MESSAGE,
10 + DATABASE_MIGRATIONS_FINISHED_MESSAGE,
11 + DATABASE_MIGRATIONS_STARTED_MESSAGE,
12 +} from "@t3tools/shared/databaseMigrationLog";
13 import { assert, describe, it } from "@effect/vitest";
14 import * as Deferred from "effect/Deferred";
15 import * as Duration from "effect/Duration";
@@ -688,12 +693,21 @@ describe("DesktopBackendManager", () => {
693 Effect.scoped(
694 Effect.gen(function* () {
695 const reports = yield* Queue.unbounded<DesktopBackendManager.BackendStartupFailure>();
691 - const startInstance = (status: Ref.Ref<number>, onReady?: Effect.Effect<void>) =>
696 + const startInstance = (
697 + status: Ref.Ref<number>,
698 + onReady?: Effect.Effect<void>,
699 + stdout = "",
700 + ) =>
701 makeTestInstance({
702 spawnerLayer: Layer.succeed(
703 ChildProcessSpawner.ChildProcessSpawner,
704 ChildProcessSpawner.make(() =>
696 - Effect.succeed(makeProcess({ exitCode: Effect.never })),
705 + Effect.succeed(
706 + makeProcess({
707 + exitCode: Effect.never,
708 + stdout: Stream.make(new TextEncoder().encode(stdout)),
709 + }),
710 + ),
711 ),
712 ),
713 httpClientLayer: layerHttpClient((request) =>
@@ -712,7 +726,30 @@ describe("DesktopBackendManager", () => {
726 yield* Deferred.await(ready);
727 assert.isTrue(Option.isNone(yield* Queue.poll(reports)));
728
715 - yield* startInstance(yield* Ref.make(503));
729 + const migratingStatus = yield* Ref.make(503);
730 + const migrated = yield* Deferred.make<void>();
731 + yield* startInstance(
732 + migratingStatus,
733 + Deferred.succeed(migrated, undefined).pipe(Effect.asVoid),
734 + // Enough progress lines that the start line scrolls out of the output tail.
735 + `INFO ${DATABASE_MIGRATIONS_STARTED_MESSAGE}\n` +
736 + Array.from(
737 + { length: 80 },
738 + (_, index) =>
739 + `INFO ${DATABASE_MIGRATION_PROGRESS_MESSAGE} migration=${index}_Migration progress="${index + 1} of 80"\n`,
740 + ).join(""),
741 + );
742 + yield* TestClock.adjust(Duration.minutes(10));
743 + assert.isTrue(Option.isNone(yield* Queue.poll(reports)));
744 + yield* Ref.set(migratingStatus, 200);
745 + yield* TestClock.adjust(Duration.millis(100));
746 + yield* Deferred.await(migrated);
747 +
748 + yield* startInstance(
749 + yield* Ref.make(503),
750 + undefined,
751 + `${DATABASE_MIGRATIONS_STARTED_MESSAGE}\n${DATABASE_MIGRATIONS_FINISHED_MESSAGE}\n`,
752 + );
753 yield* TestClock.adjust(Duration.seconds(179));
754 assert.isTrue(Option.isNone(yield* Queue.poll(reports)));
755 yield* TestClock.adjust(Duration.seconds(1));
apps/desktop/src/backend/DesktopBackendManager.ts
+7
@@ -25,6 +25,7 @@ import {
25 DesktopTelemetryControlMessage,
26 type DesktopTelemetryControlMessage as DesktopTelemetryControlMessageValue,
27 } from "@t3tools/contracts";
28 +import { isRunningDatabaseMigrations } from "@t3tools/shared/databaseMigrationLog";
29 import { waitForHttpReady as waitForHttpReadyShared } from "@t3tools/shared/httpReadiness";
30
31 import * as DesktopObservability from "../app/DesktopObservability.ts";
@@ -971,6 +972,12 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
972 }),
973 onReadinessFailure: Effect.fn("desktop.backendInstance.onReadinessFailure")(
974 function* (error) {
975 + if (isRunningDatabaseMigrations(yield* Ref.get(outputTail))) {
976 + yield* logInstanceWarning("backend is still migrating its database; waiting", {
977 + error: error.message,
978 + });
979 + return;
980 + }
981 yield* logInstanceWarning("backend readiness check failed during bootstrap", {
982 error: error.message,
983 });
apps/server/src/persistence/Migrations.ts
+9 -2
@@ -1,6 +1,11 @@
1 import * as Migrator from "effect/sql/Migrator";
2 import * as Effect from "effect/Effect";
3 import * as SqlClient from "effect/sql/SqlClient";
4 +import {
5 + DATABASE_MIGRATION_PROGRESS_MESSAGE,
6 + DATABASE_MIGRATIONS_FINISHED_MESSAGE,
7 + DATABASE_MIGRATIONS_STARTED_MESSAGE,
8 +} from "@t3tools/shared/databaseMigrationLog";
9 import { reconcileV2PreviewMigration } from "./reconcileV2PreviewMigration.ts";
10
11 import Migration0001 from "./Migrations/001_OrchestrationEvents.ts";
@@ -264,6 +269,8 @@ export interface RunMigrationsOptions {
269 export const runMigrations = Effect.fn("runMigrations")(function* ({
270 toMigrationInclusive,
271 }: RunMigrationsOptions = {}) {
272 + // The desktop waits past its readiness budget until the finished line follows this one.
273 + if (yield* hasPendingMigrations()) yield* Effect.log(DATABASE_MIGRATIONS_STARTED_MESSAGE);
274 const includesV2 =
275 toMigrationInclusive === undefined || toMigrationInclusive >= FIRST_V2_MIGRATION_ID;
276 const previewMigrations = includesV2 ? yield* reconcileV2PreviewMigration() : [];
@@ -290,7 +297,7 @@ export const runMigrations = Effect.fn("runMigrations")(function* ({
297 const executedMigrations: Array<readonly [id: number, name: string]> = [...previewMigrations];
298 for (const [index, [id, name]] of pending.entries()) {
299 if (latestRecordedId > 0) {
293 - yield* Effect.log("Upgrading database").pipe(
300 + yield* Effect.log(DATABASE_MIGRATION_PROGRESS_MESSAGE).pipe(
301 Effect.annotateLogs({
302 migration: `${id}_${name}`,
303 progress: `${index + 1} of ${pending.length}`,
@@ -302,7 +309,7 @@ export const runMigrations = Effect.fn("runMigrations")(function* ({
309 const migrations = executedMigrations.map(([id, name]) => `${id}_${name}`);
310 yield* migrations.length === 0
311 ? Effect.logDebug("Database schema is current")
305 - : Effect.log("Migrations ran successfully").pipe(Effect.annotateLogs({ migrations }));
312 + : Effect.log(DATABASE_MIGRATIONS_FINISHED_MESSAGE).pipe(Effect.annotateLogs({ migrations }));
313
314 const recorded = yield* sql<{
315 readonly migration_id: number;
packages/shared/package.json
+4
@@ -367,6 +367,10 @@
367 "types": "./src/testing/symlinks.ts",
368 "import": "./src/testing/symlinks.ts"
369 },
370 + "./databaseMigrationLog": {
371 + "types": "./src/databaseMigrationLog.ts",
372 + "import": "./src/databaseMigrationLog.ts"
373 + },
374 "./httpReadiness": {
375 "types": "./src/httpReadiness.ts",
376 "import": "./src/httpReadiness.ts"
packages/shared/src/databaseMigrationLog.ts new
+14
@@ -0,0 +1,14 @@
1 +// The server logs these around pending migrations; the desktop reads them from its output tail,
2 +// which keeps only the last few thousand characters, so each progress line also marks a running upgrade.
3 +export const DATABASE_MIGRATIONS_STARTED_MESSAGE = "Running database migrations";
4 +export const DATABASE_MIGRATION_PROGRESS_MESSAGE = "Upgrading database";
5 +export const DATABASE_MIGRATIONS_FINISHED_MESSAGE = "Migrations ran successfully";
6 +
7 +export function isRunningDatabaseMigrations(output: string): boolean {
8 + return (
9 + Math.max(
10 + output.lastIndexOf(DATABASE_MIGRATIONS_STARTED_MESSAGE),
11 + output.lastIndexOf(DATABASE_MIGRATION_PROGRESS_MESSAGE),
12 + ) > output.lastIndexOf(DATABASE_MIGRATIONS_FINISHED_MESSAGE)
13 + );
14 +}