import type { ThtRunner } from "../tht/tht-runner.js"; import type { SemanticRuntimeConfig } from "../workspaces/runtime-renderer.js"; import type { CatalogSyncRun, WorkspaceDatabase } from "./types.js"; /** Internal continuation of an applied physical sync, using the harness Memory boundary. */ export function createMemoryCleanup(runner: Pick, runtime: SemanticRuntimeConfig) { return async (database: WorkspaceDatabase, run: CatalogSyncRun): Promise => { if (run.phase !== "memory_cleanup" || !run.plannedDiff) throw new Error("Physical cleanup is not committed"); const result = await runner.withPrincipal({ issuer: "installation", subject: "catalog-sync", roles: ["admin"], permissions: ["memory.manage"], isAdmin: true, }).runWithRuntimeSnapshot(["memory", "admin", "--workspace", database.workspaceId], JSON.stringify({ action: "cleanup", runtime, request: { sync_id: run.id, database: database.databaseName, schema_name: database.schema, removed_tables: run.plannedDiff.deletedTables, removed_columns: run.plannedDiff.deletedColumns.map(column => ({ table: column.tableName, column: column.columnName })), }, })); const payload = JSON.parse(result.stdout); if (result.code !== 0 || payload.indexed !== true || !Number.isInteger(payload.deleted) || payload.deleted < 0) { throw new Error("Memory cleanup is incomplete"); } return payload.deleted; }; }