-
Notifications
You must be signed in to change notification settings - Fork 17
(MOT-4325) feat(context-manager): named-part counting and an assemble breakdown #683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
488a6f1
(MOT-4325) feat(context-manager): count named parts and the tools sha…
rohitg00 83fd29c
(MOT-4325) feat(context-manager): report a per-category breakdown fro…
rohitg00 27542ba
(MOT-4325) refactor(context-manager): one home for the role partition…
rohitg00 324ceb8
Merge branch 'main' into feat/context-manager-count-parts
rohitg00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ use schemars::JsonSchema; | |
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| use crate::core::budget::{default_reserved, preserve_recent_budget, usable}; | ||
| use crate::core::estimate::{estimator_for_model, Estimator}; | ||
| use crate::core::estimate::{by_role_from_sizes, estimator_for_model, Estimator}; | ||
| use crate::core::lease; | ||
| use crate::core::prune::{emergency_reduce_with_sizes, prune_with_sizes, PruneParams}; | ||
| use crate::core::selection::select; | ||
|
|
@@ -24,7 +24,9 @@ use crate::core::summary::{ | |
| use crate::error::ContextError; | ||
| use crate::functions::resolve_model; | ||
| use crate::ports::{Deps, SummarizeRequest}; | ||
| use crate::types::{AgentFunction, AgentMessage, ModelInput, Role, ThinkingLevel}; | ||
| use crate::types::{ | ||
| AgentFunction, AgentMessage, ByRoleTokens, EstimatorName, ModelInput, Role, ThinkingLevel, | ||
| }; | ||
|
|
||
| #[derive(Debug, Default, Deserialize, JsonSchema)] | ||
| pub struct AssembleOptions { | ||
|
|
@@ -112,6 +114,22 @@ pub struct Applied { | |
| pub summarized_head_tokens: Option<u64>, | ||
| } | ||
|
|
||
| /// Where the returned `token_count` sits, by category — the same sums the | ||
| /// pipeline already maintains, exposed so callers can render a context | ||
| /// breakdown without re-counting: `token_count` equals the by_role sum plus | ||
| /// `system_prompt_tokens`, `tools_tokens`, and the request overhead. | ||
| #[derive(Debug, Serialize, JsonSchema)] | ||
| pub struct AssembleBreakdown { | ||
| /// Estimated tokens of the returned system prompt (any compaction | ||
| /// summary section included). | ||
| pub system_prompt_tokens: u64, | ||
| /// Estimated tokens of the invocation schemas. | ||
| pub tools_tokens: u64, | ||
| /// The returned messages' tokens by role. | ||
| pub by_role: ByRoleTokens, | ||
| pub estimator: EstimatorName, | ||
|
Comment on lines
+117
to
+130
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win Expose request overhead in
Proposed fix pub struct AssembleBreakdown {
pub system_prompt_tokens: u64,
pub tools_tokens: u64,
+ pub request_overhead_tokens: u64,
pub by_role: ByRoleTokens,
pub estimator: EstimatorName,
}
breakdown: AssembleBreakdown {
system_prompt_tokens: prompt_tokens,
tools_tokens: tool_tokens,
+ request_overhead_tokens,
by_role,
estimator: match estimator.kind() {📍 Affects 3 files
🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| #[derive(Debug, Serialize, JsonSchema)] | ||
| pub struct AssembleResponse { | ||
| pub system_prompt: String, | ||
|
|
@@ -127,6 +145,7 @@ pub struct AssembleResponse { | |
| pub effective_max_output_tokens: u64, | ||
| pub model_resolved: ModelResolvedWire, | ||
| pub applied: Applied, | ||
| pub breakdown: AssembleBreakdown, | ||
| } | ||
|
|
||
| /// Test-only re-export of [`count_context`] so sibling function tests | ||
|
|
@@ -312,6 +331,8 @@ pub async fn handle(deps: &Deps, req: AssembleRequest) -> Result<AssembleRespons | |
| }); | ||
| } | ||
|
|
||
| let by_role = by_role_from_sizes(&working, &sizes); | ||
|
|
||
| Ok(AssembleResponse { | ||
| system_prompt, | ||
| messages: working, | ||
|
|
@@ -324,6 +345,12 @@ pub async fn handle(deps: &Deps, req: AssembleRequest) -> Result<AssembleRespons | |
| crate::core::budget::ModelResolved::Fallback => ModelResolvedWire::Fallback, | ||
| }, | ||
| applied, | ||
| breakdown: AssembleBreakdown { | ||
| system_prompt_tokens: prompt_tokens, | ||
| tools_tokens: tool_tokens, | ||
| by_role: by_role.into(), | ||
| estimator: estimator.kind().into(), | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Reject mismatched message and size slices before zipping.
Line 112 truncates to the shorter slice. If
sizesdrifts frommessages, the breakdown silently omits entries and can no longer reconcile withtoken_count. Enforce equal lengths, or return an error when callers control these inputs.Proposed invariant check
pub fn by_role_from_sizes(messages: &[AgentMessage], sizes: &[u64]) -> ByRole { + assert_eq!( + messages.len(), + sizes.len(), + "messages and sizes must have equal lengths" + ); let mut by_role = ByRole::default(); - for (message, size) in messages.iter().zip(sizes) { + for (message, size) in messages.iter().zip(sizes.iter()) {📝 Committable suggestion
🤖 Prompt for AI Agents