Skip to main content

harn_cli/commands/
mod.rs

1pub(crate) mod agents_conformance;
2pub(crate) mod bench;
3pub(crate) mod canon;
4pub(crate) mod check;
5pub(crate) mod codemod;
6pub(crate) mod config_cmd;
7pub(crate) mod conformance_helper;
8pub(crate) mod connect;
9pub(crate) mod connector;
10pub(crate) mod connector_schema_codegen;
11pub(crate) mod contracts;
12pub(crate) mod counterfactual;
13pub(crate) mod crystallize;
14pub mod demo;
15pub(crate) mod dev;
16pub(crate) mod diagnostics_catalog;
17pub(crate) mod dispatch_explain;
18pub(crate) mod doc;
19pub(crate) mod doctor;
20pub(crate) mod dump_highlight_keywords;
21pub(crate) mod dump_protocol_artifacts;
22pub(crate) mod dump_trigger_quickref;
23pub mod eval_coding_agent;
24pub(crate) mod eval_coding_agent_preset;
25pub mod eval_context;
26pub(crate) mod eval_model_selector;
27pub mod eval_prompt;
28pub(crate) mod eval_prompt_context;
29pub(crate) mod eval_scope_triage;
30pub mod eval_skill_gate;
31pub(crate) mod eval_tool_calls;
32pub(crate) mod explain;
33pub(crate) mod fix;
34pub mod flow;
35pub(crate) mod graph;
36pub(crate) mod guard;
37pub(crate) mod hardware;
38pub(crate) mod host;
39pub(crate) mod init;
40pub(crate) mod json_schemas;
41pub(crate) mod local;
42pub(crate) mod local_readiness;
43pub(crate) mod mcp;
44pub(crate) mod merge_captain;
45pub(crate) mod merge_captain_mock;
46pub(crate) mod models;
47pub mod orchestrator;
48pub mod pack;
49pub(crate) mod package_scaffold;
50pub(crate) mod parse_tokens;
51pub mod persona;
52pub mod persona_activation;
53pub mod persona_apply;
54pub mod persona_dispatch;
55pub mod persona_doctor;
56pub mod persona_prompt;
57pub mod persona_scaffold;
58pub mod persona_supervision;
59#[cfg(test)]
60pub(crate) mod persona_test_support;
61pub(crate) mod pg_codegen;
62pub mod playground;
63pub(crate) mod portal;
64pub mod precompile;
65pub(crate) mod protocol_conformance;
66pub(crate) mod provider;
67pub(crate) mod provider_capabilities;
68pub(crate) mod provider_limits;
69pub(crate) mod provider_report;
70pub(crate) mod provider_support;
71pub(crate) mod provider_tool_calibrate;
72pub(crate) mod providers;
73pub(crate) use provider_tool_calibrate::run as run_tool_calibrate;
74pub(crate) mod quickstart;
75pub(crate) mod repl;
76pub(crate) mod replay;
77pub(crate) mod routes;
78pub(crate) mod rule;
79pub(crate) mod rules_cli;
80pub mod run;
81pub(crate) mod scaffold_common;
82pub(crate) mod scan;
83pub(crate) mod serve;
84pub(crate) mod session;
85pub(crate) mod skill;
86pub(crate) mod skills;
87pub(crate) mod supervisor;
88pub(crate) mod test;
89pub mod test_bench;
90pub(crate) mod test_worker;
91pub mod time;
92pub(crate) mod tool;
93pub(crate) mod tool_mode_parity;
94pub(crate) mod trace;
95pub mod trigger;
96pub(crate) mod trust;
97pub(crate) mod try_cmd;
98pub(crate) mod upgrade;
99pub(crate) mod usage;
100pub(crate) mod viz;
101pub(crate) mod workflow;
102
103use std::path::{Path, PathBuf};
104
105use ignore::WalkBuilder;
106
107/// Nearest-rank percentile over a pre-sorted slice. `quantile` is clamped to
108/// `[0, 1]`; returns `None` for an empty slice. Shared by the latency
109/// summaries in the eval and orchestrator stats commands.
110pub(crate) fn nearest_rank_percentile(sorted: &[u64], quantile: f64) -> Option<u64> {
111    if sorted.is_empty() {
112        return None;
113    }
114    let rank = ((sorted.len() as f64 * quantile.clamp(0.0, 1.0)).ceil() as usize)
115        .saturating_sub(1)
116        .min(sorted.len() - 1);
117    sorted.get(rank).copied()
118}
119
120const GENERATED_SOURCE_WALK_DIRS: &[&str] = &[
121    ".burin",
122    ".build",
123    ".claude",
124    ".codex",
125    ".git",
126    ".harn",
127    ".harn-runs",
128    ".next",
129    ".svelte-kit",
130    ".turbo",
131    ".venv",
132    "build",
133    "coverage",
134    "dist",
135    "node_modules",
136    "target",
137];
138
139pub(crate) fn should_skip_recursive_source_dir(dir: &Path) -> bool {
140    let Some(name) = dir.file_name().and_then(|name| name.to_str()) else {
141        return false;
142    };
143    GENERATED_SOURCE_WALK_DIRS.contains(&name) || name.starts_with(".harn-")
144}
145
146pub(crate) fn should_skip_recursive_source_file(file: &Path) -> bool {
147    let Some(name) = file.file_name().and_then(|name| name.to_str()) else {
148        return false;
149    };
150    name.starts_with(".harn-")
151}
152
153#[derive(Default)]
154pub(crate) struct SourceTargets {
155    pub(crate) harn: Vec<PathBuf>,
156    pub(crate) prompts: Vec<PathBuf>,
157}
158
159impl SourceTargets {
160    fn sort_and_dedup(&mut self) {
161        self.harn.sort();
162        self.harn.dedup();
163        self.prompts.sort();
164        self.prompts.dedup();
165    }
166}
167
168pub(crate) fn collect_source_targets(
169    targets: &[&str],
170    include_harn: bool,
171    include_prompts: bool,
172) -> SourceTargets {
173    let mut files = SourceTargets::default();
174    for target in targets {
175        let path = Path::new(target);
176        if path.is_dir() {
177            collect_source_targets_dir(path, include_harn, include_prompts, &mut files);
178        } else {
179            push_matching_source_target(path, include_harn, include_prompts, false, &mut files);
180        }
181    }
182    files.sort_and_dedup();
183    files
184}
185
186fn collect_source_targets_dir(
187    dir: &Path,
188    include_harn: bool,
189    include_prompts: bool,
190    files: &mut SourceTargets,
191) {
192    let root = dir.to_path_buf();
193    let mut walker = WalkBuilder::new(dir);
194    walker
195        .hidden(false)
196        .ignore(true)
197        .git_ignore(true)
198        .git_global(true)
199        .git_exclude(true)
200        .require_git(false)
201        .parents(true)
202        .follow_links(false)
203        .filter_entry(move |entry| {
204            let path = entry.path();
205            if path == root {
206                return true;
207            }
208            if path.is_dir() {
209                !should_skip_recursive_source_dir(path)
210            } else {
211                !should_skip_recursive_source_file(path)
212            }
213        });
214
215    for entry in walker.build().filter_map(Result::ok) {
216        let path = entry.path();
217        if entry
218            .file_type()
219            .is_some_and(|file_type| file_type.is_file())
220        {
221            push_matching_source_target(path, include_harn, include_prompts, true, files);
222        }
223    }
224}
225
226fn push_matching_source_target(
227    path: &Path,
228    include_harn: bool,
229    include_prompts: bool,
230    honor_skip_marker: bool,
231    files: &mut SourceTargets,
232) {
233    if include_prompts && is_harn_prompt_file(path) {
234        files.prompts.push(path.to_path_buf());
235    } else if include_harn && is_harn_program_file(path) {
236        let skip_marker = path.with_extension("conformance-skip");
237        if !honor_skip_marker || !skip_marker.exists() {
238            files.harn.push(path.to_path_buf());
239        }
240    }
241}
242
243pub(crate) fn is_harn_program_file(path: &Path) -> bool {
244    let Some(name) = path.file_name().and_then(|name| name.to_str()) else {
245        return false;
246    };
247    if name.ends_with(".harn.prompt") || name.ends_with(".prompt") {
248        return false;
249    }
250    name.ends_with(".harn") || name.ends_with(".harn.txt")
251}
252
253pub(crate) fn is_harn_prompt_file(path: &Path) -> bool {
254    let Some(name) = path.file_name().and_then(|name| name.to_str()) else {
255        return false;
256    };
257    name.ends_with(".harn.prompt") || name.ends_with(".prompt")
258}
259
260/// Recursively collect `.harn` files under `dir`, sorted by path. Files with a
261/// sibling `<name>.conformance-skip` marker are excluded — used to temporarily
262/// park tests that are tracking a known regression in an issue so `make test`
263/// + `harn test conformance` can stay green while the fix is in flight.
264pub(crate) fn collect_harn_files(dir: &Path, out: &mut Vec<PathBuf>) {
265    if let Ok(entries) = std::fs::read_dir(dir) {
266        let mut entries: Vec<_> = entries.filter_map(|e| e.ok()).collect();
267        entries.sort_by_key(|e| e.path());
268        for entry in entries {
269            let path = entry.path();
270            if path.is_dir() {
271                if should_skip_recursive_source_dir(&path) {
272                    continue;
273                }
274                collect_harn_files(&path, out);
275            } else if should_skip_recursive_source_file(&path) {
276                continue;
277            } else if path.extension().is_some_and(|ext| ext == "harn") {
278                let skip_marker = path.with_extension("conformance-skip");
279                if skip_marker.exists() {
280                    continue;
281                }
282                out.push(path);
283            }
284        }
285    }
286}
287
288#[cfg(test)]
289mod tests {
290    use super::nearest_rank_percentile;
291
292    #[test]
293    fn percentile_handles_empty_and_bounds() {
294        assert_eq!(nearest_rank_percentile(&[], 0.5), None);
295        let data = [10u64, 20, 30, 40, 50];
296        assert_eq!(nearest_rank_percentile(&data, 0.0), Some(10));
297        assert_eq!(nearest_rank_percentile(&data, 0.5), Some(30));
298        assert_eq!(nearest_rank_percentile(&data, 1.0), Some(50));
299        // Out-of-range quantiles clamp instead of panicking.
300        assert_eq!(nearest_rank_percentile(&data, 2.0), Some(50));
301        assert_eq!(nearest_rank_percentile(&[7u64], 0.99), Some(7));
302    }
303}