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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion skillopt_sleep/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions skillopt_sleep/cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions tests/test_harvest_checkpoint_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 "<project>::Codex" cursor may hold a time written
Expand Down