phalus 0.7.0

Private Headless Automated License Uncoupling System — AI-powered clean room software reimplementation
Documentation
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum ConfigError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
    #[error("Parse error: {0}")]
    Parse(#[from] toml::de::Error),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct RetryConfig {
    /// Maximum number of retry attempts (not counting the initial attempt).
    pub max_retries: u32,
    /// Initial backoff in milliseconds; doubles on each retry.
    pub initial_backoff_ms: u64,
    /// Per-request timeout in seconds.
    pub timeout_secs: u64,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: 3,
            initial_backoff_ms: 500,
            timeout_secs: 600,
        }
    }
}

#[derive(Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct LlmConfig {
    pub agent_a_provider: String,
    pub agent_a_model: String,
    pub agent_a_api_key: String,
    pub agent_a_base_url: String,
    pub agent_a_max_tokens: u32,
    pub agent_b_provider: String,
    pub agent_b_model: String,
    pub agent_b_api_key: String,
    pub agent_b_base_url: String,
    pub agent_b_max_tokens: u32,
    pub retry: RetryConfig,
}

impl Default for LlmConfig {
    fn default() -> Self {
        Self {
            agent_a_provider: "anthropic".to_string(),
            agent_a_model: "claude-sonnet-4-6".to_string(),
            agent_a_api_key: String::new(),
            agent_a_base_url: String::new(),
            agent_a_max_tokens: 16384,
            agent_b_provider: "anthropic".to_string(),
            agent_b_model: "claude-sonnet-4-6".to_string(),
            agent_b_api_key: String::new(),
            agent_b_base_url: String::new(),
            agent_b_max_tokens: 65536,
            retry: RetryConfig::default(),
        }
    }
}

impl std::fmt::Debug for LlmConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LlmConfig")
            .field("agent_a_provider", &self.agent_a_provider)
            .field("agent_a_model", &self.agent_a_model)
            .field(
                "agent_a_api_key",
                &if self.agent_a_api_key.is_empty() {
                    "(empty)"
                } else {
                    "***"
                },
            )
            .field("agent_a_base_url", &self.agent_a_base_url)
            .field("agent_a_max_tokens", &self.agent_a_max_tokens)
            .field("agent_b_provider", &self.agent_b_provider)
            .field("agent_b_model", &self.agent_b_model)
            .field(
                "agent_b_api_key",
                &if self.agent_b_api_key.is_empty() {
                    "(empty)"
                } else {
                    "***"
                },
            )
            .field("agent_b_base_url", &self.agent_b_base_url)
            .field("agent_b_max_tokens", &self.agent_b_max_tokens)
            .field("retry", &self.retry)
            .finish()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct IsolationConfig {
    pub mode: String,
    /// Docker image used when isolation mode is "container"
    pub docker_image: String,
    /// Memory limit for the isolation container (e.g. "256m")
    pub memory_limit: String,
    /// CPU limit for the isolation container (e.g. "1.0")
    pub cpu_limit: String,
    /// Seconds before the container run is considered timed out
    pub timeout_secs: u64,
    /// Docker network mode for the isolation container ("none", "host", etc.)
    pub network_mode: String,
    /// Maximum number of PIDs in the isolation container
    pub pids_limit: u32,
}

impl Default for IsolationConfig {
    fn default() -> Self {
        Self {
            mode: "context".to_string(),
            docker_image: "alpine:3".to_string(),
            memory_limit: "256m".to_string(),
            cpu_limit: "1.0".to_string(),
            timeout_secs: 60,
            network_mode: "none".to_string(),
            pids_limit: 64,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct LimitsConfig {
    pub max_packages_per_job: u32,
    pub max_package_size_mb: u32,
    pub concurrency: u32,
}

impl Default for LimitsConfig {
    fn default() -> Self {
        Self {
            max_packages_per_job: 50,
            max_package_size_mb: 10,
            concurrency: 3,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct ValidationConfig {
    pub similarity_threshold: f64,
    pub run_tests: bool,
    pub syntax_check: bool,
}

impl Default for ValidationConfig {
    fn default() -> Self {
        Self {
            similarity_threshold: 0.70,
            run_tests: true,
            syntax_check: true,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct OutputConfig {
    pub default_license: String,
    pub output_dir: String,
    pub include_csp: bool,
    pub include_audit: bool,
}

impl Default for OutputConfig {
    fn default() -> Self {
        Self {
            default_license: "mit".to_string(),
            output_dir: "./phalus-output".to_string(),
            include_csp: true,
            include_audit: true,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct WebConfig {
    pub enabled: bool,
    pub host: String,
    pub port: u16,
}

impl Default for WebConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            host: "127.0.0.1".to_string(),
            port: 3000,
        }
    }
}

#[derive(Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct DocFetcherConfig {
    pub max_readme_size_kb: u32,
    pub max_type_def_size_kb: u32,
    pub max_code_example_lines: u32,
    pub github_token: String,
}

impl Default for DocFetcherConfig {
    fn default() -> Self {
        Self {
            max_readme_size_kb: 500,
            max_type_def_size_kb: 200,
            max_code_example_lines: 10,
            github_token: String::new(),
        }
    }
}

impl std::fmt::Debug for DocFetcherConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DocFetcherConfig")
            .field("max_readme_size_kb", &self.max_readme_size_kb)
            .field("max_type_def_size_kb", &self.max_type_def_size_kb)
            .field("max_code_example_lines", &self.max_code_example_lines)
            .field(
                "github_token",
                &if self.github_token.is_empty() {
                    "(empty)"
                } else {
                    "***"
                },
            )
            .finish()
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct PhalusConfig {
    pub llm: LlmConfig,
    pub isolation: IsolationConfig,
    pub limits: LimitsConfig,
    pub validation: ValidationConfig,
    pub output: OutputConfig,
    pub web: WebConfig,
    pub doc_fetcher: DocFetcherConfig,
}

impl PhalusConfig {
    /// Returns the default config file path: ~/.phalus/config.toml
    pub fn default_path() -> PathBuf {
        dirs::home_dir()
            .unwrap_or_else(|| PathBuf::from("."))
            .join(".phalus")
            .join("config.toml")
    }

    /// Load config from the default path, falling back to defaults if not found.
    pub fn load() -> Result<Self, ConfigError> {
        let path = Self::default_path();
        if path.exists() {
            Self::load_from_file(&path)
        } else {
            Ok(Self::default())
        }
    }

    /// Load config from a specific file path.
    pub fn load_from_file(path: &Path) -> Result<Self, ConfigError> {
        let contents = std::fs::read_to_string(path)?;
        let config: Self = toml::from_str(&contents)?;
        Ok(config)
    }

    /// Apply PHALUS_* environment variable overrides.
    ///
    /// Variables use double underscore for nesting, e.g.:
    ///   PHALUS_LLM__AGENT_A_MODEL=gpt-4
    pub fn with_env_overrides(mut config: Self) -> Self {
        for (key, value) in std::env::vars() {
            let Some(rest) = key.strip_prefix("PHALUS_") else {
                continue;
            };
            // Split on __ to get section and field
            let parts: Vec<&str> = rest.splitn(2, "__").collect();
            if parts.len() != 2 {
                continue;
            }
            let section = parts[0].to_ascii_lowercase();
            let field = parts[1].to_ascii_lowercase();

            match section.as_str() {
                "llm" => apply_llm_override(&mut config.llm, &field, &value),
                "isolation" => apply_isolation_override(&mut config.isolation, &field, &value),
                "limits" => apply_limits_override(&mut config.limits, &field, &value),
                "validation" => apply_validation_override(&mut config.validation, &field, &value),
                "output" => apply_output_override(&mut config.output, &field, &value),
                "web" => apply_web_override(&mut config.web, &field, &value),
                "doc_fetcher" => {
                    apply_doc_fetcher_override(&mut config.doc_fetcher, &field, &value)
                }
                _ => {}
            }
        }
        config
    }
}

fn apply_llm_override(cfg: &mut LlmConfig, field: &str, value: &str) {
    match field {
        "agent_a_provider" => cfg.agent_a_provider = value.to_string(),
        "agent_a_model" => cfg.agent_a_model = value.to_string(),
        "agent_a_api_key" => cfg.agent_a_api_key = value.to_string(),
        "agent_a_base_url" => cfg.agent_a_base_url = value.to_string(),
        "agent_a_max_tokens" => {
            if let Ok(v) = value.parse() {
                cfg.agent_a_max_tokens = v;
            }
        }
        "agent_b_provider" => cfg.agent_b_provider = value.to_string(),
        "agent_b_model" => cfg.agent_b_model = value.to_string(),
        "agent_b_api_key" => cfg.agent_b_api_key = value.to_string(),
        "agent_b_base_url" => cfg.agent_b_base_url = value.to_string(),
        "agent_b_max_tokens" => {
            if let Ok(v) = value.parse() {
                cfg.agent_b_max_tokens = v;
            }
        }
        "retry_max_retries" => {
            if let Ok(v) = value.parse() {
                cfg.retry.max_retries = v;
            }
        }
        "retry_initial_backoff_ms" => {
            if let Ok(v) = value.parse() {
                cfg.retry.initial_backoff_ms = v;
            }
        }
        "retry_timeout_secs" => {
            if let Ok(v) = value.parse() {
                cfg.retry.timeout_secs = v;
            }
        }
        _ => {}
    }
}

fn apply_isolation_override(cfg: &mut IsolationConfig, field: &str, value: &str) {
    match field {
        "mode" => cfg.mode = value.to_string(),
        "docker_image" => cfg.docker_image = value.to_string(),
        "memory_limit" => cfg.memory_limit = value.to_string(),
        "cpu_limit" => cfg.cpu_limit = value.to_string(),
        "timeout_secs" => {
            if let Ok(v) = value.parse() {
                cfg.timeout_secs = v;
            }
        }
        "network_mode" => cfg.network_mode = value.to_string(),
        "pids_limit" => {
            if let Ok(v) = value.parse() {
                cfg.pids_limit = v;
            }
        }
        _ => {}
    }
}

fn apply_limits_override(cfg: &mut LimitsConfig, field: &str, value: &str) {
    match field {
        "max_packages_per_job" => {
            if let Ok(v) = value.parse() {
                cfg.max_packages_per_job = v;
            }
        }
        "max_package_size_mb" => {
            if let Ok(v) = value.parse() {
                cfg.max_package_size_mb = v;
            }
        }
        "concurrency" => {
            if let Ok(v) = value.parse() {
                cfg.concurrency = v;
            }
        }
        _ => {}
    }
}

fn apply_validation_override(cfg: &mut ValidationConfig, field: &str, value: &str) {
    match field {
        "similarity_threshold" => {
            if let Ok(v) = value.parse() {
                cfg.similarity_threshold = v;
            }
        }
        "run_tests" => {
            if let Ok(v) = value.parse() {
                cfg.run_tests = v;
            }
        }
        "syntax_check" => {
            if let Ok(v) = value.parse() {
                cfg.syntax_check = v;
            }
        }
        _ => {}
    }
}

fn apply_output_override(cfg: &mut OutputConfig, field: &str, value: &str) {
    match field {
        "default_license" => cfg.default_license = value.to_string(),
        "output_dir" => cfg.output_dir = value.to_string(),
        "include_csp" => {
            if let Ok(v) = value.parse() {
                cfg.include_csp = v;
            }
        }
        "include_audit" => {
            if let Ok(v) = value.parse() {
                cfg.include_audit = v;
            }
        }
        _ => {}
    }
}

fn apply_web_override(cfg: &mut WebConfig, field: &str, value: &str) {
    match field {
        "enabled" => {
            if let Ok(v) = value.parse() {
                cfg.enabled = v;
            }
        }
        "host" => cfg.host = value.to_string(),
        "port" => {
            if let Ok(v) = value.parse() {
                cfg.port = v;
            }
        }
        _ => {}
    }
}

fn apply_doc_fetcher_override(cfg: &mut DocFetcherConfig, field: &str, value: &str) {
    match field {
        "max_readme_size_kb" => {
            if let Ok(v) = value.parse() {
                cfg.max_readme_size_kb = v;
            }
        }
        "max_type_def_size_kb" => {
            if let Ok(v) = value.parse() {
                cfg.max_type_def_size_kb = v;
            }
        }
        "max_code_example_lines" => {
            if let Ok(v) = value.parse() {
                cfg.max_code_example_lines = v;
            }
        }
        "github_token" => cfg.github_token = value.to_string(),
        _ => {}
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    #[test]
    fn test_default_config() {
        let config = PhalusConfig::default();
        assert_eq!(config.llm.agent_a_model, "claude-sonnet-4-6");
        assert_eq!(config.limits.max_packages_per_job, 50);
        assert_eq!(config.validation.similarity_threshold, 0.70);
        assert_eq!(config.output.default_license, "mit");
    }

    #[test]
    fn test_load_from_toml() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        let mut f = std::fs::File::create(&path).unwrap();
        writeln!(
            f,
            r#"
[llm]
agent_a_model = "gpt-4"

[limits]
max_packages_per_job = 10
"#
        )
        .unwrap();

        let config = PhalusConfig::load_from_file(&path).unwrap();
        assert_eq!(config.llm.agent_a_model, "gpt-4");
        assert_eq!(config.limits.max_packages_per_job, 10);
        assert_eq!(config.validation.similarity_threshold, 0.70);
    }

    #[test]
    fn test_env_override() {
        unsafe {
            std::env::set_var("PHALUS_LLM__AGENT_A_MODEL", "test-model");
        }
        let config = PhalusConfig::with_env_overrides(PhalusConfig::default());
        assert_eq!(config.llm.agent_a_model, "test-model");
        unsafe {
            std::env::remove_var("PHALUS_LLM__AGENT_A_MODEL");
        }
    }
}