@@ -333,6 +333,7 @@ import {
333333} from "../tool-result-context-guard.js" ;
334334import {
335335 resolveLiveToolResultMaxChars ,
336+ resolveLiveToolResultAggregateMaxChars ,
336337 createToolResultPromptProjectionState ,
337338 truncateOversizedToolResultsInMessages ,
338339 type ToolResultPromptProjectionState ,
@@ -532,7 +533,7 @@ export {
532533} ;
533534
534535const MAX_BTW_SNAPSHOT_MESSAGES = 100 ;
535- const PROMPT_TOOL_RESULT_AGGREGATE_CAP_MULTIPLIER = 4 ;
536+ const aggregateToolResultPressureWarnings = new Set < string > ( ) ;
536537
537538function pluginMetadataSnapshotCoversProvider (
538539 snapshot : PluginMetadataSnapshot | undefined ,
@@ -4212,25 +4213,49 @@ export async function runEmbeddedAttempt(
42124213 cfg : params . config ,
42134214 agentId : sessionAgentId ,
42144215 } ) ;
4216+ const promptToolResultAggregateMaxChars = resolveLiveToolResultAggregateMaxChars ( {
4217+ contextWindowTokens : contextTokenBudget ,
4218+ perResultMaxChars : promptToolResultMaxChars ,
4219+ } ) ;
42154220 let promptHistoryMessages = activeSession . messages ;
42164221 const promptToolResultTruncation = truncateOversizedToolResultsInMessages (
42174222 activeSession . messages ,
42184223 contextTokenBudget ,
42194224 promptToolResultMaxChars ,
4220- promptToolResultMaxChars * PROMPT_TOOL_RESULT_AGGREGATE_CAP_MULTIPLIER ,
4225+ promptToolResultAggregateMaxChars ,
42214226 toolResultPromptProjectionState ,
42224227 ) ;
4223- if ( promptToolResultTruncation . messages !== activeSession . messages ) {
4228+ const promptHistoryChanged =
4229+ promptToolResultTruncation . messages !== activeSession . messages ;
4230+ const { aggregatePressureEngaged } = promptToolResultTruncation ;
4231+ if ( promptHistoryChanged ) {
42244232 promptHistoryMessages = promptToolResultTruncation . messages ;
4225- log . info (
4233+ }
4234+ if ( promptHistoryChanged || aggregatePressureEngaged ) {
4235+ const sessionLogKey = params . sessionKey ?? params . sessionId ?? "unknown" ;
4236+ const truncationLog =
42264237 `[tool-result-truncation] Truncated ${ promptToolResultTruncation . truncatedCount } ` +
4227- `tool result(s) for prompt history ` +
4228- `(maxChars=${ promptToolResultMaxChars } ` +
4229- `aggregateBudgetChars=${
4230- promptToolResultMaxChars * PROMPT_TOOL_RESULT_AGGREGATE_CAP_MULTIPLIER
4231- } ) ` +
4232- `sessionKey=${ params . sessionKey ?? params . sessionId ?? "unknown" } ` ,
4233- ) ;
4238+ `tool result(s) for prompt history ` +
4239+ `(maxChars=${ promptToolResultMaxChars } ` +
4240+ `aggregateBudgetChars=${ promptToolResultAggregateMaxChars } ` +
4241+ `aggregate=${ promptToolResultTruncation . aggregateTruncatedCount } ) ` +
4242+ `sessionKey=${ sessionLogKey } ` ;
4243+ if ( aggregatePressureEngaged ) {
4244+ if ( ! aggregateToolResultPressureWarnings . has ( sessionLogKey ) ) {
4245+ aggregateToolResultPressureWarnings . add ( sessionLogKey ) ;
4246+ log . warn (
4247+ `${ truncationLog } ; aggregate tool-result pressure detected, compaction has been requested; consider /compact or /new if pressure persists` ,
4248+ ) ;
4249+ }
4250+ // Compaction and aggregate truncation both target about half the window;
4251+ // compact-then-truncate prevents re-hitting the same cap on the next turn.
4252+ preflightRecovery = { route : "compact_then_truncate" } ;
4253+ promptError = new Error ( PREEMPTIVE_OVERFLOW_ERROR_TEXT ) ;
4254+ promptErrorSource = "precheck" ;
4255+ skipPromptSubmission = true ;
4256+ } else {
4257+ log . info ( truncationLog ) ;
4258+ }
42344259 }
42354260
42364261 const promptSubmission = resolveRuntimeContextPromptParts ( {
@@ -4778,7 +4803,7 @@ export async function runEmbeddedAttempt(
47784803 messages ,
47794804 contextTokenBudget ,
47804805 promptToolResultMaxChars ,
4781- promptToolResultMaxChars * PROMPT_TOOL_RESULT_AGGREGATE_CAP_MULTIPLIER ,
4806+ promptToolResultAggregateMaxChars ,
47824807 toolResultPromptProjectionState ,
47834808 ) ;
47844809 return providerPromptHistoryTruncation . messages !== messages
0 commit comments