From cad2302647045e64fa8dcafb481a859d2003e4a1 Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Sat, 31 Oct 2026 10:33:49 -0700 Subject: [PATCH 1/4] Read a Hash Inside a Compose Mapping Key as Key Text Before a Block Scalar in the Prose Gate Co-Authored-By: Claude Sonnet 5.5 --- .github/actions/prose-gate/prose_lint.py | 4 ++-- tests/test_prose_lint.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/.github/actions/prose-gate/prose_lint.py b/.github/actions/prose-gate/prose_lint.py index e9f464874..302769b59 100755 --- a/.github/actions/prose-gate/prose_lint.py +++ b/.github/actions/prose-gate/prose_lint.py @@ -1309,8 +1309,8 @@ def rule_sentence_split(unit: Unit, ctx: Context) -> list[Finding]: ) LONG_MOUNT = re.compile(r"""^\s*(?:-\s+)?target:\s+["']?(?P/[^\s"']*)""") BLOCK_SCALAR = re.compile( - r"^(?P\s*(?:-\s+)*)(?P[^\s#][^#]*?:\s+)?(?:[!&]\S*\s+)*[|>]" - r"(?:[-+][1-9]?|[1-9][-+]?)?\s*(?:#.*)?$" + r"^(?P\s*(?:-\s+)*)(?P[^\s#](?:[^#]|(?<=\S)#)*?:\s+)?(?:[!&]\S*\s+)*[|>]" + r"(?:[-+][1-9]?|[1-9][-+]?)?(?:\s+#.*|\s*)$" ) diff --git a/tests/test_prose_lint.py b/tests/test_prose_lint.py index c72d9599b..edb35826f 100755 --- a/tests/test_prose_lint.py +++ b/tests/test_prose_lint.py @@ -3626,6 +3626,20 @@ def test_a_compose_block_scalar_is_text_rather_than_a_volume(self) -> None: ) with self.subTest(indicator=indicator): self.assertEqual(["home-path"], self.kinds(text, {"home-path"}, "compose.yml")) + for key, indicator in ( + ("x-a#b", "|"), + ("x-a#b", "| # note"), + ("x-a#b", ">-"), + ('"x-a#b"', "|"), + ("'x a'", "|"), + ("x", "| # note"), + ): + text = ( + f"services:\n ledger:\n {key}: {indicator}\n volumes:\n" + f" - ledger-cache:{NIX_HOME}/data\n{real}" + ) + with self.subTest(key=key, indicator=indicator): + self.assertEqual(["home-path"], self.kinds(text, {"home-path"}, "compose.yml")) target = f" target: {NIX_HOME}/media" for entry in ( f" - type: bind\n x-note: |\n - ./old:{NIX_HOME}/media\n{target}", From 9a28f18a81200df94870e993251a3790d542d7e4 Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Sat, 31 Oct 2026 10:37:56 -0700 Subject: [PATCH 2/4] Detect Compose Block Scalar Openers Through the YAML Lexer in the Prose Gate Co-Authored-By: Claude Sonnet 5.5 --- .github/actions/prose-gate/prose_lint.py | 30 ++++++++++++++++++------ tests/test_prose_lint.py | 2 ++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/.github/actions/prose-gate/prose_lint.py b/.github/actions/prose-gate/prose_lint.py index 302769b59..826909cfa 100755 --- a/.github/actions/prose-gate/prose_lint.py +++ b/.github/actions/prose-gate/prose_lint.py @@ -32,6 +32,7 @@ from __future__ import annotations import argparse +import bisect import contextlib import functools import json @@ -1308,10 +1309,24 @@ def rule_sentence_split(unit: Unit, ctx: Context) -> list[Finding]: r"""^\s*-\s+["']?(?:[A-Za-z]:(?=[\\/]))?(?:\$\{[^}]*\}|[^:\s"'{}])+:(?P/[^:\s"']*)""" ) LONG_MOUNT = re.compile(r"""^\s*(?:-\s+)?target:\s+["']?(?P/[^\s"']*)""") -BLOCK_SCALAR = re.compile( - r"^(?P\s*(?:-\s+)*)(?P[^\s#](?:[^#]|(?<=\S)#)*?:\s+)?(?:[!&]\S*\s+)*[|>]" - r"(?:[-+][1-9]?|[1-9][-+]?)?(?:\s+#.*|\s*)$" -) +SCALAR_LEAD = re.compile(r"^\s*(?:-\s+)*") +SCALAR_KEY = re.compile(r":\s+(?:[!&]\S*\s+)*$") + + +def scalar_openers(texts: list[str]) -> dict[int, int]: + """The line index of each YAML block scalar header, and the column its content sits deeper than.""" + out: dict[int, int] = {} + starts = [0] + for text in texts: + starts.append(starts[-1] + len(text) + 1) + for offset, kind, value in lexer_for("yaml").get_tokens_unprocessed("\n".join(texts)): + if kind not in Punctuation.Indicator or value not in "|>": + continue + row = bisect.bisect_right(starts, offset) - 1 + text, col = texts[row], offset - starts[row] + lead = len(SCALAR_LEAD.match(text)[0]) # type: ignore[index] + out[row] = lead if SCALAR_KEY.search(text[lead:col]) else len(text) - len(text.lstrip()) + return out def container_paths(lines: tuple[Line, ...]) -> dict[int, range]: @@ -1324,7 +1339,8 @@ def container_paths(lines: tuple[Line, ...]) -> dict[int, range]: out: dict[int, range] = {} block: int | None = None opaque: int | None = None - for line in lines: + openers = scalar_openers([line.text for line in lines]) + for row, line in enumerate(lines): body = line.text.lstrip() if not body or body.startswith("#"): continue @@ -1332,8 +1348,8 @@ def container_paths(lines: tuple[Line, ...]) -> dict[int, range]: if opaque is not None and indent > opaque: continue opaque = None - if scalar := BLOCK_SCALAR.match(line.text): - opaque = len(scalar["lead"]) if scalar["key"] else indent + if row in openers: + opaque = openers[row] if block is not None and (indent < block or (indent == block and body[0] != "-")): block = None if VOLUMES_KEY.match(line.text): diff --git a/tests/test_prose_lint.py b/tests/test_prose_lint.py index edb35826f..7479f1e3f 100755 --- a/tests/test_prose_lint.py +++ b/tests/test_prose_lint.py @@ -3632,6 +3632,8 @@ def test_a_compose_block_scalar_is_text_rather_than_a_volume(self) -> None: ("x-a#b", ">-"), ('"x-a#b"', "|"), ("'x a'", "|"), + ('"x #b"', "|"), + ("'x # b'", "|"), ("x", "| # note"), ): text = ( From 3cd90445188953aacd6f813e9f220435883f44d5 Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Sat, 31 Oct 2026 10:44:07 -0700 Subject: [PATCH 3/4] Reuse Starts for the Block Scalar Opener Line Lookup in the Prose Gate Co-Authored-By: Claude Sonnet 5.5 --- .github/actions/prose-gate/prose_lint.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/.github/actions/prose-gate/prose_lint.py b/.github/actions/prose-gate/prose_lint.py index 826909cfa..df10c5640 100755 --- a/.github/actions/prose-gate/prose_lint.py +++ b/.github/actions/prose-gate/prose_lint.py @@ -32,7 +32,6 @@ from __future__ import annotations import argparse -import bisect import contextlib import functools import json @@ -1316,16 +1315,15 @@ def rule_sentence_split(unit: Unit, ctx: Context) -> list[Finding]: def scalar_openers(texts: list[str]) -> dict[int, int]: """The line index of each YAML block scalar header, and the column its content sits deeper than.""" out: dict[int, int] = {} - starts = [0] - for text in texts: - starts.append(starts[-1] + len(text) + 1) - for offset, kind, value in lexer_for("yaml").get_tokens_unprocessed("\n".join(texts)): + text = "\n".join(texts) + starts = Starts(text) + for offset, kind, value in lexer_for("yaml").get_tokens_unprocessed(text): if kind not in Punctuation.Indicator or value not in "|>": continue - row = bisect.bisect_right(starts, offset) - 1 - text, col = texts[row], offset - starts[row] - lead = len(SCALAR_LEAD.match(text)[0]) # type: ignore[index] - out[row] = lead if SCALAR_KEY.search(text[lead:col]) else len(text) - len(text.lstrip()) + number, col = starts.at(offset) + row, line = number - 1, texts[number - 1] + lead = len(SCALAR_LEAD.match(line)[0]) # type: ignore[index] + out[row] = lead if SCALAR_KEY.search(line[lead:col]) else len(line) - len(line.lstrip()) return out From f24fba52b03bc362e83c7ae169906d31d7d4422e Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Sat, 31 Oct 2026 10:53:00 -0700 Subject: [PATCH 4/4] Measure the Block Scalar Lead Without a Type Suppression in the Prose Gate Co-Authored-By: Claude Sonnet 5.5 --- .github/actions/prose-gate/prose_lint.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/prose-gate/prose_lint.py b/.github/actions/prose-gate/prose_lint.py index df10c5640..15844099f 100755 --- a/.github/actions/prose-gate/prose_lint.py +++ b/.github/actions/prose-gate/prose_lint.py @@ -1322,7 +1322,7 @@ def scalar_openers(texts: list[str]) -> dict[int, int]: continue number, col = starts.at(offset) row, line = number - 1, texts[number - 1] - lead = len(SCALAR_LEAD.match(line)[0]) # type: ignore[index] + lead = len(line) - len(SCALAR_LEAD.sub("", line, count=1)) out[row] = lead if SCALAR_KEY.search(line[lead:col]) else len(line) - len(line.lstrip()) return out