Fix #144: hermetic user_prompt_submit tests via TERRAPHIM_DEFAULT_DATA_PATH - #14
Conversation
Summary
This PR makes two coordinated changes. In production:
In tests:
What was done well: the production change is strictly additive (env var unset == previous behaviour), the rationale is laid out in the commit message with a clear before/after, no mocks or What remains problematic: the new Confidence Score: 4/5
Important Files Changed
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[user_prompt_submit test] --> B[create_hermetic_root]
B --> C["root: PathBuf"]
A --> D["set_hermetic_env(cmd, &root)"]
D --> E["cmd.env(TERRAPHIM_DEFAULT_DATA_PATH, root/data)"]
A --> F[spawn terraphim-agent learn hook]
F --> G["LearningCaptureConfig::default()"]
G --> H{"TERRAPHIM_DEFAULT_DATA_PATH set?"}
H -->|Yes| I["global_dir = root/data/terraphim/learnings"]
H -->|No| J["global_dir = dirs::data_dir() ... (legacy)"]
I --> K[storage_location returns global_dir]
J --> L{"project_dir exists?"}
L -->|Yes| M[storage_location returns project_dir]
L -->|No| K
K --> N[hook writes correction file]
M --> N
N --> O[test reads file via hermetic_learnings_dir root]
O --> P[assert one file or zero files]
style B fill:#d4edda,stroke:#28a745
style D fill:#d4edda,stroke:#28a745
style H fill:#fff3cd,stroke:#ffc107
style I fill:#d4edda,stroke:#28a745
style K fill:#d4edda,stroke:#28a745
Inline FindingsP2
P2 The helper creates Last reviewed commit: e00c958 | Reviews (1) |
…ULT_DATA_PATH (Refs #144)
The user-prompt-submit hook path uses LearningCaptureConfig::default() which
resolves global_dir via dirs::data_dir(). On macOS/Windows that ignores
XDG_DATA_HOME and returns $HOME/Library/Application Support, so the test
that set HOME and XDG_DATA_HOME never found the file it expected — 3 of 4
tests have been failing on every non-Linux runner since the --lib-only
regression of 2026-07-31.
Production:
- Honour TERRAPHIM_DEFAULT_DATA_PATH in Default::default() (matches the
existing settings.toml field of the same name, bringing the hook path in
line with every other terraphim-agent learn subcommand).
- storage_location() short-circuits to global_dir when TERRAPHIM_DEFAULT_DATA_PATH
is set, so a project-local .terraphim/ directory no longer wins. This is a
strict extension: when the env var is unset, behaviour is identical.
Test:
- Rewrite user_prompt_submit_tests to use support::cli_test_env::{create_hermetic_root,
set_hermetic_env}. Each test gets a unique temp root; the spawned subprocess
inherits TERRAPHIM_DEFAULT_DATA_PATH; the test reads back from the same
hermetic root the hook wrote to.
- No mocks, no #[ignore], no timeout increases. The platform-specific
dirs::data_dir() behaviour no longer matters.
All 4 tests pass (3 previously failing + 1 already passing, now non-vacuous
on macOS).
e00c958 to
0258ba5
Compare
Fixes #144:
user_prompt_submit_testscapture failures viaterraphim_agent::learnings, which historically writes correctionartefacts under
dirs::data_dir(). On macOS that path resolves to$HOME/Library/Application Supportand ignoresXDG_DATA_HOME,so the tests could not redirect storage into a hermetic temp root.
Two coordinated changes:
Production:
LearningCaptureConfig::default()honours theTERRAPHIM_DEFAULT_DATA_PATHenvironment variable when computingits
global_dir. This brings the hook path in line with theexisting
terraphim_settings::DeviceSettings, which already readsthe same env var.
storage_location()short-circuits to theglobal_dirwhen that variable is set, so the test never lands inthe per-project directory by accident.
Tests: each
user_prompt_submit_testscase gets a unique hermeticroot, derives its
learnings_dir = root.join("data").join("terraphim").join("learnings"),spawns the agent with
TERRAPHIM_DEFAULT_DATA_PATHpointing atthe root's data dir, and reads back the correction files from
that derived path.
cli_test_env::create_hermetic_root()andset_hermetic_env()are new helpers intests/support/cli_test_env.rs.Local verification:
cargo test -p terraphim_agent --test user_prompt_submit_testsreports4 passed; 0 failedafter thischange.