Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
1fcb438
test(cli-core): record the command surface as a reference fixture
wyattjoh Dec 1, 2026
903a758
refactor(cli-core): describe commands as Effect-free specs for effect…
wyattjoh Dec 1, 2026
11d958c
test(cli-core): count accumulating argParsers as repeatable in the su…
wyattjoh Dec 1, 2026
1667c76
refactor(cli-core): describe logs, api, telemetry and completion as c…
wyattjoh Dec 1, 2026
5c08163
feat(cli-core): tri-state switches, groups with actions and | in argu…
wyattjoh Dec 1, 2026
f1c8439
refactor(cli-core): describe users, impersonate, auth, open and env a…
wyattjoh Dec 1, 2026
b23677c
refactor(cli-core): describe deploy, update and switch-env as command…
wyattjoh Dec 1, 2026
d3b4fa1
refactor(cli-core): describe migrate and feedback as command specs
wyattjoh Dec 1, 2026
2b287a8
refactor(cli-core): describe security, mcp, agents and webhooks as co…
wyattjoh Dec 1, 2026
99d3541
refactor(cli-core)!: parse commands with effect/cli and drop commander
wyattjoh Dec 1, 2026
31b733b
feat(cli-core)!: render prompts with effect/cli and drop @clack/prompts
wyattjoh Dec 1, 2026
72b72b5
docs(cli-core): show repeated or comma-separated values for former va…
wyattjoh Dec 1, 2026
efb20a8
docs(changeset): rebuild command parsing and prompts on Effect's CLI …
wyattjoh Dec 1, 2026
6e104fd
perf(cli-core): answer --help and --version without loading Effect
wyattjoh Dec 1, 2026
e32198e
fix(cli-core): read short clusters, switches and --mode as before
wyattjoh Dec 1, 2026
45f9a84
fix(cli-core): keep mcp_client_not_supported for an unknown --client
wyattjoh Dec 1, 2026
031b077
test(cli-core): diff command-line parsing against the Commander build
wyattjoh Dec 1, 2026
4c031e1
test(cli-core): cover the live terminal key reader
wyattjoh Dec 1, 2026
e968631
test(cli-core): pin the help and version path outside Effect
wyattjoh Dec 1, 2026
45c0fef
docs(cli-core): update rules and comments for the new parser
wyattjoh Dec 1, 2026
4e16338
docs(cli-core): drop two more stale parser references
wyattjoh Dec 1, 2026
f831496
docs(changeset): describe help placement and values starting with -v
wyattjoh Dec 1, 2026
f6897b0
fix(cli-core): keep --input-json values attached to their flags
wyattjoh Dec 1, 2026
abd01fa
fix(cli-core): split short clusters in shell completion too
wyattjoh Dec 1, 2026
38f0cf8
test(cli-core): pin the live terminal input defaults
wyattjoh Dec 1, 2026
10f706a
test(cli-core): add an argv corpus recorder and more command lines
wyattjoh Dec 1, 2026
c195385
test(cli-core): move a trailing import to the top
wyattjoh Dec 1, 2026
f48bac0
docs(cli-core): fix stale parser wording and boundary notes
wyattjoh Dec 1, 2026
3bd77b1
docs(changeset): note --input-json values and help edge cases
wyattjoh Dec 1, 2026
7ed8d0f
test(cli-core): share the prompt test platform and terminal setup
wyattjoh Dec 1, 2026
52bad15
test(cli-core): force the mode with the shared fixture in init tests
wyattjoh Dec 1, 2026
1faa1fb
test(cli-core): trim the integration harness and completion tests
wyattjoh Dec 1, 2026
5b39433
style(cli-core): restore blank lines between tests
wyattjoh Dec 1, 2026
0517e9d
build(deps): bump effect, @effect/platform-bun and @effect/vitest to …
wyattjoh Dec 1, 2026
be7fe6d
fix(cli-core): give the terminals the size stream effect 4.1 requires
wyattjoh Dec 1, 2026
a7df719
fix(cli-core): cast the forwarded method to HttpMethod for effect 4.1
wyattjoh Dec 1, 2026
835d36a
test(cli-core): match effect 4.1's file info and spawn command types
wyattjoh Dec 1, 2026
3f7ca4b
refactor(cli-core): silence effect/cli through a CliOutput presenter
wyattjoh Dec 1, 2026
39ec63e
refactor(cli-core): keep switches off the next word with Flag.withOpt…
wyattjoh Dec 1, 2026
d1b94ec
fix(cli-core): keep a repeated --version printing the version on effe…
wyattjoh Dec 1, 2026
32ff6e2
docs(changeset): add an empty changeset for the effect 4.1.7 upgrade
wyattjoh Dec 1, 2026
b514fe3
fix(cli-core): keep repeated --version=<v> and --help=<v> from erroring
wyattjoh Dec 1, 2026
f9583a6
fix(cli-core): reject --=<x> as an unrecognized flag again
wyattjoh Dec 1, 2026
bf00bf5
test(cli-core): keep negated and short switches off the next word
wyattjoh Dec 1, 2026
75461ba
refactor(cli-core): share the fixed terminal size stream
wyattjoh Dec 1, 2026
bf28d8f
docs(changeset): describe the effect 4.1.7 upgrade as a patch
wyattjoh Dec 1, 2026
12c5898
fix(cli-core): treat every spelling of --help and --version as one flag
wyattjoh Dec 1, 2026
a13dffa
refactor(cli-core): name a fixed terminal size once
wyattjoh Dec 1, 2026
6a4ea5f
test(e2e): run fixture tooling on Node instead of the bun --bun shim
wyattjoh Dec 1, 2026
db755df
test(e2e): run the CLI with stdin from /dev/null
wyattjoh Dec 1, 2026
5f6be21
test(e2e): keep vitest's worker env vars out of fixture tooling
wyattjoh Dec 1, 2026
7c8ccda
test(e2e): rerun a fixture setup the Platform API throttled
wyattjoh Dec 1, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/effect-4-1-7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"clerk": patch
---

Update the CLI's internal runtime. Commands, flags and exit codes behave as before.
12 changes: 12 additions & 0 deletions .changeset/effect-rewrite-cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"clerk": major
---

Rebuild command parsing and interactive prompts on Effect's CLI toolkit, with these breaking changes:

- A malformed command line (unknown flag or subcommand, missing value, value outside the allowed choices) now exits 2 with a usage error, printed as the `usage_error` JSON envelope in agent mode, instead of exiting 1 with plain text. The error messages are worded differently.
- `-h` or `--help` anywhere before `--` prints help for the most specific command named, even after `--version` or where an option's value was expected. A value that starts with `-v` (`-d "-v …"`) now reaches its option instead of printing the version, and an `--input-json` value always reaches its option, whatever it looks like. `clerk -hv` prints help, and `clerk help mcp run` fails with the removed-command error, as `clerk mcp run` does.
- `--keys` (`clerk config pull`, `clerk config schema`), `--for` (`clerk enable billing`, `clerk disable billing`) and `--android-fingerprint` (`clerk init`, `clerk doctor`) take one value per flag: repeat the flag or separate values with commas. `--keys a b` no longer works.
- Command groups no longer have a `help` subcommand (`clerk migrate help`). Use `clerk help <command>` or `--help`.
- Prompts look and respond differently: confirm takes y or n, multi-select adds select-all and invert rows, and an invalid answer is corrected in place.
- Pressing Ctrl-C while a spinner is running now exits 130 instead of 0, so scripts see the interruption.
114 changes: 54 additions & 60 deletions .claude/rules/command-registration.md
Original file line number Diff line number Diff line change
@@ -1,103 +1,97 @@
---
description: Command registration conventions — every command group registers via register<Name>(program) from its index.ts, listed in the registrants array
description: Command registration conventions — every command group exports a command spec from its index.ts, listed in COMMANDS
paths:
- "packages/cli-core/src/cli.ts"
- "packages/cli-core/src/cli-program.ts"
- "packages/cli-core/src/commands/*/index.ts"
- "packages/cli-core/src/startup/**"
- "packages/cli-core/src/lib/command-parser.ts"
- "packages/cli-core/src/test/surface/**"
- "packages/cli-core/src/startup-boundary.test.ts"
- "packages/cli-core/src/test/lib/startup-boundary.ts"
alwaysApply: false
---

Every command group is wired into the root program through a **registrant function**, never inline in `createProgram()`.
Every command group is described by a **command spec**, plain data exported from the group's `index.ts`, never built inline in `createProgram()`. Specs are parsed and dispatched by `effect/cli`, loaded only once a command line is parsed.

## The pattern

1. Each command group exports `register<Name>(program: Program): void` from `packages/cli-core/src/commands/<name>/index.ts`. It builds the whole `program.command("<name>")` subtree (options, arguments, `.setExamples()`, subcommands) and wires each `.action()` to the handler functions in sibling files.
2. `cli-program.ts` imports that function and adds it to the `registrants: CommandRegistrant[]` array. `createProgram()` only configures the root program + global hooks, then loops `for (const register of registrants) register(program)`.
1. Each command group exports `<name>Command`, built with `command(name, { … })` from `packages/cli-core/src/startup/command-spec.ts`: its description, arguments, options, examples, subcommands, and an `action` that loads and calls the handler.
2. `cli-program.ts` imports the spec and adds it to the `COMMANDS` array. `createProgram()` returns the root spec: the global options, `COMMANDS`, and `clerk help`.

**Do not** build a command tree inline inside `createProgram()`. If you're adding a `program.command(...)` (or `webhooks`-style group) directly in `cli-program.ts`, stop — move it to a `register<Name>` in the group's `index.ts` and append the function to `registrants` instead.
**Do not** build a command tree inline inside `createProgram()`. A new group gets its own `index.ts` spec, appended to `COMMANDS`.

## `index.ts` shape

```ts
import type { Program } from "../../cli-program.ts";

export function registerApps(program: Program): void {
const apps = program.command("apps").description("Manage your Clerk applications");

apps
.command("list")
.description("List your Clerk applications")
.option("--json", "Output as JSON")
.setExamples([{ command: "clerk apps list", description: "List all applications" }])
.action(async (options) => {
// Loaded on demand so other commands don't pay Effect's startup cost.
const { list } = await import("./list.ts");
await list(options);
});

apps
.command("create")
.argument("<name>", "Application name")
.action(async (name: string, options) => {
// Loaded on demand so other commands don't pay Effect's startup cost.
const { create } = await import("./create.ts");
await create(name, options);
});
}
import { argument, command, option } from "../../startup/command-spec.ts";

export const appsCommand = command("apps", {
description: "Manage your Clerk applications",
subcommands: [
command("list", {
description: "List your Clerk applications",
options: { json: option("--json", "Output as JSON") },
examples: [{ command: "clerk apps list", description: "List all applications" }],
action: async (_args, options) => {
// Loaded on demand so other commands don't pay Effect's startup cost.
const { list } = await import("./list.ts");
await list(options);
},
}),
command("create", {
description: "Create a new application",
arguments: { name: argument("<name>", "Application name") },
action: async ({ name }, options) => {
const { create } = await import("./create.ts");
await create(name, options);
},
}),
],
});
```

- Import the `Program` type from `../../cli-program.ts` (type-only — no runtime cycle, this is the established pattern).
- Keep handler _logic_ in sibling files (`list.ts`, `create.ts`, …); `index.ts` is wiring only. A type-only import from a handler file (e.g. `import type { UsersOpenOptions } from "./open.ts"`) is fine for typing an action's options, because it is erased.
- `option(flags, description, config?)` takes Commander-style flags (`"-o, --output <path>"`, `"--json"`, `"--no-browser"`); the option's key in `options` is the name the action reads. `config` sets `choices`, `default`, `hidden`. `repeatableOption` collects every occurrence into an array (`--type a --type b`); `parsedOption(flags, description, parse)` validates and converts the value with a parser from `startup/option-parsers.ts`. A flag takes one value per occurrence: there are no variadic options.
- Declaring `--x` and `--no-x` on the same command gives one tri-state value under `--x`'s key: `true`, `false`, or `undefined` when neither is passed.
- `argument("<name>")` is required, `"[name]"` optional, `"[names...]"` takes the rest of the positionals.
- `aliases`, `hidden`, `defaultSubcommand` (the subcommand a bare group runs), `passThrough` (accepts flags and arguments it doesn't declare), `summary` (the one-liner `clerk --help` shows) and `helpText` (appended to `--help`) cover the rest.
- An action receives `(args, options, context)`: `context.path` is the command path and `context.options` the merged options of the command, every group above it, and the globals. A `before(context)` on a spec runs ahead of every action at or below it.
- Keep handler _logic_ in sibling files (`list.ts`, `create.ts`, …); `index.ts` is the spec only. A type-only import from a handler file is fine for typing an action's options, because it is erased.

## Parsing

`runProgram` in `cli-program.ts` runs `expandInputJson`, then `prepareArgv` (`startup/argv.ts`: canonical aliases, the default subcommand, `--` after a pass-through command, last-wins for a repeated flag, and the telemetry flag names), then `parseAndRun` from `lib/command-parser.ts`, which builds the `effect/cli` tree from the specs and runs the action. The outcome comes back as data: help (rendered by `startup/help-format.ts`), the version, a bare group (its help on stderr, exit 1), a usage error (exit 2, the `usage_error` envelope in agent mode), or the action's failure for the global handler. The startup work around each action (telemetry, `--verbose`, `--mode`, the environment and its banner; the update notice after) is `actionHooks` in `cli-program.ts`.

## Handlers load lazily

Every command runs `createProgram()`, so whatever a registrant imports statically loads before even `clerk --version` can answer. Every handler runs on Effect, and Effect's modules add about 12 ms to that, so a handler is always loaded with a dynamic `import()` inside its action, never imported at the top of `index.ts`. What `index.ts` may import statically is what registration itself needs: `@commander-js/extra-typings` helpers, option parsers (`startup/option-parsers.ts`), and the constants that feed `.choices()` (`FRAMEWORK_NAMES` in `startup/framework.ts`, `KEYLESS_TEMPLATES` in `startup/keyless.ts`, `PACKAGE_MANAGERS` in `startup/package-manager.ts`).
Every command runs `createProgram()`, so whatever a spec imports statically loads before even `clerk --version` can answer. Every handler runs on Effect, and Effect's modules add about 12 ms to that, so a handler is always loaded with a dynamic `import()` inside its action, never imported at the top of `index.ts`. What `index.ts` may import statically is what the spec itself needs: `startup/command-spec.ts`, option parsers (`startup/option-parsers.ts`), and the constants that feed `choices` (`FRAMEWORK_NAMES` in `startup/framework.ts`, `KEYLESS_TEMPLATES` in `startup/keyless.ts`, `PACKAGE_MANAGERS` in `startup/package-manager.ts`).

The handler's promise adapter (`list(options)` above) is the only thing an action calls: it runs the command's Effect program with `runCliEffect(program, MainLayer)`. One command reusing another calls the other's program, not its adapter, since it is already on Effect (autoclaim runs `envPullProgram`; `link` runs the login program; `init` and `doctor --fix` run the login, link, and env pull programs; `migrate import` runs the login and link programs).

## The startup boundary

The code that runs before an action is the startup boundary, declared in `src/test/lib/startup-boundary.ts`:

- `src/startup/`, the Effect-free modules `cli-program.ts` loads: the global error handler and the `startup/failure.ts` data it reads, `startup/telemetry.ts` and its `loggedFetch` send, the post-command `startup/update-check.ts` notice, `startup/config.ts` and `startup/environment.ts` for the `preAction` hook, `startup/input-json.ts`, and the clack renderers (`startup/prompts.ts`, `startup/spinner.ts`, `startup/ui.ts`)
- `src/startup/`, the Effect-free modules `cli-program.ts` loads: the spec DSL, `argv.ts` and the help formatter, the global error handler and the `startup/failure.ts` data it reads, `startup/telemetry.ts` and its `loggedFetch` send, the post-command `startup/update-check.ts` notice, `startup/config.ts` and `startup/environment.ts` for the action hooks, and `startup/input-json.ts`
- the entry points `cli.ts` (with its `__complete` fast path) and `cli-program.ts`
- each `commands/<name>/index.ts` registrant
- `REGISTRATION_HELPERS`, the few command-local files a registrant needs to build its options (the completion scripts, the MCP client table, the migrate sources)
- each `commands/<name>/index.ts` spec
- `REGISTRATION_HELPERS`, the few command-local files a spec needs to build its options (the completion scripts, the MCP client table, the migrate sources)

Two tests hold it closed:

- `startup-boundary.test.ts` scans the static imports of every module inside the boundary and fails on one that resolves outside it, or that names `effect` or an `@effect/*` package. A dynamic `import()` and an `import type` are allowed.
- `cli-program.subprocess.test.ts` runs `createProgram()` and the `__complete` path in a child process and fails if any module outside the boundary, or any `effect` / `@effect/*` module, has loaded.
- `cli-program.subprocess.test.ts` runs `createProgram()`, the `__complete` path, `--version` and `--help` in child processes and fails if any module outside the boundary, or any `effect` / `@effect/*` module, has loaded.

Because `commands/<name>/index.ts` is inside the boundary by name, never use `index.ts` for a command's handler module: name it after the command (`orgs/orgs.ts`, not `orgs/index.ts`). When a registrant genuinely needs another Effect-free file at registration, add it to `REGISTRATION_HELPERS`; anything a handler shares with startup belongs in `startup/`. Effect services live in `lib/`, and several share a base name with a `startup/` module (`lib/log.ts` is the `Log` service, `startup/log.ts` the plain `log`), so a registrant importing `../../lib/log.ts` fails the boundary check. See [errors.md](./errors.md) for how a tagged failure reaches the handler as plain data.
Because `commands/<name>/index.ts` is inside the boundary by name, never use `index.ts` for a command's handler module: name it after the command (`orgs/orgs.ts`, not `orgs/index.ts`). When a spec genuinely needs another Effect-free file, add it to `REGISTRATION_HELPERS`; anything a handler shares with startup belongs in `startup/`. Effect services live in `lib/`, and several share a base name with a `startup/` module (`lib/log.ts` is the `Log` service, `startup/log.ts` the plain `log`), so a spec importing `../../lib/log.ts` fails the boundary check. See [errors.md](./errors.md) for how a tagged failure reaches the handler as plain data.

## `cli-program.ts` shape
## The surface fixture

```ts
import { registerApps } from "./commands/apps/index.ts";
// …
const registrants: CommandRegistrant[] = [
registerInit,
registerApps,
// … one entry per command group, in display order …
registerExtras,
];

export function createProgram(): Program {
const program = new Command() /* … global options … */ as Program;
program.hook("preAction" /* … */);
for (const register of registrants) register(program);
return program;
}
```
`src/test/surface/surface.json` records the CLI's surface (commands, flags, completions, parse-error exits) as it was under Commander. `surface.test.ts`, `surface.subprocess.test.ts` and `spec-surface.test.ts` diff the live surface against it; every difference must be listed, with the reason, in `breaking-changes.json`. Never re-record `surface.json`; `record.ts <out.json>` writes the live surface elsewhere for comparison.

Helpers used by only one group (e.g. `createOption`, `parseIntegerOption`, `getAuthToken`) belong in that group's `index.ts`, not imported into `cli-program.ts`.
The tree diff doesn't see how a line is parsed. `argv-corpus.subprocess.test.ts` runs the command lines in `argv-corpus.ts` and diffs exit code and error code against `argv-corpus.json`, recorded from the Commander build; list an intended difference, with its reason, in the test's `CHANGED` table. After adding a line to the corpus, re-record with `record-argv-corpus.ts <commander-binary>`, which only accepts a binary built from commit `d22a4ce0`.

## Groups with global options, an optional group-level hook, and subcommands
## Groups with shared options and subcommands

Build the group exactly as above and attach its concerns inside the same `register<Name>`: parent `.option(...)` flags inherited via `optsWithGlobals()`, an optional group `.hook("preAction", …)` for shared gating (e.g. auth), and one `.command(...)` per subcommand. See `commands/users/index.ts` (`registerUsers`) for a multi-subcommand group with a handler-map and options inherited via `optsWithGlobals()`. A group only needs a `preAction` gate when its subcommands require shared setup; auth-free groups like `commands/webhooks/index.ts` (`registerWebhooks`) — `token` (relay token generator), `listen` (relay tunnel), and `verify` (offline HMAC) — omit it entirely.
A group's own `options` are shared with its subcommands: they parse before or after the subcommand, and the subcommand's action reads them from `context.options`. A group that needs shared setup (e.g. auth) puts it in the group's `before`. See `commands/users/index.ts` for a group with its own action, shared options and subcommands; auth-free groups like `commands/webhooks/index.ts` (`token`, `listen`, `verify`) need no `before`.

Related: [commands.md](./commands.md) (per-command directory + README + agent-mode rules) and [completion.md](./completion.md) (keep `.choices()` / `__complete.ts` in sync when adding commands or options).
Related: [commands.md](./commands.md) (per-command directory + README + agent-mode rules) and [completion.md](./completion.md) (keep `choices` / `__complete.ts` in sync when adding commands or options).
6 changes: 3 additions & 3 deletions .claude/rules/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ When creating or modifying a command, evaluate whether it needs an agent mode. C

## Shell completion

When adding or modifying a command, ensure tab-completion stays correct. See `.claude/rules/completion.md` for the full guide. Short version: use `.choices()` on arguments and options so completions are automatic; only touch `__complete.ts` when `.choices()` is not suitable.
When adding or modifying a command, ensure tab-completion stays correct. See `.claude/rules/completion.md` for the full guide. Short version: use the `choices` config on arguments and options so completions are automatic; only touch `__complete.ts` when `choices` is not suitable.

## Shipping a command behind `CLERK_EXPERIMENTAL`

A command can merge to `main`, and reach `@canary` and releases, before it's ready, behind a name in `CLERK_EXPERIMENTAL`. `src/lib/experimental.ts` holds the gate: `isExperimentEnabled(name)` and `requireExperiment(name)`. `clerk migrate` and `clerk logs` shipped this way, one slice per PR.
A command can merge to `main`, and reach `@canary` and releases, before it's ready, behind a name in `CLERK_EXPERIMENTAL`. `src/startup/experimental.ts` holds the gate: `isExperimentEnabled(name)` and `requireExperiment(name)`. `clerk migrate` and `clerk logs` shipped this way, one slice per PR.

To gate a command:

1. In its `register<Name>`, check `isExperimentEnabled(name, env)` before building the tree. When it's off, register a hidden stub instead, and return: `program.command(name, { hidden: true })` with `.helpOption(false)`, `.allowUnknownOption()`, `.allowExcessArguments()`, `.argument("[args...]")`, and an action that calls `requireExperiment(name, env)`. Override the stub's `helpInformation` to call `requireExperiment` too, so `clerk help <name>` refuses. The stub has no subcommands, so it stays out of `clerk --help` and completion, and every invocation exits 2 with `experiment_disabled`, naming the variable to set.
1. In its `index.ts`, export a hidden stub in place of the full spec when `isExperimentEnabled(name, env)` is off: `command(name, { hidden: true, passThrough: true, arguments: { args: argument("[args...]", "") }, action: () => requireExperiment(name, env) })`. A pass-through command hands `--help` to its action too, so `clerk <name> --help` refuses, and `clerk help <name>` runs a hidden pass-through command's action for the same reason. The stub has no subcommands, so it stays out of `clerk --help` and completion, and every invocation exits 2 with `experiment_disabled`, naming the variable to set.
2. Set `CLERK_EXPERIMENTAL=<name>` in the command's tests and E2E runs.
3. Say so in its README and every changeset: the command is experimental and needs `CLERK_EXPERIMENTAL=<name>`.

Expand Down
Loading
Loading