Skip to content

refactor: ship segment-first compaction system#82

Open
minpeter wants to merge 8 commits intomainfrom
feat/compact-system-polish
Open

refactor: ship segment-first compaction system#82
minpeter wants to merge 8 commits intomainfrom
feat/compact-system-polish

Conversation

@minpeter
Copy link
Owner

@minpeter minpeter commented Mar 12, 2026

Summary

  • move harness compaction onto segment-based prepared artifacts, policy/planner helpers, and shared orchestration
  • align TUI and headless runtimes with the shared compaction flow and add a CEA guard for empty prepared message lists
  • update harness release notes and add a changeset for the segment-first compaction rollout

Verification

  • pnpm run check
  • pnpm run typecheck
  • pnpm run test
  • pnpm run build
  • node --import tsx manual QA for segment-only prepared compaction flow
  • node --import tsx manual QA for zero-budget truncation and empty-message guard

Summary by cubic

Ships the segment-first compaction system and a shared compaction orchestrator across @ai-sdk-tool/harness, @ai-sdk-tool/tui, and @ai-sdk-tool/headless. This unifies compaction, improves reliability and performance, and updates the API and docs.

  • Refactors

    • Moved MessageHistory to segment-based state and prepared artifacts (PreparedCompaction.segments, baseSegmentIds).
    • Added CompactionOrchestrator with shared cores (applyReadyCompactionCore, blockAtHardLimitCore, discardAllJobsCore); TUI and headless now use it.
    • Extracted policy/planner helpers (shouldStartSpeculativeCompaction, needsCompactionFromUsage, isAtHardContextLimitFromUsage, getRecommendedMaxOutputTokens, calculateCompactionSplitIndex).
    • Safety: preserve the last valid tool pair on zero-budget truncation; CEA now throws on empty prepared message lists.
    • Refined compaction rejection handling (no refire on reject, clear callbacks) and cached materialized prefix checks for faster prepared-apply paths.
  • Migration

    • Replace compactionConfig with compaction (use summarizeFn, speculativeStartRatio).
    • Prefer getMessagesForLLM(); getMessagesForLLMAsync() remains as a deprecated wrapper.
    • Use prepareSpeculativeCompaction() + applyPreparedCompaction() or compact() (no fire-and-forget).
    • If you consume prepared payloads, switch from messages/summaries/baseSummaryIds to segments/baseSegmentIds.

Written for commit c6e628f. Summary will update on new commits.

minpeter and others added 6 commits March 13, 2026 02:33
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2026

Important

Review skipped

Auto reviews are disabled on this repository. To trigger a review, include @crb review in the PR description. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a2b5ccf2-cbd0-4819-97a1-2e088b16bf66

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/compact-system-polish
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Tip

CodeRabbit can use Trivy to scan for security misconfigurations and secrets in Infrastructure as Code files.

Add a .trivyignore file to your project to customize which findings Trivy reports.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant refactoring of the message compaction system within the AI SDK harness. The core change involves transitioning to a segment-based approach for managing message history, which is supported by new, dedicated modules for compaction planning, policy, and orchestration. This centralization and modularization aim to improve the reliability and maintainability of context management. The changes are integrated across the TUI and headless runtimes, ensuring a unified and more robust handling of conversation context, including a new guard against empty message lists for model calls.

Highlights

  • Segment-First Compaction System: The harness compaction system has been moved to a segment-based approach, utilizing prepared artifacts, policy/planner helpers, and shared orchestration logic.
  • Runtime Alignment: Both the Text User Interface (TUI) and headless runtimes have been aligned with the new shared compaction flow, ensuring consistent message history management.
  • Context Guard for Empty Messages: A safeguard has been added to the CEA (Contextual Entity Agent) to prevent model calls with empty prepared message lists, especially under tight context budgets, improving system robustness.
  • Updated Documentation and Changelog: Harness release notes and documentation have been updated to reflect the new segment-first compaction rollout and API changes.
Changelog
  • .changeset/segment-first-compaction.md
    • Refactored to ship the segment-first compaction system across the shared runtimes.
    • Moved harness compaction onto segment-based state and prepared artifacts.
    • Shared compaction orchestration across TUI and headless runtimes.
    • Guarded CEA model calls from empty prepared message lists under tight context budgets.
  • packages/harness/CHANGELOG.md
    • Updated compaction API description: removed fire-and-forget compaction race condition, now using explicit compact() or prepared compaction via prepareSpeculativeCompaction() / applyPreparedCompaction().
    • Clarified getMessagesForLLMAsync() as a deprecated compatibility wrapper around getMessagesForLLM().
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a significant and well-executed refactoring of the compaction system. By moving to a segment-based state in MessageHistory and extracting logic into dedicated compaction-policy, compaction-planner, and compaction-orchestrator modules, you've greatly improved modularity, reduced code duplication between the TUI and headless runners, and enhanced overall maintainability. The changes are robust and the addition of related tests is appreciated. My feedback includes a few minor suggestions to improve code clarity by removing some redundant checks in the new compaction-orchestrator.ts file.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

2 issues found across 18 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/tui/src/agent-tui.ts">

<violation number="1" location="packages/tui/src/agent-tui.ts:567">
P2: Rejected compaction can produce duplicate “Compaction skipped” notices because `onRejected` is invoked twice in the apply-ready path.</violation>
</file>

<file name="packages/harness/src/message-history.ts">

<violation number="1" location="packages/harness/src/message-history.ts:1216">
P1: `getMaterializedMessages()` is called on every iteration of the `every()` callback, re-cloning all messages each time. Cache the result in a local variable before the prefix checks to avoid O(N×M) allocations.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 618d458d80

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

minpeter and others added 2 commits March 13, 2026 02:59
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
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