fix(cli): treat owned app add duplicates as success

Merged
RI
2mo ago
+342-20

Summary (AI generated)

  • Make app add idempotent when POST /app returns a duplicate-app error but the current API key can already read that app.
  • Detect duplicates via HTTP 409, app_id_already_exists, or App ID already exists response bodies.
  • Verify ownership with GET /app/ before succeeding; still throw when the app ID is globally taken.
  • Emit CLI Recovered App Already Exists analytics instead of App Created on the recovery path.
  • Add test:app-add-exists unit coverage and update the app-add source contract test.

Motivation (AI generated)

PostHog Capgo project 22029 shows recurring CLI exceptions over the last 7 days:

  • Message: Could not add app app_id_already_exists | App ID already exists
  • Impact: ~29 unique users running app add / onboarding twice (or init then add) currently crash instead of continuing.

This follows the interactive-recovery pattern from #3184: do not convert the duplicate-owned case to CliUserError just to hide PostHog. Real failures stay tracked; this is a genuine success path when the app already exists for the caller's API key.

Business Impact (AI generated)

  • Users who re-run app add for an app they already own can continue onboarding/upload flows without a hard stop.
  • PostHog keeps capturing true failures (taken app IDs, RBAC, validation) while recovery events provide a success funnel for idempotent adds.
  • Reduces support friction and abandoned onboarding for repeat CLI runs.

Test Plan (AI generated)

  • bun run test:app-add-exists
  • bun run test:app-created-source
  • bun run lint in cli/
  • bun run build in cli/
  • CI cli test suite

Generated with AI

Open in Web Open in Cursor 

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

Summary by CodeRabbit

  • Bug Fixes

    • App creation now detects duplicate app IDs from service responses and error messages.
    • Existing apps you own can be recovered with a clear success message.
    • App IDs already claimed by another organization are rejected with a distinct message.
    • Unrelated creation and permission errors continue to be reported appropriately.
  • Tests

    • Added coverage for duplicate detection, ownership verification, organization lookup, recovery, and error handling.