fix(code-runtime-python): flush logs before framing the completion value

The load gate bounds maxLogBytes and maxValueBytes independently against the
address space, but the child framed the completion value (materializing its
escaped form to meter it, then encoding the frame) while a newline-free log tail
still sat unflushed in _pending. Those two peaks added, so two budgets each
admitted alone could together breach RLIMIT_AS and die as worker-exit instead of
settling. The success path now flushes both log streams before _done_with_value
runs; the trailing flush stays for the exception path and is an idempotent no-op
after a successful settle. A combined-peak regression test (32 MiB each against
512 MiB) asserts the over-budget value reports output-limit rather than OOMing.

Also corrects the worst-case-multiple JSDoc and Agent Note: after 1088d6f03d
made flush_line drop pending before its push, the settlement-flush path holds
two copies, not three, so the newline path is the sole 12x worst case. The
reorder is recorded as a called-out untested fix (the 12x gate already admits
only configs safe under both flush orders).
This commit is contained in:
Chinesezjc
2026-08-31 14:24:59 +08:00
committed by Tianyi Cui
parent 9d9525549d
commit 9a8663cc4c
6 changed files with 65 additions and 16 deletions
@@ -240,13 +240,14 @@ const CLOSE_REAP_MARGIN_MS = 2_000
* as a multiple of the budget. The child's ledgers trigger on CHARACTER count
* against a serialized-BYTE budget, and an astral character is one character but
* four bytes of CPython `str` storage and four UTF-8 bytes — so a budget's worth
* of astral characters is ~4x the budget in each string that holds it. THREE
* such copies are live at the peak: on the newline path a single
* `sys.stdout.write(line + "\n")` holds the caller's `text` argument (alive for
* of astral characters is ~4x the budget in each string that holds it. The
* heaviest path holds THREE such copies at once: a single
* `sys.stdout.write(line + "\n")` keeps the caller's `text` argument (alive for
* the whole `write` call, ~4x), the line slice `text[pos:newline]` handed to
* `LogBuffer.push` (~4x), and the `text.encode("utf-8")` copy `_push_locked`
* takes to charge and ship it (~4x); the settlement `flush_line` path holds the
* pending chunks, their `"".join(...)`, and that same encode copy. Twelve covers
* takes to charge and ship it (~4x). The settlement `flush_line` path holds only
* two (its `"".join(...)` and that encode copy — it drops the pending chunks
* before pushing), so the newline path is the binding worst case. Twelve covers
* those three simultaneous ~4x copies. The interpreter baseline is NOT in this
* multiple — it is reserved separately as {@link INTERPRETER_BASELINE_BYTES} —
* because it is a fixed cost, not one that scales with the budget. Used to bound