From 8fa3ff6f275ff88a751e23f21d35cd168163fd71 Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Tue, 20 Oct 2026 05:06:38 -0500 Subject: [PATCH] fix(workflow): check draft links past unclosed inner code examples issue_drafts() stripped nested examples via unfenced(block), whose reparse dropped every line from an unclosed inner fence onward, so a draft pointer after an unclosed embedded example was never checked (#401, residual of #393/#400). - fenced_blocks() now has a closure-aware sibling (_fenced_blocks); when a draft's own fence was never closed at document level, a trailing bare fence line in the block is the draft's closing fence (consumed as the example's close) and is dropped before stripping. - New draft_prose() strips examples from a draft with recovery: a bare fence closes the innermost example whatever its length (the 4-open/3-close slip), and an example still open at the end of the draft has its buffered lines kept as prose and checked instead of dropped. A properly closed example's contents stay exempt. Four regression tests cover both shapes with broken and valid pointers; the two broken-pointer tests fail pre-fix. Full workflow suite: 52 passed. Real-tree check returns ok: true. Fixes #401 --- scripts/research_workflow.py | 96 +++++++++++++++++++++---- scripts/tests/test_research_workflow.py | 65 +++++++++++++++++ 2 files changed, 149 insertions(+), 12 deletions(-) diff --git a/scripts/research_workflow.py b/scripts/research_workflow.py index cf2384dba6..211dccd4f5 100644 --- a/scripts/research_workflow.py +++ b/scripts/research_workflow.py @@ -453,25 +453,82 @@ def prose(text): return re.sub(r"(`+)(?:(?!\1).)+?\1", "", clean) -def fenced_blocks(text): - """Return the contents of each fenced code block, in order. +def _fenced_blocks(text): + """Yield (content, closed) for each fenced 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). + A trailing block whose fence is never closed is yielded as well + (closed=False), so a trigger-issue draft in an unclosed fence is + checked rather than silently dropped (#393). """ - blocks, current = [], None + 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)) + yield "\n".join(current), True current = None if current is not None: - blocks.append("\n".join(current)) - return blocks + yield "\n".join(current), False + + +def fenced_blocks(text): + """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). + """ + return [block for block, _closed in _fenced_blocks(text)] + + +def draft_prose(block): + """Return a draft's prose with embedded code examples stripped. + + Examples are buffered while their fence is open and discarded + when it closes, so a properly closed example's contents stay + exempt from link checks (#393). Two authoring slips are recovered + instead of silently dropping the rest of the draft (#401): + + - a bare fence closes the innermost example whatever its length: + an example opened with four backticks and closed with three + was left open by fence_events()' length rule, and every line + after the attempted close went unchecked; + - an example still open at the end of the draft has no known + end, so its buffered lines are kept as prose and checked + rather than dropped: a draft whose example is never closed + cannot exempt everything after the example's opening fence. + Authors who want example contents exempt must close the + example. + """ + out, buffer, stack = [], [], [] + for line in block.splitlines(): + found = re.match(r"^ {0,3}(`{3,}|~{3,})\s*(.*?)\s*$", line) + if not found: + (buffer if stack else out).append(line) + continue + token, info = found[1], found[2] + if stack and token[0] == stack[-1][0] and not info: + stack.pop() + if stack: + buffer.append(line) + else: + buffer = [] + elif stack and token[0] != stack[-1][0]: + # A fence of the other character is literal example content. + buffer.append(line) + else: + if stack: + # An info-string fence opens a nested example. + stack.append(token) + buffer.append(line) + else: + stack = [token] + buffer = [line] + if stack: + out.extend(buffer) + return "\n".join(out) def issue_drafts(text): @@ -482,13 +539,28 @@ def issue_drafts(text): base check_link applies to ordinary prose links. Other fenced blocks are raw evidence/code and stay exempt, as unfenced() documents. """ - for block in fenced_blocks(text): + for block, closed in _fenced_blocks(text): if "workflow:issue" in block: + if not closed: + # The draft's fence was never closed at document level, + # so a trailing bare fence line in the block was the + # draft's own closing fence, consumed as the embedded + # example's close (#401): drop it so draft_prose() sees + # the example as the unclosed one it is. + lines = block.splitlines() + found = ( + re.match(r"^ {0,3}(`{3,}|~{3,})\s*(.*?)\s*$", lines[-1]) + if lines + else None + ) + if found and not found[2]: + block = "\n".join(lines[:-1]) # 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) + # not link-checked either (#393); draft_prose() recovers + # the parts after an unclosed example instead (#401). + clean = re.sub(r"", "", draft_prose(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 b17ee0149d..3932a5a0ca 100644 --- a/scripts/tests/test_research_workflow.py +++ b/scripts/tests/test_research_workflow.py @@ -851,6 +851,71 @@ def test_fenced_issue_draft_with_an_inner_code_block_and_a_broken_pointer_fails_ ) self.check(package, ok=False) + def append_issue_draft_with_unclosed_inner_code( + self, package, concept_pointer, short_close=False + ): + document = package / "concepts" / "C001-source-preservation.md" + # Byte-level append, for the same \r\n reason as append_issue_draft. + # The draft's embedded code example is never closed (#401): with + # short_close=False its only closing fence is the draft's own, so + # the example consumes it; with short_close=True the example opens + # with four backticks and its attempted close uses three. Either + # way the Context Pointer sits after the example, past where the + # pre-fix checker stopped looking. + if short_close: + example = "````python\nprint('example')\n```\n" + else: + example = "```python\nprint('example')\n" + draft = ( + "\n\n## Trigger Issue Draft\n\n```markdown\n" + "# Research: Exact source preservation\n\n" + f"{example}" + f"- Concept Document: [concept]({concept_pointer})\n" + '\n' + "```\n" + ) + document.write_bytes(document.read_bytes() + draft.encode("utf-8")) + + def test_issue_draft_with_an_unclosed_inner_code_block_and_a_broken_pointer_fails_check( + self, + ): + package = self.init() + # #401: the unclosed inner example used to exempt every line + # after its opening fence, so this broken pointer passed silently. + self.append_issue_draft_with_unclosed_inner_code( + package, "concepts/C001-source-preservation.md" + ) + self.check(package, ok=False) + + def test_issue_draft_with_an_unclosed_inner_code_block_passes_when_valid(self): + package = self.init() + self.append_issue_draft_with_unclosed_inner_code( + package, "C001-source-preservation.md" + ) + self.check(package, ok=True) + + def test_issue_draft_with_a_short_closed_inner_code_block_and_a_broken_pointer_fails_check( + self, + ): + package = self.init() + # #401: an example opened with four backticks and closed with + # three was treated as unclosed, so the pointer after its + # attempted close went unchecked. + self.append_issue_draft_with_unclosed_inner_code( + package, "concepts/C001-source-preservation.md", short_close=True + ) + self.check(package, ok=False) + + def test_issue_draft_with_a_short_closed_inner_code_block_passes_when_valid( + self, + ): + package = self.init() + self.append_issue_draft_with_unclosed_inner_code( + package, "C001-source-preservation.md", short_close=True + ) + self.check(package, ok=True) + 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