File size: 12,279 Bytes
d90101d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | use std::hash::{DefaultHasher, Hash, Hasher};
use std::path::PathBuf;
use derive_more::Display;
use derive_setters::Setters;
use serde::{Deserialize, Serialize};
use crate::{Effort, ModelConfig};
/// All discrete mutations that can be applied to the application configuration.
///
/// Instead of replacing the entire config, callers describe exactly which field
/// they want to change. Implementations receive a list of operations, apply
/// each in order, and persist the result atomically.
#[derive(Debug, Clone, PartialEq)]
pub enum ConfigOperation {
/// Set the active session provider and model atomically.
///
/// When the provider differs from the current session provider the entire
/// session (provider + model) is replaced atomically. When they match only
/// the model field is updated.
SetSessionConfig(ModelConfig),
/// Set the commit-message generation configuration.
///
/// `None` clears the commit configuration so the active session
/// provider/model is used for commit message generation.
SetCommitConfig(Option<ModelConfig>),
/// Set the shell-command suggestion configuration.
SetSuggestConfig(ModelConfig),
/// Set the reasoning effort level for all agents.
SetReasoningEffort(Effort),
}
const VERSION: &str = match option_env!("APP_VERSION") {
Some(val) => val,
None => env!("CARGO_PKG_VERSION"),
};
/// Represents the minimal runtime environment in which the application is
/// running.
///
/// Contains only the five fields that cannot be sourced from [`ForgeConfig`]:
/// `os`, `cwd`, `home`, `shell`, and `base_path`. All configuration
/// values previously carried here are now accessed through
/// `EnvironmentInfra::get_config()`.
#[derive(Debug, Setters, Clone, PartialEq, Serialize, Deserialize, fake::Dummy)]
#[serde(rename_all = "camelCase")]
#[setters(strip_option)]
pub struct Environment {
/// The operating system of the environment.
pub os: String,
/// The current working directory.
pub cwd: PathBuf,
/// The home directory.
pub home: Option<PathBuf>,
/// The shell being used.
pub shell: String,
/// The base path relative to which everything else is stored.
pub base_path: PathBuf,
}
impl Environment {
pub fn log_path(&self) -> PathBuf {
self.base_path.join("logs")
}
/// Returns the history file path.
///
/// # Arguments
/// * `custom_path` - An optional custom path sourced from
/// `ForgeConfig::custom_history_path`. When present it overrides the
/// default location inside `base_path`.
pub fn history_path(&self, custom_path: Option<&PathBuf>) -> PathBuf {
custom_path
.cloned()
.unwrap_or(self.base_path.join(".forge_history"))
}
pub fn snapshot_path(&self) -> PathBuf {
self.base_path.join("snapshots")
}
pub fn mcp_user_config(&self) -> PathBuf {
self.base_path.join(".mcp.json")
}
/// Returns the path where the MCP trust store is persisted across restarts.
pub fn mcp_trust_path(&self) -> PathBuf {
self.base_path.join(".mcp_trust.json")
}
pub fn agent_path(&self) -> PathBuf {
self.base_path.join("agents")
}
pub fn agent_cwd_path(&self) -> PathBuf {
self.cwd.join(".forge/agents")
}
pub fn permissions_path(&self) -> PathBuf {
self.base_path.join("permissions.yaml")
}
pub fn mcp_local_config(&self) -> PathBuf {
self.cwd.join(".mcp.json")
}
pub fn version(&self) -> String {
VERSION.to_string()
}
pub fn app_config(&self) -> PathBuf {
self.base_path.join(".config.json")
}
pub fn database_path(&self) -> PathBuf {
self.base_path.join(".forge.db")
}
/// Returns the path to the cache directory
pub fn cache_dir(&self) -> PathBuf {
self.base_path.join("cache")
}
/// Returns the global skills directory path (~/forge/skills)
pub fn global_skills_path(&self) -> PathBuf {
self.base_path.join("skills")
}
/// Returns the agents skills directory path (~/.agents/skills)
///
/// Returns `None` when the home directory cannot be determined.
pub fn agents_skills_path(&self) -> Option<PathBuf> {
self.home.as_ref().map(|home| home.join(".agents/skills"))
}
/// Returns the project-local skills directory path (.forge/skills)
pub fn local_skills_path(&self) -> PathBuf {
self.cwd.join(".forge/skills")
}
/// Returns the global commands directory path (base_path/commands)
pub fn command_path(&self) -> PathBuf {
self.base_path.join("commands")
}
/// Returns the project-local commands directory path (.forge/commands)
pub fn command_path_local(&self) -> PathBuf {
self.cwd.join(".forge/commands")
}
/// Returns the global AGENTS.md path (base_path/AGENTS.md)
pub fn global_agentsmd_path(&self) -> PathBuf {
self.base_path.join("AGENTS.md")
}
/// Returns the project-local AGENTS.md path (cwd/AGENTS.md)
pub fn local_agentsmd_path(&self) -> PathBuf {
self.cwd.join("AGENTS.md")
}
/// Returns the plans directory path relative to the current working
/// directory (cwd/plans)
pub fn plans_path(&self) -> PathBuf {
self.cwd.join("plans")
}
/// Returns the path to the custom provider configuration file
/// (base_path/provider.json)
pub fn provider_config_path(&self) -> PathBuf {
self.base_path.join("provider.json")
}
/// Returns the path to the credentials file where provider API keys are
/// stored
pub fn credentials_path(&self) -> PathBuf {
self.base_path.join(".credentials.json")
}
pub fn workspace_hash(&self) -> WorkspaceHash {
let mut hasher = DefaultHasher::default();
self.cwd.hash(&mut hasher);
WorkspaceHash(hasher.finish())
}
}
#[derive(Clone, Copy, Display)]
pub struct WorkspaceHash(u64);
impl WorkspaceHash {
pub fn new(id: u64) -> Self {
WorkspaceHash(id)
}
pub fn id(&self) -> u64 {
self.0
}
}
#[cfg(test)]
mod tests {
use fake::{Fake, Faker};
use pretty_assertions::assert_eq;
use super::*;
#[test]
fn test_agent_cwd_path() {
let fixture: Environment = Faker.fake();
let fixture = fixture.cwd(PathBuf::from("/current/working/dir"));
let actual = fixture.agent_cwd_path();
let expected = PathBuf::from("/current/working/dir/.forge/agents");
assert_eq!(actual, expected);
}
#[test]
fn test_agent_cwd_path_independent_from_agent_path() {
let fixture: Environment = Faker.fake();
let fixture = fixture
.cwd(PathBuf::from("/different/current/dir"))
.base_path(PathBuf::from("/completely/different/base"));
let agent_path = fixture.agent_path();
let agent_cwd_path = fixture.agent_cwd_path();
let expected_agent_path = PathBuf::from("/completely/different/base/agents");
let expected_agent_cwd_path = PathBuf::from("/different/current/dir/.forge/agents");
// Verify that agent_path uses base_path
assert_eq!(agent_path, expected_agent_path);
// Verify that agent_cwd_path is independent and always relative to CWD
assert_eq!(agent_cwd_path, expected_agent_cwd_path);
// Verify they are different paths
assert_ne!(agent_path, agent_cwd_path);
}
#[test]
fn test_global_skills_path() {
let fixture: Environment = Faker.fake();
let fixture = fixture.base_path(PathBuf::from("/home/user/.forge"));
let actual = fixture.global_skills_path();
let expected = PathBuf::from("/home/user/.forge/skills");
assert_eq!(actual, expected);
}
#[test]
fn test_agents_skills_path_with_home() {
let fixture: Environment = Faker.fake();
let fixture = fixture.home(PathBuf::from("/home/user"));
let actual = fixture.agents_skills_path();
let expected = Some(PathBuf::from("/home/user/.agents/skills"));
assert_eq!(actual, expected);
}
#[test]
fn test_agents_skills_path_without_home() {
let fixture: Environment = Faker.fake();
// Explicitly clear the home field
let mut fixture = fixture;
fixture.home = None;
let actual = fixture.agents_skills_path();
assert_eq!(actual, None);
}
#[test]
fn test_local_skills_path() {
let fixture: Environment = Faker.fake();
let fixture = fixture.cwd(PathBuf::from("/projects/my-app"));
let actual = fixture.local_skills_path();
let expected = PathBuf::from("/projects/my-app/.forge/skills");
assert_eq!(actual, expected);
}
#[test]
fn test_skills_paths_independent() {
let fixture: Environment = Faker.fake();
let fixture = fixture
.cwd(PathBuf::from("/projects/my-app"))
.base_path(PathBuf::from("/home/user/.forge"));
let global_path = fixture.global_skills_path();
let local_path = fixture.local_skills_path();
let expected_global = PathBuf::from("/home/user/.forge/skills");
let expected_local = PathBuf::from("/projects/my-app/.forge/skills");
// Verify global path uses base_path
assert_eq!(global_path, expected_global);
// Verify local path uses cwd
assert_eq!(local_path, expected_local);
// Verify they are different paths
assert_ne!(global_path, local_path);
}
#[test]
fn test_command_path() {
let fixture: Environment = Faker.fake();
let fixture = fixture.base_path(PathBuf::from("/home/user/.forge"));
let actual = fixture.command_path();
let expected = PathBuf::from("/home/user/.forge/commands");
assert_eq!(actual, expected);
}
#[test]
fn test_command_path_local() {
let fixture: Environment = Faker.fake();
let fixture = fixture.cwd(PathBuf::from("/projects/my-app"));
let actual = fixture.command_path_local();
let expected = PathBuf::from("/projects/my-app/.forge/commands");
assert_eq!(actual, expected);
}
#[test]
fn test_command_paths_independent() {
let fixture: Environment = Faker.fake();
let fixture = fixture
.cwd(PathBuf::from("/projects/my-app"))
.base_path(PathBuf::from("/home/user/.forge"));
let global_path = fixture.command_path();
let local_path = fixture.command_path_local();
let expected_global = PathBuf::from("/home/user/.forge/commands");
let expected_local = PathBuf::from("/projects/my-app/.forge/commands");
assert_eq!(global_path, expected_global);
assert_eq!(local_path, expected_local);
assert_ne!(global_path, local_path);
}
#[test]
fn test_global_agents_md_path() {
let fixture: Environment = Faker.fake();
let fixture = fixture.base_path(PathBuf::from("/home/user/.forge"));
let actual = fixture.global_agentsmd_path();
let expected = PathBuf::from("/home/user/.forge/AGENTS.md");
assert_eq!(actual, expected);
}
#[test]
fn test_local_agents_md_path() {
let fixture: Environment = Faker.fake();
let fixture = fixture.cwd(PathBuf::from("/projects/my-app"));
let actual = fixture.local_agentsmd_path();
let expected = PathBuf::from("/projects/my-app/AGENTS.md");
assert_eq!(actual, expected);
}
#[test]
fn test_plans_path() {
let fixture: Environment = Faker.fake();
let fixture = fixture.cwd(PathBuf::from("/projects/my-app"));
let actual = fixture.plans_path();
let expected = PathBuf::from("/projects/my-app/plans");
assert_eq!(actual, expected);
}
#[test]
fn test_provider_config_path() {
let fixture: Environment = Faker.fake();
let fixture = fixture.base_path(PathBuf::from("/home/user/.forge"));
let actual = fixture.provider_config_path();
let expected = PathBuf::from("/home/user/.forge/provider.json");
assert_eq!(actual, expected);
}
}
|