fix(code-runtime-python): commit a flushed open prefix before the truncation marker

The review's warning: a flushed unterminated line is billed and committed
(README wire contract says so), but every truncation arm — the child truncated
frame, an over-budget open frame, an over-budget closing frame, and admit's two
budget arms — pushed only the marker, dropping the held prefix: the ledger
charged for output that vanished. All arms now funnel through truncateLogs(),
which pushes the (already billed) held prefix before the marker and clears
openParts, so the prefix survives and only the marker stays last; the finish()
guard drops the now-dead !logsTruncated check (a truncated run has an empty
hold). A regression case asserts [prefix, marker]; the forged-flood and
closing-overflow cases now expect the committed prefix plus the marker.
This commit is contained in:
Chinesezjc
2026-08-31 15:04:43 +08:00
committed by Tianyi Cui
parent e7ac747e2d
commit 89fbe54cb1
2 changed files with 45 additions and 25 deletions
@@ -1895,7 +1895,25 @@ describe('PythonCodeRuntime — programs and bindings', () => {
bindings: [],
})
expect(result.error).toBeUndefined()
expect(result.logs).toEqual([logTruncationMarker(64)])
expect(result.logs).toEqual(['a'.repeat(60), logTruncationMarker(64)])
}, 15_000)
it('commits a flushed open prefix before the truncation marker', async () => {
// A flushed unterminated line is billed and committed; when a later
// over-budget write truncates, the committed prefix must appear BEFORE the
// marker — the ledger charged for it, so it cannot vanish. (The bug: all
// truncation arms pushed only the marker, dropping the held prefix.)
const { runtime } = await setup({ maxLogBytes: 64 })
const result = await runtime.run({
program: [
"print('committed', end='', flush=True)",
"print('x' * 100)",
'return "done"',
].join('\n'),
bindings: [],
})
expect(result.error).toBeUndefined()
expect(result.logs).toEqual(['committed', logTruncationMarker(64)])
}, 15_000)
it('no-ops a closing frame once an open flood already truncated the ledger', async () => {
@@ -1914,7 +1932,7 @@ describe('PythonCodeRuntime — programs and bindings', () => {
bindings: [],
})
expect(result.error).toBeUndefined()
expect(result.logs).toEqual([logTruncationMarker(64)])
expect(result.logs).toEqual(['a'.repeat(60), logTruncationMarker(64)])
}, 15_000)
it('bills a merged open entry once, not per fragment', async () => {
@@ -2055,7 +2073,7 @@ describe('PythonCodeRuntime — programs and bindings', () => {
bindings: [],
})
expect(result.error).toBeUndefined()
expect(result.logs).toEqual([logTruncationMarker(64)])
expect(result.logs).toEqual(['x'.repeat(40), logTruncationMarker(64)])
}, 15_000)
it('keeps a float completion exact when the program mutates the decimal context', async () => {