Files
deepseek-harness/packages/code-runtime/code-runtime-python/tests/residual-detach.spec.ts
T
Chinesezjc c388169cff feat(code-runtime-python): add the CPython subprocess backend
Land the PythonCodeRuntime implementation on top of the fd-3 protocol
seam: python3 -I per run, binding namespace over fd 3, RLIMIT_CPU/AS,
wall-clock timer, and SIGTERM->grace->SIGKILL process-group teardown,
with the real-subprocess integration suite.

Fixes three defects surfaced on the source PR's review before they ship:
- boot-write failure resolved a worker-exit through finish()/settle()
  that read wallTimer/onAbort/live in their TDZ, rejecting run() instead;
  the boot write now runs after those bindings and the v8-ignore that hid
  the branch is removed.
- log capture serialized against settlement with no lock while model
  daemon threads keep writing; LogBuffer now owns one shared re-entrant
  lock taken by write/flush_line/push.
- the fd-3 line residual was a subarray view pinning the whole joined
  frame; it is copied into a right-sized Buffer via detachResidual so
  pendingBytes measures what is retained.
2026-08-31 14:14:32 +08:00

30 lines
1.4 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { detachResidual } from '../src/index.ts'
describe('detachResidual — fd-3 residual detachment', () => {
it('returns a copy that does NOT share the source frame allocation', () => {
// Simulate the data handler's state: one large joined frame from
// Buffer.concat, sliced past its newline to leave a small residual VIEW.
const joined = Buffer.alloc(1024 * 1024, 0x61) // 1 MiB backing allocation
joined[512] = 0x0a // a newline partway through
const residual = joined.subarray(513) // a view onto `joined`'s backing store
// Before the fix the handler carried this view forward verbatim, pinning the
// whole 1 MiB `joined` allocation behind a residual that reports far fewer
// bytes. A right-sized copy must not point back into `joined`.
const [carried] = detachResidual(residual)
expect(carried).toBeDefined()
expect(carried!.length).toBe(residual.length)
expect(carried!.equals(residual)).toBe(true)
// The copy's backing store is its own, sized to its content — not the 1 MiB
// frame. A subarray view would report the source's full byteLength here.
expect(carried!.buffer.byteLength).toBe(carried!.length)
expect(carried!.buffer).not.toBe(joined.buffer)
})
it('carries nothing forward for an empty residual', () => {
expect(detachResidual(Buffer.alloc(0))).toEqual([])
})
})