Skip to main content

borg/
learn.rs

1//! The conscience's live learning loop.
2//!
3//! **Capture** (hooks, deterministic, 0 tokens): an engineer correction is a prompt that pushes
4//! back on what the drone just proposed ("no…", "double check…", "why do you…"), or a rejected
5//! tool call / plan whose rejection carries the engineer's words. Each becomes a raw *episode*:
6//! the task, the drone's proposal, the correction.
7//!
8//! **Generalise** (`borg learn`, at session end, one model call per episode): the episode becomes a
9//! lesson (question + situations → consequence) or is merged into an existing lesson that
10//! expresses the same concern — the old wording is kept in `history` and `support` grows, so the
11//! library consolidates instead of piling up near-duplicates. Each lesson lists the engineers
12//! (git user.email) whose corrections it was learned from, its area (the kind of concern, for trust per
13//! engineer × area, trust.rs) and, when the correction contradicts a lesson it was shown, which
14//! lessons it `supersedes`; all from the same model call.
15//!
16//! **Outcomes** (`borg learn`, 0 tokens): a merge into a lesson the conscience asked earlier in the
17//! same session, or a new lesson superseding one it asked, means asking did not prevent the
18//! correction; a correction siding with one side of a rendered disagreement corrects the other
19//! sides instead; see outcome.rs.
20//!
21//! **Vetoes** (`borg no`, veto.rs): an engineer's "no" is an episode of kind `veto`, asked with its own
22//! prompt (`veto::OPENING`), whose answer may also name an invariant. It is learned at the end of any
23//! session, not only its own (a terminal veto may name an ended session). A veto merged into a lesson
24//! an earlier veto made counts as `veto_repeat`; an invariant of a lesson placed in this store becomes
25//! a proposed constraint in the queen's queue (`veto::propose`), never a constraint.
26
27use std::collections::{HashMap, HashSet};
28use std::io::Write;
29use std::path::{Path, PathBuf};
30use std::process::Command;
31use std::sync::LazyLock;
32
33use regex::Regex;
34use serde::{Deserialize, Serialize};
35use serde_json::{Value, json};
36
37use crate::conscience::{self, Challenge};
38use crate::outcome;
39// After `MAX_FAILURES` failed model calls in a row `borg learn` stops (runner.rs: the model is
40// unavailable, and asking about every remaining episode would only burn time and quota).
41use crate::runner::MAX_FAILURES;
42use crate::trust;
43
44pub mod regret;
45
46static CORRECTION: LazyLock<Regex> = LazyLock::new(|| {
47    Regex::new(r"(?i)^\s*(no\b|nope|don'?t|do not|stop|wrong|that'?s not|not what|why did you|why do you|i said|instead|you should|never\b|always\b|please don'?t|double check|carefully check|check against|are you sure|hmm|but\b|just a second|still\b|can'?t we|don'?t we|isn'?t)").unwrap()
48});
49static REJECTION: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?is)want to proceed with this tool use.*?the user said:\s*(.*)").unwrap());
50/// A tool call the engineer refused without saying why: omp's approval prompt ("Tool call denied by
51/// user: bash"), omp over ACP ("Tool call rejected by user (bash)"), Claude's plain rejection.
52static DENIAL: LazyLock<Regex> = LazyLock::new(|| {
53    Regex::new(r"(?i)^\s*(tool call denied by user: |tool call rejected by user \(|the user doesn'?t want to proceed with this tool use)").unwrap()
54});
55
56/// The longest excerpt an episode keeps of a correction, a rejected tool call, and the task as the
57/// learn prompt shows it. Guard: bounds what one episode adds to the store and to its prompt (a pasted
58/// log would otherwise go in whole); a regret's excerpts share it (regret.rs `CAP`).
59pub const EXCERPT_CHARS: usize = 1500;
60/// The session's task an episode keeps (hook.rs `capture_prompt`): the prompt it started from. Guard:
61/// bounds the `.task` state file and each episode's `task` (a pasted spec would otherwise go in whole);
62/// the learn prompt shows its first `EXCERPT_CHARS`.
63pub const TASK_CHARS: usize = 3000;
64/// The end of the drone's last proposal an episode keeps (hook.rs `proposal`). Guard: bounds one
65/// episode's `agent` text.
66pub const PROPOSAL_CHARS: usize = 2000;
67
68/// A prompt that pushes back on the drone's last proposal (same heuristic as the offline miner).
69pub fn is_correction(prompt: &str) -> bool {
70    let t = prompt.trim();
71    t.len() > 3 && !t.starts_with('<') && CORRECTION.is_match(t)
72}
73
74/// The engineer's words from a rejected tool call or plan, if any.
75pub fn rejection(error: &str) -> Option<String> {
76    let said = REJECTION.captures(error)?.get(1)?.as_str();
77    let said = said.split("Note: The user").next().unwrap_or(said).trim();
78    (said.len() > 3).then(|| said.chars().take(EXCERPT_CHARS).collect())
79}
80
81/// A refusal that carries no words (see `DENIAL`): the engineer's reason is their next prompt.
82pub fn denied(error: &str) -> bool {
83    rejection(error).is_none() && DENIAL.is_match(error)
84}
85
86#[derive(Debug, Clone, Default, Serialize, Deserialize)]
87pub struct Episode {
88    pub id: String,
89    pub ms: i64,
90    pub repo: String,
91    pub session: String,
92    pub task: String,
93    pub agent: String,
94    pub rejected_tool: Option<String>,
95    pub correction: String,
96    /// Who corrected: `git config user.email` in the episode's cwd, empty if unknown.
97    #[serde(default)]
98    pub engineer: String,
99    /// Empty for a correction; `regret::KIND` for a decision a later session fixed.
100    #[serde(default, skip_serializing_if = "String::is_empty")]
101    pub kind: String,
102    #[serde(default, skip_serializing_if = "Option::is_none")]
103    pub regret: Option<regret::Regret>,
104    /// How the engineer said it, where that is not a prompt or a rejected tool call: [`ANSWER`] for an
105    /// answer to the agent's own question (the questions and their options are the `agent` text).
106    #[serde(default, skip_serializing_if = "String::is_empty")]
107    pub via: String,
108    /// The harness whose transcript the episode was read from (`Harness::name`); empty for one the hook
109    /// captured live.
110    #[serde(default, skip_serializing_if = "String::is_empty")]
111    pub harness: String,
112    /// That harness's version as its transcript names it (Claude Code's `version`, omp's
113    /// `runtime.version`, opencode 1.x's session `version`, Codex's `cli_version`); empty where it names none.
114    #[serde(default, skip_serializing_if = "String::is_empty")]
115    pub harness_version: String,
116}
117
118/// `Episode::via` of an answer to the agent's own question, and its `why` in the usage statistics.
119pub const ANSWER: &str = "answer";
120
121/// Two prompt episodes of one session this close together are the same correction. Method: a dedupe
122/// window; the live hook's capture time and the transcript's message time of one prompt differ by a
123/// second or two, two corrections of a session by far more. Episodes of a tool call are matched by
124/// their id and words instead ([`Known::holds`]).
125pub const SAME_MS: i64 = 5_000;
126
127/// The id of an episode read from a transcript: `<session8>-T<call>` for one about a tool call (the
128/// harness's own id for it: Claude's `tool_use_id`, omp's `toolCallId`, opencode's part id, Codex's
129/// `call_id`), else `<session8>-<ms>` as the hook names a prompt's. Content, never a position: a
130/// transcript still being written cannot shift it.
131pub fn transcript_id(session: &str, call: &str, ms: i64) -> String {
132    let s8: String = session.chars().take(8).collect();
133    if call.is_empty() { format!("{s8}-{ms}") } else { format!("{s8}-T{call}") }
134}
135
136/// A correction's words with its whitespace collapsed: the hook and a transcript may wrap or pad the
137/// same words differently.
138fn words(t: &str) -> String {
139    t.split_whitespace().collect::<Vec<_>>().join(" ")
140}
141
142/// What a store already holds, so an episode read again (the transcript sweep, session end,
143/// `borg assimilate`) is never appended twice. Read once, then grown with what is appended.
144#[derive(Debug, Default)]
145pub struct Known {
146    ids: HashSet<String>,
147    /// Per session: each episode's time, whether it was a prompt's, whether a transcript named it by its
148    /// tool call ([`from_call`]), and its words ([`words`]).
149    sessions: HashMap<String, Vec<Held>>,
150}
151
152impl Known {
153    /// The episodes of `store`'s `episodes.jsonl`.
154    pub fn read(store: &Path) -> Self {
155        let mut k = Known::default();
156        for e in read_jsonl::<Episode>(&store.join("episodes.jsonl")) {
157            k.add(&e);
158        }
159        k
160    }
161
162    /// Whether `e` is already held: an episode with its id; one of its session with the same words that no
163    /// transcript named by its call (the hook stamps capture time, a transcript message time, so their ids
164    /// differ; two calls the transcript named are two events, whatever their words); or, for a prompt's
165    /// episode, a prompt's of its session within [`SAME_MS`] (the hook and the transcript cut a prompt
166    /// alike, but an older store's wording may differ). An answer is matched by its id alone: only a
167    /// transcript records answers, and the same pick to two questions is two answers.
168    pub fn holds(&self, e: &Episode) -> bool {
169        if self.ids.contains(&e.id) {
170            return true;
171        }
172        let said = words(&e.correction);
173        let (prompt, answer) = (prompt_episode(e), e.via == ANSWER);
174        self.sessions
175            .get(&e.session)
176            .is_some_and(|seen| seen.iter().any(|h| (!answer && !h.from_call && h.words == said) || (prompt && h.prompt && (h.ms - e.ms).abs() < SAME_MS)))
177    }
178
179    /// Remember `e` as held.
180    pub fn add(&mut self, e: &Episode) {
181        self.ids.insert(e.id.clone());
182        let held = Held { ms: e.ms, prompt: prompt_episode(e), from_call: from_call(e), words: words(&e.correction) };
183        self.sessions.entry(e.session.clone()).or_default().push(held);
184    }
185}
186
187/// One held episode, as [`Known::holds`] compares it.
188#[derive(Debug)]
189struct Held {
190    ms: i64,
191    prompt: bool,
192    from_call: bool,
193    words: String,
194}
195
196/// Whether a transcript named `e` by its tool call ([`transcript_id`]'s `<session8>-T<call>`); the hook
197/// names its episodes `<session8>-<ms>`.
198fn from_call(e: &Episode) -> bool {
199    let s8: String = e.session.chars().take(8).collect();
200    e.id.strip_prefix(&s8).is_some_and(|rest| rest.starts_with("-T"))
201}
202
203/// An episode said in a prompt: a pushback or a veto, not a rejected tool call or an answer.
204fn prompt_episode(e: &Episode) -> bool {
205    e.rejected_tool.is_none() && e.via.is_empty() && e.kind != regret::KIND
206}
207
208impl AsMut<Episode> for Episode {
209    fn as_mut(&mut self) -> &mut Episode {
210        self
211    }
212}
213
214/// Appends to `store` each of `eps` it does not hold yet ([`Known`]), each counted as
215/// `record_episode_in` counts it, and returns those appended, in order. `ready` completes an episode
216/// (its repo, its engineer) just before it is appended, so one already held costs nothing. The one
217/// entry for transcript capture (the transcript sweep, `borg assimilate --apply`).
218///
219/// What the store holds is read under the lock of its `episodes.jsonl`, which every caller of this
220/// takes: a writer that read the store earlier (an assimilate that learned another collective
221/// meanwhile, a turn-end sweep of the same session) never appends an episode another one appended
222/// since.
223///
224/// # Errors
225/// When an episode cannot be appended; none after it is.
226pub fn record_new_in<T: AsMut<Episode>>(store: &Path, eps: Vec<T>, mut ready: impl FnMut(&mut T)) -> std::io::Result<Vec<T>> {
227    if eps.is_empty() {
228        return Ok(Vec::new());
229    }
230    std::fs::create_dir_all(store)?;
231    crate::live::locked(&store.join("episodes.jsonl"), || {
232        let mut known = Known::read(store);
233        let mut appended = Vec::new();
234        for mut x in eps {
235            if known.holds(x.as_mut()) {
236                continue;
237            }
238            ready(&mut x);
239            record_episode_in(store, x.as_mut())?;
240            known.add(x.as_mut());
241            appended.push(x);
242        }
243        Ok(appended)
244    })
245}
246
247/// A lesson as stored: the conscience's `Challenge` plus provenance and consolidation state.
248/// Append-only: a merge writes a new version with the same id; readers keep the latest.
249#[derive(Debug, Clone, Default, Serialize, Deserialize)]
250pub struct Lesson {
251    pub id: String,
252    pub origin_ms: i64,
253    pub repo: String,
254    pub question: String,
255    pub situations: Vec<String>,
256    pub consequence: String,
257    pub support: u32,
258    pub episodes: Vec<String>,
259    pub history: Vec<Value>,
260    pub version: u32,
261    /// Engineers whose corrections this lesson was learned from, first contributor first.
262    #[serde(default)]
263    pub engineers: Vec<String>,
264    /// When each later engineer joined (ms of their first episode merged in): trust.rs counts a
265    /// lesson's outcomes for an engineer only from then on. The first contributor is absent.
266    #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
267    pub joined: std::collections::BTreeMap<String, i64>,
268    /// Kind of concern (trust.rs `AREAS`), set by the generalising model call; "other" if absent.
269    #[serde(default = "other_area")]
270    pub area: String,
271    /// Existing lessons this one replaces or contradicts; the conscience asks this one instead.
272    #[serde(default, skip_serializing_if = "Vec::is_empty")]
273    pub supersedes: Vec<String>,
274    /// "queen" for a queen answer (queen.rs), "import" for team knowledge read from a directory
275    /// (assimilate_dir.rs), kept across merges; empty for learned lessons.
276    #[serde(default, skip_serializing_if = "String::is_empty")]
277    pub source: String,
278    /// Where the lesson applies, as names (systems, repos, services: "Orion", "harbor_orion"). Empty:
279    /// wherever it matches. A scoped lesson is asked only when the drone's context names one of them
280    /// (conscience.rs `in_scope`), so a team rule for one system never reaches another.
281    #[serde(default, skip_serializing_if = "Vec::is_empty")]
282    pub scope: Vec<String>,
283    /// The outside rule or idiom this lesson replaces, and why ("upstream petri-net-design seeds a budget
284    /// with a reset arc; Orion drains instead because ..."). Lessons replace lessons via `supersedes`;
285    /// this names what is not a lesson (a vendored skill, upstream docs). Rendered with the question:
286    /// an override that does not name what it replaces loses to it (seen in an earlier eval).
287    #[serde(default, skip_serializing_if = "String::is_empty")]
288    pub overrides: String,
289    /// Source location of an imported lesson (`path:line` in the source repo); empty for learned ones.
290    #[serde(default, skip_serializing_if = "String::is_empty")]
291    pub origin: String,
292    /// Alternative phrasings of the lesson for search (facets.rs): the concept without names, the symptom,
293    /// the action, the consequence, German, the identifiers as aliases. Each is its own BM25 document
294    /// (conscience.rs `rank`); never shown to a drone. Empty for lessons learned before facets existed.
295    #[serde(default, skip_serializing_if = "Vec::is_empty")]
296    pub facets: Vec<String>,
297    /// Who should hear the lesson (reach.rs): empty for the team, `repo` for its repo only, `personal`
298    /// for a tombstone withdrawn to an engineer's private collective.
299    #[serde(default, skip_serializing_if = "String::is_empty")]
300    pub reach: String,
301}
302
303fn other_area() -> String {
304    trust::OTHER.into()
305}
306
307pub fn home() -> PathBuf {
308    std::env::var("BORG_HOME").map(PathBuf::from).unwrap_or_else(|_| PathBuf::from(std::env::var("HOME").unwrap_or_default()).join(".local/share/borg"))
309}
310
311pub(crate) fn append_jsonl<T: Serialize>(path: &Path, v: &T) -> std::io::Result<()> {
312    append_line(path, &serde_json::to_string(v)?)
313}
314
315/// Appends `line` and its newline in one write. Under O_APPEND one write lands whole; `writeln!` makes
316/// two (the record, then "\n"), so two writers at once (two drones on one store) can leave
317/// `{A}{B}\n\n`, and every reader drops both unparseable records without a word.
318pub(crate) fn append_line(path: &Path, line: &str) -> std::io::Result<()> {
319    if let Some(d) = path.parent() {
320        std::fs::create_dir_all(d)?;
321    }
322    let mut f = std::fs::OpenOptions::new().create(true).append(true).open(path)?;
323    f.write_all(format!("{line}\n").as_bytes())
324}
325
326fn read_jsonl<T: for<'de> Deserialize<'de>>(path: &Path) -> Vec<T> {
327    std::fs::read_to_string(path).unwrap_or_default().lines().filter_map(|l| serde_json::from_str(l).ok()).collect()
328}
329
330/// False when BORG_LEARN=off (eval drones): nothing is captured or recorded.
331pub fn learning() -> bool {
332    std::env::var("BORG_LEARN").as_deref() != Ok("off")
333}
334
335/// The engineer working in `cwd`: their git user.email, empty when unknown.
336pub fn engineer(cwd: &str) -> String {
337    if cwd.is_empty() {
338        return String::new();
339    }
340    Command::new("git")
341        .args(["-C", cwd, "config", "user.email"])
342        .stdin(std::process::Stdio::null())
343        .stderr(std::process::Stdio::null())
344        .output()
345        .ok()
346        .filter(|o| o.status.success())
347        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
348        .unwrap_or_default()
349}
350
351pub fn record_episode(e: &Episode) -> std::io::Result<()> {
352    record_episode_in(&home(), e)
353}
354
355/// `record_episode` into `store` (the end-of-session job writes to the route's store).
356///
357/// # Errors
358/// When the episode cannot be appended.
359pub fn record_episode_in(store: &Path, e: &Episode) -> std::io::Result<()> {
360    append_jsonl(&store.join("episodes.jsonl"), e)?;
361    count_correction(store, e);
362    Ok(())
363}
364
365/// A correction in the usage statistics: a rejected tool call, a pushback, an answer to the agent's question
366/// (`why` [`ANSWER`], rolled up apart from the corrections, stats.rs `fold`) or a regret, with a short excerpt.
367pub fn count_correction(store: &Path, e: &Episode) {
368    // A veto counts itself (`stats::VETO`, veto.rs).
369    if e.kind == crate::veto::KIND {
370        return;
371    }
372    let snippet = crate::stats::excerpt(&e.correction, crate::stats::FEED_CHARS);
373    let (why, text) = match &e.rejected_tool {
374        _ if e.kind == regret::KIND => ("regret", snippet),
375        _ if e.via == ANSWER => (ANSWER, snippet),
376        Some(tool) => ("rejected_tool", format!("{tool}: {snippet}")),
377        None => ("pushback", snippet),
378    };
379    crate::stats::count(store, crate::stats::CORRECTION, &e.session, |ev| {
380        ev.ms = e.ms;
381        ev.engineer.clone_from(&e.engineer);
382        ev.repo.clone_from(&e.repo);
383        ev.why = why.into();
384        ev.text = text;
385        ev.source = crate::stats::source(&e.harness, &e.harness_version);
386    });
387}
388
389/// The latest version of every lesson.
390pub fn lessons(path: &Path) -> Vec<Lesson> {
391    let mut by: HashMap<String, Lesson> = HashMap::new();
392    for l in read_jsonl::<Lesson>(path) {
393        if by.get(&l.id).is_none_or(|o| o.version <= l.version) {
394            by.insert(l.id.clone(), l);
395        }
396    }
397    let mut v: Vec<Lesson> = by.into_values().collect();
398    v.sort_by_key(|l| l.origin_ms);
399    v
400}
401
402/// The correction prompt's opening.
403const OPENING: &str = "An engineer corrected a coding agent. Decide whether the correction expresses a reusable engineering \
404lesson: a critical concern about quality, design, correctness, reuse, spec conformance or root cause that this engineer would \
405plausibly raise again about similar future work. Operational remarks (\"still working?\", \"stop\", \"push\", \"restart\") are NOT lessons.";
406
407/// Who should hear a lesson: shared by the correction and the regret prompt.
408const REACH_RULES: &str = "Then decide its reach: who should hear it. Ask: would a teammate working on the same code get this correction too?
409- personal: how this one engineer likes the agent to work with them, not a property of the code: commit message style \
410or length, AI attribution or co-author trailers, whether and when to commit, push, branch or open a PR, what to stage, \
411how to report or ask, language and tone, their own tooling habits.
412- repo: a convention of the repo it happened in that other repos do not share (its branch or deploy workflow, a tool \
413or command it mandates).
414- team: everything else: an engineering concern about the code, design, correctness or verification.";
415
416const FIELDS: &str = "If it is a lesson, write:
417- question: ONE question the agent should ask itself next time, general enough to transfer, keeping the technical substance;
418- situations: 1-3 lines, each `<action> on/in <kind of state or component> [under <condition>]`, without project/class names;
419- consequence: one line, what goes wrong or what the engineer requires instead;";
420
421/// The last field of either prompt.
422const AREA: &str = "- area: the one kind of concern: concurrency, spec-conformance, reuse-existing, architecture-boundary, verification, minimality, \
423protocol-correctness, data-source, security, dependency-version, scope, or other if none fits.";
424
425/// Merging and superseding: shared by the correction and the regret prompt.
426const MERGE_RULES: &str = "Existing lessons that may express the same concern are listed. If one of them IS the same concern (the same kind of mistake) \
427and agrees with this correction, set same_as to its id and write the merged, improved wording in question/situations/consequence. \
428Otherwise same_as is empty.
429
430If this correction contradicts or replaces a listed lesson (in the same situation it requires something different or the \
431opposite, e.g. the engineer reversed an earlier correction), list that lesson's id in supersedes; never the same_as lesson. \
432Otherwise supersedes is empty.";
433
434/// The prompt for one episode: a correction's, a veto's (veto.rs) or a regret's (regret.rs).
435fn episode_prompt(e: &Episode, listed: &[String]) -> String {
436    let existing = if listed.is_empty() { "(none)".into() } else { listed.join("\n") };
437    if e.kind == crate::veto::KIND {
438        let agent = if e.agent.trim().is_empty() { "(not available)" } else { e.agent.as_str() };
439        return format!(
440            "{}\n\n{REACH_RULES}\n\n{FIELDS}\n{AREA}\n{}\n\n{MERGE_RULES}\n\n## Task the agent had\n{}\n\n## What the agent proposed (excerpt)\n{agent}\n\n## Engineer's veto\n{}\n\n## Existing lessons\n{existing}\n",
441            crate::veto::OPENING,
442            crate::veto::INVARIANT,
443            e.task.chars().take(EXCERPT_CHARS).collect::<String>(),
444            e.correction,
445        );
446    }
447    if let Some(r) = e.regret.as_ref().filter(|_| e.kind == regret::KIND) {
448        let (head, sections) = (regret::head(r), regret::sections(r, &e.task));
449        let (reach, merge) = (regret::neutral(REACH_RULES), regret::neutral(MERGE_RULES));
450        return format!("{head}\n\n{reach}\n\n{}\n{AREA}\n\n{merge}\n\n{sections}## Existing lessons\n{existing}\n", regret::FIELDS);
451    }
452    format!(
453        "{OPENING}\n\n{REACH_RULES}\n\n{FIELDS}\n{AREA}\n\n{MERGE_RULES}\n\n## Task the agent had\n{}\n\n## What the agent proposed (excerpt)\n{}\n{}\n\n## Engineer's correction\n{}\n\n## Existing lessons\n{existing}\n",
454        e.task.chars().take(EXCERPT_CHARS).collect::<String>(),
455        e.agent,
456        if e.via == ANSWER { ANSWERED } else { e.rejected_tool.as_deref().unwrap_or("") },
457        e.correction,
458    )
459}
460
461/// Under an answer's proposal (the agent's own questions): what makes an answer a lesson.
462const ANSWERED: &str = "(The agent asked these questions; the correction below is the engineer's answer. An answer that only picks \
463what fits this one task is NOT a lesson; one that states a rule, a preference or a reason that holds beyond it is.)";
464
465fn schema() -> Value {
466    json!({"type": "object", "additionalProperties": false, "properties": {
467        "is_lesson": {"type": "boolean"}, "question": {"type": "string"},
468        "situations": {"type": "array", "items": {"type": "string"}}, "consequence": {"type": "string"},
469        "same_as": {"type": "string"}, "area": {"type": "string", "enum": trust::AREAS},
470        "supersedes": {"type": "array", "items": {"type": "string"}},
471        "reach": {"type": "string", "enum": ["team", "repo", "personal"]}},
472        "required": ["is_lesson", "question", "situations", "consequence", "same_as", "area", "supersedes", "reach"]})
473}
474
475/// `schema` plus the invariant a veto's reason may name (veto.rs).
476fn veto_schema() -> Value {
477    let mut s = schema();
478    s["properties"]["invariant"] = json!({"type": "string"});
479    if let Some(r) = s["required"].as_array_mut() {
480        r.push(json!("invariant"));
481    }
482    s
483}
484
485/// The answer schema for `prompt`: a veto's prompt (it starts with `veto::OPENING`) gets the invariant
486/// field; correction and regret calls stay as they were.
487pub fn schema_for(prompt: &str) -> Value {
488    if prompt.starts_with(crate::veto::OPENING) { veto_schema() } else { schema() }
489}
490
491/// The model call failed (quota, rate limit, not logged in, network, malformed answer): unlike a
492/// verdict of "not a lesson", the episode stays waiting and a later `borg learn` retries it.
493#[derive(Debug, Clone, Copy, PartialEq, Eq)]
494pub struct AskFailed;
495
496/// The model's verdict on an episode that is a lesson.
497struct Answer {
498    question: String,
499    situations: Vec<String>,
500    consequence: String,
501    area: String,
502    reach: String,
503    same_as: String,
504    supersedes: Vec<String>,
505    /// A veto's invariant (veto.rs); empty for every other episode, and when the model named none.
506    invariant: String,
507}
508
509impl Answer {
510    /// `Ok(None)` not a lesson; `Err` no usable verdict.
511    fn parse(v: &Value) -> Result<Option<Answer>, AskFailed> {
512        match v["is_lesson"].as_bool() {
513            Some(true) => {}
514            Some(false) => return Ok(None),
515            None => return Err(AskFailed),
516        }
517        let strs = |k: &str| v[k].as_array().into_iter().flatten().filter_map(|x| x.as_str().map(str::to_string)).collect::<Vec<_>>();
518        let text = |k: &str| v[k].as_str().unwrap_or_default().to_string();
519        let question = v["question"].as_str().filter(|q| !q.trim().is_empty()).ok_or(AskFailed)?.to_string();
520        let reach = v["reach"].as_str().filter(|r| [crate::reach::REPO, crate::reach::PERSONAL].contains(r)).unwrap_or_default();
521        Ok(Some(Answer {
522            question,
523            situations: strs("situations"),
524            consequence: text("consequence"),
525            area: trust::area(v["area"].as_str().unwrap_or_default()).to_string(),
526            reach: reach.to_string(),
527            same_as: text("same_as"),
528            supersedes: strs("supersedes"),
529            invariant: text("invariant"),
530        }))
531    }
532}
533
534/// The lessons most like an episode that the learn prompt offers for merging (`similar`). Baseline:
535/// the count since 0.1.0, never evaluated, the reference a later value is compared against. A duplicate
536/// ranked below it is not offered, so its episode can become a second lesson instead of a merge.
537const MERGE_CANDIDATES: usize = 5;
538
539/// The library's lessons most like the episode (at most `MERGE_CANDIDATES`), offered for merging.
540fn similar<'a>(e: &Episode, library: &'a [Lesson]) -> Vec<&'a Lesson> {
541    let as_challenges: Vec<Challenge> = library
542        .iter()
543        .map(|l| Challenge {
544            id: l.id.clone(),
545            origin_ms: l.origin_ms,
546            question: format!("{} {}", l.question, l.situations.join(" ")),
547            situations: l.situations.clone(),
548            consequence: l.consequence.clone(),
549            // A superseded lesson is not offered for merging; its live successor is, in its place. A
550            // re-reversal then contradicts the successor and becomes a new lesson superseding it.
551            supersedes: l.supersedes.clone(),
552            engineers: l.engineers.clone(),
553            source: l.source.clone(),
554            ..Default::default() // similarity for merging is about meaning, not usefulness or trust
555        })
556        .collect();
557    let query = format!("{} {} {}", e.task, e.agent, e.correction);
558    conscience::select(&as_challenges, &query, MERGE_CANDIDATES).iter().filter_map(|c| library.iter().find(|l| l.id == c.id)).collect()
559}
560
561/// `old` with the episode merged in: the new wording, the old one kept in `history`.
562fn merged(old: &Lesson, e: &Episode, a: Answer, mut supersedes: Vec<String>) -> Lesson {
563    let mut history = old.history.clone();
564    history.push(json!({"question": old.question, "consequence": old.consequence, "version": old.version}));
565    let mut episodes = old.episodes.clone();
566    episodes.push(e.id.clone());
567    let (mut engineers, mut joined) = (old.engineers.clone(), old.joined.clone());
568    // Emails compare case-insensitively; an alias (collective.rs "Engineers") is stored as
569    // its raw email and mapped when read, so the store never depends on this machine's config.
570    let same = |x: &String| x.trim().to_lowercase() == e.engineer.trim().to_lowercase();
571    if !e.engineer.trim().is_empty() && !engineers.iter().any(same) {
572        engineers.push(e.engineer.clone());
573        joined.insert(e.engineer.clone(), e.ms);
574    }
575    for id in old.supersedes.iter().rev() {
576        if !supersedes.contains(id) {
577            supersedes.insert(0, id.clone());
578        }
579    }
580    Lesson {
581        question: a.question,
582        situations: a.situations,
583        consequence: a.consequence,
584        support: old.support + 1,
585        episodes,
586        history,
587        version: old.version + 1,
588        engineers,
589        joined,
590        area: a.area,
591        supersedes,
592        // New wording: the old facets describe the old wording; `borg learn` generates new ones.
593        facets: Vec::new(),
594        reach: crate::reach::narrower(&old.reach, &a.reach).to_string(),
595        ..old.clone()
596    }
597}
598
599/// A new lesson from the episode alone.
600fn fresh(e: &Episode, a: Answer, supersedes: Vec<String>) -> Lesson {
601    Lesson {
602        id: format!("L{}", e.id),
603        origin_ms: e.ms,
604        repo: e.repo.clone(),
605        question: a.question,
606        situations: a.situations,
607        consequence: a.consequence,
608        support: 1,
609        episodes: vec![e.id.clone()],
610        version: 1,
611        engineers: Some(e.engineer.clone()).filter(|x| !x.is_empty()).into_iter().collect(),
612        area: a.area,
613        supersedes,
614        reach: a.reach,
615        ..Lesson::default()
616    }
617}
618
619/// Generalise one episode against the existing library: `Ok(Some)` a new lesson or a merged version
620/// of an existing one, `Ok(None)` not a lesson, `Err` no verdict. `ask` performs the model call
621/// (injectable for tests). A regret (regret.rs) gets its own prompt, the same verdict.
622///
623/// # Errors
624/// `AskFailed` when the model gave no usable verdict.
625pub fn generalise(e: &Episode, library: &[Lesson], ask: &dyn Fn(&str) -> Option<Value>) -> Result<Option<Lesson>, AskFailed> {
626    generalise_with(e, library, ask).map(|o| o.map(|(l, _)| l))
627}
628
629/// `generalise`, with the invariant a veto's answer named (empty otherwise).
630///
631/// # Errors
632/// `AskFailed` when the model gave no usable verdict.
633pub fn generalise_with(e: &Episode, library: &[Lesson], ask: &dyn Fn(&str) -> Option<Value>) -> Result<Option<(Lesson, String)>, AskFailed> {
634    // A withdrawn personal lesson (reach.rs) is no merge target: it lives in a private collective now.
635    let library: Vec<Lesson> = library.iter().filter(|l| l.reach != crate::reach::PERSONAL).cloned().collect();
636    let similar = similar(e, &library);
637    let listed: Vec<String> = similar.iter().map(|l| format!("[{}] {} -> {}", l.id, l.question, l.consequence)).collect();
638    let Some(a) = Answer::parse(&ask(&episode_prompt(e, &listed)).ok_or(AskFailed)?)? else { return Ok(None) };
639    let same_as = similar.iter().find(|l| a.same_as == l.id);
640    // Only lessons the model was shown can be superseded, and never the one it merges into.
641    let supersedes: Vec<String> =
642        a.supersedes.iter().filter(|id| similar.iter().any(|l| &l.id == *id) && same_as.is_none_or(|m| &m.id != *id)).cloned().collect();
643    let invariant = a.invariant.trim().to_string();
644    Ok(Some((
645        match same_as {
646            Some(old) => merged(old, e, a, supersedes),
647            None => fresh(e, a, supersedes),
648        },
649        invariant,
650    )))
651}
652
653/// One structured call to the drone's model (runner.rs picks claude, omp or opencode).
654pub fn ask_claude(prompt: &str) -> Option<Value> {
655    ask_model(prompt, &schema_for(prompt)).map(|(v, _)| v)
656}
657
658/// The model call behind `ask_claude`, for any JSON schema: the structured answer and its cost in USD
659/// (None when the CLI reported none). `BORG_LEARN_MODEL` picks the model, `runner::choose` the CLI.
660pub fn ask_model(prompt: &str, schema: &Value) -> Option<(Value, Option<f64>)> {
661    let started = std::time::Instant::now();
662    let answer = call_model(prompt, schema);
663    crate::stats::count(&home(), crate::stats::MODEL_CALL, "", |ev| {
664        ev.why = crate::stats::purpose().into();
665        ev.usd = answer.as_ref().and_then(|(_, c)| *c).unwrap_or(0.0);
666        ev.dur_ms = started.elapsed().as_millis() as u64;
667        if answer.is_none() {
668            ev.text = "no answer".into();
669        }
670    });
671    answer
672}
673
674fn call_model(prompt: &str, schema: &Value) -> Option<(Value, Option<f64>)> {
675    let candidates = crate::runner::choose(std::env::var("BORG_LEARN_MODEL").ok().as_deref());
676    crate::runner::structured(&candidates, prompt, schema, learn_timeout())
677}
678
679/// How long one learn-side model call may take (`BORG_LEARN_TIMEOUT_S`, default 300 s): it runs in
680/// the background, but a hung runner must not keep `borg learn` alive forever.
681fn learn_timeout() -> std::time::Duration {
682    std::time::Duration::from_secs(std::env::var("BORG_LEARN_TIMEOUT_S").ok().and_then(|v| v.parse().ok()).unwrap_or(300))
683}
684
685pub fn now_ms() -> i64 {
686    std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as i64).unwrap_or(0)
687}
688
689/// `borg learn [--session ID]`: generalise every episode not yet folded into a lesson.
690/// `--session` is given when that session ended: its asked lessons and the placebos the judge never
691/// reached are then settled. Any run also settles sessions idle for a day, so a missing `end` hook does
692/// not bias the outcomes.
693/// Returns (new lessons, merges, episodes still waiting because the model gave no verdict).
694pub fn learn_pending(session: Option<&str>, ask: &dyn Fn(&str) -> Option<Value>) -> (usize, usize, usize) {
695    learn_in(&home(), session, ask, learning(), now_ms())
696}
697
698/// `learn_pending` on the store in `home`. With `track`, outcomes of asked lessons are recorded
699/// (outcome.rs): a merge into a lesson asked earlier in the episode's session, and a lesson asked
700/// earlier that the new one supersedes, are `asked_then_corrected` (`outcome::contradicted`); at the end of `session`, and in every session idle for
701/// `outcome::IDLE_MS` at `now`, the other asked lessons are `asked_not_corrected`. So any run
702/// settles, not only the `end` hook's; a session it skipped episodes of is not settled as idle.
703/// Only a "not a lesson" verdict is recorded as skipped: an episode whose model call failed stays
704/// waiting (and its session unsettled) for the next run, and after `MAX_FAILURES` failures in a row
705/// the run stops asking. Returns (new lessons, merges, episodes still waiting for a verdict).
706pub fn learn_in(home: &Path, session: Option<&str>, ask: &dyn Fn(&str) -> Option<Value>, track: bool, now: i64) -> (usize, usize, usize) {
707    learn_in_reporting(home, session, ask, track, now, &mut |_| {})
708}
709
710/// A progress line on stderr: `[12/104 · 18 s · ~28 min left] new lesson L…: <question>`.
711pub fn print_step(st: Step) {
712    let left = (st.total - st.done) as f64 * st.secs / 60.0;
713    let mut v: String = st.verdict.chars().take(110).collect();
714    if v.len() < st.verdict.len() {
715        v.push('…');
716    }
717    eprintln!("[{}/{} · {:.0} s · ~{:.0} min left] {v}", st.done, st.total, st.secs, left);
718}
719
720/// One model verdict during a learn run, for a progress line: the episode's place among those this
721/// run asks about (`done` of `total`), and what came of it.
722pub struct Step<'a> {
723    pub done: usize,
724    pub total: usize,
725    pub verdict: &'a str,
726    pub secs: f64,
727}
728
729/// Whether a veto's lesson, placed as `placed`, proposes its `invariant` as a constraint: only a lesson
730/// placed in this store; a personal or dropped one never does, a preference being no invariant.
731fn proposes(placed: &crate::reach::Placed, invariant: &str) -> bool {
732    matches!(placed, crate::reach::Placed::Here(_)) && !invariant.trim().is_empty()
733}
734
735/// False when BORG_REGRET_CREDIT=off: a regret no longer counts against the lessons asked before the
736/// decision (`credit_regrets`).
737pub fn regret_credit() -> bool {
738    std::env::var("BORG_REGRET_CREDIT").as_deref() != Ok("off")
739}
740
741/// Crediting a session's own green test runs on (`outcome::tested`): learning is, and BORG_TEST_CREDIT is
742/// not `off` (an eval's frozen arm).
743pub fn test_credit() -> bool {
744    learning() && std::env::var("BORG_TEST_CREDIT").as_deref() != Ok("off")
745}
746
747/// Each regret episode generalised into a lesson of this store (`library`, as stored: a lesson placed in
748/// a private store is not in it) blames the lessons asked in its deciding session before the decision
749/// (`outcome::regretted`). Runs over every such episode on every run, so regrets learned before this
750/// existed are credited once too; the (cause, lesson) key keeps it idempotent. A regret the model judged
751/// "not a lesson", one still waiting, and one without `decision_ms` credit nothing. For a review regret
752/// the deciding session is the episode's own: a lesson `contradicted` already marked corrected there is
753/// not blamed twice.
754///
755/// Indexed once per run, so a run costs one pass over the log and the library plus each deciding
756/// session's own records: `regretted` reads only the deciding session's records, and a regret's credits
757/// carry that session too.
758///
759/// A regret is also a correction of the deciding session's work: the lesson it was folded into, and the
760/// lessons that one supersedes, if a graded check of that session found them applying before the
761/// decision and the session never asked them, were questions needed and not asked
762/// (`applied_then_corrected`, `outcome::missed`, at the decision's time and with the regret as `cause`),
763/// as for a correction merged into a lesson. These misses are recorded with `credit` off too: they blame
764/// no lesson asked.
765fn credit_regrets(regrets: &[Episode], library: &[Lesson], outcomes: &mut Vec<outcome::Outcome>, path: &Path, credit: bool) {
766    let mut holder: HashMap<&str, &Lesson> = HashMap::new();
767    for l in library {
768        for id in &l.episodes {
769            holder.entry(id.as_str()).or_insert(l);
770        }
771    }
772    let deciding: HashSet<&str> = regrets.iter().filter_map(|e| e.regret.as_ref()).map(|r| r.decision_session.as_str()).collect();
773    let mut by_session: HashMap<String, Vec<outcome::Outcome>> = HashMap::new();
774    for o in outcomes.iter().filter(|o| deciding.contains(o.session.as_str())) {
775        by_session.entry(o.session.clone()).or_default().push(o.clone());
776    }
777    for e in regrets {
778        let Some(r) = &e.regret else { continue };
779        let Some(holder) = holder.get(e.id.as_str()) else { continue };
780        let target: Vec<String> = std::iter::once(holder.id.clone()).chain(holder.supersedes.iter().cloned()).collect();
781        let records = by_session.entry(r.decision_session.clone()).or_default();
782        let blamed = match credit {
783            true => outcome::regretted(records, &r.decision_session, r.decision_ms, &e.id, &target, regret::strength(r), e.ms, &e.repo),
784            false => Vec::new(),
785        };
786        let missed = match r.decision_ms > 0 && !r.decision_session.is_empty() {
787            true => outcome::missed(records, &r.decision_session, r.decision_ms, Some(&holder.id), &holder.supersedes),
788            false => Vec::new(),
789        };
790        let missed = missed.into_iter().map(|l| outcome::Outcome {
791            kind: outcome::MISSED.into(),
792            session: r.decision_session.clone(),
793            lessons: vec![l],
794            ms: r.decision_ms,
795            repo: e.repo.clone(),
796            cause: e.id.clone(),
797            ..Default::default()
798        });
799        for o in blamed.into_iter().chain(missed.collect::<Vec<_>>()) {
800            let _ = outcome::append(path, &o);
801            records.push(o.clone());
802            outcomes.push(o);
803        }
804    }
805}
806
807/// Whether the session whose state file is `ctx` is still open at `now`: the file changed within
808/// `outcome::IDLE_MS` (every prompt and touch appends to it). No file: not open.
809fn open(ctx: &Path, now: i64) -> bool {
810    let ms = std::fs::metadata(ctx).and_then(|m| m.modified()).ok().and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()).map(|d| d.as_millis() as i64);
811    ms.is_some_and(|ms| ms > now - outcome::IDLE_MS)
812}
813
814/// The sessions besides `session` whose waiting episodes (not in `done` nor `skipped`) a run with
815/// `session` learns too: quiet ones, whose newest episode is `outcome::IDLE_MS` old at `now` and which
816/// `is_open` (its state file, `open`) says are not open. A session left open never sends its end, and
817/// the transcript sweep (sweep.rs `idle`) adds its last corrections after it went quiet. Each such
818/// session costs one stat.
819fn quiet(
820    episodes: &[Episode],
821    session: Option<&str>,
822    done: &HashSet<String>,
823    skipped: &HashSet<String>,
824    now: i64,
825    is_open: impl Fn(&str) -> bool,
826) -> HashSet<String> {
827    let Some(session) = session else { return HashSet::new() };
828    let mut newest: HashMap<&str, i64> = HashMap::new();
829    for e in episodes {
830        let n = newest.entry(e.session.as_str()).or_insert(e.ms);
831        *n = (*n).max(e.ms);
832    }
833    let waiting: HashSet<&str> =
834        episodes.iter().filter(|e| e.session != session && !done.contains(&e.id) && !skipped.contains(&e.id)).map(|e| e.session.as_str()).collect();
835    waiting.into_iter().filter(|s| newest[s] <= now - outcome::IDLE_MS && !is_open(s)).map(str::to_string).collect()
836}
837
838/// Episode kinds the removed hive made (a group's reflection, a rework), skipped where an older store has them.
839const RETIRED: [&str; 2] = ["group", "rework"];
840
841/// `learn_in`, calling `report` after every model call (the CLI prints it; the hooks pass a no-op).
842///
843/// One run per store at a time (an exclusive lock on `lessons.jsonl.lock`): session-end runs of several
844/// drones share the veto episodes and the regret credits, and two runs reading the store before either
845/// writes would learn a veto twice and count its credits twice. A waiting run then reads what the first
846/// one wrote and has nothing left to do for them.
847pub fn learn_in_reporting(
848    home: &Path,
849    session: Option<&str>,
850    ask: &dyn Fn(&str) -> Option<Value>,
851    track: bool,
852    now: i64,
853    report: &mut dyn FnMut(Step),
854) -> (usize, usize, usize) {
855    crate::live::locked(&home.join("lessons.jsonl"), || learn_unlocked(home, session, ask, track, now, report))
856}
857
858fn learn_unlocked(
859    home: &Path,
860    session: Option<&str>,
861    ask: &dyn Fn(&str) -> Option<Value>,
862    track: bool,
863    now: i64,
864    report: &mut dyn FnMut(Step),
865) -> (usize, usize, usize) {
866    let (ep_path, lessons_path) = (home.join("episodes.jsonl"), home.join("lessons.jsonl"));
867    let outcomes_path = outcome::path_beside(&lessons_path);
868    let mut outcomes = if track { outcome::read(&outcomes_path) } else { Vec::new() };
869    let mut library = lessons(&lessons_path);
870    let done: std::collections::HashSet<String> = library.iter().flat_map(|l| l.episodes.clone()).collect();
871    let skipped_path = home.join("episodes-not-lessons.txt");
872    let skipped: std::collections::HashSet<String> = std::fs::read_to_string(&skipped_path).unwrap_or_default().lines().map(str::to_string).collect();
873    let (mut new, mut merged, mut failed, mut failures_in_row) = (0, 0, 0, 0);
874    // Newest episode per session, and sessions with episodes this run left pending (--session).
875    let mut last_episode: std::collections::HashMap<String, i64> = std::collections::HashMap::new();
876    let mut pending: std::collections::HashSet<String> = std::collections::HashSet::new();
877    // `RETIRED` kinds an older store holds are never learned.
878    let episodes: Vec<Episode> = read_jsonl::<Episode>(&ep_path).into_iter().filter(|e| !RETIRED.contains(&e.kind.as_str())).collect();
879    let regrets: Vec<Episode> = episodes.iter().filter(|e| e.kind == regret::KIND && e.regret.is_some()).cloned().collect();
880    let state = std::env::var_os("BORG_STATE").map_or_else(|| home.join("state"), std::path::PathBuf::from);
881    let ctx = |s: &str| state.join(format!("{}.ctx", crate::notes::safe(s)));
882    // A veto is learned at the end of any session (veto.rs): a terminal veto may name one already ended.
883    // So is a quiet session's episode (`quiet`): one that never sends its end would keep it waiting.
884    let quiet = quiet(&episodes, session, &done, &skipped, now, |s| open(&ctx(s), now));
885    let mine = |e: &Episode| session.is_none_or(|s| s == e.session) || e.kind == crate::veto::KIND || quiet.contains(&e.session);
886    let vetoes: HashSet<String> = episodes.iter().filter(|e| e.kind == crate::veto::KIND).map(|e| e.id.clone()).collect();
887    let total = episodes.iter().filter(|e| !done.contains(&e.id) && !skipped.contains(&e.id) && mine(e)).count();
888    let mut asked = 0;
889    for e in episodes {
890        let last = last_episode.entry(e.session.clone()).or_insert(e.ms);
891        *last = (*last).max(e.ms);
892        if done.contains(&e.id) || skipped.contains(&e.id) {
893            continue;
894        }
895        if !mine(&e) {
896            pending.insert(e.session.clone());
897            continue;
898        }
899        if failures_in_row >= MAX_FAILURES {
900            failed += 1;
901            pending.insert(e.session.clone());
902            continue;
903        }
904        let started = std::time::Instant::now();
905        let verdict = generalise_with(&e, &library, ask);
906        asked += 1;
907        let what = match &verdict {
908            Err(AskFailed) => "no answer from the model, left pending".to_string(),
909            Ok(Some((l, _))) if l.version > 1 => format!("merged into {}: {}", l.id, l.question),
910            Ok(Some((l, _))) => format!("new lesson {}: {}", l.id, l.question),
911            Ok(None) => "not a lesson".to_string(),
912        };
913        report(Step { done: asked, total, verdict: &what, secs: started.elapsed().as_secs_f64() });
914        failures_in_row = if verdict.is_err() { failures_in_row + 1 } else { 0 };
915        match verdict {
916            Err(AskFailed) => {
917                failed += 1;
918                pending.insert(e.session.clone());
919            }
920            Ok(Some((l, invariant))) => {
921                if l.version > 1 {
922                    merged += 1
923                } else {
924                    new += 1
925                }
926                // A personal lesson goes to the engineer's private collective, never into a team store (reach.rs).
927                let placed = crate::reach::place(home, &l);
928                // Where it came from, for the feed: a review finding, a later fix, or a correction.
929                let source = match &e.regret {
930                    Some(r) if regret::is_review(r) => "review",
931                    Some(_) => "regret",
932                    None if e.kind == crate::veto::KIND => "veto",
933                    None => "session",
934                };
935                let (kind, why, counted_in) = match &placed {
936                    crate::reach::Placed::Here(l) => {
937                        let _ = append_jsonl(&lessons_path, &**l);
938                        (if l.version > 1 { crate::stats::LESSON_MERGED } else { crate::stats::LESSON_NEW }, source, home.to_path_buf())
939                    }
940                    crate::reach::Placed::Private(store) => (crate::stats::LESSON_NEW, "personal", store.clone()),
941                    crate::reach::Placed::Dropped => (crate::stats::NOT_A_LESSON, "personal", home.to_path_buf()),
942                };
943                crate::stats::count(&counted_in, kind, &e.session, |ev| {
944                    ev.engineer = e.engineer.clone();
945                    ev.repo = e.repo.clone();
946                    ev.lessons = vec![l.id.clone()];
947                    ev.why = why.into();
948                });
949                if e.kind == crate::veto::KIND {
950                    // The same "no" said twice: merged into a lesson an earlier veto made.
951                    let earlier = library.iter().find(|x| x.id == l.id);
952                    if l.version > 1 && earlier.is_some_and(|o| o.episodes.iter().any(|id| id != &e.id && vetoes.contains(id))) {
953                        // Dated when the engineer said it, the week of its own `veto`, not when it was
954                        // learned: a weekly share of repeated vetoes pairs counts of the same no's.
955                        crate::stats::count(&counted_in, crate::stats::VETO_REPEAT, &e.session, |ev| {
956                            ev.ms = if e.ms > 0 { e.ms } else { ev.ms };
957                            ev.engineer = e.engineer.clone();
958                            ev.repo = e.repo.clone();
959                            ev.lessons = vec![l.id.clone()];
960                            ev.text = crate::stats::excerpt(&e.correction, crate::stats::FEED_CHARS);
961                        });
962                    }
963                    if proposes(&placed, &invariant) {
964                        let _ = crate::veto::propose(home, &l, &invariant, &e, now);
965                    }
966                }
967                let target = (l.version > 1).then_some(l.id.as_str());
968                // A veto credited the lessons shown before it already (veto.rs): it never doubles as a
969                // correction, nor as a missed question (`applied_then_corrected`).
970                let track_results = track && e.kind != crate::veto::KIND;
971                let contradicted = match track_results {
972                    true => outcome::contradicted(&outcomes, &e.session, e.ms, target, &l.supersedes),
973                    false => Vec::new(),
974                };
975                for id in contradicted {
976                    let o = outcome::Outcome {
977                        kind: outcome::CORRECTED.into(),
978                        session: e.session.clone(),
979                        lessons: vec![id],
980                        ms: e.ms,
981                        repo: e.repo.clone(),
982                        ..Default::default()
983                    };
984                    let _ = outcome::append(&outcomes_path, &o);
985                    outcomes.push(o);
986                }
987                let missed = match track_results {
988                    true => outcome::missed(&outcomes, &e.session, e.ms, target, &l.supersedes),
989                    false => Vec::new(),
990                };
991                for id in missed {
992                    let o = outcome::Outcome {
993                        kind: outcome::MISSED.into(),
994                        session: e.session.clone(),
995                        lessons: vec![id],
996                        ms: e.ms,
997                        repo: e.repo.clone(),
998                        ..Default::default()
999                    };
1000                    let _ = outcome::append(&outcomes_path, &o);
1001                    outcomes.push(o);
1002                }
1003                library.retain(|x| x.id != l.id);
1004                library.push(l);
1005            }
1006            Ok(None) => {
1007                let _ = append_line(&skipped_path, &e.id);
1008                crate::stats::count(home, crate::stats::NOT_A_LESSON, &e.session, |ev| ev.engineer = e.engineer.clone());
1009            }
1010        }
1011    }
1012    if track {
1013        credit_regrets(&regrets, &lessons(&lessons_path), &mut outcomes, &outcomes_path, regret_credit());
1014        // Settled only once ended: its `end` hook ran (`session`), or it is idle by the log and its state
1015        // file is as quiet (`open`): a session working without asks or episodes for a day is still open.
1016        let mut ended: Vec<String> = outcome::idle_sessions(&outcomes, &last_episode, now)
1017            .into_iter()
1018            .filter(|s| !pending.contains(*s) && !open(&ctx(s), now))
1019            .map(str::to_string)
1020            .collect();
1021        if let Some(s) = session.filter(|s| !ended.iter().any(|x| x == s) && !pending.contains(*s)) {
1022            ended.push(s.to_string());
1023        }
1024        for s in &ended {
1025            // The session's own tests that went green after an ask (hook.rs `capture_test`) credit the
1026            // lessons asked, once each, as it settles.
1027            if test_credit() {
1028                let tests = std::fs::read_to_string(state.join(format!("{}.tests", crate::notes::safe(s)))).unwrap_or_default();
1029                for o in outcome::tested(&outcomes, s, &tests) {
1030                    let _ = outcome::append(&outcomes_path, &o);
1031                    outcomes.push(o);
1032                }
1033            }
1034            // Each lesson left without a verdict says why (hook.rs `unjudged`, from the state file):
1035            // censored, never an ignore (memory.rs).
1036            let why = crate::hook::unjudged(&std::fs::read_to_string(ctx(s)).unwrap_or_default());
1037            for o in outcome::settle_with(&outcomes, s, now, &|l| why.get(l).copied().unwrap_or(outcome::UNJUDGED_NO_CALL)) {
1038                let _ = outcome::append(&outcomes_path, &o);
1039                outcomes.push(o);
1040            }
1041        }
1042    }
1043    (new, merged, failed)
1044}
1045
1046#[cfg(test)]
1047mod tests {
1048    use super::*;
1049
1050    #[test]
1051    fn concurrent_jsonl_appends_keep_every_line() {
1052        let dir = std::env::temp_dir().join(format!("borg-learn-append-{}", std::process::id()));
1053        let _ = std::fs::remove_dir_all(&dir);
1054        let p = dir.join("episodes.jsonl");
1055        let (writers, each) = (8, 2000);
1056        std::thread::scope(|scope| {
1057            for w in 0..writers {
1058                let p = &p;
1059                scope.spawn(move || (0..each).for_each(|i| append_jsonl(p, &serde_json::json!({"w": w, "i": i})).unwrap()));
1060            }
1061        });
1062        assert_eq!(read_jsonl::<Value>(&p).len(), writers * each);
1063        let _ = std::fs::remove_dir_all(dir);
1064    }
1065
1066    #[test]
1067    fn a_regret_is_asked_with_its_own_prompt_and_the_shared_rules() {
1068        let r = regret::Regret {
1069            decision_task: "read the flag in b".into(),
1070            fix_subject: "fix: take the lock".into(),
1071            signals: vec!["subject".into()],
1072            age_days: 3,
1073            ..regret::Regret::default()
1074        };
1075        let e = Episode {
1076            kind: regret::KIND.into(),
1077            regret: Some(r),
1078            task: "make the flag safe".into(),
1079            ..ep("Rdddd-ffff", "Fixed later (3d, subject): fix: take the lock")
1080        };
1081        let shown = std::cell::RefCell::new(String::new());
1082        let l = generalise(&e, &[], &|p| {
1083            *shown.borrow_mut() = p.to_string();
1084            Some(json!({"is_lesson": true, "question": "Before reading a shared flag without a lock, has a concurrent writer been considered?",
1085                "situations": ["reading a shared flag across threads"], "consequence": "a race", "same_as": "", "area": "concurrency", "supersedes": []}))
1086        })
1087        .unwrap()
1088        .unwrap();
1089        let p = shown.into_inner();
1090        assert!(p.starts_with("A coding agent made a decision in an earlier session") && p.contains("3 days later"), "{p}");
1091        for part in [
1092            regret::neutral(REACH_RULES).as_str(),
1093            regret::FIELDS,
1094            AREA,
1095            regret::neutral(MERGE_RULES).as_str(),
1096            "## The decision (task)\nread the flag in b",
1097            "## The fix (subject, task)\nfix: take the lock\nmake the flag safe",
1098        ] {
1099            assert!(p.contains(part), "{part}");
1100        }
1101        assert!(!p.contains("## Engineer's correction") && !p.contains(OPENING) && !p.contains("correction"), "{p}");
1102        assert_eq!((l.id.as_str(), l.area.as_str(), l.episodes.as_slice()), ("LRdddd-ffff", "concurrency", &["Rdddd-ffff".to_string()][..]));
1103        // A correction keeps its prompt; a kind without its regret is asked as a correction.
1104        assert!(episode_prompt(&ep("e1", "no, the lock"), &[]).starts_with(OPENING));
1105        assert!(episode_prompt(&Episode { kind: regret::KIND.into(), ..ep("e2", "no") }, &[]).starts_with(OPENING));
1106    }
1107
1108    /// The injected model for vetoes: like `fake_ask` on the veto's section, plus `invariant:<rest>`.
1109    fn fake_veto_ask(p: &str) -> Option<Value> {
1110        let reason = p.split("## Engineer's veto\n").nth(1)?.lines().next()?;
1111        let after = |k: &str| reason.split(k).nth(1).and_then(|r| r.split_whitespace().next()).unwrap_or("").to_string();
1112        let invariant = reason.split("invariant:").nth(1).unwrap_or("").trim().to_string();
1113        Some(json!({"is_lesson": true, "question": format!("Q {reason}"), "situations": [], "consequence": "c",
1114            "same_as": after("merge:"), "supersedes": [], "invariant": invariant}))
1115    }
1116
1117    fn veto(id: &str, session: &str, ms: i64, reason: &str) -> Episode {
1118        Episode { kind: crate::veto::KIND.into(), ..at(id, session, ms, "ann@x", reason) }
1119    }
1120
1121    #[test]
1122    fn a_veto_is_asked_with_its_own_prompt_and_schema() {
1123        let p = episode_prompt(&veto("no-s-1", "s", 1, "never read the commit log for merges"), &[]);
1124        assert!(p.starts_with(crate::veto::OPENING), "{p}");
1125        for part in [
1126            "## Engineer's veto\nnever read the commit log for merges",
1127            crate::veto::INVARIANT,
1128            REACH_RULES,
1129            FIELDS,
1130            MERGE_RULES,
1131            "## Task the agent had\nplan the relay",
1132        ] {
1133            assert!(p.contains(part), "{part}");
1134        }
1135        assert!(!p.contains(OPENING) && !p.contains("## Engineer's correction"));
1136        let without = episode_prompt(&Episode { agent: String::new(), ..veto("no-s-2", "s", 1, "no") }, &[]);
1137        assert!(without.contains("## What the agent proposed (excerpt)\n(not available)"), "{without}");
1138        assert!(schema_for(&p)["required"].as_array().unwrap().contains(&json!("invariant")));
1139        assert!(schema_for(&p)["properties"]["invariant"].is_object());
1140        let correction = episode_prompt(&ep("e1", "no, the lock"), &[]);
1141        assert_eq!(schema_for(&correction), schema(), "a correction's call is unchanged");
1142        // The invariant comes back with the lesson; a correction's answer has none.
1143        let (l, inv) =
1144            generalise_with(&veto("no-s-3", "s", 1, "merge lookups invariant: lookups go through the review API"), &[], &fake_veto_ask).unwrap().unwrap();
1145        assert_eq!((l.id.as_str(), inv.as_str()), ("Lno-s-3", "lookups go through the review API"));
1146        let (_, none) = generalise_with(&ep("e2", "no, the lock merge:x"), &[], &fake_ask).unwrap().unwrap();
1147        assert!(none.is_empty());
1148    }
1149
1150    #[test]
1151    fn a_veto_is_learned_at_any_sessions_end_and_never_counted_as_a_correction() {
1152        let home = temp_home("veto-any");
1153        crate::learn::record_episode_in(&home, &veto("no-s1-1", "s1", 1, "never read the commit log for merges")).unwrap();
1154        append_jsonl(&home.join("episodes.jsonl"), &at("e1", "s1", 2, "ann@x", "a correction of s1")).unwrap();
1155        assert!(!crate::stats::events(&home).iter().any(|e| e.kind == crate::stats::CORRECTION), "count_correction skips a veto");
1156        // s2 ends: the veto of s1 is learned, s1's correction waits for its own session.
1157        assert_eq!(learn_in(&home, Some("s2"), &fake_veto_ask, true, 3), (1, 0, 0));
1158        let ls = lessons(&home.join("lessons.jsonl"));
1159        assert_eq!(ls.iter().map(|l| l.id.as_str()).collect::<Vec<_>>(), ["Lno-s1-1"]);
1160        let news: Vec<crate::stats::Event> = crate::stats::events(&home).into_iter().filter(|e| e.kind == crate::stats::LESSON_NEW).collect();
1161        assert_eq!(news[0].why, "veto");
1162        let _ = std::fs::remove_dir_all(home);
1163    }
1164
1165    #[test]
1166    fn a_veto_merged_into_an_earlier_vetos_lesson_is_a_repeat() {
1167        let home = temp_home("veto-repeat");
1168        let repeats = |home: &Path| crate::stats::events(home).iter().filter(|e| e.kind == crate::stats::VETO_REPEAT).count();
1169        // A lesson from a correction, then a veto merged into it: no repeat (the first "no").
1170        append_jsonl(&home.join("episodes.jsonl"), &at("e0", "s0", 1, "ann@x", "use the lock on the flag")).unwrap();
1171        append_jsonl(&home.join("episodes.jsonl"), &veto("no-s1-2", "s1", 2, "the lock again merge:Le0")).unwrap();
1172        // Le0 was asked in s1 before the veto: the veto credits it (veto.rs), learning never adds a correction.
1173        let asked = outcome::Outcome { kind: outcome::ASKED.into(), session: "s1".into(), lessons: vec!["Le0".into()], ms: 1, ..Default::default() };
1174        outcome::append(&home.join("outcomes.jsonl"), &asked).unwrap();
1175        learn_in(&home, None, &|p| if p.starts_with(crate::veto::OPENING) { fake_veto_ask(p) } else { fake_ask(p) }, true, 3);
1176        assert_eq!(repeats(&home), 0);
1177        let results = outcome::read(&home.join("outcomes.jsonl"));
1178        assert!(!results.iter().any(|o| o.kind == outcome::CORRECTED || o.kind == outcome::MISSED), "{results:?}");
1179        assert!(!crate::stats::events(&home).iter().any(|e| e.kind == crate::stats::CORRECTED));
1180        // A second veto with the same reason: merged into the lesson a veto is part of.
1181        append_jsonl(&home.join("episodes.jsonl"), &veto("no-s2-4", "s2", 4, "the lock, third time merge:Le0")).unwrap();
1182        assert_eq!(learn_in(&home, Some("s2"), &fake_veto_ask, true, 5), (0, 1, 0));
1183        assert_eq!(repeats(&home), 1);
1184        let r = crate::stats::events(&home).into_iter().find(|e| e.kind == crate::stats::VETO_REPEAT).unwrap();
1185        assert_eq!((r.session.as_str(), r.lessons.clone()), ("s2", vec!["Le0".to_string()]));
1186        assert_eq!(r.ms, 4, "dated when the veto was said, the week of its own veto");
1187        let _ = std::fs::remove_dir_all(home);
1188    }
1189
1190    #[test]
1191    fn an_invariant_of_a_veto_becomes_a_proposed_constraint_never_a_constraint() {
1192        let home = temp_home("veto-invariant");
1193        append_jsonl(&home.join("episodes.jsonl"), &veto("no-s1-1", "s1", 1, "no commit log invariant: merge lookups use the review API")).unwrap();
1194        append_jsonl(&home.join("episodes.jsonl"), &veto("no-s1-2", "s1", 2, "not now, later")).unwrap();
1195        learn_in(&home, Some("s1"), &fake_veto_ask, true, 3);
1196        let queue = crate::queen::entries(&home);
1197        assert_eq!(queue.len(), 1, "only the veto that named an invariant");
1198        assert_eq!((queue[0].kind, queue[0].session.as_str()), (crate::queen::Kind::Constraint, "s1"));
1199        assert!(
1200            queue[0].evidence.contains(&"invariant: merge lookups use the review API".to_string())
1201                && queue[0].evidence.contains(&"lesson Lno-s1-1".to_string())
1202        );
1203        assert!(!crate::veto::constraints_path(&home).exists());
1204        // A personal or dropped lesson never proposes; nor does one without an invariant.
1205        let l = Lesson { id: "L".into(), ..Lesson::default() };
1206        assert!(proposes(&crate::reach::Placed::Here(Box::new(l)), "x") && !proposes(&crate::reach::Placed::Dropped, "x"));
1207        assert!(!proposes(&crate::reach::Placed::Private(home.clone()), "x") && !proposes(&crate::reach::Placed::Here(Box::default()), " "));
1208        let _ = std::fs::remove_dir_all(home);
1209    }
1210
1211    #[test]
1212    fn detects_corrections_and_rejections() {
1213        assert!(is_correction("double check against spec document"));
1214        assert!(is_correction("No, let's not move this into the net"));
1215        assert!(!is_correction("let's pick up the gpt live plan"));
1216        let err = "The user doesn't want to proceed with this tool use. The tool use was rejected. the user said:\ncarefully check TOCTOU\nNote: The user's next message may contain a correction";
1217        assert_eq!(rejection(err).as_deref(), Some("carefully check TOCTOU"));
1218        assert_eq!(rejection("Exit code 1\nTraceback"), None);
1219        // omp's refusals carry no words; the reason follows as the next prompt.
1220        assert!(denied("Tool call denied by user: bash"));
1221        assert!(denied("Tool call rejected by user (edit)"));
1222        assert!(denied(
1223            "The user doesn't want to proceed with this tool use. The tool use was rejected (eg. if it was a file edit, the new_string was NOT written to the file). STOP what you are doing and wait for the user to tell you how to proceed."
1224        ));
1225        assert!(!denied(err), "a rejection with words is captured directly");
1226        assert!(!denied("Exit code 1\nTool call denied by user: bash"), "only the harness's own message, not tool output");
1227    }
1228
1229    #[test]
1230    fn known_holds_an_id_the_same_words_or_a_prompt_moments_apart() {
1231        let at = |id: &str, ms: i64, tool: Option<&str>, said: &str| Episode {
1232            id: id.into(),
1233            ms,
1234            session: "s1".into(),
1235            rejected_tool: tool.map(String::from),
1236            correction: said.into(),
1237            ..Episode::default()
1238        };
1239        let mut k = Known::default();
1240        // What the hook recorded: a rejection at capture time, a prompt.
1241        k.add(&at("s1-100000", 100_000, Some("Edit {}"), "use backoff,\n  not a fixed sleep"));
1242        k.add(&at("s1-200000", 200_000, None, "no, the other crate"));
1243        assert!(k.holds(&at("s1-Ttoolu_1", 70_000, Some("Edit {}"), "use backoff, not a fixed sleep")), "same words, whitespace aside");
1244        assert!(k.holds(&at("s1-100000", 1, None, "anything")), "same id");
1245        assert!(k.holds(&at("s1-201500", 201_500, None, "no, the other crate!")), "a prompt within SAME_MS");
1246        assert!(!k.holds(&at("s1-Ttoolu_2", 201_000, Some("Bash {}"), "keep the cache")), "a tool call's episode needs its id or its words");
1247        assert!(!k.holds(&Episode { via: ANSWER.into(), ..at("s1-Ttoolu_3", 200_500, None, "Exponential") }), "nor does an answer");
1248        assert!(!k.holds(&Episode { session: "s2".into(), ..at("s2-Tx", 100_000, Some("Edit {}"), "use backoff, not a fixed sleep") }));
1249        // Two calls the transcript named are two events, though their words match: a second rejection
1250        // with the same words, the same pick to another question.
1251        let mut t = Known::default();
1252        t.add(&at("s1-Ttoolu_4", 300_000, Some("Edit {}"), "keep it in the relay crate"));
1253        t.add(&Episode { via: ANSWER.into(), ..at("s1-Ttoolu_5", 400_000, None, "Create, no parent") });
1254        assert!(!t.holds(&at("s1-Ttoolu_6", 900_000, Some("Edit {}"), "keep it in the relay crate")), "another rejected call");
1255        assert!(!t.holds(&Episode { via: ANSWER.into(), ..at("s1-Ttoolu_7", 900_000, None, "Create, no parent") }), "another question");
1256        // An answer is never matched to a hook's episode by words.
1257        assert!(!k.holds(&Episode { via: ANSWER.into(), ..at("s1-Ttoolu_8", 900_000, None, "no, the other crate") }));
1258        assert_eq!(transcript_id("abcdefghij", "toolu_9", 5), "abcdefgh-Ttoolu_9");
1259        assert_eq!(transcript_id("abcdefghij", "", 5), "abcdefgh-5");
1260
1261        // record_new_in appends once, and once within one batch.
1262        let store = std::env::temp_dir().join(format!("borg-known-{}", std::process::id()));
1263        let _ = std::fs::remove_dir_all(&store);
1264        let e = at("s1-Tq", 5, None, "Exponential");
1265        let mut readied = 0;
1266        assert_eq!(record_new_in(&store, vec![e.clone(), e.clone()], |_| readied += 1).unwrap().len(), 1);
1267        assert!(record_new_in(&store, vec![e.clone()], |_| panic!("a held episode is not readied")).unwrap().is_empty());
1268        assert_eq!(readied, 1);
1269        assert!(Known::read(&store).holds(&e));
1270        assert_eq!(read_jsonl::<Episode>(&store.join("episodes.jsonl")).len(), 1);
1271        let _ = std::fs::remove_dir_all(&store);
1272    }
1273
1274    #[test]
1275    fn writers_at_once_append_an_episode_once() {
1276        let store = std::env::temp_dir().join(format!("borg-known-race-{}", std::process::id()));
1277        let _ = std::fs::remove_dir_all(&store);
1278        let e = Episode { id: "s1-Tx".into(), session: "s1".into(), correction: "keep the cache dir".into(), ..Episode::default() };
1279        let start = std::sync::Barrier::new(8);
1280        std::thread::scope(|s| {
1281            for _ in 0..8 {
1282                s.spawn(|| {
1283                    start.wait();
1284                    record_new_in(&store, vec![e.clone()], |_| std::thread::yield_now()).unwrap();
1285                });
1286            }
1287        });
1288        assert_eq!(read_jsonl::<Episode>(&store.join("episodes.jsonl")).len(), 1);
1289        let _ = std::fs::remove_dir_all(&store);
1290    }
1291
1292    #[test]
1293    fn an_answer_is_asked_as_an_answer_and_counted_as_one() {
1294        let answer = Episode { via: ANSWER.into(), agent: "Which backoff?\n  options: Exponential | Linear".into(), ..ep("a1", "Exponential") };
1295        let p = episode_prompt(&answer, &[]);
1296        assert!(p.contains(ANSWERED) && p.contains("## Engineer's correction\nExponential"), "{p}");
1297        assert!(!episode_prompt(&ep("e1", "no"), &[]).contains(ANSWERED));
1298        let store = std::env::temp_dir().join(format!("borg-answer-count-{}", std::process::id()));
1299        let _ = std::fs::remove_dir_all(&store);
1300        count_correction(&store, &answer);
1301        let ev = crate::stats::live(&store);
1302        assert_eq!((ev.len(), ev[0].why.as_str()), (1, ANSWER));
1303        let _ = std::fs::remove_dir_all(&store);
1304    }
1305
1306    fn ep(id: &str, correction: &str) -> Episode {
1307        Episode {
1308            id: id.into(),
1309            ms: 1,
1310            repo: "r".into(),
1311            session: "s".into(),
1312            task: "plan the relay".into(),
1313            agent: "one ReentrantLock guards delivery".into(),
1314            rejected_tool: None,
1315            correction: correction.into(),
1316            ..Episode::default()
1317        }
1318    }
1319
1320    /// The injected model: `same_as` is the lesson id after "merge:" in the correction, if any, and
1321    /// `supersedes` the one after "supersede:".
1322    fn fake_ask(p: &str) -> Option<Value> {
1323        let correction = p.split("## Engineer's correction\n").nth(1)?.lines().next()?;
1324        let after = |k: &str| correction.split(k).nth(1).and_then(|r| r.split_whitespace().next()).unwrap_or("").to_string();
1325        let supersedes: Vec<String> = Some(after("supersede:")).filter(|x| !x.is_empty()).into_iter().collect();
1326        Some(json!({"is_lesson": true, "question": format!("Q {correction}"), "situations": [], "consequence": "c",
1327            "same_as": after("merge:"), "supersedes": supersedes}))
1328    }
1329
1330    fn results(home: &Path) -> Vec<(String, String, String)> {
1331        outcome::read(&home.join("outcomes.jsonl")).into_iter().filter(|o| o.kind != outcome::ASKED).map(|o| (o.kind, o.session, o.lessons.join(","))).collect()
1332    }
1333
1334    #[test]
1335    fn every_model_call_is_reported_with_its_place_and_verdict() {
1336        let home = temp_home("progress");
1337        for e in [at("e0", "s0", 1, "ann@x", "use the lock on the flag"), at("e1", "s0", 2, "bob@x", "the lock again merge:Le0")] {
1338            append_jsonl(&home.join("episodes.jsonl"), &e).unwrap();
1339        }
1340        let mut steps = Vec::new();
1341        learn_in_reporting(&home, None, &fake_ask, false, 3, &mut |st| steps.push((st.done, st.total, st.verdict.split(':').next().unwrap().to_string())));
1342        assert_eq!(steps, [(1, 2, "new lesson Le0".to_string()), (2, 2, "merged into Le0".to_string())]);
1343        // A second run has nothing left to ask, so it reports nothing.
1344        steps.clear();
1345        learn_in_reporting(&home, None, &fake_ask, false, 4, &mut |st| steps.push((st.done, st.total, String::new())));
1346        assert!(steps.is_empty());
1347        let _ = std::fs::remove_dir_all(home);
1348    }
1349
1350    #[test]
1351    fn a_lesson_contradicted_after_asking_is_corrected_not_followed() {
1352        let home = temp_home("contradicted");
1353        append_jsonl(&home.join("episodes.jsonl"), &at("e0", "s0", 1, "ann@x", "use the lock on the flag")).unwrap();
1354        learn_in(&home, Some("s0"), &fake_ask, true, 2);
1355        // s1 asks Le0, then the engineer contradicts it: a new lesson superseding Le0.
1356        asked(&home, "s1", 10, &["Le0"]);
1357        append_jsonl(&home.join("episodes.jsonl"), &at("e1", "s1", 20, "bob@x", "no, CAS on the flag supersede:Le0")).unwrap();
1358        assert_eq!(learn_in(&home, Some("s1"), &fake_ask, true, 30), (1, 0, 0));
1359        assert_eq!(results(&home), [(outcome::CORRECTED.into(), "s1".into(), "Le0".into())]);
1360        let _ = std::fs::remove_dir_all(home);
1361    }
1362
1363    #[test]
1364    fn a_correction_of_a_lesson_that_applied_but_was_not_asked_is_a_miss() {
1365        let home = temp_home("missed");
1366        for e in [at("e0", "s0", 1, "ann@x", "use the lock on the flag"), at("e1", "s0", 2, "bob@x", "retry with jitter")] {
1367            append_jsonl(&home.join("episodes.jsonl"), &e).unwrap();
1368        }
1369        learn_in(&home, Some("s0"), &fake_ask, true, 3);
1370        // s1's check graded both as applying and chose to ask only Le1; the engineer then corrects the
1371        // drone on Le0's concern: Le0 was needed and not asked.
1372        let graded = outcome::Outcome {
1373            kind: outcome::GRADED.into(),
1374            session: "s1".into(),
1375            lessons: vec!["Le0".into(), "Le1".into()],
1376            ms: 10,
1377            repo: "r".into(),
1378            grades: vec![2, 3],
1379            ranks: vec![0, 1],
1380            applied: 2,
1381            chosen: vec!["Le1".into()],
1382            ..Default::default()
1383        };
1384        outcome::append(&home.join("outcomes.jsonl"), &graded).unwrap();
1385        asked(&home, "s1", 11, &["Le1"]);
1386        append_jsonl(&home.join("episodes.jsonl"), &at("e2", "s1", 20, "cy@x", "no, the lock on the flag merge:Le0")).unwrap();
1387        assert_eq!(learn_in(&home, Some("s1"), &fake_ask, true, 30), (0, 1, 0));
1388        let r = results(&home);
1389        assert!(r.contains(&(outcome::MISSED.into(), "s1".into(), "Le0".into())), "{r:?}");
1390        // The asked one is not a miss; a second run records nothing new.
1391        assert!(!r.iter().any(|(k, _, l)| k == outcome::MISSED && l == "Le1"), "{r:?}");
1392        learn_in(&home, Some("s1"), &fake_ask, true, 40);
1393        assert_eq!(results(&home).iter().filter(|(k, ..)| k == outcome::MISSED).count(), 1);
1394        let _ = std::fs::remove_dir_all(home);
1395    }
1396
1397    #[test]
1398    fn siding_with_one_side_of_a_disagreement_corrects_the_other() {
1399        let home = temp_home("sides");
1400        for e in [at("e0", "s0", 1, "ann@x", "use the lock on the flag"), at("e1", "s0", 2, "bob@x", "use CAS on the flag")] {
1401            append_jsonl(&home.join("episodes.jsonl"), &e).unwrap();
1402        }
1403        learn_in(&home, Some("s0"), &fake_ask, true, 3);
1404        // The conscience rendered Le0 and Le1 as one disagreement; the engineer sides with Le0.
1405        let o = outcome::Outcome {
1406            kind: outcome::ASKED.into(),
1407            session: "s1".into(),
1408            lessons: vec!["Le0".into(), "Le1".into()],
1409            ms: 10,
1410            repo: "r".into(),
1411            groups: vec![vec!["Le0".into(), "Le1".into()]],
1412            ..Default::default()
1413        };
1414        outcome::append(&home.join("outcomes.jsonl"), &o).unwrap();
1415        append_jsonl(&home.join("episodes.jsonl"), &at("e2", "s1", 20, "cy@x", "no, the lock on the flag merge:Le0")).unwrap();
1416        assert_eq!(learn_in(&home, Some("s1"), &fake_ask, true, 30), (0, 1, 0));
1417        assert_eq!(results(&home), [(outcome::CORRECTED.into(), "s1".into(), "Le1".into()), (outcome::NOT_CORRECTED.into(), "s1".into(), "Le0".into())]);
1418        // cy joined Le0 at the correction; ann's earlier evidence is not his.
1419        let l0 = lessons(&home.join("lessons.jsonl")).into_iter().find(|l| l.id == "Le0").unwrap();
1420        assert_eq!(l0.joined.get("cy@x"), Some(&20));
1421        let _ = std::fs::remove_dir_all(home);
1422    }
1423
1424    #[test]
1425    fn the_learn_prompt_offers_at_most_merge_candidates_lessons_for_merging() {
1426        let lib: Vec<Lesson> = (0..MERGE_CANDIDATES + 2)
1427            .map(|i| Lesson {
1428                id: format!("L{i}"),
1429                origin_ms: i as i64,
1430                question: format!("Is the ReentrantLock held while delivery {i} runs?"),
1431                consequence: "c".into(),
1432                version: 1,
1433                ..Lesson::default()
1434            })
1435            .collect();
1436        let e = ep("e1", "no, the ReentrantLock must guard delivery");
1437        let offered = similar(&e, &lib);
1438        assert_eq!(offered.len(), MERGE_CANDIDATES, "every lesson matches; the prompt offers the best few");
1439        let prompt = std::cell::RefCell::new(String::new());
1440        let _ = generalise(&e, &lib, &|p| {
1441            *prompt.borrow_mut() = p.to_string();
1442            None
1443        });
1444        let named = lib.iter().filter(|l| prompt.borrow().contains(&format!("[{}]", l.id))).count();
1445        assert_eq!(named, MERGE_CANDIDATES, "{}", prompt.borrow());
1446    }
1447
1448    #[test]
1449    fn a_superseded_lesson_is_not_a_merge_target_its_successor_is_shown() {
1450        let lesson = |id: &str, ms: i64, q: &str, sup: &[&str]| Lesson {
1451            id: id.into(),
1452            origin_ms: ms,
1453            repo: "r".into(),
1454            question: q.into(),
1455            situations: vec![],
1456            consequence: "c".into(),
1457            support: 1,
1458            episodes: vec![],
1459            history: vec![],
1460            version: 1,
1461            engineers: vec!["ann@x".into()],
1462            joined: Default::default(),
1463            area: "concurrency".into(),
1464            supersedes: sup.iter().map(|s| s.to_string()).collect(),
1465            source: String::new(),
1466            scope: vec![],
1467            overrides: String::new(),
1468            origin: String::new(),
1469            facets: vec![],
1470            reach: String::new(),
1471        };
1472        let lib = [lesson("L0", 1, "Is the closed flag guarded by the ReentrantLock?", &[]), lesson("L1", 2, "Would a CAS suffice?", &["L0"])];
1473        // ann reverts to the lock: the model only sees L1 (in L0's place), contradicts it, and the
1474        // restored view becomes a new lesson that supersedes L1 and is asked again.
1475        let shown = std::cell::RefCell::new(String::new());
1476        let back = generalise(&ep("e9", "no, guard the closed flag with the ReentrantLock"), &lib, &|p| {
1477            *shown.borrow_mut() = p.to_string();
1478            Some(json!({"is_lesson": true, "question": "Is the closed flag guarded by the ReentrantLock?", "situations": [],
1479                "consequence": "c", "same_as": "L0", "area": "concurrency", "supersedes": ["L1"]}))
1480        })
1481        .unwrap()
1482        .unwrap();
1483        let shown = shown.into_inner();
1484        assert!(shown.contains("[L1]") && !shown.contains("[L0]"), "{shown}");
1485        assert_eq!((back.version, back.supersedes.clone()), (1, vec!["L1".to_string()]), "same_as on a hidden lesson is no merge");
1486        let all: Vec<Challenge> = lib
1487            .iter()
1488            .chain([&back])
1489            .map(|l| Challenge {
1490                id: l.id.clone(),
1491                origin_ms: l.origin_ms,
1492                question: l.question.clone(),
1493                engineers: l.engineers.clone(),
1494                supersedes: l.supersedes.clone(),
1495                ..Default::default()
1496            })
1497            .collect();
1498        assert_eq!(trust::successors(&all).get("L0").map(String::as_str), Some(back.id.as_str()));
1499    }
1500
1501    fn temp_home(name: &str) -> PathBuf {
1502        let d = std::env::temp_dir().join(format!("borg-learn-{name}-{}", std::process::id()));
1503        let _ = std::fs::remove_dir_all(&d);
1504        std::fs::create_dir_all(&d).unwrap();
1505        d
1506    }
1507
1508    fn at(id: &str, session: &str, ms: i64, engineer: &str, correction: &str) -> Episode {
1509        Episode { ms, session: session.into(), engineer: engineer.into(), ..ep(id, correction) }
1510    }
1511
1512    fn asked(home: &Path, session: &str, ms: i64, ids: &[&str]) {
1513        let o = outcome::Outcome {
1514            kind: outcome::ASKED.into(),
1515            session: session.into(),
1516            lessons: ids.iter().map(|i| i.to_string()).collect(),
1517            ms,
1518            repo: "r".into(),
1519            ..Default::default()
1520        };
1521        outcome::append(&home.join("outcomes.jsonl"), &o).unwrap();
1522    }
1523
1524    fn regret_at(id: &str, decision_ms: i64) -> Episode {
1525        let r = regret::Regret { decision_session: "s-dec".into(), decision_ms, signals: vec!["subject".into()], ..regret::Regret::default() };
1526        Episode { kind: regret::KIND.into(), regret: Some(r), ..at(id, "s-fix", 100, "bob@x", "Fixed later (3d, subject): fix: take the lock") }
1527    }
1528
1529    /// The model's verdict on a regret: a lesson, or (`lesson` false) not one.
1530    fn regret_ask(lesson: bool) -> impl Fn(&str) -> Option<Value> {
1531        move |_| {
1532            Some(json!({"is_lesson": lesson, "question": "Before reading a shared flag, has a writer been considered?", "situations": [],
1533            "consequence": "a race", "same_as": "", "supersedes": []}))
1534        }
1535    }
1536
1537    #[test]
1538    fn a_settled_lesson_says_why_the_judge_gave_it_no_verdict() {
1539        let home = temp_home("unjudged");
1540        asked(&home, "s1", 10, &["A", "B", "C", "D"]);
1541        let ignored =
1542            outcome::Outcome { kind: outcome::IGNORED.into(), session: "s1".into(), lessons: vec!["A".into()], ms: 12, repo: "r".into(), ..Default::default() };
1543        outcome::append(&home.join("outcomes.jsonl"), &ignored).unwrap();
1544        std::fs::create_dir_all(home.join("state")).unwrap();
1545        let ctx = "#borg-asked A,B,C,D @0123456789ab\n#borg-judged B=pending\n#borg-judged B=pending\n#borg-unjudged C why=slots\n";
1546        std::fs::write(home.join("state/s1.ctx"), ctx).unwrap();
1547        learn_in(&home, Some("s1"), &fake_ask, true, 100);
1548        let why: Vec<(String, String)> = outcome::read(&home.join("outcomes.jsonl"))
1549            .into_iter()
1550            .filter(|o| o.kind == outcome::NOT_CORRECTED)
1551            .map(|o| (o.lessons.join(","), o.reason))
1552            .collect();
1553        let want = [("A", outcome::UNJUDGED_NONE), ("B", outcome::UNJUDGED_TRIES), ("C", outcome::UNJUDGED_SLOTS), ("D", outcome::UNJUDGED_NO_CALL)];
1554        assert_eq!(why, want.map(|(l, r)| (l.to_string(), r.to_string())));
1555        let _ = std::fs::remove_dir_all(home);
1556    }
1557
1558    /// Settle waits for a session's end: idle by the log (no ask or episode for a day) is not enough while
1559    /// its state file is still being written.
1560    #[test]
1561    fn an_idle_session_still_working_is_not_settled() {
1562        let home = temp_home("open");
1563        asked(&home, "s1", 10, &["A"]);
1564        std::fs::create_dir_all(home.join("state")).unwrap();
1565        std::fs::write(home.join("state/s1.ctx"), "a prompt just now\n").unwrap();
1566        let now = now_ms();
1567        learn_in(&home, None, &fake_ask, true, now);
1568        assert!(results(&home).is_empty(), "still open: {:?}", results(&home));
1569        // A day after its last line, it ended.
1570        learn_in(&home, None, &fake_ask, true, now + outcome::IDLE_MS + 60_000);
1571        assert_eq!(results(&home), [(outcome::NOT_CORRECTED.into(), "s1".into(), "A".into())]);
1572        let _ = std::fs::remove_dir_all(home);
1573    }
1574
1575    /// A regret is a correction of the deciding session's work too: the lesson it merges into, graded
1576    /// applying in that session before the decision and never asked there, was a question needed and not
1577    /// asked.
1578    #[test]
1579    fn a_regret_merged_into_a_lesson_graded_but_not_asked_before_the_decision_is_a_miss() {
1580        let home = temp_home("regret-miss");
1581        append_jsonl(&home.join("episodes.jsonl"), &at("e0", "s0", 1, "ann@x", "use the lock on the flag")).unwrap();
1582        learn_in(&home, Some("s0"), &fake_ask, true, 2);
1583        let graded = |ms: i64| outcome::Outcome {
1584            kind: outcome::GRADED.into(),
1585            session: "s-dec".into(),
1586            lessons: vec!["Le0".into()],
1587            ms,
1588            repo: "r".into(),
1589            grades: vec![2],
1590            ..Default::default()
1591        };
1592        for g in [graded(10), graded(30)] {
1593            outcome::append(&home.join("outcomes.jsonl"), &g).unwrap();
1594        }
1595        append_jsonl(&home.join("episodes.jsonl"), &regret_at("R1", 20)).unwrap();
1596        let merge = |_: &str| Some(json!({"is_lesson": true, "question": "Q", "situations": [], "consequence": "c", "same_as": "Le0", "supersedes": []}));
1597        learn_in(&home, None, &merge, true, 200);
1598        let missed: Vec<(String, String, i64, String)> = outcome::read(&home.join("outcomes.jsonl"))
1599            .into_iter()
1600            .filter(|o| o.kind == outcome::MISSED)
1601            .map(|o| (o.lessons.join(","), o.session, o.ms, o.cause))
1602            .collect();
1603        assert_eq!(missed, [("Le0".to_string(), "s-dec".to_string(), 20, "R1".to_string())]);
1604        learn_in(&home, None, &merge, true, 300);
1605        assert_eq!(outcome::read(&home.join("outcomes.jsonl")).iter().filter(|o| o.kind == outcome::MISSED).count(), 1, "once");
1606        let _ = std::fs::remove_dir_all(home);
1607    }
1608
1609    #[test]
1610    fn a_regret_blames_lessons_asked_before_the_decision_once() {
1611        let regretted = |home: &Path| -> Vec<(String, String, f64, String)> {
1612            outcome::read(&home.join("outcomes.jsonl"))
1613                .into_iter()
1614                .filter(|o| o.kind == outcome::REGRETTED)
1615                .map(|o| (o.lessons.join(","), o.session, o.credit, o.cause))
1616                .collect()
1617        };
1618        let setup = |name: &str, e: Episode| {
1619            let home = temp_home(name);
1620            asked(&home, "s-dec", 10, &["Le0", "Le1"]);
1621            asked(&home, "s-dec", 50, &["Le2"]);
1622            append_jsonl(&home.join("episodes.jsonl"), &e).unwrap();
1623            home
1624        };
1625        let home = setup("regret", regret_at("R1", 20));
1626        assert_eq!(learn_in(&home, None, &regret_ask(true), true, 200), (1, 0, 0));
1627        let got = regretted(&home);
1628        assert_eq!(
1629            got,
1630            [("Le0".into(), "s-dec".into(), 0.25, "R1".into()), ("Le1".into(), "s-dec".into(), 0.25, "R1".into())],
1631            "Le2 was asked after the decision"
1632        );
1633        let before = std::fs::read_to_string(home.join("outcomes.jsonl")).unwrap();
1634        learn_in(&home, None, &regret_ask(true), true, 300);
1635        assert_eq!(std::fs::read_to_string(home.join("outcomes.jsonl")).unwrap(), before, "credited once");
1636        let _ = std::fs::remove_dir_all(home);
1637        // Not a lesson, no decision time, not tracking: nothing is blamed.
1638        let home = setup("regret-no", regret_at("R1", 20));
1639        learn_in(&home, None, &regret_ask(false), true, 200);
1640        assert!(regretted(&home).is_empty(), "not a lesson");
1641        let _ = std::fs::remove_dir_all(home);
1642        let home = setup("regret-old", regret_at("R1", 0));
1643        learn_in(&home, None, &regret_ask(true), true, 200);
1644        assert!(regretted(&home).is_empty(), "a regret from before decision times were kept");
1645        let _ = std::fs::remove_dir_all(home);
1646        let home = setup("regret-untracked", regret_at("R1", 20));
1647        learn_in(&home, None, &regret_ask(true), false, 200);
1648        assert!(regretted(&home).is_empty(), "BORG_LEARN=off writes nothing");
1649        let _ = std::fs::remove_dir_all(home);
1650    }
1651
1652    #[test]
1653    fn records_outcomes_of_asked_lessons() {
1654        let home = temp_home("outcomes");
1655        // Session s0 teaches lessons Le0 (TOCTOU) and Le1 (spec).
1656        for e in [at("e0", "s0", 1, "ann@x", "check TOCTOU on the flag"), at("e1", "s0", 2, "", "check against the spec")] {
1657            append_jsonl(&home.join("episodes.jsonl"), &e).unwrap();
1658        }
1659        assert_eq!(learn_in(&home, Some("s0"), &fake_ask, true, 3), (2, 0, 0));
1660        assert!(outcome::read(&home.join("outcomes.jsonl")).is_empty(), "nothing asked, nothing settled");
1661        // s1: both asked at 10; the TOCTOU concern is corrected again at 20 (merged into Le0).
1662        // Le1 asked again after that correction, which must not count as corrected.
1663        asked(&home, "s1", 10, &["Le0", "Le1"]);
1664        append_jsonl(&home.join("episodes.jsonl"), &at("e2", "s1", 20, "bob@x", "flag still racy merge:Le0")).unwrap();
1665        asked(&home, "s1", 30, &["Le1"]);
1666        assert_eq!(learn_in(&home, Some("s1"), &fake_ask, true, 40), (0, 1, 0));
1667        let result: Vec<(String, String, i64)> =
1668            outcome::read(&home.join("outcomes.jsonl")).into_iter().filter(|o| o.kind != outcome::ASKED).map(|o| (o.kind, o.lessons.join(","), o.ms)).collect();
1669        assert_eq!(result, [(outcome::CORRECTED.into(), "Le0".into(), 20), (outcome::NOT_CORRECTED.into(), "Le1".into(), 40)]);
1670        // Re-running learn for the same session appends nothing: deterministic and idempotent.
1671        let before = std::fs::read_to_string(home.join("outcomes.jsonl")).unwrap();
1672        learn_in(&home, Some("s1"), &fake_ask, true, 50);
1673        assert_eq!(std::fs::read_to_string(home.join("outcomes.jsonl")).unwrap(), before);
1674        // Engineers accumulate on the merged lesson.
1675        let l0 = lessons(&home.join("lessons.jsonl")).into_iter().find(|l| l.id == "Le0").unwrap();
1676        assert_eq!(l0.engineers, ["ann@x", "bob@x"]);
1677        // The same email in another case is the same engineer: not added, no join recorded.
1678        append_jsonl(&home.join("episodes.jsonl"), &at("e3", "s2", 60, "Ann@X", "flag racy again merge:Le0")).unwrap();
1679        assert_eq!(learn_in(&home, Some("s2"), &fake_ask, true, 70), (0, 1, 0));
1680        let l0 = lessons(&home.join("lessons.jsonl")).into_iter().find(|l| l.id == "Le0").unwrap();
1681        assert_eq!((l0.engineers.as_slice(), l0.joined.get("Ann@X")), (&["ann@x".to_string(), "bob@x".to_string()][..], None));
1682        let _ = std::fs::remove_dir_all(home);
1683    }
1684
1685    #[test]
1686    fn idle_sessions_settle_without_their_end_hook() {
1687        let home = temp_home("idle");
1688        for e in [at("e0", "s0", 1, "", "check TOCTOU on the flag"), at("e1", "s0", 2, "", "check against the spec")] {
1689            append_jsonl(&home.join("episodes.jsonl"), &e).unwrap();
1690        }
1691        learn_in(&home, Some("s0"), &fake_ask, true, 3);
1692        // s1 and s3 never send `end`. s1 asks both and is corrected on Le0; s3 asks Le0, no correction.
1693        asked(&home, "s1", 10, &["Le0", "Le1"]);
1694        append_jsonl(&home.join("episodes.jsonl"), &at("e2", "s1", 20, "", "flag still racy merge:Le0")).unwrap();
1695        asked(&home, "s3", 5, &["Le0"]);
1696        let idle = 20 + outcome::IDLE_MS;
1697        let results = || -> Vec<(String, String, String)> {
1698            outcome::read(&home.join("outcomes.jsonl"))
1699                .into_iter()
1700                .filter(|o| o.kind != outcome::ASKED)
1701                .map(|o| (o.kind, o.session, o.lessons.join(",")))
1702                .collect()
1703        };
1704        let not = |s: &str, l: &str| (outcome::NOT_CORRECTED.to_string(), s.to_string(), l.to_string());
1705        // s2 ends a day later: its own ask settles, and idle s3; s1 not: it is still open (its state file
1706        // is fresh), so its episode is still pending.
1707        let ctx = home.join("state/s1.ctx");
1708        std::fs::create_dir_all(ctx.parent().unwrap()).unwrap();
1709        std::fs::write(&ctx, "").unwrap();
1710        asked(&home, "s2", idle - 1, &["Le1"]);
1711        learn_in(&home, Some("s2"), &fake_ask, true, idle);
1712        assert_eq!(results(), [not("s3", "Le0"), not("s2", "Le1")]);
1713        // s1 goes quiet; a plain `borg learn` folds e2 in and settles s1 too, so s1 gives both kinds of outcome.
1714        std::fs::remove_file(&ctx).unwrap();
1715        assert_eq!(learn_in(&home, None, &fake_ask, true, idle), (0, 1, 0));
1716        let corrected = (outcome::CORRECTED.to_string(), "s1".to_string(), "Le0".to_string());
1717        assert_eq!(results(), [not("s3", "Le0"), not("s2", "Le1"), corrected.clone(), not("s1", "Le1")]);
1718        // A fresh session is not settled, and re-running appends nothing.
1719        asked(&home, "s4", idle, &["Le1"]);
1720        learn_in(&home, None, &fake_ask, true, idle + 1);
1721        assert_eq!(results().len(), 4);
1722        let _ = std::fs::remove_dir_all(home);
1723    }
1724
1725    #[test]
1726    fn a_quiet_sessions_episodes_are_learned_by_another_sessions_end() {
1727        let home = temp_home("quiet");
1728        // s1 never ends; the transcript sweep added its last correction after it went quiet (sweep.rs `idle`).
1729        append_jsonl(&home.join("episodes.jsonl"), &at("e0", "s1", 10, "", "check TOCTOU on the flag")).unwrap();
1730        // s9 is busy (an episode an hour ago) and open; s2 ends now.
1731        append_jsonl(&home.join("episodes.jsonl"), &at("e1", "s9", 10 + outcome::IDLE_MS, "", "check against the spec")).unwrap();
1732        append_jsonl(&home.join("episodes.jsonl"), &at("e2", "s2", 20 + outcome::IDLE_MS, "", "no, keep the cache")).unwrap();
1733        let now = 30 + outcome::IDLE_MS;
1734        // While s1's state file is fresh it is open, and waits for its own end.
1735        let ctx = home.join("state/s1.ctx");
1736        std::fs::create_dir_all(ctx.parent().unwrap()).unwrap();
1737        std::fs::write(&ctx, "").unwrap();
1738        assert_eq!(learn_in(&home, Some("s2"), &fake_ask, true, now), (1, 0, 0), "s2's own episode only");
1739        // Quiet: s2's end learns it too; busy s9 still waits.
1740        std::fs::remove_file(&ctx).unwrap();
1741        append_jsonl(&home.join("episodes.jsonl"), &at("e3", "s3", 25 + outcome::IDLE_MS, "", "no, the other crate")).unwrap();
1742        assert_eq!(learn_in(&home, Some("s3"), &fake_ask, true, now), (2, 0, 0), "s3's own and quiet s1's");
1743        let learned: HashSet<String> = lessons(&home.join("lessons.jsonl")).into_iter().flat_map(|l| l.episodes).collect();
1744        assert!(learned.contains("e0") && !learned.contains("e1"), "{learned:?}");
1745        let _ = std::fs::remove_dir_all(home);
1746    }
1747
1748    #[test]
1749    fn no_outcomes_written_without_tracking() {
1750        let home = temp_home("off");
1751        append_jsonl(&home.join("episodes.jsonl"), &at("e0", "s0", 1, "", "check TOCTOU")).unwrap();
1752        learn_in(&home, Some("s0"), &fake_ask, true, 2);
1753        asked(&home, "s1", 10, &["Le0"]);
1754        let before = std::fs::read_to_string(home.join("outcomes.jsonl")).unwrap();
1755        append_jsonl(&home.join("episodes.jsonl"), &at("e1", "s1", 20, "", "TOCTOU again merge:Le0")).unwrap();
1756        assert_eq!(learn_in(&home, Some("s1"), &fake_ask, false, 30), (0, 1, 0));
1757        assert_eq!(std::fs::read_to_string(home.join("outcomes.jsonl")).unwrap(), before);
1758        let _ = std::fs::remove_dir_all(home);
1759    }
1760
1761    #[test]
1762    fn failed_model_calls_leave_episodes_waiting_and_stop_early() {
1763        let home = temp_home("failed");
1764        for i in 0..5 {
1765            append_jsonl(&home.join("episodes.jsonl"), &at(&format!("e{i}"), "s0", i, "", &format!("check case {i}"))).unwrap();
1766        }
1767        asked(&home, "s0", 0, &["Lx"]);
1768        let calls = std::cell::Cell::new(0);
1769        let down = |_: &str| -> Option<Value> {
1770            calls.set(calls.get() + 1);
1771            None
1772        };
1773        // Quota hit: nothing is marked "not a lesson", the run gives up after MAX_FAILURES calls,
1774        // and the session is not settled while its episodes wait.
1775        assert_eq!(learn_in(&home, Some("s0"), &down, true, 1 + outcome::IDLE_MS), (0, 0, 5));
1776        assert_eq!(calls.get(), MAX_FAILURES);
1777        assert!(!home.join("episodes-not-lessons.txt").exists());
1778        assert!(outcome::read(&home.join("outcomes.jsonl")).iter().all(|o| o.kind == outcome::ASKED));
1779        // The model is back: the next run retries every episode.
1780        assert_eq!(learn_in(&home, Some("s0"), &fake_ask, true, 2 + outcome::IDLE_MS), (5, 0, 0));
1781        let _ = std::fs::remove_dir_all(home);
1782    }
1783
1784    #[test]
1785    fn area_and_supersedes_come_from_the_same_call() {
1786        let lib = vec![
1787            generalise(&ep("e1", "use a lock here"), &[], &|_| {
1788                Some(json!({"is_lesson": true, "question": "Is the flag guarded by a lock?", "situations": ["flag across threads"],
1789                    "consequence": "race", "same_as": "", "area": "Concurrency", "supersedes": ["Lnope"]}))
1790            })
1791            .unwrap()
1792            .unwrap(),
1793        ];
1794        assert_eq!(lib[0].area, "concurrency", "case-insensitive; normalised");
1795        assert!(lib[0].supersedes.is_empty(), "an id the model was not shown is dropped");
1796        // A lesson without an area (older store, or a model that ignores it) is "other".
1797        let old: Lesson = serde_json::from_str(
1798            r#"{"id":"L","origin_ms":1,"repo":"r","question":"q","situations":[],"consequence":"c",
1799            "support":1,"episodes":[],"history":[],"version":1}"#,
1800        )
1801        .unwrap();
1802        assert_eq!((old.area.as_str(), old.supersedes.len()), ("other", 0));
1803        let id = lib[0].id.clone();
1804        let reversed = generalise(&ep("e2", "no, the lock is wrong here, use the atomic flag"), &lib, &move |_| {
1805            Some(json!({"is_lesson": true, "question": "Is a lock-free atomic enough for the flag?", "situations": ["flag across threads"],
1806                "consequence": "lock contention", "same_as": "", "area": "made-up", "supersedes": [id.clone()]}))
1807        })
1808        .unwrap()
1809        .unwrap();
1810        assert_eq!((reversed.area.as_str(), reversed.supersedes.clone()), ("other", vec![lib[0].id.clone()]));
1811        assert!(!serde_json::to_string(&lib[0]).unwrap().contains("supersedes"), "empty supersedes stays off the line");
1812        // The merge target is never superseded; a merge keeps what the old version superseded.
1813        let (rid, lid) = (reversed.id.clone(), lib[0].id.clone());
1814        let merged = generalise(&ep("e3", "again: atomic, not a lock"), &[lib[0].clone(), reversed.clone()], &move |_| {
1815            Some(json!({"is_lesson": true, "question": "Is a lock-free atomic enough?", "situations": ["flag across threads"],
1816                "consequence": "lock contention", "same_as": rid.clone(), "area": "concurrency", "supersedes": [rid.clone(), lid.clone()]}))
1817        })
1818        .unwrap()
1819        .unwrap();
1820        assert_eq!((merged.id.as_str(), merged.version, merged.supersedes.clone()), (reversed.id.as_str(), 2, vec![lib[0].id.clone()]));
1821    }
1822
1823    #[test]
1824    fn new_lesson_then_merge_keeps_history() {
1825        let first = generalise(&ep("e1", "carefully check TOCTOU on the atomic flags"), &[], &|_| {
1826            Some(json!({"is_lesson": true, "question": "Is every check-then-act on a shared flag TOCTOU-safe?",
1827                "situations": ["check-then-act on a shared mutable flag across threads"], "consequence": "race", "same_as": ""}))
1828        })
1829        .unwrap()
1830        .unwrap();
1831        assert_eq!((first.version, first.support), (1, 1));
1832        let id = first.id.clone();
1833        let merged = generalise(&ep("e2", "again: the closed flag check is racy"), std::slice::from_ref(&first), &move |p| {
1834            assert!(p.contains(&id), "the similar lesson must be offered for merging");
1835            Some(json!({"is_lesson": true, "question": "Is each check-then-act on shared flags atomic or harmless on race?",
1836                "situations": ["check-then-act on a shared mutable flag across threads"], "consequence": "race", "same_as": id}))
1837        })
1838        .unwrap()
1839        .unwrap();
1840        assert_eq!((merged.id.as_str(), merged.version, merged.support, merged.history.len()), (first.id.as_str(), 2, 2, 1));
1841        let skipped =
1842            generalise(&ep("e3", "stop"), &[], &|_| Some(json!({"is_lesson": false, "question": "", "situations": [], "consequence": "", "same_as": ""})));
1843        assert!(matches!(skipped, Ok(None)), "a verdict: not a lesson");
1844        assert_eq!(generalise(&ep("e4", "no, the lock"), &[], &|_| None).map(|_| ()), Err(AskFailed), "no answer is no verdict");
1845        let malformed = generalise(&ep("e5", "no, the lock"), &[], &|_| Some(json!({"error": "rate limited"})));
1846        assert_eq!(malformed.map(|_| ()), Err(AskFailed));
1847    }
1848}