Open History to see the last commit
1#!/usr/bin/env node
2
3import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import * as NodeOS from "node:os";
6import * as NodeURL from "node:url";
7import { resolveWorktreeT3Home } from "@t3tools/shared/devHome";
8import * as Console from "effect/Console";
9import * as Effect from "effect/Effect";
10import * as FileSystem from "effect/FileSystem";
11import * as Layer from "effect/Layer";
12import * as Option from "effect/Option";
13import * as Path from "effect/Path";
14import * as Schema from "effect/Schema";
15import * as SqlClient from "effect/sql/SqlClient";
16import { Command, Flag } from "effect/cli";
17
18import * as ProjectionStore from "../src/orchestration-v2/ProjectionStore.ts";
19import { rebuildMessageSearch } from "../src/orchestration-v2/messageSearchIndex.ts";
20import { runMigrations } from "../src/persistence/Migrations.ts";
21import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
22
23export class MigrateDevDbNotInWorktreeError extends Schema.TaggedError<MigrateDevDbNotInWorktreeError>()(
24 "MigrateDevDbNotInWorktreeError",
25 {},
26) {
27 override get message(): string {
28 return "Not inside a linked git worktree. Pass --base-dir to target an isolated .t3 directory.";
29 }
30}
31
32export class MigrateDevDbSharedHomeError extends Schema.TaggedError<MigrateDevDbSharedHomeError>()(
33 "MigrateDevDbSharedHomeError",
34 {},
35) {
36 override get message(): string {
37 return "Refusing to rebuild the shared ~/.t3 database. Use an isolated --base-dir.";
38 }
39}
40
41export class MigrateDevDbSourceMissingError extends Schema.TaggedError<MigrateDevDbSourceMissingError>()(
42 "MigrateDevDbSourceMissingError",
43 {
44 sourcePath: Schema.String,
45 },
46) {
47 override get message(): string {
48 return `Source database does not exist at '${this.sourcePath}'.`;
49 }
50}
51
52export class MigrateDevDbSourceIsDestinationError extends Schema.TaggedError<MigrateDevDbSourceIsDestinationError>()(
53 "MigrateDevDbSourceIsDestinationError",
54 {
55 sourcePath: Schema.String,
56 },
57) {
58 override get message(): string {
59 return `Source database '${this.sourcePath}' resolves to a path this command rewrites. Pick a different --source or --base-dir.`;
60 }
61}
62
63export class MigrateDevDbServerRunningError extends Schema.TaggedError<MigrateDevDbServerRunningError>()(
64 "MigrateDevDbServerRunningError",
65 {
66 databasePath: Schema.String,
67 pid: Schema.Number,
68 },
69) {
70 override get message(): string {
71 return `Dev database at '${this.databasePath}' is open by a running server (pid ${this.pid} per server-runtime.json). Stop that server first; if that pid is not actually a T3 server (stale descriptor, reused pid), delete the server-runtime.json next to the database and retry.`;
72 }
73}
74
75export class MigrateDevDbDestinationBusyError extends Schema.TaggedError<MigrateDevDbDestinationBusyError>()(
76 "MigrateDevDbDestinationBusyError",
77 {
78 databasePath: Schema.String,
79 reason: Schema.Literals(["write-locked", "wal-held"]),
80 cause: Schema.optional(Schema.Defect()),
81 },
82) {
83 override get message(): string {
84 const detail =
85 this.reason === "write-locked"
86 ? "the database is write-locked"
87 : "another connection is holding its WAL";
88 return `Dev database at '${this.databasePath}' looks in use (${detail}). Stop the dev server first; if none is running, delete the -wal/-shm files next to it.`;
89 }
90}
91
92export class MigrateDevDbPhaseError extends Schema.TaggedError<MigrateDevDbPhaseError>()(
93 "MigrateDevDbPhaseError",
94 {
95 phase: Schema.Literals(["snapshot", "prune", "compact", "migrate"]),
96 databasePath: Schema.String,
97 cause: Schema.Defect(),
98 },
99) {
100 override get message(): string {
101 return `migrate-dev-db failed during ${this.phase} on '${this.databasePath}'.`;
102 }
103}
104
105export interface RunMigrateDevDbInput {
106 readonly baseDir?: string | undefined;
107 readonly source?: string | undefined;
108 readonly projects: number;
109 readonly threadsPerProject: number;
110}
111
112export interface RunMigrateDevDbOptions {
113 /** Test override for the directory writes must never target. */
114 readonly sharedHome?: string | undefined;
115}
116
117interface KeptProject {
118 readonly title: string;
119 readonly threads: number;
120}
121
122const removeDatabaseFiles = Effect.fn("removeDatabaseFiles")(function* (databasePath: string) {
123 const fs = yield* FileSystem.FileSystem;
124 for (const suffix of ["", "-wal", "-shm"]) {
125 yield* fs.remove(`${databasePath}${suffix}`).pipe(Effect.orElseSucceed(() => undefined));
126 }
127});
128
129const ServerRuntimeState = Schema.fromJsonString(Schema.Struct({ pid: Schema.Number }));
130const decodeServerRuntimeState = Schema.decodeEffect(ServerRuntimeState);
131
132const isProcessAlive = (pid: number): boolean => {
133 try {
134 process.kill(pid, 0);
135 return true;
136 } catch (error) {
137 // EPERM means the process exists but belongs to someone else.
138 return (error as NodeJS.ErrnoException).code === "EPERM";
139 }
140};
141
142// The runtime PID catches idle servers; leftover -shm files can come from read-only connections.
143const ensureNotInUse = Effect.fn("ensureDevDbNotInUse")(function* (databasePath: string) {
144 const fs = yield* FileSystem.FileSystem;
145 const path = yield* Path.Path;
146
147 const runtimeStatePath = path.join(path.dirname(databasePath), "server-runtime.json");
148 const runtimeState = yield* fs
149 .readFileString(runtimeStatePath)
150 .pipe(Effect.flatMap(decodeServerRuntimeState), Effect.option);
151 if (Option.isSome(runtimeState) && isProcessAlive(runtimeState.value.pid)) {
152 return yield* new MigrateDevDbServerRunningError({
153 databasePath,
154 pid: runtimeState.value.pid,
155 });
156 }
157
158 if (!(yield* fs.exists(databasePath))) {
159 return;
160 }
161 const checkpoint = yield* Effect.gen(function* () {
162 const sql = yield* SqlClient.SqlClient;
163 yield* sql.unsafe("PRAGMA busy_timeout = 0").unprepared;
164 yield* sql.unsafe("BEGIN IMMEDIATE").unprepared;
165 yield* sql.unsafe("ROLLBACK").unprepared;
166 return yield* sql.unsafe<{ busy: number }>("PRAGMA wal_checkpoint(TRUNCATE)").unprepared;
167 }).pipe(
168 Effect.provide(NodeSqliteClient.layer({ filename: databasePath })),
169 Effect.mapError(
170 (cause) =>
171 new MigrateDevDbDestinationBusyError({
172 databasePath,
173 reason: "write-locked",
174 cause,
175 }),
176 ),
177 );
178 if (checkpoint[0] !== undefined && Number(checkpoint[0].busy) !== 0) {
179 return yield* new MigrateDevDbDestinationBusyError({
180 databasePath,
181 reason: "wal-held",
182 });
183 }
184});
185
186const RECOVERY_KINDS: ReadonlyArray<ProjectionStore.ProjectionRecoveryKind> = [
187 "queued-runs",
188 "runtime",
189 "subagent-results",
190 "delegated-completions",
191];
192
193const CLEARED_TABLES: ReadonlyArray<string> = [
194 "scheduled_tasks",
195 "orchestration_v2_effect_outbox",
196 "orchestration_v2_thread_launch_workflows",
197 "orchestration_command_receipts",
198 "provider_session_runtime",
199 "auth_sessions",
200 "auth_pairing_links",
201 "peer_environment_links",
202];
203
204const PROVIDER_SESSIONS_TABLE = "orchestration_v2_projection_provider_sessions";
205const FTS5_SHADOW_SUFFIXES = ["_data", "_idx", "_content", "_docsize", "_config"];
206
207// Settled threads stay: they are most of real history, and the UI shows them.
208const rootIsVisible = (sql: SqlClient.SqlClient) => sql`
209 root.deleted_at IS NULL
210 AND json_extract(root.payload_json, '$.deletedAt') IS NULL
211 AND json_extract(root.payload_json, '$.archivedAt') IS NULL`;
212
213// A single read transaction keeps the copy consistent with concurrent server writes.
214const copySourceSlice = Effect.fn("copyDevDbSourceSlice")(function* (sourcePath: string) {
215 const sql = yield* SqlClient.SqlClient;
216 yield* sql`ATTACH DATABASE ${`${NodeURL.pathToFileURL(sourcePath).href}?mode=ro`} AS src`;
217 const schema = yield* sql<{ type: string; name: string; sql: string }>`
218 SELECT type, name, sql FROM src.sqlite_master
219 WHERE sql IS NOT NULL AND name NOT LIKE 'sqlite_%'`;
220 const threadColumns = yield* sql<{ name: string; notnull: number }>`
221 SELECT m.name, c."notnull" FROM src.sqlite_master m, pragma_table_info(m.name, 'src') c
222 WHERE m.type = 'table' AND c.name = 'thread_id'`;
223 const threadIdNullable = new Map(threadColumns.map((row) => [row.name, row.notnull === 0]));
224 // FTS5 makes its own shadow tables and contentless rows cannot be copied:
225 // create the virtual table, skip its shadow tables and rows, and rebuild the
226 // index once the messages and triggers are in.
227 const virtualTables = schema
228 .filter((entry) => entry.type === "table" && /^CREATE VIRTUAL TABLE/i.test(entry.sql))
229 .map((entry) => entry.name);
230 const isShadowTable = (name: string) =>
231 virtualTables.some((virtual) =>
232 FTS5_SHADOW_SUFFIXES.some((suffix) => name === `${virtual}${suffix}`),
233 );
234 const tables = schema.filter((entry) => entry.type === "table" && !isShadowTable(entry.name));
235 const filtered = tables.some((table) => table.name === "orchestration_v2_projection_threads");
236 // A row that points into a cleared table cannot be copied, so whatever references one is
237 // cleared with it, however far down the chain a newer migration added it.
238 const references = yield* sql<{ name: string; parent: string }>`
239 SELECT m.name, f."table" AS parent FROM src.sqlite_master m, pragma_foreign_key_list(m.name, 'src') f
240 WHERE m.type = 'table'`;
241 const cleared = new Set(CLEARED_TABLES);
242 for (let grew = true; grew;) {
243 grew = false;
244 for (const { name, parent } of references) {
245 if (cleared.has(parent) && !cleared.has(name)) {
246 cleared.add(name);
247 grew = true;
248 }
249 }
250 }
251
252 yield* sql.withTransaction(
253 Effect.gen(function* () {
254 for (const table of tables) yield* sql.unsafe(table.sql).unprepared;
255 if (filtered) {
256 yield* sql`CREATE TEMP TABLE copied_threads (thread_id TEXT PRIMARY KEY)`;
257 yield* sql`INSERT OR IGNORE INTO copied_threads
258 SELECT family.thread_id FROM src.orchestration_v2_projection_threads family
259 JOIN src.orchestration_v2_projection_threads root ON root.thread_id =
260 COALESCE(json_extract(family.payload_json, '$.lineage.rootThreadId'), family.thread_id)
261 WHERE ${rootIsVisible(sql)}`;
262 }
263 for (const { name } of tables) {
264 if (cleared.has(name) || virtualTables.includes(name)) continue;
265 const nullable = threadIdNullable.get(name);
266 const filter = !filtered
267 ? ""
268 : name === "orchestration_events"
269 ? // Two ranges let SQLite seek the (aggregate_kind, stream_id) index instead of scanning.
270 `WHERE (aggregate_kind = 'thread' AND stream_id IN (SELECT thread_id FROM copied_threads))
271 OR aggregate_kind < 'thread' OR aggregate_kind > 'thread'`
272 : nullable === undefined || name === PROVIDER_SESSIONS_TABLE
273 ? ""
274 : `WHERE thread_id IN (SELECT thread_id FROM copied_threads)${nullable ? " OR thread_id IS NULL" : ""}`;
275 yield* sql.unsafe(`INSERT INTO main."${name}" SELECT * FROM src."${name}" ${filter}`)
276 .unprepared;
277 }
278 yield* sql`DELETE FROM main.sqlite_sequence`;
279 yield* sql`INSERT INTO main.sqlite_sequence SELECT name, seq FROM src.sqlite_sequence`;
280 for (const entry of schema) {
281 if (entry.type !== "table") yield* sql.unsafe(entry.sql).unprepared;
282 }
283 yield* rebuildMessageSearch(sql);
284 }),
285 );
286 yield* sql`DETACH DATABASE src`;
287});
288
289const pruneSnapshot = Effect.fn("pruneDevDbSnapshot")(function* (input: RunMigrateDevDbInput) {
290 const sql = yield* SqlClient.SqlClient;
291
292 // Usage-limit recovery also depends on settings; exclude latest-run failures and recovery choices.
293 const projections = yield* ProjectionStore.ProjectionStoreV2;
294 const recoveryThreadIds = yield* Effect.forEach(RECOVERY_KINDS, (kind) =>
295 projections.getRecoveryThreadIds(kind),
296 );
297 yield* sql`CREATE TEMP TABLE live_threads (thread_id TEXT PRIMARY KEY)`;
298 for (const threadId of new Set(recoveryThreadIds.flat())) {
299 yield* sql`INSERT INTO live_threads (thread_id) VALUES (${threadId})`;
300 }
301 yield* sql`INSERT OR IGNORE INTO live_threads (thread_id)
302 SELECT r.thread_id FROM orchestration_v2_projection_runs r
303 WHERE r.status = 'failed' AND r.ordinal = (
304 SELECT MAX(latest.ordinal) FROM orchestration_v2_projection_runs latest
305 WHERE latest.thread_id = r.thread_id)
306 UNION
307 SELECT thread_id FROM orchestration_v2_projection_threads
308 WHERE json_extract(payload_json, '$.limitRecovery') IS NOT NULL`;
309
310 // Clone or drop whole thread families to preserve fork and subagent lineage reads.
311 yield* sql`CREATE TEMP TABLE thread_families AS
312 SELECT thread_id, updated_at,
313 COALESCE(json_extract(payload_json, '$.lineage.rootThreadId'), thread_id) AS root_id
314 FROM orchestration_v2_projection_threads`;
315
316 yield* sql`CREATE TEMP TABLE stopped_families AS
317 SELECT f.root_id, root.project_id, MAX(f.updated_at) AS updated_at, COUNT(*) AS size
318 FROM thread_families f
319 JOIN orchestration_v2_projection_threads root ON root.thread_id = f.root_id
320 WHERE ${rootIsVisible(sql)}
321 GROUP BY f.root_id, root.project_id
322 HAVING SUM(f.thread_id IN (SELECT thread_id FROM live_threads)) = 0`;
323
324 yield* sql`CREATE TEMP TABLE kept_projects AS
325 SELECT p.project_id
326 FROM projection_projects p
327 LEFT JOIN (
328 SELECT project_id, MAX(updated_at) AS last_stopped_at
329 FROM stopped_families
330 GROUP BY project_id
331 ) q ON q.project_id = p.project_id
332 WHERE p.deleted_at IS NULL
333 ORDER BY (q.last_stopped_at IS NULL) ASC,
334 COALESCE(q.last_stopped_at, p.updated_at) DESC
335 LIMIT ${input.projects}`;
336
337 yield* sql`CREATE TEMP TABLE kept_threads AS
338 SELECT f.thread_id FROM thread_families f
339 WHERE f.root_id IN (
340 SELECT root_id FROM (
341 SELECT
342 sf.root_id,
343 ROW_NUMBER() OVER (
344 PARTITION BY sf.project_id
345 ORDER BY sf.updated_at DESC
346 ) AS recency_rank
347 FROM stopped_families sf
348 JOIN kept_projects kp ON kp.project_id = sf.project_id
349 -- One orchestrator family can hold hundreds of subagents and most of
350 -- the database, so a family larger than the limit is skipped.
351 WHERE sf.size <= ${input.threadsPerProject}
352 )
353 WHERE recency_rank <= ${input.threadsPerProject}
354 )`;
355
356 const threadTables = yield* sql<{ name: string }>`
357 SELECT m.name FROM sqlite_master m, pragma_table_info(m.name) c
358 WHERE m.type = 'table' AND c.name = 'thread_id'
359 AND m.name <> ${PROVIDER_SESSIONS_TABLE}`;
360
361 yield* sql.withTransaction(
362 Effect.gen(function* () {
363 yield* sql`DELETE FROM projection_projects
364 WHERE project_id NOT IN (SELECT project_id FROM kept_projects)`;
365 yield* sql`DELETE FROM ${sql(PROVIDER_SESSIONS_TABLE)}
366 WHERE COALESCE(thread_id, '') NOT IN (SELECT thread_id FROM kept_threads)
367 AND provider_session_id NOT IN (
368 SELECT provider_session_id FROM orchestration_v2_projection_provider_session_bindings
369 WHERE thread_id IN (SELECT thread_id FROM kept_threads)
370 )`;
371 for (const { name } of threadTables) {
372 yield* sql.unsafe(
373 `DELETE FROM "${name}" WHERE thread_id NOT IN (SELECT thread_id FROM kept_threads)`,
374 ).unprepared;
375 }
376 yield* sql`DELETE FROM orchestration_v2_projection_context_transfers
377 WHERE source_thread_id NOT IN (SELECT thread_id FROM kept_threads)
378 OR target_thread_id NOT IN (SELECT thread_id FROM kept_threads)`;
379 yield* sql`DELETE FROM orchestration_events
380 WHERE (aggregate_kind = 'thread'
381 AND stream_id NOT IN (SELECT thread_id FROM kept_threads))
382 OR (aggregate_kind = 'project'
383 AND stream_id NOT IN (SELECT project_id FROM kept_projects))`;
384 yield* sql`UPDATE orchestration_v2_projection_threads
385 SET payload_json = json_set(payload_json, '$.pullRequests', (
386 SELECT json_group_array(json_remove(link.value, '$.watch') ORDER BY link.key)
387 FROM json_each(payload_json, '$.pullRequests') AS link))
388 WHERE EXISTS (
389 SELECT 1 FROM json_each(payload_json, '$.pullRequests') AS link
390 WHERE json_extract(link.value, '$.watch') IS NOT NULL)`;
391 for (const table of CLEARED_TABLES) {
392 yield* sql`DELETE FROM ${sql(table)}`;
393 }
394 }),
395 );
396
397 const keptProjects = yield* sql<{ title: string; threads: number }>`
398 SELECT
399 p.title,
400 (SELECT COUNT(*) FROM orchestration_v2_projection_threads t
401 WHERE t.project_id = p.project_id) AS threads
402 FROM projection_projects p
403 ORDER BY p.updated_at DESC`;
404 const [events] = yield* sql<{ count: number }>`
405 SELECT COUNT(*) AS count FROM orchestration_events`;
406
407 return {
408 projects: keptProjects as ReadonlyArray<KeptProject>,
409 eventCount: events?.count ?? 0,
410 };
411});
412
413// Peer links preview builds recorded 68-70 before main took those slots for other migrations.
414// A snapshot with exactly that history drops the preview's schema so main's 68+ can run.
415const PEER_LINKS_PREVIEW_HISTORY = [
416 "68:PeerLinks",
417 "69:PeerLinkFence",
418 "70:WaitingLinkRequestIndex",
419];
420
421const forgetPeerLinksPreviewHistory = Effect.fn("forgetPeerLinksPreviewHistory")(function* () {
422 const sql = yield* SqlClient.SqlClient;
423 const tables = yield* sql`
424 SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'effect_sql_migrations'`;
425 if (tables.length === 0) return;
426 const tail = yield* sql<{ migration_id: number; name: string }>`
427 SELECT migration_id, name FROM effect_sql_migrations WHERE migration_id >= 68
428 ORDER BY migration_id`;
429 const history = tail.map((row) => `${row.migration_id}:${row.name}`);
430 if (history.join() !== PEER_LINKS_PREVIEW_HISTORY.join()) return;
431 yield* sql.withTransaction(
432 Effect.gen(function* () {
433 yield* sql`DROP INDEX IF EXISTS orchestration_v2_projection_turn_items_waiting_link_idx`;
434 yield* sql`DROP TABLE IF EXISTS peer_environment_links`;
435 yield* sql`DELETE FROM effect_sql_migrations WHERE migration_id >= 68`;
436 }),
437 );
438});
439
440export const runMigrateDevDb = Effect.fn("runMigrateDevDb")(function* (
441 input: RunMigrateDevDbInput,
442 options: RunMigrateDevDbOptions = {},
443) {
444 // Negative SQLite LIMITs mean unlimited; validate direct callers as well as CLI flags.
445 if (input.projects < 1 || input.threadsPerProject < 0) {
446 return yield* Effect.die("projects must be >= 1 and threadsPerProject >= 0");
447 }
448 const fs = yield* FileSystem.FileSystem;
449 const path = yield* Path.Path;
450
451 const sharedHome = path.resolve(options.sharedHome ?? path.join(NodeOS.homedir(), ".t3"));
452 const sourcePath = path.resolve(
453 input.source ?? path.join(sharedHome, "userdata", "statev2.sqlite"),
454 );
455
456 const baseDir =
457 input.baseDir !== undefined
458 ? path.resolve(input.baseDir)
459 : yield* resolveWorktreeT3Home(process.cwd());
460 if (baseDir === undefined) {
461 return yield* new MigrateDevDbNotInWorktreeError();
462 }
463 const stateDir = path.join(baseDir, "userdata");
464 const databasePath = path.join(stateDir, "statev2.sqlite");
465 const snapshotPath = `${databasePath}.migrate-dev-db-tmp`;
466
467 if (!(yield* fs.exists(sourcePath))) {
468 return yield* new MigrateDevDbSourceMissingError({ sourcePath });
469 }
470 const [canonicalBaseDir, canonicalSharedHome] = yield* Effect.all([
471 fs.realPath(baseDir).pipe(Effect.orElseSucceed(() => baseDir)),
472 fs.realPath(sharedHome).pipe(Effect.orElseSucceed(() => sharedHome)),
473 ]);
474 if (canonicalBaseDir === canonicalSharedHome) {
475 return yield* new MigrateDevDbSharedHomeError();
476 }
477 // Both destination and snapshot are deleted below; reject sources resolving to either.
478 const canonicalSourcePath = yield* fs
479 .realPath(sourcePath)
480 .pipe(Effect.orElseSucceed(() => sourcePath));
481 for (const destination of [databasePath, snapshotPath]) {
482 const canonicalDestination = yield* fs
483 .realPath(destination)
484 .pipe(Effect.orElseSucceed(() => destination));
485 if (canonicalSourcePath === canonicalDestination) {
486 return yield* new MigrateDevDbSourceIsDestinationError({ sourcePath });
487 }
488 }
489
490 yield* fs.makeDirectory(stateDir, { recursive: true });
491 yield* ensureNotInUse(databasePath);
492
493 const wrapPhase =
494 (phase: MigrateDevDbPhaseError["phase"], phaseDatabasePath: string) =>
495 <A, E, R>(effect: Effect.Effect<A, E, R>) =>
496 effect.pipe(
497 Effect.mapError(
498 (cause) => new MigrateDevDbPhaseError({ phase, databasePath: phaseDatabasePath, cause }),
499 ),
500 );
501
502 yield* removeDatabaseFiles(snapshotPath);
503 const { executedMigrations, pruned } = yield* Effect.gen(function* () {
504 yield* Console.log(`Copying visible threads from ${sourcePath} (read-only)...`);
505 yield* copySourceSlice(sourcePath).pipe(
506 Effect.provide(NodeSqliteClient.layer({ filename: snapshotPath })),
507 wrapPhase("snapshot", sourcePath),
508 );
509
510 // Migrate first so prune queries can use columns missing from older sources.
511 yield* Console.log("Running migrations on the snapshot...");
512 const executed = yield* Effect.gen(function* () {
513 const sql = yield* SqlClient.SqlClient;
514 yield* sql.unsafe("PRAGMA foreign_keys = ON").unprepared;
515 yield* forgetPeerLinksPreviewHistory();
516 return yield* runMigrations();
517 }).pipe(
518 Effect.provide(NodeSqliteClient.layer({ filename: snapshotPath })),
519 wrapPhase("migrate", snapshotPath),
520 );
521
522 yield* Console.log(
523 `Pruning to ${input.projects} projects, ${input.threadsPerProject} stopped threads each...`,
524 );
525 const result = yield* pruneSnapshot(input).pipe(
526 Effect.provide(
527 ProjectionStore.layer.pipe(
528 Layer.provideMerge(NodeSqliteClient.layer({ filename: snapshotPath })),
529 ),
530 ),
531 wrapPhase("prune", snapshotPath),
532 );
533
534 yield* Console.log(`Compacting into ${databasePath}...`);
535 // A server may have started during pruning; re-check before replacing its database.
536 yield* ensureNotInUse(databasePath);
537 yield* removeDatabaseFiles(databasePath);
538 yield* Effect.gen(function* () {
539 const sql = yield* SqlClient.SqlClient;
540 yield* sql`VACUUM INTO ${databasePath}`;
541 }).pipe(
542 Effect.provide(NodeSqliteClient.layer({ filename: snapshotPath })),
543 wrapPhase("compact", databasePath),
544 );
545 return { executedMigrations: executed, pruned: result };
546 }).pipe(Effect.ensuring(removeDatabaseFiles(snapshotPath)));
547 yield* fs.chmod(databasePath, 0o600);
548
549 yield* Effect.gen(function* () {
550 const sql = yield* SqlClient.SqlClient;
551 // VACUUM INTO loses WAL mode; restore the server boot setting.
552 yield* sql.unsafe("PRAGMA journal_mode = WAL").unprepared;
553 }).pipe(
554 Effect.provide(NodeSqliteClient.layer({ filename: databasePath })),
555 wrapPhase("compact", databasePath),
556 );
557
558 const size = (yield* fs.stat(databasePath)).size;
559 return {
560 databasePath,
561 sizeBytes: Number(size),
562 projects: pruned.projects,
563 eventCount: pruned.eventCount,
564 executedMigrations: executedMigrations.map(([id, name]) => `${id}_${name}`),
565 };
566});
567
568const formatSize = (bytes: number): string =>
569 bytes >= 1024 * 1024
570 ? `${(bytes / (1024 * 1024)).toFixed(1)} MB`
571 : `${(bytes / 1024).toFixed(0)} KB`;
572
573export const migrateDevDbCommand = Command.make(
574 "migrate-dev-db",
575 {
576 projects: Flag.Int("projects").pipe(
577 Flag.withDefault(5),
578 Flag.withDescription("How many recently updated projects to keep."),
579 ),
580 threadsPerProject: Flag.Int("threads-per-project").pipe(
581 Flag.withDefault(10),
582 Flag.withDescription(
583 "How many recent stopped threads, with their forks and subagents, to keep per project. A family with more threads than this is skipped.",
584 ),
585 ),
586 baseDir: Flag.String("base-dir").pipe(
587 Flag.optional,
588 Flag.withDescription("Isolated .t3 directory. Defaults to the current worktree's .t3."),
589 ),
590 source: Flag.String("source").pipe(
591 Flag.optional,
592 Flag.withDescription("Source database. Defaults to ~/.t3/userdata/statev2.sqlite."),
593 ),
594 },
595 ({ projects, threadsPerProject, baseDir, source }) =>
596 Effect.gen(function* () {
597 const result = yield* runMigrateDevDb({
598 projects,
599 threadsPerProject,
600 baseDir: Option.getOrUndefined(baseDir),
601 source: Option.getOrUndefined(source),
602 });
603 yield* Console.log("");
604 yield* Console.log(
605 `Dev database ready: ${result.databasePath} (${formatSize(result.sizeBytes)})`,
606 );
607 for (const project of result.projects) {
608 yield* Console.log(` ${project.title}: ${project.threads} threads`);
609 }
610 yield* Console.log(` ${result.eventCount} orchestration events kept`);
611 yield* Console.log(
612 result.executedMigrations.length === 0
613 ? " Migrations: already current (no new migrations in this checkout)"
614 : ` Migrations applied: ${result.executedMigrations.join(", ")}`,
615 );
616 }),
617).pipe(
618 Command.withDescription(
619 "Rebuild the worktree dev database from a pruned snapshot of the real ~/.t3 data, then run migrations.",
620 ),
621);
622
623if (import.meta.main) {
624 Command.run(migrateDevDbCommand, { version: "0.0.0" }).pipe(
625 Effect.provide(NodeServices.layer),
626 NodeRuntime.runMain,
627 );
628}