Repository navigation
feat(evolution): RSI P0 self-improvement loop, token budgets, sleep backoff, and isolated command-gate replay - #1467
Open
newexploreor wants to merge 2 commits into
Open
newexploreor wants to merge 2 commits into
newexploreor wants to merge 2 commits into
Conversation
added 2 commits
November 27, 2026 00:50
…refine loop (RSI P0)
…hared-key guard, and network-isolated command gates
Robert0622
reviewed
Nov 26, 2026
Robert0622
left a comment
Collaborator
There was a problem hiding this comment.
Review:RSI P0 自进化闭环、Token 预算、休眠退避与命令门禁回放(#1467)
结论:先不 Approve。 主链路在真实部署里能跑通,但有 1 个 P1 要在合入前修:在项目设置里保存一次,RSI Token 预算就会被清空或改回旧值。另有几条 P2 建议一起处理。
- 评审的 head:
a2777adc5c662e7d041962d2ffa6cf01f00a097a(basemain@be6918d7,无冲突) - 实测方式:Go 1.26.11 本机编译 PR,用全新的 MySQL 8.4.7 库部署。执行器是 CodeBuddy CLI 2.187.0,模型
deepseek-v4.1-flash,真实跑业务运行、改进器、回放和反思修正。页面用 Chromium(Playwright)操作。测试项目都是本机新建的,没有绑定任何仓库。 - CI:
core第一次是跑满 30 分钟超时被取消的,各测试步骤本身都通过了。重跑后 16/16 全部通过。
实测结果(PR 实例,全部真实运行)
| 场景 | 结果 |
|---|---|
| 单节点 SOP「会议纪要摘要」先跑 3 次真实运行(每次约 14–17 万 tokens),再开启策略(batchSize 5,P1 = 省 Token) | ✅ 立刻自动触发批次优化(auto-batch triggered … (5/5 runs)),用的是策略保存人的个人 Key |
| 提案回放与目标门禁 | ✅ 回放 6 条(诊断 2、留出 2、回归 2)。结论 通过:修好 3,回归 0,持平 3;P1 省 Token:平均 Token 152104 → 87135(-42.7%),goalMet=true |
| 预算计量 | ✅ 概览里 dailyUsedTokens 随回放增长:542,712 → 1,079,174 → 1,599,815,与回放 run 的 usage 之和一致 |
| 预算超限(日上限调到 1,000,000) | ✅ 自动批次和 SOP 检测都跳过(skipped: 项目今日 RSI Token 预算已用尽(1599815 / 1000000))。页面顶部出现「已暂停后台自动进化」横幅,预算按钮显示「预算已达上限」 |
| 预算接口校验与权限 | ✅ 负数返回 400;月上限小于日上限返回 400;viewer 返回 403;非成员返回 403;项目不存在返回 404;普通 member 可以修改(见 P2-5) |
| 连续 3 次无收益 → 休眠 | ✅ 人工打回 3 次后 consecutiveNoGain 依次为 1 → 2 → 3,状态变为 sleeping,sleepingUntil 为 +1 天。详情页出现「重新开启自动进化」,点击后回到 monitor,计数清零 |
| 手动优化唤醒 | ✅ 处于休眠的 SOP 手动点优化后,自动唤醒并清零计数 |
| 自动任务不使用平台共享 Key | ✅ 策略保存人 bob(member,没有个人 Key)在只配了平台共享 Key 时,自动批次被跳过。设置 AGENTFLOW_EVOLUTION_AUTO_ALLOW_SHARED_KEY=true 重启后,同一场景会触发 |
| 命令门禁回放校验 | ✅ 角色带 mode: command 门禁时,native、未断网的 Docker、断网但特权的 Docker 三种沙箱都在调用改进器前被拒(进化回放仅允许在网络隔离的 Docker 沙箱…)。networkDisabled: true 的非特权 Docker 能通过校验 |
| 自动反思修正 | ✅ 回放未通过后自动做了 1 轮反思修正,refineHistory 记了 round 1,复验仍未通过后提案结束为 failed。整个过程没有循环 |
页面 pageerror |
0 |
单测:在 go vet 和 go test 下跑了 ./internal/evolution/... ./internal/domain/...、httpapi TestEvolution|TestSOPDetection、engine Evolution、repo Evolution|SOPDetection,全部通过。我分别注释掉了网络隔离校验、P1 token 门槛、共享 Key 开关这三处实现,每次都有对应的新测试失败,说明测试能抓到回退。
P1
P1-1 保存项目设置会清空或回滚 RSI Token 预算
- 位置:
internal/repo/projects.go:213-221。PutProject对SOPDetection保留数据库里已存的值,但对新增的EvolutionBudget直接用请求体里的值。前端buildProjectPayload(web/app/projects.js:677)用的是{...base},base来自页面缓存的项目对象,而PUT /api/evolution/budget不会更新这份缓存。 - 实测(同一个标签页,不刷新):
- 在 RSI 工作台把预算设为 日 200,000 / 月 3,000,000,接口确认已保存。
- 切到
#/projects/rsi2,只改项目说明,点保存,提示「项目设置已保存」。 - 再看
GET /api/evolution/overview,预算变成了dailyTokenLimit: 0, monthlyTokenLimit: 0,也就是不限额。 - 直接用 API
PUT /api/projects且不带evolutionBudget字段,结果同样被清空。
- 影响:预算是这个 PR 新加的成本护栏。项目设置、同步计划、需求页等地方都会保存项目,任何一次保存都可能让预算悄悄失效。
- 建议:像
SOPDetection一样,保存项目时保留已存的stored.EvolutionBudget,由/api/evolution/budget单独负责修改。再补一条「先设预算 →PUT /api/projects→ 预算不变」的测试。
P2
P2-1 配置错误和基础设施失败也算作「无收益」,SOP 会被送去休眠
- 位置:
failProposal(internal/httpapi/evolution_jobs.go:136)对所有失败都调用recordSOPProposalOutcome(p, false, …),包括「样本不可回放」「排队超时」「资产已删除」,这些失败里改进器根本没有被调用。 - 实测:命令门禁 SOP 放在 native 沙箱里,连续 3 次优化都在校验阶段失败,全程没有调用模型。结果 SOP 直接进入
sleeping,提示「休眠中(连续 3 次无收益,预计 11-27 18:26 UTC 恢复)」。 - 建议:只把真正尝试过、但没有收益的结果计入退避:回放或灰度未达标、人工打回。校验失败、凭据缺失、排队超时这类失败不计数,或者单独提示。
P2-2 命令门禁的「网络隔离」可能放过有网络的情况,也可能让回放完全跑不起来
需要说明:下面几条是读代码得出的结论。本机没有 Docker 守护进程,没有用真实容器验证。
- 校验和执行看的不是同一处环境:
workflowSandboxNetworkIsolated(internal/engine/evolution_replay.go:79)读的是平台进程的AGENTFLOW_DOCKER_NETWORK,真正起容器的执行机器读的是自己的环境变量。另外,旧版 Runner 会忽略networkDisabled字段,这里也没有加 Runner 版本门槛(#1323、#1329 给特权和资源限额都加过)。 - 网络可能被换掉:只配了
AGENTFLOW_DOCKER_NETWORK=none时,sandbox_docker.go:938-946在ConversationRoot或RunnerLoginRelay存在的情况下会把网络换成 relay 网络。 - 回放可能根本跑不起来:Agent CLI 是在容器里通过
docker exec执行的(execOnce),--network none之后容器内的 CLI 也连不上模型 API。这样「允许命令门禁回放」的那一种配置,回放节点大概率会失败。 - 建议:回放只认沙箱配置里的
networkDisabled,在执行端再校验一次,校验不过就拒绝执行。同时确认模型流量怎么出容器(例如走本机代理或 socket),并在文档里写清楚。
P2-3 预算只统计回放 run 的 token,手动入口也不受预算限制
- 位置:
EvolutionReplayTokensSince(internal/repo/runs_sop_detection.go:113)只统计purpose = evolution_replay的 run。 - 没有计入的消耗:改进器生成提案、反思修正、SOP 轨迹检测这几次模型调用都没算进去。
- 手动入口:实测预算超限时,手动
POST /sops/{id}/optimize仍会正常发起。手动不拦可以理解,但它的回放 token 会继续占用同一份额度。建议在文档和界面上说明这一点。 - 超额:同一次 sweep 里,多个 SOP 都对着同一个「已用量」做预检,会一起超额。
P2-4 P1 效率门槛可能失效,0 值也被当成缺失
- 位置:
measurementValue(internal/evolution/replay.go:195-207)把Tokens == 0和ToolCalls == 0都当作「没有数据」。 - 影响一:回放把工具调用降到 0 本来是最好的结果,却会被剔除,不进入平均值。
- 影响二:所有样本都没有可用数据时,效率门槛整个跳过,只按正确性判定通过。
- 另一处:
ArmMetricAcc也跳过 0 值,灰度均值会因此偏高。
P2-5 普通 member 可以改预算;唤醒会把后续自动任务的计费人改成自己
- 位置:
putBudget用的是写权限校验,普通 member 就能把预算改成 0(不限额)。预算属于成本控制,建议限制为 Owner、maintainer 或平台管理员。 - 唤醒的副作用:唤醒和手动优化都会把
policy.UpdatedBy改成当前操作人,之后的自动任务就用这个人的个人 Key。建议唤醒时不要改策略的归属人,或者在界面上明确提示。
P3
- 退避计数:休眠到期后
ConsecutiveNoGain没有清零,再失败一次就直接进入下一档(3 天)。发布后再回滚不算一次 strike。 - 时间字段:
sleepingUntil等time.Time字段加omitempty不起作用,接口里一律返回0001-01-01T00:00:00Z,前端要自己判断零值。 - 静默跳过:自动批次因为策略人缺 Key 被跳过时,只在日志里每 40 秒打一行。SOP 列表仍显示「监测中」,用户看不出原因。
- viewer 的按钮:viewer 也能看到「调整预算」「重新开启自动进化」按钮,点了才得到 403。
- 历史记录:
DeleteEvoReplays在反思轮开始前删掉了第 1 轮的回放行,RefineHistory只留了汇总,第 1 轮单条样本的证据就没有了。
与本 PR 无关、但实测中看到的
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.






Summary
Implements the RSI P0 self-improvement effectiveness upgrades and production cost/safety guardrails (zero DB migrations, fully backward compatible):
1. Self-Improvement Core Upgrades
BuildToolTraceSummary): Extracts node-scoped, chronologically normalized tool call hotspots (top tools, exact duplicates, failed calls with errors, slow/heavy outputs, compact step sequence) fromrun_telemetryand feeds them + recent failed/rejected negative experience into the improver prompt.SummarizeWithPreferences: when optimizing an efficiency P1 target (tokens/tools/duration) on already-clean runs (fixable == 0), requires BOTH zero correctness regression AND measurable P1 reduction (≥8% tokens, ≥10% or ≥1 tool call, ≥8% duration).JudgeCanaryWithPreferences: aggregates per-armAvgTokens/AvgTools/AvgDurationonEvoArmStatsand enforces both Beta-Binomial clean-rate guardrails and P1 efficiency gain (≥5% / ≥0.8 tool calls) before passing.refineProposalJob): When replay fails, archives the round inRefineHistoryand automatically runs 1 bounded reflection round with previous draft, failure verdict, diagnosis-only replay metrics & tool traces (holdout/regression details are never leaked — only aggregate counts), then re-validates on the same samples.2. Production Cost & Safety Hardening
PUT /api/evolution/budget): Persisted onProject.EvolutionBudget;RunsRepo.EvolutionReplayTokensSincetracks UTC daily/monthly replay spend; pre-checked beforesweepSOPBatches,sweepSOPDetections, and automatic reflection rounds. Surfaced inGET /api/evolution/overviewand the SOP workbench UI.sleepingstage): After 3 consecutive no-gain optimization outcomes (failed / rejected / canary failed), SOP automatically enterssleepingwith progressive intervals 1d → 3d → 7d → max 14d. Users can reopen anytime viaPOST /api/evolution/sops/{workflowId}/wake, toggling policywake: true/enabled: true, or triggering manual optimization.resolveSOPActorUser,resolveRefineActor, and auto-triggeredstartSOPOptimizationrefuse platformSharedCodeBuddyKeyfallback unless explicitly opted in viaAGENTFLOW_EVOLUTION_AUTO_ALLOW_SHARED_KEY=true.SandboxProfile.NetworkDisabled(enforcing--network nonein Docker runtime).ValidateEvolutionReplaynow rejectsmode: "command"verification gates unless the workflow uses a non-privileged, network-isolated Docker sandbox (networkDisabled: trueorAGENTFLOW_DOCKER_NETWORK=none). Updateddocs/api/evolution.mdanddocs/rsi-workbench.mdto eliminate the prior doc contradiction.Verification
go vet ./internal/domain/... ./internal/repo/... ./internal/runtime/... ./internal/engine/... ./internal/evolution/... ./internal/httpapi/... ./web/...go test ./internal/domain/... ./internal/evolution/... ./internal/runeval/... -count=1go test ./internal/repo -run 'Evolution|SOPDetection' -count=1go test ./internal/engine -run 'Evolution' -count=1go test ./internal/httpapi -run 'TestEvolution|TestSOPDetection' -count=1go test ./web -run 'TestAppBundleFilesAreEmbeddedAndListed|TestAppJSParses|Harness|Evolution|Hui|NoNativeDialogs' -count=1node web/evolution_sop_test.js&node --test webtest/tests/evolution-sop.test.mjs(fullwebtestcoverage: 85.85%,evolution-sop.js: 96.7%)