How lmjtfy decides what to do with a query: rules over facts, compiled into a Rete network that the page draws.
A fact is something known about the query: whether Jev can answer it, which kinds of question it reads as, whether it is several questions. A rule is a list of tests on facts and what to do when they all hold. The network is those rules with their shared tests merged: one alpha node per distinct test, and a chain of join nodes per rule, shared where rules begin alike.
Most facts are Jev's to give: each is one typed question about the input.
A rules engine would ask for them one at a time, as each rule came to
need one. This one does not. [Network::next] collects every Jev fact
that any rule still alive is waiting on, and asks for them together, so
they go out as one request with N questions (the user, 2026-10-02: "roll
up multiple rules that all use jev and parallelize them by firing a
single request with N questions to backfill"). Asking Jev more in one
request is what keeps the LLM, which costs far more, for the queries
that need it.
Nothing here does I/O. The Worker asks [Network::next] what to do, does
it, records what it learned in [Known], and asks again.
22#![forbid(unsafe_code)]
A way to read an input as a question, which is also the Jev type that answers it.
Yes or no, or how likely.
33 Noul,
The best among several possibilities.
35 Choice,
A degree, an amount, a rating.
37 Score,
The chance of something: how likely, what are the odds. Jev answers it as a yes-or-no about the thing itself, and the probability of yes is the answer.
Something known about a query.
Jev can judge the input at all.
52 Answerable,
It asks more than one separate question.
54 Several,
The input is to be read as this kind of question. Jev gives a probability for each kind; the likeliest is read, and so is the runner-up when Jev is split between them. So one, or two, hold.
58 Reads(Kind),
A how-much question that needs a scale written for it: a general scale would answer it badly.
61 Scale,
The answer to the input, read as a yes-or-no question.
63 Yes,
The answer to the input, read as a how-much question, on a fixed scale that needs nobody to write it.
66 Degree,
Whether the thing the input asks the chances of is so. The answer to a how-likely question is this fact's probability.
69 Chance,
The input is fit to show to strangers on a public page.
71 Fit,
The LLM wrote at least one question Jev can take.
73 Drafted,
A Jev question about the input. Every such fact that is wanted is asked for in the same request.
83 Jev,
The LLM, when a rule's effect asks it.
85 Llm,
90impl Fact { 91 pub const ALL: [Fact; 13] = [ 92 Fact::Answerable, 93 Fact::Several, 94 Fact::Reads(Kind::Noul), 95 Fact::Reads(Kind::Choice), 96 Fact::Reads(Kind::Score), 97 Fact::Reads(Kind::Chance), 98 Fact::Scale, 99 Fact::Yes, 100 Fact::Degree, 101 Fact::Chance, 102 Fact::Fit, 103 Fact::Drafted, 104 Fact::Judged, 105 ]; 106 107 pub fn source(self) -> Source { 108 match self { 109 Fact::Drafted => Source::Llm, 110 Fact::Judged => Source::JevAgain, 111 _ => Source::Jev, 112 } 113 }
Everything the fact can turn out to be.
The fact's name on the diagram and in a playground link.
124 pub fn name(self) -> &'static str { 125 match self { 126 Fact::Answerable => "answerable", 127 Fact::Several => "several", 128 Fact::Reads(Kind::Noul) => "yes-or-no", 129 Fact::Reads(Kind::Choice) => "pick", 130 Fact::Reads(Kind::Score) => "how-much", 131 Fact::Reads(Kind::Chance) => "how-likely", 132 Fact::Chance => "chance", 133 Fact::Scale => "scale", 134 Fact::Yes => "yes", 135 Fact::Degree => "degree", 136 Fact::Fit => "fit", 137 Fact::Drafted => "drafted", 138 Fact::Judged => "judged", 139 } 140 } 141}
What a fact turned out to be.
One test on one fact: an alpha node.
The test without its fact's name, as the diagram labels the node.
What the LLM is asked to write.
The options of a pick.
189 Options,
The levels of a how-much.
191 Scale,
Something a rule does that costs a call and teaches a fact.
Ask the LLM to write Jev's questions. Teaches [Fact::Drafted].
Every drafting rule that holds is served by the one call
([Network::wants]).
202 Draft(Want),
Something that is so when a rule holds, and that decides no step: the Worker reads it once the query has ended.
The question may go on the public feed.
228 List,
This fact is an answer to show: Jev's own (Yes, Degree, Chance),
or its answers to what the LLM wrote (Judged).
234#[derive(Clone, Copy, Debug, PartialEq, Eq)] 235pub enum Then { 236 Do(Effect), 237 End(End), 238 Note(Note), 239} 240 241pub struct Rule { 242 pub name: &'static str, 243 pub when: &'static [Test], 244 pub then: Then, 245} 246 247const YES: Value = Value::Bool(true); 248const NO: Value = Value::Bool(false); 249const ONE: [Test; 2] = [Test::Is(Fact::Answerable, YES), Test::Is(Fact::Several, NO)];
lmjtfy's rules. Order is priority: of the rules that hold, the first with something left to do decides. When Jev is split between two readings, the rules of both hold, and both are answered.
254pub const RULES: &[Rule] = &[ 255 Rule { name: "refuse", when: &[Test::Is(Fact::Answerable, NO)], then: Then::End(End::NotAQuestion) }, 256 Rule { 257 name: "answer yes or no", 258 when: &[ONE[0], ONE[1], Test::Is(Fact::Reads(Kind::Noul), YES), Test::Known(Fact::Yes)], 259 then: Then::Note(Note::Show(Fact::Yes)), 260 }, 261 Rule { 262 name: "grade it", 263 when: &[ 264 ONE[0], 265 ONE[1], 266 Test::Is(Fact::Reads(Kind::Score), YES), 267 Test::Is(Fact::Scale, NO), 268 Test::Known(Fact::Degree), 269 ], 270 then: Then::Note(Note::Show(Fact::Degree)), 271 }, 272 Rule { 273 name: "give the odds", 274 when: &[ONE[0], ONE[1], Test::Is(Fact::Reads(Kind::Chance), YES), Test::Known(Fact::Chance)], 275 then: Then::Note(Note::Show(Fact::Chance)), 276 }, 277 Rule { 278 name: "draft a scale", 279 when: &[ONE[0], ONE[1], Test::Is(Fact::Reads(Kind::Score), YES), Test::Is(Fact::Scale, YES)], 280 then: Then::Do(Effect::Draft(Want::Scale)), 281 }, 282 Rule { 283 name: "draft options", 284 when: &[ONE[0], ONE[1], Test::Is(Fact::Reads(Kind::Choice), YES)], 285 then: Then::Do(Effect::Draft(Want::Options)), 286 }, 287 Rule { 288 name: "split it up", 289 when: &[Test::Is(Fact::Answerable, YES), Test::Is(Fact::Several, YES)], 290 then: Then::Do(Effect::Draft(Want::Split)), 291 }, 292 Rule { name: "judge the drafts", when: &[Test::Is(Fact::Drafted, YES)], then: Then::Do(Effect::Judge) }, 293 Rule { 294 name: "show the answers", 295 when: &[Test::Is(Fact::Judged, YES)], 296 then: Then::Note(Note::Show(Fact::Judged)), 297 }, 298 Rule { 299 name: "list it", 300 when: &[Test::Is(Fact::Answerable, YES), Test::Is(Fact::Fit, YES)], 301 then: Then::Note(Note::List), 302 }, 303];
What is known about a query so far.
Where a node stands, given what is known.
Every test up to here holds.
327 Holds,
A test up to here is known to fail. Nothing downstream can fire.
329 Fails,
A join node: everything left requires, and one more test. left is
another join, or None when this is a rule's first test.
Index into [Network::alphas].
350 pub alpha: usize,
A rule's place in the network: the join that holds when the rule does.
One per distinct test, however many rules use it.
367 pub alphas: Vec<Test>,
Shared by every rule that begins with the same tests.
A rule ends the query outright.
380 End(End),
No rule has anything left to do and nothing more can be learned: the query is over, and what to show is in the notes.
386impl Network { 387 pub fn compile(rules: &[Rule]) -> Self { 388 let mut network = Network { alphas: Vec::new(), joins: Vec::new(), terminals: Vec::new() }; 389 for rule in rules { 390 let mut left = None; 391 for (depth, test) in rule.when.iter().enumerate() { 392 let alpha = network.alphas.iter().position(|known| known == test).unwrap_or_else(|| { 393 network.alphas.push(*test); 394 network.alphas.len() - 1 395 }); 396 let join = Join { left, alpha, depth: depth + 1 }; 397 left = Some(network.joins.iter().position(|known| *known == join).unwrap_or_else(|| { 398 network.joins.push(join); 399 network.joins.len() - 1 400 })); 401 } 402 let join = left.expect("a rule has at least one test"); 403 network.terminals.push(Terminal { name: rule.name, join, then: rule.then }); 404 } 405 network 406 }
413 pub fn alpha(&self, alpha: usize, known: &Known) -> State { 414 let test = self.alphas[alpha]; 415 match (test, known.get(test.fact())) { 416 (_, None) => State::Waits, 417 (Test::Known(_), Some(_)) => State::Holds, 418 (Test::Is(_, wanted), Some(value)) if wanted == value => State::Holds, 419 (Test::Is(..), Some(_)) => State::Fails, 420 } 421 } 422 423 pub fn join(&self, join: usize, known: &Known) -> State { 424 let Join { left, alpha, .. } = self.joins[join]; 425 let here = self.alpha(alpha, known); 426 left.map_or(here, |left| self.join(left, known).and(here)) 427 }
The facts a join's tests are on, first test first.
What to do, given what is known, and the rule that says so (its index
in [Network::terminals]), when one does.
A rule that holds decides, in rule order; an effect whose fact is already known has been done and is passed over. If no rule decides, every Jev fact that a rule not yet failed is waiting on is wanted, and they are asked for together.
444 pub fn decide(&self, known: &Known) -> (Option<usize>, Next) { 445 for (index, terminal) in self.terminals.iter().enumerate() { 446 if self.join(terminal.join, known) != State::Holds { 447 continue; 448 } 449 match terminal.then { 450 Then::End(end) => return (Some(index), Next::End(end)), 451 Then::Do(effect) if known.get(effect.teaches()).is_none() => return (Some(index), Next::Do(effect)), 452 Then::Do(_) | Then::Note(_) => {} 453 } 454 } 455 let mut wanted: Vec<Fact> = self 456 .terminals 457 .iter() 458 .filter(|terminal| self.join(terminal.join, known) == State::Waits) 459 .flat_map(|terminal| self.facts(terminal.join)) 460 .filter(|fact| fact.source() == Source::Jev && known.get(*fact).is_none()) 461 .collect(); 462 wanted.sort(); 463 wanted.dedup(); 464 (None, if wanted.is_empty() { Next::Done } else { Next::Ask(wanted) }) 465 }
What to do, given what is known.
The names of the rules that hold, in rule order.
Whether a rule that holds says note.
The answers to show, in rule order.
Everything the LLM is to write, for every drafting rule that holds: one call serves them all.
The joins and alphas that a rule that holds stands on: what, of everything known, turned out to matter.
513 pub fn used(&self, known: &Known) -> (Vec<bool>, Vec<bool>) { 514 let mut joins = vec![false; self.joins.len()]; 515 let mut alphas = vec![false; self.alphas.len()]; 516 for terminal in &self.terminals { 517 if self.join(terminal.join, known) != State::Holds { 518 continue; 519 } 520 let mut at = Some(terminal.join); 521 while let Some(join) = at { 522 joins[join] = true; 523 alphas[self.joins[join].alpha] = true; 524 at = self.joins[join].left; 525 } 526 } 527 (joins, alphas) 528 } 529}