🐛 fix(memory): require newline after context tag

This commit is contained in:
墨綠BG
2026-05-02 12:26:46 +08:00
committed by kshitij
parent 341c8d3030
commit 50e93f23f2
2 changed files with 46 additions and 7 deletions

View File

@@ -126,7 +126,10 @@ class StreamingContextScrubber:
idx = self._find_boundary_open_tag(buf)
if idx == -1:
# No open tag — hold back a potential partial open tag
held = self._max_partial_suffix(buf, self._OPEN_TAG)
held = (
self._max_pending_open_suffix(buf)
or self._max_partial_suffix(buf, self._OPEN_TAG)
)
if held:
self._append_visible(out, buf[:-held])
self._buf = buf[-held:]
@@ -179,10 +182,25 @@ class StreamingContextScrubber:
idx = buf_lower.find(self._OPEN_TAG, search_start)
if idx == -1:
return -1
if self._is_block_boundary(buf, idx):
if self._is_block_boundary(buf, idx) and self._has_block_opener_suffix(buf, idx):
return idx
search_start = idx + 1
def _max_pending_open_suffix(self, buf: str) -> int:
"""Hold a complete boundary tag until the following char confirms it."""
if not buf.lower().endswith(self._OPEN_TAG):
return 0
idx = len(buf) - len(self._OPEN_TAG)
if not self._is_block_boundary(buf, idx):
return 0
return len(self._OPEN_TAG)
def _has_block_opener_suffix(self, buf: str, idx: int) -> bool:
after_idx = idx + len(self._OPEN_TAG)
if after_idx >= len(buf):
return False
return buf[after_idx] in "\r\n"
def _is_block_boundary(self, buf: str, idx: int) -> bool:
if idx == 0:
return self._at_block_boundary