From 81f0da0f30a5b57953e67d3e96e63f04974f319c Mon Sep 17 00:00:00 2001 From: Houston <347629597+HoustonJia@users.noreply.github.com> Date: Thu, 3 Dec 2026 08:28:15 -0500 Subject: [PATCH] fix(sleep): preserve the harvest cursor on task-file replays --- CHANGELOG.md | 3 ++ skillopt_sleep/__main__.py | 5 ++- skillopt_sleep/cycle.py | 8 +++- tests/test_harvest_checkpoint_source.py | 52 +++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7542320d2..3104d0a45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -154,6 +154,9 @@ All notable changes to SkillOpt are documented here. This project adheres to value is rejected with a clear error. ### Fixed +- Replaying a reviewed tasks file with `skillopt-sleep run --tasks-file` now + preserves the transcript harvest cursor so the next harvest can still read + sessions that arrived before the replay. - `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/__main__.py b/skillopt_sleep/__main__.py index 7c02f70cc..d882cf44f 100644 --- a/skillopt_sleep/__main__.py +++ b/skillopt_sleep/__main__.py @@ -526,7 +526,10 @@ def _run_handoff(cfg, args, *, seed_tasks, task_meta: Dict[str, Any], dry: bool) return rc outcome = None try: - outcome = run_sleep_cycle(cfg, seed_tasks=tasks, dry_run=dry, backend=backend) + outcome = run_sleep_cycle( + cfg, seed_tasks=tasks, seed_tasks_from_harvest=seed_tasks is None, + dry_run=dry, backend=backend, + ) except PendingCalls: pass if backend.pending: diff --git a/skillopt_sleep/cycle.py b/skillopt_sleep/cycle.py index 030a37011..00e3c2b86 100644 --- a/skillopt_sleep/cycle.py +++ b/skillopt_sleep/cycle.py @@ -766,6 +766,7 @@ def run_sleep_cycle( cfg: Optional[SleepConfig] = None, *, seed_tasks: Optional[List[TaskRecord]] = None, + seed_tasks_from_harvest: bool = False, dry_run: bool = False, clock: Optional[float] = None, backend: Optional[Backend] = None, @@ -777,6 +778,8 @@ def run_sleep_cycle( cfg : SleepConfig seed_tasks : optional pre-built TaskRecords (used by the experiment to inject a known persona instead of harvesting ~/.claude). + seed_tasks_from_harvest : the handoff driver already harvested this + night's tasks, so completing the night should advance its cursor. dry_run : harvest+mine+replay but DO NOT stage/adopt (report only). clock : fixed epoch seconds for deterministic timestamps in tests. backend : optional pre-built Backend; the handoff driver passes one so @@ -1006,10 +1009,12 @@ def budget_exhausted() -> bool: gate_no_regression=bool(cfg.get("gate_no_regression", False)), ) + if seed_tasks is None or seed_tasks_from_harvest: + state.set_last_harvest(project, started, harvest_source) + if not tasks: report.ended_at = _now_iso(clock) report.notes.append("no tasks mined — nothing to consolidate") - state.set_last_harvest(project, started, harvest_source) state.record_night({"night": night, "accepted": False, "n_tasks": 0}) if not dry_run: state.save() @@ -1334,7 +1339,6 @@ def budget_exhausted() -> bool: ) except Exception: pass - state.set_last_harvest(project, started, harvest_source) night_scores = ( {"baseline": result.baseline_score, "candidate": result.candidate_score} if scores_measured diff --git a/tests/test_harvest_checkpoint_source.py b/tests/test_harvest_checkpoint_source.py index 39e0785b0..95b616b99 100644 --- a/tests/test_harvest_checkpoint_source.py +++ b/tests/test_harvest_checkpoint_source.py @@ -132,6 +132,33 @@ def test_second_source_run_does_not_hide_the_first_sources_sessions(tmp_path): assert state.last_harvest_for(proj, "claude") is None +@pytest.mark.parametrize("empty", [True, False], ids=["empty", "nonempty"]) +def test_tasks_file_run_leaves_unread_sessions_for_the_next_harvest(tmp_path, empty): + from skillopt_sleep.__main__ import main + from skillopt_sleep.cycle import run_sleep_cycle + + project = tmp_path / "proj" + project.mkdir() + claude_home = tmp_path / ".claude" + _write_claude_session(str(claude_home / "projects"), "unread", str(project)) + cfg = _cfg(str(project), str(claude_home), str(tmp_path / ".cursor"), "claude") + state = SleepState.load(cfg.state_path) + previous = "2020-01-01T00:00:00" + state.set_last_harvest(str(project), previous, "claude") + state.save() + tasks_file = tmp_path / "tasks.json" + tasks = [] if empty else [{"id": "reviewed", "project": str(project), "intent": "Summarize the changelog"}] + tasks_file.write_text(json.dumps({"reviewed": True, "tasks": tasks}), encoding="utf-8") + + assert main(["run", "--project", str(project), "--claude-home", str(claude_home), + "--backend", "mock", "--tasks-file", str(tasks_file)]) == 0 + + state = SleepState.load(cfg.state_path) + assert state.night == 1 + assert run_sleep_cycle(cfg, dry_run=True).report.n_sessions == 1 + assert state.last_harvest_for(str(project), "claude") == previous + + def test_a_source_still_skips_what_it_already_harvested(tmp_path): from skillopt_sleep.cycle import run_sleep_cycle @@ -151,6 +178,31 @@ def test_a_source_still_skips_what_it_already_harvested(tmp_path): assert run_sleep_cycle(claude, dry_run=True).report.n_sessions == 0 +@pytest.mark.parametrize("from_file", [True, False], ids=["reviewed-file", "harvested-snapshot"]) +def test_handoff_checkpoints_only_its_own_harvest(tmp_path, from_file): + from skillopt_sleep.__main__ import main + from skillopt_sleep.handoff_backend import HandoffBackend + + project = tmp_path / "proj" + handoff = project / ".skillopt-sleep-handoff" + handoff.mkdir(parents=True) + cfg = _cfg(str(project), str(tmp_path / ".claude"), str(tmp_path / ".cursor"), "claude") + args = ["run", "--backend", "handoff", "--project", str(project), + "--claude-home", cfg.get("claude_home")] + tasks_file = handoff / "tasks.json" + task = {"id": "one", "project": str(project), "intent": "Summarize the changelog"} + tasks_file.write_text(json.dumps({"reviewed": from_file, "tasks": [task]}), encoding="utf-8") + if from_file: + args.extend(["--tasks-file", str(tasks_file)]) + + with mock.patch.object(HandoffBackend, "_call", return_value="[]"): + assert main(args) == 0 + + state = SleepState.load(cfg.state_path) + assert state.night == 1 + assert (state.last_harvest_for(str(project), "claude") is None) == from_file + + # ── source spelling (follow-up to #354) ───────────────────────────────────── # harvest_for_config accepts "Codex" or " codex " as Codex, so the cursor has # to be the Codex one too. A "::Codex" cursor may hold a time written