Skip to content

feat(mctx)!: configure separate state and cache directories - #292

Merged
twitchyliquid64 merged 1 commit into
mainfrom
tom/update
Jun 2, 2026
Merged

feat(mctx)!: configure separate state and cache directories#292
twitchyliquid64 merged 1 commit into
mainfrom
tom/update

Conversation

@twitchyliquid64

@twitchyliquid64 twitchyliquid64 commented Jun 2, 2026

Copy link
Copy Markdown
Member

Under Minimal One, we are going to have minimal state (like sandboxes, sessions etc) live in one directory (typically $XDG_STATE_DIR/minimal or $HOME/.local/state/minimal) and cached objects (like the artifact cache, fetched sources etc) live in another ($XDG_CACHE_DIR/minimal or $HOME/.local/cache/minimal). This separation better follows the XDG specification.

The default remains the same when the directories are not specified via the mctx::ConfigBuilder. We should pick a date in the future to make a hard switch.

Summary by CodeRabbit

  • Refactor
    • Separated state and cache storage into distinct base directories and updated local built-artifact cache handling. This clarifies where state vs. cache data live, improves isolation of cached artifacts, and makes cache initialization and access more predictable.

@coderabbitai

coderabbitai Bot commented Jun 2, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2cc7e79d-3083-48b8-a339-40223117fc48

📥 Commits

Reviewing files that changed from the base of the PR and between f79d700 and a77c944.

📒 Files selected for processing (3)
  • crates/mctx/src/config.rs
  • crates/mctx/src/env.rs
  • crates/mctx/src/lib.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/mctx/src/lib.rs
  • crates/mctx/src/config.rs

📝 Walkthrough

Walkthrough

The PR splits Config's single minimal directory into two bases: minimal_state_dir (state paths) and minimal_cache_dir (cache paths). Builder, Config, path helpers, Context cache initialization/accessor, a test, and a doc comment are updated to use the new layout.

Changes

Directory Layout Split

Layer / File(s) Summary
Config builder and struct directory split
crates/mctx/src/config.rs
ConfigBuilder fields and Config struct now use minimal_state_dir and minimal_cache_dir. Adds with_cache_dir() and updates with_state_dir() to target minimal_state_dir.
Config initialization and round-trip conversion
crates/mctx/src/config.rs
ConfigBuilder::build() initializes both minimal bases; Config::into_builder() reconstructs the builder preserving both fields.
Config directory helper methods
crates/mctx/src/config.rs
Path helpers refactored: state paths (builds_base_dir, state_base_dir, task_base_dir) join from minimal_state_dir; cache paths (built_cache_dir, downloads_dir, vcs_dir, index_dir, stdlib_dir, layer_cache_dir) join from minimal_cache_dir. cache_dir() removed in favor of built_cache_dir().
Context cache initialization and accessors
crates/mctx/src/lib.rs
Context::sub_setup creates built_cache_dir() and initializes Cache with it. Context::cache_base_dir() returns built_cache_dir().
Test adjustment and minimal_file() documentation
crates/mctx/src/env.rs, crates/mctx/src/lib.rs
env_channel_add_session test uses ctx.cache.temp_dir() for rootfs and adds a pre-install assertion; Context::minimal_file() doc comment clarified.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Two burrows now, neatly aligned,
State for the roots, cache for the rind,
Builder hops, fields split in two,
Context finds the cache anew,
A little change, tidy and kind.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately and specifically describes the main change: introducing separate configuration for state and cache directories in the mctx crate.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
crates/mctx/src/config.rs (1)

163-179: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Fix the fallback path: tilde won't expand in PathBuf.

The fallback PathBuf::from("~/.cache") (lines 165-166, 177-178) creates a path with a literal ~ character, not the user's home directory. Shell tilde expansion doesn't occur in PathBuf::from(). This will create a directory named ~ relative to the current directory, causing unexpected behavior.

The TODO comments at lines 159 and 171 show the correct approach using dirs::home_dir().

🐛 Proposed fix using dirs::home_dir()
             minimal_state_dir: self.minimal_state_dir.unwrap_or_else(|| {
                 dirs::cache_dir()
-                    .unwrap_or_else(|| PathBuf::from("~/.cache"))
+                    .unwrap_or_else(|| dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")).join(".cache"))
                     .join("minimal")
             }),
             minimal_cache_dir: self.minimal_cache_dir.unwrap_or_else(|| {
                 dirs::cache_dir()
-                    .unwrap_or_else(|| PathBuf::from("~/.cache"))
+                    .unwrap_or_else(|| dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")).join(".cache"))
                     .join("minimal")
             }),

Note: Added a final fallback to . (current directory) in case both dirs::cache_dir() and dirs::home_dir() return None, which can happen in unusual environments.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/mctx/src/config.rs` around lines 163 - 179, The fallback
PathBuf::from("~/.cache") used when building minimal_state_dir and
minimal_cache_dir is incorrect because tilde isn't expanded; replace that
fallback with logic that uses dirs::home_dir() to construct
home_dir.join(".cache") and, if both dirs::cache_dir() and dirs::home_dir() are
None, fall back to PathBuf::from(".") (current directory); update the
expressions for minimal_state_dir and minimal_cache_dir (referencing
minimal_state_dir, minimal_cache_dir, dirs::cache_dir(), dirs::home_dir(), and
the existing unwrap_or_else closures) to build the cache path via home_dir
instead of the literal "~/.cache".
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@crates/mctx/src/config.rs`:
- Around line 163-179: The fallback PathBuf::from("~/.cache") used when building
minimal_state_dir and minimal_cache_dir is incorrect because tilde isn't
expanded; replace that fallback with logic that uses dirs::home_dir() to
construct home_dir.join(".cache") and, if both dirs::cache_dir() and
dirs::home_dir() are None, fall back to PathBuf::from(".") (current directory);
update the expressions for minimal_state_dir and minimal_cache_dir (referencing
minimal_state_dir, minimal_cache_dir, dirs::cache_dir(), dirs::home_dir(), and
the existing unwrap_or_else closures) to build the cache path via home_dir
instead of the literal "~/.cache".

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: da113a19-145b-4c1c-80c2-4f09c992a6ca

📥 Commits

Reviewing files that changed from the base of the PR and between 105e63d and f79d700.

📒 Files selected for processing (2)
  • crates/mctx/src/config.rs
  • crates/mctx/src/lib.rs

@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.

LGTM

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