Fix batch 8a-fix2: library seams PA.f89, PA.f92, PA.f88 note

PA.f89: SourceSpan::locate (ghostlight-persona-projection/src/lib.rs) used
str::match_indices, which never reports overlapping matches, so a
boundary-invalid candidate at an earlier byte offset hid a later,
boundary-valid candidate that overlapped it: locate("Juno no no", "no no")
and locate("xab ab ab", "ab ab") both returned None. Fixed by scanning with
find() from an advancing offset and, on a rejected candidate, resuming one
char past that candidate's own start rather than skipping past its end. The
UAX #29 boundary rule itself is unchanged; whitespace-only and
punctuation-only spans still locate.

PA.f92: WorldPatch::sets_persona_of_existing_subject (ghostlight/src/patch.rs)
had only single-op fixtures, so a .take(1) mutation on its operations scan
would have passed every test. Added a multi-op test where a harmless
Relocate sits at index 0 and the persona write to an existing subject arrives
at index 1.

PA.f88 (partial, library side): the PLAY_TOOLS fixture in
ghostlight/src/table.rs is the library's own test copy, not a policy owner;
added a one-line comment saying so and naming Dungeon's PLAY_TOOLS in
ghostlight-dungeon/src/play.rs as the actual owner. The Dungeon-side pin test
lands in the Dungeon commit.

Tests: cargo test -p ghostlight-persona-projection (30 passed, was 26) and
cargo test -p ghostlight --lib (622 passed, was 621). Both new/changed tests
were run against their own reverting mutation (match_indices restored;
.take(1) added) and failed as expected before being restored.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-22 17:33:56 +02:00
commit 3d6a58e958
3 changed files with 108 additions and 11 deletions

View file

@ -371,6 +371,18 @@ impl SourceSpan {
/// offsets `unicode-segmentation` reports for `source`, or `source.len()`;
/// a first occurrence that fails either edge is skipped in favor of a
/// later one.
///
/// PA.f89: `str::match_indices` only ever reports non-overlapping
/// matches, so a candidate whose start lies inside an earlier, rejected
/// candidate's own span is never even offered — `locate("Juno no no",
/// "no no")` used to return `None`, because the boundary-valid match at
/// byte 5 overlaps the boundary-invalid one `match_indices` yields first
/// at byte 2. The search below finds a candidate the same way, but on a
/// rejection resumes the scan one *char* past that candidate's own start
/// (never past its end), so an overlapping later candidate is still
/// reachable. A span made only of whitespace or punctuation is unaffected:
/// it is still accepted whenever its own edges land on a word boundary,
/// exactly as before.
pub fn locate(source: &str, quote: &str) -> Option<Self> {
if quote.is_empty() {
return None;
@ -380,17 +392,28 @@ impl SourceSpan {
.map(|(byte, _)| byte)
.collect();
boundaries.insert(source.len());
source
.match_indices(quote)
.find(|(start_byte, matched)| {
boundaries.contains(start_byte)
&& boundaries.contains(&(start_byte + matched.len()))
})
.map(|(start_byte, matched)| Self {
start_byte,
end_byte: start_byte + matched.len(),
verbatim: matched.to_owned(),
})
let mut search_from = 0usize;
loop {
let haystack = source.get(search_from..)?;
let relative_start = haystack.find(quote)?;
let start_byte = search_from + relative_start;
let end_byte = start_byte + quote.len();
if boundaries.contains(&start_byte) && boundaries.contains(&end_byte) {
return Some(Self {
start_byte,
end_byte,
verbatim: quote.to_owned(),
});
}
// Resume one char past this candidate's own start, not past its
// end, so a later candidate overlapping this one is still found.
let mut chars = source[start_byte..].char_indices();
chars.next();
search_from = match chars.next() {
Some((offset, _)) => start_byte + offset,
None => return None,
};
}
}
pub fn whole(source: &str) -> Self {
@ -862,6 +885,48 @@ mod tests {
assert_eq!(SourceSpan::locate("Stop.Now", "Stop."), None);
}
/// PA.f89: `match_indices` never reports overlapping matches, so the
/// boundary-valid "no no" starting at byte 5 was unreachable once the
/// boundary-invalid one starting at byte 2 (inside "Juno") was tried
/// first and rejected — `locate` used to return `None` here. Mutation:
/// reverting to `source.match_indices(quote).find(...)` fails this.
#[test]
fn an_overlapping_later_candidate_is_found_after_a_rejected_earlier_one() {
let source = "Juno no no";
let located = SourceSpan::locate(source, "no no").expect("the overlapping later candidate locates");
assert_eq!(located.start_byte(), 5);
assert_eq!(located.end_byte(), source.len());
assert_eq!(located.verbatim(), "no no");
}
/// The same defect, checked against the spec's own second example.
#[test]
fn a_second_overlapping_candidate_is_found_after_a_rejected_first() {
let source = "xab ab ab";
let located = SourceSpan::locate(source, "ab ab").expect("the overlapping later candidate locates");
assert_eq!(located.start_byte(), 4);
assert_eq!(located.end_byte(), source.len());
assert_eq!(located.verbatim(), "ab ab");
}
/// A span made only of whitespace still locates when its own edges land
/// on a word boundary: the UAX #29 rule is unchanged by the overlap fix.
#[test]
fn a_whitespace_only_span_is_still_accepted() {
let source = "left right";
let located = SourceSpan::locate(source, " ").expect("a whitespace-only quote locates");
assert_eq!(located.verbatim(), " ");
}
/// A span made only of punctuation still locates when its own edges land
/// on a word boundary: the UAX #29 rule is unchanged by the overlap fix.
#[test]
fn a_punctuation_only_span_is_still_accepted() {
let source = "Wait... now";
let located = SourceSpan::locate(source, "...").expect("a punctuation-only quote locates");
assert_eq!(located.verbatim(), "...");
}
#[test]
fn persisted_exact_turn_rehydrates_with_the_same_receipt() {
let recorded = turn("I tell Mara, \"The western brace is giving way.\"");

View file

@ -7770,6 +7770,34 @@ mod tests {
assert!(!draft.sets_persona_of_existing_subject());
}
/// PA.f92: a single-op fixture only ever exercises `operations[0]`, so a
/// `.take(1)` mutation on the scan inside `sets_persona_of_existing_subject`
/// would still pass every test above. Here op 0 is a harmless `Relocate`
/// with no bearing on personas at all, and the persona write to an
/// existing subject arrives only in op 1 — a query that stopped after the
/// first operation would miss it and wrongly report `false`.
#[test]
fn sets_persona_of_existing_subject_scans_every_operation_not_just_the_first() {
let patch = WorldPatch {
declarations: Vec::new(),
operations: vec![
ComponentOp::Relocate {
subject: Ref::Draft(DraftHandle::new("wanderer")),
via: Ref::Draft(DraftHandle::new("path")),
},
ComponentOp::SetPersonaMaterial {
subject: Ref::Existing(SubjectId::issue()),
values: Vec::new(),
voice: Statement::new("flat and even").unwrap(),
memories: Vec::new(),
reads: Vec::new(),
},
],
evidence: Vec::new(),
};
assert!(patch.sets_persona_of_existing_subject());
}
fn entity(handle: &str, label: &str, kind: EntityKind) -> Declaration {
Declaration::Entity(EntityDeclaration {
handle: DraftHandle::new(handle),

View file

@ -1583,6 +1583,10 @@ mod tests {
/// test.
// PA.f75: `communicate` was removed from `PLAY_TOOLS`; Dungeon drops it
// from its own list too. This is now the complete 24-tool list.
// PA.f88: this is the library's own test fixture, kept in sync by hand.
// Dungeon's `PLAY_TOOLS` in `ghostlight-dungeon/src/play.rs` is the
// single policy owner of what the play agent may actually call; this
// copy pins nothing there and is not read by production code.
const PLAY_TOOLS: &[&str] = &[
"relocate",
"transfer",