Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

migrate-dev-db.ts

593 lines23.1 KB

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 }
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 }
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 }
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 }
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 }
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 }
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 }
104​
105export interface RunMigrateDevDbInput {
106 readonly baseDir?: string | undefined;
107 readonly source?: string | undefined;
108 readonly projects: number;
109 readonly threadsPerProject: number;
111​
112export interface RunMigrateDevDbOptions {
113 /** Test override for the directory writes must never target. */
114 readonly sharedHome?: string | undefined;
116​
117interface KeptProject {
118 readonly title: string;
119 readonly threads: number;
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 for (const table of CLEARED_TABLES) {
385 yield* sql`DELETE FROM ${sql(table)}`;
386 }
387 }),
388 );
389​
390 const keptProjects = yield* sql<{ title: string; threads: number }>`
391 SELECT
392 p.title,
393 (SELECT COUNT(*) FROM orchestration_v2_projection_threads t
394 WHERE t.project_id = p.project_id) AS threads
395 FROM projection_projects p
396 ORDER BY p.updated_at DESC`;
397 const [events] = yield* sql<{ count: number }>`
398 SELECT COUNT(*) AS count FROM orchestration_events`;
399​
400 return {
401 projects: keptProjects as ReadonlyArray<KeptProject>,
402 eventCount: events?.count ?? 0,
403 };
404});
405​
406export const runMigrateDevDb = Effect.fn("runMigrateDevDb")(function* (
407 input: RunMigrateDevDbInput,
408 options: RunMigrateDevDbOptions = {},
409) {
410 // Negative SQLite LIMITs mean unlimited; validate direct callers as well as CLI flags.
411 if (input.projects < 1 || input.threadsPerProject < 0) {
412 return yield* Effect.die("projects must be >= 1 and threadsPerProject >= 0");
413 }
414 const fs = yield* FileSystem.FileSystem;
415 const path = yield* Path.Path;
416​
417 const sharedHome = path.resolve(options.sharedHome ?? path.join(NodeOS.homedir(), ".t3"));
418 const sourcePath = path.resolve(
419 input.source ?? path.join(sharedHome, "userdata", "statev2.sqlite"),
420 );
421​
422 const baseDir =
423 input.baseDir !== undefined
424 ? path.resolve(input.baseDir)
425 : yield* resolveWorktreeT3Home(process.cwd());
426 if (baseDir === undefined) {
427 return yield* new MigrateDevDbNotInWorktreeError();
428 }
429 const stateDir = path.join(baseDir, "userdata");
430 const databasePath = path.join(stateDir, "statev2.sqlite");
431 const snapshotPath = `${databasePath}.migrate-dev-db-tmp`;
432​
433 if (!(yield* fs.exists(sourcePath))) {
434 return yield* new MigrateDevDbSourceMissingError({ sourcePath });
435 }
436 const [canonicalBaseDir, canonicalSharedHome] = yield* Effect.all([
437 fs.realPath(baseDir).pipe(Effect.orElseSucceed(() => baseDir)),
438 fs.realPath(sharedHome).pipe(Effect.orElseSucceed(() => sharedHome)),
439 ]);
440 if (canonicalBaseDir === canonicalSharedHome) {
441 return yield* new MigrateDevDbSharedHomeError();
442 }
443 // Both destination and snapshot are deleted below; reject sources resolving to either.
444 const canonicalSourcePath = yield* fs
445 .realPath(sourcePath)
446 .pipe(Effect.orElseSucceed(() => sourcePath));
447 for (const destination of [databasePath, snapshotPath]) {
448 const canonicalDestination = yield* fs
449 .realPath(destination)
450 .pipe(Effect.orElseSucceed(() => destination));
451 if (canonicalSourcePath === canonicalDestination) {
452 return yield* new MigrateDevDbSourceIsDestinationError({ sourcePath });
453 }
454 }
455​
456 yield* fs.makeDirectory(stateDir, { recursive: true });
457 yield* ensureNotInUse(databasePath);
458​
459 const wrapPhase =
460 (phase: MigrateDevDbPhaseError["phase"], phaseDatabasePath: string) =>
461 <A, E, R>(effect: Effect.Effect<A, E, R>) =>
462 effect.pipe(
463 Effect.mapError(
464 (cause) => new MigrateDevDbPhaseError({ phase, databasePath: phaseDatabasePath, cause }),
465 ),
466 );
467​
468 yield* removeDatabaseFiles(snapshotPath);
469 const { executedMigrations, pruned } = yield* Effect.gen(function* () {
470 yield* Console.log(`Copying visible threads from ${sourcePath} (read-only)...`);
471 yield* copySourceSlice(sourcePath).pipe(
472 Effect.provide(NodeSqliteClient.layer({ filename: snapshotPath })),
473 wrapPhase("snapshot", sourcePath),
474 );
475​
476 // Migrate first so prune queries can use columns missing from older sources.
477 yield* Console.log("Running migrations on the snapshot...");
478 const executed = yield* Effect.gen(function* () {
479 const sql = yield* SqlClient.SqlClient;
480 yield* sql.unsafe("PRAGMA foreign_keys = ON").unprepared;
481 return yield* runMigrations();
482 }).pipe(
483 Effect.provide(NodeSqliteClient.layer({ filename: snapshotPath })),
484 wrapPhase("migrate", snapshotPath),
485 );
486​
487 yield* Console.log(
488 `Pruning to ${input.projects} projects, ${input.threadsPerProject} stopped threads each...`,
489 );
490 const result = yield* pruneSnapshot(input).pipe(
491 Effect.provide(
492 ProjectionStore.layer.pipe(
493 Layer.provideMerge(NodeSqliteClient.layer({ filename: snapshotPath })),
494 ),
495 ),
496 wrapPhase("prune", snapshotPath),
497 );
498​
499 yield* Console.log(`Compacting into ${databasePath}...`);
500 // A server may have started during pruning; re-check before replacing its database.
501 yield* ensureNotInUse(databasePath);
502 yield* removeDatabaseFiles(databasePath);
503 yield* Effect.gen(function* () {
504 const sql = yield* SqlClient.SqlClient;
505 yield* sql`VACUUM INTO ${databasePath}`;
506 }).pipe(
507 Effect.provide(NodeSqliteClient.layer({ filename: snapshotPath })),
508 wrapPhase("compact", databasePath),
509 );
510 return { executedMigrations: executed, pruned: result };
511 }).pipe(Effect.ensuring(removeDatabaseFiles(snapshotPath)));
512 yield* fs.chmod(databasePath, 0o600);
513​
514 yield* Effect.gen(function* () {
515 const sql = yield* SqlClient.SqlClient;
516 // VACUUM INTO loses WAL mode; restore the server boot setting.
517 yield* sql.unsafe("PRAGMA journal_mode = WAL").unprepared;
518 }).pipe(
519 Effect.provide(NodeSqliteClient.layer({ filename: databasePath })),
520 wrapPhase("compact", databasePath),
521 );
522​
523 const size = (yield* fs.stat(databasePath)).size;
524 return {
525 databasePath,
526 sizeBytes: Number(size),
527 projects: pruned.projects,
528 eventCount: pruned.eventCount,
529 executedMigrations: executedMigrations.map(([id, name]) => `${id}_${name}`),
530 };
531});
532​
533const formatSize = (bytes: number): string =>
534 bytes >= 1024 * 1024
535 ? `${(bytes / (1024 * 1024)).toFixed(1)} MB`
536 : `${(bytes / 1024).toFixed(0)} KB`;
537​
538export const migrateDevDbCommand = Command.make(
539 "migrate-dev-db",
540 {
541 projects: Flag.Int("projects").pipe(
542 Flag.withDefault(5),
543 Flag.withDescription("How many recently updated projects to keep."),
544 ),
545 threadsPerProject: Flag.Int("threads-per-project").pipe(
546 Flag.withDefault(10),
547 Flag.withDescription(
548 "How many recent stopped threads, with their forks and subagents, to keep per project. A family with more threads than this is skipped.",
549 ),
550 ),
551 baseDir: Flag.String("base-dir").pipe(
552 Flag.optional,
553 Flag.withDescription("Isolated .t3 directory. Defaults to the current worktree's .t3."),
554 ),
555 source: Flag.String("source").pipe(
556 Flag.optional,
557 Flag.withDescription("Source database. Defaults to ~/.t3/userdata/statev2.sqlite."),
558 ),
559 },
560 ({ projects, threadsPerProject, baseDir, source }) =>
561 Effect.gen(function* () {
562 const result = yield* runMigrateDevDb({
563 projects,
564 threadsPerProject,
565 baseDir: Option.getOrUndefined(baseDir),
566 source: Option.getOrUndefined(source),
567 });
568 yield* Console.log("");
569 yield* Console.log(
570 `Dev database ready: ${result.databasePath} (${formatSize(result.sizeBytes)})`,
571 );
572 for (const project of result.projects) {
573 yield* Console.log(` ${project.title}: ${project.threads} threads`);
574 }
575 yield* Console.log(` ${result.eventCount} orchestration events kept`);
576 yield* Console.log(
577 result.executedMigrations.length === 0
578 ? " Migrations: already current (no new migrations in this checkout)"
579 : ` Migrations applied: ${result.executedMigrations.join(", ")}`,
580 );
581 }),
582).pipe(
583 Command.withDescription(
584 "Rebuild the worktree dev database from a pruned snapshot of the real ~/.t3 data, then run migrations.",
585 ),
586);
587​
588if (import.meta.main) {
589 Command.run(migrateDevDbCommand, { version: "0.0.0" }).pipe(
590 Effect.provide(NodeServices.layer),
591 NodeRuntime.runMain,
592 );