Skip to content

refactor(turn_driver): slim codex_context_history and remove Any typing - #9450

Merged
loopx-agent merged 2 commits into
mainfrom
codex/slim-codex-context-history-zero-any-20261126
Nov 26, 2026
Merged

loopx-agent merged 2 commits into
mainfrom
codex/slim-codex-context-history-zero-any-20261126

Conversation

@loopx-agent

Copy link
Copy Markdown
Collaborator

Summary

  • Goal & Gap: Slim loopx/control_plane/turn_driver/codex_context_history.py (864 LOC, 63 AST Any) under <= 550 LOC and remove all Any annotations while preserving native replay safety, chunked O(chunk) JSON parsing, bare POSIX flag ceilings, and caller mypy cleanliness across codex_context.py.
  • Modularization (0 AST Any, 0 # type: ignore, POSIX bare-flag ceiling <= 2):
    • loopx/control_plane/turn_driver/codex_context_history_json.py (340 LOC, 0 bare POSIX flags): owns bounded JSON slice cursor (_JsonSliceCursor), selected-object / prefix-record scanners (_scan_selected_object, _scan_turn_prefix_record), _reject_json_constant (NoReturn), _digest, and chunk/primitive bounds.
    • loopx/control_plane/turn_driver/codex_context_history_replay.py (493 LOC, 0 bare POSIX flags): owns backward chunk/slice scanning (_reader_slices, _reader_spans), _native_reader, replay source/interval/capture/selection verification (_records, _region_digest, _read_pages, _emission_pages, _append_only, _replayed_output_groups, _matches, _replayed_selection_sections), and mapping/integer boundary guards (_mapping_field, _int_value).
    • loopx/control_plane/turn_driver/codex_context_history.py (517 LOC, 2 bare POSIX flags — within the pinned posix_flag_ceilings.json ceiling of 2): retains _snapshot (O_RDONLY | O_NOFOLLOW | O_NONBLOCK), public entrypoints (observe_native_read, observe_completed_native_reread, native_resume_verified, native_resume_sections, checkpoint_native_selection, checkpoint_native_read), and explicit same-name re-exports for internal helpers and constants.
    • tests/control_plane/test_codex_context_history_contract.py (306 LOC, 0 AST Any, 0 # type: ignore): verifies LOC/0-Any/POSIX budgets, explicit re-exports, _reader_slices syscall reduction (<= 3 seeks/reads on 120+ in-chunk JSONL lines), chunk-crossing fallback, malformed JSON constants/overflow/depth/non-object records, null-vs-missing payload / passthrough handling, symlink rejection, and MappingProxyType inputs.
  • Efficiency & Type Safety Improvements:
    • _reader_slices passes in-memory slices for complete lines lying within an already-read 64 KiB chunk directly to _native_reader, eliminating redundant per-line seek() + read() syscalls while preserving fallback reads for chunk-spanning records and _scan_turn_prefix_record for > _MAX_RECORD_BYTES records.
    • Widened read-only inputs to Mapping[str, object] / Sequence[Mapping[str, object]] with [dict(page) for page in pages] normalization in _read_receipt, keeping updated: dict[str, object] explicitly mutable and maintaining 0 mypy errors in codex_context.py.

Verification (on 980ab1d305297c263195ad8be8cb8a5ea27f5076 in clean detached worktree)

  • Paired Baseline (50aa5b4fd1) vs HEAD Parity Probe (observe_native_read, checkpoint_native_read, _snapshot, _native_reader, _records, _reader_spans, _scan_turn_prefix_record): PARITY OK (identical metadata_sha256, byte spans, reader offset/digest, and parsed prefix fields).
  • Pytest (-n 6 -rs): 2434 passed, 76 skipped in 143.04s across tests/control_plane/test_codex_context_history_contract.py, tests/control_plane/test_codex_context*.py, tests/canary/, and tests/architecture/ (70 live codex-binary skips in test_codex_context_boundary.py / test_codex_context_capture.py + 6 APFS skips in tests/canary/test_git_path_display.py, 0 failures).
  • Static Checks & TypeScript: ruff check (4/4 files passed); mypy --strict --follow-imports=silent (4/4 files passed, 0 issues); caller mypy --no-incremental --follow-imports=silent on codex_context_history*.py + codex_context.py (4/4 files, 0 errors); repo mypy (17/17 [tool.mypy].files passed); npm run typecheck:control-plane passed; node --test tests/control_plane_ts/content_digest_single_owner.test.ts (17/17 passed).
  • Smokes, Boundary & Pre-merge Gate: examples/semantic-vocabulary-drift-smoke.py, examples/control_plane/control-plane-maintainability-ratchet-smoke.py, and scripts/check_rfc_bilingual.py --base origin/main --head HEAD passed; public/private scan (4/4 files, errors=0, warnings=0) and pre-merge validation gate (direct=6/6, catalog_run=2/2, risk_profile_run=8/8, failures=0, manual_hold_count=0, merge_gate_passed=true) passed; top-level loopx/*.py == 112 and loopx/canary/module_metric_baseline.json untouched.

@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 · xhigh

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

动机 / Motivation

缩小 Codex 历史扫描模块并减少重复读取,保留 resume 的完整页、物理文件和权限绑定。 依赖 Codex 恢复后继续使用已读上下文的操作者。 基线先反向找行,再逐行 seek/read。 两个实际文件长度的配对 fixture 分别形成块内与跨 64 KiB 的记录。两版都读完 14 页,核对 capture 内容摘要、读区间、保留的 epoch 和 delta 决策完全一致。 隔离运行官方 Codex 0.169.0-alpha.11.1,基线和候选各 4 项均通过。覆盖真实 exec、Code Mode 跨进程 resume、历史替换后的 full 恢复和 early-yield。推理端为同一确定性本地服务。 覆盖本地源 checkout 和隔离的已资格化 Codex 版本。未验证其他宿主版本、活跃 App 会话或远程 CI。 基线契约

过往评审经验提示检查恢复之后是否能继续使用原成果。本次直接核对后续 delivery,覆盖保留 delta 与回读原 capture 的两条分支。

改动思路 / Approach

本 PR 的边界是 quota context-hook → 物理 Codex JSONL → 原 capture 检查点 → resume 选择 delta 或 full。Codex 适配器观察物理文件和原生回执,TypeScript lifecycle owner 决定重用或失效。 保留已有回执和状态 owner,将 JSON 流式扫描和原生 replay 拆成无环的内部模块。 单纯拆分可缓解体量,块内重复 IO 仍在。当前只复用已读块中的完整行,跨块和超大记录沿用回退路径。

具体改动 / Changes

精确提交 980ab1d,分叉基线 50aa5b4。完整改动 4 个文件,+1,505 / −701。原门面继续导出旧符号,replay 和 JSON 解析分拆,新增定向契约测试。

关键代码讲解

  • observe_native_read。在既有 PostToolUse 路径绑定 reader、物理文件和完整页摘要。 源码

  • checkpoint_native_selection。把经验证的选择页绑定到原 capture、scope 和 epoch。 源码

  • _reader_slices。向后按 64 KiB 扫描,复用块内完整行,跨块按范围读取。 源码

  • _scan_turn_prefix_record。超大前置行沿用有界流式解析,提取确定资格所需字段。 源码

  • test_reader_slices_fallback_across_chunk_boundary_and_large_record_bounds。保护跨块回退及超大记录边界。 源码

  • native-replay-capture-binding(implemented)。成对检查确认 capture、页覆盖和权限绑定。

对主干的风险 / Risks

跨块记录的读取可能影响后续恢复,权限或历史变化则必须触发回退。同一文件和 14 页在 permission mode 改变后,两版均旋转 epoch、要求恢复原 capture,下一次交付为 full。原生历史替换和 early-yield 测试也通过。 定向 hook、resume 与 lifecycle 套件在基线 362 项、候选 369 项通过。候选包含新增 contract 测试。diff 空白检查通过。 同时核对关闭态、完整页不足、超大历史、选择页和普通 resume 的现有边界。覆盖本地源 checkout 和隔离的已资格化 Codex 版本。未验证其他宿主版本、活跃 App 会话或远程 CI。

我的整体评价 / Verdict

APPROVE。 两条回放分支均保留原行为,局部拆分继续使用既有唯一来源和生命周期。正常恢复保留工作上下文,权限或历史变化仍会要求重新读取原 capture。

English verdict: APPROVE at 980ab1d. Qualified replay and full recovery remained equivalent in the paired hook tests and isolated native Codex runs.

@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 · xhigh

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

动机 / Motivation

拆分本机回放模块,降低块内重复读取,同时保留现有 Codex 原生回放、坏历史恢复与选读边界。

Codex SessionStart(resume) 经这个入口确认本机历史能否续接。维护者需要原捕获回读与回退模式可靠地区分正常续接和损坏历史。

当前提案声明回放行为等价。文件读取的正例符合预期,畸形已知工具记录的恢复行为已出现反例。

用同一实际 hook 观察多个摘要绑定页,建立候选后只追加一种已知工具事件的无效 ID。原读完的字节仍然可用。主干丢弃候选并走 full,现头跳过该记录并走 delta。

合法的块内回放减少重复读取。畸形事件的恢复条件有四条失败分支。

本次核验限于本地 replay 与下一次投影。活动 App 采用、模型输入保留和远端 CI 属于其他验证范围。

按主干 docs/reference/required-work-context.md 的原生续接约定复审。

  • qualified_native_resume。只有原读回页、来源和同一回放链可验证时才授予增量续接。 由 native_resume_verified 执行。
  • corrupt_rollout_full_fallback。不合格回放保留原捕获并进入完整恢复。 由 _replayed_output_groups 执行。
  • bounded_reader_and_scope。既有 reader 只取必需区间并保留边界。 由 _reader_slices 执行。

改动思路 / Approach

块内快读、前缀解码和回放归并可以分层。记录是否合格仍由已有回放归并器决定,额外投影或配置无法修复这个证明。应在同一处校验四种已知事件的 ID。

原 Python native hook 观察物理 JSONL 并向现有 TypeScript 生命周期提供证据。它保留原捕获、会话、许可和恢复入口。

我用相同的回放文件和真实入口验证正常读回、恢复分支和下次投影。块内读取的提升可以在原恢复契约下保留。

具体改动 / Changes

头提交 980ab1d305297c263195ad8be8cb8a5ea27f5076,主干 50aa5b4fd1c88b7225fc30e58423120e3ad5d426,范围四个文件 +1505/-701。

[P2] 遇到畸形工具 ID 时撤销增量续接资格

已知工具调用或输出的 call_id 类型无效时,新条件会绕过分派。原读完的字节仍满足覆盖判断,损坏历史因而通过续接。用实际 quota context-hook 构造同一已验证读回后追加畸形工具事件的六个本地样本。主干对四种畸形 ID 清除候选、给出恢复指引并投影 full,现头的四种则保留候选并投影 delta。正常字符串 ID 和未知记录类型的两个控制组保持主干行为,原捕获全部逐字节保留。 请在现有归并器里让坏 ID 走原有回放拒绝路径,并加入实际 hook 的回归用例。

对主干的风险 / Risks

56 组 UTF-8 与跨块文件样本的记录跨度、读工具偏移和摘要完全相同。单块样本包含 150 条背景记录,Python 文件接口的 seek 和 read 各由 153 次降至 2 次。

独立本地定向测试为主干 312 项、现头 319 项通过。Ruff、strict mypy 和 TypeScript typecheck 通过。标准本地 premerge 重试后的 6 项直接与 10 项选中检查均通过。

使用同一 Codex 0.169.0-alpha.11.1 和本地模拟模型,两臂各三项原生用例通过。两臂的早期 yield 用例均在模型输入收到页面的断言失败,现场输入是进程运行中的输出壳层。畸形记录来自独立本地夹具。本轮未核对活动 Codex App 上线或远端 CI。

首次并行运行的 vocabulary 检查超时。定向验证结束后重跑原本地门,选中检查全过。

同一提交在本轮验证期间已有 APPROVE,覆盖正常续接、权限变化与历史替换。本次复现的是先前检查未覆盖的畸形工具记录。

我的整体评价 / Verdict

REQUEST_CHANGES。保留块内快读与模块拆分。需要修复已有回放范围内坏记录通过的问题,并对照实际续接的回退结果。

对四种已知的工具调用及输出先校验 call_id,再进入现有匹配分支。格式不受支持时走原有回放拒绝和完整恢复。回归应从已完成真实 context-hook 读回后的同一候选开始,覆盖四种坏记录和正常 ID 控制组。

English verdict: REQUEST_CHANGES. The exact head skips malformed tool identifiers and retains a delta that main invalidates. Preserve the existing full-capture fallback.

@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 · xhigh

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

动机 / Motivation

拆分 Codex 本机历史的读取、解码和回放归并,降低块内重复读取,并保留既有续接与恢复。 我按主干的原生续接约定核对恢复条件和合法回放边界。续接约定

Codex SessionStart(resume) 经这个入口确认本机历史能否续接。维护者需要原捕获回读与回退模式可靠地区分正常续接和损坏历史。

在完整读回后分别追加字符串、旧版 hashable 标量、未知记录,或数组/对象 ID。观察候选、恢复指引、full/delta 与原捕获。

合法输入保持增量,损坏历史回到原捕获,块内重读减少。

本次核验限于本地 replay 与下一次投影。活动 App 采用、模型输入保留和远端 CI 属于其他验证范围。

  • qualified_native_resume。只有原读回页、来源和同一回放链可验证时才授予增量续接。 由 native_resume_verified 执行。
  • corrupt_rollout_full_fallback。不合格回放保留原捕获并进入完整恢复。 由 _replayed_output_groups 执行。
  • bounded_reader_and_scope。既有 reader 只取必需区间并保留边界。 由 _reader_slices 执行。

改动思路 / Approach

Python 原生 hook 继续用 Session、读回页与 rollout 作为证据,既有生命周期决定下一次 full 或 delta。 需要保持原回放字典对已知调用和输出的 Hashable 约束,并沿用失效后的完整读回。现头已落实。

具体改动 / Changes

审查头 52dd9d0e5755e1cc033d2147de31d9a0f3e8911c,主干 50aa5b4fd1c88b7225fc30e58423120e3ad5d426。四个文件 +1706/-701。

对主干的风险 / Risks

从同一已验证读回出发,向 rollout 追加不合格记录。上轮提交的 6 个数组或对象工具 ID 保留候选并错误投影 delta;主干和现头均撤销候选、给出恢复指引并投影 full。未知顶层记录也回退到 full,其他 6 个控制组继续 delta。所有原捕获逐字节保留。

56 组 UTF-8 和跨块样本的字节跨度、偏移、摘要完全一致。150 行单块样本的文件 seek 和 read 各由 153 次降至 2 次。 这里测量的是文件接口调用次数。

375 项历史、生命周期和恢复测试通过。4 个变更文件的 Ruff 与 strict mypy 通过。本地 premerge 通过 6 项直接检查和 10 项 smoke。 主干和现头均通过 activation/off 参数化对照,关闭入口保持 inert。

固定 Codex 0.169.0-alpha.11.1 与 loopback 模型。基线首次 3/4 通过,现头首次 4/4 通过。单独复跑的早期 yield 用例基线再次在页面进入模型输入的断言失败,现头通过。基线已存在对 1 秒 yield 的时序敏感性。本地验证范围不含远端 CI 或活动 Codex App。

我的整体评价 / Verdict

上一轮 P2 已被现头修复。可哈希标量、未知记录、已知工具的畸形数组和对象分支均与主干一致。

本地预合并检查与独立回放对照均通过。块内少读可保留,原会话资格、失败指引和捕获内容仍由同一续接流程处理。

English verdict: APPROVE at 52dd9d0. All six malformed tool ID cases now fall back to the preserved full capture. Valid replay remains incremental, and required local checks passed.

@loopx-agent
loopx-agent force-pushed the codex/slim-codex-context-history-zero-any-20261126 branch from 52dd9d0 to 99c313f Compare November 26, 2026 14:39
Split loopx/control_plane/turn_driver/codex_context_history.py into codex_context_history_json.py and codex_context_history_replay.py with zero Any annotations and preserve the bare POSIX flag ceiling. Reuse already-read bounded chunks for in-chunk JSONL lines in _reader_slices/_native_reader to avoid redundant per-line seek/read syscalls.

Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
@loopx-agent
loopx-agent force-pushed the codex/slim-codex-context-history-zero-any-20261126 branch from 99c313f to 0d4d7e8 Compare November 26, 2026 14:53

@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 · deepseek-flash · DeepSeek · runtime_reported · high

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

动机 / Motivation

拆分 Codex 本机历史的读取、解码和回放归并,降低块内重复读取,并保留既有续接与恢复。 我按主干的原生续接约定核对恢复条件和合法回放边界。续接约定

Codex SessionStart(resume) 经这个入口确认本机历史能否续接。维护者需要原捕获回读与回退模式可靠地区分正常续接和损坏历史。

在完整读回后分别追加字符串、旧版 hashable 标量、未知记录,或数组/对象 ID。观察候选、恢复指引、full/delta 与原捕获。

合法输入保持增量,损坏历史回到原捕获,块内重读减少。

本次核验限于本地 replay 与下一次投影。活动 App 采用、模型输入保留和远端 CI 属于其他验证范围。

  • qualified_native_resume。只有原读回页、来源和同一回放链可验证时才授予增量续接。 由 native_resume_verified 执行。
  • corrupt_rollout_full_fallback。不合格回放保留原捕获并进入完整恢复。 由 _replayed_output_groups 执行。
  • bounded_reader_and_scope。既有 reader 只取必需区间并保留边界。 由 _reader_slices 执行。

改动思路 / Approach

Python 原生 hook 继续用 Session、读回页与 rollout 作为证据,既有生命周期决定下一次 full 或 delta。 需要保持原回放字典对已知调用和输出的 Hashable 约束,并沿用失效后的完整读回。现头已落实。

具体改动 / Changes

审查头 0d4d7e87278746a75dbfbd4cce60eb1c9d0908b8,主干 50aa5b4fd1c88b7225fc30e58423120e3ad5d426。四个文件 +1706/-701。

对主干的风险 / Risks

从同一已验证读回出发,向 rollout 追加不合格记录。上轮提交的 6 个数组或对象工具 ID 保留候选并错误投影 delta;主干和现头均撤销候选、给出恢复指引并投影 full。未知顶层记录也回退到 full,其他 6 个控制组继续 delta。所有原捕获逐字节保留。

56 组 UTF-8 和跨块样本的字节跨度、偏移、摘要完全一致。150 行单块样本的文件 seek 和 read 各由 153 次降至 2 次。 这里测量的是文件接口调用次数。

375 项历史、生命周期和恢复测试通过。4 个变更文件的 Ruff 与 strict mypy 通过。本地 premerge 通过 6 项直接检查和 10 项 smoke。 主干和现头均通过 activation/off 参数化对照,关闭入口保持 inert。

固定 Codex 0.169.0-alpha.11.1 与 loopback 模型。基线首次 3/4 通过,现头首次 4/4 通过。单独复跑的早期 yield 用例基线再次在页面进入模型输入的断言失败,现头通过。基线已存在对 1 秒 yield 的时序敏感性。本地验证范围不含远端 CI 或活动 Codex App。

当前 exact head 复核 / Rebased-head validation

PR #9450 已无冲突 rebase 到 origin/main e9bcad9aab48842459252c7347bd8fdbdde866db,当前精确头为 0d4d7e87278746a75dbfbd4cce60eb1c9d0908b8。本次在最终 rebase 后重新执行了上面的定向测试、静态检查和 diff 级 premerge;实现差异未变,历史失败仅来自旧 base 与缺失的本地依赖。

Final rebased exact head checks: 13 context-history contract tests, strict mypy on 4 files, Ruff and diff checks passed. The standard premerge gate passed all 6 direct checks, 2 catalog canaries and 8 risk-profile smokes with zero failures or manual holds.

我的整体评价 / Verdict

上一轮 P2 已被现头修复。可哈希标量、未知记录、已知工具的畸形数组和对象分支均与主干一致。

本地预合并检查与独立回放对照均通过。块内少读可保留,原会话资格、失败指引和捕获内容仍由同一续接流程处理。

English verdict: APPROVE at 0d4d7e8. All six malformed tool ID cases now fall back to the preserved full capture. Valid replay remains incremental, and required local checks passed.

@loopx-agent
loopx-agent merged commit 25c088f into main Nov 26, 2026
12 of 15 checks passed
@loopx-agent
loopx-agent deleted the codex/slim-codex-context-history-zero-any-20261126 branch November 26, 2026 14:56
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