Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM python:3.12-slim@sha256:57cd7c3a7a273101a6485ba99423ee568157882804b1124b4dd04266317710de
RUN python -m pip install --no-cache-dir attrs==26.1.0 jsonschema==4.26.0 jsonschema-specifications==2025.9.1 referencing==0.37.0 rpds-py==2026.6.3 typing-extensions==4.16.0
LABEL jacobian.checksum="63ab71931d2312ef7d7a296f3caf1d2ae40024e783be96c302d3e151338b84dd"
LABEL jacobian.checksum="6036fee901aab3df23258e3cbbbaa97da624f0bbd14ae34d0602d58927ad0a1e"
LABEL jacobian.task="jacobian/exact-farkas-ldl-slice"
COPY expected.json input.json test.sh verifier.py verifier_support.py public_contract.json /tests/
COPY input.json /app/input.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
W, E = Path("/app"), Path("/tests")
MAX_INPUT_BYTES = 1_048_576
MAX_SUBMISSION_BYTES = 1_048_576
MAX_EVIDENCE_BYTES = 1_048_576


def _load_frozen():
Expand Down Expand Up @@ -198,12 +197,10 @@ def main():
and isinstance(submission.get("evidence"), list)
and len(submission["evidence"]) == 1
):
evidence_path = W / "evidence" / "farkas-slice-certificate.json"
if is_regular_bounded_file(evidence_path, max_bytes=MAX_EVIDENCE_BYTES):
evidence = read_evidence_json(
submission["evidence"][0],
expected_path="evidence/farkas-slice-certificate.json",
)
evidence = read_evidence_json(
submission["evidence"][0],
expected_path="evidence/farkas-slice-certificate.json",
)
evidence_valid = bool(
evidence
and set(evidence) == {"schema_version", "task_id", "result", "limitations"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import codecs
import hashlib
import json
import math
Expand Down Expand Up @@ -262,14 +263,57 @@ def resolve_evidence(
return target


_JSON_WHITESPACE = frozenset(" \t\n\r")
_JSON_WHITESPACE_CHARS = " \t\n\r"


def _drain_stream_tail(stream, decoder) -> None:
"""Reject any non-whitespace content after the parsed JSON value."""

while True:
block = stream.read(65_536)
if not block:
break
tail = decoder.decode(block)
if tail and not all(character in _JSON_WHITESPACE for character in tail):
raise ValueError("non-whitespace after evidence JSON value")
tail = decoder.decode(b"", final=True)
if tail and not all(character in _JSON_WHITESPACE for character in tail):
raise ValueError("non-whitespace after evidence JSON value")


def _read_streaming_json_value(stream) -> Any:
"""Parse one JSON value without retaining arbitrary whitespace padding."""

decoder = codecs.getincrementaldecoder("utf-8")()
parser = json.JSONDecoder(object_pairs_hook=_reject_duplicate_keys)
buffer = ""
while True:
block = stream.read(65_536)
if block:
buffer += decoder.decode(block)
if buffer[:1] in _JSON_WHITESPACE:
buffer = buffer.lstrip(_JSON_WHITESPACE_CHARS)
try:
value, end = parser.raw_decode(buffer)
Comment on lines +294 to +298

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Stream internal JSON whitespace instead of buffering it

When a schema-valid evidence object contains large legal whitespace between tokens (rather than only at the beginning or end covered by the new test), this loop retains the entire prefix and reruns raw_decode over it after every 64 KiB read. Runtime therefore grows quadratically: a 64 MiB string/prefix already takes about 30 seconds locally, so a roughly 128 MiB artifact within the task's 4 GiB storage allowance can exceed the 120-second verifier timeout and produce no deterministic reward. Parse the bounded evidence schema incrementally, including discarding whitespace at token boundaries, so removing the unpublished byte cap does not replace it with a practical timeout cap.

AGENTS.md reference: AGENTS.md:L180-L183

Useful? React with 👍 / 👎.

except json.JSONDecodeError:
if not block:
raise
continue
if not all(character in _JSON_WHITESPACE for character in buffer[end:]):
raise ValueError("non-whitespace after evidence JSON value")
_drain_stream_tail(stream, decoder)
return value


def read_evidence_json(
descriptor: object,
*,
expected_path: str,
workspace: Path = WORKSPACE,
max_bytes: int | None = None,
) -> dict[str, Any] | None:
"""Resolve and parse a digest-bound evidence object."""
"""Resolve and stream a digest-bound evidence object without a byte cap."""

target = resolve_evidence(
descriptor,
Expand All @@ -280,10 +324,8 @@ def read_evidence_json(
if target is None:
return None
try:
value = json.loads(
target.read_text(),
object_pairs_hook=_reject_duplicate_keys,
)
with target.open("rb") as stream:
value = _read_streaming_json_value(stream)
except (OSError, ValueError, RecursionError, MemoryError):
return None
return value if isinstance(value, dict) else None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,58 @@ def test_exact_farkas_slice_rejects_missing_evidence_envelope(tmp_path: Path) ->
rejected = support._run_verifier(task, app, logs)
assert rejected.details["evidence_validity"] == 0.0
assert rejected.reward == 0.0


def test_exact_farkas_slice_accepts_large_valid_evidence_padding(
tmp_path: Path,
) -> None:
"""Legal JSON whitespace must not create a hidden evidence-size limit."""
task, app, logs = _prepare_farkas_slice_case(tmp_path)
submission = json.loads((app / "submission.json").read_text())
evidence_path = app / "evidence" / "farkas-slice-certificate.json"
evidence_path.write_text(
" \n" * (600 * 1024)
+ json.dumps(
{
"schema_version": "1",
"task_id": submission["task_id"],
"result": submission["result"],
"limitations": submission["limitations"],
},
sort_keys=True,
separators=(",", ":"),
)
+ "\n"
+ " " * (600 * 1024)
)
submission["evidence"][0]["sha256"] = support._digest(evidence_path)
support._write_json(app / "submission.json", submission)

accepted = support._run_verifier(task, app, logs)
assert accepted.details["evidence_validity"] == 1.0
assert accepted.reward == pytest.approx(1.0)


@pytest.mark.parametrize("prefix,suffix", [("garbage", ""), ("", "garbage")])
def test_exact_farkas_slice_rejects_evidence_json_garbage(
tmp_path: Path,
prefix: str,
suffix: str,
) -> None:
"""Streaming acceptance must still reject content outside the JSON value."""
task, app, logs = _prepare_farkas_slice_case(tmp_path)
submission = json.loads((app / "submission.json").read_text())
evidence_path = app / "evidence" / "farkas-slice-certificate.json"
payload = {
"schema_version": "1",
"task_id": submission["task_id"],
"result": submission["result"],
"limitations": submission["limitations"],
}
evidence_path.write_text(prefix + json.dumps(payload) + suffix)
submission["evidence"][0]["sha256"] = support._digest(evidence_path)
support._write_json(app / "submission.json", submission)

rejected = support._run_verifier(task, app, logs)
assert rejected.details["evidence_validity"] == 0.0
assert rejected.reward == 0.0
Loading