Skip to content

Commit 7e20991

Browse files
committed
fix(memory-flush): fallback to estimatePromptTokensFromSessionTranscript when usage data is unavailable
When a model provider (e.g., MiniMax) does not return usage data in its API response, the session entry's totalTokens stays undefined. The preflight compaction path already handles this via estimatePromptTokensFromSessionTranscript, but the memory-flush path did not have the same fallback. This caused entry.totalTokens to remain undefined across compactions, preventing the system from recognizing context reductions and triggering redundant compactions. This change adds the same fallback from the preflight path into the memory-flush path, ensuring entry.totalTokens is populated even when the model does not provide usage data.
1 parent 800a0d3 commit 7e20991

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

src/auto-reply/reply/agent-runner-memory.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,24 @@ export async function runMemoryFlushIfNeeded(params: {
870870
typeof transcriptByteSize === "number" && transcriptByteSize >= forceFlushTranscriptBytes;
871871

872872
const transcriptUsageSnapshot = sessionLogSnapshot?.usage;
873-
const transcriptPromptTokens = transcriptUsageSnapshot?.promptTokens;
874-
const transcriptOutputTokens = transcriptUsageSnapshot?.outputTokens;
873+
const transcriptUsageTokensFallback =
874+
sessionLogSnapshot &&
875+
transcriptUsageSnapshot?.promptTokens === undefined &&
876+
entry
877+
? await estimatePromptTokensFromSessionTranscript({
878+
sessionId: entry.sessionId,
879+
sessionEntry: entry,
880+
sessionKey: params.sessionKey ?? params.followupRun.run.sessionKey,
881+
sessionFile: entry.sessionFile ?? params.followupRun.run.sessionFile,
882+
storePath: params.storePath,
883+
})
884+
: undefined;
885+
const transcriptPromptTokens =
886+
transcriptUsageSnapshot?.promptTokens ??
887+
transcriptUsageTokensFallback?.promptTokens;
888+
const transcriptOutputTokens =
889+
transcriptUsageSnapshot?.outputTokens ??
890+
transcriptUsageTokensFallback?.outputTokens;
875891
const hasReliableTranscriptPromptTokens =
876892
typeof transcriptPromptTokens === "number" &&
877893
Number.isFinite(transcriptPromptTokens) &&

0 commit comments

Comments
 (0)