AnyBot/docs/restructure/notes/b02k-checkpoint-oplog-follo...

35 lines
4.7 KiB
Markdown
Raw Permalink Normal View History

# b02k — 持久三檔:`checkpoint.rs`(270) /rewind + `oplog.rs`(821) 崩溃恢复 + `followup.rs`(474) 追進度
## checkpoint.rs — M1 /rewind(编辑前存档,可复原)
- 常量:KEEP_DAYS=7、MAX_FILE_BYTES=2_000_000。
- Entry{at,turn,computer,place,path,before,too_big};Files=HashMap<path,Vec<Entry>>(按 at 升序)。
- 规则:只 edit_file 的档;只本机;不存命令、不存别的装置档;>2MB 不存(too_big 事件);档案不存在时存档=记「不存在」(复原=把该档删除)。
- **remember(bot,dir,turn,computer,place,path,now)**:log_lock(bot.id)(lock.rs)下:档案读 bytes→store.blob_put(sha256 命名 .bin)→Entry 追加;剪 >KEEP_DAYS;每档留 50 条;files 写 per-bot JSON。
- **load(bot.dir)**:JSON 读回(坏档→空)。
- **restore_one(files,store,dir,path,now,keep)**→Restore{undone,nothing,unreadable,missing,unchanged}:取最新未用 at<now-keep 的条目;bytes 读不回→unreadable;档案现况==before→unchanged;写回→undone(条目标记 used→写 files)。
- **restore_turn**:复原该轮所有档(逆序);只 edit_file 路径;缺→missing;全 unchanged→nothing。
- **list**→CheckpointFile{path,turn,at,computer,size,too_big,undone,restorable}。
- 命令词:/rewind 单独→提示卡;「列出所有存檔點」→list 卡;「這輪之前/回這輪之前/回最近…→复原(复原卡→「已復原 N 個檔案」/「檔案內容跟当时一样」/「复原不了:读不回/这轮没编辑过」)。
- 测试锁:remembering_a_new_file_undoes_to_deleted、too_big_is_recorded_not_saved、restore_turn_restores_every_file_of_that_turn_in_reverse、only_edit_file 等。
## oplog.rs — 每 bot 一份操作日志(pi durable-harness 设计,裁剪到 AnyBot;运行中无人读,只崩溃重建)
- 常量:TRUNC=640、BUDGET=32*1024(写前预算)、CUT=1024、MAX_RECORDS=2000、HEAD_KEEP=1024、TAIL_KEEP=8192。
- Record{turn,model_call,tool_start,tool_end,typed_message,prompt}(各自 {at,turn_id,role,text,tool_id,name,args,status,result,cwd,...});truncate;trim(头尾留,中段丢)。
- **Log{path,head,tail,dropped,closed,failed,fell_over,disk,turn_id,started_at_ms,model_call_id,tool_ids}**:open(per bot dir oplog.jsonl);load(读回+清旧 crash turn+重建 turn 计数);begin_turn(prompt 记录;turn_id=ms);note_model_call;tool_begin/tool_finish;note_typing(使用者消息);append(预算内写盘,超→dropped/trim)。
- fell_over():turn_start 无 turn 结束→崩溃轮(recover 判 interrupted:有未结束工具/未应答卡片→要接)。
- **recover**:重建崩溃轮现场(head 消息+tail 结果)→Recovered(turn_id、messages、unresolved tool ids、unanswered card 文本)。ui 决定接不接(agent.rs resume_turn 用)。
- Replay{Safe,Never};plan{turn,started_at_ms,tool_ids,model_call_id,last_model_request}→agent.rs 重放锁。
- 测试锁:records_survive_a_restart、crash 检测、budget、recover 各形态。
## followup.rs — 追進度(无人值守的进度大脑,非 LLM,定时轮询)
- 常量:TOOL=follow_up、MIN_EVERY_SECS=15、DEFAULT=60、MAX=3600、DEFAULT_GIVE_UP_MINS=180、MAX=1440、REPORT_EVERY_MS=10min、ERRORS_IN_A_ROW=3、MAX_PER_BOT=8、OUTPUT_KEEP=1500、MAXWHAT=120。
- FollowUp{id,bot,what,every,give_up_after,started,last_seen,last_output,running,sent,errors,fell_over,done,failed,given_up,progress,stopped};Verdict{Done,NotYet,Failed,Stuck,Running};Judge{verdict,reason,output};State(进度心跳)。
- **judge(f,g,now,running)**:done+exit0→Done;done+exit≠0→Failed;give_up→GivenUp…;running 心跳超时→Stuck;watch 命令退出码→…
- **tick**:全 bot 的 follow_ups 扫一遍(1/sec 调度):running 命令查活;到 every→检查(shell 于 bot 沙箱跑 check 命令);进度心跳(每 REPORT_EVERY_MS 回报给 ui);连续 ERRORS_IN_A_ROW→fell_over。
- wake 文(wake_text):「[追進度 #N」「what」…(完成了/失败了/卡住了/放弃了)+最后输出截」;done/failed/stuck/given_up→叫醒 bot(agent.rs 起轮);sent 标记只叫一次。
- **spec/started_text/describe**:follow_up 工具({what,check,every,give_up_after_minutes});已登记 #N 文;list/status 命令文(/follow-ups…)。
- 测试锁:judge 各态、wake 文与 tasks.rs continues 互锁、give_up、MAX_PER_BOT。
## 迁移笔记
checkpoint 进 core::memory(store.rs 同域;blob_put 依赖 store)。oplog 进 core::agent(崩溃恢复与 agent.rs resume/replay 同域)。followup 进 core::schedule::followup(brain tick 在 app;wake 交 agent)。三档的 ui 面(卡/命令)留 app。