Skip to content

narrow_inputs: an opt-in that gives a job only the toolchain files it uses - #26

Open
ndbroadbent wants to merge 3 commits into
mainfrom
modules/narrow
Open

ndbroadbent wants to merge 3 commits into
mainfrom
modules/narrow

Conversation

@ndbroadbent

@ndbroadbent ndbroadbent commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Build PR 2 of the modules design (#10): narrow_inputs.

What changed

  • narrow_inputs, an explicit opt-in, at the top level or per job (the job's value wins). By default every job still gets every toolchain family's pins and lockfiles, modules or not.

  • A narrowed job gets:

    • its modules' families;
    • the families whose commands it visibly runs;
    • every family as soon as it runs anything cigen can't see into.

    The shared pins (.tool-versions and mise's config files) go into every job either way.

  • What cigen reads (src/inputs/narrow.rs, src/inputs/shell/):

    • run steps, used commands and local composite actions;
    • the shell scripts they run or source;
    • when a Node package manager runs, the scripts of every package.json in the project.

    Every file it read goes into the hash, so changing a script that decided the families reruns the job.

  • Working directories: scripts resolve where they run: a step's working-directory, GitHub's defaults.run (job, workflow, top level), and literal cds, including ... A sourced script's cd carries on; a subshell's doesn't. A cd cigen can't follow makes later scripts opaque.

  • Runners are read for the command they run: bundle exec, gem exec, npx, pnpm exec/dlx, yarn exec/dlx, npm exec, bunx, rustup run <toolchain>, uv run, poetry run, corepack, find -exec, a word pnpm, Yarn or Bun runs that isn't their own subcommand or a package script, and prefixes such as timeout and sudo. A toolchain command counts however it's quoted or escaped (\cargo).

  • toolchains: [node, rust] on a job adds families it uses without cigen seeing it; an unknown name is a config error with a suggestion. A narrowed job gets the package.json files (now a default input, Every package.json is a derived input: npm run runs its scripts #38) only with node.

  • Tool directory options are followed: --cwd, pnpm -C/--dir, terraform -chdir, cargo --manifest-path, go -C.

  • A toolchain's own commands are trusted to use only that toolchain (cargo test, rake, pnpm build). That's the one assumption narrowing makes; it's in the docs and STATUS (problem 23), with its common failure modes, and a job names what cigen can't see with toolchains: [node].

  • Pins where the toolchain runs: a family's pins and lockfiles count in each directory its commands ran in and the directories above, as well as the root (cd web && pnpm install brings web/pnpm-lock.yaml).

  • Opaque cases (anything the reader can't positively understand):

    • unknown commands, including a bare word that happens to name a file (it runs from PATH), aliases (git my-alias, cargo aliases) and unknown actions;
    • subcommands that run project code: cargo run, cargo xtask, go run, go generate;
    • sh -c (bash -lc, sh -ec), -s, a shell reading its input, trap, commands from variables;
    • commands hidden in arguments: git rebase --exec, git submodule foreach, git bisect run, awk system/getline, sed's e, ${X:-$(cmd)}, <(cmd), tar --to-command;
    • a << that isn't a readable heredoc (an unquoted CircleCI << parameter >>), or a heredoc that never ends;
    • a cd it can't follow, including one in a subshell, a pipeline or a function, or one that might not happen (under if, a loop, case, after ||, or after && once the list ends), before a script or a toolchain command;
    • runners that pick packages (pnpm --filter … exec, npm exec --workspace), find -execdir, cargo-make;
    • settings that name commands: alias x=, sort --compress-program, GIT_EXTERNAL_DIFF= and other GIT_* command variables, git config keys outside a plain list;
    • a CircleCI parameter as the command, quoted or not;
    • a step whose shell: isn't a POSIX shell, a Windows runner, or a runner chosen by an expression.
  • More pin files, in every job by default too (so default hashes change once): go.mod, go.work, go.work.sum, .terraform.lock.hcl, .yarnrc.yml, .npmrc, the Gradle and Maven wrapper properties, and mise's other config files.

  • A CircleCI job's working_directory needs nothing: the checkout goes there, so commands start in the project root.

  • Reasons: each family's choice carries a reason (FamilyChoice: "module rust", "it runs bundle in scripts/more.sh", "narrowed: nothing the job runs uses it"), ready for cigen explain.

  • src/inputs.rs is now src/inputs/: families, files, narrowing and the shell reader each have their own module, all under 300 lines.

  • Docs: a narrow_inputs section in the Inputs page, with the trade-off; STATUS problem 1 says narrowing is opt-in.

Tests

  • tests/narrow_inputs.rs, in mockci:
    • a default job reruns on another toolchain's lockfile;
    • a narrowed job doesn't, but reruns on its own lockfile, a shared pin, a script it runs or sources, and a package.json whose scripts it runs (the root's or a sub-package's);
    • scripts resolve in the step's working-directory and after cd;
    • make, shell: python and a pwsh default give every family;
    • a job can opt out of a narrowed config, or narrow itself.
  • Unit tests for the shell reader: commands in any position, neutral commands, opaque cases, scripts, heredocs, comments, cd resolution, unfollowable directories, subshell scoping, and bare commands.
  • src/inputs/shell/tests/hidden.rs: one test per false skip the independent review found (runner subcommands, fake heredocs, combined shell flags, cd in a subshell, commands hidden in arguments). Each fails on the first commit (189b641) and passes now.
  • tests/narrow_inputs_readers.rs, by hash: default working directories, Windows runners and runners from expressions, CircleCI orb and unknown named steps, a CircleCI working_directory, known, unknown and local (composite and not) actions, pins where a toolchain runs, and the new pin files. The Windows, pins-in-directories and new-pin tests fail on 189b641; the rest cover behaviour that already worked.
  • sed's script reader has its own unit tests.
  • src/inputs/shell/tests/second_review.rs: one test per finding of the second review (runners like rustup run, escaped commands, conditional cd, directory options, quoted CircleCI parameters, command-naming settings). Each failed before its fix.
  • tests/narrow_inputs_readers.rs also checks toolchains (and its error) and that a narrowed job gets package.json only with node.
  • I checked each scenario can fail:
    • disabling the opaque rule fails the make scenario;
    • not hashing followed files fails the sourced-script scenario;
    • ignoring the working directory fails the cd/working-directory scenario;
    • reading only the root package.json fails the sub-package scenario;
    • accepting any shell: fails the shell: scenario.
  • task ci passes with no warnings.

ndbroadbent added a commit that referenced this pull request Dec 4, 2026
The shell reader now makes a job opaque whenever it can't positively
understand what a line runs:

- Runners are read for the command they run: bundle exec, npx, pnpm
  exec/dlx, yarn exec/dlx, npm exec, bunx, uv run, poetry run, corepack.
  cargo and go subcommands that run project code (run, xtask, generate,
  aliases) are opaque.
- Only a real heredoc (unquoted <<WORD, <<-WORD, <<'WORD' outside quotes
  and comments) hides lines; an unquoted << parameter >> or a heredoc
  that never ends is opaque, and a command after a heredoc is read.
- -c or -s inside combined shell flags (bash -lc, sh -ec) is opaque.
- A cd in a subshell, pipeline or function leaves the directory unknown,
  so later scripts and toolchain commands are opaque.
- Commands hidden in arguments are opaque: trap, git aliases, git
  rebase --exec, submodule foreach, bisect run, awk system/getline,
  sed's e command and flag, ${X:-$(cmd)}, <(cmd), tar --to-command.

Also: a toolchain's pins and lockfiles count in each directory its
commands ran in and above; Windows runners and runners from expressions
are opaque; more pin files (go.mod, go.work, .terraform.lock.hcl,
.yarnrc.yml, .npmrc, Gradle and Maven wrappers, mise's config files).
Words are split respecting quotes.
… uses

By default every job still gets every toolchain family's pins and lockfiles. With
narrow_inputs: true (top level, or per job) a job gets its modules' families, the
families whose commands it visibly runs, and every family when it runs anything
cigen can't see into. Scripts resolve in the step's working directory and after
literal cds; every package.json's scripts count when a Node package manager runs;
every file read becomes an input.
The shell reader now makes a job opaque whenever it can't positively
understand what a line runs:

- Runners are read for the command they run: bundle exec, npx, pnpm
  exec/dlx, yarn exec/dlx, npm exec, bunx, uv run, poetry run, corepack.
  cargo and go subcommands that run project code (run, xtask, generate,
  aliases) are opaque.
- Only a real heredoc (unquoted <<WORD, <<-WORD, <<'WORD' outside quotes
  and comments) hides lines; an unquoted << parameter >> or a heredoc
  that never ends is opaque, and a command after a heredoc is read.
- -c or -s inside combined shell flags (bash -lc, sh -ec) is opaque.
- A cd in a subshell, pipeline or function leaves the directory unknown,
  so later scripts and toolchain commands are opaque.
- Commands hidden in arguments are opaque: trap, git aliases, git
  rebase --exec, submodule foreach, bisect run, awk system/getline,
  sed's e command and flag, ${X:-$(cmd)}, <(cmd), tar --to-command.

Also: a toolchain's pins and lockfiles count in each directory its
commands ran in and above; Windows runners and runners from expressions
are opaque; more pin files (go.mod, go.work, .terraform.lock.hcl,
.yarnrc.yml, .npmrc, Gradle and Maven wrappers, mise's config files).
Words are split respecting quotes.
- Toolchain commands that run commands: rustup run <toolchain>, gem exec,
  and a word pnpm, Yarn or Bun runs that isn't their own subcommand or a
  package script. cargo-<plugin> is checked like cargo <plugin>.
  Family command handling moves to src/inputs/shell/family.rs.
- A toolchain command counts however it's written (\cargo, ca''rgo).
- A cd that might not happen (under if, loops, case, after ||, or after
  && once the list ends) leaves the directory unknown.
- Options that move a tool are followed (--cwd, -C, --dir, -chdir,
  --manifest-path, go -C); runners that pick packages (--filter,
  --workspace, -r) and find -execdir are opaque.
- A quoted CircleCI parameter as the command is opaque.
- Settings that name commands are opaque: alias x=, sort
  --compress-program, GIT_* command variables, git config keys outside a
  plain list.
- A job's toolchains: [node, ...] adds families a narrowed job uses
  without cigen seeing it; unknown names are config errors with a fix.
- A narrowed job gets the package.json files only with node.
- Docs: the common ways the trust model fails, the toolchains key, and
  the keys tables (modules, narrow_inputs, toolchains; packages removed).

This branch has not been deployed

No deployments
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.

1 participant