Skip to content

Preserve admitted Turn settlement across lease renewal - #9257

Merged
loopx-agent merged 1 commit into
mainfrom
codex/refresh-lease-renewal-admission-floor-20261122
Nov 23, 2026
Merged

loopx-agent merged 1 commit into
mainfrom
codex/refresh-lease-renewal-admission-floor-20261122

Conversation

@loopx-agent

@loopx-agent loopx-agent commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

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-turn command 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

  • The production CLI regression exercises File and SQLite. It covers rejection of a pre-admission or future proof, plain refresh after renewal, and a replacement lease held by the same worker. Failed attempts preserve the authority and receipt state. Successful combined settlement and exact replay consume one slot in total.
  • Isolated Core wheels built from base bbd2683836 and head 5d865e1cd4 reproduce the rejection on the baseline and confirm settlement and replay on the candidate through the installed CLI for both providers.
  • The combined-settlement, execution-loss, lease-preparation, and claim-proof suites reported 198 passed and 10 skipped across two runs.
  • The standard diff-selected premerge run completed with one timeout in the CLI output-budget regression. That same check passed in a standalone run pinned to the PR base. All other selected checks passed in the standard run.

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 from 5d865e1cd4 in 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.

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 侧另起炉灶:

  1. 入口与参数透传: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 均保持原语义不变。
  2. 唯一守卫边界收敛: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。
  3. 架构权衡与边界:核心守卫不变量(同世代版本区间校验 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)。

关键代码讲解

  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。
  2. _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 与原生调用共用同一精确世代下界。
  3. 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 或扣额事件。
  4. 既有共用调用方 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

  1. 核心失败路径与负例走查:
    • 未到准入下界(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 隔离通过)。
  2. 发现 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 之后仍锚定原始准入,需要同步扩展 TypeScript QuotaSettlementReadback(settlement_readback.ts)与 Python settlement.py 暴露 original_prepared_receipt,超出当前最小修复范围。可作为安全后续优化单独评估。
  3. 本地验证矩阵(均在隔离临时目录执行,未轮询远程 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.py 0 跳过,其余跳过由 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.

@loopx-agent
loopx-agent merged commit feb4b78 into main Nov 23, 2026
44 of 57 checks passed
@loopx-agent
loopx-agent deleted the codex/refresh-lease-renewal-admission-floor-20261122 branch November 23, 2026 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant