insert_after puts the content one line too late when the target ends with a newline
insert_after in skillopt/optimizer/skill.py inserts the new content after the end of the target's line. It finds that end by searching for the next \n from the end of the target. When the target already ends with \n (a whole line copied from the skill, which analysts often do), that search finds the end of the next line, so the content lands one line too late.
Code: https://github.com/microsoft/SkillOpt/blob/45f65ca04f5f6713bca7b9797b495696ba812751/skillopt/optimizer/skill.py#L121-L125
Steps to reproduce
from skillopt.optimizer.skill import apply_patch_with_report
skill = "# QA Skill\n\n## Rules\n## Output format\n- One line.\n"
edit = {"op": "insert_after", "target": "## Rules\n", "content": "- Cite the source."}
new, report = apply_patch_with_report(skill, {"edits": [edit]})
print(report[0]["status"])
print(new)
Expected
The rule goes under ## Rules, the same as with target "## Rules":
## Rules
- Cite the source.
## Output format
- One line.
Actual
applied_insert_after, but the rule is filed under the wrong heading:
## Rules
## Output format
- Cite the source.
- One line.
If the target's line is directly followed by <!-- SLOW_UPDATE_START --> or <!-- APPENDIX_START -->, the same skip puts the new text inside the protected region.
Environment
main at 45f65ca, Python 3.10, Windows 11.
I have a fix with a regression test and will open a PR.