fix(learn): anchor the managed block on its last start marker and append when the end marker is missing

_merge_into_file located the managed block with a pattern that matched
from the FIRST occurrence of the start-marker string to the nearest end
marker. Hand-written prose above the block that quoted the start marker
literally therefore became the start of the match, and --apply deleted
everything between that mention and the real block. When the start
marker was present but no end marker followed, the pattern matched
nothing, sub() returned the file unchanged, and every recommendation of
the run was silently dropped while the CLI still reported a write.

The block is now the span from the LAST start marker that has a
following end marker to that end marker; text before it is left alone.
A start marker with no end marker is treated as "no managed block": a
fresh section is appended, as for a file with no markers at all.

Two tests pin both cases; both fail on main.

Refs #4339

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
HetCreep
2026-10-22 02:54:58 +07:00
co-authored by Claude Fable 5.1
parent 4c7acba593
commit 2c6853ea52
2 changed files with 44 additions and 2 deletions
+8 -2
View File
@@ -364,8 +364,14 @@ def _merge_into_file(file_path: Path, new_recommendations: list[Recommendation])
section = _build_section(merged)
if file_path.exists():
existing = _read_text_tolerant(file_path)
if _MARKER_START in existing:
return _MARKER_PATTERN.sub(lambda _match: section, existing)
# The block is the LAST start marker that still has an end marker after
# it: prose above the block may quote the start marker, and a start
# marker with no end marker is not a block to rewrite (#4339).
last_end = existing.rfind(_MARKER_END)
start = existing.rfind(_MARKER_START, 0, last_end) if last_end >= 0 else -1
if start >= 0:
end = existing.index(_MARKER_END, start) + len(_MARKER_END)
return existing[:start] + section + existing[end:]
return existing.rstrip() + "\n\n" + section + "\n"
return section + "\n"
+36
View File
@@ -299,6 +299,42 @@ class TestClaudeCodeWriter:
assert r"C:\Users\john.doe\repo" in full_content
assert "stale" not in full_content
def test_merge_keeps_prose_that_quotes_the_start_marker_above_the_block(self, tmp_path):
"""Prose quoting the start marker above the block must survive the rewrite."""
context_file = tmp_path / "CLAUDE.local.md"
prose = f"The block below starts with {_MARKER_START} and is managed."
context_file.write_text(
f"{prose}\n"
"- Hand-written rule: never force-push\n\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 "- Hand-written rule: never force-push" in final
assert prose in final
assert "- Use uv" in final
assert final.count(_MARKER_START) == 2
assert final.count(_MARKER_END) == 1
def test_merge_appends_a_block_when_the_end_marker_is_missing(self, tmp_path):
"""A start marker with no end marker is not a block: the recommendations must land."""
context_file = tmp_path / "CLAUDE.local.md"
original = f"{_MARKER_START}\n- Hand-written rule: never force-push\n"
context_file.write_text(original)
recommendation = _rec(RecommendationTarget.CONTEXT_FILE, "Tooling", "- Use uv")
final = _merge_into_file(context_file, [recommendation])
assert final != original
assert "- Use uv" in final
assert "- Hand-written rule: never force-push" in final
def test_memory_md_carry_forward(self, tmp_path):
"""Carry-forward also works for MEMORY.md."""
proj = _project(tmp_path)