Skip to content

core: CLI flags read through unchecked casts accept bare, non-integer and unknown enum values #1352

Closed

Description

@schlessera

Part of #1341.

Context

Delivered prerequisite outputs: #1351 (merged in #1470), which made brain list --limit strict through parseListLimit (packages/core/src/cli/commands/list.ts:23-30) and the shared listLimitSchema (packages/core/src/lib/ops/read.ts:160). That pattern is the one to follow here.

How the parser actually behaves. Flags is Record<string, string | boolean> (packages/core/src/cli/io.ts:31). A flag followed by a value stores that string; a flag with no value, or followed by another --flag, stores true; a repeated flag overwrites the earlier value, so the last occurrence wins (packages/core/src/cli/io.ts:112-117). Repeated flags never become arrays, and this issue does not change that.

So the real defect is that 20 sites read a flag as flags.x as string (or an enum or integer) without checking it is a string, and parse integers and enums without validating them. On a6b945b, grep -rnE 'flags(\.[a-zA-Z]+|\["[^"]+"\]) as ' packages/core/src/cli finds 11 in search.ts, 4 in list.ts, 3 in add.ts and 1 each in briefing.ts, context.ts and read.ts. The concrete malformed cases:

  • brain search --limit is parseInt(flags.limit as string, 10) with no check (packages/core/src/cli/commands/search.ts:74), bound into LIMIT ? (packages/core/src/lib/search-engine.ts:434). --limit abc fails inside SQLite with datatype mismatch; --limit -5 binds and means "no limit"; --limit 1.5 and --limit 10abc are partially parsed.
  • brain search --mode is cast, not validated (search.ts:65). An unknown mode runs neither lane (packages/core/src/lib/search-engine.ts:851-862), so a typo returns zero results with no warning. --sort and the date flags are already validated before use (search.ts:47-52); their casts only need to move to the accessor.
  • A bare string flag (brain search --type, brain list --tag --status open) stores true, which then flows on as if it were a string.
  • brain context --max-tokens uses the same unchecked parseInt (packages/core/src/cli/commands/context.ts:27). brain briefing --limit falls back with || 15 (packages/core/src/cli/commands/briefing.ts:329), so malformed input silently becomes 15.

MCP brain_search validates mode with z.enum(["fts","vector","hybrid"]) (packages/core/src/mcp-server.ts:207) and clamps limit to 1..MAX_SEARCH_LIMIT (50) (packages/core/src/mcp-server.ts:242). This issue does not change MCP.

Scope

  • Add a small typed flag accessor in packages/core/src/cli (string / positive integer / enum) that throws UsageError for a non-string value, an integer that is not a full integer literal in range, or a value outside the enum. Follow parseListLimit's full-string integer parsing.
  • Use it at every flag-cast site found by the grep above.
  • Intended bounds: search --limit and context --max-tokens and briefing --limit are whole numbers ≥ 1; the CLI keeps having no upper bound on search --limit (no new cap). search --mode accepts fts, vector, hybrid; search --sort accepts the members of SEARCH_SORTS.

Out of scope

Acceptance criteria

  • grep -rnE 'flags(\.[a-zA-Z]+|\["[^"]+"\]) as ' packages/core/src/cli returns nothing.
  • Real CLI tests, each failing before the change, show a usage error (exit 1, human and --json modes) without running a query for: brain search q --limit abc, --limit 0, --limit -5, --limit 1.5, --limit 10abc; brain search q --mode bogus; brain search q --type (bare); brain context q --max-tokens abc; brain briefing --limit abc.
  • brain search q --sort bogus and a malformed --updated-since keep their existing usage errors.
  • brain search q --limit 5 --limit 7 still behaves as --limit 7, proved by a test.
  • Valid inputs keep their current results: brain search q --limit 50, --mode fts, and every existing search/list/add/read/context/briefing test passes unchanged.
  • A mutation that removes the integer check from the accessor fails the --limit -5 test, recorded in the PR.

Verification

grep -rnE 'flags(\.[a-zA-Z]+|\["[^"]+"\]) as ' packages/core/src/cli
bun run test packages/core
bun run typecheck

Contract impact

CLI-only: malformed input that previously crashed, partially parsed or silently returned nothing now exits with a usage error. Following #1351's precedent this is a documented CLI behavior change: contract label, CONTRACT: commit, the integration contract updated in the same commit, and a minor changeset naming the newly refused inputs. No valid input changes meaning, MCP schemas are untouched, and no new upper bound is introduced; if implementation finds a currently valid input that would be refused, stop and raise it on the issue instead.

2026-10-24: rewrote around the parser's actual behavior (repeated flags overwrite; they never become arrays), folded in the 2026-10-22 search limit/mode findings as named malformed cases with bounds, kept MCP clamping out of scope, and added contract per #1351's precedent.

Activity

  1. added
    type: fix
    area: core
    priority: p3
    agent-ready
    on Oct 9, 2026
  2. schlessera commented on Oct 10, 2026

    @schlessera
    OwnerAuthor

    Its listed blocker #1351 is closed (merged in #1470): brain list --limit is now validated by the shared listLimitSchema in packages/core/src/lib/ops/read.ts, so this issue's flag audit can treat --limit on list as done.

  3. schlessera commented on Oct 21, 2026

    @schlessera
    OwnerAuthor

    New evidence from a repository review on 2026-10-22, at main dcf5341aa. The current flag-cast count and two runtime consequences:

    • The cast count is now 20, not 19. grep -rnE 'flags(\.[a-zA-Z]+|\["[^"]+"\]) as ' packages/core/src/cli finds 11 in search.ts, 4 in list.ts, 3 in add.ts, and 1 each in briefing.ts, context.ts and read.ts.
    • brain search --limit abc fails inside SQLite, not as a usage error. packages/core/src/cli/commands/search.ts:74 passes parseInt(flags.limit as string, 10) straight into SearchOptions.limit, and the FTS lane binds it into LIMIT ? (packages/core/src/lib/search-engine.ts:429). bun:sqlite rejects a NaN bind with datatype mismatch. A negative value binds and means "no limit": LIMIT -5 returns every row. Both were probed directly against bun:sqlite. MCP brain_search clamps to 1..MAX_SEARCH_LIMIT (packages/core/src/mcp-server.ts:240), and brain list already refuses bad limits through parseListLimit (packages/core/src/cli/commands/list.ts:23-30). So search is the remaining CLI/MCP drift on limits.
    • brain search --mode <anything> is cast, not validated (packages/core/src/cli/commands/search.ts:65). hybridSearch runs neither lane for an unknown mode (packages/core/src/lib/search-engine.ts:844,855), so a typo returns zero results with no warning. MCP uses z.enum(["fts","vector","hybrid"]) (packages/core/src/mcp-server.ts:205). This conclusion comes from reading the source. It was not run.
    • context.ts:27 (--max-tokens) has the same parseInt pattern. briefing.ts:329 falls back with || 15, so it does not leak NaN.

    Suggested extra acceptance criteria: brain search --limit abc, --limit 0 and --limit -5 each exit with a usage error; brain search --mode bogus exits with a usage error. Each should come with a test that fails before the change. The integer accessor should share listLimitSchema-style bounds with the MCP side.

  4. changed the title [-]core: validate the 19 CLI flags read with an unchecked string cast[/-] [+]core: CLI flags read through unchecked casts accept bare, non-integer and unknown enum values[/+] on Oct 24, 2026
  5. added
    contract
    on Oct 24, 2026
  6. schlessera commented on Oct 24, 2026

    @schlessera
    OwnerAuthor

    2026-10-24: body rewritten. The original premise that a repeated flag becomes an array is wrong for this parser: Flags is Record<string, string | boolean> and a repeated flag overwrites the earlier value (packages/core/src/cli/io.ts:31, :112-117). The acceptance criteria now name the concrete malformed cases from the 2026-10-22 comment (search --limit/--mode, bare string flags, context --max-tokens, briefing --limit) and state the bounds. contract added following #1351's precedent, where tightening CLI input handling was treated as contract work. MCP clamping is explicitly out of scope.

    Removed body line, kept as history: "Updated 2026-10-12: removed completed blocker declarations, retained delivered-output context and all remaining prerequisites; readiness and existing rulings preserved."

  7. self-assigned this
    on Nov 2, 2026
  8. schlessera commented on Nov 2, 2026

    @schlessera
    OwnerAuthor

    Claimed (claim-1341-1352-637ac0ec) as part of the #1341 integration lane. Branch fix/1352-cli-flag-accessor, targeting the epic integration branch epic/1341-shared-core-ops; the epic branch merges into main in one go once every ready child has landed there. Collision report: open PR #3288 also edits packages/core/src/cli/io.ts; whichever lands second rebases.

  9. added this to the 0.45.0 milestone on Nov 3, 2026
  10. schlessera commented on Nov 3, 2026

    @schlessera
    OwnerAuthor

    Delivered on main by the epic #1341 landing, #3505: merge commit 06473ef31, parents 559fe7b41 (main) and bd2decac7 (epic tip). This issue's change is #3292, squashed into the epic branch as 0238a1835 after passing ready proof on its exact head against the epic tip. The acceptance criteria and failing-first/mutation receipts are in that PR. Milestone 0.45.0. Closing manually: closing keywords do not fire for PRs that target a non-default branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

agent-readyarea: corecontractpriority: p3type: fix

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions