Browse Source

docs(code-runtime-python): trim metering prose and cover encodeJsonPlain depth

- Drop the review-history narrative from checkDoneValue's JSDoc (the "in the
  previous implementation … now avoids" clause); state the current contract only.
- Assert encodeJsonPlain on the same 100k-deep value the metering test uses:
  its headline contract is stack-safety (JSON.stringify would throw), but no
  test exercised the encoder on a deep value.
Chinesezjc 1 month ago
parent
commit
203bfca0ea

+ 3 - 3
packages/code-runtime/code-runtime-python/src/protocol.ts

@@ -387,9 +387,9 @@ function jsonStringBytesUpTo(text: string, maxBytes: number): number | undefined
  * Meter a `JSON.parse`-produced done value's compact-JSON byte length AND its
  * number losslessness in one traversal, stopping the instant `maxBytes` is
  * crossed. This bounds the INCREMENTAL allocation the check itself would add on
- * top of the already-parsed value — the enqueued children (and, in the previous
- * implementation, an escaped-string copy that {@link jsonStringBytesUpTo} now
- * avoids) — not the parse that produced `value`.
+ * top of the already-parsed value — the enqueued children; strings and keys are
+ * metered by {@link jsonStringBytesUpTo} without allocating an escaped copy —
+ * not the parse that produced `value`.
  * That upstream width is bounded separately, by the host-side cap on inbound
  * fd-3 frame size before `JSON.parse` runs (owned by the runtime that reads the
  * channel), so `value` cannot be arbitrarily large when it reaches here, while

+ 5 - 1
packages/code-runtime/code-runtime-python/tests/protocol.spec.ts

@@ -283,11 +283,15 @@ describe('checkDoneValue', () => {
     expect(checkDoneValue(Infinity, 3)).toEqual({ ok: false, reason: 'over-budget' })
   })
 
-  it('meters deep nesting iteratively without overflowing the stack', () => {
+  it('meters and encodes deep nesting iteratively without overflowing the stack', () => {
     let deep: unknown = 0
     for (let i = 0; i < 100_000; i++) deep = [deep]
     // 100000 '[' + '0' + 100000 ']' = 200001 bytes.
     expect(checkDoneValue(deep, 1_000_000)).toEqual({ ok: true, bytes: 200_001 })
+    // encodeJsonPlain's headline contract is the same stack-safety (JSON.stringify
+    // recurses per level and throws RangeError a few thousand deep), so exercise
+    // it on the same 100k-deep value — JSON.stringify would throw here.
+    expect(encodeJsonPlain(deep)).toBe(`${'['.repeat(100_000)}0${']'.repeat(100_000)}`)
   })
 
   it('emits exact digits for beyond-safe integral doubles', () => {