{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/cli/commands/history-import/index.ts"],"sourcesContent":["import fs from 'node:fs';\nimport path from 'node:path';\n\nimport { protectAndImportHistory, type HistoryOwnership } from '@agimon-ai/doompi-core/history-import';\nimport { createHistoryOwnership } from '@agimon-ai/doompi-core/history-ownership';\nimport { importV3WithPinnedUpstream } from '@agimon-ai/doompi-core/jsonl-session-repo';\nimport { importSqliteHistory, verifySqliteHistory } from '@agimon-ai/doompi-core/sqlite-history-import';\n\nimport { wantsHelp } from '../../router';\nimport { historyImportHelp } from './help';\n\ninterface HistoryImportPaths {\n  sourcePath: string;\n  destinationPath: string;\n  format: 'jsonl' | 'sqlite';\n}\n\nexport interface HistoryImportOutput {\n  write(chunk: string): void;\n}\n\nfunction parsePaths(args: readonly string[], currentDirectory: string): HistoryImportPaths {\n  const positional: string[] = [];\n  let confirmed = false;\n  let format: 'jsonl' | 'sqlite' = 'jsonl';\n\n  for (let index = 0; index < args.length; index += 1) {\n    const argument = args[index]!;\n    if (argument === '--format') {\n      if (format === 'sqlite') throw new Error('--format was specified more than once');\n      const next = args[++index];\n      if (next !== 'sqlite') throw new Error('history-import --format supports sqlite');\n      format = next;\n      continue;\n    }\n    if (argument === '--') throw new Error('doompi history-import does not accept passthrough arguments');\n    if (argument === '--confirm-offline') {\n      if (confirmed) throw new Error('--confirm-offline was specified more than once');\n      confirmed = true;\n      continue;\n    }\n    if (argument.startsWith('-')) throw new Error(`doompi history-import does not accept ${argument}`);\n    positional.push(argument);\n  }\n\n  if (!confirmed) throw new Error('doompi history-import requires --confirm-offline after stopping Pi');\n  if (positional.length !== 2) {\n    throw new Error('doompi history-import requires a v3 source and distinct v4 destination');\n  }\n\n  const sourcePath = path.resolve(currentDirectory, positional[0]!);\n  const destinationPath = path.resolve(currentDirectory, positional[1]!);\n  if (sourcePath === destinationPath) throw new Error('Protected history destination must differ from source');\n  return { sourcePath, destinationPath, format };\n}\n\n/** Imports one explicitly confirmed v3 journal without starting Pi or the harness. */\nexport async function runHistoryImport(\n  args: readonly string[],\n  _environment: NodeJS.ProcessEnv = process.env,\n  currentDirectory = process.cwd(),\n  output: HistoryImportOutput = process.stdout,\n  owner?: HistoryOwnership,\n): Promise<number> {\n  if (wantsHelp(args.slice(1))) {\n    output.write(historyImportHelp());\n    return 0;\n  }\n\n  const paths = parsePaths(args.slice(1), currentDirectory);\n  const header =\n    paths.format === 'sqlite'\n      ? (JSON.parse(fs.readFileSync(paths.sourcePath, 'utf8').split('\\n', 1)[0]!) as { v?: number })\n      : undefined;\n  const result = await protectAndImportHistory({\n    sourcePath: paths.sourcePath,\n    destinationPath: paths.destinationPath,\n    owner:\n      owner ??\n      createHistoryOwnership({\n        sourceFormat: header?.v === 4 ? 'v4' : 'v3',\n        additionalPaths: [paths.destinationPath],\n      }),\n    importStaging: paths.format === 'sqlite' ? importSqliteHistory : importV3WithPinnedUpstream,\n    ...(paths.format === 'sqlite' ? { verifyStaging: verifySqliteHistory } : {}),\n  });\n  output.write(\n    `${JSON.stringify({\n      status: result.status,\n      sourcePath: result.sourceIdentity.path,\n      originalPath: result.originalPath,\n      destinationPath: result.destinationPath,\n      statePath: result.statePath,\n      entryCount: result.verification.entries.length,\n      branchCount: result.verification.branches.length,\n    })}\\n`,\n  );\n  return 0;\n}\n"],"mappings":";;;;;;;;;AAqBA,SAAS,WAAW,MAAyB,kBAA8C;CACzF,MAAM,aAAuB,CAAC;CAC9B,IAAI,YAAY;CAChB,IAAI,SAA6B;CAEjC,KAAK,IAAI,QAAQ,GAAG,QAAQ,KAAK,QAAQ,SAAS,GAAG;EACnD,MAAM,WAAW,KAAK;EACtB,IAAI,aAAa,YAAY;GAC3B,IAAI,WAAW,UAAU,MAAM,IAAI,MAAM,uCAAuC;GAChF,MAAM,OAAO,KAAK,EAAE;GACpB,IAAI,SAAS,UAAU,MAAM,IAAI,MAAM,yCAAyC;GAChF,SAAS;GACT;EACF;EACA,IAAI,aAAa,MAAM,MAAM,IAAI,MAAM,6DAA6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