codexuse-cli 2.3.1 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../lib/auto-roll-settings.ts","../../../lib/sqlite-db.ts","../../../lib/profile-manager.ts","../../../lib/codex-settings.ts","../../../lib/logger.ts","../../../lib/codex-cli-channel.ts","../../../lib/license-service.ts","../../../lib/license-secret.ts","../../../lib/license-guard.ts","../src/codex-cli.ts","../../../lib/codex-rpc.ts","../../../lib/codex-cli.ts","../../../lib/rate-limit-notifier.ts","../src/index.ts"],"sourcesContent":["export interface AutoRollSettings {\n enabled: boolean;\n warningThreshold: number;\n switchThreshold: number;\n}\n\nexport const DEFAULT_AUTO_ROLL_ENABLED = false;\nexport const DEFAULT_AUTO_ROLL_WARNING_THRESHOLD = 85;\nexport const DEFAULT_AUTO_ROLL_SWITCH_THRESHOLD = 95;\nexport const AUTO_ROLL_WARNING_MIN = 50;\n\nfunction clampNumber(value: number, min: number, max: number): number {\n return Math.min(max, Math.max(min, value));\n}\n\n/**\n * Normalize partial auto-roll settings into a safe shape with bounds.\n * Ensures switch threshold is always above warning and both live within [50, 100].\n */\nexport function normalizeAutoRollSettings(\n raw?: Partial<AutoRollSettings> | null,\n): AutoRollSettings {\n const enabled = typeof raw?.enabled === 'boolean' ? raw.enabled : DEFAULT_AUTO_ROLL_ENABLED;\n const rawWarning =\n typeof raw?.warningThreshold === 'number' && Number.isFinite(raw.warningThreshold)\n ? raw.warningThreshold\n : DEFAULT_AUTO_ROLL_WARNING_THRESHOLD;\n const rawSwitch =\n typeof raw?.switchThreshold === 'number' && Number.isFinite(raw.switchThreshold)\n ? raw.switchThreshold\n : DEFAULT_AUTO_ROLL_SWITCH_THRESHOLD;\n\n // First clamp warning to valid range, then ensure switch is above it\n const normalizedWarning = clampNumber(rawWarning, AUTO_ROLL_WARNING_MIN, 99);\n const normalizedSwitch = clampNumber(rawSwitch, normalizedWarning + 1, 100);\n\n return {\n enabled,\n warningThreshold: normalizedWarning,\n switchThreshold: normalizedSwitch,\n };\n}\n","import { readFileSync, writeFileSync, existsSync, mkdirSync, promises as fsPromises, statSync } from 'node:fs';\n\nimport path from 'node:path';\nimport { createRequire } from 'node:module';\nimport initSqlJs, {\n type Database as SqlJsDatabase,\n type Statement as SqlJsStatement,\n type SqlJsStatic,\n} from 'sql.js';\nimport {\n DEFAULT_AUTO_ROLL_ENABLED,\n DEFAULT_AUTO_ROLL_SWITCH_THRESHOLD,\n DEFAULT_AUTO_ROLL_WARNING_THRESHOLD,\n} from './auto-roll-settings';\n\ninterface SqliteRunResult {\n changes: number;\n}\n\nexport interface SqliteStatement {\n get(...params: unknown[]): Record<string, unknown> | undefined;\n all(...params: unknown[]): Array<Record<string, unknown>>;\n run(...params: unknown[]): SqliteRunResult;\n}\n\nexport interface SqliteDatabase {\n readonly open: boolean;\n prepare(sql: string): SqliteStatement;\n close(): void;\n}\n\nconst require = createRequire(__filename);\nconst wasmDir = path.dirname(require.resolve('sql.js/dist/sql-wasm.wasm'));\nconst sqlModulePromise: Promise<SqlJsStatic> = initSqlJs({\n locateFile: (file: string) => path.join(wasmDir, file),\n});\n\nclass SqliteWasmDatabase implements SqliteDatabase {\n private closed = false;\n\n constructor(private readonly driver: SqlJsDatabase, private readonly dbPath: string) { }\n\n get open(): boolean {\n return !this.closed;\n }\n\n prepare(sql: string): SqliteStatement {\n this.assertOpen();\n return new SqliteWasmStatement(this, sql);\n }\n\n createStatement(sql: string): SqlJsStatement {\n this.assertOpen();\n return this.driver.prepare(sql);\n }\n\n exec(sql: string): void {\n this.assertOpen();\n this.driver.exec(sql);\n }\n\n getRowsModified(): number {\n this.assertOpen();\n return this.driver.getRowsModified();\n }\n\n private persistTimeout: NodeJS.Timeout | null = null;\n\n async persist(): Promise<void> {\n this.assertOpen();\n ensureStorageDirExists(this.dbPath);\n\n if (this.persistTimeout) {\n clearTimeout(this.persistTimeout);\n }\n\n this.persistTimeout = setTimeout(async () => {\n try {\n const contents = Buffer.from(this.driver.export());\n await fsPromises.writeFile(this.dbPath, contents);\n this.persistTimeout = null;\n } catch (error) {\n console.error('Failed to persist database:', error);\n }\n }, 500);\n }\n\n close(): void {\n if (!this.closed) {\n // Synchronously flush any pending writes before closing\n if (this.persistTimeout) {\n clearTimeout(this.persistTimeout);\n this.persistTimeout = null;\n try {\n const contents = Buffer.from(this.driver.export());\n writeFileSync(this.dbPath, contents);\n } catch (error) {\n // Silently ignore ENOENT errors (directory already cleaned up, e.g., in tests)\n if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {\n console.error('Failed to flush database on close:', error);\n }\n }\n }\n this.driver.close();\n this.closed = true;\n }\n }\n\n async flushPersistence(): Promise<void> {\n if (this.persistTimeout) {\n clearTimeout(this.persistTimeout);\n this.persistTimeout = null;\n try {\n const contents = Buffer.from(this.driver.export());\n await fsPromises.writeFile(this.dbPath, contents);\n } catch (error) {\n console.error('Failed to flush database persistence:', error);\n }\n }\n }\n\n private assertOpen(): void {\n if (!this.open) {\n throw new Error('SQLite database connection is closed.');\n }\n }\n}\n\nexport async function flushDatabase(): Promise<void> {\n if (globalCache.cachedDbPromise) {\n const db = await globalCache.cachedDbPromise;\n // We know it's SqliteWasmDatabase in this implementation\n if (db instanceof SqliteWasmDatabase) {\n await db.flushPersistence();\n }\n }\n}\n\nclass SqliteWasmStatement implements SqliteStatement {\n constructor(private readonly database: SqliteWasmDatabase, private readonly sql: string) { }\n\n get(...params: unknown[]): Record<string, unknown> | undefined {\n const statement = this.database.createStatement(this.sql);\n try {\n bindParameters(statement, params);\n if (!statement.step()) {\n return undefined;\n }\n return statement.getAsObject();\n } finally {\n statement.free();\n }\n }\n\n all(...params: unknown[]): Array<Record<string, unknown>> {\n const rows: Array<Record<string, unknown>> = [];\n const statement = this.database.createStatement(this.sql);\n try {\n bindParameters(statement, params);\n while (statement.step()) {\n rows.push(statement.getAsObject());\n }\n } finally {\n statement.free();\n }\n return rows;\n }\n\n run(...params: unknown[]): SqliteRunResult {\n const statement = this.database.createStatement(this.sql);\n try {\n bindParameters(statement, params);\n // Execute statement once; INSERT/UPDATE statements don't produce rows.\n statement.step();\n } finally {\n statement.free();\n }\n const changes = this.database.getRowsModified();\n void this.database.persist();\n return { changes };\n }\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\nfunction normalizeNamedParameters(params: Record<string, unknown>): Record<string, unknown> {\n const normalized: Record<string, unknown> = {};\n for (const [key, rawValue] of Object.entries(params)) {\n if (key.startsWith('@') || key.startsWith(':') || key.startsWith('$')) {\n normalized[key] = rawValue;\n } else {\n normalized[`@${key}`] = rawValue;\n }\n }\n return normalized;\n}\n\nfunction bindParameters(statement: SqlJsStatement, args: unknown[]): void {\n if (args.length === 0) {\n return;\n }\n\n if (args.length === 1 && isPlainObject(args[0])) {\n statement.bind(normalizeNamedParameters(args[0]));\n return;\n }\n\n statement.bind(args as unknown[]);\n}\n\ntype SqliteCache = {\n cachedDbPath: string | null;\n cachedDbPromise: Promise<SqliteWasmDatabase> | null;\n};\n\nconst STORAGE_DIRECTORY_NAME = '.f86eb5e712267207';\nconst STORAGE_FILENAME = 'state-d64ce728d7a20214.sqlite';\n\nconst GLOBAL_CACHE_KEY = Symbol.for('codex.sqliteCache');\nconst globalCache: SqliteCache =\n (globalThis as Record<symbol, SqliteCache>)[GLOBAL_CACHE_KEY] ??\n ((globalThis as Record<symbol, SqliteCache>)[GLOBAL_CACHE_KEY] = {\n cachedDbPath: null,\n cachedDbPromise: null,\n });\n\nfunction resolveHomeDir(): string {\n const homeDir = process.env.HOME || process.env.USERPROFILE;\n if (!homeDir) {\n throw new Error('HOME directory is not set. Unable to initialize CodexUse storage.');\n }\n return homeDir;\n}\n\nfunction resolveStorageDirectory(): string {\n return path.join(resolveHomeDir(), STORAGE_DIRECTORY_NAME);\n}\n\nfunction resolveDbPath(): string {\n return path.join(resolveStorageDirectory(), STORAGE_FILENAME);\n}\n\nfunction ensureStorageDirExists(targetPath: string): void {\n mkdirSync(path.dirname(targetPath), { recursive: true });\n}\n\nasync function instantiateDatabase(dbPath: string): Promise<SqliteWasmDatabase> {\n try {\n const bakPath = `${dbPath}.bak`;\n if (existsSync(dbPath)) {\n const size = statSync(dbPath).size;\n if (size < 1024 && existsSync(bakPath)) {\n const bakSize = statSync(bakPath).size;\n if (bakSize > size) {\n const backup = readFileSync(bakPath);\n writeFileSync(dbPath, backup);\n console.warn(\"Detected near-empty SQLite file; restored from backup.\");\n }\n }\n }\n } catch (error) {\n console.warn(\"Failed to check/restore SQLite backup:\", error);\n }\n\n const SQL = await sqlModulePromise;\n let fileBuffer: Uint8Array | undefined;\n if (existsSync(dbPath)) {\n fileBuffer = new Uint8Array(readFileSync(dbPath));\n }\n const driver = fileBuffer ? new SQL.Database(fileBuffer) : new SQL.Database();\n const database = new SqliteWasmDatabase(driver, dbPath);\n database.exec('PRAGMA journal_mode = WAL;');\n database.exec('PRAGMA foreign_keys = ON;');\n runMigrations(database);\n database.persist();\n return database;\n}\n\nfunction readUserVersion(database: SqliteWasmDatabase): number {\n const result = database.prepare('PRAGMA user_version').get();\n const version = result?.user_version;\n return typeof version === 'number' ? version : 0;\n}\n\nfunction ensureChatTables(database: SqliteWasmDatabase): void {\n database.exec(`\n PRAGMA foreign_keys = ON;\n\n CREATE TABLE IF NOT EXISTS chat_threads (\n thread_key TEXT PRIMARY KEY,\n title TEXT,\n account_id TEXT,\n profile_name TEXT,\n project_label TEXT,\n project_path TEXT,\n source_path TEXT,\n started_at TEXT,\n ended_at TEXT,\n last_message_at TEXT,\n message_count INTEGER DEFAULT 0\n );\n\n CREATE TABLE IF NOT EXISTS chat_messages (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n thread_key TEXT NOT NULL,\n role TEXT,\n content TEXT NOT NULL,\n account_id TEXT,\n profile_name TEXT,\n created_at TEXT,\n FOREIGN KEY(thread_key) REFERENCES chat_threads(thread_key) ON DELETE CASCADE\n );\n\n CREATE INDEX IF NOT EXISTS idx_chat_messages_thread ON chat_messages(thread_key);\n CREATE INDEX IF NOT EXISTS idx_chat_messages_created ON chat_messages(created_at);\n\n CREATE TABLE IF NOT EXISTS chat_ingest_offsets (\n file_path TEXT PRIMARY KEY,\n offset INTEGER NOT NULL,\n mtime_ms INTEGER NOT NULL,\n size INTEGER NOT NULL\n );\n `);\n}\n\nfunction ensureSessionTables(database: SqliteWasmDatabase): void {\n database.exec(`\n PRAGMA foreign_keys = ON;\n\n CREATE TABLE IF NOT EXISTS sessions (\n id TEXT PRIMARY KEY,\n title TEXT,\n status TEXT,\n model TEXT,\n project_label TEXT,\n project_path TEXT,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL,\n last_message_at TEXT\n );\n\n CREATE TABLE IF NOT EXISTS tool_panels (\n id TEXT PRIMARY KEY,\n session_id TEXT NOT NULL,\n type TEXT NOT NULL,\n title TEXT,\n state TEXT,\n metadata TEXT,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL,\n FOREIGN KEY(session_id) REFERENCES sessions(id) ON DELETE CASCADE\n );\n\n CREATE INDEX IF NOT EXISTS idx_tool_panels_session ON tool_panels(session_id);\n CREATE INDEX IF NOT EXISTS idx_tool_panels_type ON tool_panels(type);\n\n CREATE TABLE IF NOT EXISTS panel_outputs (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n panel_id TEXT NOT NULL,\n session_id TEXT NOT NULL,\n type TEXT NOT NULL,\n data TEXT NOT NULL,\n created_at TEXT NOT NULL,\n FOREIGN KEY(panel_id) REFERENCES tool_panels(id) ON DELETE CASCADE,\n FOREIGN KEY(session_id) REFERENCES sessions(id) ON DELETE CASCADE\n );\n\n CREATE INDEX IF NOT EXISTS idx_panel_outputs_panel ON panel_outputs(panel_id);\n CREATE INDEX IF NOT EXISTS idx_panel_outputs_session ON panel_outputs(session_id);\n CREATE INDEX IF NOT EXISTS idx_panel_outputs_created ON panel_outputs(created_at);\n\n CREATE TABLE IF NOT EXISTS notes (\n id TEXT PRIMARY KEY,\n user_id TEXT,\n title TEXT NOT NULL,\n content TEXT NOT NULL,\n tags TEXT,\n is_favorited INTEGER NOT NULL DEFAULT 0,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL,\n synced_at TEXT\n );\n\n CREATE INDEX IF NOT EXISTS idx_notes_user ON notes(user_id);\n CREATE INDEX IF NOT EXISTS idx_notes_updated ON notes(updated_at);\n `);\n}\n\nfunction ensureUsageTables(database: SqliteWasmDatabase): void {\n database.exec(`\n CREATE TABLE IF NOT EXISTS usage (\n session_id TEXT PRIMARY KEY,\n rollout_path TEXT NOT NULL,\n project_path TEXT,\n model TEXT,\n input_tokens INTEGER NOT NULL,\n cached_input_tokens INTEGER NOT NULL,\n output_tokens INTEGER NOT NULL,\n reasoning_output_tokens INTEGER NOT NULL,\n total_tokens INTEGER NOT NULL,\n timestamp TEXT NOT NULL\n );\n\n CREATE INDEX IF NOT EXISTS idx_usage_timestamp ON usage(timestamp);\n `);\n}\n\nfunction runMigrations(database: SqliteWasmDatabase): void {\n let userVersion = readUserVersion(database);\n\n if (userVersion < 1) {\n database.exec(`\n CREATE TABLE IF NOT EXISTS profiles (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n name TEXT NOT NULL UNIQUE,\n data TEXT NOT NULL,\n account_id TEXT,\n workspace_id TEXT,\n workspace_name TEXT,\n email TEXT,\n auth_method TEXT,\n created_at TEXT,\n updated_at TEXT\n );\n\n CREATE UNIQUE INDEX IF NOT EXISTS idx_profiles_account_workspace\n ON profiles(account_id, workspace_id)\n WHERE account_id IS NOT NULL;\n\n PRAGMA user_version = 1;\n `);\n userVersion = 1;\n }\n\n if (userVersion < 2) {\n database.exec(`PRAGMA user_version = 2;`);\n userVersion = 2;\n }\n\n if (userVersion < 3) {\n database.exec(`PRAGMA user_version = 3;`);\n userVersion = 3;\n }\n\n if (userVersion < 4) {\n let legacySettings: Record<string, unknown> | null = null;\n\n try {\n const hasKvStore = Boolean(\n database.prepare(\n \"SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'kv_store'\",\n ).get() as { name?: string } | undefined,\n );\n if (hasKvStore) {\n const row = database\n .prepare(\"SELECT value FROM kv_store WHERE key = 'settings'\")\n .get() as { value?: string } | undefined;\n if (row?.value) {\n const parsed = JSON.parse(row.value) as Record<string, unknown>;\n if (parsed && typeof parsed === 'object') {\n legacySettings = parsed;\n }\n }\n }\n } catch (error) {\n console.warn(\"Failed to read legacy Codex settings:\", error);\n }\n\n database.exec(`\n PRAGMA foreign_keys = OFF;\n\n DROP TABLE IF EXISTS rate_limit_snapshots;\n DROP TABLE IF EXISTS chat_messages;\n DROP TABLE IF EXISTS chat_threads;\n DROP TABLE IF EXISTS kv_store;\n\n CREATE TABLE IF NOT EXISTS app_settings (\n id INTEGER PRIMARY KEY CHECK (id = 1),\n last_profile_name TEXT,\n license_key TEXT,\n purchase_email TEXT,\n last_verified_at TEXT,\n next_check_at TEXT,\n last_verification_error TEXT,\n status TEXT\n );\n\n INSERT OR IGNORE INTO app_settings (id) VALUES (1);\n\n PRAGMA foreign_keys = ON;\n `);\n\n if (legacySettings) {\n const license = legacySettings.license as Record<string, unknown> | undefined;\n database.prepare(\n `UPDATE app_settings\n SET last_profile_name = @lastProfileName,\n license_key = @licenseKey,\n purchase_email = @purchaseEmail,\n last_verified_at = @lastVerifiedAt,\n next_check_at = @nextCheckAt,\n last_verification_error = @lastVerificationError,\n status = @status\n WHERE id = 1`,\n ).run({\n lastProfileName: typeof legacySettings.lastProfileName === 'string'\n ? legacySettings.lastProfileName\n : null,\n licenseKey: license && typeof license.licenseKey === 'string' ? license.licenseKey : null,\n purchaseEmail: license && typeof license.purchaseEmail === 'string' ? license.purchaseEmail : null,\n lastVerifiedAt: license && typeof license.lastVerifiedAt === 'string' ? license.lastVerifiedAt : null,\n nextCheckAt: license && typeof license.nextCheckAt === 'string' ? license.nextCheckAt : null,\n lastVerificationError:\n license && typeof license.lastVerificationError === 'string'\n ? license.lastVerificationError\n : null,\n status: license && typeof license.status === 'string' ? license.status : null,\n });\n }\n\n database.exec(`PRAGMA user_version = 4;`);\n userVersion = 4;\n }\n\n if (userVersion < 5) {\n database.exec(`\n ALTER TABLE app_settings ADD COLUMN auto_roll_enabled INTEGER;\n ALTER TABLE app_settings ADD COLUMN auto_roll_warning_threshold INTEGER;\n ALTER TABLE app_settings ADD COLUMN auto_roll_switch_threshold INTEGER;\n\n PRAGMA user_version = 5;\n `);\n userVersion = 5;\n }\n\n if (userVersion < 6) {\n database.exec(`\n CREATE TABLE IF NOT EXISTS app_kv (\n key TEXT PRIMARY KEY,\n value TEXT NOT NULL\n );\n `);\n\n let appSettingsRow: Record<string, unknown> | null = null;\n try {\n const hasAppSettings = Boolean(\n database.prepare(\n \"SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'app_settings'\",\n ).get() as { name?: string } | undefined,\n );\n if (hasAppSettings) {\n appSettingsRow = database\n .prepare(\n `SELECT last_profile_name, license_key, purchase_email,\n last_verified_at, next_check_at, last_verification_error, status,\n auto_roll_enabled, auto_roll_warning_threshold, auto_roll_switch_threshold\n FROM app_settings\n WHERE id = 1`,\n )\n .get() as Record<string, unknown> | null;\n }\n } catch (error) {\n console.warn(\"Failed to read legacy app_settings row:\", error);\n }\n\n try {\n const upsertKv = database.prepare(\"INSERT OR REPLACE INTO app_kv (key, value) VALUES (@key, @value)\");\n\n if (appSettingsRow) {\n const lastProfile = typeof appSettingsRow.last_profile_name === 'string'\n ? appSettingsRow.last_profile_name.trim()\n : '';\n if (lastProfile) {\n upsertKv.run({ key: 'last_profile_name', value: JSON.stringify(lastProfile) });\n }\n\n const license = {\n licenseKey: typeof appSettingsRow.license_key === 'string' ? appSettingsRow.license_key : null,\n purchaseEmail: typeof appSettingsRow.purchase_email === 'string' ? appSettingsRow.purchase_email : null,\n lastVerifiedAt: typeof appSettingsRow.last_verified_at === 'string' ? appSettingsRow.last_verified_at : null,\n nextCheckAt: typeof appSettingsRow.next_check_at === 'string' ? appSettingsRow.next_check_at : null,\n lastVerificationError:\n typeof appSettingsRow.last_verification_error === 'string'\n ? appSettingsRow.last_verification_error\n : null,\n status: typeof appSettingsRow.status === 'string' ? appSettingsRow.status : null,\n };\n const hasLicenseData = Boolean(\n license.licenseKey ??\n license.purchaseEmail ??\n license.lastVerifiedAt ??\n license.nextCheckAt ??\n license.lastVerificationError ??\n license.status,\n );\n if (hasLicenseData) {\n upsertKv.run({ key: 'license', value: JSON.stringify(license) });\n }\n\n const hasAutoRollData =\n typeof appSettingsRow.auto_roll_enabled === 'number' ||\n typeof appSettingsRow.auto_roll_warning_threshold === 'number' ||\n typeof appSettingsRow.auto_roll_switch_threshold === 'number';\n if (hasAutoRollData) {\n const warning =\n typeof appSettingsRow.auto_roll_warning_threshold === 'number'\n ? appSettingsRow.auto_roll_warning_threshold\n : DEFAULT_AUTO_ROLL_WARNING_THRESHOLD;\n const switchThreshold =\n typeof appSettingsRow.auto_roll_switch_threshold === 'number'\n ? appSettingsRow.auto_roll_switch_threshold\n : DEFAULT_AUTO_ROLL_SWITCH_THRESHOLD;\n const autoRoll = {\n enabled: Boolean(appSettingsRow.auto_roll_enabled ?? DEFAULT_AUTO_ROLL_ENABLED),\n warningThreshold: warning,\n switchThreshold,\n };\n upsertKv.run({ key: 'auto_roll', value: JSON.stringify(autoRoll) });\n }\n }\n\n try {\n database.exec(\"DROP TABLE IF EXISTS app_settings;\");\n } catch (error) {\n console.warn(\"Failed to drop legacy app_settings table:\", error);\n }\n } catch (error) {\n console.warn(\"Failed to migrate app_settings to app_kv:\", error);\n }\n\n database.exec(`PRAGMA user_version = 6;`);\n userVersion = 6;\n }\n\n if (userVersion < 7) {\n try {\n const columns = database\n .prepare(\"PRAGMA table_info(profiles)\")\n .all() as Array<{ name?: string }> | undefined;\n const hasWorkspaceId = Boolean(columns?.some(column => column.name === 'workspace_id'));\n const hasWorkspaceName = Boolean(columns?.some(column => column.name === 'workspace_name'));\n\n if (!hasWorkspaceId) {\n database.exec(`ALTER TABLE profiles ADD COLUMN workspace_id TEXT;`);\n }\n if (!hasWorkspaceName) {\n database.exec(`ALTER TABLE profiles ADD COLUMN workspace_name TEXT;`);\n }\n } catch (error) {\n console.warn('Failed to add workspace columns during migration:', error);\n }\n\n database.exec(`\n DROP INDEX IF EXISTS idx_profiles_account;\n DROP INDEX IF EXISTS idx_profiles_account_workspace;\n\n CREATE UNIQUE INDEX IF NOT EXISTS idx_profiles_account_workspace\n ON profiles(account_id, workspace_id)\n WHERE account_id IS NOT NULL;\n `);\n\n try {\n const rows = database\n .prepare('SELECT name, workspace_id FROM profiles')\n .all() as Array<{ name?: string; workspace_id?: string }>;\n const update = database.prepare(\n 'UPDATE profiles SET workspace_id = @workspaceId WHERE name = @name AND (workspace_id IS NULL OR workspace_id = \\'\\')',\n );\n for (const row of rows) {\n const name = typeof row.name === 'string' ? row.name : null;\n if (!name) {\n continue;\n }\n const existingWorkspace = typeof row.workspace_id === 'string' ? row.workspace_id.trim() : '';\n if (existingWorkspace) {\n continue;\n }\n update.run({ name, workspaceId: '__default__' });\n }\n } catch (error) {\n console.warn('Failed to backfill workspace_id during migration:', error);\n }\n\n database.exec(`PRAGMA user_version = 7;`);\n userVersion = 7;\n }\n\n if (userVersion < 8) {\n ensureChatTables(database);\n database.exec(`PRAGMA user_version = 8;`);\n userVersion = 8;\n }\n\n if (userVersion < 9) {\n ensureSessionTables(database);\n database.exec(`PRAGMA user_version = 9;`);\n userVersion = 9;\n }\n\n if (userVersion < 10) {\n ensureUsageTables(database);\n database.exec(`PRAGMA user_version = 10;`);\n userVersion = 10;\n }\n\n if (userVersion < 11) {\n try {\n database.exec(`ALTER TABLE usage ADD COLUMN model TEXT;`);\n } catch {\n // ignore if column already exists\n }\n database.exec(`PRAGMA user_version = 11;`);\n userVersion = 11;\n }\n\n // Make sure foreign keys are restored if a migration temporarily disabled them.\n try {\n database.exec('PRAGMA foreign_keys = ON;');\n } catch {\n // best-effort; errors are logged elsewhere\n }\n\n // Idempotent guard: make sure chat tables exist even if user_version was already bumped.\n ensureChatTables(database);\n ensureSessionTables(database);\n ensureUsageTables(database);\n}\n\nexport async function getDatabase(): Promise<SqliteDatabase> {\n const dbPath = resolveDbPath();\n\n if (!globalCache.cachedDbPromise || globalCache.cachedDbPath !== dbPath) {\n if (globalCache.cachedDbPromise) {\n try {\n const previous = await globalCache.cachedDbPromise;\n previous.close();\n } catch {\n // ignore errors closing old database\n }\n }\n globalCache.cachedDbPath = dbPath;\n globalCache.cachedDbPromise = instantiateDatabase(dbPath);\n }\n\n const db = await globalCache.cachedDbPromise;\n try {\n // Ensure migrations run even if the database was opened before this version of the app.\n runMigrations(db as SqliteWasmDatabase);\n } catch (error) {\n console.warn(\"Failed to ensure migrations:\", error);\n }\n return db;\n}\n\nexport async function resetDatabaseConnection(): Promise<void> {\n if (globalCache.cachedDbPromise) {\n try {\n const db = await globalCache.cachedDbPromise;\n db.close();\n } catch {\n // ignore failures during reset\n }\n }\n globalCache.cachedDbPromise = null;\n globalCache.cachedDbPath = null;\n}\n\n/**\n * @internal - exposed for tests to assert the resolved database paths.\n */\nexport function __debugResolveDbPath(): string {\n return resolveDbPath();\n}\n","import { promises as fs } from 'fs';\nimport path from 'path';\nimport { join } from 'path';\nimport { parse, stringify } from '@iarna/toml';\nimport {\n Profile,\n ProfileData,\n ActiveAuth,\n ProfileMetadata,\n OrganizationInfo,\n SubscriptionInfo,\n TokenAlert,\n TokenIssue,\n TokenStatus,\n} from './types';\nimport { getLastProfileName, persistLastProfileName } from './codex-settings';\nimport { logError, logWarn } from './logger';\n\nconst TOKEN_EXPIRING_SOON_WINDOW_MS = 15 * 60 * 1000;\nexport const REFRESH_TOKEN_REDEEMED_SNIPPET = 'refresh token was already used';\nexport const REFRESH_TOKEN_REDEEMED_REASON =\n 'Your access token could not be refreshed because your refresh token was already used. Please log out and sign in again.';\nconst AUTH_BACKUP_MISSING_MARKER = '__codexuse_missing_auth__';\nconst DEFAULT_WORKSPACE_ID = '__default__';\nconst PROFILE_RECORD_FILENAME = 'profile.json';\n\nexport function detectRefreshTokenRedeemedMessage(output: string | Buffer | null | undefined): string | null {\n if (!output) {\n return null;\n }\n\n const text = output.toString();\n if (!text) {\n return null;\n }\n\n const normalized = text\n .toLowerCase()\n .replace(/\\s+/g, ' ');\n if (!normalized.includes(REFRESH_TOKEN_REDEEMED_SNIPPET)) {\n return null;\n }\n\n const trimmed = text.trim();\n return trimmed.length > 0 ? trimmed : REFRESH_TOKEN_REDEEMED_REASON;\n}\n\ntype ProfileRecord = {\n name: string;\n displayName: string | null;\n data: ProfileData;\n metadata?: ProfileMetadata;\n accountId: string | null;\n workspaceId: string | null;\n workspaceName: string | null;\n email: string | null;\n authMethod: string | null;\n createdAt: string | null;\n updatedAt: string | null;\n};\n\ntype DbProfileRow = {\n name?: string | null;\n data?: string | null;\n account_id?: string | null;\n workspace_id?: string | null;\n workspace_name?: string | null;\n email?: string | null;\n auth_method?: string | null;\n created_at?: string | null;\n updated_at?: string | null;\n};\n\nexport class ProfileManager {\n private codexDir: string;\n private profilesDir: string;\n private profileHomesRoot: string;\n private migrationsDir: string;\n private profileMigrationMarker: string;\n private activeAuth: string;\n private activeAuthBackup: string;\n private lastActiveAuthErrorSignature: string | null;\n private authSwapLock: Promise<void>;\n constructor() {\n const homeDir = process.env.HOME || process.env.USERPROFILE || '';\n this.codexDir = join(homeDir, '.codex');\n this.profilesDir = join(this.codexDir, 'profiles');\n this.profileHomesRoot = join(this.codexDir, 'profile-homes');\n this.migrationsDir = join(this.codexDir, 'migrations');\n this.profileMigrationMarker = join(this.migrationsDir, 'profiles-v1');\n this.activeAuth = join(this.codexDir, 'auth.json');\n this.activeAuthBackup = `${this.activeAuth}.swap`;\n this.lastActiveAuthErrorSignature = null;\n this.authSwapLock = Promise.resolve();\n }\n\n private computeExpiryIso(data: ProfileData | null | undefined): string | undefined {\n if (!data) {\n return undefined;\n }\n if (typeof data.expired === 'string' && data.expired.trim()) {\n const parsed = Date.parse(data.expired);\n if (!Number.isNaN(parsed)) {\n return new Date(parsed).toISOString();\n }\n }\n const expiresIn = typeof data.expires_in === 'number' && Number.isFinite(data.expires_in)\n ? data.expires_in\n : undefined;\n const issuedMs =\n typeof data.timestamp === 'number' && Number.isFinite(data.timestamp)\n ? data.timestamp\n : undefined;\n const issuedAt = issuedMs ? issuedMs : undefined;\n const baseMs = issuedAt ?? Date.now();\n if (expiresIn && expiresIn > 0) {\n return new Date(baseMs + expiresIn * 1000).toISOString();\n }\n return undefined;\n }\n\n private normalizeProfileName(name: string): string {\n if (typeof name !== 'string') {\n throw new Error('Profile name is required');\n }\n\n const trimmed = name.trim();\n if (!trimmed) {\n throw new Error('Profile name must not be empty.');\n }\n\n if (trimmed === '.' || trimmed === '..') {\n throw new Error(`Profile name '${name}' is not allowed.`);\n }\n\n if (/[\\\\/]/.test(trimmed)) {\n throw new Error(\"Profile name cannot contain path separators.\");\n }\n\n if (trimmed.includes('\\0')) {\n throw new Error('Profile name contains invalid characters.');\n }\n\n return trimmed;\n }\n\n private isNotFoundError(error: unknown): boolean {\n return Boolean(\n error &&\n typeof error === 'object' &&\n 'code' in error &&\n (error as NodeJS.ErrnoException).code === 'ENOENT',\n );\n }\n\n private async readPreferredProfileName(): Promise<string | null> {\n try {\n return await getLastProfileName();\n } catch (error) {\n logWarn('Failed to read preferred profile name:', error);\n return null;\n }\n }\n\n private async persistPreferredProfileName(name: string | null): Promise<void> {\n try {\n await persistLastProfileName(name);\n } catch (error) {\n logWarn('Failed to persist preferred profile name:', error);\n }\n }\n\n private getProfileRecordPath(profileName: string): string {\n return join(this.getProfileHomePath(profileName), PROFILE_RECORD_FILENAME);\n }\n\n private parseProfileRecordPayload(profileName: string, parsed: Record<string, unknown>): ProfileRecord | null {\n if (!parsed || typeof parsed !== 'object') {\n return null;\n }\n\n const dataRaw = parsed['data'];\n let data: ProfileData | null = null;\n if (dataRaw && typeof dataRaw === 'object') {\n data = dataRaw as ProfileData;\n } else if (typeof dataRaw === 'string') {\n try {\n data = JSON.parse(dataRaw) as ProfileData;\n } catch (error) {\n logWarn(`Failed to parse data payload for profile '${profileName}':`, error);\n return null;\n }\n } else {\n return null;\n }\n const createdAt =\n typeof parsed['createdAt'] === 'string'\n ? parsed['createdAt'] as string\n : typeof parsed['created_at'] === 'string'\n ? parsed['created_at'] as string\n : null;\n const updatedAt =\n typeof parsed['updatedAt'] === 'string'\n ? parsed['updatedAt'] as string\n : typeof parsed['updated_at'] === 'string'\n ? parsed['updated_at'] as string\n : null;\n\n const metadata =\n parsed['metadata'] && typeof parsed['metadata'] === 'object'\n ? parsed['metadata'] as ProfileMetadata\n : undefined;\n\n const displayName =\n typeof parsed['displayName'] === 'string'\n ? parsed['displayName'] as string\n : typeof parsed['display_name'] === 'string'\n ? parsed['display_name'] as string\n : null;\n\n return {\n name: profileName,\n displayName,\n data,\n metadata,\n accountId:\n typeof parsed['accountId'] === 'string'\n ? parsed['accountId']\n : typeof parsed['account_id'] === 'string'\n ? parsed['account_id']\n : null,\n workspaceId:\n typeof parsed['workspaceId'] === 'string'\n ? parsed['workspaceId']\n : typeof parsed['workspace_id'] === 'string'\n ? parsed['workspace_id']\n : null,\n workspaceName:\n typeof parsed['workspaceName'] === 'string'\n ? parsed['workspaceName']\n : typeof parsed['workspace_name'] === 'string'\n ? parsed['workspace_name']\n : null,\n email:\n typeof parsed['email'] === 'string'\n ? parsed['email']\n : typeof parsed['email'] === 'boolean'\n ? null\n : null,\n authMethod:\n typeof parsed['authMethod'] === 'string'\n ? parsed['authMethod']\n : typeof parsed['auth_method'] === 'string'\n ? parsed['auth_method']\n : null,\n createdAt,\n updatedAt,\n };\n }\n\n private decodeProfileRecord(profileName: string, raw: string, sourceLabel: string): ProfileRecord | null {\n try {\n const parsed = JSON.parse(raw) as Record<string, unknown>;\n return this.parseProfileRecordPayload(profileName, parsed);\n } catch (error) {\n logWarn(`Failed to parse profile record '${profileName}' from '${sourceLabel}':`, error);\n return null;\n }\n }\n\n private async recoverProfileRecord(recordPath: string, profileName: string): Promise<ProfileRecord | null> {\n const candidates = [`${recordPath}.bak`, `${recordPath}.tmp`];\n\n for (const candidate of candidates) {\n let raw: string;\n try {\n raw = await fs.readFile(candidate, 'utf8');\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to read backup for '${profileName}' from '${candidate}':`, error);\n }\n continue;\n }\n\n const decoded = this.decodeProfileRecord(profileName, raw, candidate);\n if (!decoded) {\n continue;\n }\n\n try {\n await this.writeProfileRecord(decoded);\n } catch (error) {\n logWarn(`Failed to restore profile '${profileName}' from '${candidate}':`, error);\n }\n return decoded;\n }\n\n return null;\n }\n\n private async readProfileRecord(profileName: string): Promise<ProfileRecord | null> {\n const recordPath = this.getProfileRecordPath(profileName);\n try {\n const raw = await fs.readFile(recordPath, 'utf8');\n const decoded = this.decodeProfileRecord(profileName, raw, recordPath);\n if (decoded) {\n return decoded;\n }\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to read profile record '${profileName}':`, error);\n }\n }\n\n return this.recoverProfileRecord(recordPath, profileName);\n }\n\n private async listProfileRecords(): Promise<ProfileRecord[]> {\n const records: ProfileRecord[] = [];\n let entries: Array<string> = [];\n try {\n entries = await fs.readdir(this.profileHomesRoot);\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn('Failed to list profile homes:', error);\n }\n return records;\n }\n\n for (const entry of entries) {\n const homePath = join(this.profileHomesRoot, entry);\n try {\n const stat = await fs.stat(homePath);\n if (!stat.isDirectory()) {\n continue;\n }\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to inspect profile home '${entry}':`, error);\n }\n continue;\n }\n\n try {\n const normalizedName = this.normalizeProfileName(entry);\n const record = await this.readProfileRecord(normalizedName);\n if (record) {\n records.push(record);\n continue;\n }\n\n // Best-effort recovery for homes missing profile.json by rehydrating from auth.json.\n const authPath = join(homePath, 'auth.json');\n try {\n const authRaw = await fs.readFile(authPath, 'utf8');\n const authData = JSON.parse(authRaw) as ActiveAuth;\n const normalized = this.normalizeProfileData(authData);\n const metadata = this.extractProfileMetadata(normalized);\n await this.persistProfileRecord(normalizedName, normalized, metadata);\n const restored = await this.readProfileRecord(normalizedName);\n if (restored) {\n records.push(restored);\n }\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to restore profile record for '${normalizedName}' from auth.json:`, error);\n }\n }\n } catch (error) {\n logWarn(`Skipping invalid profile home '${entry}':`, error);\n }\n }\n\n return records;\n }\n\n private async snapshotExistingProfileRecord(recordPath: string): Promise<void> {\n try {\n const existing = await fs.readFile(recordPath, 'utf8');\n if (existing && existing.trim()) {\n await this.writeAtomic(`${recordPath}.bak`, existing);\n }\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to snapshot existing profile record '${recordPath}':`, error);\n }\n }\n }\n\n private async writeProfileRecord(record: ProfileRecord): Promise<void> {\n const recordPath = this.getProfileRecordPath(record.name);\n const payload = {\n name: record.name,\n display_name: record.displayName,\n data: record.data,\n metadata: record.metadata,\n account_id: record.accountId,\n workspace_id: record.workspaceId,\n workspace_name: record.workspaceName,\n email: record.email,\n auth_method: record.authMethod,\n created_at: record.createdAt,\n updated_at: record.updatedAt,\n };\n const serialized = `${JSON.stringify(payload, null, 2)}\\n`;\n await this.snapshotExistingProfileRecord(recordPath);\n await this.writeAtomic(recordPath, serialized);\n try {\n await this.writeAtomic(`${recordPath}.bak`, serialized);\n } catch (error) {\n logWarn(`Failed to persist profile backup for '${record.name}':`, error);\n }\n }\n\n private resolveAuthMethod(data: ProfileData | null | undefined): string {\n const raw = typeof data?.auth_method === 'string' ? data.auth_method.trim().toLowerCase() : '';\n return raw || 'codex-cli';\n }\n\n private async readActiveAuthFile(): Promise<ActiveAuth | null> {\n try {\n const raw = await fs.readFile(this.activeAuth, 'utf8');\n const trimmed = raw.trim();\n if (!trimmed) {\n return null;\n }\n return JSON.parse(trimmed) as ActiveAuth;\n } catch (error) {\n if (this.isNotFoundError(error)) {\n return null;\n }\n const message = error instanceof Error ? error.message : 'unknown error';\n const signature = typeof message === 'string' ? `${message}:${this.activeAuth}` : this.activeAuth;\n if (this.lastActiveAuthErrorSignature !== signature) {\n logWarn('Failed to read active auth file:', error);\n this.lastActiveAuthErrorSignature = signature;\n }\n return null;\n }\n }\n\n private async getActiveAuthAccountId(): Promise<string | undefined> {\n const activeAuth = await this.readActiveAuthFile();\n if (!activeAuth) {\n return undefined;\n }\n return this.getAccountIdFromData(activeAuth);\n }\n\n /**\n * Decode a JWT payload into an object without validating the signature.\n */\n private decodeJwtPayload(token?: string | null): Record<string, unknown> | null {\n if (!token || typeof token !== 'string') {\n return null;\n }\n\n const segments = token.split('.');\n if (segments.length < 2) {\n return null;\n }\n\n try {\n const payload = Buffer.from(segments[1], 'base64url').toString('utf8');\n return JSON.parse(payload);\n } catch {\n return null;\n }\n }\n\n private toIsoStringFromSeconds(seconds?: number): string | undefined {\n if (typeof seconds !== 'number' || Number.isNaN(seconds)) {\n return undefined;\n }\n\n try {\n return new Date(seconds * 1000).toISOString();\n } catch {\n return undefined;\n }\n }\n\n private normalizeEmailCandidate(value: unknown): string | undefined {\n if (typeof value !== 'string') {\n return undefined;\n }\n\n const trimmed = value.trim();\n if (!trimmed || trimmed.includes(' ')) {\n return undefined;\n }\n\n const atIndex = trimmed.indexOf('@');\n if (atIndex <= 0 || atIndex === trimmed.length - 1) {\n return undefined;\n }\n\n const domain = trimmed.slice(atIndex + 1);\n if (!domain || !domain.includes('.')) {\n return undefined;\n }\n\n return trimmed;\n }\n\n private pickFirstEmail(candidates: Array<string | undefined>): string | undefined {\n for (const candidate of candidates) {\n const normalized = this.normalizeEmailCandidate(candidate);\n if (normalized) {\n return normalized;\n }\n }\n return undefined;\n }\n\n private findEmailInObject(value: unknown, seen = new Set<unknown>()): string | undefined {\n if (!value || typeof value !== 'object') {\n return undefined;\n }\n\n if (seen.has(value)) {\n return undefined;\n }\n seen.add(value);\n\n if (Array.isArray(value)) {\n for (const entry of value) {\n const nested = this.findEmailInObject(entry, seen);\n if (nested) {\n return nested;\n }\n }\n return undefined;\n }\n\n for (const [key, entry] of Object.entries(value as Record<string, unknown>)) {\n if (typeof entry === 'string') {\n const normalized = this.normalizeEmailCandidate(entry);\n const lowerKey = key.toLowerCase();\n const keyHintsAtEmail =\n lowerKey.includes('email') ||\n lowerKey.includes('contact') ||\n lowerKey.includes('username') ||\n lowerKey.includes('login');\n\n if (normalized && (keyHintsAtEmail || entry.includes('@'))) {\n return normalized;\n }\n }\n\n if (entry && typeof entry === 'object') {\n const nested = this.findEmailInObject(entry, seen);\n if (nested) {\n return nested;\n }\n }\n }\n\n return undefined;\n }\n\n private resolveProfileEmail(data: ProfileData, metadata?: ProfileMetadata): string | undefined {\n const direct = this.normalizeEmailCandidate(data.email);\n if (direct) {\n return direct;\n }\n\n if (metadata?.email) {\n return metadata.email;\n }\n\n return this.findEmailInObject(data);\n }\n\n private evaluateTokenStatus(data: ProfileData, metadata?: ProfileMetadata): TokenStatus {\n const expiresAt = metadata?.tokenExpiresAt ?? this.computeExpiryIso(data);\n const parsedExpiry = expiresAt ? Date.parse(expiresAt) : Number.NaN;\n const hasExpiry = !Number.isNaN(parsedExpiry);\n const tokenAlert = data.tokenAlert;\n\n if (!data || typeof data !== 'object' || Object.keys(data).length === 0) {\n return {\n state: 'missing',\n reason: 'No authentication data saved for this profile.',\n expiresAt,\n issue: 'auth-missing',\n requiresUserAction: true,\n };\n }\n\n if (tokenAlert?.issue) {\n const reason = typeof tokenAlert.reason === 'string' && tokenAlert.reason.trim().length > 0\n ? tokenAlert.reason.trim()\n : tokenAlert.issue === 'refresh-redeemed'\n ? REFRESH_TOKEN_REDEEMED_REASON\n : 'Authentication needs attention. Re-login this profile.';\n const issue = (tokenAlert.issue as TokenIssue) ?? 'auth-missing';\n return {\n state: 'invalid',\n reason,\n expiresAt,\n issue,\n requiresUserAction: true,\n };\n }\n\n if (!data.access_token) {\n return {\n state: 'missing',\n reason: 'Access token is missing. Re-authenticate with Codex CLI.',\n expiresAt,\n issue: 'access-missing',\n requiresUserAction: true,\n };\n }\n\n if (!this.decodeJwtPayload(data.access_token)) {\n return {\n state: 'invalid',\n reason: 'Access token is corrupted or not a valid JWT.',\n expiresAt,\n issue: 'access-invalid',\n requiresUserAction: true,\n };\n }\n\n const refreshToken = typeof data.refresh_token === 'string' ? data.refresh_token.trim() : '';\n if (!refreshToken) {\n return {\n state: 'missing',\n reason: 'Refresh token is missing. Run Codex login again.',\n expiresAt,\n issue: 'refresh-missing',\n requiresUserAction: true,\n };\n }\n\n if (hasExpiry) {\n const diff = parsedExpiry - Date.now();\n if (diff <= 0) {\n return {\n state: 'expiring',\n reason: 'Access token expired.',\n expiresAt,\n accessTokenExpired: true,\n issue: 'access-expired',\n requiresUserAction: false,\n };\n }\n\n if (diff <= TOKEN_EXPIRING_SOON_WINDOW_MS) {\n return {\n state: 'expiring',\n reason: 'Access token expires soon.',\n expiresAt,\n issue: 'access-expiring',\n requiresUserAction: false,\n };\n }\n }\n\n return {\n state: 'ok',\n expiresAt,\n requiresUserAction: false,\n };\n }\n\n private extractProfileMetadata(data: ProfileData): ProfileMetadata | undefined {\n const idPayload = this.decodeJwtPayload(data.id_token) as Record<string, unknown> | null;\n const accessPayload = this.decodeJwtPayload(data.access_token) as Record<string, unknown> | null;\n\n if (!idPayload && !accessPayload) {\n return undefined;\n }\n\n const authInfo = (idPayload?.['https://api.openai.com/auth'] ??\n accessPayload?.['https://api.openai.com/auth']) as Record<string, unknown> | undefined;\n\n const profileInfo = accessPayload?.['https://api.openai.com/profile'] as Record<string, unknown> | undefined;\n\n const getNumber = (obj: Record<string, unknown> | null | undefined, key: string): number | undefined =>\n typeof obj?.[key] === 'number' ? (obj[key] as number) : undefined;\n\n const getString = (obj: Record<string, unknown> | undefined | null, key: string): string | undefined =>\n typeof obj?.[key] === 'string' ? (obj[key] as string) : undefined;\n\n const exp = getNumber(idPayload, 'exp') ?? getNumber(accessPayload, 'exp');\n const iat = getNumber(idPayload, 'iat') ?? getNumber(accessPayload, 'iat');\n const authTime = getNumber(idPayload, 'auth_time') ?? getNumber(accessPayload, 'auth_time');\n\n const organizationsRaw = Array.isArray(authInfo?.['organizations'])\n ? (authInfo?.['organizations'] as unknown[])\n : undefined;\n\n const organizations = organizationsRaw\n ? organizationsRaw\n .map((org): OrganizationInfo => {\n const record = org as Record<string, unknown> | undefined;\n return {\n id: getString(record, 'id'),\n title: getString(record, 'title'),\n role: getString(record, 'role'),\n isDefault: typeof record?.['is_default'] === 'boolean' ? (record?.['is_default'] as boolean) : undefined,\n };\n })\n .filter(\n org =>\n org.id ||\n org.title ||\n org.role ||\n typeof org.isDefault === 'boolean'\n )\n : undefined;\n\n const groups = Array.isArray(authInfo?.['groups'])\n ? (authInfo?.['groups'] as unknown[]).filter((group): group is string => typeof group === 'string')\n : undefined;\n\n const emailVerified =\n typeof profileInfo?.['email_verified'] === 'boolean'\n ? (profileInfo?.['email_verified'] as boolean)\n : typeof idPayload?.['email_verified'] === 'boolean'\n ? (idPayload?.['email_verified'] as boolean)\n : typeof accessPayload?.['email_verified'] === 'boolean'\n ? (accessPayload?.['email_verified'] as boolean)\n : undefined;\n\n const email =\n this.pickFirstEmail([\n getString(profileInfo, 'email'),\n getString(profileInfo, 'email_address'),\n getString(profileInfo, 'primary_email'),\n getString(profileInfo, 'default_email'),\n getString(profileInfo, 'contact_email'),\n ]) ??\n this.findEmailInObject(profileInfo) ??\n this.pickFirstEmail([\n getString(authInfo, 'user_email'),\n getString(authInfo, 'email'),\n getString(authInfo, 'userEmail'),\n getString(authInfo, 'chatgpt_user_email'),\n ]) ??\n this.findEmailInObject(authInfo) ??\n this.pickFirstEmail([\n getString(idPayload, 'email'),\n getString(idPayload, 'preferred_username'),\n getString(idPayload, 'username'),\n ]) ??\n this.findEmailInObject(idPayload) ??\n this.pickFirstEmail([\n getString(accessPayload, 'email'),\n ]) ??\n this.findEmailInObject(accessPayload);\n\n const subscription: SubscriptionInfo | undefined = authInfo\n ? {\n activeStart: getString(authInfo, 'chatgpt_subscription_active_start'),\n activeUntil: getString(authInfo, 'chatgpt_subscription_active_until'),\n lastChecked: getString(authInfo, 'chatgpt_subscription_last_checked'),\n }\n : undefined;\n\n const planType = getString(authInfo, 'chatgpt_plan_type');\n const chatgptUserId = getString(authInfo, 'chatgpt_user_id');\n const chatgptAccountUserId = getString(authInfo, 'chatgpt_account_user_id');\n const userId =\n getString(authInfo, 'user_id') ??\n chatgptAccountUserId ??\n getString(idPayload ?? undefined, 'sub') ??\n getString(accessPayload ?? undefined, 'sub');\n\n const metadata: ProfileMetadata = {\n email,\n planType,\n subscription,\n organizations,\n groups,\n userId,\n chatgptUserId,\n emailVerified,\n tokenExpiresAt: this.toIsoStringFromSeconds(exp),\n tokenIssuedAt: this.toIsoStringFromSeconds(iat),\n tokenAuthTime: this.toIsoStringFromSeconds(authTime),\n };\n\n const hasMeaningfulData =\n Boolean(metadata.planType) ||\n Boolean(\n metadata.subscription &&\n (metadata.subscription.activeStart ||\n metadata.subscription.activeUntil ||\n metadata.subscription.lastChecked)\n ) ||\n Boolean(metadata.organizations && metadata.organizations.length > 0) ||\n Boolean(metadata.groups && metadata.groups.length > 0) ||\n Boolean(metadata.userId) ||\n Boolean(metadata.chatgptUserId) ||\n Boolean(metadata.email) ||\n typeof metadata.emailVerified === 'boolean' ||\n Boolean(metadata.tokenExpiresAt) ||\n Boolean(metadata.tokenIssuedAt) ||\n Boolean(metadata.tokenAuthTime);\n\n return hasMeaningfulData ? metadata : undefined;\n }\n\n /**\n * Initialize the profile manager and create necessary directories\n */\n async initialize(): Promise<void> {\n try {\n await fs.mkdir(this.codexDir, { recursive: true });\n } catch (error) {\n logError('Failed to ensure Codex directory exists:', error);\n throw new Error('Failed to initialize profile manager');\n }\n try {\n await fs.mkdir(this.profileHomesRoot, { recursive: true });\n } catch (error) {\n logError('Failed to ensure profile homes directory exists:', error);\n throw new Error('Failed to initialize profile manager');\n }\n\n await this.recoverActiveAuthBackup();\n const migrationsComplete = await this.hasCompletedMigrations();\n if (!migrationsComplete) {\n await this.migrateLegacyProfiles();\n await this.migrateProfilesFromDatabase();\n await this.removeLegacyArtifacts();\n await this.markMigrationsComplete();\n }\n }\n\n /**\n * Remove deprecated files that previously tracked the current profile.\n * These are now redundant since we infer the active profile from auth.json.\n */\n private async removeLegacyArtifacts(): Promise<void> {\n const legacyTargets = [\n join(this.codexDir, 'current-profile.json'),\n join(this.profilesDir, '.current'),\n `${this.activeAuth}.backup`,\n join(this.codexDir, 'cache', 'rate-limits'),\n ];\n\n for (const target of legacyTargets) {\n try {\n await fs.rm(target, { recursive: true, force: true });\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to remove legacy artifact '${target}':`, error);\n }\n }\n }\n\n try {\n await fs.rm(this.profilesDir, { recursive: true, force: true });\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to remove legacy profiles directory '${this.profilesDir}':`, error);\n }\n }\n }\n\n private async hasCompletedMigrations(): Promise<boolean> {\n try {\n await fs.access(this.profileMigrationMarker);\n return true;\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn('Failed to read profile migration marker:', error);\n }\n return false;\n }\n }\n\n private async markMigrationsComplete(): Promise<void> {\n try {\n await fs.mkdir(this.migrationsDir, { recursive: true });\n await fs.writeFile(this.profileMigrationMarker, 'ok');\n } catch (error) {\n logWarn('Failed to persist profile migration marker:', error);\n }\n }\n\n private async migrateLegacyProfiles(): Promise<void> {\n let files: string[];\n try {\n files = await fs.readdir(this.profilesDir);\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn('Failed to inspect legacy profiles directory:', error);\n }\n return;\n }\n\n if (files.length === 0) {\n return;\n }\n\n type LegacyCandidate = {\n name: string;\n data: ProfileData;\n profile: Profile;\n accountId?: string;\n };\n\n const accountCandidates = new Map<string, LegacyCandidate>();\n const orphanCandidates: LegacyCandidate[] = [];\n const insertedNames = new Set<string>();\n\n for (const file of files) {\n if (!file.endsWith('.json')) {\n continue;\n }\n\n const name = file.replace(/\\.json$/, '');\n const filePath = join(this.profilesDir, file);\n\n try {\n const raw = await fs.readFile(filePath, 'utf8');\n const data = JSON.parse(raw) as ProfileData;\n const normalizedName = this.normalizeProfileName(name);\n const metadata = this.extractProfileMetadata(data);\n const resolvedEmail = this.resolveProfileEmail(data, metadata);\n if (resolvedEmail) {\n data.email = resolvedEmail;\n }\n const accountId = this.getAccountIdFromData(data);\n const candidate: LegacyCandidate = {\n name: normalizedName,\n data,\n profile: this.buildProfileFromData(normalizedName, data),\n accountId,\n };\n\n if (accountId) {\n const existing = accountCandidates.get(accountId);\n if (existing) {\n const preferred = this.selectPreferredProfile([existing.profile, candidate.profile]);\n if (preferred === candidate.profile) {\n logWarn(\n `Replacing legacy profile '${existing.name}' with '${normalizedName}' for account '${accountId}'.`,\n );\n accountCandidates.set(accountId, candidate);\n } else {\n logWarn(\n `Skipping legacy profile '${name}' because account '${accountId}' already exists as '${existing.name}'.`,\n );\n }\n } else {\n accountCandidates.set(accountId, candidate);\n }\n } else {\n orphanCandidates.push(candidate);\n }\n\n await fs.rm(filePath, { force: true });\n } catch (error) {\n logWarn(`Failed to migrate legacy profile '${name}':`, error);\n }\n }\n\n const persistQueue: LegacyCandidate[] = [...accountCandidates.values(), ...orphanCandidates];\n const existingRecords = await this.listProfileRecords();\n for (const record of existingRecords) {\n insertedNames.add(record.name);\n }\n\n for (const candidate of persistQueue) {\n if (insertedNames.has(candidate.name)) {\n logWarn(`Skipping legacy profile '${candidate.name}' because it was already imported.`);\n continue;\n }\n\n try {\n await this.persistProfileRecord(candidate.name, candidate.data, candidate.profile.metadata);\n insertedNames.add(candidate.name);\n } catch (error) {\n logWarn(`Failed to persist migrated profile '${candidate.name}':`, error);\n }\n }\n }\n\n private async migrateProfilesFromDatabase(): Promise<void> {\n let rows: DbProfileRow[] = [];\n try {\n const { getDatabase } = await import('./sqlite-db');\n const db = await getDatabase();\n rows = db\n .prepare(\n 'SELECT name, data, account_id, workspace_id, workspace_name, email, auth_method, created_at, updated_at FROM profiles',\n )\n .all() as DbProfileRow[];\n } catch (error) {\n logWarn('Failed to read existing database profiles for migration:', error);\n return;\n }\n\n if (!rows || rows.length === 0) {\n return;\n }\n\n const existingNames = new Set<string>((await this.listProfileRecords()).map(record => record.name));\n\n for (const row of rows) {\n const name = typeof row?.name === 'string' ? row.name : null;\n const serialized = typeof row?.data === 'string' ? row.data : null;\n if (!name || !serialized) {\n continue;\n }\n\n let data: ProfileData;\n try {\n data = JSON.parse(serialized) as ProfileData;\n } catch (error) {\n logWarn(`Failed to parse database profile '${name}' during migration:`, error);\n continue;\n }\n\n try {\n const normalizedName = this.normalizeProfileName(name);\n if (existingNames.has(normalizedName)) {\n continue;\n }\n\n data.created_at = data.created_at ?? (typeof row?.created_at === 'string' ? row.created_at : undefined);\n data.email = data.email ?? (typeof row?.email === 'string' ? row.email : undefined);\n data.auth_method = data.auth_method ?? (typeof row?.auth_method === 'string' ? row.auth_method : undefined);\n data.workspace_id = data.workspace_id ?? (typeof row?.workspace_id === 'string' ? row.workspace_id : undefined);\n data.workspace_name =\n data.workspace_name ?? (typeof row?.workspace_name === 'string' ? row.workspace_name : undefined);\n data.updated_at = data.updated_at ?? (typeof row?.updated_at === 'string' ? row.updated_at : undefined);\n\n const metadata = this.extractProfileMetadata(data);\n await this.persistProfileRecord(normalizedName, data, metadata);\n existingNames.add(normalizedName);\n } catch (error) {\n logWarn(`Failed to migrate database profile '${name}':`, error);\n }\n }\n }\n\n private enqueueAuthSwap<T>(task: () => Promise<T>): Promise<T> {\n const run = this.authSwapLock.then(task, task);\n this.authSwapLock = run.then(\n () => undefined,\n () => undefined,\n );\n return run;\n }\n\n private async recoverActiveAuthBackup(): Promise<void> {\n let backup: string;\n try {\n backup = await fs.readFile(this.activeAuthBackup, 'utf8');\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn('Failed to inspect active auth backup:', error);\n }\n return;\n }\n\n try {\n if (backup === AUTH_BACKUP_MISSING_MARKER) {\n await fs.rm(this.activeAuth, { force: true });\n } else {\n try {\n await this.writeAtomic(this.activeAuth, backup);\n } catch (error) {\n logWarn('Failed to restore active auth from backup, falling back to direct write:', error);\n try {\n await fs.writeFile(this.activeAuth, backup, 'utf8');\n } catch (fallbackError) {\n logWarn('Direct write failed while restoring active auth backup:', fallbackError);\n }\n }\n }\n } catch (error) {\n logWarn('Failed to restore active auth from backup:', error);\n }\n\n try {\n await fs.rm(this.activeAuthBackup, { force: true });\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn('Failed to remove active auth backup:', error);\n }\n }\n }\n\n private async writeAtomic(filePath: string, contents: string): Promise<void> {\n const tempPath = `${filePath}.tmp`;\n const dir = path.dirname(filePath);\n await fs.mkdir(dir, { recursive: true });\n\n await fs.writeFile(tempPath, contents, 'utf8');\n try {\n await fs.rename(tempPath, filePath);\n } catch (error) {\n // Fall back to writing directly if rename fails for any reason.\n try {\n await fs.writeFile(filePath, contents, 'utf8');\n } catch (writeError) {\n // Surface the original error context to aid debugging.\n const original = error instanceof Error ? error.message : String(error);\n const fallback = writeError instanceof Error ? writeError.message : String(writeError);\n throw new Error(`Atomic write failed (rename: ${original}; direct write: ${fallback})`);\n }\n }\n // Best-effort cleanup of temp file if it still exists.\n try {\n await fs.rm(tempPath, { force: true });\n } catch {\n // ignore cleanup failures\n }\n }\n\n /**\n * Extract account_id from auth data (supports both formats)\n */\n private getAccountIdFromData(data: ProfileData | ActiveAuth): string | undefined {\n if ('account_id' in data && typeof data.account_id === 'string' && data.account_id.trim()) {\n return data.account_id.trim();\n }\n\n if ('tokens' in data && data.tokens && typeof data.tokens === 'object') {\n const accountId = (data.tokens as { account_id?: unknown }).account_id;\n if (typeof accountId === 'string' && accountId.trim()) {\n return accountId.trim();\n }\n }\n\n const projectId =\n 'project_id' in data && typeof (data as ProfileData).project_id === 'string'\n ? (data as ProfileData).project_id?.trim()\n : undefined;\n if (projectId) {\n return projectId;\n }\n\n const email =\n 'email' in data && typeof (data as ProfileData).email === 'string'\n ? (data as ProfileData).email?.trim()\n : undefined;\n return email || undefined;\n }\n\n private resolveWorkspaceIdentity(data: ProfileData | ActiveAuth, metadata?: ProfileMetadata): { id: string; name?: string } {\n const directId =\n 'workspace_id' in data && typeof data.workspace_id === 'string'\n ? data.workspace_id.trim()\n : undefined;\n const directName =\n 'workspace_name' in data && typeof data.workspace_name === 'string'\n ? data.workspace_name.trim()\n : undefined;\n\n if (directId) {\n return { id: directId, name: directName };\n }\n\n const organizations = metadata?.organizations;\n if (organizations && organizations.length > 0) {\n const preferred = organizations.find(org => org.isDefault) ?? organizations[0];\n if (preferred?.id) {\n return { id: preferred.id, name: preferred.title ?? directName };\n }\n }\n\n return { id: DEFAULT_WORKSPACE_ID, name: directName };\n }\n\n private async getProfileRowByName(name: string): Promise<ProfileRecord | undefined> {\n const normalized = this.normalizeProfileName(name);\n const record = await this.readProfileRecord(normalized);\n return record ?? undefined;\n }\n\n private async getProfileRowByAccountId(\n accountId: string,\n options?: { authMethod?: string | null; preferAuthMethod?: string | null },\n ): Promise<ProfileRecord | undefined> {\n const records = await this.listProfileRecords();\n const matches = records.filter(record => {\n const storedAccountId = record.accountId ?? this.getAccountIdFromData(record.data);\n return storedAccountId === accountId;\n });\n\n if (matches.length === 0) {\n return undefined;\n }\n\n const mustMatch = typeof options?.authMethod === 'string' ? options.authMethod.trim().toLowerCase() : null;\n if (mustMatch) {\n return matches.find(record => this.resolveAuthMethod(record.data) === mustMatch);\n }\n\n const prefer = typeof options?.preferAuthMethod === 'string' ? options.preferAuthMethod.trim().toLowerCase() : null;\n if (prefer) {\n return matches.find(record => this.resolveAuthMethod(record.data) === prefer) ?? matches[0];\n }\n\n return matches[0];\n }\n\n private async getProfileRowByAccountAndWorkspace(\n accountId: string,\n workspaceId: string,\n options?: { authMethod?: string | null; preferAuthMethod?: string | null },\n ): Promise<ProfileRecord | undefined> {\n const workspaceKey = workspaceId && workspaceId.trim().length > 0 ? workspaceId.trim() : DEFAULT_WORKSPACE_ID;\n const records = await this.listProfileRecords();\n const matches = records.filter(record => {\n const storedAccountId = record.accountId ?? this.getAccountIdFromData(record.data);\n if (storedAccountId !== accountId) {\n return false;\n }\n const storedWorkspace = record.workspaceId ?? record.data.workspace_id ?? DEFAULT_WORKSPACE_ID;\n return (storedWorkspace && storedWorkspace.trim() ? storedWorkspace.trim() : DEFAULT_WORKSPACE_ID) === workspaceKey;\n });\n\n if (matches.length === 0) {\n return undefined;\n }\n\n const mustMatch = typeof options?.authMethod === 'string' ? options.authMethod.trim().toLowerCase() : null;\n if (mustMatch) {\n return matches.find(record => this.resolveAuthMethod(record.data) === mustMatch);\n }\n\n const prefer = typeof options?.preferAuthMethod === 'string' ? options.preferAuthMethod.trim().toLowerCase() : null;\n if (prefer) {\n return matches.find(record => this.resolveAuthMethod(record.data) === prefer) ?? matches[0];\n }\n\n return matches[0];\n }\n\n private async persistProfileRecord(\n name: string,\n data: ProfileData,\n metadata?: ProfileMetadata,\n options?: { displayName?: string | null },\n ): Promise<void> {\n const resolvedName = this.normalizeProfileName(name);\n const existingRecord = await this.readProfileRecord(resolvedName);\n if (typeof data.auth_method === 'string') {\n data.auth_method = data.auth_method.trim().toLowerCase();\n }\n if (!data.auth_method) {\n data.auth_method = 'codex-cli';\n }\n const resolvedMetadata = metadata ?? this.extractProfileMetadata(data) ?? existingRecord?.metadata;\n const workspace = this.resolveWorkspaceIdentity(data, resolvedMetadata);\n const workspaceId = workspace.id || DEFAULT_WORKSPACE_ID;\n const workspaceName = workspace.name ?? null;\n if (!data.workspace_id) {\n data.workspace_id = workspaceId;\n }\n if (!data.workspace_name && workspaceName) {\n data.workspace_name = workspaceName;\n }\n\n const now = new Date().toISOString();\n const accountId = this.getAccountIdFromData(data) ?? null;\n const resolvedEmail = this.resolveProfileEmail(data, resolvedMetadata) ?? data.email ?? null;\n if (resolvedEmail) {\n data.email = resolvedEmail;\n }\n const createdAt = data.created_at ?? existingRecord?.createdAt ?? now;\n data.created_at = createdAt;\n const updatedAt = data.updated_at ?? now;\n data.updated_at = updatedAt;\n\n const displayName =\n typeof options?.displayName === 'string' && options.displayName.trim().length > 0\n ? options.displayName.trim()\n : existingRecord?.displayName ?? resolvedName;\n\n const record: ProfileRecord = {\n name: resolvedName,\n displayName,\n data,\n metadata: resolvedMetadata,\n accountId,\n workspaceId: data.workspace_id ?? workspaceId,\n workspaceName: data.workspace_name ?? workspaceName,\n email: resolvedEmail ?? null,\n authMethod: typeof data.auth_method === 'string' ? data.auth_method : null,\n createdAt,\n updatedAt,\n };\n\n await fs.mkdir(this.getProfileHomePath(resolvedName), { recursive: true });\n await this.writeProfileRecord(record);\n }\n\n private buildProfileFromRow(row: ProfileRecord): Profile {\n const profile = this.buildProfileFromData(row.name, row.data, {\n displayName: row.displayName ?? undefined,\n email: row.email ?? undefined,\n createdAt: row.createdAt ?? undefined,\n authMethod: row.authMethod ?? undefined,\n accountId: row.accountId ?? undefined,\n workspaceId: row.workspaceId ?? undefined,\n workspaceName: row.workspaceName ?? undefined,\n metadata: row.metadata,\n });\n\n return profile;\n }\n\n private buildProfileFromData(\n name: string,\n data: ProfileData,\n fallback?: {\n displayName?: string;\n email?: string;\n createdAt?: string;\n authMethod?: string;\n accountId?: string;\n projectId?: string;\n workspaceId?: string;\n workspaceName?: string;\n metadata?: ProfileMetadata;\n },\n ): Profile {\n const metadata = fallback?.metadata ?? this.extractProfileMetadata(data);\n const workspace = this.resolveWorkspaceIdentity(data, metadata);\n const workspaceId = workspace.id || fallback?.workspaceId || DEFAULT_WORKSPACE_ID;\n const workspaceName = workspace.name ?? fallback?.workspaceName ?? undefined;\n const email = this.resolveProfileEmail(data, metadata) ?? fallback?.email ?? undefined;\n const tokenStatus = this.evaluateTokenStatus(data, metadata);\n const accountId = this.getAccountIdFromData(data) ?? fallback?.accountId;\n const projectId =\n typeof data.project_id === 'string' && data.project_id.trim().length > 0\n ? data.project_id.trim()\n : fallback?.projectId;\n\n return {\n name,\n displayName: fallback?.displayName ?? name,\n isValid: Boolean(data && Object.keys(data).length > 0),\n accountId,\n projectId,\n workspaceId,\n workspaceName,\n email,\n createdAt: data.created_at ?? fallback?.createdAt ?? undefined,\n authMethod: typeof data.auth_method === 'string'\n ? data.auth_method.trim().toLowerCase()\n : fallback?.authMethod ?? undefined,\n metadata,\n tokenStatus,\n };\n }\n\n private parseTimestamp(value?: string | null): number | null {\n if (typeof value !== 'string' || value.trim() === '') {\n return null;\n }\n\n const parsed = Date.parse(value);\n return Number.isNaN(parsed) ? null : parsed;\n }\n\n private compareProfilesForPreference(a: Profile, b: Profile): number {\n if (Boolean(a.isValid) !== Boolean(b.isValid)) {\n return a.isValid ? -1 : 1;\n }\n\n const aTimestamp = this.parseTimestamp(a.createdAt ?? null);\n const bTimestamp = this.parseTimestamp(b.createdAt ?? null);\n\n if (aTimestamp !== null || bTimestamp !== null) {\n if (aTimestamp !== null && bTimestamp !== null) {\n if (aTimestamp > bTimestamp) {\n return -1;\n }\n if (aTimestamp < bTimestamp) {\n return 1;\n }\n } else if (aTimestamp !== null) {\n return -1;\n } else {\n return 1;\n }\n }\n\n return a.name.localeCompare(b.name);\n }\n\n private selectPreferredProfile(profiles: Profile[]): Profile {\n if (profiles.length <= 1) {\n return profiles[0];\n }\n\n return profiles\n .slice()\n .sort((a, b) => this.compareProfilesForPreference(a, b))[0];\n }\n\n /**\n * Convert profile data to Codex's expected format\n */\n private convertToCodexFormat(data: ProfileData): ActiveAuth {\n // Check if data is already in Codex format\n if ('tokens' in data) {\n return data as ActiveAuth;\n }\n\n // Convert flat structure to nested Codex format\n return {\n OPENAI_API_KEY: null,\n tokens: {\n id_token: data.id_token,\n access_token: data.access_token,\n refresh_token: data.refresh_token,\n account_id: data.account_id,\n },\n last_refresh: new Date().toISOString(),\n };\n }\n\n /**\n * Normalize Codex auth data to flat profile format\n */\n private normalizeProfileData(data: ActiveAuth): ProfileData {\n // If data is already in flat format, return as-is\n if ('id_token' in data) {\n return data as ProfileData;\n }\n\n // Convert nested Codex format to flat\n const tokens = data.tokens || {};\n const profile: ProfileData = {\n id_token: tokens.id_token,\n access_token: tokens.access_token,\n refresh_token: tokens.refresh_token,\n account_id: tokens.account_id,\n };\n\n if (data.email) {\n profile.email = data.email;\n }\n\n return profile;\n }\n\n private mergeProfileRecords(existing: ProfileData, incoming: ProfileData): {\n profile: ProfileData;\n metadata?: ProfileMetadata;\n } {\n const merged: ProfileData = {\n ...existing,\n ...incoming,\n created_at: existing.created_at ?? incoming.created_at ?? new Date().toISOString(),\n };\n\n if (typeof merged.auth_method === 'string') {\n merged.auth_method = merged.auth_method.trim().toLowerCase();\n }\n if (!merged.auth_method) {\n merged.auth_method = 'codex-cli';\n }\n\n const metadata = this.extractProfileMetadata(merged);\n const resolvedEmail = this.resolveProfileEmail(merged, metadata);\n\n if (resolvedEmail) {\n merged.email = resolvedEmail;\n }\n\n const workspace = this.resolveWorkspaceIdentity(merged, metadata);\n if (!merged.workspace_id) {\n merged.workspace_id = workspace.id || existing.workspace_id || DEFAULT_WORKSPACE_ID;\n }\n if (!merged.workspace_name) {\n merged.workspace_name = workspace.name ?? existing.workspace_name;\n }\n\n if (this.hasTokenChanges(existing, incoming)) {\n delete merged.tokenAlert;\n }\n\n return { profile: merged, metadata };\n }\n\n private hasTokenChanges(existing: ProfileData, incoming: ProfileData): boolean {\n const tokenKeys: Array<keyof ProfileData> = ['id_token', 'access_token', 'refresh_token', 'account_id', 'workspace_id'];\n\n return tokenKeys.some(key => {\n const nextValue = incoming[key];\n if (typeof nextValue !== 'string' || nextValue.length === 0) {\n return false;\n }\n return nextValue !== existing[key];\n });\n }\n\n async flagRefreshTokenRedeemed(name: string, reason?: string, options?: { observedAt?: string }): Promise<void> {\n await this.initialize();\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n logWarn(`Cannot flag refresh token issue for missing profile '${profileName}'.`);\n return;\n }\n\n const observedAt = options?.observedAt ? Date.parse(options.observedAt) : null;\n const updatedAt = record.updatedAt ? Date.parse(record.updatedAt) : null;\n if (observedAt !== null && updatedAt !== null && observedAt <= updatedAt) {\n return;\n }\n\n const data: ProfileData = record.data;\n\n const trimmedReason = typeof reason === 'string' && reason.trim().length > 0 ? reason.trim() : undefined;\n const normalized = trimmedReason?.toLowerCase() ?? '';\n const storedReason = normalized.includes(REFRESH_TOKEN_REDEEMED_SNIPPET)\n ? REFRESH_TOKEN_REDEEMED_REASON\n : trimmedReason ?? REFRESH_TOKEN_REDEEMED_REASON;\n const alert: TokenAlert = {\n issue: 'refresh-redeemed',\n reason: storedReason,\n recordedAt: new Date().toISOString(),\n };\n\n data.tokenAlert = alert;\n\n try {\n const metadata = this.extractProfileMetadata(data);\n await this.persistProfileRecord(profileName, data, metadata);\n } catch (error) {\n logError(`Failed to persist token alert for profile '${profileName}':`, error);\n }\n }\n\n private async syncProfileTokensFromActiveAuth(\n profileName: string,\n baselineProfile: ProfileData,\n authPath?: string,\n ): Promise<void> {\n const targetPath = authPath ?? this.activeAuth;\n try {\n const authRaw = await fs.readFile(targetPath, 'utf8');\n await this.syncProfileTokensFromAuthContent(profileName, baselineProfile, authRaw);\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to read Codex auth for '${profileName}' while syncing tokens:`, error);\n }\n }\n }\n\n private async syncProfileTokensFromAuthContent(\n profileName: string,\n baselineProfile: ProfileData,\n authContent: string,\n ): Promise<void> {\n let activeAuth: ActiveAuth;\n try {\n activeAuth = JSON.parse(authContent) as ActiveAuth;\n } catch (error) {\n logWarn(`Failed to parse Codex auth while syncing tokens for '${profileName}':`, error);\n return;\n }\n\n const normalized = this.normalizeProfileData(activeAuth);\n const metadata = this.extractProfileMetadata(normalized);\n const workspace = this.resolveWorkspaceIdentity(normalized, metadata);\n normalized.workspace_id = normalized.workspace_id ?? workspace.id ?? DEFAULT_WORKSPACE_ID;\n if (workspace.name) {\n normalized.workspace_name = normalized.workspace_name ?? workspace.name;\n }\n const hasTokens =\n typeof normalized.id_token === 'string' ||\n typeof normalized.access_token === 'string' ||\n typeof normalized.refresh_token === 'string';\n\n if (!hasTokens) {\n return;\n }\n\n let existing = baselineProfile;\n const latestRow = await this.getProfileRowByName(profileName);\n if (latestRow) {\n existing = latestRow.data;\n }\n\n const existingAccountId = this.getAccountIdFromData(existing);\n const newAccountId = this.getAccountIdFromData(normalized);\n\n if (existingAccountId && newAccountId && existingAccountId !== newAccountId) {\n logWarn(\n `Skipped syncing tokens for profile '${profileName}' because Codex auth switched to account '${newAccountId}'.`,\n );\n return;\n }\n\n if (!this.hasTokenChanges(existing, normalized)) {\n return;\n }\n\n const { profile: merged, metadata: mergedMetadata } = this.mergeProfileRecords(existing, normalized);\n\n try {\n await this.persistProfileRecord(profileName, merged, mergedMetadata);\n } catch (error) {\n logError(`Failed to persist refreshed tokens for profile '${profileName}':`, error);\n }\n }\n\n private async copyCodexConfig(targetCodexHome: string): Promise<void> {\n const candidates = ['config.toml', 'config.json', 'config.yaml', 'config.yml'];\n for (const file of candidates) {\n const source = join(this.codexDir, file);\n const destination = join(targetCodexHome, file);\n try {\n await fs.copyFile(source, destination);\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to copy Codex config '${file}' to profile home:`, error);\n }\n }\n }\n }\n\n private getProfileHomePath(profileName: string): string {\n return join(this.profileHomesRoot, profileName);\n }\n\n private ensureTrailingNewline(content: string): string {\n return content.endsWith('\\n') ? content : `${content}\\n`;\n }\n\n private async sanitizeProfileConfig(profileHome: string): Promise<void> {\n const configPath = join(profileHome, 'config.toml');\n let raw: string;\n try {\n raw = await fs.readFile(configPath, 'utf8');\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to read config for profile home '${profileHome}':`, error);\n }\n return;\n }\n\n let parsed: Record<string, unknown>;\n try {\n parsed = parse(raw) as Record<string, unknown>;\n } catch (error) {\n logWarn(`Failed to parse config.toml for profile home '${profileHome}':`, error);\n return;\n }\n\n if (!Object.prototype.hasOwnProperty.call(parsed, 'model_reasoning_effort')) {\n return;\n }\n\n delete parsed['model_reasoning_effort'];\n const sanitized = this.ensureTrailingNewline(stringify(parsed as Record<string, unknown>));\n try {\n await fs.writeFile(configPath, sanitized, 'utf8');\n } catch (error) {\n logWarn(`Failed to write sanitized config.toml for profile home '${profileHome}':`, error);\n }\n }\n\n private async prepareProfileHome(profileName: string, authData: ActiveAuth): Promise<string> {\n const profileHome = this.getProfileHomePath(profileName);\n await fs.mkdir(profileHome, { recursive: true });\n const authPath = join(profileHome, 'auth.json');\n await this.writeAtomic(authPath, JSON.stringify(authData, null, 2));\n await this.copyCodexConfig(profileHome);\n await this.sanitizeProfileConfig(profileHome);\n return profileHome;\n }\n\n /**\n * List all available profiles\n */\n async profileExists(name: string): Promise<boolean> {\n await this.initialize();\n const profileName = this.normalizeProfileName(name);\n return Boolean(await this.getProfileRowByName(profileName));\n }\n\n async listProfiles(): Promise<Profile[]> {\n await this.initialize();\n try {\n const records = await this.listProfileRecords();\n return records\n .map(record => this.buildProfileFromRow(record))\n .filter(profile => !profile.authMethod || profile.authMethod === 'codex-cli')\n .sort((a, b) => {\n const aLabel = a.displayName ?? a.name;\n const bLabel = b.displayName ?? b.name;\n const base = aLabel.localeCompare(bLabel);\n return base !== 0 ? base : a.name.localeCompare(b.name);\n });\n } catch (error) {\n logError('Error reading profiles from disk:', error);\n return [];\n }\n }\n\n /**\n * Create a new profile by running codex login\n */\n async createProfile(name: string): Promise<Profile | null> {\n await this.initialize();\n\n const profileName = this.normalizeProfileName(name);\n if (await this.getProfileRowByName(profileName)) {\n throw new Error(`Profile '${profileName}' already exists!`);\n }\n\n let authData: ActiveAuth;\n try {\n const authRaw = await fs.readFile(this.activeAuth, 'utf8');\n authData = JSON.parse(authRaw) as ActiveAuth;\n } catch (error) {\n logError('Error creating profile:', error);\n throw new Error('Failed to create profile. Make sure Codex CLI is installed and you are logged in.');\n }\n\n const normalizedProfile = this.normalizeProfileData(authData);\n normalizedProfile.created_at = normalizedProfile.created_at ?? new Date().toISOString();\n if (typeof normalizedProfile.auth_method === 'string') {\n normalizedProfile.auth_method = normalizedProfile.auth_method.trim().toLowerCase();\n }\n normalizedProfile.auth_method = normalizedProfile.auth_method ?? 'codex-cli';\n\n const metadata = this.extractProfileMetadata(normalizedProfile);\n const workspace = this.resolveWorkspaceIdentity(normalizedProfile, metadata);\n normalizedProfile.workspace_id = normalizedProfile.workspace_id ?? workspace.id ?? DEFAULT_WORKSPACE_ID;\n if (workspace.name) {\n normalizedProfile.workspace_name = normalizedProfile.workspace_name ?? workspace.name;\n }\n\n const accountId = this.getAccountIdFromData(normalizedProfile);\n if (accountId) {\n const requestedWorkspace = normalizedProfile.workspace_id ?? DEFAULT_WORKSPACE_ID;\n const duplicate = await this.getProfileRowByAccountAndWorkspace(accountId, requestedWorkspace, { authMethod: 'codex-cli' });\n if (duplicate) {\n normalizedProfile.workspace_id = `${requestedWorkspace}:${profileName}`;\n normalizedProfile.workspace_name = normalizedProfile.workspace_name ?? profileName;\n }\n }\n\n const resolvedEmail = this.resolveProfileEmail(normalizedProfile, metadata);\n if (resolvedEmail) {\n normalizedProfile.email = resolvedEmail;\n }\n\n try {\n await this.persistProfileRecord(profileName, normalizedProfile, metadata);\n const stored = await this.getProfileRowByName(profileName);\n return stored ? this.buildProfileFromRow(stored) : null;\n } catch (error) {\n logError('Error creating profile:', error);\n throw new Error('Failed to create profile. Make sure Codex CLI is installed and you are logged in.');\n }\n }\n\n /**\n * Switch to a different profile\n */\n async switchToProfile(name: string): Promise<boolean> {\n await this.initialize();\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n throw new Error(`Profile '${profileName}' not found!`);\n }\n\n try {\n const profileData = record.data;\n const codexFormat = this.convertToCodexFormat(profileData);\n await this.writeAtomic(this.activeAuth, JSON.stringify(codexFormat, null, 2));\n await this.persistPreferredProfileName(profileName);\n return true;\n } catch (error) {\n logError('Error switching profile:', error);\n throw new Error('Failed to switch profile');\n }\n }\n\n async refreshProfileAuth(name: string): Promise<Profile> {\n await this.initialize();\n\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n throw new Error(`Profile '${profileName}' not found!`);\n }\n\n const existing: ProfileData = record.data;\n let activeAuth: ActiveAuth;\n try {\n const authContent = await fs.readFile(this.activeAuth, 'utf8');\n activeAuth = JSON.parse(authContent) as ActiveAuth;\n } catch (error) {\n if (this.isNotFoundError(error)) {\n throw new Error('Codex CLI did not produce an auth file. Complete the login before refreshing this profile.');\n }\n logError('Failed to read Codex auth file during refresh:', error);\n throw new Error('Failed to read Codex auth file. Complete the login and try again.');\n }\n\n const normalized = this.normalizeProfileData(activeAuth);\n const existingAccountId = this.getAccountIdFromData(existing);\n const newAccountId = this.getAccountIdFromData(normalized);\n const normalizedMetadata = this.extractProfileMetadata(normalized);\n const normalizedWorkspace = this.resolveWorkspaceIdentity(normalized, normalizedMetadata);\n normalized.workspace_id = normalized.workspace_id ?? normalizedWorkspace.id ?? DEFAULT_WORKSPACE_ID;\n if (normalizedWorkspace.name) {\n normalized.workspace_name = normalized.workspace_name ?? normalizedWorkspace.name;\n }\n\n if (existingAccountId && newAccountId && existingAccountId !== newAccountId) {\n throw new Error(\n `Active Codex login is for account '${newAccountId}', but profile '${profileName}' is tied to '${existingAccountId}'. Create a new profile for the new account instead.`,\n );\n }\n\n if (existingAccountId && normalized.workspace_id) {\n const currentWorkspace = this.resolveWorkspaceIdentity(existing, this.extractProfileMetadata(existing));\n const targetWorkspaceId = normalized.workspace_id;\n const currentWorkspaceId = currentWorkspace.id ?? DEFAULT_WORKSPACE_ID;\n if (targetWorkspaceId !== currentWorkspaceId) {\n const duplicate = await this.getProfileRowByAccountAndWorkspace(existingAccountId, targetWorkspaceId, {\n authMethod: this.resolveAuthMethod(existing),\n });\n if (duplicate && duplicate.name !== profileName) {\n throw new Error(\n `Account is already saved as profile '${duplicate.name}'. Switch to or delete that profile before creating another.`,\n );\n }\n }\n }\n\n const { profile: merged, metadata } = this.mergeProfileRecords(existing, normalized);\n delete merged.tokenAlert;\n\n await this.persistProfileRecord(profileName, merged, metadata);\n const updated = await this.getProfileRowByName(profileName);\n if (!updated) {\n throw new Error('Failed to persist refreshed profile data.');\n }\n\n const profile = this.buildProfileFromRow(updated);\n if (metadata) {\n profile.metadata = metadata;\n profile.tokenStatus = this.evaluateTokenStatus(merged, metadata);\n }\n return profile;\n }\n\n /**\n * Execute an action with a profile's auth injected.\n * Creates an isolated CODEX_HOME with the profile's auth/config, invokes the action\n * with env overrides, then syncs any refreshed tokens back to the profile.\n */\n async runWithProfileAuth<T>(\n name: string,\n action: (env: NodeJS.ProcessEnv) => Promise<T>,\n ): Promise<T> {\n return this.enqueueAuthSwap(async () => {\n await this.initialize();\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n throw new Error(`Profile '${profileName}' not found!`);\n }\n\n const profileData: ProfileData = record.data;\n const profileHome = this.getProfileHomePath(profileName);\n await fs.mkdir(profileHome, { recursive: true });\n\n if (profileData.auth_method && profileData.auth_method !== 'codex-cli') {\n throw new Error('Unsupported auth method. Codex CLI profiles only.');\n }\n const codexFormat = this.convertToCodexFormat(profileData);\n await this.prepareProfileHome(profileName, codexFormat);\n const authPath = path.join(profileHome, 'auth.json');\n\n const envOverrides: NodeJS.ProcessEnv = {\n ...process.env,\n HOME: profileHome,\n USERPROFILE: profileHome,\n CODEX_HOME: profileHome,\n };\n\n try {\n return await action(envOverrides);\n } finally {\n let finalAuthContent: string | null = null;\n try {\n finalAuthContent = await fs.readFile(authPath, 'utf8');\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to read isolated auth for '${profileName}' after action:`, error);\n }\n }\n\n try {\n if (finalAuthContent) {\n await this.syncProfileTokensFromAuthContent(profileName, profileData, finalAuthContent);\n } else {\n await this.syncProfileTokensFromActiveAuth(profileName, profileData, authPath);\n }\n } catch (error) {\n logWarn(`Failed to sync refreshed tokens for profile '${profileName}':`, error);\n }\n }\n });\n }\n\n /**\n * Rename a profile\n */\n async renameProfile(oldName: string, newName: string): Promise<boolean> {\n await this.initialize();\n const sourceName = this.normalizeProfileName(oldName);\n const targetName = this.normalizeProfileName(newName);\n\n const existing = await this.getProfileRowByName(sourceName);\n if (!existing) {\n throw new Error(`Profile '${sourceName}' not found!`);\n }\n\n if (await this.getProfileRowByName(targetName)) {\n throw new Error(`Profile '${targetName}' already exists!`);\n }\n\n const preferred = await this.readPreferredProfileName();\n if (preferred && preferred === sourceName) {\n await this.persistPreferredProfileName(targetName);\n }\n\n const oldHome = this.getProfileHomePath(sourceName);\n const newHome = this.getProfileHomePath(targetName);\n try {\n await fs.mkdir(this.profileHomesRoot, { recursive: true });\n await fs.rename(oldHome, newHome);\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to move profile home from '${sourceName}' to '${targetName}':`, error);\n }\n }\n\n try {\n const updatedRecord: ProfileRecord = {\n ...existing,\n name: targetName,\n };\n await this.writeProfileRecord(updatedRecord);\n } catch (error) {\n logWarn(`Failed to rewrite profile record after renaming '${sourceName}' to '${targetName}':`, error);\n }\n\n return true;\n }\n\n async updateProfileDisplayName(name: string, displayName: string): Promise<boolean> {\n await this.initialize();\n\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n throw new Error(`Profile '${profileName}' not found!`);\n }\n\n const trimmed = typeof displayName === 'string' ? displayName.trim() : '';\n if (!trimmed) {\n throw new Error('Profile name must not be empty.');\n }\n\n const now = new Date().toISOString();\n record.displayName = trimmed;\n record.updatedAt = now;\n (record.data as ProfileData & { updated_at?: string }).updated_at = now;\n\n await this.writeProfileRecord(record);\n return true;\n }\n\n /**\n * Delete a profile\n */\n async deleteProfile(name: string): Promise<boolean> {\n await this.initialize();\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n throw new Error(`Profile '${profileName}' not found!`);\n }\n const preferred = await this.readPreferredProfileName();\n if (preferred && preferred === profileName) {\n await this.persistPreferredProfileName(null);\n }\n try {\n await fs.rm(this.getProfileHomePath(profileName), { recursive: true, force: true });\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to remove profile home for '${profileName}':`, error);\n }\n }\n return true;\n }\n\n /**\n * Delete every profile that does not appear in the allow-list.\n * Used to enforce plan limits when local storage was tampered with.\n */\n async deleteProfilesNotIn(allowedNames: Iterable<string>): Promise<string[]> {\n await this.initialize();\n const normalized = new Set<string>();\n for (const name of allowedNames) {\n try {\n normalized.add(this.normalizeProfileName(name));\n } catch {\n // Skip invalid names so we only retain well-formed entries.\n }\n }\n\n const records = await this.listProfileRecords();\n const deleteCandidates = records\n .map(record => {\n try {\n return this.normalizeProfileName(record.name);\n } catch {\n return null;\n }\n })\n .filter((name): name is string => Boolean(name) && !normalized.has(name));\n\n if (deleteCandidates.length === 0) {\n return [];\n }\n\n const removed: string[] = [];\n for (const name of deleteCandidates) {\n try {\n await fs.rm(this.getProfileHomePath(name), { recursive: true, force: true });\n removed.push(name);\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to remove profile '${name}' during plan enforcement:`, error);\n }\n }\n }\n const preferred = await this.readPreferredProfileName();\n if (preferred && removed.includes(preferred)) {\n await this.persistPreferredProfileName(null);\n }\n return removed;\n }\n\n /**\n * Check if the current auth matches a profile (for smart detection)\n */\n async getCurrentProfile(): Promise<{ name: string | null; trusted: boolean }> {\n await this.initialize();\n const preferredName = await this.readPreferredProfileName();\n if (preferredName) {\n try {\n const normalizedPreferred = this.normalizeProfileName(preferredName);\n const preferredRecord = await this.getProfileRowByName(normalizedPreferred);\n if (preferredRecord) {\n return { name: normalizedPreferred, trusted: true };\n }\n } catch {\n // fall through to active auth detection\n }\n }\n const activeAuth = await this.readActiveAuthFile();\n\n if (activeAuth) {\n const normalizedAuth = this.normalizeProfileData(activeAuth);\n const authMetadata = this.extractProfileMetadata(normalizedAuth);\n const workspace = this.resolveWorkspaceIdentity(normalizedAuth, authMetadata);\n const activeAccountId = this.getAccountIdFromData(normalizedAuth);\n\n if (activeAccountId) {\n const scoped = await this.getProfileRowByAccountAndWorkspace(activeAccountId, workspace.id ?? DEFAULT_WORKSPACE_ID, {\n preferAuthMethod: 'codex-cli',\n });\n if (scoped) {\n const normalized = this.normalizeProfileName(scoped.name);\n await this.persistPreferredProfileName(normalized);\n return { name: normalized, trusted: true };\n }\n\n const matching = await this.getProfileRowByAccountId(activeAccountId, { preferAuthMethod: 'codex-cli' });\n if (matching) {\n const normalized = this.normalizeProfileName(matching.name);\n await this.persistPreferredProfileName(normalized);\n return { name: normalized, trusted: true };\n }\n }\n }\n\n const preferred = await this.readPreferredProfileName();\n if (preferred) {\n try {\n const normalized = this.normalizeProfileName(preferred);\n const exists = await this.getProfileRowByName(normalized);\n if (exists) {\n return { name: normalized, trusted: true };\n }\n } catch {\n await this.persistPreferredProfileName(null);\n return { name: null, trusted: false };\n }\n await this.persistPreferredProfileName(null);\n }\n\n return { name: null, trusted: false };\n }\n\n /**\n * Export a profile to a portable format.\n * Sensitive tokens are included but should be handled securely.\n */\n async exportProfile(name: string): Promise<{\n name: string;\n displayName: string | null;\n email: string | null;\n accountId: string | null;\n workspaceId: string | null;\n workspaceName: string | null;\n authMethod: string | null;\n createdAt: string | null;\n metadata: ProfileMetadata | undefined;\n exportedAt: string;\n }> {\n await this.initialize();\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n throw new Error(`Profile '${profileName}' not found!`);\n }\n\n return {\n name: record.name,\n displayName: record.displayName,\n email: record.email,\n accountId: record.accountId,\n workspaceId: record.workspaceId,\n workspaceName: record.workspaceName,\n authMethod: record.authMethod,\n createdAt: record.createdAt,\n metadata: record.metadata,\n exportedAt: new Date().toISOString(),\n };\n }\n\n /**\n * Export all profiles to a portable format.\n */\n async exportAllProfiles(): Promise<Array<{\n name: string;\n displayName: string | null;\n email: string | null;\n accountId: string | null;\n workspaceId: string | null;\n workspaceName: string | null;\n authMethod: string | null;\n createdAt: string | null;\n metadata: ProfileMetadata | undefined;\n exportedAt: string;\n }>> {\n await this.initialize();\n const records = await this.listProfileRecords();\n const exportedAt = new Date().toISOString();\n \n return records.map(record => ({\n name: record.name,\n displayName: record.displayName,\n email: record.email,\n accountId: record.accountId,\n workspaceId: record.workspaceId,\n workspaceName: record.workspaceName,\n authMethod: record.authMethod,\n createdAt: record.createdAt,\n metadata: record.metadata,\n exportedAt,\n }));\n }\n}\n","import { promises as fs } from 'node:fs';\nimport path from 'node:path';\nimport { logWarn } from './logger';\nimport { normalizeAutoRollSettings, type AutoRollSettings } from './auto-roll-settings';\nimport { normalizeCodexCliChannel, parseCodexCliChannel, type CodexCliChannel } from './codex-cli-channel';\n\ntype LicenseStatusCode = 'inactive' | 'active' | 'grace' | 'error';\n\ninterface StoredLicense {\n licenseKey?: string | null;\n purchaseEmail?: string | null;\n lastVerifiedAt?: string | null;\n nextCheckAt?: string | null;\n lastVerificationError?: string | null;\n status?: LicenseStatusCode;\n signature?: string | null;\n}\n\ninterface CodexSettings {\n license?: StoredLicense | null;\n lastProfileName?: string | null;\n autoRoll?: AutoRollSettings | null;\n lastAppVersion?: string | null;\n pendingUpdateVersion?: string | null;\n cliChannel?: CodexCliChannel | null;\n}\n\nconst KEY_LAST_PROFILE_NAME = 'last_profile_name';\nconst KEY_LICENSE = 'license';\nconst KEY_AUTO_ROLL = 'auto_roll';\nconst KEY_LAST_APP_VERSION = 'last_app_version';\nconst KEY_PENDING_UPDATE_VERSION = 'pending_update_version';\nconst KEY_CLI_CHANNEL = 'cli_channel';\nconst SETTINGS_FILE = 'settings.json';\nconst SETTINGS_BACKUP_FILE = 'settings.json.bak';\n\nfunction resolveCodexDir(): string {\n const homeDir = process.env.HOME || process.env.USERPROFILE || '';\n return homeDir ? path.join(homeDir, '.codex') : '.codex';\n}\n\nfunction resolveSettingsPath(): string {\n return path.join(resolveCodexDir(), SETTINGS_FILE);\n}\n\nfunction resolveBackupSettingsPath(): string {\n return path.join(resolveCodexDir(), SETTINGS_BACKUP_FILE);\n}\n\nasync function ensureCodexDir(): Promise<void> {\n try {\n await fs.mkdir(resolveCodexDir(), { recursive: true });\n } catch (error) {\n logWarn('Failed to ensure Codex directory exists:', error);\n }\n}\n\nasync function writeAtomic(filePath: string, contents: string): Promise<void> {\n const tempPath = `${filePath}.tmp`;\n await fs.mkdir(path.dirname(filePath), { recursive: true });\n await fs.writeFile(tempPath, contents, 'utf8');\n try {\n await fs.rename(tempPath, filePath);\n } catch (error) {\n const code = (error as NodeJS.ErrnoException).code;\n if (code === 'ENOENT') {\n await fs.writeFile(filePath, contents, 'utf8');\n } else {\n throw error;\n }\n }\n try {\n await fs.rm(tempPath, { force: true });\n } catch {\n // ignore cleanup\n }\n}\n\nfunction normalizeStoredLicense(raw?: unknown): StoredLicense | null {\n if (!raw || typeof raw !== 'object') {\n return null;\n }\n\n const source = raw as Record<string, unknown>;\n const licenseKey =\n typeof source.licenseKey === 'string'\n ? source.licenseKey\n : typeof source.license_key === 'string'\n ? source.license_key\n : null;\n const purchaseEmail =\n typeof source.purchaseEmail === 'string'\n ? source.purchaseEmail\n : typeof source.purchase_email === 'string'\n ? source.purchase_email\n : null;\n const lastVerifiedAt =\n typeof source.lastVerifiedAt === 'string'\n ? source.lastVerifiedAt\n : typeof source.last_verified_at === 'string'\n ? source.last_verified_at\n : null;\n const nextCheckAt =\n typeof source.nextCheckAt === 'string'\n ? source.nextCheckAt\n : typeof source.next_check_at === 'string'\n ? source.next_check_at\n : null;\n const lastVerificationError =\n typeof source.lastVerificationError === 'string'\n ? source.lastVerificationError\n : typeof source.last_verification_error === 'string'\n ? source.last_verification_error\n : null;\n const signature =\n typeof source.signature === 'string'\n ? source.signature\n : null;\n const statusCandidate =\n typeof source.status === 'string'\n ? (source.status as LicenseStatusCode)\n : undefined;\n\n const license: StoredLicense = {\n licenseKey,\n purchaseEmail,\n lastVerifiedAt,\n nextCheckAt,\n lastVerificationError,\n signature,\n status: statusCandidate,\n };\n\n const hasLicenseData = Boolean(\n license.licenseKey ??\n license.purchaseEmail ??\n license.lastVerifiedAt ??\n license.nextCheckAt ??\n license.lastVerificationError ??\n license.status,\n );\n\n return hasLicenseData ? license : null;\n}\n\nfunction normalizeStoredAutoRoll(raw?: unknown): AutoRollSettings | null {\n if (!raw) {\n return null;\n }\n\n const parsed = typeof raw === 'string' ? parseJsonValue(raw) : raw;\n if (!parsed || typeof parsed !== 'object') {\n return null;\n }\n\n try {\n return normalizeAutoRollSettings(parsed as Partial<AutoRollSettings>);\n } catch {\n return null;\n }\n}\n\nfunction parseStoredProfileName(raw?: unknown): string | null {\n if (raw === null || raw === undefined) {\n return null;\n }\n\n if (typeof raw === 'string' || typeof raw === 'number') {\n const normalized = String(raw).trim();\n return normalized.length > 0 ? normalized : null;\n }\n\n const candidate = typeof raw === 'string' ? parseJsonValue(raw) : parseJsonValue(raw);\n if (typeof candidate === 'string' || typeof candidate === 'number') {\n const normalized = String(candidate).trim();\n return normalized.length > 0 ? normalized : null;\n }\n\n return null;\n}\n\nasync function markSettingsCorrupt(settingsPath: string, raw: string): Promise<void> {\n try {\n const suffix = new Date().toISOString().replace(/[:.]/g, \"-\");\n const backupPath = `${settingsPath}.corrupt-${suffix}`;\n await fs.writeFile(backupPath, raw, \"utf8\");\n } catch {\n // best effort backup\n }\n}\n\nasync function tryRestoreSettings(fromPath: string, _targetPath: string): Promise<CodexSettings | null> {\n try {\n const raw = await fs.readFile(fromPath, \"utf8\");\n const parsed = JSON.parse(raw) as Record<string, unknown>;\n if (!parsed || typeof parsed !== \"object\") {\n return null;\n }\n const normalized = normalizeParsedSettings(parsed);\n await writeSettingsFile(normalized);\n return normalized;\n } catch {\n return null;\n }\n}\n\nfunction normalizeParsedSettings(parsed: Record<string, unknown>): CodexSettings {\n const license = normalizeStoredLicense((parsed as CodexSettings).license ?? (parsed as Record<string, unknown>)[KEY_LICENSE]);\n const autoRoll = normalizeStoredAutoRoll((parsed as CodexSettings).autoRoll ?? (parsed as Record<string, unknown>)[KEY_AUTO_ROLL]);\n const lastProfileName = parseStoredProfileName(\n (parsed as CodexSettings).lastProfileName ?? (parsed as Record<string, unknown>)[KEY_LAST_PROFILE_NAME],\n );\n const lastAppVersion =\n typeof (parsed as CodexSettings).lastAppVersion === 'string'\n ? (parsed as CodexSettings).lastAppVersion\n : typeof (parsed as Record<string, unknown>)[KEY_LAST_APP_VERSION] === 'string'\n ? (parsed as Record<string, unknown>)[KEY_LAST_APP_VERSION] as string\n : null;\n const pendingUpdateVersion =\n typeof (parsed as CodexSettings).pendingUpdateVersion === 'string'\n ? (parsed as CodexSettings).pendingUpdateVersion\n : typeof (parsed as Record<string, unknown>)[KEY_PENDING_UPDATE_VERSION] === 'string'\n ? (parsed as Record<string, unknown>)[KEY_PENDING_UPDATE_VERSION] as string\n : null;\n const cliChannel = parseCodexCliChannel(\n (parsed as CodexSettings).cliChannel ?? (parsed as Record<string, unknown>)[KEY_CLI_CHANNEL],\n );\n\n return {\n ...(license ? { license } : {}),\n ...(autoRoll ? { autoRoll } : {}),\n ...(lastProfileName ? { lastProfileName } : {}),\n ...(lastAppVersion ? { lastAppVersion } : {}),\n ...(pendingUpdateVersion ? { pendingUpdateVersion } : {}),\n ...(cliChannel ? { cliChannel } : {}),\n };\n}\n\nasync function readSettingsFromDisk(): Promise<CodexSettings | null> {\n const settingsPath = resolveSettingsPath();\n try {\n const raw = await fs.readFile(settingsPath, 'utf8');\n const parsed = JSON.parse(raw) as Record<string, unknown>;\n if (!parsed || typeof parsed !== 'object') {\n return null;\n }\n return normalizeParsedSettings(parsed);\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return null;\n }\n if (error instanceof SyntaxError) {\n try {\n const raw = await fs.readFile(settingsPath, 'utf8');\n await markSettingsCorrupt(settingsPath, raw);\n\n const restoredFromTemp = await tryRestoreSettings(`${settingsPath}.tmp`, settingsPath);\n if (restoredFromTemp) {\n return restoredFromTemp;\n }\n\n const restoredFromBackup = await tryRestoreSettings(resolveBackupSettingsPath(), settingsPath);\n if (restoredFromBackup) {\n return restoredFromBackup;\n }\n\n await fs.writeFile(settingsPath, '{}\\n', 'utf8');\n } catch {\n // ignore backup/cleanup failures\n }\n }\n logWarn('Failed to parse Codex settings file, using defaults:', error);\n return null;\n }\n}\n\nasync function migrateSettingsFromDatabase(): Promise<CodexSettings | null> {\n try {\n const { __debugResolveDbPath, getDatabase } = await import('./sqlite-db');\n const dbPath = __debugResolveDbPath();\n try {\n await fs.access(dbPath);\n } catch {\n return null;\n }\n const db = await getDatabase();\n const rows = db\n .prepare(\n `SELECT key, value\n FROM app_kv\n WHERE key IN ('${KEY_LAST_PROFILE_NAME}', '${KEY_LICENSE}', '${KEY_AUTO_ROLL}', '${KEY_LAST_APP_VERSION}', '${KEY_PENDING_UPDATE_VERSION}', '${KEY_CLI_CHANNEL}')`,\n )\n .all() as Array<{ key?: string; value?: unknown }>;\n\n if (!rows || rows.length === 0) {\n return null;\n }\n\n const kv = new Map<string, unknown>();\n for (const row of rows) {\n if (!row.key) continue;\n if (typeof row.value === 'string') {\n kv.set(row.key, parseJsonValue(row.value));\n } else {\n kv.set(row.key, row.value);\n }\n }\n\n const license = normalizeStoredLicense(kv.get(KEY_LICENSE));\n const autoRoll = normalizeStoredAutoRoll(kv.get(KEY_AUTO_ROLL));\n const lastProfileName = parseStoredProfileName(kv.get(KEY_LAST_PROFILE_NAME));\n const lastAppVersion = typeof kv.get(KEY_LAST_APP_VERSION) === 'string' ? (kv.get(KEY_LAST_APP_VERSION) as string) : null;\n const pendingUpdateVersion =\n typeof kv.get(KEY_PENDING_UPDATE_VERSION) === 'string' ? (kv.get(KEY_PENDING_UPDATE_VERSION) as string) : null;\n const cliChannel = parseCodexCliChannel(kv.get(KEY_CLI_CHANNEL));\n\n const settings: CodexSettings = {\n ...(license ? { license } : {}),\n ...(autoRoll ? { autoRoll } : {}),\n ...(lastProfileName ? { lastProfileName } : {}),\n ...(lastAppVersion ? { lastAppVersion } : {}),\n ...(pendingUpdateVersion ? { pendingUpdateVersion } : {}),\n ...(cliChannel ? { cliChannel } : {}),\n };\n\n if (Object.keys(settings).length === 0) {\n return null;\n }\n\n await writeSettingsFile(settings);\n return settings;\n } catch (error) {\n logWarn('Failed to migrate Codex settings from database:', error);\n return null;\n }\n}\n\nasync function readSettingsFile(): Promise<CodexSettings> {\n await ensureCodexDir();\n\n const diskSettings = await readSettingsFromDisk();\n if (diskSettings) {\n return diskSettings;\n }\n\n const migrated = await migrateSettingsFromDatabase();\n if (migrated) {\n return migrated;\n }\n\n return {};\n}\n\nasync function writeSettingsFile(settings: CodexSettings): Promise<void> {\n await ensureCodexDir();\n const normalizedAutoRoll = settings.autoRoll ? normalizeAutoRollSettings(settings.autoRoll) : null;\n const normalizedLicense = settings.license\n ? {\n licenseKey: settings.license.licenseKey ?? null,\n purchaseEmail: settings.license.purchaseEmail ?? null,\n lastVerifiedAt: settings.license.lastVerifiedAt ?? null,\n nextCheckAt: settings.license.nextCheckAt ?? null,\n lastVerificationError: settings.license.lastVerificationError ?? null,\n status: settings.license.status ?? undefined,\n signature: settings.license.signature ?? null,\n }\n : null;\n\n const payload: CodexSettings = {\n ...(settings.lastProfileName ? { lastProfileName: settings.lastProfileName } : {}),\n ...(normalizedLicense ? { license: normalizedLicense } : {}),\n ...(normalizedAutoRoll ? { autoRoll: normalizedAutoRoll } : {}),\n ...(settings.lastAppVersion ? { lastAppVersion: settings.lastAppVersion } : {}),\n ...(settings.pendingUpdateVersion ? { pendingUpdateVersion: settings.pendingUpdateVersion } : {}),\n ...(settings.cliChannel ? { cliChannel: settings.cliChannel } : {}),\n };\n\n const serialized = JSON.stringify(payload, null, 2);\n const settingsPath = resolveSettingsPath();\n const backupPath = resolveBackupSettingsPath();\n const contents = `${serialized}\\n`;\n await writeAtomic(settingsPath, contents);\n try {\n await writeAtomic(backupPath, contents);\n } catch (error) {\n logWarn('Failed to write Codex settings backup:', error);\n }\n}\n\nfunction parseJsonValue(value?: unknown): unknown | null {\n if (typeof value !== 'string') {\n return null;\n }\n try {\n return JSON.parse(value);\n } catch {\n return value;\n }\n}\n\nexport async function getCodexSettings(): Promise<CodexSettings> {\n return readSettingsFile();\n}\n\nexport async function setCodexSettings(settings: CodexSettings): Promise<void> {\n await writeSettingsFile(settings);\n}\n\nexport async function getLastProfileName(): Promise<string | null> {\n const settings = await getCodexSettings();\n const candidate = settings.lastProfileName;\n if (typeof candidate !== 'string') {\n return null;\n }\n const trimmed = candidate.trim();\n return trimmed.length === 0 ? null : trimmed;\n}\n\nexport async function persistLastProfileName(profileName: string | null): Promise<void> {\n const settings = await getCodexSettings();\n if (profileName && profileName.trim().length > 0) {\n settings.lastProfileName = profileName.trim();\n } else if (settings.lastProfileName) {\n delete settings.lastProfileName;\n }\n await setCodexSettings(settings);\n}\n\nexport async function getStoredLicense(): Promise<StoredLicense | null> {\n const settings = await getCodexSettings();\n if (!settings.license) {\n return null;\n }\n return settings.license;\n}\n\nexport async function persistLicense(license: StoredLicense | null): Promise<void> {\n const settings = await getCodexSettings();\n if (license) {\n settings.license = {\n licenseKey: license.licenseKey ?? null,\n purchaseEmail: license.purchaseEmail ?? null,\n lastVerifiedAt: license.lastVerifiedAt ?? null,\n nextCheckAt: license.nextCheckAt ?? null,\n lastVerificationError: license.lastVerificationError ?? null,\n status: license.status ?? 'inactive',\n signature: license.signature ?? null,\n };\n } else if (settings.license) {\n delete settings.license;\n }\n\n await setCodexSettings(settings);\n}\n\nexport async function getStoredAutoRollSettings(): Promise<AutoRollSettings | null> {\n const settings = await getCodexSettings();\n if (!settings.autoRoll) {\n return null;\n }\n\n return normalizeAutoRollSettings(settings.autoRoll);\n}\n\nexport async function persistAutoRollSettings(settings: AutoRollSettings | null): Promise<void> {\n const current = await getCodexSettings();\n if (settings) {\n current.autoRoll = normalizeAutoRollSettings(settings);\n } else if (current.autoRoll) {\n delete current.autoRoll;\n }\n\n await setCodexSettings(current);\n}\n\nexport async function getStoredAppVersion(): Promise<string | null> {\n const settings = await getCodexSettings();\n const candidate = settings.lastAppVersion;\n if (typeof candidate !== 'string') {\n return null;\n }\n const trimmed = candidate.trim();\n return trimmed.length === 0 ? null : trimmed;\n}\n\nexport async function persistAppVersion(version: string | null): Promise<void> {\n const settings = await getCodexSettings();\n if (version && version.trim().length > 0) {\n settings.lastAppVersion = version.trim();\n } else if (settings.lastAppVersion) {\n delete settings.lastAppVersion;\n }\n\n await setCodexSettings(settings);\n}\n\nexport async function getPendingUpdateVersion(): Promise<string | null> {\n const settings = await getCodexSettings();\n const candidate = settings.pendingUpdateVersion;\n if (typeof candidate !== 'string') {\n return null;\n }\n const trimmed = candidate.trim();\n return trimmed.length === 0 ? null : trimmed;\n}\n\nexport async function persistPendingUpdateVersion(version: string | null): Promise<void> {\n const settings = await getCodexSettings();\n if (version && version.trim().length > 0) {\n settings.pendingUpdateVersion = version.trim();\n } else if (settings.pendingUpdateVersion) {\n delete settings.pendingUpdateVersion;\n }\n\n await setCodexSettings(settings);\n}\n\nexport async function getCodexCliChannel(): Promise<CodexCliChannel> {\n const settings = await getCodexSettings();\n return normalizeCodexCliChannel(settings.cliChannel);\n}\n\nexport async function persistCodexCliChannel(channel: CodexCliChannel | null): Promise<void> {\n const settings = await getCodexSettings();\n const normalized = channel ? normalizeCodexCliChannel(channel) : null;\n if (!normalized || normalized === 'stable') {\n if (settings.cliChannel) {\n delete settings.cliChannel;\n }\n } else {\n settings.cliChannel = normalized;\n }\n await setCodexSettings(settings);\n}\n\nexport type { StoredLicense, LicenseStatusCode };\nexport type { AutoRollSettings } from './auto-roll-settings';\nexport type { CodexCliChannel } from './codex-cli-channel';\n","const isTestEnv =\n process.env.NODE_ENV === 'test' ||\n process.env.VITEST === 'true' ||\n process.env.VITEST === '1';\n\nfunction isMocked(fn: unknown): boolean {\n return Boolean(fn && typeof fn === 'function' && 'mock' in (fn as Record<string, unknown>));\n}\n\nexport function logWarn(...args: Parameters<typeof console.warn>): void {\n if (isTestEnv && !isMocked(console.warn)) {\n return;\n }\n console.warn(...args);\n}\n\nexport function logError(...args: Parameters<typeof console.error>): void {\n if (isTestEnv && !isMocked(console.error)) {\n return;\n }\n console.error(...args);\n}\n\nexport function logInfo(...args: Parameters<typeof console.warn>): void {\n if (isTestEnv && !isMocked(console.warn)) {\n return;\n }\n console.warn(...args);\n}\n","export type CodexCliChannel = \"stable\" | \"alpha\";\n\nconst KNOWN_CHANNELS = new Set<CodexCliChannel>([\"stable\", \"alpha\"]);\n\nexport function parseCodexCliChannel(value?: unknown): CodexCliChannel | null {\n if (typeof value !== \"string\") {\n return null;\n }\n\n const normalized = value.trim().toLowerCase();\n if (!normalized) {\n return null;\n }\n\n return KNOWN_CHANNELS.has(normalized as CodexCliChannel)\n ? (normalized as CodexCliChannel)\n : null;\n}\n\nexport function normalizeCodexCliChannel(\n value?: unknown,\n fallback: CodexCliChannel = \"stable\",\n): CodexCliChannel {\n return parseCodexCliChannel(value) ?? fallback;\n}\n","import crypto from 'node:crypto';\nimport { persistLicense, getStoredLicense, type StoredLicense } from './codex-settings';\nimport { getLicenseSecret } from './license-secret';\n\nconst PRODUCT_PERMALINK = 'codex-use';\nconst PRODUCT_ID = '3_CcyVEXt2FOMiEpPx8xzw==';\nconst BASE_PRICE = 19;\nconst BASE_PRICE_DISPLAY = '$19';\nconst PROMO_CODE = 'NOELNEWYEAR50';\nconst PROMO_PERCENT = 50;\nconst PROMO_ACTIVE = true;\nconst PROMO_PRICE_DISPLAY = `$${(BASE_PRICE * (100 - PROMO_PERCENT) / 100).toFixed(2)}`;\nconst PRODUCT_URL = PROMO_ACTIVE\n ? `https://hweihwang.gumroad.com/l/${PRODUCT_PERMALINK}/${PROMO_CODE}`\n : 'https://hweihwang.gumroad.com/l/codex-use';\nconst LICENSE_PRICE_DISPLAY = PROMO_ACTIVE ? PROMO_PRICE_DISPLAY : BASE_PRICE_DISPLAY;\nconst LICENSE_MAX_USES = 5;\nconst FREE_PROFILE_LIMIT = 2;\nconst LICENSE_REFRESH_INTERVAL_MS = 5 * 60 * 1000;\nconst MAX_NEXT_CHECK_MS = 60 * 60 * 1000;\nconst GRACE_MAX_AGE_MS = 3 * 60 * 60 * 1000;\n\ntype LicenseTier = 'free' | 'pro';\ntype LicenseState = 'inactive' | 'active' | 'grace' | 'verifying' | 'error';\n\ninterface LicenseStatus {\n tier: LicenseTier;\n state: LicenseState;\n isPro: boolean;\n profileLimit: number | null;\n profilesRemaining: number | null;\n purchaseEmail: string | null;\n maskedLicenseKey: string | null;\n lastVerifiedAt: string | null;\n nextCheckAt: string | null;\n message: string | null;\n error: string | null;\n productPermalink: string;\n productUrl: string;\n productId: string;\n priceDisplay: string;\n basePriceDisplay: string;\n promoCode: string | null;\n promoPercent: number | null;\n}\n\ninterface GumroadLicenseResponse {\n success: boolean;\n message?: string;\n uses?: number;\n purchase?: {\n id: string;\n email: string;\n product_id?: string;\n product_permalink?: string;\n refunded?: boolean;\n chargebacked?: boolean;\n subscription_cancelled?: boolean;\n subscription_failed?: boolean;\n license_disabled?: boolean;\n variants?: string;\n };\n license_disabled?: boolean;\n}\n\ntype VerificationMode = 'activation' | 'refresh';\n\nclass LicenseError extends Error {\n readonly code: 'network' | 'invalid' | 'revoked';\n\n constructor(message: string, code: 'network' | 'invalid' | 'revoked') {\n super(message);\n this.name = 'LicenseError';\n this.code = code;\n }\n}\n\nfunction nowIso(): string {\n return new Date().toISOString();\n}\n\nfunction maskLicenseKey(key: string | null | undefined): string | null {\n if (!key || typeof key !== 'string') {\n return null;\n }\n\n const trimmed = key.trim();\n if (trimmed.length <= 4) {\n return '••••';\n }\n\n const visible = trimmed.slice(-4);\n return `••••${visible}`;\n}\n\nfunction resolveProfilesRemaining(limit: number | null, currentProfiles: number): number | null {\n if (typeof limit !== 'number') {\n return null;\n }\n return Math.max(0, limit - currentProfiles);\n}\n\nfunction determineTierFromStored(stored: StoredLicense | null): LicenseTier {\n if (!stored || !stored.licenseKey) {\n return 'free';\n }\n\n if (stored.status === 'inactive') {\n return 'free';\n }\n\n return 'pro';\n}\n\nfunction licenseSignaturePayload(license: StoredLicense): string {\n const payload = {\n licenseKey: license.licenseKey ?? null,\n purchaseEmail: license.purchaseEmail ?? null,\n lastVerifiedAt: license.lastVerifiedAt ?? null,\n nextCheckAt: license.nextCheckAt ?? null,\n lastVerificationError: license.lastVerificationError ?? null,\n status: license.status ?? null,\n };\n return JSON.stringify(payload);\n}\n\nfunction signLicense(license: StoredLicense, secret: string): string {\n return crypto.createHmac('sha256', secret).update(licenseSignaturePayload(license)).digest('hex');\n}\n\nfunction withSignature(license: StoredLicense, secret: string): StoredLicense {\n return {\n ...license,\n signature: signLicense(license, secret),\n };\n}\n\nfunction isSignatureValid(license: StoredLicense, secret: string): boolean {\n if (!license.signature) {\n return false;\n }\n const expected = signLicense(license, secret);\n return expected === license.signature;\n}\n\nasync function requestGumroadVerify(licenseKey: string, mode: VerificationMode): Promise<GumroadLicenseResponse> {\n const controller = new AbortController();\n const timeout = setTimeout(() => controller.abort(), 10_000);\n\n try {\n const body = new URLSearchParams({\n product_permalink: PRODUCT_PERMALINK,\n product_id: PRODUCT_ID,\n license_key: licenseKey,\n increment_uses_count: mode === 'activation' ? 'true' : 'false',\n });\n\n const response = await fetch('https://api.gumroad.com/v2/licenses/verify', {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded',\n },\n body,\n signal: controller.signal,\n });\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => null);\n const message = errorText\n ? `License verification failed (${response.status}): ${errorText}`\n : `License verification failed (${response.status}).`;\n throw new LicenseError(message, 'network');\n }\n\n const json = (await response.json()) as GumroadLicenseResponse;\n return json;\n } catch (error) {\n if (error instanceof LicenseError) {\n throw error;\n }\n\n if (error instanceof Error && error.name === 'AbortError') {\n throw new LicenseError('License verification timed out.', 'network');\n }\n\n throw new LicenseError(\n error instanceof Error ? error.message : 'Unable to reach Gumroad for license verification.',\n 'network',\n );\n } finally {\n clearTimeout(timeout);\n }\n}\n\nfunction normalizeVerificationResult(response: GumroadLicenseResponse): { email: string | null } {\n if (!response.success) {\n const message = response.message ?? 'License key is invalid.';\n throw new LicenseError(message, 'invalid');\n }\n\n if (!response.purchase) {\n throw new LicenseError('License verification response was incomplete.', 'network');\n }\n\n if (response.purchase.chargebacked || response.purchase.refunded || response.license_disabled) {\n throw new LicenseError('This license has been revoked or refunded.', 'revoked');\n }\n\n if (response.purchase.subscription_cancelled || response.purchase.subscription_failed) {\n throw new LicenseError('This license subscription is no longer active.', 'revoked');\n }\n\n const email = response.purchase.email ?? null;\n return { email };\n}\n\nfunction ensureWithinUseLimit(response: GumroadLicenseResponse): void {\n if (!response.success) {\n return;\n }\n\n if (typeof response.uses !== 'number') {\n return;\n }\n\n if (response.uses > LICENSE_MAX_USES) {\n throw new LicenseError(\n `This license has reached the activation limit (${LICENSE_MAX_USES}). Contact support to move it to another device.`,\n 'invalid',\n );\n }\n}\n\nfunction toLicenseStatus(stored: StoredLicense | null, overrides: Partial<LicenseStatus> = {}): LicenseStatus {\n const tier = determineTierFromStored(stored);\n const isPro = tier === 'pro';\n\n const state: LicenseState =\n stored && stored.status\n ? stored.status\n : isPro\n ? 'active'\n : 'inactive';\n\n const base: LicenseStatus = {\n tier,\n state,\n isPro,\n profileLimit: isPro ? null : FREE_PROFILE_LIMIT,\n profilesRemaining: null,\n purchaseEmail: stored?.purchaseEmail ?? null,\n maskedLicenseKey: maskLicenseKey(stored?.licenseKey),\n lastVerifiedAt: stored?.lastVerifiedAt ?? null,\n nextCheckAt: stored?.nextCheckAt ?? null,\n message: null,\n error: stored?.lastVerificationError ?? null,\n productUrl: PRODUCT_URL,\n productId: PRODUCT_ID,\n productPermalink: PRODUCT_PERMALINK,\n priceDisplay: LICENSE_PRICE_DISPLAY,\n basePriceDisplay: BASE_PRICE_DISPLAY,\n promoCode: PROMO_ACTIVE ? PROMO_CODE : null,\n promoPercent: PROMO_ACTIVE ? PROMO_PERCENT : null,\n };\n\n return { ...base, ...overrides };\n}\n\nfunction parseTimestamp(value: string | null | undefined): number | null {\n if (typeof value !== 'string' || value.trim() === '') {\n return null;\n }\n\n const parsed = Date.parse(value);\n return Number.isNaN(parsed) ? null : parsed;\n}\n\nclass LicenseService {\n private cache: LicenseStatus | null = null;\n private refreshPromise: Promise<void> | null = null;\n private verificationPromise: Promise<LicenseStatus> | null = null;\n\n async getCachedStatus(): Promise<LicenseStatus> {\n return this.getStatus();\n }\n\n async getStatus(options: { forceRefresh?: boolean } = {}): Promise<LicenseStatus> {\n const forceRefresh = Boolean(options.forceRefresh);\n const secret = await getLicenseSecret();\n const stored = await getStoredLicense();\n\n if (!stored?.licenseKey) {\n const status = toLicenseStatus(null);\n this.cache = status;\n return status;\n }\n\n const now = Date.now();\n const nextCheckTs = parseTimestamp(stored.nextCheckAt);\n const lastVerifiedTs = parseTimestamp(stored.lastVerifiedAt);\n const graceStale =\n (stored.status === 'grace' || stored.status === 'error') &&\n (lastVerifiedTs === null || lastVerifiedTs + GRACE_MAX_AGE_MS < now);\n\n let workingStored: StoredLicense = { ...stored };\n const signatureValid = isSignatureValid(workingStored, secret);\n\n let cappedNextCheckTs = nextCheckTs;\n\n if (nextCheckTs !== null && nextCheckTs > now + MAX_NEXT_CHECK_MS) {\n cappedNextCheckTs = now;\n const updated = {\n ...workingStored,\n nextCheckAt: new Date(cappedNextCheckTs).toISOString(),\n };\n const signed = withSignature(updated, secret);\n workingStored = signed;\n await persistLicense(signed);\n }\n\n const tampered = Boolean(workingStored.licenseKey) && !signatureValid;\n\n const cached = tampered\n ? toLicenseStatus(null, {\n state: 'verifying',\n message: 'License data changed, rechecking.',\n error: 'Untrusted license data.',\n })\n : toLicenseStatus(workingStored);\n this.cache = cached;\n\n const shouldRefresh =\n forceRefresh || graceStale || tampered || cappedNextCheckTs === null || cappedNextCheckTs <= now;\n\n if (!shouldRefresh) {\n return cached;\n }\n\n if (this.verificationPromise && !forceRefresh) {\n return this.verificationPromise;\n }\n\n const verify = async (): Promise<LicenseStatus> => {\n try {\n const result = await requestGumroadVerify(workingStored.licenseKey!, 'refresh');\n ensureWithinUseLimit(result);\n const normalized = normalizeVerificationResult(result);\n const updated: StoredLicense = {\n licenseKey: workingStored.licenseKey,\n purchaseEmail: normalized.email,\n lastVerifiedAt: nowIso(),\n nextCheckAt: new Date(Date.now() + LICENSE_REFRESH_INTERVAL_MS).toISOString(),\n status: 'active',\n lastVerificationError: null,\n };\n\n const signed = withSignature(updated, secret);\n\n await persistLicense(signed);\n const status = toLicenseStatus(signed);\n this.cache = status;\n return status;\n } catch (error) {\n const message = error instanceof Error ? error.message : 'License verification failed.';\n\n if (error instanceof LicenseError && error.code === 'network') {\n const fallbackStored: StoredLicense = withSignature(\n {\n ...workingStored,\n status: tampered ? 'inactive' : workingStored.status === 'inactive' ? 'inactive' : 'grace',\n lastVerificationError: message,\n nextCheckAt: new Date(Date.now() + LICENSE_REFRESH_INTERVAL_MS).toISOString(),\n },\n secret,\n );\n await persistLicense(fallbackStored);\n const fallback = toLicenseStatus(fallbackStored, {\n state: fallbackStored.status ?? 'grace',\n error: message,\n });\n this.cache = fallback;\n return fallback;\n }\n\n const fallbackStored: StoredLicense = withSignature({\n licenseKey: workingStored.licenseKey,\n purchaseEmail: workingStored.purchaseEmail ?? null,\n lastVerifiedAt: workingStored.lastVerifiedAt ?? null,\n nextCheckAt: null,\n status: 'inactive',\n lastVerificationError: message,\n }, secret);\n await persistLicense(fallbackStored);\n const fallback = toLicenseStatus(fallbackStored, { error: message });\n this.cache = fallback;\n return fallback;\n }\n };\n\n this.verificationPromise = verify();\n try {\n return await this.verificationPromise;\n } finally {\n this.verificationPromise = null;\n }\n }\n\n async activate(licenseKey: string): Promise<LicenseStatus> {\n const trimmed = licenseKey.trim();\n if (!trimmed) {\n throw new Error('License key is required.');\n }\n\n const secret = await getLicenseSecret();\n const digest = crypto.createHash('sha256').update(trimmed).digest('hex');\n\n const result = await requestGumroadVerify(trimmed, 'activation');\n ensureWithinUseLimit(result);\n const normalized = normalizeVerificationResult(result);\n const stored: StoredLicense = {\n licenseKey: trimmed,\n purchaseEmail: normalized.email,\n lastVerifiedAt: nowIso(),\n nextCheckAt: new Date(Date.now() + LICENSE_REFRESH_INTERVAL_MS).toISOString(),\n status: 'active',\n lastVerificationError: null,\n };\n\n const signed = withSignature(stored, secret);\n\n await persistLicense(signed);\n const status = toLicenseStatus(signed, {\n message: `License verified (${digest.slice(0, 8)}).`,\n });\n this.cache = status;\n return status;\n }\n\n applyProfileCount(status: LicenseStatus, profileCount: number): LicenseStatus {\n const profilesRemaining = resolveProfilesRemaining(status.profileLimit, profileCount);\n return {\n ...status,\n profilesRemaining,\n };\n }\n\n refreshStatusInBackground(options: { force?: boolean } = {}): void {\n if (this.refreshPromise) {\n return;\n }\n\n const forceRefresh = Boolean(options.force);\n this.refreshPromise = (async () => {\n try {\n await this.getStatus({ forceRefresh });\n } catch (error) {\n console.warn('Background license refresh failed:', error);\n } finally {\n this.refreshPromise = null;\n }\n })();\n }\n}\n\nexport const licenseService = new LicenseService();\nexport type { LicenseStatus, LicenseTier, LicenseState };\nexport { FREE_PROFILE_LIMIT, LICENSE_PRICE_DISPLAY, PRODUCT_PERMALINK, PRODUCT_URL, PRODUCT_ID };\n\nfunction isExplicitInvalidation(message: string | null | undefined): boolean {\n if (!message) {\n return false;\n }\n const normalized = message.toLowerCase();\n return (\n normalized.includes('revoked') ||\n normalized.includes('refunded') ||\n normalized.includes('invalid') ||\n normalized.includes('no longer active')\n );\n}\n\n/**\n * Determines when it is safe to enforce profile limits (e.g., pruning).\n * We skip enforcement when the license state is uncertain (grace/error/verifying)\n * and only enforce limits for true free/unlicensed states or explicit revocations.\n */\nexport function shouldEnforceProfileLimit(status: LicenseStatus): boolean {\n if (status.isPro) {\n return false;\n }\n\n if (status.state === 'grace' || status.state === 'verifying' || status.state === 'error') {\n return false;\n }\n\n if (isExplicitInvalidation(status.error)) {\n return true;\n }\n\n return status.state === 'inactive';\n}\n","import { promises as fs } from 'node:fs';\nimport path from 'node:path';\nimport crypto from 'node:crypto';\n\nconst SECRET_FILE = 'license.secret';\n\nfunction resolveCodexDir(): string {\n const homeDir = process.env.HOME || process.env.USERPROFILE || '';\n return homeDir ? path.join(homeDir, '.codex') : '.codex';\n}\n\nfunction resolveSecretPath(): string {\n return path.join(resolveCodexDir(), SECRET_FILE);\n}\n\nasync function generateSecret(): Promise<string> {\n return crypto.randomBytes(32).toString('hex');\n}\n\nexport async function getLicenseSecret(): Promise<string> {\n const secretPath = resolveSecretPath();\n try {\n const existing = await fs.readFile(secretPath, 'utf8');\n const trimmed = existing.trim();\n if (trimmed.length > 0) {\n return trimmed;\n }\n } catch {\n // fall through and create a new secret\n }\n\n const secret = await generateSecret();\n try {\n await fs.mkdir(path.dirname(secretPath), { recursive: true });\n } catch {\n // best effort\n }\n\n await fs.writeFile(secretPath, `${secret}\\n`, { mode: 0o600 });\n return secret;\n}\n","import { licenseService } from \"./license-service\";\nimport { ProfileManager } from \"./profile-manager\";\n\nexport async function assertProfileCreationAllowed(\n profileManager?: ProfileManager,\n): Promise<void> {\n const manager = profileManager ?? new ProfileManager();\n await manager.initialize();\n\n const license = await licenseService.getStatus();\n const profiles = await manager.listProfiles();\n const licenseWithCounts = licenseService.applyProfileCount(license, profiles.length);\n\n if (\n typeof licenseWithCounts.profileLimit === \"number\" &&\n licenseWithCounts.profilesRemaining !== null &&\n licenseWithCounts.profilesRemaining <= 0\n ) {\n throw new Error(\"CodexUse Free supports up to 2 profiles. Upgrade to CodexUse Pro for unlimited profiles.\");\n }\n}\n","import { spawn } from \"node:child_process\";\nimport { statSync } from \"node:fs\";\nimport path from \"node:path\";\n\nconst ENV_HINTS = [\"CODEX_BINARY\", \"CODEX_CLI_PATH\", \"CODEX_PATH\"] as const;\nconst NODE_MODULE_CANDIDATES = [\n [\"@openai\", \"codex\"],\n [\"@openai\", \"codex-alpha\"],\n];\n\nexport type CodexLoginMode = \"browser\" | \"device\";\n\nfunction fileExists(candidate: string | null | undefined): string | null {\n if (!candidate) return null;\n const resolved = path.resolve(candidate);\n try {\n const stat = statSync(resolved);\n if (stat.isFile()) return resolved;\n } catch {\n return null;\n }\n return null;\n}\n\nfunction resolveFromEnv(): string | null {\n for (const key of ENV_HINTS) {\n const value = process.env[key];\n if (!value) continue;\n const resolved = fileExists(value);\n if (resolved) return resolved;\n }\n return null;\n}\n\nfunction resolveFromAppResources(): string | null {\n const roots = [\n path.resolve(__dirname, \"..\"),\n path.resolve(__dirname, \"..\", \"..\"),\n ];\n\n for (const root of roots) {\n const base = path.join(root, \"app.asar.unpacked\", \"node_modules\");\n for (const segments of NODE_MODULE_CANDIDATES) {\n const candidate = fileExists(path.join(base, ...segments, \"bin\", \"codex\"));\n if (candidate) return candidate;\n const jsCandidate = fileExists(path.join(base, ...segments, \"bin\", \"codex.js\"));\n if (jsCandidate) return jsCandidate;\n }\n }\n\n return null;\n}\n\nfunction resolveFromNodeModules(): string | null {\n let current = path.resolve(__dirname, \"..\");\n let last = \"\";\n\n while (current !== last) {\n for (const segments of NODE_MODULE_CANDIDATES) {\n const candidate = fileExists(path.join(current, \"node_modules\", ...segments, \"bin\", \"codex\"));\n if (candidate) return candidate;\n const jsCandidate = fileExists(path.join(current, \"node_modules\", ...segments, \"bin\", \"codex.js\"));\n if (jsCandidate) return jsCandidate;\n }\n last = current;\n current = path.dirname(current);\n }\n\n return null;\n}\n\nfunction resolveFromPath(): string | null {\n const pathValue = process.env.PATH ?? \"\";\n const entries = pathValue.split(path.delimiter).filter(Boolean);\n const names = process.platform === \"win32\"\n ? [\"codex.exe\", \"codex.cmd\", \"codex.bat\", \"codex\"]\n : [\"codex\"];\n\n for (const entry of entries) {\n for (const name of names) {\n const candidate = fileExists(path.join(entry, name));\n if (candidate) return candidate;\n }\n }\n\n return null;\n}\n\nexport function resolveCodexBinary(): string | null {\n return (\n resolveFromEnv() ||\n resolveFromAppResources() ||\n resolveFromNodeModules() ||\n resolveFromPath()\n );\n}\n\nexport function requireCodexBinary(context?: string): string {\n const resolved = resolveCodexBinary();\n if (resolved) return resolved;\n const hint = \"Install Codex CLI (npm i -g @openai/codex) or set CODEX_BINARY.\";\n const message = context ? `${context} ${hint}` : hint;\n throw new Error(message);\n}\n\nfunction buildCodexCommand(codexPath: string, args: string[]): { command: string; args: string[]; shell: boolean } {\n const normalized = codexPath.toLowerCase();\n const isJs = normalized.endsWith(\".js\");\n if (isJs) {\n return { command: process.execPath, args: [codexPath, ...args], shell: false };\n }\n\n const useShell = process.platform === \"win32\";\n return { command: codexPath, args, shell: useShell };\n}\n\nfunction isHeadless(): boolean {\n if (process.env.CODEXUSE_HEADLESS === \"1\") return true;\n if (process.env.CI) return true;\n if (process.env.SSH_CONNECTION || process.env.SSH_TTY) return true;\n if (process.platform === \"linux\") {\n if (!process.env.DISPLAY && !process.env.WAYLAND_DISPLAY) return true;\n }\n return false;\n}\n\nexport function resolveLoginMode(preferred?: CodexLoginMode | null): CodexLoginMode {\n if (preferred === \"browser\" || preferred === \"device\") {\n return preferred;\n }\n const envMode = process.env.CODEXUSE_LOGIN_MODE;\n if (envMode === \"browser\" || envMode === \"device\") {\n return envMode;\n }\n return isHeadless() ? \"device\" : \"browser\";\n}\n\nexport async function runCodexLogin(mode?: CodexLoginMode | null): Promise<void> {\n const codexPath = requireCodexBinary(\"Codex CLI is required to login.\");\n const resolvedMode = resolveLoginMode(mode ?? null);\n const loginArgs = resolvedMode === \"device\"\n ? [\"login\", \"--device-auth\"]\n : [\"login\"];\n const { command, args, shell } = buildCodexCommand(codexPath, loginArgs);\n\n const child = spawn(command, args, {\n stdio: \"inherit\",\n env: process.env,\n shell,\n });\n\n const exitCode = await new Promise<number>((resolve) => {\n child.on(\"close\", (code) => resolve(code ?? 0));\n });\n\n if (exitCode !== 0) {\n throw new Error(`Codex CLI login failed (exit code ${exitCode}).`);\n }\n}\n","import { spawn } from \"node:child_process\";\nimport readline from \"node:readline\";\nimport { once } from \"node:events\";\nimport { promises as fs } from \"node:fs\";\nimport os from \"node:os\";\nimport path from \"node:path\";\nimport { requireCodexCli } from \"./codex-cli\";\nimport type { RateLimitSnapshot, RateLimitWindow } from \"./types\";\n\ntype RpcMessage = { id?: number; result?: unknown; error?: unknown };\n\ntype RpcRateLimitWindow = {\n usedPercent?: number;\n windowDurationMins?: number;\n resetsAt?: number;\n};\n\ntype RpcRateLimitSnapshot = {\n primary?: RpcRateLimitWindow | null;\n secondary?: RpcRateLimitWindow | null;\n};\n\nconst RPC_TIMEOUT_MS = 10_000;\n\nasync function sendPayload(\n child: ReturnType<typeof spawn>,\n payload: Record<string, unknown>,\n): Promise<void> {\n child.stdin?.write(JSON.stringify(payload));\n child.stdin?.write(\"\\n\");\n}\n\nasync function readMessage(\n rl: readline.Interface,\n timeoutMs: number,\n): Promise<RpcMessage> {\n const timeout = new Promise<never>((_, reject) => {\n const timer = setTimeout(() => {\n clearTimeout(timer);\n reject(new Error(\"codex RPC timed out\"));\n }, timeoutMs);\n });\n\n const linePromise = once(rl, \"line\").then(args => args[0] as string);\n\n const line = (await Promise.race([linePromise, timeout])) as string;\n try {\n return JSON.parse(line) as RpcMessage;\n } catch {\n throw new Error(\"codex RPC returned malformed JSON\");\n }\n}\n\nfunction toWindow(rpc?: RpcRateLimitWindow | null): RateLimitWindow | undefined {\n if (!rpc) return undefined;\n const usedPercent =\n typeof rpc.usedPercent === \"number\" && Number.isFinite(rpc.usedPercent)\n ? rpc.usedPercent\n : undefined;\n const windowMinutes =\n typeof rpc.windowDurationMins === \"number\" && Number.isFinite(rpc.windowDurationMins)\n ? rpc.windowDurationMins\n : undefined;\n\n let resetsAt: string | undefined;\n let resetsInSeconds: number | undefined;\n if (typeof rpc.resetsAt === \"number\" && Number.isFinite(rpc.resetsAt)) {\n const ms = rpc.resetsAt > 10_000_000_000 ? rpc.resetsAt : rpc.resetsAt * 1000;\n resetsAt = new Date(ms).toISOString();\n resetsInSeconds = Math.max(0, Math.round((ms - Date.now()) / 1000));\n }\n\n if (\n typeof usedPercent === \"undefined\" &&\n typeof windowMinutes === \"undefined\" &&\n typeof resetsAt === \"undefined\"\n ) {\n return undefined;\n }\n\n const window: RateLimitWindow = {};\n if (typeof usedPercent === \"number\") {\n window.usedPercent = usedPercent;\n }\n if (typeof windowMinutes === \"number\") {\n window.windowMinutes = windowMinutes;\n }\n if (typeof resetsAt === \"string\") {\n window.resetsAt = resetsAt;\n }\n if (typeof resetsInSeconds === \"number\") {\n window.resetsInSeconds = resetsInSeconds;\n }\n return window;\n}\n\nexport async function fetchRateLimitsViaRpc(\n envOverride?: NodeJS.ProcessEnv,\n options: { authPath?: string; codexPath?: string } = {},\n): Promise<RateLimitSnapshot | null> {\n const binaryPath = options.codexPath ?? await requireCodexCli();\n const tempHome = await fs.mkdtemp(path.join(os.tmpdir(), \"codex-rpc-\"));\n\n const sourceAuthPath =\n options.authPath ??\n (envOverride?.CODEX_HOME\n ? path.join(envOverride.CODEX_HOME, \"auth.json\")\n : path.join(\n envOverride?.HOME ?? process.env.HOME ?? process.env.USERPROFILE ?? os.homedir(),\n \".codex\",\n \"auth.json\",\n ));\n\n try {\n const authContent = await fs.readFile(sourceAuthPath, \"utf8\").catch(() => null);\n if (!authContent) {\n return null;\n }\n await fs.writeFile(path.join(tempHome, \"auth.json\"), authContent, \"utf8\");\n } catch {\n await fs.rm(tempHome, { recursive: true, force: true }).catch(() => {});\n return null;\n }\n\n const child = spawn(process.execPath, [binaryPath, \"-s\", \"read-only\", \"-a\", \"untrusted\", \"app-server\"], {\n stdio: [\"pipe\", \"pipe\", \"pipe\"],\n env: {\n ...process.env,\n ...(envOverride ?? {}),\n HOME: tempHome,\n USERPROFILE: tempHome,\n CODEX_HOME: tempHome,\n CODEX_TELEMETRY_LABEL: \"codex-rpc\",\n ELECTRON_RUN_AS_NODE: \"1\",\n },\n });\n\n const rl = readline.createInterface({\n input: child.stdout!,\n crlfDelay: Infinity,\n });\n\n try {\n await sendPayload(child, {\n id: 1,\n method: \"initialize\",\n params: { clientInfo: { name: \"codexuse\", version: \"0.0.0\" } },\n });\n await readMessage(rl, RPC_TIMEOUT_MS);\n\n await sendPayload(child, { method: \"initialized\", params: {} });\n\n await sendPayload(child, { id: 2, method: \"account/rateLimits/read\", params: {} });\n const message = await readMessage(rl, RPC_TIMEOUT_MS);\n const result = message.result as { rateLimits?: RpcRateLimitSnapshot } | undefined;\n const limits = result?.rateLimits;\n const primary = toWindow(limits?.primary ?? null);\n const secondary = toWindow(limits?.secondary ?? null);\n\n if (!primary && !secondary) {\n return null;\n }\n\n const snapshot: RateLimitSnapshot = {\n lastUpdated: new Date().toISOString(),\n source: \"codex-rpc\",\n };\n if (primary) {\n snapshot.primary = primary;\n }\n if (secondary) {\n snapshot.secondary = secondary;\n }\n return snapshot;\n } finally {\n child.kill();\n rl.close();\n await fs.rm(tempHome, { recursive: true, force: true }).catch(() => {});\n }\n}\n","import { readFileSync, statSync } from \"node:fs\";\nimport path from \"node:path\";\nimport type { CodexCliInfoPayload, CodexCliStatusPayload } from \"./ipc-types\";\nimport { getCodexCliChannel } from \"./codex-settings\";\nimport type { CodexCliChannel } from \"./codex-cli-channel\";\n\ntype MutableStatus = CodexCliStatusPayload;\n\nlet cachedStatus: MutableStatus | null = null;\nlet cachedChannel: CodexCliChannel | null = null;\n\nfunction fileExists(candidate: string | undefined | null): string | null {\n if (!candidate) {\n return null;\n }\n\n const normalized = path.resolve(candidate);\n\n try {\n const stats = statSync(normalized);\n if (stats.isFile()) {\n return normalized;\n }\n } catch {\n // ignore missing paths\n }\n\n return null;\n}\n\nfunction resolveBundledCodexBinary(channel: CodexCliChannel): string | null {\n const candidates: string[] = [];\n const processWithResources = process as NodeJS.Process & { resourcesPath?: string };\n const resourcesPath = processWithResources.resourcesPath;\n const packageSegments = channel === \"alpha\" ? [\"@openai\", \"codex-alpha\"] : [\"@openai\", \"codex\"];\n\n if (resourcesPath) {\n candidates.push(\n path.join(resourcesPath, \"app.asar.unpacked\", \"node_modules\", ...packageSegments, \"bin\", \"codex.js\"),\n );\n candidates.push(\n path.join(resourcesPath, \"app.asar.unpacked\", \"node_modules\", ...packageSegments, \"bin\", \"codex\"),\n );\n }\n\n const projectRoot = process.cwd();\n candidates.push(path.join(projectRoot, \"node_modules\", ...packageSegments, \"bin\", \"codex.js\"));\n candidates.push(path.join(projectRoot, \"node_modules\", ...packageSegments, \"bin\", \"codex\"));\n\n for (const candidate of candidates) {\n const resolved = fileExists(candidate);\n if (resolved) {\n return resolved;\n }\n }\n\n return null;\n}\n\nfunction resolvePackageJsonPath(cliPath: string): string | null {\n const normalized = path.resolve(cliPath);\n const binDir = path.dirname(normalized);\n const packageDir = path.basename(binDir) === \"bin\" ? path.dirname(binDir) : path.dirname(normalized);\n const pkgPath = path.join(packageDir, \"package.json\");\n return fileExists(pkgPath);\n}\n\nfunction readCodexCliVersion(cliPath: string): string | null {\n const packageJsonPath = resolvePackageJsonPath(cliPath);\n if (!packageJsonPath) {\n return null;\n }\n try {\n const raw = readFileSync(packageJsonPath, \"utf8\");\n const parsed = JSON.parse(raw) as { version?: unknown };\n if (typeof parsed?.version !== \"string\") {\n return null;\n }\n const normalized = parsed.version.trim();\n return normalized.length > 0 ? normalized : null;\n } catch {\n return null;\n }\n}\n\nfunction resolveBundledCodexBinaryWithFallback(requestedChannel: CodexCliChannel): {\n path: string | null;\n resolvedChannel: CodexCliChannel | null;\n fallbackUsed: boolean;\n} {\n const direct = resolveBundledCodexBinary(requestedChannel);\n if (direct) {\n return { path: direct, resolvedChannel: requestedChannel, fallbackUsed: false };\n }\n\n if (requestedChannel === \"alpha\") {\n const stable = resolveBundledCodexBinary(\"stable\");\n if (stable) {\n return { path: stable, resolvedChannel: \"stable\", fallbackUsed: true };\n }\n }\n\n return { path: null, resolvedChannel: null, fallbackUsed: false };\n}\n\nfunction buildUnavailableStatus(channel: CodexCliChannel): MutableStatus {\n const channelLabel = channel === \"alpha\" ? \"alpha \" : \"\";\n const channelSuffix =\n channel === \"alpha\"\n ? \" Switch to Stable in Settings or reinstall CodexUse to restore the CLI.\"\n : \" Reinstall CodexUse to restore the CLI.\";\n return {\n available: false,\n path: null,\n reason: `Bundled ${channelLabel}Codex CLI is missing.${channelSuffix}`,\n source: null,\n channel,\n requestedChannel: channel,\n fallbackUsed: false,\n };\n}\n\nfunction evaluateCodexCliStatus(requestedChannel: CodexCliChannel): MutableStatus {\n const resolved = resolveBundledCodexBinaryWithFallback(requestedChannel);\n if (resolved.path && resolved.resolvedChannel) {\n return {\n available: true,\n path: resolved.path,\n reason: null,\n source: \"bundled\",\n channel: resolved.resolvedChannel,\n requestedChannel,\n fallbackUsed: resolved.fallbackUsed,\n };\n }\n\n return buildUnavailableStatus(requestedChannel);\n}\n\nexport async function refreshCodexStatus(): Promise<CodexCliStatusPayload> {\n const channel = await getCodexCliChannel();\n cachedChannel = channel;\n cachedStatus = evaluateCodexCliStatus(channel);\n return { ...cachedStatus };\n}\n\nexport async function getCodexCliStatus(): Promise<CodexCliStatusPayload> {\n const channel = await getCodexCliChannel();\n if (!cachedStatus || cachedChannel !== channel) {\n return refreshCodexStatus();\n }\n return { ...cachedStatus };\n}\n\nexport async function getCodexCliInfo(): Promise<CodexCliInfoPayload> {\n const status = await getCodexCliStatus();\n const version =\n status.available && status.path\n ? readCodexCliVersion(status.path)\n : null;\n return { ...status, version };\n}\n\nexport class CodexCliMissingError extends Error {\n readonly status: CodexCliStatusPayload;\n\n constructor(status: CodexCliStatusPayload, message?: string) {\n super(message ?? status.reason ?? \"Codex CLI is not available\");\n Object.setPrototypeOf(this, new.target.prototype);\n this.name = \"CodexCliMissingError\";\n this.status = { ...status };\n }\n}\n\nexport async function requireCodexCli(): Promise<string> {\n const status = await refreshCodexStatus();\n if (!status.available || !status.path) {\n throw new CodexCliMissingError(status);\n }\n return status.path;\n}\n","import type { RateLimitSnapshot } from \"@/lib/types\";\n\ntype Notify = (payload: { title: string; body: string }) => void;\n\ninterface RateLimitNotifierOptions {\n notify: Notify;\n now?: () => number;\n approachThresholdPercent?: number;\n approachCooldownMs?: number;\n resetCooldownMs?: number;\n exhaustedThresholdPercent?: number;\n recoveryThresholdPercent?: number;\n}\n\nconst DEFAULT_APPROACH_THRESHOLD = 95;\nconst DEFAULT_APPROACH_COOLDOWN_MS = 30 * 60 * 1000; // 30 minutes\nconst DEFAULT_RESET_COOLDOWN_MS = 5 * 60 * 1000; // 5 minutes\nconst DEFAULT_EXHAUSTED_THRESHOLD = 98;\nconst DEFAULT_RECOVERY_THRESHOLD = 50;\nconst APPROACH_RENOTIFY_FLOOR_MS = 2 * 60 * 1000; // Prevents back-to-back alerts when windows jitter\nconst RESET_WINDOW_BUCKET_MS = 5 * 60 * 1000;\n\ninterface RateLimitState {\n lastSnapshot: RateLimitSnapshot | null;\n lastApproachWindow: string | null;\n lastApproachAt: number | null;\n lastResetAt: number | null;\n exhaustedWindow: string | null;\n}\n\nfunction pickResetsAt(snapshot: RateLimitSnapshot | null | undefined): string | null {\n if (!snapshot) {\n return null;\n }\n\n return snapshot.primary?.resetsAt ?? snapshot.secondary?.resetsAt ?? null;\n}\n\nexport function maxUsedPercent(snapshot: RateLimitSnapshot | null | undefined): number | null {\n if (!snapshot) {\n return null;\n }\n\n const candidates = [\n snapshot.primary?.usedPercent,\n snapshot.secondary?.usedPercent,\n ].filter((value): value is number => typeof value === \"number\" && Number.isFinite(value));\n\n if (candidates.length === 0) {\n return null;\n }\n\n return Math.max(...candidates);\n}\n\nfunction normalizeResetWindowKey(resetsAt: string | null | undefined): string | null {\n if (!resetsAt) {\n return null;\n }\n\n const parsed = Date.parse(resetsAt);\n if (Number.isNaN(parsed)) {\n return resetsAt;\n }\n\n const bucket = Math.floor(parsed / RESET_WINDOW_BUCKET_MS);\n return `reset-${bucket}`;\n}\n\nfunction formatResetWindow(snapshot: RateLimitSnapshot | null | undefined): string | null {\n const resetsInSeconds = snapshot?.primary?.resetsInSeconds ?? snapshot?.secondary?.resetsInSeconds ?? null;\n if (typeof resetsInSeconds !== \"number\" || Number.isNaN(resetsInSeconds)) {\n return null;\n }\n\n const minutes = Math.max(0, Math.round(resetsInSeconds / 60));\n if (minutes >= 120) {\n const hours = Math.round(minutes / 60);\n return `${hours}h`;\n }\n\n return `${minutes}m`;\n}\n\nexport class RateLimitNotifier {\n private readonly notify: Notify;\n private readonly now: () => number;\n private readonly approachThreshold: number;\n private readonly approachCooldownMs: number;\n private readonly resetCooldownMs: number;\n private readonly exhaustedThreshold: number;\n private readonly recoveryThreshold: number;\n\n private readonly states = new Map<string, RateLimitState>();\n\n constructor(options: RateLimitNotifierOptions) {\n this.notify = options.notify;\n this.now = options.now ?? (() => Date.now());\n this.approachThreshold = options.approachThresholdPercent ?? DEFAULT_APPROACH_THRESHOLD;\n this.approachCooldownMs = options.approachCooldownMs ?? DEFAULT_APPROACH_COOLDOWN_MS;\n this.resetCooldownMs = options.resetCooldownMs ?? DEFAULT_RESET_COOLDOWN_MS;\n this.exhaustedThreshold = options.exhaustedThresholdPercent ?? DEFAULT_EXHAUSTED_THRESHOLD;\n this.recoveryThreshold = options.recoveryThresholdPercent ?? DEFAULT_RECOVERY_THRESHOLD;\n }\n\n handleSnapshot(accountId: string, profileName: string, snapshot: RateLimitSnapshot | null | undefined): void {\n if (!accountId || !profileName) {\n return;\n }\n\n const state = this.states.get(accountId) ?? {\n lastSnapshot: null,\n lastApproachAt: null,\n lastApproachWindow: null,\n lastResetAt: null,\n exhaustedWindow: null,\n };\n\n const previousUsage = maxUsedPercent(state.lastSnapshot);\n const currentUsage = maxUsedPercent(snapshot);\n const previousWindow = pickResetsAt(state.lastSnapshot);\n const currentWindow = pickResetsAt(snapshot);\n const normalizedPreviousWindow = normalizeResetWindowKey(previousWindow);\n const normalizedCurrentWindow = normalizeResetWindowKey(currentWindow);\n const now = this.now();\n\n const wasExhausted =\n typeof previousUsage === \"number\" && previousUsage >= this.exhaustedThreshold;\n const currentWindowKey =\n normalizedCurrentWindow ??\n normalizedPreviousWindow ??\n currentWindow ??\n previousWindow ??\n \"unknown\";\n\n if (wasExhausted) {\n state.exhaustedWindow = normalizedPreviousWindow ?? state.exhaustedWindow ?? currentWindowKey;\n }\n\n // Reset notifications disabled - they're just noise. Nobody waits around for limits to reset.\n // Clear exhausted state silently so we can re-notify on next approach.\n if (this.shouldNotifyReset(state, currentUsage, currentWindowKey, previousWindow, now)) {\n state.lastResetAt = now;\n state.exhaustedWindow = null;\n state.lastApproachWindow = null;\n state.lastApproachAt = null;\n }\n\n if (\n typeof currentUsage === \"number\" &&\n currentUsage >= this.approachThreshold &&\n this.canNotifyApproach(state, currentWindowKey, now)\n ) {\n const windowText = formatResetWindow(snapshot);\n const body = windowText\n ? `${currentUsage}% used. Resets in ${windowText}.`\n : `${currentUsage}% used.`;\n this.notify({\n title: `${profileName} is nearing its limit`,\n body,\n });\n state.lastApproachWindow = currentWindowKey;\n state.lastApproachAt = now;\n }\n\n if (typeof currentUsage === \"number\" && currentUsage >= this.exhaustedThreshold) {\n state.exhaustedWindow = currentWindowKey;\n }\n\n state.lastSnapshot = snapshot ?? null;\n this.states.set(accountId, state);\n }\n\n private canNotifyApproach(state: RateLimitState, windowKey: string, now: number): boolean {\n if (!windowKey) {\n return false;\n }\n\n if (typeof state.lastApproachAt === \"number\" && now - state.lastApproachAt < APPROACH_RENOTIFY_FLOOR_MS) {\n return false;\n }\n\n if (state.lastApproachWindow !== windowKey) {\n return true;\n }\n\n if (typeof state.lastApproachAt !== \"number\") {\n return true;\n }\n\n return now - state.lastApproachAt >= this.approachCooldownMs;\n }\n\n private shouldNotifyReset(\n state: RateLimitState,\n currentUsage: number | null,\n currentWindowKey: string,\n previousWindow: string | null,\n now: number,\n ): boolean {\n if (!state.exhaustedWindow) {\n return false;\n }\n\n const windowChanged =\n Boolean(currentWindowKey) && currentWindowKey !== state.exhaustedWindow;\n const usageRecovered =\n typeof currentUsage === \"number\" && currentUsage <= this.recoveryThreshold && Boolean(previousWindow);\n\n if (!windowChanged && !usageRecovered) {\n return false;\n }\n\n if (typeof state.lastResetAt === \"number\" && now - state.lastResetAt < this.resetCooldownMs) {\n return false;\n }\n\n return true;\n }\n}\n","import { ProfileManager } from \"../../../lib/profile-manager\";\nimport { licenseService } from \"../../../lib/license-service\";\nimport { assertProfileCreationAllowed } from \"../../../lib/license-guard\";\nimport { runCodexLogin, resolveCodexBinary, resolveLoginMode, type CodexLoginMode } from \"./codex-cli\";\nimport { fetchRateLimitsViaRpc } from \"../../../lib/codex-rpc\";\nimport { maxUsedPercent } from \"../../../lib/rate-limit-notifier\";\nimport type { OrganizationInfo, Profile, RateLimitWindow } from \"../../../lib/types\";\n\ndeclare const __CODEXUSE_CLI_VERSION__: string;\n\nconst VERSION = typeof __CODEXUSE_CLI_VERSION__ === \"string\"\n ? __CODEXUSE_CLI_VERSION__\n : \"0.0.0\";\n\nfunction printHelp(): void {\n console.log(`CodexUse CLI v${VERSION}\n\nUsage:\n codexuse profile list [--no-usage] [--compact]\n codexuse profile current\n codexuse profile add <name> [--skip-login] [--device-auth] [--login=browser|device]\n codexuse profile refresh <name> [--skip-login] [--device-auth] [--login=browser|device]\n codexuse profile switch <name>\n codexuse profile delete <name>\n codexuse profile rename <old> <new>\n\n codexuse license status [--refresh]\n codexuse license activate <license-key>\n\nFlags:\n -h, --help Show help\n -v, --version Show version\n --no-usage Skip rate-limit usage fetch\n --compact Names only\n --device-auth Use device auth for Codex login\n --login=MODE Login mode: browser | device\n`);\n}\n\nfunction hasFlag(args: string[], flag: string): boolean {\n return args.includes(flag);\n}\n\nfunction stripFlags(args: string[]): string[] {\n return args.filter(arg => !arg.startsWith(\"-\"));\n}\n\nfunction parseLoginMode(flags: string[]): CodexLoginMode | null {\n if (flags.includes(\"--device-auth\")) return \"device\";\n const explicit = flags.find(flag => flag.startsWith(\"--login=\"));\n if (!explicit) return null;\n const value = explicit.split(\"=\")[1];\n if (value === \"browser\" || value === \"device\") {\n return value;\n }\n return null;\n}\n\nfunction formatProfileLabel(name: string, displayName?: string | null): string {\n if (displayName && displayName.trim() && displayName !== name) {\n return `${displayName} (${name})`;\n }\n return name;\n}\n\nfunction toTitleCase(value: string): string {\n return value.replace(/\\w\\S*/g, word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase());\n}\n\nfunction formatShortDate(value?: string | null): string | null {\n if (!value) return null;\n const date = new Date(value);\n if (Number.isNaN(date.getTime())) return null;\n if (typeof Intl !== \"undefined\" && Intl.DateTimeFormat) {\n return new Intl.DateTimeFormat(undefined, { month: \"short\", day: \"numeric\" }).format(date);\n }\n return date.toLocaleDateString();\n}\n\nfunction formatRelativeTime(value?: string | null): string | null {\n if (!value) return null;\n const parsed = Date.parse(value);\n if (Number.isNaN(parsed)) return null;\n const diffMs = parsed - Date.now();\n const absMs = Math.abs(diffMs);\n if (absMs < 60_000) {\n return null;\n }\n const minutes = Math.round(absMs / 60_000);\n if (minutes < 60) {\n return diffMs >= 0 ? `in ${minutes}m` : `${minutes}m ago`;\n }\n const hours = Math.round(minutes / 60);\n if (hours < 24) {\n return diffMs >= 0 ? `in ${hours}h` : `${hours}h ago`;\n }\n const days = Math.round(hours / 24);\n return diffMs >= 0 ? `in ${days}d` : `${days}d ago`;\n}\n\nfunction formatOrganizations(organizations?: OrganizationInfo[]): string | null {\n if (!organizations || !Array.isArray(organizations) || organizations.length === 0) {\n return null;\n }\n const parts = organizations\n .map(org => {\n if (!org) return null;\n const name = org.title || org.id;\n if (!name) return null;\n return org.role ? `${name} (${org.role})` : name;\n })\n .filter((value): value is string => Boolean(value));\n return parts.length > 0 ? parts.join(\", \") : null;\n}\n\nfunction formatWindowLabel(windowMinutes?: number): string | null {\n if (typeof windowMinutes !== \"number\" || Number.isNaN(windowMinutes)) {\n return null;\n }\n if (windowMinutes >= 2880) {\n const days = windowMinutes / 1440;\n const rounded = Math.round(days * 10) / 10;\n const display = Number.isInteger(rounded) ? rounded.toFixed(0) : rounded.toFixed(1);\n return `${display.replace(/\\.0$/, \"\")}d window`;\n }\n if (windowMinutes >= 60) {\n const hours = windowMinutes / 60;\n const rounded = Math.round(hours * 10) / 10;\n const display = Number.isInteger(rounded) ? rounded.toFixed(0) : rounded.toFixed(1);\n return `${display.replace(/\\.0$/, \"\")}h window`;\n }\n const minutes = Math.round(windowMinutes);\n return `${minutes}m window`;\n}\n\nfunction formatDurationFromSeconds(seconds?: number | null): string | null {\n if (typeof seconds !== \"number\" || Number.isNaN(seconds)) {\n return null;\n }\n if (seconds <= 0) {\n return \"now\";\n }\n const minutes = Math.floor(seconds / 60);\n if (minutes === 0) {\n return `${Math.max(1, Math.round(seconds))}s`;\n }\n if (minutes < 60) {\n return `${minutes}m`;\n }\n const hours = Math.floor(minutes / 60);\n const remainingMinutes = minutes % 60;\n if (hours < 24) {\n return remainingMinutes ? `${hours}h ${remainingMinutes}m` : `${hours}h`;\n }\n const days = Math.floor(hours / 24);\n const remainingHours = hours % 24;\n return remainingHours ? `${days}d ${remainingHours}h` : `${days}d`;\n}\n\nfunction formatResetText(window: RateLimitWindow): string | null {\n if (window.resetsAt) {\n return formatRelativeTime(window.resetsAt) ?? window.resetsAt;\n }\n const durationText = formatDurationFromSeconds(window.resetsInSeconds);\n if (!durationText) return null;\n if (durationText === \"now\") return \"now\";\n return `in ${durationText}`;\n}\n\ntype RateLimitRow = {\n label: string;\n used: string;\n reset: string | null;\n};\n\nfunction formatWindowUsage(window: RateLimitWindow): RateLimitRow | null {\n const label = formatWindowLabel(window.windowMinutes) ?? \"window\";\n const usedPercent =\n typeof window.usedPercent === \"number\" && Number.isFinite(window.usedPercent)\n ? `${Math.round(Math.max(0, Math.min(100, window.usedPercent)))}%`\n : \"n/a\";\n const resetText = formatResetText(window);\n return {\n label,\n used: usedPercent,\n reset: resetText ? `reset ${resetText}` : null,\n };\n}\n\ntype UsageInfo = {\n summary: string;\n rows: RateLimitRow[] | null;\n};\n\nfunction fallbackUsage(profile: Profile): UsageInfo {\n if (!profile.isValid) {\n return { summary: \"invalid\", rows: null };\n }\n if (profile.tokenStatus?.requiresUserAction) {\n return { summary: \"auth required\", rows: null };\n }\n return { summary: \"n/a\", rows: null };\n}\n\nasync function resolveProfileUsage(\n manager: ProfileManager,\n profile: Profile,\n codexPath: string,\n): Promise<UsageInfo> {\n if (!profile.isValid || profile.tokenStatus?.requiresUserAction) {\n return fallbackUsage(profile);\n }\n\n try {\n const snapshot = await manager.runWithProfileAuth(profile.name, (env) =>\n fetchRateLimitsViaRpc(env, { codexPath }),\n );\n if (!snapshot) {\n return { summary: \"n/a\", rows: null };\n }\n const rows: RateLimitRow[] = [];\n if (snapshot.primary) {\n const row = formatWindowUsage(snapshot.primary);\n if (row) rows.push(row);\n }\n if (snapshot.secondary) {\n const row = formatWindowUsage(snapshot.secondary);\n if (row) rows.push(row);\n }\n const maxPercent = maxUsedPercent(snapshot);\n const summary = typeof maxPercent === \"number\"\n ? `${Math.round(maxPercent)}%`\n : \"n/a\";\n return { summary, rows: rows.length > 0 ? rows : null };\n } catch {\n return { summary: \"n/a\", rows: null };\n }\n}\n\nfunction formatPlan(profile: Profile): string {\n const metadata = profile.metadata;\n if (!metadata) {\n return \"No plan details\";\n }\n const parts: string[] = [];\n if (metadata.planType) {\n parts.push(toTitleCase(metadata.planType));\n }\n const until = metadata.subscription?.activeUntil;\n if (until) {\n const formatted = formatShortDate(until);\n if (formatted) {\n parts.push(`Until ${formatted}`);\n }\n }\n return parts.join(\" · \") || \"No plan details\";\n}\n\nfunction formatAuth(profile: Profile): string {\n const metadata = profile.metadata;\n const relative = formatRelativeTime(metadata?.tokenAuthTime);\n if (relative) return relative;\n if (metadata?.tokenAuthTime) return metadata.tokenAuthTime;\n return \"Not authenticated yet\";\n}\n\nfunction printProfileDetails(profile: Profile, usage?: UsageInfo | null): void {\n const email = profile.email ?? \"Unknown\";\n const plan = formatPlan(profile);\n const orgs = formatOrganizations(profile.metadata?.organizations) ?? \"No organizations\";\n const auth = formatAuth(profile);\n const workspace = profile.workspaceName ?? profile.workspaceId ?? null;\n\n const entries: Array<{ label: string; value: string }> = [\n { label: \"email\", value: email },\n { label: \"plan\", value: plan },\n { label: \"orgs\", value: orgs },\n { label: \"auth\", value: auth },\n ];\n if (workspace) {\n entries.push({ label: \"workspace\", value: workspace });\n }\n const pad = Math.max(...entries.map(entry => entry.label.length));\n for (const entry of entries) {\n console.log(` ${entry.label.padEnd(pad)} : ${entry.value}`);\n }\n\n if (usage) {\n if (usage.rows && usage.rows.length > 0) {\n const labelPad = Math.max(...usage.rows.map(row => row.label.length));\n const usedPad = Math.max(...usage.rows.map(row => row.used.length));\n console.log(` rate limits:`);\n for (const row of usage.rows) {\n const resetText = row.reset ? ` ${row.reset}` : \"\";\n console.log(` ${row.label.padEnd(labelPad)} ${row.used.padStart(usedPad)}${resetText}`);\n }\n return;\n }\n console.log(` rate limits: ${usage.summary}`);\n }\n}\n\nasync function mapWithConcurrency<T, R>(\n items: T[],\n limit: number,\n fn: (item: T, index: number) => Promise<R>,\n): Promise<R[]> {\n const results: R[] = new Array(items.length);\n const workerCount = Math.max(1, Math.min(limit, items.length));\n let nextIndex = 0;\n\n const workers = Array.from({ length: workerCount }, async () => {\n while (true) {\n const current = nextIndex++;\n if (current >= items.length) {\n return;\n }\n results[current] = await fn(items[current], current);\n }\n });\n\n await Promise.all(workers);\n return results;\n}\n\nasync function handleProfile(args: string[]): Promise<void> {\n const flags = args.filter(arg => arg.startsWith(\"-\"));\n const params = stripFlags(args);\n const sub = params[0];\n\n if (!sub || hasFlag(flags, \"--help\") || hasFlag(flags, \"-h\")) {\n printHelp();\n return;\n }\n\n const manager = new ProfileManager();\n\n switch (sub) {\n case \"list\": {\n const profiles = await manager.listProfiles();\n const current = await manager.getCurrentProfile();\n if (!profiles.length) {\n console.log(\"No profiles found.\");\n return;\n }\n const compact = hasFlag(flags, \"--compact\");\n const withUsage = !hasFlag(flags, \"--no-usage\");\n let usageMap: Map<string, UsageInfo> | null = null;\n if (withUsage) {\n usageMap = new Map();\n const codexPath = resolveCodexBinary();\n const codexAvailable = Boolean(codexPath);\n if (!codexAvailable || !codexPath) {\n for (const profile of profiles) {\n usageMap.set(profile.name, { summary: \"codex cli missing\", rows: null });\n }\n } else {\n const limitEnv = Number.parseInt(process.env.CODEXUSE_CLI_USAGE_CONCURRENCY ?? \"3\", 10);\n const limit = Number.isFinite(limitEnv) && limitEnv > 0 ? limitEnv : 3;\n const results = await mapWithConcurrency(profiles, limit, (profile) =>\n resolveProfileUsage(manager, profile, codexPath),\n );\n for (const [index, profile] of profiles.entries()) {\n usageMap.set(profile.name, results[index]);\n }\n }\n }\n for (const [index, profile] of profiles.entries()) {\n const marker = current.name === profile.name ? \"*\" : \" \";\n const label = formatProfileLabel(profile.name, profile.displayName);\n if (index > 0) {\n console.log(\"\");\n }\n const activeLabel = current.name === profile.name ? \" (active)\" : \"\";\n console.log(`${marker} ${label}${activeLabel}`);\n if (compact) continue;\n const usage = usageMap?.get(profile.name) ?? null;\n printProfileDetails(profile, usage);\n }\n return;\n }\n case \"current\": {\n const current = await manager.getCurrentProfile();\n if (!current.name) {\n console.log(\"No active profile.\");\n return;\n }\n console.log(current.name);\n return;\n }\n case \"add\": {\n const name = params[1];\n if (!name) {\n throw new Error(\"Profile name is required.\");\n }\n await assertProfileCreationAllowed(manager);\n if (!hasFlag(flags, \"--skip-login\")) {\n const loginMode = resolveLoginMode(parseLoginMode(flags));\n await runCodexLogin(loginMode);\n }\n const profile = await manager.createProfile(name);\n const label = profile ? formatProfileLabel(profile.name, profile.displayName) : name;\n console.log(`Profile created: ${label}`);\n return;\n }\n case \"refresh\": {\n const name = params[1];\n if (!name) {\n throw new Error(\"Profile name is required.\");\n }\n if (!hasFlag(flags, \"--skip-login\")) {\n const loginMode = resolveLoginMode(parseLoginMode(flags));\n await runCodexLogin(loginMode);\n }\n const profile = await manager.refreshProfileAuth(name);\n const label = formatProfileLabel(profile.name, profile.displayName);\n console.log(`Profile refreshed: ${label}`);\n return;\n }\n case \"switch\": {\n const name = params[1];\n if (!name) {\n throw new Error(\"Profile name is required.\");\n }\n await manager.switchToProfile(name);\n console.log(`Switched to profile: ${name}`);\n return;\n }\n case \"delete\": {\n const name = params[1];\n if (!name) {\n throw new Error(\"Profile name is required.\");\n }\n await manager.deleteProfile(name);\n console.log(`Profile deleted: ${name}`);\n return;\n }\n case \"rename\": {\n const from = params[1];\n const to = params[2];\n if (!from || !to) {\n throw new Error(\"Old and new profile names are required.\");\n }\n await manager.renameProfile(from, to);\n console.log(`Profile renamed: ${from} -> ${to}`);\n return;\n }\n default:\n printHelp();\n return;\n }\n}\n\nasync function handleLicense(args: string[]): Promise<void> {\n const flags = args.filter(arg => arg.startsWith(\"-\"));\n const params = stripFlags(args);\n const sub = params[0];\n\n if (!sub || hasFlag(flags, \"--help\") || hasFlag(flags, \"-h\")) {\n printHelp();\n return;\n }\n\n switch (sub) {\n case \"status\": {\n const forceRefresh = hasFlag(flags, \"--refresh\");\n const status = await licenseService.getStatus({ forceRefresh });\n console.log(`Tier: ${status.tier}`);\n console.log(`State: ${status.state}`);\n if (status.profileLimit !== null) {\n console.log(`Profile limit: ${status.profileLimit}`);\n }\n if (status.profilesRemaining !== null) {\n console.log(`Profiles remaining: ${status.profilesRemaining}`);\n }\n if (status.purchaseEmail) {\n console.log(`Email: ${status.purchaseEmail}`);\n }\n if (status.maskedLicenseKey) {\n console.log(`License: ${status.maskedLicenseKey}`);\n }\n if (status.message) {\n console.log(`Message: ${status.message}`);\n }\n if (status.error) {\n console.log(`Error: ${status.error}`);\n }\n return;\n }\n case \"activate\": {\n const key = params[1];\n if (!key) {\n throw new Error(\"License key is required.\");\n }\n const status = await licenseService.activate(key);\n console.log(status.message ?? \"License activated.\");\n return;\n }\n default:\n printHelp();\n return;\n }\n}\n\nasync function main(): Promise<void> {\n const args = process.argv.slice(2);\n\n if (args.length === 0 || hasFlag(args, \"--help\") || hasFlag(args, \"-h\")) {\n printHelp();\n return;\n }\n\n if (hasFlag(args, \"--version\") || hasFlag(args, \"-v\")) {\n console.log(VERSION);\n return;\n }\n\n const command = args[0];\n const rest = args.slice(1);\n\n switch (command) {\n case \"profile\":\n await handleProfile(rest);\n return;\n case \"license\":\n await handleLicense(rest);\n return;\n default:\n printHelp();\n return;\n }\n}\n\nmain().catch((error) => {\n const message = error instanceof Error ? error.message : String(error);\n console.error(message);\n process.exitCode = 1;\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,SAAS,YAAY,OAAe,KAAa,KAAqB;AACpE,SAAO,KAAK,IAAI,KAAK,KAAK,IAAI,KAAK,KAAK,CAAC;AAC3C;AAMO,SAAS,0BACd,KACkB;AAClB,QAAM,UAAU,OAAO,KAAK,YAAY,YAAY,IAAI,UAAU;AAClE,QAAM,aACJ,OAAO,KAAK,qBAAqB,YAAY,OAAO,SAAS,IAAI,gBAAgB,IAC7E,IAAI,mBACJ;AACN,QAAM,YACJ,OAAO,KAAK,oBAAoB,YAAY,OAAO,SAAS,IAAI,eAAe,IAC3E,IAAI,kBACJ;AAGN,QAAM,oBAAoB,YAAY,YAAY,uBAAuB,EAAE;AAC3E,QAAM,mBAAmB,YAAY,WAAW,oBAAoB,GAAG,GAAG;AAE1E,SAAO;AAAA,IACL;AAAA,IACA,kBAAkB;AAAA,IAClB,iBAAiB;AAAA,EACnB;AACF;AAzCA,IAMa,2BACA,qCACA,oCACA;AATb;AAAA;AAAA;AAMO,IAAM,4BAA4B;AAClC,IAAM,sCAAsC;AAC5C,IAAM,qCAAqC;AAC3C,IAAM,wBAAwB;AAAA;AAAA;;;ACTrC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAgIA,eAAsB,gBAA+B;AACnD,MAAI,YAAY,iBAAiB;AAC/B,UAAM,KAAK,MAAM,YAAY;AAE7B,QAAI,cAAc,oBAAoB;AACpC,YAAM,GAAG,iBAAiB;AAAA,IAC5B;AAAA,EACF;AACF;AA+CA,SAAS,cAAc,OAAkD;AACvE,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;AAC5E;AAEA,SAAS,yBAAyB,QAA0D;AAC1F,QAAM,aAAsC,CAAC;AAC7C,aAAW,CAAC,KAAK,QAAQ,KAAK,OAAO,QAAQ,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UAAQ,MAAM,OAAO;AACrB,UAAQ,WAAW;AACrB,CAAC;","names":["path","require","initSqlJs","fsPromises","import_path","import_node_fs","import_node_path","path","fs","__debugResolveDbPath","getDatabase","fs","getDatabase","path","import_node_crypto","import_node_fs","import_node_path","resolveCodexDir","path","crypto","fs","crypto","fallbackStored","fallback","import_node_fs","import_node_path","path","import_node_child_process","import_node_fs","import_node_path","import_node_fs","import_node_path","fileExists","path","fs","path","os","readline"]}
1
+ {"version":3,"sources":["../../../lib/profile-manager.ts","../../../lib/auto-roll-settings.ts","../../../lib/codex-cli-channel.ts","../../../lib/app-state.ts","../../../lib/codex-settings.ts","../../../lib/logger.ts","../../../lib/license-service.ts","../../../lib/license-secret.ts","../../../lib/cloud-sync-service.ts","../../../lib/cloud-sync-types.ts","../../../lib/type-guards.ts","../../../lib/cloud-sync-client.ts","../../../lib/codex-config.ts","../../../lib/codex-config-metadata.ts","../../../lib/codex-cli.ts","../../../lib/errors.ts","../../../lib/license-guard.ts","../src/codex-cli.ts","../../../lib/codex-rpc.ts","../../../lib/rate-limit-notifier.ts","../src/index.ts"],"sourcesContent":["import { promises as fs } from 'fs';\nimport path from 'path';\nimport { join } from 'path';\nimport { parse, stringify } from '@iarna/toml';\nimport {\n Profile,\n ProfileData,\n ActiveAuth,\n ProfileMetadata,\n OrganizationInfo,\n SubscriptionInfo,\n TokenAlert,\n TokenIssue,\n TokenStatus,\n} from './types';\nimport type { CloudSyncProfileRecord } from './cloud-sync-types';\nimport { getLastProfileName, persistLastProfileName } from './codex-settings';\nimport { getAppState, getUserDataDir, updateAppState, type ProfileRecordState } from './app-state';\nimport { logError, logWarn } from './logger';\n\nconst TOKEN_EXPIRING_SOON_WINDOW_MS = 15 * 60 * 1000;\nexport const REFRESH_TOKEN_REDEEMED_SNIPPET = 'refresh token was already used';\nexport const REFRESH_TOKEN_REDEEMED_REASON =\n 'Your access token could not be refreshed because your refresh token was already used. Please log out and sign in again.';\nconst AUTH_BACKUP_MISSING_MARKER = '__codexuse_missing_auth__';\nconst DEFAULT_WORKSPACE_ID = '__default__';\n\nexport function detectRefreshTokenRedeemedMessage(output: string | Buffer | null | undefined): string | null {\n if (!output) {\n return null;\n }\n\n const text = output.toString();\n if (!text) {\n return null;\n }\n\n const normalized = text\n .toLowerCase()\n .replace(/\\s+/g, ' ');\n if (!normalized.includes(REFRESH_TOKEN_REDEEMED_SNIPPET)) {\n return null;\n }\n\n const trimmed = text.trim();\n return trimmed.length > 0 ? trimmed : REFRESH_TOKEN_REDEEMED_REASON;\n}\n\ntype ProfileRecord = {\n name: string;\n displayName: string | null;\n data: ProfileData;\n metadata?: ProfileMetadata;\n accountId: string | null;\n workspaceId: string | null;\n workspaceName: string | null;\n email: string | null;\n authMethod: string | null;\n createdAt: string | null;\n updatedAt: string | null;\n};\n\nexport class ProfileManager {\n private codexDir: string;\n private profileHomesRoot: string;\n private activeAuth: string;\n private activeAuthBackup: string;\n private lastActiveAuthErrorSignature: string | null;\n private authSwapLock: Promise<void>;\n constructor() {\n const homeDir = process.env.HOME || process.env.USERPROFILE || '';\n this.codexDir = join(homeDir, '.codex');\n this.profileHomesRoot = join(getUserDataDir(), 'profile-homes');\n this.activeAuth = join(this.codexDir, 'auth.json');\n this.activeAuthBackup = `${this.activeAuth}.swap`;\n this.lastActiveAuthErrorSignature = null;\n this.authSwapLock = Promise.resolve();\n }\n\n private computeExpiryIso(data: ProfileData | null | undefined): string | undefined {\n if (!data) {\n return undefined;\n }\n if (typeof data.expired === 'string' && data.expired.trim()) {\n const parsed = Date.parse(data.expired);\n if (!Number.isNaN(parsed)) {\n return new Date(parsed).toISOString();\n }\n }\n const expiresIn = typeof data.expires_in === 'number' && Number.isFinite(data.expires_in)\n ? data.expires_in\n : undefined;\n const issuedMs =\n typeof data.timestamp === 'number' && Number.isFinite(data.timestamp)\n ? data.timestamp\n : undefined;\n const issuedAt = issuedMs ? issuedMs : undefined;\n const baseMs = issuedAt ?? Date.now();\n if (expiresIn && expiresIn > 0) {\n return new Date(baseMs + expiresIn * 1000).toISOString();\n }\n return undefined;\n }\n\n private normalizeProfileName(name: string): string {\n if (typeof name !== 'string') {\n throw new Error('Profile name is required');\n }\n\n const trimmed = name.trim();\n if (!trimmed) {\n throw new Error('Profile name must not be empty.');\n }\n\n if (trimmed === '.' || trimmed === '..') {\n throw new Error(`Profile name '${name}' is not allowed.`);\n }\n\n if (/[\\\\/]/.test(trimmed)) {\n throw new Error(\"Profile name cannot contain path separators.\");\n }\n\n if (trimmed.includes('\\0')) {\n throw new Error('Profile name contains invalid characters.');\n }\n\n return trimmed;\n }\n\n private isNotFoundError(error: unknown): boolean {\n return Boolean(\n error &&\n typeof error === 'object' &&\n 'code' in error &&\n (error as NodeJS.ErrnoException).code === 'ENOENT',\n );\n }\n\n private async readPreferredProfileName(): Promise<string | null> {\n try {\n return await getLastProfileName();\n } catch (error) {\n logWarn('Failed to read preferred profile name:', error);\n return null;\n }\n }\n\n private async persistPreferredProfileName(name: string | null): Promise<void> {\n try {\n await persistLastProfileName(name);\n } catch (error) {\n logWarn('Failed to persist preferred profile name:', error);\n }\n }\n\n private toStateRecord(record: ProfileRecord): ProfileRecordState {\n return {\n name: record.name,\n displayName: record.displayName ?? null,\n data: record.data,\n metadata: record.metadata,\n accountId: record.accountId ?? null,\n workspaceId: record.workspaceId ?? null,\n workspaceName: record.workspaceName ?? null,\n email: record.email ?? null,\n authMethod: record.authMethod ?? null,\n createdAt: record.createdAt ?? null,\n updatedAt: record.updatedAt ?? null,\n };\n }\n\n private fromStateRecord(profileName: string, raw: ProfileRecordState): ProfileRecord | null {\n if (!raw || typeof raw !== 'object') {\n return null;\n }\n\n const dataRaw = raw.data;\n if (!dataRaw || typeof dataRaw !== 'object') {\n return null;\n }\n\n return {\n name: profileName,\n displayName: typeof raw.displayName === 'string' ? raw.displayName : null,\n data: dataRaw as ProfileData,\n metadata: raw.metadata,\n accountId: typeof raw.accountId === 'string' ? raw.accountId : null,\n workspaceId: typeof raw.workspaceId === 'string' ? raw.workspaceId : null,\n workspaceName: typeof raw.workspaceName === 'string' ? raw.workspaceName : null,\n email: typeof raw.email === 'string' ? raw.email : null,\n authMethod: typeof raw.authMethod === 'string' ? raw.authMethod : null,\n createdAt: typeof raw.createdAt === 'string' ? raw.createdAt : null,\n updatedAt: typeof raw.updatedAt === 'string' ? raw.updatedAt : null,\n };\n }\n\n private async readProfilesStateMap(): Promise<Record<string, ProfileRecordState>> {\n const state = await getAppState();\n return { ...(state.profilesByName ?? {}) };\n }\n\n private async writeProfilesStateMap(nextMap: Record<string, ProfileRecordState>): Promise<void> {\n await updateAppState((state) => ({\n ...state,\n profilesByName: nextMap,\n }), { mode: 'replace' });\n }\n\n private async readProfileRecord(profileName: string): Promise<ProfileRecord | null> {\n const map = await this.readProfilesStateMap();\n const raw = map[profileName];\n if (!raw) {\n return null;\n }\n return this.fromStateRecord(profileName, raw);\n }\n\n private async listProfileRecords(): Promise<ProfileRecord[]> {\n const map = await this.readProfilesStateMap();\n const records: ProfileRecord[] = [];\n for (const [profileName, raw] of Object.entries(map)) {\n const record = this.fromStateRecord(profileName, raw);\n if (record) {\n records.push(record);\n }\n }\n return records;\n }\n\n private async writeProfileRecord(record: ProfileRecord): Promise<void> {\n const map = await this.readProfilesStateMap();\n map[record.name] = this.toStateRecord(record);\n await this.writeProfilesStateMap(map);\n }\n\n private async deleteProfileRecord(name: string): Promise<void> {\n const map = await this.readProfilesStateMap();\n if (!Object.prototype.hasOwnProperty.call(map, name)) {\n return;\n }\n delete map[name];\n await this.writeProfilesStateMap(map);\n }\n\n private resolveAuthMethod(data: ProfileData | null | undefined): string {\n const raw = typeof data?.auth_method === 'string' ? data.auth_method.trim().toLowerCase() : '';\n return raw || 'codex-cli';\n }\n\n private async readActiveAuthFile(): Promise<ActiveAuth | null> {\n try {\n const raw = await fs.readFile(this.activeAuth, 'utf8');\n const trimmed = raw.trim();\n if (!trimmed) {\n return null;\n }\n return JSON.parse(trimmed) as ActiveAuth;\n } catch (error) {\n if (this.isNotFoundError(error)) {\n return null;\n }\n const message = error instanceof Error ? error.message : 'unknown error';\n const signature = typeof message === 'string' ? `${message}:${this.activeAuth}` : this.activeAuth;\n if (this.lastActiveAuthErrorSignature !== signature) {\n logWarn('Failed to read active auth file:', error);\n this.lastActiveAuthErrorSignature = signature;\n }\n return null;\n }\n }\n\n private async getActiveAuthAccountId(): Promise<string | undefined> {\n const activeAuth = await this.readActiveAuthFile();\n if (!activeAuth) {\n return undefined;\n }\n return this.getAccountIdFromData(activeAuth);\n }\n\n /**\n * Decode a JWT payload into an object without validating the signature.\n */\n private decodeJwtPayload(token?: string | null): Record<string, unknown> | null {\n if (!token || typeof token !== 'string') {\n return null;\n }\n\n const segments = token.split('.');\n if (segments.length < 2) {\n return null;\n }\n\n try {\n const payload = Buffer.from(segments[1], 'base64url').toString('utf8');\n return JSON.parse(payload);\n } catch {\n return null;\n }\n }\n\n private toIsoStringFromSeconds(seconds?: number): string | undefined {\n if (typeof seconds !== 'number' || Number.isNaN(seconds)) {\n return undefined;\n }\n\n try {\n return new Date(seconds * 1000).toISOString();\n } catch {\n return undefined;\n }\n }\n\n private normalizeEmailCandidate(value: unknown): string | undefined {\n if (typeof value !== 'string') {\n return undefined;\n }\n\n const trimmed = value.trim();\n if (!trimmed || trimmed.includes(' ')) {\n return undefined;\n }\n\n const atIndex = trimmed.indexOf('@');\n if (atIndex <= 0 || atIndex === trimmed.length - 1) {\n return undefined;\n }\n\n const domain = trimmed.slice(atIndex + 1);\n if (!domain || !domain.includes('.')) {\n return undefined;\n }\n\n return trimmed;\n }\n\n private pickFirstEmail(candidates: Array<string | undefined>): string | undefined {\n for (const candidate of candidates) {\n const normalized = this.normalizeEmailCandidate(candidate);\n if (normalized) {\n return normalized;\n }\n }\n return undefined;\n }\n\n private findEmailInObject(value: unknown, seen = new Set<unknown>()): string | undefined {\n if (!value || typeof value !== 'object') {\n return undefined;\n }\n\n if (seen.has(value)) {\n return undefined;\n }\n seen.add(value);\n\n if (Array.isArray(value)) {\n for (const entry of value) {\n const nested = this.findEmailInObject(entry, seen);\n if (nested) {\n return nested;\n }\n }\n return undefined;\n }\n\n for (const [key, entry] of Object.entries(value as Record<string, unknown>)) {\n if (typeof entry === 'string') {\n const normalized = this.normalizeEmailCandidate(entry);\n const lowerKey = key.toLowerCase();\n const keyHintsAtEmail =\n lowerKey.includes('email') ||\n lowerKey.includes('contact') ||\n lowerKey.includes('username') ||\n lowerKey.includes('login');\n\n if (normalized && (keyHintsAtEmail || entry.includes('@'))) {\n return normalized;\n }\n }\n\n if (entry && typeof entry === 'object') {\n const nested = this.findEmailInObject(entry, seen);\n if (nested) {\n return nested;\n }\n }\n }\n\n return undefined;\n }\n\n private resolveProfileEmail(data: ProfileData, metadata?: ProfileMetadata): string | undefined {\n const direct = this.normalizeEmailCandidate(data.email);\n if (direct) {\n return direct;\n }\n\n if (metadata?.email) {\n return metadata.email;\n }\n\n return this.findEmailInObject(data);\n }\n\n private evaluateTokenStatus(data: ProfileData, metadata?: ProfileMetadata): TokenStatus {\n const expiresAt = metadata?.tokenExpiresAt ?? this.computeExpiryIso(data);\n const parsedExpiry = expiresAt ? Date.parse(expiresAt) : Number.NaN;\n const hasExpiry = !Number.isNaN(parsedExpiry);\n const tokenAlert = data.tokenAlert;\n\n if (!data || typeof data !== 'object' || Object.keys(data).length === 0) {\n return {\n state: 'missing',\n reason: 'No authentication data saved for this profile.',\n expiresAt,\n issue: 'auth-missing',\n requiresUserAction: true,\n };\n }\n\n if (tokenAlert?.issue) {\n const reason = typeof tokenAlert.reason === 'string' && tokenAlert.reason.trim().length > 0\n ? tokenAlert.reason.trim()\n : tokenAlert.issue === 'refresh-redeemed'\n ? REFRESH_TOKEN_REDEEMED_REASON\n : 'Authentication needs attention. Re-login this profile.';\n const issue = (tokenAlert.issue as TokenIssue) ?? 'auth-missing';\n return {\n state: 'invalid',\n reason,\n expiresAt,\n issue,\n requiresUserAction: true,\n };\n }\n\n if (!data.access_token) {\n return {\n state: 'missing',\n reason: 'Access token is missing. Re-authenticate with Codex CLI.',\n expiresAt,\n issue: 'access-missing',\n requiresUserAction: true,\n };\n }\n\n if (!this.decodeJwtPayload(data.access_token)) {\n return {\n state: 'invalid',\n reason: 'Access token is corrupted or not a valid JWT.',\n expiresAt,\n issue: 'access-invalid',\n requiresUserAction: true,\n };\n }\n\n const refreshToken = typeof data.refresh_token === 'string' ? data.refresh_token.trim() : '';\n if (!refreshToken) {\n return {\n state: 'missing',\n reason: 'Refresh token is missing. Run Codex login again.',\n expiresAt,\n issue: 'refresh-missing',\n requiresUserAction: true,\n };\n }\n\n if (hasExpiry) {\n const diff = parsedExpiry - Date.now();\n if (diff <= 0) {\n return {\n state: 'expiring',\n reason: 'Access token expired.',\n expiresAt,\n accessTokenExpired: true,\n issue: 'access-expired',\n requiresUserAction: false,\n };\n }\n\n if (diff <= TOKEN_EXPIRING_SOON_WINDOW_MS) {\n return {\n state: 'expiring',\n reason: 'Access token expires soon.',\n expiresAt,\n issue: 'access-expiring',\n requiresUserAction: false,\n };\n }\n }\n\n return {\n state: 'ok',\n expiresAt,\n requiresUserAction: false,\n };\n }\n\n private extractProfileMetadata(data: ProfileData): ProfileMetadata | undefined {\n const idPayload = this.decodeJwtPayload(data.id_token) as Record<string, unknown> | null;\n const accessPayload = this.decodeJwtPayload(data.access_token) as Record<string, unknown> | null;\n\n if (!idPayload && !accessPayload) {\n return undefined;\n }\n\n const authInfo = (idPayload?.['https://api.openai.com/auth'] ??\n accessPayload?.['https://api.openai.com/auth']) as Record<string, unknown> | undefined;\n\n const profileInfo = accessPayload?.['https://api.openai.com/profile'] as Record<string, unknown> | undefined;\n\n const getNumber = (obj: Record<string, unknown> | null | undefined, key: string): number | undefined =>\n typeof obj?.[key] === 'number' ? (obj[key] as number) : undefined;\n\n const getString = (obj: Record<string, unknown> | undefined | null, key: string): string | undefined =>\n typeof obj?.[key] === 'string' ? (obj[key] as string) : undefined;\n\n const exp = getNumber(idPayload, 'exp') ?? getNumber(accessPayload, 'exp');\n const iat = getNumber(idPayload, 'iat') ?? getNumber(accessPayload, 'iat');\n const authTime = getNumber(idPayload, 'auth_time') ?? getNumber(accessPayload, 'auth_time');\n\n const organizationsRaw = Array.isArray(authInfo?.['organizations'])\n ? (authInfo?.['organizations'] as unknown[])\n : undefined;\n\n const organizations = organizationsRaw\n ? organizationsRaw\n .map((org): OrganizationInfo => {\n const record = org as Record<string, unknown> | undefined;\n return {\n id: getString(record, 'id'),\n title: getString(record, 'title'),\n role: getString(record, 'role'),\n isDefault: typeof record?.['is_default'] === 'boolean' ? (record?.['is_default'] as boolean) : undefined,\n };\n })\n .filter(\n org =>\n org.id ||\n org.title ||\n org.role ||\n typeof org.isDefault === 'boolean'\n )\n : undefined;\n\n const groups = Array.isArray(authInfo?.['groups'])\n ? (authInfo?.['groups'] as unknown[]).filter((group): group is string => typeof group === 'string')\n : undefined;\n\n const emailVerified =\n typeof profileInfo?.['email_verified'] === 'boolean'\n ? (profileInfo?.['email_verified'] as boolean)\n : typeof idPayload?.['email_verified'] === 'boolean'\n ? (idPayload?.['email_verified'] as boolean)\n : typeof accessPayload?.['email_verified'] === 'boolean'\n ? (accessPayload?.['email_verified'] as boolean)\n : undefined;\n\n const email =\n this.pickFirstEmail([\n getString(profileInfo, 'email'),\n getString(profileInfo, 'email_address'),\n getString(profileInfo, 'primary_email'),\n getString(profileInfo, 'default_email'),\n getString(profileInfo, 'contact_email'),\n ]) ??\n this.findEmailInObject(profileInfo) ??\n this.pickFirstEmail([\n getString(authInfo, 'user_email'),\n getString(authInfo, 'email'),\n getString(authInfo, 'userEmail'),\n getString(authInfo, 'chatgpt_user_email'),\n ]) ??\n this.findEmailInObject(authInfo) ??\n this.pickFirstEmail([\n getString(idPayload, 'email'),\n getString(idPayload, 'preferred_username'),\n getString(idPayload, 'username'),\n ]) ??\n this.findEmailInObject(idPayload) ??\n this.pickFirstEmail([\n getString(accessPayload, 'email'),\n ]) ??\n this.findEmailInObject(accessPayload);\n\n const subscription: SubscriptionInfo | undefined = authInfo\n ? {\n activeStart: getString(authInfo, 'chatgpt_subscription_active_start'),\n activeUntil: getString(authInfo, 'chatgpt_subscription_active_until'),\n lastChecked: getString(authInfo, 'chatgpt_subscription_last_checked'),\n }\n : undefined;\n\n const planType = getString(authInfo, 'chatgpt_plan_type');\n const chatgptUserId = getString(authInfo, 'chatgpt_user_id');\n const chatgptAccountUserId = getString(authInfo, 'chatgpt_account_user_id');\n const userId =\n getString(authInfo, 'user_id') ??\n chatgptAccountUserId ??\n getString(idPayload ?? undefined, 'sub') ??\n getString(accessPayload ?? undefined, 'sub');\n\n const metadata: ProfileMetadata = {\n email,\n planType,\n subscription,\n organizations,\n groups,\n userId,\n chatgptUserId,\n emailVerified,\n tokenExpiresAt: this.toIsoStringFromSeconds(exp),\n tokenIssuedAt: this.toIsoStringFromSeconds(iat),\n tokenAuthTime: this.toIsoStringFromSeconds(authTime),\n };\n\n const hasMeaningfulData =\n Boolean(metadata.planType) ||\n Boolean(\n metadata.subscription &&\n (metadata.subscription.activeStart ||\n metadata.subscription.activeUntil ||\n metadata.subscription.lastChecked)\n ) ||\n Boolean(metadata.organizations && metadata.organizations.length > 0) ||\n Boolean(metadata.groups && metadata.groups.length > 0) ||\n Boolean(metadata.userId) ||\n Boolean(metadata.chatgptUserId) ||\n Boolean(metadata.email) ||\n typeof metadata.emailVerified === 'boolean' ||\n Boolean(metadata.tokenExpiresAt) ||\n Boolean(metadata.tokenIssuedAt) ||\n Boolean(metadata.tokenAuthTime);\n\n return hasMeaningfulData ? metadata : undefined;\n }\n\n /**\n * Initialize the profile manager and create necessary directories\n */\n async initialize(): Promise<void> {\n this.profileHomesRoot = join(getUserDataDir(), 'profile-homes');\n\n try {\n await fs.mkdir(this.codexDir, { recursive: true });\n } catch (error) {\n logError('Failed to ensure Codex directory exists:', error);\n throw new Error('Failed to initialize profile manager');\n }\n try {\n await fs.mkdir(this.profileHomesRoot, { recursive: true });\n } catch (error) {\n logError('Failed to ensure profile homes directory exists:', error);\n throw new Error('Failed to initialize profile manager');\n }\n\n await this.recoverActiveAuthBackup();\n }\n\n private enqueueAuthSwap<T>(task: () => Promise<T>): Promise<T> {\n const run = this.authSwapLock.then(task, task);\n this.authSwapLock = run.then(\n () => undefined,\n () => undefined,\n );\n return run;\n }\n\n private async recoverActiveAuthBackup(): Promise<void> {\n let backup: string;\n try {\n backup = await fs.readFile(this.activeAuthBackup, 'utf8');\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn('Failed to inspect active auth backup:', error);\n }\n return;\n }\n\n try {\n if (backup === AUTH_BACKUP_MISSING_MARKER) {\n await fs.rm(this.activeAuth, { force: true });\n } else {\n try {\n await this.writeAtomic(this.activeAuth, backup);\n } catch (error) {\n logWarn('Failed to restore active auth from backup, falling back to direct write:', error);\n try {\n await fs.writeFile(this.activeAuth, backup, 'utf8');\n } catch (fallbackError) {\n logWarn('Direct write failed while restoring active auth backup:', fallbackError);\n }\n }\n }\n } catch (error) {\n logWarn('Failed to restore active auth from backup:', error);\n }\n\n try {\n await fs.rm(this.activeAuthBackup, { force: true });\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn('Failed to remove active auth backup:', error);\n }\n }\n }\n\n private async writeAtomic(filePath: string, contents: string): Promise<void> {\n const tempPath = `${filePath}.tmp`;\n const dir = path.dirname(filePath);\n await fs.mkdir(dir, { recursive: true });\n\n await fs.writeFile(tempPath, contents, 'utf8');\n try {\n await fs.rename(tempPath, filePath);\n } catch (error) {\n // Fall back to writing directly if rename fails for any reason.\n try {\n await fs.writeFile(filePath, contents, 'utf8');\n } catch (writeError) {\n // Surface the original error context to aid debugging.\n const original = error instanceof Error ? error.message : String(error);\n const fallback = writeError instanceof Error ? writeError.message : String(writeError);\n throw new Error(`Atomic write failed (rename: ${original}; direct write: ${fallback})`);\n }\n }\n // Best-effort cleanup of temp file if it still exists.\n try {\n await fs.rm(tempPath, { force: true });\n } catch {\n // ignore cleanup failures\n }\n }\n\n /**\n * Extract account_id from auth data (supports both formats)\n */\n private getAccountIdFromData(data: ProfileData | ActiveAuth): string | undefined {\n if ('account_id' in data && typeof data.account_id === 'string' && data.account_id.trim()) {\n return data.account_id.trim();\n }\n\n if ('tokens' in data && data.tokens && typeof data.tokens === 'object') {\n const accountId = (data.tokens as { account_id?: unknown }).account_id;\n if (typeof accountId === 'string' && accountId.trim()) {\n return accountId.trim();\n }\n }\n\n const projectId =\n 'project_id' in data && typeof (data as ProfileData).project_id === 'string'\n ? (data as ProfileData).project_id?.trim()\n : undefined;\n if (projectId) {\n return projectId;\n }\n\n const email =\n 'email' in data && typeof (data as ProfileData).email === 'string'\n ? (data as ProfileData).email?.trim()\n : undefined;\n return email || undefined;\n }\n\n private resolveWorkspaceIdentity(data: ProfileData | ActiveAuth, metadata?: ProfileMetadata): { id: string; name?: string } {\n const directId =\n 'workspace_id' in data && typeof data.workspace_id === 'string'\n ? data.workspace_id.trim()\n : undefined;\n const directName =\n 'workspace_name' in data && typeof data.workspace_name === 'string'\n ? data.workspace_name.trim()\n : undefined;\n\n if (directId) {\n return { id: directId, name: directName };\n }\n\n const organizations = metadata?.organizations;\n if (organizations && organizations.length > 0) {\n const preferred = organizations.find(org => org.isDefault) ?? organizations[0];\n if (preferred?.id) {\n return { id: preferred.id, name: preferred.title ?? directName };\n }\n }\n\n return { id: DEFAULT_WORKSPACE_ID, name: directName };\n }\n\n private async getProfileRowByName(name: string): Promise<ProfileRecord | undefined> {\n const normalized = this.normalizeProfileName(name);\n const record = await this.readProfileRecord(normalized);\n return record ?? undefined;\n }\n\n private async getProfileRowByAccountId(\n accountId: string,\n options?: { authMethod?: string | null; preferAuthMethod?: string | null },\n ): Promise<ProfileRecord | undefined> {\n const records = await this.listProfileRecords();\n const matches = records.filter(record => {\n const storedAccountId = record.accountId ?? this.getAccountIdFromData(record.data);\n return storedAccountId === accountId;\n });\n\n if (matches.length === 0) {\n return undefined;\n }\n\n const mustMatch = typeof options?.authMethod === 'string' ? options.authMethod.trim().toLowerCase() : null;\n if (mustMatch) {\n return matches.find(record => this.resolveAuthMethod(record.data) === mustMatch);\n }\n\n const prefer = typeof options?.preferAuthMethod === 'string' ? options.preferAuthMethod.trim().toLowerCase() : null;\n if (prefer) {\n return matches.find(record => this.resolveAuthMethod(record.data) === prefer) ?? matches[0];\n }\n\n return matches[0];\n }\n\n private async getProfileRowByAccountAndWorkspace(\n accountId: string,\n workspaceId: string,\n options?: { authMethod?: string | null; preferAuthMethod?: string | null },\n ): Promise<ProfileRecord | undefined> {\n const workspaceKey = workspaceId && workspaceId.trim().length > 0 ? workspaceId.trim() : DEFAULT_WORKSPACE_ID;\n const records = await this.listProfileRecords();\n const matches = records.filter(record => {\n const storedAccountId = record.accountId ?? this.getAccountIdFromData(record.data);\n if (storedAccountId !== accountId) {\n return false;\n }\n const storedWorkspace = record.workspaceId ?? record.data.workspace_id ?? DEFAULT_WORKSPACE_ID;\n return (storedWorkspace && storedWorkspace.trim() ? storedWorkspace.trim() : DEFAULT_WORKSPACE_ID) === workspaceKey;\n });\n\n if (matches.length === 0) {\n return undefined;\n }\n\n const mustMatch = typeof options?.authMethod === 'string' ? options.authMethod.trim().toLowerCase() : null;\n if (mustMatch) {\n return matches.find(record => this.resolveAuthMethod(record.data) === mustMatch);\n }\n\n const prefer = typeof options?.preferAuthMethod === 'string' ? options.preferAuthMethod.trim().toLowerCase() : null;\n if (prefer) {\n return matches.find(record => this.resolveAuthMethod(record.data) === prefer) ?? matches[0];\n }\n\n return matches[0];\n }\n\n private async persistProfileRecord(\n name: string,\n data: ProfileData,\n metadata?: ProfileMetadata,\n options?: { displayName?: string | null },\n ): Promise<void> {\n const resolvedName = this.normalizeProfileName(name);\n const existingRecord = await this.readProfileRecord(resolvedName);\n if (typeof data.auth_method === 'string') {\n data.auth_method = data.auth_method.trim().toLowerCase();\n }\n if (!data.auth_method) {\n data.auth_method = 'codex-cli';\n }\n const resolvedMetadata = metadata ?? this.extractProfileMetadata(data) ?? existingRecord?.metadata;\n const workspace = this.resolveWorkspaceIdentity(data, resolvedMetadata);\n const workspaceId = workspace.id || DEFAULT_WORKSPACE_ID;\n const workspaceName = workspace.name ?? null;\n if (!data.workspace_id) {\n data.workspace_id = workspaceId;\n }\n if (!data.workspace_name && workspaceName) {\n data.workspace_name = workspaceName;\n }\n\n const now = new Date().toISOString();\n const accountId = this.getAccountIdFromData(data) ?? null;\n const resolvedEmail = this.resolveProfileEmail(data, resolvedMetadata) ?? data.email ?? null;\n if (resolvedEmail) {\n data.email = resolvedEmail;\n }\n const createdAt = data.created_at ?? existingRecord?.createdAt ?? now;\n data.created_at = createdAt;\n const updatedAt = data.updated_at ?? now;\n data.updated_at = updatedAt;\n\n const displayName =\n typeof options?.displayName === 'string' && options.displayName.trim().length > 0\n ? options.displayName.trim()\n : existingRecord?.displayName ?? resolvedName;\n\n const record: ProfileRecord = {\n name: resolvedName,\n displayName,\n data,\n metadata: resolvedMetadata,\n accountId,\n workspaceId: data.workspace_id ?? workspaceId,\n workspaceName: data.workspace_name ?? workspaceName,\n email: resolvedEmail ?? null,\n authMethod: typeof data.auth_method === 'string' ? data.auth_method : null,\n createdAt,\n updatedAt,\n };\n\n await fs.mkdir(this.getProfileHomePath(resolvedName), { recursive: true });\n await this.writeProfileRecord(record);\n }\n\n private buildProfileFromRow(row: ProfileRecord): Profile {\n const profile = this.buildProfileFromData(row.name, row.data, {\n displayName: row.displayName ?? undefined,\n email: row.email ?? undefined,\n createdAt: row.createdAt ?? undefined,\n authMethod: row.authMethod ?? undefined,\n accountId: row.accountId ?? undefined,\n workspaceId: row.workspaceId ?? undefined,\n workspaceName: row.workspaceName ?? undefined,\n metadata: row.metadata,\n });\n\n return profile;\n }\n\n private buildProfileFromData(\n name: string,\n data: ProfileData,\n fallback?: {\n displayName?: string;\n email?: string;\n createdAt?: string;\n authMethod?: string;\n accountId?: string;\n projectId?: string;\n workspaceId?: string;\n workspaceName?: string;\n metadata?: ProfileMetadata;\n },\n ): Profile {\n const metadata = fallback?.metadata ?? this.extractProfileMetadata(data);\n const workspace = this.resolveWorkspaceIdentity(data, metadata);\n const workspaceId = workspace.id || fallback?.workspaceId || DEFAULT_WORKSPACE_ID;\n const workspaceName = workspace.name ?? fallback?.workspaceName ?? undefined;\n const email = this.resolveProfileEmail(data, metadata) ?? fallback?.email ?? undefined;\n const tokenStatus = this.evaluateTokenStatus(data, metadata);\n const accountId = this.getAccountIdFromData(data) ?? fallback?.accountId;\n const projectId =\n typeof data.project_id === 'string' && data.project_id.trim().length > 0\n ? data.project_id.trim()\n : fallback?.projectId;\n\n return {\n name,\n displayName: fallback?.displayName ?? name,\n isValid: Boolean(data && Object.keys(data).length > 0),\n accountId,\n projectId,\n workspaceId,\n workspaceName,\n email,\n createdAt: data.created_at ?? fallback?.createdAt ?? undefined,\n authMethod: typeof data.auth_method === 'string'\n ? data.auth_method.trim().toLowerCase()\n : fallback?.authMethod ?? undefined,\n metadata,\n tokenStatus,\n };\n }\n\n private parseTimestamp(value?: string | null): number | null {\n if (typeof value !== 'string' || value.trim() === '') {\n return null;\n }\n\n const parsed = Date.parse(value);\n return Number.isNaN(parsed) ? null : parsed;\n }\n\n private compareProfilesForPreference(a: Profile, b: Profile): number {\n if (Boolean(a.isValid) !== Boolean(b.isValid)) {\n return a.isValid ? -1 : 1;\n }\n\n const aTimestamp = this.parseTimestamp(a.createdAt ?? null);\n const bTimestamp = this.parseTimestamp(b.createdAt ?? null);\n\n if (aTimestamp !== null || bTimestamp !== null) {\n if (aTimestamp !== null && bTimestamp !== null) {\n if (aTimestamp > bTimestamp) {\n return -1;\n }\n if (aTimestamp < bTimestamp) {\n return 1;\n }\n } else if (aTimestamp !== null) {\n return -1;\n } else {\n return 1;\n }\n }\n\n return a.name.localeCompare(b.name);\n }\n\n private selectPreferredProfile(profiles: Profile[]): Profile {\n if (profiles.length <= 1) {\n return profiles[0];\n }\n\n return profiles\n .slice()\n .sort((a, b) => this.compareProfilesForPreference(a, b))[0];\n }\n\n /**\n * Convert profile data to Codex's expected format\n */\n private convertToCodexFormat(data: ProfileData): ActiveAuth {\n // Check if data is already in Codex format\n if ('tokens' in data) {\n return data as ActiveAuth;\n }\n\n // Convert flat structure to nested Codex format\n return {\n OPENAI_API_KEY: null,\n tokens: {\n id_token: data.id_token,\n access_token: data.access_token,\n refresh_token: data.refresh_token,\n account_id: data.account_id,\n },\n last_refresh: new Date().toISOString(),\n };\n }\n\n /**\n * Normalize Codex auth data to flat profile format\n */\n private normalizeProfileData(data: ActiveAuth): ProfileData {\n // If data is already in flat format, return as-is\n if ('id_token' in data) {\n return data as ProfileData;\n }\n\n // Convert nested Codex format to flat\n const tokens = data.tokens || {};\n const profile: ProfileData = {\n id_token: tokens.id_token,\n access_token: tokens.access_token,\n refresh_token: tokens.refresh_token,\n account_id: tokens.account_id,\n };\n\n if (data.email) {\n profile.email = data.email;\n }\n\n return profile;\n }\n\n private mergeProfileRecords(existing: ProfileData, incoming: ProfileData): {\n profile: ProfileData;\n metadata?: ProfileMetadata;\n } {\n const merged: ProfileData = {\n ...existing,\n ...incoming,\n created_at: existing.created_at ?? incoming.created_at ?? new Date().toISOString(),\n };\n\n if (typeof merged.auth_method === 'string') {\n merged.auth_method = merged.auth_method.trim().toLowerCase();\n }\n if (!merged.auth_method) {\n merged.auth_method = 'codex-cli';\n }\n\n const metadata = this.extractProfileMetadata(merged);\n const resolvedEmail = this.resolveProfileEmail(merged, metadata);\n\n if (resolvedEmail) {\n merged.email = resolvedEmail;\n }\n\n const workspace = this.resolveWorkspaceIdentity(merged, metadata);\n if (!merged.workspace_id) {\n merged.workspace_id = workspace.id || existing.workspace_id || DEFAULT_WORKSPACE_ID;\n }\n if (!merged.workspace_name) {\n merged.workspace_name = workspace.name ?? existing.workspace_name;\n }\n\n if (this.hasTokenChanges(existing, incoming)) {\n delete merged.tokenAlert;\n }\n\n return { profile: merged, metadata };\n }\n\n private hasTokenChanges(existing: ProfileData, incoming: ProfileData): boolean {\n const tokenKeys: Array<keyof ProfileData> = ['id_token', 'access_token', 'refresh_token', 'account_id', 'workspace_id'];\n\n return tokenKeys.some(key => {\n const nextValue = incoming[key];\n if (typeof nextValue !== 'string' || nextValue.length === 0) {\n return false;\n }\n return nextValue !== existing[key];\n });\n }\n\n async flagRefreshTokenRedeemed(name: string, reason?: string, options?: { observedAt?: string }): Promise<void> {\n await this.initialize();\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n logWarn(`Cannot flag refresh token issue for missing profile '${profileName}'.`);\n return;\n }\n\n const observedAt = options?.observedAt ? Date.parse(options.observedAt) : null;\n const updatedAt = record.updatedAt ? Date.parse(record.updatedAt) : null;\n if (observedAt !== null && updatedAt !== null && observedAt <= updatedAt) {\n return;\n }\n\n const data: ProfileData = record.data;\n\n const trimmedReason = typeof reason === 'string' && reason.trim().length > 0 ? reason.trim() : undefined;\n const normalized = trimmedReason?.toLowerCase() ?? '';\n const storedReason = normalized.includes(REFRESH_TOKEN_REDEEMED_SNIPPET)\n ? REFRESH_TOKEN_REDEEMED_REASON\n : trimmedReason ?? REFRESH_TOKEN_REDEEMED_REASON;\n const alert: TokenAlert = {\n issue: 'refresh-redeemed',\n reason: storedReason,\n recordedAt: new Date().toISOString(),\n };\n\n data.tokenAlert = alert;\n\n try {\n const metadata = this.extractProfileMetadata(data);\n await this.persistProfileRecord(profileName, data, metadata);\n } catch (error) {\n logError(`Failed to persist token alert for profile '${profileName}':`, error);\n }\n }\n\n private async syncProfileTokensFromActiveAuth(\n profileName: string,\n baselineProfile: ProfileData,\n authPath?: string,\n ): Promise<void> {\n const targetPath = authPath ?? this.activeAuth;\n try {\n const authRaw = await fs.readFile(targetPath, 'utf8');\n await this.syncProfileTokensFromAuthContent(profileName, baselineProfile, authRaw);\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to read Codex auth for '${profileName}' while syncing tokens:`, error);\n }\n }\n }\n\n private async syncProfileTokensFromAuthContent(\n profileName: string,\n baselineProfile: ProfileData,\n authContent: string,\n ): Promise<void> {\n let activeAuth: ActiveAuth;\n try {\n activeAuth = JSON.parse(authContent) as ActiveAuth;\n } catch (error) {\n logWarn(`Failed to parse Codex auth while syncing tokens for '${profileName}':`, error);\n return;\n }\n\n const normalized = this.normalizeProfileData(activeAuth);\n const metadata = this.extractProfileMetadata(normalized);\n const workspace = this.resolveWorkspaceIdentity(normalized, metadata);\n normalized.workspace_id = normalized.workspace_id ?? workspace.id ?? DEFAULT_WORKSPACE_ID;\n if (workspace.name) {\n normalized.workspace_name = normalized.workspace_name ?? workspace.name;\n }\n const hasTokens =\n typeof normalized.id_token === 'string' ||\n typeof normalized.access_token === 'string' ||\n typeof normalized.refresh_token === 'string';\n\n if (!hasTokens) {\n return;\n }\n\n let existing = baselineProfile;\n const latestRow = await this.getProfileRowByName(profileName);\n if (latestRow) {\n existing = latestRow.data;\n }\n\n const existingAccountId = this.getAccountIdFromData(existing);\n const newAccountId = this.getAccountIdFromData(normalized);\n\n if (existingAccountId && newAccountId && existingAccountId !== newAccountId) {\n logWarn(\n `Skipped syncing tokens for profile '${profileName}' because Codex auth switched to account '${newAccountId}'.`,\n );\n return;\n }\n\n if (!this.hasTokenChanges(existing, normalized)) {\n return;\n }\n\n const { profile: merged, metadata: mergedMetadata } = this.mergeProfileRecords(existing, normalized);\n\n try {\n await this.persistProfileRecord(profileName, merged, mergedMetadata);\n } catch (error) {\n logError(`Failed to persist refreshed tokens for profile '${profileName}':`, error);\n }\n }\n\n private async copyCodexConfig(targetCodexHome: string): Promise<void> {\n const candidates = ['config.toml', 'config.json', 'config.yaml', 'config.yml'];\n for (const file of candidates) {\n const source = join(this.codexDir, file);\n const destination = join(targetCodexHome, file);\n try {\n await fs.copyFile(source, destination);\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to copy Codex config '${file}' to profile home:`, error);\n }\n }\n }\n }\n\n private getProfileHomePath(profileName: string): string {\n return join(this.profileHomesRoot, profileName);\n }\n\n private ensureTrailingNewline(content: string): string {\n return content.endsWith('\\n') ? content : `${content}\\n`;\n }\n\n private async sanitizeProfileConfig(profileHome: string): Promise<void> {\n const configPath = join(profileHome, 'config.toml');\n let raw: string;\n try {\n raw = await fs.readFile(configPath, 'utf8');\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to read config for profile home '${profileHome}':`, error);\n }\n return;\n }\n\n let parsed: Record<string, unknown>;\n try {\n parsed = parse(raw) as Record<string, unknown>;\n } catch (error) {\n logWarn(`Failed to parse config.toml for profile home '${profileHome}':`, error);\n return;\n }\n\n if (!Object.prototype.hasOwnProperty.call(parsed, 'model_reasoning_effort')) {\n return;\n }\n\n delete parsed['model_reasoning_effort'];\n const sanitized = this.ensureTrailingNewline(stringify(parsed as Record<string, unknown>));\n try {\n await fs.writeFile(configPath, sanitized, 'utf8');\n } catch (error) {\n logWarn(`Failed to write sanitized config.toml for profile home '${profileHome}':`, error);\n }\n }\n\n private async prepareProfileHome(profileName: string, authData: ActiveAuth): Promise<string> {\n const profileHome = this.getProfileHomePath(profileName);\n await fs.mkdir(profileHome, { recursive: true });\n const authPath = join(profileHome, 'auth.json');\n await this.writeAtomic(authPath, JSON.stringify(authData, null, 2));\n await this.copyCodexConfig(profileHome);\n await this.sanitizeProfileConfig(profileHome);\n return profileHome;\n }\n\n /**\n * List all available profiles\n */\n async profileExists(name: string): Promise<boolean> {\n await this.initialize();\n const profileName = this.normalizeProfileName(name);\n return Boolean(await this.getProfileRowByName(profileName));\n }\n\n async listProfiles(): Promise<Profile[]> {\n await this.initialize();\n try {\n const records = await this.listProfileRecords();\n return records\n .map(record => this.buildProfileFromRow(record))\n .filter(profile => !profile.authMethod || profile.authMethod === 'codex-cli')\n .sort((a, b) => {\n const aLabel = a.displayName ?? a.name;\n const bLabel = b.displayName ?? b.name;\n const base = aLabel.localeCompare(bLabel);\n return base !== 0 ? base : a.name.localeCompare(b.name);\n });\n } catch (error) {\n logError('Error reading profiles from disk:', error);\n return [];\n }\n }\n\n /**\n * Create a new profile by running codex login\n */\n async createProfile(name: string): Promise<Profile | null> {\n await this.initialize();\n\n const profileName = this.normalizeProfileName(name);\n if (await this.getProfileRowByName(profileName)) {\n throw new Error(`Profile '${profileName}' already exists!`);\n }\n\n let authData: ActiveAuth;\n try {\n const authRaw = await fs.readFile(this.activeAuth, 'utf8');\n authData = JSON.parse(authRaw) as ActiveAuth;\n } catch (error) {\n logError('Error creating profile:', error);\n throw new Error('Failed to create profile. Make sure Codex CLI is installed and you are logged in.');\n }\n\n const normalizedProfile = this.normalizeProfileData(authData);\n normalizedProfile.created_at = normalizedProfile.created_at ?? new Date().toISOString();\n if (typeof normalizedProfile.auth_method === 'string') {\n normalizedProfile.auth_method = normalizedProfile.auth_method.trim().toLowerCase();\n }\n normalizedProfile.auth_method = normalizedProfile.auth_method ?? 'codex-cli';\n\n const metadata = this.extractProfileMetadata(normalizedProfile);\n const workspace = this.resolveWorkspaceIdentity(normalizedProfile, metadata);\n normalizedProfile.workspace_id = normalizedProfile.workspace_id ?? workspace.id ?? DEFAULT_WORKSPACE_ID;\n if (workspace.name) {\n normalizedProfile.workspace_name = normalizedProfile.workspace_name ?? workspace.name;\n }\n\n const accountId = this.getAccountIdFromData(normalizedProfile);\n if (accountId) {\n const requestedWorkspace = normalizedProfile.workspace_id ?? DEFAULT_WORKSPACE_ID;\n const duplicate = await this.getProfileRowByAccountAndWorkspace(accountId, requestedWorkspace, { authMethod: 'codex-cli' });\n if (duplicate) {\n normalizedProfile.workspace_id = `${requestedWorkspace}:${profileName}`;\n normalizedProfile.workspace_name = normalizedProfile.workspace_name ?? profileName;\n }\n }\n\n const resolvedEmail = this.resolveProfileEmail(normalizedProfile, metadata);\n if (resolvedEmail) {\n normalizedProfile.email = resolvedEmail;\n }\n\n try {\n await this.persistProfileRecord(profileName, normalizedProfile, metadata);\n const stored = await this.getProfileRowByName(profileName);\n return stored ? this.buildProfileFromRow(stored) : null;\n } catch (error) {\n logError('Error creating profile:', error);\n throw new Error('Failed to create profile. Make sure Codex CLI is installed and you are logged in.');\n }\n }\n\n /**\n * Switch to a different profile\n */\n async switchToProfile(name: string): Promise<boolean> {\n await this.initialize();\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n throw new Error(`Profile '${profileName}' not found!`);\n }\n\n try {\n const profileData = record.data;\n const codexFormat = this.convertToCodexFormat(profileData);\n await this.writeAtomic(this.activeAuth, JSON.stringify(codexFormat, null, 2));\n await this.persistPreferredProfileName(profileName);\n return true;\n } catch (error) {\n logError('Error switching profile:', error);\n throw new Error('Failed to switch profile');\n }\n }\n\n async refreshProfileAuth(name: string): Promise<Profile> {\n await this.initialize();\n\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n throw new Error(`Profile '${profileName}' not found!`);\n }\n\n const existing: ProfileData = record.data;\n let activeAuth: ActiveAuth;\n try {\n const authContent = await fs.readFile(this.activeAuth, 'utf8');\n activeAuth = JSON.parse(authContent) as ActiveAuth;\n } catch (error) {\n if (this.isNotFoundError(error)) {\n throw new Error('Codex CLI did not produce an auth file. Complete the login before refreshing this profile.');\n }\n logError('Failed to read Codex auth file during refresh:', error);\n throw new Error('Failed to read Codex auth file. Complete the login and try again.');\n }\n\n const normalized = this.normalizeProfileData(activeAuth);\n const existingAccountId = this.getAccountIdFromData(existing);\n const newAccountId = this.getAccountIdFromData(normalized);\n const normalizedMetadata = this.extractProfileMetadata(normalized);\n const normalizedWorkspace = this.resolveWorkspaceIdentity(normalized, normalizedMetadata);\n normalized.workspace_id = normalized.workspace_id ?? normalizedWorkspace.id ?? DEFAULT_WORKSPACE_ID;\n if (normalizedWorkspace.name) {\n normalized.workspace_name = normalized.workspace_name ?? normalizedWorkspace.name;\n }\n\n if (existingAccountId && newAccountId && existingAccountId !== newAccountId) {\n throw new Error(\n `Active Codex login is for account '${newAccountId}', but profile '${profileName}' is tied to '${existingAccountId}'. Create a new profile for the new account instead.`,\n );\n }\n\n if (existingAccountId && normalized.workspace_id) {\n const currentWorkspace = this.resolveWorkspaceIdentity(existing, this.extractProfileMetadata(existing));\n const targetWorkspaceId = normalized.workspace_id;\n const currentWorkspaceId = currentWorkspace.id ?? DEFAULT_WORKSPACE_ID;\n if (targetWorkspaceId !== currentWorkspaceId) {\n const duplicate = await this.getProfileRowByAccountAndWorkspace(existingAccountId, targetWorkspaceId, {\n authMethod: this.resolveAuthMethod(existing),\n });\n if (duplicate && duplicate.name !== profileName) {\n throw new Error(\n `Account is already saved as profile '${duplicate.name}'. Switch to or delete that profile before creating another.`,\n );\n }\n }\n }\n\n const { profile: merged, metadata } = this.mergeProfileRecords(existing, normalized);\n delete merged.tokenAlert;\n\n await this.persistProfileRecord(profileName, merged, metadata);\n const updated = await this.getProfileRowByName(profileName);\n if (!updated) {\n throw new Error('Failed to persist refreshed profile data.');\n }\n\n const profile = this.buildProfileFromRow(updated);\n if (metadata) {\n profile.metadata = metadata;\n profile.tokenStatus = this.evaluateTokenStatus(merged, metadata);\n }\n return profile;\n }\n\n /**\n * Execute an action with a profile's auth injected.\n * Creates an isolated CODEX_HOME with the profile's auth/config, invokes the action\n * with env overrides, then syncs any refreshed tokens back to the profile.\n */\n async runWithProfileAuth<T>(\n name: string,\n action: (env: NodeJS.ProcessEnv) => Promise<T>,\n ): Promise<T> {\n return this.enqueueAuthSwap(async () => {\n await this.initialize();\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n throw new Error(`Profile '${profileName}' not found!`);\n }\n\n const profileData: ProfileData = record.data;\n const profileHome = this.getProfileHomePath(profileName);\n await fs.mkdir(profileHome, { recursive: true });\n\n if (profileData.auth_method && profileData.auth_method !== 'codex-cli') {\n throw new Error('Unsupported auth method. Codex CLI profiles only.');\n }\n const codexFormat = this.convertToCodexFormat(profileData);\n await this.prepareProfileHome(profileName, codexFormat);\n const authPath = path.join(profileHome, 'auth.json');\n\n const envOverrides: NodeJS.ProcessEnv = {\n ...process.env,\n HOME: profileHome,\n USERPROFILE: profileHome,\n CODEX_HOME: profileHome,\n };\n\n try {\n return await action(envOverrides);\n } finally {\n let finalAuthContent: string | null = null;\n try {\n finalAuthContent = await fs.readFile(authPath, 'utf8');\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to read isolated auth for '${profileName}' after action:`, error);\n }\n }\n\n try {\n if (finalAuthContent) {\n await this.syncProfileTokensFromAuthContent(profileName, profileData, finalAuthContent);\n } else {\n await this.syncProfileTokensFromActiveAuth(profileName, profileData, authPath);\n }\n } catch (error) {\n logWarn(`Failed to sync refreshed tokens for profile '${profileName}':`, error);\n }\n }\n });\n }\n\n /**\n * Rename a profile\n */\n async renameProfile(oldName: string, newName: string): Promise<boolean> {\n await this.initialize();\n const sourceName = this.normalizeProfileName(oldName);\n const targetName = this.normalizeProfileName(newName);\n\n const existing = await this.getProfileRowByName(sourceName);\n if (!existing) {\n throw new Error(`Profile '${sourceName}' not found!`);\n }\n\n if (await this.getProfileRowByName(targetName)) {\n throw new Error(`Profile '${targetName}' already exists!`);\n }\n\n const preferred = await this.readPreferredProfileName();\n if (preferred && preferred === sourceName) {\n await this.persistPreferredProfileName(targetName);\n }\n\n const oldHome = this.getProfileHomePath(sourceName);\n const newHome = this.getProfileHomePath(targetName);\n try {\n await fs.mkdir(this.profileHomesRoot, { recursive: true });\n await fs.rename(oldHome, newHome);\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to move profile home from '${sourceName}' to '${targetName}':`, error);\n }\n }\n\n try {\n const updatedRecord: ProfileRecord = {\n ...existing,\n name: targetName,\n };\n await this.writeProfileRecord(updatedRecord);\n await this.deleteProfileRecord(sourceName);\n } catch (error) {\n logWarn(`Failed to rewrite profile record after renaming '${sourceName}' to '${targetName}':`, error);\n }\n\n return true;\n }\n\n async updateProfileDisplayName(name: string, displayName: string): Promise<boolean> {\n await this.initialize();\n\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n throw new Error(`Profile '${profileName}' not found!`);\n }\n\n const trimmed = typeof displayName === 'string' ? displayName.trim() : '';\n if (!trimmed) {\n throw new Error('Profile name must not be empty.');\n }\n\n const now = new Date().toISOString();\n record.displayName = trimmed;\n record.updatedAt = now;\n (record.data as ProfileData & { updated_at?: string }).updated_at = now;\n\n await this.writeProfileRecord(record);\n return true;\n }\n\n /**\n * Delete a profile\n */\n async deleteProfile(name: string): Promise<boolean> {\n await this.initialize();\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n throw new Error(`Profile '${profileName}' not found!`);\n }\n const preferred = await this.readPreferredProfileName();\n if (preferred && preferred === profileName) {\n await this.persistPreferredProfileName(null);\n }\n try {\n await fs.rm(this.getProfileHomePath(profileName), { recursive: true, force: true });\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to remove profile home for '${profileName}':`, error);\n }\n }\n await this.deleteProfileRecord(profileName);\n return true;\n }\n\n /**\n * Delete every profile that does not appear in the allow-list.\n * Used to enforce plan limits when local storage was tampered with.\n */\n async deleteProfilesNotIn(allowedNames: Iterable<string>): Promise<string[]> {\n await this.initialize();\n const normalized = new Set<string>();\n for (const name of allowedNames) {\n try {\n normalized.add(this.normalizeProfileName(name));\n } catch {\n // Skip invalid names so we only retain well-formed entries.\n }\n }\n\n const records = await this.listProfileRecords();\n const deleteCandidates = records\n .map(record => {\n try {\n return this.normalizeProfileName(record.name);\n } catch {\n return null;\n }\n })\n .filter((name): name is string => Boolean(name) && !normalized.has(name));\n\n if (deleteCandidates.length === 0) {\n return [];\n }\n\n const removed: string[] = [];\n for (const name of deleteCandidates) {\n try {\n await fs.rm(this.getProfileHomePath(name), { recursive: true, force: true });\n await this.deleteProfileRecord(name);\n removed.push(name);\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to remove profile '${name}' during plan enforcement:`, error);\n }\n }\n }\n const preferred = await this.readPreferredProfileName();\n if (preferred && removed.includes(preferred)) {\n await this.persistPreferredProfileName(null);\n }\n return removed;\n }\n\n /**\n * Check if the current auth matches a profile (for smart detection)\n */\n async getCurrentProfile(): Promise<{ name: string | null; trusted: boolean }> {\n await this.initialize();\n const preferredName = await this.readPreferredProfileName();\n if (preferredName) {\n try {\n const normalizedPreferred = this.normalizeProfileName(preferredName);\n const preferredRecord = await this.getProfileRowByName(normalizedPreferred);\n if (preferredRecord) {\n return { name: normalizedPreferred, trusted: true };\n }\n } catch {\n // fall through to active auth detection\n }\n }\n const activeAuth = await this.readActiveAuthFile();\n\n if (activeAuth) {\n const normalizedAuth = this.normalizeProfileData(activeAuth);\n const authMetadata = this.extractProfileMetadata(normalizedAuth);\n const workspace = this.resolveWorkspaceIdentity(normalizedAuth, authMetadata);\n const activeAccountId = this.getAccountIdFromData(normalizedAuth);\n\n if (activeAccountId) {\n const scoped = await this.getProfileRowByAccountAndWorkspace(activeAccountId, workspace.id ?? DEFAULT_WORKSPACE_ID, {\n preferAuthMethod: 'codex-cli',\n });\n if (scoped) {\n const normalized = this.normalizeProfileName(scoped.name);\n await this.persistPreferredProfileName(normalized);\n return { name: normalized, trusted: true };\n }\n\n const matching = await this.getProfileRowByAccountId(activeAccountId, { preferAuthMethod: 'codex-cli' });\n if (matching) {\n const normalized = this.normalizeProfileName(matching.name);\n await this.persistPreferredProfileName(normalized);\n return { name: normalized, trusted: true };\n }\n }\n }\n\n const preferred = await this.readPreferredProfileName();\n if (preferred) {\n try {\n const normalized = this.normalizeProfileName(preferred);\n const exists = await this.getProfileRowByName(normalized);\n if (exists) {\n return { name: normalized, trusted: true };\n }\n } catch {\n await this.persistPreferredProfileName(null);\n return { name: null, trusted: false };\n }\n await this.persistPreferredProfileName(null);\n }\n\n return { name: null, trusted: false };\n }\n\n /**\n * Export a profile to a portable format.\n * Sensitive tokens are included but should be handled securely.\n */\n async exportProfile(name: string): Promise<{\n name: string;\n displayName: string | null;\n email: string | null;\n accountId: string | null;\n workspaceId: string | null;\n workspaceName: string | null;\n authMethod: string | null;\n createdAt: string | null;\n metadata: ProfileMetadata | undefined;\n exportedAt: string;\n }> {\n await this.initialize();\n const profileName = this.normalizeProfileName(name);\n const record = await this.getProfileRowByName(profileName);\n if (!record) {\n throw new Error(`Profile '${profileName}' not found!`);\n }\n\n return {\n name: record.name,\n displayName: record.displayName,\n email: record.email,\n accountId: record.accountId,\n workspaceId: record.workspaceId,\n workspaceName: record.workspaceName,\n authMethod: record.authMethod,\n createdAt: record.createdAt,\n metadata: record.metadata,\n exportedAt: new Date().toISOString(),\n };\n }\n\n /**\n * Export profiles for cloud sync (includes token-bearing auth data).\n */\n async exportProfilesForCloudSync(): Promise<CloudSyncProfileRecord[]> {\n await this.initialize();\n const records = await this.listProfileRecords();\n return records.map(record => ({\n name: record.name,\n displayName: record.displayName,\n data: record.data,\n metadata: record.metadata,\n accountId: record.accountId,\n workspaceId: record.workspaceId,\n workspaceName: record.workspaceName,\n email: record.email,\n authMethod: record.authMethod,\n createdAt: record.createdAt,\n updatedAt: record.updatedAt,\n }));\n }\n\n /**\n * Replace local profiles with a full snapshot from cloud sync.\n */\n async replaceProfilesFromCloudSync(records: CloudSyncProfileRecord[]): Promise<{ imported: number; removed: number }> {\n await this.initialize();\n const now = new Date().toISOString();\n const normalized = new Map<string, ProfileRecord>();\n const existingMap = await this.readProfilesStateMap();\n const existingNames = new Set(Object.keys(existingMap));\n\n for (const candidate of records ?? []) {\n try {\n const name = this.normalizeProfileName(candidate?.name ?? '');\n const data =\n candidate && typeof candidate.data === 'object' && candidate.data !== null\n ? (candidate.data as ProfileData)\n : null;\n if (!data) {\n continue;\n }\n\n const metadata =\n candidate.metadata && typeof candidate.metadata === 'object'\n ? candidate.metadata\n : undefined;\n\n const record: ProfileRecord = {\n name,\n displayName: typeof candidate.displayName === 'string' ? candidate.displayName : null,\n data,\n metadata,\n accountId: typeof candidate.accountId === 'string' ? candidate.accountId : null,\n workspaceId: typeof candidate.workspaceId === 'string' ? candidate.workspaceId : null,\n workspaceName: typeof candidate.workspaceName === 'string' ? candidate.workspaceName : null,\n email: typeof candidate.email === 'string' ? candidate.email : null,\n authMethod: typeof candidate.authMethod === 'string' ? candidate.authMethod : this.resolveAuthMethod(data),\n createdAt: typeof candidate.createdAt === 'string' ? candidate.createdAt : now,\n updatedAt: typeof candidate.updatedAt === 'string' ? candidate.updatedAt : now,\n };\n\n normalized.set(name, record);\n } catch {\n // Ignore malformed record entries.\n }\n }\n\n const nextMap: Record<string, ProfileRecordState> = {};\n for (const record of normalized.values()) {\n nextMap[record.name] = this.toStateRecord(record);\n }\n await this.writeProfilesStateMap(nextMap);\n\n let removed = 0;\n\n for (const name of existingNames) {\n if (normalized.has(name)) {\n continue;\n }\n try {\n await fs.rm(this.getProfileHomePath(name), { recursive: true, force: true });\n removed += 1;\n } catch (error) {\n if (!this.isNotFoundError(error)) {\n logWarn(`Failed to remove profile '${name}' during cloud sync replace:`, error);\n }\n }\n }\n\n const preferred = await this.readPreferredProfileName();\n if (preferred) {\n try {\n const normalizedPreferred = this.normalizeProfileName(preferred);\n if (!normalized.has(normalizedPreferred)) {\n await this.persistPreferredProfileName(null);\n }\n } catch {\n await this.persistPreferredProfileName(null);\n }\n }\n\n return {\n imported: normalized.size,\n removed,\n };\n }\n}\n","export interface AutoRollSettings {\n enabled: boolean;\n warningThreshold: number;\n switchThreshold: number;\n}\n\nexport const DEFAULT_AUTO_ROLL_ENABLED = false;\nexport const DEFAULT_AUTO_ROLL_WARNING_THRESHOLD = 85;\nexport const DEFAULT_AUTO_ROLL_SWITCH_THRESHOLD = 95;\nexport const AUTO_ROLL_WARNING_MIN = 50;\n\nfunction clampNumber(value: number, min: number, max: number): number {\n return Math.min(max, Math.max(min, value));\n}\n\n/**\n * Normalize partial auto-roll settings into a safe shape with bounds.\n * Ensures switch threshold is always above warning and both live within [50, 100].\n */\nexport function normalizeAutoRollSettings(\n raw?: Partial<AutoRollSettings> | null,\n): AutoRollSettings {\n const enabled = typeof raw?.enabled === 'boolean' ? raw.enabled : DEFAULT_AUTO_ROLL_ENABLED;\n const rawWarning =\n typeof raw?.warningThreshold === 'number' && Number.isFinite(raw.warningThreshold)\n ? raw.warningThreshold\n : DEFAULT_AUTO_ROLL_WARNING_THRESHOLD;\n const rawSwitch =\n typeof raw?.switchThreshold === 'number' && Number.isFinite(raw.switchThreshold)\n ? raw.switchThreshold\n : DEFAULT_AUTO_ROLL_SWITCH_THRESHOLD;\n\n // First clamp warning to valid range, then ensure switch is above it\n const normalizedWarning = clampNumber(rawWarning, AUTO_ROLL_WARNING_MIN, 99);\n const normalizedSwitch = clampNumber(rawSwitch, normalizedWarning + 1, 100);\n\n return {\n enabled,\n warningThreshold: normalizedWarning,\n switchThreshold: normalizedSwitch,\n };\n}\n","export type CodexCliChannel = \"stable\" | \"alpha\";\n\nconst KNOWN_CHANNELS = new Set<CodexCliChannel>([\"stable\", \"alpha\"]);\n\nexport function parseCodexCliChannel(value?: unknown): CodexCliChannel | null {\n if (typeof value !== \"string\") {\n return null;\n }\n\n const normalized = value.trim().toLowerCase();\n if (!normalized) {\n return null;\n }\n\n return KNOWN_CHANNELS.has(normalized as CodexCliChannel)\n ? (normalized as CodexCliChannel)\n : null;\n}\n\nexport function normalizeCodexCliChannel(\n value?: unknown,\n fallback: CodexCliChannel = \"stable\",\n): CodexCliChannel {\n return parseCodexCliChannel(value) ?? fallback;\n}\n","import { promises as fs } from \"node:fs\";\nimport { AsyncLocalStorage } from \"node:async_hooks\";\nimport path from \"node:path\";\nimport os from \"node:os\";\nimport type { ProfileData, ProfileMetadata } from \"./types\";\n\nconst APP_STATE_FILE = \"app-state.json\";\nconst APP_NAME = \"codexuse-desktop\";\n\nexport type LicenseStatusCode = \"inactive\" | \"active\" | \"grace\" | \"error\";\nexport type ReasoningEffort = \"minimal\" | \"low\" | \"medium\" | \"high\" | \"xhigh\" | \"none\";\n\nexport type ProfileRecordState = {\n name: string;\n displayName: string | null;\n data: ProfileData;\n metadata?: ProfileMetadata;\n accountId: string | null;\n workspaceId: string | null;\n workspaceName: string | null;\n email: string | null;\n authMethod: string | null;\n createdAt: string | null;\n updatedAt: string | null;\n};\n\nexport type SkillsInstallState = {\n id: string;\n repo?: string | null;\n repoPath?: string | null;\n sourceLabel?: string | null;\n sourceType?: \"official\" | \"community\" | \"local\";\n viewUrl?: string | null;\n createdAt?: string | null;\n};\n\nexport type SyncState = {\n lastPushAt: string | null;\n lastPullAt: string | null;\n lastError: string | null;\n remoteUpdatedAt: string | null;\n};\n\nexport type AppState = {\n schemaVersion: 1;\n autoRoll: {\n enabled: boolean;\n warningThreshold: number;\n switchThreshold: number;\n };\n app: {\n lastAppVersion: string | null;\n pendingUpdateVersion: string | null;\n cliChannel: \"stable\" | \"alpha\";\n lastProfileName: string | null;\n };\n license: {\n licenseKey: string | null;\n purchaseEmail: string | null;\n lastVerifiedAt: string | null;\n nextCheckAt: string | null;\n lastVerificationError: string | null;\n status: LicenseStatusCode;\n signature: string | null;\n };\n providers: {\n selectedProviderId: string;\n defaultModel: string | null;\n defaultReasoningEffort: ReasoningEffort;\n list: Array<{\n id: string;\n name: string;\n models: string[];\n apiKey?: string;\n baseUrl?: string;\n }>;\n overridesByPath: Record<string, { selectedModel?: string | null; reasoningEffort?: ReasoningEffort }>;\n };\n sandbox: {\n defaultMode: string;\n defaultApprovalPolicy: string;\n overridesByPath: Record<string, { mode?: string; approvalPolicy?: string }>;\n };\n preferences: {\n excludeFolders: string[];\n enableTaskCompleteBeep: boolean;\n preventSleepDuringTasks: boolean;\n folderHistory: Array<{ path: string; name: string; lastVisited: number }>;\n pinnedPaths: string[];\n };\n projectSettingsByPath: Record<\n string,\n {\n systemPrompt?: string;\n runScript?: string;\n buildScript?: string;\n openIdeCommand?: string;\n }\n >;\n conversationCategoriesByCwd: Record<string, Array<{ id: string; name: string }>>;\n conversationCategoryAssignmentsByCwd: Record<string, Record<string, string>>;\n skills: {\n sources: Array<{\n id: string;\n repo: string;\n label: string;\n branch?: string | null;\n url?: string | null;\n official?: boolean;\n compatibilityNote?: string | null;\n priority?: number;\n }>;\n installsBySlug: Record<string, SkillsInstallState>;\n };\n sync: SyncState;\n profilesByName: Record<string, ProfileRecordState>;\n migration: {\n status: \"pending\" | \"pending_local_storage\" | \"complete\";\n startedAt: string | null;\n completedAt: string | null;\n localStorageImportedAt: string | null;\n lastError?: string | null;\n };\n};\n\nexport type AppStatePatch = DeepPartial<AppState>;\nexport type AppStateUpdateMode = \"replace\" | \"patch\";\nexport type AppStateUpdateOptions = {\n allowBeforeMigrationComplete?: boolean;\n mode?: AppStateUpdateMode;\n};\ntype AppStateUpdateTransform = (current: AppState) => AppState | AppStatePatch;\n\ntype DeepPartial<T> = {\n [K in keyof T]?: T[K] extends Array<infer U>\n ? U[]\n : T[K] extends object\n ? DeepPartial<T[K]>\n : T[K];\n};\n\nlet configuredUserDataDir: string | null = null;\nlet appStateCache: AppState | null = null;\nlet writeLock: Promise<void> = Promise.resolve();\nconst writeLockContext = new AsyncLocalStorage<boolean>();\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return Boolean(value) && typeof value === \"object\" && !Array.isArray(value);\n}\n\nfunction clone<T>(value: T): T {\n return JSON.parse(JSON.stringify(value)) as T;\n}\n\nfunction resolveDefaultUserDataDir(): string {\n const home = process.env.HOME || process.env.USERPROFILE || os.homedir();\n if (!home) {\n throw new Error(\"Unable to resolve home directory for app state.\");\n }\n\n if (process.platform === \"darwin\") {\n return path.join(home, \"Library\", \"Application Support\", APP_NAME);\n }\n if (process.platform === \"win32\") {\n const appData = process.env.APPDATA;\n if (appData) {\n return path.join(appData, APP_NAME);\n }\n return path.join(home, \"AppData\", \"Roaming\", APP_NAME);\n }\n return path.join(home, \".config\", APP_NAME);\n}\n\nexport function getUserDataDir(): string {\n return configuredUserDataDir ?? resolveDefaultUserDataDir();\n}\n\nfunction resolveAppStatePath(): string {\n return path.join(getUserDataDir(), APP_STATE_FILE);\n}\n\nfunction createDefaultAppState(): AppState {\n return {\n schemaVersion: 1,\n autoRoll: {\n enabled: false,\n warningThreshold: 85,\n switchThreshold: 95,\n },\n app: {\n lastAppVersion: null,\n pendingUpdateVersion: null,\n cliChannel: \"stable\",\n lastProfileName: null,\n },\n license: {\n licenseKey: null,\n purchaseEmail: null,\n lastVerifiedAt: null,\n nextCheckAt: null,\n lastVerificationError: null,\n status: \"inactive\",\n signature: null,\n },\n providers: {\n selectedProviderId: \"openai\",\n defaultModel: null,\n defaultReasoningEffort: \"medium\",\n list: [],\n overridesByPath: {},\n },\n sandbox: {\n defaultMode: \"chat\",\n defaultApprovalPolicy: \"on-failure\",\n overridesByPath: {},\n },\n preferences: {\n excludeFolders: [],\n enableTaskCompleteBeep: true,\n preventSleepDuringTasks: true,\n folderHistory: [],\n pinnedPaths: [],\n },\n projectSettingsByPath: {},\n conversationCategoriesByCwd: {},\n conversationCategoryAssignmentsByCwd: {},\n skills: {\n sources: [],\n installsBySlug: {},\n },\n sync: {\n lastPushAt: null,\n lastPullAt: null,\n lastError: null,\n remoteUpdatedAt: null,\n },\n profilesByName: {},\n migration: {\n status: \"pending\",\n startedAt: null,\n completedAt: null,\n localStorageImportedAt: null,\n lastError: null,\n },\n };\n}\n\nfunction asString(value: unknown): string | null {\n if (typeof value !== \"string\") {\n return null;\n }\n const trimmed = value.trim();\n return trimmed.length > 0 ? trimmed : null;\n}\n\nfunction normalizeAppState(raw: unknown): AppState {\n const defaults = createDefaultAppState();\n if (!isRecord(raw)) {\n return defaults;\n }\n\n const next = deepMerge(defaults, raw as AppStatePatch);\n const merged = clone(next);\n\n merged.schemaVersion = 1;\n if (typeof merged.autoRoll.enabled !== \"boolean\") {\n merged.autoRoll.enabled = false;\n }\n if (!Number.isFinite(merged.autoRoll.warningThreshold)) {\n merged.autoRoll.warningThreshold = 85;\n }\n if (!Number.isFinite(merged.autoRoll.switchThreshold)) {\n merged.autoRoll.switchThreshold = 95;\n }\n if (merged.autoRoll.warningThreshold < 50 || merged.autoRoll.warningThreshold > 99) {\n merged.autoRoll.warningThreshold = 85;\n }\n if (merged.autoRoll.switchThreshold <= merged.autoRoll.warningThreshold || merged.autoRoll.switchThreshold > 100) {\n merged.autoRoll.switchThreshold = Math.min(100, Math.max(merged.autoRoll.warningThreshold + 1, 95));\n }\n merged.app.cliChannel = merged.app.cliChannel === \"alpha\" ? \"alpha\" : \"stable\";\n merged.app.lastAppVersion = asString(merged.app.lastAppVersion);\n merged.app.pendingUpdateVersion = asString(merged.app.pendingUpdateVersion);\n merged.app.lastProfileName = asString(merged.app.lastProfileName);\n\n merged.license.licenseKey = asString(merged.license.licenseKey);\n merged.license.purchaseEmail = asString(merged.license.purchaseEmail);\n merged.license.lastVerifiedAt = asString(merged.license.lastVerifiedAt);\n merged.license.nextCheckAt = asString(merged.license.nextCheckAt);\n merged.license.lastVerificationError = asString(merged.license.lastVerificationError);\n merged.license.signature = asString(merged.license.signature);\n if (![\"inactive\", \"active\", \"grace\", \"error\"].includes(merged.license.status)) {\n merged.license.status = \"inactive\";\n }\n\n if (!Array.isArray(merged.providers.list)) {\n merged.providers.list = [];\n }\n merged.providers.selectedProviderId = asString(merged.providers.selectedProviderId) ?? \"openai\";\n merged.providers.defaultModel = asString(merged.providers.defaultModel);\n if (![\"minimal\", \"low\", \"medium\", \"high\", \"xhigh\", \"none\"].includes(merged.providers.defaultReasoningEffort)) {\n merged.providers.defaultReasoningEffort = \"medium\";\n }\n\n if (!isRecord(merged.providers.overridesByPath)) {\n merged.providers.overridesByPath = {};\n }\n if (!isRecord(merged.sandbox.overridesByPath)) {\n merged.sandbox.overridesByPath = {};\n }\n\n if (!Array.isArray(merged.preferences.excludeFolders)) {\n merged.preferences.excludeFolders = [];\n }\n if (!Array.isArray(merged.preferences.folderHistory)) {\n merged.preferences.folderHistory = [];\n }\n if (!Array.isArray(merged.preferences.pinnedPaths)) {\n merged.preferences.pinnedPaths = [];\n }\n\n if (!isRecord(merged.projectSettingsByPath)) {\n merged.projectSettingsByPath = {};\n }\n if (!isRecord(merged.conversationCategoriesByCwd)) {\n merged.conversationCategoriesByCwd = {};\n }\n if (!isRecord(merged.conversationCategoryAssignmentsByCwd)) {\n merged.conversationCategoryAssignmentsByCwd = {};\n }\n\n if (!isRecord(merged.skills)) {\n merged.skills = clone(defaults.skills);\n }\n if (!Array.isArray(merged.skills.sources)) {\n merged.skills.sources = [];\n }\n if (!isRecord(merged.skills.installsBySlug)) {\n merged.skills.installsBySlug = {};\n }\n\n if (!isRecord(merged.sync)) {\n merged.sync = clone(defaults.sync);\n }\n merged.sync.lastPushAt = asString(merged.sync.lastPushAt);\n merged.sync.lastPullAt = asString(merged.sync.lastPullAt);\n merged.sync.lastError = asString(merged.sync.lastError);\n merged.sync.remoteUpdatedAt = asString(merged.sync.remoteUpdatedAt);\n\n if (!isRecord(merged.profilesByName)) {\n merged.profilesByName = {};\n }\n\n if (!isRecord(merged.migration)) {\n merged.migration = clone(defaults.migration);\n }\n if (![\"pending\", \"pending_local_storage\", \"complete\"].includes(merged.migration.status)) {\n merged.migration.status = \"pending\";\n }\n merged.migration.startedAt = asString(merged.migration.startedAt);\n merged.migration.completedAt = asString(merged.migration.completedAt);\n merged.migration.localStorageImportedAt = asString(merged.migration.localStorageImportedAt);\n merged.migration.lastError = asString(merged.migration.lastError);\n\n return merged;\n}\n\nfunction deepMerge<T>(base: T, patch: DeepPartial<T>): T {\n if (!isRecord(base) || !isRecord(patch)) {\n return clone((patch as T) ?? base);\n }\n\n const next: Record<string, unknown> = { ...(base as Record<string, unknown>) };\n for (const [key, patchValue] of Object.entries(patch)) {\n if (patchValue === undefined) {\n continue;\n }\n\n const currentValue = next[key];\n if (Array.isArray(patchValue)) {\n next[key] = clone(patchValue);\n continue;\n }\n\n if (isRecord(currentValue) && isRecord(patchValue)) {\n next[key] = deepMerge(currentValue, patchValue as DeepPartial<typeof currentValue>);\n continue;\n }\n\n next[key] = clone(patchValue);\n }\n\n return next as T;\n}\n\nasync function writeAtomic(filePath: string, contents: string): Promise<void> {\n const dir = path.dirname(filePath);\n const base = path.basename(filePath);\n const token = `${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}`;\n const tempPath = path.join(dir, `${base}.${token}.tmp`);\n\n await fs.mkdir(dir, { recursive: true });\n await fs.writeFile(tempPath, contents, \"utf8\");\n try {\n await fs.rename(tempPath, filePath);\n } finally {\n await fs.rm(tempPath, { force: true }).catch(() => undefined);\n }\n}\n\nasync function writeAppStateToDisk(state: AppState): Promise<void> {\n const filePath = resolveAppStatePath();\n await writeAtomic(filePath, `${JSON.stringify(state, null, 2)}\\n`);\n}\n\nasync function readAppStateFromDisk(): Promise<AppState | null> {\n const filePath = resolveAppStatePath();\n try {\n const raw = await fs.readFile(filePath, \"utf8\");\n return normalizeAppState(JSON.parse(raw) as unknown);\n } catch (error) {\n const code = (error as NodeJS.ErrnoException).code;\n if (code === \"ENOENT\") {\n return null;\n }\n throw error;\n }\n}\n\nasync function ensureInitialized(): Promise<AppState> {\n if (appStateCache) {\n return appStateCache;\n }\n\n const filePath = resolveAppStatePath();\n await fs.mkdir(path.dirname(filePath), { recursive: true });\n\n const loaded = await readAppStateFromDisk();\n const normalized = loaded ?? normalizeAppState(null);\n appStateCache = normalized;\n\n if (loaded === null) {\n await writeAppStateToDisk(normalized);\n }\n\n return appStateCache;\n}\n\nasync function withWriteLock<T>(task: () => Promise<T>): Promise<T> {\n if (writeLockContext.getStore()) {\n return task();\n }\n\n const previous = writeLock;\n let release: (() => void) | null = null;\n writeLock = new Promise<void>(resolve => {\n release = resolve;\n });\n\n await previous;\n try {\n return await writeLockContext.run(true, task);\n } finally {\n release?.();\n }\n}\n\nexport async function initializeAppState(userDataDir: string): Promise<AppState> {\n configuredUserDataDir = userDataDir;\n const state = await ensureInitialized();\n return clone(state);\n}\n\nexport async function getAppState(): Promise<AppState> {\n const state = await ensureInitialized();\n return clone(state);\n}\n\nexport async function updateAppState(\n transform: AppStateUpdateTransform,\n options: AppStateUpdateOptions = {},\n): Promise<AppState> {\n const mode = options.mode ?? \"patch\";\n const allowBeforeMigrationComplete = options.allowBeforeMigrationComplete === true;\n\n return withWriteLock(async () => {\n const current = (await readAppStateFromDisk()) ?? (await ensureInitialized());\n appStateCache = current;\n if (!allowBeforeMigrationComplete && current.migration.status !== \"complete\") {\n throw new Error(\"Storage migration is not complete yet.\");\n }\n\n const transformed = transform(clone(current));\n const nextState = mode === \"replace\"\n ? normalizeAppState(transformed)\n : normalizeAppState(deepMerge(current, transformed as AppStatePatch));\n await writeAppStateToDisk(nextState);\n appStateCache = nextState;\n return clone(nextState);\n });\n}\n\nexport async function patchAppState(patch: AppStatePatch): Promise<AppState> {\n return updateAppState(() => patch, {\n mode: \"patch\",\n allowBeforeMigrationComplete: false,\n });\n}\n","import { normalizeAutoRollSettings, type AutoRollSettings } from \"./auto-roll-settings\";\nimport { normalizeCodexCliChannel, parseCodexCliChannel, type CodexCliChannel } from \"./codex-cli-channel\";\nimport { getAppState, patchAppState, type AppState } from \"./app-state\";\n\ntype LicenseStatusCode = \"inactive\" | \"active\" | \"grace\" | \"error\";\n\ninterface StoredLicense {\n licenseKey?: string | null;\n purchaseEmail?: string | null;\n lastVerifiedAt?: string | null;\n nextCheckAt?: string | null;\n lastVerificationError?: string | null;\n status?: LicenseStatusCode;\n signature?: string | null;\n}\n\ninterface CodexSettings {\n license?: StoredLicense | null;\n lastProfileName?: string | null;\n autoRoll?: AutoRollSettings | null;\n lastAppVersion?: string | null;\n pendingUpdateVersion?: string | null;\n cliChannel?: CodexCliChannel | null;\n}\n\nfunction toCodexSettings(state: AppState): CodexSettings {\n const license = state.license.licenseKey\n ? {\n licenseKey: state.license.licenseKey,\n purchaseEmail: state.license.purchaseEmail,\n lastVerifiedAt: state.license.lastVerifiedAt,\n nextCheckAt: state.license.nextCheckAt,\n lastVerificationError: state.license.lastVerificationError,\n status: state.license.status,\n signature: state.license.signature,\n }\n : null;\n\n const autoRoll = normalizeAutoRollSettings(state.autoRoll);\n\n return {\n ...(license ? { license } : {}),\n ...(state.app.lastProfileName ? { lastProfileName: state.app.lastProfileName } : {}),\n ...(autoRoll ? { autoRoll } : {}),\n ...(state.app.lastAppVersion ? { lastAppVersion: state.app.lastAppVersion } : {}),\n ...(state.app.pendingUpdateVersion ? { pendingUpdateVersion: state.app.pendingUpdateVersion } : {}),\n ...(state.app.cliChannel ? { cliChannel: state.app.cliChannel } : {}),\n };\n}\n\nfunction asString(value: unknown): string | null {\n if (typeof value !== \"string\") {\n return null;\n }\n const trimmed = value.trim();\n return trimmed.length > 0 ? trimmed : null;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return Boolean(value) && typeof value === \"object\" && !Array.isArray(value);\n}\n\nfunction parseStoredLicense(raw?: unknown): StoredLicense | null {\n if (!isRecord(raw)) {\n return null;\n }\n\n const statusCandidate = asString(raw.status);\n const status = [\"inactive\", \"active\", \"grace\", \"error\"].includes(statusCandidate ?? \"\")\n ? (statusCandidate as LicenseStatusCode)\n : undefined;\n\n const license: StoredLicense = {\n licenseKey: asString(raw.licenseKey ?? raw.license_key),\n purchaseEmail: asString(raw.purchaseEmail ?? raw.purchase_email),\n lastVerifiedAt: asString(raw.lastVerifiedAt ?? raw.last_verified_at),\n nextCheckAt: asString(raw.nextCheckAt ?? raw.next_check_at),\n lastVerificationError: asString(raw.lastVerificationError ?? raw.last_verification_error),\n status,\n signature: asString(raw.signature),\n };\n\n const hasValue = Boolean(\n license.licenseKey ||\n license.purchaseEmail ||\n license.lastVerifiedAt ||\n license.nextCheckAt ||\n license.lastVerificationError ||\n license.status,\n );\n\n return hasValue ? license : null;\n}\n\nfunction parseAutoRoll(raw: unknown): AutoRollSettings | null {\n if (!raw) {\n return null;\n }\n try {\n return normalizeAutoRollSettings(raw as Partial<AutoRollSettings>);\n } catch {\n return null;\n }\n}\n\nexport async function getCodexSettings(): Promise<CodexSettings> {\n const state = await getAppState();\n return toCodexSettings(state);\n}\n\nexport async function setCodexSettings(settings: CodexSettings): Promise<void> {\n const autoRoll = parseAutoRoll(settings.autoRoll);\n const license = parseStoredLicense(settings.license);\n await patchAppState({\n app: {\n lastProfileName: asString(settings.lastProfileName),\n lastAppVersion: asString(settings.lastAppVersion),\n pendingUpdateVersion: asString(settings.pendingUpdateVersion),\n cliChannel: settings.cliChannel ? normalizeCodexCliChannel(settings.cliChannel) : undefined,\n },\n autoRoll: autoRoll\n ? {\n enabled: autoRoll.enabled,\n warningThreshold: autoRoll.warningThreshold,\n switchThreshold: autoRoll.switchThreshold,\n }\n : undefined,\n license: license\n ? {\n licenseKey: license.licenseKey ?? null,\n purchaseEmail: license.purchaseEmail ?? null,\n lastVerifiedAt: license.lastVerifiedAt ?? null,\n nextCheckAt: license.nextCheckAt ?? null,\n lastVerificationError: license.lastVerificationError ?? null,\n status: license.status ?? \"inactive\",\n signature: license.signature ?? null,\n }\n : {\n licenseKey: null,\n purchaseEmail: null,\n lastVerifiedAt: null,\n nextCheckAt: null,\n lastVerificationError: null,\n status: \"inactive\",\n signature: null,\n },\n });\n}\n\nexport async function getLastProfileName(): Promise<string | null> {\n const state = await getAppState();\n return asString(state.app.lastProfileName);\n}\n\nexport async function persistLastProfileName(profileName: string | null): Promise<void> {\n await patchAppState({\n app: {\n lastProfileName: asString(profileName),\n },\n });\n}\n\nexport async function getStoredLicense(): Promise<StoredLicense | null> {\n const state = await getAppState();\n return parseStoredLicense(state.license);\n}\n\nexport async function persistLicense(license: StoredLicense | null): Promise<void> {\n const parsed = parseStoredLicense(license);\n await patchAppState({\n license: parsed\n ? {\n licenseKey: parsed.licenseKey ?? null,\n purchaseEmail: parsed.purchaseEmail ?? null,\n lastVerifiedAt: parsed.lastVerifiedAt ?? null,\n nextCheckAt: parsed.nextCheckAt ?? null,\n lastVerificationError: parsed.lastVerificationError ?? null,\n status: parsed.status ?? \"inactive\",\n signature: parsed.signature ?? null,\n }\n : {\n licenseKey: null,\n purchaseEmail: null,\n lastVerifiedAt: null,\n nextCheckAt: null,\n lastVerificationError: null,\n status: \"inactive\",\n signature: null,\n },\n });\n}\n\nexport async function getStoredAutoRollSettings(): Promise<AutoRollSettings | null> {\n const state = await getAppState();\n return normalizeAutoRollSettings(state.autoRoll);\n}\n\nexport async function persistAutoRollSettings(settings: AutoRollSettings | null): Promise<void> {\n const normalized = normalizeAutoRollSettings(settings ?? undefined);\n await patchAppState({\n autoRoll: {\n enabled: normalized.enabled,\n warningThreshold: normalized.warningThreshold,\n switchThreshold: normalized.switchThreshold,\n },\n });\n}\n\nexport async function getStoredAppVersion(): Promise<string | null> {\n const state = await getAppState();\n return asString(state.app.lastAppVersion);\n}\n\nexport async function persistAppVersion(version: string | null): Promise<void> {\n await patchAppState({\n app: {\n lastAppVersion: asString(version),\n },\n });\n}\n\nexport async function getPendingUpdateVersion(): Promise<string | null> {\n const state = await getAppState();\n return asString(state.app.pendingUpdateVersion);\n}\n\nexport async function persistPendingUpdateVersion(version: string | null): Promise<void> {\n await patchAppState({\n app: {\n pendingUpdateVersion: asString(version),\n },\n });\n}\n\nexport async function getCodexCliChannel(): Promise<CodexCliChannel> {\n const state = await getAppState();\n return normalizeCodexCliChannel(state.app.cliChannel);\n}\n\nexport async function persistCodexCliChannel(channel: CodexCliChannel | null): Promise<void> {\n const normalized = channel ? normalizeCodexCliChannel(channel) : \"stable\";\n await patchAppState({\n app: {\n cliChannel: normalized,\n },\n });\n}\n\nexport async function readCodexSettingsJsonRaw(): Promise<Record<string, unknown>> {\n const state = await getAppState();\n return {\n lastProfileName: state.app.lastProfileName,\n lastAppVersion: state.app.lastAppVersion,\n pendingUpdateVersion: state.app.pendingUpdateVersion,\n cliChannel: state.app.cliChannel,\n autoRoll: state.autoRoll,\n license: state.license,\n providers: state.providers.list,\n selectedProviderId: state.providers.selectedProviderId,\n defaultModel: state.providers.defaultModel,\n defaultReasoningEffort: state.providers.defaultReasoningEffort,\n selectionsByCwd: state.providers.overridesByPath,\n mode: state.sandbox.defaultMode,\n approvalPolicy: state.sandbox.defaultApprovalPolicy,\n sandboxSelectionsByCwd: state.sandbox.overridesByPath,\n excludeFolders: state.preferences.excludeFolders,\n enableTaskCompleteBeep: state.preferences.enableTaskCompleteBeep,\n preventSleepDuringTasks: state.preferences.preventSleepDuringTasks,\n folderHistory: state.preferences.folderHistory,\n pinnedPaths: state.preferences.pinnedPaths,\n projectSettingsByPath: state.projectSettingsByPath,\n categoriesByCwd: state.conversationCategoriesByCwd,\n conversationCategoryByCwd: state.conversationCategoryAssignmentsByCwd,\n sync: state.sync,\n };\n}\n\nexport async function writeCodexSettingsJsonRaw(payload: Record<string, unknown> | null | undefined): Promise<void> {\n if (!isRecord(payload)) {\n return;\n }\n\n const autoRoll = parseAutoRoll(payload.autoRoll ?? payload.auto_roll);\n const license = parseStoredLicense(payload.license);\n const cliChannel = parseCodexCliChannel(payload.cliChannel ?? payload.cli_channel);\n\n await patchAppState({\n app: {\n lastProfileName: asString(payload.lastProfileName ?? payload.last_profile_name),\n lastAppVersion: asString(payload.lastAppVersion ?? payload.last_app_version),\n pendingUpdateVersion: asString(payload.pendingUpdateVersion ?? payload.pending_update_version),\n cliChannel: cliChannel ?? undefined,\n },\n autoRoll: autoRoll\n ? {\n enabled: autoRoll.enabled,\n warningThreshold: autoRoll.warningThreshold,\n switchThreshold: autoRoll.switchThreshold,\n }\n : undefined,\n license: license\n ? {\n licenseKey: license.licenseKey ?? null,\n purchaseEmail: license.purchaseEmail ?? null,\n lastVerifiedAt: license.lastVerifiedAt ?? null,\n nextCheckAt: license.nextCheckAt ?? null,\n lastVerificationError: license.lastVerificationError ?? null,\n status: license.status ?? \"inactive\",\n signature: license.signature ?? null,\n }\n : undefined,\n providers: {\n list: Array.isArray(payload.providers)\n ? (payload.providers as AppState[\"providers\"][\"list\"])\n : undefined,\n selectedProviderId: asString(payload.selectedProviderId) ?? undefined,\n defaultModel: asString(payload.defaultModel ?? payload.selectedModel),\n defaultReasoningEffort:\n typeof payload.defaultReasoningEffort === \"string\"\n ? (payload.defaultReasoningEffort as AppState[\"providers\"][\"defaultReasoningEffort\"])\n : typeof payload.reasoningEffort === \"string\"\n ? (payload.reasoningEffort as AppState[\"providers\"][\"defaultReasoningEffort\"])\n : undefined,\n overridesByPath:\n isRecord(payload.selectionsByCwd) ? (payload.selectionsByCwd as AppState[\"providers\"][\"overridesByPath\"]) : undefined,\n },\n sandbox: {\n defaultMode: asString(payload.mode ?? payload.defaultMode) ?? undefined,\n defaultApprovalPolicy:\n asString(payload.approvalPolicy ?? payload.defaultApprovalPolicy) ?? undefined,\n overridesByPath:\n isRecord(payload.sandboxSelectionsByCwd)\n ? (payload.sandboxSelectionsByCwd as AppState[\"sandbox\"][\"overridesByPath\"])\n : undefined,\n },\n preferences: {\n excludeFolders: Array.isArray(payload.excludeFolders)\n ? (payload.excludeFolders as string[])\n : undefined,\n enableTaskCompleteBeep:\n typeof payload.enableTaskCompleteBeep === \"boolean\"\n ? payload.enableTaskCompleteBeep\n : undefined,\n preventSleepDuringTasks:\n typeof payload.preventSleepDuringTasks === \"boolean\"\n ? payload.preventSleepDuringTasks\n : undefined,\n folderHistory: Array.isArray(payload.folderHistory)\n ? (payload.folderHistory as AppState[\"preferences\"][\"folderHistory\"])\n : undefined,\n pinnedPaths: Array.isArray(payload.pinnedPaths)\n ? (payload.pinnedPaths as string[])\n : undefined,\n },\n projectSettingsByPath: isRecord(payload.projectSettingsByPath)\n ? (payload.projectSettingsByPath as AppState[\"projectSettingsByPath\"])\n : undefined,\n conversationCategoriesByCwd: isRecord(payload.categoriesByCwd)\n ? (payload.categoriesByCwd as AppState[\"conversationCategoriesByCwd\"])\n : undefined,\n conversationCategoryAssignmentsByCwd: isRecord(payload.conversationCategoryByCwd)\n ? (payload.conversationCategoryByCwd as AppState[\"conversationCategoryAssignmentsByCwd\"])\n : undefined,\n sync: isRecord(payload.sync) ? (payload.sync as AppState[\"sync\"]) : undefined,\n });\n}\n\nexport type { StoredLicense, LicenseStatusCode };\nexport type { AutoRollSettings } from \"./auto-roll-settings\";\nexport type { CodexCliChannel } from \"./codex-cli-channel\";\n","const isTestEnv =\n process.env.NODE_ENV === 'test' ||\n process.env.VITEST === 'true' ||\n process.env.VITEST === '1';\n\nfunction isMocked(fn: unknown): boolean {\n return Boolean(fn && typeof fn === 'function' && 'mock' in (fn as Record<string, unknown>));\n}\n\nexport function logWarn(...args: Parameters<typeof console.warn>): void {\n if (isTestEnv && !isMocked(console.warn)) {\n return;\n }\n console.warn(...args);\n}\n\nexport function logError(...args: Parameters<typeof console.error>): void {\n if (isTestEnv && !isMocked(console.error)) {\n return;\n }\n console.error(...args);\n}\n\nexport function logInfo(...args: Parameters<typeof console.warn>): void {\n if (isTestEnv && !isMocked(console.warn)) {\n return;\n }\n console.warn(...args);\n}\n","import crypto from 'node:crypto';\nimport { persistLicense, getStoredLicense, type StoredLicense } from './codex-settings';\nimport { getLicenseSecret } from './license-secret';\n\nconst PRODUCT_PERMALINK = 'codex-use';\nconst PRODUCT_ID = '3_CcyVEXt2FOMiEpPx8xzw==';\nconst BASE_PRICE = 19;\nconst BASE_PRICE_DISPLAY = '$19';\nconst PROMO_CODE = 'NOELNEWYEAR50';\nconst PROMO_PERCENT = 50;\nconst PROMO_ACTIVE = true;\nconst PROMO_PRICE_DISPLAY = `$${(BASE_PRICE * (100 - PROMO_PERCENT) / 100).toFixed(2)}`;\nconst PRODUCT_URL = PROMO_ACTIVE\n ? `https://hweihwang.gumroad.com/l/${PRODUCT_PERMALINK}/${PROMO_CODE}`\n : 'https://hweihwang.gumroad.com/l/codex-use';\nconst LICENSE_PRICE_DISPLAY = PROMO_ACTIVE ? PROMO_PRICE_DISPLAY : BASE_PRICE_DISPLAY;\nconst LICENSE_MAX_USES = 5;\nconst FREE_PROFILE_LIMIT = 2;\nconst LICENSE_REFRESH_INTERVAL_MS = 5 * 60 * 1000;\nconst MAX_NEXT_CHECK_MS = 60 * 60 * 1000;\nconst GRACE_MAX_AGE_MS = 3 * 60 * 60 * 1000;\n\ntype LicenseTier = 'free' | 'pro';\ntype LicenseState = 'inactive' | 'active' | 'grace' | 'verifying' | 'error';\n\ninterface LicenseStatus {\n tier: LicenseTier;\n state: LicenseState;\n isPro: boolean;\n profileLimit: number | null;\n profilesRemaining: number | null;\n purchaseEmail: string | null;\n maskedLicenseKey: string | null;\n lastVerifiedAt: string | null;\n nextCheckAt: string | null;\n message: string | null;\n error: string | null;\n productPermalink: string;\n productUrl: string;\n productId: string;\n priceDisplay: string;\n basePriceDisplay: string;\n promoCode: string | null;\n promoPercent: number | null;\n}\n\ninterface GumroadLicenseResponse {\n success: boolean;\n message?: string;\n uses?: number;\n purchase?: {\n id: string;\n email: string;\n product_id?: string;\n product_permalink?: string;\n refunded?: boolean;\n chargebacked?: boolean;\n subscription_cancelled?: boolean;\n subscription_failed?: boolean;\n license_disabled?: boolean;\n variants?: string;\n };\n license_disabled?: boolean;\n}\n\ntype VerificationMode = 'activation' | 'refresh';\n\nclass LicenseError extends Error {\n readonly code: 'network' | 'invalid' | 'revoked';\n\n constructor(message: string, code: 'network' | 'invalid' | 'revoked') {\n super(message);\n this.name = 'LicenseError';\n this.code = code;\n }\n}\n\nfunction nowIso(): string {\n return new Date().toISOString();\n}\n\nfunction maskLicenseKey(key: string | null | undefined): string | null {\n if (!key || typeof key !== 'string') {\n return null;\n }\n\n const trimmed = key.trim();\n if (trimmed.length <= 4) {\n return '••••';\n }\n\n const visible = trimmed.slice(-4);\n return `••••${visible}`;\n}\n\nfunction resolveProfilesRemaining(limit: number | null, currentProfiles: number): number | null {\n if (typeof limit !== 'number') {\n return null;\n }\n return Math.max(0, limit - currentProfiles);\n}\n\nfunction determineTierFromStored(stored: StoredLicense | null): LicenseTier {\n if (!stored || !stored.licenseKey) {\n return 'free';\n }\n\n if (stored.status === 'inactive') {\n return 'free';\n }\n\n return 'pro';\n}\n\nfunction licenseSignaturePayload(license: StoredLicense): string {\n const payload = {\n licenseKey: license.licenseKey ?? null,\n purchaseEmail: license.purchaseEmail ?? null,\n lastVerifiedAt: license.lastVerifiedAt ?? null,\n nextCheckAt: license.nextCheckAt ?? null,\n lastVerificationError: license.lastVerificationError ?? null,\n status: license.status ?? null,\n };\n return JSON.stringify(payload);\n}\n\nfunction signLicense(license: StoredLicense, secret: string): string {\n return crypto.createHmac('sha256', secret).update(licenseSignaturePayload(license)).digest('hex');\n}\n\nfunction withSignature(license: StoredLicense, secret: string): StoredLicense {\n return {\n ...license,\n signature: signLicense(license, secret),\n };\n}\n\nfunction isSignatureValid(license: StoredLicense, secret: string): boolean {\n if (!license.signature) {\n return false;\n }\n const expected = signLicense(license, secret);\n return expected === license.signature;\n}\n\nasync function requestGumroadVerify(licenseKey: string, mode: VerificationMode): Promise<GumroadLicenseResponse> {\n const controller = new AbortController();\n const timeout = setTimeout(() => controller.abort(), 10_000);\n\n try {\n const body = new URLSearchParams({\n product_permalink: PRODUCT_PERMALINK,\n product_id: PRODUCT_ID,\n license_key: licenseKey,\n increment_uses_count: mode === 'activation' ? 'true' : 'false',\n });\n\n const response = await fetch('https://api.gumroad.com/v2/licenses/verify', {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded',\n },\n body,\n signal: controller.signal,\n });\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => null);\n const message = errorText\n ? `License verification failed (${response.status}): ${errorText}`\n : `License verification failed (${response.status}).`;\n throw new LicenseError(message, 'network');\n }\n\n const json = (await response.json()) as GumroadLicenseResponse;\n return json;\n } catch (error) {\n if (error instanceof LicenseError) {\n throw error;\n }\n\n if (error instanceof Error && error.name === 'AbortError') {\n throw new LicenseError('License verification timed out.', 'network');\n }\n\n throw new LicenseError(\n error instanceof Error ? error.message : 'Unable to reach Gumroad for license verification.',\n 'network',\n );\n } finally {\n clearTimeout(timeout);\n }\n}\n\nfunction normalizeVerificationResult(response: GumroadLicenseResponse): { email: string | null } {\n if (!response.success) {\n const message = response.message ?? 'License key is invalid.';\n throw new LicenseError(message, 'invalid');\n }\n\n if (!response.purchase) {\n throw new LicenseError('License verification response was incomplete.', 'network');\n }\n\n if (response.purchase.chargebacked || response.purchase.refunded || response.license_disabled) {\n throw new LicenseError('This license has been revoked or refunded.', 'revoked');\n }\n\n if (response.purchase.subscription_cancelled || response.purchase.subscription_failed) {\n throw new LicenseError('This license subscription is no longer active.', 'revoked');\n }\n\n const email = response.purchase.email ?? null;\n return { email };\n}\n\nfunction ensureWithinUseLimit(response: GumroadLicenseResponse): void {\n if (!response.success) {\n return;\n }\n\n if (typeof response.uses !== 'number') {\n return;\n }\n\n if (response.uses > LICENSE_MAX_USES) {\n throw new LicenseError(\n `This license has reached the activation limit (${LICENSE_MAX_USES}). Contact support to move it to another device.`,\n 'invalid',\n );\n }\n}\n\nfunction toLicenseStatus(stored: StoredLicense | null, overrides: Partial<LicenseStatus> = {}): LicenseStatus {\n const tier = determineTierFromStored(stored);\n const isPro = tier === 'pro';\n\n const state: LicenseState =\n stored && stored.status\n ? stored.status\n : isPro\n ? 'active'\n : 'inactive';\n\n const base: LicenseStatus = {\n tier,\n state,\n isPro,\n profileLimit: isPro ? null : FREE_PROFILE_LIMIT,\n profilesRemaining: null,\n purchaseEmail: stored?.purchaseEmail ?? null,\n maskedLicenseKey: maskLicenseKey(stored?.licenseKey),\n lastVerifiedAt: stored?.lastVerifiedAt ?? null,\n nextCheckAt: stored?.nextCheckAt ?? null,\n message: null,\n error: stored?.lastVerificationError ?? null,\n productUrl: PRODUCT_URL,\n productId: PRODUCT_ID,\n productPermalink: PRODUCT_PERMALINK,\n priceDisplay: LICENSE_PRICE_DISPLAY,\n basePriceDisplay: BASE_PRICE_DISPLAY,\n promoCode: PROMO_ACTIVE ? PROMO_CODE : null,\n promoPercent: PROMO_ACTIVE ? PROMO_PERCENT : null,\n };\n\n return { ...base, ...overrides };\n}\n\nfunction parseTimestamp(value: string | null | undefined): number | null {\n if (typeof value !== 'string' || value.trim() === '') {\n return null;\n }\n\n const parsed = Date.parse(value);\n return Number.isNaN(parsed) ? null : parsed;\n}\n\nclass LicenseService {\n private cache: LicenseStatus | null = null;\n private refreshPromise: Promise<void> | null = null;\n private verificationPromise: Promise<LicenseStatus> | null = null;\n\n async getCachedStatus(): Promise<LicenseStatus> {\n return this.getStatus();\n }\n\n async getStatus(options: { forceRefresh?: boolean } = {}): Promise<LicenseStatus> {\n const forceRefresh = Boolean(options.forceRefresh);\n const secret = await getLicenseSecret();\n const stored = await getStoredLicense();\n\n if (!stored?.licenseKey) {\n const status = toLicenseStatus(null);\n this.cache = status;\n return status;\n }\n\n const now = Date.now();\n const nextCheckTs = parseTimestamp(stored.nextCheckAt);\n const lastVerifiedTs = parseTimestamp(stored.lastVerifiedAt);\n const graceStale =\n (stored.status === 'grace' || stored.status === 'error') &&\n (lastVerifiedTs === null || lastVerifiedTs + GRACE_MAX_AGE_MS < now);\n\n let workingStored: StoredLicense = { ...stored };\n const signatureValid = isSignatureValid(workingStored, secret);\n\n let cappedNextCheckTs = nextCheckTs;\n\n if (nextCheckTs !== null && nextCheckTs > now + MAX_NEXT_CHECK_MS) {\n cappedNextCheckTs = now;\n const updated = {\n ...workingStored,\n nextCheckAt: new Date(cappedNextCheckTs).toISOString(),\n };\n const signed = withSignature(updated, secret);\n workingStored = signed;\n await persistLicense(signed);\n }\n\n const tampered = Boolean(workingStored.licenseKey) && !signatureValid;\n\n const cached = tampered\n ? toLicenseStatus(null, {\n state: 'verifying',\n message: 'License data changed, rechecking.',\n error: 'Untrusted license data.',\n })\n : toLicenseStatus(workingStored);\n this.cache = cached;\n\n const shouldRefresh =\n forceRefresh || graceStale || tampered || cappedNextCheckTs === null || cappedNextCheckTs <= now;\n\n if (!shouldRefresh) {\n return cached;\n }\n\n if (this.verificationPromise && !forceRefresh) {\n return this.verificationPromise;\n }\n\n const verify = async (): Promise<LicenseStatus> => {\n try {\n const result = await requestGumroadVerify(workingStored.licenseKey!, 'refresh');\n ensureWithinUseLimit(result);\n const normalized = normalizeVerificationResult(result);\n const updated: StoredLicense = {\n licenseKey: workingStored.licenseKey,\n purchaseEmail: normalized.email,\n lastVerifiedAt: nowIso(),\n nextCheckAt: new Date(Date.now() + LICENSE_REFRESH_INTERVAL_MS).toISOString(),\n status: 'active',\n lastVerificationError: null,\n };\n\n const signed = withSignature(updated, secret);\n\n await persistLicense(signed);\n const status = toLicenseStatus(signed);\n this.cache = status;\n return status;\n } catch (error) {\n const message = error instanceof Error ? error.message : 'License verification failed.';\n\n if (error instanceof LicenseError && error.code === 'network') {\n const fallbackStored: StoredLicense = withSignature(\n {\n ...workingStored,\n status: tampered ? 'inactive' : workingStored.status === 'inactive' ? 'inactive' : 'grace',\n lastVerificationError: message,\n nextCheckAt: new Date(Date.now() + LICENSE_REFRESH_INTERVAL_MS).toISOString(),\n },\n secret,\n );\n await persistLicense(fallbackStored);\n const fallback = toLicenseStatus(fallbackStored, {\n state: fallbackStored.status ?? 'grace',\n error: message,\n });\n this.cache = fallback;\n return fallback;\n }\n\n const fallbackStored: StoredLicense = withSignature({\n licenseKey: workingStored.licenseKey,\n purchaseEmail: workingStored.purchaseEmail ?? null,\n lastVerifiedAt: workingStored.lastVerifiedAt ?? null,\n nextCheckAt: null,\n status: 'inactive',\n lastVerificationError: message,\n }, secret);\n await persistLicense(fallbackStored);\n const fallback = toLicenseStatus(fallbackStored, { error: message });\n this.cache = fallback;\n return fallback;\n }\n };\n\n this.verificationPromise = verify();\n try {\n return await this.verificationPromise;\n } finally {\n this.verificationPromise = null;\n }\n }\n\n async activate(licenseKey: string): Promise<LicenseStatus> {\n const trimmed = licenseKey.trim();\n if (!trimmed) {\n throw new Error('License key is required.');\n }\n\n const secret = await getLicenseSecret();\n const digest = crypto.createHash('sha256').update(trimmed).digest('hex');\n\n const result = await requestGumroadVerify(trimmed, 'activation');\n ensureWithinUseLimit(result);\n const normalized = normalizeVerificationResult(result);\n const stored: StoredLicense = {\n licenseKey: trimmed,\n purchaseEmail: normalized.email,\n lastVerifiedAt: nowIso(),\n nextCheckAt: new Date(Date.now() + LICENSE_REFRESH_INTERVAL_MS).toISOString(),\n status: 'active',\n lastVerificationError: null,\n };\n\n const signed = withSignature(stored, secret);\n\n await persistLicense(signed);\n const status = toLicenseStatus(signed, {\n message: `License verified (${digest.slice(0, 8)}).`,\n });\n this.cache = status;\n return status;\n }\n\n applyProfileCount(status: LicenseStatus, profileCount: number): LicenseStatus {\n const profilesRemaining = resolveProfilesRemaining(status.profileLimit, profileCount);\n return {\n ...status,\n profilesRemaining,\n };\n }\n\n refreshStatusInBackground(options: { force?: boolean } = {}): void {\n if (this.refreshPromise) {\n return;\n }\n\n const forceRefresh = Boolean(options.force);\n this.refreshPromise = (async () => {\n try {\n await this.getStatus({ forceRefresh });\n } catch (error) {\n console.warn('Background license refresh failed:', error);\n } finally {\n this.refreshPromise = null;\n }\n })();\n }\n}\n\nexport const licenseService = new LicenseService();\nexport type { LicenseStatus, LicenseTier, LicenseState };\nexport { FREE_PROFILE_LIMIT, LICENSE_PRICE_DISPLAY, PRODUCT_PERMALINK, PRODUCT_URL, PRODUCT_ID };\n\nfunction isExplicitInvalidation(message: string | null | undefined): boolean {\n if (!message) {\n return false;\n }\n const normalized = message.toLowerCase();\n return (\n normalized.includes('revoked') ||\n normalized.includes('refunded') ||\n normalized.includes('invalid') ||\n normalized.includes('no longer active')\n );\n}\n\n/**\n * Determines when it is safe to enforce profile limits (e.g., pruning).\n * We skip enforcement when the license state is uncertain (grace/error/verifying)\n * and only enforce limits for true free/unlicensed states or explicit revocations.\n */\nexport function shouldEnforceProfileLimit(status: LicenseStatus): boolean {\n if (status.isPro) {\n return false;\n }\n\n if (status.state === 'grace' || status.state === 'verifying' || status.state === 'error') {\n return false;\n }\n\n if (isExplicitInvalidation(status.error)) {\n return true;\n }\n\n return status.state === 'inactive';\n}\n","import { promises as fs } from 'node:fs';\nimport path from 'node:path';\nimport crypto from 'node:crypto';\nimport { getUserDataDir } from './app-state';\n\nconst SECRET_FILE = 'license.secret';\n\nfunction resolveSecretPath(): string {\n return path.join(getUserDataDir(), SECRET_FILE);\n}\n\nasync function generateSecret(): Promise<string> {\n return crypto.randomBytes(32).toString('hex');\n}\n\nasync function readSecretIfExists(secretPath: string): Promise<string | null> {\n try {\n const existing = await fs.readFile(secretPath, 'utf8');\n const trimmed = existing.trim();\n if (trimmed.length > 0) {\n return trimmed;\n }\n return null;\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return null;\n }\n throw error;\n }\n}\n\nasync function writeSecret(secretPath: string, secret: string): Promise<void> {\n await fs.mkdir(path.dirname(secretPath), { recursive: true });\n await fs.writeFile(secretPath, `${secret}\\n`, { mode: 0o600 });\n await fs.chmod(secretPath, 0o600).catch(() => undefined);\n}\n\nexport async function getLicenseSecret(): Promise<string> {\n const secretPath = resolveSecretPath();\n const existing = await readSecretIfExists(secretPath);\n if (existing) {\n return existing;\n }\n\n const secret = await generateSecret();\n await writeSecret(secretPath, secret);\n return secret;\n}\n","import { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport {\n CLOUD_SYNC_SCHEMA_VERSION,\n type CloudSyncProfileRecord,\n type CloudSyncSnapshot,\n} from \"./cloud-sync-types\";\nimport {\n CloudSyncClientError,\n fetchRemoteSnapshotMeta,\n fetchRemoteSnapshot,\n getCloudSyncApiBaseUrl,\n pushRemoteSnapshot,\n} from \"./cloud-sync-client\";\nimport { ProfileManager } from \"./profile-manager\";\nimport { getCodexConfigSnapshot, saveCodexConfigContent } from \"./codex-config\";\nimport {\n getStoredLicense,\n readCodexSettingsJsonRaw,\n writeCodexSettingsJsonRaw,\n} from \"./codex-settings\";\nimport { getAppState, getUserDataDir, patchAppState } from \"./app-state\";\nimport { licenseService } from \"./license-service\";\nimport { logInfo, logWarn } from \"./logger\";\nimport { formatUserFacingError } from \"./errors\";\nimport type { CloudSyncRunResultPayload, CloudSyncStatusPayload } from \"./ipc-types\";\nimport { isRecord } from \"./type-guards\";\n\nconst SYNC_BACKUP_DIR = \"sync-backups\";\nconst PRE_PULL_BACKUP_PREFIX = \"pre-pull-\";\nconst PRE_PULL_BACKUP_SUFFIX = \".json\";\nconst PRE_PULL_BACKUP_KEEP_COUNT = 3;\nconst SYNC_SIZE_WARN_BYTES = 1 * 1024 * 1024;\nconst SYNC_SIZE_MAX_BYTES = 5 * 1024 * 1024;\nconst MB_DIVISOR = 1024 * 1024;\n\ninterface CloudSyncStateFile {\n lastPushAt?: string;\n lastPullAt?: string;\n lastError?: string;\n remoteUpdatedAt?: string;\n}\n\ntype Eligibility = {\n canSync: boolean;\n reason: string | null;\n licenseKey: string | null;\n licenseState: string | null;\n};\n\ntype BuildLocalSnapshotOptions = {\n enforcePushGuards?: boolean;\n};\n\nfunction mapProfilesFromAppState(raw: unknown): CloudSyncProfileRecord[] {\n if (!isRecord(raw)) {\n return [];\n }\n\n const profiles: CloudSyncProfileRecord[] = [];\n\n for (const [name, value] of Object.entries(raw)) {\n if (!isRecord(value)) {\n continue;\n }\n\n const data = isRecord(value.data) ? value.data : null;\n if (!data) {\n continue;\n }\n\n const normalizedName = typeof name === \"string\" ? name.trim() : \"\";\n if (!normalizedName) {\n continue;\n }\n\n profiles.push({\n name: normalizedName,\n displayName: typeof value.displayName === \"string\" ? value.displayName : null,\n data,\n metadata: isRecord(value.metadata) ? value.metadata : undefined,\n accountId: typeof value.accountId === \"string\" ? value.accountId : null,\n workspaceId: typeof value.workspaceId === \"string\" ? value.workspaceId : null,\n workspaceName: typeof value.workspaceName === \"string\" ? value.workspaceName : null,\n email: typeof value.email === \"string\" ? value.email : null,\n authMethod: typeof value.authMethod === \"string\" ? value.authMethod : null,\n createdAt: typeof value.createdAt === \"string\" ? value.createdAt : null,\n updatedAt: typeof value.updatedAt === \"string\" ? value.updatedAt : null,\n });\n }\n\n return profiles;\n}\n\nfunction summarizeSnapshot(snapshot: CloudSyncSnapshot): {\n profiles: number;\n configBytes: number;\n settingsKeys: number;\n} {\n const configBytes =\n typeof snapshot.configTomlContent === \"string\" ? snapshot.configTomlContent.length : 0;\n const settingsKeys =\n isRecord(snapshot.settingsJson) ? Object.keys(snapshot.settingsJson).length : 0;\n\n return {\n profiles: snapshot.profiles.length,\n configBytes,\n settingsKeys,\n };\n}\n\nasync function readState(): Promise<CloudSyncStateFile> {\n const state = await getAppState();\n return {\n lastPushAt: state.sync.lastPushAt ?? undefined,\n lastPullAt: state.sync.lastPullAt ?? undefined,\n lastError: state.sync.lastError ?? undefined,\n remoteUpdatedAt: state.sync.remoteUpdatedAt ?? undefined,\n };\n}\n\nasync function writeState(patch: Partial<CloudSyncStateFile>): Promise<void> {\n await patchAppState({\n sync: {\n ...(typeof patch.lastPushAt === \"string\" ? { lastPushAt: patch.lastPushAt } : {}),\n ...(typeof patch.lastPullAt === \"string\" ? { lastPullAt: patch.lastPullAt } : {}),\n ...(typeof patch.lastError === \"string\" ? { lastError: patch.lastError } : patch.lastError === undefined ? { lastError: null } : {}),\n ...(typeof patch.remoteUpdatedAt === \"string\"\n ? { remoteUpdatedAt: patch.remoteUpdatedAt }\n : {}),\n },\n });\n}\n\nasync function resolveEligibility(): Promise<Eligibility> {\n const stored = await getStoredLicense();\n const licenseKey = typeof stored?.licenseKey === \"string\" ? stored.licenseKey.trim() : \"\";\n\n if (!licenseKey) {\n return {\n canSync: false,\n reason: \"License key is required for cloud sync.\",\n licenseKey: null,\n licenseState: null,\n };\n }\n\n const status = await licenseService.getStatus();\n const active = status.state === \"active\" || status.state === \"grace\";\n if (!active) {\n return {\n canSync: false,\n reason: \"Cloud sync requires an active Pro license.\",\n licenseKey,\n licenseState: status.state,\n };\n }\n\n return {\n canSync: true,\n reason: null,\n licenseKey,\n licenseState: status.state,\n };\n}\n\nfunction formatMegabytes(bytes: number): string {\n return (bytes / MB_DIVISOR).toFixed(2);\n}\n\nfunction resolveSyncBackupsDir(): string {\n return path.join(getUserDataDir(), SYNC_BACKUP_DIR);\n}\n\nfunction toSafeIsoForFileName(iso: string): string {\n return iso.replace(/:/g, \"-\");\n}\n\nasync function pruneOldPrePullBackups(backupsDir: string): Promise<void> {\n const entries = await fs.readdir(backupsDir, { withFileTypes: true });\n const files = entries\n .filter(\n (entry) =>\n entry.isFile() &&\n entry.name.startsWith(PRE_PULL_BACKUP_PREFIX) &&\n entry.name.endsWith(PRE_PULL_BACKUP_SUFFIX),\n )\n .map((entry) => entry.name)\n .sort((a, b) => b.localeCompare(a));\n\n if (files.length <= PRE_PULL_BACKUP_KEEP_COUNT) {\n return;\n }\n\n for (const stale of files.slice(PRE_PULL_BACKUP_KEEP_COUNT)) {\n await fs.rm(path.join(backupsDir, stale), { force: true });\n }\n}\n\nasync function createPrePullBackup(profileManager: ProfileManager): Promise<string> {\n const snapshot = await buildLocalSnapshot(profileManager, {\n enforcePushGuards: false,\n });\n const backupsDir = resolveSyncBackupsDir();\n await fs.mkdir(backupsDir, { recursive: true });\n\n const timestamp = toSafeIsoForFileName(new Date().toISOString());\n const backupPath = path.join(\n backupsDir,\n `${PRE_PULL_BACKUP_PREFIX}${timestamp}${PRE_PULL_BACKUP_SUFFIX}`,\n );\n await fs.writeFile(backupPath, `${JSON.stringify(snapshot, null, 2)}\\n`, \"utf8\");\n await pruneOldPrePullBackups(backupsDir);\n\n logInfo(\"[cloud-sync] created pre-pull backup\", { backupPath });\n return backupPath;\n}\n\nasync function buildLocalSnapshot(\n profileManager: ProfileManager,\n options: BuildLocalSnapshotOptions = {},\n): Promise<CloudSyncSnapshot> {\n const enforcePushGuards = options.enforcePushGuards !== false;\n const profilesFromManager = await profileManager.exportProfilesForCloudSync();\n const state = await getAppState();\n const profilesFromState = mapProfilesFromAppState(state.profilesByName);\n const profiles = profilesFromManager.length > 0 ? profilesFromManager : profilesFromState;\n const config = await getCodexConfigSnapshot();\n const settingsJson = await readCodexSettingsJsonRaw();\n\n const hasProfiles = profiles.length > 0;\n const hasConfig = typeof config.content === \"string\" && config.content.trim().length > 0;\n const hasSettings = isRecord(settingsJson) && Object.keys(settingsJson).length > 0;\n\n if (enforcePushGuards && !hasProfiles && !hasConfig && !hasSettings) {\n throw new Error(\"Refusing to push an empty cloud sync snapshot.\");\n }\n\n const snapshot: CloudSyncSnapshot = {\n schemaVersion: CLOUD_SYNC_SCHEMA_VERSION,\n updatedAt: new Date().toISOString(),\n profiles,\n configTomlContent: config.exists ? config.content : null,\n settingsJson,\n };\n\n if (!enforcePushGuards) {\n return snapshot;\n }\n\n return snapshot;\n}\n\nasync function applyRemoteSnapshot(profileManager: ProfileManager, snapshot: CloudSyncSnapshot): Promise<void> {\n await profileManager.replaceProfilesFromCloudSync(snapshot.profiles ?? []);\n\n if (typeof snapshot.configTomlContent === \"string\") {\n await saveCodexConfigContent(snapshot.configTomlContent);\n }\n\n if (snapshot.settingsJson && isRecord(snapshot.settingsJson)) {\n await writeCodexSettingsJsonRaw(snapshot.settingsJson);\n } else {\n await writeCodexSettingsJsonRaw({});\n }\n}\n\nfunction toRunError(mode: \"push\" | \"pull\", error: unknown): CloudSyncRunResultPayload {\n const message = formatUserFacingError(error, {\n fallback: `Cloud sync ${mode} failed.`,\n });\n\n return {\n mode,\n status: \"error\",\n message,\n localUpdatedAt: null,\n remoteUpdatedAt: null,\n };\n}\n\nexport async function getCloudSyncStatus(): Promise<CloudSyncStatusPayload> {\n const eligibility = await resolveEligibility();\n const state = await readState();\n\n let remoteUpdatedAt: string | null = state.remoteUpdatedAt ?? null;\n if (eligibility.canSync && eligibility.licenseKey) {\n try {\n remoteUpdatedAt = await fetchRemoteSnapshotMeta(eligibility.licenseKey);\n } catch (error) {\n if (error instanceof CloudSyncClientError && error.status === 404) {\n try {\n const remote = await fetchRemoteSnapshot(eligibility.licenseKey);\n remoteUpdatedAt = remote?.updatedAt ?? null;\n } catch {\n // Keep status lightweight when remote endpoint is unavailable.\n }\n }\n // Keep status lightweight when remote endpoint is unavailable.\n }\n }\n\n return {\n endpoint: getCloudSyncApiBaseUrl(),\n canSync: eligibility.canSync,\n reason: eligibility.reason,\n licenseState: eligibility.licenseState,\n hasLicenseKey: Boolean(eligibility.licenseKey),\n lastPushAt: state.lastPushAt ?? null,\n lastPullAt: state.lastPullAt ?? null,\n lastError: state.lastError ?? null,\n remoteUpdatedAt,\n };\n}\n\nexport async function pushCloudSync(): Promise<CloudSyncRunResultPayload> {\n const eligibility = await resolveEligibility();\n if (!eligibility.canSync || !eligibility.licenseKey) {\n return {\n mode: \"push\",\n status: \"skipped\",\n message: eligibility.reason ?? \"Cloud sync unavailable.\",\n localUpdatedAt: null,\n remoteUpdatedAt: null,\n };\n }\n\n const profileManager = new ProfileManager();\n\n try {\n await profileManager.initialize();\n const localSnapshot = await buildLocalSnapshot(profileManager, { enforcePushGuards: true });\n const serializedSnapshot = JSON.stringify(localSnapshot);\n const snapshotBytes = Buffer.byteLength(serializedSnapshot, \"utf8\");\n if (snapshotBytes > SYNC_SIZE_WARN_BYTES) {\n logWarn(\"[cloud-sync] push snapshot is large\", {\n bytes: snapshotBytes,\n megabytes: formatMegabytes(snapshotBytes),\n });\n }\n if (snapshotBytes > SYNC_SIZE_MAX_BYTES) {\n throw new Error(\n `Snapshot too large to sync (${formatMegabytes(snapshotBytes)} MB). Reduce profiles or config size.`,\n );\n }\n if (process.env.NODE_ENV !== \"production\") {\n logInfo(\"[cloud-sync] push snapshot summary\", summarizeSnapshot(localSnapshot));\n }\n const remote = await pushRemoteSnapshot(eligibility.licenseKey, localSnapshot, {\n serializedSnapshot,\n });\n\n await writeState({\n lastPushAt: new Date().toISOString(),\n remoteUpdatedAt: remote.snapshot.updatedAt,\n lastError: undefined,\n });\n\n if (remote.status === \"stale\") {\n return {\n mode: \"push\",\n status: \"stale\",\n message: \"Remote cloud snapshot is newer. Pull first.\",\n localUpdatedAt: localSnapshot.updatedAt,\n remoteUpdatedAt: remote.snapshot.updatedAt,\n };\n }\n\n return {\n mode: \"push\",\n status: \"applied\",\n message: \"Cloud sync push completed.\",\n localUpdatedAt: localSnapshot.updatedAt,\n remoteUpdatedAt: remote.snapshot.updatedAt,\n };\n } catch (error) {\n const result = toRunError(\"push\", error);\n const lastError = error instanceof CloudSyncClientError ? error.message : result.message;\n await writeState({ lastError });\n return result;\n }\n}\n\nexport async function pullCloudSync(): Promise<CloudSyncRunResultPayload> {\n const eligibility = await resolveEligibility();\n if (!eligibility.canSync || !eligibility.licenseKey) {\n return {\n mode: \"pull\",\n status: \"skipped\",\n message: eligibility.reason ?? \"Cloud sync unavailable.\",\n localUpdatedAt: null,\n remoteUpdatedAt: null,\n };\n }\n\n const profileManager = new ProfileManager();\n\n try {\n await profileManager.initialize();\n const remote = await fetchRemoteSnapshot(eligibility.licenseKey);\n\n if (!remote) {\n return {\n mode: \"pull\",\n status: \"skipped\",\n message: \"No cloud snapshot found.\",\n localUpdatedAt: null,\n remoteUpdatedAt: null,\n };\n }\n\n let backupWarningSuffix = \"\";\n try {\n await createPrePullBackup(profileManager);\n } catch (backupError) {\n logWarn(\"[cloud-sync] pre-pull backup failed; continuing pull\", backupError);\n const reason = formatUserFacingError(backupError, { fallback: \"unknown error\" });\n backupWarningSuffix = ` Warning: pre-pull backup failed (${reason}).`;\n }\n await applyRemoteSnapshot(profileManager, remote);\n await writeState({\n lastPullAt: new Date().toISOString(),\n remoteUpdatedAt: remote.updatedAt,\n lastError: undefined,\n });\n\n return {\n mode: \"pull\",\n status: \"applied\",\n message: `Cloud sync pull completed.${backupWarningSuffix}`,\n localUpdatedAt: remote.updatedAt,\n remoteUpdatedAt: remote.updatedAt,\n };\n } catch (error) {\n const result = toRunError(\"pull\", error);\n const lastError = error instanceof CloudSyncClientError ? error.message : result.message;\n await writeState({ lastError });\n return result;\n }\n}\n","import type { ProfileData, ProfileMetadata } from \"./types\";\n\nexport const CLOUD_SYNC_SCHEMA_VERSION = 1 as const;\n\nexport interface CloudSyncProfileRecord {\n name: string;\n displayName: string | null;\n data: ProfileData;\n metadata?: ProfileMetadata;\n accountId: string | null;\n workspaceId: string | null;\n workspaceName: string | null;\n email: string | null;\n authMethod: string | null;\n createdAt: string | null;\n updatedAt: string | null;\n}\n\nexport interface CloudSyncSnapshot {\n schemaVersion: typeof CLOUD_SYNC_SCHEMA_VERSION;\n updatedAt: string;\n profiles: CloudSyncProfileRecord[];\n configTomlContent: string | null;\n settingsJson: Record<string, unknown> | null;\n}\n\nexport interface CloudSyncGetResponse {\n snapshot: CloudSyncSnapshot | null;\n}\n\nexport interface CloudSyncMetaResponse {\n updatedAt: string | null;\n}\n\nexport type CloudSyncPutStatus = \"applied\" | \"stale\";\n\nexport interface CloudSyncPutResponse {\n status: CloudSyncPutStatus;\n snapshot: CloudSyncSnapshot;\n}\n","export function isRecord(value: unknown): value is Record<string, unknown> {\n return Boolean(value) && typeof value === \"object\" && !Array.isArray(value);\n}\n\nexport function toIsoOrNull(value: unknown): string | null {\n if (typeof value !== \"string\") {\n return null;\n }\n\n const parsed = Date.parse(value);\n return Number.isNaN(parsed) ? null : new Date(parsed).toISOString();\n}\n","import {\n CLOUD_SYNC_SCHEMA_VERSION,\n type CloudSyncGetResponse,\n type CloudSyncMetaResponse,\n type CloudSyncPutResponse,\n type CloudSyncSnapshot,\n} from \"./cloud-sync-types\";\nimport { isRecord, toIsoOrNull } from \"./type-guards\";\n\nconst DEFAULT_CLOUD_SYNC_API_BASE_URL = \"https://api.codexuse.com\";\nconst CLOUD_SYNC_TIMEOUT_MS = 15_000;\n\nexport class CloudSyncClientError extends Error {\n readonly status: number | null;\n\n constructor(message: string, status: number | null = null) {\n super(message);\n this.name = \"CloudSyncClientError\";\n this.status = status;\n }\n}\n\nfunction normalizeBaseUrl(value: string): string {\n const trimmed = value.trim();\n return trimmed.endsWith(\"/\") ? trimmed.slice(0, -1) : trimmed;\n}\n\nexport function getCloudSyncApiBaseUrl(): string {\n const fromEnv = typeof process.env.CODEXUSE_API_BASE_URL === \"string\"\n ? process.env.CODEXUSE_API_BASE_URL\n : \"\";\n\n if (fromEnv.trim().length === 0) {\n return DEFAULT_CLOUD_SYNC_API_BASE_URL;\n }\n\n return normalizeBaseUrl(fromEnv);\n}\n\nfunction buildSyncUrl(pathname: string): string {\n const baseUrl = getCloudSyncApiBaseUrl();\n const normalizedPath = pathname.startsWith(\"/\") ? pathname : `/${pathname}`;\n return `${baseUrl}${normalizedPath}`;\n}\n\nfunction normalizeSnapshot(value: unknown): CloudSyncSnapshot | null {\n if (!isRecord(value)) {\n return null;\n }\n\n // Tolerate malformed legacy payloads where snapshot is nested one level:\n // { snapshot: { ...actualSnapshot } }\n if (isRecord(value.snapshot)) {\n const nested = normalizeSnapshot(value.snapshot);\n if (nested) {\n return nested;\n }\n }\n\n const updatedAt = toIsoOrNull(value.updatedAt);\n if (!updatedAt) {\n return null;\n }\n\n const schemaVersion = Number(value.schemaVersion);\n if (!Number.isFinite(schemaVersion) || schemaVersion !== CLOUD_SYNC_SCHEMA_VERSION) {\n return null;\n }\n\n const rawProfiles = Array.isArray(value.profiles) ? value.profiles : [];\n const profiles = rawProfiles\n .map((entry) => (isRecord(entry) ? entry : null))\n .filter((entry): entry is Record<string, unknown> => Boolean(entry))\n .map((entry) => {\n const data = isRecord(entry.data) ? entry.data : {};\n const metadata = isRecord(entry.metadata) ? entry.metadata : undefined;\n return {\n name: typeof entry.name === \"string\" ? entry.name : \"\",\n displayName: typeof entry.displayName === \"string\" ? entry.displayName : null,\n data,\n metadata,\n accountId: typeof entry.accountId === \"string\" ? entry.accountId : null,\n workspaceId: typeof entry.workspaceId === \"string\" ? entry.workspaceId : null,\n workspaceName: typeof entry.workspaceName === \"string\" ? entry.workspaceName : null,\n email: typeof entry.email === \"string\" ? entry.email : null,\n authMethod: typeof entry.authMethod === \"string\" ? entry.authMethod : null,\n createdAt: typeof entry.createdAt === \"string\" ? entry.createdAt : null,\n updatedAt: typeof entry.updatedAt === \"string\" ? entry.updatedAt : null,\n };\n })\n .filter((entry) => entry.name.trim().length > 0);\n\n const rawSettings = value.settingsJson;\n const settingsJson = isRecord(rawSettings) ? rawSettings : null;\n\n return {\n schemaVersion: CLOUD_SYNC_SCHEMA_VERSION,\n updatedAt,\n profiles,\n configTomlContent: typeof value.configTomlContent === \"string\" ? value.configTomlContent : null,\n settingsJson,\n };\n}\n\nasync function requestJson<T>(\n method: \"GET\" | \"PUT\",\n pathname: string,\n licenseKey: string,\n body?: unknown,\n rawBody?: string,\n): Promise<T> {\n const controller = new AbortController();\n const timeout = setTimeout(() => controller.abort(), CLOUD_SYNC_TIMEOUT_MS);\n\n try {\n const response = await fetch(buildSyncUrl(pathname), {\n method,\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-codexuse-license-key\": licenseKey,\n },\n body: typeof rawBody === \"string\"\n ? rawBody\n : body === undefined\n ? undefined\n : JSON.stringify(body),\n signal: controller.signal,\n });\n\n if (!response.ok) {\n const message = await response.text().catch(() => \"\");\n throw new CloudSyncClientError(\n message || `Cloud sync request failed (${response.status}).`,\n response.status,\n );\n }\n\n return (await response.json()) as T;\n } catch (error) {\n if (error instanceof CloudSyncClientError) {\n throw error;\n }\n\n if (error instanceof Error && error.name === \"AbortError\") {\n throw new CloudSyncClientError(\"Cloud sync request timed out.\");\n }\n\n throw new CloudSyncClientError(\n error instanceof Error ? error.message : \"Cloud sync request failed.\",\n );\n } finally {\n clearTimeout(timeout);\n }\n}\n\nexport async function fetchRemoteSnapshot(licenseKey: string): Promise<CloudSyncSnapshot | null> {\n const response = await requestJson<CloudSyncGetResponse>(\"GET\", \"/v1/sync/snapshot\", licenseKey);\n return normalizeSnapshot(response.snapshot);\n}\n\nexport async function fetchRemoteSnapshotMeta(licenseKey: string): Promise<string | null> {\n const response = await requestJson<CloudSyncMetaResponse>(\n \"GET\",\n \"/v1/sync/snapshot/meta\",\n licenseKey,\n );\n return toIsoOrNull(response.updatedAt);\n}\n\nexport async function pushRemoteSnapshot(\n licenseKey: string,\n snapshot: CloudSyncSnapshot,\n options?: { serializedSnapshot?: string },\n): Promise<CloudSyncPutResponse> {\n const serializedSnapshot = typeof options?.serializedSnapshot === \"string\"\n ? options.serializedSnapshot\n : JSON.stringify(snapshot);\n const response = await requestJson<CloudSyncPutResponse>(\n \"PUT\",\n \"/v1/sync/snapshot\",\n licenseKey,\n undefined,\n `{\"snapshot\":${serializedSnapshot}}`,\n );\n\n const normalized = normalizeSnapshot(response.snapshot);\n if (!normalized) {\n throw new CloudSyncClientError(\"Cloud sync server returned an invalid snapshot payload.\");\n }\n\n const status = response.status === \"stale\" ? \"stale\" : \"applied\";\n return {\n status,\n snapshot: normalized,\n };\n}\n","import { mkdir, readFile, stat, writeFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { parse, stringify } from \"@iarna/toml\";\nimport {\n getCodexConfigMetadata,\n type CodexConfigMetadata,\n type CodexFeatureStage,\n} from \"./codex-config-metadata\";\nimport { formatUserFacingError } from \"./errors\";\n\ntype RawConfig = Record<string, unknown>;\n\nexport type CodexDiagnosticCategory = \"feature\" | \"security\" | \"risk\" | \"config\";\nexport type CodexDiagnosticSeverity = \"info\" | \"warning\" | \"risk\";\n\nexport interface CodexConfigDiagnostic {\n id: string;\n category: CodexDiagnosticCategory;\n severity: CodexDiagnosticSeverity;\n title: string;\n message: string;\n fixId: string | null;\n fixLabel: string | null;\n}\n\nexport interface CodexConfigFeaturesSummary {\n unifiedExec: boolean | null;\n shellSnapshot: boolean | null;\n values: Record<string, boolean | null>;\n}\n\nexport interface CodexConfigToolsSummary {\n webSearch: boolean | null;\n viewImage: boolean | null;\n}\n\nexport interface CodexConfigNoticeSummary {\n hideFullAccessWarning: boolean | null;\n hideGpt51MigrationPrompt: boolean | null;\n hideLegacyGpt51CodexMaxMigrationPrompt: boolean | null;\n hideRateLimitModelNudge: boolean | null;\n modelMigrations: Record<string, string> | null;\n}\n\nexport interface CodexConfigSettingsSummary {\n model: string | null;\n reviewModel: string | null;\n modelReasoningEffort: string | null;\n modelReasoningSummary: string | null;\n modelVerbosity: string | null;\n approvalPolicy: string | null;\n sandboxMode: string | null;\n webSearch: string | null;\n personality: string | null;\n toolOutputTokenLimit: number | null;\n modelAutoCompactTokenLimit: number | null;\n modelContextWindow: number | null;\n features: CodexConfigFeaturesSummary;\n tools: CodexConfigToolsSummary;\n notice: CodexConfigNoticeSummary;\n}\n\nexport interface CodexConfigSnapshotPayload {\n path: string;\n exists: boolean;\n content: string;\n settings: CodexConfigSettingsSummary;\n mcpServers: McpServerConfig[];\n rmcpClientEnabled: boolean;\n parseError: string | null;\n isYoloPreset: boolean;\n updatedAt: number | null;\n metadata: CodexConfigMetadata;\n diagnostics: CodexConfigDiagnostic[];\n}\n\nexport interface CodexFeatureUpdateMap {\n [featureName: string]: boolean | null | undefined;\n unifiedExec?: boolean | null;\n shellSnapshot?: boolean | null;\n}\n\nexport interface CodexConfigUpdatePayload {\n model?: string | null;\n reviewModel?: string | null;\n modelReasoningEffort?: string | null;\n modelReasoningSummary?: string | null;\n modelVerbosity?: string | null;\n approvalPolicy?: string | null;\n sandboxMode?: string | null;\n webSearch?: string | null;\n personality?: string | null;\n toolOutputTokenLimit?: number | null;\n modelAutoCompactTokenLimit?: number | null;\n modelContextWindow?: number | null;\n features?: CodexFeatureUpdateMap;\n tools?: {\n webSearch?: boolean | null;\n viewImage?: boolean | null;\n };\n notice?: {\n hideFullAccessWarning?: boolean | null;\n hideGpt51MigrationPrompt?: boolean | null;\n hideLegacyGpt51CodexMaxMigrationPrompt?: boolean | null;\n hideRateLimitModelNudge?: boolean | null;\n modelMigrations?: Record<string, string> | null;\n };\n applyFixes?: string[] | null;\n}\n\nexport interface McpServerConfig {\n name: string;\n transport: \"stdio\" | \"http\" | \"sse\";\n enabled: boolean;\n command?: string | null;\n args?: string[] | null;\n cwd?: string | null;\n env?: Record<string, string> | null;\n envVars?: string[] | null;\n url?: string | null;\n bearerTokenEnvVar?: string | null;\n httpHeaders?: Record<string, string> | null;\n envHttpHeaders?: Record<string, string> | null;\n startupTimeoutSec?: number | null;\n toolTimeoutSec?: number | null;\n enabledTools?: string[] | null;\n disabledTools?: string[] | null;\n}\n\ntype ParsedResult = {\n data: RawConfig | null;\n error: string | null;\n};\n\nconst DEFAULT_CONFIG_TEMPLATE = [\n \"# ~/.codex/config.toml (managed by CodexUse)\",\n \"# Safe defaults with prompts and sandboxed access.\",\n \"# To enable full access, opt into the YOLO preset from settings.\",\n \"\",\n 'model = \"gpt-5.3-codex\"',\n 'review_model = \"gpt-5.3-codex\"',\n 'model_reasoning_effort = \"medium\"',\n 'model_reasoning_summary = \"auto\"',\n 'model_verbosity = \"medium\"',\n 'approval_policy = \"on-request\"',\n 'sandbox_mode = \"read-only\"',\n 'web_search = \"cached\"',\n \"\",\n].join(\"\\n\");\n\nconst SUPPORTED_REASONING_EFFORT = new Set([\"minimal\", \"low\", \"medium\", \"high\", \"xhigh\", \"none\"]);\nconst CLI_SUPPORTED_REASONING_EFFORT = new Set([\"minimal\", \"low\", \"medium\", \"high\", \"none\"]);\n\nconst KNOWN_APPROVAL_POLICIES = new Set([\"untrusted\", \"on-failure\", \"on-request\", \"never\"]);\nconst KNOWN_SANDBOX_MODES = new Set([\"read-only\", \"workspace-write\", \"danger-full-access\"]);\n\nconst YOLO_PRESET: CodexConfigUpdatePayload = {\n model: \"gpt-5.3-codex\",\n reviewModel: \"gpt-5.3-codex\",\n modelReasoningEffort: \"xhigh\",\n modelReasoningSummary: \"detailed\",\n modelVerbosity: \"high\",\n approvalPolicy: \"never\",\n sandboxMode: \"danger-full-access\",\n webSearch: \"live\",\n personality: \"pragmatic\",\n toolOutputTokenLimit: 25_000,\n modelAutoCompactTokenLimit: 233_000,\n features: {\n unifiedExec: true,\n shellSnapshot: true,\n },\n notice: {\n hideFullAccessWarning: true,\n hideGpt51MigrationPrompt: true,\n hideRateLimitModelNudge: true,\n },\n};\n\nfunction resolveHomeDir(): string {\n const homeDir = process.env.HOME || process.env.USERPROFILE;\n if (!homeDir) {\n throw new Error(\"Unable to determine home directory for Codex config.\");\n }\n return homeDir;\n}\n\nfunction getConfigPath(): string {\n const homeDir = resolveHomeDir();\n return path.join(homeDir, \".codex\", \"config.toml\");\n}\n\nasync function ensureConfigDirExists(filePath: string): Promise<void> {\n const dir = path.dirname(filePath);\n await mkdir(dir, { recursive: true });\n}\n\nfunction normalizeLineEndings(content: string): string {\n return content.replace(/\\r\\n/g, \"\\n\");\n}\n\nfunction ensureTrailingNewline(content: string): string {\n return content.endsWith(\"\\n\") ? content : `${content}\\n`;\n}\n\nfunction tryParseToml(content: string): ParsedResult {\n try {\n const parsed = parse(content) as RawConfig;\n return { data: parsed ?? {}, error: null };\n } catch (error) {\n const maybe = error as Partial<{ line: number; column: number; col: number }>;\n const line = typeof maybe?.line === \"number\" ? maybe.line : null;\n const column = typeof maybe?.column === \"number\" ? maybe.column : typeof maybe?.col === \"number\" ? maybe.col : null;\n const location = line !== null ? `line ${line}${column !== null ? `, column ${column}` : \"\"}` : null;\n\n const friendly = formatUserFacingError(error, {\n fallback: \"Invalid config.toml syntax. Fix it and try again.\",\n maxLength: 180,\n });\n const message = location ? `${friendly} (near ${location}).` : friendly;\n return { data: null, error: message };\n }\n}\n\nfunction toStringOrNull(value: unknown): string | null {\n if (typeof value !== \"string\") {\n return null;\n }\n const trimmed = value.trim();\n return trimmed.length > 0 ? trimmed : null;\n}\n\nfunction toBooleanOrNull(value: unknown): boolean | null {\n if (typeof value === \"boolean\") {\n return value;\n }\n return null;\n}\n\nfunction toNumberOrNull(value: unknown): number | null {\n if (typeof value !== \"number\") {\n return null;\n }\n return Number.isFinite(value) ? value : null;\n}\n\nfunction toStringArray(value: unknown): string[] {\n if (!Array.isArray(value)) {\n return [];\n }\n return value\n .map(entry => (typeof entry === \"string\" ? entry.trim() : \"\"))\n .filter(entry => entry.length > 0);\n}\n\nfunction toStringRecord(value: unknown): Record<string, string> {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) {\n return {};\n }\n\n const result: Record<string, string> = {};\n for (const [key, raw] of Object.entries(value)) {\n if (typeof raw !== \"string\") {\n continue;\n }\n const normalizedKey = key.trim();\n const normalizedValue = raw.trim();\n if (!normalizedKey || !normalizedValue) {\n continue;\n }\n result[normalizedKey] = normalizedValue;\n }\n return result;\n}\n\nfunction buildSettingsSummary(data: RawConfig | null): CodexConfigSettingsSummary {\n const noticeCandidate = data && typeof data[\"notice\"] === \"object\" && data[\"notice\"] !== null\n ? (data[\"notice\"] as RawConfig)\n : null;\n\n const featuresCandidate =\n data && typeof data[\"features\"] === \"object\" && data[\"features\"] !== null && !Array.isArray(data[\"features\"])\n ? (data[\"features\"] as RawConfig)\n : null;\n\n const toolsCandidate =\n data && typeof data[\"tools\"] === \"object\" && data[\"tools\"] !== null && !Array.isArray(data[\"tools\"])\n ? (data[\"tools\"] as RawConfig)\n : null;\n\n const featureValues: Record<string, boolean | null> = {};\n if (featuresCandidate) {\n for (const [key, value] of Object.entries(featuresCandidate)) {\n featureValues[key] = toBooleanOrNull(value);\n }\n }\n\n const modelMigrationsRaw = noticeCandidate ? toStringRecord(noticeCandidate[\"model_migrations\"]) : {};\n\n return {\n model: data ? toStringOrNull(data[\"model\"]) : null,\n reviewModel: data ? toStringOrNull(data[\"review_model\"]) : null,\n modelReasoningEffort: data ? toStringOrNull(data[\"model_reasoning_effort\"]) : null,\n modelReasoningSummary: data ? toStringOrNull(data[\"model_reasoning_summary\"]) : null,\n modelVerbosity: data ? toStringOrNull(data[\"model_verbosity\"]) : null,\n approvalPolicy: data ? toStringOrNull(data[\"approval_policy\"]) : null,\n sandboxMode: data ? toStringOrNull(data[\"sandbox_mode\"]) : null,\n webSearch: data ? toStringOrNull(data[\"web_search\"]) : null,\n personality: data ? toStringOrNull(data[\"personality\"]) : null,\n toolOutputTokenLimit: data ? toNumberOrNull(data[\"tool_output_token_limit\"]) : null,\n modelAutoCompactTokenLimit: data ? toNumberOrNull(data[\"model_auto_compact_token_limit\"]) : null,\n modelContextWindow: data ? toNumberOrNull(data[\"model_context_window\"]) : null,\n features: {\n unifiedExec: featuresCandidate ? toBooleanOrNull(featuresCandidate[\"unified_exec\"]) : null,\n shellSnapshot: featuresCandidate ? toBooleanOrNull(featuresCandidate[\"shell_snapshot\"]) : null,\n values: featureValues,\n },\n tools: {\n webSearch: toolsCandidate ? toBooleanOrNull(toolsCandidate[\"web_search\"]) : null,\n viewImage: toolsCandidate ? toBooleanOrNull(toolsCandidate[\"view_image\"]) : null,\n },\n notice: {\n hideFullAccessWarning: noticeCandidate ? toBooleanOrNull(noticeCandidate[\"hide_full_access_warning\"]) : null,\n hideGpt51MigrationPrompt: noticeCandidate ? toBooleanOrNull(noticeCandidate[\"hide_gpt5_1_migration_prompt\"]) : null,\n hideLegacyGpt51CodexMaxMigrationPrompt: noticeCandidate\n ? toBooleanOrNull(noticeCandidate[\"hide_gpt-5.1-codex-max_migration_prompt\"])\n : null,\n hideRateLimitModelNudge: noticeCandidate ? toBooleanOrNull(noticeCandidate[\"hide_rate_limit_model_nudge\"]) : null,\n modelMigrations: Object.keys(modelMigrationsRaw).length > 0 ? modelMigrationsRaw : null,\n },\n };\n}\n\nfunction getUnsupportedReasoningEffort(\n data: RawConfig | null,\n allowed: Set<string> = SUPPORTED_REASONING_EFFORT,\n): string | null {\n if (!data || !Object.prototype.hasOwnProperty.call(data, \"model_reasoning_effort\")) {\n return null;\n }\n\n const raw = data[\"model_reasoning_effort\"];\n if (typeof raw !== \"string\") {\n return String(raw ?? \"\");\n }\n\n const value = raw.trim();\n if (!value) {\n return null;\n }\n\n return allowed.has(value) ? null : value;\n}\n\nfunction parseRmcpClientEnabled(data: RawConfig | null): boolean {\n const rawFeatures =\n data && typeof data[\"features\"] === \"object\" && data[\"features\"] !== null && !Array.isArray(data[\"features\"])\n ? (data[\"features\"] as RawConfig)\n : null;\n\n if (rawFeatures && typeof rawFeatures[\"rmcp_client\"] === \"boolean\") {\n return rawFeatures[\"rmcp_client\"] as boolean;\n }\n\n if (data && typeof data[\"experimental_use_rmcp_client\"] === \"boolean\") {\n return data[\"experimental_use_rmcp_client\"] as boolean;\n }\n\n return false;\n}\n\nfunction parseMcpServer(name: string, rawValue: unknown): McpServerConfig | null {\n if (typeof rawValue !== \"object\" || rawValue === null || Array.isArray(rawValue)) {\n return null;\n }\n const raw = rawValue as RawConfig;\n const typeValue = toStringOrNull(raw[\"type\"]);\n const command = toStringOrNull(raw[\"command\"]);\n const args = toStringArray(raw[\"args\"]);\n const env = toStringRecord(raw[\"env\"]);\n const envVars = toStringArray(raw[\"env_vars\"]);\n const cwd = toStringOrNull(raw[\"cwd\"]);\n const url = toStringOrNull(raw[\"url\"]);\n const bearerTokenEnvVar = toStringOrNull(raw[\"bearer_token_env_var\"]);\n const httpHeaders = toStringRecord(raw[\"http_headers\"]);\n const envHttpHeaders = toStringRecord(raw[\"env_http_headers\"]);\n const startupTimeoutSec = toNumberOrNull(raw[\"startup_timeout_sec\"]);\n const toolTimeoutSec = toNumberOrNull(raw[\"tool_timeout_sec\"]);\n const enabledTools = toStringArray(raw[\"enabled_tools\"]);\n const disabledTools = toStringArray(raw[\"disabled_tools\"]);\n const enabled = typeof raw[\"enabled\"] === \"boolean\" ? raw[\"enabled\"] : true;\n const transport: \"stdio\" | \"http\" | \"sse\" =\n typeValue === \"stdio\" || typeValue === \"http\" || typeValue === \"sse\"\n ? typeValue\n : url\n ? \"http\"\n : \"stdio\";\n\n return {\n name,\n transport,\n enabled,\n command,\n args: args.length > 0 ? args : null,\n cwd,\n env: Object.keys(env).length > 0 ? env : null,\n envVars: envVars.length > 0 ? envVars : null,\n url,\n bearerTokenEnvVar,\n httpHeaders: Object.keys(httpHeaders).length > 0 ? httpHeaders : null,\n envHttpHeaders: Object.keys(envHttpHeaders).length > 0 ? envHttpHeaders : null,\n startupTimeoutSec,\n toolTimeoutSec,\n enabledTools: enabledTools.length > 0 ? enabledTools : null,\n disabledTools: disabledTools.length > 0 ? disabledTools : null,\n };\n}\n\nfunction buildMcpServersList(data: RawConfig | null): McpServerConfig[] {\n const rawServers = data?.[\"mcp_servers\"];\n if (typeof rawServers !== \"object\" || rawServers === null || Array.isArray(rawServers)) {\n return [];\n }\n\n const servers: McpServerConfig[] = [];\n for (const [name, rawValue] of Object.entries(rawServers)) {\n const parsed = parseMcpServer(name, rawValue);\n if (parsed) {\n servers.push(parsed);\n }\n }\n return servers;\n}\n\nfunction getFeatureStage(metadata: CodexConfigMetadata, featureKey: string): CodexFeatureStage {\n const found = metadata.featureCatalog.find(entry => entry.key === featureKey);\n return found?.stage ?? \"unknown\";\n}\n\nfunction looksLikeSecretValue(value: string): boolean {\n const normalized = value.trim();\n if (normalized.length < 12) {\n return false;\n }\n if (/\\s/.test(normalized)) {\n return false;\n }\n if (!/^[A-Za-z0-9._:-]+$/.test(normalized)) {\n return false;\n }\n const hasLetters = /[A-Za-z]/.test(normalized);\n const hasNumbers = /\\d/.test(normalized);\n return hasLetters && hasNumbers;\n}\n\nfunction buildConfigDiagnostics(\n data: RawConfig | null,\n summary: CodexConfigSettingsSummary,\n mcpServers: McpServerConfig[],\n metadata: CodexConfigMetadata,\n): CodexConfigDiagnostic[] {\n const diagnostics: CodexConfigDiagnostic[] = [];\n\n const featureTable =\n data && typeof data[\"features\"] === \"object\" && data[\"features\"] !== null && !Array.isArray(data[\"features\"])\n ? (data[\"features\"] as RawConfig)\n : null;\n\n const knownTopLevelKeys = new Set(metadata.schemaTopLevelKeys);\n if (data) {\n for (const key of Object.keys(data)) {\n if (knownTopLevelKeys.has(key)) {\n continue;\n }\n diagnostics.push({\n id: `unknown-top-level:${key}`,\n category: \"config\",\n severity: \"warning\",\n title: `Unknown config key: ${key}`,\n message: `This top-level key is not in the known Codex schema and may be ignored by Codex CLI.`,\n fixId: null,\n fixLabel: null,\n });\n }\n }\n\n if (featureTable) {\n const knownFeatureKeys = new Set(metadata.schemaFeatureKeys);\n for (const [featureKey] of Object.entries(featureTable)) {\n if (!knownFeatureKeys.has(featureKey)) {\n diagnostics.push({\n id: `unknown-feature:${featureKey}`,\n category: \"feature\",\n severity: \"warning\",\n title: `Unknown feature flag: features.${featureKey}`,\n message: \"This feature is not recognized in the current schema. Keeping it is non-destructive, but it may do nothing.\",\n fixId: `remove-feature:${featureKey}`,\n fixLabel: \"Remove flag\",\n });\n continue;\n }\n\n const stage = getFeatureStage(metadata, featureKey);\n if (stage === \"deprecated\") {\n diagnostics.push({\n id: `deprecated-feature:${featureKey}`,\n category: \"feature\",\n severity: \"warning\",\n title: `Deprecated feature flag: features.${featureKey}`,\n message: \"Codex marks this feature as deprecated. Prefer removing it.\",\n fixId: `remove-feature:${featureKey}`,\n fixLabel: \"Remove deprecated flag\",\n });\n }\n }\n }\n\n for (const server of mcpServers) {\n if (server.transport === \"stdio\" && server.args) {\n for (let index = 0; index < server.args.length; index += 1) {\n const value = server.args[index] ?? \"\";\n if (/(api[-_]?key|token|secret)\\s*=\\s*/i.test(value)) {\n diagnostics.push({\n id: `mcp-secret-arg:${server.name}:${index}`,\n category: \"security\",\n severity: \"warning\",\n title: `Potential secret in MCP args (${server.name})`,\n message: \"Argument text appears to contain a literal token/API key. Move secrets to environment variables.\",\n fixId: null,\n fixLabel: null,\n });\n }\n }\n }\n\n if (server.httpHeaders) {\n for (const [header, headerValue] of Object.entries(server.httpHeaders)) {\n if (!looksLikeSecretValue(headerValue)) {\n continue;\n }\n diagnostics.push({\n id: `mcp-secret-header:${server.name}:${header}`,\n category: \"security\",\n severity: \"warning\",\n title: `Potential secret in MCP header (${server.name})`,\n message: `HTTP header '${header}' appears to contain a literal secret. Prefer env_http_headers or bearer_token_env_var.`,\n fixId: null,\n fixLabel: null,\n });\n }\n }\n }\n\n if (summary.approvalPolicy === \"never\" && summary.sandboxMode === \"danger-full-access\") {\n diagnostics.push({\n id: \"risk:yolo-combo\",\n category: \"risk\",\n severity: \"risk\",\n title: \"High-risk execution mode enabled\",\n message: \"approval_policy=never + sandbox_mode=danger-full-access removes approval and sandbox guardrails.\",\n fixId: null,\n fixLabel: null,\n });\n }\n\n return diagnostics;\n}\n\nfunction isYoloApplied(summary: CodexConfigSettingsSummary): boolean {\n return (\n summary.model === YOLO_PRESET.model &&\n summary.reviewModel === YOLO_PRESET.reviewModel &&\n summary.modelReasoningEffort === YOLO_PRESET.modelReasoningEffort &&\n summary.modelReasoningSummary === YOLO_PRESET.modelReasoningSummary &&\n summary.modelVerbosity === YOLO_PRESET.modelVerbosity &&\n summary.approvalPolicy === YOLO_PRESET.approvalPolicy &&\n summary.sandboxMode === YOLO_PRESET.sandboxMode &&\n summary.webSearch === YOLO_PRESET.webSearch &&\n summary.personality === YOLO_PRESET.personality &&\n summary.toolOutputTokenLimit === YOLO_PRESET.toolOutputTokenLimit &&\n summary.modelAutoCompactTokenLimit === YOLO_PRESET.modelAutoCompactTokenLimit &&\n summary.features.unifiedExec === true &&\n summary.features.shellSnapshot === true &&\n summary.notice.hideFullAccessWarning === true &&\n summary.notice.hideGpt51MigrationPrompt === true &&\n summary.notice.hideRateLimitModelNudge === true\n );\n}\n\nasync function readConfigContent(): Promise<{ exists: boolean; content: string }> {\n const configPath = getConfigPath();\n try {\n const content = await readFile(configPath, \"utf8\");\n return {\n exists: true,\n content: normalizeLineEndings(content),\n };\n } catch (error) {\n const nodeError = error as NodeJS.ErrnoException;\n if (nodeError.code === \"ENOENT\") {\n return {\n exists: false,\n content: DEFAULT_CONFIG_TEMPLATE,\n };\n }\n throw error;\n }\n}\n\nasync function writeConfigContent(content: string): Promise<void> {\n const configPath = getConfigPath();\n await ensureConfigDirExists(configPath);\n const normalized = ensureTrailingNewline(normalizeLineEndings(content));\n await writeFile(configPath, normalized, \"utf8\");\n}\n\nasync function getUpdatedAt(configPath: string, exists: boolean): Promise<number | null> {\n if (!exists) {\n return null;\n }\n\n try {\n const stats = await stat(configPath);\n return stats.mtimeMs;\n } catch (error) {\n const nodeError = error as NodeJS.ErrnoException;\n if (nodeError.code === \"ENOENT\") {\n return null;\n }\n throw error;\n }\n}\n\nasync function buildSnapshot(content: string, exists: boolean): Promise<CodexConfigSnapshotPayload> {\n const configPath = getConfigPath();\n const parsed = tryParseToml(content);\n const summary = buildSettingsSummary(parsed.data);\n const updatedAt = await getUpdatedAt(configPath, exists);\n const mcpServers = buildMcpServersList(parsed.data);\n const rmcpClientEnabled = parseRmcpClientEnabled(parsed.data);\n const metadata = await getCodexConfigMetadata();\n const diagnostics = parsed.error\n ? []\n : buildConfigDiagnostics(parsed.data, summary, mcpServers, metadata);\n\n return {\n path: configPath,\n exists,\n content,\n settings: summary,\n mcpServers,\n rmcpClientEnabled,\n parseError: parsed.error,\n isYoloPreset: parsed.error ? false : isYoloApplied(summary),\n updatedAt,\n metadata,\n diagnostics,\n };\n}\n\nfunction sanitizeString(value: string | null | undefined): string | null {\n if (value === null || value === undefined) {\n return null;\n }\n const trimmed = value.trim();\n return trimmed.length > 0 ? trimmed : null;\n}\n\nfunction sanitizeNumber(value: number | null | undefined): number | null {\n if (value === undefined || value === null) {\n return null;\n }\n if (!Number.isFinite(value)) {\n throw new Error(\"Numeric value must be finite.\");\n }\n return value;\n}\n\nfunction normalizeStringArrayInput(values?: string[] | null): string[] | null {\n if (!values) {\n return null;\n }\n const normalized = values\n .map(value => sanitizeString(value))\n .filter((value): value is string => Boolean(value));\n return normalized.length > 0 ? normalized : null;\n}\n\nfunction normalizeStringRecordInput(record?: Record<string, string> | null): Record<string, string> | null {\n if (!record) {\n return null;\n }\n const normalized: Record<string, string> = {};\n for (const [key, value] of Object.entries(record)) {\n const normalizedKey = sanitizeString(key);\n const normalizedValue = sanitizeString(value);\n if (normalizedKey && normalizedValue) {\n normalized[normalizedKey] = normalizedValue;\n }\n }\n return Object.keys(normalized).length > 0 ? normalized : null;\n}\n\nfunction applySuggestedFixes(current: RawConfig, fixIds?: string[] | null): RawConfig {\n if (!fixIds || fixIds.length === 0) {\n return current;\n }\n\n let next: RawConfig = { ...current };\n\n for (const fixId of fixIds) {\n if (!fixId.startsWith(\"remove-feature:\")) {\n continue;\n }\n\n const featureKey = fixId.slice(\"remove-feature:\".length).trim();\n if (!featureKey) {\n continue;\n }\n\n const rawFeatures =\n typeof next[\"features\"] === \"object\" && next[\"features\"] !== null && !Array.isArray(next[\"features\"])\n ? { ...(next[\"features\"] as RawConfig) }\n : {};\n\n if (!Object.prototype.hasOwnProperty.call(rawFeatures, featureKey)) {\n continue;\n }\n\n delete rawFeatures[featureKey];\n if (Object.keys(rawFeatures).length === 0) {\n delete next[\"features\"];\n } else {\n next[\"features\"] = rawFeatures;\n }\n }\n\n return next;\n}\n\nfunction applyUpdatesToConfig(current: RawConfig, updates: CodexConfigUpdatePayload): RawConfig {\n const next: RawConfig = { ...current };\n\n const applyStringField = (key: string, candidate?: string | null) => {\n if (candidate === undefined) {\n return;\n }\n const normalized = sanitizeString(candidate);\n if (!normalized) {\n delete next[key];\n } else {\n next[key] = normalized;\n }\n };\n\n const applyNumberField = (key: string, candidate?: number | null) => {\n if (candidate === undefined) {\n return;\n }\n const normalized = sanitizeNumber(candidate);\n if (normalized === null) {\n delete next[key];\n } else {\n next[key] = normalized;\n }\n };\n\n applyStringField(\"model\", updates.model);\n applyStringField(\"review_model\", updates.reviewModel);\n applyStringField(\"model_reasoning_effort\", updates.modelReasoningEffort);\n applyStringField(\"model_reasoning_summary\", updates.modelReasoningSummary);\n applyStringField(\"model_verbosity\", updates.modelVerbosity);\n applyStringField(\"approval_policy\", updates.approvalPolicy);\n applyStringField(\"sandbox_mode\", updates.sandboxMode);\n applyStringField(\"web_search\", updates.webSearch);\n applyStringField(\"personality\", updates.personality);\n\n applyNumberField(\"tool_output_token_limit\", updates.toolOutputTokenLimit);\n applyNumberField(\"model_auto_compact_token_limit\", updates.modelAutoCompactTokenLimit);\n applyNumberField(\"model_context_window\", updates.modelContextWindow);\n\n if (updates.features) {\n const rawFeatures =\n typeof next[\"features\"] === \"object\" && next[\"features\"] !== null && !Array.isArray(next[\"features\"])\n ? { ...(next[\"features\"] as RawConfig) }\n : {};\n\n const applyFeatureField = (key: string, candidate: boolean | null | undefined) => {\n if (candidate === undefined) {\n return;\n }\n if (candidate === null) {\n delete rawFeatures[key];\n return;\n }\n rawFeatures[key] = candidate;\n };\n\n for (const [rawKey, candidate] of Object.entries(updates.features)) {\n const key = rawKey === \"unifiedExec\"\n ? \"unified_exec\"\n : rawKey === \"shellSnapshot\"\n ? \"shell_snapshot\"\n : rawKey;\n applyFeatureField(key, candidate);\n }\n\n if (Object.keys(rawFeatures).length === 0) {\n delete next[\"features\"];\n } else {\n next[\"features\"] = rawFeatures;\n }\n }\n\n if (updates.tools) {\n const rawTools =\n typeof next[\"tools\"] === \"object\" && next[\"tools\"] !== null && !Array.isArray(next[\"tools\"])\n ? { ...(next[\"tools\"] as RawConfig) }\n : {};\n\n const applyToolField = (key: string, candidate?: boolean | null) => {\n if (candidate === undefined) {\n return;\n }\n if (candidate === null) {\n delete rawTools[key];\n } else {\n rawTools[key] = candidate;\n }\n };\n\n applyToolField(\"web_search\", updates.tools.webSearch);\n applyToolField(\"view_image\", updates.tools.viewImage);\n\n if (Object.keys(rawTools).length === 0) {\n delete next[\"tools\"];\n } else {\n next[\"tools\"] = rawTools;\n }\n }\n\n if (updates.notice) {\n const rawNotice = next[\"notice\"];\n const currentNotice =\n rawNotice && typeof rawNotice === \"object\" && rawNotice !== null\n ? { ...(rawNotice as RawConfig) }\n : {};\n\n const applyBooleanField = (key: string, candidate?: boolean | null) => {\n if (candidate === undefined) {\n return;\n }\n if (candidate === null) {\n delete currentNotice[key];\n } else {\n currentNotice[key] = candidate;\n }\n };\n\n const applyRecordField = (key: string, candidate?: Record<string, string> | null) => {\n if (candidate === undefined) {\n return;\n }\n const normalized = normalizeStringRecordInput(candidate);\n if (!normalized) {\n delete currentNotice[key];\n } else {\n currentNotice[key] = normalized;\n }\n };\n\n applyBooleanField(\"hide_full_access_warning\", updates.notice.hideFullAccessWarning);\n applyBooleanField(\"hide_gpt5_1_migration_prompt\", updates.notice.hideGpt51MigrationPrompt);\n applyBooleanField(\n \"hide_gpt-5.1-codex-max_migration_prompt\",\n updates.notice.hideLegacyGpt51CodexMaxMigrationPrompt,\n );\n applyBooleanField(\"hide_rate_limit_model_nudge\", updates.notice.hideRateLimitModelNudge);\n applyRecordField(\"model_migrations\", updates.notice.modelMigrations);\n\n if (Object.keys(currentNotice).length === 0) {\n delete next[\"notice\"];\n } else {\n next[\"notice\"] = currentNotice;\n }\n }\n\n return applySuggestedFixes(next, updates.applyFixes);\n}\n\nfunction validateMcpServerName(name: string): string {\n const normalized = sanitizeString(name);\n if (!normalized) {\n throw new Error(\"MCP server name is required.\");\n }\n if (!/^[A-Za-z0-9._-]+$/.test(normalized)) {\n throw new Error(\"MCP server name must use letters, numbers, periods, underscores, or hyphens.\");\n }\n return normalized;\n}\n\nfunction applyMcpServerUpsert(\n current: RawConfig,\n server: McpServerConfig,\n rmcpClientEnabled?: boolean | null,\n): RawConfig {\n const name = validateMcpServerName(server.name);\n const next: RawConfig = { ...current };\n const rawServers =\n typeof next[\"mcp_servers\"] === \"object\" && next[\"mcp_servers\"] !== null && !Array.isArray(next[\"mcp_servers\"])\n ? { ...(next[\"mcp_servers\"] as RawConfig) }\n : {};\n\n const base =\n typeof rawServers[name] === \"object\" && rawServers[name] !== null && !Array.isArray(rawServers[name])\n ? { ...(rawServers[name] as RawConfig) }\n : {};\n\n const updated: RawConfig = { ...base };\n\n const setStringField = (key: string, value: string | null | undefined) => {\n const normalized = sanitizeString(value ?? null);\n if (!normalized) {\n delete updated[key];\n return;\n }\n updated[key] = normalized;\n };\n\n const setStringArrayField = (key: string, values: string[] | null | undefined) => {\n const normalized = normalizeStringArrayInput(values);\n if (!normalized) {\n delete updated[key];\n return;\n }\n updated[key] = normalized;\n };\n\n const setRecordField = (key: string, values: Record<string, string> | null | undefined) => {\n const normalized = normalizeStringRecordInput(values);\n if (!normalized) {\n delete updated[key];\n return;\n }\n updated[key] = normalized;\n };\n\n const setNumberField = (key: string, value: number | null | undefined) => {\n if (value === null || value === undefined) {\n delete updated[key];\n return;\n }\n if (!Number.isFinite(value)) {\n throw new Error(`Invalid numeric value for ${key}.`);\n }\n updated[key] = value;\n };\n\n updated[\"enabled\"] = Boolean(server.enabled);\n updated[\"type\"] = server.transport;\n setNumberField(\"startup_timeout_sec\", server.startupTimeoutSec);\n setNumberField(\"tool_timeout_sec\", server.toolTimeoutSec);\n setStringArrayField(\"enabled_tools\", server.enabledTools);\n setStringArrayField(\"disabled_tools\", server.disabledTools);\n\n if (server.transport !== \"stdio\") {\n setStringField(\"url\", server.url);\n if (!sanitizeString(server.url)) {\n throw new Error(\"HTTP/SSE MCP server requires a URL.\");\n }\n setStringField(\"bearer_token_env_var\", server.bearerTokenEnvVar);\n setRecordField(\"http_headers\", server.httpHeaders);\n setRecordField(\"env_http_headers\", server.envHttpHeaders);\n delete updated[\"command\"];\n delete updated[\"args\"];\n delete updated[\"env\"];\n delete updated[\"env_vars\"];\n delete updated[\"cwd\"];\n } else {\n setStringField(\"command\", server.command);\n if (!sanitizeString(server.command)) {\n throw new Error(\"STDIO MCP server requires a command.\");\n }\n setStringArrayField(\"args\", server.args);\n setRecordField(\"env\", server.env);\n setStringArrayField(\"env_vars\", server.envVars);\n setStringField(\"cwd\", server.cwd);\n delete updated[\"url\"];\n delete updated[\"bearer_token_env_var\"];\n delete updated[\"http_headers\"];\n delete updated[\"env_http_headers\"];\n }\n\n rawServers[name] = updated;\n if (Object.keys(rawServers).length > 0) {\n next[\"mcp_servers\"] = rawServers;\n } else {\n delete next[\"mcp_servers\"];\n }\n\n const rmcpEnabledValue = rmcpClientEnabled ?? parseRmcpClientEnabled(next);\n return applyRmcpClientFlag(next, rmcpEnabledValue);\n}\n\nfunction applyMcpServerRemoval(current: RawConfig, serverName: string): RawConfig {\n const name = validateMcpServerName(serverName);\n const next: RawConfig = { ...current };\n const rawServers =\n typeof next[\"mcp_servers\"] === \"object\" && next[\"mcp_servers\"] !== null && !Array.isArray(next[\"mcp_servers\"])\n ? { ...(next[\"mcp_servers\"] as RawConfig) }\n : {};\n\n if (!Object.prototype.hasOwnProperty.call(rawServers, name)) {\n return next;\n }\n\n delete rawServers[name];\n if (Object.keys(rawServers).length > 0) {\n next[\"mcp_servers\"] = rawServers;\n } else {\n delete next[\"mcp_servers\"];\n }\n return next;\n}\n\nfunction applyRmcpClientFlag(current: RawConfig, enabled: boolean): RawConfig {\n const next: RawConfig = { ...current };\n const rawFeatures =\n typeof next[\"features\"] === \"object\" && next[\"features\"] !== null && !Array.isArray(next[\"features\"])\n ? { ...(next[\"features\"] as RawConfig) }\n : {};\n\n if (enabled) {\n rawFeatures[\"rmcp_client\"] = true;\n } else {\n delete rawFeatures[\"rmcp_client\"];\n }\n\n if (Object.keys(rawFeatures).length > 0) {\n next[\"features\"] = rawFeatures;\n } else {\n delete next[\"features\"];\n }\n\n delete next[\"experimental_use_rmcp_client\"];\n return next;\n}\n\nasync function readParsedConfigOrThrow(): Promise<RawConfig> {\n const { content } = await readConfigContent();\n const parsed = tryParseToml(content);\n\n if (!parsed.data) {\n throw new Error(parsed.error ?? \"config.toml contains invalid TOML syntax.\");\n }\n\n return parsed.data;\n}\n\nexport async function getCodexConfigSnapshot(): Promise<CodexConfigSnapshotPayload> {\n const { content, exists } = await readConfigContent();\n return buildSnapshot(content, exists);\n}\n\nexport async function saveCodexConfigContent(content: string): Promise<CodexConfigSnapshotPayload> {\n const normalized = ensureTrailingNewline(normalizeLineEndings(content));\n const parsed = tryParseToml(normalized);\n if (!parsed.data) {\n throw new Error(parsed.error ?? \"config.toml contains invalid TOML syntax.\");\n }\n await writeConfigContent(normalized);\n return buildSnapshot(normalized, true);\n}\n\nexport async function updateCodexConfigValues(\n updates: CodexConfigUpdatePayload,\n): Promise<CodexConfigSnapshotPayload> {\n const parsed = await readParsedConfigOrThrow();\n const updated = applyUpdatesToConfig(parsed, updates);\n const serialized = ensureTrailingNewline(stringify(updated as Record<string, any>));\n await writeConfigContent(serialized);\n return buildSnapshot(serialized, true);\n}\n\nexport async function applyYoloOpinionatedMode(): Promise<CodexConfigSnapshotPayload> {\n return updateCodexConfigValues(YOLO_PRESET);\n}\n\nexport async function upsertMcpServer(\n server: McpServerConfig,\n options?: { rmcpClientEnabled?: boolean | null },\n): Promise<CodexConfigSnapshotPayload> {\n const parsed = await readParsedConfigOrThrow();\n const updated = applyMcpServerUpsert(parsed, server, options?.rmcpClientEnabled);\n const serialized = ensureTrailingNewline(stringify(updated as Record<string, any>));\n await writeConfigContent(serialized);\n return buildSnapshot(serialized, true);\n}\n\nexport async function removeMcpServer(serverName: string): Promise<CodexConfigSnapshotPayload> {\n const parsed = await readParsedConfigOrThrow();\n const updated = applyMcpServerRemoval(parsed, serverName);\n const serialized = ensureTrailingNewline(stringify(updated as Record<string, any>));\n await writeConfigContent(serialized);\n return buildSnapshot(serialized, true);\n}\n\nexport async function setRmcpClient(enabled: boolean): Promise<CodexConfigSnapshotPayload> {\n const parsed = await readParsedConfigOrThrow();\n const updated = applyRmcpClientFlag(parsed, enabled);\n const serialized = ensureTrailingNewline(stringify(updated as Record<string, any>));\n await writeConfigContent(serialized);\n return buildSnapshot(serialized, true);\n}\n\nexport async function sanitizeCodexConfigForCli(): Promise<(() => Promise<void>) | null> {\n const { content } = await readConfigContent();\n const parsed = tryParseToml(content);\n\n if (!parsed.data) {\n throw new Error(parsed.error ?? \"config.toml contains invalid TOML syntax.\");\n }\n\n const unsupportedReasoning = getUnsupportedReasoningEffort(parsed.data, CLI_SUPPORTED_REASONING_EFFORT);\n if (!unsupportedReasoning) {\n return null;\n }\n\n const sanitized = { ...(parsed.data as RawConfig) };\n delete sanitized[\"model_reasoning_effort\"];\n const sanitizedContent = ensureTrailingNewline(stringify(sanitized as Record<string, any>));\n await writeConfigContent(sanitizedContent);\n\n let restored = false;\n return async () => {\n if (restored) {\n return;\n }\n restored = true;\n const originalContent = ensureTrailingNewline(normalizeLineEndings(content));\n await writeConfigContent(originalContent);\n };\n}\n\nexport type { McpServerConfig };\n\nexport function isKnownApprovalPolicy(value: string | null): value is \"untrusted\" | \"on-failure\" | \"on-request\" | \"never\" {\n return Boolean(value && KNOWN_APPROVAL_POLICIES.has(value));\n}\n\nexport function isKnownSandboxMode(value: string | null): value is \"read-only\" | \"workspace-write\" | \"danger-full-access\" {\n return Boolean(value && KNOWN_SANDBOX_MODES.has(value));\n}\n","import { spawn } from \"node:child_process\";\nimport { readFile } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { requireCodexCli } from \"./codex-cli\";\n\nexport type CodexFeatureStage = \"stable\" | \"experimental\" | \"under development\" | \"deprecated\" | \"unknown\";\n\nexport interface CodexFeatureCatalogEntry {\n key: string;\n stage: CodexFeatureStage;\n enabled: boolean | null;\n}\n\nexport interface CodexConfigMetadata {\n schemaTopLevelKeys: string[];\n schemaFeatureKeys: string[];\n featureCatalog: CodexFeatureCatalogEntry[];\n codexVersion: string | null;\n fetchedAt: number;\n}\n\nconst CONFIG_SCHEMA_URL = \"https://raw.githubusercontent.com/openai/codex/main/codex-rs/core/config.schema.json\";\nconst METADATA_CACHE_TTL_MS = 60_000;\n\nconst FALLBACK_SCHEMA_TOP_LEVEL_KEYS = [\n \"agents\",\n \"analytics\",\n \"approval_policy\",\n \"apps\",\n \"chatgpt_base_url\",\n \"check_for_update_on_startup\",\n \"cli_auth_credentials_store\",\n \"compact_prompt\",\n \"developer_instructions\",\n \"disable_paste_burst\",\n \"experimental_compact_prompt_file\",\n \"experimental_use_freeform_apply_patch\",\n \"experimental_use_unified_exec_tool\",\n \"features\",\n \"feedback\",\n \"file_opener\",\n \"forced_chatgpt_workspace_id\",\n \"forced_login_method\",\n \"ghost_snapshot\",\n \"hide_agent_reasoning\",\n \"history\",\n \"instructions\",\n \"log_dir\",\n \"mcp_oauth_callback_port\",\n \"mcp_oauth_credentials_store\",\n \"mcp_servers\",\n \"model\",\n \"model_auto_compact_token_limit\",\n \"model_context_window\",\n \"model_instructions_file\",\n \"model_provider\",\n \"model_providers\",\n \"model_reasoning_effort\",\n \"model_reasoning_summary\",\n \"model_supports_reasoning_summaries\",\n \"model_verbosity\",\n \"notice\",\n \"notify\",\n \"oss_provider\",\n \"otel\",\n \"personality\",\n \"profile\",\n \"profiles\",\n \"project_doc_fallback_filenames\",\n \"project_doc_max_bytes\",\n \"project_root_markers\",\n \"projects\",\n \"review_model\",\n \"sandbox_mode\",\n \"sandbox_workspace_write\",\n \"shell_environment_policy\",\n \"show_raw_agent_reasoning\",\n \"skills\",\n \"suppress_unstable_features_warning\",\n \"tool_output_token_limit\",\n \"tools\",\n \"tui\",\n \"web_search\",\n \"windows_wsl_setup_acknowledged\",\n];\n\nconst FALLBACK_FEATURE_KEYS = [\n \"apply_patch_freeform\",\n \"apps\",\n \"child_agents_md\",\n \"collab\",\n \"collaboration_modes\",\n \"connectors\",\n \"elevated_windows_sandbox\",\n \"enable_experimental_windows_sandbox\",\n \"enable_request_compression\",\n \"experimental_use_freeform_apply_patch\",\n \"experimental_use_unified_exec_tool\",\n \"experimental_windows_sandbox\",\n \"include_apply_patch_tool\",\n \"memory_tool\",\n \"personality\",\n \"powershell_utf8\",\n \"remote_models\",\n \"request_rule\",\n \"responses_websockets\",\n \"responses_websockets_v2\",\n \"runtime_metrics\",\n \"search_tool\",\n \"shell_snapshot\",\n \"shell_tool\",\n \"skill_env_var_dependency_prompt\",\n \"skill_mcp_dependency_install\",\n \"sqlite\",\n \"steer\",\n \"undo\",\n \"unified_exec\",\n \"use_linux_sandbox_bwrap\",\n \"web_search\",\n \"web_search_cached\",\n \"web_search_request\",\n];\n\nlet metadataCache: CodexConfigMetadata | null = null;\nlet metadataCacheAt = 0;\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return Boolean(value) && typeof value === \"object\" && !Array.isArray(value);\n}\n\nfunction uniqueSorted(values: string[]): string[] {\n return Array.from(new Set(values)).sort((a, b) => a.localeCompare(b));\n}\n\nasync function runCommand(\n command: string,\n args: string[],\n timeoutMs = 3_000,\n): Promise<{ stdout: string; exitCode: number | null }> {\n return new Promise((resolve, reject) => {\n const child = spawn(command, args, {\n stdio: [\"ignore\", \"pipe\", \"pipe\"],\n env: process.env,\n });\n\n let stdout = \"\";\n let stderr = \"\";\n\n child.stdout?.on(\"data\", chunk => {\n stdout += chunk.toString();\n });\n\n child.stderr?.on(\"data\", chunk => {\n stderr += chunk.toString();\n });\n\n const timeout = setTimeout(() => {\n child.kill();\n reject(new Error(`Command timed out: ${command} ${args.join(\" \")}`));\n }, timeoutMs);\n\n child.on(\"error\", error => {\n clearTimeout(timeout);\n reject(error);\n });\n\n child.on(\"close\", code => {\n clearTimeout(timeout);\n if (code !== 0 && !stdout.trim() && stderr.trim()) {\n resolve({ stdout: stderr, exitCode: code });\n return;\n }\n resolve({ stdout, exitCode: code });\n });\n });\n}\n\nasync function runCodexCommand(args: string[]): Promise<{ stdout: string; exitCode: number | null }> {\n const codexPath = await requireCodexCli();\n const isJsLauncher = codexPath.endsWith(\".js\");\n const command = isJsLauncher ? process.execPath : codexPath;\n const commandArgs = isJsLauncher ? [codexPath, ...args] : args;\n return runCommand(command, commandArgs);\n}\n\nfunction parseFeatureCatalog(output: string): CodexFeatureCatalogEntry[] {\n const entries: CodexFeatureCatalogEntry[] = [];\n const lines = output.split(/\\r?\\n/).map(line => line.trim()).filter(Boolean);\n\n for (const line of lines) {\n const match = line.match(/^(\\S+)\\s+(stable|experimental|deprecated|under development)\\s+(true|false)$/i);\n if (!match) {\n continue;\n }\n const [, key, stageRaw, enabledRaw] = match;\n const stage = stageRaw.toLowerCase() as Exclude<CodexFeatureStage, \"unknown\">;\n entries.push({\n key,\n stage,\n enabled: enabledRaw === \"true\",\n });\n }\n\n return entries.sort((a, b) => a.key.localeCompare(b.key));\n}\n\nfunction parseSchemaKeys(schemaRaw: string): { topLevelKeys: string[]; featureKeys: string[] } {\n const parsed = JSON.parse(schemaRaw) as unknown;\n if (!isRecord(parsed)) {\n return {\n topLevelKeys: FALLBACK_SCHEMA_TOP_LEVEL_KEYS,\n featureKeys: FALLBACK_FEATURE_KEYS,\n };\n }\n\n const properties = isRecord(parsed.properties) ? parsed.properties : {};\n const topLevelKeys = uniqueSorted(Object.keys(properties));\n\n const features = isRecord(properties.features) ? properties.features : {};\n const featureProperties = isRecord(features.properties) ? features.properties : {};\n const featureKeys = uniqueSorted(Object.keys(featureProperties));\n\n return {\n topLevelKeys: topLevelKeys.length > 0 ? topLevelKeys : FALLBACK_SCHEMA_TOP_LEVEL_KEYS,\n featureKeys: featureKeys.length > 0 ? featureKeys : FALLBACK_FEATURE_KEYS,\n };\n}\n\nasync function readSchemaFromLocal(): Promise<string | null> {\n const candidates = [\n process.env.CODEX_CONFIG_SCHEMA_PATH,\n path.join(process.cwd(), \"codex-rs\", \"core\", \"config.schema.json\"),\n path.join(process.cwd(), \"node_modules\", \"@openai\", \"codex\", \"codex-rs\", \"core\", \"config.schema.json\"),\n path.join(process.cwd(), \"node_modules\", \"@openai\", \"codex-alpha\", \"codex-rs\", \"core\", \"config.schema.json\"),\n ].filter((candidate): candidate is string => Boolean(candidate));\n\n for (const candidate of candidates) {\n try {\n return await readFile(candidate, \"utf8\");\n } catch {\n // Continue to next candidate.\n }\n }\n\n return null;\n}\n\nasync function readSchemaFromRemote(): Promise<string | null> {\n const controller = new AbortController();\n const timeout = setTimeout(() => controller.abort(), 2_500);\n\n try {\n const response = await fetch(CONFIG_SCHEMA_URL, {\n signal: controller.signal,\n headers: {\n \"User-Agent\": \"codexuse-desktop\",\n },\n });\n\n if (!response.ok) {\n return null;\n }\n\n return await response.text();\n } catch {\n return null;\n } finally {\n clearTimeout(timeout);\n }\n}\n\nasync function readSchemaKeys(): Promise<{ topLevelKeys: string[]; featureKeys: string[] }> {\n const local = await readSchemaFromLocal();\n if (local) {\n try {\n return parseSchemaKeys(local);\n } catch {\n // Fallback to remote/fallback sets.\n }\n }\n\n const remote = await readSchemaFromRemote();\n if (remote) {\n try {\n return parseSchemaKeys(remote);\n } catch {\n // Fallback below.\n }\n }\n\n return {\n topLevelKeys: FALLBACK_SCHEMA_TOP_LEVEL_KEYS,\n featureKeys: FALLBACK_FEATURE_KEYS,\n };\n}\n\nasync function readCodexVersion(): Promise<string | null> {\n try {\n const { stdout } = await runCodexCommand([\"--version\"]);\n const value = stdout.trim();\n return value.length > 0 ? value : null;\n } catch {\n return null;\n }\n}\n\nasync function readFeatureCatalog(): Promise<CodexFeatureCatalogEntry[]> {\n try {\n const { stdout } = await runCodexCommand([\"features\", \"list\"]);\n return parseFeatureCatalog(stdout);\n } catch {\n return [];\n }\n}\n\nexport async function getCodexConfigMetadata(forceRefresh = false): Promise<CodexConfigMetadata> {\n const now = Date.now();\n if (!forceRefresh && metadataCache && (now - metadataCacheAt) < METADATA_CACHE_TTL_MS) {\n return metadataCache;\n }\n\n const [schemaKeys, featureCatalog, codexVersion] = await Promise.all([\n readSchemaKeys(),\n readFeatureCatalog(),\n readCodexVersion(),\n ]);\n\n const catalogMap = new Map<string, CodexFeatureCatalogEntry>();\n for (const entry of featureCatalog) {\n catalogMap.set(entry.key, entry);\n }\n\n for (const featureKey of schemaKeys.featureKeys) {\n if (!catalogMap.has(featureKey)) {\n catalogMap.set(featureKey, {\n key: featureKey,\n stage: \"unknown\",\n enabled: null,\n });\n }\n }\n\n const mergedFeatureCatalog = Array.from(catalogMap.values()).sort((a, b) => a.key.localeCompare(b.key));\n\n const metadata: CodexConfigMetadata = {\n schemaTopLevelKeys: uniqueSorted(schemaKeys.topLevelKeys),\n schemaFeatureKeys: uniqueSorted(schemaKeys.featureKeys),\n featureCatalog: mergedFeatureCatalog,\n codexVersion,\n fetchedAt: now,\n };\n\n metadataCache = metadata;\n metadataCacheAt = now;\n return metadata;\n}\n","import { readFileSync, statSync } from \"node:fs\";\nimport path from \"node:path\";\nimport type { CodexCliInfoPayload, CodexCliStatusPayload } from \"./ipc-types\";\nimport { getCodexCliChannel } from \"./codex-settings\";\nimport type { CodexCliChannel } from \"./codex-cli-channel\";\n\ntype MutableStatus = CodexCliStatusPayload;\n\nlet cachedStatus: MutableStatus | null = null;\nlet cachedChannel: CodexCliChannel | null = null;\n\nfunction fileExists(candidate: string | undefined | null): string | null {\n if (!candidate) {\n return null;\n }\n\n const normalized = path.resolve(candidate);\n\n try {\n const stats = statSync(normalized);\n if (stats.isFile()) {\n return normalized;\n }\n } catch {\n // ignore missing paths\n }\n\n return null;\n}\n\nfunction resolveBundledCodexBinary(channel: CodexCliChannel): string | null {\n const candidates: string[] = [];\n const processWithResources = process as NodeJS.Process & { resourcesPath?: string };\n const resourcesPath = processWithResources.resourcesPath;\n const packageSegments = channel === \"alpha\" ? [\"@openai\", \"codex-alpha\"] : [\"@openai\", \"codex\"];\n\n if (resourcesPath) {\n candidates.push(\n path.join(resourcesPath, \"app.asar.unpacked\", \"node_modules\", ...packageSegments, \"bin\", \"codex.js\"),\n );\n candidates.push(\n path.join(resourcesPath, \"app.asar.unpacked\", \"node_modules\", ...packageSegments, \"bin\", \"codex\"),\n );\n }\n\n const projectRoot = process.cwd();\n candidates.push(path.join(projectRoot, \"node_modules\", ...packageSegments, \"bin\", \"codex.js\"));\n candidates.push(path.join(projectRoot, \"node_modules\", ...packageSegments, \"bin\", \"codex\"));\n\n for (const candidate of candidates) {\n const resolved = fileExists(candidate);\n if (resolved) {\n return resolved;\n }\n }\n\n return null;\n}\n\nfunction resolvePackageJsonPath(cliPath: string): string | null {\n const normalized = path.resolve(cliPath);\n const binDir = path.dirname(normalized);\n const packageDir = path.basename(binDir) === \"bin\" ? path.dirname(binDir) : path.dirname(normalized);\n const pkgPath = path.join(packageDir, \"package.json\");\n return fileExists(pkgPath);\n}\n\nfunction readCodexCliVersion(cliPath: string): string | null {\n const packageJsonPath = resolvePackageJsonPath(cliPath);\n if (!packageJsonPath) {\n return null;\n }\n try {\n const raw = readFileSync(packageJsonPath, \"utf8\");\n const parsed = JSON.parse(raw) as { version?: unknown };\n if (typeof parsed?.version !== \"string\") {\n return null;\n }\n const normalized = parsed.version.trim();\n return normalized.length > 0 ? normalized : null;\n } catch {\n return null;\n }\n}\n\nfunction resolveBundledCodexBinaryWithFallback(requestedChannel: CodexCliChannel): {\n path: string | null;\n resolvedChannel: CodexCliChannel | null;\n fallbackUsed: boolean;\n} {\n const direct = resolveBundledCodexBinary(requestedChannel);\n if (direct) {\n return { path: direct, resolvedChannel: requestedChannel, fallbackUsed: false };\n }\n\n if (requestedChannel === \"alpha\") {\n const stable = resolveBundledCodexBinary(\"stable\");\n if (stable) {\n return { path: stable, resolvedChannel: \"stable\", fallbackUsed: true };\n }\n }\n\n return { path: null, resolvedChannel: null, fallbackUsed: false };\n}\n\nfunction buildUnavailableStatus(channel: CodexCliChannel): MutableStatus {\n const channelLabel = channel === \"alpha\" ? \"alpha \" : \"\";\n const channelSuffix =\n channel === \"alpha\"\n ? \" Switch to Stable in Settings or reinstall CodexUse to restore the CLI.\"\n : \" Reinstall CodexUse to restore the CLI.\";\n return {\n available: false,\n path: null,\n reason: `Bundled ${channelLabel}Codex CLI is missing.${channelSuffix}`,\n source: null,\n channel,\n requestedChannel: channel,\n fallbackUsed: false,\n };\n}\n\nfunction evaluateCodexCliStatus(requestedChannel: CodexCliChannel): MutableStatus {\n const resolved = resolveBundledCodexBinaryWithFallback(requestedChannel);\n if (resolved.path && resolved.resolvedChannel) {\n return {\n available: true,\n path: resolved.path,\n reason: null,\n source: \"bundled\",\n channel: resolved.resolvedChannel,\n requestedChannel,\n fallbackUsed: resolved.fallbackUsed,\n };\n }\n\n return buildUnavailableStatus(requestedChannel);\n}\n\nexport async function refreshCodexStatus(): Promise<CodexCliStatusPayload> {\n const channel = await getCodexCliChannel();\n cachedChannel = channel;\n cachedStatus = evaluateCodexCliStatus(channel);\n return { ...cachedStatus };\n}\n\nexport async function getCodexCliStatus(): Promise<CodexCliStatusPayload> {\n const channel = await getCodexCliChannel();\n if (!cachedStatus || cachedChannel !== channel) {\n return refreshCodexStatus();\n }\n return { ...cachedStatus };\n}\n\nexport async function getCodexCliInfo(): Promise<CodexCliInfoPayload> {\n const status = await getCodexCliStatus();\n const version =\n status.available && status.path\n ? readCodexCliVersion(status.path)\n : null;\n return { ...status, version };\n}\n\nexport class CodexCliMissingError extends Error {\n readonly status: CodexCliStatusPayload;\n\n constructor(status: CodexCliStatusPayload, message?: string) {\n super(message ?? status.reason ?? \"Codex CLI is not available\");\n Object.setPrototypeOf(this, new.target.prototype);\n this.name = \"CodexCliMissingError\";\n this.status = { ...status };\n }\n}\n\nexport async function requireCodexCli(): Promise<string> {\n const status = await refreshCodexStatus();\n if (!status.available || !status.path) {\n throw new CodexCliMissingError(status);\n }\n return status.path;\n}\n","/* eslint-disable-next-line no-control-regex */\nconst ANSI_PATTERN = /\\u001B\\[[0-9;]*m/g;\n\nfunction stripAnsi(value: string): string {\n return value.replace(ANSI_PATTERN, \"\");\n}\n\nfunction normalizeWhitespace(value: string): string {\n return value.replace(/\\s+/g, \" \").trim();\n}\n\nexport function compactErrorText(value: string, maxLength = 220): string {\n if (!value) {\n return \"\";\n }\n const cleaned = normalizeWhitespace(stripAnsi(String(value)));\n if (!cleaned) {\n return \"\";\n }\n if (cleaned.length <= maxLength) {\n return cleaned;\n }\n return `${cleaned.slice(0, maxLength).trimEnd()}…`;\n}\n\nexport function formatUserFacingError(\n error: unknown,\n options: {\n fallback?: string;\n notFoundFallback?: string;\n maxLength?: number;\n } = {},\n): string {\n const fallback = options.fallback ?? \"Something went wrong. Please try again.\";\n if (error === null || typeof error === \"undefined\") {\n return fallback;\n }\n\n const rawMessage =\n typeof error === \"string\"\n ? error\n : error instanceof Error\n ? error.message\n : String(error);\n\n let cleaned = normalizeWhitespace(stripAnsi(rawMessage));\n if (!cleaned) {\n return fallback;\n }\n\n // Strip Electron IPC error prefix\n if (cleaned.startsWith(\"Error invoking remote method\")) {\n cleaned = cleaned.replace(/^Error invoking remote method '[^']+': Error: /, \"\");\n }\n\n const lower = cleaned.toLowerCase();\n\n if (options.notFoundFallback && (lower.includes(\"enoent\") || lower.includes(\"not found\"))) {\n return options.notFoundFallback;\n }\n\n if (lower.includes(\"rate limit\") || lower.includes(\"quota exceeded\") || lower.includes(\"too many requests\") || lower.includes(\"429\")) {\n cleaned = \"Rate limit exceeded. Please wait a moment or upgrade your plan.\";\n } else if (lower.includes(\"timeout\") || lower.includes(\"timed out\")) {\n cleaned = \"Request timed out. Check your connection and try again.\";\n } else if (\n lower.includes(\"network\") ||\n lower.includes(\"econn\") ||\n lower.includes(\"networkerror\") ||\n lower.includes(\"connection\")\n ) {\n cleaned = \"Network issue. Check your connection and retry.\";\n } else if (lower.includes(\"permission\") || lower.includes(\"eacces\")) {\n cleaned = \"Permission denied. Restart CodexUse and retry.\";\n } else if (lower.includes(\"openai codex\") && lower.includes(\"workdir:\")) {\n // If it's just the CLI banner, it's likely a generic failure or the actual error is buried.\n // We'll try to find a more specific error message within it, or default to a generic one.\n if (lower.includes(\"error:\")) {\n // Try to extract the part after \"error:\"\n const parts = cleaned.split(/error:/i);\n if (parts.length > 1) {\n cleaned = parts[parts.length - 1].trim();\n }\n } else {\n cleaned = \"Codex CLI failed to respond. Please try again.\";\n }\n }\n\n const maxLength = typeof options.maxLength === \"number\" ? options.maxLength : 220;\n if (cleaned.length > maxLength) {\n cleaned = `${cleaned.slice(0, maxLength).trimEnd()}…`;\n }\n\n return cleaned;\n}\n","import { licenseService } from \"./license-service\";\nimport { ProfileManager } from \"./profile-manager\";\n\nexport async function assertProfileCreationAllowed(\n profileManager?: ProfileManager,\n): Promise<void> {\n const manager = profileManager ?? new ProfileManager();\n await manager.initialize();\n\n const license = await licenseService.getStatus();\n const profiles = await manager.listProfiles();\n const licenseWithCounts = licenseService.applyProfileCount(license, profiles.length);\n\n if (\n typeof licenseWithCounts.profileLimit === \"number\" &&\n licenseWithCounts.profilesRemaining !== null &&\n licenseWithCounts.profilesRemaining <= 0\n ) {\n throw new Error(\"CodexUse Free supports up to 2 profiles. Upgrade to CodexUse Pro for unlimited profiles.\");\n }\n}\n","import { spawn } from \"node:child_process\";\nimport { statSync } from \"node:fs\";\nimport path from \"node:path\";\n\nconst ENV_HINTS = [\"CODEX_BINARY\", \"CODEX_CLI_PATH\", \"CODEX_PATH\"] as const;\nconst NODE_MODULE_CANDIDATES = [\n [\"@openai\", \"codex\"],\n [\"@openai\", \"codex-alpha\"],\n];\n\nexport type CodexLoginMode = \"browser\" | \"device\";\n\nfunction fileExists(candidate: string | null | undefined): string | null {\n if (!candidate) return null;\n const resolved = path.resolve(candidate);\n try {\n const stat = statSync(resolved);\n if (stat.isFile()) return resolved;\n } catch {\n return null;\n }\n return null;\n}\n\nfunction resolveFromEnv(): string | null {\n for (const key of ENV_HINTS) {\n const value = process.env[key];\n if (!value) continue;\n const resolved = fileExists(value);\n if (resolved) return resolved;\n }\n return null;\n}\n\nfunction resolveFromAppResources(): string | null {\n const roots = [\n path.resolve(__dirname, \"..\"),\n path.resolve(__dirname, \"..\", \"..\"),\n ];\n\n for (const root of roots) {\n const base = path.join(root, \"app.asar.unpacked\", \"node_modules\");\n for (const segments of NODE_MODULE_CANDIDATES) {\n const candidate = fileExists(path.join(base, ...segments, \"bin\", \"codex\"));\n if (candidate) return candidate;\n const jsCandidate = fileExists(path.join(base, ...segments, \"bin\", \"codex.js\"));\n if (jsCandidate) return jsCandidate;\n }\n }\n\n return null;\n}\n\nfunction resolveFromNodeModules(): string | null {\n let current = path.resolve(__dirname, \"..\");\n let last = \"\";\n\n while (current !== last) {\n for (const segments of NODE_MODULE_CANDIDATES) {\n const candidate = fileExists(path.join(current, \"node_modules\", ...segments, \"bin\", \"codex\"));\n if (candidate) return candidate;\n const jsCandidate = fileExists(path.join(current, \"node_modules\", ...segments, \"bin\", \"codex.js\"));\n if (jsCandidate) return jsCandidate;\n }\n last = current;\n current = path.dirname(current);\n }\n\n return null;\n}\n\nfunction resolveFromPath(): string | null {\n const pathValue = process.env.PATH ?? \"\";\n const entries = pathValue.split(path.delimiter).filter(Boolean);\n const names = process.platform === \"win32\"\n ? [\"codex.exe\", \"codex.cmd\", \"codex.bat\", \"codex\"]\n : [\"codex\"];\n\n for (const entry of entries) {\n for (const name of names) {\n const candidate = fileExists(path.join(entry, name));\n if (candidate) return candidate;\n }\n }\n\n return null;\n}\n\nexport function resolveCodexBinary(): string | null {\n return (\n resolveFromEnv() ||\n resolveFromAppResources() ||\n resolveFromNodeModules() ||\n resolveFromPath()\n );\n}\n\nexport function requireCodexBinary(context?: string): string {\n const resolved = resolveCodexBinary();\n if (resolved) return resolved;\n const hint = \"Install Codex CLI (npm i -g @openai/codex) or set CODEX_BINARY.\";\n const message = context ? `${context} ${hint}` : hint;\n throw new Error(message);\n}\n\nfunction buildCodexCommand(codexPath: string, args: string[]): { command: string; args: string[]; shell: boolean } {\n const normalized = codexPath.toLowerCase();\n const isJs = normalized.endsWith(\".js\");\n if (isJs) {\n return { command: process.execPath, args: [codexPath, ...args], shell: false };\n }\n\n const useShell = process.platform === \"win32\";\n return { command: codexPath, args, shell: useShell };\n}\n\nfunction isHeadless(): boolean {\n if (process.env.CODEXUSE_HEADLESS === \"1\") return true;\n if (process.env.CI) return true;\n if (process.env.SSH_CONNECTION || process.env.SSH_TTY) return true;\n if (process.platform === \"linux\") {\n if (!process.env.DISPLAY && !process.env.WAYLAND_DISPLAY) return true;\n }\n return false;\n}\n\nexport function resolveLoginMode(preferred?: CodexLoginMode | null): CodexLoginMode {\n if (preferred === \"browser\" || preferred === \"device\") {\n return preferred;\n }\n const envMode = process.env.CODEXUSE_LOGIN_MODE;\n if (envMode === \"browser\" || envMode === \"device\") {\n return envMode;\n }\n return isHeadless() ? \"device\" : \"browser\";\n}\n\nexport async function runCodexLogin(mode?: CodexLoginMode | null): Promise<void> {\n const codexPath = requireCodexBinary(\"Codex CLI is required to login.\");\n const resolvedMode = resolveLoginMode(mode ?? null);\n const loginArgs = resolvedMode === \"device\"\n ? [\"login\", \"--device-auth\"]\n : [\"login\"];\n const { command, args, shell } = buildCodexCommand(codexPath, loginArgs);\n\n const child = spawn(command, args, {\n stdio: \"inherit\",\n env: process.env,\n shell,\n });\n\n const exitCode = await new Promise<number>((resolve) => {\n child.on(\"close\", (code) => resolve(code ?? 0));\n });\n\n if (exitCode !== 0) {\n throw new Error(`Codex CLI login failed (exit code ${exitCode}).`);\n }\n}\n","import { spawn } from \"node:child_process\";\nimport readline from \"node:readline\";\nimport { once } from \"node:events\";\nimport { promises as fs } from \"node:fs\";\nimport os from \"node:os\";\nimport path from \"node:path\";\nimport { requireCodexCli } from \"./codex-cli\";\nimport type { RateLimitSnapshot, RateLimitWindow } from \"./types\";\n\ntype RpcMessage = { id?: number; result?: unknown; error?: unknown };\n\ntype RpcRateLimitWindow = {\n usedPercent?: number;\n windowDurationMins?: number;\n resetsAt?: number;\n};\n\ntype RpcRateLimitSnapshot = {\n primary?: RpcRateLimitWindow | null;\n secondary?: RpcRateLimitWindow | null;\n};\n\nconst RPC_TIMEOUT_MS = 10_000;\n\nasync function sendPayload(\n child: ReturnType<typeof spawn>,\n payload: Record<string, unknown>,\n): Promise<void> {\n child.stdin?.write(JSON.stringify(payload));\n child.stdin?.write(\"\\n\");\n}\n\nasync function readMessage(\n rl: readline.Interface,\n timeoutMs: number,\n): Promise<RpcMessage> {\n const timeout = new Promise<never>((_, reject) => {\n const timer = setTimeout(() => {\n clearTimeout(timer);\n reject(new Error(\"codex RPC timed out\"));\n }, timeoutMs);\n });\n\n const linePromise = once(rl, \"line\").then(args => args[0] as string);\n\n const line = (await Promise.race([linePromise, timeout])) as string;\n try {\n return JSON.parse(line) as RpcMessage;\n } catch {\n throw new Error(\"codex RPC returned malformed JSON\");\n }\n}\n\nfunction toWindow(rpc?: RpcRateLimitWindow | null): RateLimitWindow | undefined {\n if (!rpc) return undefined;\n const usedPercent =\n typeof rpc.usedPercent === \"number\" && Number.isFinite(rpc.usedPercent)\n ? rpc.usedPercent\n : undefined;\n const windowMinutes =\n typeof rpc.windowDurationMins === \"number\" && Number.isFinite(rpc.windowDurationMins)\n ? rpc.windowDurationMins\n : undefined;\n\n let resetsAt: string | undefined;\n let resetsInSeconds: number | undefined;\n if (typeof rpc.resetsAt === \"number\" && Number.isFinite(rpc.resetsAt)) {\n const ms = rpc.resetsAt > 10_000_000_000 ? rpc.resetsAt : rpc.resetsAt * 1000;\n resetsAt = new Date(ms).toISOString();\n resetsInSeconds = Math.max(0, Math.round((ms - Date.now()) / 1000));\n }\n\n if (\n typeof usedPercent === \"undefined\" &&\n typeof windowMinutes === \"undefined\" &&\n typeof resetsAt === \"undefined\"\n ) {\n return undefined;\n }\n\n const window: RateLimitWindow = {};\n if (typeof usedPercent === \"number\") {\n window.usedPercent = usedPercent;\n }\n if (typeof windowMinutes === \"number\") {\n window.windowMinutes = windowMinutes;\n }\n if (typeof resetsAt === \"string\") {\n window.resetsAt = resetsAt;\n }\n if (typeof resetsInSeconds === \"number\") {\n window.resetsInSeconds = resetsInSeconds;\n }\n return window;\n}\n\nexport async function fetchRateLimitsViaRpc(\n envOverride?: NodeJS.ProcessEnv,\n options: { authPath?: string; codexPath?: string } = {},\n): Promise<RateLimitSnapshot | null> {\n const binaryPath = options.codexPath ?? await requireCodexCli();\n const tempHome = await fs.mkdtemp(path.join(os.tmpdir(), \"codex-rpc-\"));\n\n const sourceAuthPath =\n options.authPath ??\n (envOverride?.CODEX_HOME\n ? path.join(envOverride.CODEX_HOME, \"auth.json\")\n : path.join(\n envOverride?.HOME ?? process.env.HOME ?? process.env.USERPROFILE ?? os.homedir(),\n \".codex\",\n \"auth.json\",\n ));\n\n try {\n const authContent = await fs.readFile(sourceAuthPath, \"utf8\").catch(() => null);\n if (!authContent) {\n return null;\n }\n await fs.writeFile(path.join(tempHome, \"auth.json\"), authContent, \"utf8\");\n } catch {\n await fs.rm(tempHome, { recursive: true, force: true }).catch(() => {});\n return null;\n }\n\n const child = spawn(process.execPath, [binaryPath, \"-s\", \"read-only\", \"-a\", \"untrusted\", \"app-server\"], {\n stdio: [\"pipe\", \"pipe\", \"pipe\"],\n env: {\n ...process.env,\n ...(envOverride ?? {}),\n HOME: tempHome,\n USERPROFILE: tempHome,\n CODEX_HOME: tempHome,\n CODEX_TELEMETRY_LABEL: \"codex-rpc\",\n ELECTRON_RUN_AS_NODE: \"1\",\n },\n });\n\n const rl = readline.createInterface({\n input: child.stdout!,\n crlfDelay: Infinity,\n });\n\n try {\n await sendPayload(child, {\n id: 1,\n method: \"initialize\",\n params: { clientInfo: { name: \"codexuse\", version: \"0.0.0\" } },\n });\n await readMessage(rl, RPC_TIMEOUT_MS);\n\n await sendPayload(child, { method: \"initialized\", params: {} });\n\n await sendPayload(child, { id: 2, method: \"account/rateLimits/read\", params: {} });\n const message = await readMessage(rl, RPC_TIMEOUT_MS);\n const result = message.result as { rateLimits?: RpcRateLimitSnapshot } | undefined;\n const limits = result?.rateLimits;\n const primary = toWindow(limits?.primary ?? null);\n const secondary = toWindow(limits?.secondary ?? null);\n\n if (!primary && !secondary) {\n return null;\n }\n\n const snapshot: RateLimitSnapshot = {\n lastUpdated: new Date().toISOString(),\n source: \"codex-rpc\",\n };\n if (primary) {\n snapshot.primary = primary;\n }\n if (secondary) {\n snapshot.secondary = secondary;\n }\n return snapshot;\n } finally {\n child.kill();\n rl.close();\n await fs.rm(tempHome, { recursive: true, force: true }).catch(() => {});\n }\n}\n","import type { RateLimitSnapshot } from \"@/lib/types\";\n\ntype Notify = (payload: { title: string; body: string }) => void;\n\ninterface RateLimitNotifierOptions {\n notify: Notify;\n now?: () => number;\n approachThresholdPercent?: number;\n approachCooldownMs?: number;\n resetCooldownMs?: number;\n exhaustedThresholdPercent?: number;\n recoveryThresholdPercent?: number;\n}\n\nconst DEFAULT_APPROACH_THRESHOLD = 95;\nconst DEFAULT_APPROACH_COOLDOWN_MS = 30 * 60 * 1000; // 30 minutes\nconst DEFAULT_RESET_COOLDOWN_MS = 5 * 60 * 1000; // 5 minutes\nconst DEFAULT_EXHAUSTED_THRESHOLD = 98;\nconst DEFAULT_RECOVERY_THRESHOLD = 50;\nconst APPROACH_RENOTIFY_FLOOR_MS = 2 * 60 * 1000; // Prevents back-to-back alerts when windows jitter\nconst RESET_WINDOW_BUCKET_MS = 5 * 60 * 1000;\n\ninterface RateLimitState {\n lastSnapshot: RateLimitSnapshot | null;\n lastApproachWindow: string | null;\n lastApproachAt: number | null;\n lastResetAt: number | null;\n exhaustedWindow: string | null;\n}\n\nfunction pickResetsAt(snapshot: RateLimitSnapshot | null | undefined): string | null {\n if (!snapshot) {\n return null;\n }\n\n return snapshot.primary?.resetsAt ?? snapshot.secondary?.resetsAt ?? null;\n}\n\nexport function maxUsedPercent(snapshot: RateLimitSnapshot | null | undefined): number | null {\n if (!snapshot) {\n return null;\n }\n\n const candidates = [\n snapshot.primary?.usedPercent,\n snapshot.secondary?.usedPercent,\n ].filter((value): value is number => typeof value === \"number\" && Number.isFinite(value));\n\n if (candidates.length === 0) {\n return null;\n }\n\n return Math.max(...candidates);\n}\n\nfunction normalizeResetWindowKey(resetsAt: string | null | undefined): string | null {\n if (!resetsAt) {\n return null;\n }\n\n const parsed = Date.parse(resetsAt);\n if (Number.isNaN(parsed)) {\n return resetsAt;\n }\n\n const bucket = Math.floor(parsed / RESET_WINDOW_BUCKET_MS);\n return `reset-${bucket}`;\n}\n\nfunction formatResetWindow(snapshot: RateLimitSnapshot | null | undefined): string | null {\n const resetsInSeconds = snapshot?.primary?.resetsInSeconds ?? snapshot?.secondary?.resetsInSeconds ?? null;\n if (typeof resetsInSeconds !== \"number\" || Number.isNaN(resetsInSeconds)) {\n return null;\n }\n\n const minutes = Math.max(0, Math.round(resetsInSeconds / 60));\n if (minutes >= 120) {\n const hours = Math.round(minutes / 60);\n return `${hours}h`;\n }\n\n return `${minutes}m`;\n}\n\nexport class RateLimitNotifier {\n private readonly notify: Notify;\n private readonly now: () => number;\n private readonly approachThreshold: number;\n private readonly approachCooldownMs: number;\n private readonly resetCooldownMs: number;\n private readonly exhaustedThreshold: number;\n private readonly recoveryThreshold: number;\n\n private readonly states = new Map<string, RateLimitState>();\n\n constructor(options: RateLimitNotifierOptions) {\n this.notify = options.notify;\n this.now = options.now ?? (() => Date.now());\n this.approachThreshold = options.approachThresholdPercent ?? DEFAULT_APPROACH_THRESHOLD;\n this.approachCooldownMs = options.approachCooldownMs ?? DEFAULT_APPROACH_COOLDOWN_MS;\n this.resetCooldownMs = options.resetCooldownMs ?? DEFAULT_RESET_COOLDOWN_MS;\n this.exhaustedThreshold = options.exhaustedThresholdPercent ?? DEFAULT_EXHAUSTED_THRESHOLD;\n this.recoveryThreshold = options.recoveryThresholdPercent ?? DEFAULT_RECOVERY_THRESHOLD;\n }\n\n handleSnapshot(accountId: string, profileName: string, snapshot: RateLimitSnapshot | null | undefined): void {\n if (!accountId || !profileName) {\n return;\n }\n\n const state = this.states.get(accountId) ?? {\n lastSnapshot: null,\n lastApproachAt: null,\n lastApproachWindow: null,\n lastResetAt: null,\n exhaustedWindow: null,\n };\n\n const previousUsage = maxUsedPercent(state.lastSnapshot);\n const currentUsage = maxUsedPercent(snapshot);\n const previousWindow = pickResetsAt(state.lastSnapshot);\n const currentWindow = pickResetsAt(snapshot);\n const normalizedPreviousWindow = normalizeResetWindowKey(previousWindow);\n const normalizedCurrentWindow = normalizeResetWindowKey(currentWindow);\n const now = this.now();\n\n const wasExhausted =\n typeof previousUsage === \"number\" && previousUsage >= this.exhaustedThreshold;\n const currentWindowKey =\n normalizedCurrentWindow ??\n normalizedPreviousWindow ??\n currentWindow ??\n previousWindow ??\n \"unknown\";\n\n if (wasExhausted) {\n state.exhaustedWindow = normalizedPreviousWindow ?? state.exhaustedWindow ?? currentWindowKey;\n }\n\n // Reset notifications disabled - they're just noise. Nobody waits around for limits to reset.\n // Clear exhausted state silently so we can re-notify on next approach.\n if (this.shouldNotifyReset(state, currentUsage, currentWindowKey, previousWindow, now)) {\n state.lastResetAt = now;\n state.exhaustedWindow = null;\n state.lastApproachWindow = null;\n state.lastApproachAt = null;\n }\n\n if (\n typeof currentUsage === \"number\" &&\n currentUsage >= this.approachThreshold &&\n this.canNotifyApproach(state, currentWindowKey, now)\n ) {\n const windowText = formatResetWindow(snapshot);\n const body = windowText\n ? `${currentUsage}% used. Resets in ${windowText}.`\n : `${currentUsage}% used.`;\n this.notify({\n title: `${profileName} is nearing its limit`,\n body,\n });\n state.lastApproachWindow = currentWindowKey;\n state.lastApproachAt = now;\n }\n\n if (typeof currentUsage === \"number\" && currentUsage >= this.exhaustedThreshold) {\n state.exhaustedWindow = currentWindowKey;\n }\n\n state.lastSnapshot = snapshot ?? null;\n this.states.set(accountId, state);\n }\n\n private canNotifyApproach(state: RateLimitState, windowKey: string, now: number): boolean {\n if (!windowKey) {\n return false;\n }\n\n if (typeof state.lastApproachAt === \"number\" && now - state.lastApproachAt < APPROACH_RENOTIFY_FLOOR_MS) {\n return false;\n }\n\n if (state.lastApproachWindow !== windowKey) {\n return true;\n }\n\n if (typeof state.lastApproachAt !== \"number\") {\n return true;\n }\n\n return now - state.lastApproachAt >= this.approachCooldownMs;\n }\n\n private shouldNotifyReset(\n state: RateLimitState,\n currentUsage: number | null,\n currentWindowKey: string,\n previousWindow: string | null,\n now: number,\n ): boolean {\n if (!state.exhaustedWindow) {\n return false;\n }\n\n const windowChanged =\n Boolean(currentWindowKey) && currentWindowKey !== state.exhaustedWindow;\n const usageRecovered =\n typeof currentUsage === \"number\" && currentUsage <= this.recoveryThreshold && Boolean(previousWindow);\n\n if (!windowChanged && !usageRecovered) {\n return false;\n }\n\n if (typeof state.lastResetAt === \"number\" && now - state.lastResetAt < this.resetCooldownMs) {\n return false;\n }\n\n return true;\n }\n}\n","import { ProfileManager } from \"../../../lib/profile-manager\";\nimport { licenseService } from \"../../../lib/license-service\";\nimport { getCloudSyncStatus, pullCloudSync, pushCloudSync } from \"../../../lib/cloud-sync-service\";\nimport { assertProfileCreationAllowed } from \"../../../lib/license-guard\";\nimport { runCodexLogin, resolveCodexBinary, resolveLoginMode, type CodexLoginMode } from \"./codex-cli\";\nimport { fetchRateLimitsViaRpc } from \"../../../lib/codex-rpc\";\nimport { maxUsedPercent } from \"../../../lib/rate-limit-notifier\";\nimport type { OrganizationInfo, Profile, RateLimitWindow } from \"../../../lib/types\";\n\ndeclare const __CODEXUSE_CLI_VERSION__: string;\n\nconst VERSION = typeof __CODEXUSE_CLI_VERSION__ === \"string\"\n ? __CODEXUSE_CLI_VERSION__\n : \"0.0.0\";\n\nfunction printHelp(): void {\n console.log(`CodexUse CLI v${VERSION}\n\nUsage:\n codexuse profile list [--no-usage] [--compact]\n codexuse profile current\n codexuse profile add <name> [--skip-login] [--device-auth] [--login=browser|device]\n codexuse profile refresh <name> [--skip-login] [--device-auth] [--login=browser|device]\n codexuse profile switch <name>\n codexuse profile delete <name>\n codexuse profile rename <old> <new>\n\n codexuse license status [--refresh]\n codexuse license activate <license-key>\n\n codexuse sync status\n codexuse sync pull\n codexuse sync push\n\nFlags:\n -h, --help Show help\n -v, --version Show version\n --no-usage Skip rate-limit usage fetch\n --compact Names only\n --device-auth Use device auth for Codex login\n --login=MODE Login mode: browser | device\n`);\n}\n\nfunction hasFlag(args: string[], flag: string): boolean {\n return args.includes(flag);\n}\n\nfunction stripFlags(args: string[]): string[] {\n return args.filter(arg => !arg.startsWith(\"-\"));\n}\n\nfunction parseLoginMode(flags: string[]): CodexLoginMode | null {\n if (flags.includes(\"--device-auth\")) return \"device\";\n const explicit = flags.find(flag => flag.startsWith(\"--login=\"));\n if (!explicit) return null;\n const value = explicit.split(\"=\")[1];\n if (value === \"browser\" || value === \"device\") {\n return value;\n }\n return null;\n}\n\nfunction formatProfileLabel(name: string, displayName?: string | null): string {\n if (displayName && displayName.trim() && displayName !== name) {\n return `${displayName} (${name})`;\n }\n return name;\n}\n\nfunction toTitleCase(value: string): string {\n return value.replace(/\\w\\S*/g, word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase());\n}\n\nfunction formatShortDate(value?: string | null): string | null {\n if (!value) return null;\n const date = new Date(value);\n if (Number.isNaN(date.getTime())) return null;\n if (typeof Intl !== \"undefined\" && Intl.DateTimeFormat) {\n return new Intl.DateTimeFormat(undefined, { month: \"short\", day: \"numeric\" }).format(date);\n }\n return date.toLocaleDateString();\n}\n\nfunction formatRelativeTime(value?: string | null): string | null {\n if (!value) return null;\n const parsed = Date.parse(value);\n if (Number.isNaN(parsed)) return null;\n const diffMs = parsed - Date.now();\n const absMs = Math.abs(diffMs);\n if (absMs < 60_000) {\n return null;\n }\n const minutes = Math.round(absMs / 60_000);\n if (minutes < 60) {\n return diffMs >= 0 ? `in ${minutes}m` : `${minutes}m ago`;\n }\n const hours = Math.round(minutes / 60);\n if (hours < 24) {\n return diffMs >= 0 ? `in ${hours}h` : `${hours}h ago`;\n }\n const days = Math.round(hours / 24);\n return diffMs >= 0 ? `in ${days}d` : `${days}d ago`;\n}\n\nfunction formatOrganizations(organizations?: OrganizationInfo[]): string | null {\n if (!organizations || !Array.isArray(organizations) || organizations.length === 0) {\n return null;\n }\n const parts = organizations\n .map(org => {\n if (!org) return null;\n const name = org.title || org.id;\n if (!name) return null;\n return org.role ? `${name} (${org.role})` : name;\n })\n .filter((value): value is string => Boolean(value));\n return parts.length > 0 ? parts.join(\", \") : null;\n}\n\nfunction formatWindowLabel(windowMinutes?: number): string | null {\n if (typeof windowMinutes !== \"number\" || Number.isNaN(windowMinutes)) {\n return null;\n }\n if (windowMinutes >= 2880) {\n const days = windowMinutes / 1440;\n const rounded = Math.round(days * 10) / 10;\n const display = Number.isInteger(rounded) ? rounded.toFixed(0) : rounded.toFixed(1);\n return `${display.replace(/\\.0$/, \"\")}d window`;\n }\n if (windowMinutes >= 60) {\n const hours = windowMinutes / 60;\n const rounded = Math.round(hours * 10) / 10;\n const display = Number.isInteger(rounded) ? rounded.toFixed(0) : rounded.toFixed(1);\n return `${display.replace(/\\.0$/, \"\")}h window`;\n }\n const minutes = Math.round(windowMinutes);\n return `${minutes}m window`;\n}\n\nfunction formatDurationFromSeconds(seconds?: number | null): string | null {\n if (typeof seconds !== \"number\" || Number.isNaN(seconds)) {\n return null;\n }\n if (seconds <= 0) {\n return \"now\";\n }\n const minutes = Math.floor(seconds / 60);\n if (minutes === 0) {\n return `${Math.max(1, Math.round(seconds))}s`;\n }\n if (minutes < 60) {\n return `${minutes}m`;\n }\n const hours = Math.floor(minutes / 60);\n const remainingMinutes = minutes % 60;\n if (hours < 24) {\n return remainingMinutes ? `${hours}h ${remainingMinutes}m` : `${hours}h`;\n }\n const days = Math.floor(hours / 24);\n const remainingHours = hours % 24;\n return remainingHours ? `${days}d ${remainingHours}h` : `${days}d`;\n}\n\nfunction formatResetText(window: RateLimitWindow): string | null {\n if (window.resetsAt) {\n return formatRelativeTime(window.resetsAt) ?? window.resetsAt;\n }\n const durationText = formatDurationFromSeconds(window.resetsInSeconds);\n if (!durationText) return null;\n if (durationText === \"now\") return \"now\";\n return `in ${durationText}`;\n}\n\ntype RateLimitRow = {\n label: string;\n used: string;\n reset: string | null;\n};\n\nfunction formatWindowUsage(window: RateLimitWindow): RateLimitRow | null {\n const label = formatWindowLabel(window.windowMinutes) ?? \"window\";\n const usedPercent =\n typeof window.usedPercent === \"number\" && Number.isFinite(window.usedPercent)\n ? `${Math.round(Math.max(0, Math.min(100, window.usedPercent)))}%`\n : \"n/a\";\n const resetText = formatResetText(window);\n return {\n label,\n used: usedPercent,\n reset: resetText ? `reset ${resetText}` : null,\n };\n}\n\ntype UsageInfo = {\n summary: string;\n rows: RateLimitRow[] | null;\n};\n\nfunction fallbackUsage(profile: Profile): UsageInfo {\n if (!profile.isValid) {\n return { summary: \"invalid\", rows: null };\n }\n if (profile.tokenStatus?.requiresUserAction) {\n return { summary: \"auth required\", rows: null };\n }\n return { summary: \"n/a\", rows: null };\n}\n\nasync function resolveProfileUsage(\n manager: ProfileManager,\n profile: Profile,\n codexPath: string,\n): Promise<UsageInfo> {\n if (!profile.isValid || profile.tokenStatus?.requiresUserAction) {\n return fallbackUsage(profile);\n }\n\n try {\n const snapshot = await manager.runWithProfileAuth(profile.name, (env) =>\n fetchRateLimitsViaRpc(env, { codexPath }),\n );\n if (!snapshot) {\n return { summary: \"n/a\", rows: null };\n }\n const rows: RateLimitRow[] = [];\n if (snapshot.primary) {\n const row = formatWindowUsage(snapshot.primary);\n if (row) rows.push(row);\n }\n if (snapshot.secondary) {\n const row = formatWindowUsage(snapshot.secondary);\n if (row) rows.push(row);\n }\n const maxPercent = maxUsedPercent(snapshot);\n const summary = typeof maxPercent === \"number\"\n ? `${Math.round(maxPercent)}%`\n : \"n/a\";\n return { summary, rows: rows.length > 0 ? rows : null };\n } catch {\n return { summary: \"n/a\", rows: null };\n }\n}\n\nfunction formatPlan(profile: Profile): string {\n const metadata = profile.metadata;\n if (!metadata) {\n return \"No plan details\";\n }\n const parts: string[] = [];\n if (metadata.planType) {\n parts.push(toTitleCase(metadata.planType));\n }\n const until = metadata.subscription?.activeUntil;\n if (until) {\n const formatted = formatShortDate(until);\n if (formatted) {\n parts.push(`Until ${formatted}`);\n }\n }\n return parts.join(\" · \") || \"No plan details\";\n}\n\nfunction formatAuth(profile: Profile): string {\n const metadata = profile.metadata;\n const relative = formatRelativeTime(metadata?.tokenAuthTime);\n if (relative) return relative;\n if (metadata?.tokenAuthTime) return metadata.tokenAuthTime;\n return \"Not authenticated yet\";\n}\n\nfunction printProfileDetails(profile: Profile, usage?: UsageInfo | null): void {\n const email = profile.email ?? \"Unknown\";\n const plan = formatPlan(profile);\n const orgs = formatOrganizations(profile.metadata?.organizations) ?? \"No organizations\";\n const auth = formatAuth(profile);\n const workspace = profile.workspaceName ?? profile.workspaceId ?? null;\n\n const entries: Array<{ label: string; value: string }> = [\n { label: \"email\", value: email },\n { label: \"plan\", value: plan },\n { label: \"orgs\", value: orgs },\n { label: \"auth\", value: auth },\n ];\n if (workspace) {\n entries.push({ label: \"workspace\", value: workspace });\n }\n const pad = Math.max(...entries.map(entry => entry.label.length));\n for (const entry of entries) {\n console.log(` ${entry.label.padEnd(pad)} : ${entry.value}`);\n }\n\n if (usage) {\n if (usage.rows && usage.rows.length > 0) {\n const labelPad = Math.max(...usage.rows.map(row => row.label.length));\n const usedPad = Math.max(...usage.rows.map(row => row.used.length));\n console.log(` rate limits:`);\n for (const row of usage.rows) {\n const resetText = row.reset ? ` ${row.reset}` : \"\";\n console.log(` ${row.label.padEnd(labelPad)} ${row.used.padStart(usedPad)}${resetText}`);\n }\n return;\n }\n console.log(` rate limits: ${usage.summary}`);\n }\n}\n\nasync function mapWithConcurrency<T, R>(\n items: T[],\n limit: number,\n fn: (item: T, index: number) => Promise<R>,\n): Promise<R[]> {\n const results: R[] = new Array(items.length);\n const workerCount = Math.max(1, Math.min(limit, items.length));\n let nextIndex = 0;\n\n const workers = Array.from({ length: workerCount }, async () => {\n while (true) {\n const current = nextIndex++;\n if (current >= items.length) {\n return;\n }\n results[current] = await fn(items[current], current);\n }\n });\n\n await Promise.all(workers);\n return results;\n}\n\nasync function handleProfile(args: string[]): Promise<void> {\n const flags = args.filter(arg => arg.startsWith(\"-\"));\n const params = stripFlags(args);\n const sub = params[0];\n\n if (!sub || hasFlag(flags, \"--help\") || hasFlag(flags, \"-h\")) {\n printHelp();\n return;\n }\n\n const manager = new ProfileManager();\n\n switch (sub) {\n case \"list\": {\n const profiles = await manager.listProfiles();\n const current = await manager.getCurrentProfile();\n if (!profiles.length) {\n console.log(\"No profiles found.\");\n return;\n }\n const compact = hasFlag(flags, \"--compact\");\n const withUsage = !hasFlag(flags, \"--no-usage\");\n let usageMap: Map<string, UsageInfo> | null = null;\n if (withUsage) {\n usageMap = new Map();\n const codexPath = resolveCodexBinary();\n const codexAvailable = Boolean(codexPath);\n if (!codexAvailable || !codexPath) {\n for (const profile of profiles) {\n usageMap.set(profile.name, { summary: \"codex cli missing\", rows: null });\n }\n } else {\n const limitEnv = Number.parseInt(process.env.CODEXUSE_CLI_USAGE_CONCURRENCY ?? \"3\", 10);\n const limit = Number.isFinite(limitEnv) && limitEnv > 0 ? limitEnv : 3;\n const results = await mapWithConcurrency(profiles, limit, (profile) =>\n resolveProfileUsage(manager, profile, codexPath),\n );\n for (const [index, profile] of profiles.entries()) {\n usageMap.set(profile.name, results[index]);\n }\n }\n }\n for (const [index, profile] of profiles.entries()) {\n const marker = current.name === profile.name ? \"*\" : \" \";\n const label = formatProfileLabel(profile.name, profile.displayName);\n if (index > 0) {\n console.log(\"\");\n }\n const activeLabel = current.name === profile.name ? \" (active)\" : \"\";\n console.log(`${marker} ${label}${activeLabel}`);\n if (compact) continue;\n const usage = usageMap?.get(profile.name) ?? null;\n printProfileDetails(profile, usage);\n }\n return;\n }\n case \"current\": {\n const current = await manager.getCurrentProfile();\n if (!current.name) {\n console.log(\"No active profile.\");\n return;\n }\n console.log(current.name);\n return;\n }\n case \"add\": {\n const name = params[1];\n if (!name) {\n throw new Error(\"Profile name is required.\");\n }\n await assertProfileCreationAllowed(manager);\n if (!hasFlag(flags, \"--skip-login\")) {\n const loginMode = resolveLoginMode(parseLoginMode(flags));\n await runCodexLogin(loginMode);\n }\n const profile = await manager.createProfile(name);\n const label = profile ? formatProfileLabel(profile.name, profile.displayName) : name;\n console.log(`Profile created: ${label}`);\n return;\n }\n case \"refresh\": {\n const name = params[1];\n if (!name) {\n throw new Error(\"Profile name is required.\");\n }\n if (!hasFlag(flags, \"--skip-login\")) {\n const loginMode = resolveLoginMode(parseLoginMode(flags));\n await runCodexLogin(loginMode);\n }\n const profile = await manager.refreshProfileAuth(name);\n const label = formatProfileLabel(profile.name, profile.displayName);\n console.log(`Profile refreshed: ${label}`);\n return;\n }\n case \"switch\": {\n const name = params[1];\n if (!name) {\n throw new Error(\"Profile name is required.\");\n }\n await manager.switchToProfile(name);\n console.log(`Switched to profile: ${name}`);\n return;\n }\n case \"delete\": {\n const name = params[1];\n if (!name) {\n throw new Error(\"Profile name is required.\");\n }\n await manager.deleteProfile(name);\n console.log(`Profile deleted: ${name}`);\n return;\n }\n case \"rename\": {\n const from = params[1];\n const to = params[2];\n if (!from || !to) {\n throw new Error(\"Old and new profile names are required.\");\n }\n await manager.renameProfile(from, to);\n console.log(`Profile renamed: ${from} -> ${to}`);\n return;\n }\n default:\n printHelp();\n return;\n }\n}\n\nasync function handleLicense(args: string[]): Promise<void> {\n const flags = args.filter(arg => arg.startsWith(\"-\"));\n const params = stripFlags(args);\n const sub = params[0];\n\n if (!sub || hasFlag(flags, \"--help\") || hasFlag(flags, \"-h\")) {\n printHelp();\n return;\n }\n\n switch (sub) {\n case \"status\": {\n const forceRefresh = hasFlag(flags, \"--refresh\");\n const status = await licenseService.getStatus({ forceRefresh });\n console.log(`Tier: ${status.tier}`);\n console.log(`State: ${status.state}`);\n if (status.profileLimit !== null) {\n console.log(`Profile limit: ${status.profileLimit}`);\n }\n if (status.profilesRemaining !== null) {\n console.log(`Profiles remaining: ${status.profilesRemaining}`);\n }\n if (status.purchaseEmail) {\n console.log(`Email: ${status.purchaseEmail}`);\n }\n if (status.maskedLicenseKey) {\n console.log(`License: ${status.maskedLicenseKey}`);\n }\n if (status.message) {\n console.log(`Message: ${status.message}`);\n }\n if (status.error) {\n console.log(`Error: ${status.error}`);\n }\n return;\n }\n case \"activate\": {\n const key = params[1];\n if (!key) {\n throw new Error(\"License key is required.\");\n }\n const status = await licenseService.activate(key);\n console.log(status.message ?? \"License activated.\");\n return;\n }\n default:\n printHelp();\n return;\n }\n}\n\nfunction printSyncResult(result: Awaited<ReturnType<typeof pushCloudSync>>): void {\n console.log(result.message);\n if (result.localUpdatedAt) {\n console.log(`Local snapshot: ${result.localUpdatedAt}`);\n }\n if (result.remoteUpdatedAt) {\n console.log(`Remote snapshot: ${result.remoteUpdatedAt}`);\n }\n}\n\nasync function handleSync(args: string[]): Promise<void> {\n const flags = args.filter(arg => arg.startsWith(\"-\"));\n const params = stripFlags(args);\n const sub = params[0];\n\n if (!sub || hasFlag(flags, \"--help\") || hasFlag(flags, \"-h\")) {\n printHelp();\n return;\n }\n\n switch (sub) {\n case \"status\": {\n const status = await getCloudSyncStatus();\n console.log(`Endpoint: ${status.endpoint}`);\n console.log(`Can sync: ${status.canSync ? \"yes\" : \"no\"}`);\n console.log(`Has key: ${status.hasLicenseKey ? \"yes\" : \"no\"}`);\n if (status.licenseState) {\n console.log(`License state: ${status.licenseState}`);\n }\n if (status.reason) {\n console.log(`Reason: ${status.reason}`);\n }\n if (status.remoteUpdatedAt) {\n console.log(`Remote snapshot: ${status.remoteUpdatedAt}`);\n }\n if (status.lastPushAt) {\n console.log(`Last push: ${status.lastPushAt}`);\n }\n if (status.lastPullAt) {\n console.log(`Last pull: ${status.lastPullAt}`);\n }\n if (status.lastError) {\n console.log(`Last error: ${status.lastError}`);\n }\n return;\n }\n case \"pull\": {\n const result = await pullCloudSync();\n printSyncResult(result);\n if (result.status === \"error\") {\n process.exitCode = 1;\n }\n return;\n }\n case \"push\": {\n const result = await pushCloudSync();\n printSyncResult(result);\n if (result.status === \"error\") {\n process.exitCode = 1;\n }\n return;\n }\n default:\n printHelp();\n return;\n }\n}\n\nasync function main(): Promise<void> {\n const args = process.argv.slice(2);\n\n if (args.length === 0 || hasFlag(args, \"--help\") || hasFlag(args, \"-h\")) {\n printHelp();\n return;\n }\n\n if (hasFlag(args, \"--version\") || hasFlag(args, \"-v\")) {\n console.log(VERSION);\n return;\n }\n\n const command = args[0];\n const rest = args.slice(1);\n\n switch (command) {\n case \"profile\":\n await handleProfile(rest);\n return;\n case \"license\":\n await handleLicense(rest);\n return;\n case \"sync\":\n await handleSync(rest);\n return;\n default:\n printHelp();\n return;\n }\n}\n\nmain().catch((error) => {\n const message = error instanceof Error ? error.message : String(error);\n console.error(message);\n process.exitCode = 1;\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gBAA+B;AAC/B,kBAAiB;AACjB,IAAAA,eAAqB;AACrB,kBAAiC;;;ACG1B,IAAM,4BAA4B;AAClC,IAAM,sCAAsC;AAC5C,IAAM,qCAAqC;AAC3C,IAAM,wBAAwB;AAErC,SAAS,YAAY,OAAe,KAAa,KAAqB;AACpE,SAAO,KAAK,IAAI,KAAK,KAAK,IAAI,KAAK,KAAK,CAAC;AAC3C;AAMO,SAAS,0BACd,KACkB;AAClB,QAAM,UAAU,OAAO,KAAK,YAAY,YAAY,IAAI,UAAU;AAClE,QAAM,aACJ,OAAO,KAAK,qBAAqB,YAAY,OAAO,SAAS,IAAI,gBAAgB,IAC7E,IAAI,mBACJ;AACN,QAAM,YACJ,OAAO,KAAK,oBAAoB,YAAY,OAAO,SAAS,IAAI,eAAe,IAC3E,IAAI,kBACJ;AAGN,QAAM,oBAAoB,YAAY,YAAY,uBAAuB,EAAE;AAC3E,QAAM,mBAAmB,YAAY,WAAW,oBAAoB,GAAG,GAAG;AAE1E,SAAO;AAAA,IACL;AAAA,IACA,kBAAkB;AAAA,IAClB,iBAAiB;AAAA,EACnB;AACF;;;ACvCA,IAAM,iBAAiB,oBA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AAEA,KAAK,EAAE,MAAM,CAAC,UAAU;AACtB,QAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;AACrE,UAAQ,MAAM,OAAO;AACrB,UAAQ,WAAW;AACrB,CAAC;","names":["import_path","os","path","fs","asString","isRecord","asString","isRecord","asString","fs","path","import_node_crypto","import_node_fs","import_node_path","path","crypto","fs","crypto","fallbackStored","fallback","import_node_fs","import_node_path","isRecord","isRecord","import_promises","import_node_path","import_toml","import_node_path","import_node_fs","import_node_path","path","isRecord","path","path","isRecord","path","fs","import_node_child_process","import_node_fs","import_node_path","fileExists","path","stat","import_node_child_process","import_node_fs","import_node_os","import_node_path","fs","path","os","readline"]}