4e7add899d
Add skillopt/sleep — a deployment-time companion to SkillOpt that gives a
local Claude agent a nightly "sleep cycle":
harvest ~/.claude transcripts -> mine recurring tasks -> replay offline
-> consolidate (reflect -> bounded edit -> held-out GATE) -> stage -> adopt
Synthesizes SkillOpt (validation-gated bounded text optimization, reusing
skillopt.evaluation.gate verbatim), Claude Dreams (offline consolidation;
input never mutated; review-then-adopt), and the agent-sleep paper
(short-term experience -> long-term competence).
Engine (skillopt/sleep/, import-light, py>=3.10):
- harvest.py read-only parse of session JSONL + history.jsonl
- mine.py sessions -> TaskRecords (heuristic miner + LLM hook)
- backend.py MockBackend (deterministic, no API) + AnthropicBackend
- replay.py offline re-run -> (hard, soft) scores
- consolidate.py one SkillOpt epoch behind a held-out gate
- memory.py protected-region edits to SKILL.md / CLAUDE.md
- staging.py stage proposals; adopt with backup (Dreams safety contract)
- cycle.py + __main__.py orchestrator + CLI (run/dry-run/status/adopt/harvest)
Plugin (skillopt-sleep-plugin/): plugin.json, /sleep command, skillopt-sleep
skill, SessionEnd hook, bundled runner + cron generator.
Validation (deterministic, no API): persona experiment proves held-out lift
(researcher 0.33->1.0, programmer 0.32->1.0) AND that the gate rejects an
injected harmful edit. 13 stdlib-unittest tests pass, incl. full cycle +
adopt-with-backup and parsing of real on-disk transcripts.
Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
128 lines
4.6 KiB
Python
128 lines
4.6 KiB
Python
"""SkillOpt-Sleep — core data types.
|
|
|
|
These dataclasses are the interfaces between the sleep-cycle stages
|
|
(harvest -> mine -> replay -> consolidate -> stage). They are intentionally
|
|
plain (no slots, no heavy deps) so the package imports cleanly on any
|
|
Python 3.8+ interpreter and the deterministic experiment runs with zero
|
|
external dependencies.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field, asdict
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
|
|
# ── Stage 1: harvest ──────────────────────────────────────────────────────────
|
|
|
|
@dataclass
|
|
class SessionDigest:
|
|
"""A normalized summary of one Claude Code session transcript.
|
|
|
|
Produced by :mod:`skillopt.sleep.harvest` from a ``<sessionId>.jsonl``
|
|
transcript plus ``history.jsonl`` entries.
|
|
"""
|
|
|
|
session_id: str
|
|
project: str
|
|
git_branch: str = ""
|
|
started_at: str = ""
|
|
ended_at: str = ""
|
|
user_prompts: List[str] = field(default_factory=list)
|
|
assistant_finals: List[str] = field(default_factory=list)
|
|
tools_used: List[str] = field(default_factory=list)
|
|
files_touched: List[str] = field(default_factory=list)
|
|
feedback_signals: List[str] = field(default_factory=list) # "still broken", "perfect", ...
|
|
n_user_turns: int = 0
|
|
n_assistant_turns: int = 0
|
|
raw_path: str = ""
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
return asdict(self)
|
|
|
|
|
|
# ── Stage 2: mine ─────────────────────────────────────────────────────────────
|
|
|
|
@dataclass
|
|
class TaskRecord:
|
|
"""A self-contained recurring task mined from one or more sessions.
|
|
|
|
This is the *training unit* of the sleep cycle — the analogue of a
|
|
SkillOpt benchmark item.
|
|
"""
|
|
|
|
id: str
|
|
project: str
|
|
intent: str # what the user wanted (the "question")
|
|
context_excerpt: str = "" # minimal context needed to attempt it
|
|
attempted_solution: str = "" # what the agent produced before
|
|
outcome: str = "unknown" # success | fail | mixed | unknown
|
|
reference_kind: str = "none" # exact | rubric | none
|
|
reference: str = "" # exact answer, or rubric text
|
|
tags: List[str] = field(default_factory=list)
|
|
source_sessions: List[str] = field(default_factory=list)
|
|
split: str = "replay" # replay (train) | holdout (test)
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
return asdict(self)
|
|
|
|
@classmethod
|
|
def from_dict(cls, d: Dict[str, Any]) -> "TaskRecord":
|
|
known = {f for f in cls.__dataclass_fields__} # type: ignore[attr-defined]
|
|
return cls(**{k: v for k, v in d.items() if k in known})
|
|
|
|
|
|
# ── Stage 3: replay ───────────────────────────────────────────────────────────
|
|
|
|
@dataclass
|
|
class ReplayResult:
|
|
"""Outcome of re-running one TaskRecord offline under a given skill+memory."""
|
|
|
|
id: str
|
|
hard: float = 0.0 # 0/1 exact, or continuous reward
|
|
soft: float = 0.0 # partial credit / judge score 0..1
|
|
response: str = ""
|
|
fail_reason: str = ""
|
|
task_type: str = "task"
|
|
judge_rationale: str = ""
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
return asdict(self)
|
|
|
|
|
|
# ── Stage 4/5: consolidation report ───────────────────────────────────────────
|
|
|
|
@dataclass
|
|
class EditRecord:
|
|
"""One bounded edit proposed/applied to skill or memory."""
|
|
|
|
target: str # "skill" | "memory"
|
|
op: str # add | delete | replace
|
|
content: str = ""
|
|
anchor: str = "" # for replace/delete: text being changed
|
|
rationale: str = ""
|
|
|
|
|
|
@dataclass
|
|
class SleepReport:
|
|
"""Everything one night produced — written to staging for review."""
|
|
|
|
night: int
|
|
project: str
|
|
started_at: str = ""
|
|
ended_at: str = ""
|
|
n_sessions: int = 0
|
|
n_tasks: int = 0
|
|
n_replayed: int = 0
|
|
baseline_score: float = 0.0
|
|
candidate_score: float = 0.0
|
|
accepted: bool = False
|
|
gate_action: str = ""
|
|
edits: List[EditRecord] = field(default_factory=list)
|
|
rejected_edits: List[EditRecord] = field(default_factory=list)
|
|
tokens_used: int = 0
|
|
notes: List[str] = field(default_factory=list)
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
d = asdict(self)
|
|
return d
|