Skip to content

Commit 674a81c

Browse files
fix(server): t3 thread and t3 project no longer create a database when statev2.sqlite is missing
Both commands built the auth layer, which opens and migrates statev2.sqlite, before reading server-runtime.json. With the database moved aside, they left an empty migrated database behind the live server. With no server registered, `t3 thread` created a whole data dir only to report that no server runs. `t3 thread` now reads the runtime state first and fails before building any storage layer. When a server is registered and the database is missing, both commands fail with "T3 Code's database is not at <path>. The server may be restarting." and create nothing. Offline `t3 project` with no server registered still creates the database on a fresh install. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 99658aa commit 674a81c

4 files changed

Lines changed: 53 additions & 26 deletions

File tree

‎apps/server/src/cli/config.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
88
import { parsePersistedServerObservabilitySettings } from "@t3tools/shared/serverSettings";
99
import { DesktopBackendBootstrap, PortSchema } from "@t3tools/contracts";
1010
import * as Config from "effect/Config";
11+
import * as Console from "effect/Console";
1112
import * as Duration from "effect/Duration";
1213
import * as Effect from "effect/Effect";
1314
import * as FileSystem from "effect/FileSystem";
1415
import * as LogLevel from "effect/LogLevel";
1516
import * as Option from "effect/Option";
1617
import * as Path from "effect/Path";
1718
import * as Redacted from "effect/Redacted";
19+
import * as Runtime from "effect/Runtime";
1820
import * as Schema from "effect/Schema";
1921
import * as SchemaIssue from "effect/SchemaIssue";
2022
import * as SchemaTransformation from "effect/SchemaTransformation";
@@ -532,6 +534,31 @@ export const resolveCliAuthConfig = (
532534
cliLogLevel,
533535
);
534536

537+
export class ServerDatabaseMissingError extends Schema.TaggedError<ServerDatabaseMissingError>()(
538+
"ServerDatabaseMissingError",
539+
{ dbPath: Schema.String },
540+
) {
541+
override readonly [Runtime.errorReported] = false;
542+
override get message(): string {
543+
return `T3 Code's database is not at ${this.dbPath}. The server may be restarting.`;
544+
}
545+
}
546+
547+
/**
548+
* A CLI that talks to a registered server must not open the database itself
549+
* when it is missing: opening creates and migrates an empty one behind it.
550+
*/
551+
export const requireServerDatabase = Effect.fn("cli.requireServerDatabase")(function* (
552+
dbPath: string,
553+
) {
554+
const fs = yield* FileSystem.FileSystem;
555+
if (!(yield* fs.exists(dbPath))) {
556+
const error = new ServerDatabaseMissingError({ dbPath });
557+
yield* Console.error(error.message);
558+
return yield* error;
559+
}
560+
});
561+
535562
const DurationShorthandPattern = /^(?<value>\d+)(?<unit>ms|s|m|h|d|w)$/i;
536563

537564
const parseDurationInput = (value: string): Duration.Duration | null => {

‎apps/server/src/cli/project.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ it.effect.each(["snapshot", "mutation"] as const)(
137137
const baseDir = NodePath.join(root, "state");
138138
const workspaceRoot = NodePath.join(root, "workspace");
139139
yield* fs.makeDirectory(workspaceRoot);
140+
// A running server owns a database; the CLI refuses to work without one.
141+
yield* readProjects(baseDir);
140142
yield* persistServerRuntimeState({
141143
path: NodePath.join(baseDir, "userdata", "server-runtime.json"),
142144
state: yield* makePersistedServerRuntimeState({

‎apps/server/src/cli/project.ts‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ import {
4141
readPersistedServerRuntimeState,
4242
} from "../serverRuntimeState.ts";
4343
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
44-
import { type CliAuthLocationFlags, projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
44+
import {
45+
type CliAuthLocationFlags,
46+
projectLocationFlags,
47+
requireServerDatabase,
48+
resolveCliAuthConfig,
49+
} from "./config.ts";
4550

4651
type ProjectMutationTarget = {
4752
readonly id: ProjectId;
@@ -346,24 +351,10 @@ const getOfflineSnapshot = Effect.fn("getOfflineSnapshot")(function* () {
346351
});
347352

348353
const tryResolveLiveProjectExecutionMode = Effect.fn("tryResolveLiveProjectExecutionMode")(
349-
function* (
350-
environmentAuth: EnvironmentAuth.EnvironmentAuth["Service"],
351-
config: ServerConfig.ServerConfig["Service"],
352-
) {
353-
const runtimeState = yield* readPersistedServerRuntimeState(config.serverRuntimeStatePath);
354-
if (Option.isNone(runtimeState)) {
355-
return Option.none<{ readonly origin: string }>();
356-
}
357-
354+
function* (environmentAuth: EnvironmentAuth.EnvironmentAuth["Service"], origin: string) {
358355
const attempt = withProjectCliSessionToken(environmentAuth, (token) =>
359-
fetchLiveOrchestrationSnapshot(
360-
runtimeState.value.origin,
361-
token,
362-
PROJECT_CLI_LIVE_SERVER_PROBE_TIMEOUT,
363-
).pipe(
364-
Effect.as({
365-
origin: runtimeState.value.origin,
366-
}),
356+
fetchLiveOrchestrationSnapshot(origin, token, PROJECT_CLI_LIVE_SERVER_PROBE_TIMEOUT).pipe(
357+
Effect.as({ origin }),
367358
),
368359
);
369360

@@ -373,7 +364,7 @@ const tryResolveLiveProjectExecutionMode = Effect.fn("tryResolveLiveProjectExecu
373364
}
374365

375366
yield* Effect.logDebug("Failed to connect to the persisted project CLI server.", {
376-
origin: runtimeState.value.origin,
367+
origin,
377368
cause: attempted.failure,
378369
});
379370
return Option.none<{ readonly origin: string }>();
@@ -401,10 +392,16 @@ const runProjectMutation = Effect.fn("runProjectMutation")(function* (
401392
const logLevel = yield* GlobalFlag.LogLevel;
402393
const config = yield* resolveCliAuthConfig(flags, logLevel);
403394
const minimumLogLevel = config.logLevel;
395+
const runtimeState = yield* readPersistedServerRuntimeState(config.serverRuntimeStatePath);
396+
if (Option.isSome(runtimeState)) {
397+
yield* requireServerDatabase(config.dbPath);
398+
}
404399

405400
return yield* Effect.gen(function* () {
406401
const environmentAuth = yield* EnvironmentAuth.EnvironmentAuth;
407-
const liveMode = yield* tryResolveLiveProjectExecutionMode(environmentAuth, config);
402+
const liveMode = Option.isSome(runtimeState)
403+
? yield* tryResolveLiveProjectExecutionMode(environmentAuth, runtimeState.value.origin)
404+
: Option.none<{ readonly origin: string }>();
408405

409406
if (Option.isSome(liveMode)) {
410407
return yield* withProjectCliSessionToken(environmentAuth, (token) =>

‎apps/server/src/cli/thread.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
3030
import * as ServerConfig from "../config.ts";
3131
import { mcpToolSession, type McpToolSession } from "../mcp/AcpMcpStdioBridge.ts";
3232
import { readPersistedServerRuntimeState } from "../serverRuntimeState.ts";
33-
import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
33+
import { projectLocationFlags, requireServerDatabase, resolveCliAuthConfig } from "./config.ts";
3434

3535
/**
3636
* Short enough that a run which stops to ask the user something is noticed
@@ -208,12 +208,13 @@ const withThreadTools = Effect.fn("cli.thread.withThreadTools")(function* <A, E,
208208
) {
209209
const logLevel = yield* GlobalFlag.LogLevel;
210210
const config = yield* resolveCliAuthConfig(flags, logLevel);
211+
const runtimeState = yield* readPersistedServerRuntimeState(config.serverRuntimeStatePath);
212+
if (Option.isNone(runtimeState)) {
213+
return yield* new ThreadServerNotRunningError({ baseDir: config.baseDir });
214+
}
215+
yield* requireServerDatabase(config.dbPath);
216+
const origin = runtimeState.value.origin;
211217
return yield* Effect.gen(function* () {
212-
const runtimeState = yield* readPersistedServerRuntimeState(config.serverRuntimeStatePath);
213-
if (Option.isNone(runtimeState)) {
214-
return yield* new ThreadServerNotRunningError({ baseDir: config.baseDir });
215-
}
216-
const origin = runtimeState.value.origin;
217218
const environmentAuth = yield* EnvironmentAuth.EnvironmentAuth;
218219
const issued = yield* Effect.acquireRelease(
219220
environmentAuth.issueMcpClientSession({

0 commit comments

Comments
 (0)