From d9943bf31f8de20377f82bb725de179ed449ac62 Mon Sep 17 00:00:00 2001 From: Lenin Mookiah <2639584+leninworld@users.noreply.github.com> Date: Sat, 28 Nov 2026 02:50:38 -0800 Subject: [PATCH] fix(sleep): read the learned block up to its own END marker extract_learned searched for the first LEARNED END marker anywhere in the document. A stray END before the learned block made the block read as empty, and the next edit rewrote it without the adopted rules. Search for END after the START marker, as _strip_learned already does. Fixes #479 --- CHANGELOG.md | 5 +++++ skillopt_sleep/memory.py | 2 +- tests/test_learned_block_roundtrip.py | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7542320d2..91b0452a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -154,6 +154,11 @@ All notable changes to SkillOpt are documented here. This project adheres to value is rejected with a clear error. ### Fixed +- **SkillOpt-Sleep keeps learned rules after a stray end marker.** + `extract_learned` read up to the first `LEARNED END` marker anywhere in the + document, so an END marker before the learned block made the block read as + empty and the next edit rewrote it without the adopted rules. It now reads to + the first END marker after the START marker. (thanks @leninworld) - `scripts/train.py` accepts `--slow_update_gate_with_selection`. The config key `optimizer.slow_update_gate_with_selection` exists, but the flag was never added, so `--slow_update_gate_with_selection true` failed with an diff --git a/skillopt_sleep/memory.py b/skillopt_sleep/memory.py index 0444ea673..344f2de87 100644 --- a/skillopt_sleep/memory.py +++ b/skillopt_sleep/memory.py @@ -23,7 +23,7 @@ def extract_learned(doc: str) -> str: s = doc.find(LEARNED_START) - e = doc.find(LEARNED_END) + e = doc.find(LEARNED_END, s + len(LEARNED_START)) if s == -1 or e == -1: return "" return doc[s + len(LEARNED_START):e].strip() diff --git a/tests/test_learned_block_roundtrip.py b/tests/test_learned_block_roundtrip.py index 08e1ea365..41f50a175 100644 --- a/tests/test_learned_block_roundtrip.py +++ b/tests/test_learned_block_roundtrip.py @@ -35,6 +35,22 @@ def _add(content: str) -> EditRecord: return EditRecord(target="skill", op="add", content=content) +def test_stray_end_before_learned_block_does_not_drop_adopted_rules() -> None: + doc = LEARNED_END + "\nstray\n" + set_learned( + "# Skill", ["Keep tests deterministic"] + ) + assert current_learned_lines(doc) == ["Keep tests deterministic"] + + new_doc, applied, unmatched = apply_edits_detailed(doc, [_add("Run ruff")]) + + assert len(applied) == 1 and unmatched == [] + assert "Keep tests deterministic" in new_doc + assert current_learned_lines(new_doc) == [ + "Keep tests deterministic", + "Run ruff", + ] + + def test_legacy_multi_line_item_survives_the_next_edit() -> None: doc = _legacy_doc( "Always run tests before committing",