{"version":3,"file":"clean.mjs","names":[],"sources":["../../src/services/clean.ts"],"sourcesContent":["import { existsSync, readdirSync, rmSync, statSync } from \"node:fs\";\nimport { join } from \"node:path\";\n\n/**\n * `bos clean` — the local-only rule's exit valve (ADR 0047): one command that\n * removes regenerable `.bos` state and refuses the artifacts that are truth\n * or identity. The partition is the contract; the handler just runs it.\n */\n\nexport interface CleanTarget {\n  /** dotted category id — the report key */\n  id: string;\n  /** absolute path under the config dir */\n  path: string;\n  /** `dir` removes the tree, `files` removes matching files in place (logs —\n   * the dir itself stays; a live session's fresh log survives), `file`\n   * removes a single file */\n  kind: \"dir\" | \"files\" | \"file\";\n  /** present ⇒ never deleted: the never-touch invariant, with the reason */\n  guarded?: string;\n}\n\n/** The absolute never-touch list — truth (PGlite data), identity (session\n * credentials), machine prefs (port pins), and the publish accelerator whose\n * cleaning would silently cost full re-uploads. `bos clean` reports these as\n * kept, always. */\nexport const CLEAN_NEVER_TOUCH: ReadonlyArray<{ name: string; reason: string }> = [\n  { name: \"db\", reason: \"PGlite data — the app's actual state\" },\n  { name: \"session.json\", reason: \"login credentials — bos logout, not bos clean\" },\n  { name: \"infra-state.json\", reason: \"machine port prefs\" },\n  {\n    name: \"deploy-state.json\",\n    reason: \"skip-unchanged accelerator — cleaning costs full re-uploads\",\n  },\n];\n\n/** Files touched within this window are never cleaned — a live dev session\n * is appending to its log and `bos logs` reads it. */\nconst LIVE_LOG_WINDOW_MS = 60 * 60 * 1000;\n\nfunction bosPath(configDir: string, name: string): string {\n  return join(configDir, \".bos\", name);\n}\n\nexport function planClean(configDir: string): CleanTarget[] {\n  const removable: CleanTarget[] = [\n    { id: \"logs\", path: bosPath(configDir, \"logs\"), kind: \"files\" },\n    { id: \"bundles\", path: bosPath(configDir, \"bundles\"), kind: \"dir\" },\n    { id: \"regression\", path: bosPath(configDir, \"regression\"), kind: \"dir\" },\n    { id: \"generated\", path: bosPath(configDir, \"generated\"), kind: \"dir\" },\n    { id: \"bundle-cache\", path: bosPath(configDir, \"bundle-cache\"), kind: \"dir\" },\n    { id: \"snapshot\", path: bosPath(configDir, \"snapshot\"), kind: \"dir\" },\n    { id: \"sync-backup\", path: bosPath(configDir, \"sync-backup\"), kind: \"dir\" },\n    { id: \"import-backup\", path: bosPath(configDir, \"import-backup\"), kind: \"dir\" },\n    { id: \"compose-retired\", path: bosPath(configDir, \"compose-retired\"), kind: \"dir\" },\n    { id: \"manifest-cache\", path: bosPath(configDir, \"manifest.json\"), kind: \"file\" },\n    { id: \"resolved-config\", path: bosPath(configDir, \"bos.resolved-config.json\"), kind: \"file\" },\n    { id: \"build-cache\", path: bosPath(configDir, \"build-cache.json\"), kind: \"file\" },\n  ];\n  const guarded: CleanTarget[] = CLEAN_NEVER_TOUCH.map(({ name, reason }) => ({\n    id: name,\n    path: bosPath(configDir, name),\n    kind: existsSync(bosPath(configDir, name)) ? \"dir\" : \"file\",\n    guarded: reason,\n  }));\n  return [...removable, ...guarded];\n}\n\nexport function dirSize(path: string): number {\n  let total = 0;\n  try {\n    for (const name of readdirSync(path)) {\n      const full = join(path, name);\n      const stat = statSync(full);\n      if (stat.isDirectory()) {\n        total += dirSize(full);\n      } else {\n        total += stat.size;\n      }\n    }\n  } catch {\n    return total;\n  }\n  return total;\n}\n\nexport interface CleanEntryResult {\n  id: string;\n  path: string;\n  bytes: number;\n  action: \"removed\" | \"would-remove\" | \"kept\" | \"absent\";\n  /** `kept` — the never-touch reason; `removed` on logs — fresh files skipped */\n  reason?: string;\n}\n\nexport interface CleanReport {\n  status: \"ok\";\n  dryRun: boolean;\n  entries: CleanEntryResult[];\n  freedBytes: number;\n}\n\nfunction removeTree(target: CleanTarget): number {\n  const bytes = dirSize(target.path);\n  rmSync(target.path, { recursive: true, force: true });\n  return bytes;\n}\n\nfunction removeFile(target: CleanTarget): number {\n  const bytes = statSync(target.path).size;\n  rmSync(target.path, { force: true });\n  return bytes;\n}\n\nfunction cleanLogs(target: CleanTarget, dryRun: boolean): CleanEntryResult {\n  const now = Date.now();\n  let bytes = 0;\n  let fresh = 0;\n  let names: string[] = [];\n  try {\n    names = readdirSync(target.path);\n  } catch {\n    return { id: target.id, path: target.path, bytes: 0, action: \"absent\" };\n  }\n  for (const name of names) {\n    const full = join(target.path, name);\n    let size = 0;\n    let mtimeMs = 0;\n    try {\n      const stat = statSync(full);\n      if (!stat.isFile()) continue;\n      size = stat.size;\n      mtimeMs = stat.mtimeMs;\n    } catch {\n      continue;\n    }\n    if (now - mtimeMs < LIVE_LOG_WINDOW_MS) {\n      fresh++;\n      continue;\n    }\n    bytes += size;\n    if (!dryRun) rmSync(full, { force: true });\n  }\n  return {\n    id: target.id,\n    path: target.path,\n    bytes,\n    action: bytes > 0 || fresh > 0 ? (dryRun ? \"would-remove\" : \"removed\") : \"absent\",\n    ...(fresh > 0 ? { reason: `${fresh} fresh file(s) kept (live sessions)` } : {}),\n  };\n}\n\nexport function executeClean(\n  configDir: string,\n  opts: { dryRun?: boolean; skip?: string[] } = {},\n): CleanReport {\n  const dryRun = opts.dryRun ?? false;\n  const skip = new Set(opts.skip ?? []);\n  const entries: CleanEntryResult[] = [];\n  let freedBytes = 0;\n\n  for (const target of planClean(configDir)) {\n    if (skip.has(target.id)) continue;\n    if (target.guarded) {\n      entries.push({\n        id: target.id,\n        path: target.path,\n        bytes: 0,\n        action: \"kept\",\n        reason: target.guarded,\n      });\n      continue;\n    }\n    if (!existsSync(target.path)) {\n      entries.push({ id: target.id, path: target.path, bytes: 0, action: \"absent\" });\n      continue;\n    }\n    if (target.kind === \"files\") {\n      const entry = cleanLogs(target, dryRun);\n      entries.push(entry);\n      freedBytes += entry.bytes;\n      continue;\n    }\n    const bytes = target.kind === \"dir\" ? dirSize(target.path) : statSync(target.path).size;\n    if (!dryRun) {\n      if (target.kind === \"dir\") removeTree(target);\n      else removeFile(target);\n    }\n    entries.push({\n      id: target.id,\n      path: target.path,\n      bytes,\n      action: dryRun ? \"would-remove\" : \"removed\",\n    });\n    freedBytes += bytes;\n  }\n\n  return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