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, 04:45 UTC
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { assert, it } from "@effect/vitest"; |
| 3 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 4 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 5 | import * as Clock from "effect/Clock"; |
| 6 | import * as Duration from "effect/Duration"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FileSystem from "effect/FileSystem"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Path from "effect/Path"; |
| 11 | import * as SqlClient from "effect/sql/SqlClient"; |
| 12 | |
| 13 | import * as DatabaseMaintenance from "./DatabaseMaintenance.ts"; |
| 14 | import * as Sqlite from "./Sqlite.ts"; |
| 15 | |
| 16 | it.layer(DatabaseMaintenance.layer.pipe(Layer.provideMerge(NodeServices.layer)))( |
| 17 | "DatabaseMaintenance", |
| 18 | (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._tag, "DatabaseInspectError"); |
| 31 | }), |
| 32 | ); |
| 33 | |
| 34 | it.effect("reports and deletes only the database from before 0.1.0", () => |
| 35 | Effect.gen(function* () { |
| 36 | const fs = yield* FileSystem.FileSystem; |
| 37 | const path = yield* Path.Path; |
| 38 | const maintenance = yield* DatabaseMaintenance.DatabaseMaintenance; |
| 39 | const userdata = yield* fs.makeTempDirectoryScoped({ prefix: "t3-legacy-" }); |
| 40 | const dbPath = path.join(userdata, "statev2.sqlite"); |
| 41 | const legacy = path.join(userdata, "state.sqlite"); |
| 42 | yield* Effect.gen(function* () { |
| 43 | const sql = yield* SqlClient.SqlClient; |
| 44 | yield* sql`CREATE TABLE projection_projects (project_id TEXT PRIMARY KEY)`; |
| 45 | yield* sql`CREATE TABLE orchestration_v2_projection_threads (thread_id TEXT PRIMARY KEY)`; |
| 46 | yield* sql`CREATE TABLE retained_data (value TEXT)`; |
| 47 | yield* sql`INSERT INTO retained_data VALUES ('keep')`; |
| 48 | }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath }))); |
| 49 | assert.isNull((yield* maintenance.inspect(dbPath)).legacyDatabaseBytes); |
| 50 | |
| 51 | const openLegacy = NodeSqliteClient.layer({ filename: legacy }); |
| 52 | yield* Effect.gen(function* () { |
| 53 | const sql = yield* SqlClient.SqlClient; |
| 54 | yield* sql`PRAGMA journal_mode = WAL`; |
| 55 | yield* sql`CREATE TABLE v1_data (value TEXT)`; |
| 56 | yield* sql`INSERT INTO v1_data VALUES ('rollback')`; |
| 57 | yield* sql`CREATE TABLE projection_projects (project_id TEXT PRIMARY KEY, deleted_at TEXT)`; |
| 58 | yield* sql`CREATE TABLE projection_threads (thread_id TEXT PRIMARY KEY, deleted_at TEXT)`; |
| 59 | yield* sql`INSERT INTO projection_projects VALUES ('legacy-project', NULL)`; |
| 60 | yield* sql`INSERT INTO projection_threads VALUES ('legacy-thread', NULL), ('deleted-thread', '2026-10-01')`; |
| 61 | }).pipe(Effect.provide(openLegacy)); |
| 62 | assert.isAbove((yield* maintenance.inspect(dbPath)).legacyDatabaseBytes ?? 0, 0); |
| 63 | |
| 64 | assert.deepEqual((yield* maintenance.inspect(dbPath)).legacyOnlyRecords, { |
| 65 | projects: 1, |
| 66 | threads: 1, |
| 67 | }); |
| 68 | const missing = yield* Effect.flip(maintenance.removeLegacyDatabase(dbPath)); |
| 69 | assert.equal(missing._tag, "LegacyDatabaseHasUnmigratedRecordsError"); |
| 70 | assert.isTrue(yield* fs.exists(legacy)); |
| 71 | |
| 72 | yield* Effect.gen(function* () { |
| 73 | const sql = yield* SqlClient.SqlClient; |
| 74 | yield* sql`INSERT INTO projection_projects VALUES ('legacy-project')`; |
| 75 | yield* sql`INSERT INTO orchestration_v2_projection_threads VALUES ('legacy-thread')`; |
| 76 | }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath }))); |
| 77 | assert.deepEqual((yield* maintenance.inspect(dbPath)).legacyOnlyRecords, { |
| 78 | projects: 0, |
| 79 | threads: 0, |
| 80 | }); |
| 81 | |
| 82 | const refused = yield* Effect.gen(function* () { |
| 83 | const sql = yield* SqlClient.SqlClient; |
| 84 | yield* sql`SELECT value FROM v1_data`; |
| 85 | const failure = yield* Effect.flip(maintenance.removeLegacyDatabase(dbPath)); |
| 86 | assert.deepEqual(yield* sql`SELECT value FROM v1_data`, [{ value: "rollback" }]); |
| 87 | return failure; |
| 88 | }).pipe(Effect.provide(openLegacy)); |
| 89 | assert.equal(refused._tag, "DatabaseInUseError"); |
| 90 | assert.isTrue(yield* fs.exists(legacy)); |
| 91 | |
| 92 | yield* Effect.flatMap(DatabaseMaintenance.DatabaseMaintenance, (service) => |
| 93 | service.removeLegacyDatabase(dbPath), |
| 94 | ).pipe( |
| 95 | Effect.provide(Layer.fresh(DatabaseMaintenance.layer)), |
| 96 | Effect.provideService(HostProcess.Platform, "win32"), |
| 97 | Effect.provideService(FileSystem.FileSystem, { |
| 98 | ...fs, |
| 99 | remove: (target, options) => |
| 100 | Effect.gen(function* () { |
| 101 | if (target === legacy) { |
| 102 | yield* Effect.gen(function* () { |
| 103 | const sql = yield* SqlClient.SqlClient; |
| 104 | yield* sql`PRAGMA busy_timeout = 0`; |
| 105 | yield* sql`BEGIN EXCLUSIVE`; |
| 106 | yield* sql`COMMIT`; |
| 107 | }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: legacy }))); |
| 108 | // Create sidecars after the probe closes so only Doctor can remove them. |
| 109 | yield* fs.writeFileString(`${legacy}-wal`, "leftover WAL"); |
| 110 | yield* fs.writeFileString(`${legacy}-shm`, "leftover SHM"); |
| 111 | } |
| 112 | yield* fs.remove(target, options); |
| 113 | }), |
| 114 | }), |
| 115 | ); |
| 116 | assert.isFalse(yield* fs.exists(legacy)); |
| 117 | assert.isFalse(yield* fs.exists(`${legacy}-wal`)); |
| 118 | assert.isFalse(yield* fs.exists(`${legacy}-shm`)); |
| 119 | assert.isTrue(yield* fs.exists(dbPath)); |
| 120 | yield* Effect.gen(function* () { |
| 121 | const sql = yield* SqlClient.SqlClient; |
| 122 | assert.deepEqual(yield* sql`SELECT value FROM retained_data`, [{ value: "keep" }]); |
| 123 | }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath, readonly: true }))); |
| 124 | assert.isNull((yield* maintenance.inspect(dbPath)).legacyDatabaseBytes); |
| 125 | }), |
| 126 | ); |
| 127 | |
| 128 | it.effect("runs a repair requested from Settings once and keeps its failure for Settings", () => |
| 129 | Effect.gen(function* () { |
| 130 | const fs = yield* FileSystem.FileSystem; |
| 131 | const path = yield* Path.Path; |
| 132 | const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-requested-reclaim-" }); |
| 133 | const dbPath = path.join(baseDir, "userdata", "statev2.sqlite"); |
| 134 | const files = DatabaseMaintenance.reclaimFiles(path, dbPath); |
| 135 | yield* Effect.void.pipe(Effect.provide(Sqlite.layerFromPath(dbPath))); |
| 136 | const autoVacuum = Effect.flatMap( |
| 137 | SqlClient.SqlClient, |
| 138 | (sql) => sql<{ readonly auto_vacuum: number }>`PRAGMA auto_vacuum`, |
| 139 | ).pipe( |
| 140 | Effect.map((rows) => rows[0]?.auto_vacuum), |
| 141 | Effect.provide(NodeSqliteClient.layer({ filename: dbPath, readonly: true })), |
| 142 | ); |
| 143 | yield* Effect.gen(function* () { |
| 144 | const sql = yield* SqlClient.SqlClient; |
| 145 | yield* sql`PRAGMA auto_vacuum = NONE`; |
| 146 | yield* sql`VACUUM`; |
| 147 | yield* sql`CREATE TABLE retained_data (value TEXT)`; |
| 148 | yield* sql`INSERT INTO retained_data VALUES ('keep')`; |
| 149 | }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath }))); |
| 150 | |
| 151 | // Every server exit runs this; without a request it must leave the database alone. |
| 152 | yield* DatabaseMaintenance.runRequestedReclaim(dbPath); |
| 153 | assert.equal(yield* autoVacuum, 0); |
| 154 | |
| 155 | yield* fs.writeFileString(files.request, ""); |
| 156 | yield* fs.writeFileString(files.failure, "an earlier failure"); |
| 157 | yield* DatabaseMaintenance.runRequestedReclaim(dbPath); |
| 158 | assert.isFalse(yield* fs.exists(files.request)); |
| 159 | assert.isFalse(yield* fs.exists(files.failure)); |
| 160 | assert.equal(yield* autoVacuum, 2); |
| 161 | yield* Effect.gen(function* () { |
| 162 | const sql = yield* SqlClient.SqlClient; |
| 163 | assert.deepEqual(yield* sql`SELECT value FROM retained_data`, [{ value: "keep" }]); |
| 164 | }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath, readonly: true }))); |
| 165 | |
| 166 | const brokenPath = path.join(baseDir, "broken", "statev2.sqlite"); |
| 167 | const broken = DatabaseMaintenance.reclaimFiles(path, brokenPath); |
| 168 | yield* fs.makeDirectory(path.dirname(brokenPath)); |
| 169 | yield* fs.writeFileString(brokenPath, "not a database"); |
| 170 | yield* fs.writeFileString(broken.request, ""); |
| 171 | yield* DatabaseMaintenance.runRequestedReclaim(brokenPath); |
| 172 | assert.isFalse( |
| 173 | yield* fs.exists(broken.request), |
| 174 | "a failed repair must not rerun on every exit", |
| 175 | ); |
| 176 | assert.equal( |
| 177 | yield* fs.readFileString(broken.failure), |
| 178 | new DatabaseMaintenance.DatabaseCompactError({ path: brokenPath, cause: null }).message, |
| 179 | ); |
| 180 | }), |
| 181 | ); |
| 182 | |
| 183 | it.effect("refuses a repair whose copies would not fit on disk", () => |
| 184 | Effect.gen(function* () { |
| 185 | const fs = yield* FileSystem.FileSystem; |
| 186 | const path = yield* Path.Path; |
| 187 | const dbPath = path.join( |
| 188 | yield* fs.makeTempDirectoryScoped({ prefix: "t3-reclaim-space-" }), |
| 189 | "statev2.sqlite", |
| 190 | ); |
| 191 | assert.isNull(yield* DatabaseMaintenance.reclaimSpaceShortfall(dbPath, 1024)); |
| 192 | const huge = 2 ** 60; |
| 193 | const shortfall = yield* DatabaseMaintenance.reclaimSpaceShortfall(dbPath, huge); |
| 194 | assert.isAtLeast(shortfall?.neededBytes ?? 0, huge); |
| 195 | assert.isBelow(shortfall?.freeBytes ?? Infinity, huge); |
| 196 | }), |
| 197 | ); |
| 198 | |
| 199 | it.effect("compacts an existing database offline only once its server has stopped", () => |
| 200 | Effect.gen(function* () { |
| 201 | const fs = yield* FileSystem.FileSystem; |
| 202 | const path = yield* Path.Path; |
| 203 | const maintenance = yield* DatabaseMaintenance.DatabaseMaintenance; |
| 204 | const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-compact-" }); |
| 205 | const dbPath = path.join(baseDir, "userdata", "statev2.sqlite"); |
| 206 | // Opening the database migrates it. |
| 207 | yield* Effect.void.pipe(Effect.provide(Sqlite.layerFromPath(dbPath))); |
| 208 | yield* Effect.gen(function* () { |
| 209 | const sql = yield* SqlClient.SqlClient; |
| 210 | yield* sql`PRAGMA auto_vacuum = NONE`; |
| 211 | yield* sql`VACUUM`; |
| 212 | yield* sql`CREATE TABLE retained_data (value TEXT)`; |
| 213 | yield* sql`INSERT INTO retained_data VALUES ('keep')`; |
| 214 | yield* sql`CREATE TABLE discarded_data (value BLOB)`; |
| 215 | yield* sql`INSERT INTO discarded_data VALUES (zeroblob(4194304))`; |
| 216 | yield* sql`DROP TABLE discarded_data`; |
| 217 | yield* sql` |
| 218 | INSERT INTO orchestration_v2_projection_messages ( |
| 219 | message_id, thread_id, run_id, node_id, role, streaming, created_at, updated_at, payload_json |
| 220 | ) |
| 221 | VALUES ( |
| 222 | 'message-1', 'thread-1', NULL, NULL, 'user', 0, |
| 223 | '2026-09-01T00:00:00.000Z', '2026-09-01T00:00:00.000Z', '{"text":"needle"}' |
| 224 | ) |
| 225 | `; |
| 226 | // An index that drifted from its rows, as a renumbering VACUUM would leave it. |
| 227 | yield* sql` |
| 228 | INSERT INTO orchestration_v2_message_search (orchestration_v2_message_search) |
| 229 | VALUES ('delete-all') |
| 230 | `; |
| 231 | yield* sql`PRAGMA wal_checkpoint(TRUNCATE)`; |
| 232 | yield* sql`INSERT INTO orchestration_v2_legacy_imports ( |
| 233 | thread_id, source_updated_at, shell_imported_at, transcript_imported_at, imported_message_count |
| 234 | ) VALUES ('imported', '2026-10-17', '2026-10-17', '2026-10-17', 0)`; |
| 235 | yield* sql`WITH RECURSIVE history(n) AS ( |
| 236 | SELECT 1 UNION ALL SELECT n + 1 FROM history WHERE n < 51 |
| 237 | ) INSERT INTO orchestration_command_receipts ( |
| 238 | command_id, aggregate_kind, aggregate_id, accepted_at, result_sequence, status, command_type |
| 239 | ) SELECT 'legacy-' || n, 'thread', 'imported', '2026-10-17', 0, 'accepted', 'legacy' FROM history`; |
| 240 | }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath }))); |
| 241 | |
| 242 | const sizeBefore = (yield* fs.stat(dbPath)).size; |
| 243 | const refused = yield* Effect.gen(function* () { |
| 244 | const sql = yield* SqlClient.SqlClient; |
| 245 | yield* sql`SELECT value FROM retained_data`; |
| 246 | return yield* maintenance.compact(dbPath).pipe(Effect.flip); |
| 247 | }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath }))); |
| 248 | assert.equal(refused._tag, "DatabaseInUseError"); |
| 249 | assert.equal((yield* fs.stat(dbPath)).size, sizeBefore); |
| 250 | |
| 251 | const clock = yield* Clock.Clock; |
| 252 | const pauses: number[] = []; |
| 253 | let activeNanos = 0n; |
| 254 | const steps: string[] = []; |
| 255 | const result = yield* maintenance |
| 256 | .compact(dbPath, (step) => Effect.sync(() => steps.push(step))) |
| 257 | .pipe( |
| 258 | Effect.provideService(Clock.Clock, { |
| 259 | ...clock, |
| 260 | // Exceed the background budget whenever the compactor checks elapsed time. |
| 261 | monotonicTimeNanos: Effect.sync(() => (activeNanos += 30_000_000n)), |
| 262 | sleep: (duration) => |
| 263 | Effect.sync(() => { |
| 264 | pauses.push(Duration.toMillis(duration)); |
| 265 | }), |
| 266 | }), |
| 267 | ); |
| 268 | assert.deepEqual(pauses, [], "offline doctor must not throttle compaction"); |
| 269 | assert.deepEqual(steps, ["prune-events", "vacuum", "rebuild-search", "checkpoint"]); |
| 270 | assert.equal(result.deletedReceiptCount, 51); |
| 271 | assert.isBelow(result.afterBytes, result.beforeBytes - 3 * 1024 * 1024); |
| 272 | yield* Effect.gen(function* () { |
| 273 | const sql = yield* SqlClient.SqlClient; |
| 274 | assert.deepEqual(yield* sql`PRAGMA auto_vacuum`, [{ auto_vacuum: 2 }]); |
| 275 | assert.deepEqual(yield* sql`SELECT value FROM retained_data`, [{ value: "keep" }]); |
| 276 | assert.deepEqual( |
| 277 | yield* sql` |
| 278 | SELECT messages.message_id AS "messageId" |
| 279 | FROM orchestration_v2_message_search AS search |
| 280 | INNER JOIN orchestration_v2_projection_messages AS messages |
| 281 | ON messages.rowid = search.rowid |
| 282 | WHERE orchestration_v2_message_search MATCH '"needle"' |
| 283 | `, |
| 284 | [{ messageId: "message-1" }], |
| 285 | ); |
| 286 | }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath, readonly: true }))); |
| 287 | }), |
| 288 | ); |
| 289 | }, |
| 290 | ); |