Skip to content

Scope the runtime encoding scan past vendored laya while its loader supplies UTF-8 - #12230

Merged
danielhanchen merged 3 commits into
mainfrom
fix-runtime-encoding-laya
Sep 28, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
fix-runtime-encoding-laya

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

tests/test_runtime_text_encoding.py::test_shipping_code_names_an_encoding is red on main. Since #12202 vendored laya, the repo-level scan walks studio/backend/vendor/laya/agent.py and flags three bare open() calls (lines 31, 47, 156).

That file is kept byte-identical to the wheel and pinned by sha256 in vendor/laya_manifest.json, so it can't be edited. Its encoding comes from core/systemone/laya_runtime.py, which injects _utf8_open into each laya.* module. studio/backend/tests/test_text_io_encoding.py::test_vendored_laya_reads_utf8_config_under_a_non_utf8_locale proves this under LC_ALL=C.

Change

  • Add UTF8_BY_LOADER to map the vendored tree to the loader that supplies UTF-8, plus a marker string.
  • Skip files under that tree only while the loader still contains .open = _utf8_open. If the injection is removed, the scan turns red again.
  • Nothing else is excluded. truststore and every other vendored or first-party path is still scanned.

Verification

  • On main the file now shows 8 passed; before this change it had 1 failure.
  • Sabotage 1: remove the _utf8_open assignment from laya_runtime.py. The scan fails again.
  • Sabotage 2: add a bare open(p) to unsloth/save.py. The scan fails.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-28T20:42:43.609714Z ab65955 Manual request
🔒 Security Review ⚠️ Failed 2026-09-28T19:42:02.806548Z 67fdb56 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 67fdb5625e

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/test_runtime_text_encoding.py Outdated
Comment on lines +358 to +359
if _utf8_by_loader(path):
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restrict the exemption to calls the loader protects

When a future pinned Laya update introduces Path.read_text()/Path.open() or a module-level bare open(), this file-level continue hides it even though laya_runtime._laya() only replaces each module's open global after exec_module returns (studio/backend/core/systemone/laya_runtime.py:149-155). Those calls would still use the platform locale while this guard remains green, so exempt only function-time bare open offenders that the loader actually intercepts rather than skipping the entire vendored file.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the file-level skip was wider than the swap. Narrowed in 7622813: only bare open(...) calls at function time, where open is not a parameter, assignment or import inside that function, are exempt. Module-level and class-body calls, Path.open, read_text and io.open are still scanned. Added test_loader_swap_exempts_only_the_calls_it_reaches, and a vendored import-time open() or read_text() now fails the scan.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 762281382c

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/test_runtime_text_encoding.py Outdated
Comment on lines +345 to +347
for vendored, (loader, marker) in UTF8_BY_LOADER.items():
if vendored.resolve() in parents:
return loader.is_file() and marker in loader.read_text(encoding = "utf-8")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Limit the exemption to eagerly patched modules

When a pinned Laya update adds a lazily imported submodule that is not re-exported from laya/__init__.py, this directory-only check exempts its bare function-time open() calls even though _laya() patches only the modules already present in sys.modules immediately after exec_module; a submodule imported later retains the locale-dependent builtin. Fresh evidence in the narrowed implementation is that _utf8_by_loader() verifies only the file's parent directory and a textual loader marker, not whether that module is among those the loader patches.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Rather than add more rules to guess which calls the loader rebind reaches, ab65955 drops the inference. The scan now exempts exactly three reviewed sites, agent.py:31, :47 and :156. All three are function-body open() calls in a module laya/init.py imports eagerly, and they're exempt only while laya_runtime still rebinds open. The vendored files are pinned by sha256, so the lines can't drift. Any new, moved or removed vendored call, including default-argument, lambda-default or lazily imported submodule cases, turns the scan red until someone re-reviews it. A reviewed site that disappears also fails as stale.

Comment thread tests/test_runtime_text_encoding.py Outdated
Comment on lines +376 to +378
if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda)):
if isinstance(child, ast.Lambda) or not _binds_open(child):
walk(child, True)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid exempting definition-time open calls

For a module-level definition such as def f(handle=open(path)): ..., walking the entire FunctionDef with in_function=True marks the default-expression call as covered, but defaults, decorators, and annotations are evaluated by exec_module before _laya() installs _utf8_open. Fresh evidence in this revision is that the narrowed traversal still equates syntactic containment in a function node with execution after module loading, allowing these import-time locale-dependent reads to escape the guard.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Rather than add more rules to guess which calls the loader rebind reaches, ab65955 drops the inference. The scan now exempts exactly three reviewed sites, agent.py:31, :47 and :156. All three are function-body open() calls in a module laya/init.py imports eagerly, and they're exempt only while laya_runtime still rebinds open. The vendored files are pinned by sha256, so the lines can't drift. Any new, moved or removed vendored call, including default-argument, lambda-default or lazily imported submodule cases, turns the scan red until someone re-reviews it. A reviewed site that disappears also fails as stale.

Comment thread tests/test_runtime_text_encoding.py Outdated
def walk(node, in_function):
for child in ast.iter_child_nodes(node):
if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef, ast.Lambda)):
if isinstance(child, ast.Lambda) or not _binds_open(child):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor lambda bindings before exempting open

When vendored code uses reader = lambda p, open=open: open(p), the default captures the builtin during exec_module, so the body never consults the module global that _laya() later patches. The unconditional lambda branch nevertheless marks that body call as covered; fresh evidence in this revision is that named-function parameters go through _binds_open() while lambda parameters bypass it entirely.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Rather than add more rules to guess which calls the loader rebind reaches, ab65955 drops the inference. The scan now exempts exactly three reviewed sites, agent.py:31, :47 and :156. All three are function-body open() calls in a module laya/init.py imports eagerly, and they're exempt only while laya_runtime still rebinds open. The vendored files are pinned by sha256, so the lines can't drift. Any new, moved or removed vendored call, including default-argument, lambda-default or lazily imported submodule cases, turns the scan red until someone re-reviews it. A reviewed site that disappears also fails as stale.

Comment thread tests/test_runtime_text_encoding.py Outdated
Comment on lines +357 to +359
for node in ast.walk(fn):
if isinstance(node, ast.Name) and node.id == "open" and isinstance(node.ctx, ast.Store):
return True

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep nested-scope bindings out of the outer function

When a vendored function uses the patched global open() and also has a nested scope that binds the same spelling—for example, text = open(path).read() followed by [open for open in items]—ast.walk(fn) sees the comprehension target's Store and incorrectly concludes that the outer function rebinds open. The outer call is then reported as an encoding offender even though Python resolves it to the loader-patched global, producing a false CI failure against byte-identical vendored code; binding detection needs to stop at nested lexical scopes.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Rather than add more rules to guess which calls the loader rebind reaches, ab65955 drops the inference. The scan now exempts exactly three reviewed sites, agent.py:31, :47 and :156. All three are function-body open() calls in a module laya/init.py imports eagerly, and they're exempt only while laya_runtime still rebinds open. The vendored files are pinned by sha256, so the lines can't drift. Any new, moved or removed vendored call, including default-argument, lambda-default or lazily imported submodule cases, turns the scan red until someone re-reviews it. A reviewed site that disappears also fails as stale.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: ab65955234

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit e7811ae into main Sep 28, 2026
21 checks passed
@danielhanchen
danielhanchen deleted the fix-runtime-encoding-laya branch September 28, 2026 21:05
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…upplies UTF-8 (unslothai#12230)

* Scope the runtime encoding scan past vendored laya while its loader supplies UTF-8

* Exempt only the vendored open calls the loader swap reaches

* Pin the reviewed vendored laya open sites instead of inferring loader coverage
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