Skip to content

fix(oar): harden multiplexed session host framing, control slots, token rotation, and contract parity - #10275

Open
songoow wants to merge 2 commits into
mainfrom
codex/oar-multiplex-invariant-hardening
Open

songoow wants to merge 2 commits into
mainfrom
codex/oar-multiplex-invariant-hardening

Conversation

@songoow

@songoow songoow commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

Follow-up invariant-hardening PR for merged #10241 fixing five live-reproduced adversarial counterexamples plus two client-side invariants found during audit, locked down with property / state-matrix tests:

  1. Control-slot starvation (loopx/control_plane/oar/session_host_server.ts):
    • Owned releasingOwnedSession oar.close_session requests no longer increment/decrement budget.active, preventing a burst of owned closes under 64-slot oar.prompt_stream saturation from starving the 8 RESERVED_CONTROL_SLOTS needed by oar.abort.
    • Preserves exact-key matching (workbenchSessionSockets.get(sessionKey) === socket) matching requireNonEmptyString / detachWorkbenchSession / hub.closeSession so neither whitespace-padded keys nor duplicate closes can bypass budget.active after detaching once.
  2. Permanent unauthorized poison on same-port token rotation (apps/workbench/packages/daemon/src/adapters/oar-host-client.ts):
    • Clears this.endpoint = null in closeTransport(), handleDisconnect(), and when receiving an unauthorized error frame (rejecting the failing request with unauthorized first, then closing the poisoned socket so the next request refreshes the locator via ensureDaemon()).
    • Adds a transportGeneration guard across closeTransport() / ensureConnected() so a transport close mid-connect cannot attach a stale socket afterward.
  3. Multi-line valid chunk burst framing (session_host_server.ts & oar-host-client.ts):
    • Switches both server (MAX_REQUEST_BYTES = 8 MiB) and client (MAX_RESPONSE_BYTES = 16 MiB) socket.on("data") handlers to incremental \n frame splitting with per-line and unterminated-tail UTF-8 byte accounting, preventing valid multi-line bursts sharing a TCP chunk from falsely triggering request_too_large / response-too-large socket teardowns while preserving immediate DoS rejection on oversized single lines.
  4. Early-failing keep_alive: true frames (session_host_server.ts):
    • Reads keepAlive = req.keep_alive === true immediately after isValidToken(req.token) (clearing only when method === "oar.shutdown"), so authenticated empty/invalid-method frames return correlated errors without closing the shared Workbench socket and destroying live sessions.
  5. Cross-language contract parity (goal_worker.ts, oar-host-client.ts):
    • loopx/control_plane/oar/goal_worker.ts: createGoalQuotaProbe() aligns with opencode2_goal_worker.py by trimming LOOPX_BIN / options.bin, consuming & scrubbing LOOPX_OAR_QUOTA_BRIDGE_* for empty/whitespace LOOPX_BIN, and falling back to "loopx" instead of execFile("").
    • apps/workbench/packages/daemon/src/adapters/oar-host-client.ts: extracts buildOarHostLaunchArgs() with optional pythonExecutable forwarding (--python <abs>) on win32 and fail-closed oar_host_python_required when missing/relative.
  6. Synchronous disconnect during CoreOarSession construction (apps/workbench/packages/daemon/src/adapters/oar-core.ts):
    • Initializes unsubscribeDisconnect = () => {} so an immediate disconnect inside new CoreOarSession(...) marks the session closed cleanly without raising TypeError in markClosed().

Property & State-Matrix Tests

  • tests/control_plane_ts/oar_session_host_multiplex.test.ts:
    • Interleaved (0..64 prompt_stream × 0..16 owned close_session × early-failing keep_alive:true frames × up to 8 concurrent oar.abort) matrix + saturated 20-burst whitespace-padded & duplicate oar.close_session detach-once regressions with a persistent live Workbench survivor session.
    • Unaligned multi-line valid near-8 MiB chunk burst matrix (incl. 8,380,112 B + 16 KB split, two 5 MiB frames, trailing unterminated tail, exact 8 MiB boundary) + oversized terminated/unterminated single-line DoS checks.
  • apps/workbench/packages/daemon/test/oar-core.test.ts:
    • OarHostClient reconnect state-machine matrix (same-port token rotation after disconnect and after closeTransport(), crash/new-port restart, stale locator, unauthorized keep-open & close-on-error, multi-line < 16 MiB response burst, synchronous disconnect on CoreOarSession open).
    • buildOarHostLaunchArgs() cross-platform --python parity matrix.
  • tests/control_plane_ts/oar_goal_worker.test.ts:
    • createGoalQuotaProbe() matrix over LOOPX_BIN ∈ {undefined, "", " ", customBin} × bridge ∈ {absent, present} + blank options.bin.

Verification

  • npm run typecheck:control-plane, npm --prefix apps/workbench run build -w @loopx/workbench-daemon & 6-workspace typecheck: PASS
  • Control-plane TS (tests/control_plane_ts/oar_*.test.ts, workbench.test.ts): 79/79 PASS
  • Workbench daemon (oar-core, oar, oar-thread-status, workbench-lifecycle): 42/42 PASS
  • Python OAR suites (tests/control_plane/test_oar_*.py): 252/252 PASS (incl. 95/95 re-run on rebased e2f575643)
  • node scripts/ci/lint-external-transports.mjs, CI ruff lints, python -m loopx.cli check (ok: True, 0 errors): PASS

…ion, and contract parity

Fixes five live-reproduced adversarial counterexamples in PR #10241 plus
two additional client-side invariants found during audit, locked down with
property/state-matrix tests:

- session_host_server.ts: do not charge owned releasingOwnedSession closes
  against budget.active so saturated prompt_stream + owned close bursts
  never starve oar.abort reserved control slots (while preserving detach-once).
- session_host_server.ts: check keep_alive immediately after token auth (and
  clear only for oar.shutdown) so early method/param validation errors return
  correlated errors without closing the shared Workbench socket.
- session_host_server.ts & oar-host-client.ts: drain complete newline frames
  incrementally and enforce MAX_REQUEST_BYTES / MAX_RESPONSE_BYTES per frame
  and unterminated tail so valid multi-line bursts never falsely trip size
  limits while preserving unbounded-input DoS protection.
- oar-host-client.ts: clear cached endpoint in closeTransport(),
  handleDisconnect(), and on unauthorized responses (disconnecting the
  poisoned socket and guarding in-flight connect against closeTransport) so
  same-port token rotation refreshes the locator and recovers cleanly.
- oar-host-client.ts: add buildOarHostLaunchArgs() with optional absolute
  pythonExecutable forwarding and fail-closed win32 parity with
  session_host_server.ts --python.
- oar-core.ts: initialize unsubscribeDisconnect so a synchronous disconnect
  during CoreOarSession construction does not raise TypeError.
- goal_worker.ts: align createGoalQuotaProbe() LOOPX_BIN trimming with
  opencode2_goal_worker.py so blank LOOPX_BIN consumes and scrubs
  LOOPX_OAR_QUOTA_BRIDGE_* and never invokes execFile('').

Signed-off-by: song <xinxinsong66@gmail.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T11:36:13.475097Z e2f5756 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@loopx-agent loopx-agent left a comment

Copy link
Copy Markdown
Collaborator

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

[P2] 已断开的会话不能从 start 成功返回。 会话确认收到后、openSession 返回前若连接关闭,候选仍将已关闭的 session 从 OarAdapter.start 当作成功返回,而且触发 onSessionId。ThreadManager 会据此保存 native ID。后续第一次 prompt 才失败,本次改动覆盖的同步关闭分支因而仍产生成功的打开结果。 当前实现

动机 / Motivation

通过 Workbench 复用 Core OAR Session Host 的对话操作者,以及运行本机 OAR Goal Worker 的操作者。 多个已授权对话共用一个持久连接。会话确认、客户端关闭和实时事件可以交叠。 共用连接在正常多会话处理和断开后保持可用,准入探测与 Python 路径对齐。 验证限于本机自有隔离宿主与 scripted CLI,未连接生产账号或真实模型。 覆盖 macOS 本机共用宿主、模拟边界确定性断开与编译。Windows 只核验参数 helper,Core Workbench 启动入口仍拒绝 Windows。 基线 7ac233d。 基线约定

改动思路 / Approach

复用现有 session host、socket RPC 和 Goal Worker bridge 所有者。在帧边界执行字节计量,连接失效时清理端点引用。 Workbench OAR Core 客户端和服务端、Goal Worker 选择及相邻测试。

具体改动 / Changes

  • PRIMARY_DELIVERY。共用连接在正常多会话处理和断开后保持可用,准入探测与 Python 路径对齐。

  • PRESERVE_CONTRACT。已断开的会话不能从 start 成功返回。

  • Core 结果绑定到本次连接并构造会话。 CoreOarHostBackend.openSession

  • 将打开的会话返回给 ThreadManager,并公布 native ID。 OarAdapter.start

  • 复用有效连接,刷新 locator 并用 generation 拒绝过期连接。 ensureConnected

  • 按行计量请求并沿同一连接分派。 data

  • 清理空 CLI 路径,保持探测选择优先级。 createGoalQuotaProbe

对主干的风险 / Risks

本地已授权会话收到打开确认后,共用客户端在 caller 拿到会话前执行正常 closeTransport。 基线在同一个确定性断开点也返回已关闭对象并调用 onSessionId,属于本补丁声称覆盖但仍未修复的生命周期缺口。 独立通过 9 项控制面测试、3 项 Workbench 测试、控制面类型检查与实际 daemon 构建。普通会话打开返回存活对象,onSessionId 恰调用一次。

Windows 差异中的 Windows 证据覆盖参数 helper。共享客户端仍未读取 pythonExecutable,managed Workbench 入口仍明确只支持 Linux 与 macOS。

我的整体评价 / Verdict

请求修改精确提交 e2f5756。在 CoreOarHostBackend.openSession 返回前检查 session.isClosed,已关闭则返回 transport_lost,让正常重试使用新连接,并确认 onSessionId 在该失败路径零调用。

English verdict: REQUEST_CHANGES at e2f5756. An OAR session that is already closed when the adapter returns must fail the open operation before Workbench records its native session ID.

Signed-off-by: Codex <codex@openai.com>
Signed-off-by: song <liusongstep@gmail.com>

@songoow songoow 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 Astra Pro · OpenAI · self_reported

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

动机 / Motivation

在 Workbench 发起普通对话的用户需要一个可以继续使用的原生会话 ID。此前 Core 已确认打开、连接却在交给调用方之前关闭时,调用方仍可能保存该 ID。现在打开与交接两处都会确认会话存活;断开会返回 transport_lost,成功重试只发布一次有效 ID。本次验收涵盖本机 Core 宿主、Workbench 客户端、正常配额探测及 Goal 恢复。

改动思路 / Approach

连接状态属于 Core 客户端,Workbench 只在已打开的会话交给线程存储时发布原生 ID。沿用会话自带的 isClosed 和类型化传输错误,在宿主返回与异步交接两个时间点检查,并由原清理路径释放资源。通过同一入口和合成 fixture 比较改动前后的运行结果,并核对错误返回时能否恢复原有工作流。

具体改动 / Changes

当前树通过 5 项 Workbench/Core 定向测试、4 项 Goal 正常执行和恢复测试,以及 5 项配额桥与宿主复用测试。Core 和 Workbench daemon 类型检查通过。主干与当前树都通过真实 Python 和 Workbench 并发启动同一宿主,以及双方共用一个 Core 会话服务的测试。在 e2f5756 上,同一个打开交接回归报 Missing expected rejection;修复后在订阅期间和返回调用方期间分别断开,均抛出 transport_lost、零次发布 ID,随后正常重试成功。

宿主按请求类别保留控制容量且在请求完成后释放,客户端以连接代次拒绝已经失效的回复;常规请求、多会话清理、配额桥重连与空白可执行路径选择已覆盖。

本次按 https://github.com/loopx-project/loopx/pull/10275#pullrequestreview-6210475060(审查记录 6210475060)核对 LIVE_SESSION_HANDOFF 的实现。其中比较的是仓库实际调用方所获得的结果,远端整套发布流水线单独记录。

对主干的风险 / Risks

本次以本机真实传输和测试宿主验证,Windows 启动参数只在 Linux 上测试。没有使用真实付费账号,亦未执行安全攻击复现。两个 PR 各可合入主干,同时落地时有两处适配器内容冲突,已有独立整合树通过交叉回归。在 e2f5756 上,同一个打开交接回归报 Missing expected rejection;修复后在订阅期间和返回调用方期间分别断开,均抛出 transport_lost、零次发布 ID,随后正常重试成功。另在主干与本分支重跑同一条 Ruff 检查,都报 executor.py 第 91 行相同的 F401。这是独立的合并门禁问题。上述证据支持本次所修改的调用边界;对未执行的平台不作延伸判断。

我的整体评价 / Verdict

之前正式要求修改的交接缺陷已经修复。连接状态属于 Core 客户端,Workbench 只在已打开的会话交给线程存储时发布原生 ID。沿用会话自带的 isClosed 和类型化传输错误,在宿主返回与异步交接两个时间点检查,并由原清理路径释放资源。

English verdict: APPROVE at 549dfe1. A disconnected open is rejected before its ID reaches the thread store, while normal open, reconnect and quota progression still work.

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.

3 participants