fix(code-runtime-python): cap the done-frame rejection diagnostic and sync stale docs

The review's remaining items:
- _done_with_value's rejection branch now caps the _check_done_value diagnostic
  through _cap_message (a reason embedding a hostile class name could otherwise
  push the done frame past the host's 64 MiB parse cap, misreporting an
  invalid-output run as a worker-exit).
- The settlement note (en + zh) updates three stale facts (load bound is now
  parse-cap minus envelope at 67108800; the sink goes directly through the
  bound primitives); the fd-3 protocol note (en + zh) no longer claims the
  package ships protocol without the runtime; FRAME_ENVELOPE_BYTES' JSDoc and
  _cap_message's docstring follow the new bound.
Pairings re-recorded.
This commit is contained in:
Chinesezjc
2026-08-31 14:52:57 +08:00
committed by Tianyi Cui
parent d90155714b
commit 3f8b45f9bb
8 changed files with 19 additions and 12 deletions
@@ -2172,7 +2172,7 @@ def _cap_message(message: str, max_bytes: int) -> str:
roughly six times that and breach the 256 MiB frame ceiling — the silent
``worker-exit`` inversion the load-time cap check exists to prevent, and a
several-hundred-MiB escape allocation besides. The seam's load bound admits
``maxValueBytes`` up to ``ceiling - envelope`` on the premise that both the
``maxValueBytes`` up to ``parse-cap - envelope`` on the premise that both the
completion value and the diagnostic are metered in serialized bytes, so this
honors that premise for the diagnostic.
@@ -2408,6 +2408,7 @@ def _done_with_value(
# `exception`. Defaults are evaluated at def time, so they are the originals.
_check_done_value: Any = _check_done_value,
_encode_json_plain: Any = _encode_json_plain,
_cap_message: Any = _cap_message,
) -> dict[str, Any] | str:
"""Build the terminal done frame under the seam's lossless-JSON contract.
@@ -2444,7 +2445,12 @@ def _done_with_value(
rejection = _check_done_value(value, max_value_bytes)
if rejection is not None:
kind, message = rejection
return {"type": "done", "error": {"kind": kind, "message": message}}
# The rejection diagnostic is capped like an exception message: a
# reason embedding a hostile class name (a huge `type(value).__name__`)
# could otherwise make the done frame exceed the host's frame parse cap
# and be silently dropped — an invalid-output run misreported as a
# worker-exit.
return {"type": "done", "error": {"kind": kind, "message": _cap_message(message, max_value_bytes)}}
# Pre-encode the value at the validation point (not in `_run`'s later send,
# which is outside the try): see the TOCTOU note in the docstring. The value
# is JSON-plain by construction, so `_encode_json_plain` is the encoder.
@@ -231,7 +231,8 @@ const MAX_PENDING_CHUNKS = 1024
/**
* Bytes a frame spends on its own JSON structure around a capped payload, used
* to bound `maxLogBytes`/`maxValueBytes` against {@link FRAME_CEILING_BYTES}.
* to bound `maxLogBytes`/`maxValueBytes` against {@link FRAME_PARSE_CAP_BYTES}
* (the receive path drops raw frames past that cap before decoding).
* The widest carrier is `{"type":"log","text":"","truncated":true}` at 41
* bytes; 64 rounds that up so adding a field to either frame does not silently
* invalidate the bound. A protocol constant, not a deployment choice.