fix(cli): pre-load lazy commands so short flags scope correctly (#1791.2) (#1803)

`ruflo hive-mind task -d "smoke"` was silently rejecting the short flag:

  $ ruflo hive-mind task -d "smoke"
  [ERROR] Task description is required

even though `taskCommand` defines `{ name: 'description', short: 'd' }`.

Root cause is the lazy-command path in the parser. When the parser walks
argv in pass 1 and encounters a name registered via `registerLazyCommandName`
(hive-mind, neural, performance, security, ruvector, …), it `break`s out
without resolving subcommands — by design, since the lazy module hasn't
been imported yet. That means `buildScopedAliases()` only ever sees the
parent's options, and the subcommand's short flags never make it into
the alias map. `-d` falls through to global resolution, gets stored as
`flags.d = "smoke"`, and the action handler reading `flags.description`
sees `undefined`.

Fix: in `CLI.run()`, before calling `parser.parse()`, eagerly load any
lazy command that appears as the first non-flag positional, then
`registerCommand()` it as a normal command. Parse then sees the full
subcommand tree and scopes flags correctly.

Cost: lazy commands now load on first use rather than after the parser
identifies them as the dispatch target — but they would have loaded
moments later anyway, so the wall-clock impact is essentially zero.
Commands the user doesn't invoke still don't load.

Adds `parser.isLazyOnly(name)` helper so the CLI can ask the question
without reaching into private state.

Closes #1791 (sub-bug 2).
This commit is contained in:
rUv authored and GitHub committed 2026-05-06 16:52:51 -04:00
1 parent 48da302b3b
commit 7ebdaf580b
2 files changed
+28

No files matched your search

+16
View File
@@ -83,6 +83,22 @@ export class CLI {
*/
async run(args: string[] = process.argv.slice(2)): Promise<void> {
try {
// #1791.2 — If the user invoked a lazy command (e.g. `hive-mind task`),
// pre-load it BEFORE parsing so the parser can build scoped flag
// aliases for its subcommands. Without this, short flags defined on
// the lazy command's subcommand options (`-d` for description, etc.)
// never get into the alias map and silently fall through to global
// resolution — the user sees `[ERROR] Task description is required`
// even though they passed `-d "smoke"`.
for (const arg of args) {
if (arg.startsWith('-')) continue;
if (this.parser.isLazyOnly(arg)) {
const cmd = await getCommandAsync(arg);
if (cmd) this.parser.registerCommand(cmd);
}
break; // only the first non-flag positional is the command name
}
// Parse arguments
const parseResult = this.parser.parse(args);
const { command: commandPath, flags, positional } = parseResult;
+12
View File
@@ -119,6 +119,18 @@ export class CommandParser {
this.lazyCommandNames.add(name);
}
/**
* #1791.2 — true when `name` is a lazy command that hasn't been promoted
* to a fully registered Command yet. The CLI uses this to eagerly load
* the module before parsing so its subcommand flags (e.g. `-d` for
* `hive-mind task --description`) are scoped into the alias map. Without
* this, lazy commands' short flags silently fall through to global
* resolution and the action handler sees an empty `flags.description`.
*/
isLazyOnly(name: string): boolean {
return this.lazyCommandNames.has(name) && !this.commands.has(name);
}
private isKnownCommandName(name: string): boolean {
return this.commands.has(name) || this.lazyCommandNames.has(name);
}