Skip to content

feat: compact - #22

Merged
bobrykov merged 1 commit into
masterfrom
feat/compact
May 9, 2026
Merged

feat: compact#22
bobrykov merged 1 commit into
masterfrom
feat/compact

Conversation

@bobrykov

@bobrykov bobrykov commented May 9, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@coderabbitai

coderabbitai Bot commented May 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 37d807a5-692c-4c5a-bb63-6edcfd98e9cb

📥 Commits

Reviewing files that changed from the base of the PR and between cf17ea7 and 9f8bcc4.

📒 Files selected for processing (2)
  • src/compact.rs
  • src/lib.rs

📝 Walkthrough

Walkthrough

A new context compaction module is introduced to manage and reduce agent conversation memory. It defines a pluggable async ContextCompactor trait, supporting types for metadata and telemetry, a built-in TruncatingCompactor implementation, utilities for splitting messages at turn boundaries, and a ContextManager that orchestrates compaction based on token thresholds and emergency zones.

Changes

Context Compaction System

Layer / File(s) Summary
Architecture & Compactor Trait Contract
src/compact.rs
Module documentation establishes "when to compact" vs "how to compact" separation. ContextCompactor trait defines the async object-safe contract for pluggable compaction strategies.
Data Contracts & Telemetry
src/compact.rs
CompactionContext, CompactReason, and CompactionOutcome define data shapes. Telemetry structures (CompactTelemetry, PreCompactStats, PostCompactStats) capture observability metadata.
Truncating Compactor Implementation
src/compact.rs
TruncatingCompactor implements ContextCompactor, dropping oldest messages while preserving a configurable number of recent messages and enforcing minimum message count.
Token Splitting Utility
src/compact.rs
TokenSplitter and SplitResult segment message histories at coherent turn boundaries (assistant→user transitions) into compactable and preserved segments with token accounting.
Error & Result Types
src/compact.rs
ContextOverflow terminal error contains token usage, window/message counts, and trigger reason. EnsureContextResult represents compaction outcome or no-action results.
Context Manager Orchestration
src/compact.rs
ContextManager estimates tokens, determines compaction triggers (threshold + emergency zone), and orchestrates compaction via configured ContextCompactor. Async ensure_context_fits and compact_manual methods return EnsureContextResult or ContextOverflow.
Tests & Module Export
src/compact.rs, src/lib.rs
Extensive unit tests verify token estimation, thresholds, splitting, truncation, compaction flows, and telemetry. Module is exported via pub mod compact; and documented in crate overview.
🚥 Pre-merge checks | ✅ 3 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'feat: compact' is overly vague and generic. While it references a real part of the changeset, it fails to clearly convey the primary purpose or main functionality being introduced. Replace with a more descriptive title such as 'feat: add context compaction system for managing agent conversation history' to clearly indicate the main feature being introduced.
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 50.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/compact

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/compact.rs`:
- Around line 253-265: The compacted constructor currently computes tokens_saved
incorrectly (tokens_after.saturating_sub(tokens_after)) and needs a real
tokens_before value; update pub fn compacted(messages: Vec<Message>,
tokens_after: u64) -> Self to accept a tokens_before: u64 parameter and set
tokens_saved = tokens_before.saturating_sub(tokens_after), leaving
messages_compacted = 0 (caller will set) and preserving success: true and error:
None; alternatively, if you prefer leaving tokens_saved to callers, remove the
tokens_saved field initialization here and document that callers must set it,
but the preferred fix is adding the tokens_before parameter and computing
tokens_saved in compacted.
- Around line 330-336: The docstring claims the compactor preserves the first
system prompt but the current compaction logic simply keeps the last
preserve_recent messages; modify the compaction routine (the code that currently
slices to keep the last preserve_recent messages) to also include the first
message when it has the expected role: check messages.first().map(|m| m.role)
and if it matches the prompt role (as described in the docstring) prepend that
first message to the preserved list and then append the last preserve_recent
messages without duplicating it (use preserve_recent, messages, and the
compaction function/logic identifiers to locate where to change the
slice/collection code); alternatively if you prefer the simpler fix, update the
docstring text to remove the claim about preserving the system prompt so it
matches the existing behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 62b2de40-7ee1-444e-8cba-78669c356360

📥 Commits

Reviewing files that changed from the base of the PR and between ad7e6cd and 5523916.

📒 Files selected for processing (2)
  • src/compact.rs
  • src/lib.rs

Comment thread src/compact.rs
Comment thread src/compact.rs
@bobrykov
bobrykov merged commit ce6f433 into master May 9, 2026
6 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Jun 30, 2026
@bobrykov
bobrykov deleted the feat/compact branch July 1, 2026 06:34
bobrykov added a commit that referenced this pull request Aug 18, 2026
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.

1 participant