Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / infra / agents / agentd / src / config.rs

config.rs175 lines · 7.2 KiBRaw
1//! Process-wide configuration, loaded once at startup from the environment.
2//!
3//! Every value comes from the compose file / env_file. Secrets are held in
4//! memory only; agentd never forwards them to the runner, the browser, or
5//! log/event output.
6
7use std::path::PathBuf;
8
9/// Test-only timeout squeezes: respected by [`Config::from_env`] when the
10/// `AGENTD_TEST_*` variables are set; ignored in production.
11#[derive(Debug, Clone, Default)]
12pub struct TestTuning {
13 /// Poll interval for scheduler + release-signal watch, in milliseconds.
14 pub poll_ms: Option<u64>,
15 /// How long `build` waits for the release signal to clear, in seconds.
16 pub build_wait_secs: Option<u64>,
17 /// Per-tool-call context/char budget overrides (unit tests use these
18 /// without standing up the HTTP server).
19 pub context_chars: Option<usize>,
20}
21
22#[derive(Debug, Clone)]
23pub struct Config {
24 /// Key for OpenRouter chat completions. Never logged or exposed.
25 pub openrouter_api_key: String,
26 /// Base URL for OpenRouter; overridden in tests to point at a mock.
27 pub openrouter_base_url: String,
28 /// Argon2 PHC hash of the single operator password.
29 pub password_hash: String,
30 /// Git push token (`Authorization: Bearer <token>`). Never logged.
31 pub git_push_token: String,
32 /// Remote repo URL, e.g. `https://git.maproj.org/Modern-Apps.git`.
33 pub repo_url: String,
34 /// Public origin, e.g. `https://agents.maproj.org` (Origin checks).
35 pub public_origin: String,
36 /// Default model sent to OpenRouter when the task form leaves it blank.
37 pub default_model: String,
38 /// Runner base URL, e.g. `http://builder:7070`.
39 pub runner_url: String,
40 /// Directory holding `agentd.db` (SQLite).
41 pub state_dir: PathBuf,
42 /// Shared bare mirror, read-only for the builder.
43 pub mirror_dir: PathBuf,
44 /// Presence of this file means a prod release build is running.
45 pub release_signal: PathBuf,
46 /// Listen address, e.g. `0.0.0.0:8080`.
47 pub listen: String,
48 /// Number of concurrent agent tasks (scheduler workers).
49 pub slots: usize,
50 /// `owner/name` repo the GitHub tools talk to (issues read + after-push
51 /// comment/label). Mirrors `GitHub::new_from_env("vayun-mathur/Modern-Apps")`.
52 pub github_repo: String,
53 /// GitHub API base URL override (tests point at a mock server).
54 pub github_api_base: String,
55 /// GitHub token for issue read/comment/label (from `AGENTD_GITHUB_TOKEN`,
56 /// empty when unset). Held in memory only; never sent to the sandbox.
57 pub github_token: Option<String>,
58 /// Test tuning (timeouts only). `None` in production.
59 pub test: TestTuning,
60 // -- test-only network flags (never read in prod paths) --
61 /// Test fixture: skip the real release-signal wait.
62 pub test_skip_release_wait: bool,
63 /// Automatic issue triage poller (`AGENTD_TRIAGE`; default on, `0` off).
64 pub triage_enabled: bool,
65 /// Triage poll period in seconds (`AGENTD_TRIAGE_INTERVAL_SECS`; 120).
66 pub triage_interval_secs: u64,
67}
68
69impl Config {
70 fn required(name: &str) -> Result<String, String> {
71 std::env::var(name)
72 .map(|v| {
73 let v = v.trim().to_string();
74 v
75 })
76 .map_err(|_| format!("missing required env var {name}"))
77 .and_then(|v| {
78 if v.is_empty() {
79 Err(format!("missing required env var {name} (empty)"))
80 } else {
81 Ok(v)
82 }
83 })
84 }
85
86 fn optional(name: &str, default: &str) -> String {
87 std::env::var(name)
88 .ok()
89 .map(|v| v.trim().to_string())
90 .filter(|v| !v.is_empty())
91 .unwrap_or_else(|| default.to_string())
92 }
93
94 pub fn from_env() -> Result<Config, String> {
95 let test = TestTuning {
96 poll_ms: std::env::var("AGENTD_TEST_POLL_MS")
97 .ok()
98 .and_then(|v| v.parse().ok()),
99 build_wait_secs: std::env::var("AGENTD_TEST_BUILD_WAIT_SECS")
100 .ok()
101 .and_then(|v| v.parse().ok()),
102 context_chars: std::env::var("AGENTD_TEST_CONTEXT_CHARS")
103 .ok()
104 .and_then(|v| v.parse().ok()),
105 };
106 let slots: usize = Self::optional("AGENTD_SLOTS", "4")
107 .parse()
108 .map_err(|_| "AGENTD_SLOTS must be a positive integer".to_string())?;
109 if slots == 0 {
110 return Err("AGENTD_SLOTS must be >= 1".to_string());
111 }
112 Ok(Config {
113 openrouter_api_key: Self::required("OPENROUTER_API_KEY")?,
114 openrouter_base_url: Self::optional(
115 "OPENROUTER_BASE_URL",
116 "https://openrouter.ai/api/v1",
117 ),
118 password_hash: Self::required("AGENTD_PASSWORD_HASH")?,
119 git_push_token: Self::required("GIT_AGENT_PUSH_TOKEN")?,
120 repo_url: Self::optional("AGENTD_REPO_URL", "https://git.maproj.org/Modern-Apps.git"),
121 public_origin: Self::required("AGENTD_PUBLIC_ORIGIN")?,
122 default_model: Self::optional("AGENTD_DEFAULT_MODEL", "anthropic/claude-sonnet-4"),
123 runner_url: Self::optional("AGENTD_RUNNER_URL", "http://builder:7070"),
124 state_dir: PathBuf::from(Self::optional("AGENTD_STATE_DIR", "/state")),
125 mirror_dir: PathBuf::from(Self::optional(
126 "AGENTD_MIRROR_DIR",
127 "/mirror/Modern-Apps.git",
128 )),
129 release_signal: PathBuf::from(Self::optional(
130 "AGENTD_RELEASE_SIGNAL",
131 "/signal/release",
132 )),
133 listen: Self::optional("AGENTD_LISTEN", "0.0.0.0:8080"),
134 slots,
135 github_repo: Self::optional("AGENTD_GITHUB_REPO", "vayun-mathur/Modern-Apps"),
136 github_api_base: Self::optional("AGENTD_GITHUB_API", crate::github::DEFAULT_BASE_URL),
137 github_token: std::env::var("AGENTD_GITHUB_TOKEN")
138 .ok()
139 .filter(|t| !t.trim().is_empty()),
140 test,
141 test_skip_release_wait: false,
142 triage_enabled: Self::optional("AGENTD_TRIAGE", "1") != "0",
143 triage_interval_secs: Self::optional("AGENTD_TRIAGE_INTERVAL_SECS", "120")
144 .parse()
145 .unwrap_or(120),
146 })
147 }
148
149 /// Release-signal poll interval.
150 pub fn poll_interval(&self) -> std::time::Duration {
151 std::time::Duration::from_millis(self.test.poll_ms.unwrap_or(3_000))
152 }
153
154 /// Max time a `build` tool call waits for the release signal to clear.
155 pub fn build_wait_limit(&self) -> std::time::Duration {
156 std::time::Duration::from_secs(self.test.build_wait_secs.unwrap_or(60 * 60))
157 }
158
159 ///conversation context budget (chars) before old tool results are trimmed.
160 pub fn context_chars(&self) -> usize {
161 self.test.context_chars.unwrap_or(120_000)
162 }
163
164 pub fn redacted_debug(&self) -> String {
165 format!(
166 "runner={} origin={} slots={} state={} mirror={} model={}",
167 self.runner_url,
168 self.public_origin,
169 self.slots,
170 self.state_dir.display(),
171 self.mirror_dir.display(),
172 self.default_model,
173 )
174 }
175}