chorus icon indicating copy to clipboard operation
chorus copied to clipboard

cli-precheck: also probe Keychain service "Claude Code" (no -credentials suffix) for API-key / Console OAuth auth

Open brendangkchan opened this issue 6 months ago • 1 comments

First — thanks for landing the Keychain fallback in #7. That made Claude OAuth Just Work for me on a fresh install and it's a nice quality-of-life touch.

I think I've run into a sibling case that the current probe doesn't quite cover.

The case

Claude Code v2.1.140 appears to use two different macOS Keychain services depending on which auth flow created the credential:

Auth flow Keychain service
Pro/Max OAuth via claude login Claude Code-credentials ✅ (covered by #7)
API-key auth (and, in my experience, some Console-account OAuth flows) Claude Code (no suffix)

Chorus's cli-precheck.js currently only checks the suffixed name (dist/lib/cli-precheck.js, lines 89–91):

const KEYCHAIN_SERVICES = {
    anthropic: 'Claude Code-credentials',
};

So for users on API-key auth or whose Console-account login wrote to the suffixless service, the precheck returns auth_missing and every Claude spawn short-circuits with:

[cli_warning] anthropic CLI is not logged in (no credential file found).

…even though claude itself works fine in the same shell.

Reproduction

# Show the entry exists with the no-suffix name
security find-generic-password -s "Claude Code" -a "$USER" >/dev/null && echo "yes"

# Show the suffixed name is absent
security find-generic-password -s "Claude Code-credentials" -a "$USER" 2>&1 | head -1
# => "security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain."

# Show claude itself is happy
echo "hi" | claude -p --output-format text

# Show chorus disagrees
# (any persona / chat invocation now fails with auth_missing for anthropic)

I verified the entry was written by claude itself by inspecting the keychain attributes — cdat timestamp matched my login times and acct was my user.

Proposed fix

Same shape as #7's fix — accept multiple candidate service names and probe each:

const KEYCHAIN_SERVICES = {
    anthropic: ['Claude Code-credentials', 'Claude Code'],
};

function hasKeychainEntry(lineage) {
    if (process.platform !== 'darwin') return false;
    const raw = KEYCHAIN_SERVICES[lineage];
    if (!raw) return false;
    const services = Array.isArray(raw) ? raw : [raw];
    for (const service of services) {
        try {
            execFileSync('security', ['find-generic-password', '-s', service], {
                stdio: 'ignore',
                timeout: 5000,
            });
            return true;
        } catch { /* try next */ }
    }
    return false;
}

Same trade-offs as #7's fix (existence-only, no -w, no ACL prompt, ~50ms subprocess, guarded by process.platform === 'darwin'). Backwards-compatible with current OAuth users since the suffixed name is probed first.

A note on what "Claude Code" (no suffix) actually stores

I don't think this is publicly documented. Empirically, on a fresh install on macOS, this entry seems to land there when:

  1. The user has an Anthropic API key configured and Claude Code v2 saves it to the keychain (vs. requiring it via ANTHROPIC_API_KEY env each session).
  2. The user runs claude login and binds to an organization/workspace that Claude Code routes through the Console API flow rather than the Pro-subscription OAuth flow.

Either way, the keychain row exists, claude itself works, and only the chorus precheck is fooled. I have no insight into whether Anthropic intends to consolidate these — if they do, the fix becomes harmless rather than necessary.

Environment

  • chorus-codes v0.8.35
  • Claude Code v2.1.140 (the same binary at ~/.local/share/fnm/node-versions/v24.15.0/installation/lib/node_modules/@anthropic-ai/claude-code/...)
  • macOS, Darwin / arm64

Workarounds (in case anyone else hits this before a fix lands)

  • Run mkdir -p ~/.claude && echo '{}' > ~/.claude/.credentials.json && chmod 600 ~/.claude/.credentials.json — chorus's file probe is existence-only, so a non-empty stub passes precheck. claude itself ignores this file when the keychain entry exists.
  • Or switch to a Pro/Max OAuth account whose login writes to Claude Code-credentials.

Thanks again for the project!

brendangkchan avatar May 13 '26 15:05 brendangkchan

Fixed in v0.8.37 (PR #42 — merged to main). hasKeychainEntry now probes both Claude Code-credentials (OAuth) and Claude Code (API-key + Console flows). Short-circuits on first match, so the common authenticated case is still a single Keychain call.

Tests cover all three branches: suffixed-only present, suffixless-only present, both absent. Thanks for the clear repro.

chorus-codes avatar May 15 '26 06:05 chorus-codes