{{ message }}
feat: introduce BASIC_MEMORY_PROJECT_ROOT for path constraints - #334
Merged
Conversation
- Add module-level caching for configuration (_CONFIG_CACHE) - Environment variables now properly override file config values - Follows Pydantic Settings best practices for env var precedence - Cache invalidated on save_config() to ensure consistency - Improves performance by avoiding repeated file reads This fixes the issue where setting env vars like BASIC_MEMORY_CLOUD_MODE didn't work because explicit values from config file took precedence. The new flow: 1. Check cache first 2. Load from file 3. Create env-based config 4. Merge: file data as base, env vars override 5. Cache and return Resolves testing issues where monkeypatching env vars didn't work. Signed-off-by: phernandez <paul@basicmachines.co>
Replaces cloud mode coupling with explicit project root constraint. Fixes #333 ## Problem BASIC_MEMORY_HOME had dual, conflicting semantics: 1. Path to the "main" (default) project directory 2. Root directory where ALL projects must be created (cloud mode) This coupling to cloud mode was inflexible and confusing. ## Solution Introduce BASIC_MEMORY_PROJECT_ROOT environment variable: - If set: ALL projects must be created underneath this directory - If not set: Projects can be created anywhere (default behavior) ## Changes ### Config (config.py:106-110) - Added project_root field to BasicMemoryConfig - Automatically populated from BASIC_MEMORY_PROJECT_ROOT env var - Optional field (defaults to None) ### Service Layer (project_service.py:102-126) - Changed from checking cloud_mode_enabled to checking project_root - Same path sanitization logic (strips /, ~/, ../) - Validates resolved paths stay within project_root boundary - Clear error messages reference BASIC_MEMORY_PROJECT_ROOT ### Tests (test_project_service.py:719-869) - Renamed cloud mode tests to project_root tests - Simplified tests using improved ConfigManager - No complex mocking needed - just set env var and invalidate cache - test_add_project_with_project_root_sanitizes_paths - test_add_project_with_project_root_rejects_escape_attempts - test_add_project_without_project_root_allows_arbitrary_paths ## Benefits ✅ Decouples path restriction from cloud mode ✅ Useful for any constrained environment (cloud, docker, multi-tenant) ✅ Backward compatible - existing users unaffected ✅ More explicit and clear naming ✅ BASIC_MEMORY_HOME remains for main project location ## Example Usage ### Cloud Mode ```bash BASIC_MEMORY_HOME=/app/data/basic-memory # main project BASIC_MEMORY_PROJECT_ROOT=/app/data # all projects under /app/data ``` ### Local Constrained Mode ```bash BASIC_MEMORY_HOME=~/basic-memory # main project BASIC_MEMORY_PROJECT_ROOT=~/my-projects # all projects constrained ``` ### Local Unrestricted (Default) ```bash BASIC_MEMORY_HOME=~/basic-memory # main project # BASIC_MEMORY_PROJECT_ROOT not set - projects anywhere ``` Signed-off-by: phernandez <paul@basicmachines.co>
Updates containerized deployment to use both environment variables for clean project organization and path constraints. ## Dockerfile Changes - Set BASIC_MEMORY_HOME=/app/data/basic-memory (main project location) - Set BASIC_MEMORY_PROJECT_ROOT=/app/data (constrain all projects to volume) - Create /app/data/basic-memory directory Benefits: - Main project cleanly separated in /app/data/basic-memory/ - All projects constrained to /app/data/ (volume mount) - Users can't accidentally create projects outside mounted volume - Consistent with cloud deployment pattern Directory structure: /app/data/ ├── basic-memory/ (main project) │ └── *.md └── other-project/ (optional additional projects) ## Test Fixture Changes - Add config cache invalidation in config_manager fixture - Ensures each test starts with clean config state - Prevents test pollution from cached config values Signed-off-by: phernandez <paul@basicmachines.co>
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Introduces
BASIC_MEMORY_PROJECT_ROOTenvironment variable to replace cloud mode path coupling and fixes ConfigManager to properly respect environment variable overrides.Fixes #333
Problem
BASIC_MEMORY_HOME had dual, conflicting semantics:
Environment variables didn't override config file values:
Cloud mode tightly coupled to path validation:
BASIC_MEMORY_CLOUD_MODESolution
1. ConfigManager Refactor (Commit 1)
Fixed environment variable override behavior:
_CONFIG_CACHEfor performance across instancessave_config()to ensure consistencyBenefits:
ConfigManager()instances)2. BASIC_MEMORY_PROJECT_ROOT (Commit 2)
Introduced new environment variable:
BASIC_MEMORY_PROJECT_ROOT: If set, all projects must be underneath this directoryBASIC_MEMORY_HOME: Location of the "main" project (unchanged, backward compatible)Implementation:
project_rootfield toBasicMemoryConfig(config.py:106-110)project_service.pyto checkproject_rootinstead ofcloud_mode_enabled/,~/,../)BASIC_MEMORY_PROJECT_ROOTTests:
Benefits:
3. Dockerfile & Test Updates (Commit 3)
Updated Dockerfile:
ENV BASIC_MEMORY_HOME=/app/data/basic-memory \ BASIC_MEMORY_PROJECT_ROOT=/app/dataDirectory structure:
Test fixtures:
config_managerfixtureExample Usage
Cloud Mode
Results:
/tmp/test→/app/data/tmp/test(sanitized)test→/app/data/test/app/data/basic-memory/(unchanged)Local Mode with Constraints
Local Mode Unrestricted (Default)
Files Changed
src/basic_memory/config.py- Addedproject_rootfield, module-level caching, env var overridesrc/basic_memory/services/project_service.py- Useproject_rootinstead ofcloud_mode_enabledtests/services/test_project_service.py- Simplified cloud mode teststests/conftest.py- Added cache invalidationDockerfile- Set both env vars for clean project structureTest Plan
make checkpytest tests/test_config.py -vpytest tests/services/test_project_service.py -vMigration Notes
For existing users:
BASIC_MEMORY_HOMEcontinues to work as beforeFor cloud deployments:
BASIC_MEMORY_PROJECT_ROOT=/app/dataBASIC_MEMORY_CLOUD_MODEcan remain for other cloud-specific behaviorsFor basic-memory-cloud:
BASIC_MEMORY_PROJECT_ROOT=/app/dataBASIC_MEMORY_CLOUD_MODEbased on other usagesBreaking Changes
None - this is fully backward compatible.
🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com