Repository navigation
Description
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 --limitisparseInt(flags.limit as string, 10)with no check (packages/core/src/cli/commands/search.ts:74), bound intoLIMIT ?(packages/core/src/lib/search-engine.ts:434).--limit abcfails inside SQLite withdatatype mismatch;--limit -5binds and means "no limit";--limit 1.5and--limit 10abcare partially parsed.brain search --modeis 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.--sortand 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) storestrue, which then flows on as if it were a string. brain context --max-tokensuses the same uncheckedparseInt(packages/core/src/cli/commands/context.ts:27).brain briefing --limitfalls 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 throwsUsageErrorfor a non-string value, an integer that is not a full integer literal in range, or a value outside the enum. FollowparseListLimit's full-string integer parsing. - Use it at every flag-cast site found by the grep above.
- Intended bounds:
search --limitandcontext --max-tokensandbriefing --limitare whole numbers ≥ 1; the CLI keeps having no upper bound onsearch --limit(no new cap).search --modeacceptsfts,vector,hybrid;search --sortaccepts the members ofSEARCH_SORTS.
Out of scope
- Changing how repeated flags behave (last wins) or making a repeated flag an error.
- Changing which flags exist, their defaults or documented meaning.
- MCP
brain_search's clamping policy, or aligning the CLI's search-limit cap with MCP. Either is a separate decision. brain list --limit, already done by core: brain list --limit accepts non-numbers and has no cap while brain_list clamps to 1-100 #1351.
Acceptance criteria
-
grep -rnE 'flags(\.[a-zA-Z]+|\["[^"]+"\]) as ' packages/core/src/clireturns nothing. - Real CLI tests, each failing before the change, show a usage error (exit 1, human and
--jsonmodes) 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 bogusand a malformed--updated-sincekeep their existing usage errors. -
brain search q --limit 5 --limit 7still 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 -5test, recorded in the PR.
Verification
grep -rnE 'flags(\.[a-zA-Z]+|\["[^"]+"\]) as ' packages/core/src/cli
bun run test packages/core
bun run typecheckContract 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
schlessera commentedon Oct 10, 2026
Its listed blocker #1351 is closed (merged in #1470):
brain list --limitis now validated by the sharedlistLimitSchemainpackages/core/src/lib/ops/read.ts, so this issue's flag audit can treat--limitonlistas done.schlessera commentedon Oct 21, 2026
New evidence from a repository review on 2026-10-22, at main
dcf5341aa. The current flag-cast count and two runtime consequences:grep -rnE 'flags(\.[a-zA-Z]+|\["[^"]+"\]) as ' packages/core/src/clifinds 11 insearch.ts, 4 inlist.ts, 3 inadd.ts, and 1 each inbriefing.ts,context.tsandread.ts.brain search --limit abcfails inside SQLite, not as a usage error.packages/core/src/cli/commands/search.ts:74passesparseInt(flags.limit as string, 10)straight intoSearchOptions.limit, and the FTS lane binds it intoLIMIT ?(packages/core/src/lib/search-engine.ts:429). bun:sqlite rejects a NaN bind withdatatype mismatch. A negative value binds and means "no limit":LIMIT -5returns every row. Both were probed directly against bun:sqlite. MCPbrain_searchclamps to 1..MAX_SEARCH_LIMIT(packages/core/src/mcp-server.ts:240), andbrain listalready refuses bad limits throughparseListLimit(packages/core/src/cli/commands/list.ts:23-30). Sosearchis the remaining CLI/MCP drift on limits.brain search --mode <anything>is cast, not validated (packages/core/src/cli/commands/search.ts:65).hybridSearchruns 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 usesz.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 sameparseIntpattern.briefing.ts:329falls back with|| 15, so it does not leak NaN.Suggested extra acceptance criteria:
brain search --limit abc,--limit 0and--limit -5each exit with a usage error;brain search --mode bogusexits with a usage error. Each should come with a test that fails before the change. The integer accessor should sharelistLimitSchema-style bounds with the MCP side.[-]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[/+]schlessera commentedon Oct 24, 2026
2026-10-24: body rewritten. The original premise that a repeated flag becomes an array is wrong for this parser:
FlagsisRecord<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.contractadded 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."
schlessera commentedon Nov 2, 2026
Claimed (claim-1341-1352-637ac0ec) as part of the #1341 integration lane. Branch
fix/1352-cli-flag-accessor, targeting the epic integration branchepic/1341-shared-core-ops; the epic branch merges intomainin one go once every ready child has landed there. Collision report: open PR #3288 also editspackages/core/src/cli/io.ts; whichever lands second rebases.CONTRACT: fix(core): refuse malformed CLI flag values with a usage er…
schlessera commentedon Nov 3, 2026
Delivered on
mainby the epic #1341 landing, #3505: merge commit06473ef31, parents559fe7b41(main) andbd2decac7(epic tip). This issue's change is #3292, squashed into the epic branch as0238a1835after passing readyproofon 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.