Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ All notable changes to SkillOpt are documented here. This project adheres to
value is rejected with a clear error.

### Fixed
- A SkillOpt-Sleep `replace` edit no longer writes the same learned item twice.
`apply_edits_detailed` skips an `add` that is already in the learned block,
but `replace` wrote its replacement once per line the anchor matched and
did not check the other lines, so the block could hold duplicate bullets.
The replacement now goes in once, at the first matching line, and is
dropped when another line already holds it (thanks @Shizoqua).
- Repeated rollouts of a SkillOpt-Sleep tool task are separate samples again.
`replay_one` forwarded `sample_id` on the text route only, so with
`dream_rollouts > 1` a `tool_called` task on a backend without a real tool
Expand Down
9 changes: 9 additions & 0 deletions skillopt_sleep/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,20 @@ def apply_edits_detailed(
elif op == "replace":
anchor = _norm(e.anchor)
replacement = _learned_item(e.content)
# Like ``add``, never write an item that is already in the block:
# the replacement goes in once, at the first matching line, and is
# dropped when an unmatched line already holds it.
kept = {_norm(line) for line in lines if not (anchor and anchor in _norm(line))}
placed = _norm(replacement) in kept
new_lines = []
changed = False
for line in lines:
if anchor and anchor in _norm(line):
if placed:
changed = True
continue
new_lines.append(replacement)
placed = True
changed = changed or replacement != line
else:
new_lines.append(line)
Expand Down
52 changes: 52 additions & 0 deletions tests/test_learned_replace_dedup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""A replace edit never writes the same learned item twice.

``add`` skips an item that is already in the learned block, but ``replace``
wrote its replacement once per matching line and did not check the other
lines, so the block could hold the same bullet twice.
"""

from __future__ import annotations

from skillopt_sleep.memory import apply_edits_detailed, current_learned_lines, set_learned
from skillopt_sleep.types import EditRecord


def _replace(anchor: str, content: str) -> EditRecord:
return EditRecord(target="skill", op="replace", anchor=anchor, content=content)


def test_replacement_that_already_exists_is_not_duplicated() -> None:
doc = set_learned("", ["Run the tests before committing.", "Write tests for every bug fix."])

new_doc, applied, unmatched = apply_edits_detailed(
doc, [_replace("Run the tests before committing.", "Write tests for every bug fix.")]
)

assert current_learned_lines(new_doc) == ["Write tests for every bug fix."]
assert len(applied) == 1 and unmatched == []


def test_anchor_matching_several_items_writes_the_replacement_once() -> None:
doc = set_learned(
"",
["Run the tests before committing.", "Prefer small commits.", "Write tests for every bug fix."],
)

new_doc, applied, _unmatched = apply_edits_detailed(
doc, [_replace("tests", "Run the full test suite before committing.")]
)

assert current_learned_lines(new_doc) == [
"Run the full test suite before committing.",
"Prefer small commits.",
]
assert len(applied) == 1


def test_replace_that_changes_nothing_is_still_unmatched() -> None:
doc = set_learned("", ["Prefer small commits.", "Prefer small commits."])

new_doc, applied, unmatched = apply_edits_detailed(doc, [_replace("no such rule", "Anything.")])

assert current_learned_lines(new_doc) == ["Prefer small commits.", "Prefer small commits."]
assert applied == [] and len(unmatched) == 1