transcript: fix close-brace finder to track string boundaries

The backward JSON scanner (JsonlBackwardIter and TailMessages) was
matching } characters inside JSON strings — code blocks full of Rust
braces being the primary offender. This caused:

- Quadratic retry behavior on code-heavy transcripts (wrong object
  boundaries → serde parse failure → retry from different position)
- Inconsistent find_last_compaction_in_file offsets across calls,
  making detect_new_compaction fire repeatedly → context reload on
  every hook call → seen set growing without bound

Fix: add string-boundary tracking with escaped-quote handling to
the close-brace finder loop, matching the existing logic in the
depth-tracking loop.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
ProofOfConcept 2026-03-24 12:27:22 -04:00
parent aa46b1d5a6
commit 38816dc56e

View file

@ -32,14 +32,31 @@ impl<'a> Iterator for JsonlBackwardIter<'a> {
type Item = &'a [u8]; type Item = &'a [u8];
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
// Find the closing } of the next object // Find the closing } of the next object, skipping } inside strings
let close = loop { let close = {
let mut in_string = false;
loop {
let p = memrchr3(b'{', b'}', b'"', &self.data[..self.pos])?; let p = memrchr3(b'{', b'}', b'"', &self.data[..self.pos])?;
self.pos = p; self.pos = p;
if self.data[p] == b'}' { let ch = self.data[p];
break p;
if in_string {
if ch == b'"' {
let mut bs = 0;
while p > bs + 1 && self.data[p - 1 - bs] == b'\\' {
bs += 1;
}
if bs % 2 == 0 { in_string = false; }
}
continue;
}
match ch {
b'}' => break p,
b'"' => in_string = true,
_ => {}
}
} }
// Skip past any { or " that aren't our closing brace
}; };
// Track brace depth to find matching { // Track brace depth to find matching {
@ -164,11 +181,31 @@ impl Iterator for TailMessages {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
loop { loop {
// Find closing } // Find closing }, skipping } inside strings
let close = loop { let close = {
let mut in_string = false;
loop {
let p = memrchr3(b'{', b'}', b'"', &self.mmap[..self.pos])?; let p = memrchr3(b'{', b'}', b'"', &self.mmap[..self.pos])?;
self.pos = p; self.pos = p;
if self.mmap[p] == b'}' { break p; } let ch = self.mmap[p];
if in_string {
if ch == b'"' {
let mut bs = 0;
while p > bs + 1 && self.mmap[p - 1 - bs] == b'\\' {
bs += 1;
}
if bs % 2 == 0 { in_string = false; }
}
continue;
}
match ch {
b'}' => break p,
b'"' => in_string = true,
_ => {}
}
}
}; };
// Track brace depth to find matching { // Track brace depth to find matching {