mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 20:28:29 +00:00
feat: add pre-prompt context size diagnostic logging (openclaw#8930) thanks @Glucksberg
Verified: - pnpm build - pnpm check - pnpm test Co-authored-by: Glucksberg <80581902+Glucksberg@users.noreply.github.com> Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
This commit is contained in:
@@ -97,6 +97,10 @@ const createUsageAccumulator = (): UsageAccumulator => ({
|
||||
lastInput: 0,
|
||||
});
|
||||
|
||||
function createCompactionDiagId(): string {
|
||||
return `ovf-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
||||
}
|
||||
|
||||
const hasUsageValues = (
|
||||
usage: ReturnType<typeof normalizeUsage>,
|
||||
): usage is NonNullable<ReturnType<typeof normalizeUsage>> =>
|
||||
@@ -515,13 +519,15 @@ export async function runEmbeddedPiAgent(
|
||||
: null;
|
||||
|
||||
if (contextOverflowError) {
|
||||
const overflowDiagId = createCompactionDiagId();
|
||||
const errorText = contextOverflowError.text;
|
||||
const msgCount = attempt.messagesSnapshot?.length ?? 0;
|
||||
log.warn(
|
||||
`[context-overflow-diag] sessionKey=${params.sessionKey ?? params.sessionId} ` +
|
||||
`provider=${provider}/${modelId} source=${contextOverflowError.source} ` +
|
||||
`messages=${msgCount} sessionFile=${params.sessionFile} ` +
|
||||
`compactionAttempts=${overflowCompactionAttempts} error=${errorText.slice(0, 200)}`,
|
||||
`diagId=${overflowDiagId} compactionAttempts=${overflowCompactionAttempts} ` +
|
||||
`error=${errorText.slice(0, 200)}`,
|
||||
);
|
||||
const isCompactionFailure = isCompactionFailureError(errorText);
|
||||
// Attempt auto-compaction on context overflow (not compaction_failure)
|
||||
@@ -529,6 +535,13 @@ export async function runEmbeddedPiAgent(
|
||||
!isCompactionFailure &&
|
||||
overflowCompactionAttempts < MAX_OVERFLOW_COMPACTION_ATTEMPTS
|
||||
) {
|
||||
if (log.isEnabled("debug")) {
|
||||
log.debug(
|
||||
`[compaction-diag] decision diagId=${overflowDiagId} branch=compact ` +
|
||||
`isCompactionFailure=${isCompactionFailure} hasOversizedToolResults=unknown ` +
|
||||
`attempt=${overflowCompactionAttempts + 1} maxAttempts=${MAX_OVERFLOW_COMPACTION_ATTEMPTS}`,
|
||||
);
|
||||
}
|
||||
overflowCompactionAttempts++;
|
||||
log.warn(
|
||||
`context overflow detected (attempt ${overflowCompactionAttempts}/${MAX_OVERFLOW_COMPACTION_ATTEMPTS}); attempting auto-compaction for ${provider}/${modelId}`,
|
||||
@@ -548,11 +561,16 @@ export async function runEmbeddedPiAgent(
|
||||
senderIsOwner: params.senderIsOwner,
|
||||
provider,
|
||||
model: modelId,
|
||||
runId: params.runId,
|
||||
thinkLevel,
|
||||
reasoningLevel: params.reasoningLevel,
|
||||
bashElevated: params.bashElevated,
|
||||
extraSystemPrompt: params.extraSystemPrompt,
|
||||
ownerNumbers: params.ownerNumbers,
|
||||
trigger: "overflow",
|
||||
diagId: overflowDiagId,
|
||||
attempt: overflowCompactionAttempts,
|
||||
maxAttempts: MAX_OVERFLOW_COMPACTION_ATTEMPTS,
|
||||
});
|
||||
if (compactResult.compacted) {
|
||||
autoCompactionCount += 1;
|
||||
@@ -576,6 +594,13 @@ export async function runEmbeddedPiAgent(
|
||||
: false;
|
||||
|
||||
if (hasOversized) {
|
||||
if (log.isEnabled("debug")) {
|
||||
log.debug(
|
||||
`[compaction-diag] decision diagId=${overflowDiagId} branch=truncate_tool_results ` +
|
||||
`isCompactionFailure=${isCompactionFailure} hasOversizedToolResults=${hasOversized} ` +
|
||||
`attempt=${overflowCompactionAttempts} maxAttempts=${MAX_OVERFLOW_COMPACTION_ATTEMPTS}`,
|
||||
);
|
||||
}
|
||||
toolResultTruncationAttempted = true;
|
||||
log.warn(
|
||||
`[context-overflow-recovery] Attempting tool result truncation for ${provider}/${modelId} ` +
|
||||
@@ -598,8 +623,26 @@ export async function runEmbeddedPiAgent(
|
||||
log.warn(
|
||||
`[context-overflow-recovery] Tool result truncation did not help: ${truncResult.reason ?? "unknown"}`,
|
||||
);
|
||||
} else if (log.isEnabled("debug")) {
|
||||
log.debug(
|
||||
`[compaction-diag] decision diagId=${overflowDiagId} branch=give_up ` +
|
||||
`isCompactionFailure=${isCompactionFailure} hasOversizedToolResults=${hasOversized} ` +
|
||||
`attempt=${overflowCompactionAttempts} maxAttempts=${MAX_OVERFLOW_COMPACTION_ATTEMPTS}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (
|
||||
(isCompactionFailure ||
|
||||
overflowCompactionAttempts >= MAX_OVERFLOW_COMPACTION_ATTEMPTS ||
|
||||
toolResultTruncationAttempted) &&
|
||||
log.isEnabled("debug")
|
||||
) {
|
||||
log.debug(
|
||||
`[compaction-diag] decision diagId=${overflowDiagId} branch=give_up ` +
|
||||
`isCompactionFailure=${isCompactionFailure} hasOversizedToolResults=unknown ` +
|
||||
`attempt=${overflowCompactionAttempts} maxAttempts=${MAX_OVERFLOW_COMPACTION_ATTEMPTS}`,
|
||||
);
|
||||
}
|
||||
const kind = isCompactionFailure ? "compaction_failure" : "context_overflow";
|
||||
return {
|
||||
payloads: [
|
||||
|
||||
Reference in New Issue
Block a user