Skip to content

fix(tools): filter compaction summaries from session_search bookends and cap content length - #43184

Closed
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/session-search-compaction-bookend
Closed

liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/session-search-compaction-bookend

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

What does this PR do?

Filters context-compaction handoff summaries from session_search discovery bookends and caps message content length to prevent prompt bloat.

Related Issue

Fixes #43175

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • tools/session_search_tool.py: Add _COMPACTION_PREFIXES constant and _is_compaction_summary() helper to detect compaction handoff messages. Filter these from bookend_start and bookend_end in _discover(). Cap bookend content to 1200 chars and window message content to 4000 chars via new max_content_len parameter on _shape_message(). Add content_truncated and original_content_chars metadata when truncation occurs.
  • tests/tools/test_session_search.py: Add 6 regression tests — prefix detection, bookend_start filtering, bookend_end filtering, bookend content capping, window content capping, and legacy [CONTEXT SUMMARY] prefix handling.

How to Test

  1. pytest tests/tools/test_session_search.py -v — all 52 tests should pass (46 existing + 6 new)
  2. Create a test session with a [CONTEXT COMPACTION — REFERENCE ONLY] message as the first user message, followed by normal messages
  3. Run session_search(query=<term in later message>) — the compaction summary should NOT appear in bookend_start
  4. Verify that long bookend/window messages are capped (bookend ≤1200 chars, window ≤4000 chars) with content_truncated: true metadata

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/tools/test_session_search.py -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Code Intelligence

  • Analyzed: tools/session_search_tool.py (_discover, _shape_message, _is_compaction_summary)
  • Analyzed: hermes_state.py (get_anchored_view — bookend SQL queries)
  • Blast radius: LOW — changes are additive (filtering + capping) in a single tool file; no control flow changes to the underlying DB queries
  • Related patterns: Content truncation follows the same pattern as session_search scroll mode's existing read truncation

…and cap content length

Context-compaction handoff summaries (prefixed with [CONTEXT COMPACTION])
were being returned as normal bookend_start/bookend_end messages in
session_search discovery mode. A single compaction handoff could be 57K+
chars, immediately bloating a fresh session prompt to 73K+ chars from one
search hit.

Changes:
- Add _COMPACTION_PREFIXES and _is_compaction_summary() helper
- Filter compaction summaries from bookend_start and bookend_end in _discover()
- Cap bookend content to 1200 chars and window content to 4000 chars
- Add content_truncated/original_content_chars metadata when truncation occurs
- Add 6 regression tests covering prefix detection, bookend filtering,
  content capping, and legacy [CONTEXT SUMMARY] prefix

Fixes NousResearch#43175
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/tools Tool registry, model_tools, toolsets tool/skills Skills system (list, view, manage) labels Jun 10, 2026

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for targeting a real current-main retrieval bloat path: _discover() still returns unbounded bookends and windows at tools/session_search_tool.py:599-601.

Problems

  • The new prefix-only predicate misses merged handoffs. Current compaction explicitly recognizes summaries after _MERGED_SUMMARY_DELIMITER in agent/context_compressor.py:2261-2270; those messages would still enter bookends.
  • Current title-match discovery has a separate response construction at tools/session_search_tool.py:487-489, using unbounded _shape_message() calls. The proposed _discover() change does not cover that path.

Suggested changes

  • Centralize bounded, compaction-aware shaping and use it for both normal and title-match discovery results.
  • Add tests for merged-summary bookends and a title query resolving to a session with a long handoff.

Automated hermes-sweeper review.


def _is_compaction_summary(content: str) -> bool:
"""Return True if *content* looks like a generated compaction handoff."""
if not content:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This only detects a handoff when the prefix begins the message. Current compaction also recognizes merged summaries whose handoff starts after _MERGED_SUMMARY_DELIMITER (agent/context_compressor.py:2261-2270); those can still be returned in bookends. Please cover that merged form as well.

@teknium1 teknium1 added sweeper:blast-contained Sweeper blast radius: contained — one narrow path / opt-in / few users area/sessions Session lifecycle, resume, persistence, history labels Jul 14, 2026
@teknium1

Copy link
Copy Markdown
Collaborator

Merged via #69334 (commit 9bb253d). Your commit was cherry-picked with authorship preserved, rebased over the schema-v23 FTS work. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/sessions Session lifecycle, resume, persistence, history comp/tools Tool registry, model_tools, toolsets P2 Medium — degraded but workaround exists sweeper:blast-contained Sweeper blast radius: contained — one narrow path / opt-in / few users tool/skills Skills system (list, view, manage) type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

session_search discovery can rehydrate huge compaction summaries via bookends

3 participants