

























@@ -35,6 +35,7 @@ import {
3535} from "./agent-runner-utils.js";
3636import {
3737hasAlreadyFlushedForCurrentCompaction,
38+resolveMaxActiveTranscriptBytes,
3839resolveMemoryFlushContextWindowTokens,
3940shouldRunMemoryFlush,
4041shouldRunPreflightCompaction,
@@ -400,8 +401,25 @@ export async function runPreflightCompactionIfNeeded(params: {
400401typeof persistedTotalTokens === "number" &&
401402Number.isFinite(persistedTotalTokens) &&
402403persistedTotalTokens > 0;
404+const maxActiveTranscriptBytes = resolveMaxActiveTranscriptBytes(params.cfg);
405+const shouldCheckActiveTranscriptBytes = typeof maxActiveTranscriptBytes === "number";
406+const transcriptSizeSnapshot = shouldCheckActiveTranscriptBytes
407+ ? await readSessionLogSnapshot({
408+sessionId: entry.sessionId,
409+sessionEntry: entry,
410+sessionKey: params.sessionKey ?? params.followupRun.run.sessionKey,
411+opts: { storePath: params.storePath },
412+includeByteSize: true,
413+includeUsage: false,
414+})
415+ : undefined;
416+const activeTranscriptBytes = transcriptSizeSnapshot?.byteSize;
417+const shouldCompactByTranscriptBytes =
418+typeof activeTranscriptBytes === "number" &&
419+typeof maxActiveTranscriptBytes === "number" &&
420+activeTranscriptBytes >= maxActiveTranscriptBytes;
403421const shouldUseTranscriptFallback = entry.totalTokensFresh === false || !hasPersistedTotalTokens;
404-if (!shouldUseTranscriptFallback) {
422+if (!shouldUseTranscriptFallback && !shouldCompactByTranscriptBytes) {
405423return entry ?? params.sessionEntry;
406424}
407425const promptTokenEstimate = estimatePromptTokensForMemoryFlush(
@@ -434,24 +452,31 @@ export async function runPreflightCompactionIfNeeded(params: {
434452`isHeartbeat=${params.isHeartbeat} isCli=${isCli} ` +
435453`persistedFresh=${entry?.totalTokensFresh === true} ` +
436454`transcriptPromptTokens=${transcriptPromptTokens ?? "undefined"} ` +
437-`promptTokensEst=${promptTokenEstimate ?? "undefined"}`,
455+`promptTokensEst=${promptTokenEstimate ?? "undefined"} ` +
456+`activeTranscriptBytes=${activeTranscriptBytes ?? "undefined"} ` +
457+`maxActiveTranscriptBytes=${maxActiveTranscriptBytes ?? "undefined"} ` +
458+`sizeTrigger=${shouldCompactByTranscriptBytes}`,
438459);
439460440-const shouldCompact = shouldRunPreflightCompaction({
461+const shouldCompactByTokens = shouldRunPreflightCompaction({
441462 entry,
442463tokenCount: tokenCountForCompaction,
443464 contextWindowTokens,
444465 reserveTokensFloor,
445466 softThresholdTokens,
446467});
468+const shouldCompact = shouldCompactByTokens || shouldCompactByTranscriptBytes;
447469if (!shouldCompact) {
448470return entry ?? params.sessionEntry;
449471}
450472473+const compactionTrigger = shouldCompactByTranscriptBytes ? "transcript_bytes" : "tokens";
451474logVerbose(
452475`preflightCompaction triggered: sessionKey=${params.sessionKey} ` +
453476`tokenCount=${tokenCountForCompaction ?? freshPersistedTokens ?? "undefined"} ` +
454-`threshold=${threshold}`,
477+`threshold=${threshold} trigger=${compactionTrigger} ` +
478+`activeTranscriptBytes=${activeTranscriptBytes ?? "undefined"} ` +
479+`maxActiveTranscriptBytes=${maxActiveTranscriptBytes ?? "undefined"}`,
455480);
456481457482params.replyOperation.setPhase("preflight_compacting");
@@ -486,7 +511,7 @@ export async function runPreflightCompactionIfNeeded(params: {
486511thinkLevel: params.followupRun.run.thinkLevel,
487512bashElevated: params.followupRun.run.bashElevated,
488513trigger: "budget",
489-currentTokenCount: tokenCountForCompaction,
514+currentTokenCount: tokenCountForCompaction ?? freshPersistedTokens,
490515senderIsOwner: params.followupRun.run.senderIsOwner,
491516ownerNumbers: params.followupRun.run.ownerNumbers,
492517abortSignal: params.replyOperation.abortSignal,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。