corp-finance-core 1.1.0

Institutional-grade corporate finance calculations with 128-bit decimal precision — DCF, WACC, comps, LBO, credit metrics, derivatives, fixed income, options, and 60+ specialty modules. No f64 in financials. WASM-compatible.
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
//! Schema validation for managed-agent cookbooks.
//!
//! Pure function: reads files from disk (the cookbook directory), performs
//! structural and file-existence checks, and returns a `ValidateOutput`.
//! No network I/O.
//!
//! Pattern mirrors `crates/corp-finance-core/src/workflows/types.rs` validation helpers.

use std::path::Path;

use crate::managed_agent::types::{
    AgentManifest, CheckAllInput, CheckAllOutput, CheckResult, CookbookOutcome, ValidateInput,
    ValidateOutput, ALLOWED_SLUGS,
};
use crate::CorpFinanceResult;

/// Validate a managed-agent cookbook for a given slug.
///
/// Checks performed:
/// 1. Slug is in the allowlist.
/// 2. `agent.json` exists and parses correctly.
/// 3. Required top-level fields are present (`name`, `model`, `system`).
/// 4. `system.file` path resolves to an existing `.md` file.
/// 5. Each `skills[].from_skill` path resolves to `.claude/skills/<slug>/SKILL.md`.
/// 6. Each `callable_agents[].manifest` path resolves to an existing file.
/// 7. `steering-examples.json` exists.
pub fn validate_manifest(input: &ValidateInput) -> CorpFinanceResult<ValidateOutput> {
    let mut checks: Vec<CheckResult> = Vec::new();
    let slug = &input.slug;

    // 1. Slug allowlist
    checks.push(check_slug_allowlist(slug));

    let cookbook_dir = Path::new(&input.cookbooks_root).join(slug);

    // 2 & 3. agent.json parse + required fields
    let manifest_path = cookbook_dir.join("agent.json");
    let (manifest_opt, manifest_check) = check_manifest_parse(&manifest_path);
    checks.push(manifest_check);

    if let Some(ref manifest) = manifest_opt {
        checks.push(check_required_fields(manifest));

        // 4. system.file resolution
        if let Some(ref file_ref) = manifest.system.file {
            checks.push(check_system_prompt(
                file_ref,
                &cookbook_dir,
                Path::new(&input.agents_root),
            ));
        } else {
            checks.push(CheckResult {
                name: "system_prompt_file".to_string(),
                passed: true,
                detail: "system uses inline text (no file reference)".to_string(),
            });
        }

        // 5. Skills
        for skill in &manifest.skills {
            checks.push(check_skill_path(
                &skill.from_skill,
                Path::new(&input.skills_root),
            ));
        }

        // 6. Callable agent manifests
        for ca in &manifest.callable_agents {
            checks.push(check_subagent_manifest(&ca.manifest, &cookbook_dir));
        }
    }

    // 7. steering-examples.json
    checks.push(check_steering_examples(&cookbook_dir));

    let ok = checks.iter().all(|c| c.passed);
    Ok(ValidateOutput {
        slug: slug.clone(),
        ok,
        checks,
    })
}

/// Validate every cookbook directory found under `cookbooks_root`.
///
/// Walks `cookbooks_root` for subdirectories containing an `agent.json`,
/// runs `validate_manifest` against each, and returns an aggregate summary.
/// Slugs not in `ALLOWED_SLUGS` will produce a failed check inside their
/// own `ValidateOutput` — they are still discovered and reported, not skipped.
pub fn validate_all(input: &CheckAllInput) -> CorpFinanceResult<CheckAllOutput> {
    let root = Path::new(&input.cookbooks_root);
    if !root.is_dir() {
        return Err(crate::error::CorpFinanceError::InvalidInput {
            field: "cookbooks_root".to_string(),
            reason: format!("not a directory: {}", root.display()),
        });
    }

    let mut outcomes: Vec<CookbookOutcome> = Vec::new();

    let entries = std::fs::read_dir(root)
        .map_err(|e| format!("Cannot read cookbooks root {}: {}", root.display(), e))?;

    let mut slugs: Vec<String> = Vec::new();
    for entry in entries.flatten() {
        let path = entry.path();
        if !path.is_dir() {
            continue;
        }
        if !path.join("agent.json").exists() {
            continue;
        }
        if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
            slugs.push(name.to_string());
        }
    }
    slugs.sort();

    for slug in &slugs {
        let v = validate_manifest(&ValidateInput {
            slug: slug.clone(),
            cookbooks_root: input.cookbooks_root.clone(),
            skills_root: input.skills_root.clone(),
            agents_root: input.agents_root.clone(),
        })?;

        let failed_checks: Vec<String> = v
            .checks
            .iter()
            .filter(|c| !c.passed)
            .map(|c| format!("{}: {}", c.name, c.detail))
            .collect();

        outcomes.push(CookbookOutcome {
            slug: v.slug,
            ok: v.ok,
            failed_checks,
        });
    }

    let passed = outcomes.iter().filter(|o| o.ok).count();
    let failed = outcomes.len() - passed;

    Ok(CheckAllOutput {
        cookbooks_root: input.cookbooks_root.clone(),
        total: outcomes.len(),
        passed,
        failed,
        outcomes,
    })
}

// ---------------------------------------------------------------------------
// Individual check helpers (all return CheckResult)
// ---------------------------------------------------------------------------

fn check_slug_allowlist(slug: &str) -> CheckResult {
    let passed = ALLOWED_SLUGS.contains(&slug);
    CheckResult {
        name: "slug_allowlist".to_string(),
        passed,
        detail: if passed {
            format!("'{}' is an allowed slug", slug)
        } else {
            format!(
                "'{}' is not in the allowed slug list: {:?}",
                slug, ALLOWED_SLUGS
            )
        },
    }
}

fn check_manifest_parse(path: &Path) -> (Option<AgentManifest>, CheckResult) {
    if !path.exists() {
        return (
            None,
            CheckResult {
                name: "agent_json_exists".to_string(),
                passed: false,
                detail: format!("agent.json not found at {}", path.display()),
            },
        );
    }
    match std::fs::read_to_string(path) {
        Err(e) => (
            None,
            CheckResult {
                name: "agent_json_exists".to_string(),
                passed: false,
                detail: format!("Cannot read {}: {}", path.display(), e),
            },
        ),
        Ok(contents) => match serde_json::from_str::<AgentManifest>(&contents) {
            Err(e) => (
                None,
                CheckResult {
                    name: "agent_json_exists".to_string(),
                    passed: false,
                    detail: format!("JSON parse error in {}: {}", path.display(), e),
                },
            ),
            Ok(m) => (
                Some(m),
                CheckResult {
                    name: "agent_json_exists".to_string(),
                    passed: true,
                    detail: "agent.json exists and parses correctly".to_string(),
                },
            ),
        },
    }
}

fn check_required_fields(manifest: &AgentManifest) -> CheckResult {
    let mut missing: Vec<&str> = Vec::new();
    if manifest.name.is_empty() {
        missing.push("name");
    }
    if manifest.model.is_empty() {
        missing.push("model");
    }
    let has_system = manifest.system.file.is_some() || manifest.system.text.is_some();
    if !has_system {
        missing.push("system.file or system.text");
    }
    let passed = missing.is_empty();
    CheckResult {
        name: "required_fields".to_string(),
        passed,
        detail: if passed {
            "name, model, system all present".to_string()
        } else {
            format!("Missing required fields: {}", missing.join(", "))
        },
    }
}

fn check_system_prompt(file_ref: &str, cookbook_dir: &Path, agents_root: &Path) -> CheckResult {
    // Try resolving relative to cookbook dir first, then agents root
    let candidate1 = cookbook_dir.join(file_ref);
    let candidate2 = agents_root.join(
        Path::new(file_ref)
            .file_name()
            .unwrap_or_else(|| std::ffi::OsStr::new(file_ref)),
    );

    let found = candidate1.exists() || candidate2.exists();
    let resolved = if candidate1.exists() {
        candidate1.display().to_string()
    } else {
        candidate2.display().to_string()
    };

    CheckResult {
        name: "system_prompt_file".to_string(),
        passed: found,
        detail: if found {
            format!("System prompt resolved: {}", resolved)
        } else {
            format!(
                "System prompt file '{}' not found (tried {} and {})",
                file_ref,
                candidate1.display(),
                candidate2.display()
            )
        },
    }
}

fn check_skill_path(from_skill: &str, skills_root: &Path) -> CheckResult {
    let skill_md = skills_root.join(from_skill).join("SKILL.md");
    let passed = skill_md.exists();
    CheckResult {
        name: format!("skill:{}", from_skill),
        passed,
        detail: if passed {
            format!("SKILL.md found: {}", skill_md.display())
        } else {
            format!("SKILL.md not found: {}", skill_md.display())
        },
    }
}

fn check_subagent_manifest(manifest_ref: &str, cookbook_dir: &Path) -> CheckResult {
    let path = cookbook_dir.join(manifest_ref);
    let passed = path.exists();
    CheckResult {
        name: format!("subagent:{}", manifest_ref),
        passed,
        detail: if passed {
            format!("Subagent manifest found: {}", path.display())
        } else {
            format!("Subagent manifest not found: {}", path.display())
        },
    }
}

fn check_steering_examples(cookbook_dir: &Path) -> CheckResult {
    let path = cookbook_dir.join("steering-examples.json");
    let passed = path.exists();
    CheckResult {
        name: "steering_examples".to_string(),
        passed,
        detail: if passed {
            "steering-examples.json exists".to_string()
        } else {
            format!("steering-examples.json not found at {}", path.display())
        },
    }
}

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

    #[test]
    fn slug_allowlist_passes_for_known_slugs() {
        assert!(check_slug_allowlist("equity-analyst").passed);
        assert!(check_slug_allowlist("private-markets-analyst").passed);
        assert!(check_slug_allowlist("credit-analyst").passed);
    }

    #[test]
    fn slug_allowlist_fails_for_unknown_slug() {
        let r = check_slug_allowlist("rogue-agent");
        assert!(!r.passed);
        assert!(r.detail.contains("not in the allowed slug list"));
    }

    #[test]
    fn required_fields_passes_when_all_present() {
        let m: AgentManifest = serde_json::from_str(
            r#"{"name":"x","model":"claude-opus-4-7","system":{"text":"hi"},"tools":[],"mcp_servers":[],"skills":[],"callable_agents":[]}"#,
        )
        .unwrap();
        assert!(check_required_fields(&m).passed);
    }

    #[test]
    fn required_fields_fails_on_empty_name() {
        let m: AgentManifest = serde_json::from_str(
            r#"{"name":"","model":"claude-opus-4-7","system":{"text":"hi"},"tools":[],"mcp_servers":[],"skills":[],"callable_agents":[]}"#,
        )
        .unwrap();
        let r = check_required_fields(&m);
        assert!(!r.passed);
        assert!(r.detail.contains("name"));
    }

    #[test]
    fn required_fields_fails_on_missing_system() {
        let m: AgentManifest = serde_json::from_str(
            r#"{"name":"x","model":"claude-opus-4-7","system":{},"tools":[],"mcp_servers":[],"skills":[],"callable_agents":[]}"#,
        )
        .unwrap();
        let r = check_required_fields(&m);
        assert!(!r.passed);
        assert!(r.detail.contains("system"));
    }

    #[test]
    fn manifest_parse_fails_for_nonexistent_file() {
        let (m, r) = check_manifest_parse(Path::new("/tmp/does_not_exist_cfa_test/agent.json"));
        assert!(m.is_none());
        assert!(!r.passed);
    }

    #[test]
    fn skill_path_fails_when_missing() {
        let r = check_skill_path("nonexistent-skill", Path::new("/tmp"));
        assert!(!r.passed);
        assert!(r.detail.contains("not found"));
    }

    #[test]
    fn subagent_manifest_fails_when_missing() {
        let r = check_subagent_manifest("./subagents/ghost.json", Path::new("/tmp"));
        assert!(!r.passed);
    }

    #[test]
    fn validate_all_errors_on_missing_root() {
        let input = CheckAllInput {
            cookbooks_root: "/tmp/cfa_no_such_dir_check_all".to_string(),
            skills_root: "/tmp".to_string(),
            agents_root: "/tmp".to_string(),
        };
        assert!(validate_all(&input).is_err());
    }

    #[test]
    fn validate_all_returns_empty_for_empty_root() {
        let tmp = std::env::temp_dir().join("cfa_check_all_empty");
        let _ = std::fs::remove_dir_all(&tmp);
        std::fs::create_dir_all(&tmp).unwrap();
        let input = CheckAllInput {
            cookbooks_root: tmp.display().to_string(),
            skills_root: "/tmp".to_string(),
            agents_root: "/tmp".to_string(),
        };
        let out = validate_all(&input).unwrap();
        assert_eq!(out.total, 0);
        assert_eq!(out.passed, 0);
        assert_eq!(out.failed, 0);
        let _ = std::fs::remove_dir_all(&tmp);
    }
}