Files
pokerfreak/src/bin/audit.rs
T
mmintelandClaude Opus 5.5 fa7fce9f39 D4 Entscheidungs-Audit automatisch: Turn+River, Preflop/Flop-Preise, Lauf- und Wochenbericht, Cockpit /audit, Haken
- engine::audit_turn: Turn-Entscheidungen im Turn+River-Spiel (E3-Löser) nachgerechnet,
  EV je Aktion fuer die echte Hand (TrSolver::hand_action_values), Gegner-Range entlang der Linie
- engine::audit_allin: Preflop gegen All-in (Equity gegen Nash-Push-Range vs Preis), Push/Fold
  first-in <= 25 bb (Nash-Spiel der Charts), Flop gegen All-in (exakte Equity vs Preis)
- bin/audit: Feld method/note, --turn-iters/--turn-combos/--turn-cards
- lesen/audit_lauf.py: exakte Spots aus dem Schatten-Log, Cache je Lauf, Bericht je Lauf
  (Street, Messlatte, Engine-Teil, Top 10), Wochensumme, Fixture-Vorschlaege
- sicht/audit_seite.py + Route: /audit, /api/audit, /api/audit/laeufe, /api/audit/woche
- betrieb/audit_haken.py + docs/notizen/D4-audit-haken.patch (runner.py, 3 Zeilen)
- tests/audit_lauf_spec.py 12/12; cargo test engine::audit 14/14

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 21:57:23 +01:00

163 lines
6.6 KiB
Rust

//! Decision audit (plan D4), one reconstructed spot per input line.
//!
//! Input line: the engine's wire request (VERTRAG.md: heroCards, board, pot, toCall, heroStack,
//! villains, history, ...) plus
//! - `"taken"`: `{"action": "FOLD|CHECK|CALL|BET|RAISE|ALL_IN", "amount": <street total or null>}`,
//! - optional `"id"`, echoed back as `id` (the caller's key for joining the result to its decision).
//!
//! Output line (one per input line, same order):
//! `{"id", "status": "ok"|"skip", "reason"?, "source": "blueprint"|"modell", "first", "class",
//! "classes", "taken", "size_gap", "acts": [{"act", "to"?, "p", "ev"}], "ev_best", "ev_mix", "ev_taken",
//! "loss", "loss_bb", "off_line", "hero_in_range", "exploitability", "bb", "method", "note"}`. EVs and
//! loss are chips of the spot (from the start of the street, solver scale); `loss_bb` is `loss / bigBlind`.
//! `method`: `river_game`, `turn_river_game`, `jam_preis`, `push_fold`, `allin_equity` (see
//! `engine::audit`, `engine::audit_turn`, `engine::audit_allin`).
//!
//! Use: `audit [--iters N] [--turn-iters N] [--turn-combos N] [--turn-cards N] [in.jsonl] > out.jsonl`
//! (stdin without a file). Single thread. Ranges come from the blueprint when `$GRUNDSTRATEGIE` is set
//! and the spot is scenario S1, else the range model.
//! Run at priority Idle (CLAUDE.md "Messlaeufe").
use rechner::cfr::river_game::Act;
use rechner::engine::audit::{audit, Opts, Taken};
use rechner::fault::Fault;
use rechner::guard::validate::check;
use rechner::json::{parse, render, Value};
use rechner::wire::decode::decode;
use std::io::{self, BufRead, Write};
use std::panic::{catch_unwind, set_hook, AssertUnwindSafe};
fn member<'a>(v: &'a Value, key: &str) -> Option<&'a Value> {
match v {
Value::Object(m) => m.iter().find(|(k, _)| k == key).map(|(_, x)| x),
_ => None,
}
}
fn num(x: f64) -> Value {
Value::Number(if x.is_finite() { x } else { 0.0 })
}
fn skip(id: &Value, reason: String) -> Value {
Value::Object(vec![
("id".to_string(), id.clone()),
("status".to_string(), Value::String("skip".to_string())),
("reason".to_string(), Value::String(reason)),
])
}
fn taken_of(root: &Value) -> Result<Taken, String> {
let t = member(root, "taken").ok_or("missing taken")?;
let name = match member(t, "action") {
Some(Value::String(s)) => s.as_str(),
_ => return Err("taken.action missing".to_string()),
};
let amount = match member(t, "amount") {
Some(Value::Number(n)) => Some(*n),
_ => None,
};
Ok(match name {
"FOLD" => Taken::Fold,
"CHECK" => Taken::Check,
"CALL" => Taken::Call,
"BET" | "RAISE" | "ALL_IN" => Taken::Aggress(amount.ok_or("bet/raise without amount")?),
other => return Err(format!("unknown taken.action {other:?}")),
})
}
fn act_value(a: &rechner::engine::audit::ActEv) -> Value {
let (name, to) = match a.act {
Act::Fold => ("Fold", None),
Act::Check => ("Check", None),
Act::Call => ("Call", None),
Act::Bet(x) => ("Bet", Some(x)),
Act::Raise(x) => ("Raise", Some(x)),
};
let mut m = vec![("act".to_string(), Value::String(name.to_string()))];
if let Some(x) = to {
m.push(("to".to_string(), num(x)));
}
m.push(("p".to_string(), num(a.prob)));
m.push(("ev".to_string(), num(a.ev)));
Value::Object(m)
}
fn one(line: &str, opts: Opts) -> Value {
let root = match parse(line) {
Ok(r) => r,
Err(e) => return skip(&Value::Null, format!("parse: {e}")),
};
let id = member(&root, "id").cloned().unwrap_or(Value::Null);
let spot = match decode(&root).and_then(|s| check(s).map_err(|e: Fault| e)) {
Ok(s) => s,
Err(e) => return skip(&id, format!("invalid spot: {e}")),
};
let taken = match taken_of(&root) {
Ok(t) => t,
Err(e) => return skip(&id, e),
};
let bb = spot.big_blind.max(1e-9);
match audit(&spot, taken, opts) {
Err(s) => skip(&id, s.text()),
Ok(a) => Value::Object(vec![
("id".to_string(), id),
("status".to_string(), Value::String("ok".to_string())),
("source".to_string(), Value::String(a.source.name().to_string())),
("first".to_string(), num(a.first as f64)),
("class".to_string(), num(a.class as f64)),
("classes".to_string(), num(a.classes as f64)),
("taken".to_string(), num(a.taken as f64)),
("size_gap".to_string(), num(a.size_gap)),
("acts".to_string(), Value::Array(a.acts.iter().map(act_value).collect())),
("ev_best".to_string(), num(a.ev_best)),
("ev_mix".to_string(), num(a.ev_mix)),
("ev_taken".to_string(), num(a.ev_taken)),
("loss".to_string(), num(a.loss)),
("loss_bb".to_string(), num(a.loss / bb)),
("off_line".to_string(), Value::Bool(a.off_line)),
("hero_in_range".to_string(), Value::Bool(a.hero_in_range)),
("exploitability".to_string(), num(a.exploitability)),
("bb".to_string(), num(bb)),
("method".to_string(), Value::String(a.method.to_string())),
("note".to_string(), Value::String(a.note.clone())),
]),
}
}
fn main() {
set_hook(Box::new(|_| {}));
let mut opts = Opts::default();
let mut iters = opts.iters;
let mut file: Option<String> = None;
let mut args = std::env::args().skip(1);
while let Some(a) = args.next() {
if a == "--iters" {
iters = args.next().and_then(|s| s.parse().ok()).unwrap_or(iters);
} else if a == "--turn-iters" {
opts.turn_iters = args.next().and_then(|s| s.parse().ok()).unwrap_or(opts.turn_iters);
} else if a == "--turn-combos" {
opts.turn_combos = args.next().and_then(|s| s.parse().ok()).unwrap_or(opts.turn_combos);
} else if a == "--turn-cards" {
opts.turn_cards = args.next().and_then(|s| s.parse().ok()).unwrap_or(opts.turn_cards);
} else {
file = Some(a);
}
}
opts.iters = iters;
let reader: Box<dyn BufRead> = match &file {
Some(p) => Box::new(io::BufReader::new(std::fs::File::open(p).expect("open input"))),
None => Box::new(io::BufReader::new(io::stdin())),
};
let stdout = io::stdout();
let mut out = stdout.lock();
for line in reader.lines() {
let line = line.expect("read line");
if line.trim().is_empty() {
continue;
}
let v = catch_unwind(AssertUnwindSafe(|| one(&line, opts)))
.unwrap_or_else(|_| skip(&Value::Null, "internal error".to_string()));
writeln!(out, "{}", render(&v)).expect("write");
}
}