Skip to content

fix(benchmark): stop reading oversized preparation resources - #550

Draft
Inference1 wants to merge 1 commit into
InferMatrixLab:mainfrom
Inference1:fix/benchmark-preparation-resource-size
Draft

Inference1 wants to merge 1 commit into
InferMatrixLab:mainfrom
Inference1:fix/benchmark-preparation-resource-size

Conversation

@Inference1

Copy link
Copy Markdown

Governing Issue and delivery slice

Refs #430

This proposed follow-up covers the fixed size and digest checks in the existing P1 preparation port. The accepted preparation scope defines Benchmark's verification and Apps' archival responsibilities.

Problem

PreparationService.prepare reads each resource stream to EOF before checking its frozen length. Once an extra byte establishes a mismatch, another read can still fail and turn that integrity failure into ArtifactUnavailable.

What

Bound each binary read by the declared remaining length plus one byte, up to the existing 1 MiB chunk size. Close an oversized stream and return ArtifactIntegrityError as soon as it supplies the first extra byte.

Why needed

Preparation can reject an invalid resource promptly and give callers the reason already established by the bytes. Apps receives a prepared reference only after the resource closure passes verification.

Design and implementation

The private Benchmark verifier retains the injected role-keyed opener and stream ownership. It checks exact length and SHA-256, including streams that supply short chunks or resources with a declared length of zero. Apps still verifies the bytes it reopens for archival. The fix adds no dependencies or contract fields.

Checks and dependencies

  • The new Apps T36 regression returns ArtifactUnavailable on 9466bc8. At the fix, it returns ArtifactIntegrityError at prepare, reads once, closes the source and leaves the archive empty.
  • Linux, Python 3.12. Benchmark preparation, Apps prepare and target import gate. 123 passed, 43 subtests passed. Targeted Ruff and git diff --check passed.
  • Upstream base 9466bc8 already fails test_project_explanation.py::test_recovered_attempt_does_not_trust_a_rewritable_declaration due to its import of the removed inferrsi.infra.artifacts module.

Remaining work and Issue closure

This draft requests maintainer scope confirmation and formal review of the preparation follow-up. Broader P1 acceptance remains with the governing Issue owner.

Bound each preparation read by the frozen resource size. Report the first excess byte as an integrity failure and continue to verify exact-size resources by digest.

Agent-assisted-by: Codex (author)
Signed-off-by: Inference1 <3617237115@qq.com>

@zengchuang-hw zengchuang-hw 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.

NO PASS

首轮独立检视:9466bc8d910c9fef55d4a9e23ee1a82950280c30 → fcecdc547566c740f67e41d46b47e1cb6e5a6dc7。这两处改动属于 #430 的冻结资源校验范围,不代表整个 P1 已验收。

批准前必修:关闭流失败仍会掩盖已确定的超长错误。 preparation.py:352–362 在退出 closing(source) 后才报告完整性错误。如果已读到第一个多余字节,但 close() 抛出 OSError,_prepare 的异常映射 仍会把结果变成 ArtifactUnavailable。请保留关闭尝试,但不要让它覆盖已确定的 ArtifactIntegrityError;建议在现有 Apps 回归旁补测该路径,断言错误阶段仍为 prepare 且无 PreparedRef、无归档。尚未取得完整性结论时的 I/O 失败不需要改分类。

实际验证:独立执行该 head 的校验函数,9 个聚焦用例通过,覆盖零长度、短分块、跨 1 MiB、首个多余字节、截断和摘要不符等;另确认读到超长后 close() 的 OSError 会逃出校验函数,并静态核对其外层映射。未运行完整 Apps/P1 测试或查看 CI。

Copy link
Copy Markdown
Collaborator

Thanks for the tightly scoped fix. I reviewed 9466bc8d910c9fef55d4a9e23ee1a82950280c30 → fcecdc547566c740f67e41d46b47e1cb6e5a6dc7.

The bounded read in _verify_resource is the right direction: streams are read with min(1 MiB, remaining + 1), hashing stops at the declared length, and the first extra byte is an ArtifactIntegrityError rather than a later I/O failure. Protocol validation already requires size_bytes / sha256 before this check, and test_t36_oversized_resource_stops_at_the_first_extra_byte covers the original second-read OSError path.

I also checked the current-head review from @zengchuang-hw and agree it is still blocking:

  • In src/inferrsi/benchmark/preparation.py, _require(...) runs only after closing(source) exits. If the first extra byte has already established an integrity failure, a close() OSError still escapes _verify_resource and is mapped by _prepare to ArtifactUnavailable. That is the same class of misclassification this PR is fixing.

Please still attempt to close the stream, but do not let a close failure replace an already determined ArtifactIntegrityError. A regression next to the existing Apps T36 case should show the error stays ArtifactIntegrityError at prepare, with no PreparedRef and no archive entry. I/O failures before an integrity conclusion can stay ArtifactUnavailable.

I am not approving this head.

@Dong1017 Dong1017 added the kind:task Bounded delivery task with a delegated slice label Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:task Bounded delivery task with a delegated slice

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants