fix(code-runtime-python): merge a flushed unterminated line into the next log entry

The review's remaining warning: an explicit flush of an unterminated line
(print(..., end='', flush=True)) pushed a full log frame, so the following
print() landed in a second entry and logs.join('\n') rendered 'a\nb' for what
the program printed as one line — a model-visible output defect. The flush
frame now carries an  flag (LogMessage gains the optional field on both
sides and in the mirror test), the host holds it and appends the next log frame
to the same entry, and finish() admits the residual if the run ends with it
still open. The settlement note registers the decimal-context fix from the
previous commit.
This commit is contained in:
Chinesezjc
2026-08-31 15:03:20 +08:00
committed by Tianyi Cui
parent 35de0682c7
commit 72691455e9
8 changed files with 92 additions and 16 deletions
@@ -1843,6 +1843,41 @@ describe('PythonCodeRuntime — programs and bindings', () => {
expect(result.error?.kind).not.toBe('worker-exit')
}, 90_000)
it('appends a flushed unterminated line to the next entry without a fake newline', async () => {
// An explicit flush of an unterminated line (print(..., end='', flush=True))
// used to push a full log frame, so the following print() landed in a
// SECOND entry and logs.join('\n') rendered 'a\nb' for what the program
// printed as one line. The flush frame now carries `open: true` and the
// host appends the next frame to the same entry.
const { runtime } = await setup()
const result = await runtime.run({
program: [
"print('a', end='', flush=True)",
"print('b')",
'return "done"',
].join('\n'),
bindings: [],
})
expect(result.error).toBeUndefined()
expect(result.logs).toEqual(['ab'])
}, 15_000)
it('keeps a flushed unterminated line when the run ends with it still open', async () => {
// The settlement flush pushes the residual with `open: true`; finish()
// admits it so a program that commits a partial line and returns does not
// lose it from logs.
const { runtime } = await setup()
const result = await runtime.run({
program: [
"print('committed', end='', flush=True)",
'return "done"',
].join('\n'),
bindings: [],
})
expect(result.error).toBeUndefined()
expect(result.logs).toEqual(['committed'])
}, 15_000)
it('keeps a float completion exact when the program mutates the decimal context', async () => {
// The float encoder's Decimal(repr(value)).normalize() used the process
// GLOBAL decimal context: a legitimate program setting