diff --git a/crates/mind-body/src/projection/simulate.rs b/crates/mind-body/src/projection/simulate.rs index a146da2..a2c35b2 100644 --- a/crates/mind-body/src/projection/simulate.rs +++ b/crates/mind-body/src/projection/simulate.rs @@ -122,23 +122,52 @@ fn state_after_turn(state: Value, _turn: &Value) -> Value { state } -/// Why she wakes this round, by the plan's rule; none leaves her asleep. -fn wake_reason(lines: &[Line], seen: usize, member: &Member, cast: &[String], wake: Wake, beat: bool) -> Option<&'static str> { - let mut reason = None; +/// How a Mind's line addressed her, by the evidence `is_for` had: a Matrix tag or a reply to her (`mention or reply`), +/// her Matrix id written as plain text (`id as text`), or one of her names in the text (`name in text`). +fn how_addressed(line: &Line, at: usize, member: &Member, lines: &[Line]) -> &'static str { + let delivery = line.delivery(at, &member.entry.id, lines); + if delivery.mentions.contains(&member.entry.id) || delivery.basis.is_some() { + "mention or reply" + } else if line.says.to_lowercase().contains(&member.entry.id.to_lowercase()) { + "id as text" + } else { + "name in text" + } +} + +/// Why she wakes this round, by the plan's rule, and the Minds' lines that addressed her; none leaves her asleep. A +/// human outranks a Mind: a wake a human's line would cause is counted as the human's, so the Mind-addressed wakes are +/// exactly those no human caused. +fn wake_reason(lines: &[Line], seen: usize, member: &Member, cast: &[String], wake: Wake, beat: bool) -> Option<(&'static str, Vec)> { + let (mut human_addressed, mut human, mut mind) = (false, false, false); + let mut by_minds = Vec::new(); for (at, line) in lines.iter().enumerate().skip(seen) { if line.from == member.entry.id || !(member.rooms.contains(&line.room) || line.dm) { continue; } - if is_for(&member.addressee, &line.delivery(at, &member.entry.id, lines)) { - return Some("addressed"); - } - if !cast.contains(&line.from) { - reason = Some("a human in her rooms"); - } else if wake == Wake::AllMessages && reason.is_none() { - reason = Some("a message in her rooms"); + let is_human = !cast.contains(&line.from); + let addressed = is_for(&member.addressee, &line.delivery(at, &member.entry.id, lines)); + match (is_human, addressed) { + (true, true) => human_addressed = true, + (true, false) => human = true, + (false, true) => by_minds.push(json!({ "from": line.from, "how": how_addressed(line, at, member, lines), "says": line.says })), + (false, false) => mind = true, } } - reason.or(beat.then_some("the beat")) + let why = if human_addressed { + "addressed by a human" + } else if human { + "a human in her rooms" + } else if !by_minds.is_empty() { + "addressed by a Mind" + } else if wake == Wake::AllMessages && mind { + "a message in her rooms" + } else if beat { + "the beat" + } else { + return None; + }; + Some((why, if why == "addressed by a Mind" { by_minds } else { Vec::new() })) } /// A seeded shuffle, so the order of those woken together is drawn, and replays. @@ -204,8 +233,8 @@ fn run(harness: &Harness, plan: &SimFile, members: &mut [Member], repeat: usize) for round in 1..=plan.rounds { scripted(round, &mut lines, &mut rounds_of)?; let beat = plan.beat_every > 0 && round % plan.beat_every == 0; - let woken: Vec<(usize, &'static str)> = members.iter().enumerate().filter_map(|(at, member)| wake_reason(&lines, seen[at], member, &cast, plan.wake, beat).map(|why| (at, why))).collect(); - for (at, why) in shuffled(woken, seed ^ (round as u64).wrapping_mul(0x9E37_79B9)) { + let woken: Vec<(usize, (&'static str, Vec))> = members.iter().enumerate().filter_map(|(at, member)| wake_reason(&lines, seen[at], member, &cast, plan.wake, beat).map(|why| (at, why))).collect(); + for (at, (why, addressed_by)) in shuffled(woken, seed ^ (round as u64).wrapping_mul(0x9E37_79B9)) { counter += 1; let member = &members[at]; let id = member.entry.id.clone(); @@ -244,7 +273,7 @@ fn run(harness: &Harness, plan: &SimFile, members: &mut [Member], repeat: usize) lines.push(Line { from: id.clone(), name: member.entry.name.clone(), room: here.clone(), at_ms: u64::try_from(now.timestamp_millis()).unwrap_or_default(), says: says.clone(), mentions: vec![], dm: false, follow_up: false, reply_to: None, new: false }); rounds_of.push(round); } - let turn = json!({ "round": round, "persona": member.entry.name, "id": id, "woke": why, "kind": kind, "room": here, "said": line, "report": report }); + let turn = json!({ "round": round, "persona": member.entry.name, "id": id, "woke": why, "addressed_by": addressed_by, "kind": kind, "room": here, "said": line, "report": report }); let member = &mut members[at]; member.state = state_after_turn(std::mem::take(&mut member.state), &turn); turns.push(turn); @@ -331,6 +360,18 @@ fn metrics(members: &[Member], lines: &[Line], rounds_of: &[usize], opening: usi }; let (all, early, late): (Keep, Keep, Keep) = (&|_| true, &|round| round <= half, &|round| round > half); let mind_lines: Vec<&Line> = lines[opening..].iter().filter(|line| cast.contains(&line.from.as_str())).collect(); + let wakes_by_round: Vec = (1..=rounds) + .map(|round| { + let these: Vec<&Value> = turns.iter().filter(|turn| turn["round"] == round).collect(); + let count = |whys: &[&str]| these.iter().filter(|turn| whys.contains(&turn["woke"].as_str().unwrap_or_default())).count(); + json!({ "round": round, "human": count(&["addressed by a human", "a human in her rooms"]), "mind_addressed": count(&["addressed by a Mind"]), "mind_unaddressed": count(&["a message in her rooms"]), "beat": count(&["the beat"]) }) + }) + .collect(); + let mind_addressed: Vec = turns + .iter() + .filter(|turn| turn["woke"] == "addressed by a Mind") + .flat_map(|turn| turn["addressed_by"].as_array().into_iter().flatten().map(move |by| json!({ "round": turn["round"], "persona": turn["persona"], "from": by["from"], "how": by["how"], "says": by["says"] }))) + .collect(); let work = mind_lines.iter().filter(|line| work_talk(&line.says)).count(); json!({ "personas": per, @@ -342,9 +383,16 @@ fn metrics(members: &[Member], lines: &[Line], rounds_of: &[usize], opening: usi "convergence_lines": { "all": mean_pairwise(&spoken_by(all)), "first_half": mean_pairwise(&spoken_by(early)), "second_half": mean_pairwise(&spoken_by(late)) }, "convergence_prose": { "all": mean_pairwise(&prose_by(all)), "first_half": mean_pairwise(&prose_by(early)), "second_half": mean_pairwise(&prose_by(late)) }, "work_talk_share": if mind_lines.is_empty() { Value::Null } else { json!(work as f64 / mind_lines.len() as f64) }, + "wakes_by_round": wakes_by_round, + "mind_addressed": mind_addressed, }) } +/// A line's text on one Markdown line: its paragraph breaks shown as ` / `. +fn one_line(text: &str) -> String { + text.lines().map(str::trim).filter(|line| !line.is_empty()).collect::>().join(" / ") +} + fn number(value: &Value) -> String { value.as_f64().map_or_else(|| "n/a".into(), |number| format!("{number:.3}")) } @@ -358,7 +406,8 @@ fn markdown(plan_path: &Path, plan: &SimFile, runs: &[Value]) -> String { let _ = writeln!(out, "- rooms: {}; rounds: {}; repeats: {}; seed: {}; minutes a round: {}", plan.rooms.join(", "), plan.rounds, plan.repeats, plan.seed, plan.round_minutes); let _ = writeln!(out, "- wake: {}; beat: {}", match plan.wake { Wake::HumansInRooms => "ruled set (addressed, any human message in her rooms, the beat; another Mind's unaddressed message wakes no one)", Wake::AllMessages => "control: any message in her rooms wakes her, a Mind's too (the old loop)" }, if plan.beat_every == 0 { "off".to_owned() } else { format!("every {} rounds", plan.beat_every) }); let _ = writeln!(out, "- speech: the double-quoted spans of her prose, joined, posted to the room she woke in; none is silence. A preview of the Interpreter's speech, not its decision"); - let _ = writeln!(out, "- convergence is the mean pairwise cosine of speakers' word counts (lexical, cheap); work-talk share is a lexical heuristic over a fixed word list, not a classifier"); + let _ = writeln!(out, "- wakes: a human-caused wake is counted as the human's even when a Mind also addressed her; Mind-addressed wakes are split by evidence (Matrix mention or reply, her id written as text, a name in the text). Spoken lines here carry no Matrix mention, so a Mind addresses another only through text +- convergence is the mean pairwise cosine of speakers' word counts (lexical, cheap); work-talk share is a lexical heuristic over a fixed word list, not a classifier"); let _ = writeln!(out, "- read-only: nothing is posted, no state is put, no Mind is asked\n"); for run in runs { let metrics = &run["metrics"]; @@ -376,10 +425,24 @@ fn markdown(plan_path: &Path, plan: &SimFile, runs: &[Value]) -> String { let _ = writeln!(out, "| {name} | {} | {} | {} | {addressed} |", row["turns"], row["messages"], number(&row["silence_rate"])); } } + let _ = writeln!(out, "\n### Wakes by round\n\nA wake a human's line would cause is counted as the human's; Mind-addressed wakes are those only a Mind's line caused.\n\n| round | human | Mind-addressed | Mind, unaddressed (control) | beat |\n|---|---|---|---|---|"); + for row in metrics["wakes_by_round"].as_array().into_iter().flatten() { + let _ = writeln!(out, "| {} | {} | {} | {} | {} |", row["round"], row["human"], row["mind_addressed"], row["mind_unaddressed"], row["beat"]); + } + let addressed = metrics["mind_addressed"].as_array().cloned().unwrap_or_default(); + let by_how = |how: &str| addressed.iter().filter(|one| one["how"] == how).count(); + let _ = writeln!(out, "\nMind-addressed wake evidence: {} lines; a Matrix mention or reply {}, her Matrix id written as text {}, a name in the text {}.\n", addressed.len(), by_how("mention or reply"), by_how("id as text"), by_how("name in text")); + let as_text: Vec<&Value> = addressed.iter().filter(|one| one["how"] == "id as text").collect(); + if !as_text.is_empty() { + let _ = writeln!(out, "Wakes where the only address was an id written as text (no mention, no reply):\n"); + for one in as_text { + let _ = writeln!(out, "- r{} {} woken by {}: {}", one["round"], one["persona"].as_str().unwrap_or_default(), one["from"].as_str().unwrap_or_default(), one_line(one["says"].as_str().unwrap_or_default())); + } + } let _ = writeln!(out, "\n### Transcript\n"); for line in run["transcript"].as_array().into_iter().flatten() { let who = if line["mind"] == true { line["name"].as_str().unwrap_or_default().to_owned() } else { format!("{} (human)", line["name"].as_str().unwrap_or_default()) }; - let _ = writeln!(out, "- r{} {} **{who}**: {}", line["round"], line["room"].as_str().unwrap_or_default(), line["says"].as_str().unwrap_or_default()); + let _ = writeln!(out, "- r{} {} **{who}**: {}", line["round"], line["room"].as_str().unwrap_or_default(), one_line(line["says"].as_str().unwrap_or_default())); } let _ = writeln!(out, "\n### Turns\n"); for turn in run["turns"].as_array().into_iter().flatten() { diff --git a/crates/mind-body/src/projection/simulate/tests.rs b/crates/mind-body/src/projection/simulate/tests.rs index 5a101aa..eb97908 100644 --- a/crates/mind-body/src/projection/simulate/tests.rs +++ b/crates/mind-body/src/projection/simulate/tests.rs @@ -114,6 +114,10 @@ fn a_scripted_interjection_wakes_everyone_in_her_room_and_addressing_is_counted( let fourth: Vec<&String> = turns.iter().filter(|(round, _, _)| *round == 4).map(|(_, who, _)| who).collect(); assert_eq!(fourth.len(), 2, "Deru's line at round 4 wakes both: {turns:?}"); assert_eq!(run["metrics"]["personas"]["Tobiah"]["addressed"]["Libby"].as_u64().unwrap(), run["metrics"]["personas"]["Tobiah"]["messages"].as_u64().unwrap()); + let rounds = run["metrics"]["wakes_by_round"].as_array().unwrap(); + assert_eq!((rounds[0]["human"].as_u64(), rounds[3]["human"].as_u64()), (Some(2), Some(2)), "the opening and Deru wake both: {rounds:?}"); + let by_minds: u64 = rounds.iter().map(|row| row["mind_addressed"].as_u64().unwrap()).sum(); + assert_eq!(by_minds as usize, run["metrics"]["mind_addressed"].as_array().unwrap().len(), "one line addressed each Mind-addressed wake"); assert_eq!(run["metrics"]["personas"]["Libby"]["silence_rate"].as_f64(), Some(1.0)); // The seam: no state moved. assert_eq!(members[0].state, control()); @@ -134,7 +138,7 @@ fn the_report_names_the_state_gap_and_carries_the_transcript_and_usage() { let model = chatty(); let harness = Harness { instance: "eureka".into(), persona: None, model: Some(&model), tuning: dir.path().join("persona-tuning.toml") }; let report = simulate(&harness, &plan(dir.path(), "humans_in_rooms", 3, "")).unwrap(); - for part in ["# Swarm simulation", "State does not evolve during a run", "## Repeat 1 (seed 11)", "### Transcript", "**Meta (human)**: good afternoon, everyone", "**Tobiah**: what a day", "
round 1, ", "## Total usage", "| claude-sonnet-5-5 | 4 |"] { + for part in ["# Swarm simulation", "State does not evolve during a run", "## Repeat 1 (seed 11)", "### Transcript", "**Meta (human)**: good afternoon, everyone", "**Tobiah**: what a day", "
round 1, ", "### Wakes by round", "| 1 | 2 | 0 | 0 | 0 |", "## Total usage", "| claude-sonnet-5-5 | 4 |"] { assert!(report.contains(part), "{part}\n{report}"); } } @@ -158,16 +162,22 @@ fn wake_reason_is_the_ruled_set_and_the_control_widens_it_to_minds() { let libby = Member { addressee: Addressee::new(LIBBY, ["libby", "Libby"]), entry: CastEntry { id: LIBBY.into(), name: "Libby".into(), state: PathBuf::new(), rooms: vec![], names: vec![] }, state: Value::Null, rooms: vec!["#minds:x".into()] }; let cast = vec![TOBIAH.to_owned(), LIBBY.to_owned()]; let line = |from: &str, room: &str, says: &str| Line { from: from.into(), name: String::new(), room: room.into(), at_ms: 0, says: says.into(), mentions: vec![], dm: false, follow_up: false, reply_to: None, new: false }; - let reason = |lines: &[Line], wake: Wake, beat: bool| wake_reason(lines, 0, &libby, &cast, wake, beat); + let reason = |lines: &[Line], wake: Wake, beat: bool| wake_reason(lines, 0, &libby, &cast, wake, beat).map(|(why, _)| why); let named = [line(TOBIAH, "#minds:x", "Libby, tea?")]; let unnamed = [line(TOBIAH, "#minds:x", "what a day")]; let human = [line("@deru:x", "#minds:x", "kettle news")]; let elsewhere = [line("@deru:x", "#other:x", "kettle news")]; - assert_eq!(reason(&named, Wake::HumansInRooms, false), Some("addressed")); + assert_eq!(reason(&named, Wake::HumansInRooms, false), Some("addressed by a Mind")); + let by_id = [line(TOBIAH, "#minds:x", "ask @libby:gamecult.org about it")]; + let (why, by) = wake_reason(&by_id, 0, &libby, &cast, Wake::HumansInRooms, false).unwrap(); + assert_eq!((why, by[0]["how"].as_str()), ("addressed by a Mind", Some("id as text"))); + assert_eq!(wake_reason(&named, 0, &libby, &cast, Wake::HumansInRooms, false).unwrap().1[0]["how"], "name in text"); + let both = [line(TOBIAH, "#minds:x", "Libby, tea?"), line("@deru:x", "#minds:x", "Libby, kettle?")]; + assert_eq!(reason(&both, Wake::HumansInRooms, false), Some("addressed by a human"), "a human's address outranks a Mind's"); assert_eq!(reason(&unnamed, Wake::HumansInRooms, false), None, "another Mind's unaddressed line wakes no one"); assert_eq!(reason(&unnamed, Wake::AllMessages, false), Some("a message in her rooms")); assert_eq!(reason(&human, Wake::HumansInRooms, false), Some("a human in her rooms")); assert_eq!(reason(&elsewhere, Wake::HumansInRooms, false), None, "not her room"); assert_eq!(reason(&unnamed, Wake::HumansInRooms, true), Some("the beat")); - assert_eq!(wake_reason(&named, 1, &libby, &cast, Wake::HumansInRooms, false), None, "a line she has seen is not new"); + assert!(wake_reason(&named, 1, &libby, &cast, Wake::HumansInRooms, false).is_none(), "a line she has seen is not new"); }