use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use serde_json::Value;
use crate::collective::{self, Config, Route};
use crate::harness::{self, Harness as _, codex, opencode};
use crate::learn::{self, Episode, Lesson};
use crate::runner;
pub const FLAGS: &[&str] = &[
"--apply",
"--since",
"--limit",
"--claude",
"--omp",
"--opencode",
"--codex",
"--session",
"--from-dir",
"--plan",
"--for",
"--collective",
"--knowledge",
"--retros",
"--families",
"--decisions",
];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Source {
Claude,
Omp,
Opencode,
Codex,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OpencodeFrom {
Cli,
Dir(PathBuf),
}
#[derive(Debug, Clone)]
pub struct Found {
pub cwd: String,
pub task_cwd: String,
pub agent_cwd: String,
pub tool_cwd: String,
pub episode: Episode,
}
impl AsMut<Episode> for Found {
fn as_mut(&mut self) -> &mut Episode {
&mut self.episode
}
}
pub struct Options {
pub claude: Option<PathBuf>,
pub omp: Option<PathBuf>,
pub opencode: Option<OpencodeFrom>,
pub codex: Option<PathBuf>,
pub session: Option<String>,
pub since: Option<i64>,
pub limit: Option<usize>,
pub apply: bool,
pub skip: Vec<String>,
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Tally {
pub home: PathBuf,
pub may_sync: bool,
pub sessions: usize,
pub episodes: usize,
pub known: usize,
pub calls: usize,
pub new_lessons: usize,
pub merged: usize,
pub pending: usize,
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Kinds {
pub rejections: usize,
pub answers: usize,
pub prompts: usize,
pub known: usize,
pub version: String,
}
#[derive(Debug, Default)]
pub struct Report {
pub scanned: usize,
pub claude: usize,
pub omp: usize,
pub opencode: Option<usize>,
pub codex: usize,
pub collectives: BTreeMap<String, Tally>,
pub off_sessions: usize,
pub off_episodes: usize,
pub harnesses: BTreeMap<String, Kinds>,
pub usd_per_call: Option<(f64, usize)>,
}
const NOTICE: &str = "want to proceed with this tool use";
const NOTICE_CHARS: usize = 200;
fn worded_rejection(text: &str) -> Option<String> {
let first: String = take(text.trim_start().lines().next().unwrap_or_default(), NOTICE_CHARS);
learn::rejection(text).filter(|_| first.contains(NOTICE))
}
fn human(t: &str) -> bool {
t.len() > 3 && !t.starts_with('<') && !t.contains("[Request interrupted")
}
fn take(s: &str, n: usize) -> String {
s.chars().take(n).collect()
}
fn tail(s: &str, n: usize) -> String {
let c: Vec<char> = s.chars().collect();
c[c.len().saturating_sub(n)..].iter().collect()
}
fn text_of(v: &Value) -> String {
match v {
Value::String(s) => s.clone(),
Value::Array(a) => a
.iter()
.filter(|b| b.get("type").is_none_or(|t| t == "text"))
.filter_map(|b| b.get("text").and_then(Value::as_str).or(b.as_str()))
.collect::<Vec<_>>()
.join("\n"),
_ => String::new(),
}
}
type Refused = (String, String, String);
#[derive(Debug, Default, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Carry {
#[serde(default)]
pub cwd: String,
#[serde(default)]
pub version: String,
#[serde(default)]
pub task: (String, String),
#[serde(default)]
pub said: (String, String),
#[serde(default)]
pub denied: Option<Refused>,
#[serde(default)]
pub calls: Vec<(String, Value)>,
}
const OPEN_CALLS: usize = 64;
#[derive(Default)]
struct Replay {
session: String,
cwd: String,
harness: &'static str,
version: String,
answers_only: bool,
task: (String, String),
said: (String, String),
denied: Option<Refused>,
out: Vec<Found>,
cwds: BTreeSet<String>,
calls: Vec<(String, Value)>,
}
impl Replay {
fn resume(session: &str, harness: &'static str, c: Carry) -> Replay {
let mut r = Replay {
session: session.to_string(),
harness,
version: c.version,
task: c.task,
said: c.said,
denied: c.denied,
calls: c.calls,
..Default::default()
};
if !c.cwd.is_empty() {
r.at(&c.cwd);
}
r
}
fn carry(&self) -> Carry {
let skip = self.calls.len().saturating_sub(OPEN_CALLS);
Carry {
cwd: self.cwd.clone(),
version: self.version.clone(),
task: self.task.clone(),
said: self.said.clone(),
denied: self.denied.clone(),
calls: self.calls[skip..].to_vec(),
}
}
fn call(&mut self, id: &str, what: Value) {
self.calls.retain(|(c, _)| c != id);
self.calls.push((id.to_string(), what));
}
fn called(&mut self, id: &str) -> Value {
match self.calls.iter().position(|(c, _)| c == id) {
Some(i) => self.calls.remove(i).1,
None => Value::Null,
}
}
fn at(&mut self, cwd: &str) {
if self.cwd != cwd {
self.cwd = cwd.to_string();
self.cwds.insert(self.cwd.clone());
}
}
fn episode(&mut self, ms: i64, call: &str, rejected_tool: Option<(String, String)>, correction: String) -> &mut Found {
let (rejected_tool, tool_cwd) = match rejected_tool {
Some((tool, cwd)) => (Some(tool), cwd),
None => (None, self.cwd.clone()),
};
let episode = Episode {
id: learn::transcript_id(&self.session, call, ms),
ms,
repo: String::new(),
session: self.session.clone(),
task: self.task.0.clone(),
agent: self.said.0.clone(),
rejected_tool,
correction,
harness: self.harness.into(),
harness_version: self.version.clone(),
..Episode::default()
};
self.out.push(Found { cwd: self.cwd.clone(), task_cwd: self.task.1.clone(), agent_cwd: self.said.1.clone(), tool_cwd, episode });
self.out.last_mut().expect("just pushed")
}
fn assistant_text(&mut self, t: &str) {
if !t.trim().is_empty() {
self.said = (tail(t.trim(), learn::PROPOSAL_CHARS), self.cwd.clone());
}
}
fn prompt(&mut self, ms: i64, t: &str) {
let t = t.trim();
if !human(t) || t.starts_with(AGENTS_MD) {
return;
}
if self.answers_only {
if !learn::is_correction(t) || self.task.0.is_empty() {
self.task = (take(t, learn::TASK_CHARS), self.cwd.clone());
}
return;
}
if let Some(reason) = crate::veto::enabled().then(|| crate::veto::reason_in(t)).flatten() {
self.denied = None;
let f = self.episode(ms, "", None, reason);
f.episode.id = format!("no-{}", f.episode.id);
f.episode.kind = crate::veto::KIND.into();
return;
}
if let Some((tool, cwd, call)) = self.denied.take() {
self.episode(ms, &call, Some((tool, cwd)), take(t, learn::EXCERPT_CHARS));
return;
}
if learn::is_correction(t) && !self.task.0.is_empty() {
self.episode(ms, "", None, take(t, learn::EXCERPT_CHARS));
} else {
self.task = (take(t, learn::TASK_CHARS), self.cwd.clone());
}
}
fn tool_result(&mut self, ms: i64, is_error: bool, text: &str, tool: String, call: &str) {
if !is_error {
self.denied = None; return;
}
if self.answers_only {
return;
}
if let Some(said) = worded_rejection(text) {
let cwd = self.cwd.clone();
self.episode(ms, call, Some((tool, cwd)), said);
} else if learn::denied(text) {
self.denied = Some((tool, self.cwd.clone(), call.to_string()));
}
}
fn answer(&mut self, ms: i64, call: &str, asked: &[Asked]) {
self.denied = None;
let Some(said) = answer_text(asked) else { return };
let (proposal, cwd) = (asked_text(asked), self.cwd.clone());
let f = self.episode(ms, call, None, said);
f.episode.via = learn::ANSWER.into();
f.episode.agent = proposal;
f.agent_cwd = cwd;
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct Asked {
label: String,
question: String,
options: Vec<String>,
chosen: Vec<String>,
typed: String,
notes: String,
}
impl Asked {
fn pick(&mut self, answer: &str) {
let a = answer.trim();
let offered = |x: &str| self.options.iter().any(|o| o == x);
let parts: Vec<&str> = a.split(", ").collect();
if offered(a) {
self.chosen.push(a.into());
} else if parts.len() > 1 && parts.iter().all(|p| offered(p)) {
self.chosen.extend(parts.iter().map(|p| p.to_string()));
} else if !a.is_empty() {
self.typed = if self.typed.is_empty() { a.to_string() } else { format!("{}\n{a}", self.typed) };
}
}
fn answer(&self) -> String {
let notes = (!self.notes.trim().is_empty()).then(|| format!("notes: {}", self.notes.trim()));
[Some(self.chosen.join(", ")), Some(self.typed.trim().to_string()), notes]
.into_iter()
.flatten()
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.join("; ")
}
}
const ANSWER_OPENINGS: &[&str] = &[
"Your questions have been answered:",
"The user answered:",
"User has answered your questions:",
"User selected:",
"User provided custom input:",
"User answers:",
];
const ANSWER_CLOSINGS: &[&str] = &[". You can now continue", ". Read the answers carefully"];
const AGENTS_MD: &str = "# AGENTS.md";
fn answer_words(text: &str) -> String {
let t = text.trim();
let t = ANSWER_OPENINGS.iter().find_map(|o| t.strip_prefix(o)).unwrap_or(t);
let end = ANSWER_CLOSINGS.iter().filter_map(|c| t.find(c)).min().unwrap_or(t.len());
t[..end].trim().to_string()
}
fn labels(options: &Value) -> Vec<String> {
options.as_array().into_iter().flatten().filter_map(|o| o["label"].as_str().or(o.as_str())).map(String::from).collect()
}
fn s(v: &Value) -> String {
v.as_str().unwrap_or_default().to_string()
}
fn question(q: &Value) -> Asked {
let label = q["header"].as_str().or(q["id"].as_str()).unwrap_or_default().to_string();
Asked { label, question: s(&q["question"]), options: labels(&q["options"]), ..Asked::default() }
}
fn or_text(mut asked: Vec<Asked>, text: &str) -> Vec<Asked> {
if asked.iter().all(|a| a.answer().is_empty()) {
asked.push(Asked { typed: answer_words(text), ..Asked::default() });
}
asked
}
fn asked_text(asked: &[Asked]) -> String {
let one = |a: &Asked| match a.options.is_empty() {
true => a.question.clone(),
false => format!("{}\n options: {}", a.question, a.options.join(" | ")),
};
take(&asked.iter().filter(|a| !a.question.is_empty()).map(one).collect::<Vec<_>>().join("\n"), learn::PROPOSAL_CHARS)
}
fn answer_text(asked: &[Asked]) -> Option<String> {
let answered: Vec<&Asked> = asked.iter().filter(|a| !a.answer().is_empty()).collect();
let text = match answered.as_slice() {
[] => return None,
[one] => one.answer(),
many => many.iter().map(|a| format!("{}: {}", if a.label.is_empty() { &a.question } else { &a.label }, a.answer())).collect::<Vec<_>>().join("\n"),
};
Some(take(&text, learn::EXCERPT_CHARS))
}
const CLAUDE_ASK: &str = "AskUserQuestion";
fn claude_asked(input: &Value, result: &Value, text: &str) -> Vec<Asked> {
let questions = input["questions"].as_array().or(result["questions"].as_array());
let asked = questions
.into_iter()
.flatten()
.map(|q| {
let mut a = question(q);
match &result["answers"][&a.question] {
Value::String(x) => a.pick(x),
Value::Array(xs) => xs.iter().filter_map(Value::as_str).for_each(|x| a.pick(x)),
_ => {}
}
a.notes = s(&result["annotations"][&a.question]["notes"]);
a
})
.collect();
or_text(asked, text)
}
const OMP_ASK: &str = "ask";
fn omp_asked(details: &Value, text: &str) -> Vec<Asked> {
let one = |d: &Value| {
let mut a = Asked { label: s(&d["id"]), question: s(&d["question"]), options: labels(&d["options"]), ..Asked::default() };
d["selectedOptions"].as_array().into_iter().flatten().filter_map(Value::as_str).for_each(|o| a.pick(o));
a.typed = [a.typed.clone(), s(&d["customInput"])].iter().filter(|t| !t.trim().is_empty()).cloned().collect::<Vec<_>>().join("\n");
a
};
let asked = match details["results"].as_array() {
Some(rs) => rs.iter().map(one).collect(),
None if details["question"].is_string() => vec![one(details)],
None => Vec::new(),
};
or_text(asked, text)
}
const OC_QUESTION: &str = "question";
fn opencode_asked(state: &Value) -> Vec<Asked> {
let answers = state["metadata"]["answers"].as_array().map(Vec::as_slice).unwrap_or_default();
let asked = state["input"]["questions"]
.as_array()
.into_iter()
.flatten()
.enumerate()
.map(|(i, q)| {
let mut a = question(q);
answers.get(i).and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str).for_each(|x| a.pick(x));
a
})
.collect();
let output = state["output"].as_str().map_or_else(|| text_of(&state["content"]), String::from);
or_text(asked, &output)
}
const CODEX_ASK: &str = "request_user_input";
fn codex_asked(arguments: &Value, output: &Value) -> Vec<Asked> {
arguments["questions"]
.as_array()
.into_iter()
.flatten()
.map(|q| {
let mut a = question(q);
let id = q["id"].as_str().unwrap_or_default();
output["answers"][id]["answers"].as_array().into_iter().flatten().filter_map(Value::as_str).for_each(|x| a.pick(x));
a
})
.collect()
}
fn json_in(v: &Value) -> Value {
v.as_str().and_then(|t| serde_json::from_str(t).ok()).unwrap_or_else(|| v.clone())
}
fn tool_text(name: &str, input: &Value) -> String {
format!("{name} {}", take(&input.to_string(), learn::EXCERPT_CHARS))
}
fn lines(path: &Path) -> impl Iterator<Item = Value> {
let file = std::fs::File::open(path).ok();
file.into_iter().flat_map(|f| BufReader::new(f).lines()).map_while(Result::ok).filter_map(|l| serde_json::from_str::<Value>(&l).ok())
}
fn ts(v: &Value) -> i64 {
v.get("timestamp").and_then(Value::as_str).map_or(0, borg_trace::parse_ts)
}
pub fn claude_session(path: &Path) -> Vec<Found> {
claude_replay(path).out
}
pub fn claude_lines(session: &str, lines: impl IntoIterator<Item = Value>) -> Vec<Found> {
claude_from(session, Carry::default(), lines).out
}
fn claude_replay(path: &Path) -> Replay {
claude_from(&path.file_stem().map(|s| s.to_string_lossy().into_owned()).unwrap_or_default(), Carry::default(), lines(path))
}
fn claude_from(session: &str, carry: Carry, lines: impl IntoIterator<Item = Value>) -> Replay {
let mut r = Replay::resume(session, harness::claude::Claude.name(), carry);
for v in lines {
if v.get("isSidechain").and_then(Value::as_bool) == Some(true) {
return Replay::default();
}
if let Some(c) = v.get("cwd").and_then(Value::as_str) {
r.at(c);
}
if let Some(version) = v.get("version").and_then(Value::as_str) {
r.version = version.to_string();
}
if v.get("isMeta") == Some(&Value::Bool(true)) || v.get("isCompactSummary") == Some(&Value::Bool(true)) {
continue;
}
let Some(msg) = v.get("message") else { continue };
let content = msg.get("content").unwrap_or(&Value::Null);
match v.get("type").and_then(Value::as_str) {
Some("assistant") => {
for b in content.as_array().into_iter().flatten() {
match b.get("type").and_then(Value::as_str) {
Some("text") => r.assistant_text(b.get("text").and_then(Value::as_str).unwrap_or_default()),
Some("tool_use") => r.call(&s(&b["id"]), serde_json::json!({"name": b["name"], "input": b["input"]})),
_ => {}
}
}
}
Some("user") => {
let results: Vec<&Value> =
content.as_array().into_iter().flatten().filter(|b| b.get("type").and_then(Value::as_str) == Some("tool_result")).collect();
if results.is_empty() {
r.prompt(ts(&v), &text_of(content));
}
for b in results {
claude_result(&mut r, &v, b);
}
}
_ => {}
}
}
r
}
fn claude_result(r: &mut Replay, v: &Value, b: &Value) {
let id = b.get("tool_use_id").and_then(Value::as_str).unwrap_or_default();
let call = r.called(id);
let (name, input) = (s(&call["name"]), call["input"].clone());
let is_error = b.get("is_error").and_then(Value::as_bool) == Some(true);
let text = text_of(b.get("content").unwrap_or(&Value::Null));
let asking = name == CLAUDE_ASK || (name.is_empty() && ANSWER_OPENINGS.iter().any(|o| text.starts_with(o)));
if !is_error && asking {
r.answer(ts(v), id, &claude_asked(&input, &v["toolUseResult"], &text));
} else {
let tool = if name.is_empty() { String::new() } else { tool_text(&name, &input) };
r.tool_result(ts(v), is_error, &text, tool, id);
}
}
pub fn omp_session(path: &Path) -> Vec<Found> {
omp_replay(path).out
}
pub fn omp_lines(session: &str, lines: impl IntoIterator<Item = Value>) -> Vec<Found> {
omp_from(session, Carry::default(), lines).out
}
fn omp_call(id: &str) -> &str {
id.split('|').next().unwrap_or(id)
}
fn omp_replay(path: &Path) -> Replay {
omp_from(&path.file_stem().map(|s| s.to_string_lossy().into_owned()).unwrap_or_default(), Carry::default(), lines(path))
}
fn omp_from(session: &str, carry: Carry, lines: impl IntoIterator<Item = Value>) -> Replay {
let mut r = Replay::resume(session, harness::omp::Omp.name(), carry);
for v in lines {
if let Some(version) = v["runtime"]["version"].as_str() {
r.version = version.to_string();
}
match v.get("type").and_then(Value::as_str) {
Some("session") => {
if v.get("parentSession").is_some_and(|p| !p.is_null()) {
return Replay::default();
}
if let Some(c) = v.get("cwd").and_then(Value::as_str) {
r.at(c);
}
if let Some(id) = v.get("id").and_then(Value::as_str) {
r.session = id.to_string();
}
}
Some("message") => {
let Some(msg) = v.get("message") else { continue };
let content = msg.get("content").unwrap_or(&Value::Null);
match msg.get("role").and_then(Value::as_str) {
Some("user") => r.prompt(ts(&v), &text_of(content)),
Some("assistant") => {
let blocks = content.as_array().into_iter().flatten();
for b in blocks {
match b.get("type").and_then(Value::as_str) {
Some("text") => r.assistant_text(b.get("text").and_then(Value::as_str).unwrap_or_default()),
Some("toolCall") => {
let name = b.get("name").and_then(Value::as_str).unwrap_or_default();
let id = b.get("id").and_then(Value::as_str).unwrap_or_default();
r.call(omp_call(id), Value::String(tool_text(name, b.get("arguments").unwrap_or(&Value::Null))));
}
_ => {}
}
}
}
Some("toolResult") => {
let id = omp_call(msg.get("toolCallId").and_then(Value::as_str).unwrap_or_default());
let tool = s(&r.called(id));
let is_error = msg.get("isError").and_then(Value::as_bool) == Some(true);
if !is_error && msg["toolName"] == OMP_ASK {
r.answer(ts(&v), id, &omp_asked(&msg["details"], &text_of(content)));
} else {
r.tool_result(ts(&v), is_error, &text_of(content), tool, id);
}
}
_ => {}
}
}
_ => {}
}
}
r
}
pub fn codex_session(path: &Path) -> Vec<Found> {
codex_replay(path).out
}
pub fn codex_lines(meta: &Value, records: impl IntoIterator<Item = Value>) -> Vec<Found> {
codex_from(meta, Carry::default(), records).out
}
fn codex_replay(path: &Path) -> Replay {
codex::first_meta(path).map(|meta| codex_from(&meta, Carry::default(), codex::records(path))).unwrap_or_default()
}
fn codex_from(meta: &Value, carry: Carry, records: impl IntoIterator<Item = Value>) -> Replay {
if !codex::main_session(meta) {
return Replay::default();
}
let started = carry.cwd.is_empty();
let mut r = Replay::resume(&s(&meta["id"]), harness::codex::Codex.name(), carry);
(r.version, r.answers_only) = (s(&meta["cli_version"]), true);
if started {
r.at(meta["cwd"].as_str().unwrap_or_default());
}
for v in records {
let p = &v["payload"];
let call = s(&p["call_id"]);
match (v["type"].as_str(), p["type"].as_str()) {
(Some("turn_context"), _) => {
if let Some(c) = p["cwd"].as_str() {
r.at(c);
}
}
(Some("response_item"), Some("message")) => {
let text = p["content"].as_array().into_iter().flatten().filter_map(|c| c["text"].as_str()).collect::<Vec<_>>().join("\n");
match p["role"].as_str() {
Some("user") => r.prompt(ts(&v), &text),
Some("assistant") => r.assistant_text(&text),
_ => {}
}
}
(Some("response_item"), Some("function_call")) if p["name"] == CODEX_ASK => r.call(&call, json_in(&p["arguments"])),
(Some("response_item"), Some("function_call_output")) => {
let arguments = r.called(&call);
if !arguments.is_null() {
r.answer(ts(&v), &call, &codex_asked(&arguments, &json_in(&p["output"])));
}
}
_ => {}
}
}
r
}
fn source_of(h: &dyn harness::Harness) -> Option<Source> {
let readers = [
(harness::claude::Claude.name(), Source::Claude),
(harness::omp::Omp.name(), Source::Omp),
(harness::codex::Codex.name(), Source::Codex),
(harness::opencode::Opencode.name(), Source::Opencode),
];
readers.into_iter().find(|(name, _)| *name == h.name()).map(|(_, s)| s)
}
pub fn log_lines(h: &dyn harness::Harness, path: &Path, session: &str, carry: Carry, lines: Vec<Value>) -> Option<(Vec<Found>, Carry)> {
let r = match source_of(h)? {
Source::Claude => claude_from(session, carry, lines),
Source::Omp => {
let unseated = carry.cwd.is_empty() && lines.first().is_none_or(|v| v["type"] != "session");
let record = unseated.then(|| harness::omp::first_line(path)).flatten();
omp_from(session, carry, record.into_iter().chain(lines))
}
Source::Codex => codex_from(&codex::first_meta(path)?, carry, lines),
Source::Opencode => return None,
};
let carry = r.carry();
Some((r.out, carry))
}
pub fn exported(h: &dyn harness::Harness, id: &str, dir: &Path) -> Option<Vec<Found>> {
let ok_id = id.starts_with("ses_") && id.chars().all(|c| c.is_ascii_alphanumeric() || c == '_');
if source_of(h)? != Source::Opencode || !ok_id {
return None;
}
let v2 = runner::opencode() == runner::Opencode::V2;
let export: Value = opencode::cli(v2, None, &opencode::export_args(v2, id), dir).and_then(|o| serde_json::from_slice(&o).ok())?;
Some(opencode_session(&export))
}
pub fn logs(h: &dyn harness::Harness, home: &Path) -> Option<Vec<PathBuf>> {
match source_of(h)? {
Source::Claude => Some(session_files(&harness::claude::projects(home))),
Source::Omp => Some(session_files(&harness::omp::sessions(home))),
Source::Codex => Some(codex::rollouts(&codex::sessions(home))),
Source::Opencode => None,
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Marks {
pub session: String,
pub cwd: String,
pub rejections: Vec<(i64, String)>,
pub answers: Vec<(i64, String)>,
pub version: String,
}
const MARK_WORDS: &[&str] =
&["the user said:", "answered", "User selected:", "User provided custom input:", "User answers:", CODEX_ASK, "function_call_output"];
pub fn marks(h: &dyn harness::Harness, path: &Path) -> Option<Marks> {
let source = source_of(h)?;
let file = std::fs::File::open(path).ok()?;
let mut m = Marks { session: path.file_stem().map(|s| s.to_string_lossy().into_owned()).unwrap_or_default(), ..Marks::default() };
let mut version = String::new();
let mut asking: HashSet<String> = HashSet::new();
for (i, line) in BufReader::new(file).lines().map_while(Result::ok).enumerate() {
if i > 0 && !m.cwd.is_empty() && !MARK_WORDS.iter().any(|w| line.contains(w)) {
continue;
}
let Ok(v) = serde_json::from_str::<Value>(&line) else { continue };
let main = match source {
Source::Claude => claude_marks(&v, &mut m, &mut version),
Source::Omp => omp_marks(&v, &mut m, &mut version),
Source::Codex => codex_marks(&v, &mut m, &mut version, &mut asking),
Source::Opencode => false,
};
if !main {
return None;
}
}
m.version = version;
Some(m)
}
fn claude_marks(v: &Value, m: &mut Marks, version: &mut String) -> bool {
if v["isSidechain"] == true {
return false;
}
if m.cwd.is_empty() {
m.cwd = s(&v["cwd"]);
}
if let Some(x) = v["version"].as_str() {
*version = x.to_string();
}
let results = v["message"]["content"].as_array().into_iter().flatten().filter(|b| b["type"] == "tool_result");
for b in results {
let text = text_of(&b["content"]);
let at = (ts(v), version.clone());
if b["is_error"] == true && worded_rejection(&text).is_some() {
m.rejections.push(at);
} else if b["is_error"] != true && ANSWER_OPENINGS.iter().any(|o| text.trim_start().starts_with(o)) {
m.answers.push(at);
}
}
true
}
fn omp_marks(v: &Value, m: &mut Marks, version: &mut String) -> bool {
if v["type"] == "session" {
if v.get("parentSession").is_some_and(|p| !p.is_null()) {
return false;
}
(m.session, m.cwd) = (s(&v["id"]), s(&v["cwd"]));
}
if let Some(x) = v["runtime"]["version"].as_str() {
*version = x.to_string();
}
let msg = &v["message"];
let text = text_of(&msg["content"]);
if msg["role"] == "toolResult" && msg["toolName"] == OMP_ASK && msg["isError"] != true && ANSWER_OPENINGS.iter().any(|o| text.starts_with(o)) {
m.answers.push((ts(v), version.clone()));
}
true
}
fn codex_marks(v: &Value, m: &mut Marks, version: &mut String, asking: &mut HashSet<String>) -> bool {
let p = &v["payload"];
if v["type"] == "session_meta" {
if !codex::main_session(p) {
return false;
}
(m.session, m.cwd, *version) = (s(&p["id"]), s(&p["cwd"]), s(&p["cli_version"]));
}
match p["type"].as_str() {
Some("function_call") if p["name"] == CODEX_ASK => {
asking.insert(s(&p["call_id"]));
}
Some("function_call_output") if asking.remove(p["call_id"].as_str().unwrap_or_default()) => m.answers.push((ts(v), version.clone())),
_ => {}
}
true
}
const OC_REJECTED: &str = "The user rejected permission to use this specific tool call.";
const OC_FEEDBACK: &str = "The user rejected permission to use this specific tool call with the following feedback:";
const OC_DECLINED: &str = "The user declined this tool call";
const OC_AUTO_REJECTED: &str = "This non-interactive run cannot ask the user for permission, so the request was rejected. Continue without this action.";
const DENIED: &str = "The user doesn't want to proceed with this tool use.";
fn canonical(kind: Option<&str>, message: &str) -> String {
let m = message.trim();
let said = |words: &str| {
format!("The user doesn't want to proceed with this tool use. The tool use was rejected. To tell you how to proceed, the user said:\n{words}")
};
match kind {
None if m == OC_REJECTED => DENIED.into(),
None => m.strip_prefix(OC_FEEDBACK).map_or_else(|| m.to_string(), |w| said(w.trim())),
Some("aborted") if m == OC_DECLINED => DENIED.into(),
Some("permission.rejected") if m == OC_AUTO_REJECTED => m.into(),
Some("permission.rejected") if m.is_empty() => DENIED.into(),
Some("permission.rejected") => said(m),
_ => m.into(),
}
}
fn unquote(t: &str) -> Cow<'_, str> {
let Some(inner) = t.trim().strip_prefix('"').and_then(|s| s.strip_suffix('"')) else { return t.into() };
if inner.match_indices('"').any(|(i, _)| !inner[..i].ends_with('\\')) {
return t.into();
}
inner.replace("\\\"", "\"").into()
}
#[derive(Debug, PartialEq)]
enum Step {
Prompt(i64, String),
Said(String),
Tool(i64, Option<String>, String, String),
Answer(i64, String, Vec<Asked>),
}
fn tool_step(ms: i64, id: &Value, name: &Value, state: &Value) -> Option<Step> {
let (name, id) = (name.as_str().unwrap_or_default(), s(id));
let tool = tool_text(name, &state["input"]);
let (kind, message) = match &state["error"] {
Value::String(s) => (None, s.as_str()),
e => (e["type"].as_str(), e["message"].as_str().unwrap_or_default()),
};
match state["status"].as_str() {
Some("completed") if name == OC_QUESTION => Some(Step::Answer(ms, id, opencode_asked(state))),
Some("completed") => Some(Step::Tool(ms, None, tool, id)),
Some("error") => Some(Step::Tool(ms, Some(canonical(kind, message)), tool, id)),
_ => None,
}
}
fn opencode_steps(m: &Value) -> Vec<Step> {
let spoken = |p: &&Value| p["type"] == "text" && p["synthetic"] != true && p["ignored"] != true;
let text = |p: &Value| p["text"].as_str().unwrap_or_default().to_string();
if let Some(info) = m.get("info") {
let ms = info["time"]["created"].as_i64().unwrap_or(0);
let parts = m["parts"].as_array().map(Vec::as_slice).unwrap_or_default();
let error = |p: &Value| p["state"]["error"].as_str().unwrap_or_default().trim().to_string();
let worded = parts.iter().any(|p| error(p).starts_with(OC_FEEDBACK));
return match info["role"].as_str() {
Some("user") if !parts.iter().any(|p| p["metadata"]["borg"] == true) => {
vec![Step::Prompt(ms, parts.iter().filter(spoken).map(text).collect::<Vec<_>>().join("\n"))]
}
Some("assistant") => by_completion(
parts
.iter()
.filter_map(|p| match p["type"].as_str() {
Some("tool") if worded && error(p) == OC_REJECTED => None,
Some("tool") => tool_step(p["state"]["time"]["end"].as_i64().unwrap_or(ms), &p["id"], &p["tool"], &p["state"]),
_ => spoken(&p).then(|| Step::Said(text(p))),
})
.collect(),
),
_ => Vec::new(),
};
}
let ms = m["time"]["created"].as_i64().unwrap_or(0);
match m["type"].as_str() {
Some("user") if m["metadata"]["borg"] != true => vec![Step::Prompt(ms, text(m))],
Some("assistant") => by_completion(
m["content"]
.as_array()
.into_iter()
.flatten()
.filter_map(|c| match c["type"].as_str() {
Some("text") => Some(Step::Said(text(c))),
Some("tool") => tool_step(c["time"]["completed"].as_i64().unwrap_or(ms), &c["id"], &c["name"], &c["state"]),
_ => None,
})
.collect(),
),
_ => Vec::new(),
}
}
fn by_completion(mut steps: Vec<Step>) -> Vec<Step> {
for run in steps.split_mut(|s| matches!(s, Step::Said(_))) {
run.sort_by_key(|s| match s {
Step::Tool(ms, ..) | Step::Answer(ms, ..) => *ms,
_ => 0,
});
}
steps
}
fn engineers_session(export: &Value) -> bool {
let info = &export["info"];
info.is_object() && info.get("parentID").is_none_or(Value::is_null) && info["title"] != runner::OPENCODE_TITLE
}
pub fn opencode_session(export: &Value) -> Vec<Found> {
if !engineers_session(export) {
return Vec::new();
}
let info = &export["info"];
let mut r = Replay { session: s(&info["id"]), harness: harness::opencode::Opencode.name(), version: s(&info["version"]), ..Default::default() };
r.at(info["directory"].as_str().or(info["location"]["directory"].as_str()).unwrap_or_default());
for step in export["messages"].as_array().into_iter().flatten().flat_map(opencode_steps) {
match step {
Step::Prompt(ms, t) => r.prompt(ms, &unquote(&t)),
Step::Said(t) => r.assistant_text(&t),
Step::Tool(ms, error, tool, id) => r.tool_result(ms, error.is_some(), error.as_deref().unwrap_or_default(), tool, &id),
Step::Answer(ms, id, asked) => r.answer(ms, &id, &asked),
}
}
r.out
}
pub type Exec<'a> = &'a dyn Fn(&[String], &Path) -> Option<Vec<u8>>;
fn strings(xs: &[&str]) -> Vec<String> {
xs.iter().map(|x| x.to_string()).collect()
}
fn db_args(since: Option<i64>) -> Vec<String> {
let since = since.map(|ms| format!(" AND time_updated >= {ms}")).unwrap_or_default();
let title = runner::OPENCODE_TITLE.replace('\'', "''");
let sql = format!("SELECT id FROM session WHERE parent_id IS NULL AND title != '{title}'{since} ORDER BY time_created");
let mut a = strings(&["db", "--pure", "--format", "json"]);
a.push(sql);
a
}
fn list_args(v2: bool) -> Vec<String> {
strings(&["session", "list", if v2 { "--standalone" } else { "--pure" }, "--format", "json", "-n", "100000"])
}
fn ids_of(out: &[u8], since: Option<i64>) -> Option<Vec<String>> {
let rows: Vec<Value> = serde_json::from_slice(out).ok()?;
let recent = |r: &Value| since.is_none_or(|s| r["updated"].as_i64().is_none_or(|u| u >= s));
Some(rows.iter().filter(|r| r["title"] != runner::OPENCODE_TITLE && recent(r)).filter_map(|r| Some(r["id"].as_str()?.to_string())).collect())
}
fn collective_dirs(cfg: Option<&Config>) -> BTreeSet<PathBuf> {
cfg.into_iter().flat_map(|c| &c.collectives).flat_map(|c| &c.paths).flat_map(|p| with_repos(collective::expand(p))).collect()
}
fn with_repos(dir: PathBuf) -> Vec<PathBuf> {
let repo = |d: &Path| d.join(".git").exists();
let subdirs = |d: &Path| -> Vec<PathBuf> { std::fs::read_dir(d).into_iter().flatten().flatten().map(|e| e.path()).filter(|p| p.is_dir()).collect() };
let mut out = Vec::new();
if !dir.ancestors().any(repo) {
for child in subdirs(&dir) {
match repo(&child) {
true => out.push(child),
false => out.extend(subdirs(&child).into_iter().filter(|g| repo(g))),
}
}
}
out.push(dir);
out
}
fn projects(dirs: &BTreeSet<PathBuf>) -> Vec<PathBuf> {
let root = |d: &Path| d.ancestors().find(|a| a.join(".git").exists()).unwrap_or(d).to_path_buf();
dirs.iter().filter(|d| d.is_dir()).map(|d| root(d)).collect::<BTreeSet<_>>().into_iter().collect()
}
fn cli_sessions(v2: bool, since: Option<i64>, only: Option<&str>, known: &BTreeSet<PathBuf>, exec: Exec, each: &mut dyn FnMut(&Value)) {
let tmp = std::env::temp_dir();
if v2 {
eprintln!(
"borg assimilate: opencode 2 lists sessions per known project only, and never the ones it migrated from 1.x (export those with a 1.x opencode into a directory and pass --opencode DIR)"
);
}
let queried = if v2 { None } else { exec(&db_args(since), &tmp).and_then(|o| ids_of(&o, since)) };
let mut listed: Vec<(String, PathBuf)> = match queried {
Some(ids) => ids.into_iter().map(|id| (id, tmp.clone())).collect(),
None => {
if !v2 {
eprintln!("borg assimilate: `opencode db` failed; reading the opencode sessions of known projects only");
}
let mut seen = HashSet::new();
let mut out = Vec::new();
for dir in projects(known) {
for id in exec(&list_args(v2), &dir).and_then(|o| ids_of(&o, since)).unwrap_or_default() {
if seen.insert(id.clone()) {
out.push((id, dir.clone()));
}
}
}
out
}
};
listed.retain(|(id, _)| only.is_none_or(|s| s == id));
if !listed.is_empty() {
eprintln!("borg assimilate: exporting {} opencode sessions", listed.len());
}
for (id, dir) in listed {
if let Some(v) = exec(&opencode::export_args(v2, &id), &dir).and_then(|o| serde_json::from_slice::<Value>(&o).ok()) {
each(&v);
}
}
}
fn dir_sessions(dir: &Path, each: &mut dyn FnMut(&Value)) {
let mut files: Vec<PathBuf> =
std::fs::read_dir(dir).into_iter().flatten().flatten().map(|e| e.path()).filter(|p| p.extension().is_some_and(|x| x == "json")).collect();
files.sort();
for f in files {
if let Some(v) = std::fs::read(&f).ok().and_then(|b| serde_json::from_slice::<Value>(&b).ok()) {
each(&v);
}
}
}
fn session_files(root: &Path) -> Vec<PathBuf> {
let mut out = Vec::new();
for slug in std::fs::read_dir(root).into_iter().flatten().flatten() {
if !slug.file_type().is_ok_and(|t| t.is_dir()) {
continue;
}
for f in std::fs::read_dir(slug.path()).into_iter().flatten().flatten() {
let p = f.path();
if f.file_type().is_ok_and(|t| t.is_file()) && p.extension().is_some_and(|x| x == "jsonl") {
out.push(p);
}
}
}
out.sort();
out
}
pub fn skip_words() -> Vec<String> {
std::env::var("BORG_ASSIMILATE_SKIP").unwrap_or_default().split(',').map(|w| w.trim().to_lowercase()).filter(|w| !w.is_empty()).collect()
}
pub fn mentions(text: &str, words: &[String]) -> bool {
!words.is_empty() && {
let t = text.to_lowercase();
words.iter().any(|w| t.contains(w.as_str()))
}
}
fn obsolete(f: &Found, words: &[String]) -> bool {
let e = &f.episode;
[&f.cwd, &f.task_cwd, &f.agent_cwd, &f.tool_cwd, &e.task, &e.agent, &e.correction, e.rejected_tool.as_deref().unwrap_or_default()]
.iter()
.any(|s| mentions(s, words))
}
fn read_jsonl<T: for<'de> serde::Deserialize<'de>>(path: &Path) -> Vec<T> {
std::fs::read_to_string(path).unwrap_or_default().lines().filter_map(|l| serde_json::from_str(l).ok()).collect()
}
fn waiting(home: &Path) -> usize {
let done: HashSet<String> = learn::lessons(&home.join("lessons.jsonl")).into_iter().flat_map(|l: Lesson| l.episodes).collect();
let skipped: HashSet<String> = std::fs::read_to_string(home.join("episodes-not-lessons.txt")).unwrap_or_default().lines().map(str::to_string).collect();
read_jsonl::<Episode>(&home.join("episodes.jsonl")).iter().filter(|e| !done.contains(&e.id) && !skipped.contains(&e.id)).count()
}
fn repo_of(cwd: &str) -> String {
borg_trace::repo_key(cwd).rsplit('/').next().unwrap_or_default().to_string()
}
pub fn run(opts: &Options, cfg: Option<&Config>, base: &Path, ask: &dyn Fn(&str) -> Option<Value>) -> Report {
let mut report = Report::default();
let mut found: Vec<Found> = Vec::new();
let mut known = collective_dirs(cfg);
let maybe = |f: &PathBuf| opts.session.as_deref().is_none_or(|s| f.file_name().is_some_and(|n| n.to_string_lossy().contains(s)));
for (root, source) in [(&opts.claude, Source::Claude), (&opts.omp, Source::Omp), (&opts.codex, Source::Codex)] {
let Some(root) = root else { continue };
let files = if source == Source::Codex { codex::rollouts(root) } else { session_files(root) };
for f in files.into_iter().filter(maybe) {
let (r, n) = match source {
Source::Claude => (claude_replay(&f), &mut report.claude),
Source::Omp => (omp_replay(&f), &mut report.omp),
_ => (codex_replay(&f), &mut report.codex),
};
*n += usize::from(source != Source::Codex || !r.session.is_empty());
known.extend(r.cwds.iter().map(PathBuf::from));
found.extend(r.out);
}
}
if let Some(from) = &opts.opencode {
let mut n = 0;
let mut each = |v: &Value| {
if engineers_session(v) && opts.session.as_deref().is_none_or(|s| v["info"]["id"] == s) {
n += 1;
found.extend(opencode_session(v));
}
};
match from {
OpencodeFrom::Dir(d) => dir_sessions(d, &mut each),
OpencodeFrom::Cli => {
let v2 = runner::opencode() == runner::Opencode::V2;
cli_sessions(v2, opts.since, opts.session.as_deref(), &known, &|a, d| opencode::cli(v2, None, a, d), &mut each)
}
}
report.opencode = Some(n);
}
report.scanned = report.claude + report.omp + report.opencode.unwrap_or(0) + report.codex;
let wanted = |f: &Found| opts.session.as_deref().is_none_or(|s| f.episode.session == s);
found.retain(|f| !obsolete(f, &opts.skip) && opts.since.is_none_or(|s| f.episode.ms >= s) && wanted(f));
found.sort_by_key(|f| f.episode.ms);
let mut by: BTreeMap<String, (PathBuf, bool, Vec<Found>)> = BTreeMap::new();
let mut off_sessions = HashSet::new();
for f in found {
let session = f.episode.session.clone();
match place(cfg, base, f) {
None => {
report.off_episodes += 1;
off_sessions.insert(session);
}
Some(p) => by.entry(p.name).or_insert_with(|| (p.home, p.may_sync, Vec::new())).2.push(p.found),
}
}
report.off_sessions = off_sessions.len();
let mut tallies: BTreeMap<String, (Tally, Vec<Found>)> = BTreeMap::new();
for (name, (home, may_sync, eps)) in by {
let mut known = learn::Known::read(&home);
let mut t = Tally { home, may_sync, ..Default::default() };
let mut fresh = Vec::new();
for f in eps {
if known.holds(&f.episode) {
t.known += 1;
report.harnesses.entry(f.episode.harness.clone()).or_default().known += 1;
continue;
}
known.add(&f.episode);
fresh.push(f);
}
tallies.insert(name, (t, fresh));
}
if let Some(n) = opts.limit {
let mut newest: Vec<(i64, String, usize)> =
tallies.iter().flat_map(|(name, (_, fresh))| fresh.iter().enumerate().map(move |(i, f)| (f.episode.ms, name.clone(), i))).collect();
newest.sort_by_key(|(ms, ..)| std::cmp::Reverse(*ms));
let keep: HashSet<(String, usize)> = newest.into_iter().take(n).map(|(_, name, i)| (name, i)).collect();
for (name, (_, fresh)) in tallies.iter_mut() {
let mut i = 0;
fresh.retain(|_| {
i += 1;
keep.contains(&(name.clone(), i - 1))
});
}
}
let mut new: Vec<&Episode> = tallies.values().flat_map(|(_, fresh)| fresh).map(|f| &f.episode).collect();
new.sort_by_key(|e| e.ms);
for e in new {
let k = report.harnesses.entry(e.harness.clone()).or_default();
*(if e.via == learn::ANSWER {
&mut k.answers
} else if e.rejected_tool.is_some() {
&mut k.rejections
} else {
&mut k.prompts
}) += 1;
if !e.harness_version.is_empty() {
k.version.clone_from(&e.harness_version);
}
}
report.usd_per_call = learn_cost(base);
let mut engineers: HashMap<String, String> = HashMap::new();
for (name, (mut t, fresh)) in tallies {
t.sessions = fresh.iter().map(|f| &f.episode.session).collect::<HashSet<_>>().len();
t.episodes = fresh.len();
t.calls = fresh.len() + waiting(&t.home);
if opts.apply {
match learn::record_new_in(&t.home, fresh, |f| complete(f, &mut engineers)) {
Ok(appended) => {
appended.iter().for_each(|f| count_veto(&t.home, &f.episode));
t.known += t.episodes - appended.len();
t.episodes = appended.len();
t.calls = waiting(&t.home);
}
Err(_) => eprintln!("borg assimilate: cannot write {}", t.home.join("episodes.jsonl").display()),
}
eprintln!("learning {name}: one model call per episode, in order (each sees the lessons before it)");
(t.new_lessons, t.merged, t.pending) = learn::learn_in_reporting(&t.home, None, ask, learn::learning(), learn::now_ms(), &mut learn::print_step);
crate::vectors::refresh(&t.home.join("lessons.jsonl"));
}
report.collectives.insert(name, t);
}
report
}
fn learn_cost(base: &Path) -> Option<(f64, usize)> {
let costs: Vec<f64> = crate::stats::live(base)
.into_iter()
.filter(|e| e.kind == crate::stats::MODEL_CALL && (e.why == "learn" || e.why == "assimilate") && e.usd > 0.0)
.map(|e| e.usd)
.collect();
(!costs.is_empty()).then(|| (costs.iter().sum::<f64>() / costs.len() as f64, costs.len()))
}
pub struct Placed {
pub name: String,
pub home: PathBuf,
pub may_sync: bool,
pub found: Found,
}
pub fn place(cfg: Option<&Config>, base: &Path, mut f: Found) -> Option<Placed> {
let r = collective::route(cfg, &f.cwd, base);
let may_sync = collective::may_sync(&r);
let Route::Collective { name, home, .. } = r else { return None };
let elsewhere = |cwd: &str| !matches!(collective::route(cfg, cwd, base), Route::Collective { name: n, .. } if n == name);
if elsewhere(&f.task_cwd) {
f.episode.task.clear();
}
if elsewhere(&f.agent_cwd) {
f.episode.agent.clear();
}
if elsewhere(&f.tool_cwd) {
f.episode.rejected_tool = None;
}
Some(Placed { name, home, may_sync, found: f })
}
pub fn complete(f: &mut Found, engineers: &mut HashMap<String, String>) {
f.episode.repo = repo_of(&f.cwd);
f.episode.engineer = engineers.entry(f.cwd.clone()).or_insert_with(|| learn::engineer(&f.cwd)).clone();
}
pub fn count_veto(store: &Path, e: &Episode) {
if e.kind == crate::veto::KIND {
crate::stats::count(store, crate::stats::VETO, &e.session, |ev| {
ev.ms = e.ms;
ev.engineer.clone_from(&e.engineer);
ev.repo.clone_from(&e.repo);
ev.why = "prompt".into();
ev.text = crate::stats::excerpt(&e.correction, crate::stats::FEED_CHARS);
});
}
}
pub fn render(r: &Report, apply: bool) -> String {
let mut s = format!("{} sessions scanned{}\n", r.scanned, if apply { "" } else { " (dry run: nothing written; --apply to assimilate)" });
let opencode = r.opencode.map_or("opencode not installed".to_string(), |n| format!("opencode {n}"));
s.push_str(&format!("sources: claude {} sessions, omp {}, {opencode}, codex {}\n", r.claude, r.omp, r.codex));
for (h, k) in &r.harnesses {
let version = if k.version.is_empty() { String::new() } else { format!(" {}", k.version) };
s.push_str(&format!("{h}{version}: {} rejections, {} answers, {} prompts new; {} already present\n", k.rejections, k.answers, k.prompts, k.known));
}
for (name, t) in &r.collectives {
let sync = if t.may_sync { "team" } else { "local, never syncs" };
s.push_str(&format!("{name} ({sync}) {}: {} new episodes from {} sessions, {} already known", t.home.display(), t.episodes, t.sessions, t.known));
if apply {
s.push_str(&format!("; {} new lessons, {} merged", t.new_lessons, t.merged));
if t.pending > 0 {
s.push_str(&format!("; {} still pending (model unavailable), re-run to retry", t.pending));
}
s.push('\n');
} else {
s.push_str(&format!("; ~{} model calls to learn\n", t.calls));
}
}
s.push_str(&format!("off: {} episodes in {} sessions skipped (not stored)\n", r.off_episodes, r.off_sessions));
if !apply {
let calls: usize = r.collectives.values().map(|t| t.calls).sum();
s.push_str(&match r.usd_per_call {
Some((usd, n)) => {
format!("learn cost: ~{calls} model calls x ${usd:.3} (the mean of this machine's last {n} learn calls) = ~${:.2}\n", usd * calls as f64)
}
None => format!("learn cost: ~{calls} model calls; unknown per call (no learn call with a reported cost on this machine yet)\n"),
});
s.push_str("Dry run: nothing was written. Run `borg assimilate --apply` to learn these episodes.\n");
}
s
}
fn parse_date(d: &str) -> Option<i64> {
let full = if d.len() == 10 { format!("{d}T00:00:00Z") } else { d.to_string() };
let ms = borg_trace::parse_ts(&full);
(ms > 0).then_some(ms)
}
const USAGE: &str =
"usage: borg assimilate [--apply] [--claude DIR] [--omp DIR] [--codex DIR] [--opencode DIR] [--since YYYY-MM-DD] [--limit N] [--session ID]";
pub fn main(args: &[&str], ask: &dyn Fn(&str) -> Option<Value>) -> i32 {
let flag = |f: &str| args.iter().position(|a| *a == f).and_then(|i| args.get(i + 1)).copied();
let home = PathBuf::from(std::env::var("HOME").unwrap_or_default());
let dir = |f: &str, default: PathBuf| Some(flag(f).map_or(default, PathBuf::from));
let since = match flag("--since").map(parse_date) {
Some(None) => {
eprintln!("borg assimilate: --since wants YYYY-MM-DD\n{USAGE}");
return 2;
}
s => s.flatten(),
};
let limit = match flag("--limit").map(str::parse::<usize>) {
Some(Err(_)) => {
eprintln!("borg assimilate: --limit wants a number\n{USAGE}");
return 2;
}
l => l.and_then(Result::ok),
};
let opts = Options {
claude: dir("--claude", harness::claude::projects(&home)),
omp: dir("--omp", harness::omp::sessions(&home)),
opencode: flag("--opencode").map(|d| OpencodeFrom::Dir(d.into())).or_else(|| runner::on_path("opencode").then_some(OpencodeFrom::Cli)),
codex: dir("--codex", codex::sessions(&home)),
session: flag("--session").map(String::from),
since,
limit,
apply: args.contains(&"--apply"),
skip: skip_words(),
};
let cfg = match collective::load() {
Ok(c) => c,
Err(e) => {
eprintln!("borg assimilate: invalid collectives config ({e}): every repo is off, nothing to assimilate");
return 1;
}
};
let report = run(&opts, cfg.as_ref(), &learn::home(), ask);
print!("{}", render(&report, opts.apply));
0
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn temp(name: &str) -> PathBuf {
let d = std::env::temp_dir().join(format!("borg-assimilate-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
d
}
fn write(path: &Path, rows: &[Value]) {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, rows.iter().map(|r| r.to_string() + "\n").collect::<String>()).unwrap();
}
fn user(cwd: &str, t: &str, text: &str) -> Value {
json!({"type": "user", "cwd": cwd, "timestamp": t, "message": {"role": "user", "content": text}})
}
fn says(cwd: &str, t: &str, text: &str) -> Value {
json!({"type": "assistant", "cwd": cwd, "timestamp": t, "message": {"content": [{"type": "text", "text": text}]}})
}
fn tool_use(cwd: &str, t: &str, id: &str) -> Value {
json!({"type": "assistant", "cwd": cwd, "timestamp": t,
"message": {"content": [{"type": "tool_use", "id": id, "name": "Bash", "input": {"command": "rm -rf build"}}]}})
}
fn result(cwd: &str, t: &str, id: &str, is_error: bool, text: &str) -> Value {
json!({"type": "user", "cwd": cwd, "timestamp": t,
"message": {"content": [{"type": "tool_result", "tool_use_id": id, "is_error": is_error, "content": text}]}})
}
fn ask(p: &str) -> Option<Value> {
let c = p.split("## Engineer's correction\n").nth(1)?.lines().next()?.to_string();
Some(json!({"is_lesson": true, "question": format!("Q {c}"), "situations": [], "consequence": "c", "same_as": ""}))
}
const CFG: &str = r#"{ "default": "off", "off": ["/w/secret"],
"collectives": [ { "name": "corp", "paths": ["/w/corp"] }, { "name": "mine", "paths": ["/w/mine"], "share": "local" } ] }"#;
fn fixture(root: &Path) -> (PathBuf, PathBuf) {
let (claude, omp) = (root.join("claude"), root.join("omp"));
let c = "/w/corp/app";
write(
&claude.join("-w-corp-app/s-corp-1.jsonl"),
&[
user(c, "2026-09-01T10:00:00.000Z", "add a retry to the relay"),
says(c, "2026-09-01T10:00:05.000Z", "I'll wrap delivery in a ReentrantLock."),
user(c, "2026-09-01T10:01:00.000Z", "no, check the TOCTOU on the closed flag first"),
tool_use(c, "2026-09-01T10:02:00.000Z", "t1"),
result(
c,
"2026-09-01T10:02:01.000Z",
"t1",
true,
"Exit code 1\nold log: The user doesn't want to proceed with this tool use. the user said: never touch prod",
),
tool_use(c, "2026-09-01T10:03:00.000Z", "t2"),
result(c, "2026-09-01T10:03:01.000Z", "t2", false, "The user doesn't want to proceed with this tool use. the user said: use the staging db"),
],
);
let m = "/w/mine/notes";
write(
&claude.join("-w-mine-notes/s-mine-1.jsonl"),
&[
user(m, "2026-09-02T09:00:00.000Z", "clean the build"),
says(m, "2026-09-02T09:00:01.000Z", "Removing build/."),
tool_use(m, "2026-09-02T09:00:02.000Z", "t1"),
result(
m,
"2026-09-02T09:00:03.000Z",
"t1",
true,
"The user doesn't want to proceed with this tool use. The tool use was rejected. the user said:\nkeep the cache dir\nNote: The user's next message may contain a correction",
),
],
);
let s = "/w/secret/diary";
write(
&claude.join("-w-secret-diary/s-off-1.jsonl"),
&[
user(s, "2026-09-03T09:00:00.000Z", "summarise the week"),
says(s, "2026-09-03T09:00:01.000Z", "Here is a summary."),
user(s, "2026-09-03T09:01:00.000Z", "no, don't mention the doctor"),
],
);
write(
&claude.join("-w-corp-app/s-corp-1/subagents/a.jsonl"),
&[user(c, "2026-09-01T10:00:00.000Z", "task"), user(c, "2026-09-01T10:00:09.000Z", "no, wrong file")],
);
write(
&omp.join("-w-corp-svc/2026-09-04_x.jsonl"),
&[
json!({"type": "session", "id": "omp-sess-1", "cwd": "/w/corp/svc", "timestamp": "2026-09-04T08:00:00.000Z"}),
json!({"type": "message", "timestamp": "2026-09-04T08:00:01.000Z", "message": {"role": "user", "content": [{"type": "text", "text": "bump the dependency"}]}}),
json!({"type": "message", "timestamp": "2026-09-04T08:00:02.000Z", "message": {"role": "assistant", "content": [
{"type": "text", "text": "Running cargo update."}, {"type": "toolCall", "id": "c1", "name": "bash", "arguments": {"command": "cargo update"}}]}}),
json!({"type": "message", "timestamp": "2026-09-04T08:00:03.000Z", "message": {"role": "toolResult", "toolCallId": "c1", "isError": true,
"content": [{"type": "text", "text": "Tool call denied by user: bash"}]}}),
json!({"type": "message", "timestamp": "2026-09-04T08:00:09.000Z", "message": {"role": "user", "content": [{"type": "text", "text": "only the one crate, pin the rest"}]}}),
json!({"type": "message", "timestamp": "2026-09-04T08:01:00.000Z", "message": {"role": "user", "content": [{"type": "text", "text": "no, the OldProject client too"}]}}),
],
);
(claude, omp)
}
fn opts(claude: &Path, omp: &Path, apply: bool) -> Options {
Options {
claude: Some(claude.into()),
omp: Some(omp.into()),
opencode: None,
codex: None,
session: None,
since: None,
limit: None,
apply,
skip: vec!["oldproject".into()],
}
}
fn files(dir: &Path) -> Vec<PathBuf> {
borg_trace::jsonl_files(dir)
}
#[test]
fn echoed_rejection_in_tool_output_is_not_a_correction() {
let root = temp("echo");
let (claude, _) = fixture(&root);
let eps = claude_session(&claude.join("-w-corp-app/s-corp-1.jsonl"));
let said: Vec<&str> = eps.iter().map(|f| f.episode.correction.as_str()).collect();
assert_eq!(said, ["no, check the TOCTOU on the closed flag first"], "only the real prompt correction");
let e = &eps[0].episode;
assert_eq!((e.task.as_str(), e.agent.as_str(), e.session.as_str()), ("add a retry to the relay", "I'll wrap delivery in a ReentrantLock.", "s-corp-1"));
assert_eq!(e.ms, borg_trace::parse_ts("2026-09-01T10:01:00.000Z"));
assert!(claude_session(&claude.join("-w-corp-app/s-corp-1/subagents/a.jsonl")).len() == 1, "parser itself is neutral");
let m = marks(&harness::claude::Claude, &claude.join("-w-corp-app/s-corp-1.jsonl")).unwrap();
assert!(m.rejections.is_empty(), "{:?}", m.rejections);
let _ = std::fs::remove_dir_all(root);
}
#[test]
fn a_veto_prompt_is_replayed_as_the_hook_reads_it() {
let mut r = Replay { session: "sess-veto-1".into(), ..Default::default() };
r.prompt(1, "fix the merge lookup");
r.prompt(2, "borg no: never read the commit log");
r.prompt(3, "no, use the review API");
r.denied = Some(("Bash git log".into(), String::new(), "t1".into()));
r.prompt(5, "borg no, stop reading history");
let eps: Vec<&Episode> = r.out.iter().map(|f| &f.episode).collect();
let got: Vec<(&str, &str, &str, &str)> = eps.iter().map(|e| (e.id.as_str(), e.kind.as_str(), e.task.as_str(), e.correction.as_str())).collect();
assert_eq!(
got,
[
("no-sess-vet-2", crate::veto::KIND, "fix the merge lookup", "never read the commit log"),
("sess-vet-3", "", "fix the merge lookup", "no, use the review API"),
("no-sess-vet-5", crate::veto::KIND, "fix the merge lookup", "stop reading history"),
]
);
assert!(eps.iter().all(|e| e.rejected_tool.is_none()) && r.denied.is_none());
}
#[test]
fn dry_run_writes_nothing_and_counts_per_collective() {
let root = temp("dry");
let (claude, omp) = fixture(&root);
let base = root.join("home");
let cfg: Config = serde_json::from_str(CFG).unwrap();
let r = run(&opts(&claude, &omp, false), Some(&cfg), &base, &|_| panic!("a dry run calls no model"));
assert!(!base.exists(), "dry run wrote something");
assert_eq!(r.scanned, 4, "main sessions only");
let corp = &r.collectives["corp"];
assert_eq!((corp.sessions, corp.episodes, corp.calls, corp.may_sync), (2, 2, 2, true));
let mine = &r.collectives["mine"];
assert_eq!((mine.sessions, mine.episodes, mine.may_sync), (1, 1, false));
assert_eq!((r.off_sessions, r.off_episodes), (1, 1));
assert!(render(&r, false).contains("dry run"));
assert!(render(&r, false).trim_end().ends_with("to learn these episodes."));
assert!(!render(&r, true).contains("Dry run"));
let _ = std::fs::remove_dir_all(root);
}
#[test]
fn apply_routes_episodes_learns_and_is_idempotent() {
let root = temp("apply");
let (claude, omp) = fixture(&root);
let base = root.join("home");
let cfg: Config = serde_json::from_str(CFG).unwrap();
let r = run(&opts(&claude, &omp, true), Some(&cfg), &base, &ask);
assert_eq!((r.collectives["corp"].new_lessons, r.collectives["mine"].new_lessons), (2, 1));
let corp: Vec<Episode> = read_jsonl(&base.join("collectives/corp/episodes.jsonl"));
let said: Vec<&str> = corp.iter().map(|e| e.correction.as_str()).collect();
assert_eq!(said, ["no, check the TOCTOU on the closed flag first", "only the one crate, pin the rest"]);
assert_eq!(corp[1].rejected_tool.as_deref(), Some(r#"bash {"command":"cargo update"}"#), "a denial's reason is the next prompt");
assert_eq!((corp[1].session.as_str(), corp[1].repo.as_str()), ("omp-sess-1", "svc"));
let mine: Vec<Episode> = read_jsonl(&base.join("collectives/mine/episodes.jsonl"));
assert_eq!(mine.len(), 1);
assert_eq!(mine[0].correction, "keep the cache dir");
assert!(mine[0].rejected_tool.as_deref().unwrap().starts_with("Bash "));
for f in files(&base) {
let t = std::fs::read_to_string(&f).unwrap();
assert!(!t.contains("doctor") && !t.contains("diary") && !t.to_lowercase().contains("oldproject"), "{}", f.display());
}
assert_eq!(learn::lessons(&base.join("collectives/corp/lessons.jsonl")).len(), 2);
let snapshot = |d: &Path| files(d).into_iter().map(|f| std::fs::read_to_string(f).unwrap()).collect::<Vec<_>>();
let before = snapshot(&base);
let again = run(&opts(&claude, &omp, true), Some(&cfg), &base, &|_| panic!("nothing new to learn"));
assert_eq!((again.collectives["corp"].episodes, again.collectives["corp"].known), (0, 2));
assert_eq!(snapshot(&base), before);
let _ = std::fs::remove_dir_all(root);
}
#[test]
fn off_repo_leaves_nothing_and_live_episodes_are_not_duplicated() {
let root = temp("off");
let (claude, omp) = fixture(&root);
let base = root.join("home");
let only_off: Config = serde_json::from_str(r#"{"default": "off", "off": ["/w"]}"#).unwrap();
let r = run(&opts(&claude, &omp, true), Some(&only_off), &base, &|_| panic!("off calls no model"));
assert!(r.collectives.is_empty() && !base.exists());
assert_eq!(r.off_episodes, 4);
let corp = base.join("collectives/corp");
std::fs::create_dir_all(&corp).unwrap();
let live = Episode {
id: "s-corp-1-live".into(),
ms: borg_trace::parse_ts("2026-09-01T10:01:00.800Z"),
repo: "app".into(),
session: "s-corp-1".into(),
task: String::new(),
agent: String::new(),
rejected_tool: None,
correction: "no".into(),
..Episode::default()
};
std::fs::write(corp.join("episodes.jsonl"), serde_json::to_string(&live).unwrap() + "\n").unwrap();
let cfg: Config = serde_json::from_str(CFG).unwrap();
let since = Options { since: parse_date("2026-09-01"), limit: Some(10), ..opts(&claude, &omp, false) };
let r = run(&since, Some(&cfg), &base, &|_| None);
assert_eq!((r.collectives["corp"].episodes, r.collectives["corp"].known, r.collectives["corp"].calls), (1, 1, 2));
let late = Options { since: parse_date("2026-09-04"), ..opts(&claude, &omp, false) };
assert!(!run(&late, Some(&cfg), &base, &|_| None).collectives.contains_key("mine"));
let one = Options { limit: Some(1), ..opts(&claude, &omp, false) };
assert_eq!(run(&one, Some(&cfg), &base, &|_| None).collectives.values().map(|t| t.episodes).sum::<usize>(), 1);
let _ = std::fs::remove_dir_all(root);
}
#[test]
fn limit_works_through_the_history_in_batches() {
let root = temp("limit");
let (claude, omp) = fixture(&root);
let base = root.join("home");
let cfg: Config = serde_json::from_str(CFG).unwrap();
let one = Options { limit: Some(1), ..opts(&claude, &omp, true) };
let stored = || -> Vec<String> {
["corp", "mine"].iter().flat_map(|c| read_jsonl::<Episode>(&base.join(format!("collectives/{c}/episodes.jsonl")))).map(|e| e.correction).collect()
};
run(&one, Some(&cfg), &base, &ask);
assert_eq!(stored(), ["only the one crate, pin the rest"]);
run(&one, Some(&cfg), &base, &ask);
assert_eq!(stored(), ["only the one crate, pin the rest", "keep the cache dir"], "the second batch is the next episode");
run(&one, Some(&cfg), &base, &ask);
let last = run(&one, Some(&cfg), &base, &|_| panic!("nothing left to learn"));
assert_eq!(stored().len(), 3);
assert_eq!(last.collectives.values().map(|t| (t.episodes, t.known)).fold((0, 0), |a, b| (a.0 + b.0, a.1 + b.1)), (0, 3));
let _ = std::fs::remove_dir_all(root);
}
#[test]
fn state_from_an_off_repo_never_reaches_a_collective() {
let root = temp("moved");
let claude = root.join("claude");
let (s, c) = ("/w/secret/notes", "/w/corp/app");
write(
&claude.join("-w-secret-notes/s-moved-1.jsonl"),
&[
user(s, "2026-09-05T09:00:00.000Z", "summarise my therapy notes"),
says(s, "2026-09-05T09:00:01.000Z", "Reading the therapy notes now."),
tool_use(s, "2026-09-05T09:00:02.000Z", "t1"),
result(
s,
"2026-09-05T09:00:03.000Z",
"t1",
true,
"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.",
),
user(c, "2026-09-05T09:05:00.000Z", "never read files outside the repo"),
user(c, "2026-09-05T09:06:00.000Z", "no, use the relay config instead"),
says(c, "2026-09-05T09:06:30.000Z", "Switching to the relay config."),
user(c, "2026-09-05T09:07:00.000Z", "no, the staging one"),
],
);
let base = root.join("home");
let cfg: Config = serde_json::from_str(CFG).unwrap();
let r = run(
&Options { claude: Some(claude), omp: None, opencode: None, codex: None, session: None, since: None, limit: None, apply: true, skip: Vec::new() },
Some(&cfg),
&base,
&ask,
);
assert_eq!(r.collectives["corp"].episodes, 3);
let corp: Vec<Episode> = read_jsonl(&base.join("collectives/corp/episodes.jsonl"));
let t = std::fs::read_to_string(base.join("collectives/corp/episodes.jsonl")).unwrap();
assert!(!t.contains("therapy") && !t.contains("rm -rf"), "off text in the team store: {t}");
assert_eq!((corp[0].task.as_str(), corp[0].agent.as_str(), corp[0].rejected_tool.as_ref()), ("", "", None));
assert_eq!((corp[0].correction.as_str(), corp[1].task.as_str()), ("never read files outside the repo", ""));
assert_eq!((corp[2].task.as_str(), corp[2].agent.as_str()), ("", "Switching to the relay config."));
for f in files(&base) {
assert!(!std::fs::read_to_string(&f).unwrap().contains("therapy"), "{}", f.display());
}
let _ = std::fs::remove_dir_all(root);
}
macro_rules! export {
($f:literal) => {
serde_json::from_str::<Value>(include_str!(concat!("../tests/fixtures/opencode/", $f))).unwrap()
};
}
fn say_v1(mut export: Value, ms: i64, text: &str) -> Value {
let m = json!({"info": {"role": "user", "time": {"created": ms}}, "parts": [{"type": "text", "text": text}]});
export["messages"].as_array_mut().unwrap().push(m);
export
}
fn say_v2(mut export: Value, ms: i64, text: &str) -> Value {
export["messages"].as_array_mut().unwrap().push(json!({"type": "user", "time": {"created": ms}, "text": text}));
export
}
fn corrections(found: &[Found]) -> Vec<&str> {
found.iter().map(|f| f.episode.correction.as_str()).collect()
}
#[test]
fn opencode_errors_take_claudes_wording_and_only_the_engineers_refusals_count() {
let feedback = canonical(None, &format!("{OC_FEEDBACK} keep the public API"));
assert_eq!(learn::rejection(&feedback).as_deref(), Some("keep the public API"));
assert!(feedback.lines().next().unwrap().contains(NOTICE), "the notice is on the first line, as the replay requires");
for wordless in [canonical(None, OC_REJECTED), canonical(Some("aborted"), OC_DECLINED), canonical(Some("permission.rejected"), "")] {
assert!(learn::denied(&wordless), "{wordless}");
}
let words = canonical(Some("permission.rejected"), "no, do not delete files, use git rm instead");
assert_eq!(learn::rejection(&words).as_deref(), Some("no, do not delete files, use git rm instead"));
for (kind, m) in [
(Some("permission.rejected"), OC_AUTO_REJECTED),
(None, "The user has specified a rule which prevents you from using this specific tool call. Here are some of the relevant rules []"),
(None, "The user dismissed this question"),
(None, "Tool execution aborted"),
(Some("aborted"), "Step interrupted"),
(None, "Model tried to call unavailable tool 'invalid'. Available tools: ."),
(Some("tool.execution"), "File not found: /home/dev/work/missing.rs"),
] {
let c = canonical(kind, m);
assert!(learn::rejection(&c).is_none() && !learn::denied(&c), "{m}");
}
}
#[test]
fn opencode_run_quoting_is_stripped_from_prompts() {
let quirk = export!("v1/run-deny-all-forced-tool-call-export.json")["messages"][0]["parts"][0]["text"].as_str().unwrap().to_string();
assert_eq!(unquote(&quirk), r#"Read relay.rs and answer with {"ok": true}"#);
assert_eq!(unquote("\"no, that is wrong, use a Mutex instead\""), "no, that is wrong, use a Mutex instead");
assert_eq!(unquote("no quotes"), "no quotes");
assert_eq!(unquote(r#""a" and "b""#), r#""a" and "b""#, "an unescaped quote inside: typed, not argv");
assert_eq!(unquote("\""), "\"");
}
#[test]
fn opencode_1_rejection_with_feedback_is_an_episode() {
let found = opencode_session(&export!("v1/reject-with-feedback.json"));
assert_eq!(corrections(&found), ["no, that is wrong, use the AtomicBoolean compareAndSet instead"]);
let (f, e) = (&found[0], &found[0].episode);
assert_eq!((f.cwd.as_str(), e.session.as_str(), e.task.as_str()), ("/home/dev/work", "ses_ec5c7766cffe7lNfHnHDzyNoUc", "Fix the race in relay.rs"));
assert!(e.rejected_tool.as_deref().unwrap().starts_with(r#"read {"filePath":"/home/dev/work/"#));
assert_eq!(e.ms, 1791978152946, "when the engineer answered, not when the message began");
let next = say_v1(export!("v1/reject-with-feedback.json"), 1791978160000, "now add a test for the relay");
assert_eq!(corrections(&opencode_session(&next)), ["no, that is wrong, use the AtomicBoolean compareAndSet instead"]);
}
#[test]
fn opencode_tool_calls_replay_in_the_order_they_finished() {
let mut export = export!("v1/reject-plain-then-correction.json");
let sibling = &mut export["messages"][1]["parts"][2]["state"];
sibling["status"] = json!("completed");
sibling["output"] = json!("fn deliver() {}");
sibling["time"]["end"] = json!(1791978164300i64);
sibling.as_object_mut().unwrap().remove("error");
let found = opencode_session(&export);
assert_eq!(corrections(&found), ["no, that is wrong, use compareAndSet instead of get then set"]);
assert!(found[0].episode.rejected_tool.as_deref().unwrap().starts_with("read "));
let steps = by_completion(vec![
Step::Tool(9, None, "b".into(), "1".into()),
Step::Tool(3, None, "a".into(), "2".into()),
Step::Said("then".into()),
Step::Tool(2, None, "c".into(), "3".into()),
]);
let order: Vec<String> = steps
.iter()
.map(|s| match s {
Step::Tool(_, _, t, _) | Step::Said(t) => t.clone(),
Step::Prompt(..) | Step::Answer(..) => unreachable!(),
})
.collect();
assert_eq!(order, ["a", "b", "then", "c"]);
}
#[test]
fn opencode_1_wordless_rejection_makes_the_next_prompt_the_reason() {
let found = opencode_session(&export!("v1/reject-plain-then-correction.json"));
assert_eq!(corrections(&found), ["no, that is wrong, use compareAndSet instead of get then set"]);
assert!(found[0].episode.rejected_tool.as_deref().unwrap().starts_with("read "));
assert_eq!(found[0].episode.ms, 1791978166088);
let found = opencode_session(&export!("v1/bash-rejected-then-correction-window.json"));
assert_eq!(corrections(&found), ["wait, revert that one. no, that is wrong, use the existing close constant instead"]);
assert!(found[0].episode.rejected_tool.as_deref().unwrap().starts_with("bash "));
assert_eq!(found[0].cwd, "/home/dev/work/service-a");
}
#[test]
fn opencode_1_correcting_prompts_are_episodes_and_a_dismissed_question_is_not() {
let found = opencode_session(&export!("v1/correction-rejection-dismissed.json"));
assert_eq!(
corrections(&found),
["no, that is wrong, use the test script of this project instead", "no, you need to wait until the server is ready, read the project notes first"]
);
assert!(found.iter().all(|f| f.episode.task == "Run the test suite and then the app." && f.episode.rejected_tool.is_none()));
assert_eq!(found[0].episode.agent, "I will look at the relevant file and then make the change.");
}
#[test]
fn opencode_1_a_rule_that_denies_a_call_is_not_the_engineer() {
let by_rule = say_v1(export!("v1/permission-denied-by-rule.json"), 1776791500000, "use the plan file");
assert!(opencode_session(&by_rule).is_empty());
let mut refused = by_rule.clone();
refused["messages"][0]["parts"][2]["state"]["error"] = json!(OC_REJECTED);
assert_eq!(corrections(&opencode_session(&refused)), ["use the plan file"]);
}
#[test]
fn opencode_1_echoes_and_synthetic_parts_are_not_the_engineer() {
let echo = say_v1(export!("v1/echo-marker-and-synthetic-part.json"), 1791978190000, "no, use a channel");
let found = opencode_session(&echo);
assert_eq!(corrections(&found), ["no, use a channel"]);
assert_eq!(found[0].episode.task, "Fix the race in relay.rs");
assert_eq!(found[0].episode.agent, "Plan: I check closed.get() on the AtomicBoolean before deliver() in relay.rs.");
let file = say_v1(export!("v1/synthetic-file-attachment.json"), 1776791100000, "no, the other plan");
assert_eq!(opencode_session(&file)[0].episode.task, "Read @docs/plan.md and tell me what is missing.");
let editor = say_v1(export!("v1/synthetic-editor-context.json"), 1779367490000, "no, the other workflow");
assert_eq!(opencode_session(&editor)[0].episode.task, "Please make the requested change in this project.");
let compacted = say_v1(export!("v1/compaction-synthetic-continue.json"), 1776858910000, "no, the other module");
assert!(opencode_session(&compacted).is_empty());
}
#[test]
fn opencode_sub_agents_and_borgs_model_calls_are_skipped() {
for export in [
say_v1(export!("v1/subagent-child.json"), 1776861800000, "no, the other file"),
export!("v1/model-call-session.json"),
export!("v1/run-deny-all-forced-tool-call-export.json"),
say_v2(export!("v2/session-export-child.json"), 1791977829000, "no, the other file"),
] {
assert!(!engineers_session(&export) && opencode_session(&export).is_empty(), "{}", export["info"]["title"]);
}
assert!(engineers_session(&export!("v2/session-export.json")) && !engineers_session(&json!({"messages": []})));
}
#[test]
fn opencode_2_session_replays_like_the_live_hook() {
let export = say_v2(export!("v2/session-export.json"), 1791977840000, "no, check the queue bound first");
let found = opencode_session(&export);
assert_eq!(
corrections(&found),
[
"no, that is wrong, use a Mutex instead of the AtomicBoolean", "SHELL clean up the file", "no, do not delete files, use git rm instead", "no, check the queue bound first",
]
);
let e: Vec<&Episode> = found.iter().map(|f| &f.episode).collect();
assert!(found.iter().all(|f| f.cwd == "/home/dev/work" && f.episode.session == "ses_ec5cd46d0ffeGGDxs4GhqC9Ifu"));
assert_eq!(
(e[0].task.as_str(), e[0].agent.as_str(), e[0].rejected_tool.as_deref()),
("READS Please fix the race in relay.rs", "Done: the mock finished this turn.", None)
);
assert_eq!(e[1].rejected_tool.as_deref(), Some(r#"shell {"command":"rm -f relay.rs"}"#));
assert_eq!(e[1].task, "SHELL remove the old file again");
assert_eq!(e[2].rejected_tool.as_deref(), Some(r#"shell {"command":"rm -f relay.rs"}"#));
assert_eq!(e[3].task, "SUBAGENT delegate a check of relay.rs", "the synthetic input and the echo are not the task");
assert!(e.windows(2).all(|w| w[0].ms < w[1].ms));
}
#[test]
fn opencode_2_session_migrated_from_1_reads_its_location() {
let found = opencode_session(&say_v2(export!("v2/session-export-migrated-from-v1.json"), 1791978280000, "no, keep the lock"));
assert_eq!(corrections(&found), ["no, keep the lock"]);
assert_eq!((found[0].cwd.as_str(), found[0].episode.task.as_str()), ("/home/dev/legacy-work", "no, that is wrong, use a Mutex instead"));
}
#[test]
fn opencode_discovery_asks_the_cli_read_only() {
let db = db_args(Some(1_700_000_000_000));
assert_eq!(&db[..4], ["db", "--pure", "--format", "json"]);
assert_eq!(
db[4],
"SELECT id FROM session WHERE parent_id IS NULL AND title != 'borg (model call)' AND time_updated >= 1700000000000 ORDER BY time_created"
);
assert!(!db_args(None)[4].contains("time_updated"));
assert_eq!(list_args(true), ["session", "list", "--standalone", "--format", "json", "-n", "100000"]);
assert_eq!(list_args(false), ["session", "list", "--pure", "--format", "json", "-n", "100000"]);
assert_eq!(opencode::export_args(true, "ses_a"), ["session", "export", "--standalone", "ses_a"]);
assert_eq!(opencode::export_args(false, "ses_a"), ["export", "--pure", "ses_a"]);
let listed = include_str!("../tests/fixtures/opencode/v2/session-list.json").as_bytes();
assert_eq!(ids_of(listed, None).unwrap().len(), 3);
assert_eq!(ids_of(listed, Some(1791977900000)).unwrap(), ["ses_ec5bbe808ffe4C17dEwmu9aHAe", "ses_ec5caa71cffeRc4s7USxbim6zn"]);
assert_eq!(ids_of(br#"[{"id": "a", "title": "borg (model call)"}, {"id": "b"}]"#, None).unwrap(), ["b"]);
assert_eq!(ids_of(b"Error: no such table", None), None);
}
#[test]
fn the_cwds_of_claude_and_omp_sessions_seed_opencode_2s_listing() {
let root = temp("cwds");
let (claude, omp) = fixture(&root);
let cwds = |r: Replay| r.cwds.into_iter().collect::<Vec<_>>();
assert_eq!(cwds(claude_replay(&claude.join("-w-corp-app/s-corp-1.jsonl"))), ["/w/corp/app"]);
assert_eq!(cwds(omp_replay(&omp.join("-w-corp-svc/2026-09-04_x.jsonl"))), ["/w/corp/svc"]);
let moved = claude.join("-w-secret-notes/moved.jsonl");
write(&moved, &[user("/w/secret/notes", "2026-09-05T09:00:00.000Z", "a task"), user("/w/corp/app", "2026-09-05T09:05:00.000Z", "next")]);
assert_eq!(cwds(claude_replay(&moved)), ["/w/corp/app", "/w/secret/notes"]);
let _ = std::fs::remove_dir_all(root);
}
#[test]
fn a_collective_folder_of_repos_lists_each_repo() {
let root = temp("collective-dirs");
let work = root.join("work-repos");
for repo in ["a", "team/b"] {
std::fs::create_dir_all(work.join(repo).join(".git")).unwrap();
}
std::fs::create_dir_all(work.join("a/src")).unwrap();
std::fs::create_dir_all(work.join("notes")).unwrap();
let cfg: Config =
serde_json::from_str(&format!(r#"{{"collectives": [{{"name": "w", "paths": ["{}", "{}"]}}]}}"#, work.display(), work.join("a").display())).unwrap();
let known = collective_dirs(Some(&cfg));
assert_eq!(known, [work.clone(), work.join("a"), work.join("team/b")].into_iter().collect(), "repos two levels deep; not inside a repo");
let cli = FakeCli { calls: Default::default(), db: None, list: "[]" };
cli.run(true, &known);
let lists: Vec<PathBuf> = cli.calls.borrow().iter().map(|(_, d)| d.clone()).collect();
assert_eq!(lists, [work.clone(), work.join("a"), work.join("team/b")]);
assert!(collective_dirs(None).is_empty());
let _ = std::fs::remove_dir_all(root);
}
#[test]
fn the_cli_wrapper_passes_every_argument_and_reads_a_long_answer_whole() {
let fake = PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/opencode/fake"));
let path = std::env::join_paths(std::iter::once(fake).chain(std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default()))).unwrap();
let dir = temp("cli-wrapper");
let args = db_args(Some(1));
let out = opencode::cli(true, Some(&path), &args, &dir).unwrap();
assert!(out.len() > 64 * 1024, "the fake answers more than a pipe holds");
let ids = ids_of(&out, None).unwrap();
assert_eq!((ids.len(), ids.last().map(String::as_str)), (3001, Some("ses_last")));
let seen = std::fs::read_to_string(dir.join("args.txt")).unwrap();
let mut want = vec![dir.display().to_string()];
want.extend(args);
assert_eq!(seen.lines().collect::<Vec<_>>(), want, "PWD, then the arguments unsplit (the query holds spaces and quotes)");
let env = std::fs::read_to_string(dir.join("env.txt")).unwrap();
assert!(env.contains("OPENCODE_CONFIG_DIR=") && env.contains("OPENCODE_DISABLE_PROJECT_CONFIG=1"), "{env}");
let _ = std::fs::remove_dir_all(dir);
}
#[test]
fn opencode_projects_are_git_roots_of_existing_dirs() {
let root = temp("projects");
let repo = root.join("repo");
std::fs::create_dir_all(repo.join(".git")).unwrap();
std::fs::create_dir_all(repo.join("src/deep")).unwrap();
std::fs::create_dir_all(root.join("loose")).unwrap();
let dirs: BTreeSet<PathBuf> = [repo.join("src/deep"), repo.clone(), root.join("loose"), root.join("gone")].into_iter().collect();
assert_eq!(projects(&dirs), [root.join("loose"), repo]);
let _ = std::fs::remove_dir_all(root);
}
struct FakeCli {
calls: std::cell::RefCell<Vec<(Vec<String>, PathBuf)>>,
db: Option<&'static str>,
list: &'static str,
}
impl FakeCli {
fn exec(&self, args: &[String], dir: &Path) -> Option<Vec<u8>> {
self.calls.borrow_mut().push((args.to_vec(), dir.to_path_buf()));
let id = args.last().unwrap().as_str();
let export = |v: Value| Some(v.to_string().into_bytes());
match args[0].as_str() {
"db" => self.db.map(|d| d.as_bytes().to_vec()),
"session" if args[1] == "list" => Some(self.list.as_bytes().to_vec()),
_ if id == "ses_v1" => export(export!("v1/reject-with-feedback.json")),
_ if id == "ses_v2" => export(export!("v2/session-export.json")),
_ => Some(b"Session not found".to_vec()),
}
}
fn run(&self, v2: bool, known: &BTreeSet<PathBuf>) -> Vec<String> {
let mut got = Vec::new();
cli_sessions(v2, None, None, known, &|a, d| self.exec(a, d), &mut |v| got.push(v["info"]["id"].as_str().unwrap().to_string()));
got
}
}
#[test]
fn opencode_1_lists_every_session_with_one_query_and_exports_each() {
let tmp = std::env::temp_dir();
let cli = FakeCli { calls: Default::default(), db: Some(r#"[{"id": "ses_v1"}, {"id": "ses_gone"}]"#), list: "[]" };
let known: BTreeSet<PathBuf> = [tmp.clone()].into_iter().collect();
assert_eq!(cli.run(false, &known).len(), 1, "an export that is no JSON is skipped");
let calls = cli.calls.borrow();
let firsts: Vec<&str> = calls.iter().map(|(a, _)| a[0].as_str()).collect();
assert_eq!(firsts, ["db", "export", "export"], "no listing when the query works");
assert!(calls.iter().all(|(_, d)| *d == tmp));
}
#[test]
fn opencode_1_without_its_query_and_opencode_2_list_the_known_projects() {
let root = temp("cli");
let (a, b) = (root.join("a"), root.join("b"));
std::fs::create_dir_all(&a).unwrap();
std::fs::create_dir_all(&b).unwrap();
let known: BTreeSet<PathBuf> = [a.clone(), b.clone(), root.join("gone")].into_iter().collect();
let list = r#"[{"id": "ses_v2", "title": "research main"}, {"id": "ses_x", "title": "borg (model call)"}, {"id": "ses_v1", "title": "t"}]"#;
for v2 in [false, true] {
let cli = FakeCli { calls: Default::default(), db: (!v2).then_some("Error: no such column"), list };
assert_eq!(cli.run(v2, &known), ["ses_ec5cd46d0ffeGGDxs4GhqC9Ifu", "ses_ec5c7766cffe7lNfHnHDzyNoUc"]);
let calls = cli.calls.borrow();
let lists: Vec<&PathBuf> = calls.iter().filter(|(c, _)| c[1] == "list").map(|(_, d)| d).collect();
assert_eq!(lists, [&a, &b], "existing dirs only");
let exports: Vec<(&str, &PathBuf)> =
calls.iter().filter(|(c, _)| c.contains(&"ses_v2".to_string()) || c.contains(&"ses_v1".to_string())).map(|(c, d)| (c[0].as_str(), d)).collect();
assert_eq!(exports.len(), 2);
assert!(exports.iter().all(|(_, d)| **d == a));
if v2 {
assert!(calls.iter().all(|(c, _)| c[0] == "session" && c.contains(&"--standalone".to_string())), "never the background service: {calls:?}");
} else {
assert_eq!(calls[0].0[0], "db");
assert!(calls[1..].iter().all(|(c, _)| c.contains(&"--pure".to_string())));
}
}
let _ = std::fs::remove_dir_all(root);
}
#[test]
fn render_names_every_source() {
let r = Report { scanned: 61, claude: 40, omp: 12, opencode: Some(9), ..Default::default() };
assert!(render(&r, false).contains("sources: claude 40 sessions, omp 12, opencode 9, codex 0\n"));
let r = Report { opencode: None, ..r };
assert!(render(&r, true).contains("sources: claude 40 sessions, omp 12, opencode not installed, codex 0\n"));
}
fn fixtures(rel: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures").join(rel)
}
const CLAUDE_SESSION: &str = "7f3c2a10-5b6d-4e8f-9a01-23456789abcd";
const CODEX_THREAD: &str = "0199e0aa-1111-7222-8333-444455556666";
fn shape(found: &[Found]) -> Vec<(String, String, String, String)> {
found
.iter()
.map(|f| {
let e = &f.episode;
let tool = e.rejected_tool.as_deref().unwrap_or_default().split(' ').next().unwrap_or_default().to_string();
(e.id.clone(), e.via.clone(), tool, e.correction.clone())
})
.collect()
}
fn row(id: &str, via: &str, tool: &str, said: &str) -> (String, String, String, String) {
(id.into(), via.into(), tool.into(), said.into())
}
#[test]
fn claude_2_1_312_transcript_yields_its_worded_rejection_and_answers() {
let found = claude_session(&fixtures(&format!("claude/transcripts/-home-dev-work-relay/{CLAUDE_SESSION}.jsonl")));
assert_eq!(
shape(&found),
[
row("7f3c2a10-Ttoolu_01RelayEdit", "", "Edit", "use exponential backoff with jitter, a fixed sleep hammers the broker"),
row(
"7f3c2a10-Ttoolu_01RelayAsk",
learn::ANSWER,
"",
"Budget: Per connection; notes: a slow consumer must not starve the others\nErrors: Timeouts, Connection resets"
),
row("7f3c2a10-Ttoolu_01RelayPlace", learn::ANSWER, "", "Keep it in the relay crate, nothing else retries this way"),
]
);
assert!(found.iter().all(|f| f.episode.harness == "claude" && f.episode.harness_version == "2.1.312" && f.cwd == "/home/dev/work/relay"));
let (rejected, asked) = (&found[0].episode, &found[1].episode);
assert_eq!(
(rejected.task.as_str(), rejected.agent.as_str()),
("Add a retry to the relay delivery", "I'll wrap delivery in a retry loop with a fixed one second sleep.")
);
assert_eq!(
asked.agent,
"Where should the retry budget live?\n options: Per message (Recommended) | Per connection\nWhich errors are retryable?\n options: Timeouts | Connection resets | Auth failures",
"the proposal is the questions with their options"
);
assert_eq!(asked.ms, borg_trace::parse_ts("2026-10-28T09:02:10.000Z"));
let bare = claude_asked(&Value::Null, &Value::Null, "The user answered: \"Q\"=\"keep it small\". You can now continue with these answers in mind.");
assert_eq!(answer_text(&bare).as_deref(), Some("\"Q\"=\"keep it small\""));
let careful = claude_asked(
&Value::Null,
&Value::Null,
"The user answered: \"Q\"=\"keep it small\". Read the answers carefully — they may request clarification, changes, or that you not proceed — and follow what they actually say.",
);
assert_eq!(answer_text(&careful).as_deref(), Some("\"Q\"=\"keep it small\""));
let q = json!({"questions": [{"question": "Q", "options": [{"label": "A"}, {"label": "B"}]}]});
let listed = claude_asked(&q, &json!({"answers": {"Q": ["A", "my own words"]}}), "");
assert_eq!(answer_text(&listed).as_deref(), Some("A; my own words"));
}
#[test]
fn a_transcripts_tail_alone_still_yields_the_same_ids() {
let path = fixtures(&format!("claude/transcripts/-home-dev-work-relay/{CLAUDE_SESSION}.jsonl"));
let all: Vec<Value> = lines(&path).collect();
let tail = claude_lines(CLAUDE_SESSION, all[2..].iter().cloned());
let whole = claude_session(&path);
let ids = |f: &[Found]| f.iter().map(|f| f.episode.id.clone()).collect::<Vec<_>>();
assert_eq!(ids(&tail), ids(&whole));
assert_eq!(tail[0].episode.rejected_tool.as_deref(), Some(""), "the call's text is before the tail");
let codex = fixtures(&format!("codex/sessions/2026/10/28/rollout-2026-10-28T11-00-00-{CODEX_THREAD}.jsonl"));
let meta = codex::first_meta(&codex).unwrap();
assert_eq!(codex_lines(&meta, codex::records(&codex)).len(), 1);
let omp: Vec<Value> = lines(&fixtures("omp/sessions/-home-dev-work-relay/2026-10-28T10-00-00-000Z_omp-relay-1.jsonl")).collect();
assert_eq!(omp_lines("omp-relay-1", omp[5..].iter().cloned()).len(), 3, "the three answers after the denial's reason");
}
#[test]
fn omp_session_yields_its_denial_reason_and_answers() {
let found = omp_session(&fixtures("omp/sessions/-home-dev-work-relay/2026-10-28T10-00-00-000Z_omp-relay-1.jsonl"));
assert_eq!(
shape(&found),
[
row("omp-rela-Tcall_bump", "", "bash", "only the broker client, pin the rest"),
row("omp-rela-Tcall_lock", learn::ANSWER, "", "Commit"),
row("omp-rela-Tcall_registry", learn::ANSWER, "", "use the internal mirror, the public one is rate limited"),
row("omp-rela-Tcall_scope", learn::ANSWER, "", "scope: All crates\ntests: run only the relay tests"),
]
);
assert!(found.iter().all(|f| f.episode.harness == "omp" && f.episode.harness_version == "18.13.3" && f.episode.session == "omp-relay-1"));
assert_eq!(found[1].episode.agent, "Commit the lockfile change?\n options: Commit | Leave it");
assert_eq!(found[0].episode.task, "bump the broker client");
}
#[test]
fn opencode_2_export_yields_its_rejection_and_question_answer_and_a_dismissed_question_is_none() {
let export: Value = serde_json::from_str(include_str!("../tests/fixtures/opencode/v2/session-export-question.json")).unwrap();
let found = opencode_session(&export);
assert_eq!(
shape(&found),
[
row("ses_0c1a-Tcall_oc_shell", "", "shell", "no, keep the retries in the client, not in the shell script"),
row("ses_0c1a-Tcall_oc_question", learn::ANSWER, "", "Backoff: Exponential\nCap: cap at 30 s, the health check gives up at 45"),
]
);
assert!(found.iter().all(|f| f.episode.harness == "opencode" && f.episode.harness_version.is_empty()), "2.x exports name no version");
assert_eq!(found[1].episode.task, "add retries to the relay");
let v1 = opencode_session(&export!("v1/correction-rejection-dismissed.json"));
assert!(v1.iter().all(|f| f.episode.via.is_empty() && f.episode.harness_version == "1.17.8"));
assert_eq!(v1.len(), 2, "the two prompts only");
}
#[test]
fn codex_rollout_yields_answers_only_and_never_a_spawned_agents() {
let day = fixtures("codex/sessions/2026/10/28");
let found = codex_session(&day.join(format!("rollout-2026-10-28T11-00-00-{CODEX_THREAD}.jsonl")));
assert_eq!(shape(&found), [row("0199e0aa-Tcall_cx_settings", learn::ANSWER, "", "Config: Config file\nDefault: 3")]);
let e = &found[0].episode;
assert_eq!((e.harness.as_str(), e.harness_version.as_str(), e.session.as_str()), ("codex", "0.153.2", CODEX_THREAD));
assert_eq!(e.task, "Make the relay retry configurable", "the injected AGENTS.md and context are not the task");
assert!(e.agent.starts_with("Where should the retry settings come from?\n options: Environment (Recommended) | Config file"));
assert!(codex_session(&day.join("rollout-2026-10-28T11-05-00-0199e0aa-9999-7222-8333-444455556666.jsonl")).is_empty());
let q = json!({"questions": [{"id": "cfg", "question": "Where from?", "options": [{"label": "Env"}]}]});
assert_eq!(answer_text(&codex_asked(&q, &json!({"answers": {}}))), None);
assert_eq!(answer_text(&codex_asked(&q, &json!({"answers": {"other": {"answers": ["Env"]}}}))), None);
let home = Path::new("/h");
if std::env::var_os("CODEX_HOME").is_none() {
assert_eq!(codex::sessions(home), home.join(".codex/sessions"));
}
assert_eq!(codex::rollout_of(&fixtures("codex/sessions"), CODEX_THREAD), Some(day.join(format!("rollout-2026-10-28T11-00-00-{CODEX_THREAD}.jsonl"))));
assert_eq!(
harness::omp::session_file(&fixtures("omp/sessions"), "omp-relay-1"),
Some(fixtures("omp/sessions/-home-dev-work-relay/2026-10-28T10-00-00-000Z_omp-relay-1.jsonl"))
);
assert_eq!(
harness::claude::session_file(&fixtures("claude/transcripts"), CLAUDE_SESSION),
Some(fixtures(&format!("claude/transcripts/-home-dev-work-relay/{CLAUDE_SESSION}.jsonl")))
);
assert_eq!(harness::omp::session_file(&fixtures("omp/sessions"), "nope"), None);
}
const DEV: &str = r#"{ "default": "off", "collectives": [ { "name": "dev", "paths": ["/home/dev/work"] } ] }"#;
fn every_harness(root: &Path, apply: bool) -> Options {
let oc = root.join("opencode");
std::fs::create_dir_all(&oc).unwrap();
std::fs::copy(fixtures("opencode/v2/session-export-question.json"), oc.join("relay.json")).unwrap();
Options {
claude: Some(fixtures("claude/transcripts")),
omp: Some(fixtures("omp/sessions")),
opencode: Some(OpencodeFrom::Dir(oc)),
codex: Some(fixtures("codex/sessions")),
session: None,
since: None,
limit: None,
apply,
skip: Vec::new(),
}
}
#[test]
fn backfill_reports_per_harness_and_rerunning_adds_nothing() {
let root = temp("every-harness");
let base = root.join("home");
let cfg: Config = serde_json::from_str(DEV).unwrap();
let dry = run(&every_harness(&root, false), Some(&cfg), &base, &|_| panic!("a dry run calls no model"));
assert!(!base.exists(), "dry run wrote something");
assert_eq!((dry.claude, dry.omp, dry.opencode, dry.codex), (1, 1, Some(1), 1), "main sessions only");
let k = |h: &str| dry.harnesses.get(h).map(|k| (k.rejections, k.answers, k.prompts, k.known, k.version.clone())).unwrap();
assert_eq!(k("claude"), (1, 2, 0, 0, "2.1.312".into()));
assert_eq!(k("omp"), (1, 3, 0, 0, "18.13.3".into()));
assert_eq!(k("opencode"), (1, 1, 0, 0, String::new()));
assert_eq!(k("codex"), (0, 1, 0, 0, "0.153.2".into()));
let text = render(&dry, false);
assert!(text.contains("claude 2.1.312: 1 rejections, 2 answers, 0 prompts new; 0 already present\n"), "{text}");
assert!(text.contains("learn cost: ~10 model calls; unknown per call"), "{text}");
let applied = run(&every_harness(&root, true), Some(&cfg), &base, &ask);
assert_eq!(applied.collectives["dev"].new_lessons, 10);
let stored: Vec<Episode> = read_jsonl(&base.join("collectives/dev/episodes.jsonl"));
assert_eq!(stored.iter().filter(|e| e.via == learn::ANSWER).count(), 7);
let whys: Vec<String> = crate::stats::live(&base.join("collectives/dev")).into_iter().map(|e| e.why).collect();
assert_eq!(whys.iter().filter(|w| *w == learn::ANSWER).count(), 7, "answers are counted as answers: {whys:?}");
let snapshot = |d: &Path| files(d).into_iter().map(|f| std::fs::read_to_string(f).unwrap()).collect::<Vec<_>>();
let before = snapshot(&base);
let again = run(&every_harness(&root, true), Some(&cfg), &base, &|_| panic!("nothing new to learn"));
assert_eq!((again.collectives["dev"].episodes, again.collectives["dev"].known), (0, 10));
assert_eq!(again.harnesses["omp"].known, 4);
assert_eq!(snapshot(&base), before);
let _ = std::fs::remove_dir_all(root);
}
#[test]
fn a_rejection_the_hook_recorded_is_the_transcripts_too() {
let root = temp("hook-and-transcript");
let store = root.join("home/collectives/dev");
std::fs::create_dir_all(&store).unwrap();
let hook = Episode {
id: "7f3c2a10-1793178071000".into(),
ms: borg_trace::parse_ts("2026-10-28T09:01:11.000Z"),
session: CLAUDE_SESSION.into(),
rejected_tool: Some("Edit {}".into()),
correction: "use exponential backoff with jitter,\n a fixed sleep hammers the broker ".into(),
..Episode::default()
};
std::fs::write(store.join("episodes.jsonl"), serde_json::to_string(&hook).unwrap() + "\n").unwrap();
let cfg: Config = serde_json::from_str(DEV).unwrap();
let one = Options { session: Some(CLAUDE_SESSION.into()), ..every_harness(&root, false) };
let r = run(&one, Some(&cfg), &root.join("home"), &|_| None);
assert_eq!((r.collectives["dev"].episodes, r.collectives["dev"].known), (2, 1), "the two answers are new, the rejection is held");
assert_eq!(r.harnesses.keys().collect::<Vec<_>>(), ["claude"], "--session reads that session alone");
assert_eq!((r.claude, r.omp, r.codex), (1, 0, 0));
let _ = std::fs::remove_dir_all(root);
}
fn opencode_dir(root: &Path) -> PathBuf {
let dir = root.join("opencode");
std::fs::create_dir_all(&dir).unwrap();
let put = |name: &str, mut v: Value, cwd: &str| {
if v["info"].get("location").is_some() {
v["info"]["location"]["directory"] = json!(cwd);
} else {
v["info"]["directory"] = json!(cwd);
}
std::fs::write(dir.join(name), v.to_string()).unwrap();
};
let mut corp = export!("v2/session-export.json");
for m in corp["messages"].as_array_mut().unwrap() {
for c in m["content"].as_array_mut().into_iter().flatten().filter(|c| c["id"] == "call_1791977813763_0") {
c["time"]["completed"] = json!(1791977830000i64);
}
}
put("corp.json", corp, "/w/corp/relay");
put("mine.json", export!("v1/reject-with-feedback.json"), "/w/mine/notes");
put("off.json", export!("v1/correction-rejection-dismissed.json"), "/w/secret/app");
put("child.json", export!("v2/session-export-child.json"), "/w/corp/relay");
put("call.json", export!("v1/model-call-session.json"), "/w/corp/relay");
std::fs::write(dir.join("notes.txt"), "not an export").unwrap();
dir
}
#[test]
fn apply_opencode_exports_routes_episodes_learns_and_is_idempotent() {
let root = temp("oc-apply");
let dir = opencode_dir(&root);
let base = root.join("home");
let cfg: Config = serde_json::from_str(CFG).unwrap();
let opts = |apply| Options {
claude: None,
omp: None,
opencode: Some(OpencodeFrom::Dir(dir.clone())),
codex: None,
session: None,
since: None,
limit: None,
apply,
skip: Vec::new(),
};
let dry = run(&opts(false), Some(&cfg), &base, &|_| panic!("a dry run calls no model"));
assert_eq!((dry.scanned, dry.claude, dry.omp, dry.opencode), (3, 0, 0, Some(3)), "main sessions only");
assert!(render(&dry, false).contains("sources: claude 0 sessions, omp 0, opencode 3, codex 0\n"));
assert_eq!((dry.off_sessions, dry.off_episodes), (1, 2));
assert!(!base.exists());
let r = run(&opts(true), Some(&cfg), &base, &ask);
assert_eq!((r.collectives["corp"].new_lessons, r.collectives["mine"].new_lessons), (3, 1));
let corp: Vec<Episode> = read_jsonl(&base.join("collectives/corp/episodes.jsonl"));
assert_eq!(
corp.iter().map(|e| e.correction.as_str()).collect::<Vec<_>>(),
["no, that is wrong, use a Mutex instead of the AtomicBoolean", "SHELL clean up the file", "no, do not delete files, use git rm instead"]
);
assert_eq!((corp[0].session.as_str(), corp[0].repo.as_str()), ("ses_ec5cd46d0ffeGGDxs4GhqC9Ifu", "relay"));
let mine: Vec<Episode> = read_jsonl(&base.join("collectives/mine/episodes.jsonl"));
assert_eq!(mine.len(), 1);
assert_eq!(mine[0].correction, "no, that is wrong, use the AtomicBoolean compareAndSet instead");
for f in files(&base) {
let t = std::fs::read_to_string(&f).unwrap();
assert!(!t.contains("test script") && !t.contains("CHILDTASK") && !t.contains("is_lesson"), "{}", f.display());
}
let snapshot = |d: &Path| files(d).into_iter().map(|f| std::fs::read_to_string(f).unwrap()).collect::<Vec<_>>();
let before = snapshot(&base);
let again = run(&opts(true), Some(&cfg), &base, &|_| panic!("nothing new to learn"));
assert_eq!((again.collectives["corp"].episodes, again.collectives["corp"].known), (0, 3));
assert_eq!(snapshot(&base), before);
let _ = std::fs::remove_dir_all(root);
}
}