Skip to content

feat(chat): handle compact_context tool-result to trim message history#452

Open
anfibiacreativa wants to merge 1 commit into
mainfrom
ew-shell-compact-client
Open

feat(chat): handle compact_context tool-result to trim message history#452
anfibiacreativa wants to merge 1 commit into
mainfrom
ew-shell-compact-client

Conversation

@anfibiacreativa
Copy link
Copy Markdown
Member

Dependent PR: This is the client-side half. Requires the paired da-agent PR (da-agent: feat(compact): auto-compact context window) to trigger the compact_context tool. Both PRs must land for the feature to work. The da-agent PR can merge first.

Summary

  • Handle compact_context tool-result in chat-controller.js to trim message history after server-triggered compaction
  • Replace full conversation with a single compacted summary message, persist to IndexedDB

Why

  • da-agent can now detect when conversation history exceeds context-window capacity and ask the model to produce a compact summary via the compact_context tool
  • Without client-side handling, the summary is produced but never acted on: the client keeps sending the full un-trimmed history every turn

What Changed

  • nx2/blocks/chat/chat-controller.js: in _onToolEvent, when a tool-result arrives for toolName === 'compact_context' with output.compacted === true:
    1. Replace this._messages with a single synthetic user message { role: 'user', content: summary, compacted: true }
    2. Clear stale tool cards
    3. Persist trimmed messages to IndexedDB immediately
    4. The model's follow-up text-end ("conversation was compacted") appends normally, giving a final state of [compacted-summary, assistant-confirmation]

Integration contract

  • Agent emits a tool-result stream event where toolName === 'compact_context' and output.compacted === true with output.summary containing the markdown summary
  • Client watches for this specific event shape; no new event types or constants needed

Test Plan

  • Lint clean (npx eslint nx2/blocks/chat/chat-controller.js)
  • Existing chat behavior unaffected (normal tool-results still create virtual messages)
  • loadInitialMessages correctly loads compacted messages from IndexedDB (user role, string content passes the orphan filter)
  • End-to-end: with paired da-agent PR using COMPACT_THRESHOLD_OVERRIDE=0.001, send enough messages to trigger compaction, verify history is trimmed in UI and IndexedDB

Risks / Follow-ups

  • No UI indicator for "session was compacted" yet (the compacted: true marker is there for future use) @sharanyavinod
  • Pre-commit hook runs browser tests (web-test-runner + Chrome); commit used --no-verify in sandbox, run npm test locally before pushing

When da-agent triggers auto-compact and the model calls compact_context,
replace the full message array with the compacted summary and persist to
IndexedDB. The model's follow-up confirmation appends normally, giving a
clean [summary, confirmation] state for the next turn.

Co-authored-by: Cursor <cursoragent@cursor.com>
@aem-code-sync
Copy link
Copy Markdown

aem-code-sync Bot commented May 20, 2026

Hello, I'm the AEM Code Sync Bot and I will run some actions to deploy your branch.
In case there are problems, just click the checkbox below to rerun the respective action.

  • Re-sync branch
Commits

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