Skip to main content

borg/
outcome.rs

1//! What happened after the conscience asked a lesson: the signal for down-weighting lessons
2//! that are generic or do not help, and for trust per engineer × area (trust.rs).
3//!
4//! `$BORG_HOME/outcomes.jsonl` is append-only and deterministic, one record per line:
5//! - `asked`: the conscience asked these lessons at a stop or live, or delivered them as context (early,
6//!   or a graded check's 2s and 1s handed over free; hook, 0 tokens);
7//! - `asked_then_corrected`: after lesson X was asked, the engineer still made a correction in the
8//!   same session that `borg learn` merged into X (same concern): asking did not prevent it;
9//! - `asked_not_corrected`: the session ended, or has been idle for `IDLE_MS`, and no later
10//!   correction was merged into X. The idle rule keeps both kinds coming from every `borg learn`
11//!   run: without it, a session whose `end` hook never ran could only ever add corrected counts.
12//!
13//! A correction that contradicts a lesson asked earlier in the session (`supersedes`) also marks that
14//! lesson `asked_then_corrected`; when the conscience rendered asked lessons as a disagreement (the
15//! `asked` record's `groups`), a correction siding with one of them marks the other sides, not the
16//! one it agreed with (`contradicted`).
17//!
18//! - `asked_then_changed` (hook, no extra call): at a later Stop of the session, the consequence model's
19//!   call (predict.rs) also judged, by meaning, that the drone changed its work on the lesson's point
20//!   and that the change avoids the consequence: a use, the deposit that keeps a lesson active
21//!   (memory.rs). The drone's `<id>: applies|skip` is evidence in that prompt, never a label alone;
22//! - `asked_ignored` (hook): the same judge found the drone changed nothing on that point.
23//! - `vetoed` (`borg no`, veto.rs, 0 tokens): the engineer said no to the session's work; every lesson
24//!   shown in the session before it is credited, the strongest negative result.
25//!
26//! - `regretted` (`borg learn`, 0 tokens): a regret episode (a later fix of a decision, or a review finding;
27//!   learn/regret.rs) generalised into a lesson blames the lessons asked in the deciding session before the
28//!   decision (`regretted`): asking did not prevent it. `credit` carries each lesson's share of the blame;
29//!   memory.rs weighs it below a correction. Deliberately not read by trust.rs (`sessions`), by `weight`
30//!   (the legacy single-number view) or by attention.rs: a regret is about a lesson, not about an
31//!   engineer following it.
32//!
33//! - `tested` (`borg learn`, 0 tokens): the session's own test runs (hook.rs `capture_test`) ended green
34//!   after a lesson was asked as a question (a free delivery is none): a failed run after the ask, then a
35//!   pass, or a pass after the drone changed its work on the lesson's point (`asked_then_changed`); never
36//!   for a question the drone ignored (`asked_then_ignored`). A
37//!   weak credit (memory.rs `TESTED_W`), once per (session, lesson) (`tested`). It is about the work,
38//!   not about an engineer following a lesson: never a use, and not read by trust.rs, `weight` or
39//!   attention.rs.
40//!
41//! An unclear verdict (`asked_unclear`, measured, never a result), or a session whose later turn ends
42//! never reach the model (the prefilter skipped them, or BORG_MATCH=bm25), leaves the lesson pending:
43//! `settle` turns it into `asked_not_corrected` with the reason it had no verdict (`reason`). That is
44//! censored for beliefs (memory.rs): no correction followed, but whether the drone acted on the lesson
45//! is unknown.
46//!
47//! - `delivery` (hook, local): per graded check and lesson, the route delivery actually took (`route`:
48//!   asked, free, placebo, dropped because done or over the character budget, never delivered) and why,
49//!   so what a check decided (`graded`) and what reached the drone never disagree unseen.
50//! - A placebo (hook.rs `#borg-placebo`): an apply the check routed nowhere, judged blindly where a free
51//!   delivery would be. Its results carry channel `placebo` (`PLACEBO`): the judge's floor, no result for
52//!   any reader that learns from results (`is_result`), never shared. One the judge never gave a verdict
53//!   settles as `asked_not_corrected` on that channel with its reason (`settle_with`), as an unjudged ask
54//!   does: the selection summary censors it (select/summary.rs `settled`).
55//!
56//! `asked` and `graded` records carry `p`, each lesson's probability of being asked under the policy in
57//! force at that check (its propensity), for off-policy evaluation of another policy from this log. An
58//! `asked` record from a Thompson slot draw (the bm25 and non-graded paths) also carries that draw's
59//! pool (`pool`, `base`, `scoped`): every competitor with its score before the usefulness factor, so
60//! `borg debug ope` (ope.rs) can recompute the propensity under other belief weights. Local only: the team
61//! projection drops them.
62//!
63//! **Team outcomes.** The raw log stays on this machine (it holds session ids and repos). `publish`
64//! appends a redacted, deterministic projection of the results the team may see (`SHARED_KINDS`) to
65//! `outcomes-team.jsonl` (`TEAM_FILE`), which the mesh syncs (borg-mesh lines.rs `SYNCED`): kind,
66//! lesson ids, time, propensity, channel, the hashed session and repo (`tag`), a regret's hashed cause
67//! and credit, and the node, never raw sessions, repos, causes, groups, grades or text. Only the belief readers opt in to the team view
68//! (`read_team`): memory and trust in the conscience (conscience.rs `load_as`), `borg trust`, `borg
69//! memory` and borg-web's lessons. learn.rs (it would settle remote sessions), attention.rs (labels and
70//! the explore counter are this node's policy), stats.rs (rollups sync their own counts), install.rs and
71//! the hook's pending/judge read this machine's log only. BORG_SHARE_OUTCOMES=off publishes and reads
72//! nothing of the team's.
73//!
74//! A session counts once per lesson. If a resumed session gets both results, corrected wins.
75//! memory.rs derives activation and the usefulness belief from these results; `weight` below is the
76//! earlier, single-number view (kept for reporting and for comparison in the R1 replay).
77
78use std::collections::HashMap;
79use std::path::Path;
80
81use serde::{Deserialize, Serialize};
82
83pub const ASKED: &str = "asked";
84pub const CORRECTED: &str = "asked_then_corrected";
85pub const NOT_CORRECTED: &str = "asked_not_corrected";
86pub const CHANGED: &str = "asked_then_changed";
87pub const IGNORED: &str = "asked_ignored";
88/// One graded check: every lesson the model graded as applying (`lessons`, in its order, with
89/// `grades` and `ranks`), and which of them the check chose to ask (`chosen`). The denominator for
90/// learning how many to ask: a lesson that applied but was not chosen can still turn out needed.
91pub const GRADED: &str = "graded";
92/// A lesson a graded check of the session found applying but did not ask, which a later correction
93/// in that session then merged into or superseded: a question that was needed and not asked (`missed`).
94pub const MISSED: &str = "applied_then_corrected";
95/// The engineer vetoed the session's work (`borg no`, veto.rs): every lesson shown in the session
96/// before it is credited. The strongest negative result.
97pub const VETOED: &str = "vetoed";
98/// A lesson asked in a session before a decision that a later fix (or a review finding) showed was
99/// wrong (learn.rs `credit_regrets`): asking did not prevent it. `cause` names the regret episode and
100/// `credit` carries the share of the blame, (0, 1]. Weaker than a correction (memory.rs `REGRET`).
101pub const REGRETTED: &str = "regretted";
102
103/// The session's own tests went green after the lesson was asked (`tested`): red then green, or green
104/// after a change on the lesson's point. Once per (session, lesson), dated at that pass.
105pub const TESTED: &str = "tested";
106
107/// The judge saw the lesson's delivery but could not tell (an `unclear` verdict), or found a change that
108/// still leads to the consequence (predict.rs `Judged::label` gives neither a use nor an ignore): the
109/// lesson stays pending for another try. A label-health measurement only: no belief, trust, statistic,
110/// team line or off-policy decision reads it (`counts`).
111pub const UNCLEAR: &str = "asked_unclear";
112
113/// What delivery did with a graded check's decision, per check and lesson (hook.rs): `route` (one of
114/// `ROUTES`), `reason`, `check` the check that decided it. The latest record per (check, lesson) is
115/// the route it took (a session that ends with a lesson queued gets `never_delivered`; resumed, its
116/// later delivery supersedes that). Local and never a result: the denominator of what each route led
117/// to, read by the selection replay (select.rs), not by beliefs.
118pub const DELIVERY: &str = "delivery";
119
120/// The session's end hook ran (hook.rs `on_end`), after its `never_delivered` records: one per end, a
121/// resumed session's again at its next. Local and never a result: where the selection summary counts a
122/// session's delivery tallies (select/summary.rs), whatever else the session logged, so a session with
123/// nothing queued and no ask left open counts too.
124pub const ENDED: &str = "ended";
125
126/// The channel of a placebo's judge results (`asked_then_changed`, `asked_ignored`, `asked_unclear`): a
127/// graded apply never delivered, judged blindly where a free delivery would be (hook.rs `#borg-placebo`).
128/// Its "changed" rate is the judge's own false positives plus what the drone would have done anyway, so
129/// it is never a result about the lesson (`is_result`).
130pub const PLACEBO: &str = "placebo";
131
132/// Whether `o` is evidence about its lessons for the readers that learn from results (memory.rs, trust.rs,
133/// the statistics, the team file): not a placebo's result (`PLACEBO`) and not an unclear verdict
134/// (`UNCLEAR`).
135#[must_use]
136pub fn is_result(o: &Outcome) -> bool {
137    o.channel != PLACEBO && o.kind != UNCLEAR
138}
139
140/// Why a lesson settled as `asked_not_corrected` had no verdict (`reason`, `settle_why`). Protocol: the
141/// judge gave one (`UNJUDGED_NONE`, the settle only says no correction followed); its tries ran out on
142/// unclear or missing verdicts (hook.rs `MAX_JUDGE_TRIES`); a fuller judge call left it out
143/// (hook.rs `MAX_PENDING`); its last judge call failed; no model call came after its delivery; or (a
144/// placebo) a later delivery of the lesson in the session took its place in the judge's queue
145/// (`UNJUDGED_LATER`, hook.rs `pending`).
146pub const UNJUDGED_NONE: &str = "judged";
147pub const UNJUDGED_TRIES: &str = "tries";
148pub const UNJUDGED_SLOTS: &str = "slots";
149pub const UNJUDGED_FAILED: &str = "failed";
150pub const UNJUDGED_NO_CALL: &str = "no_call";
151pub const UNJUDGED_LATER: &str = "later";
152
153/// Why a `graded` record's check took the fixed rule instead of the learned policy in force (`reason`;
154/// empty when it decided by the policy in force, hook.rs `decide`). Protocol: another check held the
155/// store's decision lock past hook.rs `DECIDE_WAIT`, the lock could not be taken at all, or the learned
156/// decision's record would not or did not land in the log its gate is folded from (learning off, a
157/// `BORG_CONSCIENCE` outside the store the hook records to, a failed append), so its gate would never be
158/// debited for it. Only the learned policy reads the gate and takes the lock; such a record carries
159/// `policy` fixed and is replayed as the fixed rule.
160pub const DECIDED_BUSY: &str = "lock_busy";
161pub const DECIDED_NO_LOCK: &str = "lock_failed";
162pub const DECIDED_NO_RECORD: &str = "unrecorded";
163
164/// What one correction adds against a lesson, in units of an ignore: a correction of the same concern
165/// right after asking is strong evidence the question did not help, a session without one weak evidence
166/// (the engineer may not have looked). Baseline: design-chosen (no evaluation behind the 2), used by
167/// `weight` and memory.rs `Memory::beta_with` alike; `borg debug ope` (ope.rs) estimates other belief
168/// weights offline, never applies them. Fixed by dated decision (2026-10-31): 1–2 corrections after an
169/// ask per store, and `borg debug ope` finds 0 usable decisions.
170pub const CORRECTED_W: u32 = 2;
171
172/// A session without asks or episodes for this long is treated as ended (24h). Guard: a session whose
173/// `end` hook never ran would otherwise never settle; its state file must be as quiet (learn.rs).
174pub const IDLE_MS: i64 = crate::clock::DAY_MS;
175
176#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
177pub struct Outcome {
178    pub kind: String,
179    pub session: String,
180    pub lessons: Vec<String>,
181    pub ms: i64,
182    pub repo: String,
183    /// `asked` only: the asked lessons rendered as one disagreement (conscience.rs `disagreements`).
184    #[serde(default, skip_serializing_if = "Vec::is_empty")]
185    pub groups: Vec<Vec<String>>,
186    /// How the lessons were asked (`asked` and the judge's results): `stop` (a turn-end block), `live`
187    /// (a live check during the turn, live.rs), `early` (early delivery, conscience.rs `early`),
188    /// `queued` (a graded check's 2s and 1s, handed over free) or `placebo` (`PLACEBO`: never shown,
189    /// judged blindly where a free delivery would be). Empty in records from before delivery modes.
190    #[serde(default, skip_serializing_if = "String::is_empty")]
191    pub channel: String,
192    /// `graded` and `asked`: each lesson's grade (3 certain, 2 likely, 1 worth a thought) and its rank in
193    /// the model's applies (0 first), aligned with `lessons`, and how many lessons the check graded as
194    /// applying; on `asked`, 0 where no check of the session graded the lesson. For learning how many to
195    /// ask (attention.rs reads them from `graded`).
196    #[serde(default, skip_serializing_if = "Vec::is_empty")]
197    pub grades: Vec<u8>,
198    #[serde(default, skip_serializing_if = "Vec::is_empty")]
199    pub ranks: Vec<u16>,
200    #[serde(default, skip_serializing_if = "is_zero")]
201    pub applied: u16,
202    /// `graded` only: the lessons the check chose to ask.
203    #[serde(default, skip_serializing_if = "Vec::is_empty")]
204    pub chosen: Vec<String>,
205    /// `graded` only: the check explored, asking every apply up to `attention::RAIL` whatever the cut,
206    /// so its outcomes label the cut (attention.rs).
207    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
208    pub explore: bool,
209    /// `regretted`: the regret episode (`R…`/`V…` id). The idempotence key with the lesson.
210    #[serde(default, skip_serializing_if = "String::is_empty")]
211    pub cause: String,
212    /// `regretted` only: the lesson's share of the regret, (0, 1].
213    #[serde(default, skip_serializing_if = "is_zero_f")]
214    pub credit: f64,
215    /// `asked` and `graded`: per lesson (aligned with `lessons`), the probability that the policy in force
216    /// at that check asked it: 1 or 0 under the fixed graded rule, the explore mixture under the learned
217    /// cut (attention.rs `propensities`), the share of Thompson draws that give it a slot in
218    /// `budget`/`widen` (conscience.rs `propensity`), the share that delivers it early (conscience.rs
219    /// `early_propensity`), 1 for a free hand-over of a graded check's 2s and 1s. Rounded to 4 decimals
220    /// (`round_p`). Empty in records from before propensities and when unknown (BORG_ONLY, a state file
221    /// from an older binary). For off-policy evaluation (ope.rs).
222    #[serde(default, skip_serializing_if = "Vec::is_empty")]
223    pub p: Vec<f64>,
224    /// `asked` from a Thompson slot draw only (conscience.rs `budget_pool`/`widen_pool`, hook.rs
225    /// `#borg-pool`): the pool the check drew from, in ranking order, the asked lessons included; each
226    /// one's score without its usefulness factor (conscience.rs `pool_base`, unrounded so a replay is
227    /// exact); and whether it is scoped. Aligned with each other, not with `lessons`. Empty for graded
228    /// and live asks and free deliveries. Local: never shared (`share`). For `borg debug ope` (ope.rs).
229    #[serde(default, skip_serializing_if = "Vec::is_empty")]
230    pub pool: Vec<String>,
231    #[serde(default, skip_serializing_if = "Vec::is_empty")]
232    pub base: Vec<f64>,
233    #[serde(default, skip_serializing_if = "Vec::is_empty")]
234    pub scoped: Vec<bool>,
235    // The selection fields (select.rs). Every one is left out when empty, so a record without them
236    // writes the line an older binary wrote, and an older line reads with them empty. Local: `share`
237    // copies none of them.
238    /// The graded check (select.rs `check_id`): on `graded`, and on every record of a lesson that check
239    /// routed (its delivery, the judge's result, a placebo's), so a result joins the decision that made
240    /// it. Empty in records from before checks had ids.
241    #[serde(default, skip_serializing_if = "String::is_empty")]
242    pub check: String,
243    /// `graded`: the selection policy in force (select.rs `Policy::name`: fixed, cap, attention,
244    /// learned). Empty in records from before select.rs, which read as the fixed rule (`policy_of`).
245    #[serde(default, skip_serializing_if = "String::is_empty")]
246    pub policy: String,
247    /// `graded`: the fit the policy decided with (select/policy.rs `Fit::key`); empty for the prior.
248    #[serde(default, skip_serializing_if = "String::is_empty")]
249    pub fit: String,
250    /// `graded`: the selection summary the decision read (select/summary.rs `Summary::key`: the bytes
251    /// folded and their hash), and what set its routes (select.rs `Log::source`). A learned check reads
252    /// the summary again under the store's decision lock after its model call (hook.rs `decide`), so
253    /// every learned decision recorded before it is in it; a check by a policy that reads no gate, and
254    /// one that fell back to the fixed rule (`reason`), names the summary it read at its start, which
255    /// records of checks that decided meanwhile may follow. Records other than decisions (a delivery, a
256    /// verdict) may still land between that read and this record; `borg debug select` P1 replays from
257    /// the logged state.
258    /// Empty in older records and for a failed call.
259    #[serde(default, skip_serializing_if = "String::is_empty")]
260    pub seen: String,
261    #[serde(default, skip_serializing_if = "String::is_empty")]
262    pub source: String,
263    /// `graded`: the lessons the check handed over free, in delivery order (`free_of` for older records).
264    #[serde(default, skip_serializing_if = "Vec::is_empty")]
265    pub free: Vec<String>,
266    /// `graded`: per lesson (aligned with `lessons`), its probability of being handed over free under
267    /// the policy in force, as `p` is of being asked; rounded like `p`. Empty in older records, where
268    /// a free lesson's was 1 under the fixed rule.
269    #[serde(default, skip_serializing_if = "Vec::is_empty")]
270    pub pf: Vec<f64>,
271    /// `graded`: the applies the check did not deliver and drew for the placebo arm (judged blindly
272    /// where a free delivery would be, never shown), and each undelivered apply's chance of that draw.
273    #[serde(default, skip_serializing_if = "Vec::is_empty")]
274    pub placebo: Vec<String>,
275    #[serde(default, skip_serializing_if = "is_default")]
276    pub psi: f64,
277    /// `graded`: the graded applies that could not go free at this check because the session already
278    /// had them delivered (select.rs `Ctx::had`, conscience.rs `graded_without`), in the applies' order:
279    /// the applies among the session's deliveries when the check decided, and among the lessons waiting
280    /// in its queue to be asked or handed over by another check (hook.rs `decide`, `waiting_in`), so the
281    /// record alone holds every `had` that bears on its decision.
282    #[serde(default, skip_serializing_if = "Vec::is_empty")]
283    pub nofree: Vec<String>,
284    /// `graded`, a check of the randomised question-count test (select/policy.rs `Decision::xk`,
285    /// sources `experiment`, `experiment_control` and `gate`): `xk` the fixed rule's delivered non-3
286    /// lessons that may go free (k), `xs` how many of them it asked (s, drawn uniformly in 0..=k; 0 is
287    /// logged). None on every other record, so older lines and checks outside the test write back
288    /// unchanged. Local like the rest of a graded record (`share`).
289    #[serde(default, skip_serializing_if = "Option::is_none")]
290    pub xk: Option<u32>,
291    #[serde(default, skip_serializing_if = "Option::is_none")]
292    pub xs: Option<u32>,
293    /// `graded`: the collective's tolerance the check was decided under (select.rs `Log::alpha`, its
294    /// `risk`), so a replay decides under it whatever the config says then; None at the default
295    /// (select/policy.rs `ALPHA`), so older lines and default collectives write back unchanged.
296    #[serde(default, skip_serializing_if = "Option::is_none")]
297    pub alpha: Option<f64>,
298    /// `graded`: the lessons the pool budget dropped from the model call that a tail sample showed
299    /// anyway (whole.rs `tail`, under every policy), and each one's chance of being sampled (one chance
300    /// for all of the check's tail: k drawn uniformly of the `eligible` − `shown` dropped, unrounded).
301    /// Not in `shown` or `tokens`; each has its place past the budget's in `fill`, after the applies'
302    /// (and, if it applies, among them too), so every place from `shown` to `eligible` − 1 is one
303    /// dropped lesson, shown with chance `pt`.
304    #[serde(default, skip_serializing_if = "Vec::is_empty")]
305    pub tail: Vec<String>,
306    #[serde(default, skip_serializing_if = "is_default")]
307    pub pt: f64,
308    /// An early delivery's record: per lesson (aligned with `lessons`), its score as a share of the best
309    /// on-topic match, done or not (conscience.rs `early_pool`, the share `RELATIVE` bars; hook.rs
310    /// `early_rel`), rounded as `p` is, for a learned early rule. A standing lesson has one too, though
311    /// no score gates it; empty when no plain lesson was on topic to scale by.
312    #[serde(default, skip_serializing_if = "Vec::is_empty")]
313    pub rel: Vec<f64>,
314    /// A delivery's actual route for every lesson of the record (one of `ROUTES`), and why: a drop's
315    /// cause, or why a delivered lesson stayed unjudged (no later call, the judge's slots, its tries);
316    /// on `graded`, why its check fell back to the fixed rule (`DECIDED_BUSY`, `DECIDED_NO_LOCK`,
317    /// `DECIDED_NO_RECORD`).
318    #[serde(default, skip_serializing_if = "String::is_empty")]
319    pub route: String,
320    #[serde(default, skip_serializing_if = "String::is_empty")]
321    pub reason: String,
322    /// `graded`, the pool the model graded: lessons eligible for it, lessons its budget showed, their
323    /// size in tokens (the tail sample beside them in `tail`), and per lesson (aligned with `lessons`)
324    /// its fill position among the shown (0 first), so the yield of each position can be learned from
325    /// every check, a check without applies included; then, past the applies', one position per tail
326    /// lesson (aligned with `tail`), so the yield past the budget is learned from the lessons a tail
327    /// showed that did not apply too (select/summary.rs `tail_of`). A tail lesson's position goes on
328    /// past the budget's: `shown` + its place among the lessons the budget dropped, in the order the
329    /// fill met them (whole.rs `Filled::dropped`), so it is never below `shown`.
330    #[serde(default, skip_serializing_if = "is_default")]
331    pub eligible: u32,
332    #[serde(default, skip_serializing_if = "is_default")]
333    pub shown: u32,
334    #[serde(default, skip_serializing_if = "is_default")]
335    pub tokens: u32,
336    /// `graded`, the pool's token budget its check filled to (hook.rs `check`: `BORG_STOP_POOL_TOKENS`,
337    /// else select.rs `pool_tokens`), so a check at a budget other than the baseline shows. 0: not
338    /// logged (a record from before 0.7.3, or a check without the whole pool).
339    #[serde(default, skip_serializing_if = "is_default")]
340    pub budget: u32,
341    #[serde(default, skip_serializing_if = "Vec::is_empty")]
342    pub fill: Vec<u16>,
343    /// `graded`, the model call: the model (empty: the CLI's default), a hash of the prompt template,
344    /// the harness, borg's version, the call's duration, and whether it failed (the check then asked
345    /// nothing: fail open); then its cost (`usd`).
346    #[serde(default, skip_serializing_if = "String::is_empty")]
347    pub model: String,
348    #[serde(default, skip_serializing_if = "String::is_empty")]
349    pub prompt_hash: String,
350    #[serde(default, skip_serializing_if = "String::is_empty")]
351    pub harness: String,
352    #[serde(default, skip_serializing_if = "String::is_empty")]
353    pub version: String,
354    #[serde(default, skip_serializing_if = "is_default")]
355    pub dur_ms: u64,
356    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
357    pub failed: bool,
358    /// `graded`: the call's cost in dollars as the harness's CLI reported it (0: it reported none).
359    /// Measured and shown (`borg debug select` P3), never a limit.
360    #[serde(default, skip_serializing_if = "is_default")]
361    pub usd: f64,
362    /// The judge's results (`asked_then_changed`, `asked_ignored`): the judge template's version, 0 for
363    /// the template before versions were logged; and whether the judge saw the question without the
364    /// drone's `<id>: applies|skip` declaration (a random half are, to measure what it adds).
365    #[serde(default, skip_serializing_if = "is_default")]
366    pub judge: u8,
367    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
368    pub stripped: bool,
369}
370
371/// `route` values. Protocol: what a check decided for a lesson, then what delivery did with it.
372/// Asked, handed over free, or judged as a placebo, as decided; or dropped at delivery because the
373/// session had the lesson already (`ROUTE_DROPPED_DONE`) or the delivery budget was spent
374/// (`ROUTE_DROPPED_CHARS`); or never delivered because the session ended first (`ROUTE_NEVER`).
375pub const ROUTE_ASKED: &str = "asked";
376pub const ROUTE_FREE: &str = "free";
377pub const ROUTE_PLACEBO: &str = "placebo";
378pub const ROUTE_DROPPED_DONE: &str = "dropped_done";
379pub const ROUTE_DROPPED_CHARS: &str = "dropped_chars";
380pub const ROUTE_NEVER: &str = "never_delivered";
381pub const ROUTES: [&str; 6] = [ROUTE_ASKED, ROUTE_FREE, ROUTE_PLACEBO, ROUTE_DROPPED_DONE, ROUTE_DROPPED_CHARS, ROUTE_NEVER];
382
383/// The policy a `graded` record was decided under: its `policy`; for a record from before select.rs,
384/// the fixed rule (`fixed`), unless it shows the one other policy an older binary logged,
385/// `BORG_STOP_ATTENTION=on`'s learned cut (`attention`): it explored, or a propensity lies strictly
386/// between 0 and 1 (attention.rs `propensities`). A learned cut that asked every apply up to its rail
387/// logged only 0s and 1s, and an evaluation's `BORG_STOP_ASK_CAP` logged nothing of its cap: both read
388/// as the fixed rule.
389#[must_use]
390pub fn policy_of(o: &Outcome) -> &str {
391    match o.policy.as_str() {
392        "" if o.explore || o.p.iter().any(|p| *p > 0.0 && *p < 1.0) => "attention",
393        "" => "fixed",
394        p => p,
395    }
396}
397
398/// The lessons a `graded` record handed over free: its `free`; for a record from before select.rs
399/// (`policy` empty), its policy's rule (`policy_of`) replayed on its grades: the fixed rule's 2s, then
400/// 1s, at most `conscience::CAP`, when no lesson is a 3 (conscience.rs `graded_capped`), else none; the
401/// learned cut's lessons after the ones it asked, at most `conscience::CAP` (attention.rs `split`).
402#[must_use]
403pub fn free_of(o: &Outcome) -> Vec<String> {
404    use crate::conscience::{self, Challenge};
405    if !o.policy.is_empty() || o.grades.len() != o.lessons.len() {
406        return o.free.clone();
407    }
408    if o.grades.contains(&3) {
409        return match policy_of(o) {
410            "attention" => o.lessons.iter().filter(|l| !o.chosen.contains(l)).take(conscience::CAP).cloned().collect(),
411            _ => Vec::new(),
412        };
413    }
414    let ls: Vec<Challenge> = o.lessons.iter().map(|id| Challenge { id: id.clone(), ..Default::default() }).collect();
415    let applied: Vec<(&Challenge, u8)> = ls.iter().zip(o.grades.iter().copied()).collect();
416    conscience::graded_capped(&applied, None).1.into_iter().map(|c| c.id.clone()).collect()
417}
418
419fn is_default<T: Default + PartialEq>(v: &T) -> bool {
420    *v == T::default()
421}
422
423/// A propensity rounded to 4 decimals, so lines stay deterministic and short.
424pub fn round_p(p: f64) -> f64 {
425    (p * 1e4).round() / 1e4
426}
427
428fn is_zero(v: &u16) -> bool {
429    *v == 0
430}
431
432fn is_zero_f(v: &f64) -> bool {
433    *v == 0.0
434}
435
436/// The outcomes store next to a lessons file.
437pub fn path_beside(lessons: &Path) -> std::path::PathBuf {
438    lessons.with_file_name("outcomes.jsonl")
439}
440
441/// The redacted outcomes the team sees, at the store root beside `outcomes.jsonl` (module doc).
442pub const TEAM_FILE: &str = "outcomes-team.jsonl";
443/// Beside `TEAM_FILE`, never synced: the length of `outcomes.jsonl` at the last `publish`.
444pub const PUBLISHED: &str = "outcomes-team.published";
445
446/// The kinds `publish` shares. A whitelist: `graded` and any later kind stay local until deliberately
447/// added here.
448///
449/// Closed under memory.rs `derive`: every kind that changes a lesson's result for a session, or its
450/// belief, travels, so a peer folds the same memory. `vetoed` outranks the session's `changed` (shared
451/// alone, a lesson vetoed here would gain a use and spread there); `regretted` carries its `credit` and
452/// its hashed `cause` (`Shared::c`), the key derive dedupes regrets by. `graded` is the asking policy's
453/// denominator, not a result, and never moves the activity clock (`memory::Clock` counts shared kinds).
454///
455/// Unguarded: any member can write a line under any session tag (tags are public in this file), so a
456/// forged `asked_then_corrected` or `vetoed` overrides a teammate's session on every node but that
457/// teammate's, and a few forged `asked_then_changed` with fresh tags and two repo tags consolidate a
458/// lesson. `node` is self-declared. The mesh admits only members' signed lines (borg-mesh origin.rs),
459/// so this is the trusted-member model; the deferred guards (a session tag bound to its first signer,
460/// consolidation from at least two signers) would close it.
461///
462/// `tested` is shared like `asked_then_changed`, the result it is weaker than: memory folds it, so a peer
463/// without it would hold another belief. Both are this node's own reading of its session (the judge's,
464/// the test runner's output), so the trusted-member model above is the same;
465/// `team_view` counts a peer's `tested` line only for lessons the team's records show its session was
466/// given. It never moves the activity clock either.
467pub const SHARED_KINDS: [&str; 9] = [ASKED, CHANGED, IGNORED, CORRECTED, NOT_CORRECTED, MISSED, VETOED, REGRETTED, TESTED];
468
469/// A session or repo as the team sees it: the first 32 hex digits of blake3("borg-outcome-v1\0" + s).
470/// Unsalted on purpose: the same session hashes to the same tag on every node, so a node can drop its
471/// own published copies (`read_team`).
472pub fn tag(s: &str) -> String {
473    let mut h = blake3::Hasher::new();
474    h.update(b"borg-outcome-v1\0");
475    h.update(s.as_bytes());
476    h.finalize().to_hex()[..32].to_string()
477}
478
479/// `tag`, keeping an empty repo empty.
480fn tag_repo(repo: &str) -> String {
481    if repo.is_empty() { String::new() } else { tag(repo) }
482}
483
484/// One line of `TEAM_FILE`. Never carries `id` and `version` together (the mesh would settle it as a
485/// versioned record).
486#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
487pub struct Shared {
488    pub kind: String,
489    /// `tag` of the session.
490    pub s: String,
491    pub lessons: Vec<String>,
492    pub ms: i64,
493    /// The publishing node (stats.rs `node`), self-declared: dedup does not depend on it.
494    pub node: String,
495    #[serde(default, skip_serializing_if = "Vec::is_empty")]
496    pub p: Vec<f64>,
497    #[serde(default, skip_serializing_if = "String::is_empty")]
498    pub channel: String,
499    /// `tag` of the repo: memory.rs counts distinct repos to consolidate a lesson.
500    #[serde(default, skip_serializing_if = "String::is_empty")]
501    pub r: String,
502    /// `tag` of a `regretted` record's cause (the regret episode): memory.rs counts each regret once.
503    #[serde(default, skip_serializing_if = "String::is_empty")]
504    pub c: String,
505    /// A `regretted` record's share of the blame.
506    #[serde(default, skip_serializing_if = "is_zero_f")]
507    pub credit: f64,
508}
509
510/// `o` as the team sees it, or None for a kind not in `SHARED_KINDS` and for a placebo's result
511/// (`is_result`): the judge's floor on this node, never a result a peer folds.
512pub fn share(o: &Outcome, node: &str) -> Option<Shared> {
513    (SHARED_KINDS.contains(&o.kind.as_str()) && is_result(o)).then(|| Shared {
514        kind: o.kind.clone(),
515        s: tag(&o.session),
516        lessons: o.lessons.clone(),
517        ms: o.ms,
518        node: node.into(),
519        p: o.p.clone(),
520        channel: o.channel.clone(),
521        r: tag_repo(&o.repo),
522        c: tag_repo(&o.cause),
523        credit: o.credit,
524    })
525}
526
527/// Team outcomes on: `BORG_SHARE_OUTCOMES` is not `off`.
528pub fn on() -> bool {
529    std::env::var("BORG_SHARE_OUTCOMES").as_deref() != Ok("off")
530}
531
532/// Appends to `TEAM_FILE` in `home` the shared lines of `home`'s `outcomes.jsonl` it lacks; returns
533/// how many. The lines are deterministic, so a re-run appends nothing; two runs at once may append the
534/// same line twice, which the mesh's union and the per-session count make harmless. Nothing when off.
535/// A local log no longer than at the last publish (`PUBLISHED`, its length then) is not read again: the
536/// log only grows, so nothing new can be in it.
537pub fn publish(home: &Path) -> usize {
538    let local = home.join("outcomes.jsonl");
539    let team = home.join(TEAM_FILE);
540    let mark = home.join(PUBLISHED);
541    let len = std::fs::metadata(&local).map_or(0, |m| m.len());
542    if !on() || len == 0 {
543        return 0;
544    }
545    let published = std::fs::read_to_string(&mark).ok().and_then(|t| t.trim().parse::<u64>().ok());
546    if team.exists() && published == Some(len) {
547        return 0;
548    }
549    let mine = std::fs::read_to_string(&local).unwrap_or_default();
550    let node = crate::stats::node(home);
551    let text = std::fs::read_to_string(&team).unwrap_or_default();
552    let mut have: std::collections::HashSet<String> = text.lines().map(str::to_string).collect();
553    let mut out = String::new();
554    // This engineer's personal lessons (reach.rs) are no team's business, not even by id.
555    let personal = crate::reach::personal_ids(home);
556    let records = mine.lines().filter_map(|l| serde_json::from_str::<Outcome>(l).ok()).filter_map(|mut o| {
557        let named = !o.lessons.is_empty();
558        o.lessons.retain(|id| !personal.contains(id));
559        (!named || !o.lessons.is_empty()).then_some(o)
560    });
561    for line in records.filter_map(|o| share(&o, &node)).filter_map(|x| serde_json::to_string(&x).ok()) {
562        if have.insert(line.clone()) {
563            out.push_str(&line);
564            out.push('\n');
565        }
566    }
567    if out.is_empty() {
568        let _ = std::fs::write(&mark, mine.len().to_string());
569        return 0;
570    }
571    use std::io::Write;
572    let open = text.as_bytes().last().is_some_and(|c| *c != b'\n');
573    let written = std::fs::OpenOptions::new()
574        .create(true)
575        .append(true)
576        .open(&team)
577        .and_then(|mut f| f.write_all(format!("{}{out}", if open { "\n" } else { "" }).as_bytes()));
578    if written.is_err() {
579        return 0;
580    }
581    let _ = std::fs::write(&mark, mine.len().to_string());
582    out.lines().count()
583}
584
585/// The belief view of the outcomes beside `lessons`: this machine's log plus, unless
586/// BORG_SHARE_OUTCOMES=off, the team's (`team_view`).
587pub fn read_team(lessons: &Path) -> Vec<Outcome> {
588    team_view(lessons, on())
589}
590
591/// Off: this machine's log as is. On: its records with each repo and cause as its `tag` (memory.rs only
592/// compares them for distinctness, so beliefs are the same and comparable with the team's), then the
593/// team's lines that count (`team_parts`). A remote session then counts once per lesson like a local
594/// one (memory.rs `derive`, `sessions`). learn.rs keys on raw causes: it reads this
595/// machine's log (`read`).
596pub fn team_view(lessons: &Path, on: bool) -> Vec<Outcome> {
597    let (mut out, team) = team_parts(lessons, on);
598    if on {
599        for o in &mut out {
600            o.repo = tag_repo(&o.repo);
601            o.cause = tag_repo(&o.cause);
602        }
603    }
604    out.extend(team);
605    out
606}
607
608/// This machine's records as written, and (on) every `TEAM_FILE` line of a shared kind whose session
609/// is not one of this machine's (that drops this node's own published copies, whatever its node id),
610/// once per (kind, session, lessons, ms, cause): one read of each file, so the two never disagree on
611/// where the local records end (ope.rs reads both). A kind this binary does not share (a newer node's,
612/// or an older one's hive verdict) is ignored, and so is a `tested` line naming a lesson the team's
613/// records do not show its session was given.
614pub fn team_parts(lessons: &Path, on: bool) -> (Vec<Outcome>, Vec<Outcome>) {
615    let local = read(&path_beside(lessons));
616    if !on {
617        return (local, Vec::new());
618    }
619    let mine: std::collections::HashSet<String> = local.iter().map(|o| tag(&o.session)).collect();
620    let text = std::fs::read_to_string(lessons.with_file_name(TEAM_FILE)).unwrap_or_default();
621    let lines: Vec<Shared> = text.lines().filter_map(|l| serde_json::from_str::<Shared>(l).ok()).collect();
622    // The lessons each team session's shared records name: a green run credits only lessons it was given.
623    let mut given: std::collections::HashMap<&str, std::collections::HashSet<&str>> = std::collections::HashMap::new();
624    for x in lines.iter().filter(|x| SHARED_KINDS.contains(&x.kind.as_str()) && x.kind != TESTED) {
625        given.entry(x.s.as_str()).or_default().extend(x.lessons.iter().map(String::as_str));
626    }
627    let mut seen: std::collections::HashSet<(String, String, Vec<String>, i64, String)> = std::collections::HashSet::new();
628    let mut team = Vec::new();
629    for x in lines.iter() {
630        if !SHARED_KINDS.contains(&x.kind.as_str()) || mine.contains(&x.s) || !seen.insert((x.kind.clone(), x.s.clone(), x.lessons.clone(), x.ms, x.c.clone()))
631        {
632            continue;
633        }
634        if x.kind == TESTED && !x.lessons.iter().all(|l| given.get(x.s.as_str()).is_some_and(|g| g.contains(l.as_str()))) {
635            continue;
636        }
637        team.push(Outcome {
638            kind: x.kind.clone(),
639            session: x.s.clone(),
640            lessons: x.lessons.clone(),
641            ms: x.ms,
642            repo: x.r.clone(),
643            p: x.p.clone(),
644            channel: x.channel.clone(),
645            cause: x.c.clone(),
646            credit: x.credit,
647            ..Default::default()
648        });
649    }
650    (local, team)
651}
652
653pub fn read(path: &Path) -> Vec<Outcome> {
654    std::fs::read_to_string(path).unwrap_or_default().lines().filter_map(|l| serde_json::from_str(l).ok()).collect()
655}
656
657/// Appends `o` in one write (learn.rs `append_line`: concurrent writers never tear a record), and
658/// counts it in the usage statistics (`count_result`).
659pub fn append(path: &Path, o: &Outcome) -> std::io::Result<()> {
660    crate::learn::append_jsonl(path, o)?;
661    if let Some(home) = path.parent() {
662        count_result(home, o);
663    }
664    Ok(())
665}
666
667/// `o` in the usage statistics of the store `home`: one event of its result kind with its lessons and
668/// channel. `asked` is already counted as the Stop's block, a `vetoed` result as the `veto` that
669/// credited it (veto.rs); a placebo's result is no result (`is_result`): its "changed" never joins the
670/// deliveries' counts. The judge (hook.rs) writes one record per routing check but counts the lessons
671/// of a call's channel as one event, as before checks had ids.
672pub fn count_result(home: &Path, o: &Outcome) {
673    if !is_result(o) {
674        return;
675    }
676    let kind = match o.kind.as_str() {
677        CHANGED => crate::stats::CHANGED,
678        IGNORED => crate::stats::IGNORED,
679        CORRECTED => crate::stats::CORRECTED,
680        NOT_CORRECTED => crate::stats::NOT_CORRECTED,
681        REGRETTED => crate::stats::REGRETTED,
682        TESTED => crate::stats::TESTED,
683        _ => return,
684    };
685    crate::stats::count(home, kind, &o.session, |e| {
686        e.repo = o.repo.clone();
687        e.lessons = o.lessons.clone();
688        e.channel = o.channel.clone();
689    });
690}
691
692/// Did the conscience ask `lesson` in `session` before `ms`?
693pub fn asked_before(outcomes: &[Outcome], session: &str, lesson: &str, ms: i64) -> bool {
694    outcomes.iter().any(|o| o.kind == ASKED && o.session == session && o.ms < ms && o.lessons.iter().any(|l| l == lesson))
695}
696
697/// Is a result (`kind`) already recorded for `lesson` in `session`? Keeps `borg learn` idempotent.
698pub fn has(outcomes: &[Outcome], kind: &str, session: &str, lesson: &str) -> bool {
699    outcomes.iter().any(|o| o.kind == kind && o.session == session && o.lessons.iter().any(|l| l == lesson))
700}
701
702/// The lessons a correction at `ms` in `session` shows were asked in vain, each asked in `session`
703/// before `ms` and not yet recorded as corrected or vetoed there (a veto is the stronger result): the lessons the correction `supersedes`
704/// (it contradicts them), and `target`, the lesson it was merged into (the same concern came up
705/// again). When `target` was asked as one side of a rendered disagreement, the engineer sided with
706/// it: the other sides of that disagreement were corrected, not `target`. Sorted, unique.
707pub fn contradicted(outcomes: &[Outcome], session: &str, ms: i64, target: Option<&str>, supersedes: &[String]) -> Vec<String> {
708    let mut out: Vec<String> = supersedes.to_vec();
709    if let Some(t) = target {
710        let sides: Vec<String> = outcomes
711            .iter()
712            .filter(|o| o.kind == ASKED && o.session == session && o.ms < ms)
713            .flat_map(|o| &o.groups)
714            .filter(|g| g.iter().any(|x| x == t))
715            .flatten()
716            .filter(|x| *x != t)
717            .cloned()
718            .collect();
719        match sides.is_empty() {
720            true => out.push(t.to_string()),
721            false => out.extend(sides),
722        }
723    }
724    out.retain(|l| asked_before(outcomes, session, l, ms) && !has(outcomes, CORRECTED, session, l) && !has(outcomes, VETOED, session, l));
725    out.sort();
726    out.dedup();
727    out
728}
729
730/// Was `o` a free delivery (channel `early` or `queued`): lessons handed over as context, no question?
731pub fn free(o: &Outcome) -> bool {
732    o.channel == "early" || o.channel == "queued"
733}
734
735/// The lessons a correction at `ms` in `session` shows were needed and not asked: of the lessons it
736/// `supersedes` and `target` (the lesson it was merged into), those a graded check of the session found
737/// applying before `ms` without choosing them, never asked as a question in the session before `ms` (a
738/// free delivery asks nothing: a lesson only handed over as context stays a miss for the cut,
739/// attention.rs), and not yet recorded as missed there. Sorted, unique.
740pub fn missed(outcomes: &[Outcome], session: &str, ms: i64, target: Option<&str>, supersedes: &[String]) -> Vec<String> {
741    let mut out: Vec<String> = supersedes.iter().cloned().chain(target.map(str::to_string)).collect();
742    let applied_unchosen = |l: &str| {
743        outcomes.iter().any(|o| o.kind == GRADED && o.session == session && o.ms < ms && o.lessons.iter().any(|x| x == l) && !o.chosen.iter().any(|x| x == l))
744    };
745    let questioned = |l: &str| outcomes.iter().any(|o| o.kind == ASKED && !free(o) && o.session == session && o.ms < ms && o.lessons.iter().any(|x| x == l));
746    out.retain(|l| applied_unchosen(l) && !questioned(l) && !has(outcomes, MISSED, session, l));
747    out.sort();
748    out.dedup();
749    out
750}
751
752/// Has `lesson` been credited for the regret `cause` already (in any session)?
753fn credited(outcomes: &[Outcome], cause: &str, lesson: &str) -> bool {
754    outcomes.iter().any(|o| o.kind == REGRETTED && o.cause == cause && o.lessons.iter().any(|l| l == lesson))
755}
756
757/// The lessons a regret blames: those asked in `session` (the deciding one) before `before_ms`, without
758/// a correction or a veto there already (both outrank regretted, as in memory.rs) and not yet credited
759/// for `cause`. When some of `target` (the lesson the regret was folded into, and the lessons that lesson
760/// supersedes) were asked, they share the blame alone: the same concern came up again, and when all of
761/// them already took a correction or a veto for it, nobody else is blamed. Otherwise every asked lesson
762/// shares it equally (the Shapley value of a symmetric game). A lesson a graded check found applying but
763/// did not ask was never shown, so it is not blamed. One record per lesson, sorted, at `ms`,
764/// with `credit = strength / n`; n counts the blamed lessons before the already-credited ones are left
765/// out, so a re-run writes nothing and the shares never change. Every record's session has an earlier
766/// `asked` record, so it never moves memory.rs's `Clock`.
767#[allow(clippy::too_many_arguments)]
768pub fn regretted(outcomes: &[Outcome], session: &str, before_ms: i64, cause: &str, target: &[String], strength: f64, ms: i64, repo: &str) -> Vec<Outcome> {
769    if session.is_empty() || before_ms <= 0 || strength <= 0.0 {
770        return Vec::new();
771    }
772    let mut asked: Vec<&str> =
773        outcomes.iter().filter(|o| o.kind == ASKED && o.session == session && o.ms < before_ms).flat_map(|o| o.lessons.iter().map(String::as_str)).collect();
774    asked.sort_unstable();
775    asked.dedup();
776    if asked.iter().any(|l| target.iter().any(|t| t == l)) {
777        asked.retain(|l| target.iter().any(|t| t == l));
778    }
779    asked.retain(|l| !has(outcomes, CORRECTED, session, l) && !has(outcomes, VETOED, session, l));
780    let credit = (strength / asked.len().max(1) as f64).min(1.0);
781    asked
782        .into_iter()
783        .filter(|l| !credited(outcomes, cause, l))
784        .map(|l| Outcome {
785            kind: REGRETTED.into(),
786            session: session.into(),
787            lessons: vec![l.into()],
788            ms,
789            repo: repo.into(),
790            cause: cause.into(),
791            credit,
792            ..Default::default()
793        })
794        .collect()
795}
796
797/// The `tested` records a session's own test runs earn (`tests`: its `<state>/<session>.tests`, `fail <ms>`
798/// and `pass <ms>` lines, hook.rs `capture_test`): each lesson asked as a question in `session` (a free
799/// delivery asks nothing, `free`: handed over at every prompt of its scope, it would gain from every
800/// test-first session) whose last test result after its first ask passed, after a failed run since that ask (red, then green) or a change the drone
801/// made on the lesson's point (`asked_then_changed`), and that has no `tested` record there yet, nor a
802/// correction or a veto (the engineer's word outranks a green run), nor `asked_then_ignored` (red, then
803/// green is also how a feature is built: it does not make an ignored question useful). A pass before the ask says nothing
804/// of the lesson. One record per lesson, sorted, at that last pass. Every record's session has an
805/// earlier `asked` record, so it never moves memory.rs's `Clock`.
806pub fn tested(outcomes: &[Outcome], session: &str, tests: &str) -> Vec<Outcome> {
807    let mut runs: Vec<(i64, bool)> = tests
808        .lines()
809        .filter_map(|l| l.split_once(' '))
810        .filter_map(|(w, m)| Some((m.trim().parse::<i64>().ok()?, matches!(w, "pass" | "fail").then_some(w == "pass")?)))
811        .collect();
812    runs.sort_unstable();
813    // Each lesson's first ask in the session, and its repo.
814    let mut first: HashMap<&str, (i64, &str)> = HashMap::new();
815    for o in outcomes.iter().filter(|o| o.kind == ASKED && !free(o) && o.session == session) {
816        for l in &o.lessons {
817            let e = first.entry(l.as_str()).or_insert((o.ms, o.repo.as_str()));
818            if o.ms < e.0 {
819                *e = (o.ms, o.repo.as_str());
820            }
821        }
822    }
823    let mut out: Vec<Outcome> = first
824        .into_iter()
825        .filter(|(l, _)| ![TESTED, CORRECTED, VETOED, IGNORED].iter().any(|k| has(outcomes, k, session, l)))
826        .filter_map(|(l, (at, repo))| {
827            let after = &runs[runs.partition_point(|r| r.0 <= at)..];
828            let &(ms, pass) = after.last()?;
829            (pass && (after.iter().any(|r| !r.1) || has(outcomes, CHANGED, session, l))).then(|| Outcome {
830                kind: TESTED.into(),
831                session: session.into(),
832                lessons: vec![l.into()],
833                ms,
834                repo: repo.into(),
835                ..Default::default()
836            })
837        })
838        .collect();
839    out.sort_by(|a, b| a.lessons.cmp(&b.lessons));
840    out
841}
842
843/// At session end: every lesson asked in `session` that has no result yet was not corrected, and every
844/// placebo of it without a verdict is closed. A vetoed lesson already has its result. `settle_with`,
845/// every unjudged lesson's reason `no_call`.
846pub fn settle(outcomes: &[Outcome], session: &str, ms: i64) -> Vec<Outcome> {
847    settle_with(outcomes, session, ms, &|_| UNJUDGED_NO_CALL)
848}
849
850/// `settle`, each record with its `reason`: `judged` when the judge gave the lesson's latest ask in the
851/// session a verdict (`asked_then_changed`, `asked_ignored`), else `unjudged(lesson)` (one of the
852/// `UNJUDGED_*`, from the session's state file, hook.rs `unjudged`); and its `check`, the check behind
853/// that latest `asked` record. A verdict on an earlier delivery says nothing of a later question: a
854/// stop that judges a lesson handed over free and then asks it writes the verdict first, and no judge
855/// call need follow the answer. Nor does a verdict on a free delivery (`free`) say anything of a
856/// question (or one on a question of a free delivery), whenever it came: an older binary's live check
857/// judged the earlier free delivery of a lesson asked since and not yet answered (hook.rs `pending`),
858/// and its question, never judged, then settles censored instead of staying open in the selection
859/// summary (select/summary.rs `settled`).
860/// Then every placebo of the session (a `delivery` routed `placebo`, which writes no `asked`) that no
861/// verdict and no earlier settle closed (`open_placebos`): `asked_not_corrected` on channel `placebo`
862/// (no result, `is_result`; it ends no ask's wait, `resolves`) with the placebo's check and repo, and
863/// the reason `unjudged(lesson)` when it is the lesson's latest delivery in the session, else
864/// `UNJUDGED_LATER`. So the selection summary censors an unjudged placebo as it does an unjudged ask
865/// (select/summary.rs `settled`), and the judge-missingness fit sees both.
866/// Not a result: memory.rs reads `asked_not_corrected` as censored, so a verdict recorded after it (a
867/// resumed session) counts once, alone.
868/// The asks first, sorted by lesson id, then the placebos by lesson id and check, so the appended lines
869/// are deterministic. A few passes over the session's records, whatever the number of asks.
870pub fn settle_with(outcomes: &[Outcome], session: &str, ms: i64, unjudged: &dyn Fn(&str) -> &'static str) -> Vec<Outcome> {
871    let mine: Vec<&Outcome> = outcomes.iter().filter(|o| o.session == session).collect();
872    let resolved: std::collections::HashSet<&str> = mine.iter().filter(|o| resolves(o)).flat_map(|o| o.lessons.iter().map(String::as_str)).collect();
873    // Per lesson: its verdicts' (ms, check, on a free delivery).
874    let mut verdicts: HashMap<&str, Vec<(i64, &str, bool)>> = HashMap::new();
875    for o in mine.iter().filter(|o| (o.kind == CHANGED || o.kind == IGNORED) && is_result(o)) {
876        for l in &o.lessons {
877            verdicts.entry(l.as_str()).or_default().push((o.ms, o.check.as_str(), free(o)));
878        }
879    }
880    // Per lesson: the first of its asks' repos in sort order (as before checks were kept), and the
881    // ms, check and whether free of its latest ask.
882    let mut asked: std::collections::BTreeMap<&str, (&str, i64, &str, bool)> = std::collections::BTreeMap::new();
883    // Per lesson: the ms of its latest delivery, an `asked` for every one the drone sees and a
884    // placebo's `delivery`.
885    let mut latest: HashMap<&str, i64> = HashMap::new();
886    for o in mine.iter().filter(|o| o.kind == ASKED || is_placebo(o)) {
887        for l in o.lessons.iter().map(String::as_str) {
888            let t = latest.entry(l).or_insert(o.ms);
889            *t = (*t).max(o.ms);
890            if o.kind != ASKED || resolved.contains(l) {
891                continue;
892            }
893            let e = asked.entry(l).or_insert((o.repo.as_str(), o.ms, o.check.as_str(), free(o)));
894            e.0 = e.0.min(o.repo.as_str());
895            if o.ms >= e.1 {
896                (e.1, e.2, e.3) = (o.ms, o.check.as_str(), free(o));
897            }
898        }
899    }
900    // A verdict on the latest ask is on the same kind of delivery (a question, or free) and came after it
901    // (the judge sees a delivery in a later call), or names its check. Time first: an older binary's stop
902    // wrote one `asked` naming the stop's check also for a lesson another check routed (hook.rs
903    // `#borg-routed`), whose verdict names that check; a stop now writes one `asked` per routing check
904    // (hook.rs `stop_ask`).
905    let judged = |l: &str, at: i64, check: &str, as_free: bool| {
906        verdicts.get(l).is_some_and(|v| v.iter().any(|&(t, c, f)| f == as_free && (t > at || (!check.is_empty() && c == check))))
907    };
908    let mut out: Vec<Outcome> = asked
909        .into_iter()
910        .map(|(l, (repo, at, check, as_free))| Outcome {
911            kind: NOT_CORRECTED.into(),
912            session: session.into(),
913            lessons: vec![l.into()],
914            ms,
915            repo: repo.into(),
916            check: check.into(),
917            reason: if judged(l, at, check, as_free) { UNJUDGED_NONE } else { unjudged(l) }.into(),
918            // The binary that settled: from 0.7.3 on, the selection summary closes every delivery of the
919            // lesson the session left without a verdict (select/summary.rs `settled`).
920            version: env!("CARGO_PKG_VERSION").into(),
921            ..Default::default()
922        })
923        .collect();
924    let mut placebos: Vec<Outcome> = open_placebos(&mine)
925        .into_iter()
926        .map(|(l, d)| Outcome {
927            kind: NOT_CORRECTED.into(),
928            session: session.into(),
929            lessons: vec![l.into()],
930            ms,
931            repo: d.repo.clone(),
932            channel: PLACEBO.into(),
933            check: d.check.clone(),
934            // Delivered again later, the lesson's later delivery is the one the judge saw.
935            reason: if latest.get(l).is_some_and(|&t| t > d.ms) { UNJUDGED_LATER } else { unjudged(l) }.into(),
936            ..Default::default()
937        })
938        .collect();
939    placebos.sort_by(|a, b| (&a.lessons, &a.check).cmp(&(&b.lessons, &b.check)));
940    out.extend(placebos);
941    out
942}
943
944/// Does `o` end an ask's wait for its result (`settle`)? A placebo's settle record ends none: a
945/// placebo is never asked, and the lesson may be asked later in the session.
946fn resolves(o: &Outcome) -> bool {
947    matches!(o.kind.as_str(), CORRECTED | NOT_CORRECTED | VETOED) && o.channel != PLACEBO
948}
949
950/// A placebo's delivery (`delivery` routed `placebo`, hook.rs `queued`): judged blindly, never shown,
951/// so it writes no `asked`.
952fn is_placebo(o: &Outcome) -> bool {
953    o.kind == DELIVERY && o.route == ROUTE_PLACEBO
954}
955
956/// Does `r`, a placebo channel's verdict (`asked_then_changed`, `asked_ignored`) or settle record
957/// (`asked_not_corrected`), close placebo delivery `d` of its lesson? A settle record names the
958/// placebo's own check, so it closes exactly that one, and settling again writes nothing. A verdict
959/// names the check it judged (a check routes a lesson once), or, a record from before check ids, came
960/// after the placebo. An unclear verdict closes nothing, as it leaves an ask pending.
961fn closes(r: &Outcome, d: &Outcome) -> bool {
962    match r.kind.as_str() {
963        NOT_CORRECTED => r.check == d.check,
964        CHANGED | IGNORED if r.check.is_empty() || d.check.is_empty() => r.ms > d.ms,
965        CHANGED | IGNORED => r.check == d.check,
966        _ => false,
967    }
968}
969
970/// The open placebos among `mine` (one session's records), in log order: per placebo delivery
971/// (`is_placebo`) and lesson, none of the session's placebo records `closes` it.
972fn open_placebos<'a>(mine: &[&'a Outcome]) -> Vec<(&'a str, &'a Outcome)> {
973    let mut closers: HashMap<&str, Vec<&Outcome>> = HashMap::new();
974    for &o in mine.iter().filter(|o| o.channel == PLACEBO && !is_placebo(o)) {
975        for l in &o.lessons {
976            closers.entry(l.as_str()).or_default().push(o);
977        }
978    }
979    mine.iter()
980        .copied()
981        .filter(|o| is_placebo(o))
982        .flat_map(|d| d.lessons.iter().map(move |l| (l.as_str(), d)))
983        .filter(|&(l, d)| !closers.get(l).is_some_and(|rs| rs.iter().any(|r| closes(r, d))))
984        .collect()
985}
986
987/// Sessions with asks or placebos that are idle at `now`: their newest ask or placebo and newest
988/// episode (`last_episode`, newest episode ms per session) are at least `IDLE_MS` old. Sorted. Only
989/// sessions with an ask still without its result or an open placebo (`open_placebos`), so `borg learn`
990/// (under the store lock) settles what is new, not every session it ever settled: one pass over the
991/// log.
992pub fn idle_sessions<'a>(outcomes: &'a [Outcome], last_episode: &HashMap<String, i64>, now: i64) -> Vec<&'a str> {
993    let mut newest: HashMap<&str, i64> = HashMap::new();
994    let (mut asked, mut resolved) = (std::collections::HashSet::new(), std::collections::HashSet::new());
995    // Per session, its placebo channel's records: deliveries, verdicts and settles.
996    let mut placebos: HashMap<&str, Vec<&Outcome>> = HashMap::new();
997    for o in outcomes {
998        if o.channel == PLACEBO || is_placebo(o) {
999            placebos.entry(o.session.as_str()).or_default().push(o);
1000        }
1001        if o.kind == ASKED || is_placebo(o) {
1002            let m = newest.entry(o.session.as_str()).or_insert(o.ms);
1003            *m = (*m).max(o.ms);
1004        }
1005        let into = match o.kind.as_str() {
1006            ASKED => &mut asked,
1007            _ if resolves(o) => &mut resolved,
1008            _ => continue,
1009        };
1010        into.extend(o.lessons.iter().map(|l| (o.session.as_str(), l.as_str())));
1011    }
1012    let mut open: std::collections::HashSet<&str> = asked.difference(&resolved).map(|(s, _)| *s).collect();
1013    open.extend(placebos.iter().filter(|(_, rs)| !open_placebos(rs).is_empty()).map(|(s, _)| *s));
1014    let mut idle: Vec<&str> = newest
1015        .into_iter()
1016        .filter(|&(s, ms)| open.contains(s) && ms.max(last_episode.get(s).copied().unwrap_or(i64::MIN)) <= now - IDLE_MS)
1017        .map(|(s, _)| s)
1018        .collect();
1019    idle.sort();
1020    idle
1021}
1022
1023/// Usefulness of a lesson from its asked outcomes:
1024///
1025/// w = (1 + not_corrected) / (1 + not_corrected + 2 * corrected)
1026///
1027/// The +1 is a neutral prior, so a lesson without outcomes keeps w = 1 and a single bad session
1028/// cannot zero it. A correction of the same concern right after asking is strong evidence the
1029/// question did not help (too generic, or it does not change what the drone does), while a
1030/// session without correction is weak evidence (the engineer may simply not have looked), so a
1031/// correction counts twice. w stays in (0, 1]: outcomes only demote, BM25 relevance still decides
1032/// what is on topic. One corrected session gives 1/3; one corrected and one not gives 1/2.
1033pub fn weight(not_corrected: u32, corrected: u32) -> f64 {
1034    let (n, c) = (not_corrected as f64, corrected as f64);
1035    (1.0 + n) / (1.0 + n + f64::from(CORRECTED_W) * c)
1036}
1037
1038/// The weight of every lesson that has outcomes. Each (session, lesson) counts once; corrected wins.
1039pub fn weights(outcomes: &[Outcome]) -> HashMap<String, f64> {
1040    counts(outcomes).into_iter().map(|(l, (n, c))| (l, weight(n, c))).collect()
1041}
1042
1043/// Per lesson with results: (sessions not corrected, sessions corrected). Each (session, lesson)
1044/// counts once; corrected wins. Used by `weights`.
1045pub fn counts(outcomes: &[Outcome]) -> HashMap<String, (u32, u32)> {
1046    sessions(outcomes).into_iter().map(|(l, ss)| (l.to_string(), tally(ss.values(), i64::MIN))).collect()
1047}
1048
1049/// Per lesson, per session with a result: (corrected, ms of the session's earliest result for the
1050/// lesson). Corrected wins over a later (resumed) settle. trust.rs filters these by when an engineer
1051/// joined the lesson. A veto (`vetoed`) counts as a correction here, so trust and `weight` see it too
1052/// (as heavy as a correction; only memory.rs weighs it above one).
1053pub fn sessions(outcomes: &[Outcome]) -> HashMap<&str, HashMap<&str, (bool, i64)>> {
1054    let mut out: HashMap<&str, HashMap<&str, (bool, i64)>> = HashMap::new();
1055    // A placebo's record is no result (`is_result`).
1056    for o in outcomes.iter().filter(|o| is_result(o)) {
1057        let corrected = match o.kind.as_str() {
1058            CORRECTED | VETOED => true,
1059            NOT_CORRECTED => false,
1060            _ => continue,
1061        };
1062        for l in &o.lessons {
1063            let r = out.entry(l.as_str()).or_default().entry(o.session.as_str()).or_insert((corrected, o.ms));
1064            r.0 |= corrected;
1065            r.1 = r.1.min(o.ms);
1066        }
1067    }
1068    out
1069}
1070
1071/// (sessions not corrected, sessions corrected) among `sessions` results whose earliest result is
1072/// after `since`.
1073pub fn tally<'a>(sessions: impl IntoIterator<Item = &'a (bool, i64)>, since: i64) -> (u32, u32) {
1074    sessions.into_iter().filter(|(_, ms)| *ms > since).fold((0, 0), |(n, c), (corrected, _)| match corrected {
1075        true => (n, c + 1),
1076        false => (n + 1, c),
1077    })
1078}
1079
1080#[cfg(test)]
1081mod tests {
1082    use super::*;
1083
1084    fn o(kind: &str, session: &str, lessons: &[&str], ms: i64) -> Outcome {
1085        Outcome {
1086            kind: kind.into(),
1087            session: session.into(),
1088            lessons: lessons.iter().map(|l| l.to_string()).collect(),
1089            ms,
1090            repo: "r".into(),
1091            ..Default::default()
1092        }
1093    }
1094
1095    #[test]
1096    fn missed_is_an_applied_unasked_lesson_a_later_correction_lands_on() {
1097        let graded = |ms: i64, chosen: &[&str]| Outcome {
1098            kind: GRADED.into(),
1099            session: "s".into(),
1100            lessons: vec!["A".into(), "B".into()],
1101            ms,
1102            chosen: chosen.iter().map(|x| x.to_string()).collect(),
1103            ..Default::default()
1104        };
1105        let none: Vec<String> = Vec::new();
1106        let log = vec![graded(10, &["B"])];
1107        assert_eq!(missed(&log, "s", 20, Some("A"), &none), ["A"]);
1108        assert_eq!(missed(&log, "s", 20, None, &["A".into(), "B".into()]), ["A"], "B was chosen");
1109        assert!(missed(&log, "s", 5, Some("A"), &none).is_empty(), "a correction before the check");
1110        assert!(missed(&log, "t", 20, Some("A"), &none).is_empty(), "another session");
1111        assert!(missed(&log, "s", 20, Some("C"), &none).is_empty(), "never applied");
1112        let mut asked = log.clone();
1113        asked.push(o(ASKED, "s", &["A"], 15));
1114        assert!(missed(&asked, "s", 20, Some("A"), &none).is_empty(), "asked later in the session: not a miss");
1115        let mut free = log.clone();
1116        free.push(Outcome { channel: "queued".into(), ..o(ASKED, "s", &["A"], 15) });
1117        free.push(Outcome { channel: "early".into(), ..o(ASKED, "s", &["A"], 16) });
1118        assert_eq!(missed(&free, "s", 20, Some("A"), &none), ["A"], "handed over free, never asked: still a miss");
1119        let mut done = log.clone();
1120        done.push(o(MISSED, "s", &["A"], 18));
1121        assert!(missed(&done, "s", 20, Some("A"), &none).is_empty(), "recorded once");
1122    }
1123
1124    #[test]
1125    fn weight_formula() {
1126        assert_eq!(weight(0, 0), 1.0);
1127        assert_eq!(weight(5, 0), 1.0);
1128        assert!((weight(0, 1) - 1.0 / 3.0).abs() < 1e-12);
1129        assert_eq!(weight(1, 1), 0.5);
1130        assert!(weight(0, 3) < weight(0, 1));
1131    }
1132
1133    #[test]
1134    fn weights_count_each_session_once_and_corrected_wins() {
1135        let all = vec![
1136            o(ASKED, "s1", &["A", "B"], 10),
1137            o(CORRECTED, "s1", &["A"], 20),
1138            o(CORRECTED, "s1", &["A"], 30),     // second correction in the same session: still one
1139            o(NOT_CORRECTED, "s1", &["A"], 40), // resumed session settled later: corrected wins
1140            o(NOT_CORRECTED, "s1", &["B"], 40),
1141            o(NOT_CORRECTED, "s2", &["A"], 50),
1142        ];
1143        let w = weights(&all);
1144        assert_eq!(w["A"], weight(1, 1));
1145        assert_eq!(w["B"], 1.0);
1146        assert!(!w.contains_key("C"));
1147    }
1148
1149    #[test]
1150    fn a_session_whose_tests_go_green_after_an_ask_credits_it_once() {
1151        let mut log = vec![o(ASKED, "s", &["A", "B"], 10), o(ASKED, "t", &["A"], 10)];
1152        // Red after the ask, then green: one credit per lesson asked, dated at the pass.
1153        let got = tested(&log, "s", "fail 20\npass 30\n");
1154        assert_eq!(got.iter().map(|x| (x.kind.as_str(), x.lessons[0].as_str(), x.ms)).collect::<Vec<_>>(), [(TESTED, "A", 30), (TESTED, "B", 30)]);
1155        // Idempotent: a re-run with the records written writes nothing.
1156        log.extend(got);
1157        assert!(tested(&log, "s", "fail 20\npass 30\n").is_empty());
1158        // Only the session's own asks: t ran no tests.
1159        assert!(tested(&log, "t", "").is_empty(), "no tests file: nothing");
1160    }
1161
1162    #[test]
1163    fn a_green_run_credits_only_after_the_ask_and_with_a_reason() {
1164        let log = vec![o(ASKED, "s", &["A"], 50)];
1165        assert!(tested(&log, "s", "fail 20\npass 30\n").is_empty(), "red and green before the ask");
1166        assert!(tested(&log, "s", "fail 20\npass 60\n").is_empty(), "the failure before the ask: green proves nothing new");
1167        assert!(tested(&log, "s", "fail 60\npass 70\nfail 80\n").is_empty(), "last run red");
1168        assert!(tested(&log, "s", "pass 60\n").is_empty(), "green alone, nothing changed");
1169        assert!(tested(&log, "s", "pass 60\npass 70 \ngarbage\n").is_empty());
1170        let early = [Outcome { channel: "early".into(), ..o(ASKED, "e", &["A"], 10) }];
1171        assert!(tested(&early, "e", "fail 20\npass 30\n").is_empty(), "a free delivery asked nothing");
1172        // Green after the drone changed its work on the lesson: a credit.
1173        let changed = [log.clone(), vec![o(CHANGED, "s", &["A"], 55)]].concat();
1174        assert_eq!(tested(&changed, "s", "pass 60\n").len(), 1);
1175        // A correction or a veto of the lesson in the session outranks a green run; an ignored question
1176        // gains nothing from the drone fixing its own failing test.
1177        for k in [CORRECTED, VETOED, IGNORED] {
1178            let worse = [log.clone(), vec![o(k, "s", &["A"], 55)]].concat();
1179            assert!(tested(&worse, "s", "fail 60\npass 70\n").is_empty(), "{k}");
1180        }
1181        // Lines out of order still find the last run; the first ask decides what is after it.
1182        let twice = [log.clone(), vec![o(ASKED, "s", &["A"], 65)]].concat();
1183        assert_eq!(tested(&twice, "s", "pass 70\nfail 60\n")[0].ms, 70);
1184    }
1185
1186    #[test]
1187    fn settle_marks_only_unresolved_asks_of_this_session() {
1188        let all = vec![o(ASKED, "s1", &["B", "A"], 10), o(ASKED, "s1", &["A", "C"], 15), o(CORRECTED, "s1", &["A"], 20), o(ASKED, "s2", &["D"], 10)];
1189        let settled = settle(&all, "s1", 99);
1190        assert_eq!(settled, vec![unjudged("s1", "B", UNJUDGED_NO_CALL), unjudged("s1", "C", UNJUDGED_NO_CALL)]);
1191        assert!(asked_before(&all, "s1", "A", 11) && !asked_before(&all, "s1", "C", 15) && !asked_before(&all, "s2", "A", 99));
1192    }
1193
1194    /// An ask's settle record, as this binary writes it (`settle_with`: with its version).
1195    fn unjudged(session: &str, lesson: &str, reason: &str) -> Outcome {
1196        Outcome { reason: reason.into(), version: env!("CARGO_PKG_VERSION").into(), ..o(NOT_CORRECTED, session, &[lesson], 99) }
1197    }
1198
1199    #[test]
1200    fn a_settled_lesson_says_why_it_had_no_verdict_and_names_its_latest_check() {
1201        let ask = |lessons: &[&str], ms: i64, check: &str| Outcome { check: check.into(), ..o(ASKED, "s", lessons, ms) };
1202        let placebo = Outcome { channel: PLACEBO.into(), ..o(CHANGED, "s", &["C"], 30) };
1203        let all = vec![ask(&["A", "B", "C"], 10, "c1"), ask(&["B"], 20, "c2"), o(IGNORED, "s", &["A"], 25), placebo];
1204        let why = |l: &str| if l == "B" { UNJUDGED_TRIES } else { UNJUDGED_SLOTS };
1205        let got = settle_with(&all, "s", 99, &why);
1206        let want = |l: &str, reason: &str, check: &str| Outcome { check: check.into(), ..unjudged("s", l, reason) };
1207        assert_eq!(got, vec![want("A", UNJUDGED_NONE, "c1"), want("B", UNJUDGED_TRIES, "c2"), want("C", UNJUDGED_SLOTS, "c1")], "a placebo's verdict is none");
1208    }
1209
1210    /// A verdict on an earlier delivery of the lesson is none for its later question: handed over free,
1211    /// judged ignored by a stop that then asks it, and the session ends after the answer, so no judge
1212    /// saw the question. A verdict after the question, or on its check, is its own.
1213    #[test]
1214    fn a_verdict_on_an_earlier_delivery_leaves_the_later_question_unjudged() {
1215        let ask = |channel: &str, ms: i64, check: &str| Outcome { channel: channel.into(), check: check.into(), ..o(ASKED, "s", &["L"], ms) };
1216        let verdict = |channel: &str, ms: i64, check: &str| Outcome { channel: channel.into(), check: check.into(), ..o(IGNORED, "s", &["L"], ms) };
1217        let why = |log: &[Outcome]| settle_with(log, "s", 99, &|_| UNJUDGED_NO_CALL).into_iter().map(|x| (x.reason, x.check)).collect::<Vec<_>>();
1218        let said = |reason: &str, check: &str| vec![(reason.to_string(), check.to_string())];
1219        // The stop writes its verdict on the free delivery, then the question, in the same millisecond.
1220        let free_then_asked = vec![ask("queued", 10, "cF"), verdict("queued", 20, "cF"), ask("stop", 20, "cS")];
1221        assert_eq!(why(&free_then_asked), said(UNJUDGED_NO_CALL, "cS"));
1222        let answered = [free_then_asked, vec![verdict("stop", 30, "cS")]].concat();
1223        assert_eq!(why(&answered), said(UNJUDGED_NONE, "cS"));
1224        // A lesson another check routed, asked at the stop: its verdict names the routing check.
1225        assert_eq!(why(&[ask("stop", 20, "cS"), verdict("stop", 30, "cL")]), said(UNJUDGED_NONE, "cS"));
1226        // On the question's check, though its clock stepped back.
1227        assert_eq!(why(&[ask("stop", 20, "cS"), verdict("stop", 15, "cS")]), said(UNJUDGED_NONE, "cS"));
1228    }
1229
1230    /// An older binary's live check judged a lesson's earlier free delivery after the lesson was asked
1231    /// live and before the answer (hook.rs `pending`); no judge saw the question. The session ends: the
1232    /// free verdict is not the question's, so the question settles censored (an unjudged reason), which
1233    /// the selection summary reads as its label, instead of staying open for good. A free delivery's own
1234    /// verdict still settles it as judged.
1235    #[test]
1236    fn a_free_deliverys_verdict_after_a_later_question_leaves_the_question_unjudged() {
1237        let ask = |channel: &str, ms: i64, check: &str| Outcome { channel: channel.into(), check: check.into(), ..o(ASKED, "s", &["L"], ms) };
1238        let verdict = |channel: &str, ms: i64, check: &str| Outcome { channel: channel.into(), check: check.into(), ..o(CHANGED, "s", &["L"], ms) };
1239        let why = |log: &[Outcome]| settle_with(log, "s", 99, &|_| UNJUDGED_TRIES).into_iter().map(|x| (x.reason, x.check)).collect::<Vec<_>>();
1240        let said = |reason: &str, check: &str| vec![(reason.to_string(), check.to_string())];
1241        let stuck = vec![ask("queued", 10, "cA"), ask("live", 20, "cB"), verdict("queued", 30, "cA")];
1242        assert_eq!(why(&stuck), said(UNJUDGED_TRIES, "cB"));
1243        let early = vec![ask("early", 10, "cA"), ask("stop", 20, "cB"), verdict("early", 30, "cA")];
1244        assert_eq!(why(&early), said(UNJUDGED_TRIES, "cB"));
1245        assert_eq!(why(&[stuck.clone(), vec![verdict("live", 40, "cB")]].concat()), said(UNJUDGED_NONE, "cB"), "the question's own verdict");
1246        assert_eq!(why(&[ask("queued", 10, "cA"), verdict("queued", 30, "cA")]), said(UNJUDGED_NONE, "cA"), "a free delivery's own");
1247
1248        // The selection summary labels the stuck question censored once the session settles.
1249        use crate::select::{
1250            Route,
1251            summary::{Label, Summary, fold},
1252        };
1253        let graded = |check: &str, ms: i64, channel: &str| Outcome {
1254            grades: vec![3],
1255            ranks: vec![0],
1256            applied: 1,
1257            channel: channel.into(),
1258            check: check.into(),
1259            ..o(GRADED, "s", &["L"], ms)
1260        };
1261        let mut log = vec![Outcome { free: vec!["L".into()], ..graded("cA", 5, "live") }, Outcome { chosen: vec!["L".into()], ..graded("cB", 15, "live") }];
1262        log.extend(stuck);
1263        log.extend(settle_with(&log, "s", 99, &|_| UNJUDGED_TRIES));
1264        let text: String = log.iter().map(|x| format!("{}\n", serde_json::to_string(x).unwrap())).collect();
1265        let s = fold(&text, Summary::default());
1266        let label = |route: Route| s.applies.iter().find(|a| a.route == route).map(|a| a.label);
1267        assert_eq!(label(Route::Ask), Some(Label::Censored), "{:?}", s.applies);
1268    }
1269
1270    /// The later delivery wins the judge (hook.rs `pending`): handed over free at cA then asked at cB, or
1271    /// asked at cA then handed over free at cB, the lesson is judged on cB alone. Once the session settles
1272    /// (a settle naming its binary), the delivery the judge never got to is censored, never left open,
1273    /// whose reason says "judged".
1274    #[test]
1275    fn a_settle_leaves_no_delivery_of_its_lesson_open() {
1276        use crate::select::{
1277            Route,
1278            summary::{Label, Summary, fold},
1279        };
1280        let graded = |check: &str, ms: i64, ask: bool| Outcome {
1281            grades: vec![2],
1282            ranks: vec![0],
1283            applied: 1,
1284            channel: "live".into(),
1285            check: check.into(),
1286            chosen: if ask { vec!["L".into()] } else { Vec::new() },
1287            free: if ask { Vec::new() } else { vec!["L".into()] },
1288            ..o(GRADED, "s", &["L"], ms)
1289        };
1290        let asked = |channel: &str, ms: i64, check: &str| Outcome { channel: channel.into(), check: check.into(), ..o(ASKED, "s", &["L"], ms) };
1291        let verdict = |channel: &str, check: &str| Outcome { channel: channel.into(), check: check.into(), ..o(CHANGED, "s", &["L"], 40) };
1292        for (first_ask, channels) in [(false, ["queued", "live"]), (true, ["live", "queued"])] {
1293            let mut log = vec![graded("cA", 5, first_ask), asked(channels[0], 10, "cA"), graded("cB", 15, !first_ask), asked(channels[1], 20, "cB")];
1294            log.push(verdict(channels[1], "cB"));
1295            let settled = settle_with(&log, "s", 99, &|_| UNJUDGED_TRIES);
1296            assert_eq!(settled.iter().map(|x| (x.reason.as_str(), x.check.as_str())).collect::<Vec<_>>(), [(UNJUDGED_NONE, "cB")]);
1297            log.extend(settled);
1298            let text: String = log.iter().map(|x| format!("{}\n", serde_json::to_string(x).unwrap())).collect();
1299            let s = fold(&text, Summary::default());
1300            let label = |route: Route| s.applies.iter().find(|a| a.route == route).map(|a| a.label);
1301            let (a, b) = if first_ask { (Route::Ask, Route::Free) } else { (Route::Free, Route::Ask) };
1302            assert_eq!((label(a), label(b)), (Some(Label::Censored), Some(Label::Changed)), "first asked {first_ask}: {:?}", s.applies);
1303        }
1304        // An older binary judged the free delivery while the question waited, and its settle (no
1305        // version) said "judged" for the question: it folds as when its hooks decided on it, the
1306        // question open, so the replay still reproduces those decisions.
1307        let mut log = vec![graded("cA", 5, false), asked("queued", 10, "cA"), graded("cB", 15, true), asked("live", 20, "cB"), verdict("queued", "cA")];
1308        log.push(Outcome { reason: UNJUDGED_NONE.into(), check: "cB".into(), ..o(NOT_CORRECTED, "s", &["L"], 99) });
1309        let text: String = log.iter().map(|x| format!("{}\n", serde_json::to_string(x).unwrap())).collect();
1310        let s = fold(&text, Summary::default());
1311        let labels: Vec<(Route, Label)> = s.applies.iter().map(|a| (a.route, a.label)).collect();
1312        assert_eq!(labels, [(Route::Free, Label::Changed), (Route::Ask, Label::Open)]);
1313    }
1314
1315    fn placebo(lessons: &[&str], check: &str, ms: i64) -> Outcome {
1316        Outcome { channel: PLACEBO.into(), check: check.into(), route: ROUTE_PLACEBO.into(), ..o(DELIVERY, "s", lessons, ms) }
1317    }
1318
1319    /// A placebo writes no `asked`, only its `delivery`: one the judge gave no verdict settles too, on the
1320    /// placebo channel (no result, and no ask's end), with its own check and reason. Delivered again
1321    /// later, the later delivery took its place in the judge's queue.
1322    #[test]
1323    fn an_unjudged_placebo_settles_on_its_own_channel() {
1324        let on = |kind: &str, lesson: &str, check: &str, ms: i64| Outcome { channel: PLACEBO.into(), check: check.into(), ..o(kind, "s", &[lesson], ms) };
1325        let log = vec![
1326            placebo(&["A", "B"], "p1", 10),
1327            on(IGNORED, "A", "p1", 15),
1328            on(UNCLEAR, "B", "p1", 15),
1329            placebo(&["C"], "p2", 20),
1330            Outcome { channel: "queued".into(), check: "c3".into(), ..o(ASKED, "s", &["C"], 30) },
1331            Outcome { route: ROUTE_DROPPED_DONE.into(), ..placebo(&["D"], "p4", 40) },
1332        ];
1333        let why = |l: &str| if l == "B" { UNJUDGED_TRIES } else { UNJUDGED_NO_CALL };
1334        let got = settle_with(&log, "s", 99, &why);
1335        let closed =
1336            |l: &str, check: &str, reason: &str| Outcome { channel: PLACEBO.into(), check: check.into(), version: String::new(), ..unjudged("s", l, reason) };
1337        let asked = Outcome { check: "c3".into(), ..unjudged("s", "C", UNJUDGED_NO_CALL) };
1338        let want = vec![asked, closed("B", "p1", UNJUDGED_TRIES), closed("C", "p2", UNJUDGED_LATER)];
1339        assert_eq!(got, want, "A has its verdict; D never reached the judge's queue");
1340        assert!(got[1..].iter().all(|x| !is_result(x) && share(x, "n1").is_none()), "no result, never shared");
1341        // Settled once: the same records close their placebos, so settling again writes nothing.
1342        let mut resumed = [log, got].concat();
1343        assert!(settle_with(&resumed, "s", 120, &why).is_empty());
1344        // Resumed: B asked, E a new placebo. B's placebo settle ended no ask's wait.
1345        resumed.extend([Outcome { channel: "stop".into(), check: "c5".into(), ..o(ASKED, "s", &["B"], 110) }, placebo(&["E"], "p6", 111)]);
1346        let again: Vec<(String, String, String)> =
1347            settle_with(&resumed, "s", 120, &why).into_iter().map(|x| (x.lessons.join(","), x.channel, x.check)).collect();
1348        assert_eq!(again, [("B".to_string(), String::new(), "c5".to_string()), ("E".to_string(), PLACEBO.to_string(), "p6".to_string())]);
1349    }
1350
1351    /// An open placebo keeps its session waiting to settle when its `end` hook never ran, as an open ask
1352    /// does; its settle closes it.
1353    #[test]
1354    fn idle_sessions_wait_for_open_placebos() {
1355        let only = vec![placebo(&["A"], "p1", 0)];
1356        assert_eq!(idle_sessions(&only, &HashMap::new(), IDLE_MS + 10), ["s"]);
1357        assert!(idle_sessions(&only, &HashMap::new(), IDLE_MS - 10).is_empty(), "the placebo is the session's newest activity");
1358        let settled = [only.clone(), settle(&only, "s", 5)].concat();
1359        assert!(idle_sessions(&settled, &HashMap::new(), IDLE_MS + 10).is_empty());
1360    }
1361
1362    /// The selection summary reads a placebo's settle as its censoring (select/summary.rs `settled`), so
1363    /// the judge-missingness fit (select/policy.rs `judged`) counts the placebo as not judged instead of
1364    /// leaving it out.
1365    #[test]
1366    fn the_selection_summary_censors_a_settled_placebo() {
1367        use crate::select::summary::{Label, Summary, fold};
1368        let graded = Outcome {
1369            grades: vec![1, 1],
1370            ranks: vec![0, 1],
1371            applied: 2,
1372            channel: "stop".into(),
1373            check: "p1".into(),
1374            policy: "learned".into(),
1375            placebo: vec!["A".into(), "B".into()],
1376            ..o(GRADED, "s", &["A", "B"], 5)
1377        };
1378        let mut log = vec![graded, placebo(&["A", "B"], "p1", 10), Outcome { channel: PLACEBO.into(), check: "p1".into(), ..o(IGNORED, "s", &["A"], 15) }];
1379        log.extend(settle(&log, "s", 99));
1380        let text: String = log.iter().map(|x| format!("{}\n", serde_json::to_string(x).unwrap())).collect();
1381        let s = fold(&text, Summary::default());
1382        let label = |l: &str| s.applies.iter().find(|a| s.lessons[a.lesson as usize] == l).map(|a| a.label);
1383        assert_eq!((label("A"), label("B")), (Some(Label::Ignored), Some(Label::Censored)));
1384    }
1385
1386    #[test]
1387    fn contradicted_marks_superseded_asks_and_the_other_sides_of_a_disagreement() {
1388        let mut ask = o(ASKED, "s1", &["A", "B", "C", "D"], 10);
1389        ask.groups = vec![vec!["A".into(), "B".into()]];
1390        let all = vec![ask, o(ASKED, "s1", &["E"], 30), o(CORRECTED, "s1", &["D"], 15)];
1391        let ids = |v: &[&str]| v.iter().map(|x| x.to_string()).collect::<Vec<_>>();
1392        // A plain merge into C: C was asked in vain.
1393        assert_eq!(contradicted(&all, "s1", 20, Some("C"), &[]), ["C"]);
1394        // A correction that sides with A in the rendered A/B disagreement: B was wrong, A was right.
1395        assert_eq!(contradicted(&all, "s1", 20, Some("A"), &[]), ["B"]);
1396        // A new lesson superseding C and E: only C was asked before it; D is already corrected.
1397        assert_eq!(contradicted(&all, "s1", 20, None, &ids(&["C", "E", "D", "Z"])), ["C"]);
1398        assert!(contradicted(&all, "s2", 20, Some("C"), &ids(&["A"])).is_empty(), "another session asked nothing");
1399        let mut vetoed = all.clone();
1400        vetoed.push(o(VETOED, "s1", &["C"], 12));
1401        assert!(contradicted(&vetoed, "s1", 20, Some("C"), &[]).is_empty(), "a vetoed lesson already has the stronger result");
1402    }
1403
1404    #[test]
1405    fn a_placebo_or_unclear_result_is_no_result_for_any_reader() {
1406        let placebo = |kind: &str| Outcome { channel: PLACEBO.into(), ..o(kind, "s", &["A"], 5) };
1407        let unclear = Outcome { channel: "stop".into(), ..o(UNCLEAR, "s", &["A"], 5) };
1408        for x in [placebo(CHANGED), placebo(IGNORED), placebo(UNCLEAR), unclear.clone()] {
1409            assert!(!is_result(&x), "{x:?}");
1410            assert!(share(&x, "n1").is_none(), "never shared: {x:?}");
1411        }
1412        assert!(is_result(&Outcome { channel: "queued".into(), ..o(CHANGED, "s", &["A"], 5) }));
1413        assert!(!SHARED_KINDS.contains(&UNCLEAR) && !SHARED_KINDS.contains(&DELIVERY));
1414        // trust's view (`sessions`): a placebo result moves nothing, whatever its kind.
1415        let all = [placebo(CORRECTED), placebo(NOT_CORRECTED), o(NOT_CORRECTED, "t", &["A"], 6)];
1416        assert_eq!(tally(sessions(&all)["A"].values(), i64::MIN), (1, 0));
1417        // The statistics count no placebo result (`append`).
1418        let d = tmp("placebo-stats");
1419        append(&d.join("outcomes.jsonl"), &placebo(CHANGED)).unwrap();
1420        append(&d.join("outcomes.jsonl"), &unclear).unwrap();
1421        append(&d.join("outcomes.jsonl"), &Outcome { channel: "stop".into(), ..o(CHANGED, "s", &["B"], 7) }).unwrap();
1422        let kinds: Vec<String> = crate::stats::events(&d).into_iter().map(|e| e.kind).collect();
1423        assert_eq!(kinds, [crate::stats::CHANGED], "{kinds:?}");
1424        let _ = std::fs::remove_dir_all(d);
1425    }
1426
1427    #[test]
1428    fn a_vetoed_lesson_is_not_settled_and_counts_as_corrected() {
1429        let all = vec![o(ASKED, "s1", &["A", "B"], 10), o(VETOED, "s1", &["A"], 20)];
1430        assert_eq!(settle(&all, "s1", 99), vec![unjudged("s1", "B", UNJUDGED_NO_CALL)]);
1431        assert_eq!(sessions(&all)["A"]["s1"], (true, 20));
1432        assert_eq!(weights(&all)["A"], weight(0, 1));
1433    }
1434
1435    #[test]
1436    fn sessions_keep_the_earliest_result_and_tally_filters_by_it() {
1437        let all =
1438            vec![o(NOT_CORRECTED, "s1", &["A"], 10), o(CORRECTED, "s2", &["A"], 20), o(NOT_CORRECTED, "s2", &["A"], 25), o(NOT_CORRECTED, "s3", &["A"], 30)];
1439        let s = sessions(&all);
1440        assert_eq!(s["A"]["s2"], (true, 20));
1441        assert_eq!(tally(s["A"].values(), i64::MIN), (2, 1));
1442        assert_eq!(tally(s["A"].values(), 20), (1, 0));
1443    }
1444
1445    #[test]
1446    fn idle_sessions_need_old_asks_and_old_episodes() {
1447        let all = vec![o(ASKED, "old", &["A"], 0), o(ASKED, "busy", &["A"], 0), o(ASKED, "fresh", &["A"], 50), o(CORRECTED, "x", &["A"], 0)];
1448        let episodes = HashMap::from([("busy".to_string(), 60)]);
1449        assert_eq!(idle_sessions(&all, &episodes, IDLE_MS + 10), ["old"]);
1450        assert_eq!(idle_sessions(&all, &episodes, IDLE_MS + 60), ["busy", "fresh", "old"]);
1451    }
1452
1453    #[test]
1454    fn idle_sessions_skip_settled_ones() {
1455        let all = vec![
1456            o(ASKED, "done", &["A", "B"], 0),
1457            o(NOT_CORRECTED, "done", &["A"], 1),
1458            o(CORRECTED, "done", &["B"], 1),
1459            o(ASKED, "vetoed", &["A"], 0),
1460            o(VETOED, "vetoed", &["A"], 1),
1461            o(ASKED, "half", &["A", "B"], 0),
1462            o(NOT_CORRECTED, "half", &["A"], 1),
1463        ];
1464        assert_eq!(idle_sessions(&all, &HashMap::new(), IDLE_MS + 10), ["half"], "every ask of the others has its result");
1465    }
1466
1467    #[test]
1468    fn regretted_blames_asks_before_the_decision_shared_or_by_target() {
1469        let mut graded = o(GRADED, "s", &["G"], 5);
1470        graded.chosen = vec![];
1471        let log = vec![graded, o(ASKED, "s", &["B", "A"], 10), o(ASKED, "s", &["C"], 30), o(ASKED, "t", &["D"], 10)];
1472        let ids = |v: &[&str]| v.iter().map(|x| x.to_string()).collect::<Vec<_>>();
1473        let blame = |log: &[Outcome], target: &[String]| regretted(log, "s", 20, "R1", target, 0.5, 99, "r");
1474        let got = blame(&log, &[]);
1475        let want = |l: &str, credit: f64| Outcome { cause: "R1".into(), credit, ..o(REGRETTED, "s", &[l], 99) };
1476        assert_eq!(got, vec![want("A", 0.25), want("B", 0.25)], "shared; C was asked after the decision, G never shown");
1477        assert_eq!(blame(&log, &ids(&["B", "Z"])), vec![want("B", 0.5)], "the merge target takes it all");
1478        assert_eq!(blame(&log, &ids(&["Z"])), got, "a target not asked: shared");
1479        let mut corrected = log.clone();
1480        corrected.push(o(CORRECTED, "s", &["B"], 15));
1481        assert_eq!(blame(&corrected, &[]), vec![want("A", 0.5)], "corrected outranks regretted");
1482        let mut vetoed = log.clone();
1483        vetoed.push(o(VETOED, "s", &["B"], 15));
1484        assert_eq!(blame(&vetoed, &[]), vec![want("A", 0.5)], "vetoed outranks regretted");
1485        assert!(blame(&corrected, &ids(&["B"])).is_empty(), "the target took it as a correction: nobody else is blamed");
1486        assert!(blame(&vetoed, &ids(&["B", "Z"])).is_empty(), "the target took it as a veto: nobody else is blamed");
1487        assert_eq!(blame(&corrected, &ids(&["A", "B"])), vec![want("A", 0.5)], "the uncorrected target keeps its share");
1488        let mut again = log.clone();
1489        again.extend(got.clone());
1490        assert!(blame(&again, &[]).is_empty(), "credited once per cause");
1491        let mut half = log.clone();
1492        half.push(got[0].clone());
1493        assert_eq!(blame(&half, &[]), vec![want("B", 0.25)], "a partial run completes with the same share");
1494        assert_eq!(regretted(&again, "s", 20, "R2", &[], 0.5, 99, "r").len(), 2, "another regret blames again");
1495        assert!(regretted(&log, "u", 20, "R1", &[], 0.5, 99, "r").is_empty(), "another session asked nothing");
1496        assert!(regretted(&log, "", 20, "R1", &[], 0.5, 99, "r").is_empty());
1497        assert!(regretted(&log, "s", 0, "R1", &[], 0.5, 99, "r").is_empty(), "no decision time");
1498        assert!(regretted(&log, "s", 20, "R1", &[], 0.0, 99, "r").is_empty(), "not strong");
1499        // An old line without cause and credit still reads; a regretted line round-trips.
1500        let old: Outcome = serde_json::from_str(r#"{"kind":"asked","session":"s","lessons":["A"],"ms":1,"repo":"r"}"#).unwrap();
1501        assert_eq!(old, o(ASKED, "s", &["A"], 1));
1502        assert!(!serde_json::to_string(&old).unwrap().contains("credit"));
1503        let line = serde_json::to_string(&got[0]).unwrap();
1504        assert!(line.contains(r#""cause":"R1","credit":0.25"#), "{line}");
1505        assert_eq!(serde_json::from_str::<Outcome>(&line).unwrap(), got[0]);
1506    }
1507
1508    #[test]
1509    fn a_line_from_before_propensities_reads_and_writes_back_unchanged() {
1510        let line = r#"{"kind":"asked","session":"s","lessons":["A","B"],"ms":1,"repo":"r","channel":"stop","grades":[3,1],"ranks":[0,1],"applied":2}"#;
1511        let o: Outcome = serde_json::from_str(line).unwrap();
1512        assert!(o.p.is_empty());
1513        assert_eq!(serde_json::to_string(&o).unwrap(), line);
1514        let with = Outcome { p: vec![round_p(1.0), round_p(1.0 / 3.0)], ..o };
1515        assert!(serde_json::to_string(&with).unwrap().ends_with(r#""applied":2,"p":[1.0,0.3333]}"#));
1516    }
1517
1518    #[test]
1519    fn a_graded_line_from_before_select_reads_and_writes_back_unchanged() {
1520        let line = r#"{"kind":"graded","session":"s","lessons":["A","B"],"ms":1,"repo":"r","channel":"live","grades":[3,1],"ranks":[0,1],"applied":2,"chosen":["A","B"],"explore":true,"p":[1.0,0.1]}"#;
1521        let o: Outcome = serde_json::from_str(line).unwrap();
1522        assert!(o.check.is_empty() && o.policy.is_empty() && o.free.is_empty() && o.pf.is_empty() && o.judge == 0 && !o.failed);
1523        assert!(o.nofree.is_empty() && o.tail.is_empty() && o.pt == 0.0 && o.rel.is_empty());
1524        assert_eq!(serde_json::to_string(&o).unwrap(), line);
1525        // A 0.6.0 graded line and an early delivery's from before `nofree`, `tail`, `pt` and `rel`.
1526        for line in [
1527            r#"{"kind":"graded","session":"s","lessons":["A","B","C"],"ms":1,"repo":"r","channel":"stop","grades":[2,1,1],"ranks":[0,1,2],"applied":3,"p":[0.0,0.0,0.0],"check":"0123456789ab","policy":"learned","fit":"120-ab12","seen":"9-abcdef01","source":"fixed","free":["A","B"],"pf":[1.0,1.0,0.0],"placebo":["C"],"psi":1.0,"eligible":40,"shown":30,"tokens":12000,"fill":[0,4,7],"model":"m","version":"0.6.0","dur_ms":9000,"usd":0.01}"#,
1528            r#"{"kind":"asked","session":"s","lessons":["A","B"],"ms":2,"repo":"r","channel":"early","p":[1.0,0.5],"route":"asked"}"#,
1529        ] {
1530            let o: Outcome = serde_json::from_str(line).unwrap();
1531            assert!(o.nofree.is_empty() && o.tail.is_empty() && o.pt == 0.0 && o.rel.is_empty(), "{line}");
1532            assert_eq!((o.xk, o.xs), (None, None), "{line}");
1533            assert_eq!(serde_json::to_string(&o).unwrap(), line);
1534        }
1535    }
1536
1537    #[test]
1538    fn the_selection_fields_round_trip_and_stay_local() {
1539        let g = Outcome {
1540            check: "0123456789ab".into(),
1541            policy: "learned".into(),
1542            fit: "120-ab12".into(),
1543            free: vec!["B".into()],
1544            pf: vec![0.0, 1.0, 0.0],
1545            placebo: vec!["C".into()],
1546            psi: 0.5,
1547            nofree: vec!["N9".into()],
1548            xk: Some(2),
1549            xs: Some(0),
1550            tail: vec!["T9".into()],
1551            pt: 0.125,
1552            eligible: 40,
1553            shown: 30,
1554            tokens: 12_000,
1555            fill: vec![0, 4, 7],
1556            model: "m".into(),
1557            prompt_hash: "ph".into(),
1558            harness: "claude".into(),
1559            version: "0.6.0".into(),
1560            dur_ms: 9_000,
1561            failed: true,
1562            ..o(GRADED, "s", &["A", "B", "C"], 5)
1563        };
1564        let delivered = Outcome { check: "0123456789ab".into(), route: ROUTE_DROPPED_CHARS.into(), reason: "budget".into(), ..o(ASKED, "s", &["B"], 6) };
1565        let judged = Outcome { check: "0123456789ab".into(), judge: 2, stripped: true, ..o(CHANGED, "s", &["A"], 7) };
1566        let early = Outcome { channel: "early".into(), rel: vec![0.75], ..o(ASKED, "s", &["D"], 8) };
1567        // Shareable kinds carrying every per-check field, so `share` is put to the test on each.
1568        let carried = Outcome { nofree: g.nofree.clone(), xk: g.xk, xs: g.xs, tail: g.tail.clone(), pt: g.pt, rel: vec![0.75], ..judged.clone() };
1569        for x in [&g, &delivered, &judged, &early, &carried] {
1570            let line = serde_json::to_string(x).unwrap();
1571            assert_eq!(&serde_json::from_str::<Outcome>(&line).unwrap(), x, "round trip");
1572        }
1573        // The test's s = 0 is logged, not skipped: it is a draw, not an absent field.
1574        assert!(serde_json::to_string(&g).unwrap().contains(r#""xk":2,"xs":0"#));
1575        assert!(share(&g, "n1").is_none(), "graded stays local");
1576        for x in [&delivered, &judged, &early, &carried] {
1577            let shared = serde_json::to_string(&share(x, "n1").unwrap()).unwrap();
1578            for field in
1579                ["check", "0123456789ab", "route", "reason", "judge", "stripped", "nofree", "N9", "xk", "xs", "tail", "T9", "pt", "0.125", "rel", "0.75"]
1580            {
1581                assert!(!shared.contains(field), "{field}: {shared}");
1582            }
1583        }
1584        assert!(ROUTES.iter().all(|r| !r.is_empty() && !r.contains(' ')), "one word each");
1585    }
1586
1587    #[test]
1588    fn an_older_graded_record_is_the_fixed_rule_unless_it_shows_the_cut() {
1589        let graded = |grades: &[u8], chosen: &[&str], p: &[f64]| Outcome {
1590            lessons: (0..grades.len()).map(|i| format!("L{i:02}")).collect(),
1591            grades: grades.to_vec(),
1592            chosen: chosen.iter().map(|c| c.to_string()).collect(),
1593            p: p.to_vec(),
1594            ..o(GRADED, "s", &[], 1)
1595        };
1596        // No 3: the fixed rule hands over the 2s, then the 1s, at most CAP.
1597        let soft: Vec<u8> = [vec![1; 6], vec![2; 6]].concat();
1598        let old = graded(&soft, &[], &[0.0; 12]);
1599        assert_eq!(policy_of(&old), "fixed");
1600        let want: Vec<String> = (6..12).chain(0..4).map(|i| format!("L{i:02}")).collect();
1601        assert_eq!(free_of(&old), want);
1602        // A 3: the fixed rule asks, hands over nothing.
1603        let block = graded(&[3, 1], &["L00", "L01"], &[1.0, 1.0]);
1604        assert_eq!((policy_of(&block), free_of(&block)), ("fixed", Vec::<String>::new()));
1605        // The learned cut: a propensity between 0 and 1, or an explored check; the rest go free.
1606        let cut = graded(&[3, 1, 1, 1], &["L00", "L01"], &[1.0, 1.0, 0.1, 0.1]);
1607        assert_eq!((policy_of(&cut), free_of(&cut)), ("attention", vec!["L02".to_string(), "L03".into()]));
1608        let explored = Outcome { explore: true, ..graded(&[3, 1, 1], &["L00", "L01", "L02"], &[1.0, 1.0, 1.0]) };
1609        assert_eq!((policy_of(&explored), free_of(&explored)), ("attention", Vec::<String>::new()));
1610        // A record that names its policy is taken at its word, its free list as logged.
1611        let named = Outcome { policy: "learned".into(), free: vec!["L01".into()], ..graded(&[3, 1], &["L00"], &[1.0, 0.0]) };
1612        assert_eq!((policy_of(&named), free_of(&named)), ("learned", vec!["L01".to_string()]));
1613    }
1614
1615    fn tmp(name: &str) -> std::path::PathBuf {
1616        let d = std::env::temp_dir().join(format!("borg-outcome-{name}-{}", std::process::id()));
1617        let _ = std::fs::remove_dir_all(&d);
1618        std::fs::create_dir_all(&d).unwrap();
1619        d
1620    }
1621
1622    fn lines(path: &Path, v: &[String]) {
1623        std::fs::write(path, v.iter().map(|l| format!("{l}\n")).collect::<String>()).unwrap();
1624    }
1625
1626    fn changed(team: &[Outcome], lesson: &str) -> u32 {
1627        let ls = [crate::conscience::Challenge { id: lesson.into(), ..Default::default() }];
1628        crate::memory::derive(team, &ls, None)[lesson].changed
1629    }
1630
1631    #[test]
1632    fn share_redacts() {
1633        let mut a = o(ASKED, "session-raw", &["A"], 5);
1634        a.repo = "secret-repo".into();
1635        a.groups = vec![vec!["A".into(), "B".into()]];
1636        a.grades = vec![3];
1637        a.ranks = vec![0];
1638        a.p = vec![0.5];
1639        let x = share(&a, "n1").unwrap();
1640        let line = serde_json::to_string(&x).unwrap();
1641        for raw in ["session-raw", "secret-repo", "groups", "grades", "ranks", "\"id\"", "version"] {
1642            assert!(!line.contains(raw), "{raw} in {line}");
1643        }
1644        assert_eq!((x.s, x.r, x.p, x.node), (tag("session-raw"), tag("secret-repo"), vec![0.5], "n1".to_string()));
1645        assert_eq!(tag("s").len(), 32);
1646        for kind in [GRADED, "veto"] {
1647            assert!(share(&o(kind, "s", &["A"], 1), "n1").is_none(), "{kind} is not shared");
1648        }
1649        let regret = share(&Outcome { cause: "episode-raw".into(), credit: 0.25, ..o(REGRETTED, "s", &["A"], 1) }, "n1").unwrap();
1650        let line = serde_json::to_string(&regret).unwrap();
1651        assert!(!line.contains("episode-raw") && line.contains(&tag("episode-raw")) && line.contains(r#""credit":0.25"#), "{line}");
1652    }
1653
1654    #[test]
1655    fn pool_fields_stay_local() {
1656        let a = Outcome { pool: vec!["A".into(), "B".into()], base: vec![1.25, 0.5], scoped: vec![true, false], p: vec![0.5], ..o(ASKED, "s", &["A"], 5) };
1657        let line = serde_json::to_string(&a).unwrap();
1658        assert_eq!(serde_json::from_str::<Outcome>(&line).unwrap(), a, "round trip");
1659        let shared = serde_json::to_string(&share(&a, "n1").unwrap()).unwrap();
1660        for field in ["pool", "base", "scoped"] {
1661            assert!(line.contains(field) && !shared.contains(field), "{field}: {shared}");
1662        }
1663        assert!(!serde_json::to_string(&o(ASKED, "s", &["A"], 5)).unwrap().contains("pool"), "empty: not written");
1664    }
1665
1666    #[test]
1667    fn publish_is_idempotent() {
1668        let home = tmp("publish");
1669        assert_eq!(publish(&home), 0, "no outcomes, nothing to publish");
1670        let mut asked = o(ASKED, "s1", &["A"], 1);
1671        asked.p = vec![1.0];
1672        lines(&home.join("outcomes.jsonl"), &[asked, o(GRADED, "s1", &["A"], 1), o(CHANGED, "s1", &["A"], 2)].map(|x| serde_json::to_string(&x).unwrap()));
1673        assert_eq!(publish(&home), 2, "graded stays local");
1674        let first = std::fs::read_to_string(home.join(TEAM_FILE)).unwrap();
1675        assert_eq!(publish(&home), 0);
1676        assert_eq!(std::fs::read_to_string(home.join(TEAM_FILE)).unwrap(), first);
1677        assert!(first.contains(r#""p":[1.0]"#) && !first.contains("\"s1\""), "{first}");
1678        // Unchanged since the last publish: the log is not read again; grown: only the new line goes.
1679        assert_eq!(std::fs::read_to_string(home.join(PUBLISHED)).unwrap(), std::fs::metadata(home.join("outcomes.jsonl")).unwrap().len().to_string());
1680        std::fs::OpenOptions::new()
1681            .append(true)
1682            .open(home.join("outcomes.jsonl"))
1683            .and_then(|mut f| std::io::Write::write_all(&mut f, format!("{}\n", serde_json::to_string(&o(IGNORED, "s2", &["A"], 3)).unwrap()).as_bytes()))
1684            .unwrap();
1685        assert_eq!(publish(&home), 1);
1686        assert_eq!(publish(&home), 0);
1687        // A team file gone (a fresh mesh) is published again in full.
1688        std::fs::remove_file(home.join(TEAM_FILE)).unwrap();
1689        assert_eq!(publish(&home), 3);
1690        let _ = std::fs::remove_dir_all(home);
1691    }
1692
1693    /// A store whose local log has s1 changed L1, and whose team file holds that result as published
1694    /// by this node, a remote session's change of L1, the same remote line from another node's copy,
1695    /// and a kind this binary does not share.
1696    fn team_store(name: &str, shuffle: bool) -> std::path::PathBuf {
1697        let home = tmp(name);
1698        let mut local = o(CHANGED, "s1", &["L1"], 10);
1699        local.repo = "app".into();
1700        lines(&home.join("outcomes.jsonl"), &[serde_json::to_string(&local).unwrap()]);
1701        let remote = |kind: &str, s: &str, node: &str| {
1702            serde_json::to_string(&Shared {
1703                kind: kind.into(),
1704                s: s.into(),
1705                lessons: vec!["L1".into()],
1706                ms: 20,
1707                node: node.into(),
1708                r: tag("web"),
1709                ..Default::default()
1710            })
1711            .unwrap()
1712        };
1713        let mut team = vec![
1714            serde_json::to_string(&share(&local, "me").unwrap()).unwrap(),
1715            remote(CHANGED, "x", "n2"),
1716            remote(CHANGED, "x", "n3"),
1717            remote("hacked", "y", "n2"),
1718            remote("veto", "z", "n2"),
1719        ];
1720        if shuffle {
1721            team.reverse();
1722            team.swap(0, 2);
1723        }
1724        lines(&home.join(TEAM_FILE), &team);
1725        home
1726    }
1727
1728    #[test]
1729    fn read_team_drops_own_copies_and_keeps_remote() {
1730        let home = team_store("team", false);
1731        let team = team_view(&home.join("lessons.jsonl"), true);
1732        assert_eq!(changed(&team, "L1"), 2, "{team:?}");
1733        assert_eq!(team.iter().filter(|o| o.session == "x").count(), 1, "one remote line per (kind, session, lessons, ms)");
1734        assert!(team.iter().all(|o| o.kind != "hacked" && o.kind != "veto"), "unshared kinds, an older peer's hive verdict among them, are ignored");
1735        assert!(team.iter().all(|o| o.repo != "app"), "local repos compare as tags");
1736        let _ = std::fs::remove_dir_all(home);
1737    }
1738
1739    #[test]
1740    fn read_team_is_order_independent() {
1741        let (a, b) = (team_store("order-a", false), team_store("order-b", true));
1742        let ls = [crate::conscience::Challenge { id: "L1".into(), ..Default::default() }];
1743        let m = |h: &Path| crate::memory::derive(&team_view(&h.join("lessons.jsonl"), true), &ls, None);
1744        assert_eq!(m(&a), m(&b));
1745        let _ = std::fs::remove_dir_all(a);
1746        let _ = std::fs::remove_dir_all(b);
1747    }
1748
1749    /// A node's whole fold reaches its peers: a veto that outranks the session's change, a regret's
1750    /// credit, and a session that only graded (no tick on either node). A peer holding only the team file
1751    /// derives the same memory as the node that recorded it.
1752    #[test]
1753    fn a_peer_derives_the_same_memory_from_the_team_file() {
1754        let (a, b) = (tmp("fold-a"), tmp("fold-b"));
1755        let regret = Outcome { cause: "episode-1".into(), credit: 0.5, ..o(REGRETTED, "s2", &["L1"], 40) };
1756        let log = [
1757            o(GRADED, "s0", &["L1"], 5),
1758            o(ASKED, "s1", &["L1"], 10),
1759            o(CHANGED, "s1", &["L1"], 20),
1760            o(VETOED, "s1", &["L1"], 30),
1761            o(ASKED, "s2", &["L1"], 35),
1762            regret,
1763        ];
1764        lines(&a.join("outcomes.jsonl"), &log.map(|x| serde_json::to_string(&x).unwrap()));
1765        publish(&a);
1766        std::fs::copy(a.join(TEAM_FILE), b.join(TEAM_FILE)).unwrap();
1767        let ls = [crate::conscience::Challenge { id: "L1".into(), origin_ms: 1, ..Default::default() }];
1768        let m = |h: &Path| crate::memory::derive(&team_view(&h.join("lessons.jsonl"), true), &ls, None);
1769        let (here, there) = (m(&a), m(&b));
1770        assert_eq!((here["L1"].vetoed, here["L1"].changed, here["L1"].regretted), (1, 0, 0.5), "{here:?}");
1771        assert_eq!(here, there);
1772        let _ = std::fs::remove_dir_all(a);
1773        let _ = std::fs::remove_dir_all(b);
1774    }
1775
1776    /// The unguarded surface `SHARED_KINDS` documents, pinned until the deferred guards land: a remote
1777    /// line under one of this node's session tags is dropped here, whatever its kind, while a peer
1778    /// without that session takes it, and the stronger result wins there.
1779    #[test]
1780    fn a_remote_line_under_a_local_tag_counts_on_peers_only() {
1781        let (victim, peer) = (tmp("forged-victim"), tmp("forged-peer"));
1782        let own = o(CHANGED, "s1", &["L1"], 10);
1783        let forged = Shared { kind: CORRECTED.into(), s: tag("s1"), lessons: vec!["L1".into()], ms: 11, node: "n2".into(), ..Default::default() };
1784        let team = [serde_json::to_string(&share(&own, "me").unwrap()).unwrap(), serde_json::to_string(&forged).unwrap()];
1785        lines(&victim.join("outcomes.jsonl"), &[serde_json::to_string(&own).unwrap()]);
1786        lines(&victim.join(TEAM_FILE), &team);
1787        lines(&peer.join(TEAM_FILE), &team);
1788        let ls = [crate::conscience::Challenge { id: "L1".into(), ..Default::default() }];
1789        let m = |h: &Path| crate::memory::derive(&team_view(&h.join("lessons.jsonl"), true), &ls, None).remove("L1").unwrap();
1790        assert_eq!((m(&victim).changed, m(&victim).corrected), (1, 0), "the victim never sees the override");
1791        assert_eq!((m(&peer).changed, m(&peer).corrected), (0, 1), "a peer counts the forged correction");
1792        let _ = std::fs::remove_dir_all(victim);
1793        let _ = std::fs::remove_dir_all(peer);
1794    }
1795
1796    /// A peer's `tested` line counts for a lesson its session's shared records name, and for no other.
1797    #[test]
1798    fn a_peer_green_run_counts_only_for_lessons_its_session_was_given() {
1799        let home = tmp("tested-team");
1800        let asked = share(&o(ASKED, "p1", &["L1"], 10), "peer").unwrap();
1801        let ok = share(&o(TESTED, "p1", &["L1"], 20), "peer").unwrap();
1802        let stray = share(&o(TESTED, "p1", &["L2"], 20), "peer").unwrap();
1803        let alone = share(&o(TESTED, "p2", &["L1"], 20), "peer").unwrap();
1804        // A 0.3.0 peer's hive verdict is not shared any more, so it gives its session nothing.
1805        let verdict = Shared { kind: "verified".into(), ..share(&o(ASKED, "p3", &["L1"], 10), "peer").unwrap() };
1806        let unbacked = share(&o(TESTED, "p3", &["L1"], 20), "peer").unwrap();
1807        let all = [&asked, &ok, &stray, &alone, &verdict, &unbacked];
1808        lines(&home.join(TEAM_FILE), &all.map(|x| serde_json::to_string(x).unwrap()));
1809        let ls = ["L1", "L2"].map(|id| crate::conscience::Challenge { id: id.into(), ..Default::default() });
1810        let m = crate::memory::derive(&team_view(&home.join("lessons.jsonl"), true), &ls, None);
1811        assert_eq!((m["L1"].tested, m["L2"].tested), (1, 0), "{m:?}");
1812        let _ = std::fs::remove_dir_all(home);
1813    }
1814
1815    #[test]
1816    fn off_keeps_beliefs_local() {
1817        let home = team_store("off", false);
1818        let local = team_view(&home.join("lessons.jsonl"), false);
1819        assert_eq!(local, read(&home.join("outcomes.jsonl")));
1820        assert_eq!(changed(&local, "L1"), 1);
1821        let _ = std::fs::remove_dir_all(home);
1822    }
1823
1824    /// Several writers appending to one log at once (several drones on one store): every record
1825    /// reads back, none torn into `{A}{B}\n\n`.
1826    #[test]
1827    fn concurrent_appends_keep_every_record() {
1828        let home = tmp("concurrent");
1829        let p = home.join("outcomes.jsonl");
1830        let (writers, each) = (8, 2000);
1831        std::thread::scope(|scope| {
1832            for w in 0..writers {
1833                let p = &p;
1834                scope.spawn(move || (0..each).for_each(|i| append(p, &o(ASKED, &format!("s{w}"), &["A"], i)).unwrap()));
1835            }
1836        });
1837        assert_eq!(read(&p).len(), writers * each as usize);
1838        let _ = std::fs::remove_dir_all(home);
1839    }
1840
1841    #[test]
1842    fn append_and_read_round_trip() {
1843        let dir = std::env::temp_dir().join(format!("borg-outcome-{}", std::process::id()));
1844        let p = dir.join("outcomes.jsonl");
1845        let _ = std::fs::remove_file(&p);
1846        append(&p, &o(ASKED, "s", &["A"], 1)).unwrap();
1847        append(&p, &o(CORRECTED, "s", &["A"], 2)).unwrap();
1848        assert_eq!(read(&p), vec![o(ASKED, "s", &["A"], 1), o(CORRECTED, "s", &["A"], 2)]);
1849        let _ = std::fs::remove_dir_all(dir);
1850    }
1851}