feat(agent): gate the final model request on a context window budget (issue #137) #352
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 614 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.384
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #352"
general
weight: 0.35
yuluo-yx
Describe what this PR does / why we need it
Fixes #137: the neutral agent API had no notion of a context window budget.
TokenCounter.countTokens(List<ChatMessage>)only measures the conversation, while the request which is finally dispatched carries more than the conversation -ReActAgent.invokeModelbuildsnew ChatRequest(composeMessages(request), effective.definitions(), request.getOptions(), request.getAttributes()), so the system message, the tool schemas and the reserved output belong to the request but stay outside the measured scope.SummarizationHookandContextEditingInterceptoreach trigger on their own message threshold and both continue when they cannot compress anything, so nothing keeps an oversized request from reaching the provider, andLargeResultEvictionInterceptorclaimed that a result was written to the filesystem even when the write failed.Does this pull request fix one issue?
Fixes #137
Describe how you did it
io.github.agentic.ai.agent.budgetinargi-agent-api:ContextWindowBudgetfor the context window and the reserved output,RequestTokenEstimatefor the decomposition into system, conversation, tool schema and reserved output with anEstimationAccuracythat tells exact and conservative counts apart,RequestTokenEstimatorwith the defaultApproximateRequestTokenEstimator,BudgetComponentandBudgetViolationfor a stable classification, andContextWindowBudgetExceededExceptionfor an explicit failure;ContextBudgetInterceptor, a neutralModelInterceptorwhich is opt-in through the existing interceptor chain: it estimates the request, asks the configuredRequestCompactorto shrink an oversized request, measures the compacted request again, validates the tool call pairing and only then dispatches; a request which still does not fit fails withContextWindowBudgetExceededExceptionand never reaches the provider;SummarizationRequestCompactordelegates to the existingSummarizationHookand rebuilds the request from the messages the hook returned; the hook keeps its own behaviour, so a failed summary returns the original history and the gate then fails explicitly instead of continuing with a broken context;argi-apiandargi-runtime-apionly.How was it tested / verified?
New
ContextBudgetInterceptorTestinargi-agent-api(offline, no model and no provider):1. the estimate covers the system message, the tool schemas and the reserved output;
2. an oversized request fails and the dispatch counter stays at zero;
3. a compacted request is measured again and then dispatched;
4. a compaction which does not help still fails without a dispatch;
5. a compaction which breaks the tool call pairing is rejected without a dispatch;
6. the estimator is replaceable, an exact estimator changes the decision;
7. the boundary is exact, a request which exactly fills the budget is dispatched and one token more is rejected.
New
SummarizationRequestCompactorTestand two additional cases inLargeResultEvictionInterceptorTestinargi-agent-framework: the compactor summarizes with the existing hook and returns the original request when the summary fails or is not needed, and a failed filesystem write produces a failure which contains no file pointer and no "saved in the filesystem" wording while keeping the metadata of the original response.Green after the change:
argi-agent-api41/41,argi-agent-framework1341/1341,argi-agent-core138/138,argi-workflow-graph-core551/551;spring-javaformat:validateandcheckstyle:checkreport no violation for the two touched modules,codespelland the newline check pass.Additional notes
argi-agent-api, one new adapter and one behavioural fix inargi-agent-framework, and no change to an existing public signature, event, configuration or persistence format;LargeResultEvictionInterceptoralways supplies a default filesystem backend when eviction is enabled, so the branch for a missing backend is defensive only and the test covers the failed write path.