Zi Kong
Self-controlled autonomous iteration for opencode itself with cross-session memory and safe rollb…
by cycleuser · MIT
The skill
Self-controlled autonomous iteration for opencode itself with cross-session memory and safe rollback. Triggers when: Needing opencode self-improvement, continuous skill iteration, or autonomous execution of long-running tasks. Commands: - /自控 <目标> - Start autonomous iteration - /自控 status - View iteration status - /自控 pause - Pause iteration - /自控 resume - Resume iteration - /自控 log - View iteration log - /auto <goal> - English command for autonomous iteration - /auto status - View status (English) - /auto pause - Pause iteration (English) - /auto resume - Resume iteration (English) Capabilities: True autonomous while(true) loop, cross-session context persistence, self-review after each iteration, budget control with time/token management, parallel task handling, decision tree for autonomous choices, rollback mechanism for safe iteration
Safety Rules
参见 _shared/core/safety-rules.md — 所有安全规则从共享层加载。
自控 (Self-Controlled Iteration)
真正的自主迭代技能。控制 opencode 自身,实现持续自我改进。 True autonomous iteration. Controls opencode itself for continuous self-improvement.
Quick Commands
| Command | Description |
|---|---|
/自控 <目标> |
启动自主迭代 |
/自控 status |
查看迭代状态 |
/自控 pause |
暂停迭代 |
/自控 resume |
恢复迭代 |
/自控 log |
查看迭代日志 |
/auto <goal> |
Start autonomous iteration |
/auto status |
Check iteration status |
/auto pause |
Pause iteration |
/auto resume |
Resume iteration |
核心架构/Core Architecture
┌─────────────────────────────────────────────────────────────────────┐
│ 自控架构/Self-Control Architecture │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────────────────────────────────────────────────────┐ │
│ │ 自主循环/Autonomous Loop │ │
│ │ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ │ │
│ │ │ 感知 │──▶│ 决策 │──▶│ 执行 │──▶│ 审查 │──┐ │ │
│ │ │Perceive│ │Decide │ │Execute │ │Review │ │ │ │
│ │ └────────┘ └────────┘ └────────┘ └────────┘ │ │ │
│ │ ▲ │ │ │
│ │ └──────────────────────────────────────────────┘ │ │
│ └──────────────────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────────────────┐ │
│ │ 持久化层/Persistence Layer │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ 状态存储 │ │ 决策日志 │ │ 回滚快照 │ │ 预算追踪 │ │ │
│ │ │ State │ │ Decision│ │ Snapshot │ │ Budget │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │
│ └──────────────────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────────────────┐ │
│ │ 安全层/Safety Layer │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ 变更审查 │ │ 回滚机制 │ │ 预算限制 │ │ 用户确认 │ │ │
│ │ │ Review │ │ Rollback │ │ Limit │ │ Confirm │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │
│ └──────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────┘
自主循环/Autonomous Loop
循环状态机/Loop State Machine
┌─────────────────────────────────────────────────────────────────────┐
│ 状态机/State Machine │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────┐ │
│ │ INITIAL │ │
│ └────┬────┘ │
│ │ start │
│ ▼ │
│ ┌─────────┐ │
│ │ RUNNING │◀────────────────────┐ │
│ └────┬────┘ │ │
│ │ │ loop │
│ │ decide ┌─┴─────┐ │
│ ▼ │ │ │
│ ┌─────────┐ YES NO │
│ │PERCEIVE │ │ │ │
│ └────┬────┘ │ │ │
│ │ │ │ │
│ ▼ │ │ │
│ ┌─────────┐ ┌┴───────▼────────┐ │
│ │ DECIDE │ │ CONTINUE? │ │
│ └────┬────┘ └─────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ EXECUTE │────▶│ REVIEW │────▶│ SAVE │ │
│ └─────────┘ └─────────┘ └────┬────┘ │
│ │ │
│ │ │
│ ┌───────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ PAUSED │ │ STOPPING│ │ DONE │ │
│ └─────────┘ └─────────┘ └─────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────┘
循环伪代码/Loop Pseudocode
class AutonomousLoop:
def __init__(self, goal: str):
self.goal = goal
self.state = "INITIAL"
self.context = load_context() # 跨会话加载
self.budget = load_budget()
self.decisions = []
self.hard_timeout = time.monotonic() + self.budget.time_limit # Hard timeout guard
def run(self):
"""主循环/Main loop"""
while True:
# Hard timeout guard: force exit if budget exceeded
if time.monotonic() >= self.hard_timeout:
self.state = "STOPPING"
self.save_and_exit()
break
# Budget check: stop if tokens or time exhausted
if not self.budget.has_remaining():
self.state = "STOPPING"
self.save_and_exit()
break
# 1. 检查状态
if self.state == "PAUSED":
wait_for_resume()
continue
if self.state == "STOPPING":
self.save_and_exit()
break
if self.state == "DONE":
self.report_and_exit()
break
# 2. 感知当前状态
perception = self.perceive()
# 3. 决策下一步
decision = self.decide(perception)
# 4. 检查预算
if not self.check_budget(decision):
self.state = "STOPPING"
continue
# 5. 执行决策
result = self.execute(decision)
# 6. 审查结果
review = self.review(result)
# 7. 保存状态
self.save_state(perception, decision, result, review)
# 8. 决定是否继续
if not self.should_continue(review):
self.state = "DONE"
跨会话记忆/Cross-Session Memory
上下文存储/Context Storage
{
"session_id": "auto_001",
"goal": "改进所有技能的神似程度",
"started_at": "2024-01-01T00:00:00Z",
"state": "RUNNING",
"progress": {
"completed_tasks": [
{"id": 1, "task": "改进莎士比亚风格", "time": "45min"},
{"id": 2, "task": "改进鲁迅风格", "time": "38min"}
],
"current_task": {"id": 3, "task": "改进金庸风格", "progress": "60%"},
"pending_tasks": [
{"id": 4, "task": "改进古龙风格"},
{"id": 5, "task": "改进卡尔维诺风格"}
]
},
"context": {
"decisions": [
{"id": 1, "choice": "深度重构", "rationale": "表面改进不够"},
{"id": 2, "choice": "添加创作练习", "rationale": "提高实用性"}
],
"assumptions": [
{"id": 1, "content": "用户希望神似而非形似", "verified": true}
],
"issues": [
{"id": 1, "description": "部分规则文件过长", "status": "resolved"}
]
},
"budget": {
"time_limit": "4h",
"time_used": "1h 23min",
"token_limit": 500000,
"token_used": 185000
},
"snapshots": [
{"id": 1, "time": "T+45min", "files_changed": 3},
{"id": 2, "time": "T+1h23min", "files_changed": 5}
],
"last_updated": "2024-01-01T01:23:00Z"
}
会话恢复/Session Recovery
新会话启动时/On New Session:
↓
检测活跃迭代/Detect active iteration
↓
┌─────────────────────────────────────┐
│ 发现未完成的迭代/Incomplete found │
├─────────────────────────────────────┤
│ 目标/Goal: {goal} │
│ 进度/Progress: {completed}/{total} │
│ 上次时间/Last: {timestamp} │
│ 预算剩余/Budget: {remaining} │
└─────────────────────────────────────┘
↓
请选择/Please Choose:
[A] 恢复迭代/Resume
[B] 查看日志/View Log
[C] 修改目标/Modify Goal
[D] 停止迭代/Stop
预算控制/Budget Control
预算分配/Budget Allocation
【时间预算/Time Budget】
tiny: 10 分钟 → 只做 P0 任务
small: 30 分钟 → P0 + 部分 P1
medium: 60 分钟 → P0 + P1 + 部分 P2
large: 120 分钟 → 完整迭代
xlarge: 240 分钟 → 多轮迭代
unlimited: 无限制 → 持续改进
【Token 预算/Token Budget】
每轮迭代估算:
- 感知/Perceive: ~2000 tokens
- 决策/Decide: ~3000 tokens
- 执行/Execute: ~10000 tokens (代码生成)
- 审查/Review: ~5000 tokens
- 保存/Save: ~1000 tokens
总计/Total: ~21000 tokens/iteration
预算检查/Budget Check
def check_budget(self, decision: dict) -> bool:
"""检查预算是否允许执行决策"""
estimated_tokens = estimate_tokens(decision)
estimated_time = estimate_time(decision)
# 时间检查
time_remaining = self.budget.time_limit - self.budget.time_used
if estimated_time > time_remaining * 0.9: # 保留 10% 余量
return False
# Token 检查
token_remaining = self.budget.token_limit - self.budget.token_used
if estimated_tokens > token_remaining * 0.9:
return False
return True
def adjust_scope(self, budget_status: str):
"""根据预算状态调整任务范围"""
if budget_status == "CRITICAL": # <5% 剩余
# 只做 P0 任务
self.task_queue = [t for t in self.task_queue if t.priority == "P0"]
elif budget_status == "LOW": # <20% 剩余
# 跳过 P2 任务
self.task_queue = [t for t in self.task_queue if t.priority != "P2"]
elif budget_status == "MEDIUM": # <50% 剩余
# 正常执行,但记录警告
self.log_warning("Budget below 50%")
自我审查/Self-Review
审查维度/Review Dimensions
## 自我审查表/Self-Review Checklist
### 代码质量/Code Quality
- [ ] 无语法错误/No syntax errors
- [ ] 命名清晰/Clear naming
- [ ] 函数简短/Short functions
- [ ] 无重复代码/No duplication
### 功能正确/Functionality
- [ ] 核心功能完整/Core complete
- [ ] 边界情况处理/Edge cases
- [ ] 错误处理完善/Error handling
- [ ] 测试覆盖/Test coverage
### 文档完整/Documentation
- [ ] README 更新/README updated
- [ ] 注释充分/Comments adequate
- [ ] 示例正确/Examples correct
### 安全审查/Security
- [ ] 无危险命令/No dangerous commands
- [ ] 无凭证泄露/No credential leak
- [ ] 无资源耗尽/No resource exhaustion
审查决策/Review Decision
def review_result(self, result: dict) -> dict:
"""审查执行结果,决定下一步"""
review = {
"passed": True,
"issues": [],
"suggestions": [],
"next_action": "continue"
}
# 检查代码质量
if not check_code_quality(result["code"]):
review["passed"] = False
review["issues"].append("Code quality issues")
review["next_action"] = "fix_quality"
# 检查测试覆盖
if get_test_coverage() < 70:
review["suggestions"].append("Increase test coverage")
# 检查预算
if self.budget.remaining < 20%:
review["next_action"] = "wrap_up"
# 检查目标完成度
if self.progress.completed >= 80%:
review["next_action"] = "finalize"
return review
安全机制/Safety Mechanisms
变更审查/Change Review
def review_changes(self, changes: list) -> dict:
"""审查所有变更,确保安全"""
review = {
"approved": [],
"rejected": [],
"needs_confirm": []
}
for change in changes:
risk = assess_risk(change)
if risk.level == "CRITICAL":
review["rejected"].append({
"change": change,
"reason": "Critical security risk"
})
elif risk.level == "HIGH":
review["needs_confirm"].append({
"change": change,
"reason": "High risk, needs user confirm"
})
else:
review["approved"].append(change)
return review
回滚机制/Rollback
def create_snapshot(self) -> str:
"""创建回滚快照"""
snapshot_id = f"snapshot_{int(time.time())}"
# 保存当前文件状态
changed_files = get_changed_files()
snapshot = {
"id": snapshot_id,
"timestamp": datetime.now().isoformat(),
"files": {}
}
for file in changed_files:
snapshot["files"][file] = {
"content": read_file(file),
"hash": hash_file(file)
}
# 保存快照
save_snapshot(snapshot_id, snapshot)
return snapshot_id
def rollback(self, snapshot_id: str) -> bool:
"""回滚到指定快照"""
snapshot = load_snapshot(snapshot_id)
if not snapshot:
return False
for file, data in snapshot["files"].items():
write_file(file, data["content"])
log_action("rollback", {"snapshot_id": snapshot_id})
return True
使用示例/Examples
示例 1:改进所有技能
用户/User: /自控 改进所有技能的神似程度
→ 启动自主循环/Start autonomous loop
→ 分析所有技能/Analyze all skills
→ 制定改进计划/Create improvement plan
→ 执行改进/Execute improvements
→ 自我审查/Self-review
→ 保存状态/Save state
→ 跨会话继续/Cross-session continue
→ ...持续迭代/Continue iterating
→ 完成目标/Complete goal
→ 输出报告/Output report
示例 2:查看状态
用户/User: /自控 status
## 迭代状态/Iteration Status
**目标/Goal**: 改进所有技能的神似程度
**状态/State**: RUNNING
**进度/Progress**: 4/7 技能完成
**预算/Budget**:
- 时间/Time: 1h 23min / 4h (34%)
- Token: 185k / 500k (37%)
**当前任务/Current**:
- 改进金庸风格 (60%)
**待执行/Pending**:
- 改进古龙风格
- 改进卡尔维诺风格
**已创建快照/Snapshots**: 3
**可回滚/Rollback Available**: ✅
性能与资源管理
内存优化
- 上下文压缩: 超过50轮对话时,压缩前20轮为摘要,仅保留核心决策点
- 规则按需加载: 不预加载所有规则文件,仅在需要时读取对应规则
- 缓存复用: 同一会话中的重复API调用结果缓存10分钟
预算智能分配
- 前期密集: 前30%预算用于深入分析问题、设计方案
- 中期执行: 40%预算用于实施和迭代修改
- 后期验证: 30%预算用于测试验证和文档更新
- 动态调整: 如果某个阶段超时,自动从后续阶段借调预算
并行任务调度
- 独立任务并行: 不互相依赖的任务同时执行
- 依赖任务串行: 有依赖关系的任务按拓扑排序串行
- 资源限制: 同时最多3个并行子任务,避免上下文爆炸
检查点与回滚
- 自动检查点: 每次文件修改前自动创建检查点
- 增量存储: 检查点仅存储变更差异,不复制整个项目
- 回滚成本: 检查点保留最近5个,更早的自动清理
跨会话持久化
- 精简状态: 仅保存任务描述、当前阶段、关键决策树,不保存完整上下文
- 恢复速度: 跨会话恢复时,先加载状态摘要,再按需重建完整上下文
- 会话合并: 如果检测到多个会话处理同一任务,自动合并进度
Rules
- rules/autonomous-loop.md - 自主循环/Autonomous Loop
- rules/memory.md - 跨会话记忆/Cross-Session Memory
- rules/budget.md - 预算控制/Budget Control
- rules/safety.md - 安全机制/Safety Mechanisms
- rules/self-review.md - 自我审查/Self-Review
- rules/anti-aigc.md - 流程文档反AIGC检测规则
配置选项/Configuration
| 参数/Param | 默认值/Default | 说明/Description |
|---|---|---|
| default_budget_time | 2h | 默认时间预算/Default time budget |
| default_budget_token | 200k | 默认 token 预算/Default token budget |
| auto_save_interval | 5min | 自动保存间隔/Auto-save interval |
| max_changes_per_loop | 10 | 每轮最大变更/Max changes per loop |
| require_confirm_risk | high | 需要确认的风险级别/Risk level for confirm |
| enable_rollback | true | 启用回滚/Enable rollback |
Anti-Patterns / 反模式
| 违规 | 严重度 | 后果 |
|---|---|---|
| 不执行自审查 (跳过self-review) | CRITICAL | 迭代方向可能错误 |
| 不保存跨session上下文 | CRITICAL | 重启后失去所有进度 |
| 回滚后不记录回滚原因 | HIGH | 可能在同一点再次失败 |
| 决策树不更新 (使用陈旧决策) | HIGH | 在已变化的条件下做旧决策 |
| 并行任务无协调机制 | HIGH | 任务冲突或资源竞争 |
| 不设置预算上限 | MEDIUM | 无限消耗资源 |
| 暂停后不记录恢复点 | MEDIUM | 恢复时丢失上下文 |
常见问题与排查 / Troubleshooting
跨会话恢复失败 / Cross-session recovery fails
- 症状/Symptom:
/自控 resume无法恢复之前的进度 / Cannot resume previous progress - 解决/Fix: 检查持久化文件是否损坏;使用
/自控 log查看最后保存点;如果状态文件丢失,使用/自控 --rebuild-state从日志重建
决策树爆炸 / Decision tree explosion
- 症状/Symptom: 决策分支过多导致上下文溢出 / Too many decision branches cause context overflow
- 解决/Fix: 使用
--max-depth 3限制决策树深度;定期使用/自控 pause修剪不必要的分支;合并相似分支
自审查循环 / Self-review loop
- 症状/Symptom: 自审查发现的问题反复出现无法根治 / Self-review finds same issues repeatedly
- 解决/Fix: 提升修复策略等级(补丁→重构);记录问题模式到
rules/safety.md;如果3轮后无改善,降低该问题优先级
边界情况 / Edge Cases
- 会话中断恢复: opencode崩溃后,自动从最后检查点恢复,跳过已完成的迭代
- 多实例冲突: 检测到多个自控实例同时运行 → 后者排队等待,前者持有锁
- 预算精确耗尽: 预算耗尽时有未提交的变更 → 自动创建检查点并保存,下次自动恢复
- 回滚链断裂: 检查点文件损坏 → 跳过该检查点,使用上一个有效检查点
- 混合语言记忆: 中英混合的状态数据 → 分别存储,恢复时按语言重建上下文
AIGC检测意识
自控迭代的状态报告和决策日志必须包含具体决策理由和量化状态,不能用"选择了JWT""预算使用合理"等空洞表述。参见 rules/anti-aigc.md 了解流程文档的反AIGC规则。
核心要求:
- 决策记录必须标注具体理由和排除选项:不写"选择了JWT",写"选择JWT而非Session——理由:(1)分布式API不需要Redis共享状态;(2)移动端无Cookie限制。[自决]"
- 状态报告必须用数字:完成11/15任务(73%),不用"大部分完成"
版本历史 / Version History
| 版本 | 日期 | 变更 |
|---|---|---|
| 1.0.0 | 2026-04-01 | 初始版本,自控循环,跨会话记忆,预算管理 |
| 1.1.0 | 2026-05-09 | 添加安全规则,性能管理,排查,边界情况,memory/budget/safety/self-review规则文件 |
相关技能
/修仙from sleepless — 不眠不休执行,适用于扩展的自控会话/强力迭代from power-iterate — 预算管理的迭代框架/修炼from skill-refiner — 将自控改进能力应用于其他技能
Use this skill
Connect your agent to the Skill Store MCP server and ask for it by ref: cycleuser/zi-kong-26579021899.
Details
- Publisher
- cycleuser
- Category
- AI & Agents › Orchestration & multi-agent
- License
- MIT
- Updated
- 2026-10-18
References
More from cycleuser
Academic Guide Search doctoral dissertations for a given academic field, compile professional sites and database…
Academic Writer Academic paper writing assistant for top-tier conferences and journals with AIGC-aware natural vo…
An Jian Security review for skills before installation, detecting dangerous patterns and assessing risk l…
Fill .docx application forms and build bundles in Word and WordCraft Fill merged-cell form templates, write the narrative, embed evidence images, and pack the files.