Skip to content

refactor(chat): move shared turn prompt to loopx.chat, deduplicate chat_agent, and retire ceiling - #8905

Open
loopx-agent wants to merge 1 commit into
mainfrom
codex/chat-agent-slim-20261117
Open

loopx-agent wants to merge 1 commit into
mainfrom
codex/chat-agent-slim-20261117

Conversation

@loopx-agent

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

Copy link
Copy Markdown
Collaborator

Summary

Slims loopx/chat_agent.py from 2,042 lines to 1,797 lines (-245 lines; -28 net lines across 13 files, +337/-365), retires its >2,000-line waiver in loopx/canary/module_metric_baseline.json, and places shared turn prompt composition cleanly in loopx/chat.py (1,298 -> 1,527 lines, preserving origin/main's #9072 wording and reply_image_directory parameter verbatim).

Delivery Brief

  • Goal / Source: Long-term module size & deduplication maintenance (module_metric_ceilings retirement for >2,000-line Python modules under the maintainability ratchet).
  • Current Gap: loopx/chat_agent.py carried a 2,047-line ceiling waiver (2,042 lines on origin/main) while owning cross-adapter prompt constants/composition (_turn_prompt, CONVERSATION_INTENT_RESOLUTION_INSTRUCTION, TRUSTED_OWNER_DIRECT_WORK_INSTRUCTION, WORK_RESULT_VERIFICATION_INSTRUCTION, COLLABORATION_INPUTS_GUIDANCE) imported by ACP, DSH, native chat, manager, providers, and evaluation suites, alongside duplicated stream-reader logic in loopx/chat_acp.py and repeated turn-loop helpers inside CodexChatAgentSession.
  • Observable Result:
    • loopx/chat_agent.py drops below the 2,000-line default limit (2,042 -> 1,797 lines) and removes "loopx/chat_agent.py": {"lines": 2047} from loopx/canary/module_metric_baseline.json.
    • Shared turn prompt constants and _turn_prompt(...) (preserving origin/main's #9072 prose and reply_image_directory: Path | None = None) move byte-identically into loopx/chat.py (1,298 -> 1,527 lines, alongside CHAT_RESPONSE_EXAMPLE_SUFFIX and parse_agent_response), with cross-provider callers and tests importing directly from loopx.chat.
    • loopx/chat_acp.py reuses chat_agent._reader(..., eof_message="ACP stdio stream closed") instead of duplicating the stdio stream reader thread function (603 -> 593 lines).
    • Internal duplication in CodexChatAgentSession is consolidated via _unwrap_stream_message (typed dict[str, Any] | Exception -> dict[str, Any]), _validated_command_item_id, _turn_deadline, emit_visible, raise_turn_failure, parameterized startup catalog checks, and removal of a dead duplicate _check_server_gate call while preserving chat_agent.{shutil,subprocess,time,sys,os} monkeypatch seams and origin/main's native_turn_response / reply_image_directory flow.
  • Owning Boundary: loopx/chat.py owns shared chat response/prompt text helpers (_turn_prompt and prompt constants); loopx/chat_agent.py owns the Codex app-server transport/session adapter; loopx/chat_acp.py owns ACP transport and reuses _reader.
  • Bounded Refactoring & Prompt Parity: Pure structural placement and deduplication; zero new flags, zero config surfaces, and byte-for-byte identical _turn_prompt / prompt constants verified across origin/main (b927e20bfb) vs HEAD (f81bb5fad9).

Verification

Rebased cleanly onto origin/main (b927e20bfb) at head f81bb5fad928428e291ce9ebed23ebbacbc3360d:

  • pytest -n 3 -q -rs across 24 chat, architecture, maintainability ratchet, and CLI-entrypoint suites (tests/test_chat_agent.py, tests/test_chat_intake_evaluation.py, tests/test_manager_context_execution.py, tests/architecture/test_content_digest_single_owner.py, tests/architecture/test_top_level_module_budget.py, tests/canary/test_maintainability_ratchet.py, tests/test_chat_acp_startup_timeout.py, tests/test_chat_internal_module_layout.py, tests/test_chat_response_parsing.py, tests/test_chat_transport_composition.py, tests/test_chat_activity_provider_tokens.py, tests/test_chat_dsh_adapter.py, tests/test_antigravity_steward_adapter.py, tests/test_chat_codex_auth.py, tests/test_chat_ordinary_project.py, tests/test_chat_long_running.py, tests/test_chat_activity.py, tests/test_chat_session_active_turn.py, tests/test_cli_entrypoint.py, tests/test_chat_runtime_preparation_recovery.py, tests/test_chat_turn_steering.py, tests/test_chat_codex_history.py, tests/test_chat_manager_context.py, tests/test_chat_manager_inspection.py) -> 1896 passed, 6 skipped (6 explicit installed native SDK tests in test_chat_codex_auth.py)
  • pytest -q tests/test_lark_private_conversations.py -k "reply_image or test_recovery_waits_for_inflight_capture_without_readmitting" -> 18 passed, 557 deselected (standalone run; an earlier -n 4 batch run hit 1 transient LockAcquireTimeoutError under parallel load that passed cleanly on standalone re-run)
  • Verbatim _turn_prompt / prompt constants SHA-256 parity and _reader default/ACP EOF/JSONDecodeError parity vs origin/main (b927e20bfb) -> ALL MATCH
  • examples/evaluations/chat-intake.py --help, examples/loopx-chat-acp-adapter-smoke.py, examples/loopx-chat-provider-adapters-smoke.py, examples/loopx-chat-runtime-smoke.py -> all ok (--live gated for paid runs)
  • ruff check across all 12 changed .py files -> All checks passed!
  • mypy --strict --follow-imports=silent loopx/chat_agent.py -> zero errors in touched lines (repo-configured python -m mypy reports 1 pre-existing unrelated error in loopx/control_plane/work_items/interaction_contract.py:1004)
  • npm run typecheck:control-plane -> passed
  • loopx check --scan-path ... (13 modified files) -> ok: true, errors: 0, public boundary scan clean: 13 files
  • loopx change-quality verify --goal-id personal-loopx-maintenance -> cqr_81a6bd17b3862a1b6ccd (decision: pass, status: valid, scope_fingerprint: 81a6bd17b3862a1b6ccdabe8644ff217bfb90ad2a649f0e54a8439a64996f970)
  • loopx canary premerge --tier quick --from-git-diff --git-diff-base origin/main --goal-id personal-loopx-maintenance -> ok: true, merge_gate_passed: true, direct_failure_count: 0, failure_count: 0
  • git diff --check origin/main...HEAD -> exit 0; DCO Signed-off-by verified on f81bb5fad9

@loopx-agent
loopx-agent force-pushed the codex/chat-agent-slim-20261117 branch from 0d1e535 to 5ff27b9 Compare November 17, 2026 04:44
@mergify

mergify Bot commented Nov 17, 2026

Copy link
Copy Markdown

This pull request has merge conflicts with main and cannot be merged
until they are resolved. Please rebase or merge the base branch, @loopx-agent.

Choose the remote for the base repository, not an out-of-date fork.
For a fork clone, first inspect git remote -v; upstream must point
to https://github.com/loopx-project/loopx.git. If it is absent, add it
with git remote add upstream https://github.com/loopx-project/loopx.git.
Then run:

git fetch upstream
git rebase upstream/main
# Resolve each conflict, git add the resolved files, then git rebase --continue.
git push --force-with-lease origin HEAD

For a same-repository clone whose origin points to
https://github.com/loopx-project/loopx.git, use origin instead of
upstream for fetch/rebase. If you prefer merging the base, use
git merge <base-remote>/main and push normally.

Keep the DCO Signed-off-by trailer on every commit when you rebase.
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase Mergify: the pull request has merge conflicts with its base branch label Nov 17, 2026
@loopx-agent
loopx-agent force-pushed the codex/chat-agent-slim-20261117 branch from 5ff27b9 to d5f8892 Compare November 17, 2026 17:23
@mergify mergify Bot removed the needs-rebase Mergify: the pull request has merge conflicts with its base branch label Nov 17, 2026
@loopx-agent
loopx-agent force-pushed the codex/chat-agent-slim-20261117 branch from d5f8892 to 45dbb61 Compare November 17, 2026 20:21

@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

LoopX Chat 多前端适配(ACP、DSH、Manager、Antigravity、Chat Providers)与可维护性基线治理。
rebase 前停留在 0514512 旧基线;同时 chat_agent.py 高达 2,042 行并在 module_metric_baseline.json 中保留 2047 行特例上限,多处适配器耦合导入 chat_agent._turn_prompt / 常量,且 chat_acp.py 存在重复 _reader 实现。本次 rebase 至 origin/main 后,将 shared prompts/guidance 与 _turn_prompt(共 216 行)原字面无改动迁回既有公开 owner loopx/chat.py,复用 chat_agent._reader(新增 eof_message 参数)删去 chat_acp._reader 重复定义,将 chat_agent.py 瘦身至 1,806 行并移除 baseline 2047 行豁免条目(13 files, +328/-356,净减 28 行)。
将 shared chat prompt 与 _turn_prompt(216 行原字面等价迁移)归位至 loopx/chat.py、合并 _reader 重复实现,使 chat_agent.py 从 2,042 行降至 1,806 行并退休 module_metric_baseline.json 中 2047 行豁免。
非目标边界:本 PR 不修改任何模型提示词语义(已逐字对比 216 行完全一致),亦不顺带修复 origin/main 既有的 test_chat_operation_actions.py 遗留 activation_state 固件问题(另案 #8946 处理)。

改动思路 / Approach

复用已有公开入口 loopx/chat.py 作为共享对话引导与 _turn_prompt 的单点 owner,同时参数化 _reader 消除 ACP 重复实现并退休 chat_agent.py 的 2047 行豁免基线。
当前 PR 边界:涉及 loopx/chat*.py、capabilities/native_chat、examples/evaluations/chat-intake.py、baseline JSON 与 3 处测试导入更新(13 files, +328/-356)。

  • 单点归属与零词义漂移:经程序比对,从 loopx/chat_agent.py 移至 loopx/chat.py:1257-1472 的 216 行指令常量及 _turn_prompt 与基线逐字节完全一致(verbatim=True),并同步更新所有调用方与评测脚本导入,无冗余 compat shim。
  • 流读去重与基线收紧:给 loopx/chat_agent.py::_reader 增加 eof_message 参数以删去 loopx/chat_acp.py::_reader 重复函数;chat_agent.py 从 2,042 行降至 1,806 行后直接移除 module_metric_baseline.json 中 2047 行特例条目。

具体改动 / Changes

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

  • CHAT-OWNER-CONSOLIDATION 为 implemented:Move shared chat prompt helpers into loopx/chat.py verbatim without prose alteration.(loopx/chat.py,verbatim match=True + 599 passed)
  • CHAT-AGENT-CEILING-RETIRED 为 implemented:Slim loopx/chat_agent.py below 2000 lines and retire module_metric_baseline.json ceiling.(loopx/chat_agent.py,1806 lines + canary premerge 4/4 passed)

关键代码讲解

  1. loopx/chat.py:1257(CONVERSATION_INTENT_RESOLUTION_INSTRUCTION / _turn_prompt):承接共享 Chat 对话引导与 _turn_prompt 唯一归属(原字面 216 行完全一致)
  2. loopx/chat_agent.py:391(_reader):增加 eof_message 参数供 chat_acp.py 复用,消除重复 stdio 读循环
  3. loopx/canary/module_metric_baseline.json:36(loopx/chat_agent.py ceiling):移除 lines=2047 特例豁免,收敛回默认 2000 行阈值(实测 1,806 行)

对主干的风险 / Risks

已同步 rebase 至 origin/main(ef099ae3ac),旧基线导致的 test_top_level_module_budget 失败已清除;已更新全部内部及评测脚本调用点,核心 599 项测试与 premerge 4 项 canary 全绿。

验证项 级别 结果 实跑输出(exact head 45dbb619d83d2c211cf0276de61d671d870a44c7)
pytest -q tests/test_chat_agent.py tests/test_chat_intake_evaluation.py tests/architecture/test_top_level_module_budget.py tests/canary/test_maintainability_ratchet.py required PASS 599 passed in 31.44s
python3 verbatim check + ruff check 11 touched files required PASS verbatim=True, ruff All checks passed!
loopx canary premerge --from-git-diff --git-diff-base origin/main --tier quick required PASS selected=4 failures=0 ok=true

我的整体评价 / Verdict

对精确 head 45dbb619d83d2c211cf0276de61d671d870a44c7 的评价:用户体验影响 not_applicable(仅内部模块归位与导入路径调整,未改动 216 行提示文本或任何用户界面/CLI 行为(inspected: loopx/chat.py, loopx/chat_agent.py)。,unverified_scope: none),长程稳定度 improved。完成 ef099ae3ac 重基与净减 28 行整合(chat_agent.py 降至 1,806 行并退休 2047 基线豁免),599 项单测与 4 项 canary 实跑全过,无未授权词义更改(compatibility_assessment=not_applicable,semantic_alignment=not_applicable),同意合入。

English verdict: APPROVE

@mergify

mergify Bot commented Nov 18, 2026

Copy link
Copy Markdown

This pull request has merge conflicts with main and cannot be merged
until they are resolved. Please rebase or merge the base branch, @loopx-agent.

Choose the remote for the base repository, not an out-of-date fork.
For a fork clone, first inspect git remote -v; upstream must point
to https://github.com/loopx-project/loopx.git. If it is absent, add it
with git remote add upstream https://github.com/loopx-project/loopx.git.
Then run:

git fetch upstream
git rebase upstream/main
# Resolve each conflict, git add the resolved files, then git rebase --continue.
git push --force-with-lease origin HEAD

For a same-repository clone whose origin points to
https://github.com/loopx-project/loopx.git, use origin instead of
upstream for fetch/rebase. If you prefer merging the base, use
git merge <base-remote>/main and push normally.

Keep the DCO Signed-off-by trailer on every commit when you rebase.
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase Mergify: the pull request has merge conflicts with its base branch label Nov 18, 2026
@loopx-agent
loopx-agent force-pushed the codex/chat-agent-slim-20261117 branch from 45dbb61 to 576e41b Compare November 18, 2026 18:21
@mergify mergify Bot removed the needs-rebase Mergify: the pull request has merge conflicts with its base branch label Nov 18, 2026

@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

Chat 适配器维护者与控制面治理人员在扩展或调整多端对话运行时(Codex app-server、ACP、DSH、Antigravity、Manager、Chat Providers 及评估脚本)时,长期面临单文件耦合与模块体积失控问题。在重基前 origin/main(41ea75e898)上,loopx/chat_agent.py 达到 2,043 行并依赖 loopx/canary/module_metric_baseline.json 中 lines: 2047 的特例豁免,同时跨端提示词与 ACP _reader 重复耦合;修改后在精确 head 576e41bab3 上,共享提示词常量与 _turn_prompt 原字面迁至 loopx/chat.py(1,496 行),loopx/chat_acp.py 复用 chat_agent._reader,loopx/chat_agent.py 降至 1,798 行并移除豁免。此前 loopx/chat_agent.py 一方面承担 Codex app-server 子进程生命周期与 Turn 调度,另一方面又作为跨端共享提示词常量与 _turn_prompt 的事实来源,迫使 chat_acp.py、chat_dsh.py、chat_manager.py、chat_providers.py、antigravity_adapter.py、external_conversations.py 以及 examples/evaluations/chat-intake.py 和多项测试反向耦合 loopx.chat_agent;同时 loopx/chat_acp.py 重复维护了一套几乎完全相同的 stdio _reader 线程函数,CodexChatAgentSession 内部也存在流消息解包、命令 item 校验、超时截止计算与 Turn 错误构造等多处重复分支。若放任不管,任何新增防护逻辑都会推高 chat_agent.py 体积并迫使豁免继续膨胀。本次改动将共享提示词常量与 _turn_prompt 原字面迁至既有公开对话契约模块 loopx/chat.py,让 loopx/chat_acp.py 复用 chat_agent._reader,收敛 CodexChatAgentSession 内部重复逻辑并移除 module_metric_baseline.json 中 chat_agent.py 的豁免上限。非目标边界为不改写任何提示词语义或控制面策略、不增加公开配置项或 compat shim,也不顺带修复主干其他模块既有的可维护性告警。

改动思路 / Approach

本 PR 的架构判定与范围边界明确:将跨端共享提示词文本与信封解析集中到既有公开模块 loopx/chat.py(而不新增顶层模块),并通过给 chat_agent._reader 增加 eof_message 参数与收敛 CodexChatAgentSession 内部重复逻辑,以净减 28 行且零新抽象的最小成本消除跨适配器反向依赖与 2047 行基线豁免。当前 PR 边界:仅调整共享提示词归属、复用 _reader 并合并 CodexChatAgentSession 局部重复逻辑,不引入新顶层模块、新配置开关或兼容重导出垫片。仓库里 loopx/chat.py 已持有 CHAT_RESPONSE_EXAMPLE_SUFFIX、parse_agent_response 与 VisibleResponseStreamFilter,是跨提供方共享对话文本与响应信封契约的天然单点 owner;因此将 CONVERSATION_INTENT_RESOLUTION_INSTRUCTION、TRUSTED_OWNER_DIRECT_WORK_INSTRUCTION、WORK_RESULT_VERIFICATION_INSTRUCTION、COLLABORATION_INPUTS_GUIDANCE 及 _turn_prompt(完整保留 origin/main 新增的 reply_image_directory: Path | None = None 参数与对应引导句)迁入 loopx/chat.py:1272-1496(1,267 -> 1,496 行,远低于 2,000 行默认阈值)最为合理。同时 loopx/chat_agent.py 与 loopx/chat_acp.py 的 stdio JSON 行读取循环完全同构,仅在流关闭时的 EOFError 文案不同,因此通过给 chat_agent._reader 增加带默认值 "Codex app-server stream closed" 的关键字参数 eof_message,即可由 chat_acp.py 直接传入 "ACP stdio stream closed" 复用,删除 16 行重复实现(603 -> 593 行)。在 CodexChatAgentSession 内部,合并 _unwrap_stream_message、_validated_command_item_id、record_command、_turn_deadline、emit_visible、raise_turn_failure 及启动模型参数校验循环,并删除 if message.get("id") != request_id: 分支内永远无法触发的重复 _check_server_gate(message) 检查(前置第 1227 行已覆盖所有 message.get("method") 情形),既保留 chat_agent.{shutil,subprocess,time,sys,os} 的既有测试 monkeypatch 接缝,也将 chat_agent.py 压降至 1,798 行(-245 行),从而安全退休 module_metric_baseline.json 中的特例豁免。正反路径均保持不变:不同 runtime_profile / execution_mode / project_work / reply_image_directory 组合输出逐字节一致;流关闭、非法 JSON、未绑定命令 item、超时和服务端门控错误路径与基线完全相同。

模块 / 基线项 origin/main(41ea75e898) 精确 head(576e41bab3) 变化说明
loopx/chat_agent.py 2,043 行(依赖 2,047 豁免) 1,798 行(无豁免) -245 行;迁出共享提示词并合并内部重复逻辑
loopx/chat.py 1,267 行 1,496 行 +229 行;承接 4 个共享常量与 _turn_prompt(含 reply_image_directory)
loopx/chat_acp.py 603 行 593 行 -10 行;删去本地 _reader 改为复用 chat_agent._reader
loopx/canary/module_metric_baseline.json 含 loopx/chat_agent.py: {"lines": 2047} 已移除 退休特例豁免,回归默认 2,000 行上限

具体改动 / Changes

规范依据(spec_basis):no_spec(spec_source: none)——本次为长期模块体积与去重维护重构,无独立书面 RFC 或新增功能规范文件,交付判断依据 problem_context 的可维护性基线收敛与行为零漂移要求,已核对 loopx/canary/maintainability_ratchet.py(MODULE_LINE_LIMIT = 2000)与 tests/architecture/test_top_level_module_budget.py 现有门禁。全量 diff 覆盖 13 个文件(+337/-365,净 -28 行),其中运行时产品代码 8 个、测试与评估脚本 4 个、基线配置 1 个。

关键代码讲解

  1. loopx/chat.py:1318(_turn_prompt 及 1272-1315 四项共享提示常量):完整承接原 loopx/chat_agent.py 中的 CONVERSATION_INTENT_RESOLUTION_INSTRUCTION、TRUSTED_OWNER_DIRECT_WORK_INSTRUCTION、WORK_RESULT_VERIFICATION_INSTRUCTION、COLLABORATION_INPUTS_GUIDANCE 与 _turn_prompt(...),并完整保留 origin/main 新增的 reply_image_directory: Path | None = None 参数及保存/引用图片提示分支。上游调用方 chat_agent.py、chat_acp.py、chat_dsh.py、chat_manager.py、chat_providers.py、antigravity_adapter.py、external_conversations.py、examples/evaluations/chat-intake.py 及 3 个测试文件全部直连 loopx.chat;经基线与精确 head 逐字节比对(4 种典型模式 + 4 个常量),SHA-256 完全一致。
  2. loopx/chat_agent.py:390(_reader)与 loopx/chat_acp.py:109(ACPStdioAdapter.start):为 _reader(stream, messages, *, eof_message: str = "Codex app-server stream closed") 增加只读关键字参数 eof_message,保持 Codex app-server 及既有单测调用零修改兼容;chat_acp.py 移除重复 _reader 定义并通过 kwargs={"eof_message": "ACP stdio stream closed"} 复用同一条流读取、JSON 解析、非 dict 跳过与 EOFError 投递逻辑。
  3. loopx/chat_agent.py:1017(_unwrap_stream_message)、1331(_validated_command_item_id)与 1757(_turn_deadline):将 _next_message 与 _request 中重复的 EOFError/Exception 运行时错误转换收拢至 _unwrap_stream_message;将 background_turn_ids 与 _command_item_ids 中重复的 commandExecution 校验收拢至 _validated_command_item_id 与局部 record_command;将 send() 循环内前置判断与 except CodexChatAgentError: 重查收拢至 _turn_deadline,并用 emit_visible、raise_turn_failure 和参数化 (model, "model", "model") / (reasoning_effort, "reasoningEffort", "reasoning effort") 循环替换重复块;同时移除第 1235 处被前置 if message.get("method") and self._check_server_gate(message) 严格支配的死检查。
  4. loopx/canary/module_metric_baseline.json(第 68-69 行移除 loopx/chat_agent.py 条目):在确认 loopx/chat_agent.py 降至 1,798 行(any_count: 50 <= 300、dict_any_count: 0 <= 300)且 loopx/chat.py 为 1,496 行后移除豁免,使 collect_module_metric_findings 返回空列表。

对主干的风险 / Risks

本次审查未发现阻塞合入的问题(no blocking finding)。对主干的主要潜在影响面与核查结论如下:

  • 内部跨模块导入迁移完整性:已在 origin/main 全仓检索 _turn_prompt、4 个指令常量及 _reader 的所有引用点,除已更新的 12 个源码/测试/评估文件外,其余 tests/test_chat_ordinary_project.py(导入保留在 chat_agent.py 中的 CHAT_REVIEW_OPEN_TAG / CHAT_REVIEW_CLOSE_TAG)与 tests/test_chat_codex_auth.py(调用保留在 chat_agent.py 中的 _reader)均不受影响。外部下游若直接导入私有符号 loopx.chat_agent._turn_prompt 将收到 ImportError,但该符号带前导下划线且仅作内部共享使用,不设冗余兼容垫片符合仓库简洁原则。
  • 重基冲突合入准确性:origin/main 近期合入了 native_turn_response 与 reply_image_directory(用于 Turn 回复图片目录提示),本次重基已将其完整合并进 loopx/chat.py:_turn_prompt,并额外运行 test_lark_private_conversations.py -k reply_image(16 passed)确认图片目录提示端到端通过。
  • 失败路径与不变式覆盖:CodexChatAgentSession 合并辅助函数保持异常类型、错误码(hard_timeout、idle_timeout、unsupported_configuration、host_gate)和 native_error_info 属性赋值一致(注意 raise_turn_failure 在 params 非 dict 时传给 _retry_error_details(None) 安全返回 {})。全仓 default_off_isolation 与 authority_semantics 均属 not_applicable(无新增开关、权限模型或外部协议状态)。
  • 既有主干红灯归因:tests/canary/test_maintainability_ratchet.py 与 control-plane-maintainability-ratchet-smoke.py 报告的 2 项未评审 oversized_decision_function(loopx/control_plane/heartbeat/builder.py:build_heartbeat_prompt 与 loopx/control_plane/work_items/interaction_contract.py:_admitted_interaction_next_cli_actions)在 origin/main(41ea75e898)与精确 head(576e41bab3)上完全一致,canary premerge 已自动归类为 advisory_inherited_failure(inherited_baseline_failure: true),与本 PR 变更路径零交集。test_chat_codex_auth.py 中 6 项标记为 explicit installed native SDK test 的用例按设计跳过;examples/evaluations/chat-intake.py --live 付费模型实测未运行,为本 PR 最强未执行外部验证项,但不影响本次纯结构搬迁的确定性验证。
验证项 级别 结果 实跑输出(exact head 576e41bab3d709b8099343c757aaaa82049475f4)
核心与适配器回归(10 个 pytest 套件:test_chat_agent.py、test_chat_intake_evaluation.py、test_manager_context_execution.py、test_content_digest_single_owner.py、test_top_level_module_budget.py、test_chat_acp_startup_timeout.py、test_chat_internal_module_layout.py、test_chat_response_parsing.py、test_chat_transport_composition.py、test_chat_activity_provider_tokens.py) required PASS 960 passed in 115.32s
回复图片目录与其余 Chat 适配器(test_lark_private_conversations.py -k reply_image + test_chat_dsh_adapter.py、test_antigravity_steward_adapter.py、test_chat_codex_auth.py、test_chat_ordinary_project.py、test_chat_long_running.py、test_chat_activity.py) required PASS 16 passed, 555 deselected + 250 passed, 6 skipped(6 项 explicit installed native SDK test)
基线 vs 精确 head _turn_prompt / _reader 逐字节与错误分支对拍 + chat-intake.py --help required PASS 4 模式与 4 常量 SHA-256 完全匹配(match=True);_reader 默认/ACP EOF 文案与 JSONDecodeError 路径匹配
ruff check(12 个变更 .py)+ mypy(配置严格集)+ npm run typecheck:control-plane required PASS All checks passed!,Success: no issues found in 17 source files,tsc --noEmit 通过
loopx check --scan-path ...(13 个变更文件)+ 3 项 Chat smoke required PASS ok: true, errors: 0(public boundary scan clean: 13 files);ACP/provider/runtime smokes 全 ok
loopx canary premerge --from-git-diff --git-diff-base origin/main --tier quick + git diff --check required PASS merge_gate_passed: true(direct_failure_count: 0, run_failure_count: 0, advisory_failure_count: 1 属主干既有告警)
tests/canary/test_maintainability_ratchet.py(对照 origin/main 与 HEAD) diagnostic FAIL pre_existing_unrelated:主干 41ea75e898 与 576e41bab3 均因未触及的 heartbeat/builder.py 与 interaction_contract.py 失败;chat_agent.py 指标告警已为 0

我的整体评价 / Verdict

对精确 head 576e41bab3d709b8099343c757aaaa82049475f4 的评价:long_horizon 判定为 improved(消除了 loopx/chat_agent.py 的 2,047 行特例豁免并将其压降至 1,798 行,同时切断跨适配器对传输层模块的反向提示词依赖,降低后续长程演进摩擦);user_experience 与 visual_review 均判定为 not_applicable(原因:纯内部模块放置与去重重构,未改变任何渲染 UI、CLI 命令、用户交互文案或模型提示文本,已核对 loopx/chat.py、loopx/chat_agent.py 与 loopx/chat_acp.py,unverified_scope: none)。observable_semantics 判定为 equivalent(4 种运行模式的 _turn_prompt、4 个提示常量及 _reader 默认与自定义 EOF 路径均与 origin/main 41ea75e898 逐字节等价)。code_volume 判定为 necessary(净减 28 行、compatibility_assessment=not_applicable:私有 _turn_prompt 与常量无外部稳定接口义务,内部调用方已一并迁移)。change_proportionality 判定为 proportionate(architecture_assessment=retain),default_off_isolation 与 authority_semantics 均为 not_applicable,semantic_alignment 为 not_applicable(未新增、扩展或重定义任何受控词表、状态枚举或持久化 schema)。本地全套必选单测、静态检查、类型检查、公开边界扫描与 canary premerge 均在精确 head 576e41bab3d709b8099343c757aaaa82049475f4 实跑通过,剩余唯一非阻断残余风险为未运行 examples/evaluations/chat-intake.py --live 付费模型实跑与外部私有下游导入私有 _turn_prompt 的假设;如需重新审查,仅当后续改动修改共享提示内容或传输层流读取逻辑时才需重跑对拍。

English verdict: APPROVE — Exact head 576e41bab3d709b8099343c757aaaa82049475f4 cleanly rebases onto origin/main (41ea75e898), preserves reply_image_directory verbatim in loopx/chat.py, shrinks loopx/chat_agent.py from 2,043 to 1,798 lines to retire its module_metric_baseline.json ceiling, consolidates _reader and session helpers without behavior drift, and passes local unit, static, typecheck, public-boundary, and quick premerge gates with zero blocking findings.

@mergify

mergify Bot commented Nov 19, 2026

Copy link
Copy Markdown

This pull request has merge conflicts with main and cannot be merged
until they are resolved. Please rebase or merge the base branch, @loopx-agent.

Choose the remote for the base repository, not an out-of-date fork.
For a fork clone, first inspect git remote -v; upstream must point
to https://github.com/loopx-project/loopx.git. If it is absent, add it
with git remote add upstream https://github.com/loopx-project/loopx.git.
Then run:

git fetch upstream
git rebase upstream/main
# Resolve each conflict, git add the resolved files, then git rebase --continue.
git push --force-with-lease origin HEAD

For a same-repository clone whose origin points to
https://github.com/loopx-project/loopx.git, use origin instead of
upstream for fetch/rebase. If you prefer merging the base, use
git merge <base-remote>/main and push normally.

Keep the DCO Signed-off-by trailer on every commit when you rebase.
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase Mergify: the pull request has merge conflicts with its base branch label Nov 19, 2026

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

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

Reviewed head 576e41bab3d709b8099343c757aaaa82049475f4

Reviewer: model_agent · gpt-6.2-sol · OpenAI (runtime_reported, max)

当前提交未发现阻塞性的代码问题。 真实 stdio 对照覆盖了正常响应、故障和超时。当前 PR 与主干有冲突,合并前要重新核对提示词。

动机 / Motivation

维护 Chat 的开发者需要让 Codex、ACP 和其他 adapter 使用同一份会话说明,避免调整一个 provider 时牵连其他会话。

旧版在构造会话提示时让 ACP 和管理入口依赖 Codex adapter。新版由共享 Chat 模块构造提示,Codex 和 ACP 以各自原有的方式处理读流结束和出错。

实跑 Codex 与 ACP 的请求后,基线与候选的提示词摘要、可见回答和错误观测逐项相同,Codex adapter 的行数降到通用上限内。

本轮核对源码检出的会话与本地宿主传输。真实模型的指令采纳和安装版 SDK 留待相应环境验证。

改动思路 / Approach

共有提示词归到 Chat,保持一个改动入口。共享的 reader 只解析和排队,实际 deadline、取消与错误归类由各自 adapter 负责。 本次只迁移共享会话文本、复用 JSONL 读取与收束 Codex Turn 内部重复。原有消息格式、权限与持久化接口继续由各自 owner 负责。

旧调用方已经共享会话意图、结构化响应示例和回复检查规则。迁移把这些内容放在各 provider 都会使用的 Chat 模块内。ACP 沿用原来的协议消息和错误,由现有 adapter 读取共享 helper 返回的 JSON 对象或异常。共享提示词 ACP reader

具体改动 / Changes

  • 普通会话、可信 owner 和确认执行的请求分别经过真实 adapter 和合成上游。比较了完整提示词摘要、可见流、最终回复、错误类型和诊断。图片输出只在本来具备对应 Turn 路径的用例出现。移除确认执行的结果验证指令时,同一条实际调用的断言失败。
  • EOF 和坏帧在请求与事件两阶段保持各自诊断。两种结构化失败均保留原错误类别,已有局部输出也只作为增量。空闲与硬超时保留各自的错误码。线程和命令的归属仍由既有检查验证。
  • 依据 docs/development/testing-and-quality.md @ 41ea75e89892ed88d0ae543e97e31aad0a181c47,real-path-parity 的 14 条实际传输对照完全一致,parity-sensitivity 的指令遗漏反例能被拒绝。另对照了 78 个含终端归属和 workspace 作用域的用例,两边通过。验证规范 Codex 流错误处理 deadline

对主干的风险 / Risks

标准 premerge 和三个本地 adapter/runtime smoke 通过。全量 maintainability 提示来自两个未修改的函数,在相同基线与 head 的报表中名称和指标一致。覆盖较广的 pytest 首次因检出缺少可选 routing 包失败一项,补齐同版包后该项在基线和候选均通过。16 个本地 Lark 图片回复用例通过,6 个安装版 SDK 用例跳过,托管 CI 未查询。

当前 main 6a0f8bc17b439f1e5fa1d389ad5a87faf7d73b25 的 #9072 已修改同一段共享提示词。解决 chat_agent.py 冲突时须迁移主干现有文本,再按相同方式核对新提交。模型实际采纳指令的效果仍待单独验证。当前主干提示词 #9072

我的整体评价 / Verdict

这次共享所有权调整有实际调用方和故障对照支撑。我认可当前提交,仍需整合主干并复审整合后的提交。改动落在会话指令、provider 传输及其诊断,没有修改页面渲染或布局。用协议和本地回读验证了可见消息和图片指令。

English verdict: APPROVE. The reviewed head preserves the exercised prompt, stream, and error contracts and retires the Codex adapter line waiver. Resolve the separate main conflict without restoring old guidance, then revalidate the new head.

…at_agent, and retire ceiling

Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com>
@loopx-agent
loopx-agent force-pushed the codex/chat-agent-slim-20261117 branch from 576e41b to f81bb5f Compare November 19, 2026 18:19
@mergify mergify Bot removed the needs-rebase Mergify: the pull request has merge conflicts with its base branch label Nov 19, 2026

@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

Chat 适配器与控制面治理维护者在扩展多端对话运行时(Codex app-server、ACP、DSH、Antigravity、Manager、Chat Providers、外部会话恢复及评估脚本)时,长期面临单文件反向依赖与模块体积超限问题。 此前在 origin/main 主干上,chat_agent.py 达到 2042 行并依赖 2047 行特例上限,跨端共享提示常量与 _turn_prompt 寄存在传输层模块迫使所有上层适配器反向导入,同时 chat_acp.py 重复维护同构的 stdio _reader 线程循环;重基整理后,4 项共享提示常量与 _turn_prompt 归位至既有公开契约模块 loopx/chat.py,chat_acp.py 复用参数化 _reader,chat_agent.py 降至 1797 行并移除 2047 行基线豁免。 若不处理,后续任何适配器修复或防护逻辑都会继续推高 chat_agent.py 体积并放大跨适配器耦合风险。本次在精确 head f81bb5f 上完成重基与收口:chat_agent.py 从 2042 行降至 1797 行并移除 module_metric_baseline.json 中 2047 行豁免条目,chat.py 原字面承接主干共享提示与 _turn_prompt(增至 1527 行),chat_acp.py 删去重复 _reader 降至 593 行,全量 13 个文件净减 28 行。 非目标边界:本 PR 不修改任何提示词语义、控制面策略、权限模型或对外配置项,也不顺带修复 origin/main 既有的 interaction_contract.py 类型标注问题。

改动思路 / Approach

模块 / 基线项 origin/main(b927e20bfb) 精确 head(f81bb5fad9) 变化说明
loopx/chat_agent.py 2,042 行(依赖 2,047 豁免) 1,797 行(无豁免) -245 行;迁出共享提示词、收束会话重复助手并补齐 _unwrap_stream_message 类型收窄
loopx/chat.py 1,298 行 1,527 行 +229 行;原字面承接主干 #9072 共享提示常量与 _turn_prompt
loopx/chat_acp.py 603 行 593 行 -10 行;删除本地 _reader 改为复用 chat_agent._reader
loopx/canary/module_metric_baseline.json 含 loopx/chat_agent.py: {"lines": 2047} 已移除 退休特例豁免,同时完整保留 #8920 / #8911 等主干条目

从架构所有权判定看,既有公开模块 loopx/chat.py 已持有 CHAT_RESPONSE_EXAMPLE_SUFFIX、parse_agent_response 与 VisibleResponseStreamFilter,是跨提供方共享对话文本与响应信封契约的天然单点 owner;因此将共享指令常量与 _turn_prompt 归位至 loopx/chat.py 优于新增顶层模块或保留反向导入。 保留 loopx/chat_agent.py 专职承担 Codex app-server 进程生命周期、JSON-RPC 请求分发、CodexChatAgentSession 状态与错误映射。当前 PR 边界严格收敛为:将主干最新共享提示常量与 _turn_prompt 原字面迁至 loopx/chat.py、通过新增关键字参数 eof_message 让 loopx/chat_acp.py 复用 chat_agent._reader、合并 CodexChatAgentSession 内部重复逻辑并收窄 _unwrap_stream_message 类型签名,同时移除 chat_agent.py 的 2047 行基线豁免;不增设 compat shim,也不触及 chat_runtime.py 或 session_open.py 的拆分结构。 在正反路径上,CONVERSATION_INTENT_RESOLUTION_INSTRUCTION、TRUSTED_OWNER_DIRECT_WORK_INSTRUCTION、WORK_RESULT_VERIFICATION_INSTRUCTION、COLLABORATION_INPUTS_GUIDANCE 及 4 种运行模式的 _turn_prompt 输出在重基后的 origin/main 与 HEAD 间 SHA-256 逐字节一致;流 EOF、非法 JSON 帧、空行/非字典帧跳过、未绑定命令 item、硬/闲置超时及服务端门控异常均保持原有错误类型与错误码不变。

具体改动 / Changes

规范依据(spec_basis):no_spec(spec_source: none)——本次为长期模块体积收敛、所有权归位与内部去重重构,无新增功能规范或独立书面 RFC,交付判断依据 problem_context 的零语义漂移与可维护性基线收敛要求,并已核对 loopx/canary/maintainability_ratchet.py 与 tests/architecture/test_top_level_module_budget.py 门禁。全量 diff 共 13 个文件(+337/-365,净 -28 行),包含运行时产品代码 8 个、基线配置 1 个、评估脚本与测试文件 4 个。

关键代码讲解

  1. loopx/chat.py:1303(CONVERSATION_INTENT_RESOLUTION_INSTRUCTION 等四项共享常量与 1349 行 _turn_prompt):完整承接 origin/main(b927e20bfb,含 #9072 精简后措辞及 reply_image_directory: Path | None = None 参数)的全部共享引导定义;同步将 chat_agent.py、chat_acp.py、chat_dsh.py、chat_manager.py、chat_providers.py、antigravity_adapter.py、external_conversations.py、examples/evaluations/chat-intake.py 及 3 个测试文件中的导入切换至 loopx.chat,全仓扫描无残留旧引用。
  2. loopx/chat_agent.py:390(_reader)与 loopx/chat_acp.py:111(ACPStdioAdapter.start):为 _reader(stream, messages, *, eof_message: str = "Codex app-server stream closed") 增加仅关键字参数 eof_message,保持 Codex 默认行为与 test_chat_codex_auth.py 既有调用零改动兼容;chat_acp.py 删去 16 行重复实现并传入 kwargs={"eof_message": "ACP stdio stream closed"} 复用同一读取与 EOFError 投递路径。
  3. loopx/chat_agent.py:1017(_unwrap_stream_message)、1331(_validated_command_item_id)与 1756(_turn_deadline):将 _next_message 与 _request 中重复的 EOFError/Exception 转换收拢进 _unwrap_stream_message(self, raw: dict[str, Any] | Exception) -> dict[str, Any](精准匹配 self.messages 队列类型,消除严格 mypy no-any-return);将 background_turn_ids 与 _command_item_ids 中重复的 commandExecution 校验收拢进 _validated_command_item_id 与局部 record_command;将 send() 循环前与 except CodexChatAgentError: 中的超时判定收拢至 _turn_deadline,合并 emit_visible、raise_turn_failure 与 (model, reasoning_effort) 校验循环,并删除 _request 内被前置 if message.get("method") and self._check_server_gate(message) 严格覆盖的死分支。
  4. loopx/canary/module_metric_baseline.json:67(移除 loopx/chat_agent.py 豁免条目):在确认 loopx/chat_agent.py 降至 1,797 行且 loopx/chat.py 为 1,527 行后移除 lines: 2047 特例豁免,保留主干 #8920、#8911 等已合入条目不变。

对主干的风险 / Risks

本次在精确 head f81bb5fad928428e291ce9ebed23ebbacbc3360d 上完成复核,未发现阻塞合入的问题(no blocking finding)。主要风险面与验证结论如下:

  • 跨端迁移与重基完整性:全仓检索确认除本 PR 更新的 12 个源码/评估/测试文件外,无其他源码、文档或测试从 loopx.chat_agent 导入 _turn_prompt 或 4 项共享常量;保留在 chat_agent.py 中的 CHAT_REVIEW_OPEN_TAG、CHAT_REVIEW_CLOSE_TAG 与 _reader 供既有测试正常访问。由于上述私有符号带前导下划线且仅作仓内跨适配器共享,外部私有代码若直接导入 loopx.chat_agent._turn_prompt 会收到 ImportError,仓内不额外保留冗余兼容垫片。重基期间同步核验了 origin/main(b927e20bfb)近期合入的 #9072 提示词精简、#9083 memory_preferences、#8911 external_conversations 拆分与 #8920 chat_runtime 拆分,均完整无损。
  • 反例路径与静态/类型契约:_check_server_gate 全部分支均以 truthy message.get("method") 为前提,删除 _request 内 message.get("id") != request_id 下的重复检查不改变任何门控语义;raise_turn_failure 与 _unwrap_stream_message 保留全部异常类型、error_code 与 native_error_info 属性赋值。default_off_isolation 与 authority_semantics 均属 not_applicable(无新增开关、权限边界或受控词表枚举);test_cli_entrypoint.py 验证公共 CLI 延迟导入边界未受 loopx.chat 迁移影响。
  • 测试、并发锁时序与既有基线差异:24 个核心 Chat、架构、CLI 入口与可维护性棘轮套件在 -n 3 下实跑 1896 passed, 6 skipped(6 项 test_chat_codex_auth.py 显式已安装原生 SDK 用例按设计跳过),tests/canary/test_maintainability_ratchet.py 单独亦 14 passed。tests/test_lark_private_conversations.py 此前在 -n 4 高并发负载下有 1 项 test_recovery_waits_for_inflight_capture_without_readmitting[True] 触发瞬时文件锁超时(LockAcquireTimeoutError),在最终 head 上独立复跑 -k "reply_image or test_recovery_waits_for_inflight_capture_without_readmitting" 获 18 passed, 557 deselected;仓内 python -m mypy 仅报 origin/main 既有未触及文件 loopx/control_plane/work_items/interaction_contract.py:1004 的 1 处预存错误,变更文件 ruff 与触及行严格 mypy 均为 0 问题。examples/evaluations/chat-intake.py --live 付费模型调用按设计未运行,为本 PR 最强未执行外部验证项,但不影响本次纯结构归位与等价重构验证。
验证项 级别 结果 实跑输出(exact head f81bb5fad928428e291ce9ebed23ebbacbc3360d)
24 项 Chat / 架构 / 可维护性 / CLI 入口套件(pytest -n 3 -q -rs) required PASS 1896 passed, 6 skipped(6 项 explicit installed native SDK test;含 test_maintainability_ratchet.py)
Lark 私聊图片目录与在途恢复重测(test_lark_private_conversations.py -k "reply_image or test_recovery_waits_for_inflight_capture_without_readmitting") required PASS 18 passed, 557 deselected(已覆盖此前 -n 4 下瞬时锁超时项)
origin/main(b927e20bfb)vs HEAD 提示词常量 / _turn_prompt SHA-256 与 _reader 对拍 + chat-intake.py --help + 3 项 Chat smoke required PASS PARITY CHECK: ALL MATCH;3 项适配器/运行时 smoke 全 ok
ruff check(12 个变更 .py)+ mypy --strict --follow-imports=silent loopx/chat_agent.py + npm run typecheck:control-plane required PASS All checks passed!;变更行 0 个 mypy strict 错误;tsc --noEmit 通过
loopx check --scan-path ...(13 个变更文件)+ change-quality verify + canary premerge --tier quick required PASS errors: 0;cqr_81a6bd17b3862a1b6ccd(valid);merge_gate_passed: true(failure_count: 0)
python -m mypy(仓库配置 17 文件) diagnostic FAIL pre_existing_unrelated:仅 origin/main 既有未触及文件 interaction_contract.py:1004 1 处错误

我的整体评价 / Verdict

对精确 head f81bb5fad928428e291ce9ebed23ebbacbc3360d 的评价:long_horizon 判定为 improved(消除 loopx/chat_agent.py 的 2,047 行特例豁免并将其压降至 1,797 行,同时解除多适配器对传输层模块的反向提示词依赖,降低跨端协作与维护演进摩擦);user_experience 与 visual_review 均判定为 not_applicable(原因:纯内部模块归属、_reader 复用与类型收窄重构,未改动任何页面渲染、UI 布局、CLI 命令参数、交互文案或模型提示词文本,已核对 loopx/chat.py、loopx/chat_agent.py 与 loopx/chat_acp.py,unverified_scope: none)。observable_semantics 判定为 equivalent(origin/main b927e20bfb 的 #9072 共享提示常量、reply_image_directory 分支在内的 4 种 _turn_prompt 模式输出及 _reader 默认/ACP EOF/JSON 异常路径均逐字节等价)。code_volume 判定为 necessary(净 -28 行,compatibility_assessment=not_applicable:私有 _turn_prompt 与引导常量仅仓内跨适配器复用且全部调用方已迁移,无需保留兼容重导出)。change_proportionality 判定为 proportionate(architecture_assessment=retain),default_off_isolation 与 authority_semantics 均为 not_applicable,semantic_alignment 为 not_applicable(未新增、扩展或重定义任何受控词表、状态枚举或持久化 schema)。本地测试、对拍、静态检查、公开边界扫描、Change Quality Receipt(cqr_81a6bd17b3862a1b6ccd)与 canary premerge 均在精确 head f81bb5fad928428e291ce9ebed23ebbacbc3360d 实跑通过;唯一残余非阻断边界为未执行 examples/evaluations/chat-intake.py --live 付费实测及外部私有下游不得直接导入内部私有 _turn_prompt 的既有边界。仅当后续改动修改共享提示内容或传输层流读取逻辑时才需重跑对拍与复审。

English verdict: APPROVE — Exact head f81bb5fad928428e291ce9ebed23ebbacbc3360d cleanly rebases onto origin/main (b927e20bfb), preserves #9072 prose and reply_image_directory verbatim in loopx/chat.py, shrinks loopx/chat_agent.py from 2,042 to 1,797 lines to retire its module_metric_baseline.json ceiling, consolidates _reader and session helpers without behavior drift, records passing CQR cqr_81a6bd17b3862a1b6ccd, and passes local pytest, parity, static, typecheck, public-boundary, and quick premerge gates with zero blocking findings.

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