chat job: route journal lines by the typed Entry, not by string prefix; a failed room exits 1 on stderr
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
1af5b69e3f
commit
c5ccac64b4
4 changed files with 81 additions and 19 deletions
|
|
@ -134,6 +134,39 @@ impl std::fmt::Display for ChatReport {
|
|||
}
|
||||
}
|
||||
|
||||
/// One line of a chat run's journal, typed so the job routes by what happened and not by how the line reads.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Entry {
|
||||
/// A room's report; a failed one is an alarm.
|
||||
Report(ChatReport),
|
||||
Refused(RefusedRoom),
|
||||
Scrubbed(ScrubHit),
|
||||
/// A failure outside any one room's report: retiring rooms.
|
||||
Trouble(String),
|
||||
}
|
||||
|
||||
impl Entry {
|
||||
/// The operator must see this on stderr: a failure, a refusal, a scrub hit.
|
||||
pub fn is_alarm(&self) -> bool {
|
||||
match self {
|
||||
Self::Report(report) => report.failure.is_some(),
|
||||
Self::Refused(_) | Self::Scrubbed(_) | Self::Trouble(_) => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Entry {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Report(report) => report.fmt(formatter),
|
||||
Self::Refused(RefusedRoom { room_id, reason, event_id: None }) => write!(formatter, "refused: {room_id} {reason}"),
|
||||
Self::Refused(RefusedRoom { room_id, reason, event_id: Some(event) }) => write!(formatter, "refused: {room_id} {reason} [{event}]"),
|
||||
Self::Scrubbed(hit) => write!(formatter, "scrubbed: {} {} {}", hit.room_id, hit.event_id, hit.kind),
|
||||
Self::Trouble(why) => write!(formatter, "huginn-sources: {why}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A joined room the scope refused, whole, and why; `event_id` when an event did it.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct RefusedRoom {
|
||||
|
|
@ -562,7 +595,7 @@ pub fn refresh_chat<E: Embedder, S: SourceIndex, H: RoomHistory>(
|
|||
index: &mut S,
|
||||
history: &mut H,
|
||||
estate: &Estate,
|
||||
journal: &mut dyn FnMut(String),
|
||||
journal: &mut dyn FnMut(Entry),
|
||||
) -> Result<ChatRefreshed> {
|
||||
let DescribedSources::Labelled { meta, .. } = index.describe_sources(INDEX_CALL_TIMEOUT).context("describing the source collection")? else {
|
||||
bail!("the source collection is absent or not Huginn's; the forge refresh creates it and chat never does");
|
||||
|
|
@ -589,13 +622,14 @@ pub fn refresh_chat<E: Embedder, S: SourceIndex, H: RoomHistory>(
|
|||
Ok(Err(reason)) => {
|
||||
// Not kept, so the retirement below purges whatever it held.
|
||||
keep.remove(room_id);
|
||||
journal(format!("refused: {room_id} {reason}"));
|
||||
out.refused.push(RefusedRoom { room_id: room_id.clone(), reason, event_id: None });
|
||||
let refused = RefusedRoom { room_id: room_id.clone(), reason, event_id: None };
|
||||
journal(Entry::Refused(refused.clone()));
|
||||
out.refused.push(refused);
|
||||
}
|
||||
// An error is not a reclassification: the room keeps its points and cursor, and the report says why.
|
||||
Err(error) => {
|
||||
let report = ChatReport::failed(room_id, None, &error.context("reading the room's facts"));
|
||||
journal(report.to_string());
|
||||
journal(Entry::Report(report.clone()));
|
||||
out.failed.push(room_id.clone());
|
||||
out.rooms.push(report);
|
||||
}
|
||||
|
|
@ -604,35 +638,35 @@ pub fn refresh_chat<E: Embedder, S: SourceIndex, H: RoomHistory>(
|
|||
match retire_chat(index, &keep) {
|
||||
Ok(done) => {
|
||||
for (room, why) in done.failed {
|
||||
journal(format!("huginn-sources: retiring {room} failed, the others go on: {why}"));
|
||||
journal(Entry::Trouble(format!("retiring {room} failed, the others go on: {why}")));
|
||||
out.failed.push(room);
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
journal(format!("huginn-sources: retiring rooms failed: {error:#}"));
|
||||
journal(Entry::Trouble(format!("retiring rooms failed: {error:#}")));
|
||||
out.failed.push("retire".to_owned());
|
||||
}
|
||||
}
|
||||
for facts in admitted {
|
||||
match ingest_room(embedder, index, history, identity.dimensions, &facts, &mut out) {
|
||||
Ok(Outcome::Done(report)) => {
|
||||
journal(report.to_string());
|
||||
journal(Entry::Report(report.clone()));
|
||||
out.rooms.push(report);
|
||||
}
|
||||
Ok(Outcome::Refused(refused)) => {
|
||||
journal(format!("refused: {} {} [{}]", refused.room_id, refused.reason, refused.event_id.as_deref().unwrap_or("")));
|
||||
journal(Entry::Refused(refused.clone()));
|
||||
out.refused.push(refused);
|
||||
}
|
||||
Err(error) => {
|
||||
let report = ChatReport::failed(&facts.room_id, facts.alias.clone(), &error);
|
||||
journal(report.to_string());
|
||||
journal(Entry::Report(report.clone()));
|
||||
out.failed.push(facts.room_id.clone());
|
||||
out.rooms.push(report);
|
||||
}
|
||||
}
|
||||
}
|
||||
for hit in &out.scrubbed {
|
||||
journal(format!("scrubbed: {} {} {}", hit.room_id, hit.event_id, hit.kind));
|
||||
journal(Entry::Scrubbed(hit.clone()));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -141,7 +141,7 @@ impl Rig {
|
|||
|
||||
fn run(&mut self, history: &mut FakeHistory) -> (Result<ChatRefreshed>, Vec<String>) {
|
||||
let mut journal = Vec::new();
|
||||
let out = refresh_chat(&mut self.embedder, &mut self.index, history, &self.estate, &mut |line| journal.push(line));
|
||||
let out = refresh_chat(&mut self.embedder, &mut self.index, history, &self.estate, &mut |line| journal.push(line.to_string()));
|
||||
(out, journal)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ use std::path::Path;
|
|||
use anyhow::{Context, Result, bail, ensure};
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
use super::chat::{RoomHistory, refresh_chat, retire_chat};
|
||||
use super::chat::{Entry, RoomHistory, refresh_chat, retire_chat};
|
||||
use super::chat_purge::{PurgeWindow, parse_windows, purge_key, request_purge};
|
||||
use super::room_scope::Estate;
|
||||
use super::{DescribedSources, Forge, SourceIndex, refresh};
|
||||
|
|
@ -23,11 +23,6 @@ pub struct Chat<'a, H> {
|
|||
pub estate: &'a Estate,
|
||||
}
|
||||
|
||||
/// A chat journal line the operator must see on stderr rather than read as a room report.
|
||||
fn is_alarm(line: &str) -> bool {
|
||||
line.starts_with("refused:") || line.starts_with("scrubbed:") || line.starts_with("huginn-sources:")
|
||||
}
|
||||
|
||||
/// One run: the forge refresh, then chat (`Some`) or the retirement of every chat point (`None`).
|
||||
/// `say` takes the journal's report lines (stdout), `complain` the failures, refusals and scrub hits (stderr).
|
||||
/// A forge failure never skips the chat run and a chat failure never hides a forge one.
|
||||
|
|
@ -60,7 +55,7 @@ pub fn run<E: Embedder, S: SourceIndex, F: Forge, H: RoomHistory>(
|
|||
}
|
||||
match chat {
|
||||
Some(Chat { history, estate }) => {
|
||||
let mut line = |line: String| if is_alarm(&line) { complain(line) } else { say(line) };
|
||||
let mut line = |entry: Entry| if entry.is_alarm() { complain(entry.to_string()) } else { say(entry.to_string()) };
|
||||
match refresh_chat(embedder, index, history, estate, &mut line) {
|
||||
Ok(refreshed) => clean &= refreshed.is_clean(),
|
||||
Err(error) => {
|
||||
|
|
@ -121,7 +116,7 @@ impl Purge {
|
|||
}
|
||||
}
|
||||
|
||||
/// Writes the tombstone and returns the line the admin sees. Touches no point and makes no Matrix call;
|
||||
/// Writes the tombstone and returns the line the admin sees; the free-text request is quoted and escaped so it cannot forge a journal line. Touches no point and makes no Matrix call;
|
||||
/// the caller holds the job's lock. The next run applies it.
|
||||
pub fn purge<S: SourceIndex>(index: &mut S, purge: &Purge) -> Result<String> {
|
||||
request_purge(index, &purge.room_id, purge.window)?;
|
||||
|
|
|
|||
|
|
@ -218,3 +218,36 @@ fn exit_status_is_clean_only_when_forge_and_chat_both_are() {
|
|||
assert!(complained.iter().any(|line| line.contains("the forge is down")), "{complained:?}");
|
||||
assert!(said.iter().any(|line| line.contains("(!swarm:s)")), "chat ran after the forge failed: {said:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_room_report_goes_to_the_complaint_stream_and_fails_the_run() {
|
||||
let mut rig = Rig::new();
|
||||
struct FactsDown(Rooms);
|
||||
impl RoomHistory for FactsDown {
|
||||
fn joined_rooms(&mut self) -> Result<Vec<String>> {
|
||||
Ok(vec!["!swarm:s".to_owned()])
|
||||
}
|
||||
fn facts(&mut self, _room_id: &str) -> Result<super::room_scope::RoomFacts> {
|
||||
bail!("facts are down")
|
||||
}
|
||||
fn events_after(&mut self, _room_id: &str, _after: Option<&str>, _limit: u32) -> Result<Option<super::chat::EventPage>> {
|
||||
bail!("unreachable")
|
||||
}
|
||||
}
|
||||
let out = rig.go(Some(&mut FactsDown(rooms(vec![]))));
|
||||
assert_eq!(out.status, 1);
|
||||
assert!(out.complained.iter().any(|line| line.contains("!swarm:s") && line.contains("facts are down")), "{:?}", out.complained);
|
||||
assert!(!out.said.iter().any(|line| line.contains("facts are down")), "{:?}", out.said);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chat_purge_makes_no_matrix_request() {
|
||||
let world = world();
|
||||
let (base, asked) = synapse(&world);
|
||||
let mut rig = Rig::new();
|
||||
rig.go(Some(&mut HttpMatrix::new(&base, TOKEN)));
|
||||
let made = asked.seen().len();
|
||||
let request = Purge::check("!swarm:s", "2026-11-10T00:00:00Z", "2026-11-11T00:00:00Z", "r", now()).unwrap();
|
||||
purge(&mut rig.index, &request).unwrap();
|
||||
assert_eq!(asked.seen().len(), made);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue