codexuse-cli 6.0.0 → 6.0.2

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 CHANGED
@@ -5175,7 +5175,19 @@ var LicenseService = class {
5175
5175
  this.verificationPromise = null;
5176
5176
  }
5177
5177
  async getCachedStatus() {
5178
- return this.getStatus({ allowStale: true, refreshInBackground: true });
5178
+ try {
5179
+ return await this.getStatus({ allowStale: true, refreshInBackground: true });
5180
+ } catch (error) {
5181
+ const message = error instanceof Error ? error.message : "License status could not be loaded.";
5182
+ console.warn("Cached license status unavailable:", error);
5183
+ return toLicenseStatus(null, {
5184
+ state: "error",
5185
+ profileLimit: null,
5186
+ profilesRemaining: null,
5187
+ message: "License status is temporarily unavailable.",
5188
+ error: message
5189
+ });
5190
+ }
5179
5191
  }
5180
5192
  async getStatus(options = {}) {
5181
5193
  const forceRefresh = Boolean(options.forceRefresh);
@@ -5661,10 +5673,15 @@ function resolveProfileCodexHomeDir(profileHomeId) {
5661
5673
  const currentExists = (0, import_node_fs3.existsSync)(current);
5662
5674
  const legacyExists = (0, import_node_fs3.existsSync)(legacy);
5663
5675
  if (currentExists && legacyExists) {
5664
- throw new Error("profile-home-location-conflict");
5676
+ return current;
5665
5677
  }
5666
5678
  return legacyExists ? legacy : current;
5667
5679
  }
5680
+ function findProfileCodexHomeLocationConflict(profileHomeId) {
5681
+ const current = resolveCurrentProfileCodexHomeDir(profileHomeId);
5682
+ const legacy = import_node_path5.default.join(resolveLegacyProfileCodexHomesRoot(), profileHomeId);
5683
+ return (0, import_node_fs3.existsSync)(current) && (0, import_node_fs3.existsSync)(legacy) ? { current, legacy } : null;
5684
+ }
5668
5685
  function resolveProfileCodexHomesRoot() {
5669
5686
  return import_node_path5.default.join(resolveHomeDir(), ".codexuse", "p");
5670
5687
  }
@@ -5688,7 +5705,7 @@ async function migrateProfileCodexHomeDir(profileHomeId) {
5688
5705
  const currentExists = (0, import_node_fs3.existsSync)(current);
5689
5706
  const legacyExists = (0, import_node_fs3.existsSync)(legacy);
5690
5707
  if (currentExists && legacyExists) {
5691
- throw new Error("profile-home-location-conflict");
5708
+ return current;
5692
5709
  }
5693
5710
  if (currentExists || !legacyExists) {
5694
5711
  return current;
@@ -36330,11 +36347,18 @@ var ProfileManager = class {
36330
36347
  }
36331
36348
  async migrateCredentialsToProfileHomes() {
36332
36349
  const source = await this.readProfilesStateSource();
36350
+ await this.reconcileProfileHomeLocationConflicts(source);
36333
36351
  const homeIdsByProfileName = /* @__PURE__ */ new Map();
36334
36352
  const profileNamesByHomeId = /* @__PURE__ */ new Map();
36335
36353
  for (const [profileName, raw] of Object.entries(source)) {
36336
- const homeId = normalizeProfileHomeId(raw.homeId);
36337
- if (!homeId) {
36354
+ let record2;
36355
+ try {
36356
+ record2 = this.fromStateRecord(profileName, raw);
36357
+ } catch {
36358
+ continue;
36359
+ }
36360
+ const homeId = record2 ? normalizeProfileHomeId(raw.homeId) : null;
36361
+ if (!record2 || !homeId) {
36338
36362
  continue;
36339
36363
  }
36340
36364
  const collisionKey = homeId.toLowerCase();
@@ -36471,6 +36495,74 @@ var ProfileManager = class {
36471
36495
  );
36472
36496
  }
36473
36497
  }
36498
+ async reconcileProfileHomeLocationConflicts(source) {
36499
+ for (const [profileName, raw] of Object.entries(source)) {
36500
+ try {
36501
+ const record2 = this.fromStateRecord(profileName, raw);
36502
+ if (!record2) {
36503
+ continue;
36504
+ }
36505
+ const conflict = findProfileCodexHomeLocationConflict(record2.homeId);
36506
+ if (!conflict) {
36507
+ continue;
36508
+ }
36509
+ const currentAuthPath = (0, import_path2.join)(conflict.current, "auth.json");
36510
+ const legacyAuthPath = (0, import_path2.join)(conflict.legacy, "auth.json");
36511
+ await this.withProfileAuthFileLock(currentAuthPath, async (currentRaw) => {
36512
+ await this.withProfileAuthFileLock(legacyAuthPath, async (legacyRaw) => {
36513
+ if (!legacyRaw?.trim()) {
36514
+ return;
36515
+ }
36516
+ let legacyAuth;
36517
+ try {
36518
+ legacyAuth = JSON.parse(legacyRaw);
36519
+ } catch (error) {
36520
+ throw errorWithCause("Legacy profile auth is invalid.", error);
36521
+ }
36522
+ if (!this.activeAuthMatchesRecord(legacyAuth, record2)) {
36523
+ throw new Error("Legacy profile auth has a different identity.");
36524
+ }
36525
+ if (!currentRaw?.trim()) {
36526
+ await this.replaceProfileAuthPathIfCurrent(
36527
+ currentAuthPath,
36528
+ currentRaw,
36529
+ legacyRaw
36530
+ );
36531
+ return;
36532
+ }
36533
+ let currentAuth;
36534
+ try {
36535
+ currentAuth = JSON.parse(currentRaw);
36536
+ } catch (error) {
36537
+ throw errorWithCause("Current profile auth is invalid.", error);
36538
+ }
36539
+ if (!this.activeAuthMatchesRecord(currentAuth, record2)) {
36540
+ throw new Error("Current profile auth has a different identity.");
36541
+ }
36542
+ const currentRecency = this.getProfileTokenRecencyMs(
36543
+ this.normalizeProfileData(currentAuth)
36544
+ );
36545
+ const legacyRecency = this.getProfileTokenRecencyMs(
36546
+ this.normalizeProfileData(legacyAuth)
36547
+ );
36548
+ if (typeof legacyRecency !== "number" || typeof currentRecency === "number" && legacyRecency <= currentRecency) {
36549
+ return;
36550
+ }
36551
+ await this.replaceProfileAuthPathIfCurrent(
36552
+ currentAuthPath,
36553
+ currentRaw,
36554
+ legacyRaw
36555
+ );
36556
+ });
36557
+ });
36558
+ } catch (error) {
36559
+ logWarn(
36560
+ `Could not reconcile the upgrade profile home for '${profileName}':`,
36561
+ error
36562
+ );
36563
+ }
36564
+ }
36565
+ }
36474
36566
  enqueueAuthSwap(task) {
36475
36567
  const run2 = globalAuthSwapLock.then(task, task);
36476
36568
  globalAuthSwapLock = run2.then(
@@ -37077,22 +37169,43 @@ var ProfileManager = class {
37077
37169
  const byIdentityWorkspace = /* @__PURE__ */ new Map();
37078
37170
  let cacheable = true;
37079
37171
  for (const [profileName, raw] of Object.entries(source)) {
37080
- const storedRecord = this.fromStateRecord(profileName, raw);
37172
+ let storedRecord;
37173
+ try {
37174
+ storedRecord = this.fromStateRecord(profileName, raw);
37175
+ } catch (error) {
37176
+ logWarn(`Failed to read saved profile '${profileName}':`, error);
37177
+ cacheable = false;
37178
+ continue;
37179
+ }
37081
37180
  if (!storedRecord) {
37082
37181
  continue;
37083
37182
  }
37084
- const hydration = await this.hydrateProfileRecordWithStatus(storedRecord);
37183
+ let hydration;
37184
+ try {
37185
+ hydration = await this.hydrateProfileRecordWithStatus(storedRecord);
37186
+ } catch (error) {
37187
+ logWarn(
37188
+ `Failed to hydrate profile '${profileName}'; using its saved metadata:`,
37189
+ error
37190
+ );
37191
+ hydration = { record: storedRecord, cacheable: false };
37192
+ }
37085
37193
  const record2 = hydration.record;
37086
37194
  cacheable = cacheable && hydration.cacheable;
37087
37195
  records.push(record2);
37088
- const identityKey = this.resolveProfileIdentityFromRecord(record2);
37089
- const workspaceId = record2.workspaceId ?? record2.data.workspace_id ?? DEFAULT_WORKSPACE_ID;
37090
- const key = this.identityWorkspaceIndexKey(identityKey, workspaceId);
37091
- const bucket = byIdentityWorkspace.get(key);
37092
- if (bucket) {
37093
- bucket.push(record2);
37094
- } else {
37095
- byIdentityWorkspace.set(key, [record2]);
37196
+ try {
37197
+ const identityKey = this.resolveProfileIdentityFromRecord(record2);
37198
+ const workspaceId = record2.workspaceId ?? record2.data.workspace_id ?? DEFAULT_WORKSPACE_ID;
37199
+ const key = this.identityWorkspaceIndexKey(identityKey, workspaceId);
37200
+ const bucket = byIdentityWorkspace.get(key);
37201
+ if (bucket) {
37202
+ bucket.push(record2);
37203
+ } else {
37204
+ byIdentityWorkspace.set(key, [record2]);
37205
+ }
37206
+ } catch (error) {
37207
+ logWarn(`Failed to index saved profile '${profileName}':`, error);
37208
+ cacheable = false;
37096
37209
  }
37097
37210
  }
37098
37211
  const index2 = { sourceFingerprint, records, byIdentityWorkspace };
@@ -37107,7 +37220,13 @@ var ProfileManager = class {
37107
37220
  if (migrationError) {
37108
37221
  return { record: record2, cacheable: false };
37109
37222
  }
37110
- const authPath = import_path.default.join(this.getProfileHomePath(record2.homeId), "auth.json");
37223
+ let authPath;
37224
+ try {
37225
+ authPath = import_path.default.join(this.getProfileHomePath(record2.homeId), "auth.json");
37226
+ } catch (error) {
37227
+ logWarn(`Failed to resolve profile home for '${record2.name}':`, error);
37228
+ return { record: record2, cacheable: false };
37229
+ }
37111
37230
  let authRaw;
37112
37231
  try {
37113
37232
  const recovered = await this.readProfileAuthFile(authPath);
@@ -38692,7 +38811,18 @@ var ProfileManager = class {
38692
38811
  await this.initialize();
38693
38812
  try {
38694
38813
  const records = await this.listProfileRecords();
38695
- return records.map((record2) => this.buildProfileFromRow(record2)).filter((profile) => !profile.authMethod || profile.authMethod === "codex-cli").sort((a, b) => {
38814
+ const profiles = [];
38815
+ for (const record2 of records) {
38816
+ try {
38817
+ const profile = this.buildProfileFromRow(record2);
38818
+ if (!profile.authMethod || profile.authMethod === "codex-cli") {
38819
+ profiles.push(profile);
38820
+ }
38821
+ } catch (error) {
38822
+ logWarn(`Failed to build saved profile '${record2.name}':`, error);
38823
+ }
38824
+ }
38825
+ return profiles.sort((a, b) => {
38696
38826
  const aLabel = a.displayName ?? a.name;
38697
38827
  const bLabel = b.displayName ?? b.name;
38698
38828
  const base2 = aLabel.localeCompare(bLabel);
@@ -38700,7 +38830,7 @@ var ProfileManager = class {
38700
38830
  });
38701
38831
  } catch (error) {
38702
38832
  logError("Error reading profiles from disk:", error);
38703
- return [];
38833
+ throw error;
38704
38834
  }
38705
38835
  }
38706
38836
  async runWithPreparedProfileHome(name, action, options = {}) {
@@ -40038,9 +40168,11 @@ async function listManagedProfiles(profileManager) {
40038
40168
  return labelOrder !== 0 ? labelOrder : left.name.localeCompare(right.name);
40039
40169
  });
40040
40170
  }
40041
- async function getCurrentManagedProfile(profileManager) {
40171
+ async function getCurrentManagedProfile(profileManager, activeCodexAuth) {
40042
40172
  const [currentOpenAiProfile, state] = await Promise.all([
40043
- profileManager.getCurrentProfile(),
40173
+ activeCodexAuth ? Promise.resolve(
40174
+ activeCodexAuth.state === "matched" && activeCodexAuth.profileName ? { name: activeCodexAuth.profileName, trusted: true } : { name: null, trusted: false }
40175
+ ) : profileManager.getCurrentProfile(),
40044
40176
  getAppState()
40045
40177
  ]);
40046
40178
  const lastProfileName = state.app.lastProfileName?.trim() || null;
@@ -44640,7 +44772,7 @@ async function ensureCliStorageReady() {
44640
44772
  }
44641
44773
 
44642
44774
  // src/app/main.ts
44643
- var VERSION = true ? "6.0.0" : "0.0.0";
44775
+ var VERSION = true ? "6.0.2" : "0.0.0";
44644
44776
  async function runCli() {
44645
44777
  const args2 = process.argv.slice(2);
44646
44778
  if (args2.length === 0) {