plans/2025-09-13-forge-history-file-env-var-v2.md
Implement support for per-project custom history files through the FORGE_HISTORY_FILE environment variable, allowing users to maintain separate prompt histories for different projects while preserving the current default behavior. The implementation will use the existing global history file by default and only override when the environment variable is explicitly set, supporting both absolute and relative paths with proper Windows compatibility.
Based on the codebase analysis, the current history implementation:
History File Location: Currently determined by Environment::history_path() method in crates/forge_domain/src/env.rs:68-70, which returns self.base_path.join(".forge_history") - a single global file.
History Usage: History is initialized in crates/forge_main/src/editor.rs:68-71 using FileBackedHistory::with_file() with the path from env.history_path().
Environment Configuration: Environment variables are parsed in ForgeEnvironmentInfra::get() method in crates/forge_infra/src/env.rs:39-78, which already supports parsing custom environment variables like FORGE_DUMP_AUTO_OPEN, FORGE_TOOL_TIMEOUT, etc.
Default Behavior Priority: The implementation prioritizes maintaining existing behavior, only overriding when users explicitly set the environment variable.
FORGE_HISTORY_FILE environment variable support to ForgeEnvironmentInfra::get() method
crates/forge_infra/src/env.rs:39-78parse_env::<String>() function to retrieve the custom history file path, keeping it optional to preserve default behaviorcustom_history_path field to Environment struct to store the resolved history file path
crates/forge_domain/src/env.rs:16-61Option<PathBuf> field with proper serialization attributes to maintain backward compatibilityEnvironment::history_path() to support custom path resolution with relative path handling
crates/forge_domain/src/env.rs:68-70std::env::current_dir()PathBuf::canonicalize() for path normalization when possiblestd::fs::create_dir_all()<task_status>
[x]: DONE - Task 1: Add FORGE_HISTORY_FILE environment variable support
[~]: IN_PROGRESS - Task 2: Add custom_history_path field to Environment struct
[ ]: PENDING - Task 3: Modify Environment::history_path() with relative path support
[ ]: PENDING - Task 4: Implement Windows-specific path handling
[ ]: PENDING - Task 5: Add path creation and validation logic
</task_status>
FORGE_HISTORY_FILE successfully overrides default history location only when set./project-history resolve correctly from current directory../shared/history work across different directory structures/home/user/history) and Windows (C:\Users\user\history)\\server\share\history) are supported correctlyRisk: Changes might affect existing user workflows Mitigation: Strict preservation of default behavior; environment variable is purely additive
Risk: Windows path handling edge cases (UNC, long paths, reserved names) Mitigation: Use Rust's built-in PathBuf and extensive Windows-specific testing; handle reserved names (CON, PRN, etc.)
Risk: Users might not understand relative path resolution context Mitigation: Clear documentation explaining resolution is relative to working directory, not forge binary location
Risk: Creating parent directories might fail due to permissions Mitigation: Graceful fallback with informative error messages; suggest alternative paths
Risk: Users specifying paths outside intended areas Mitigation: Document security considerations; users control their own environment so flexibility is acceptable
./.forge_history instead of global fileTrade-offs: More intuitive per-project behavior but breaks existing user expectations
.forge_history in current directory firstTrade-offs: Zero configuration but might create unexpected behavior changes
Trade-offs: More permanent per-project setting but requires configuration file management
std::fs, std::env, and std::path functionalitydirs crate for default path resolutionFORGE_HISTORY_FILE=./project-history for relative pathsFORGE_HISTORY_FILE=/absolute/path/history for absolute pathsFORGE_HISTORY_FILE=C:\Users\Name\history successfullyC:\path\to\history, D:\projects\history\\server\share\history network pathsC:/path/to/history) and backslashes (C:\path\to\history)# Unix/Linux/macOS examples
FORGE_HISTORY_FILE=./project-history
FORGE_HISTORY_FILE=../shared/team-history
FORGE_HISTORY_FILE=/home/user/forge-histories/project1
# Windows examples
FORGE_HISTORY_FILE=.\project-history
FORGE_HISTORY_FILE=..\shared\team-history
FORGE_HISTORY_FILE=C:\Users\Name\ForgeHistories\project1
FORGE_HISTORY_FILE=\\server\share\team-histories\project1