Repository navigation
Preserve admitted Turn settlement across lease renewal - #9257
Conversation
Signed-off-by: LoopX Agent <[email protected]>
loopx-agent
left a comment
There was a problem hiding this comment.
Reviewer: model_agent · Gemini · Google · self_reported
Approval conclusion (author-owned PR; GitHub blocks formal self-approval)
结论概述 / Summary:对精确提交 5d865e1cd4e961b51c5cd4808e700030c8332f50 给出 APPROVE 结论。PR 仅用 4 files, +98/-4(生产代码仅 +9/-1)即消除了硬租约(hard_lease)Turn 在同世代续期后原组合写回命令被永久拒绝的卡死问题,并同步补齐了 turn_run_once 原先缺失的准入下界校验;同时发现 1 个低风险非阻塞 P2 边界(lease_readiness 重入后旧命令兼容),建议作为后续改进跟进。
动机 / Motivation
在启用硬租约(hard_lease)的 Goal 上,当 Agent 或宿主运行时已经通过 quota should-run 获得带租约版本号(如 v2)的组合写回命令 refresh-state --settle-turn 后,若执行过程中触发了同一租约世代内的续期(同 idempotency_key、同 lease_epoch,版本号升至 v3),写回时仍持有初始准入命令行或初始证明的调用方与运维人员会受到影响。
改动前(base bbd2683):当 Turn 在 v2 准入后同世代续期到 v3,宿主或 Agent 执行准入阶段生成的 refresh-state --settle-turn ... --task-lease-expected-version 2 会被 guard_first_writeback_execution 以 refresh_task_lease_unavailable(exit 1)直接拒绝,且没有任何结果或扣额回执写入,单纯重试同一命令永远无法自愈;与此同时,已启用 allow_same_generation_lease_renewal=True 的原生调用路径只检查 expected_version <= current_version,未约束 >= admitted_version,在隔离探针中意外放行了准入前旧版本 v1。改动后(head 5d865e1):CLI 组合写回路径在满足 --settle-turn、非空 task_lease_proof、--todo-id 与 --turn-instance-id 时开启同一世代续期兼容,并在守卫中严格校验同 key、同 epoch 且 current_version >= submitted_version >= admitted_version(含两端),使原 v2 命令在 File 与 SQLite 下均可完成首轮写回与扣额结算并支持幂等重放,同时拒绝 v1、v4、已过期租约、已替换 epoch=2 租约以及未带 --settle-turn 的普通 refresh-state。
经过验证的可观测改进:CLI refresh-state --settle-turn 在同世代续期后(无论 Todo 仍为 open、已通过同一 claim_operation_id 完成、还是无扣额 blocked 收尾)均返回 ok=true、settlement_progress.state=settled,精确重放返回 idempotent_replay=true 且总扣额计数保持 1;普通 refresh-state(无 --settle-turn)、准入前版本 v1、超前版本 v4、过期租约以及跨 epoch 替换租约全部继续以 refresh_task_lease_unavailable 失败封闭。尚存的非阻塞边界(P2 follow-up):若续期后再次调用 quota should-run --turn-instance-id 重入并追加了 lease_readiness(v3),prepared_lease_version 会前移到 3;重入本身返回的新命令(v3)可正常结算,但若仍拿旧命令(v2)提交会被拒绝。
不做的范围 / Non-goals:本 PR 不允许跨租约世代/跨所有者恢复(新 lease_epoch 或新 idempotency_key 仍须走 execution-loss-closeout 或重新准入),不放宽未携带 --settle-turn 的普通 refresh-state 的当前版本要求,不修改 TypeScript settlement_readback 契约以暴露 original_prepared_receipt,也不改变配额扣额或 Todo 完成语义。
改动思路 / Approach
本次改动沿着现有控制面职责边界复用既有守卫,而非在 CLI 侧另起炉灶:
- 入口与参数透传:
handle_refresh_state_command(loopx/cli_commands/project_lifecycle_refresh_state.py)仅在组合结算场景(--settle-turn为真、task_lease_proof非空,且同时具备--todo-id与--turn-instance-id)下向refresh_state_run传入allow_same_generation_lease_renewal=True。普通refresh-state(未传--settle-turn)以及提前返回分支--execution-loss-closeout均保持原语义不变。 - 唯一守卫边界收敛:
state_refresh.py:refresh_state_run将标记透传给guard_first_writeback_execution与_hard_lease_execution_bound(loopx/control_plane/quota/execution_preparation.py)。execution_preparation.py在放行前强制同时校验:lease_epoch is not None(旧无 epoch 回执不可晋级)、current_proof["idempotency_key"] == requested_proof["idempotency_key"]、严格整数类型校验(type(admitted_version) is int,排除bool与None),以及闭合区间current_proof["expected_version"] >= requested_proof["expected_version"] >= admitted_version。满足后再升格为current_proof并经_clamp_admitted_lease_version与prepared_lease比对key_sha256、version与epoch。 - 架构权衡与边界:核心守卫不变量(同世代版本区间校验 current_version >= submitted_version >= admitted_version 且同 key/同 epoch)保留在唯一真实裁决边界 _hard_lease_execution_bound 中;CLI 适配器 project_lifecycle_refresh_state.py 只按调用语义转发布尔标记 allow_same_generation_lease_renewal;持久化读取仍复用既有 _read_execution_snapshot 与 TypeScript settlement_readback,未引入新的存储写路径或平行决策器。仅修改 CLI 参数转发(+6)、Python 硬租约区间下界检查(+3/-1)、TurnEnvelope EN/ZH 协议说明(+8/-2)及对应 File/SQLite CLI 回归测试(+81/-1),共 +98/-4;保留“lease_readiness 重入前移后仍提交过期旧 v2 命令”作为可安全延后的 P2 follow-up,不把 TS 读回结构扩展强行拉入本切片。
下表汇总在基线 bbd2683836 与精确待审头 5d865e1cd4 上通过真实 CLI 与直接守卫探针(File 与 SQLite 双后端完全一致)实测的分支对照(基于源码路径验证,非模糊猜测):
| 场景与提交证明 | 基线 bbd2683836 |
待审头 5d865e1cd4 |
判定依据 |
|---|---|---|---|
v2 准入 -> 续期 v3 -> CLI --settle-turn 提交 v2 |
FAIL (refresh_task_lease_unavailable) |
PASS (settled,重放 idempotent_replay=true,总扣额 1) |
目标修复闭环,File & SQLite 一致 |
v2 准入 -> 续期 v3 -> 提交准入前 v1 (CLI) |
FAIL (refresh_task_lease_unavailable) |
FAIL (refresh_task_lease_unavailable,无副作用) |
下界 >= admitted_version 阻断 |
v2 准入 -> 续期 v3 -> 原生 turn_run_once 路径提交 v1 |
ALLOW (基线缺陷) | FAIL (RefreshTaskLeaseUnavailable) |
补齐原生路径缺失的下界校验 |
v2 准入 -> 续期 v3 -> 普通 refresh-state(无 --settle-turn)提交 v2 |
FAIL (refresh_task_lease_unavailable) |
FAIL (refresh_task_lease_unavailable) |
default-off 行为严格隔离 |
v2 准入 -> 续期 v3 -> 租约过期 (TTL=1 后) 提交 v2 |
FAIL (refresh_task_lease_unavailable) |
FAIL (refresh_task_lease_unavailable) |
过期租约 current_proof=None,失败封闭 |
v2 准入 -> release 后重领 (epoch=2) -> 提交旧 v2 |
FAIL (refresh_task_lease_unavailable) |
FAIL (refresh_task_lease_unavailable) |
跨世代 key/epoch 不匹配,失败封闭 |
v2 准入 -> 续期 v3 -> 重入 quota should-run 发新 v3 命令 |
PASS (settled) |
PASS (settled) |
重入后的最新 settlement_plan 正常结算 |
v2 准入 -> 续期 v3 -> 重入发 lease_readiness(v3) 后再交旧 v2 |
CLI: FAIL / 原生 guard: ALLOW | CLI & 原生 guard: FAIL (refresh_task_lease_unavailable) |
P2 边界:admitted_version 取自 prepared_lease(详见 Risks) |
具体改动 / Changes
全部变更集中在 4 个文件(+98/-4):生产代码 loopx/cli_commands/project_lifecycle_refresh_state.py(+6/-0)与 loopx/control_plane/quota/execution_preparation.py(+3/-1);协议文档 docs/reference/protocols/turn-envelope-v0.md(+8/-2);测试 tests/control_plane/test_execution_loss_closeout_cli.py(+81/-1)。
关键代码讲解
handle_refresh_state_command(loopx/cli_commands/project_lifecycle_refresh_state.py:581-586):在组装refresh_state_run参数时新增布尔表达allow_same_generation_lease_renewal=(getattr(args, "settle_turn", False) and task_lease_proof is not None and bool(getattr(args, "todo_id", None)) and bool(getattr(args, "turn_instance_id", None)))。这一判断把同世代续期放行严格限定为带完整 Todo/Turn/租约证明的组合结算请求,不影响未带--settle-turn的普通刷新、非硬租约路径或早退的--execution-loss-closeout。_hard_lease_execution_bound(loopx/control_plane/quota/execution_preparation.py:403-422):从settlement.prepared_lease读取admitted_version,并在续期兼容分支增加type(admitted_version) is int与下界比较current_proof["expected_version"] >= requested_proof["expected_version"] >= admitted_version。此前只检查上限>=导致同世代更早版本(如准入前的v1)在原生路径下也可通过;加了闭合区间与type(...) is int(天然排除bool)后,CLI 与原生调用共用同一精确世代下界。guard_first_writeback_execution(loopx/control_plane/quota/execution_preparation.py:456-530):保持交叉维护锁、snapshot.get("handoff_mode") == "hard_lease"检查以及todo完成态回执验证结构不变,直接委托_hard_lease_execution_bound裁决,失败统一抛出RefreshTaskLeaseUnavailable(refresh_task_lease_unavailable),确保校验失败时不写入任何 Run、Todo 或扣额事件。- 既有共用调用方
refresh_candidate(loopx/cli_commands/turn_run_once.py:322-360):作为未修改的兄弟生产调用方,原本已在 line 358 传入allow_same_generation_lease_renewal=refresh_task_lease_proof is not None。本次修复相当于把原生调用方与 CLI--settle-turn对齐到同一条带有准入版本下界的契约上。
规范基准与逐项映射
以合并基线提交 bbd26838366cf03b371f5a2308833e03d1e1229b 处的公开协议文档 docs/reference/protocols/turn-envelope-v0.md 为独立规范基线(即在本 PR 对其第 332-336 行英文与第 373-375 行中文同步增补之前),其对 refresh-state --settle-turn 的既有核心要求映射如下:
TE-SETTLE-COMBINED(已实现 /implemented):refresh-state --settle-turn必须在写回通过后按原 Goal、Agent、Todo 与 Turn 身份执行待结算命令并核验回执;现通过project_lifecycle_refresh_state.py:handle_refresh_state_command与execution_preparation.py:_hard_lease_execution_bound在同世代续期后继续保持可执行,已由tests/control_plane/test_execution_loss_closeout_cli.py:test_combined_writeback_keeps_the_admitted_lease_generation与 File/SQLite 真实 CLI 探针验证。TE-SETTLE-RETRY-NO-DOUBLE-DEBIT(已实现 /implemented):精确重试不可重复扣额;已由test_combined_writeback_keeps_the_admitted_lease_generation与open_admitted_v2_replay验证总扣额保持为1(idempotent_replay=true)。TE-PLAIN-REFRESH-PARITY(已实现 /implemented):未传入--settle-turn的普通refresh-state必须保留原有行为(继续要求当前最新版本);已由open_admitted_v2_plain探针与测试中combined=False拒绝用例验证。- 对本 PR 同步修订
docs/reference/protocols/turn-envelope-v0.md(EN L332-336 + ZH L373-375)的独立评估:原基线文本明确了普通refresh不受影响,但未显式规定组合写回在同世代续期时允许的版本下界。本 PR 同步补齐中英文协议描述("介于准入版本和当前版本之间(含两端),租约仍须与原准入匹配"),使文档与双调用方运行时行为一致,未借改文档来放水或削弱任何既有安全约束。
对主干的风险 / Risks
- 核心失败路径与负例走查:
- 未到准入下界(
v1)、超前版本(v4)、普通refresh-state(无--settle-turn)、跨lease_epoch/ 新idempotency_key替换租约、以及开启状态下TTL=1到期失活的租约,全部经真实 File 与 SQLite CLI 探针及单测验证在写回与扣额前拒绝(appended=false、spend_run_count=0)。 - 未引入新枚举/字面量词汇,未引入新持久化字段,协议/报错文本完全保持领域中立(
domain_neutrality),普通模式default-off行为等价(default_off_isolation隔离通过)。
- 未到准入下界(
- 发现 1 项非阻塞 P2 边界(
lease_readiness重入后提交旧命令):- 触发条件与链路:若 Turn 在
v2准入后先同世代续期到v3,随后在首次写回前再次调用quota should-run --turn-instance-id <T>(settlement_cli.py:179->heartbeat_receipt_identity.ts:heartbeatWorkRequalification),系统会追加settlement_receipt_revision="lease_readiness"记录并把prepared_lease_version前移到3,同时在该次调用返回的dec2计划中已生成--task-lease-expected-version 3。由于execution_preparation.py:403读取的是settlement.prepared_lease.prepared_lease_version(来自settlement_readback.ts:effectiveHeartbeatReceipt)而非初始准入事件original_prepared_receipt(heartbeatValidationEvent),此时若调用方忽略dec2的v3命令而继续提交最初dec1的v2命令(或未带--resume-turn-key的活跃turn_run_once并发遭遇外部重入追加lease_readiness),3 >= 2 >= 3将变为False并以refresh_task_lease_unavailable拒绝。 - 为何定位为 P2 Follow-up 而非阻断项:(a) 触发前提是已经重新进入
quota should-run --turn-instance-id <T>,而该重入本身直接给出可用的v3命令(已实测 PASS);turn_run_once --resume-turn-key也会通过rebind_resumed_task_lease把本地 proof 与lease_readiness一并更新到v3。(b) 基线之所以在该场景对原生守卫放行v2,仅仅是因为基线完全没有下界校验(同时也会误放行v1)。(c) 要让admitted_version在lease_readiness之后仍锚定原始准入,需要同步扩展 TypeScriptQuotaSettlementReadback(settlement_readback.ts)与 Pythonsettlement.py暴露original_prepared_receipt,超出当前最小修复范围。可作为安全后续优化单独评估。
- 触发条件与链路:若 Turn 在
- 本地验证矩阵(均在隔离临时目录执行,未轮询远程 GitHub Actions CI):
tests/control_plane/test_execution_loss_closeout_cli.py:PASS(12 passed)tests/control_plane/test_quota_settlement_cli.py:PASS(222 passed)- 组合套
test_execution_loss_closeout_cli.py+test_quota_execution_preparation.py+test_refresh_settle_turn.py:PASS(170 passed, 10 skipped;其中test_execution_loss_closeout_cli.py0 跳过,其余跳过由 Windows/主机条件环境门触发) tests/test_loopx_turn_driver.py -k lease:PASS(18 passed, 416 deselected)- TypeScript 控制面套(
native_child_admission.test.ts、quota_settlement_readback.test.ts、quota_settlement_plan.test.ts、execution_loss_closeout.test.ts):PASS(216 pass, 0 fail) - 静态与规范检查:
ruff check(涉及 3 个 Python 文件 PASS)、mypy全仓配置文件(PASS,17 source files零错误)及mypy --follow-imports=silent loopx/control_plane/quota/execution_preparation.py(PASS)、scripts/check_rfc_bilingual.py --base origin/main --head HEAD(PASS)、loopx check --scan-path loopx --scan-path docs(PASS,errors=0、warnings=22均为既有非字面量告警)。 loopx canary premerge --from-git-diff --git-diff-base origin/main(标准档,挂载仓库node_modules以支持 TS 扫描):17 项受选检查 PASS(6 项直接检查、semantic-vocabulary-drift-smoke.py等 8 项目录 canary、8 项风险档 smoke、4 文件公私边界零命中);注:change_quality_qualification状态为receipt_missing(合并前独立的 CQR 门控,不阻塞代码评审结论)。- UNVERIFIED:未对独立运行的真实 PostgreSQL 服务器实例执行可选存储后端测试;远程 GitHub Actions CI 未查阅(仓库策略
wait_for_ci: false)。
我的整体评价 / Verdict
- 长程与用户体验影响(
outcome_impact):long_horizon为improved——消除了同世代租约续期后组合写回与扣额死锁且保持重放幂等与单次扣额不变量;user_experience为improved——原本由系统自动生成并在提示中下发的refresh-state --settle-turn命令不再因后台保活续期而失效,且普通refresh-state行为不变。visual_review为not_applicable(本次改动仅涉及 CLI 参数接线(loopx/cli_commands/project_lifecycle_refresh_state.py)、Python 配额准入守卫(loopx/control_plane/quota/execution_preparation.py)、协议文档(docs/reference/protocols/turn-envelope-v0.md)与 CLI 测试(tests/control_plane/test_execution_loss_closeout_cli.py),未改动任何前端、看板或渲染 UI。,未验证视觉范围:none)。 - 代码体量、相称性与语义对齐:
code_volume判为necessary,compatibility_assessment判为follow_up(后续若需在lease_readiness重入后仍兼容过期dec1证明,再单独评估暴露original_prepared_receipt)。change_proportionality判为proportionate,architecture_assessment判为retain:核心守卫不变量(同世代版本区间校验 current_version >= submitted_version >= admitted_version 且同 key/同 epoch)保留在唯一真实裁决边界 _hard_lease_execution_bound 中;CLI 适配器 project_lifecycle_refresh_state.py 只按调用语义转发布尔标记 allow_same_generation_lease_renewal;持久化读取仍复用既有 _read_execution_snapshot 与 TypeScript settlement_readback,未引入新的存储写路径或平行决策器。仅修改 CLI 参数转发(+6)、Python 硬租约区间下界检查(+3/-1)、TurnEnvelope EN/ZH 协议说明(+8/-2)及对应 File/SQLite CLI 回归测试(+81/-1),共 +98/-4;保留“lease_readiness 重入前移后仍提交过期旧 v2 命令”作为可安全延后的 P2 follow-up,不把 TS 读回结构扩展强行拉入本切片。observable_semantics为intentional_change_validated、default_off_isolation为isolated、authority_semantics为aligned。semantic_alignment为aligned(reuse_existing):本 PR 直接复用既有prepared_lease与allow_same_generation_lease_renewal语义并同步更新docs/reference/protocols/turn-envelope-v0.md,未新增或分裂状态词表,且semantic-vocabulary-drift-smoke.py与check_rfc_bilingual.py均在精确提交5d865e1cd4e961b51c5cd4808e700030c8332f50通过验证。 - 精确提交与合并门控说明:在复核远端状态仍为
OPEN且精确头未变(5d865e1cd4e961b51c5cd4808e700030c8332f50)后发表本结论。请仓库维护者在最终合入前按目标规范补齐精确提交的 CQR 门控(change_quality_qualification)及所需维护者授权。
English verdict: APPROVE — bounded fix preserves admitted combined Turn settlement across same-generation lease renewals and closes the pre-admission lower-bound gap.
Problem
A Turn can start with a valid hard-lease proof and then renew the same lease before first writeback. The generated combined settlement command still presents the admitted proof. Before this change the CLI requires the current version and rejects that command before writing any result or settlement receipt. Retrying the same command cannot recover by itself.
Change
The original explicit
refresh-state --settle-turncommand can survive a same-generation lease renewal. The existing writeback guard requires the original key and epoch and a submitted version between the admitted version and the current version, inclusive. This also gives the existing native caller the same lower bound. Plain refresh keeps its current-proof requirement. The guard still uses the original Turn identity.Validation
bbd2683836and head5d865e1cd4reproduce the rejection on the baseline and confirm settlement and replay on the candidate through the installed CLI for both providers.Upgrade and rollback
On both File and SQLite, the official 2.4.0 runtime (
a6ffb3be6e) admitted a Turn and rejected its original combined command after a same-generation lease renewal. The real local installer then switched both CLI launchers to a build from5d865e1cd4in isolated roots. That build preserved the original admission and lease, rejected a pre-admission proof without effects, and settled the original command without a second guard. Replay kept total quota consumption at one. The official runtime, restored with the normal rollback command, recognized the same Turn as settled.