fix(cli): treat existing channel as success on channel add

Merged
RI
2mo ago
+188-4

Summary (AI generated)

  • Make channel add idempotent when POST /channel returns a unique_name_app_id duplicate error but the current API key can already read that channel.
  • Verify readability via PostgREST findChannel (same path as console/preview keys) before succeeding.
  • Emit CLI Recovered Channel Already Exists analytics instead of Create channel on the recovery path.
  • Add test:channel-add-exists unit coverage for duplicate detection and ownership verification.

Motivation (AI generated)

PostHog Capgo project 22029 shows recurring CLI exceptions for duplicate channel creation:

  • Issue: 019feae0-980e-7443-b2b7-3fc76c463a6e (generic non-2xx; duplicate constraint split across fingerprints)
  • Symptom: capgo init already treats unique_name_app_id as recoverable (isChannelAlreadyExistsError + "use existing" in cli/src/init/channel-conflict.ts), but bare channel add throws on any createChannel error, so CI re-runs and repeat CLI calls die on duplicate channels.

This follows the success-path pattern from #3186 (app add idempotency): real failures stay tracked in PostHog; this is a genuine success path when the channel already exists for the caller's API key.

Contrast with #3179: that PR only reclassifies 4xx errors as CliUserError to silence PostHog — it does not recover. Do not merge #3179 for this use case.

Business Impact (AI generated)

  • Users and CI pipelines that re-run channel add for an existing channel can continue without a hard stop.
  • PostHog keeps capturing true failures (RBAC, 5xx, network, inaccessible duplicates) while recovery events provide a success funnel for idempotent adds.
  • Aligns channel add behavior with capgo init and upcoming app-add idempotency (#3186).

Test Plan (AI generated)

  • bun run test:channel-add-exists
  • bun run test:init-app-conflict
  • 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

    • Improved channel creation handling when a channel already exists.
    • Readable duplicates are recovered successfully.
    • Inaccessible duplicates now show a specific error.
    • Access-check failures are reported separately.
    • API error messages are formatted more clearly when details are available.
  • Tests

    • Added coverage for readable, inaccessible, and non-duplicate channel creation outcomes.
    • Added validation for invalid access-check results and formatted conflict errors.