From b29a56e445ce95416101990dacbed49d42811817 Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Tue, 20 Oct 2026 03:05:20 -0500 Subject: [PATCH] fix(workflow): check draft links past inner code fences (#393) fenced_blocks() used a naive open/close toggle, so a trigger-issue draft embedding a fenced code example was split at the inner fence: the pointer fragment carried no workflow:issue marker and the marker fragment carried no links, so neither was link-checked. An unclosed draft fence was dropped entirely. The same toggle in unfenced() also leaked the embedded example's contents into prose. Replace both toggles with one nesting-aware scanner, fence_events(): a fence line with an info string can never close a block (a CommonMark closing fence has none), so it opens a nested level; only a bare fence at least as long as the innermost one closes a level. fenced_blocks() now also returns a trailing unclosed block, and issue_drafts() strips nested blocks whole so embedded code stays exempt from link checks. Regression tests: nested draft with a valid pointer passes (with a link-shaped line inside the code example, which stays exempt), the same draft with the #382 broken pointer fails, and an unclosed draft with the broken pointer fails. All three fail against the pre-fix checker; the broken-pointer tests keep link-shaped text out of the code example so they cannot pass for the wrong reason (the pre-fix prose leak). --- scripts/research_workflow.py | 86 ++++++++++++++++++------- scripts/tests/test_research_workflow.py | 56 ++++++++++++++++ 2 files changed, 117 insertions(+), 25 deletions(-) diff --git a/scripts/research_workflow.py b/scripts/research_workflow.py index 771940daae..cf2384dba6 100644 --- a/scripts/research_workflow.py +++ b/scripts/research_workflow.py @@ -404,20 +404,48 @@ def anchor(text): return re.sub(r"[^\w\- ]", "", text.lower()).replace(" ", "-") +def fence_events(text): + """Classify each line as text, open, content or close of a fence. + + Fences nest: a fence line carrying an info string (e.g. ```python) + can never close a block — a CommonMark closing fence has no info + string — so inside a block it opens a nested fence instead, and + only a bare fence at least as long as the innermost one closes a + level. A naive open/close toggle splits a block at the first + inner fence (#393): a trigger-issue draft embedding a code example + fell into two fragments, neither carrying both its links and its + trailing workflow:issue marker, so neither was checked; the code + example's own contents also leaked out and were read as prose. + """ + stack = [] + for line in text.splitlines(): + found = re.match(r"^ {0,3}(`{3,}|~{3,})\s*(.*?)\s*$", line) + if not found: + yield ("content" if stack else "text"), line + continue + token, info = found[1], found[2] + if not stack: + stack = [token] + yield "open", line + elif token[0] != stack[-1][0]: + # A fence of the other character is literal block content. + yield "content", line + elif not info and len(token) >= len(stack[-1]): + stack.pop() + # An inner fence's closing line stays in the outer block + # so the nested block can be stripped whole downstream. + yield ("content" if stack else "close"), line + else: + # An info-string fence opens a nested block; a bare fence + # shorter than the innermost one is its literal content. + if info: + stack.append(token) + yield "content", line + + def unfenced(text): """Exclude raw evidence/code from authored workflow syntax and link checks.""" - result, fence = [], None - for line in text.splitlines(): - found = re.match(r"^ {0,3}(`{3,}|~{3,})", line) - if found: - token = found[1] - if fence is None: - fence = token - elif token[0] == fence[0] and len(token) >= len(fence): - fence = None - elif fence is None: - result.append(line) - return "\n".join(result) + return "\n".join(line for kind, line in fence_events(text) if kind == "text") def prose(text): @@ -426,19 +454,23 @@ def prose(text): def fenced_blocks(text): - """Yield the contents of each fenced code block, in order.""" - blocks, fence, current = [], None, [] - for line in text.splitlines(): - found = re.match(r"^ {0,3}(`{3,}|~{3,})", line) - if found: - token = found[1] - if fence is None: - fence, current = token, [] - elif token[0] == fence[0] and len(token) >= len(fence): - blocks.append("\n".join(current)) - fence = None - elif fence is not None: + """Return the contents of each fenced code block, in order. + + A trailing block whose fence is never closed is returned as well, + so a trigger-issue draft in an unclosed fence is checked rather + than silently dropped (#393). + """ + blocks, current = [], None + for kind, line in fence_events(text): + if kind == "open": + current = [] + elif kind == "content" and current is not None: current.append(line) + elif kind == "close": + blocks.append("\n".join(current)) + current = None + if current is not None: + blocks.append("\n".join(current)) return blocks @@ -452,7 +484,11 @@ def issue_drafts(text): """ for block in fenced_blocks(text): if "workflow:issue" in block: - clean = re.sub(r"", "", block, flags=re.S) + # Nested fenced blocks inside a draft are embedded code + # examples, not draft prose: strip them whole (as + # unfenced() does at document level) so their contents are + # not link-checked either (#393). + clean = re.sub(r"", "", unfenced(block), flags=re.S) yield re.sub(r"(`+)(?:(?!\1).)+?\1", "", clean) diff --git a/scripts/tests/test_research_workflow.py b/scripts/tests/test_research_workflow.py index ef75279fb0..b17ee0149d 100644 --- a/scripts/tests/test_research_workflow.py +++ b/scripts/tests/test_research_workflow.py @@ -807,6 +807,62 @@ def test_fenced_issue_draft_with_a_broken_pointer_fails_check(self): self.append_issue_draft(package, "concepts/C001-source-preservation.md") self.check(package, ok=False) + def append_issue_draft_with_inner_code( + self, package, concept_pointer, close=True, code_link=True + ): + document = package / "concepts" / "C001-source-preservation.md" + # Byte-level append, for the same \r\n reason as append_issue_draft. + # The draft embeds a fenced code example between its Context + # Pointers and the trailing workflow:issue marker (#393). The + # broken-pointer tests pass code_link=False on purpose: with a + # link-shaped line in the example, the pre-fix checker failed + # those checks for the wrong reason (the example leaked into + # prose), masking that the pointer itself went unchecked. + example = "# [not a pointer](missing/code-example.md)\n" if code_link else "" + example += "print('example')\n" + draft = ( + "\n\n## Trigger Issue Draft\n\n```markdown\n" + "# Research: Exact source preservation\n\n" + f"- Concept Document: [concept]({concept_pointer})\n\n" + "```python\n" + f"{example}" + "```\n\n" + '\n' + ) + if close: + draft += "```\n" + document.write_bytes(document.read_bytes() + draft.encode("utf-8")) + + def test_fenced_issue_draft_with_an_inner_code_block_passes_when_valid(self): + package = self.init() + self.append_issue_draft_with_inner_code(package, "C001-source-preservation.md") + self.check(package, ok=True) + + def test_fenced_issue_draft_with_an_inner_code_block_and_a_broken_pointer_fails_check( + self, + ): + package = self.init() + # #393: the inner fence used to split the draft, so the pointer + # fragment had no marker and the marker fragment had no links — + # neither was checked and this broken pointer passed silently. + self.append_issue_draft_with_inner_code( + package, "concepts/C001-source-preservation.md", code_link=False + ) + self.check(package, ok=False) + + def test_unclosed_issue_draft_with_a_broken_pointer_fails_check(self): + package = self.init() + # #393: a draft whose fence is never closed used to be dropped + # by the fence parser entirely; it is checked like any draft. + self.append_issue_draft_with_inner_code( + package, + "concepts/C001-source-preservation.md", + close=False, + code_link=False, + ) + self.check(package, ok=False) + def test_root_relative_links_and_inline_code_links_pass(self): package = self.init() main = package / "main.md"