Skip to content

feat(cli): reject unknown flags on every command, from a declared surface - #494

Merged
sandstream merged 1 commit into
mainfrom
feat/488-flag-floor
Aug 20, 2026
Merged

sandstream merged 1 commit into
mainfrom
feat/488-flag-floor

Conversation

@sandstream

Copy link
Copy Markdown
Owner

Closes #488.

What

Every command verb now rejects flags it does not accept — 71/71, from 2/45.

The shape is the one read-only-surface.ts already established for the write surface: a declared table plus one refusal at dispatch. Sweeping 43 handlers would have fixed today and rotted tomorrow; the defect #488 names is that nothing enumerated which flags a command accepts, so "did we cover everything?" had no answer.

$ kit status --bogus-flag
unknown flag for kit status: --bogus-flag
accepted: --attest --json --mode --no-auto-install --non-interactive --read-only --readonly --env --help --version
$ echo $?
1

The table is generated, not hand-written

#488 warned that an allowlist from the wrong source is its own outage — CHECK_FLAGS was built from commands/check.ts's own literals and rejected kit check --attest and --no-auto-install, both documented, both read one import away in cli-checks-shared.ts.

So src/flag-surface.ts is emitted by scripts/derive-command-flags.mjs, which unions, per verb:

  • every --flag literal in the command's own module — needed because commands/security.ts reads --only / --skip / --exclude through a local commaList(name) helper, which a reader-only scan missed;
  • the literals its direct imports hand to an argv reader (hasFlag / flagValue / flagInt / includes / indexOf / === / startsWith) — depth 1, which is exactly where the --attest regression lived;
  • every flag docs/COMMANDS.md documents for that verb;
  • one hand-maintained COMPAT_FLAGS entry: agent-config --install-gate, which commands/agent.ts states "is still accepted for backward compatibility" and which nothing reads — so no scan could find it, and a scan-only floor would have started rejecting an invocation kit's own generated banner still prints.

GLOBAL_FLAGS are unioned in by the guard rather than listed per verb, so a global can never be missing from one — the omission that made kit check --read-only exit 1 without running. Everything after -- passes through untouched, so kit run -- pnpm test --watch is unaffected.

The sets err toward accepting: a flag kit only passes to a subprocess can appear. Over-accepting leaves a working invocation working; under-accepting breaks one. The guard's job is to catch what does nothing.

Two defects the sweep surfaced (both measured, both fixed)

1. kit add --list provisioned a service named --list. kit documented it in two places — the help table (kit add --list # List all available adapters) and the command's own one-line help — and read it nowhere, so argv[3] took the flag as the service name. On clean main:

$ kit add --list
Provisioning --list...
  ✗ Available services: stripe/payments, supabase/db, …
  Error: Unknown service: --list

--list now lists and exits 0. Bare kit add still exits 1: a forgotten argument is not a completed provision.

2. kit setup --activate-mise never existed. mise-path.ts said that flag appends the mise-shims PATH line. No code reads it, and ensureMiseActivation — the appender — has no production caller at all (it is in self-audit's unwired-code advisory). kit doctor reports the gap and prints the line. The claim is corrected rather than the feature quietly invented: writing to a user's shell profile needs its own consent design. Flagged as a follow-up, not smuggled in here.

Both were silent before the floor. After it they would have become rejections of kit's own documented advice — worse than the silence — so a test now pins that kit never prints an invocation its own floor rejects. That test is what found #1.

The self-audit row now measures the property, not the fix's shape

flagValidationCoverage grepped src/commands/*.ts for the string unknownFlags(. That measured whether a file contained a guard: a module with several handlers could hold one and leave its other verbs open, and two verbs (fix, plugin) do not live under src/commands at all, so no grep there could ever see them. Coverage is now "every verb in COMMAND_REGISTRY has an entry in the declared surface", and a verb without one is reported as unvalidated rather than counted as covered. A missing table reports every verb as missing — fail-open there would have called a repo with no flag surface fully validated.

before: WARN flag validation: … (2/45 command modules validate)   [43 advisory rows]
after:  PASS flag validation: all 71 command verb(s) reject unknown flags

self-audit advisories: 143 → 100.

Tests

src/flag-surface.test.ts — 17 tests:

  • coverage: every dispatchable verb has an entry; no stale entry for a verb that no longer dispatches; flagsForCommand returns null (skip + report) rather than [] (reject everything) for an unknown verb
  • drift: the table is compared against the source scan itself, so a new flag cannot land unlisted; documented-flag spot checks for the three real regressions (--category, --attest/--no-auto-install, --only/--skip/--exclude)
  • rejectUnknownFlags: names every unknown flag, prints the accepted set once (deduped — callers union with GLOBAL_FLAGS, which overlap), leaves -- pass-through alone, writes to stderr so a --json stdout stays parseable
  • behaviour, compiled CLI: rejection exits 1 with an empty stdout; a global before the verb still works; kit upgrade --self. is caught; kit secrets --drry-run is caught (the side-effectful module 43 command modules still accept unknown flags silently — sweep the flag-validation advisory to zero #488 put first); kit audit --limit 1 still runs
  • the invariant above: every kit <verb> --flag in a string literal under src/** or in the CI workflows is accepted by the floor

Rewrote the three flagValidationCoverage fixtures for the verb-based oracle, plus a new one for hyphenated verbs (gha-audit) and one asserting a missing table fails closed.

Verification

  • npm test: 4299 tests, 0 fail, 0 skipped (macOS, non-root). The suite is the real safety net here — a floor that rejects a valid flag shows up as a failing command test, and none did.
  • kit self-audit: 16 rules, 0 fail, 0 warn
  • kit check --category security: 24/25, the one finding being the pre-existing historical-secret warn on git history
  • Cross-checked by hand that no invocation in .github/workflows/*.yml, src/hooks.ts or the agent-config generators is rejected

Not in scope

  • Sub-command granularity (kit check verify-attestation accepting only its own flags). commands/check.ts keeps its finer per-form guards; the floor is the union, i.e. the floor, not a replacement.
  • Wiring ensureMiseActivation (needs a consent design for shell-profile writes).
  • Tightening the generated sets by hand to drop subprocess flags — deliberate, see above.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown

✅ Docker image built successfully

  • CLI image: docker.io/sandstream/kit:test

…face

Two command modules validated their flags and 43 did not (#488). The class is
not theoretical: `kit check --category security` ran the FULL check for six
majors because nothing rejected the flag, and `kit upgrade --self.` — one
trailing period — fell through to the lock-file branch, rewrote every
`installedAt`, installed nothing and printed the success line.

Sweeping 43 handlers would fix today and rot tomorrow, so the shape is the one
read-only-surface.ts already uses for the write surface: a declared table
(src/flag-surface.ts, 71 verbs) and one refusal at dispatch.

The table is GENERATED from the source — scripts/derive-command-flags.mjs unions
every `--flag` literal in a command's own module (security.ts reads --only /
--skip / --exclude through a local commaList() helper, which a reader-only scan
missed), the literals its DIRECT imports hand to an argv reader (depth 1 is
where the --attest regression lived: a hand-built CHECK_FLAGS from check.ts's own
literals rejected `kit check --attest` and --no-auto-install, both documented and
both read in cli-checks-shared.ts), and every flag docs/COMMANDS.md documents for
that verb. flag-surface.test.ts fails when the table drifts, so a new flag cannot
land unlisted. GLOBAL_FLAGS are unioned in by the guard rather than listed per
verb — that omission is what made `kit check --read-only` exit 1 without running.
Everything after `--` passes through untouched.

The sets err toward accepting: over-accepting leaves a working invocation
working, under-accepting breaks one. The guard catches what does NOTHING.

The sweep surfaced two real defects, both measured:

  - `kit add --list` is documented in kit's own help twice and read nowhere, so
    argv[3] took the flag as the SERVICE NAME: the documented invocation printed
    "Provisioning --list..." then "Unknown service: --list". It now lists and
    exits 0; bare `kit add` still exits 1, since a forgotten argument is not a
    completed provision.
  - `kit setup --activate-mise` never existed. mise-path.ts claimed that flag
    appends the mise-shims PATH line; no code reads it and ensureMiseActivation
    has no production caller at all. The claim is corrected rather than the
    feature quietly invented — writing to a shell profile needs its own consent
    design.

Both were silent before the floor and would have become rejections of kit's own
advice after it, so a test pins that kit never prints an invocation its own floor
rejects. That test found the first one.

self-audit's flag-validation row now measures verbs against the declared surface
instead of grepping src/commands/*.ts for the string `unknownFlags(` — that
measured the shape of the fix, missed verbs inside multi-handler modules, and
could never see `fix` or `plugin`, which do not live under src/commands at all.

Closes #488

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@sandstream
sandstream force-pushed the feat/488-flag-floor branch from acceae3 to a903821 Compare August 20, 2026 08:20
@github-actions

Copy link
Copy Markdown

✅ Docker image built successfully

  • CLI image: docker.io/sandstream/kit:test

@sandstream
sandstream merged commit 012a04a into main Aug 20, 2026
19 checks passed
@sandstream
sandstream deleted the feat/488-flag-floor branch August 20, 2026 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

43 command modules still accept unknown flags silently — sweep the flag-validation advisory to zero

1 participant