Skip to content

feat(init): create real app when authed in agent mode#254

Merged
wyattjoh merged 2 commits into
mainfrom
wyattjoh/agent-init-authed-link
May 1, 2026
Merged

feat(init): create real app when authed in agent mode#254
wyattjoh merged 2 commits into
mainfrom
wyattjoh/agent-init-authed-link

Conversation

@wyattjoh
Copy link
Copy Markdown
Contributor

@wyattjoh wyattjoh commented May 1, 2026

Summary

When clerk init runs in agent mode without --app and the user is already authenticated, the CLI now creates a real Clerk application named after the project and links it, instead of falling back to keyless setup. Previously an authed agent run created an unowned keyless app and wrote a deferred-claim breadcrumb, so the project ended up unlinked even though the user had logged in before the run; auto-claim only fired on a subsequent clerk auth login, which authed users had no reason to re-run. Keyless still runs in agent mode when the user is not authenticated.

The change adds a createIfMissing library option to link so the agent-mode autolink path can fall through to a deterministic create instead of the existing usage error, and threads a project-name derivation (package.json#name, --name, or directory basename) through init's authenticated branch.

Test plan

  • bun run typecheck
  • bun run lint
  • bun run test
  • Manual: clerk init --framework next --pm npm in agent mode while logged in → real app appears in the dashboard, .env.local has dev keys, clerk doctor reports linked
  • Manual: same command while not logged in → keyless flow still runs, breadcrumb still written, original deferred-claim path still works on next clerk auth login

When `clerk init` runs in agent mode without `--app` and the user is
already authenticated, create a real Clerk application named after the
project and link it instead of falling back to keyless setup. Keyless
still runs in agent mode when the user is not authenticated.

Previously an authed agent run created an unowned keyless app and wrote
a deferred-claim breadcrumb, leaving the project unlinked even though
the user had logged in before the run; auto-claim only fired on a
subsequent `clerk auth login`, which authed users had no reason to
re-run.

Adds a `createIfMissing` library option to `link` so the agent-mode
autolink path can fall through to a deterministic create instead of the
existing usage error, and threads a project-name derivation
(`package.json#name`, `--name`, or directory basename) through `init`'s
authenticated branch.
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 1, 2026

🦋 Changeset detected

Latest commit: b175cd3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
clerk Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@wyattjoh wyattjoh marked this pull request as ready for review May 1, 2026 20:19
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 1, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 96294811-95d1-416a-a6ee-15885647d03e

📥 Commits

Reviewing files that changed from the base of the PR and between bc4212c and b175cd3.

📒 Files selected for processing (3)
  • packages/cli-core/src/commands/link/index.test.ts
  • packages/cli-core/src/commands/link/index.ts
  • packages/cli-core/src/test/integration/agent-mode.test.ts

📝 Walkthrough

Walkthrough

This PR makes clerk init in agent mode create and link real Clerk applications when the runner is authenticated, otherwise preserving keyless behavior for unauthenticated runs. It adds a deriveProjectName helper and uses it from autoclaim and init flows to compute app names, introduces a createIfMissing option passed from init into link to auto-create apps, updates docs, and adjusts unit and integration tests to cover authenticated agent flows and app-creation behavior.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: creating a real app when authenticated in agent mode, which is the primary objective of this PR.
Description check ✅ Passed The description is directly related to the changeset, explaining the motivation and implementation details of the authentication-dependent app creation behavior in agent mode.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Review rate limit: 4/5 reviews remaining, refill in 12 minutes.

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@Railly Railly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed locally. Logic checks out, autolink runs first to avoid duplicates, the guard still trips when createIfMissing is missing.

A couple things worth a look:

PLAPI accepts duplicate app names. Created two apps with the same name to confirm, different IDs each time. Agents running clerk init across multiple Next default projects will fill the dashboard with "my-app" entries. Worth either disambiguating the derived name or mentioning it in the changeset.

Also poked at createAndFetchApp and the second call is redundant. The raw POST /v1/platform/applications response already includes application_id, name, and a full instances[] array with instance_id, environment_type, secret_key, publishable_key. The follow-up GET /v1/platform/applications/{id} returns the same shape minus secret_key, so it actually drops information from the create response. Doesn't break anything because link only persists the instance ID and env pull fetches keys later, but it's an extra PLAPI round-trip per authed init that buys nothing. Easy to drop the second call, or leave a comment if it's defensive.

Otherwise looks good.

The PLAPI POST /v1/platform/applications response already returns the
full SerializableStack (application_id, name, instances[] with
instance_id, environment_type, secret_key, publishable_key), so the
follow-up GET was an extra round-trip that produced an identical shape.
@wyattjoh wyattjoh requested a review from Railly May 1, 2026 21:36
@wyattjoh wyattjoh merged commit e1885f6 into main May 1, 2026
10 checks passed
@wyattjoh wyattjoh deleted the wyattjoh/agent-init-authed-link branch May 1, 2026 21:45
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.

2 participants