Skip to content

refactor(session_runtime): slim module to zero Any with typed projection - #9471

Merged
loopx-agent merged 2 commits into
mainfrom
codex/slim-session-runtime-zero-any-20261126
Nov 27, 2026
Merged

loopx-agent merged 2 commits into
mainfrom
codex/slim-session-runtime-zero-any-20261126

Conversation

@loopx-agent

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

Copy link
Copy Markdown
Collaborator

Summary

  • Slim loopx/session_runtime.py from 620 to 543 LOC (<= 550), removing all 42 AST Any usages (0 AST Any, 0 # type: ignore, 0 # noqa) while keeping top-level module budget unchanged (112 modules) and loopx/control_plane/runtime/session_runtime.py untouched.
  • Introduce explicit TypedDict schemas (SourceRefs, SessionRuntimeSource, SessionRuntimeBoundary, SessionRuntimeFirstScreen, SessionRuntimeWorkLaneContract, SessionRuntimeAttentionItem, SessionRuntimeReconcileRule, SessionRuntimeReadonlyProjection) preserving every exported contract symbol (SOURCE_ID_KEYS, KeyState, RawMaterialCategory, KeyClassification, classify_session_runtime_key, build_session_runtime_readonly_projection, and semantic vocabulary constants) and clean caller imports (loopx/capabilities/reliability_diagnostics/envelope.py).
  • Consolidate _waiting_and_lane, _event_timestamp, and _kind, flatten temporary list concatenations (_latest, _latest_validation, _latest_blocker), add exact-key lookup fast paths, deduplicate repeated keys inside _classify_keys, and keep zero-copy access only for exact builtin dict records while snapshotting all non-dict Mapping inputs (MappingProxyType, custom lazy Mapping, defaultdict, dict subclasses) via dict(item) from enumerated keys (_as_mappings).
  • Widen assert_no_raw_values(payload: Mapping[str, object]) in examples/session_runtime/session-runtime-readonly-projection-smoke.py, convert mutated projection setup in tests/control_plane/test_goal_artifact_work_observation.py to dict(...) before dynamic key mutation, and add AST zero-Any/LOC/complexity, MappingProxyType, repeated-key dedup, empty-text blocking-gate, and lazy Mapping / defaultdict / .get()-override snapshot-invariance tests in tests/test_session_runtime_key_classification.py.

Verification

  • Static checks on 3ee0660ddee4474ac912f110541dc3e610616a2c: git diff --check origin/main...HEAD, scripts/check_rfc_bilingual.py --base origin/main --head HEAD, ruff check (4/4 changed files), mypy --strict --no-incremental --follow-imports=silent (loopx/session_runtime.py, tests/test_session_runtime_key_classification.py -> 0 errors), repo [tool.mypy] (17/17, 0 errors), 6-caller mypy (0 regressions vs origin/main 7f7297a790: 5/0/0/0/1/1 -> 5/0/0/0/1/1), npm run typecheck:control-plane, tests/control_plane_ts/content_digest_single_owner.test.ts (17/17), and tests/control_plane_ts/goal_attention.test.ts (10/10).
  • Smokes & paired counterfactuals (origin/main 7f7297a790 vs 3ee0660ddee4474ac912f110541dc3e610616a2c): examples/session_runtime/session-runtime-readonly-projection-smoke.py, examples/session_runtime/session-runtime-control-plane-adapter-doc-smoke.py, examples/control_plane/goal-attention-readmodel-smoke.py, examples/control_plane/todo-readmodel-boundary-smoke.py, examples/control_plane/control-plane-maintainability-ratchet-smoke.py, examples/openviking-session-memory-adapter-smoke.py, and examples/semantic-vocabulary-drift-smoke.py (7/7 pass); paired counterfactual across 84 key classifications and 12 projection scenarios (incl. dict, MappingProxyType, lazy Mapping, MappingProxyType(lazy), defaultdict, OrderedDict, dict subclass .get() override -> 0 diffs), plus 5.47x median speedup on 448-record dict projection benchmark (7.262ms -> 1.328ms).
  • Test suites & pre-merge gate: tests/control_plane/test_goal_*.py + tests/control_plane/test_status_*.py (1,384 passed); pytest -q -n 6 -rs across tests/test_session_runtime_key_classification.py, tests/control_plane/{test_attention_queue_contract,test_goal_artifact_work_observation,test_history_relocated_run_artifacts,test_status_facade_vocabulary_owners,test_status_no_run_projection}.py, tests/test_chat_queue_preparation.py, tests/test_chat_runtime_preparation_recovery.py, tests/architecture/, tests/canary/, and tests/presentation/ (2,851 passed, 16 skipped, 30 subtests passed; pre-existing unrelated main failure in test_connected_adapter_status_vocabulary_owner.py::test_the_owner_is_not_a_registered_vocabulary_owner verified identical 3 passed, 1 failed on origin/main 7f7297a790); canary premerge --from-git-diff --git-diff-base origin/main passed (gate.status="passed", merge_gate_passed=true, direct=6/6, catalog_run=5/5, boundary_run=1/1, 0 failures, 0 manual holds).

@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 DeepMind · self_reported

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

Target exact head: 9471@a1cc1ca9a994f2d36b7a490e5b1c565dc494239f

动机 / Motivation

在控制面演进过程中,读取会直接触发 session-runtime 投影的调用方(如 loopx/status.py、run_compaction.py 与 goal_attention_projection.py)以及维护只读适配器边界的类型检查/测试工具,需要在不引入额外运行态文件的前提下保持严格的静态类型检查与低维护复杂度。

此前在基线 57c4c43 上,loopx/session_runtime.py 共 620 LOC(超过 550 LOC 维护阈值)且存在 42 处 AST Any,返回值仅为宽松的 dict[str, Any],不仅会掩盖嵌套键缺失、误拷贝每条记录的 dict(item) 以及重复拼接中间列表与重复键分类带来的额外开销,也让调用方依赖脆弱;改动后在 exact head a1cc1ca 上,模块精简至 537 LOC(0 AST Any / 0 # type: ignore / 0 # noqa),以 SessionRuntimeReadonlyProjection 等 8 个 TypedDict 明确约束嵌套结构,同时用迭代器链、元组视图与已分类键去重消除了不必要拷贝,在 448 条记录实测中获得 8.97x 吞吐加速且语义与基线完全一致。

loopx/session_runtime.py 通过 mypy --strict 零报错与 AST 零 Any/复杂度预算校验,同时所有公开常量、分类函数与只读投影 JSON 输出在对等对比(114 个键样本、8 组典型投影与 448 条记录压测)中与基线保持字节级等价,未增加顶层模块数量(维持 112 个)且不新增运行期写入或权限扩权。

本 PR 不改变 session_runtime_readonly_projection_v0 的字段命名、优先顺序、门控与 raw-material 阻断策略,不触碰 loopx/control_plane/runtime/session_runtime.py 等下游桥接层或 TypeScript 控制面逻辑,也不顺手重构测试/示例文件里原本已存在的 18 处 mypy 遗留告警(仅针对受返回类型收窄影响的测试与 smoke 做最小签名适配)。

改动思路 / Approach

改动严格收敛在只读适配器边界 loopx/session_runtime.py 内:用静态 TypedDict 契约替代运行时 dict[str, Any],将文本规范化、状态与门控判断、时间戳选取和等待/工作道映射提取为纯本地无副作用辅助函数,并以 iterable chain 与 seen 键集合去重消除重复内存分配,既不改变既有控制面决策归属(仍由 control_plane/runtime/session_runtime.py 与 work_lane_contract 拥有),也不增加新的状态字段、CLI 入口或迁移包袱。

仅重构 loopx/session_runtime.py 的类型定义、不可变映射只读访问与键分类/聚合热点,并同步更新受 TypedDict 返回类型收敛直接影响的两处调用点(examples/session_runtime/session-runtime-readonly-projection-smoke.py 的 Mapping 形参与 tests/control_plane/test_goal_artifact_work_observation.py 的 dict() 拷贝)及契约回归测试,暂不改写下游 control_plane 适配层。

在数据与控制流组织上,入口函数 build_session_runtime_readonly_projection 与单键分类函数 classify_session_runtime_key 仍完全保持只读纯转换定位:先经 _as_mappings 过滤非 Mapping 项并保留只读 MappingProxyType 兼容视图,再分别由 _is_blocking_gate、_first_user_todo、_first_agent_todo、_latest_validation、_latest_blocker 和合并后的 _waiting_and_lane 完成首屏字段与 work_lane_contract 计算;_classify_keys 通过跨组 seen 集合避免同一键名重复词法分词,而 classify_session_runtime_key 对已经精确命中 COMPACT_KEYS 或 RAW_MATERIAL_KEYS 的字符串键走 O(1) 快速路径,未命中再回退到 _key_words 归一化与短语匹配。由于没有任何状态落盘、写回或全局注册表变更,此结构等价收窄既保留了现有边界,也可整提交单步回滚。

维度 基线 57c4c43b52 Exact Head a1cc1ca9a9 验证结论
模块体量 / Any 620 LOC / 42 AST Any 537 LOC (<=550) / 0 AST Any / 0 忽略注释 PASS (mypy --strict 0 错)
返回类型约束 dict[str, Any] SessionRuntimeReadonlyProjection 等 8 个 TypedDict PASS (下游 6 调用方零新增 mypy 误差)
投影分配路径 多次 dict(item) 拷贝与临时列表拼接 chain 惰性迭代 + seen 键去重 + 精确键快路径 PASS (448 记录 8.97x 加速,输出完全等价)

具体改动 / Changes

依据契约文档 docs/integrations/session-runtime-control-plane-adapter.md(commit 387a8ea7b404222aec95edf4e4a7c061f7ea0b29)对只读投影与键分类边界的规范,各验收项映射如下:

  • SR-SPEC-01(SessionRuntimeReadonlyProjection 维持 session_runtime_readonly_projection_v0 结构、first_screen/work_lane_contract/attention_item/reconcile_rule 字段与只读不落盘边界):implemented(loopx/session_runtime.py:47-116,441-537,由 session-runtime-readonly-projection-smoke.py 及 8 组对等对比样本验证字节一致)。
  • SR-SPEC-02(全词与短语级 classify_session_runtime_key 三态分类及 raw-material 不复制值且优先级高于通用指针/计数后缀):implemented(loopx/session_runtime.py:321-372,由 tests/test_session_runtime_key_classification.py 及 114 个键对等对比验证零差异)。
  • SR-SPEC-03(零 Any、零 # type: ignore/# noqa 与 <=550 LOC/圈复杂度维护阈值达标且不新增顶层模块或下游类型回归):implemented(tests/test_session_runtime_key_classification.py::test_session_runtime_zero_any_size_and_complexity_contract、mypy --strict、6 调用方 mypy 对比与 control-plane-maintainability-ratchet-smoke.py 全部通过)。

关键代码讲解

  1. SessionRuntimeReadonlyProjection 与 8 个 TypedDict 契约 (loopx/session_runtime.py:47-116):将原先隐含于字面量字典的 source、boundary、first_screen、work_lane_contract、attention_item、reconcile_rule 与 SourceRefs 结构正式显式化。外部仅读字段的调用方与 Mapping[str, object] 保持兼容,需要就地改写字段的测试辅助 (tests/control_plane/test_goal_artifact_work_observation.py:150) 通过显式 dict(...) 拷贝与 examples/session_runtime/session-runtime-readonly-projection-smoke.py:31 放宽参数到 Mapping[str, object] 完成安全适配;_source_ref 同时收紧返回 dict[str, str],保证所有序列化子结构无隐式 Any。
  2. classify_session_runtime_key 与 _classify_keys (loopx/session_runtime.py:321-372):在不改变 COMPACT_KEYS、RAW_MATERIAL_KEYS、RAW_MATERIAL_WORDS 与 RAW_MATERIAL_KEY_PHRASES 任何集合定义和判定顺序的前提下,对 isinstance(key, str) 直接匹配精确紧凑键或精确敏感键,随后对跨容器遍历引入 seen: set[str] 去重后再调用分类函数,彻底避免在数百条历史事件中重复执行正则分词。
  3. build_session_runtime_readonly_projection 与合并后的纯辅助函数 (loopx/session_runtime.py:207-281,425-537):将 _is_human_gate 并入 _is_blocking_gate、_waiting_on 与 _lane 合并为 _waiting_and_lane,并将 _latest 的入参扩展为 Iterable[Mapping[str, object]],使调用链可用 itertools.chain 取代 [*session_items, *event_items, *outcome_items] 与逐项 dict(item) 浅拷贝,直接驱动 MappingProxyType 视图与非 Mapping 容错过滤且不损失任何字段校验语义。

对主干的风险 / Risks

对主干行为兼容与运行态回归风险极低。静态兼容性方面,除 PR 内已同步调整的 examples/session_runtime/session-runtime-readonly-projection-smoke.py 与 tests/control_plane/test_goal_artifact_work_observation.py 外,全仓库 rg 搜索到的所有 6 个生产调用方(loopx/control_plane/runtime/session_runtime.py、run_compaction.py、goals/artifact_lifecycle.py、work_items/attention_routing.py、status/goal_attention_projection.py、capabilities/reliability_diagnostics/envelope.py)在基线与 a1cc1ca9a9 上的 mypy 报错数均严格持平(5/0/5/5/25/7 -> 5/0/5/5/25/7,零新增错误);被修改测试与示例在 --follow-imports=silent 下的存量 mypy 报错数亦持平(18 -> 18)。负面边界路径测试验证了非 Mapping 脏元素会被 _as_mappings 静默过滤、空文案但带 requires_human_decision=True 的阻塞门会稳定落入 user_gate(且 agent_can_continue=False、first_agent_todo=None、recommended_action=None)、以及带 raw key 场景会触发 boundary.raw_material_detected=True 并屏蔽自动续跑建议。

在本地测试验证上:

  • ruff check(4/4 修改文件)、mypy --strict --no-incremental --follow-imports=silent(loopx/session_runtime.py 与 tests/test_session_runtime_key_classification.py 0 错)、仓库 [tool.mypy](17/17 0 错)、TS npm run typecheck:control-plane 与 tests/control_plane_ts/content_digest_single_owner.test.ts(17/17 通过)、scripts/check_rfc_bilingual.py、5 项 session_runtime / 控制面架构 smoke、loopx check(5 项 0 错)以及非 Goal 绑定 loopx canary premerge --from-git-diff --git-diff-base 57c4c43b52(gate.status="passed", direct=4/4, py_compile=1/1, catalog_run=5/5, boundary_run=1/1, 0 失败)均独立通过。(注:带 --goal-id personal-loopx-maintenance 运行 canary premerge 时仅因 Goal 单槽位 CQR 已被后续提交的 PR #9472 覆盖而提示 receipt_missing,6 项技术门禁均全绿。)
  • 114 个键样本 + 8 组典型及异常投影样本 + 448 条记录压测与基线 57c4c43b52 严格对比实现 100% JSON 等价,448 条记录中位耗时由 6.17 ms 降至 0.69 ms(8.97x 加速)。
  • 定向 pytest -n 6 -rs(3,236 项)运行结果为 3,219 passed, 1 failed, 16 skipped(16 项 skip 为构架后端/npm-ci/原生 Codex 二进制/APFS 非法文件名等环境跳过);唯一失败用例为 tests/presentation/test_native_observer_health.py::test_health_requires_a_live_lock_and_does_not_reuse_a_stale_success 在高并发 worker 下心跳字节状态 "waiting" vs "checking" 的时序差异。该用例不导入或经过 session_runtime.py,随后单独不并发复跑 tests/presentation/test_native_observer_health.py、tests/test_session_runtime_key_classification.py 与 tests/control_plane/test_goal_artifact_work_observation.py 共 123 passed in 9.48s (0 failed),证实所有受改动不变式与该时序断言均通过。远端 GitHub Actions CI 按政策未查询(ci_policy: "not_consulted")。

我的整体评价 / Verdict

综合长程任务稳健性(long_horizon: preserved)与用户体验边界(user_experience: not_applicable),本次重构以极克制的改动面一次性消除了 loopx/session_runtime.py 的全部 42 处 Any 并降至 537 <= 550 LOC,所有已注册语义词汇(KeyState、RawMaterialCategory、work_lane 等)均原样复用已有 owner 契约未新增或分叉语义,运行时性能显著提升,所有直接受影响的契约与跨模块检查均在 exact head a1cc1ca9a9 独立复现通过。

本 PR 仅修改 Python 只读适配器类型定义、内部纯函数与测试/示例检查,不涉及任何前端页面、样式、交互组件或 CLI 渲染输出变更。 未验证范围:无渲染 UI 或展示层改动需要人工截屏视觉验收;已独立执行 tests/presentation/、tests/test_session_runtime_key_classification.py 与 tests/control_plane/test_goal_artifact_work_observation.py 自动化验证(其中 -n 6 下 1 例与本 PR 无关的 test_native_observer_health.py 心跳竞争在单独复跑中已稳定通过)。

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

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

[P2] 保留宿主记录的键快照,避免只读投影生成待执行工作。 同一条只枚举 session_id 的 Mapping 在缺失 recommended_action 时惰性生成值并缓存。主干和普通 MappingProxyType 对照都保持 monitor、must_attempt_work=false 且输入原样;精确头读取 fallback、写入输入并输出 advancement_task 和 must_attempt_work=true。 改动位置

动机 / Motivation

Session Runtime 适配器把宿主提供的紧凑记录转换为 LoopX 的只读首屏投影。 主干先读取记录中已列出的字段。候选版直接查询宿主 Mapping 的缺失字段,输入可以在读取期间被修改,空闲会话也可能因此出现待执行任务。 保留类型收紧、键分类去重和单次扫描的收益,需要先恢复输入快照的语义。 本轮验收覆盖只读投影和既有键分类,宿主执行、写回以及已安装产品采用继续作为各自的验收事项。

改动思路 / Approach

既有 session_runtime.py 持有键分类与首屏组合。TypedDict 为这些投影标注形状,链式迭代合并时间比较和 gate/等待计算。保持权限决策在原投影边界内,生产模块可以继续消除重复扫描;输入键快照则是行为约束。冷门 Mapping 可以保留一次快照,明确定义可直接读取的普通 dict 快路径。这样既能交付类型整理,也能保存现有调用者的输入语义。

具体改动 / Changes

四个路径中,生产文件净减 83 行,三个调用和验证文件净加 142 行。键分类单测新增 AST 预算、只读代理、重复键和空 gate 文本。Goal artifact 测试用普通 dict 构造动态变体,首屏 smoke 的 helper 接受 Mapping。诊断 envelope 导入原有 SOURCE_ID_KEYS,模块路径及常量保留。当前源码中投影 builder 的直接调用来自测试和公开 smoke,故这个反例界定已承诺的适配器接口,活跃宿主实际输入形状仍待核验。

验收依据为 docs/reference/protocols/session-runtime-loopx-projection-v0.md,固定版本为 57c4c43。 验收契约

  • SR-1。用紧凑身份和引用关联工作,保留安全的投影边界。 classify_session_runtime_key / _source_refs
  • SR-2。缺失宿主事实时显示空字段或明确等待,保留只读边界。 惰性 Mapping 产生未提供的 recommended_action 并被改写。
  • SR-3。公共示例保持合成数据。 session-runtime-readonly-projection-smoke.py

关键代码讲解

  • classify_session_runtime_key。精确键快路径保留整词、词序和隐私类别。重复键只分类一次。 源码
  • _as_mappings。旧实现复制每条记录的枚举键,新实现保留其 Mapping 对象。_first_text 的缺失键读取随后进入宿主的 getter。 源码
  • build_session_runtime_readonly_projection。合并等待与工作 lane 的决策,仍输出同一 JSON 形状。合成反例由首屏动作一路改变 must_attempt_work。 源码

对主干的风险 / Risks

长程风险是把空闲状态误投影为待继续工作。体验风险是把未提供的动作置于首屏。对候选版的风险结论限于实际复验的 Python 适配器入口。

让投影从枚举出来的字段快照读取,或仅对能证明读取等价的内置 dict 走快路径。把惰性 Mapping 与静态快照的完整投影和输入不变性放入回归测试。

PASS 115 项定向测试、只读投影和适配器文档两个公开 smoke,基线/候选的静态 MappingProxyType 对照一致,git diff --check 通过。FAIL 惰性 Mapping 的只读和缺失事实对照。UNVERIFIED 真实宿主使用同类 Mapping 的频率、全仓门禁、本机独立性能复测和已安装 App。没有查询 CI。

我的整体评价 / Verdict

当前 PR 的合理范围是类型和扫描整理,同时保留已接受的只读、缺失事实和工作 lane 语义。 现有状态词汇和发布授权边界继续有效。修复后应在同一输入上复跑反例、正常对照与相关源码测试,核对返回结果和状态均与契约一致。

English verdict: REQUEST_CHANGES at a1cc1ca. A lazy Mapping can mutate its input and turn a monitor projection into mandatory work. Preserve enumerated-key snapshot semantics.

@loopx-agent

Copy link
Copy Markdown
Collaborator Author

Addressed PRR_kwDOStEbEs8AAAABabgd-A ([P2]) at exact head 3ee0660ddee4474ac912f110541dc3e610616a2c: _as_mappings in loopx/session_runtime.py now snapshots every non-exact-builtin-dict Mapping via dict(item) from its enumerated keys (keeping zero-copy access strictly for exact builtin dict records), and tests/test_session_runtime_key_classification.py::test_lazy_mapping_preserves_enumerated_key_snapshot_and_input_invariance verifies full projection parity and input invariance across lazy Mapping, MappingProxyType(lazy), defaultdict, dict subclass .get() override, and static MappingProxyType controls.

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

动机 / Motivation

构建首屏只读投影(Session Runtime Readonly Projection)的控制面状态渲染器、目标制品工作观测(goal_artifact_work_observation)调用方,以及接入宿主紧凑记录的适配器开发与维护者。当只枚举 session_id 的惰性 Mapping(如缺失 recommended_action 时在 getitem/get 合成值的宿主适配器或 dict 子类)传入构建函数时:未做键快照前查询缺失键可能惰性改写宿主输入并把空闲会话误判为可继续执行的 advancement_task;在 3ee0660 中,_as_mappings 对非精确 builtin dict 的 Mapping 统一以 dict(item) 抓取已枚举键快照、对精确 builtin dict 走零拷贝快路径,使惰性 Mapping、MappingProxyType、defaultdict 与自定义 dict 子类全部稳定维持 monitor 与 must_attempt_work=False 且零输入变异。loopx/session_runtime.py 稳定在 543 行(0 AST Any、0 # type: ignore、0 # noqa),448 条精确 builtin dict 输入中位耗时从 1.204ms 降至 0.366ms(提速 3.29 倍),全部惰性 Mapping 变体经单测与反事实验证零输入变异且与 57c4c43 基线字节级对齐。本次 PR 不修改投影输出 schema、不改动 word-level 分类词表、不接入写入能力,也不改造下游目标制品观察测试本身未涉及的类型注解。

改动思路 / Approach

在既有的 loopx/session_runtime.py 单文件边界内以 TypedDict 和 Zero-Any 收窄替代 dict[str, Any],通过 item if type(item) is dict else dict(item) 把只读输入快照隔离到非精确 Mapping,既消除惰性 Mapping 键值合成风险,又保持精确 builtin dict 零拷贝性能与原有词级分类和优先级逻辑不变。严格限定在 loopx/session_runtime.py 的 TypedDict/Zero-Any 收窄、单遍键分类与 _as_mappings 快照边界修复,以及对应契约测试与调用类型对齐,不更改投影 schema、词级分类规则或跨文件架构。

从实现分层看,_as_mappings 在入口处统一把 6 组输入中的非精确 Mapping 快照为只读普通字典并过滤非 Mapping 项;随后 _classify_keys 通过单一 seen 集合单遍扫描全部唯一键;build_session_runtime_readonly_projection 则基于显式静态 TypedDict(SourceRefs、SessionRuntimeBoundary、SessionRuntimeFirstScreen、SessionRuntimeWorkLaneContract 等)组装只读首屏投影,保持零写回副作用与零原值拷贝。

具体改动 / Changes

  • 改动行分类: loopx/session_runtime.py(543 <= 550 行,0 AST Any,0 ignores/noqa)、tests/test_session_runtime_key_classification.py(441 行,新增 AST 预算检查与惰性 Mapping 快照矩阵,0 AST Any)、tests/control_plane/test_goal_artifact_work_observation.py(222 行,dict(...) 对齐非可变检查,0 AST Any)、examples/session_runtime/session-runtime-readonly-projection-smoke.py(385 行,assert_no_raw_values 收窄为 Mapping[str, object],保留原有 3 条 sys.path noqa: E402)。
  • 规范映射: 依据 docs/reference/protocols/session-runtime-loopx-projection-v0.md(baseline 57c4c43b5243003c6da1988730c49ac2267626f7):
    • c1_typed_word_level_classification(implemented):loopx/session_runtime.py#classify_session_runtime_key 与 _classify_keys 严格维持 word/sequence 级分类(COMPACT、RAW_MATERIAL、上限 24 的 UNCLASSIFIED)与显式安全碰撞例外(由 tests/test_session_runtime_key_classification.py 验证)。
    • c2_readonly_first_screen_and_snapshot_isolation(implemented):loopx/session_runtime.py#_as_mappings 与 build_session_runtime_readonly_projection 实现只读不落盘、原始值零拷贝以及任意非精确 Mapping 的枚举键快照隔离(由惰性 Mapping 矩阵单测与 examples/session_runtime/session-runtime-readonly-projection-smoke.py 验证)。

关键代码讲解

  1. _as_mappings (loopx/session_runtime.py#L207-216):采用 item if type(item) is dict else dict(item)(配合 isinstance(item, Mapping) 过滤)。精确 builtin dict 无自定义 __getitem__/.get() 副作用,故可直接零拷贝透传;而 MappingProxyType、自定义 Mapping、defaultdict 与覆盖 .get() 的 dict 子类一律经 dict(item) 预先提取已枚举键,彻底阻断后续缺失键查询触发惰性值合成。
  2. _classify_keys (loopx/session_runtime.py#L358-378):合并原本多遍遍历为单一去重循环,利用 seen: set[str] 跨 6 组输入只对每个唯一键调用一次 classify_session_runtime_key,按分类归集排序后的原始材料键名/类别及前 UNCLASSIFIED_KEY_LIMIT(24)个未分类键名。
  3. classify_session_runtime_key (loopx/session_runtime.py#L327-355):保持严格的优先级判定顺序(COMPACT_KEYS -> RAW_MATERIAL_KEYS -> 驼峰与分隔符拆分后精确词与词序列匹配 -> *_id/tokens 紧凑后缀 -> UNCLASSIFIED),确保 secret_id/api_key_id 被拦截而 catalog/tokenizer/trace_id 不误报。
  4. build_session_runtime_readonly_projection (loopx/session_runtime.py#L447-543):对 6 路输入统一过 _as_mappings,汇总阻断门禁、未完成 blocker、验证状态与 todo 优先级,保证有 raw_keys 或 blocking_gate 时强行关闭 agent_can_continue 与 must_attempt_work 并维持全 TypedDict 强类型返回。

对主干的风险 / Risks

  • Caller & 类型检查验证: 已排查全仓 6 个直接调用/测试文件,基线与当前头逐行 mypy 对比零新增回归(goal-attention-readmodel-smoke.py: 10->10,adapter-doc-smoke: 0->0,readonly-projection-smoke: 3->3,session_runtime.py: 0->0,test_goal_artifact_work_observation.py: 15->15,test_session_runtime_key_classification.py: 0->0);session_runtime.py 与 test_session_runtime_key_classification.py 在 mypy --strict 下 0 错误,[tool.mypy] 17 个默认目标 0 错误。
  • P2 修复闭合与失效突变校验: 针对 a1cc1ca9a9 遗留 [P2](6068641272)的最小修复已在 3ee0660dde 中用 exact-dict 检测 + non-dict 快照彻底堵住。手工注入回归突变验证:把 item if type(item) is dict else dict(item) 改回 item 后 test_session_runtime_key_classification.py 立刻失败(rc=1:1 failed, 95 passed);破坏 _contains_word_sequence 立刻失败(rc=1:54 failed, 42 passed)。
  • 陈旧提交批注提示: 合并前需注意旧提交 a1cc1ca9a9 上曾留有 6068641272 的 [P2] 评论,当前精确提交已实质修复但未越权 dismiss 旧评审,后续按合入关闭规则独立核实剩余阻塞状态即可。

我的整体评价 / Verdict

  • Outcome Impact: long_horizon = improved(_as_mappings 消除惰性宿主 Mapping 在空监控周期中误生成 advancement_task 的长程状态漂移风险,同时 448 条 builtin dict 投影中位耗时从 1.204ms 优化至 0.366ms,提速 3.29x);user_experience = preserved(首屏字段、门禁优先级与 Markdown 渲染与基线完全一致)。visual_review = not_applicable:仅重构 Python 只读投影内部类型、单遍扫描与输入键快照逻辑,未改动浏览器界面、视口排版或状态渲染样式(7 项 clean-HOME 投影/文档/状态 smoke 与成对输出均完全一致)。unverified_scope = none。
  • 等价性、体积与校验汇总: 基线 57c4c43b52 与精确提交 3ee0660dde 在 23 个键和 10 个投影场景(含 4 种惰性 Mapping)上成对实验 100% 等价(16,338 字节)。loopx/session_runtime.py(543 <= 550 行)与测试文件全部达成 0 AST Any 与 0 # type: ignore(smoke 仅保留原有 3 条 E402),顶层 loopx/*.py 数量与 ratchet 基线未增删。本地校验全部通过:ruff check(4/4 PASS)、mypy --strict(session_runtime.py+key_classification PASS)、[tool.mypy](17/17 PASS)、6 个关联文件 mypy 零新增错误、check_rfc_bilingual.py PASS、npm run typecheck:control-plane + 27 项 TS 测试 PASS、7 项 clean-HOME smokes PASS、loopx check 0 errors、canary premerge --profile standard 6/6 PASS、直接核心集 141 passed in 5.21s,以及 167 套件 pytest -n 6 -rs 4,195 passed, 16 skipped, 30 subtests passed(16 个跳过为 APFS/build-backend/npm-ci/Codex 环境预期跳过,0 failures)。CI 未被咨询(wait_for_ci: false)。无阻塞缺陷。

English verdict: APPROVE at 3ee0660.

@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

保持紧凑会话摘要的只读输入、键分类与工作门槛,验证旧提交的惰性 Mapping 回归已经消失。 基线从枚举键快照读取。旧候选 a1cc1ca 直接查询惰性缺失字段,可能写入原 Mapping 并生成虚假的待执行任务。 构建会话只读首屏并从已持久摘要读回状态的宿主适配器和 LoopX 状态使用者。

空闲会话出现虚假待执行动作会改变自动续跑判断,并修改原宿主输入。 精确头与基线对普通 dict、惰性 Mapping、MappingProxyType 和 defaultdict 返回相同的 monitor 投影,无缺失键查询和输入变异。真实 status 可读回明确的推进事项。 定向 pytest 基线 112、精确头 116 项通过。独立探针加载旧精确提交验证故障,再通过真实 status 从 SQLite 支持的隔离 Goal 读回 monitor、必要推进与禁止原始字段三种情况,持久源字节一致。git diff --check 与 merge-tree 通过。 源检出验证覆盖真实 CLI、SQLite 读回和指定 Mapping 类型。此次未独立测量作者性能比、真实宿主适配器、安装态或远程 CI。 基线契约

改动思路 / Approach

该改动沿用 build_session_runtime_readonly_projection → persisted run → loopx status → session_runtime_projection。保留同一 builder 和已部署的读侧形状,集中决定等待方和 lane,非普通记录仍从枚举键快照取得值。 每组输入只生成一次已枚举记录的快照。精确 builtin dict 在同步投影内读取;非精确 Mapping 包括非精确 dict 和只读包装都保留一次映射复制。稳定原时间及证据比较顺序。 原规则下只为非精确 dict 保留快照,避免为类型整理新增存储、兼容层或宿主协议。

具体改动 / Changes

精确提交 3ee0660,本次对照目标 57c4c43。自分叉点 57c4c43 比较,共 4 个文件,+444/-303。

关键代码讲解

  • _as_mappings。只对精确 dict 复用原记录,其余 Mapping 从枚举字段快照。 源码

  • classify_session_runtime_key。保留精确键、完整词和完整词组的分类优先级。 源码

  • build_session_runtime_readonly_projection。组合首屏、证据线索和原 work lane 边界。 源码

  • readonly-session-input-snapshot-and-typed-lane(implemented)。仅从紧凑且被枚举的输入事实生成运行状态,原始材料只记录类别,不拷贝原值或授予执行。

对主干的风险 / Risks

旧候选会缓存自造动作并使 must_attempt_work=true。当前精确头避免该副作用。真实 status 仍拒绝从含原始 transcript 字段的摘要产生执行许可,未显示值。 已验证旧问题的触发能力。实际回读时原始材料分支仍保留 advancement_task lane 名称,但 must_attempt_work=false,消费者需沿原布尔条件判断可执行性。此次未测性能增幅。 保持非精确映射的枚举键快照,继续在真实状态入口验证合法与拒绝分支。

我的整体评价 / Verdict

APPROVE。 旧头的快照 P2 已在当前精确提交上修复。普通记录和非精确 Mapping 的实测语义与基线一致,实际状态读取也保持工作与拒绝边界。旧问题记录保留在原提交。 旧头 P2 当前状态、可执行门槛和原始记录隔离仍需通过精确证据核验。

English verdict: APPROVE at 3ee0660. The original lazy-Mapping P2 is resolved. Independent snapshot checks and persisted status readback preserve idle, allowed-work, and raw-material rejection behavior.

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-session-runtime-zero-any-20261126 branch from 3ee0660 to b1a9ccf Compare November 27, 2026 16:27

@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

保持紧凑会话摘要的只读输入、键分类与工作门槛,验证旧提交的惰性 Mapping 回归已经消失。 基线从枚举键快照读取。旧候选 a1cc1ca 直接查询惰性缺失字段,可能写入原 Mapping 并生成虚假的待执行任务。 构建会话只读首屏并从已持久摘要读回状态的宿主适配器和 LoopX 状态使用者。

空闲会话出现虚假待执行动作会改变自动续跑判断,并修改原宿主输入。 精确头与基线对普通 dict、惰性 Mapping、MappingProxyType 和 defaultdict 返回相同的 monitor 投影,无缺失键查询和输入变异。真实 status 可读回明确的推进事项。 定向 pytest 基线 112、精确头 116 项通过。独立探针加载旧精确提交验证故障,再通过真实 status 从 SQLite 支持的隔离 Goal 读回 monitor、必要推进与禁止原始字段三种情况,持久源字节一致。git diff --check 与 merge-tree 通过。 源检出验证覆盖真实 CLI、SQLite 读回和指定 Mapping 类型。此次未独立测量作者性能比、真实宿主适配器、安装态或远程 CI。 基线契约

改动思路 / Approach

该改动沿用 build_session_runtime_readonly_projection → persisted run → loopx status → session_runtime_projection。保留同一 builder 和已部署的读侧形状,集中决定等待方和 lane,非普通记录仍从枚举键快照取得值。 每组输入只生成一次已枚举记录的快照。精确 builtin dict 在同步投影内读取;非精确 Mapping 包括非精确 dict 和只读包装都保留一次映射复制。稳定原时间及证据比较顺序。 原规则下只为非精确 dict 保留快照,避免为类型整理新增存储、兼容层或宿主协议。

具体改动 / Changes

精确提交 b1a9ccf,本次对照目标 57c4c43。自分叉点 57c4c43 比较,共 4 个文件,+444/-303。

关键代码讲解

  • _as_mappings。只对精确 dict 复用原记录,其余 Mapping 从枚举字段快照。 源码

  • classify_session_runtime_key。保留精确键、完整词和完整词组的分类优先级。 源码

  • build_session_runtime_readonly_projection。组合首屏、证据线索和原 work lane 边界。 源码

  • readonly-session-input-snapshot-and-typed-lane(implemented)。仅从紧凑且被枚举的输入事实生成运行状态,原始材料只记录类别,不拷贝原值或授予执行。

对主干的风险 / Risks

旧候选会缓存自造动作并使 must_attempt_work=true。当前精确头避免该副作用。真实 status 仍拒绝从含原始 transcript 字段的摘要产生执行许可,未显示值。 已验证旧问题的触发能力。实际回读时原始材料分支仍保留 advancement_task lane 名称,但 must_attempt_work=false,消费者需沿原布尔条件判断可执行性。此次未测性能增幅。 保持非精确映射的枚举键快照,继续在真实状态入口验证合法与拒绝分支。

当前 exact head 复核 / Rebased-head validation

PR #9471 已无冲突 rebase 到 origin/main 2302e42d9cad35a8a4150e24496ffecfbfdce8cb,当前精确头为 b1a9ccf1b0def6ce3efd16d865abcd52234635dc。本轮在新 base 上重新执行定向测试、静态检查和 diff 级 premerge。

最终 exact head 复核:116 项 session-runtime 与 Goal artifact observation 测试通过;核心文件严格 mypy、Ruff、diff 检查和标准 premerge 的 15 项选定检查全部通过。惰性 Mapping、defaultdict、MappingProxyType 和重复键语义保持。

我的整体评价 / Verdict

APPROVE。 旧头的快照 P2 已在当前精确提交上修复。普通记录和非精确 Mapping 的实测语义与基线一致,实际状态读取也保持工作与拒绝边界。旧问题记录保留在原提交。 旧头 P2 当前状态、可执行门槛和原始记录隔离仍需通过精确证据核验。

English verdict: APPROVE at b1a9ccf. The original lazy-Mapping P2 is resolved. Independent snapshot checks and persisted status readback preserve idle, allowed-work, and raw-material rejection behavior.

@loopx-agent
loopx-agent merged commit f466201 into main Nov 27, 2026
18 of 22 checks passed
@loopx-agent
loopx-agent deleted the codex/slim-session-runtime-zero-any-20261126 branch November 27, 2026 16:33
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