Skip to content

Commit 951d56b

Browse files
fix(server): doctor reports read failures as inspection failures
--dry-run against an unreadable database said compaction failed. Also say "No recommended repairs" under --fix and show KiB below 1 MiB. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent aab4d54 commit 951d56b

4 files changed

Lines changed: 34 additions & 11 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import * as CliService from "./service.ts";
1212
const formatBytes = (bytes: number) =>
1313
bytes >= 1024 ** 3
1414
? `${(bytes / 1024 ** 3).toFixed(2)} GiB`
15-
: `${(bytes / 1024 ** 2).toFixed(1)} MiB`;
15+
: bytes >= 1024 ** 2
16+
? `${(bytes / 1024 ** 2).toFixed(1)} MiB`
17+
: `${Math.ceil(bytes / 1024)} KiB`;
1618

1719
export const runDoctor = Effect.fn("runDoctor")(function* (
1820
databasePath: string,
@@ -69,7 +71,11 @@ export const runDoctor = Effect.fn("runDoctor")(function* (
6971
}),
7072
).pipe(Effect.catchTags({ QuitError: () => Effect.succeed([]) }));
7173
if (selected.length === 0) {
72-
yield* Console.log("No repairs selected; nothing changed.");
74+
yield* Console.log(
75+
options.fix
76+
? "No recommended repairs; nothing changed."
77+
: "No repairs selected; nothing changed.",
78+
);
7379
return;
7480
}
7581
const service = yield* BootService.BootService;

‎apps/server/src/cloud/bootService.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,8 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
960960
// `is-active` fails while a crash-looping unit sits in `activating
961961
// (auto-restart)`, and a throttled launchd job is loaded without a pid;
962962
// both would start the server mid-repair. So the unit is always
963-
// stopped, and started again when it was enabled or loaded before.
963+
// stopped, and started again when it was active, enabled or loaded
964+
// before; a disabled unit caught crash-looping stays stopped.
964965
const restore =
965966
manager.kind === "systemd"
966967
? yield* Effect.map(

‎apps/server/src/persistence/DatabaseMaintenance.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
1616
it.layer(DatabaseMaintenance.layer.pipe(Layer.provideMerge(NodeServices.layer)))(
1717
"DatabaseMaintenance",
1818
(it) => {
19+
it.effect("reports an unreadable database as an inspection failure", () =>
20+
Effect.gen(function* () {
21+
const fs = yield* FileSystem.FileSystem;
22+
const path = yield* Path.Path;
23+
const maintenance = yield* DatabaseMaintenance.DatabaseMaintenance;
24+
const dbPath = path.join(
25+
yield* fs.makeTempDirectoryScoped({ prefix: "t3-inspect-" }),
26+
"statev2.sqlite",
27+
);
28+
yield* fs.writeFileString(dbPath, "not a database");
29+
const failure = yield* Effect.flip(maintenance.inspect(dbPath));
30+
assert.equal(failure.reason, "inspect");
31+
}).pipe(Effect.scoped),
32+
);
33+
1934
it.effect("compacts an existing database offline only once its server has stopped", () =>
2035
Effect.gen(function* () {
2136
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/persistence/DatabaseMaintenance.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
1414
export class DatabaseMaintenanceError extends Schema.TaggedError<DatabaseMaintenanceError>()(
1515
"DatabaseMaintenanceError",
1616
{
17-
reason: Schema.Literals(["running", "missing", "invalid-projections", "compact"]),
17+
reason: Schema.Literals(["running", "missing", "invalid-projections", "inspect", "compact"]),
1818
cause: Schema.optional(Schema.Defect()),
1919
},
2020
) {
@@ -26,6 +26,8 @@ export class DatabaseMaintenanceError extends Schema.TaggedError<DatabaseMainten
2626
return "No existing T3 Code database was found in this data directory.";
2727
case "invalid-projections":
2828
return "Database projections need repair before compaction. Start T3 Code once so it rebuilds them, then run doctor again. If this CLI is older than the one that wrote the database, update T3 Code first.";
29+
case "inspect":
30+
return "Could not read the database. Check that this user can open it.";
2931
case "compact":
3032
return "Database compaction failed. Check that no other process has the database open and enough disk space is available.";
3133
}
@@ -82,18 +84,17 @@ const make = Effect.gen(function* () {
8284
}
8385
});
8486

85-
const asMaintenanceError = Effect.mapError((cause: unknown) =>
86-
isDatabaseMaintenanceError(cause)
87-
? cause
88-
: new DatabaseMaintenanceError({ reason: "compact", cause }),
89-
);
87+
const asMaintenanceError = (reason: "inspect" | "compact") =>
88+
Effect.mapError((cause: unknown) =>
89+
isDatabaseMaintenanceError(cause) ? cause : new DatabaseMaintenanceError({ reason, cause }),
90+
);
9091

9192
const inspect = Effect.fn("DatabaseMaintenance.inspect")(function* (databasePath: string) {
9293
yield* locate(databasePath);
9394
return yield* readStorage.pipe(
9495
Effect.provide(NodeSqliteClient.layer({ filename: databasePath, readonly: true })),
9596
);
96-
}, asMaintenanceError);
97+
}, asMaintenanceError("inspect"));
9798

9899
const compact = Effect.fn("DatabaseMaintenance.compact")(function* (databasePath: string) {
99100
yield* locate(databasePath);
@@ -142,7 +143,7 @@ const make = Effect.gen(function* () {
142143
deletedEventCount: result.deletedEventCount,
143144
deletedReceiptCount: result.deletedReceiptCount,
144145
};
145-
}, asMaintenanceError);
146+
}, asMaintenanceError("compact"));
146147

147148
return DatabaseMaintenance.of({ inspect, compact });
148149
});

0 commit comments

Comments
 (0)