atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
c4d70621d377d45e3d418ac9197443c1b5d36399
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git

Blame: src/game/oracle.rs

3f9091557ec3atagen2026-10-11 11:43 UTC1
//! Where the game's decisions come from. The seeded rng is the default; an
3f9091557ec3atagen2026-10-11 11:43 UTC2
//! operator can instead point the server at a System One service and let the
3f9091557ec3atagen2026-10-11 11:43 UTC3
//! model answer every roll over HTTP (JELQD_ORACLE=clm).
3f9091557ec3atagen2026-10-11 11:43 UTC4
//!
3f9091557ec3atagen2026-10-11 11:43 UTC5
//! Identity randomness (run ids, etag salts, nonces) never goes to the model.
3f9091557ec3atagen2026-10-11 11:43 UTC6
//! The Clm arm carries its own rng for those, seeded like the rng arm's.
3f9091557ec3atagen2026-10-11 11:43 UTC7
3f9091557ec3atagen2026-10-11 11:43 UTC8
use rand::{Rng, SeedableRng, rngs::StdRng};
3f9091557ec3atagen2026-10-11 11:43 UTC9
use serde_json::{Value, json};
3f9091557ec3atagen2026-10-11 11:43 UTC10
3f9091557ec3atagen2026-10-11 11:43 UTC11
/// One named decision. The state prose is what the model reads as context, the
3f9091557ec3atagen2026-10-11 11:43 UTC12
/// ask is the question, and the site is a stable name for logs and caching.
3f9091557ec3atagen2026-10-11 11:43 UTC13
pub(crate) struct Roll {
3f9091557ec3atagen2026-10-11 11:43 UTC14
    pub(crate) state: String,
3f9091557ec3atagen2026-10-11 11:43 UTC15
    pub(crate) site: &'static str,
3f9091557ec3atagen2026-10-11 11:43 UTC16
    pub(crate) ask: String,
3f9091557ec3atagen2026-10-11 11:43 UTC17
}
3f9091557ec3atagen2026-10-11 11:43 UTC18
3f9091557ec3atagen2026-10-11 11:43 UTC19
pub(crate) enum Oracle {
3f9091557ec3atagen2026-10-11 11:43 UTC20
    Rng(StdRng),
3f9091557ec3atagen2026-10-11 11:43 UTC21
    Clm { clm: Clm, technical: StdRng },
3f9091557ec3atagen2026-10-11 11:43 UTC22
}
3f9091557ec3atagen2026-10-11 11:43 UTC23
3f9091557ec3atagen2026-10-11 11:43 UTC24
pub(crate) enum OracleTemplate {
3f9091557ec3atagen2026-10-11 11:43 UTC25
    Rng,
3f9091557ec3atagen2026-10-11 11:43 UTC26
    Clm { url: String },
3f9091557ec3atagen2026-10-11 11:43 UTC27
}
3f9091557ec3atagen2026-10-11 11:43 UTC28
159f0998263batagen2026-10-12 11:01 UTC29
impl OracleTemplate {
159f0998263batagen2026-10-12 11:01 UTC30
    pub(crate) fn blocks(&self) -> bool {
159f0998263batagen2026-10-12 11:01 UTC31
        matches!(self, Self::Clm { .. })
159f0998263batagen2026-10-12 11:01 UTC32
    }
159f0998263batagen2026-10-12 11:01 UTC33
}
159f0998263batagen2026-10-12 11:01 UTC34
3f9091557ec3atagen2026-10-11 11:43 UTC35
pub(crate) struct Clm {
3f9091557ec3atagen2026-10-11 11:43 UTC36
    url: String,
3f9091557ec3atagen2026-10-11 11:43 UTC37
    agent: ureq::Agent,
3f9091557ec3atagen2026-10-11 11:43 UTC38
}
3f9091557ec3atagen2026-10-11 11:43 UTC39
3f9091557ec3atagen2026-10-11 11:43 UTC40
impl Oracle {
3f9091557ec3atagen2026-10-11 11:43 UTC41
    pub(crate) fn build(template: &OracleTemplate, seed: u64) -> Self {
3f9091557ec3atagen2026-10-11 11:43 UTC42
        match template {
3f9091557ec3atagen2026-10-11 11:43 UTC43
            OracleTemplate::Rng => Self::Rng(StdRng::seed_from_u64(seed)),
3f9091557ec3atagen2026-10-11 11:43 UTC44
            OracleTemplate::Clm { url } => Self::Clm {
3f9091557ec3atagen2026-10-11 11:43 UTC45
                clm: Clm::new(url),
3f9091557ec3atagen2026-10-11 11:43 UTC46
                technical: StdRng::seed_from_u64(seed),
3f9091557ec3atagen2026-10-11 11:43 UTC47
            },
3f9091557ec3atagen2026-10-11 11:43 UTC48
        }
3f9091557ec3atagen2026-10-11 11:43 UTC49
    }
3f9091557ec3atagen2026-10-11 11:43 UTC50
3f9091557ec3atagen2026-10-11 11:43 UTC51
    fn technical(&mut self) -> &mut StdRng {
3f9091557ec3atagen2026-10-11 11:43 UTC52
        match self {
3f9091557ec3atagen2026-10-11 11:43 UTC53
            Self::Rng(rng) => rng,
3f9091557ec3atagen2026-10-11 11:43 UTC54
            Self::Clm { technical, .. } => technical,
3f9091557ec3atagen2026-10-11 11:43 UTC55
        }
3f9091557ec3atagen2026-10-11 11:43 UTC56
    }
3f9091557ec3atagen2026-10-11 11:43 UTC57
3f9091557ec3atagen2026-10-11 11:43 UTC58
    /// Identity randomness, never a game decision.
3f9091557ec3atagen2026-10-11 11:43 UTC59
    pub(crate) fn u64(&mut self) -> u64 {
3f9091557ec3atagen2026-10-11 11:43 UTC60
        self.technical().random()
3f9091557ec3atagen2026-10-11 11:43 UTC61
    }
3f9091557ec3atagen2026-10-11 11:43 UTC62
3f9091557ec3atagen2026-10-11 11:43 UTC63
    /// A yes/no game roll with stated odds of 1 in `odds`.
3f9091557ec3atagen2026-10-11 11:43 UTC64
    pub(crate) fn chance(&mut self, roll: Roll, odds: u32) -> bool {
3f9091557ec3atagen2026-10-11 11:43 UTC65
        match self {
3f9091557ec3atagen2026-10-11 11:43 UTC66
            Self::Rng(rng) => rng.random_ratio(1, odds),
3f9091557ec3atagen2026-10-11 11:43 UTC67
            Self::Clm { clm, .. } => {
3f9091557ec3atagen2026-10-11 11:43 UTC68
                let ask = format!("{}, with odds of 1 in {odds}.", roll.ask);
3f9091557ec3atagen2026-10-11 11:43 UTC69
                clm.decide(&roll.state, roll.site, &ask) > 0.5
3f9091557ec3atagen2026-10-11 11:43 UTC70
            }
3f9091557ec3atagen2026-10-11 11:43 UTC71
        }
3f9091557ec3atagen2026-10-11 11:43 UTC72
    }
3f9091557ec3atagen2026-10-11 11:43 UTC73
3f9091557ec3atagen2026-10-11 11:43 UTC74
    /// A yes/no game roll where the ask describes the calm outcome, not the
3f9091557ec3atagen2026-10-11 11:43 UTC75
    /// event. The model believes dull statements more readily than rare
3f9091557ec3atagen2026-10-11 11:43 UTC76
    /// warnings, so the interrupts that read as drama get phrased as calm.
3f9091557ec3atagen2026-10-11 11:43 UTC77
    /// The rng arm still draws the event, never the calm.
3f9091557ec3atagen2026-10-11 11:43 UTC78
    pub(crate) fn chance_not(&mut self, roll: Roll, odds: u32) -> bool {
3f9091557ec3atagen2026-10-11 11:43 UTC79
        match self {
3f9091557ec3atagen2026-10-11 11:43 UTC80
            Self::Rng(rng) => rng.random_ratio(1, odds),
3f9091557ec3atagen2026-10-11 11:43 UTC81
            Self::Clm { clm, .. } => {
3f9091557ec3atagen2026-10-11 11:43 UTC82
                let ask = format!("{}, with odds of 1 in {odds}.", roll.ask);
3f9091557ec3atagen2026-10-11 11:43 UTC83
                clm.decide(&roll.state, roll.site, &ask) <= 0.5
3f9091557ec3atagen2026-10-11 11:43 UTC84
            }
3f9091557ec3atagen2026-10-11 11:43 UTC85
        }
3f9091557ec3atagen2026-10-11 11:43 UTC86
    }
3f9091557ec3atagen2026-10-11 11:43 UTC87
3f9091557ec3atagen2026-10-11 11:43 UTC88
    /// A yes/no game roll with general odds of `num` in `den`, put to the
3f9091557ec3atagen2026-10-11 11:43 UTC89
    /// model as a choice between the calm answer and the event. The noul head
3f9091557ec3atagen2026-10-11 11:43 UTC90
    /// answers interrupt questions with yes; the choice head, offered the two
3f9091557ec3atagen2026-10-11 11:43 UTC91
    /// answers side by side, does not.
3f9091557ec3atagen2026-10-11 11:43 UTC92
    pub(crate) fn ratio_choice(
3f9091557ec3atagen2026-10-11 11:43 UTC93
        &mut self,
3f9091557ec3atagen2026-10-11 11:43 UTC94
        roll: Roll,
3f9091557ec3atagen2026-10-11 11:43 UTC95
        num: u32,
3f9091557ec3atagen2026-10-11 11:43 UTC96
        den: u32,
3f9091557ec3atagen2026-10-11 11:43 UTC97
        calm: &str,
3f9091557ec3atagen2026-10-11 11:43 UTC98
        event: &str,
3f9091557ec3atagen2026-10-11 11:43 UTC99
    ) -> bool {
3f9091557ec3atagen2026-10-11 11:43 UTC100
        match self {
3f9091557ec3atagen2026-10-11 11:43 UTC101
            Self::Rng(rng) => rng.random_ratio(num, den),
3f9091557ec3atagen2026-10-11 11:43 UTC102
            Self::Clm { clm, .. } => {
3f9091557ec3atagen2026-10-11 11:43 UTC103
                let i = clm.choose(&roll.state, roll.site, &roll.ask, &[calm, event]);
3f9091557ec3atagen2026-10-11 11:43 UTC104
                i == 1
3f9091557ec3atagen2026-10-11 11:43 UTC105
            }
3f9091557ec3atagen2026-10-11 11:43 UTC106
        }
3f9091557ec3atagen2026-10-11 11:43 UTC107
    }
3f9091557ec3atagen2026-10-11 11:43 UTC108
3f9091557ec3atagen2026-10-11 11:43 UTC109
    /// A yes/no game roll with general odds of `num` in `den`.
3f9091557ec3atagen2026-10-11 11:43 UTC110
    pub(crate) fn ratio(&mut self, roll: Roll, num: u32, den: u32) -> bool {
3f9091557ec3atagen2026-10-11 11:43 UTC111
        match self {
3f9091557ec3atagen2026-10-11 11:43 UTC112
            Self::Rng(rng) => rng.random_ratio(num, den),
3f9091557ec3atagen2026-10-11 11:43 UTC113
            Self::Clm { clm, .. } => {
3f9091557ec3atagen2026-10-11 11:43 UTC114
                let ask = format!("{}, with odds of about {num} in {den}.", roll.ask);
3f9091557ec3atagen2026-10-11 11:43 UTC115
                clm.decide(&roll.state, roll.site, &ask) > 0.5
3f9091557ec3atagen2026-10-11 11:43 UTC116
            }
3f9091557ec3atagen2026-10-11 11:43 UTC117
        }
3f9091557ec3atagen2026-10-11 11:43 UTC118
    }
3f9091557ec3atagen2026-10-11 11:43 UTC119
3f9091557ec3atagen2026-10-11 11:43 UTC120
    /// One bucket chosen by weighted draw. The rng arm draws by `weights` and
3f9091557ec3atagen2026-10-11 11:43 UTC121
    /// ignores the descriptions; the clm arm reads them all and picks the one
3f9091557ec3atagen2026-10-11 11:43 UTC122
    /// the model believes, weights unused.
3f9091557ec3atagen2026-10-11 11:43 UTC123
    pub(crate) fn pick(&mut self, roll: Roll, weights: &[u32], options: &[&str]) -> usize {
3f9091557ec3atagen2026-10-11 11:43 UTC124
        match self {
3f9091557ec3atagen2026-10-11 11:43 UTC125
            Self::Rng(rng) => {
3f9091557ec3atagen2026-10-11 11:43 UTC126
                let total: u32 = weights.iter().sum();
3f9091557ec3atagen2026-10-11 11:43 UTC127
                let mut draw = rng.random_range(0..total);
3f9091557ec3atagen2026-10-11 11:43 UTC128
                for (i, w) in weights.iter().enumerate() {
3f9091557ec3atagen2026-10-11 11:43 UTC129
                    if draw < *w {
3f9091557ec3atagen2026-10-11 11:43 UTC130
                        return i;
3f9091557ec3atagen2026-10-11 11:43 UTC131
                    }
3f9091557ec3atagen2026-10-11 11:43 UTC132
                    draw -= *w;
3f9091557ec3atagen2026-10-11 11:43 UTC133
                }
3f9091557ec3atagen2026-10-11 11:43 UTC134
                weights.len() - 1
3f9091557ec3atagen2026-10-11 11:43 UTC135
            }
3f9091557ec3atagen2026-10-11 11:43 UTC136
            Self::Clm { clm, .. } => clm.choose(&roll.state, roll.site, &roll.ask, options),
3f9091557ec3atagen2026-10-11 11:43 UTC137
        }
3f9091557ec3atagen2026-10-11 11:43 UTC138
    }
3f9091557ec3atagen2026-10-11 11:43 UTC139
3f9091557ec3atagen2026-10-11 11:43 UTC140
    /// A value in `lo..hi`. The rng arm draws uniformly; the clm arm picks from
3f9091557ec3atagen2026-10-11 11:43 UTC141
    /// a 0.1-step grid so the model is choosing among described options.
3f9091557ec3atagen2026-10-11 11:43 UTC142
    pub(crate) fn span(&mut self, roll: Roll, lo: f64, hi: f64) -> f64 {
3f9091557ec3atagen2026-10-11 11:43 UTC143
        match self {
3f9091557ec3atagen2026-10-11 11:43 UTC144
            Self::Rng(rng) => rng.random_range(lo..hi),
3f9091557ec3atagen2026-10-11 11:43 UTC145
            Self::Clm { clm, .. } => {
3f9091557ec3atagen2026-10-11 11:43 UTC146
                let mut last = format!("{lo:.1}");
3f9091557ec3atagen2026-10-11 11:43 UTC147
                let mut values = Vec::new();
3f9091557ec3atagen2026-10-11 11:43 UTC148
                let mut v = lo;
3f9091557ec3atagen2026-10-11 11:43 UTC149
                while v < hi {
3f9091557ec3atagen2026-10-11 11:43 UTC150
                    values.push(last);
3f9091557ec3atagen2026-10-11 11:43 UTC151
                    v += 0.1;
3f9091557ec3atagen2026-10-11 11:43 UTC152
                    last = format!("{v:.1}");
3f9091557ec3atagen2026-10-11 11:43 UTC153
                }
3f9091557ec3atagen2026-10-11 11:43 UTC154
                let options: Vec<&str> = values.iter().map(String::as_str).collect();
3f9091557ec3atagen2026-10-11 11:43 UTC155
                let ask = format!("{}. Answer with a value.", roll.ask);
3f9091557ec3atagen2026-10-11 11:43 UTC156
                let i = clm.choose(&roll.state, roll.site, &ask, &options);
3f9091557ec3atagen2026-10-11 11:43 UTC157
                lo + 0.1 * i as f64
3f9091557ec3atagen2026-10-11 11:43 UTC158
            }
3f9091557ec3atagen2026-10-11 11:43 UTC159
        }
3f9091557ec3atagen2026-10-11 11:43 UTC160
    }
3f9091557ec3atagen2026-10-11 11:43 UTC161
3f9091557ec3atagen2026-10-11 11:43 UTC162
    /// An integer in `lo..=hi`. The rng arm draws uniformly; the clm arm picks
3f9091557ec3atagen2026-10-11 11:43 UTC163
    /// from the integers as described options.
3f9091557ec3atagen2026-10-11 11:43 UTC164
    pub(crate) fn span_int(&mut self, roll: Roll, lo: i32, hi: i32) -> i32 {
3f9091557ec3atagen2026-10-11 11:43 UTC165
        match self {
3f9091557ec3atagen2026-10-11 11:43 UTC166
            Self::Rng(rng) => rng.random_range(lo..=hi),
3f9091557ec3atagen2026-10-11 11:43 UTC167
            Self::Clm { clm, .. } => {
3f9091557ec3atagen2026-10-11 11:43 UTC168
                let values: Vec<String> = (lo..=hi).map(|v| v.to_string()).collect();
3f9091557ec3atagen2026-10-11 11:43 UTC169
                let options: Vec<&str> = values.iter().map(String::as_str).collect();
3f9091557ec3atagen2026-10-11 11:43 UTC170
                let ask = format!("{}. Answer with a whole number.", roll.ask);
3f9091557ec3atagen2026-10-11 11:43 UTC171
                let i = clm.choose(&roll.state, roll.site, &ask, &options);
3f9091557ec3atagen2026-10-11 11:43 UTC172
                lo + i as i32
3f9091557ec3atagen2026-10-11 11:43 UTC173
            }
3f9091557ec3atagen2026-10-11 11:43 UTC174
        }
3f9091557ec3atagen2026-10-11 11:43 UTC175
    }
3f9091557ec3atagen2026-10-11 11:43 UTC176
}
3f9091557ec3atagen2026-10-11 11:43 UTC177
3f9091557ec3atagen2026-10-11 11:43 UTC178
impl Clm {
3f9091557ec3atagen2026-10-11 11:43 UTC179
    fn new(url: &str) -> Self {
3f9091557ec3atagen2026-10-11 11:43 UTC180
        Self {
3f9091557ec3atagen2026-10-11 11:43 UTC181
            url: url.trim_end_matches('/').to_owned(),
3f9091557ec3atagen2026-10-11 11:43 UTC182
            agent: ureq::AgentBuilder::new()
3f9091557ec3atagen2026-10-11 11:43 UTC183
                .timeout(std::time::Duration::from_secs(60))
3f9091557ec3atagen2026-10-11 11:43 UTC184
                .build(),
3f9091557ec3atagen2026-10-11 11:43 UTC185
        }
3f9091557ec3atagen2026-10-11 11:43 UTC186
    }
3f9091557ec3atagen2026-10-11 11:43 UTC187
3f9091557ec3atagen2026-10-11 11:43 UTC188
    /// POST /v1/systemone with a single question; panics on any failure, which
3f9091557ec3atagen2026-10-11 11:43 UTC189
    /// surfaces as a 500 through the catch_panic layer. A dead oracle must not
3f9091557ec3atagen2026-10-11 11:43 UTC190
    /// quietly fall back to the rng.
3f9091557ec3atagen2026-10-11 11:43 UTC191
    fn ask(&self, state: &str, site: &str, question: Value) -> Value {
3f9091557ec3atagen2026-10-11 11:43 UTC192
        let body = json!({
3f9091557ec3atagen2026-10-11 11:43 UTC193
            "state": state,
3f9091557ec3atagen2026-10-11 11:43 UTC194
            "model": "clm-latest",
3f9091557ec3atagen2026-10-11 11:43 UTC195
            "questions": { site: question },
3f9091557ec3atagen2026-10-11 11:43 UTC196
            "temperature": 1.0,
3f9091557ec3atagen2026-10-11 11:43 UTC197
        });
3f9091557ec3atagen2026-10-11 11:43 UTC198
        let response = self
3f9091557ec3atagen2026-10-11 11:43 UTC199
            .agent
3f9091557ec3atagen2026-10-11 11:43 UTC200
            .post(&self.url)
3f9091557ec3atagen2026-10-11 11:43 UTC201
            .send_json(&body)
3f9091557ec3atagen2026-10-11 11:43 UTC202
            .unwrap_or_else(|e| panic!("oracle unreachable at {}: {e}", self.url));
3f9091557ec3atagen2026-10-11 11:43 UTC203
        let answer: Value = response
3f9091557ec3atagen2026-10-11 11:43 UTC204
            .into_json()
3f9091557ec3atagen2026-10-11 11:43 UTC205
            .unwrap_or_else(|e| panic!("oracle answer unreadable: {e}"));
3f9091557ec3atagen2026-10-11 11:43 UTC206
        answer["answers"][site].clone()
3f9091557ec3atagen2026-10-11 11:43 UTC207
    }
3f9091557ec3atagen2026-10-11 11:43 UTC208
3f9091557ec3atagen2026-10-11 11:43 UTC209
    fn decide(&self, state: &str, site: &str, ask: &str) -> f64 {
3f9091557ec3atagen2026-10-11 11:43 UTC210
        self.ask(state, site, json!({ "type": "noul", "instructions": ask }))["noul"]
3f9091557ec3atagen2026-10-11 11:43 UTC211
            .as_f64()
3f9091557ec3atagen2026-10-11 11:43 UTC212
            .unwrap_or_else(|| panic!("oracle noul answer for {site} missing"))
3f9091557ec3atagen2026-10-11 11:43 UTC213
    }
3f9091557ec3atagen2026-10-11 11:43 UTC214
3f9091557ec3atagen2026-10-11 11:43 UTC215
    fn choose(&self, state: &str, site: &str, ask: &str, options: &[&str]) -> usize {
3f9091557ec3atagen2026-10-11 11:43 UTC216
        let criteria: serde_json::Map<String, Value> = options
3f9091557ec3atagen2026-10-11 11:43 UTC217
            .iter()
3f9091557ec3atagen2026-10-11 11:43 UTC218
            .enumerate()
3f9091557ec3atagen2026-10-11 11:43 UTC219
            .map(|(i, text)| (i.to_string(), json!(text)))
3f9091557ec3atagen2026-10-11 11:43 UTC220
            .collect();
3f9091557ec3atagen2026-10-11 11:43 UTC221
        let q = if ask.trim_end().ends_with('?') {
3f9091557ec3atagen2026-10-11 11:43 UTC222
            ""
3f9091557ec3atagen2026-10-11 11:43 UTC223
        } else {
3f9091557ec3atagen2026-10-11 11:43 UTC224
            "?"
3f9091557ec3atagen2026-10-11 11:43 UTC225
        };
3f9091557ec3atagen2026-10-11 11:43 UTC226
        let instructions = format!("{ask}{q}");
3f9091557ec3atagen2026-10-11 11:43 UTC227
        let answer = self.ask(
3f9091557ec3atagen2026-10-11 11:43 UTC228
            state,
3f9091557ec3atagen2026-10-11 11:43 UTC229
            site,
3f9091557ec3atagen2026-10-11 11:43 UTC230
            json!({ "type": "choice", "instructions": instructions, "criteria": criteria }),
3f9091557ec3atagen2026-10-11 11:43 UTC231
        );
3f9091557ec3atagen2026-10-11 11:43 UTC232
        let choice = answer["choice"]
3f9091557ec3atagen2026-10-11 11:43 UTC233
            .as_str()
3f9091557ec3atagen2026-10-11 11:43 UTC234
            .unwrap_or_else(|| panic!("oracle choice answer for {site} missing"));
3f9091557ec3atagen2026-10-11 11:43 UTC235
        choice
3f9091557ec3atagen2026-10-11 11:43 UTC236
            .parse::<usize>()
e807266c323batagen2026-10-12 09:46 UTC237
            .ok()
e807266c323batagen2026-10-12 09:46 UTC238
            .filter(|&i| i < options.len())
e807266c323batagen2026-10-12 09:46 UTC239
            .unwrap_or_else(|| panic!("oracle chose {choice:?} for {site}, not an option index"))
3f9091557ec3atagen2026-10-11 11:43 UTC240
    }
3f9091557ec3atagen2026-10-11 11:43 UTC241
}