fix(learn): ignore marker lines inside fenced code blocks when locating the managed block

Review found that _find_managed_block still treated a standalone marker
line inside a fenced Markdown example (```markdown ... ```) as an active
delimiter: with an example above the real block, a merge replaced the
example's contents and left the real block untouched.

Track CommonMark-style fences (``` or ~~~, at most three spaces of
indent, closed by the same character with a run at least as long) and
skip marker detection while inside one. Every reader and writer still
uses this one span, so the example stays byte-identical and the real
block receives the recommendation.

Two tests: a fenced example of the block above the real block (fails on
the previous head), and a closed code fence inside a recommendation body
that must not hide the block's end marker.

Refs #4339

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HetCreep
2026-10-22 07:35:28 +07:00
co-authored by Claude Opus 5.5
parent 35cf4a980c
commit 40880d1ea5
2 changed files with 56 additions and 6 deletions
+16 -6
View File
@@ -37,20 +37,30 @@ def _find_managed_block(text: str) -> tuple[int, int] | None:
The block is a line that is exactly the start marker through the next line
that is exactly the end marker -- the shape ``_build_section`` writes. A
marker quoted inside prose (sharing its line with other text, or indented)
is not a block boundary, and a start marker with no end marker before the
next start marker is not a block. Every reader and writer of the block in
or inside a fenced code block (a Markdown example) is not a block
boundary, and a start marker with no end marker before the next start
marker is not a block. Every reader and writer of the block in
this module uses this one span, so the block that is parsed is the block
that is replaced (#4339). ``end`` stops after the end marker, before its
line break.
"""
offset = 0
start: int | None = None
fence: str | None = None # the open fence's run, e.g. "```" or "~~~~"
for line in text.splitlines(keepends=True):
bare = line.rstrip()
if bare == _MARKER_START:
start = offset
elif bare == _MARKER_END and start is not None:
return start, offset + len(bare)
stripped = bare.lstrip(" ")
if len(bare) - len(stripped) <= 3 and stripped[:3] in ("```", "~~~"):
run = len(stripped) - len(stripped.lstrip(stripped[0]))
if fence is None:
fence = stripped[0] * run
elif stripped[0] == fence[0] and run >= len(fence) and not stripped[run:]:
fence = None
elif fence is None:
if bare == _MARKER_START:
start = offset
elif bare == _MARKER_END and start is not None:
return start, offset + len(bare)
offset += len(line)
return None
+40
View File
@@ -382,6 +382,46 @@ class TestClaudeCodeWriter:
assert "### Environment" in block
assert "- Use uv" in block
def test_merge_leaves_a_fenced_example_of_the_block_untouched(self, tmp_path):
"""Markers inside a fenced Markdown example are not the managed block."""
context_file = tmp_path / "CLAUDE.local.md"
example = f"```markdown\n{_MARKER_START}\nEXAMPLE_SENTINEL\n{_MARKER_END}\n```\n"
context_file.write_text(
"How the managed block looks:\n\n" + example + "\n"
f"{_MARKER_START}\n"
"## Headroom Learned Patterns\n\n"
"### Environment\n"
"- old bullet\n\n"
f"{_MARKER_END}\n"
)
recommendation = _rec(RecommendationTarget.CONTEXT_FILE, "Tooling", "- Use uv")
final = _merge_into_file(context_file, [recommendation])
assert example in final
real_block = final[final.index(example) + len(example) :]
assert "- Use uv" in real_block
assert "- old bullet" in real_block
assert "- Use uv" not in final[: final.index(example) + len(example)]
def test_fenced_code_inside_the_block_does_not_hide_its_end_marker(self, tmp_path):
"""A closed code fence in a recommendation body keeps the block recognisable."""
context_file = tmp_path / "CLAUDE.local.md"
context_file.write_text(
f"{_MARKER_START}\n"
"## Headroom Learned Patterns\n\n"
"### Build\n"
"```bash\nmake test\n```\n\n"
f"{_MARKER_END}\n"
)
recommendation = _rec(RecommendationTarget.CONTEXT_FILE, "Tooling", "- Use uv")
final = _merge_into_file(context_file, [recommendation])
assert final.count(_MARKER_START) == 1
assert final.count(_MARKER_END) == 1
assert "- Use uv" in final
def test_memory_md_carry_forward(self, tmp_path):
"""Carry-forward also works for MEMORY.md."""
proj = _project(tmp_path)