|
| 1 | +import type { Command } from "commander"; |
| 2 | +import { formatErrorMessage } from "../../infra/errors.js"; |
| 3 | +import { defaultRuntime, writeRuntimeJson, writeRuntimeStdout } from "../../runtime.js"; |
| 4 | +import { |
| 5 | + OPENCLAW_DATABASE_SCHEMA_DOCS_URL, |
| 6 | + preflightOpenClawStateDatabasePath, |
| 7 | +} from "../../state/openclaw-database-preflight.js"; |
| 8 | +import { resolveDatabasePath } from "../../state/openclaw-state-db-maintenance.js"; |
| 9 | +import { claimOpenClawStateOwnership } from "../../state/openclaw-state-ownership-operations.js"; |
| 10 | +import { inspectOpenClawStateOwnershipAtPath } from "../../state/openclaw-state-ownership.js"; |
| 11 | +import { applyParentDefaultHelpAction } from "./parent-default-help.js"; |
| 12 | + |
| 13 | +type DatabaseOutputOptions = { json?: boolean }; |
| 14 | + |
| 15 | +function writeDatabaseError(error: unknown, json: boolean): void { |
| 16 | + const message = formatErrorMessage(error); |
| 17 | + if (json) { |
| 18 | + writeRuntimeJson(defaultRuntime, { error: message }); |
| 19 | + } else { |
| 20 | + defaultRuntime.error(message); |
| 21 | + } |
| 22 | + defaultRuntime.exit(1); |
| 23 | +} |
| 24 | + |
| 25 | +async function runDatabasePreflight(databasePath: string, options: DatabaseOutputOptions) { |
| 26 | + const result = await preflightOpenClawStateDatabasePath(databasePath); |
| 27 | + if (options.json) { |
| 28 | + writeRuntimeJson(defaultRuntime, result); |
| 29 | + } else { |
| 30 | + const detail = result.reason ?? result.issues[0]?.message; |
| 31 | + writeRuntimeStdout( |
| 32 | + defaultRuntime, |
| 33 | + `Database preflight: ${result.status} (found ${result.foundVersion ?? "unknown"}, target ${result.targetVersion}).${detail ? `\n${detail}` : ""}\nSee ${OPENCLAW_DATABASE_SCHEMA_DOCS_URL}.\n`, |
| 34 | + ); |
| 35 | + } |
| 36 | + if (result.status === "incompatible" || result.status === "indeterminate") { |
| 37 | + defaultRuntime.exit(1); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function runDatabaseOwnership(options: DatabaseOutputOptions & { manager?: string }): void { |
| 42 | + try { |
| 43 | + const databasePath = resolveDatabasePath({ env: process.env }); |
| 44 | + const ownership = |
| 45 | + options.manager !== undefined |
| 46 | + ? claimOpenClawStateOwnership(options.manager, { |
| 47 | + path: databasePath, |
| 48 | + env: process.env, |
| 49 | + }) |
| 50 | + : inspectOpenClawStateOwnershipAtPath(databasePath); |
| 51 | + const status = ownership |
| 52 | + ? { status: "external" as const, ownership } |
| 53 | + : { status: "unowned" as const }; |
| 54 | + if (options.json) { |
| 55 | + writeRuntimeJson(defaultRuntime, { databasePath, ...status }); |
| 56 | + return; |
| 57 | + } |
| 58 | + const message = |
| 59 | + status.status === "external" |
| 60 | + ? `Shared state is externally owned by ${status.ownership.managerId}.` |
| 61 | + : "Shared state is not externally owned."; |
| 62 | + writeRuntimeStdout(defaultRuntime, `${message}\nSee ${OPENCLAW_DATABASE_SCHEMA_DOCS_URL}.\n`); |
| 63 | + } catch (error) { |
| 64 | + writeDatabaseError(error, options.json === true); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +export function registerDatabaseCommand(program: Command): void { |
| 69 | + const database = program |
| 70 | + .command("database") |
| 71 | + .description("Inspect shared-state schema compatibility and write ownership") |
| 72 | + .addHelpText("after", `\nDocs: ${OPENCLAW_DATABASE_SCHEMA_DOCS_URL}\n`); |
| 73 | + |
| 74 | + database |
| 75 | + .command("preflight") |
| 76 | + .description("Compare one copied SQLite file with this release's state schema") |
| 77 | + .argument("<path>", "explicit copied SQLite database path") |
| 78 | + .option("--json", "emit machine-readable JSON", false) |
| 79 | + .action(async (databasePath: string, options: DatabaseOutputOptions) => { |
| 80 | + await runDatabasePreflight(databasePath, options); |
| 81 | + }); |
| 82 | + |
| 83 | + const ownership = database.command("ownership").description("Inspect or claim write ownership"); |
| 84 | + ownership |
| 85 | + .command("status") |
| 86 | + .description("Show durable shared-state write ownership") |
| 87 | + .option("--json", "emit machine-readable JSON", false) |
| 88 | + .action((options: DatabaseOutputOptions) => runDatabaseOwnership(options)); |
| 89 | + ownership |
| 90 | + .command("claim") |
| 91 | + .description("Claim shared-state writes for the active external supervisor") |
| 92 | + .requiredOption("--manager <id>", "stable external manager identifier") |
| 93 | + .option("--json", "emit machine-readable JSON", false) |
| 94 | + .action((options: DatabaseOutputOptions & { manager: string }) => |
| 95 | + runDatabaseOwnership(options), |
| 96 | + ); |
| 97 | + applyParentDefaultHelpAction(ownership); |
| 98 | + applyParentDefaultHelpAction(database); |
| 99 | +} |
0 commit comments