Skip to content

Plugin CLI commands load every core command group before they run #8174

Description

@lanjak

Summary

Every plugin CLI command (bb git, bb workflows, bb browser-automation, and other plugin commands) loads, evaluates and registers all 20 core command groups, and adds the JSON shape help, before the CLI asks the server for plugin commands. The plugin proxy then runs the command and calls process.exit, so none of that work is used. On this machine it costs about 120 ms of CPU and 40 to 60 ms of wall time on each plugin command. Agents call plugin commands in loops (for example one bb browser-automation call for each page action), so the cost adds up.

Versions and environment

  • bb from source at origin/main fb045a0.
  • Linux (CachyOS, kernel 7.3.4), Node v26.13.1.
  • CLI built with pnpm exec turbo run build --filter=@bb/cli (split bundle, as shipped).

Steps to reproduce

  1. Build the CLI: pnpm exec turbo run build --filter=@bb/cli.
  2. Start a fixture server on 127.0.0.1 that answers GET /api/v1/plugins/contributions with one plugin command fixture inspect and answers POST /api/v1/plugins/fixture-plugin/cli with {"exitCode":0,"stdout":"ran\n","stderr":""}.
  3. Run BB_CLI_REEXEC=1 BB_SERVER_URL=<fixture url> node apps/cli/dist/index.js fixture inspect x 30 times and record wall time and user+sys CPU time.
  4. Record the modules that the run loads with a Node resolve hook (the method of apps/cli/src/__tests__/startup-graph.test.ts).

Expected vs actual

Expected: a plugin command loads no core command group, because the plugin proxy handles it and exits.

Actual: the run loads all 20 core group chunks (browser-*.js, status-*.js, settings-*.js, ... voice-*.js) and json-shapes-*.js.

Timings for bb fixture inspect x, 30 runs each, two rounds:

Measure Round 1 Round 2
Wall time, median 206.5 ms 228.3 ms
CPU user+sys, median 321 ms 304 ms

For comparison, bb --version takes 32.5 ms wall time.

Evidence

  • main() loads the groups, registers them and runs addJsonShapeHelp() first, and calls tryPluginCommandProxy only after that:

    bb/apps/cli/src/index.ts

    Lines 207 to 259 in fb045a0

    async function main(): Promise<void> {
    const firstArg = process.argv[2];
    const groups = selectCommandGroups(firstArg);
    if (groups.length === 0) {
    await parseProgram(null);
    return;
    }
    const [contextEnv, ...registrars] = await Promise.all([
    import("./context-env.js"),
    ...groups.map((group) => group.load()),
    ]);
    const deps = createCommandGroupDeps(contextEnv);
    program.addHelpText("after", () => {
    const context = deps.getContext();
    const project = context.projectId ?? "<unset>";
    const thread = context.threadId ?? "<unset>";
    return `
    Current context:
    BB_PROJECT_ID: ${project}
    BB_THREAD_ID: ${thread}
    BB_SERVER_URL: ${context.serverUrl}
    Quick start:
    bb status
    bb project list
    bb thread show <id>
    bb thread spawn --project <id> --provider codex --prompt "..."
    `;
    });
    for (const register of registrars) {
    register(program, deps);
    }
    await addJsonShapeHelp();
    const candidate = pluginProxyCandidate(firstArg, KNOWN_COMMAND_NAMES);
    if (candidate !== null) {
    const { TOP_LEVEL_SOFT_ALIASES } = await import("./command-resolution.js");
    const aliasTarget = TOP_LEVEL_SOFT_ALIASES[candidate];
    await tryPluginCommandProxy(
    candidate,
    deps.getUrl,
    aliasTarget !== undefined,
    );
    if (aliasTarget !== undefined) process.argv[2] = aliasTarget;
    }
    await rejectHelpForUnknownSubcommand(deps.getUrl);
    await parseProgram(deps.getUrl);
    }
  • selectCommandGroups returns all CORE_COMMAND_GROUPS when the first argument is not a core command name, which is always true for a plugin command:
    export function selectCommandGroups(
    firstArg: string | undefined,
    ): readonly CommandGroup[] {
    if (firstArg === "--version" || firstArg === "-V") return [];
    const match = CORE_COMMAND_GROUPS.find((entry) => entry.name === firstArg);
    return match === undefined ? CORE_COMMAND_GROUPS : [match];
    }
  • tryPluginCommandProxy reads only process.argv, the server URL and the server response. It calls process.exit when a plugin owns the command:

    bb/apps/cli/src/index.ts

    Lines 48 to 112 in fb045a0

    async function tryPluginCommandProxy(
    candidate: string,
    getUrl: () => string,
    isSoftAlias: boolean,
    ): Promise<void> {
    const proxy = await import("./plugin-cli-proxy.js");
    const result = await proxy.fetchPluginCliContributions(getUrl());
    if (result.outcome === "unreachable") {
    if (isSoftAlias) return;
    const message = proxy.describeUnreachableServer(
    getUrl(),
    result.cause,
    result.lastTimeoutMs,
    result.attempts,
    );
    console.error(message);
    const { isJsonInvocation, writeCliErrorEnvelope } =
    await import("./cli-error-output.js");
    writeCliErrorEnvelope(
    { code: "server_unreachable", hint: null, message },
    isJsonInvocation(process.argv),
    );
    process.exit(1);
    }
    if (result.outcome === "invalid") return;
    const match = proxy.findPluginCliCommand(result.contributions, candidate);
    if (match === undefined) {
    const disabledId = await proxy.findDisabledPluginForCommand(
    getUrl(),
    candidate,
    );
    if (disabledId !== null) {
    console.error(
    `bb ${candidate} is provided by the "${disabledId}" plugin, which is disabled — ` +
    `run \`bb plugin enable ${disabledId}\` or enable it in Plugins.`,
    );
    process.exit(1);
    }
    return;
    }
    const argv = process.argv.slice(3);
    const command = match.commands.find((entry) => entry.name === argv[0]);
    if (
    command !== undefined &&
    match.rendersHelp !== true &&
    argv.slice(1).some((arg) => arg === "--help" || arg === "-h")
    ) {
    console.log(command.usage);
    process.exit(0);
    }
    const exitCode = await proxy.runPluginCliCommand(
    getUrl(),
    match.pluginId,
    argv,
    );
    if (exitCode !== 0) {
    await logCliError({
    code: "plugin_command_failed",
    command: proxy.pluginCommandLabel(match, argv),
    exitCode,
    token: null,
    });
    }
    process.exit(exitCode);
    }
  • Cause of the order: before perf: load bb command groups on demand and split the CLI bundle (C1) #2349, tryPluginCommandProxy built the set of known command names from program.commands, so it had to run after registration. perf: load bb command groups on demand and split the CLI bundle (C1) #2349 moved the names to the static CORE_COMMAND_GROUPS table (KNOWN_COMMAND_NAMES), but kept the old order.

Timing method: a Node script ran the built CLI 30 times for each case and took the median of process.hrtime around execFile. CPU time came from the bash time builtin (TIMEFORMAT='%U %S'). The machine had other load, so the numbers vary between rounds.

What you ruled out

  • The proxy does not depend on the registered program: it passes its own command label to the error log, and it does not call commander.
  • Core commands (bb thread --help) already load only their own group, so they are not affected.
  • bb --version loads no group and is not affected.

AGENT GENERATED

Activity

  1. added theissue type on Nov 25, 2026
  2. added
    cli
    confirmed-repro
    on Nov 25, 2026
  3. bb-slop-cop commented on Nov 25, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · Priority Medium · Effort Low; area cli, plus perf.
    • Report: Reproduction and root-cause report.
    • Verdict: REPRODUCED · root-cause confidence high · reproduction label confirmed-repro.
    • Root cause: Core command imports/registration and JSON-shape help run before plugin dispatch, although the successful plugin proxy exits without using them.
    • Verification: Two clean trusted-main checkouts at fb045a0466 each load all 20 core groups and JSON-shape help for a successful plugin command. The startup probe fails its zero-core-import assertion in both; all 8 existing startup tests pass. Exact timing savings were not measured.
    • No duplicate fix PR: Open #8175 already closes this issue. Its diff addresses the ordering cause statically; its branch was not checked out or executed.
  4. added a commit that references this issue on Nov 25, 2026
    9b67e8d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cliconfirmed-reproperf

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions