Skip to content

fix(livecodebench): expose .buffer on mocked stdin/stdout - #518

Open
yqy003 wants to merge 2 commits into
AISBench:masterfrom
yqy003:fix/lcb-stdio-buffer
Open

yqy003 wants to merge 2 commits into
AISBench:masterfrom
yqy003:fix/lcb-stdio-buffer

Conversation

@yqy003

@yqy003 yqy003 commented Sep 6, 2026

Copy link
Copy Markdown

The sandbox that runs generated solutions replaces sys.stdin with a plain StringIO (testing_util.call_method) and sys.stdout with a plain StringIO (testing_util.Capturing). Neither exposes the binary .buffer view that the real standard streams have, so any solution using the common competitive programming fast-IO idiom crashes:

data = sys.stdin.buffer.read().split()
sys.stdout.buffer.write(str(ans).encode())

AttributeError: '_io.StringIO' object has no attribute 'buffer'

The exception is swallowed and recorded as -1 (Runtime Error), so the solution silently scores 0 and pass@1 is under-reported. How large the loss is depends only on how often the evaluated model happens to use that idiom, which is why it can go unnoticed for a long time.

Measured on LiveCodeBench v6 (release_version 2025-05, 175 problems) with a model that uses sys.stdin.buffer in 68 of its 175 answers: 67 of the 74 failures were this AttributeError. After the fix 58 of them pass and pass@1 goes from 57.71 to 90.86; the 9 remaining failures are genuine (7 wrong answers, 1 IndexError, 1 TLE).

Fix:

  • _StdinWithBuffer backs .buffer with a BytesIO over the same input.
  • _StdoutWithBuffer._Buffer decodes binary writes and forwards them into the same StringIO, so Capturing.__exit__'s getvalue() still returns the complete output. A separate BytesIO would have turned the Runtime Error into an equally wrong empty-output Wrong Answer.

Adds 4 regression tests to tests/UT/datasets/livecodebench/, which fail before this change and pass after it.

Thanks for your contribution; we appreciate it a lot. The following instructions will make your pull request healthier and help you get feedback more easily. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.
感谢您的贡献,我们非常重视。以下说明将使您的拉取请求更健康,更易于获得反馈。如果您不理解某些项目,请不要担心,只需提交拉取请求并从维护人员那里寻求帮助即可。

PR Type / PR类型

  • Feature(功能新增)
  • Bugfix(Bug 修复)
  • Docs(文档更新)
  • CI/CD(持续集成/持续部署)
  • Refactor(代码重构)
  • Perf(性能优化)
  • Dependency(依赖项更新)
  • Test-Cases(测试用例更新)
  • Other(其他)

Related Issue | 关联 Issue
Fixes #(issue ID / issue 编号) / Relates to #(issue ID / issue 编号)

🔍 Motivation / 变更动机

Please describe the motivation of this PR and the goal you want to achieve through this PR.
请描述您的拉取请求的动机和您希望通过此拉取请求实现的目标。

📝 Modification / 修改内容

Please briefly describe what modification is made in this PR.
请简要描述此拉取请求中进行的修改。

📐 Associated Test Results / 关联测试结果

Please provide links to the related test results, such as CI pipelines, test reports, etc.
请提供相关测试结果的链接,例如 CI 管道、测试报告等。

⚠️ BC-breaking (Optional) / 向后不兼容变更(可选)

Does the modification introduce changes that break the backward compatibility of the downstream repositories? If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.
是否引入了会破坏下游存储库向后兼容性的更改?如果是,请描述它如何破坏兼容性,以及下游项目应该如何修改其代码以保持与此 PR 的兼容性。

⚠️ Performance degradation (Optional) / 性能下降(可选)

If the modification introduces performance degradation, please describe the impact of the performance degradation and the expected performance improvement.
如果引入了性能下降,请描述性能下降的影响和预期的性能改进。

🌟 Use cases (Optional) / 使用案例(可选)

If this PR introduces a new feature, it is better to list some use cases here and update the documentation.
如果此拉取请求引入了新功能,最好在此处列出一些用例并更新文档。

✅ Checklist / 检查列表

Before PR:

  • Pre-commit or other linting tools are used to fix the potential lint issues. / 使用预提交或其他 linting 工具来修复潜在的 lint 问题。
  • Bug fixes are fully covered by unit tests, the case that causes the bug should be added in the unit tests. / 修复的 Bug 已完全由单元测试覆盖,导致 Bug 的情况应在单元测试中添加。
  • The modification is covered by complete unit tests. If not, please add more unit tests to ensure the correctness. / 此拉取请求中的修改已完全由单元测试覆盖。如果不是,请添加更多单元测试以确保正确性。
  • All relevant documentation (API docs, docstrings, example tutorials) has been updated to reflect these changes. / 所有相关文档(API 文档、文档字符串、示例教程)已更新以反映这些更改。

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with those projects. / 如果此拉取请求对下游或其他相关项目有潜在影响,应在那些项目中测试此 PR。
  • CLA has been signed and all committers have signed the CLA in this PR. / CLA 已签署,且本 PR 中的所有提交者均已签署 CLA。

👥 Collaboration Info / 协作信息

  • Suggested Reviewers / 建议审核人: @xxx
  • Relevant Module Owners / 相关模块负责人: @xxx
  • Other Collaboration Notes / 其他协作说明:

🌟 Useful CI Command / 实用的CI命令

Command / 命令 Introduction / 介绍
/gemini review Performs a code review for the current pull request in its current state by Gemini. / 对当前拉取请求在当前状态下由 Gemini 执行代码审核。
/gemini summary Provides a summary of the current pull request in its current state by Gemini. / 对当前拉取请求在当前状态下由 Gemini 提供摘要。
/gemini help Displays a list of available commands of Gemini. / 显示 Gemini 可用命令的列表。
/readthedocs build Triggers a build of the documentation for the current pull request in its current state by Read the Docs. / 触发当前拉取请求在当前状态下由 Read the Docs 构建文档。
/pr_check 手动重新触发 PR 合入质量检查工作流(PR Quality Check),并在 PR 上评论即可生效。 / Manually re-runs the PR Quality Check workflow by commenting on the pull request.

The sandbox that runs generated solutions replaces sys.stdin with a plain
StringIO (testing_util.call_method) and sys.stdout with a plain StringIO
(testing_util.Capturing). Neither exposes the binary `.buffer` view that the
real standard streams have, so any solution using the common competitive
programming fast-IO idiom crashes:

    data = sys.stdin.buffer.read().split()
    sys.stdout.buffer.write(str(ans).encode())

    AttributeError: '_io.StringIO' object has no attribute 'buffer'

The exception is swallowed and recorded as -1 (Runtime Error), so the
solution silently scores 0 and pass@1 is under-reported. How large the loss
is depends only on how often the evaluated model happens to use that idiom,
which is why it can go unnoticed for a long time.

Measured on LiveCodeBench v6 (release_version 2025-05, 175 problems) with a
model that uses `sys.stdin.buffer` in 68 of its 175 answers: 67 of the 74
failures were this AttributeError. After the fix 58 of them pass and pass@1
goes from 57.71 to 90.86; the 9 remaining failures are genuine (7 wrong
answers, 1 IndexError, 1 TLE).

Fix:
- `_StdinWithBuffer` backs `.buffer` with a BytesIO over the same input.
- `_StdoutWithBuffer._Buffer` decodes binary writes and forwards them into
  the same StringIO, so `Capturing.__exit__`'s `getvalue()` still returns the
  complete output. A separate BytesIO would have turned the Runtime Error
  into an equally wrong empty-output Wrong Answer.

Adds 4 regression tests to tests/UT/datasets/livecodebench/, which fail
before this change and pass after it.
@github-actions github-actions Bot added no_issue PR 未关联任何 Issue,合入前必须关联 Issue no_review PR 未包含任何检视意见,合入前必须包含检视意见 labels Sep 6, 2026
@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown

🚫 PR 合入质量检查未通过,PR 已被禁止合入。

未通过项:no_issue、no_review

  • 未关联 Issue:请在 PR 描述中关联 Issue,例如 Fixes #123 或 Relates to #123。
  • 无检视意见:请至少邀请一位 reviewer 提交检视意见(Approve / Request changes / Comment 均可)。

请按上述提示整改后,重新推送(push)或评论本 PR 即可触发再次检查。

@yqy003

yqy003 commented Sep 6, 2026

Copy link
Copy Markdown
Author

Closes #517

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Three moderate issues remain regarding stream positioning, incremental UTF-8 decoding, and import-time exception handling.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Medium severity

Open (2)
What changed in this PR

Fixes LiveCodeBench mocked stdin/stdout streams to expose .buffer for binary fast I/O.

Changes:

  • Adds binary stdin and stdout wrappers.
  • Adds four regression tests for binary and mixed I/O.
  • Preserves captured output through binary writes.
File Summary
ais_bench/​benchmark/​datasets/​livecodebench/​testing_util.py Adds .buffer support. Findings: moderate shared-position issue (2 votes) and moderate split UTF-8 write issue (1 vote).
tests/​UT/​datasets/​livecodebench/​test_livecodebench.py Adds regression coverage. Finding: moderate import-exception handling issue (3 votes).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +709 to +711
def __init__(self, data):
super().__init__(data)
self.buffer = io.BytesIO(data.encode())
Comment on lines +443 to +444
except ImportError:
raise unittest.SkipTest('testing_util not available')
@github-actions github-actions Bot added review_opened PR 中存在未闭环的检视意见,所有检视意见必须闭环后才能合入 and removed no_review PR 未包含任何检视意见,合入前必须包含检视意见 labels Sep 23, 2026
@github-actions

Copy link
Copy Markdown

🚫 PR 合入质量检查未通过,PR 已被禁止合入。

未通过项:no_issue、review_opened

  • 未关联 Issue:请在 PR 描述中关联 Issue,例如 Fixes #123 或 Relates to #123。
  • 检视意见未闭环:还存在 2 条未 Resolve 的检视意见,请全部 Resolve 后再合入。

请按上述提示整改后,重新推送(push)或评论本 PR 即可触发再次检查。

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

[review] 动机核实结论:合理,且与上游问题完全对应。上游 LiveCodeBench 官方仓库确有同题 issue:LiveCodeBench/LiveCodeBench#160 "Code-generation judge rejects valid solutions using sys.stdin.buffer.readline() or sys.stdout.buffer"(https://github.com/LiveCodeBench/LiveCodeBench/issues/160,目前仍 open),正文明确指出 Capturing 把 sys.stdout 换成 StringIO 后没有 .buffer,sys.stdout.buffer.write(...) 会抛 AttributeError:并用一次完整 175 题 LCB v6 重判量化了影响(官方判 154/175,修好二进制 IO 后 159/175,涉及 abc393_e/abc398_f/arc191_a)。对应修复 PR 为 LiveCodeBench/LiveCodeBench#161 "Fix binary stdin and stdout handling in the code-generation judge"(open,尚未合并,同时修 stdin 游标与 stdout.buffer),其他 fork 也有同类改动(wasiahmad/livecodebench#1、RouteWorks/RouterArena#234)。我核对上游 main 的 lcb_runner/evaluation/testing_util.py,其 Capturing 仍是裸 StringIO,说明该 fix 上游尚未落地,本 PR 的方向正确。

需要说明的两点差异:1) 本 PR 标题写的是「stdin/stdout」,但改动只补了 stdout(testing_util.py 里 pre-PR 就有 BytesIO/TextIOWrapper 的 stdin mock,tests/UT/.../test_testing_util.py 里未被本 PR 修改的 test_call_method_supports_stdin_buffer 也已在验证 sys.stdin.buffer),所以新增的两个 stdin 用例只是既有行为的回归覆盖,并非本次修复对象;2) 上游 #161 还修了官方 MockBuffer.readline「连续调用永远返回第一行」的缺陷,本 PR 的新用例只做了单次 read/readline,覆盖不到这类问题,若本仓库的 stdin mock 与上游同源,建议补一条连续两次 readline 的用例。另外实现细节上还有返回值语义、非 UTF-8 字节、buffer 接口不完整三处小问题,见下。此外可留意:同类「mock 流缺少 .buffer」问题在 execute_utils.py 的 WriteOnlyStringIO(swallow_io)中也存在,若该路径也用于判 LCB 选手代码,可能残留误判(不在本 diff 内,故未给行号)。

def write(self, data):
if isinstance(data, (bytes, bytearray)):
data = data.decode()
return self._owner.write(data)

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.

[review] 返回值语义与真实 sys.stdout.buffer 不一致:真实 file/buffer 对象的 write() 返回「写入的字节数」,这里返回的是 StringIO.write 的「字符数」。对多字节 UTF-8 输出两者不等,若生成代码用 n = sys.stdout.buffer.write(data) 或断言写入长度(这类写法在竞赛代码里存在),仍会被判错,偏离「不再误判」的目标。建议在解码前记录字节长度并返回它,例如:raw = data if isinstance(data, (bytes, bytearray)) else data.encode(); self._owner.write(raw.decode()); return len(raw)。


def write(self, data):
if isinstance(data, (bytes, bytearray)):
data = data.decode()

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.

[review] data.decode() 使用默认 UTF-8 且无 errors 参数:真实 stdout.buffer 可写入任意字节,而这里遇到非 UTF-8 字节(如 latin-1、部分二进制串)会抛 UnicodeDecodeError;另外若一次多字节字符被拆成两次 write(分批 flush 的写法),第二次解码也会失败。这些异常仍会被 run_test 捕获成 Runtime Error,属于本次要修的问题的残留。建议改为 data.decode('utf-8', errors='replace')(或 'surrogateescape'),或按字节缓冲、只在 getvalue/比较阶段统一解码。

for line in lines:
self.write(line)

def flush(self):

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.

[review] _Buffer 只实现了 write/writelines/flush,缺少 close/isatty/fileno 等常见接口,且没有 getattr 兜底;生成代码里出现 sys.stdout.buffer.close()、sys.stdout.buffer.isatty()、fileno() 时依旧 AttributeError → 仍被记成 Runtime Error。建议补上 close()(no-op)、isatty() -> False,或加 __getattr__ 委托给 owner,使伪 buffer 更接近真实对象。顺带一提:同一 hunk 里的 self._stringio.close = lambda x: 1 需要一个位置参数,调用 sys.stdout.close()(真实 stdout 无参)会抛 TypeError,属既有代码,但既然在补 stdin/stdout 伪装,建议一并改成 lambda *a, **kw: 1。

' print(sum(map(int, data)))\n')
self.assertEqual(self._run(source, '1 2\n3\n'), '6')

def test_stdin_buffer_readline(self):

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.

[review] 新增的 stdin 用例与既有覆盖重复且没有覆盖上游已知缺陷:call_method 在本 PR 前就已支持 sys.stdin.buffer(见同目录 test_testing_util.py 中未改动的 test_call_method_supports_stdin_buffer / test_call_method_shares_text_and_binary_stream_position),本次改动只作用于 stdout,所以 test_stdin_buffer_read / test_stdin_buffer_readline 属于既有行为的重复验证。更值得补的是上游 issue #160 指出的那个坑:官方 MockBuffer.readline() 永远返回第一行,连续两次 sys.stdin.buffer.readline() 会拿到同一行导致 Wrong Answer(如 abc391_f)。建议把这两个用例合并/精简,并新增一条连续两次 readline 应返回两行、以及 text(input()) 与 binary 读取共享位置的多行用例,才能真正防住这类误判。

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

no_issue PR 未关联任何 Issue,合入前必须关联 Issue review_opened PR 中存在未闭环的检视意见,所有检视意见必须闭环后才能合入

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants