use std::collections::BTreeMap;
use std::path::Path;
use serde::Serialize;
use crate::queen::{self, Entry};
use crate::{notes, stats};
pub const STAMP_W: f64 = 1.0;
pub const MISSED_W: f64 = crate::memory::VETOED_W as f64;
const ANSWER_CHARS: usize = 120;
const YES: [&str; 11] = ["yes", "y", "ok", "okay", "fine", "sure", "go", "ahead", "proceed", "approved", "lgtm"];
fn norm(text: &str) -> String {
text.to_lowercase().split(|c: char| !c.is_alphanumeric()).filter(|w| !w.is_empty()).collect::<Vec<_>>().join(" ")
}
pub fn stamp(e: &Entry, answer: &str) -> bool {
let a = norm(answer);
let plan = norm(e.kind.proceeds());
(!plan.is_empty() && a.starts_with(&plan)) || yes(answer)
}
pub fn yes(answer: &str) -> bool {
let a = norm(answer);
!a.is_empty() && a.split(' ').all(|w| YES.contains(&w))
}
pub fn answered(store: &Path, e: &Entry, answer: &str, author: &str, now: i64) -> Option<&'static str> {
if !e.kind.escalates() {
return None;
}
let kind = if stamp(e, answer) { stats::QUEEN_STAMP } else { stats::QUEEN_NO };
stats::count(store, kind, &e.session, |ev| {
ev.ms = now;
ev.engineer = author.into();
ev.repo = e.repo.clone();
ev.why = format!("{:?}", e.kind);
ev.text = format!("{} {}", e.id, stats::excerpt(answer, ANSWER_CHARS));
});
Some(kind)
}
pub fn escalated(store: &Path, session: &str) -> bool {
!session.is_empty() && queen::entries(store).iter().any(|e| e.session == session && e.kind.escalates())
}
pub fn merged(state: &Path, session: &str) -> bool {
!notes::landed_by(state, session).is_empty()
}
pub fn missed(store: &Path, session: &str, cause: &str, id: &str, repo: &str, now: i64) -> bool {
if session.is_empty() || escalated(store, session) {
return false;
}
if stats::events(store).iter().any(|e| e.kind == stats::QUEEN_MISSED && e.text.split(' ').next() == Some(id)) {
return false;
}
stats::count(store, stats::QUEEN_MISSED, session, |ev| {
ev.ms = now;
ev.repo = repo.into();
ev.why = cause.into();
ev.text = id.into();
});
true
}
#[derive(Debug, Clone, Default, PartialEq, Serialize)]
pub struct Yield {
pub answered: u64,
pub no: u64,
pub stamps: u64,
pub missed: u64,
pub share: Option<f64>,
pub reward: f64,
pub alpha: f64,
pub beta: f64,
}
pub fn of(counts: &BTreeMap<String, u64>) -> Yield {
let n = |k: &str| counts.get(k).copied().unwrap_or(0);
let (no, stamps, missed) = (n(stats::QUEEN_NO), n(stats::QUEEN_STAMP), n(stats::QUEEN_MISSED));
let answered = no + stamps;
Yield {
answered,
no,
stamps,
missed,
share: (answered > 0).then(|| no as f64 / answered as f64),
reward: no as f64 - STAMP_W * stamps as f64 - MISSED_W * missed as f64,
alpha: STAMP_W,
beta: MISSED_W,
}
}
pub fn team(home: &Path) -> Yield {
let mut all = stats::Bucket::default();
for b in stats::current(home) {
all.add(&b);
}
of(&all.counts)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::queen::Kind;
fn tmp(tag: &str) -> std::path::PathBuf {
let d = std::env::temp_dir().join(format!("borg-escalation-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
d
}
fn entry(id: &str, kind: Kind, session: &str) -> Entry {
let (a, b) = kind.options();
Entry {
id: id.into(),
decision_key: format!("k{id}"),
kind,
question: format!("q {id}"),
options: vec![a.into(), b.into()],
default: a.into(),
evidence: vec![],
session: session.into(),
cwd: String::new(),
repo: "app".into(),
engineer: "ann@x".into(),
ms: 1,
answer: None,
answered_by: None,
answered_ms: None,
disagreeing: vec![],
dismissed: None,
dismissed_ms: None,
payload: None,
}
}
#[test]
fn letting_the_plan_go_ahead_is_a_stamp_and_changing_course_is_a_no() {
let irreversible = entry("q1", Kind::Irreversible, "s");
assert!(stamp(&irreversible, "proceed as planned"));
assert!(stamp(&irreversible, "Yes, fine."));
assert!(stamp(&irreversible, "ok go ahead"));
assert!(!stamp(&irreversible, "make it reversible first (backup, soft delete, a revert path)"));
assert!(!stamp(&irreversible, "yes but keep a backup"), "a yes with a condition changes the course");
assert!(!stamp(&irreversible, "no"));
let push = entry("q2", Kind::ExternalMutation, "s");
assert!(stamp(&push, "run it as planned"));
assert!(!stamp(&push, &push.default), "holding it is the no");
}
#[test]
fn the_yield_folds_counts_with_the_design_weights() {
let counts: BTreeMap<String, u64> =
[(stats::QUEEN_NO, 3), (stats::QUEEN_STAMP, 1), (stats::QUEEN_MISSED, 1)].into_iter().map(|(k, v)| (k.to_string(), v)).collect();
let y = of(&counts);
assert_eq!((y.answered, y.no, y.stamps, y.missed), (4, 3, 1, 1));
assert_eq!(y.share, Some(0.75));
assert_eq!(y.reward, 3.0 - 1.0 - 3.0, "a missed escalation weighs a veto");
assert_eq!(of(&BTreeMap::new()), Yield { alpha: 1.0, beta: 3.0, ..Yield::default() }, "no answer, no share");
}
#[test]
fn only_the_queens_own_escalations_earn_and_each_answer_logs_once() {
let store = tmp("answered");
let mut e = entry("q1", Kind::PublicApi, "s1");
assert_eq!(answered(&store, &e, "keep the old contract working", "ann@x", 10), Some(stats::QUEEN_NO));
assert_eq!(answered(&store, &e, "proceed as planned", "ann@x", 11), Some(stats::QUEEN_STAMP));
for k in [Kind::Constraint, Kind::Reach, Kind::OpenDecision] {
e.kind = k;
assert_eq!(answered(&store, &e, "anything", "ann@x", 12), None, "{k:?} is a gate, not the queen's choice");
}
let y = team(&store);
assert_eq!((y.no, y.stamps), (1, 1));
let _ = std::fs::remove_dir_all(&store);
}
#[test]
fn a_no_without_an_escalation_is_missed_once_and_never_after_one() {
let store = tmp("missed");
assert!(missed(&store, "s1", "veto", "no-s1-5", "app", 5));
assert!(!missed(&store, "s1", "veto", "no-s1-5", "app", 6), "once per id");
assert!(!missed(&store, "", "regret", "r1", "app", 6), "an unknown session is nobody's miss");
let line = serde_json::to_string(&entry("q9", Kind::CrossRepo, "s2")).unwrap();
std::fs::create_dir_all(queen::dir(&store)).unwrap();
std::fs::write(queen::dir(&store).join("queue.jsonl"), format!("{line}\n")).unwrap();
assert!(escalated(&store, "s2"));
assert!(!missed(&store, "s2", "regret", "r2", "app", 7));
let line = serde_json::to_string(&entry("q10", Kind::Constraint, "s3")).unwrap();
std::fs::write(queen::dir(&store).join("queue.jsonl"), format!("{line}\n")).unwrap();
assert!(missed(&store, "s3", "regret", "r3", "app", 8));
let y = team(&store);
assert_eq!((y.missed, y.reward), (2, -6.0));
let _ = std::fs::remove_dir_all(&store);
}
}