loopx icon indicating copy to clipboard operation
loopx copied to clipboard

[Bug]: managed Codex Turn reads its CLI event stream with the host locale codec, so a non-UTF-8 host cannot confirm the session identity

Open JasonBuildAI opened this issue 1 month ago • 0 comments

LoopX version or commit

cd148933b (current main); the same behavior reproduces at fdf03ec61.

Host or runtime surface

Codex CLI — the managed agent session observed by the Turn driver (CodexAgentSession), including its checkpoint context refresh.

LoopX area

Control plane (Turn driver)

Preflight

  • [x] I searched existing issues and discussions for this behavior.
  • [x] This report is not an unpatched security vulnerability.
  • [x] The report and attachments contain no credentials or private/internal material.

Issue origin

Observed or reproduced in a real environment

Summary

CodexAgentSession.observe() reads the Codex CLI exec JSONL through path.open() with no codec, so the stream is decoded with the host default encoding instead of UTF-8. On a non-UTF-8 host the read raises, the only thread.started identity in the stream is never observed, and the Turn fails:

File "loopx/control_plane/turn_driver/codex_agent_session.py", line 96, in observe
    for line in stream:
UnicodeDecodeError: 'gbk' codec can't decode byte 0x90 in position 50: illegal multibyte sequence

The class contract then makes the failure permanent rather than retryable: an unconfirmed native identity never authorizes dispatching a fresh conversation, so the wake is stuck and the already-completed Codex work cannot be attached to the Goal.

Impact (why this matters)

  • Who is affected. Every host whose default encoding is not UTF-8. That includes zh-CN Windows, where locale.getpreferredencoding(False) is cp936 and this repository ships a dedicated windows-powershell CI lane; a POSIX host with an ASCII default (LC_ALL=C, PYTHONCOERCECLOCALE=0, no UTF-8 mode) behaves the same way. Both codec families were checked against the CLI's event bytes: cp936, ascii, cp1252, cp1250 and cp1257 reject them.
  • How often, and which call paths reach it. Any managed Codex wake whose event stream carries non-ASCII text; the failure is deterministic, not a race. Reach at cd148933b, checked by searching the tree: CodexAgentSession.observe() has no caller under loopx/**. Its production callers are benchmark/runtime/worker.py:510 and benchmark/runtime/checkpoint_wake.py:214, plus benchmark/runtime/checkpoint_session_refresh.py:80, the only production caller of prepare_checkpoint_refresh. The ordinary Turn path streams events over a pipe (codex_cli.py) and never performs this file read, so the capability affected today is the benchmark wake / checkpoint-refresh harness — narrower than "every managed Turn", and stated here so the fix is not oversold.
  • Severity. The Turn is already spent: the model ran, the events exist on disk, and observation then aborts. Because the observed identity is what binds the provider conversation to the Goal, the wake has to be recovered by hand; LoopX reports should_run=true while the loop makes no progress.
  • Why it is not an edge case. Non-ASCII is the normal case for the users this repository targets, and the UTF-8 file gate (tests/test_loopx_text_io_utf8.py) exists precisely because the class had already produced real incidents (#4338, #4942, #4997) and a series of merged repairs (#6119, #6166, #6168, #6230, #6399, #6472, #6558).
  • Why it survived those repairs. The gate inspects read_text() / write_text() / builtin open() only. Path.open() and os.fdopen() are the same text-mode APIs but are not checked, so this site and the sibling writes below were invisible to every sweep.

Minimal reproduction

  1. On a host whose default encoding is not UTF-8 (here: cp936; on POSIX use LC_ALL=C PYTHONCOERCECLOCALE=0 with -X utf8=0), prepare an agent-scoped Codex session the way tests/control_plane/test_codex_checkpoint_context_refresh.py does (store a session, set binding, admission, session_id, receipt, checkpoint_refresh).
  2. Call prepare_checkpoint_refresh(review, attempt, output) so a refresh is pending.
  3. Write output as the Codex CLI does — UTF-8 JSONL, one ordinary event carrying a non-ASCII message plus the {"type": "thread.started", "thread_id": ...} identity line.
  4. Call observe(output).

Expected vs actual

  • Expected: the identity line is observed and observe() returns; the provider conversation is bound to the Goal.
  • Actual: UnicodeDecodeError from the locale codec (or, when the locale codec happens to accept the bytes, a silently mangled line that json.loads drops), followed by RuntimeError: Codex did not confirm the expected session identity.

Same contract, unguarded sibling sites

Both writes below persist JSON that their own reader re-reads as UTF-8 bytes (json.loads(path.read_bytes())), so they belong to the same contract, and neither is visible to the gate:

  • loopx/control_plane/turn_driver/codex_agent_session.py — os.fdopen(descriptor, "w") for the checkpoint-refresh attempt record.
  • loopx/hosts/codex_checkpoint.py — os.fdopen(os.open(attempt_path, ...), "w") for the native fork attempt record; its record carries guest_workspace and guest_home, so a non-ASCII workspace path reaches it directly.

Their payload is ASCII today only because json.dump defaults to ensure_ascii=True; the codec contract they rely on is still undeclared.

Suggested fix

Pin encoding="utf-8" on the three sites, and extend the shipped file gate to inspect text-mode os.fdopen() as well, so the API class cannot hide a locale codec again. I am preparing that as a PR.

JasonBuildAI avatar Oct 17 '26 04:10 JasonBuildAI