



















@@ -396,7 +396,7 @@ function resolveSessionLogPath(
396396function deriveTranscriptUsageSnapshot(
397397snapshot:
398398| {
399-usage: ReturnType<typeof normalizeUsage> | undefined;
399+usage?: ReturnType<typeof normalizeUsage>;
400400trailingBytes?: number;
401401}
402402| undefined,
@@ -472,30 +472,42 @@ async function readSessionLogSnapshot(params: {
472472}
473473474474const snapshot: SessionLogSnapshot = {};
475+let usageScan: SessionLogUsageScan | undefined;
475476476-if (params.includeByteSize) {
477+if (params.includeUsage) {
477478try {
478-const stat = await fs.promises.stat(logPath);
479-const size = Math.floor(stat.size);
480-snapshot.byteSize = Number.isFinite(size) && size >= 0 ? size : undefined;
479+usageScan = await readLastNonzeroUsageFromSessionLog(logPath);
480+snapshot.usage = deriveTranscriptUsageSnapshot(usageScan);
481481} catch {
482-snapshot.byteSize = undefined;
482+snapshot.usage = undefined;
483483}
484484}
485485486-if (params.includeUsage) {
486+if (params.includeByteSize) {
487+const scannedSize = usageScan?.byteSize;
488+if (typeof scannedSize === "number" && Number.isFinite(scannedSize) && scannedSize >= 0) {
489+snapshot.byteSize = Math.floor(scannedSize);
490+return snapshot;
491+}
487492try {
488-const lastUsage = await readLastNonzeroUsageFromSessionLog(logPath);
489-snapshot.usage = deriveTranscriptUsageSnapshot(lastUsage);
493+const stat = await fs.promises.stat(logPath);
494+const size = Math.floor(stat.size);
495+snapshot.byteSize = Number.isFinite(size) && size >= 0 ? size : undefined;
490496} catch {
491-snapshot.usage = undefined;
497+snapshot.byteSize = undefined;
492498}
493499}
494500495501return snapshot;
496502}
497503498-async function readLastNonzeroUsageFromSessionLog(logPath: string) {
504+type SessionLogUsageScan = {
505+usage?: ReturnType<typeof normalizeUsage>;
506+trailingBytes?: number;
507+byteSize: number;
508+};
509+510+async function readLastNonzeroUsageFromSessionLog(logPath: string): Promise<SessionLogUsageScan> {
499511const handle = await fs.promises.open(logPath, "r");
500512try {
501513const stat = await handle.stat();
@@ -525,6 +537,7 @@ async function readLastNonzeroUsageFromSessionLog(logPath: string) {
525537return {
526538 usage,
527539trailingBytes: suffixBytesOutsideCombined + trailingBytesInChunk,
540+byteSize: stat.size,
528541};
529542}
530543}
@@ -535,8 +548,9 @@ async function readLastNonzeroUsageFromSessionLog(logPath: string) {
535548 ? {
536549 usage,
537550trailingBytes: Math.max(0, stat.size - Buffer.byteLength(leadingPartial, "utf8")),
551+byteSize: stat.size,
538552}
539- : undefined;
553+ : { byteSize: stat.size };
540554} finally {
541555await handle.close();
542556}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。