stablyai/orca · error · Error

Codex reset-credit attempt state is inconsistent.

Error message

Codex reset-credit attempt state is inconsistent.

What it means

Thrown by getPendingResetAttemptForAccount when an idempotency key is present in unresolvedResetKeyByAccountScope but the corresponding attempt in resetAttemptsByKey is NOT in the 'providerPending' state. This is an internal invariant violation: the two maps must agree — a key is only tracked as 'unresolved' while its attempt is providerPending. It signals the reset-credit attempt bookkeeping fell out of sync, almost always due to ledger corruption or a bug in the persist/hydrate path rather than caller error.

Source

Thrown at src/main/codex-accounts/service.ts:451

    })
  }

  private getPendingResetAttemptForAccount(
    target: RateLimitRuntimeTarget,
    account: CodexManagedAccount
  ): { idempotencyKey: string; expectedScope: CodexResetCreditExpectedScope } | null {
    const accountScopeKey = resetAccountScopeKey({
      target,
      accountId: account.id,
      accountRevision: account.updatedAt
    })
    const idempotencyKey = this.unresolvedResetKeyByAccountScope.get(accountScopeKey)
    if (!idempotencyKey) {
      return null
    }
    const attempt = this.resetAttemptsByKey.get(idempotencyKey)
    if (attempt?.state !== 'providerPending') {
      throw new Error('Codex reset-credit attempt state is inconsistent.')
    }
    // Why: a durable providerPending attempt can only be resolved with its original key.
    return { idempotencyKey, expectedScope: attempt.expectedScope }
  }

  private hasPendingResetForTarget(target: RateLimitRuntimeTarget): boolean {
    return [...this.resetAttemptsByKey.values()].some(
      (attempt) =>
        attempt.state === 'providerPending' &&
        sameRateLimitTarget(attempt.expectedScope.target, target)
    )
  }

  private startResetCreditAttempt(
    idempotencyKey: string,
    expectedScope: CodexResetCreditExpectedScope,
    attempt: CodexResetCreditAttempt
  ): Promise<CodexResetCreditConsumeResult> {

View on GitHub (pinned to 1136503c6a)

Solutions

  1. Treat as a bug, not a usage error: collect the ledger contents and resetAttemptsByKey/unresolvedResetKeyByAccountScope state and report it.
  2. Clear/reset the corrupt durable ledger so hydrateResetCreditAttempts rebuilds a consistent in-memory state (note this loses pending-attempt reconciliation for genuinely in-flight resets).
  3. Audit recent changes to persistResetAttempt (service.ts:638), the promise settle handlers (service.ts:525), and discardResetAttemptsForRemovedAccount (service.ts:671) for a map that is mutated without its pair.
  4. Add a regression test asserting unresolvedResetKeyByAccountScope only ever contains keys whose attempts are providerPending.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await service.consumeCurrentRateLimitResetCredit()
} catch (error) {
  if (error instanceof Error && error.message === 'Codex reset-credit attempt state is inconsistent.') {
    // internal invariant violation — report and offer to reset the ledger
    reportInvariantViolation(error)
    return
  }
  throw error
}

Prevention

When it happens

Trigger: Reached only via consumeCurrentRateLimitResetCredit (service.ts:386) when an account is selected and getPendingResetAttemptForAccount is called. Fires when unresolvedResetKeyByAccountScope holds a key whose attempt state is 'fresh' or 'settled' (or is missing entirely from resetAttemptsByKey).

Common situations: Manual edits to the persisted CodexResetCreditAttemptLedger, a partially-failed persistResetAttempt that updated one map but not the other, or a version-skew between app releases that changed the state model while old ledger entries were rehydrated.

Related errors


AI-assisted analysis of stablyai/orca@1136503c6a (2026-08-12). Data as JSON: /api/errors/7e1cecbdb9a31381. Report an issue: GitHub.