mirror of
https://github.com/bdnugget/rs-sdk.git
synced 2026-11-07 09:07:30 +00:00
fix(engine): self-heal bug-report log after a truncated append
A partial write (ENOSPC during the disk-full event) left a record cut mid-line, and the next append spliced its JSON into the same line, permanently breaking JSONL parsing for both. Before appending, check the log's last byte and start on a fresh line if the previous append was cut short. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
60c5445f45
commit
e4cb65ec47
1 file changed
+27
-1
@@ -36,12 +36,38 @@ export interface BugReport {
|
||||
sdkVersion: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* True if the log's last byte is something other than a newline - i.e. a
|
||||
* previous append was cut short (ENOSPC during the 2026-08 disk-full event
|
||||
* left one record truncated mid-line, and the next append spliced into it).
|
||||
*/
|
||||
function lastLineIsTruncated(): boolean {
|
||||
let fd: number | null = null;
|
||||
try {
|
||||
fd = fs.openSync(REPORTS_FILE, 'r');
|
||||
const size = fs.fstatSync(fd).size;
|
||||
if (size === 0) {
|
||||
return false;
|
||||
}
|
||||
const tail = Buffer.alloc(1);
|
||||
fs.readSync(fd, tail, 0, 1, size - 1);
|
||||
return tail[0] !== 0x0a;
|
||||
} catch {
|
||||
return false;
|
||||
} finally {
|
||||
if (fd !== null) {
|
||||
fs.closeSync(fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function appendReport(report: BugReport): void {
|
||||
const dir = path.dirname(REPORTS_FILE);
|
||||
if (dir && dir !== '.' && !fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
fs.appendFileSync(REPORTS_FILE, JSON.stringify(report) + '\n');
|
||||
const prefix = lastLineIsTruncated() ? '\n' : '';
|
||||
fs.appendFileSync(REPORTS_FILE, prefix + JSON.stringify(report) + '\n');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user