fix(transcripts): complete short reads within the requested tail

This commit is contained in:
Afloat committed 2026-11-17 10:34:44 +08:00
1 parent 8abdf329ec
commit 5c7eeba436
2 files changed
+96 -1

No files matched your search

+6 -1
View File
@@ -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,
+90
View File
@@ -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);