Skip to content

fix: route pre-agent PostHog API 401s through AuthErrorScreen#477

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-pre-agent-auth-error-ux
Draft

fix: route pre-agent PostHog API 401s through AuthErrorScreen#477
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-pre-agent-auth-error-ux

Conversation

@posthog
Copy link
Copy Markdown

@posthog posthog Bot commented May 26, 2026

Problem

A 401 from PostHog's /api/projects/... (raised before the Claude Code agent starts) propagates through getOrAskForProjectDatarunProgrambin.ts and aborts opaquely:

  • CI mode shows a generic Something went wrong: Authentication failed while trying to fetch project data (bin.ts:862-879).
  • TUI mode silently swallows it unless DEBUG is set (bin.ts:739-743) — the process exits cleanly with no message.

The AuthErrorScreen flow added in #432 only catches the Claude Code SDK's 401 text inside agent-interface.ts:1156-1181, so PostHog-API 401s raised earlier in the pipeline bypass it entirely.

Surfaced by error tracking issue 019e619a-ebf7-7780-ab9c-68de2f59a4f9 (16 occurrences, single developer's workbench — zero real-user impact, but the gap is real for anyone hitting an expired or wrong-region personal API key).

Changes

  • New handleProjectDataAuthError helper in src/utils/setup-utils.ts that mirrors the SDK-side 401 handler: routes through getUI().showAuthError({ hasSettingsConflict: false, logFilePath: getLogFilePath() }) then wizardAbort({ message: 'Authentication failed (401)', error }). Non-401 errors pass through so the generic error path renders.
  • runProgram in src/lib/agent/agent-runner.ts and the skipAgent branch in bin.ts now wrap getOrAskForProjectData in a try/catch that calls the helper before re-throwing.
  • The TUI runWizard catch in bin.ts no longer silently exits — it always prints a short error message and exits 1, with the full stack still gated behind DEBUG / POSTHOG_WIZARD_DEBUG.
  • hasSettingsConflict: false is correct here: a PostHog-API 401 is never caused by a Claude Code settings.json conflict, so the screen shows the PAT / scope / region / region-mismatch checklist instead of the "log out of Claude" guidance.

Test plan

  • pnpm build — passes
  • pnpm test — 647 pass, 1 pre-existing flake in provision-cli.test.ts:141 reproduces identically on clean main (unrelated timing issue, not introduced by this change)
  • pnpm fix — no new lint errors
  • New unit tests in src/utils/__tests__/handle-project-data-auth-error.test.ts cover the 401 / non-401-status / non-ApiError branches (3/3 pass)

Created with PostHog Code

A 401 from `fetchProjectData` (raised before the Claude Code agent boots)
propagated unhandled — CI mode rendered a generic "Something went wrong"
and the TUI silently exited unless DEBUG was set, bypassing the
AuthErrorScreen flow added in #432 for SDK-side 401s.

Catch `ApiError` with `statusCode === 401` around both
`getOrAskForProjectData` call sites and route through the same
`showAuthError` + `wizardAbort` flow used by the SDK-side handler in
`agent-interface.ts`. Also surface unexpected errors from the TUI catch
in `bin.ts` instead of swallowing them when DEBUG is off.

Generated-By: PostHog Code
Task-Id: d476f6aa-7a61-4799-8821-37d442cde0fe
@github-actions
Copy link
Copy Markdown

🧙 Wizard CI

Run the Wizard CI and test your changes against wizard-workbench example apps by replying with a GitHub comment using one of the following commands:

Test all apps:

  • /wizard-ci all

Test all apps in a directory:

  • /wizard-ci basic-integration
  • /wizard-ci misc
  • /wizard-ci revenue

Test an individual app:

  • /wizard-ci basic-integration/android
  • /wizard-ci basic-integration/angular
  • /wizard-ci basic-integration/astro
Show more apps
  • /wizard-ci basic-integration/django
  • /wizard-ci basic-integration/fastapi
  • /wizard-ci basic-integration/flask
  • /wizard-ci basic-integration/javascript-node
  • /wizard-ci basic-integration/javascript-web
  • /wizard-ci basic-integration/laravel
  • /wizard-ci basic-integration/next-js
  • /wizard-ci basic-integration/nuxt
  • /wizard-ci basic-integration/python
  • /wizard-ci basic-integration/rails
  • /wizard-ci basic-integration/react-native
  • /wizard-ci basic-integration/react-router
  • /wizard-ci basic-integration/sveltekit
  • /wizard-ci basic-integration/swift
  • /wizard-ci basic-integration/tanstack-router
  • /wizard-ci basic-integration/tanstack-start
  • /wizard-ci basic-integration/vue
  • /wizard-ci misc/quack-quack
  • /wizard-ci revenue/stripe

Results will be posted here when complete.

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.

0 participants