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

main
ts 421 lines 16.7 KB
Raw
1 import * as HostProcess from "@t3tools/shared/HostProcess";
2 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 import * as Context from "effect/Context";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Layer from "effect/Layer";
7 import * as Option from "effect/Option";
8 import * as Path from "effect/Path";
9 import * as Schedule from "effect/Schedule";
10 import * as Schema from "effect/Schema";
11 // @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect has no free-space query.
12 import * as NodeFSP from "node:fs/promises";
13 import * as NodeOS from "node:os";
14 import * as NodeURL from "node:url";
15 import * as SqlClient from "effect/sql/SqlClient";
16 import { isSqlError } from "effect/sql/SqlError";
17
18 import * as ProjectionMaintenance from "../orchestration-v2/ProjectionMaintenance.ts";
19 import { rebuildMessageSearch } from "../orchestration-v2/messageSearchIndex.ts";
20
21 export class DatabaseMissingError extends Schema.TaggedError<DatabaseMissingError>()(
22 "DatabaseMissingError",
23 { path: Schema.String },
24 ) {
25 override get message() {
26 return "No existing T3 Code database was found in this data directory.";
27 }
28 }
29
30 export class DatabaseInUseError extends Schema.TaggedError<DatabaseInUseError>()(
31 "DatabaseInUseError",
32 {
33 path: Schema.String,
34 cause: Schema.Defect(),
35 pid: Schema.optional(Schema.Int),
36 serviceManaged: Schema.optional(Schema.Boolean),
37 },
38 ) {
39 override get message() {
40 if (this.pid !== undefined) {
41 return `Stop T3 Code server process ${this.pid}${this.serviceManaged ? "" : ", which the background service did not start,"} before repairing the database.`;
42 }
43 return "Stop the T3 Code server and desktop app, including any version before 0.1.0, before repairing the database.";
44 }
45 }
46
47 export class DatabaseInspectError extends Schema.TaggedError<DatabaseInspectError>()(
48 "DatabaseInspectError",
49 { path: Schema.String, cause: Schema.Defect() },
50 ) {
51 override get message() {
52 return "Could not read the database. Check that this user can open it.";
53 }
54 }
55
56 export class DatabaseCompactError extends Schema.TaggedError<DatabaseCompactError>()(
57 "DatabaseCompactError",
58 { path: Schema.String, cause: Schema.Defect() },
59 ) {
60 override get message() {
61 return "Database compaction failed. Check that no other process has the database open and enough disk space is available.";
62 }
63 }
64
65 export class LegacyDatabaseRemoveError extends Schema.TaggedError<LegacyDatabaseRemoveError>()(
66 "LegacyDatabaseRemoveError",
67 { path: Schema.String, cause: Schema.Defect() },
68 ) {
69 override get message() {
70 return "Could not delete the database from before 0.1.0. Check that both databases are readable and the data directory is writable.";
71 }
72 }
73
74 export class LegacyDatabaseHasUnmigratedRecordsError extends Schema.TaggedError<LegacyDatabaseHasUnmigratedRecordsError>()(
75 "LegacyDatabaseHasUnmigratedRecordsError",
76 { projects: Schema.Int, threads: Schema.Int },
77 ) {
78 override get message() {
79 return `The current database is missing legacy records. Projects missing: ${this.projects}. Threads missing: ${this.threads}. Keep the old database for recovery.`;
80 }
81 }
82
83 export class DiskSpaceCheckError extends Schema.TaggedError<DiskSpaceCheckError>()(
84 "DiskSpaceCheckError",
85 { path: Schema.String, cause: Schema.Defect() },
86 ) {
87 override get message() {
88 return "Could not check free disk space.";
89 }
90 }
91
92 type LegacyOnlyRecords = { readonly projects: number; readonly threads: number };
93
94 export type DatabaseCompactStep = "prune-events" | "vacuum" | "rebuild-search" | "checkpoint";
95
96 export class DatabaseMaintenance extends Context.Service<
97 DatabaseMaintenance,
98 {
99 readonly inspect: (databasePath: string) => Effect.Effect<
100 {
101 readonly databaseBytes: number;
102 readonly reclaimableBytes: number;
103 readonly incrementalVacuumEnabled: boolean;
104 /** Size of the database from before 0.1.0 still kept beside it, or null. */
105 readonly legacyDatabaseBytes: number | null;
106 readonly legacyOnlyRecords: LegacyOnlyRecords | null;
107 },
108 DatabaseMissingError | DatabaseInspectError
109 >;
110 /**
111 * Compacts history, then VACUUMs into incremental auto_vacuum. Refuses while a server has it open.
112 * Calls `onStep` as each step starts; VACUUM time grows with the whole database size.
113 */
114 readonly compact: (
115 databasePath: string,
116 onStep?: (step: DatabaseCompactStep) => Effect.Effect<void>,
117 ) => Effect.Effect<
118 {
119 readonly beforeBytes: number;
120 readonly afterBytes: number;
121 readonly deletedEventCount: number;
122 readonly deletedReceiptCount: number;
123 },
124 DatabaseMissingError | DatabaseInUseError | DatabaseCompactError
125 >;
126 /** Deletes only the pre-0.1.0 database; earlier versions still use it. */
127 readonly removeLegacyDatabase: (
128 databasePath: string,
129 ) => Effect.Effect<
130 void,
131 DatabaseInUseError | LegacyDatabaseRemoveError | LegacyDatabaseHasUnmigratedRecordsError
132 >;
133 }
134 >()("t3/persistence/DatabaseMaintenance") {}
135
136 const isDomainError = Schema.is(Schema.Union([DatabaseMissingError, DatabaseInUseError]));
137
138 const readStorage = Effect.gen(function* () {
139 const sql = yield* SqlClient.SqlClient;
140 const pages = yield* sql<{ readonly page_count: number }>`PRAGMA page_count`;
141 const free = yield* sql<{ readonly freelist_count: number }>`PRAGMA freelist_count`;
142 const size = yield* sql<{ readonly page_size: number }>`PRAGMA page_size`;
143 const mode = yield* sql<{ readonly auto_vacuum: number }>`PRAGMA auto_vacuum`;
144 const pageSize = size[0]?.page_size ?? 0;
145 return {
146 databaseBytes: (pages[0]?.page_count ?? 0) * pageSize,
147 reclaimableBytes: (free[0]?.freelist_count ?? 0) * pageSize,
148 incrementalVacuumEnabled: mode[0]?.auto_vacuum === 2,
149 };
150 });
151
152 /**
153 * Holds an exclusive lock on the connection's database. In WAL mode this is
154 * refused while any other connection has the file open, even an idle one,
155 * which is how a running server or desktop app is detected.
156 */
157 const lockExclusively = Effect.fn("DatabaseMaintenance.lockExclusively")(function* (
158 databasePath: string,
159 ) {
160 const sql = yield* SqlClient.SqlClient;
161 yield* sql`PRAGMA busy_timeout = 0`;
162 yield* sql`PRAGMA main.locking_mode = EXCLUSIVE`;
163 yield* sql`BEGIN EXCLUSIVE`.pipe(
164 Effect.mapError((cause) =>
165 isSqlError(cause) && cause.reason._tag === "LockTimeoutError"
166 ? new DatabaseInUseError({ path: databasePath, cause })
167 : cause,
168 ),
169 );
170 yield* sql`COMMIT`;
171 });
172
173 const make = Effect.gen(function* () {
174 const fs = yield* FileSystem.FileSystem;
175 const path = yield* Path.Path;
176 const legacyPath = (databasePath: string) =>
177 path.join(path.dirname(databasePath), "state.sqlite");
178
179 const locate = Effect.fn("DatabaseMaintenance.locate")(function* (databasePath: string) {
180 if (!(yield* fs.exists(databasePath))) {
181 return yield* new DatabaseMissingError({ path: databasePath });
182 }
183 });
184
185 const readLegacyOnlyRecords = Effect.fn("DatabaseMaintenance.readLegacyOnlyRecords")(function* (
186 databasePath: string,
187 ) {
188 const sql = yield* SqlClient.SqlClient;
189 const currentUri = `${NodeURL.pathToFileURL(databasePath).href}?mode=ro`;
190 yield* sql`ATTACH DATABASE ${currentUri} AS current_v2`;
191 const [counts] = yield* sql<LegacyOnlyRecords>`
192 SELECT
193 (SELECT COUNT(*) FROM main.projection_projects legacy
194 WHERE legacy.deleted_at IS NULL AND NOT EXISTS (
195 SELECT 1 FROM current_v2.projection_projects current
196 WHERE current.project_id = legacy.project_id
197 )) AS projects,
198 (SELECT COUNT(*) FROM main.projection_threads legacy
199 WHERE legacy.deleted_at IS NULL AND NOT EXISTS (
200 SELECT 1 FROM current_v2.orchestration_v2_projection_threads current
201 WHERE current.thread_id = legacy.thread_id
202 )) AS threads
203 `;
204 if (!counts) return yield* Effect.die(new Error("Missing result from legacy database counts"));
205 return counts;
206 });
207
208 const inspect = Effect.fn("DatabaseMaintenance.inspect")(
209 function* (databasePath: string) {
210 yield* locate(databasePath);
211 const storage = yield* readStorage.pipe(
212 Effect.provide(NodeSqliteClient.layer({ filename: databasePath, readonly: true })),
213 );
214 const legacy = legacyPath(databasePath);
215 const legacyDatabaseBytes = (yield* fs.exists(legacy))
216 ? Number((yield* fs.stat(legacy)).size)
217 : null;
218 const legacyOnlyRecords =
219 legacyDatabaseBytes === null
220 ? null
221 : yield* readLegacyOnlyRecords(databasePath).pipe(
222 Effect.provide(NodeSqliteClient.layer({ filename: legacy, readonly: true })),
223 Effect.orElseSucceed(() => null),
224 );
225 return { ...storage, legacyDatabaseBytes, legacyOnlyRecords };
226 },
227 (effect, databasePath) =>
228 effect.pipe(
229 Effect.mapError((cause) =>
230 isDomainError(cause) ? cause : new DatabaseInspectError({ path: databasePath, cause }),
231 ),
232 ),
233 );
234
235 const compact = Effect.fn("DatabaseMaintenance.compact")(
236 function* (
237 databasePath: string,
238 onStep: (step: DatabaseCompactStep) => Effect.Effect<void> = () => Effect.void,
239 ) {
240 yield* locate(databasePath);
241 const beforeBytes = Number((yield* fs.stat(databasePath)).size);
242 const result = yield* Effect.gen(function* () {
243 const sql = yield* SqlClient.SqlClient;
244 yield* lockExclusively(databasePath);
245 const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
246 yield* onStep("prune-events");
247 const summary = yield* maintenance.pruneEventStore();
248 yield* onStep("vacuum");
249 // VACUUM is required to enable incremental auto_vacuum on existing databases.
250 yield* sql`PRAGMA auto_vacuum = INCREMENTAL`;
251 yield* sql`VACUUM`;
252 yield* onStep("rebuild-search");
253 yield* rebuildMessageSearch(sql);
254 yield* onStep("checkpoint");
255 yield* sql`PRAGMA wal_checkpoint(TRUNCATE)`;
256 return summary;
257 }).pipe(
258 Effect.provide(
259 ProjectionMaintenance.layer.pipe(
260 Layer.provideMerge(NodeSqliteClient.layer({ filename: databasePath })),
261 ),
262 ),
263 );
264 const afterBytes = Number((yield* fs.stat(databasePath)).size);
265 return {
266 beforeBytes,
267 afterBytes,
268 deletedEventCount: result.deletedEventCount,
269 deletedReceiptCount: result.deletedReceiptCount,
270 };
271 },
272 (effect, databasePath) =>
273 effect.pipe(
274 Effect.mapError((cause) =>
275 isDomainError(cause) ? cause : new DatabaseCompactError({ path: databasePath, cause }),
276 ),
277 ),
278 );
279
280 const removeLegacyDatabase = Effect.fn("DatabaseMaintenance.removeLegacyDatabase")(
281 function* (databasePath: string) {
282 const legacy = legacyPath(databasePath);
283 if (!(yield* fs.exists(legacy))) return;
284 // Delete the database before WAL to preserve commits if unlink fails.
285 const removeFiles = Effect.forEach(
286 ["", "-wal", "-shm"],
287 (suffix) => fs.remove(legacy + suffix, { force: true }),
288 { discard: true },
289 );
290 const connection = NodeSqliteClient.layer({ filename: legacy });
291 const check = lockExclusively(legacy).pipe(
292 Effect.andThen(readLegacyOnlyRecords(databasePath)),
293 Effect.flatMap((counts) =>
294 counts.projects > 0 || counts.threads > 0
295 ? new LegacyDatabaseHasUnmigratedRecordsError(counts)
296 : Effect.void,
297 ),
298 );
299 if ((yield* HostProcess.Platform) === "win32") {
300 // Windows blocks unlink while any SQLite handle is open, including our own.
301 yield* check.pipe(Effect.provide(connection));
302 yield* removeFiles;
303 } else {
304 yield* check.pipe(Effect.andThen(removeFiles), Effect.provide(connection));
305 }
306 },
307 (effect, databasePath) =>
308 effect.pipe(
309 Effect.mapError((cause) =>
310 cause._tag === "DatabaseInUseError" ||
311 cause._tag === "LegacyDatabaseHasUnmigratedRecordsError"
312 ? cause
313 : new LegacyDatabaseRemoveError({ path: legacyPath(databasePath), cause }),
314 ),
315 ),
316 );
317
318 return DatabaseMaintenance.of({ inspect, compact, removeLegacyDatabase });
319 });
320
321 export const layer = Layer.effect(DatabaseMaintenance, make);
322
323 /** Settings' Reclaim writes the request; the failure stays for Settings until the next request. */
324 export const reclaimFiles = (path: Path.Path, databasePath: string) => ({
325 request: path.join(path.dirname(databasePath), "reclaim-requested"),
326 failure: path.join(path.dirname(databasePath), "reclaim-failed"),
327 });
328
329 /** Runs a repair that Settings requested. The server calls this on exit, once its database is closed. */
330 export const runRequestedReclaim = Effect.fn("DatabaseMaintenance.runRequestedReclaim")(
331 function* (databasePath: string) {
332 const fs = yield* FileSystem.FileSystem;
333 const files = reclaimFiles(yield* Path.Path, databasePath);
334 if (!(yield* fs.exists(files.request))) return;
335 // One attempt per request, so a repair killed midway (it rolls back) cannot delay later exits.
336 yield* fs.remove(files.request, { force: true });
337 yield* Effect.logInfo("Reclaiming database space before the restart. This can take minutes.");
338 const maintenance = yield* DatabaseMaintenance;
339 yield* maintenance.compact(databasePath).pipe(
340 // Worker threads and agent processes can hold the file for a moment after the server stops.
341 Effect.retry({
342 times: 10,
343 while: (error) => error._tag === "DatabaseInUseError",
344 schedule: Schedule.spaced("1 second"),
345 }),
346 Effect.matchEffect({
347 onSuccess: (result) =>
348 fs
349 .remove(files.failure, { force: true })
350 .pipe(Effect.andThen(Effect.logInfo("Reclaimed database space", result))),
351 onFailure: (error) =>
352 fs
353 .writeFileString(
354 files.failure,
355 error._tag === "DatabaseInUseError"
356 ? "Another program kept the database open. Close other T3 Code apps and t3 commands on this machine, then try again."
357 : error.message,
358 )
359 .pipe(
360 Effect.andThen(Effect.logWarning("Database space reclamation failed", { error })),
361 ),
362 }),
363 );
364 },
365 Effect.catchCause((cause) => Effect.logWarning("Database space reclamation failed", { cause })),
366 );
367
368 // SQLite's search order for its temporary files (unixTempFileDir in os_unix.c).
369 const sqliteTempDirectory = Effect.gen(function* () {
370 if ((yield* HostProcess.Platform) === "win32") return NodeOS.tmpdir();
371 const fs = yield* FileSystem.FileSystem;
372 const env = yield* HostProcess.Environment;
373 for (const directory of [env.SQLITE_TMPDIR, env.TMPDIR, "/var/tmp", "/usr/tmp", "/tmp"]) {
374 if (directory === undefined || directory === "") continue;
375 const info = yield* fs.stat(directory).pipe(Effect.option);
376 if (Option.isNone(info) || info.value.type !== "Directory") continue;
377 // SQLite skips directories it cannot write to and search: osAccess(zDir, 03).
378 const usable = yield* Effect.tryPromise({
379 try: () => NodeFSP.access(directory, NodeFSP.constants.W_OK | NodeFSP.constants.X_OK),
380 catch: (cause) => new DiskSpaceCheckError({ path: directory, cause }),
381 }).pipe(
382 Effect.as(true),
383 Effect.orElseSucceed(() => false),
384 );
385 if (usable) return directory;
386 }
387 return ".";
388 });
389
390 const freeBytes = (directory: string) =>
391 Effect.tryPromise({
392 try: () => NodeFSP.statfs(directory),
393 catch: (cause) => new DiskSpaceCheckError({ path: directory, cause }),
394 }).pipe(Effect.map((stats) => stats.bavail * stats.bsize));
395
396 /**
397 * VACUUM copies the live pages to a temporary file, then writes them again to the WAL.
398 * Returns the free space the repair needs when either file system has less, else null.
399 */
400 export const reclaimSpaceShortfall = Effect.fn("DatabaseMaintenance.reclaimSpaceShortfall")(
401 function* (databasePath: string, liveBytes: number) {
402 const fs = yield* FileSystem.FileSystem;
403 const path = yield* Path.Path;
404 const databaseDirectory = path.dirname(databasePath);
405 const tempDirectory = yield* sqliteTempDirectory;
406 const copyBytes = Math.ceil(liveBytes * 1.1);
407 const [databaseFree, tempFree, databaseDevice, tempDevice] = yield* Effect.all([
408 freeBytes(databaseDirectory),
409 freeBytes(tempDirectory),
410 fs.stat(databaseDirectory).pipe(Effect.map((info) => info.dev)),
411 fs.stat(tempDirectory).pipe(Effect.map((info) => info.dev)),
412 ]);
413 if (databaseDevice === tempDevice) {
414 return databaseFree < 2 * copyBytes
415 ? { neededBytes: 2 * copyBytes, freeBytes: databaseFree }
416 : null;
417 }
418 if (databaseFree < copyBytes) return { neededBytes: copyBytes, freeBytes: databaseFree };
419 return tempFree < copyBytes ? { neededBytes: copyBytes, freeBytes: tempFree } : null;
420 },
421 );