diff --git a/CHANGELOG.md b/CHANGELOG.md index 7542320d2..711e6de92 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 +- The success analyst sees the trajectory memory when a step buffer is present. + `run_success_analyst_minibatch` built its previous-steps context as the step + buffer or the trajectory memory, so the memory was dropped whenever a step + buffer was set. The failure analyst always keeps both. Both analysts now build + the same context, the step buffer first (thanks @Shizoqua). - `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/gradient/reflect.py b/skillopt/gradient/reflect.py index 67bdbbefc..a27f07685 100644 --- a/skillopt/gradient/reflect.py +++ b/skillopt/gradient/reflect.py @@ -471,7 +471,11 @@ def run_success_analyst_minibatch( f"## {payload_label(mode, title=True)} Budget\n" f"Produce at most L={edit_budget} {payload_label(mode)}.\n\n" ) - ctx = step_buffer_context or trajectory_memory_context or "" + # Same context as the failure analyst: the step buffer first, then the + # trajectory memory, so the two analysts see the same epoch history. + ctx = step_buffer_context or "" + if trajectory_memory_context: + ctx = f"{ctx}\n{trajectory_memory_context}" if ctx else trajectory_memory_context if ctx.strip(): user += f"## Previous Steps in This Epoch\n{ctx}\n\n" optimizer_ctx = format_meta_skill_context(meta_skill_context) diff --git a/tests/test_reflect_success_memory_context.py b/tests/test_reflect_success_memory_context.py new file mode 100644 index 000000000..bfbb71794 --- /dev/null +++ b/tests/test_reflect_success_memory_context.py @@ -0,0 +1,92 @@ +"""The success analyst sees the same epoch history as the failure analyst. + +``run_success_analyst_minibatch`` used ``step_buffer_context or +trajectory_memory_context``, so the trajectory memory was dropped whenever a +step buffer was set. The failure analyst keeps both. The success analyst now +builds the same context: the step buffer first, then the trajectory memory. +""" + +from __future__ import annotations + +import json + +import pytest + +from skillopt.gradient import reflect as rf + + +@pytest.fixture +def captured(monkeypatch): + prompts: list[str] = [] + + def fake_chat(**kwargs): + prompts.append(kwargs.get("user", "")) + return '{"patch": {"reasoning": "r", "edits": []}}', {} + + monkeypatch.setattr(rf, "chat_optimizer", fake_chat) + return prompts + + +@pytest.fixture +def items(tmp_path): + pred = tmp_path / "predictions" + (pred / "q1").mkdir(parents=True) + conversation = [{"type": "message", "content": "answer 42"}] + (pred / "q1" / "conversation.json").write_text(json.dumps(conversation)) + return [{"id": "q1", "task_description": "What is revenue?", "hard": 1.0, "soft": 1.0}], str(pred) + + +def _run_success(items, pred, **context): + return rf.run_success_analyst_minibatch("skill", items, pred, edit_budget=2, **context) + + +def test_step_buffer_and_trajectory_memory_are_both_included(captured, items): + batch, pred = items + + _run_success(batch, pred, step_buffer_context="STEP-BUFFER-NOTE", trajectory_memory_context="MEMORY-NOTE") + + prompt = captured[0] + assert "STEP-BUFFER-NOTE" in prompt + assert "MEMORY-NOTE" in prompt + assert prompt.index("STEP-BUFFER-NOTE") < prompt.index("MEMORY-NOTE") + + +def test_trajectory_memory_alone_is_included(captured, items): + batch, pred = items + + _run_success(batch, pred, trajectory_memory_context="MEMORY-NOTE") + + assert "MEMORY-NOTE" in captured[0] + + +def test_step_buffer_alone_is_included(captured, items): + batch, pred = items + + _run_success(batch, pred, step_buffer_context="STEP-BUFFER-NOTE") + + assert "STEP-BUFFER-NOTE" in captured[0] + + +def test_no_context_adds_no_previous_steps_section(captured, items): + batch, pred = items + + _run_success(batch, pred) + + assert "## Previous Steps in This Epoch" not in captured[0] + + +def test_failure_analyst_context_is_unchanged(captured, items): + batch, pred = items + failing = [dict(batch[0], hard=0.0, fail_reason="wrong answer")] + + rf.run_error_analyst_minibatch( + "skill", + failing, + pred, + edit_budget=2, + step_buffer_context="STEP-BUFFER-NOTE", + trajectory_memory_context="MEMORY-NOTE", + ) + + prompt = captured[0] + assert "STEP-BUFFER-NOTE" in prompt and "MEMORY-NOTE" in prompt