Files
openbot/scripts/reset-dev-state.ts
T
NBandClaude Opus 5.5 04643172f3 Make dev scripts reject unknown options (#1427)
* Make dev scripts reject unknown options

Add one strict parser (`node:util` `parseArgs`, `strict: true`) in
`scripts/script-arguments.ts`. `dev:seed`, `dev:reset`, `dev:verify`,
`dev:bench`, and `dev:automation` use it. `--help` prints the usage and
exits 0 before any side effect. An unknown option prints the usage and
exits 2. Before this change, `dev:reset --help` deleted the dev profiles.

`dev:reset` now refuses while a profile has a live `SingletonLock`. It
checks all profiles before it deletes one, and prints each target before
it deletes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Redact rejected arguments and detect a Windows dev profile lock

The error for an unknown argument repeats the typed value. Pass it
through redactText, because the value can be a token.

Chromium on Windows holds <profile>/lockfile, not SingletonLock. On
win32, treat lockfile as a live profile, so seed and reset do not
delete an open profile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Exit 2 when dev:automation gets an extra positional

cli.ts threw for an extra positional, and its catch set exit code 1.
readScriptArguments now takes maxPositionals and rejects the extra
positional with the usage and exit code 2, as for an unknown option.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 15:44:10 +02:00

109 lines
3.8 KiB
TypeScript

import { lstat, rm } from "node:fs/promises";
import { homedir } from "node:os";
import { dirname, parse, resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { createOpenBotLogger, toLogValue } from "@openbot/logging";
import { isMissingFileError } from "../src/backend/file-errors";
import { type DevelopmentProfile, developmentUserDataName } from "../src/main/development-profile";
import { resolveDevelopmentAppDataRoot } from "./development-state-paths";
import { readScriptArguments } from "./script-arguments";
import { cleanupSeedOwnedTransfers, isDevelopmentProfileActive } from "./seed-dev-state";
export { resolveDevelopmentAppDataRoot } from "./development-state-paths";
const logger = createOpenBotLogger("reset-dev-state");
const developmentProfiles = ["app", "test-client"] as const satisfies readonly DevelopmentProfile[];
const legacyDevelopmentStateNames = ["OpenBot Dev Host"] as const;
const USAGE = [
"Usage: bun run dev:reset",
"",
"Deletes the app, test-client, and legacy host development profiles. It refuses while a",
"dev app has one of them open. It takes no options.",
].join("\n");
export function developmentStatePaths(appDataRoot: string): string[] {
const safeRoot = resolve(appDataRoot);
if (safeRoot === parse(safeRoot).root) {
throw new Error("The application data root cannot be a filesystem root.");
}
return [
...developmentProfiles.map((profile) => developmentUserDataName(profile)),
...legacyDevelopmentStateNames,
].map((name) => {
const target = resolve(safeRoot, name);
if (dirname(target) !== safeRoot) {
throw new Error(`Unsafe OpenBot dev state path: ${target}`);
}
return target;
});
}
export async function resetDevelopmentState(
appDataRoot: string,
homeDirectory = homedir(),
beforeDelete: (statePath: string) => void = () => {},
): Promise<string[]> {
const existingPaths: string[] = [];
for (const statePath of developmentStatePaths(appDataRoot)) {
try {
await lstat(statePath);
} catch (error) {
if (isMissingFileError(error)) {
continue;
}
throw error;
}
existingPaths.push(statePath);
}
// Check every profile before deleting any, so a refusal leaves all of them as they were.
for (const statePath of existingPaths) {
if (await isDevelopmentProfileActive(statePath)) {
throw new Error(`Quit the OpenBot dev app before you reset its state. ${statePath} is open.`);
}
}
for (const statePath of existingPaths) {
beforeDelete(statePath);
await cleanupSeedOwnedTransfers(statePath, homeDirectory);
await rm(statePath, { force: true, maxRetries: 3, recursive: true, retryDelay: 100 });
}
return existingPaths;
}
async function main(): Promise<void> {
if (readScriptArguments({ usage: USAGE, options: {} }) === null) return;
const appDataRoot = resolveDevelopmentAppDataRoot();
const deletedPaths = await resetDevelopmentState(appDataRoot, homedir(), (statePath) => {
logger.info(`Deleting ${statePath}`);
});
if (deletedPaths.length === 0) {
logger.info("No OpenBot development state was found.");
} else {
logger.info("OpenBot development state reset.");
}
logger.info("Seed-owned transfer files were removed. Other shared files were not changed.");
logger.info("Agent workspaces, ~/.codex, and ~/.claude were not changed.");
// The downloaded provider CLIs are the computer's now, not this profile's, so a reset no longer
// takes them. Say so: it used to cost a fresh download of every one of them.
logger.info("Downloaded provider CLIs were not changed.");
}
function isMainModule(): boolean {
const entryPath = process.argv[1];
return entryPath !== undefined && import.meta.url === pathToFileURL(resolve(entryPath)).href;
}
if (isMainModule()) {
main().catch((error: unknown) => {
logger.error(toLogValue(error));
process.exitCode = 1;
});
}