fix(tools): improve session_status cache-aware usage reporting

Co-authored-by: Lucian Feraru <1ucian@users.noreply.github.com>
This commit is contained in:
Peter Steinberger
2026-02-23 19:05:27 +00:00
parent 40db3fef49
commit cf38339f25
3 changed files with 17 additions and 3 deletions

View File

@@ -243,7 +243,20 @@ const readUsageFromSessionLog = (
}
try {
const lines = fs.readFileSync(logPath, "utf-8").split(/\n+/);
// Read the tail only; we only need the most recent usage entries.
const TAIL_BYTES = 8192;
const stat = fs.statSync(logPath);
const offset = Math.max(0, stat.size - TAIL_BYTES);
const buf = Buffer.alloc(Math.min(TAIL_BYTES, stat.size));
const fd = fs.openSync(logPath, "r");
try {
fs.readSync(fd, buf, 0, buf.length, offset);
} finally {
fs.closeSync(fd);
}
const tail = buf.toString("utf-8");
const lines = (offset > 0 ? tail.slice(tail.indexOf("\n") + 1) : tail).split(/\n+/);
let input = 0;
let output = 0;
let promptTokens = 0;
@@ -270,7 +283,7 @@ const readUsageFromSessionLog = (
}
model = parsed.message?.model ?? parsed.model ?? model;
} catch {
// ignore bad lines
// ignore bad lines (including a truncated first tail line)
}
}