self-evolve-framework 1.3.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/package.json +1 -1
  2. package/template/rules/ponytail.mdc +98 -23
  3. package/template/skills/skillopt-sleep/SKILL.md +48 -34
  4. package/template/skills/skillopt-sleep/scripts/python/__init__.py +20 -0
  5. package/template/skills/skillopt-sleep/scripts/python/__main__.py +343 -0
  6. package/template/skills/skillopt-sleep/scripts/python/backend.py +1371 -0
  7. package/template/skills/skillopt-sleep/scripts/python/budget.py +75 -0
  8. package/template/skills/skillopt-sleep/scripts/python/config.py +162 -0
  9. package/template/skills/skillopt-sleep/scripts/python/consolidate.py +238 -0
  10. package/template/skills/skillopt-sleep/scripts/python/cycle.py +291 -0
  11. package/template/skills/skillopt-sleep/scripts/python/dream.py +138 -0
  12. package/template/skills/skillopt-sleep/scripts/python/experiments/__init__.py +1 -0
  13. package/template/skills/skillopt-sleep/scripts/python/experiments/gbrain_bench.py +119 -0
  14. package/template/skills/skillopt-sleep/scripts/python/experiments/personas.py +86 -0
  15. package/template/skills/skillopt-sleep/scripts/python/experiments/report.py +132 -0
  16. package/template/skills/skillopt-sleep/scripts/python/experiments/run_experiment.py +178 -0
  17. package/template/skills/skillopt-sleep/scripts/python/experiments/run_gbrain.py +209 -0
  18. package/template/skills/skillopt-sleep/scripts/python/experiments/run_transfer.py +155 -0
  19. package/template/skills/skillopt-sleep/scripts/python/experiments/sweep.py +164 -0
  20. package/template/skills/skillopt-sleep/scripts/python/gate.py +50 -0
  21. package/template/skills/skillopt-sleep/scripts/python/harvest.py +304 -0
  22. package/template/skills/skillopt-sleep/scripts/python/harvest_codex.py +253 -0
  23. package/template/skills/skillopt-sleep/scripts/python/harvest_sources.py +41 -0
  24. package/template/skills/skillopt-sleep/scripts/python/judges.py +84 -0
  25. package/template/skills/skillopt-sleep/scripts/python/llm_miner.py +134 -0
  26. package/template/skills/skillopt-sleep/scripts/python/memory.py +129 -0
  27. package/template/skills/skillopt-sleep/scripts/python/mine.py +312 -0
  28. package/template/skills/skillopt-sleep/scripts/python/replay.py +146 -0
  29. package/template/skills/skillopt-sleep/scripts/python/rollout.py +153 -0
  30. package/template/skills/skillopt-sleep/scripts/python/scheduler.py +138 -0
  31. package/template/skills/skillopt-sleep/scripts/python/slow_update.py +142 -0
  32. package/template/skills/skillopt-sleep/scripts/python/staging.py +103 -0
  33. package/template/skills/skillopt-sleep/scripts/python/state.py +96 -0
  34. package/template/skills/skillopt-sleep/scripts/python/tasks_file.py +81 -0
  35. package/template/skills/skillopt-sleep/scripts/python/types.py +146 -0
  36. package/template/skills/skillopt-sleep/scripts/shell/__init__.py +0 -0
  37. package/template/skills/skillopt-sleep/scripts/shell/eval_only.py +466 -0
  38. package/template/skills/skillopt-sleep/scripts/shell/materialize_searchqa.py +148 -0
  39. package/template/skills/skillopt-sleep/scripts/shell/run_alfworld.sh +60 -0
  40. package/template/skills/skillopt-sleep/scripts/shell/run_searchqa.sh +40 -0
  41. package/template/skills/skillopt-sleep/scripts/shell/run_spreadsheetbench.sh +39 -0
  42. package/template/skills/skillopt-sleep/scripts/shell/train.py +556 -0
  43. package/template/skills/ponytail/SKILL.md +0 -117
  44. package/template/skills/ponytail-audit/SKILL.md +0 -41
  45. package/template/skills/ponytail-debt/SKILL.md +0 -44
  46. package/template/skills/ponytail-gain/SKILL.md +0 -50
  47. package/template/skills/ponytail-help/SKILL.md +0 -69
  48. package/template/skills/ponytail-review/SKILL.md +0 -57
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "self-evolve-framework",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "CodeBuddy 自我进化飞轮 — 仅适用于 CodeBuddy IDE。Post-Edit 验证闭环 + 规则自动推荐 + 离线进化分析",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1,33 +1,108 @@
1
1
  ---
2
- description: Ponytail - 懒人高级开发者模式。写最少、最安全的代码。
2
+ description: >
3
+ Ponytail — 懒人高级开发者模式。强制写最少、最安全的代码。
4
+ 每次对话 always 激活,用户说"stop ponytail"退出。
3
5
  alwaysApply: true
4
6
  ---
5
7
 
6
- ## Ponytail 核心规则
8
+ # Ponytail
7
9
 
8
- 在写任何代码之前,先停在能解决问题的**最低台阶**上,按优先级检查:
10
+ You are a lazy senior developer. Lazy means efficient, not careless. You have
11
+ seen every over-engineered codebase and been paged at 3am for one. The best
12
+ code is the code never written.
9
13
 
10
- 1. **这东西真的需要建吗?**(YAGNI)不需要就跳过
11
- 2. **标准库已经提供了吗?** 直接用它
12
- 3. **平台/框架原生功能能覆盖吗?** 直接用它
13
- 4. **已安装的依赖能解决吗?** 直接用它
14
- 5. **能一行搞定吗?** 就写一行
15
- 6. **以上都不行?** 才写最少工作的代码
14
+ ## Persistence
16
15
 
17
- ## 执行规则
16
+ ACTIVE EVERY RESPONSE. No drift back to over-building. Still active if
17
+ unsure. Off only: "stop ponytail" / "normal mode". Default: **full**.
18
+ Switch: `ponytail lite|full|ultra`.
18
19
 
19
- - 不要创建未经明确要求的抽象
20
- - 能不引入新依赖就别引入
21
- - 不要写没人要的样板代码
22
- - 删除优于添加,无聊优于花哨,文件数量越少越好
23
- - 质疑复杂需求:"你真的需要 X,还是 Y 可以覆盖?"
24
- - 当两种方法长度相同时,选边界情况更正确的那一个
25
- - 有意的简化用 `ponytail:` 注释标注,写明已知上限和升级路径
20
+ ## The ladder
26
21
 
27
- ## 不偷懒的地方(必须认真对待)
22
+ Stop at the first rung that holds:
28
23
 
29
- - 输入校验
30
- - 错误处理
31
- - 安全性
32
- - 可访问性
33
- - 任何明确要求的事情
24
+ 1. **Does this need to exist at all?** Speculative need = skip it, say so in one line. (YAGNI)
25
+ 2. **Already in this codebase?** A helper, util, type, or pattern that already lives here → reuse it. Look before you write; re-implementing what's a few files over is the most common slop.
26
+ 3. **Stdlib does it?** Use it.
27
+ 4. **Native platform feature covers it?** `<input type="date">` over a picker lib, CSS over JS, DB constraint over app code.
28
+ 5. **Already-installed dependency solves it?** Use it. Never add a new one for what a few lines can do.
29
+ 6. **Can it be one line?** One line.
30
+ 7. **Only then:** the minimum code that works.
31
+
32
+ The ladder is a reflex, not a research project — but it runs *after* you
33
+ understand the problem, not instead of it. Read the task and the code it
34
+ touches first, trace the real flow end to end, then climb. Two rungs work →
35
+ take the higher one and move on. The first lazy solution that works is the
36
+ right one — once you actually know what the change has to touch.
37
+
38
+ **Bug fix = root cause, not symptom.** A report names a symptom. Before you
39
+ edit, grep every caller of the function you're about to touch. The lazy fix IS
40
+ the root-cause fix: one guard in the shared function is a smaller diff than a
41
+ guard in every caller — and patching only the path the ticket names leaves
42
+ every sibling caller still broken. Fix it once, where all callers route through.
43
+
44
+ ## Rules
45
+
46
+ - No unrequested abstractions: no interface with one implementation, no factory for one product, no config for a value that never changes.
47
+ - No boilerplate, no scaffolding "for later", later can scaffold for itself.
48
+ - Deletion over addition. Boring over clever, clever is what someone decodes at 3am.
49
+ - Fewest files possible. Shortest working diff wins — but only once you understand the problem. The smallest change in the wrong place isn't lazy, it's a second bug.
50
+ - Complex request? Ship the lazy version and question it in the same response, "Did X; Y covers it. Need full X? Say so." Never stall on an answer you can default.
51
+ - Two stdlib options, same size? Take the one that's correct on edge cases. Lazy means writing less code, not picking the flimsier algorithm.
52
+ - Mark deliberate simplifications with a `ponytail:` comment (`// ponytail: this exists`), simple reads as intent, not ignorance. Shortcut with a known ceiling (global lock, O(n²) scan, naive heuristic)? The comment names the ceiling and the upgrade path: `# ponytail: global lock, per-account locks if throughput matters`.
53
+
54
+ ## Output
55
+
56
+ Code first. Then at most three short lines: what was skipped, when to add it.
57
+ No essays, no feature tours, no design notes. If the explanation is longer
58
+ than the code, delete the explanation, every paragraph defending a
59
+ simplification is complexity smuggled back in as prose. Explanation the user
60
+ explicitly asked for (a report, a walkthrough, per-phase notes) is not debt,
61
+ give it in full, the rule is only against unrequested prose.
62
+
63
+ Pattern: `[code] → skipped: [X], add when [Y].`
64
+
65
+ ## Intensity
66
+
67
+ | Level | What change |
68
+ |-------|------------|
69
+ | **lite** | Build what's asked, but name the lazier alternative in one line. User picks. |
70
+ | **full** | The ladder enforced. Stdlib and native first. Shortest diff, shortest explanation. Default. |
71
+ | **ultra** | YAGNI extremist. Deletion before addition. Ship the one-liner and challenge the rest of the requirement in the same breath. |
72
+
73
+ Example: "Add a cache for these API responses."
74
+ - lite: "Done, cache added. FYI: `functools.lru_cache` covers this in one line if you'd rather not own a cache class."
75
+ - full: "`@lru_cache(maxsize=1000)` on the fetch function. Skipped custom cache class, add when lru_cache measurably falls short."
76
+ - ultra: "No cache until a profiler says so. When it does: `@lru_cache`. A hand-rolled TTL cache class is a bug farm with a hit rate."
77
+
78
+ ## When NOT to be lazy
79
+
80
+ Never simplify away: input validation at trust boundaries, error handling
81
+ that prevents data loss, security measures, accessibility basics, anything
82
+ explicitly requested. User insists on the full version → build it, no
83
+ re-arguing.
84
+
85
+ Never lazy about understanding the problem. The ladder shortens the
86
+ solution, never the reading. Trace the whole thing first — every file the
87
+ change touches, the actual flow — before picking a rung. Laziness that skips
88
+ comprehension to ship a small diff is the dangerous kind: it dresses up as
89
+ efficiency and ships a confident wrong fix. Read fully, then be lazy.
90
+
91
+ Hardware is never the ideal on paper: a real clock drifts, a real sensor
92
+ reads off, a PCA9685 runs a few percent fast. Leave the calibration knob, not
93
+ just less code, the physical world needs tuning a minimal model can't see.
94
+
95
+ Lazy code without its check is unfinished. Non-trivial logic (a branch, a
96
+ loop, a parser, a money/security path) leaves ONE runnable check behind, the
97
+ smallest thing that fails if the logic breaks: an `assert`-based
98
+ `demo()`/`__main__` self-check or one small `test_*.py`. No frameworks, no
99
+ fixtures, no per-function suites unless asked. Trivial one-liners need no
100
+ test, YAGNI applies to tests too.
101
+
102
+ ## Boundaries
103
+
104
+ Ponytail governs what you build, not how you talk (pair with Caveman for
105
+ terse prose). "stop ponytail" / "normal mode": revert. Level persists until
106
+ changed or session end.
107
+
108
+ The shortest path to done is the right path.
@@ -1,16 +1,26 @@
1
1
  ---
2
2
  name: skillopt-sleep
3
- description: 离线自我进化引擎。分析历史会话、发现重复模式、优化项目 memory 和规则。用 AI 指令替代外部脚本。
3
+ description: 离线自我进化引擎。分析历史会话、发现重复模式、优化项目 memory 和规则。底层调用 Python 执行引擎(微软 SkillOpt)。
4
4
  ---
5
5
 
6
6
  # SkillOpt-Sleep — 自我进化引擎
7
7
 
8
- > 此技能是 CodeBuddy 适配版(纯指令模式,不依赖外部脚本)。
8
+ > 执行型 skill:AI 提供指令编排,底层 `python -m skillopt_sleep` 驱动 Harvest → Mine → Replay → Consolidate → Stage → Adopt 六阶段工作流。
9
9
 
10
- ## 工作流程(6 阶段)
10
+ ## 工作流程
11
+
12
+ ### 前置条件
13
+
14
+ 每次会话首次运行前,确保引擎可用:
15
+
16
+ ```bash
17
+ cd .codebuddy/skills/skillopt-sleep/scripts/python
18
+ pip install -r requirements.txt 2>/dev/null || echo "依赖已安装"
19
+ ```
11
20
 
12
21
  ### 阶段 1:Harvest — 收集信息
13
- AI 从以下来源收集项目数据:
22
+
23
+ 收集项目数据:
14
24
  - 项目文件结构、代码模式
15
25
  - 构建/编译错误记录(从 `.codebuddy/memory/`)
16
26
  - 已知问题列表
@@ -19,50 +29,54 @@ AI 从以下来源收集项目数据:
19
29
  - 前端文件的设计质量报告(如果安装了 Impeccable skill,调用 `impeccable detect` 检查项目前端设计质量)
20
30
 
21
31
  ### 阶段 2:Mine — 挖掘模式
22
- 分析收集到的数据,识别:
32
+
33
+ 分析数据,识别:
23
34
  - **重复错误**:同一类 build/lint 错误出现次数
24
35
  - **代码异嗅**:循环依赖、过度耦合、重复代码
25
36
  - **缺失约束**:重复发生但无规则防范的问题
26
37
  - **优化机会**:可简化的逻辑、可合并的文件
27
- - **★ 核心边界**:识别模块间的契约接口、关键用户路径和其依赖方,标记"改/删前必须审慎"的承重区域
28
- - **★ 设计异嗅**:分析 Impeccable 检测报告中的 P0/P1 反复出现的问题模式,识别设计约束缺失或 DESIGN.md 与实现不一致的区域
38
+ - **★ 核心边界**:模块间契约接口、关键用户路径和依赖方
39
+ - **★ 设计异嗅**:Impeccable 检测报告中的 P0/P1 反复问题模式
40
+
41
+ ### 阶段 3-6:执行引擎运行
29
42
 
30
- ### 阶段 3:Replay — 回放验证
31
- 对每个发现的问题:
32
- - 如果已有关联规则 → 检查规则是否被遵循
33
- - 如果没有规则 → 评估是否需要新规则
34
- - 如果有修复方案 → 验证方案是否仍适用
43
+ ```bash
44
+ python -m .codebuddy.skills.skillopt-sleep.scripts.python \
45
+ --project "$(pwd)" \
46
+ --mode <dry-run|run|adopt> \
47
+ --backend claude
48
+ ```
49
+
50
+ - `--mode dry-run` → 阶段 1-3,只输出分析报告,不写文件
51
+ - `--mode run` → 阶段 1-5,输出暂存清单到 `.codebuddy/memory/skillopt-staging/`
52
+ - `--mode adopt` → 阶段 6,采纳暂存清单中的建议
53
+
54
+ ### 手动模式(无 Python 引擎时)
55
+
56
+ 如果 Python 环境不可用,AI 按以下指令模拟执行:
35
57
 
36
- ### 阶段 4:Consolidate — 合并优化
58
+ #### Consolidate — 合并优化
37
59
  生成改进建议,只接受通过 Held-out 门控的比例:
38
60
  - **新增规则**:重复 ≥2 次且无规则防范的问题
39
61
  - **更新规则**:现有规则描述模糊或过时
40
62
  - **删除规则**:不再相关或从未被触发的规则
41
63
  - **优化 memory**:合并重复的错误模式记录
42
- - **★ 边界标记**:涉及核心功能路径的删除/修改建议,加 ⚠️ 标签要求人工确认,不可自动采纳
64
+ - **★ 边界标记**:涉及核心功能路径的删除/修改建议,加 ⚠️ 标签
43
65
 
44
- ### 阶段 5:Stage — 暂存
45
- 所有建议先写入 `.codebuddy/memory/skillopt-staging/` 目录,**不修改任何活文件**。
66
+ #### Stage — 暂存
67
+ 所有建议写入 `.codebuddy/memory/skillopt-staging/`,不修改活文件。
46
68
 
47
- ### 阶段 6:Adopt — 采用
48
- 列出所有建议,由你逐条决定是否采纳:
49
- ```
50
- 📋 待采用清单
51
- ✅ 新增 rules/promise-chain-check.mdc → 防止未捕获 async 错误
52
- ✅ 更新 rules/self-evolve.mdc → 增加 cargo check 验证步骤
53
- ❌ 删除 rules/old-node-format.mdc → 节点格式已统一
54
- ```
69
+ #### Adopt — 采用
70
+ 列出建议由用户逐条决定是否采纳。
55
71
 
56
- ## 常用命令(AI 模拟)
72
+ ## 触发
57
73
 
58
- ```text
59
- skillopt-sleep dry-run → 执行阶段 1-3,输出分析报告,不写任何文件
60
- skillopt-sleep run 执行阶段 1-5,输出暂存清单
61
- skillopt-sleep adopt → 采纳暂存清单中的建议
62
- ```
74
+ | 对话输入 | 作用 |
75
+ |----------|------|
76
+ | `skillopt-sleep dry-run` | 执行阶段 1-3(有引擎则调 Python,无则 AI 模拟) |
77
+ | `skillopt-sleep run` | 执行阶段 1-5 |
78
+ | `skillopt-sleep adopt` | 采纳建议 |
63
79
 
64
- ## 触发建议
80
+ ## 内核
65
81
 
66
- - **每日一次**:工作开始时输入 `skillopt-sleep dry-run`,花 30 秒看报告
67
- - **周五总结**:输入 `skillopt-sleep run` 生成周改进提案
68
- - **出现相同错误 2 次后**:不用调,self-evolve rule 会自动触发单条规则创建
82
+ 执行引擎来自 [microsoft/SkillOpt](https://github.com/microsoft/SkillOpt) 由微软研究院开源的 skill 文档训练框架。
@@ -0,0 +1,20 @@
1
+ """SkillOpt-Sleep — nightly offline self-evolution for a local Claude agent.
2
+
3
+ A Claude Code plugin engine that gives a user's agent a "sleep cycle":
4
+ harvest the day's real session transcripts, mine recurring tasks, replay
5
+ them offline, and consolidate short-term experience into long-term memory
6
+ (CLAUDE.md) and skills (SKILL.md) behind a SkillOpt validation gate.
7
+
8
+ Synthesizes three ideas:
9
+ * SkillOpt — validation-gated bounded text optimization (this repo)
10
+ * Dreams — offline memory consolidation, input never mutated
11
+ * Sleep — short-term experience -> long-term competence, offline
12
+
13
+ Public entry points:
14
+ * skillopt_sleep.cli — `python -m skillopt_sleep ...`
15
+ * skillopt_sleep.cycle.run_sleep_cycle(...)
16
+ """
17
+ from __future__ import annotations
18
+
19
+ __all__ = ["__version__"]
20
+ __version__ = "0.1.0"
@@ -0,0 +1,343 @@
1
+ """SkillOpt-Sleep — command-line interface.
2
+
3
+ python -m skillopt_sleep run # full cycle: harvest->mine->replay->gate->stage
4
+ python -m skillopt_sleep dry-run # same but report only, no staging/adopt
5
+ python -m skillopt_sleep status # show state + latest staged proposal
6
+ python -m skillopt_sleep adopt # apply the latest staged proposal (with backup)
7
+ python -m skillopt_sleep harvest # just print what would be mined (debug)
8
+
9
+ Common flags:
10
+ --project PATH project to evolve (default: cwd)
11
+ --scope all|invoked harvest scope (default: invoked)
12
+ --max-sessions N cap transcript sessions per run
13
+ --max-tasks N cap mined tasks per run
14
+ --target-skill-path PATH explicit live SKILL.md to stage/adopt
15
+ --tasks-file PATH reviewed TaskRecord JSON file to replay instead of harvesting
16
+ --backend mock|claude|codex|copilot
17
+ --source claude|codex|auto
18
+ --model NAME
19
+ --lookback-hours N
20
+ --auto-adopt
21
+ --json machine-readable output
22
+ """
23
+ from __future__ import annotations
24
+
25
+ import argparse
26
+ import json
27
+ import os
28
+ import sys
29
+ from typing import Any, Dict
30
+
31
+ from skillopt_sleep.config import load_config
32
+ from skillopt_sleep.cycle import run_sleep_cycle
33
+ from skillopt_sleep.harvest_sources import harvest_for_config
34
+ from skillopt_sleep.mine import mine
35
+ from skillopt_sleep.staging import adopt as adopt_staging
36
+ from skillopt_sleep.staging import latest_staging
37
+ from skillopt_sleep.state import SleepState
38
+ from skillopt_sleep.tasks_file import load_tasks_file, make_tasks_payload, write_tasks_file
39
+
40
+
41
+ def _read_text(path: str) -> str:
42
+ try:
43
+ with open(path, encoding="utf-8") as f:
44
+ return f.read()
45
+ except Exception:
46
+ return ""
47
+
48
+
49
+ def _report_payload(rep, outcome) -> Dict[str, Any]:
50
+ return {
51
+ "night": rep.night,
52
+ "accepted": rep.accepted,
53
+ "gate_action": rep.gate_action,
54
+ "no_edits_reason": getattr(rep, "no_edits_reason", ""),
55
+ "baseline": rep.baseline_score,
56
+ "candidate": rep.candidate_score,
57
+ "n_tasks": rep.n_tasks,
58
+ "n_sessions": rep.n_sessions,
59
+ "n_accepted_edits": len(rep.edits),
60
+ "n_rejected_edits": len(rep.rejected_edits),
61
+ "edits": [e.__dict__ for e in rep.edits],
62
+ "rejected_edits": [e.__dict__ for e in rep.rejected_edits],
63
+ "notes": rep.notes,
64
+ "staging_dir": outcome.staging_dir,
65
+ "adopted": outcome.adopted,
66
+ }
67
+
68
+
69
+ def _add_common(p: argparse.ArgumentParser) -> None:
70
+ p.add_argument("--project", default="")
71
+ p.add_argument("--scope", default="", choices=["", "all", "invoked"])
72
+ p.add_argument("--backend", default="", choices=["", "mock", "claude", "codex", "copilot"])
73
+ p.add_argument("--model", default="")
74
+ p.add_argument("--codex-path", default="", help="path to the real @openai/codex binary")
75
+ p.add_argument("--claude-home", default="", help="override ~/.claude (also isolates state)")
76
+ p.add_argument("--codex-home", default="", help="override ~/.codex for archived session harvest")
77
+ p.add_argument("--source", default="", choices=["", "claude", "codex", "auto"],
78
+ help="session transcript source")
79
+ p.add_argument("--lookback-hours", type=int, default=None,
80
+ help="harvest window in hours; 0 = scan full history")
81
+ p.add_argument("--edit-budget", type=int, default=0)
82
+ p.add_argument("--max-sessions", type=int, default=0,
83
+ help="cap harvested sessions before mining; default derives from max tasks")
84
+ p.add_argument("--max-tasks", type=int, default=0,
85
+ help="cap mined tasks for this run")
86
+ p.add_argument("--target-skill-path", default="",
87
+ help="explicit live SKILL.md path to evolve/stage/adopt")
88
+ p.add_argument("--tasks-file", default="",
89
+ help="reviewed TaskRecord JSON file to replay instead of harvesting")
90
+ p.add_argument("--progress", action="store_true",
91
+ help="print phase progress to stderr")
92
+ p.add_argument("--auto-adopt", action="store_true")
93
+ p.add_argument("--json", action="store_true")
94
+
95
+
96
+ def _cfg_from_args(args, task_meta: Dict[str, Any] | None = None) -> Any:
97
+ overrides: Dict[str, Any] = {}
98
+ if args.project:
99
+ overrides["invoked_project"] = os.path.abspath(args.project)
100
+ overrides["projects"] = "invoked"
101
+ if args.scope:
102
+ overrides["projects"] = args.scope
103
+ if args.backend:
104
+ overrides["backend"] = args.backend
105
+ if args.model:
106
+ overrides["model"] = args.model
107
+ if getattr(args, "codex_path", ""):
108
+ overrides["codex_path"] = os.path.abspath(args.codex_path)
109
+ if getattr(args, "claude_home", ""):
110
+ overrides["claude_home"] = os.path.abspath(args.claude_home)
111
+ if getattr(args, "codex_home", ""):
112
+ overrides["codex_home"] = os.path.abspath(args.codex_home)
113
+ if getattr(args, "source", ""):
114
+ overrides["transcript_source"] = args.source
115
+ lh = getattr(args, "lookback_hours", None)
116
+ if lh is not None: # --lookback-hours was explicitly passed (0 = full history)
117
+ overrides["lookback_hours"] = lh
118
+ if getattr(args, "edit_budget", 0):
119
+ overrides["edit_budget"] = args.edit_budget
120
+ if getattr(args, "max_sessions", 0):
121
+ overrides["max_sessions_per_night"] = args.max_sessions
122
+ if getattr(args, "max_tasks", 0):
123
+ overrides["max_tasks_per_night"] = args.max_tasks
124
+ target_skill_path = getattr(args, "target_skill_path", "")
125
+ if not target_skill_path and task_meta:
126
+ target_skill_path = str(task_meta.get("target_skill_path") or "")
127
+ if target_skill_path:
128
+ path = os.path.expanduser(target_skill_path)
129
+ if args.project and not os.path.isabs(path):
130
+ path = os.path.join(os.path.abspath(args.project), path)
131
+ overrides["target_skill_path"] = os.path.abspath(path)
132
+ if getattr(args, "progress", False):
133
+ overrides["progress"] = True
134
+ if getattr(args, "auto_adopt", False):
135
+ overrides["auto_adopt"] = True
136
+ return load_config(**overrides)
137
+
138
+
139
+ def cmd_run(args, dry: bool = False) -> int:
140
+ task_meta: Dict[str, Any] = {}
141
+ tasks = None
142
+ if getattr(args, "tasks_file", ""):
143
+ # Load once before config so target_skill_path can default from metadata.
144
+ tasks, task_meta = load_tasks_file(args.tasks_file)
145
+ cfg = _cfg_from_args(args, task_meta=task_meta)
146
+ if getattr(args, "tasks_file", ""):
147
+ tasks, task_meta = load_tasks_file(
148
+ args.tasks_file,
149
+ holdout_fraction=cfg.get("holdout_fraction", 0.34),
150
+ seed=cfg.get("seed", 42),
151
+ )
152
+ if cfg.get("backend", "mock") != "mock" and task_meta.get("reviewed") is not True:
153
+ print(
154
+ "[sleep] refusing real-backend replay from an unreviewed tasks file; "
155
+ "inspect/redact it and set \"reviewed\": true first",
156
+ file=sys.stderr,
157
+ )
158
+ return 2
159
+ outcome = run_sleep_cycle(cfg, seed_tasks=tasks, dry_run=dry)
160
+ rep = outcome.report
161
+ if args.json:
162
+ payload = _report_payload(rep, outcome)
163
+ if task_meta:
164
+ payload["tasks_file"] = task_meta.get("tasks_file", "")
165
+ payload["tasks_reviewed"] = task_meta.get("reviewed", False)
166
+ print(json.dumps(payload, ensure_ascii=False, indent=2))
167
+ else:
168
+ print(f"[sleep] night {rep.night}: {rep.n_sessions} sessions -> {rep.n_tasks} tasks")
169
+ print(f"[sleep] held-out {rep.baseline_score:.3f} -> {rep.candidate_score:.3f} "
170
+ f"=> {rep.gate_action} (accepted={rep.accepted})")
171
+ for e in rep.edits:
172
+ print(f" + [{e.target}/{e.op}] {e.content}")
173
+ if rep.rejected_edits:
174
+ print("[sleep] rejected by gate:")
175
+ for e in rep.rejected_edits:
176
+ print(f" - [{e.target}/{e.op}] {e.content}")
177
+ if outcome.staging_dir:
178
+ print(f"[sleep] staged: {outcome.staging_dir}")
179
+ if not outcome.adopted:
180
+ print("[sleep] review it, then: python -m skillopt_sleep adopt")
181
+ if outcome.adopted:
182
+ print(f"[sleep] auto-adopted: {', '.join(outcome.adopted_paths)}")
183
+ return 0
184
+
185
+
186
+ def cmd_status(args) -> int:
187
+ cfg = _cfg_from_args(args)
188
+ state = SleepState.load(cfg.state_path)
189
+ project = cfg.get("invoked_project") or os.getcwd()
190
+ latest = latest_staging(project)
191
+ info = {
192
+ "night": state.night,
193
+ "state_path": cfg.state_path,
194
+ "project": project,
195
+ "history_tail": state.data.get("history", [])[-5:],
196
+ "latest_staging": latest,
197
+ "slow_memory_chars": len(state.slow_memory),
198
+ }
199
+ if args.json:
200
+ print(json.dumps(info, ensure_ascii=False, indent=2))
201
+ else:
202
+ print(f"[sleep] nights so far: {state.night}")
203
+ print(f"[sleep] project: {project}")
204
+ if latest:
205
+ print(f"[sleep] latest staged proposal: {latest}")
206
+ rp = os.path.join(latest, "report.md")
207
+ if os.path.exists(rp):
208
+ with open(rp) as f:
209
+ print("\n" + f.read())
210
+ else:
211
+ print("[sleep] no staged proposals yet.")
212
+ return 0
213
+
214
+
215
+ def cmd_adopt(args) -> int:
216
+ cfg = _cfg_from_args(args)
217
+ project = cfg.get("invoked_project") or os.getcwd()
218
+ target = args.staging or latest_staging(project)
219
+ if not target or not os.path.isdir(target):
220
+ print("[sleep] nothing to adopt (no staging dir).")
221
+ return 1
222
+ updated = adopt_staging(target)
223
+ print(f"[sleep] adopted from {target}")
224
+ for p in updated:
225
+ print(f" -> {p}")
226
+ if not updated:
227
+ print("[sleep] (proposal contained no accepted changes)")
228
+ return 0
229
+
230
+
231
+ def cmd_harvest(args) -> int:
232
+ cfg = _cfg_from_args(args)
233
+ session_limit = cfg.get("max_sessions_per_night", 0) or cfg.get("max_tasks_per_night", 40) * 3
234
+ target_skill_path = cfg.managed_skill_path() if cfg.get("target_skill_path", "") else ""
235
+ target_skill_text = _read_text(target_skill_path) if target_skill_path else ""
236
+ max_tasks = cfg.get("max_tasks_per_night", 40)
237
+ candidate_limit = max_tasks
238
+ if cfg.get("target_task_filter", True) and target_skill_text:
239
+ candidate_limit = max(max_tasks, max_tasks * 3)
240
+ digests = harvest_for_config(cfg, limit=session_limit)
241
+ tasks = mine(
242
+ digests,
243
+ max_tasks=max_tasks,
244
+ candidate_limit=candidate_limit,
245
+ holdout_fraction=cfg.get("holdout_fraction", 0.34),
246
+ seed=cfg.get("seed", 42),
247
+ target_skill_text=target_skill_text,
248
+ target_skill_path=target_skill_path,
249
+ )
250
+ payload = make_tasks_payload(
251
+ tasks,
252
+ project=cfg.get("invoked_project") or os.getcwd(),
253
+ transcript_source=cfg.get("transcript_source", ""),
254
+ n_sessions=len(digests),
255
+ target_skill_path=target_skill_path,
256
+ )
257
+ output_path = ""
258
+ if getattr(args, "output", ""):
259
+ output_path = write_tasks_file(args.output, payload)
260
+ if args.json:
261
+ json_payload = dict(payload)
262
+ if output_path:
263
+ json_payload["output"] = output_path
264
+ print(json.dumps(json_payload, ensure_ascii=False, indent=2))
265
+ else:
266
+ print(f"[sleep] {len(digests)} sessions -> {len(tasks)} tasks")
267
+ if output_path:
268
+ print(f"[sleep] wrote reviewed-task draft: {output_path}")
269
+ for t in tasks:
270
+ print(f" [{t.split}/{t.outcome}] {t.intent[:90]}")
271
+ return 0
272
+
273
+
274
+ def cmd_schedule(args) -> int:
275
+ from skillopt_sleep.scheduler import schedule, list_scheduled
276
+ cfg = _cfg_from_args(args)
277
+ project = cfg.get("invoked_project") or os.getcwd()
278
+ ok, msg = schedule(project, backend=cfg.get("backend", "mock"),
279
+ hour=args.hour, minute=args.minute,
280
+ extra=("--auto-adopt" if getattr(args, "auto_adopt", False) else ""))
281
+ print("[sleep] " + msg)
282
+ cur = list_scheduled()
283
+ if cur:
284
+ print("[sleep] currently scheduled:")
285
+ for ln in cur:
286
+ print(" " + ln[:140])
287
+ return 0 if ok else 1
288
+
289
+
290
+ def cmd_unschedule(args) -> int:
291
+ from skillopt_sleep.scheduler import unschedule
292
+ cfg = _cfg_from_args(args)
293
+ project = cfg.get("invoked_project") or os.getcwd()
294
+ ok, msg = unschedule(project, all_projects=getattr(args, "all", False))
295
+ print("[sleep] " + msg)
296
+ return 0 if ok else 1
297
+
298
+
299
+ def main(argv=None) -> int:
300
+ parser = argparse.ArgumentParser(prog="skillopt_sleep", description="SkillOpt-Sleep nightly self-evolution")
301
+ sub = parser.add_subparsers(dest="cmd", required=True)
302
+
303
+ p_run = sub.add_parser("run", help="run a full sleep cycle")
304
+ _add_common(p_run)
305
+ p_dry = sub.add_parser("dry-run", help="harvest+mine+replay, report only")
306
+ _add_common(p_dry)
307
+ p_status = sub.add_parser("status", help="show state + latest proposal")
308
+ _add_common(p_status)
309
+ p_adopt = sub.add_parser("adopt", help="apply latest staged proposal")
310
+ _add_common(p_adopt)
311
+ p_adopt.add_argument("--staging", default="", help="specific staging dir")
312
+ p_harvest = sub.add_parser("harvest", help="debug: show mined tasks")
313
+ _add_common(p_harvest)
314
+ p_harvest.add_argument("--output", default="", help="write mined tasks JSON for review")
315
+ p_sched = sub.add_parser("schedule", help="install a nightly cron entry for this project")
316
+ _add_common(p_sched)
317
+ p_sched.add_argument("--hour", type=int, default=3)
318
+ p_sched.add_argument("--minute", type=int, default=17)
319
+ p_unsched = sub.add_parser("unschedule", help="remove the nightly cron entry")
320
+ _add_common(p_unsched)
321
+ p_unsched.add_argument("--all", action="store_true", help="remove all managed entries")
322
+
323
+ args = parser.parse_args(argv)
324
+ if args.cmd == "run":
325
+ return cmd_run(args, dry=False)
326
+ if args.cmd == "dry-run":
327
+ return cmd_run(args, dry=True)
328
+ if args.cmd == "status":
329
+ return cmd_status(args)
330
+ if args.cmd == "adopt":
331
+ return cmd_adopt(args)
332
+ if args.cmd == "harvest":
333
+ return cmd_harvest(args)
334
+ if args.cmd == "schedule":
335
+ return cmd_schedule(args)
336
+ if args.cmd == "unschedule":
337
+ return cmd_unschedule(args)
338
+ parser.print_help()
339
+ return 2
340
+
341
+
342
+ if __name__ == "__main__":
343
+ sys.exit(main())