Studio: strip invalid document citations that contain brackets

The invalid-citation regex stopped at the first closing bracket, so a
citation whose filename contained brackets left its tail (".pdf, p. 9]") in
the report. Match a balanced bracketed span so the whole invalid citation is
removed; valid citations stay protected by the earlier tokenization pass.
This commit is contained in:
danielhanchen 2026-07-22 09:00:51 +00:00
commit a3b4fbc1e6
2 changed files with 11 additions and 1 deletions

View file

@ -42,7 +42,7 @@ _SOURCES_HEADING = re.compile(
_NUMBERED_CITATION = re.compile(r"(?<!\^)\[(\d+)]")
_AUTOLINK = re.compile(r"<(https?://[^>\s]+)>")
_RAW_URL = re.compile(r"https?://[^\s<>]+")
_DOCUMENT_CITATION = re.compile(r"\[Document:[^\]]+\]")
_DOCUMENT_CITATION = re.compile(r"\[Document:(?:[^\[\]]+|\[[^\[\]]*\])*\]")
# Wrapper delimiters used in the decision/synthesis prompts. Any occurrence inside
# untrusted evidence is escaped so gathered content cannot close a block early.
_PROMPT_DELIMITER_TAGS = re.compile(

View file

@ -111,6 +111,16 @@ def test_document_citation_strips_unknown_source():
assert "not-a-real-file" not in out
def test_document_citation_strips_unknown_source_with_brackets():
# An invalid citation whose filename contains brackets must be removed whole; the old regex
# stopped at the first ``]`` and left the tail (".pdf, p. 9]") behind.
report = "Ghost cite [Document: invented [final].pdf, p. 9] end."
out = _validate_report_document_sources(report, [{"filename": "real.pdf", "page": 1}])
assert "invented" not in out
assert ".pdf" not in out
assert out == "Ghost cite end."
def _make_payload(**overrides) -> CreateResearchRun:
payload = {"threadId": "t1", "userMessageId": "u1", "inferenceRequest": {"model": "m"}}
payload.update(overrides)