Skip to content

Conversation

@katzdave
Copy link
Collaborator

Token counts updated in the stream immediately after compaction.

Remove lots of unused code from compaction; only return the provider usage.

Modify update_session_metrics to support using summarization usage to estimate new context window (with system prompt token estimate).

Remove lots of custom code in CLI around token counting + manual summarization trigger

Rename /summarize -> /compact.

@katzdave katzdave changed the title Compaction counting fix + cli cleanup Manual compaction counting fix + cli cleanup Oct 30, 2025
@katzdave katzdave marked this pull request as ready for review October 30, 2025 16:58
@katzdave katzdave requested a review from jamadeo October 30, 2025 19:03
Copilot AI review requested due to automatic review settings November 3, 2025 17:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the conversation compaction feature by simplifying the return type and centralizing token tracking logic. The changes rename "summarize" to "compact" throughout the codebase for consistency, remove redundant token count tracking in the compaction flow, and integrate metrics updates directly into the agent response handling.

Key changes:

  • Simplified compact_messages return type from (Conversation, Vec<usize>, Option<ProviderUsage>) to (Conversation, ProviderUsage)
  • Centralized token tracking logic in update_session_metrics with special handling for compaction usage
  • Renamed "/summarize" command to "/compact" with backward compatibility for the deprecated command

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
crates/goose/src/context_mgmt/mod.rs Simplified compact_messages return type and removed token count tracking logic
crates/goose/src/agents/reply_parts.rs Added compaction-aware token tracking with SYSTEM_PROMPT_TOKEN_ESTIMATE and is_compaction_usage parameter
crates/goose/src/agents/agent.rs Updated compact_messages call sites to use new signature and added session metrics updates
crates/goose/src/agents/mod.rs Exported MANUAL_COMPACT_TRIGGER constant for CLI usage
crates/goose-cli/src/session/mod.rs Refactored manual compaction to use agent response processing instead of direct API calls
crates/goose-cli/src/session/input.rs Renamed InputResult::Summarize to InputResult::Compact with deprecation warning for /summarize

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

use crate::session::SessionManager;
use rmcp::model::Tool;

const SYSTEM_PROMPT_TOKEN_ESTIMATE: i32 = 5000;
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This magic number lacks documentation explaining why 5000 tokens is used as an estimate for the system prompt. Add a comment explaining the rationale behind this value and consider making it configurable if system prompt size varies significantly across different use cases.

Suggested change
const SYSTEM_PROMPT_TOKEN_ESTIMATE: i32 = 5000;
/// Estimated token count for the system prompt.
/// The default value of 5000 tokens is chosen based on typical prompt sizes and model limitations.
/// If your use case requires a different estimate, you can override this value by setting the
/// `SYSTEM_PROMPT_TOKEN_ESTIMATE` environment variable.
fn system_prompt_token_estimate() -> i32 {
std::env::var("SYSTEM_PROMPT_TOKEN_ESTIMATE")
.ok()
.and_then(|v| v.parse::<i32>().ok())
.unwrap_or(5000)
}

Copilot uses AI. Check for mistakes.
accumulate(session.accumulated_output_tokens, usage.usage.output_tokens);

let (current_total, current_input, current_output) = if is_compaction_usage {
// After compaction: summary output becomes new input context
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after manual compaction, should this not really just go to zero? have we sent the manually compacted conversation to the model yet or only to the client? although I could see how that would be confusing to a user

it would be good to avoid a hard coded system prompt estimate, just because that could really be all over the place

I suppose we could do something like use the token counts from the first message you send, but that would be complicated to do

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think this one is more of a question of what we want to show the user. Let's try dropping it to the summary size, there will be a bit of whiplash for the user when the system prompt re-enters on the next message, but I don't love that constant either.

/// * A tuple containing:
/// - `Conversation`: The compacted messages
/// - `Vec<usize>`: Token counts for each message
/// - `Option<ProviderUsage>`: Provider usage from summarization
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, I saw we didn't use this. There's some code on the frontend you could clean up with this too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack will take another pass on the frontend too.

…se into dkatz/manual-compact-fix-usage

* 'dkatz/manual-compact-fix-usage' of github.com:block/goose: (35 commits)
  Fix image processing (#5544)
  docs: AI attribution for PRs (#5547)
  chore(tests/mcp): testing for MCP sampling (#5456)
  docs: adding HOWTOAI.md (#5533)
  added configuration content, also added signoff, fix merging issue with another commit by creating a clean branch. removed and closed commits that caused signoff issues. (#5519)
  Fixes Gemini API parse issue by converting nullable type arrays to single types in tool schemas (#5530)
  Troubleshooting diagnostics doc (#5526)
  fix link to Ollama FAQ (#5531)
  docs: remove speech-mcp (#5514)
  fix: adds ProviderRetry to openai provider (#5518)
  docs: extensions directory minor updates (#5466)
  Docs/json recipe support (#5492)
  docs: recipe buttons (#5507)
  Improve system theme detection and fallback (#5427)
  [Autovisualiser] remove unnecessary content from mermaid HTML template (#5505)
  Improve subagents docs (#5484)
  FIX: prefer linux in WSL and add INSTALL_OS override for CLI (#5215)
  Propagate session ID in LLM and MCP requests (#5165)
  feat: YT Short for Canva MCP + goose (#5495)
  Change Recipes Test Script (#5457)
  ...
Copilot AI review requested due to automatic review settings November 3, 2025 20:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


let (current_total, current_input, current_output) = if is_compaction_usage {
// After compaction: summary output becomes new input context
let new_input = usage.usage.output_tokens.map(|out| out);
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The identity closure map(|out| out) is redundant. Consider simplifying to just usage.usage.output_tokens directly.

Suggested change
let new_input = usage.usage.output_tokens.map(|out| out);
let new_input = usage.usage.output_tokens;

Copilot uses AI. Check for mistakes.
s if s == CMD_SUMMARIZE => Some(InputResult::Summarize),
s if s == CMD_COMPACT => Some(InputResult::Compact),
s if s == CMD_SUMMARIZE_DEPRECATED => {
println!("{}", console::style("⚠️ Note: /summarize has been renamed to /compact and will be removed in a future release.").yellow());
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The deprecation message string is very long (over 100 characters). Consider breaking it into multiple lines or storing it as a constant for better readability and maintainability.

Copilot uses AI. Check for mistakes.
@katzdave katzdave merged commit 9466d92 into main Nov 3, 2025
14 checks passed
katzdave added a commit that referenced this pull request Nov 4, 2025
* 'main' of github.com:block/goose: (21 commits)
  Manual compaction counting fix + cli cleanup (#5480)
  chore(deps): bump prismjs and react-syntax-highlighter in /ui/desktop (#5549)
  fix: remove qwen3-coder from provider/mcp smoke tests (#5551)
  fix: do not build unsigned desktop app bundles on every PR in ci. add manual option. (#5550)
  fix: update Husky prepare script to v9 format (#5522)
  Fix 404 for responsible coding guide (#5543)
  fix hermit `text file busy` issues on linux (#5372)
  Fix image processing (#5544)
  docs: AI attribution for PRs (#5547)
  chore(tests/mcp): testing for MCP sampling (#5456)
  docs: adding HOWTOAI.md (#5533)
  added configuration content, also added signoff, fix merging issue with another commit by creating a clean branch. removed and closed commits that caused signoff issues. (#5519)
  Fixes Gemini API parse issue by converting nullable type arrays to single types in tool schemas (#5530)
  Troubleshooting diagnostics doc (#5526)
  fix link to Ollama FAQ (#5531)
  docs: remove speech-mcp (#5514)
  fix: adds ProviderRetry to openai provider (#5518)
  docs: extensions directory minor updates (#5466)
  Docs/json recipe support (#5492)
  docs: recipe buttons (#5507)
  ...
wpfleger96 added a commit that referenced this pull request Nov 4, 2025
* main: (85 commits)
  improve linux tray icon support (#5425)
  feat: log rotation (#5561)
  use app.isPackaged instead of checking for node env development (#5465)
  disable RPM build-ID generation to prevent package conflicts (#5563)
  Add Diagnostics Info to Q&A and Bug Report Templates (#5565)
  fix: improve server error messages to include HTTP status code (#5532)
  improvement: add useful error message when attempting to use unauthenticated cursor-agent (#5300)
  fix: unblock acp via databricks (#5562)
  feat: add --output-format json flag to goose run command (#5525)
  Sessions required (#5548)
  feat: add grouped extension loading notification (#5529)
  we should run this on main and also test open models at least via ope… (#5556)
  info: print location of sessions.db via goose info (#5557)
  chore: remove yarn usage from documentation (#5555)
  cli: adjust default theme to address #1905 (#5552)
  Manual compaction counting fix + cli cleanup (#5480)
  chore(deps): bump prismjs and react-syntax-highlighter in /ui/desktop (#5549)
  fix: remove qwen3-coder from provider/mcp smoke tests (#5551)
  fix: do not build unsigned desktop app bundles on every PR in ci. add manual option. (#5550)
  fix: update Husky prepare script to v9 format (#5522)
  ...
wpfleger96 added a commit that referenced this pull request Nov 5, 2025
* main: (54 commits)
  add clippy warning for string_slice (#5422)
  improve linux tray icon support (#5425)
  feat: log rotation (#5561)
  use app.isPackaged instead of checking for node env development (#5465)
  disable RPM build-ID generation to prevent package conflicts (#5563)
  Add Diagnostics Info to Q&A and Bug Report Templates (#5565)
  fix: improve server error messages to include HTTP status code (#5532)
  improvement: add useful error message when attempting to use unauthenticated cursor-agent (#5300)
  fix: unblock acp via databricks (#5562)
  feat: add --output-format json flag to goose run command (#5525)
  Sessions required (#5548)
  feat: add grouped extension loading notification (#5529)
  we should run this on main and also test open models at least via ope… (#5556)
  info: print location of sessions.db via goose info (#5557)
  chore: remove yarn usage from documentation (#5555)
  cli: adjust default theme to address #1905 (#5552)
  Manual compaction counting fix + cli cleanup (#5480)
  chore(deps): bump prismjs and react-syntax-highlighter in /ui/desktop (#5549)
  fix: remove qwen3-coder from provider/mcp smoke tests (#5551)
  fix: do not build unsigned desktop app bundles on every PR in ci. add manual option. (#5550)
  ...
fbalicchia pushed a commit to fbalicchia/goose that referenced this pull request Nov 7, 2025
BlairAllan pushed a commit to BlairAllan/goose that referenced this pull request Nov 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants