use std::path::PathBuf;
use std::process::ExitCode;
use chrono::{NaiveDate, Utc};
use clap::{Parser, Subcommand, ValueEnum};
use serde_json::{json, Value};
use anamnesis::model::{
compose_reasoning, gen_id, normalize_tags, Claim, ClaimKind, Forecast, NumericForecast,
Outcome, Resolution,
};
use anamnesis::scoring::{self, NumericSample};
use anamnesis::untrusted::printable;
use anamnesis::{mcp, report, store};
#[derive(Parser)]
#[command(
name = "ana",
version,
about = "Anamnesis — an instrument against self-deception",
long_about = "Record what you believe, how sure you are, and why — before the outcome is \
known. Later, face the real shape of your judgement: where you are overconfident, whether you \
can tell truth from falsehood, and how honestly you change your mind."
)]
struct Cli {
#[arg(long, global = true, value_name = "FILE")]
data: Option<PathBuf>,
#[arg(long, global = true)]
json: bool,
#[command(subcommand)]
cmd: Cmd,
}
#[derive(Subcommand)]
enum Cmd {
Add {
statement: String,
#[arg(short, long, value_parser = prob_arg, allow_negative_numbers = true)]
prob: Option<f64>,
#[arg(long, value_name = "LOW..HIGH")]
interval: Option<String>,
#[arg(long, default_value_t = 0.80)]
level: f64,
#[arg(long, value_name = "YYYY-MM-DD")]
by: Option<String>,
#[arg(long, value_delimiter = ',')]
tags: Vec<String>,
#[arg(long)]
because: Option<String>,
#[arg(long, default_value_t = 1.0)]
stake: f64,
#[arg(long, value_parser = prob_arg, allow_negative_numbers = true)]
second_prob: Option<f64>,
#[arg(long)]
reference_class: Option<String>,
#[arg(long, value_name = "COMMAND")]
check: Option<String>,
},
Run {
id: String,
#[arg(last = true, required = true, num_args = 1.., allow_hyphen_values = true)]
command: Vec<String>,
},
Update {
id: String,
#[arg(short, long, value_parser = prob_arg, allow_negative_numbers = true)]
prob: Option<f64>,
#[arg(long, value_name = "LOW..HIGH")]
interval: Option<String>,
#[arg(long)]
level: Option<f64>,
#[arg(long)]
because: Option<String>,
},
Resolve {
id: String,
#[arg(value_enum, ignore_case = true)]
outcome: Option<OutcomeArg>,
#[arg(long)]
value: Option<f64>,
#[arg(long)]
note: Option<String>,
},
List {
#[arg(long)]
open: bool,
#[arg(long)]
resolved: bool,
#[arg(long)]
due: bool,
#[arg(long)]
tag: Option<String>,
#[arg(long)]
void: bool,
},
Show { id: String },
Report {
#[arg(long)]
tag: Option<String>,
#[arg(long, default_value_t = 10)]
bins: usize,
#[arg(long)]
plain: bool,
#[arg(long)]
html: bool,
#[arg(long)]
badge: bool,
},
Decide {
#[arg(long, short = 'p', value_parser = prob_arg, allow_negative_numbers = true)]
prob: f64,
#[arg(long, default_value_t = 1.0)]
stake: f64,
#[arg(long, default_value_t = 0.2)]
verify_cost: f64,
#[arg(long)]
tag: Option<String>,
},
Mcp,
Hook {
#[arg(value_enum)]
event: HookEvent,
},
Demo {
#[arg(long)]
keep: Option<PathBuf>,
#[arg(long)]
plain: bool,
},
Export {
#[arg(long)]
anonymize: bool,
#[arg(long)]
out: Option<PathBuf>,
#[arg(long)]
keep_tags: Option<String>,
},
Import {
file: PathBuf,
#[arg(long)]
dry_run: bool,
},
Void {
id: String,
#[arg(long)]
reason: String,
},
Amend {
id: String,
#[arg(long)]
statement: Option<String>,
#[arg(long)]
tags: Option<String>,
},
Trial {
#[arg(value_enum)]
action: TrialAction,
#[arg(long, value_parser = parse_date)]
from: Option<NaiveDate>,
#[arg(long, default_value_t = anamnesis::trial::DEFAULT_PAIRS)]
pairs: u32,
},
Where,
}
impl Cmd {
fn mutates(&self) -> bool {
matches!(
self,
Cmd::Add { .. }
| Cmd::Update { .. }
| Cmd::Resolve { .. }
| Cmd::Void { .. }
| Cmd::Amend { .. }
| Cmd::Import { .. }
)
}
}
#[derive(Clone, Copy, ValueEnum)]
enum TrialAction {
Start,
Status,
}
#[derive(Clone, Copy, ValueEnum)]
enum HookEvent {
SessionStart,
UserPrompt,
PostTool,
PreTool,
PostToolFailure,
Stop,
}
impl From<HookEvent> for anamnesis::hook::Event {
fn from(e: HookEvent) -> Self {
match e {
HookEvent::SessionStart => anamnesis::hook::Event::SessionStart,
HookEvent::UserPrompt => anamnesis::hook::Event::UserPrompt,
HookEvent::PostTool => anamnesis::hook::Event::PostTool,
HookEvent::PreTool => anamnesis::hook::Event::PreTool,
HookEvent::PostToolFailure => anamnesis::hook::Event::PostToolFailure,
HookEvent::Stop => anamnesis::hook::Event::Stop,
}
}
}
#[derive(Clone, Copy, ValueEnum)]
enum OutcomeArg {
#[value(alias = "true", alias = "y")]
Yes,
#[value(alias = "false", alias = "n")]
No,
}
impl From<OutcomeArg> for Outcome {
fn from(o: OutcomeArg) -> Self {
match o {
OutcomeArg::Yes => Outcome::True,
OutcomeArg::No => Outcome::False,
}
}
}
fn main() -> ExitCode {
let cli = Cli::parse();
if let Cmd::Run { id, command } = &cli.cmd {
return match cmd_run(&cli, id, command) {
Ok(code) => code,
Err(msg) => {
eprintln!("error: {msg}");
ExitCode::FAILURE
}
};
}
match run(cli) {
Ok(()) => ExitCode::SUCCESS,
Err(msg) => {
eprintln!("error: {msg}");
ExitCode::FAILURE
}
}
}
fn cmd_run(cli: &Cli, id: &str, command: &[String]) -> Result<ExitCode, String> {
use anamnesis::hook::{judge_run, RunJudgement};
use anamnesis::model::{collapse_spaces, ResolvedBy};
use anamnesis::untrusted::{line, MAX_LINE};
let path = data_path(cli)?;
let ledger = store::load(&path).map_err(|e| load_error(&path, e))?;
let idx = ledger.index_of(id)?;
let c = &ledger.claims[idx];
let cid = c.id.clone();
if c.is_void() {
return Err(format!("[{cid}] is void, so there is nothing to settle"));
}
if c.is_resolved() {
return Err(format!("[{cid}] is already resolved"));
}
if !matches!(c.kind, ClaimKind::Binary) {
return Err(format!(
"[{cid}] is a numeric claim; an exit status settles a yes/no claim"
));
}
let Some(check) = c.check.clone() else {
return Err(format!(
"[{cid}] has no check pinned, so there is nothing to hold the command to. Log a claim with --check \"<command>\", or settle this one with `ana resolve`."
));
};
if !c.check_matches(command) {
return Err(format!(
"[{cid}] is pinned to `{}`, and you gave `{}`. Nothing was run. The command is fixed when the claim is logged so that it cannot be chosen once the outcome is known.",
line(&check, MAX_LINE),
line(&command.join(" "), MAX_LINE)
));
}
drop(ledger);
let ran = anamnesis::check::run_command(command)
.map_err(|e| format!("could not start `{}`: {e}", line(&command[0], 80)))?;
let Some(code) = ran.code else {
eprintln!(
"ana: the command was ended by a signal, so there is no exit status and [{cid}] was not graded. It is still open."
);
return Ok(ExitCode::FAILURE);
};
let exit = ExitCode::from(anamnesis::check::exit_byte(code));
let cmdline = collapse_spaces(&command.join(" "));
let happened = match judge_run(command, i64::from(code), &ran.facts) {
RunJudgement::Passed => true,
RunJudgement::Failed => false,
RunJudgement::NotGraded(why) => {
eprintln!(
"ana: `{}` exited {code}, but {why}, so [{cid}] was not graded. It is still open.",
line(&cmdline, MAX_LINE)
);
return Ok(exit);
}
};
let _guard = store::lock(&path).map_err(|e| format!("locking {}: {e}", path.display()))?;
let mut ledger = store::load(&path).map_err(|e| load_error(&path, e))?;
let idx = ledger.index_of(&cid)?;
{
let c = &ledger.claims[idx];
if c.is_resolved() || c.is_void() || !c.check_matches(command) {
eprintln!(
"ana: [{cid}] changed while the command ran, so it was not graded. Nothing was written."
);
return Ok(exit);
}
}
let prob = ledger.claims[idx].first_prob().unwrap_or(0.5);
ledger.claims[idx].resolution = Some(Resolution {
at: Utc::now(),
outcome: Some(if happened {
Outcome::True
} else {
Outcome::False
}),
value: None,
note: Some(format!("auto: `{cmdline}` exited {code} (ana run)")),
resolved_by: Some(ResolvedBy::Auto),
});
store::save(&path, &ledger).map_err(|e| format!("saving: {e}"))?;
let brier = (prob - if happened { 1.0 } else { 0.0 }).powi(2);
if cli.json {
out_json(json!({
"id": cid, "outcome": happened, "exit_code": code,
"resolved_by": "auto", "brier": brier, "score_basis": "first",
}));
} else {
let truth = if happened { "TRUE" } else { "FALSE" };
println!(
"[{cid}] resolved {truth}. you said {} → Brier {:.3} on this one",
pct(prob),
brier
);
println!(
" graded from `{}` exiting {code}, the command you pinned before the outcome. Grading is not up to you; choosing that command was.",
line(&cmdline, MAX_LINE)
);
}
Ok(exit)
}
fn home_dir() -> Option<PathBuf> {
#[allow(deprecated)]
std::env::home_dir().filter(|p| !p.as_os_str().is_empty())
}
fn data_path(cli: &Cli) -> Result<PathBuf, String> {
if let Some(p) = &cli.data {
return Ok(p.clone());
}
if let Ok(p) = std::env::var("ANAMNESIS_DATA") {
if !p.is_empty() {
return Ok(PathBuf::from(p));
}
}
match home_dir() {
Some(h) => Ok(h.join(".anamnesis.json")),
None => Err(
"no home directory found, so there is nowhere to keep your ledger.\n \
Pass --data <FILE>, or set ANAMNESIS_DATA, to say where it should live."
.into(),
),
}
}
fn agent_ledger_path() -> Result<PathBuf, String> {
if let Ok(p) = std::env::var("ANAMNESIS_AGENT_DATA") {
if !p.is_empty() {
return Ok(PathBuf::from(p));
}
}
match home_dir() {
Some(h) => Ok(h.join(".anamnesis").join("agent.json")),
None => Err(
"no home directory found, so there is nowhere to keep the agent ledger.\n \
Pass --data <FILE>, or set ANAMNESIS_AGENT_DATA, to say where it should live."
.into(),
),
}
}
fn load_error(path: &std::path::Path, e: std::io::Error) -> String {
let mut msg = format!("reading {}: {e}", path.display());
if e.kind() == std::io::ErrorKind::InvalidData {
let bak = store::backup_path(path);
msg.push_str("\n the ledger was NOT modified.");
if bak.exists() {
msg.push_str(&format!(
"\n last good copy: {} (inspect it before overwriting anything)",
bak.display()
));
}
}
msg
}
fn cmd_where(cli: &Cli) -> Result<(), String> {
let human = data_path(cli);
let agent = agent_ledger_path();
let env = |k: &str| std::env::var(k).ok().filter(|v| !v.is_empty());
if cli.json {
let describe = |p: &Result<PathBuf, String>| match p {
Ok(p) => json!({
"path": p.display().to_string(),
"exists": p.exists(),
"lock": store::lock_path(p).display().to_string(),
"backup": store::backup_path(p).display().to_string(),
}),
Err(e) => json!({ "error": e }),
};
out_json(json!({
"version": env!("CARGO_PKG_VERSION"),
"human_ledger": describe(&human),
"agent_ledger": describe(&agent),
"home": home_dir().map(|h| h.display().to_string()),
"env": {
"ANAMNESIS_DATA": env("ANAMNESIS_DATA"),
"ANAMNESIS_AGENT_DATA": env("ANAMNESIS_AGENT_DATA"),
},
"data_flag": cli.data.as_ref().map(|p| p.display().to_string()),
"hooks": anamnesis::hook::hook_stats().iter().map(|h| json!({
"event": h.event, "last": h.last, "fires": h.fires, "model_bytes": h.model_bytes,
})).collect::<Vec<_>>(),
}));
return Ok(());
}
println!("ana {}", env!("CARGO_PKG_VERSION"));
println!(
"home {}",
home_dir().map_or("(none found)".into(), |h| h.display().to_string())
);
for (label, p, var) in [
("human ledger", &human, "ANAMNESIS_DATA"),
("agent ledger", &agent, "ANAMNESIS_AGENT_DATA"),
] {
match p {
Ok(p) => {
println!(
"\n{label} {} {}",
p.display(),
if p.exists() {
"(exists)"
} else {
"(not created yet)"
}
);
println!(" lock {}", store::lock_path(p).display());
println!(" backup {}", store::backup_path(p).display());
}
Err(e) => println!("\n{label} ERROR: {e}"),
}
match env(var) {
Some(v) => println!(" overridden by {var}={v}"),
None => println!(" {var} is unset"),
}
}
if let Some(p) = &cli.data {
println!("\n--data {} overrides both of the above.", p.display());
}
println!("\nhooks on this machine last ran (UTC) runs to the model");
for h in anamnesis::hook::hook_stats() {
let per = if h.fires > 0 {
format!(
"{} bytes in all, about {} tokens a run",
h.model_bytes,
(h.model_bytes as f64 / h.fires as f64 / 4.0).round()
)
} else {
"-".into()
};
println!(
" {:<22} {:<22} {:>5} {per}",
h.event,
h.last.as_deref().unwrap_or("never"),
h.fires
);
}
println!(" (counted since this engine was installed; tokens are bytes / 4, a rough guide)");
Ok(())
}
fn cmd_demo(cli: &Cli, keep: Option<&std::path::Path>, plain: bool) -> Result<(), String> {
let ledger = anamnesis::demo::ledger();
let today = Utc::now().date_naive();
if cli.json {
println!("{}", report::render_json(&ledger, None, 10, today));
} else if plain {
print!("{}", report::render_plain(&ledger, None, 10, today));
} else {
print!("{}", report::render(&ledger, None, 10, today));
}
match keep {
Some(path) => {
store::save(path, &ledger).map_err(|e| format!("writing {}: {e}", path.display()))?;
if !cli.json {
println!("\nDemo ledger written to {}", path.display());
println!("Poke at it: ana --data {} list", path.display());
}
}
None if !cli.json => {
println!("\nThis was a fictional ledger — nothing of yours was touched.");
println!("Your own first prediction:");
println!(" ana add \"this refactor takes under an hour\" --prob 0.7 --by {today}");
println!(" ana resolve <id> yes|no # the moment reality answers");
println!(" ana report");
println!("\nKeep a copy to explore: ana demo --keep demo.json");
}
None => {}
}
Ok(())
}
fn cmd_export(
ledger: &anamnesis::model::Ledger,
anonymize: bool,
out: Option<&std::path::Path>,
keep_tags: Option<&str>,
) -> Result<(), String> {
if !anonymize {
return Err(
"refusing to export raw statements. Pass --anonymize; there is no other mode.".into(),
);
}
let allowed: Vec<String> = keep_tags
.map(|s| {
s.split(',')
.map(|t| t.trim().to_lowercase())
.filter(|t| !t.is_empty())
.collect()
})
.unwrap_or_else(|| {
["kind", "who", "model", "project"]
.iter()
.map(|s| s.to_string())
.collect()
});
let claims: Vec<Value> = ledger
.claims
.iter()
.enumerate()
.map(|(i, c)| {
let tags: Vec<&String> = c
.tags
.iter()
.filter(|t| {
t.split_once(':')
.is_some_and(|(ns, _)| allowed.iter().any(|a| a == ns))
})
.collect();
let forecasts: Vec<Value> = c
.forecasts
.iter()
.map(|f| {
json!({
"at": f.at.date_naive().to_string(),
"prob": f.prob,
"interval": f.interval,
})
})
.collect();
json!({
"id": format!("c{i:05}"),
"created_at": c.created_at.date_naive().to_string(),
"resolve_by": c.resolve_by.map(|d| d.to_string()),
"kind": c.kind,
"stake": c.stake,
"tags": tags,
"forecasts": forecasts,
"resolution": c.resolution.as_ref().map(|r| json!({
"at": r.at.date_naive().to_string(),
"outcome": r.outcome,
"value": r.value,
"resolved_by": r.resolved_by,
})),
"void": c.void.is_some(),
"amendments": c.amendments.len(),
})
})
.collect();
let doc = json!({
"anonymized": true,
"note": "Statements, reasoning, notes and ids are removed; dates are rounded to the day. Numbers are unchanged.",
"claims": claims,
});
let text = serde_json::to_string_pretty(&doc).map_err(|e| e.to_string())?;
match out {
Some(p) => {
std::fs::write(p, &text).map_err(|e| format!("writing {}: {e}", p.display()))?;
eprintln!(
"wrote {} anonymized claim(s) to {}",
ledger.claims.len(),
p.display()
);
}
None => println!("{text}"),
}
Ok(())
}
fn cmd_import(
cli: &Cli,
path: &std::path::Path,
ledger: &mut anamnesis::model::Ledger,
file: &std::path::Path,
dry_run: bool,
) -> Result<(), String> {
let text =
std::fs::read_to_string(file).map_err(|e| format!("reading {}: {e}", file.display()))?;
let mut rows = text
.lines()
.enumerate()
.filter(|(_, l)| !l.trim().is_empty());
let (_, header) = rows.next().ok_or("the file is empty")?;
let cols: Vec<String> = split_csv(header)
.into_iter()
.map(|c| c.trim().to_lowercase())
.collect();
let col = |name: &str| cols.iter().position(|c| c == name);
let (Some(i_stmt), Some(i_prob)) = (col("statement"), col("prob")) else {
return Err(format!(
"the header must name at least `statement` and `prob`; found: {}",
cols.join(", ")
));
};
let (i_created, i_by, i_outcome, i_resolved, i_tags) = (
col("created"),
col("resolve_by"),
col("outcome"),
col("resolved_at"),
col("tags"),
);
let now = Utc::now();
let mut added = 0usize;
let mut skipped: Vec<String> = Vec::new();
let mut salt = now.timestamp_nanos_opt().unwrap_or(0) as u64;
for (n, line) in rows {
let f = split_csv(line);
let get = |i: Option<usize>| {
i.and_then(|i| f.get(i))
.map(|s| s.trim())
.filter(|s| !s.is_empty())
};
let lineno = n + 1;
let Some(statement) = f.get(i_stmt).map(|s| s.trim()).filter(|s| !s.is_empty()) else {
skipped.push(format!("line {lineno}: empty statement"));
continue;
};
let prob: f64 = match f.get(i_prob).map(|s| s.trim()).unwrap_or("").parse() {
Ok(p) => p,
Err(_) => {
skipped.push(format!("line {lineno}: unreadable prob"));
continue;
}
};
if !(0.0..=1.0).contains(&prob) {
skipped.push(format!("line {lineno}: prob {prob} is not between 0 and 1"));
continue;
}
let created = match get(i_created) {
Some(d) => match parse_date(d) {
Ok(d) => d
.and_hms_opt(12, 0, 0)
.map(|dt| dt.and_utc())
.unwrap_or(now),
Err(e) => {
skipped.push(format!("line {lineno}: {e}"));
continue;
}
},
None => now,
};
let resolve_by = match get(i_by) {
Some(d) => match parse_date(d) {
Ok(d) => Some(d),
Err(e) => {
skipped.push(format!("line {lineno}: {e}"));
continue;
}
},
None => None,
};
let outcome = match get(i_outcome).map(|o| o.to_lowercase()) {
None => None,
Some(o) => match o.as_str() {
"yes" | "true" | "1" | "y" => Some(Outcome::True),
"no" | "false" | "0" | "n" => Some(Outcome::False),
other => {
skipped.push(format!("line {lineno}: unreadable outcome '{other}'"));
continue;
}
},
};
let resolved_at = get(i_resolved)
.and_then(|d| parse_date(d).ok())
.and_then(|d| d.and_hms_opt(12, 0, 0))
.map(|dt| dt.and_utc());
let tags = get(i_tags)
.map(|t| {
normalize_tags(
&t.split([',', ';', ' '])
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect::<Vec<_>>(),
)
})
.unwrap_or_default();
salt = salt.wrapping_add(1);
let mut id = gen_id(statement, salt);
while ledger.has_id(&id) {
salt = salt.wrapping_add(1);
id = gen_id(statement, salt);
}
ledger.claims.push(Claim {
id,
statement: statement.to_string(),
created_at: created,
resolve_by,
horizon_days: Some(anamnesis::evidence::horizon_for(&tags)),
tags,
kind: ClaimKind::Binary,
stake: 1.0,
forecasts: vec![Forecast {
at: created,
prob: Some(prob),
interval: None,
because: None,
}],
resolution: outcome.map(|o| Resolution {
at: resolved_at
.or_else(|| {
resolve_by
.and_then(|d| d.and_hms_opt(12, 0, 0))
.map(|d| d.and_utc())
})
.unwrap_or(now),
outcome: Some(o),
value: None,
note: None,
resolved_by: None,
}),
void: None,
check: None,
extra: Default::default(),
amendments: Vec::new(),
});
added += 1;
}
if !dry_run {
store::save(path, ledger).map_err(|e| format!("saving: {e}"))?;
}
if cli.json {
out_json(json!({
"imported": added,
"skipped": skipped.len(),
"problems": skipped,
"dry_run": dry_run,
"ledger": path.display().to_string(),
}));
} else {
let verb = if dry_run { "would import" } else { "imported" };
println!("{verb} {added} claim(s) from {}", file.display());
if !skipped.is_empty() {
println!("skipped {}:", skipped.len());
for s in skipped.iter().take(10) {
println!(" {s}");
}
if skipped.len() > 10 {
println!(" … and {} more", skipped.len() - 10);
}
}
if dry_run {
println!("(--dry-run: nothing was written)");
} else {
println!("Now run: ana report");
}
}
Ok(())
}
fn split_csv(line: &str) -> Vec<String> {
let mut out = Vec::new();
let mut cur = String::new();
let mut in_quotes = false;
let mut chars = line.chars().peekable();
while let Some(c) = chars.next() {
match c {
'"' if in_quotes && chars.peek() == Some(&'"') => {
cur.push('"');
chars.next();
}
'"' => in_quotes = !in_quotes,
',' if !in_quotes => out.push(std::mem::take(&mut cur)),
_ => cur.push(c),
}
}
out.push(cur);
out
}
fn prob_arg(s: &str) -> Result<f64, String> {
let t = s.trim();
let (num, percent) = match t.strip_suffix('%') {
Some(n) => (n.trim(), true),
None => (t, false),
};
let v: f64 = num
.parse()
.map_err(|_| format!("'{s}' is not a probability; write it as 0.7 or 70%"))?;
if !percent && v > 1.0 && v <= 100.0 {
return Err(format!(
"probability must be between 0 and 1, got {v}; for {v} percent write {v}% or {}",
v / 100.0
));
}
let p = if percent { v / 100.0 } else { v };
if !p.is_finite() || !(0.0..=1.0).contains(&p) {
return Err(format!("probability must be between 0 and 1, got {s}"));
}
Ok(p)
}
fn check_deadline(by: NaiveDate, today: NaiveDate) -> Result<(), String> {
if by < today - chrono::Duration::days(1) {
return Err(format!(
"--by {by} is in the past: a claim must be logged before its answer can be known. Use today ({today}) or later; to record past forecasts, use `ana import`."
));
}
Ok(())
}
fn check_prob(p: f64) -> Result<(), String> {
if (0.0..=1.0).contains(&p) {
Ok(())
} else {
Err(format!("probability must be between 0 and 1, got {p}"))
}
}
fn check_level(l: f64) -> Result<(), String> {
if l > 0.0 && l < 1.0 {
Ok(())
} else {
Err(format!("level must be strictly between 0 and 1, got {l}"))
}
}
fn parse_interval(s: &str) -> Result<(f64, f64), String> {
let (lo, hi) = s
.split_once("..")
.ok_or_else(|| format!("interval must look like LOW..HIGH, got '{s}'"))?;
let lo: f64 = lo
.trim()
.parse()
.map_err(|_| format!("bad interval low '{lo}'"))?;
let hi: f64 = hi
.trim()
.parse()
.map_err(|_| format!("bad interval high '{hi}'"))?;
if !lo.is_finite() || !hi.is_finite() {
return Err("interval bounds must be finite".into());
}
if lo > hi {
return Err(format!("interval low must be ≤ high ({lo} > {hi})"));
}
Ok((lo, hi))
}
fn cmd_trial(
cli: &Cli,
ledger_path: &std::path::Path,
action: TrialAction,
from: Option<NaiveDate>,
pairs: u32,
) -> Result<(), String> {
use anamnesis::trial;
let today = Utc::now().date_naive();
let t = match action {
TrialAction::Start => trial::start(ledger_path, from, pairs, today)?,
TrialAction::Status => match trial::load(ledger_path) {
Some(t) => t,
None => {
if cli.json {
println!("{}", json!({ "enrolled": false }));
} else {
println!(
"no trial enrolled ({} does not exist or cannot be read). `ana trial start` enrols one.",
trial::path(ledger_path).display()
);
}
return Ok(());
}
},
};
let week = t.week_of(today);
if cli.json {
println!(
"{}",
json!({
"enrolled": true, "id": t.id, "start": t.start, "end": t.end(),
"pairs": t.on_first.len(), "week": week, "file": trial::path(ledger_path),
})
);
return Ok(());
}
let verb = if matches!(action, TrialAction::Start) {
"enrolled"
} else {
"trial"
};
println!(
"{verb} [{}]: {} pairs of weeks, Monday {} to Sunday {} (UTC dates)",
t.id,
t.on_first.len(),
t.start,
t.end() - chrono::Duration::days(1)
);
match week {
Some(w) => println!(" today is in week {w} of {}", 2 * t.on_first.len()),
None if today < t.start => println!(" it has not started"),
None => println!(" it has ended: python3 validation/crossover_report.py reads it"),
}
println!(
" the schedule is in {} and is not printed, so a model running this never reads it",
trial::path(ledger_path).display()
);
Ok(())
}
fn parse_date(s: &str) -> Result<NaiveDate, String> {
NaiveDate::parse_from_str(s, "%Y-%m-%d")
.map_err(|_| format!("could not parse date '{s}' (expected YYYY-MM-DD)"))
}
fn pct(p: f64) -> String {
format!("{:.0}%", p * 100.0)
}
fn out_json(v: Value) {
println!("{}", serde_json::to_string_pretty(&v).unwrap());
}
fn run(cli: Cli) -> Result<(), String> {
if matches!(cli.cmd, Cmd::Mcp) {
let ledger_path = match cli.data.clone() {
Some(p) => p,
None => agent_ledger_path()?,
};
return mcp::serve(ledger_path).map_err(|e| format!("mcp: {e}"));
}
if matches!(cli.cmd, Cmd::Where) {
return cmd_where(&cli);
}
if let Cmd::Hook { event } = &cli.cmd {
let ledger_path = match cli.data.clone() {
Some(p) => p,
None => agent_ledger_path()?,
};
return anamnesis::hook::run((*event).into(), &ledger_path);
}
if let Cmd::Trial {
action,
from,
pairs,
} = &cli.cmd
{
let ledger_path = match cli.data.clone() {
Some(p) => p,
None => agent_ledger_path()?,
};
return cmd_trial(&cli, &ledger_path, *action, *from, *pairs);
}
if let Cmd::Demo { keep, plain } = &cli.cmd {
return cmd_demo(&cli, keep.as_deref(), *plain);
}
let path = data_path(&cli)?;
let _guard = if cli.cmd.mutates() {
Some(store::lock(&path).map_err(|e| format!("locking {}: {e}", path.display()))?)
} else {
None
};
let mut ledger = store::load(&path).map_err(|e| load_error(&path, e))?;
match &cli.cmd {
Cmd::Run { .. } => unreachable!("`ana run` is dispatched in main"),
Cmd::Add {
statement,
prob,
interval,
level,
by,
tags,
because,
stake,
second_prob,
reference_class,
check,
} => {
let statement = statement.trim().to_string();
let check = check.as_deref().map(anamnesis::check::pin).transpose()?;
if statement.is_empty() {
return Err("statement must not be empty".into());
}
let stake = *stake;
if !(stake.is_finite() && stake >= 0.0) {
return Err(format!("stake must be a finite number ≥ 0, got {stake}"));
}
let resolve_by = by.as_deref().map(parse_date).transpose()?;
let now = Utc::now();
if let Some(d) = resolve_by {
check_deadline(d, now.date_naive())?;
}
if let Some(p) = prob.filter(|p| *p == 0.0 || *p == 1.0) {
eprintln!(
"note: {} is certainty. If it turns out wrong, its log score is the worst there is (clamped to 13.8); say 0.99 or 0.01 if any doubt is left.",
pct(p)
);
}
let (kind, forecast) = match (prob, interval) {
(Some(_), Some(_)) => {
return Err(
"give either --prob (binary) or --interval (numeric), not both".into(),
)
}
(Some(p), None) => {
check_prob(*p)?;
let (p_eff, estimates) = match second_prob {
Some(sp) => {
check_prob(*sp)?;
(scoring::dialectical_mean(*p, *sp), Some((*p, *sp)))
}
None => (*p, None),
};
(
ClaimKind::Binary,
Forecast {
at: now,
prob: Some(p_eff),
interval: None,
because: compose_reasoning(
because.as_deref(),
reference_class.as_deref(),
estimates,
),
},
)
}
(None, Some(iv)) => {
check_level(*level)?;
let (low, high) = parse_interval(iv)?;
(
ClaimKind::Numeric,
Forecast {
at: now,
prob: None,
interval: Some(NumericForecast {
low,
high,
level: *level,
}),
because: compose_reasoning(
because.as_deref(),
reference_class.as_deref(),
None,
),
},
)
}
(None, None) => {
return Err("give --prob 0.7 (binary) or --interval 120..180 (numeric)".into())
}
};
let mut salt = now.timestamp_nanos_opt().unwrap_or(0) as u64;
let id = loop {
let candidate = gen_id(&statement, salt);
if !ledger.has_id(&candidate) {
break candidate;
}
salt = salt.wrapping_add(1);
};
let disp_prob = forecast.prob;
let disp_iv = forecast.interval;
ledger.claims.push(Claim {
id: id.clone(),
statement: statement.clone(),
created_at: now,
resolve_by,
horizon_days: Some(anamnesis::evidence::horizon_for(&normalize_tags(tags))),
tags: normalize_tags(tags),
kind,
stake,
forecasts: vec![forecast],
resolution: None,
void: None,
check: check.clone(),
extra: anamnesis::pin::origin_here(),
amendments: Vec::new(),
});
store::save(&path, &ledger).map_err(|e| format!("saving: {e}"))?;
if let (Some(check), false) = (&check, cli.json) {
println!(" pinned to `{check}`: settle it with `ana run {id} -- {check}`");
}
if cli.json {
out_json(
json!({"id": id, "kind": kind, "prob": disp_prob, "interval": disp_iv, "statement": statement}),
);
} else {
match kind {
ClaimKind::Binary => {
println!(
"added [{id}] {} \"{}\"",
pct(disp_prob.unwrap()),
printable(&statement)
)
}
ClaimKind::Numeric => {
let iv = disp_iv.unwrap();
println!(
"added [{id}] {:.0}% interval [{}, {}] \"{}\"",
iv.level * 100.0,
iv.low,
iv.high,
printable(&statement)
)
}
}
}
}
Cmd::Update {
id,
prob,
interval,
level,
because,
} => {
let idx = ledger.index_of(id)?;
if ledger.claims[idx].is_resolved() {
return Err(format!(
"[{}] is already resolved; its history is final",
ledger.claims[idx].id
));
}
let kind = ledger.claims[idx].kind;
let now = Utc::now();
let (forecast, human) = match kind {
ClaimKind::Binary => {
let p = prob.ok_or("this is a binary claim; revise it with --prob 0..1")?;
if interval.is_some() {
return Err("this is a binary claim; use --prob, not --interval".into());
}
check_prob(p)?;
let old = ledger.claims[idx].current_prob().unwrap_or(p);
(
Forecast {
at: now,
prob: Some(p),
interval: None,
because: because.clone(),
},
format!("{} → {}", pct(old), pct(p)),
)
}
ClaimKind::Numeric => {
let iv = interval
.as_deref()
.ok_or("this is a numeric claim; revise it with --interval LOW..HIGH")?;
if prob.is_some() {
return Err("this is a numeric claim; use --interval, not --prob".into());
}
let (low, high) = parse_interval(iv)?;
let lvl = level
.or_else(|| ledger.claims[idx].current_interval().map(|i| i.level))
.unwrap_or(0.80);
check_level(lvl)?;
(
Forecast {
at: now,
prob: None,
interval: Some(NumericForecast {
low,
high,
level: lvl,
}),
because: because.clone(),
},
format!("[{low}, {high}] @ {:.0}%", lvl * 100.0),
)
}
};
ledger.claims[idx].forecasts.push(forecast);
let cid = ledger.claims[idx].id.clone();
let rev = ledger.claims[idx].forecasts.len();
store::save(&path, &ledger).map_err(|e| format!("saving: {e}"))?;
if cli.json {
out_json(json!({"id": cid, "kind": kind, "revision": rev}));
} else {
println!("[{cid}] {human} (revision #{rev})");
}
}
Cmd::Resolve {
id,
outcome,
value,
note,
} => {
let idx = ledger.index_of(id)?;
if ledger.claims[idx].is_resolved() {
return Err(format!("[{}] is already resolved", ledger.claims[idx].id));
}
if let Some(check) = &ledger.claims[idx].check {
return Err(anamnesis::check::pinned_message(
&ledger.claims[idx].id,
check,
));
}
let kind = ledger.claims[idx].kind;
let now = Utc::now();
let cid = ledger.claims[idx].id.clone();
match kind {
ClaimKind::Binary => {
if value.is_some() {
return Err(
"this is a binary claim; resolve it with yes/no, not --value".into(),
);
}
let o: Outcome = (*outcome
.as_ref()
.ok_or("resolve a binary claim with yes or no")?)
.into();
let prob = ledger.claims[idx].first_prob().unwrap_or(0.5);
let final_prob = ledger.claims[idx].current_prob();
let revisions = ledger.claims[idx].forecasts.len();
ledger.claims[idx].resolution = Some(Resolution {
at: now,
outcome: Some(o),
value: None,
note: note.clone(),
resolved_by: None,
});
store::save(&path, &ledger).map_err(|e| format!("saving: {e}"))?;
let truth_f = if o.happened() { 1.0 } else { 0.0 };
let brier = (prob - truth_f).powi(2);
let final_brier = final_prob.map(|fp| (fp - truth_f).powi(2));
if cli.json {
out_json(json!({
"id": cid,
"kind": kind,
"outcome": o.happened(),
"prob": prob,
"brier": brier,
"score_basis": "first",
"final_prob": final_prob,
"final_brier": final_brier,
}));
} else {
let truth = if o.happened() { "TRUE" } else { "FALSE" };
println!(
"[{cid}] resolved {truth}. you said {} → Brier {:.3} on this one",
pct(prob),
brier
);
if revisions > 1 {
if let (Some(fp), Some(fb)) = (final_prob, final_brier) {
println!(
" graded on your FIRST forecast; you later revised to {} ({:.3} — shown, not graded)",
pct(fp),
fb
);
}
}
}
}
ClaimKind::Numeric => {
if outcome.is_some() {
return Err("this is a numeric claim; resolve it with --value N".into());
}
let v = value.ok_or("resolve a numeric claim with --value N")?;
let iv = ledger.claims[idx]
.current_interval()
.ok_or("numeric claim has no interval forecast")?;
ledger.claims[idx].resolution = Some(Resolution {
at: now,
outcome: None,
value: Some(v),
note: note.clone(),
resolved_by: None,
});
store::save(&path, &ledger).map_err(|e| format!("saving: {e}"))?;
let ns = NumericSample {
low: iv.low,
high: iv.high,
level: iv.level,
value: v,
};
let w = scoring::winkler(&ns);
let caught = ns.contains();
if cli.json {
out_json(
json!({"id": cid, "kind": kind, "value": v, "interval": iv, "inside": caught, "winkler": w}),
);
} else {
let verdict = if caught { "caught it" } else { "MISSED" };
println!(
"[{cid}] resolved value {v}. interval [{}, {}] {verdict} → Winkler {:.3}",
iv.low, iv.high, w
);
}
}
}
}
Cmd::Void { id, reason } => {
let reason = reason.trim().to_string();
if reason.is_empty() {
return Err("a void needs a reason — it is the whole record of why".into());
}
let idx = ledger.index_of(id)?;
let c = &mut ledger.claims[idx];
if c.is_void() {
return Err(format!("[{}] is already void", c.id));
}
if c.is_resolved() {
return Err(format!(
"[{}] is already resolved, and a resolved claim cannot be voided: that would remove an outcome after it was seen. A void is for a question that stopped making sense before you knew the answer.",
c.id
));
}
let cid = c.id.clone();
c.void = Some(anamnesis::model::Void {
at: Utc::now(),
reason: reason.clone(),
});
store::save(&path, &ledger).map_err(|e| format!("saving: {e}"))?;
if cli.json {
out_json(json!({"id": cid, "void": true, "reason": reason}));
} else {
println!("[{cid}] voided — excluded from every score, kept in history.");
println!(" reason: {reason}");
}
}
Cmd::Amend {
id,
statement,
tags,
} => {
if statement.is_none() && tags.is_none() {
return Err("nothing to amend — pass --statement and/or --tags".into());
}
let idx = ledger.index_of(id)?;
let c = &mut ledger.claims[idx];
if c.is_resolved() {
return Err(format!(
"[{}] is already resolved — a resolved claim is the record, and the record does not change",
c.id
));
}
let cid = c.id.clone();
let mut am = anamnesis::model::Amendment {
at: Utc::now(),
old_statement: None,
new_statement: None,
old_tags: None,
new_tags: None,
};
if let Some(new) = statement {
let new = new.trim().to_string();
if new.is_empty() {
return Err("statement must not be empty".into());
}
am.old_statement = Some(c.statement.clone());
am.new_statement = Some(new.clone());
c.statement = new;
}
if let Some(new) = tags {
let parsed: Vec<String> = new
.split(',')
.map(|t| t.trim().to_lowercase())
.filter(|t| !t.is_empty())
.collect();
am.old_tags = Some(c.tags.clone());
am.new_tags = Some(parsed.clone());
c.tags = parsed;
}
c.amendments.push(am);
let statement = ledger.claims[idx].statement.clone();
let tags = ledger.claims[idx].tags.clone();
store::save(&path, &ledger).map_err(|e| format!("saving: {e}"))?;
if cli.json {
out_json(json!({"id": cid, "statement": statement, "tags": tags}));
} else {
println!("[{cid}] amended — \"{}\"", printable(&statement));
if !tags.is_empty() {
println!(" tags: {}", tags.join(", "));
}
println!(" the previous wording is kept in the claim's history.");
}
}
Cmd::List {
open,
resolved,
due,
tag,
void,
} => {
let today = Utc::now().date_naive();
let tagf = tag.as_ref().map(|t| t.to_lowercase());
let keep = |c: &Claim| {
if let Some(t) = &tagf {
if !c.tags.iter().any(|x| x == t) {
return false;
}
}
if *void {
return c.is_void();
}
if c.is_void() {
return false;
}
if *due {
c.is_due(today)
} else if *open {
c.is_open()
} else if *resolved {
c.is_resolved()
} else {
true
}
};
if cli.json {
let arr: Vec<Value> = ledger
.claims
.iter()
.filter(|c| keep(c))
.map(|c| {
json!({
"id": c.id,
"kind": c.kind,
"statement": c.statement,
"prob": c.current_prob(),
"interval": c.current_interval(),
"tags": c.tags,
"resolve_by": c.resolve_by,
"resolved": c.is_resolved(),
"outcome": c.outcome().map(|o| o.happened()),
"value": c.value(),
})
})
.collect();
out_json(json!(arr));
return Ok(());
}
let mut shown = 0;
for c in ledger.claims.iter().filter(|c| keep(c)) {
shown += 1;
let belief = match c.kind {
ClaimKind::Binary => c.current_prob().map(pct).unwrap_or_else(|| " ?".into()),
ClaimKind::Numeric => c
.current_interval()
.map(|i| format!("[{},{}]", i.low, i.high))
.unwrap_or_else(|| " ?".into()),
};
let status = match c.kind {
ClaimKind::Binary => match c.outcome() {
Some(Outcome::True) => "✓ true".to_string(),
Some(Outcome::False) => "✗ false".to_string(),
None => open_status(c, today),
},
ClaimKind::Numeric => match c.value() {
Some(v) => format!("= {v}"),
None => open_status(c, today),
},
};
let tags = if c.tags.is_empty() {
String::new()
} else {
format!(" [{}]", printable(&c.tags.join(",")))
};
println!(
"{:<6} {:>9} {:<11} {}{tags}",
c.id,
belief,
status,
truncate(&printable(&c.statement), 52)
);
}
if shown == 0 {
println!("(no matching claims)");
}
}
Cmd::Show { id } => {
let idx = ledger.index_of(id)?;
let c = &ledger.claims[idx];
if cli.json {
out_json(serde_json::to_value(c).map_err(|e| e.to_string())?);
return Ok(());
}
println!("[{}] {}", c.id, printable(&c.statement));
println!(
" kind: {}",
match c.kind {
ClaimKind::Binary => "binary",
ClaimKind::Numeric => "numeric",
}
);
println!(" created {}", c.created_at.date_naive());
if let Some(d) = c.resolve_by {
println!(" resolve by {d}");
}
if !c.tags.is_empty() {
println!(" tags: {}", printable(&c.tags.join(", ")));
}
if let Some(check) = &c.check {
let shown = anamnesis::untrusted::line(check, anamnesis::untrusted::MAX_LINE);
println!(
" check: `{shown}` — settle it with: ana run {} -- {shown}",
c.id
);
}
if let Some(v) = &c.void {
println!(
" VOID since {} — excluded from every score",
v.at.date_naive()
);
println!(" reason: {}", printable(&v.reason));
}
for a in &c.amendments {
if let (Some(o), Some(n)) = (&a.old_statement, &a.new_statement) {
println!(
" amended {}: \"{}\" → \"{}\"",
a.at.date_naive(),
printable(o),
printable(n)
);
}
if let (Some(o), Some(n)) = (&a.old_tags, &a.new_tags) {
println!(
" amended {}: tags [{}] → [{}]",
a.at.date_naive(),
printable(&o.join(", ")),
printable(&n.join(", "))
);
}
}
println!(" forecasts:");
for (i, f) in c.forecasts.iter().enumerate() {
let marker = if i + 1 == c.forecasts.len() {
"→"
} else {
" "
};
let belief = match (f.prob, f.interval) {
(Some(p), _) => pct(p),
(_, Some(iv)) => {
format!("[{}, {}] @ {:.0}%", iv.low, iv.high, iv.level * 100.0)
}
_ => "?".into(),
};
println!(" {marker} {} {}", f.at.date_naive(), belief);
if let Some(b) = &f.because {
println!(" because: {}", printable(b));
}
}
match &c.resolution {
Some(r) => {
match c.kind {
ClaimKind::Binary => {
let truth = if c.outcome().map(|o| o.happened()).unwrap_or(false) {
"TRUE"
} else {
"FALSE"
};
let how = match r.resolved_by {
Some(anamnesis::model::ResolvedBy::Auto) => {
" (graded from the exit status of the command pinned when it was logged)"
}
Some(anamnesis::model::ResolvedBy::Human) => {
" (graded by a human)"
}
_ => "",
};
println!(" resolved {truth} on {}{how}", r.at.date_naive());
if let Some(s) = c.sample() {
println!(
" Brier on your FIRST forecast: {:.3}",
(s.prob - s.outcome).powi(2)
);
if let Some(f) = c.sample_final() {
if (f.prob - s.prob).abs() > 1e-9 {
println!(
" (final forecast {:.2} would score {:.3} — shown, not graded)",
f.prob,
(f.prob - f.outcome).powi(2)
);
}
}
}
}
ClaimKind::Numeric => {
if let Some(ns) = c.numeric_sample() {
let verdict = if ns.contains() { "inside" } else { "OUTSIDE" };
println!(
" resolved value {} on {} ({verdict} the interval)",
ns.value,
r.at.date_naive()
);
println!(
" Winkler on final interval: {:.3}",
scoring::winkler(&ns)
);
}
}
}
if let Some(n) = &r.note {
println!(" post-mortem: {}", printable(n));
}
}
None => println!(" (open — reality has not yet spoken)"),
}
}
Cmd::Report {
tag,
bins,
plain,
html,
badge,
} => {
let today = Utc::now().date_naive();
let tag = tag.as_deref();
if cli.json {
println!("{}", report::render_json(&ledger, tag, *bins, today));
} else if *html {
print!("{}", report::render_html(&ledger, tag, *bins, today));
} else if *badge {
print!("{}", report::render_badge_svg(&ledger, tag, *bins, today));
} else if *plain {
print!("{}", report::render_plain(&ledger, tag, *bins, today));
} else {
print!("{}", report::render(&ledger, tag, *bins, today));
}
}
Cmd::Decide {
prob,
stake,
verify_cost,
tag,
} => {
let p = *prob;
if !(0.0..=1.0).contains(&p) {
return Err(format!("prob must be between 0 and 1, got {p}"));
}
let (recal, earned, n, e) =
report::earned_recalibration(&ledger, tag.as_deref(), Utc::now().date_naive());
let map = if earned { recal } else { None };
let d = scoring::decide(p, map, *stake, *verify_cost);
let verb = match d.act {
scoring::Act::Proceed => "PROCEED",
scoring::Act::Verify => "VERIFY",
scoring::Act::Abstain => "ABSTAIN",
};
if cli.json {
println!(
"{}",
json!({
"act": verb.to_lowercase(),
"stated": p,
"adjusted": d.adjusted_p,
"proceed_threshold": d.proceed_threshold,
"margin": d.margin,
"stake": *stake,
"verify_cost": *verify_cost,
"used_recalibration": earned,
"map_kind": d.map_kind.as_str(),
"n": n,
"eprocess": e,
})
);
} else {
let gloss = match d.act {
scoring::Act::Proceed => "confidence clears the bar for the stakes",
scoring::Act::Verify => "in the doubt zone — check before you commit: run it here if you can; if it lives off this machine, read the primary source",
scoring::Act::Abstain => {
"more likely to fail than succeed — replan or escalate"
}
};
let corr = if earned {
format!(
" (corrected {:.0}%→{:.0}% from {n} resolved calls)",
p * 100.0,
d.adjusted_p * 100.0
)
} else {
String::new()
};
println!("{verb} — {gloss}");
println!(
" need ≥{:.0}% to proceed at stake {:.1}; you have {:.0}%{corr}",
d.proceed_threshold * 100.0,
*stake,
d.adjusted_p * 100.0
);
if d.map_kind == scoring::MapKind::Constant {
println!(
" note: your stated confidence hasn't tracked outcomes over {n} calls, so the number you gave was replaced with your base rate — every --prob returns this same answer until that changes."
);
}
}
}
Cmd::Import { file, dry_run } => cmd_import(&cli, &path, &mut ledger, file, *dry_run)?,
Cmd::Export {
anonymize,
out,
keep_tags,
} => cmd_export(&ledger, *anonymize, out.as_deref(), keep_tags.as_deref())?,
Cmd::Mcp | Cmd::Where | Cmd::Demo { .. } | Cmd::Hook { .. } | Cmd::Trial { .. } => {
unreachable!("dispatched before the ledger load")
}
}
Ok(())
}
fn open_status(c: &Claim, today: NaiveDate) -> String {
match c.resolve_by {
Some(d) if d <= today => format!("● due {d}"),
Some(d) => format!("○ by {d}"),
None => "○ open".to_string(),
}
}
fn truncate(s: &str, max: usize) -> String {
if s.chars().count() <= max {
s.to_string()
} else {
let mut t: String = s.chars().take(max.saturating_sub(1)).collect();
t.push('…');
t
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_deadline_is_refused_only_when_it_is_past_everywhere() {
let today = NaiveDate::from_ymd_opt(2026, 11, 8).unwrap();
assert!(check_deadline(today, today).is_ok());
assert!(check_deadline(NaiveDate::from_ymd_opt(2026, 11, 7).unwrap(), today).is_ok());
let e = check_deadline(NaiveDate::from_ymd_opt(2026, 11, 6).unwrap(), today).unwrap_err();
assert!(e.contains("is in the past"), "{e}");
}
#[test]
fn probabilities_are_read_as_people_write_them() {
for (s, want) in [
("0.7", 0.7),
(".7", 0.7),
("70%", 0.7),
("0%", 0.0),
("100%", 1.0),
("1", 1.0),
] {
assert!((prob_arg(s).unwrap() - want).abs() < 1e-12, "{s}");
}
assert!(prob_arg("70").unwrap_err().contains("write 70% or 0.7"));
for bad in ["-0.2", "1.5", "150%", "nan", "inf", "seventy"] {
assert!(prob_arg(bad).is_err(), "{bad} was accepted");
}
}
}