From 5c7eeba436341bbbac653c249d5ff43251c2adce Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 10:34:44 +0800 Subject: [PATCH] fix(transcripts): complete short reads within the requested tail --- scripts/lib/file-tail.js | 7 ++- tests/lib/file-tail.test.js | 90 +++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/scripts/lib/file-tail.js b/scripts/lib/file-tail.js index 78ad9b650..20f22fcb6 100644 --- a/scripts/lib/file-tail.js +++ b/scripts/lib/file-tail.js @@ -22,7 +22,12 @@ function readFileTail(filePath, tailBytes, options = {}) { } const buffer = Buffer.alloc(length); - const bytesRead = fs.readSync(fd, buffer, 0, length, start); + let bytesRead = 0; + while (bytesRead < length) { + const count = fs.readSync(fd, buffer, bytesRead, length - bytesRead, start + bytesRead); + if (count === 0) break; + bytesRead += count; + } return { text: buffer.toString('utf8', 0, bytesRead), truncated: start > 0, diff --git a/tests/lib/file-tail.test.js b/tests/lib/file-tail.test.js index bd28ed72a..b4a244b5f 100644 --- a/tests/lib/file-tail.test.js +++ b/tests/lib/file-tail.test.js @@ -75,6 +75,96 @@ test('transcript-context still exports the same reader', () => { assert.strictEqual(context.DEFAULT_TRANSCRIPT_TAIL_BYTES, DEFAULT_TRANSCRIPT_TAIL_BYTES); }); +function withReadSync(read, fn) { + const original = fs.readSync; + fs.readSync = (...args) => read(original, ...args); + try { + fn(); + } finally { + fs.readSync = original; + } +} + +test('fills the requested tail after short reads', () => { + withReadSync((read, fd, buffer, offset, length, position) => + read(fd, buffer, offset, Math.min(length, 1), position), () => { + assert.deepStrictEqual(readFileTail(file, 4, { includeByteLength: true }), { + text: 'cdef', truncated: true, byteLength: 4 + }); + }); +}); + +test('decodes UTF-8 after joining short reads across multibyte characters', () => { + const unicode = path.join(dir, 'unicode.txt'); + const text = '你好🌍abc'; + fs.writeFileSync(unicode, text); + withReadSync((read, fd, buffer, offset, length, position) => + read(fd, buffer, offset, Math.min(length, 2), position), () => { + assert.deepStrictEqual(readFileTail(unicode, 1024, { includeByteLength: true }), { + text, truncated: false, byteLength: Buffer.byteLength(text) + }); + }); +}); + +test('stops at EOF if the file shrinks while reading', () => { + const shrinking = path.join(dir, 'shrinking.txt'); + fs.writeFileSync(shrinking, 'abcdef'); + let calls = 0; + withReadSync((read, fd, buffer, offset, length, position) => { + calls++; + if (calls === 1) fs.truncateSync(shrinking, 3); + return read(fd, buffer, offset, Math.min(length, 2), position); + }, () => { + assert.deepStrictEqual(readFileTail(shrinking, 1024, { includeByteLength: true }), { + text: 'abc', truncated: false, byteLength: 3 + }); + assert.strictEqual(calls, 3); + }); +}); + +test('does not read beyond the original tail when the file grows', () => { + const growing = path.join(dir, 'growing.txt'); + fs.writeFileSync(growing, 'abcdef'); + let calls = 0; + withReadSync((read, fd, buffer, offset, length, position) => { + if (calls++ === 0) fs.appendFileSync(growing, 'ghijkl'); + return read(fd, buffer, offset, Math.min(length, 2), position); + }, () => { + assert.deepStrictEqual(readFileTail(growing, 4, { includeByteLength: true }), { + text: 'cdef', truncated: true, byteLength: 4 + }); + assert.strictEqual(calls, 2); + }); +}); + +test('returns null and closes the descriptor if a later read fails', () => { + let calls = 0; + let descriptor; + withReadSync((read, fd, buffer, offset, length, position) => { + descriptor = fd; + if (calls++ > 0) throw Object.assign(new Error('read failed'), { code: 'EIO' }); + return read(fd, buffer, offset, 1, position); + }, () => { + assert.strictEqual(readFileTail(file, 1024), null); + assert.throws(() => fs.fstatSync(descriptor), { code: 'EBADF' }); + }); +}); + +test('context detection retains the latest usage record after short reads', () => { + const transcript = path.join(dir, 'transcript.jsonl'); + fs.writeFileSync(transcript, [1000, 170000].map(tokens => JSON.stringify({ + type: 'assistant', + message: { model: 'claude-sonnet-4-6', usage: { input_tokens: tokens } } + })).join('\n')); + const { readLatestContextTokens } = require('../../scripts/lib/transcript-context'); + withReadSync((read, fd, buffer, offset, length, position) => + read(fd, buffer, offset, Math.min(length, 32), position), () => { + assert.deepStrictEqual(readLatestContextTokens(transcript), { + tokens: 170000, model: 'claude-sonnet-4-6' + }); + }); +}); + fs.rmSync(dir, { recursive: true, force: true }); console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`); process.exit(failed > 0 ? 1 : 0);