Skip to content

fix(control-plane,turn-driver): preserve selected-scope resume across epochs and bind physical native replay calls - #8942

Open
loopx-agent wants to merge 2 commits into
mainfrom
codex/c1-standalone-core-20261118
Open

loopx-agent wants to merge 2 commits into
mainfrom
codex/c1-standalone-core-20261118

Conversation

@loopx-agent

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

Copy link
Copy Markdown
Collaborator

Standalone C1 core slice extracted directly against main (0514512ee7). Refs #8266.

Goal & Root-Cause Context

Mega-PR #8266 (301 files, +27,305 / -4,938 LOC across 537 commits) accumulated ledger churn (+9,081 net lines in docs/architecture/), standalone test permutations (+8,640 net lines), and parallel v0/v1 compatibility glue across CLI adapters. Rather than continuing to stack onto conflicting #8266 (stacked PR #8882 closed as superseded), this PR extracts one self-contained, single-owner C1 native replay & epoch-continuity slice (5 files, +599 / -171 LOC, 0 ledger edits, conflict-free MERGEABLE against main and active PRs):

  1. Selected-scope resume across epoch rotation (loopx/control_plane/quota/native_context_lifecycle.ts, +25 / -13):
    When SessionStart(source="resume") observes a failed full-capture replay (candidate.native_resume invalidation), rotate the capture file epoch via invalidate(), while advancing read_scope and preserving consumed_pointers, confirmed_read_sections, and selected_resume when independently verified selected_resume sections remain current in the rotated epoch. Clear them if omitted or contradicted.
  2. Turn-scoped physical replay binding, multi-page selected coverage, and deduplicated IO helpers (loopx/control_plane/turn_driver/codex_context_history.py, +278 / -127; codex_context.py, +4 / -3):
    • Replace tail string-scan heuristics (_reader_start + Counter) with physical Turn-bounded reader anchoring (_reader_tail, _turn_boundary, _native_reader) verifying CommandExecution completion/turn identity and unique open Code Mode custom_tool_call (exec) parents without scanning older history prefixes.
    • Support multi-page & out-of-order selected-read coverage keyed by verified view digest (_covers_bytes, selections), observe_completed_native_reread (_MAX_COMPLETE_REREADS = 32), and shared helper consolidation (_REPLAY_ERRORS, _read_receipt, _matching_resume_session, _verified_replay_capture, _same_replay_source).
  3. Standalone verification coverage (tests/control_plane_ts/context_delivery.test.ts, +35 / -1; tests/control_plane/test_codex_context_resume.py, +257 / -27):
    • Add direct TypeScript unit coverage for updateNativeContextLifecycle epoch-rotated selected_resume retention/clearing.
    • Cover direct, batched, multi-page, out-of-order, missing-hook/lost-page, cross-turn reread, unobserved duplicate copy, and repeated-resume epoch survival scenarios in test_codex_context_resume.py.

Verification (run against committed 0ba725fef26d1f8fa3c5f2596f0d5245388da256)

  • uv run --extra test python -m ruff check loopx/control_plane/turn_driver/codex_context_history.py loopx/control_plane/turn_driver/codex_context.py tests/control_plane/test_codex_context_resume.py -> All checks passed!
  • uv run --extra test python -m mypy --follow-imports=silent loopx/control_plane/turn_driver/codex_context_history.py -> Success: no issues found in 1 source file; uv run --extra test python -m mypy -> Success: no issues found in 17 source files
  • npm run typecheck:control-plane (tsc --noEmit) -> exit 0
  • node --no-warnings --experimental-sqlite --experimental-strip-types --test tests/control_plane_ts/context_*.test.ts tests/control_plane_ts/progressive_context.test.ts tests/control_plane_ts/turn*.test.ts -> 343 passed, 0 failed, 0 skipped
  • uv run --extra test python -m pytest -n auto -q --no-cov -rs tests/control_plane/test_codex_context_resume.py -> 145 passed in 10.49s (0 skipped)
  • uv run --extra test python -m pytest -n auto -q --no-cov tests/control_plane/test_codex_context_resume.py tests/control_plane/test_codex_context_lifecycle.py tests/control_plane/test_context_delivery_cli.py tests/control_plane/test_turn_host_context_delivery.py tests/control_plane/test_codex_context_boundary.py tests/architecture/test_posix_flag_portability.py tests/architecture/test_content_digest_single_owner.py tests/architecture/test_semantic_vocabulary_drift.py tests/architecture/test_rfc_status_index.py -> 799 passed, 24 skipped in 147.16s
  • uv run --extra test python scripts/check_rfc_bilingual.py --base origin/main --head HEAD -> RFC bilingual diff check passed.
  • uv run --extra test python examples/control_plane/control-plane-maintainability-ratchet-smoke.py -> control-plane-maintainability-ratchet: ok (unreviewed: 0, magnitude_regressions: 0)
  • Isolated HOME uv run --extra test loopx check --scan-path <all 5 changed paths> -> ok: True, errors=0, public boundary scan clean: 5 files
  • Isolated HOME uv run --extra test loopx canary premerge --from-git-diff --git-diff-base origin/main -> status: passed, merge_gate_passed: true (direct_checks: 6/6, catalog_canaries: 5/5 failures=0, risk_profile_smokes: 8/8 failures=0)

Rebase onto origin/main (648d06db54) & [P2] Streaming Scan Fix (dca761a9e5)

  • Rebased conflict-free onto origin/main 648d06db54 (includes #8881, #8956, #9000, #9004; fixes stale base lint error).
  • Resolved gpt-6.2-sol [P2] review on _native_reader: replaced full-line multi-MiB buffering in _reader_tail with O(64 KiB) _reader_spans + _JsonSliceCursor / _scan_turn_prefix_record using bounded {1,64} regex span scanning (_JSON_STR_SPAN), streaming oversized (>16 MiB) earlier same-turn records (background_event / closed custom_tool_call_output) to _turn_boundary (task_started) at < 2 MiB peak memory (147 KiB micro-benchmark peak on 17 MiB escaped JSON) while still rejecting active >16 MiB reads, malformed JSON/control chars, and duplicate concurrently open Code Mode exec parents.
  • Exact-head dca761a9e5 Verification:
    • tests/control_plane_ts/context_delivery.test.ts + quota_settlement_plan.test.ts: 49 passed, 0 failed (tsc --noEmit exit 0)
    • tests/control_plane/test_codex_context_resume.py -k large_earlier_record: 14 passed, 145 deselected in 16.07s
    • Core Python suite (test_codex_context_resume.py, test_codex_context_lifecycle.py, test_top_level_module_budget.py, test_goal_mode_mcp_settlement.py): 344 passed in 88.17s (0 failed, 0 skipped)
    • 10-suite C1 sweep: 985 passed, 14 skipped, 4 failed (the 4 failures at test_context_first_read.py:874 and test_saved_context_project.py:711 reproduce identically on unmodified origin/main@648d06db54)
    • uv run --extra test ruff check ..., mypy --strict --follow-imports=silent, uv run --extra test mypy (17 files): 0 issues
    • uv run --extra test loopx canary premerge --tier quick --from-git-diff --git-diff-base origin/main: ok=true (6/6 direct, 3/3 selected canaries failures=0)
    • uv run --extra test loopx check --scan-path ...: ok=true (errors=0, public boundary clean across 5 files)
    • Isolated SQLite + real subprocess CLI host matrix (quota should-run, context-read, context-hook, refresh-state, spend-slot): verified 17 MiB same-turn Code Mode selected_resume, unchanged-SHA previously_delivered_fields hit, changed-SHA safety, PreCompact recovery with 0 extra admissions and single debit (1 -> 2), duplicate spend-slot blocked, and wrong-model / missing-page (2/3) / stale-SHA rejection.

@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 | self_reported | Gemini | Google DeepMind

Approval conclusion (author-owned PR; GitHub blocks formal self-approval)

动机 / Motivation

运行长程多轮会话并经 session_start resume 或选区重放恢复上下文的 Codex App/CLI Agent 与 turn_driver 维护者。
此前当选区恢复后发生失败的完整重放并触发 epoch 轮换时,原有逻辑在清理 candidate 的同时把旧 recovery 文件与 selected_resume 的选区状态一并清空,迫使 Agent 在恢复回合退回重新全量读盘;本次把该逻辑从 300 文件巨无霸 PR 中干净摘出为 5 文件聚焦切片(+599/-171,零旧兼容账本膨胀),跨 epoch 保留选区重放锚点并直接绑定物理 native replay 调用。
在 native_context_lifecycle.ts 精确以 readScopeCurrent && selectedResumeCurrent 在 epoch 轮换时迁延 selected_resume 及其已校验 sections,并在 codex_context_history.py 补全 _replayed_output_groups / observe_completed_native_reread 物理捕获校验。
非目标边界:本 PR 为独立的 C1 上下文恢复核心切片,不混入无关模块搬迁、UI 改造或历史 ledger 回写。

独立拆出 C1 核心切片避免了超大混合分支的持续合入冲突与审评僵局,让长程断点恢复在 epoch 轮换下保持高命中、高可信。

改动思路 / Approach

将选区恢复的跨 epoch 保留判定严格收口进 TypeScript 单一生命周期所有者 updateNativeContextLifecycle,同时由 Python codex_context_history 仅负责物理 JSONL/字节区间与摘要核验,两层职责清晰、无双重状态机。
当前 PR 边界:仅涵盖 C1 selected_resume 跨 epoch 保留与物理 native replay 锚定(5 files: native_context_lifecycle.ts, codex_context.py, codex_context_history.py 与对应 TS/Python 测试,+599/-171)。

  • 状态转移不变量:仅当 readScopeCurrent 且 selectedResumeCurrent 同时成立时才允许跨 context_epoch 迁延已被前序字节摘要核验过的 retained sections;若普通失效但无有效复核选区,则维持清空 consumed_pointers 与 confirmed_read_sections,绝不越权免读。
  • 物理证据不变量:_verified_replay_capture 与 observe_completed_native_reread 强制依据会话 rollout JSONL 偏移量、字节覆盖区间与 SHA 摘要核验真实完整重读和选区重放,防止伪造读记录绕过门禁。

具体改动 / Changes

依据规范 docs/architecture/rfcs/controller-executor-unified-contract-v0.md(修订 ef099ae3ac3c08054b9d928fff48aefe2615b950)核对核心条款:

  • C1-SELECTED-RESUME-EPOCH:选区恢复跨 epoch 仅保留经严格摘要比对的字段并同步更新 selected_resume.epoch(由 tests/control_plane_ts/context_delivery.test.ts 36/36 PASS 验证)。
  • C1-PHYSICAL-REPLAY-ANCHOR:物理 native replay 校验完整字节范围与页摘要覆盖(由 tests/control_plane/test_codex_context_resume.py 与 budget 测试共 151 passed 验证)。

关键代码讲解

  1. loopx/control_plane/quota/native_context_lifecycle.ts:68(updateNativeContextLifecycle):计算 readScopeCurrent 与 selectedResumeCurrent,在 candidate?.native_resume 失败触发 epoch 轮换后,将经 digest 核验的 retained sections 与新 context_epoch 一并写入 state.read_scope 与 state.selected_resume。
  2. loopx/control_plane/turn_driver/codex_context_history.py:182(_verified_replay_capture / _replayed_output_groups / observe_completed_native_reread):重整重放捕捉切片逻辑,核对 rollout 字节偏移与完整覆盖区间,补齐 PostToolUse 完整重读回执观测。
  3. loopx/control_plane/turn_driver/codex_context.py:417(codex_context_hook):从分页输出过滤出全量页面并串联 checkpoint_native_read 与 observe_completed_native_reread。

对主干的风险 / Risks

变更收敛在 5 个核心与测试文件,无新增顶层 Python 模块,无新持久化配置文件或外部依赖。潜逃风险主要是重放日志截断或损坏,此时 _covers_bytes 与 _matching_replay_interval 会 fail-closed 回退为无效重放并要求重新完整读盘,不放行半截状态。远端 Actions kernel-static-checks 当前失败系基底 origin/main 上 tests/control_plane/test_contract_health.py:10 F401(本 PR 未改该文件)。

验证项 级别 结果 实跑输出(exact head f3620ddf7c9742060b8044fb1de7177e7f96a4e2)
pytest -q tests/control_plane/test_codex_context_resume.py tests/architecture/test_top_level_module_budget.py required PASS 151 passed in 21.23s
node --experimental-strip-types --test tests/control_plane_ts/context_delivery.test.ts & tsc --noEmit required PASS 36 passed, 0 failed; tsc rc=0
ruff check loopx/control_plane/turn_driver/codex_context.py loopx/control_plane/turn_driver/codex_context_history.py tests/control_plane/test_codex_context_resume.py required PASS All checks passed!
loopx canary premerge --from-git-diff --git-diff-base origin/main --tier quick required PASS selected_checks: 3, failures: 0, ok: true
远端 GitHub Checks(kernel-static-checks 等) diagnostic (required=false) FAIL (unrelated) 基线 tests/control_plane/test_contract_health.py:10 F401 预存故障

我的整体评价 / Verdict

对精确 head f3620ddf7c9742060b8044fb1de7177e7f96a4e2 的评价:用户体验 preserved / 视觉维度 not_applicable(仅涉及 turn_driver 上下文生命周期与 TS 状态转移,未改动任何前端 UI、CSS 或可渲染界面(inspected: loopx/control_plane/quota/native_context_lifecycle.ts, loopx/control_plane/turn_driver/codex_context_history.py)。,unverified_scope: none),长程延续性 improved。作为严格解耦出来的 C1 核心切片,无兼容性旧包袱或账本冗余(compatibility_assessment=not_applicable,semantic_alignment=not_applicable),Python/TS 双侧核心单测、类型编译、ruff 与 premerge canary 均已实跑 PASS,同意合入。

English verdict: APPROVE

@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 · gpt-6.2-sol · OpenAI (runtime_reported, max)

Request changes conclusion (author-owned PR; GitHub blocks formal self-review)

REQUEST_CHANGES。有完整物理读回时能保留续跑依据。Code Mode 同轮较早的大记录会使当前有效绑定丢失。

动机 / Motivation

Codex 恢复会话时,持续任务需要找回已核验的当前条件,避免在证据完整时重复读取。

基线依赖最近工具输出与回调标识。当前提交核对物理记录和已读字节,并尝试在完整回放丢失时独立保留所选字段。

有完整物理读回时能保留续跑依据。Code Mode 同轮较早的大记录会使当前有效绑定丢失。

本次范围是 C1 原生回放和 epoch,不涉及扩大任务权限、重新批准或重做整个 #8266。

本轮较早大记录可以让后续证据完整的 Code Mode 调用失去原生回放依据。 修复原生绑定的扫描边界,重跑真实读回入口和匹配、拒绝对照。 当前 PR 只负责 Codex 原生读回与已选上下文的恢复。捕获所有权、调度准备和结算留在既有 owner。

改动思路 / Approach

TS 继续负责 epoch 和选区状态归约,Python 继续负责验证实际 rollout。当前问题出在一个父调用证明读取的记录范围,局部修复足以解决。

当前 PR 只负责 Codex 原生读回与已选上下文的恢复。捕获所有权、调度准备和结算留在既有 owner。

同一捕获的完整字节、少页和非当前输出的拒绝路径通过。经过独立选区验证的 scope 可在完整回放失败后移到新 epoch。 保留执行成功、Turn 和唯一父调用的证明,同时避免读取已结束调用之前的大正文。在原有 IO owner 内验证边界。证据不足时仍回落到原捕获读取。 同一 session、工作目录与捕获引用使用只读验证,接续执行仍由 Guard 与原结算校验决定。

具体改动 / Changes

本次核对 f3620dd,提案基线 421aa77。5 个文件 +599/−171,3 个生产文件与 2 个测试文件。 比较实验使用 421aa77,保留该版本的实际行为证据。

规范依据为 docs/architecture/rfcs/controller-executor-unified-contract-v0/contract-and-context.md,版本 421aa77。

  • C1-capture-retention (implemented)。区分保留证据、已读字节与稳定捕获。
  • C1-native-replay-coverage (not_met)。本轮较早大记录可以让后续证据完整的 Code Mode 调用失去原生回放依据。

关键代码讲解

  • _native_reader。向后寻与读回对应的本轮调用。直接调用可按 ID 完成匹配,Code Mode 还须查唯一尚未关闭的父调用。
  • _replayed_output_groups。把已观察的物理调用与保留的实际输出相交。同哈希的其他调用不能补页。
  • updateNativeContextLifecycle。独立验证的选择字段可以在完整回放失效时跨 epoch 保留。只接纳匹配摘要和作用域。

对主干的风险 / Risks

[P2] Protect replay evidence from earlier large records

本轮先产生一个 17 MiB 的正常背景事件,其后用 Code Mode 完整读回当前捕获的 15 页,再恢复同一会话。 共同基线四组对照均保留已核验候选。当前仅大记录处在本轮且使用 Code Mode 的组合丢失 native_resume,恢复后候选为空。短记录、上一轮相同大记录和直接工具调用对照均通过。 单页选区也独立复现。当前同轮 Code Mode 对照在恢复前已消费该字段,恢复后丢失该字段的保留依据,其余三组与基线四组均保留。 保留执行成功、Turn 和唯一父调用的证明,同时避免读取已结束调用之前的大正文。在原有 IO owner 内验证边界。证据不足时仍回落到原捕获读取。

验证记录。同一真实 CLI 调用和记录 fixture 在基线 4/4 通过。 定向 Python 414 通过、24 跳过,TypeScript 343 通过,控制面类型检查通过,预合并 6 项直接和 13 项选中检查通过。 原生宿主记录由隔离 fixture 提供。真实 Hook、reader 与 TS 归约均执行。样本中的文件 delta 仍可重建。实际线上频率、原生模型保留字节量和长期成本尚未测量。 与采样主干的本地合并树可生成。托管 CI 未查询。

语义与 CI 对齐

C1 接受规格的完整捕获、保留与覆盖契约。 既有捕获、resume、epoch 和选区字段。物理调用关联属于私有 IO 状态。 隔离运行 quota context-read 和 quota context-hook。交替把大背景记录放在本轮与上一轮,并比较 15 页读回后的恢复候选。 修复原生绑定的扫描边界,重跑真实读回入口和匹配、拒绝对照。

我的整体评价 / Verdict

REQUEST_CHANGES。同一捕获的完整字节、少页和非当前输出的拒绝路径通过。经过独立选区验证的 scope 可在完整回放失败后移到新 epoch。 本轮较早大记录可以让后续证据完整的 Code Mode 调用失去原生回放依据。 保留执行成功、Turn 和唯一父调用的证明,同时避免读取已结束调用之前的大正文。在原有 IO owner 内验证边界。证据不足时仍回落到原捕获读取。

原生宿主记录由隔离 fixture 提供。真实 Hook、reader 与 TS 归约均执行。样本中的文件 delta 仍可重建。实际线上频率、原生模型保留字节量和长期成本尚未测量。

English verdict: REQUEST_CHANGES — the Code Mode hook loses a fully observed native replay checkpoint when a large earlier record is in the same turn. Preserve the original binding guarantees while bounding verification to the relevant record interval.

… epochs and bind physical native replay calls

Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
…ve_reader

Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
@loopx-agent
loopx-agent force-pushed the codex/c1-standalone-core-20261118 branch from f3620dd to dca761a Compare November 18, 2026 13:03

@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)

已针对 PR #8942 基于最新 origin/main(648d06db54,已含 #8881、#8956、#9000、#9004)rebase 并修复 [P2] 后的精确提交 dca761a9e5e022d847ff910823d4639382fa0b79 完成完整代码走查、规范核对与隔离真实宿主验证(审查结论:APPROVE,无未解决阻塞缺陷)。

依据规范 docs/architecture/rfcs/controller-executor-unified-contract-v0.md(版本 09d0fa13594235fcd3dc13c5bd63abaf35a2d9aa)核对核心准则:

  • C1-selected-resume-epoch:经字节重放独立核验的选区在候选完整重放失败触发 context_epoch 轮换时迁延至新 epoch 并更新 read_scope 与 selected_resume,否则清空(由 tests/control_plane_ts/context_delivery.test.ts:1014-1046 与 test_codex_context_resume.py 验证)。
  • C1-physical-replay-turn-prefix:物理 rollout 绑定的 Turn 逆向扫描在已绑定开放 Code Mode exec 父调用后,通过 O(64 KiB) 流式头部分析(_reader_spans + _scan_turn_prefix_record)跳过同 Turn 更早 >16 MiB 记录正文同时严格追至 task_started 拒绝并发双开 exec 与损坏 JSON(由 14 组 test_same_turn_large_earlier_record_* 与隔离 SQLite 真实 CLI 宿主验证)。

动机 / Motivation

所有通过 loopx/control_plane/turn_driver/codex_context.py(codex_context_hook)和 loopx/control_plane/quota/native_context_lifecycle.ts(updateNativeContextLifecycle)恢复选区或原生上下文的 Codex App/CLI Agent 与 turn_driver 维护者。

修改前(f3620ddf7c 及此前 origin/main@648d06db54):当选区恢复后完整捕获重放失败触发 epoch 轮换时 updateNativeContextLifecycle 会一并清空 selected_resume 与 consumed_pointers,且 _native_reader/_reader_tail 在命中开放 Code Mode exec 父调用后仍将同 Turn 更早的 >16 MiB 记录整行拼接缓冲,导致 [P2] 同轮 17 MiB background_event 或前置大输出抛出 unsupported native reader record;修改后(dca761a9e5e022d847ff910823d4639382fa0b79):仅在 readScopeCurrent && (fileEpochCurrent || selectedResumeCurrent) 且逐项字节摘要核验一致时将 selected_resume 与已验证选区迁延到新 epoch,_reader_spans 结合 _scan_turn_prefix_record 以 (1, 64) 受限 C 正则流式校验并提取头部元数据(O(64 KiB) 内存)追至 task_started 保留并发双开 exec 拒绝,实测 159/159 恢复用例、TS 与真实 CLI 全通过。

精确 head dca761a9e5e022d847ff910823d4639382fa0b79 上:test_codex_context_resume.py 全量 159 例(含 14 例新 17 MiB 同轮/跨轮回归,tracemalloc < 2 MiB)与 4 组核心 Python 套件 344 passed in 88.17s(0 failed / 0 skipped);10 组 C1 Python 集成套件 985 passed, 14 skipped, 4 failed(4 失败已确认在未改动 origin/main@648d06db54 同现);TS 控制面 49 passed, 0 failed(706.55ms);tsc --noEmit、ruff、17 文件 mypy 全 0 错;canary premerge --tier quick ok=true(failures=0);loopx check --scan-path ok=true(errors=0);隔离 SQLite + 真实 CLI 宿主矩阵 ok=true(同轮 17 MiB Code Mode selected_resume、未变 SHA previously_delivered_fields 命中、变更 SHA 防误跳过、PreCompact 原件恢复零重入与 1 -> 2 单一扣费、错模型/缺页 2/3/篡改 SHA 拒绝全通过)。

本 PR 仅聚焦 C1 原生重放锚定、选区跨 epoch 迁延与同轮大前序流式扫描,不修改 _records 重放区间 _MAX_RECORD_BYTES 边界,亦不触碰 value-scout 在途 #8963 / #8981 / #9005 / #9008 / #9010(主干 #8956/#9004 后 test_context_first_read.py:874 与 test_saved_context_project.py:711 共 4 例失败在未改动 origin/main@648d06db54 同现,留由对应作者分支收敛,本 PR 不跨范围修改)。

改动思路 / Approach

全部生命周期迁延收口在单一 TS 所有者 updateNativeContextLifecycle,物理 rollout 解析与同轮大记录流式元数据提取限定在单一 Python IO 只读所有者 codex_context_history.py,未引入并行状态机或第三方解析依赖。

当前 PR 边界:严格收敛在 native_context_lifecycle.ts、codex_context.py、codex_context_history.py 及其 TS/Python 回归测试共 5 个文件(+1072 / -181),零新持久化字段、零新 CLI 参数、零兼容垫片。

  • 选区跨 epoch 迁延(loopx/control_plane/quota/native_context_lifecycle.ts:65-103):计算 readScopeCurrent 与 selectedResumeCurrent,在 SessionStart(source="resume") 且 candidate?.native_resume 失效触发 invalidate() 后,仅把在当前字节重放中逐项一致的 retained sections 与新 context_epoch 迁延写入 state.read_scope、state.consumed_pointers 与 state.selected_resume。
  • 物理 Turn 边界 + O(64 KiB) 流式大前序记录扫描(loopx/control_plane/turn_driver/codex_context_history.py:129-447):
    1. _reader_spans(stream, end) 以 64 KiB 逆向块扫定位每行起止偏移,消除多 MiB 拼接缓冲;
    2. parent is None 时继续严格执行 raw_len <= _MAX_RECORD_BYTES、完整 json.loads 与 _digest(record);一旦成功命中唯一开放 exec 父调用(parent is not None),对同 Turn 更早 >64 KiB 行通过 _scan_turn_prefix_record + _JsonSliceCursor(以受限重复 {1,64} 非回溯 C 正则 _JSON_STR_SPAN 跳过巨型正文,并做增量严格 UTF-8 校验及 JSON 语法检查)提取 type 与 payload 头部标识并继续向后追至 task_started(_turn_boundary)。
end ─(逆向 _reader_spans 64 KiB)─▶ item_completed(exit_code=0, turn_id) ─▶ open exec parent (parent!=None)
                                                                                  │
                 ◀──(继续扫至 task_started,拒绝同轮第 2 个未闭合 exec)─────────────┘
                 ├─ len <= 64 KiB ──▶ json.loads(raw)
                 └─ len > 64 KiB  ──▶ _scan_turn_prefix_record (C正则 {1,64} 跳正文,仅解头部元数据)

(说明:来源驱动示意图,总结 codex_context_history.py:129-447 控制流,非独立执行证据。)

具体改动 / Changes

共涉及 5 个文件(相对 origin/main@648d06db54 +1072 / -181):

  1. loopx/control_plane/quota/native_context_lifecycle.ts(+25 / -13):updateNativeContextLifecycle 支持经字节重放核验的 selected_resume 跨 epoch 轮换迁延。
  2. loopx/control_plane/turn_driver/codex_context.py(+4 / -3):在 PostToolUse 路径串联 checkpoint_native_read、observe_completed_native_reread 与 checkpoint_native_selection。
  3. loopx/control_plane/turn_driver/codex_context_history.py(+557 / -143,共 815 行):以 _reader_spans、_JsonSliceCursor、_scan_turn_prefix_record 与 _native_reader 替换旧尾扫启发式,补全 _replayed_output_groups、_replayed_selection_sections 与 observe_completed_native_reread。
  4. tests/control_plane/test_codex_context_resume.py(+451 / -21):新增 14 组 test_same_turn_large_earlier_record_*(覆盖 17 MiB 带转义及 UTF-8 background_event/closed_output、Code Mode 15+ 页与选区 tracemalloc < 2 MiB、并发双开 exec 与控制字符 fail-closed 拒绝)。
  5. tests/control_plane_ts/context_delivery.test.ts(+35 / -1):新增 updateNativeContextLifecycle 选区跨 epoch 保留与清空单测。

对主干的风险 / Risks

  • 兼容性与内存安全:所有大于 16 MiB 的活跃读取/直接回复(parent is None 或 call == reader)仍 fail-closed 拒绝;_records 重放区间继续受 _MAX_RECORD_BYTES 约束。17 MiB(445 万转义)微基准实测 _sre 峰值自 4.59 MiB 降至 147,420 B、耗时 0.461s。
  • 主干既有用例与远端 CI 说明:main@648d06db54 在 #8956/#9004 后既有 test_context_first_read.py:874(2 例)与 test_saved_context_project.py:711(2 例)失败;远端 CI 在最新提交 dca761a9e5e022d847ff910823d4639382fa0b79 排队中,合并需维护者明确授权与远端必需检查全绿。
验证项 级别 结果 实测输出(exact head dca761a9e5e022d847ff910823d4639382fa0b79)
ruff / mypy --strict / 全仓 17 文件 mypy / tsc --noEmit required PASS All checks passed! / 0 issues (17 files) / tsc exit 0
node --experimental-strip-types --test ... required PASS 49 passed, 0 failed (706.55ms)
pytest -q tests/control_plane/test_codex_context_resume.py -k large_earlier_record required PASS 14 passed, 145 deselected in 16.07s
4 组核心 Python 套件(resume, lifecycle, budget, mcp_settlement) required PASS 344 passed in 88.17s (0 failed, 0 skipped)
10 组 C1 Python 套件全扫 required MIXED (4 主干同现) 985 passed, 14 skipped, 4 failed(4 失败在未改 648d06db54 同现)
隔离 SQLite + 真实 CLI 宿主矩阵 & canary premerge & loopx check required PASS ok=true(3/3 canaries failures=0, 5 文件 errors=0, 选区恢复/拒绝/零重入单一扣费 1->2 全通过)

我的整体评价 / Verdict

  • Long-horizon 影响:improved — C1 恢复链在 epoch 轮换与同轮前序大记录场景下保持选区复用与原件恢复契约。
  • User-experience 影响:improved — 消除 Codex Code Mode 下因早期大背景/工具记录引起的静默降级全量重读。
  • visual_review:not_applicable — 纯控制面生命周期、turn_driver IO 与测试,无 UI 改动。
  • 可观测语义 / 复用 / 词表:observable_semantics 为 intentional_change_validated,repository_reuse 为 reused,authority_semantics 与 semantic_alignment 为 aligned(candidate_decision="reuse_existing")。

已完全解决 gpt-6.2-sol [P2] 阻断项并与最新 origin/main 无冲突对齐,建议进入维护者合入门禁复核。

English verdict: APPROVE exact head dca761a (author-owned COMMENTED approval review); rebased cleanly onto origin/main@648d06db54 and resolved the [P2] same-turn oversized-record binding bug in _native_reader via O(64 KiB) streaming _reader_spans and bounded {1,64} regex JSON header scanning (_scan_turn_prefix_record), verified with 344 passed Python core/budget/MCP tests (incl. 14 new 17 MiB tests), 985 passed across the 10-suite C1 sweep (4 pre-existing main failures reproduced on 648d06d), 49 passed TypeScript tests, 17-file mypy clean, quick premerge canaries (0 failures), and isolated SQLite real-CLI host continuity/recovery qualification.

This branch has not been deployed

No deployments
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