fix: patch checks full target span + marker injection/integrity (algo #8)

This commit is contained in:
2026-07-16 06:20:29 -04:00
parent caea9150b3
commit 65126feada
4 changed files with 178 additions and 7 deletions
+65
View File
@@ -383,3 +383,68 @@ class TestApplyPatch:
assert "line2" not in out
assert "TAIL" in out
assert all(r["status"].startswith("applied") for r in reports)
class TestSpanOverlapProtection:
"""冻结区跨度保护 + marker 注入拦截(算法 #8 加固)。"""
def test_replace_spanning_into_protected_marker_preserved(self) -> None:
"""target 末端跨入含 marker 的 appendix 冻结区时被拦截,marker 不被破坏。"""
body = "正文最后一段。"
appendix = f"{APPENDIX_START}\n## 执行提醒\n- 规则A\n{APPENDIX_END}"
content = body + "\n\n" + appendix
# target 从正文末尾跨入 APPENDIX_START(含 marker 字面量)
target = "正文最后一段。\n\n" + APPENDIX_START
edits = [{"op": "delete", "target": target, "content": ""}]
new_content, report = apply_patch_with_report(
content, edits, protected_spans=[appendix]
)
# 无论经 marker 注入拦截还是跨度拦截,marker 都必须完整保留
assert APPENDIX_START in new_content and APPENDIX_END in new_content
assert report[0]["status"] in ("skipped_protected", "skipped_marker_injection")
def test_replace_spanning_into_frozen_section_is_skipped(self) -> None:
"""target 起点在正文、末端伸入无 marker 的冻结区段 → 跨度拦截跳过。"""
body = "可改正文段落。"
frozen = "## 输出格式\n必须输出 JSON。"
content = body + "\n" + frozen
# target 从可改正文跨入冻结区段(不含任何 marker 字面量)
target = "可改正文段落。\n## 输出格式"
edits = [{"op": "replace", "target": target, "content": "破坏内容"}]
new_content, report = apply_patch_with_report(
content, edits, protected_spans=[frozen]
)
assert "破坏内容" not in new_content
assert "## 输出格式" in new_content
assert report[0]["status"] == "skipped_protected"
def test_insert_after_spanning_into_frozen_section_is_skipped(self) -> None:
"""insert_after 的 target 末端伸入无 marker 冻结区段时跨度拦截跳过。"""
body = "可改正文。"
frozen = "## 输出格式\n固定内容。"
content = body + "\n" + frozen
target = "可改正文。\n## 输出格式"
edits = [{"op": "insert_after", "target": target, "content": "注入内容"}]
new_content, report = apply_patch_with_report(
content, edits, protected_spans=[frozen]
)
assert "注入内容" not in new_content
assert report[0]["status"] == "skipped_protected"
def test_edit_payload_with_marker_literal_rejected(self) -> None:
"""edit payload 含 marker 字面量 → 拒绝该 edit。"""
edits = [
{"op": "append", "target": "", "content": f"注入 {APPENDIX_START} 破坏"}
]
_, report = apply_patch_with_report("正文", edits, protected_spans=[])
assert any(
"marker" in str(s).lower() or "reject" in str(s).lower() for s in report[0].values()
)
def test_edit_target_with_marker_literal_rejected(self) -> None:
"""edit target 含 momentum marker 字面量 → 拒绝该 edit。"""
edits = [
{"op": "replace", "target": f"{MOMENTUM_START}x", "content": "y"}
]
_, report = apply_patch_with_report("正文", edits, protected_spans=[])
assert report[0]["status"] == "skipped_marker_injection"