Skip to content

fix(claude-env.sh): capture op stderr to surface actual 1P errors#12

Open
busla wants to merge 1 commit intomainfrom
bash-1p-debug
Open

fix(claude-env.sh): capture op stderr to surface actual 1P errors#12
busla wants to merge 1 commit intomainfrom
bash-1p-debug

Conversation

@busla
Copy link
Copy Markdown
Contributor

@busla busla commented May 8, 2026

Why

After PR #10 merged (active legacy-wrapper removal), some macOS users still see:

ERROR: Failed to retrieve API key from 1Password
Warning: Could not retrieve Claude API key

…even after manually removing legacy wrappers. The error has no diagnostic infoop ... 2>/dev/null discards op's actual error message, so we have to guess at the cause.

What

Captures op's stderr to a temp file (mirror of PR #11 for PowerShell) and surfaces it in the failure block. Same pattern, parallel to the Windows fix.

Before

ERROR: Failed to retrieve API key from 1Password
Warning: Could not retrieve Claude API key

After

ERROR: Failed to retrieve API key from 1Password
  account: aproorg.1password.eu
  paths tried (with op stderr):
    - op://Employee/<item>/<project>
        [ERROR] 2026/05/08 ... error initializing client: found no accounts for filter "..."
    - op://Employee/<item>/API Key
        [ERROR] 2026/05/08 ... error initializing client: found no accounts for filter "..."
  Common fixes:
    - Sign in if session expired:    op signin --account aproorg.1password.eu
    - List items in vault to verify path:
                                     op item list --vault Employee --account aproorg.1password.eu
    - OP_ITEM in ~/.config/claude/local.env should be op://<Vault>/<Item>
      (the wrapper appends /<project> and /API Key to look up fields)
    - Field name is case-sensitive: 'API Key' (capital K) for the default field
Warning: Could not retrieve Claude API key

Verification

  • ✅ Syntax check (bash -n claude-env.sh)
  • ✅ Unit-tested with deliberately bogus OP_ACCOUNT and OP_ITEM — diagnostic block correctly surfaces op's "found no accounts" error
  • ✅ Successful path unchanged (only failure path gains the diagnostic block)

What this is not

This doesn't fix any specific 1P retrieval bug — it makes all of them self-diagnose. After this lands, the next user to hit the issue will see exactly what op is complaining about (account mismatch / expired session / wrong path / field name), and can either fix their setup or paste the diagnostic block for a real fix.

🤖 Generated with Claude Code

Until now, when 1Password retrieval failed, users saw a generic:

  ERROR: Failed to retrieve API key from 1Password
  Warning: Could not retrieve Claude API key

…with no diagnostic info — `2>/dev/null` discarded op's actual error
message. Real-world causes the user couldn't distinguish from this:

  - account mismatch (--account aproorg.1password.eu but signed into a
    different account → "found no accounts for filter ...")
  - expired op session (1Password app locked → "you are not signed in")
  - wrong OP_ITEM shape (full field reference instead of op://Vault/Item
    → "could not find item")
  - field name case mismatch (item has "API key" with lowercase k vs
    the wrapper's hardcoded "API Key")

Each had identical symptoms. This change makes the error self-diagnose:

  - Capture op's stderr to a temp file (mktemp) instead of discarding.
  - Track each attempted path with the corresponding stderr lines.
  - On final failure, print the full block: account, each path tried,
    op's actual error per path, and a list of common remediations
    (op signin, op item list, OP_ITEM shape rules, field-name case).

The temp file is reused (truncated with `: >`) between the project-key
lookup and the API-Key fallback so we get fresh stderr per attempt.

Verified with bogus values: the diagnostic block correctly surfaces
op's real error (e.g. 'error initializing client: found no accounts
for filter ...') making the cause obvious without further debugging.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant