mirror of
https://github.com/outbackdingo/optimclaw.git
synced 2026-08-26 23:50:17 +00:00
Three follow-up fixes for multi-tenant isolation: 1. Multi-user heartbeat now runs memory hygiene per user before each heartbeat check, matching single-user heartbeat behavior. 2. /model command in multi-tenant mode only persists to per-user settings (selected_model) without calling set_model() on the shared LlmProvider. The per-request model_override in the dispatcher reads from the same setting. Added multi_tenant flag to AgentConfig (auto-detected from GATEWAY_USER_TOKENS). 3. RigAdapter now supports per-request model overrides by injecting the model name into rig-core's additional_params. OpenAI/Anthropic/Ollama API servers use last-key-wins for duplicate JSON keys, so the override takes effect via serde's flatten serialization order. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
151 lines
6.0 KiB
Rust
151 lines
6.0 KiB
Rust
use std::time::Duration;
|
|
|
|
use crate::config::helpers::{optional_env, parse_bool_env, parse_option_env, parse_optional_env};
|
|
use crate::error::ConfigError;
|
|
use crate::settings::Settings;
|
|
|
|
/// Agent behavior configuration.
|
|
#[derive(Debug, Clone)]
|
|
pub struct AgentConfig {
|
|
pub name: String,
|
|
pub max_parallel_jobs: usize,
|
|
pub job_timeout: Duration,
|
|
pub stuck_threshold: Duration,
|
|
pub repair_check_interval: Duration,
|
|
pub max_repair_attempts: u32,
|
|
/// Whether to use planning before tool execution.
|
|
pub use_planning: bool,
|
|
/// Session idle timeout. Sessions inactive longer than this are pruned.
|
|
pub session_idle_timeout: Duration,
|
|
/// Allow chat to use filesystem/shell tools directly (bypass sandbox).
|
|
pub allow_local_tools: bool,
|
|
/// Maximum daily LLM spend in cents (e.g. 10000 = $100). None = unlimited.
|
|
pub max_cost_per_day_cents: Option<u64>,
|
|
/// Maximum LLM/tool actions per hour. None = unlimited.
|
|
pub max_actions_per_hour: Option<u64>,
|
|
/// Maximum daily LLM spend per user in cents. None = unlimited.
|
|
pub max_cost_per_user_per_day_cents: Option<u64>,
|
|
/// Maximum tool-call iterations per agentic loop invocation. Default 50.
|
|
pub max_tool_iterations: usize,
|
|
/// When true, skip tool approval checks entirely. For benchmarks/CI.
|
|
pub auto_approve_tools: bool,
|
|
/// Default timezone for new sessions (IANA name, e.g. "America/New_York").
|
|
pub default_timezone: String,
|
|
/// Maximum tokens per job (0 = unlimited).
|
|
pub max_tokens_per_job: u64,
|
|
/// Whether the deployment is multi-tenant (multiple users sharing one
|
|
/// instance). Auto-detected from GATEWAY_USER_TOKENS presence.
|
|
pub multi_tenant: bool,
|
|
}
|
|
|
|
impl AgentConfig {
|
|
/// Create a test-friendly config without reading env vars.
|
|
#[cfg(feature = "libsql")]
|
|
pub fn for_testing() -> Self {
|
|
Self {
|
|
name: "test-rig".to_string(),
|
|
max_parallel_jobs: 1,
|
|
job_timeout: Duration::from_secs(30),
|
|
stuck_threshold: Duration::from_secs(300),
|
|
repair_check_interval: Duration::from_secs(3600),
|
|
max_repair_attempts: 0,
|
|
use_planning: false,
|
|
session_idle_timeout: Duration::from_secs(3600),
|
|
allow_local_tools: true,
|
|
max_cost_per_day_cents: None,
|
|
max_actions_per_hour: None,
|
|
max_cost_per_user_per_day_cents: None,
|
|
max_tool_iterations: 10,
|
|
auto_approve_tools: true,
|
|
default_timezone: "UTC".to_string(),
|
|
max_tokens_per_job: 0,
|
|
multi_tenant: false,
|
|
}
|
|
}
|
|
|
|
pub(crate) fn resolve(settings: &Settings) -> Result<Self, ConfigError> {
|
|
Ok(Self {
|
|
name: parse_optional_env("AGENT_NAME", settings.agent.name.clone())?,
|
|
max_parallel_jobs: parse_optional_env(
|
|
"AGENT_MAX_PARALLEL_JOBS",
|
|
settings.agent.max_parallel_jobs as usize,
|
|
)?,
|
|
job_timeout: Duration::from_secs(parse_optional_env(
|
|
"AGENT_JOB_TIMEOUT_SECS",
|
|
settings.agent.job_timeout_secs,
|
|
)?),
|
|
stuck_threshold: Duration::from_secs(parse_optional_env(
|
|
"AGENT_STUCK_THRESHOLD_SECS",
|
|
settings.agent.stuck_threshold_secs,
|
|
)?),
|
|
repair_check_interval: Duration::from_secs(parse_optional_env(
|
|
"SELF_REPAIR_CHECK_INTERVAL_SECS",
|
|
settings.agent.repair_check_interval_secs,
|
|
)?),
|
|
max_repair_attempts: parse_optional_env(
|
|
"SELF_REPAIR_MAX_ATTEMPTS",
|
|
settings.agent.max_repair_attempts,
|
|
)?,
|
|
use_planning: parse_bool_env("AGENT_USE_PLANNING", settings.agent.use_planning)?,
|
|
session_idle_timeout: Duration::from_secs(parse_optional_env(
|
|
"SESSION_IDLE_TIMEOUT_SECS",
|
|
settings.agent.session_idle_timeout_secs,
|
|
)?),
|
|
allow_local_tools: parse_bool_env("ALLOW_LOCAL_TOOLS", false)?,
|
|
max_cost_per_day_cents: parse_option_env("MAX_COST_PER_DAY_CENTS")?,
|
|
max_actions_per_hour: parse_option_env("MAX_ACTIONS_PER_HOUR")?,
|
|
max_cost_per_user_per_day_cents: parse_option_env("MAX_COST_PER_USER_PER_DAY_CENTS")?,
|
|
max_tool_iterations: parse_optional_env(
|
|
"AGENT_MAX_TOOL_ITERATIONS",
|
|
settings.agent.max_tool_iterations,
|
|
)?,
|
|
auto_approve_tools: parse_bool_env(
|
|
"AGENT_AUTO_APPROVE_TOOLS",
|
|
settings.agent.auto_approve_tools,
|
|
)?,
|
|
default_timezone: {
|
|
let tz: String = parse_optional_env(
|
|
"DEFAULT_TIMEZONE",
|
|
settings.agent.default_timezone.clone(),
|
|
)?;
|
|
if crate::timezone::parse_timezone(&tz).is_none() {
|
|
return Err(ConfigError::InvalidValue {
|
|
key: "DEFAULT_TIMEZONE".into(),
|
|
message: format!("invalid IANA timezone: '{tz}'"),
|
|
});
|
|
}
|
|
tz
|
|
},
|
|
max_tokens_per_job: parse_optional_env(
|
|
"AGENT_MAX_TOKENS_PER_JOB",
|
|
settings.agent.max_tokens_per_job,
|
|
)?,
|
|
multi_tenant: parse_bool_env(
|
|
"MULTI_TENANT",
|
|
optional_env("GATEWAY_USER_TOKENS")?.is_some(),
|
|
)?,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_default_timezone_rejects_invalid() {
|
|
let mut settings = Settings::default();
|
|
settings.agent.default_timezone = "Fake/Zone".to_string();
|
|
|
|
let result = AgentConfig::resolve(&settings);
|
|
assert!(result.is_err(), "invalid IANA timezone should be rejected");
|
|
}
|
|
|
|
#[test]
|
|
fn test_default_timezone_accepts_valid() {
|
|
let settings = Settings::default(); // default is "UTC"
|
|
let config = AgentConfig::resolve(&settings).expect("resolve");
|
|
assert_eq!(config.default_timezone, "UTC");
|
|
}
|
|
}
|