chat: review fixes (stale corpus refusal, age parsing, mutant-killing tests, CRLF restored)

- A collection built for another corpus set is refused with a stale-index
  error on every read (listing included), not answered with an empty page.
- --after/--before ages parse on char boundaries and overflow is an error.
- Tests for inclusive span bounds, scroll order/filter, nearest filter and
  the corpus and ts_end_ms indexes; all ten surviving mutants now caught.
- index/tests.rs keeps the base file's CRLF endings (diff is content only).
- Drop the exclusion claim from the CLI help; document the listing limit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Eureka 2026-11-11 18:56:05 +00:00
commit f72966ce11
6 changed files with 2092 additions and 1995 deletions

View file

@ -13,7 +13,7 @@ pub fn instant_ms(flag: &str, text: &str, now: DateTime<Utc>) -> Result<u64, Tro
let moment = if let Ok(time) = DateTime::parse_from_rfc3339(text) {
time.with_timezone(&Utc)
} else {
let (digits, unit) = text.split_at(text.len().saturating_sub(1));
let (digits, unit) = text.split_at(text.char_indices().next_back().map_or(0, |(at, _)| at));
if digits.is_empty() || !digits.bytes().all(|byte| byte.is_ascii_digit()) {
return Err(wrong());
}
@ -26,7 +26,7 @@ pub fn instant_ms(flag: &str, text: &str, now: DateTime<Utc>) -> Result<u64, Tro
"w" => 604_800,
_ => return Err(wrong()),
};
now - chrono::Duration::try_seconds(i64::from(count) * seconds).ok_or_else(wrong)?
now.checked_sub_signed(chrono::Duration::try_seconds(i64::from(count) * seconds).ok_or_else(wrong)?).ok_or_else(wrong)?
};
u64::try_from(moment.timestamp_millis()).map_err(|_| wrong())
}
@ -50,7 +50,7 @@ mod tests {
assert_eq!(at("6h").unwrap(), "2026-11-11T06:00:00.000Z");
assert_eq!(at("2d").unwrap(), "2026-11-09T12:00:00.000Z");
assert_eq!(at("90s").unwrap(), "2026-11-11T11:58:30.000Z");
for bad in ["", "h", "6", "6x", "-2d", "yesterday", "1e3h", "+2d"] {
for bad in ["", "h", "6", "6x", "-2d", "yesterday", "1e3h", "+2d", "6日", "日", "4000000000w", "99999999999999999999d"] {
assert!(matches!(at(bad), Err(Trouble::InvalidInput(ref detail)) if detail.contains("--after")), "{bad}");
}
}

View file

@ -74,7 +74,7 @@ struct Cli {
#[derive(Subcommand, Debug)]
enum ChatCommand {
/// Chunks of the joined rooms (never DMs or Discord), each citing its room,
/// Chunks of the indexed rooms, each citing its room,
/// event ids and times. With texts, ranked by meaning; with none, a listing
/// oldest first, at most --top-k, ending in the `--after` to continue from.
Search {

File diff suppressed because it is too large Load diff

View file

@ -568,7 +568,9 @@ fn distinct_texts(asked: Vec<&str>) -> Vec<&str> {
/// The label of the collection a daemon is about to read, if it was built
/// under the model this daemon embeds with. Absent, unlabelled and other-model
/// collections are loud refusals: vectors of two models do not compare, and an
/// empty page is not an answer.
/// empty page is not an answer. A collection built for another corpus set (an
/// old forge-only one) is refused the same way until the rebuild lands, since
/// it would answer a chat search with an empty page.
fn readable_under_this_model<E: Embedder, S: SourceIndex>(embedder: &mut E, index: &mut S, budget: &Budget) -> Result<(SourcesMeta, ModelIdentity)> {
let built = built_label(index, budget)?;
let identity = embedder.model_identity(budget.left()?).context("reading the embedding model's identity")?;
@ -589,7 +591,14 @@ fn built_label<S: SourceIndex>(index: &mut S, budget: &Budget) -> Result<Sources
match index.describe_sources(budget.left()?).context("describing the source collection")? {
DescribedSources::Absent => bail!("source index not built"),
DescribedSources::Unlabelled => bail!("source index not built: {COLLECTION} carries no Huginn metadata"),
DescribedSources::Labelled { meta, .. } => Ok(meta),
DescribedSources::Labelled { meta, .. } => {
ensure!(
meta.corpus == CORPUS,
"the source index is stale: it was built for corpus {} and this daemon reads {CORPUS}; the rebuild is pending",
meta.corpus
);
Ok(meta)
}
}
}

View file

@ -936,3 +936,41 @@ fn the_chat_read_path_holds_no_discord_matrix_or_second_fuse() {
}
assert_eq!(source.matches("pub fn fuse").count(), 1);
}
/// An old forge-only collection answers both searches with the stale-index
/// error until the rebuild lands, not with an empty page.
#[test]
fn an_old_corpus_collection_is_refused_until_it_is_rebuilt() {
let (mut rig, mut embedder) = chat_rig(three_rooms());
let Some(DescribedSources::Labelled { mut meta, cursors }) = rig.index.state.lock().unwrap().described.clone() else { panic!() };
meta.corpus = "forge:GameCult".into();
rig.index.plant(DescribedSources::Labelled { meta, cursors });
let error = format!("{:#}", search(&mut embedder, &mut rig.index, &query("one", 3, None), WITHIN).unwrap_err());
assert!(error.contains("stale") && error.contains("forge:GameCult") && error.contains("rebuild is pending"), "{error}");
for texts in [&[][..], &["topic-0"][..]] {
let error = format!("{:#}", search_chat(&mut embedder, &mut rig.index, &chat_query(texts, &[], None, None, 3), WITHIN).unwrap_err());
assert!(error.contains("stale") && error.contains("rebuild is pending"), "{error}");
}
}
/// A message exactly on `after` or `before` is in the span, and so is a chunk
/// whose end is exactly `after` or whose start is exactly `before`.
#[test]
fn the_span_bounds_are_inclusive_for_chunks_and_for_messages() {
let (mut rig, mut embedder) = chat_rig(three_rooms());
let mut hits = |after, before| {
let found = search_chat(&mut embedder, &mut rig.index, &chat_query(&[], &["#swarm:x"], after, before, 10), WITHIN).unwrap();
found.hits.iter().map(|hit| (hit.first_event_id.clone(), hit.messages.len())).collect::<Vec<_>>()
};
let owned = |pairs: &[(&str, usize)]| pairs.iter().map(|(id, n)| ((*id).to_owned(), *n)).collect::<Vec<_>>();
// Chunk $a1..$a2 ends at 200: after=200 keeps it, trimmed to the message on the bound.
assert_eq!(hits(Some(200), None), owned(&[("$a2", 1), ("$a3", 2)]));
// Chunk $a3..$a4 starts at 300: before=300 keeps it, trimmed to the message on the bound.
assert_eq!(hits(None, Some(300)), owned(&[("$a1", 2), ("$a3", 1)]));
// Both bounds on one message.
assert_eq!(hits(Some(200), Some(200)), owned(&[("$a2", 1)]));
let filter = ChatFilter { rooms: vec![], after_ms: Some(200), before_ms: Some(300) };
assert!(filter.keeps("!a:x", None, 100, 200) && filter.keeps("!a:x", None, 300, 400));
assert!(!filter.keeps("!a:x", None, 100, 199) && !filter.keeps("!a:x", None, 301, 400));
}

View file

@ -157,3 +157,9 @@ ingest lands the collection holds no chat points and a chat search answers empty
In a listing across rooms, the last hit's `ts_end_ms + 1` can repeat messages of
an earlier-starting longer chunk of another room; within one room it continues
exactly.
Known limits (owner of the fix: cut chat-window). Hits are ordered by their
trimmed start, not by the stored chunk start, and a long chunk trimmed to a span
that crosses rooms can be listed again by the continuation `--after`. Behaviour
is deliberately unchanged here; the event-id window in cut chat-window replaces
the time cursor.