Overview
What is the State Box?
The State Box is a mandatory architectural boundary that separates code from data:- Location:
~/.switchailocal/(customizable viaSWITCHAI_STATE_DIR) - Purpose: Centralized storage for all persistent state
- Security: Automatic permission hardening (
0700directories,0600files) - Durability: Atomic write operations with automatic backups
- Immutable-Friendly: Full support for read-only environments (NixOS, containers)
Directory Structure
- Directories:
0700(owner read/write/execute only) - Files:
0600(owner read/write only)
Configuration
Environment Variables
SWITCHAI_STATE_DIR
Override the default State Box location:
~/.switchailocal
SWITCHAI_READONLY
Enable read-only mode for immutable environments:
- All write operations are blocked with clear error messages
- Management Dashboard displays a “Read-Only Mode” indicator
- Save and edit buttons are disabled
- Discovery processes skip writing model registries
- Feedback collection is disabled
Perfect for Immutable Deployments
Perfect for Immutable Deployments
Read-only mode is ideal for:
- NixOS: Declarative, immutable system configuration
- Docker: Read-only root filesystems
- Kubernetes: ConfigMap-based configuration
- CI/CD: Ephemeral test environments
Features
1. Atomic File Operations
The State Box uses the rename-swap pattern to ensure data durability:- Buffered Write: Data is written to a temporary file (
.tmp.{uuid}) - Sync:
fsync()is called to flush OS buffers to disk - Atomic Rename: The temporary file is atomically renamed to the target
- Backup: The previous version is saved as
.bakbefore overwriting
- Power failures during writes don’t corrupt files
- Previous versions always available for recovery
- No partial writes or corrupted state
2. Automatic Security Hardening
On startup, switchAILocal automatically audits and corrects file permissions:- Directories:
0700(owner only) - Sensitive Files:
0600(owner only) - Logging: All corrections logged as security audit events
3. Read-Only Mode Support
Perfect for immutable environments like NixOS or containerized deployments:- All write operations return clear error messages
- UI displays read-only indicator
- System continues to function for queries and routing
- No data loss or corruption
4. Backward Compatibility
If you have an existingauth-dir configuration, switchAILocal will:
- Check the legacy
auth-dirlocation first - Fall back to State Box credentials if not found
- Automatically migrate credentials on first write
API Endpoint
GET /api/state-box/status
Query the current State Box status:Management Dashboard
The Management Dashboard displays State Box status in the settings panel:- Read-Only Indicator: Shows when the system is in read-only mode
- State Box Path: Displays the current State Box location
- File Status: Shows discovery registry and feedback database status
- Permission Status: Indicates if permissions are correctly hardened
Read-Only Mode Indicator
WhenSWITCHAI_READONLY=1 is set:
- A “Read-Only Mode” badge appears in the top-right corner
- All “Save” and “Edit” buttons are disabled
- Hovering over the badge shows an explanation tooltip
Docker & Container Deployment
Using State Box with Docker
Mount the State Box directory as a volume:Custom State Box Location in Docker
Read-Only Mode in Docker
NixOS Integration
Declarative Configuration
Stateful Directory
Troubleshooting
State Box Directory Not Found
Error:State Box root directory does not exist
Solution: The directory will be created automatically on first write. If you need to pre-create it:
Permission Denied Errors
Error:permission denied when accessing State Box files
Solution: Run the permission hardening manually:
Read-Only Mode Errors
Error:Read-only environment: write operations disabled
Solution: This is expected when SWITCHAI_READONLY=1 is set. To enable writes:
Corrupted Files
Error:failed to parse registry.json
Solution: The system will automatically restore from the .bak file:
Security Best Practices
1. Protect Credentials
Never commit credentials to version control:2. Use Environment Variables
For sensitive data, use environment variables instead of config files:3. Regular Backups
Back up your State Box regularly:4. Monitor Permissions
Verify permissions are correct:5. Read-Only Deployments
For production deployments, use read-only mode:Advanced Topics
Custom State Box Location
For multi-user systems or special deployments:Migrating Existing Data
If you have existing data in a different location:Monitoring State Box Operations
Enable debug logging to see State Box operations:FAQ
Q: Can I use a network drive for State Box?A: Not recommended. Network drives may not support atomic operations reliably. Use local storage for best results. Q: What happens if State Box is on a read-only filesystem?
A: Set
SWITCHAI_READONLY=1 to enable read-only mode. The system will continue to function for queries.
Q: Can I share State Box between multiple machines?A: Not recommended. Each machine should have its own State Box to avoid conflicts. Q: How do I recover from a corrupted State Box?
A: The system automatically restores from
.bak files. If needed, delete the corrupted file and restart.
Q: Is State Box compatible with cloud storage (Dropbox, iCloud)?A: Not recommended. Cloud sync may interfere with atomic writes. Use local storage. Q: Can I disable State Box?
A: No, State Box is mandatory. However, you can use read-only mode to prevent writes.
See Also
- Configuration Guide - Complete config.yaml options
- Management Dashboard - Web UI for state management
- API Reference - HTTP API documentation