loopx icon indicating copy to clipboard operation
loopx copied to clipboard

[Bug]: isolated presentation view validator encodes its stdio with the host locale, so non-ASCII views fail on a non-UTF-8 host (cp936)

Open JasonBuildAI opened this issue 1 month ago • 0 comments

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

LoopX version or commit

Reproduced on adb247e5f (current main); the affected code exists since the presentation-surface contract was introduced and is present in 6f53c6867 too.

Host or runtime surface

Not host-specific. Reproduced on a Windows 11 host with a zh-CN (cp936) locale, which is the ordinary case for the affected users.

LoopX area

Capability or extension (providers, adapters, skills)

Problem

load_presentation_view_validator (loopx/extensions/presentation.py) runs an extension-declared view_validator inside the extension's own runtime interpreter as a generated python -I -c <helper> script. The transport owner writes the request to the child as UTF-8 bytes and decodes the child's stdout as UTF-8. The generated helper itself reads sys.stdin and writes sys.stdout as text, and python -I ignores PYTHONIOENCODING/PYTHONUTF8, so those streams use the host locale codec. On a host whose locale is not UTF-8 (for example Windows zh-CN, cp936), a view containing non-ASCII text is re-encoded into the locale codec on its way back:

  • The parent's completed.stdout.decode("utf-8") raises UnicodeDecodeError, and load_presentation_view_validator reports presentation surface view_validator ... returned invalid output (line 389 / 384 today).
  • When the locale bytes happen to survive UTF-8 decoding in a degraded form, the decoded text carries mojibake with lone surrogates; persisting it then fails because the envelope hash and the projection file are serialized with ensure_ascii=False and encoded as UTF-8 (UnicodeEncodeError).

Who is affected: any extension provider publishing a presentation projection whose view text contains non-ASCII characters (the bundled Finance decision-research dashboard with Chinese content is one concrete case), on any host whose locale is not UTF-8. The failure is deterministic and there is no user-side workaround, because -I drops PYTHONIOENCODING/PYTHONUTF8.

This is the remaining entrypoint named by the merge note of #6168: that PR pinned the three bundled provider entrypoints to UTF-8 and explicitly stopped before the two LoopX-generated python -I -c helpers in presentation.py and readiness.py.

Minimal reproduction

On a non-UTF-8 host (python -c "import locale; print(locale.getpreferredencoding(False))" prints cp936):

import sys
from loopx.extensions.presentation import load_presentation_view_validator
from loopx.extensions.readiness import ResolvedRuntimeEntrypoint

entrypoint = ResolvedRuntimeEntrypoint(
    argv_prefix=(sys.executable,),
    identity="repro",
    python_executable=sys.executable,
)
validator = load_presentation_view_validator(
    {"view_validator": "builtins:str"},
    runtime_entrypoint=entrypoint,
)
print(validator({"headline": "中文标题,带标点。"}))

builtins:str is only a minimal echo validator; any real extension validator that returns non-ASCII text behaves the same.

Expected behavior

The call returns str({"headline": "中文标题,带标点。"}) and the published projection round-trips the text unchanged, on every host locale.

Actual behavior

ValueError: presentation surface view_validator `builtins:str` returned invalid output

caused by

UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 33-34: invalid continuation byte

Running the same generated helper with a cp936 stdin/stdout pair shows the child exiting 0 while emitting cp936 bytes where the transport expects UTF-8.

Sanitized diagnostics

$ python -c "import locale, sys; print(locale.getpreferredencoding(False), sys.stdout.encoding)"
cp936 gbk
$ python -I -c "import sys; print(sys.stdout.encoding)"
gbk
$ $env:PYTHONUTF8='1'; python -I -c "import sys; print(sys.stdout.encoding)"
gbk
$ python -c "import sys; print(sys.stdout.encoding)"
utf-8

The last two lines show that -I deliberately ignores PYTHONUTF8, so the helper cannot be repaired from the outside.

Additional context

Follow-up to #6168 (merged), which names this entrypoint as its remaining successor. The other named helper, loopx/extensions/readiness.py::_RESOLVE_DECLARED_MODULE_ORIGIN, is ASCII-safe because it serializes with the json.dump default (ensure_ascii=True), so it is not affected.

JasonBuildAI avatar Oct 11 '26 10:10 JasonBuildAI