Skip to content

fix(mfile,mctx,graph): a state key is one directory name - #3362

Open
bryan-minimal wants to merge 2 commits into
mainfrom
fix/1244-state-key-one-component
Open

bryan-minimal wants to merge 2 commits into
mainfrom
fix/1244-state-key-one-component

Conversation

@bryan-minimal

Copy link
Copy Markdown
Member

state_key (on a task or in [defaults]) and a package's env_state_wiring prefix each name a directory under the project's state. They are joined onto the state base, so each has to be a single directory name; until now nothing checked that.

  • mfile::is_state_key: one name, not empty, . or .., no /, backslash or NUL.
  • The mfile refuses a bad state_key at load (Error::InvalidStateKey, exit 3), naming the table that set it and suggesting a plain name.
  • graph refuses a bad env_state_wiring prefix where it reads the wiring; mctx checks both again where each becomes a path.
  • Every env_state_wiring prefix in the public registry today is one name (cargo, gocache, npm-cache, ...), so nothing that loads today stops loading.

Test: a_state_key_that_is_not_one_name_is_refused_at_load (the predicate's cases, a task's and [defaults]' refusal with exit 3, and a plain name loading).

Fixes gominimal/inbox#1244

🤖 Generated with Claude Code

`state_key` (on a task or in `[defaults]`) and a package's
`env_state_wiring` prefix each name a directory under the project's
state, and are now checked to be exactly one name: not empty, `.` or
`..`, and with no `/`, `\` or NUL. The mfile refuses a bad key at load
with exit 3 and the table that set it, graph refuses a bad prefix where
it reads the wiring, and mctx checks again where each becomes a path.
Every prefix in the package registry today is one name.

Fixes gominimal/inbox#1244

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

  • Run on-demand review

This review includes 6 billable files and costs up to $1.50.

  • Ask an admin to make reviews automatic

Open in CodeRabbit

Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing.

Or wait 33 minutes for your next included review.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 8ee25c57-b595-4dbc-86d4-a32505216ec8
Commits

Reviewing files that changed from the base of the PR and between 3e4398f and 0b19b8d.

Files selected for processing: 6

  • crates/graph/src/env_setup.rs
  • crates/mctx/src/env.rs
  • crates/mctx/src/lib.rs
  • crates/mfile/src/error.rs
  • crates/mfile/src/lib.rs
  • crates/minimald/src/box_manager/composables.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@bryan-minimal
bryan-minimal marked this pull request as ready for review November 13, 2026 04:37
@bryan-minimal
bryan-minimal requested a review from a team as a code owner November 13, 2026 04:37
@minimal-foundry

minimal-foundry Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Reviewed: no findings; approval withheld, 6 threads unresolved at 0b19b8d3de3b.

APPROVE withheld: 6 unresolved threads — norrietaylor crates/mfile/src/lib.rs:1294, norrietaylor crates/mfile/src/lib.rs:1294, norrietaylor crates/graph/src/env_setup.rs:96, norrietaylor crates/graph/src/env_setup.rs:95, norrietaylor crates/graph/src/env_setup.rs:96, …. Resolving the last thread releases the approval on this head.

Resolve the open threads. Resolving the last one releases the approval on this head; no new review is required.

Clobber: none seen.

@minimal-foundry minimal-foundry Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minimal-review — advisory review

Verdict COMMENT — 3 findings, 3 inline.

Panel: 3 of 3 personas ran (correctness, security, supply-chain).

Comment thread crates/mfile/src/lib.rs Resolved
Comment thread crates/mfile/src/lib.rs Resolved

@norrietaylor norrietaylor left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scope matches the title. All five files add the one-name check for a state key or an env_state_wiring prefix, plus a refusal error and a test. Clobber not checked: git log was not available in this sandbox. The diff only adds lines and removes none. Correctness The mfile check and its test are sound for the project file. The new refusal also runs when the package graph loads an upstream layer through File::from_dir_for_graph. That path skips task checks on purpose, because the user cannot edit an upstream file. The session composition path (minimald/src/box_manager/composables.rs::extract_state_wiring) reads the same prefix and still checks only for /. So the new rule is applied in three places and not in the fourth. Two open threads already cover the empty state_key and the misplaced doc comment on commit_index_object. I did not raise them again. Also, minimald/src/env.rs::state_dirs_from_env_vars accepts .. and .. The only result is a create_dir_all on a directory that already exists, so I did not file it. Not checked I did not compile or run tests, because the review contract does not allow a build.

Comment thread crates/mfile/src/lib.rs
entry: "[defaults]".to_owned(),
value: key.to_owned(),
});
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium · The state_key refusal runs for every load, including File::from_dir_for_graph (TaskCheck::Skip). decode::Layer::new (decode/src/lib.rs:144) uses that path to read upstream package layers. That path skips task checks on purpose: the graph never runs a layer's tasks, and "the user cannot fix that file" (lib.rs:1287-1293). Now an upstream layer with state_key = "" or a path-shaped key in a task or [defaults] makes the user's whole graph load fail with exit 3. The key in that layer is never used. Gate both state_key checks on tasks == TaskCheck::Refuse, as refuse_interactive is gated.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken in 0b19b8d: both state_key checks now run only when tasks == TaskCheck::Refuse, as refuse_interactive does. A layer read through from_dir_for_graph no longer fails on a key it never uses. mctx still checks the key where it becomes a path, so a bad key cannot reach the state dir another way. The test checks that a layer with a bad task key or default key passes check(TaskCheck::Skip).

Comment thread crates/mfile/src/lib.rs
entry: "[defaults]".to_owned(),
value: key.to_owned(),
});
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low · A user who writes the current spelling [defaults.task]\nstate_key = "../x" gets an error that names [defaults]. legacy::settle copies [defaults.task] state_key into defaults.state_key before check runs (legacy.rs:296-305, lib.rs:1106 then 1186). So the user is sent to a table that is not in their file. The legacy hint tells users to move the key out of [defaults]. Name the table the key came from, or name [defaults.task] when the legacy hint did not fire.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken in 0b19b8d: the default key's refusal now names [defaults.task]. That is where the current spelling writes it. A file with the legacy [defaults] state_key also gets the legacy hint, which tells the user to move the key to [defaults.task], so both readers are sent to the same table. Telling the two spellings apart in the error would need a new field on Defaults that only this message reads, so I left that out.

std::io::ErrorKind::InvalidData,
format!("env_state_wiring prefix {prefix:?} is not one directory name"),
));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low · The fix covers the task path but not the session composition path. minimald/src/box_manager/composables.rs::extract_state_wiring reads the same env_state_wiring prefix and checks only w.prefix.contains('/') (line 91). A prefix of .., ., empty or with a backslash still composes into the session's env there, for example GOCACHE=/state/... Use mfile::is_state_key in extract_state_wiring too, so that one rule covers both readers. This is the only other reader of the prefix that I found.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken in 0b19b8d: extract_state_wiring now uses mfile::is_state_key, so .., ., empty and a backslash are refused on the session path as on the task path. The existing test there still passes its cache/artifacts case through the new rule.

return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("env_state_wiring prefix {prefix:?} is not one directory name"),
));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low · The refusal does not name the package that declares the prefix, and it gives no next step. With several packages in a closure, the user cannot tell which one to fix or report. b.name is in scope. The parallel check in composables.rs:95 already names the package: "package {} declares env_state_wiring with prefix ...". Use the same wording here so both paths say the same thing.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken in 0b19b8d: both readers now use one message from mfile::state_prefix_refusal. It names the package and the prefix, says what a prefix may be, and says to fix the prefix in that package or report it to its maintainers.

std::io::ErrorKind::InvalidData,
format!("env_state_wiring prefix {prefix:?} is not one directory name"),
));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low · No test drives the new env_state_wiring prefix refusal. The PR's only test covers the mfile state_key check. If the graph check is removed later, a ../x prefix will be joined onto the state dir again and nothing will fail. composables.rs::multi_component_state_wiring_prefix_errors builds a layer with a bad prefix. Copy that test to cover SetupForPackages::build with .. and a/b.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken in 0b19b8d: a_state_wiring_prefix_that_is_not_one_name_is_refused in graph's env_setup tests builds a package with each of .. and a/b and checks that SetupForPackages::build fails naming the package and the prefix. It also checks that a plain name is wired to /state/<name>.

Comment thread crates/mctx/src/lib.rs
Some(name) if !name.is_empty() && !mfile::is_state_key(name) => {
return Err(Error::Other(anyhow!(
"state key {name:?} is not one directory name; use a plain name such as \"dev\""
)));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low · The message joins two facts with a semicolon. It also does not say where the key came from, so the user cannot find it to fix it. The mfile error names the table. The two new messages in mctx/src/env.rs (lines 91 and 1387) have the same problem: they say "a package's state directory" and do not name the package. Write two sentences, and name the source where the caller knows it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken in 0b19b8d for the message in make_env: it is now two sentences, names the task, and says to set state_key on the task or in [defaults.task]. The two in mctx/src/env.rs are now two sentences too, but they cannot name the package. They read SetupForPackages::state_dirs, a set of names that no longer says which package declared each one. The graph refuses such a prefix first and names the package, so these say so and ask for a bug report. Carrying the package into state_dirs would change its type for every reader, which seemed too much for a second line of defence.

…used

An empty `state_key` loads again: it opts a task out of the default key,
and the task gets a temporary state directory. A graph layer's keys are
not checked, as its tasks never run. The default key's refusal names
`[defaults.task]`, where the key is now written.

The session composition path checks a package's `env_state_wiring`
prefix with the same rule as the task path, and both name the package
and the next step in one shared message. A graph test covers `..` and
`a/b`. The mctx messages are two sentences and name the task.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

@minimal-foundry minimal-foundry Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minimal-review — approval withheld

Verdict COMMENT — APPROVE withheld: 6 unresolved threads — norrietaylor crates/mfile/src/lib.rs:1294, norrietaylor crates/mfile/src/lib.rs:1294, norrietaylor crates/graph/src/env_setup.rs:96, norrietaylor crates/graph/src/env_setup.rs:95, norrietaylor crates/graph/src/env_setup.rs:96, …. No findings.

Panel: 3 of 3 personas ran (correctness, security, supply-chain).

@norrietaylor norrietaylor left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scope matches the title. The diff validates state_key at load and validates the env_state_wiring prefix in graph and minimald. It also re-checks both in mctx where they become paths. Clobber none. The composables.rs hunk replaces the old /-only check with the stricter shared rule. Correctness The head commit answers all earlier threads: an empty key is an opt-out, the load gates on TaskCheck::Refuse, the error names [defaults.task], the session path uses the shared rule, and a graph test exists. Every task path that passes task.state_key goes through Context::make_env_with_network, and that function now has the guard. The guard covers mip run, the minimald exec and env paths and the mctx env channel. The existing multi_component_state_wiring_prefix_errors test asserts only the package name and the prefix, so the new wording does not break it. One printed-text finding remains, in the bug-path message in mctx/src/env.rs. Not checked I did not compile or run tests. I did not confirm that SetupForPackages writes the /state/wired value that the new graph test asserts.

Comment thread crates/mctx/src/env.rs
fn state_dir_refusal(dir: &str) -> String {
format!(
"A package's `env_state_wiring` prefix {dir:?} is not one directory name. The package \
graph should have refused it and named the package, so please report this as a bug."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low · This text prints as error: on min install and in task setup. It names an internal component ("the package graph"). It says what the code "should have" done, and the code cannot prove that. It asks for a bug report but names no command. min bug exists (crates/minimal/src/cli.rs:127). State the situation and give one action. An example: "A package's env_state_wiring prefix ".." is not one directory name. This is a bug in Minimal. Run min bug to report it." Both call sites (lines 91 and 1400) use this function, so one change fixes both.

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.

2 participants