Skip to content

Conversation

@ethanndickson
Copy link
Member

@ethanndickson ethanndickson commented Nov 18, 2025 β€’

Generated with mux

Stack

  1. πŸ€– feat: add auto-compaction configuration UI #685
  2. πŸ€– feat: add auto-compaction with progressive warnings #683
  3. πŸ€– refactor: move compaction logic to backend #670 β¬… This PR
  4. πŸ€– refactor: use message queue for compact continue messages #650 (base)

Summary

Moves history compaction handling from WorkspaceStore (frontend) to agentSession (backend) to centralize server-side operations and fix race conditions.

Relates to #651.

Changes

Backend (agentSession.ts)

  • Added handleCompactionCompletion() - detects compaction stream-end, extracts summary from event.parts, performs history replacement
  • Added handleCompactionAbort() - handles Ctrl+A (accept early with [truncated]) and Ctrl+C (cancel) flows
  • Added performCompaction() - atomically replaces chat history with summary message including cumulative usage
  • Implemented abandonPartial flag flow from IPC through to StreamAbortEvent
  • Extracts truncated message content from history instead of partialService

Frontend (WorkspaceStore.ts)

  • Removed handleCompactionCompletion() and handleCompactionAbort() methods
  • Removed performCompaction() method
  • Removed processedCompactionRequestIds Set
  • Simplified cancelCompaction() - just calls interruptStream with abandonPartial: true
  • Fixed Ctrl+A keybind to pass abandonPartial: false for early accept

Shared

  • Updated StreamAbortEvent to include abandonPartial?: boolean
  • historyService.clearHistory() now returns deleted sequence numbers
  • Created calculateCumulativeUsage() utility in displayUsage.ts to extract and sum usage from messages

Testing

  • Manual: /compact completes successfully
  • Manual: Ctrl+A during compaction accepts early with [truncated]
  • Manual: Ctrl+C during compaction cancels and enters edit mode
  • Verify cumulative usage preserved across multiple compactions

@ethanndickson ethanndickson force-pushed the fix-compaction-message-race branch from b186d47 to 7d2df23 Compare November 18, 2025 07:02
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.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 πŸ‘.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@ethanndickson ethanndickson changed the base branch from main to compact-messages-queue-system November 18, 2025 07:08
@ethanndickson ethanndickson force-pushed the fix-compaction-message-race branch from 7d2df23 to 2b2b2fc Compare November 18, 2025 07:08
@ethanndickson
Copy link
Member Author

@codex 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.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 πŸ‘.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@ethanndickson
Copy link
Member Author

@codex review

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@ethanndickson ethanndickson force-pushed the fix-compaction-message-race branch from eeafe28 to 1eda7a1 Compare November 18, 2025 10:51
Move history compaction handling from WorkspaceStore to agentSession to centralize server-side operations.

Backend changes:
- Added handleCompactionCompletion() to detect and handle compaction stream-end events
- Added handleCompactionAbort() to handle Ctrl+A (accept early with [truncated]) and Ctrl+C (cancel) flows
- Added performCompaction() to atomically replace chat history with summary message
- Added processedCompactionRequestIds Set to dedupe repeated compaction events
- Implemented abandonPartial flag flow from IPC through to StreamAbortEvent
- Extract truncated message content from history instead of partialService
- Wrap compaction handlers in try-catch to ensure stream events always forwarded
- Made performCompaction return Result type for proper error handling

Frontend changes:
- Removed compaction handlers from WorkspaceStore
- Simplified cancelCompaction() to just call interruptStream with abandonPartial flag
- Fixed Ctrl+A keybind to pass abandonPartial: false for early accept

Shared changes:
- Updated StreamAbortEvent to include abandonPartial flag
- historyService.clearHistory() now returns deleted sequence numbers
- Created calculateCumulativeUsage() utility to extract and sum usage from messages
- aiService respects abandonPartial flag - skips committing when true
@ethanndickson ethanndickson force-pushed the fix-compaction-message-race branch from 1eda7a1 to 4560627 Compare November 18, 2025 10:51
Move compaction handling from agentSession to dedicated CompactionHandler class.

Changes:
- Created src/node/services/compactionHandler.ts with CompactionHandler class
- Extracted handleAbort, handleCompletion, and performCompaction methods
- Updated agentSession.ts to delegate to CompactionHandler
- Moved frontend compaction handler to browser utils
- Reduced agentSession.ts from 764 to 589 lines

Benefits:
- Better separation of concerns
- Easier to test compaction logic independently
- Cleaner session orchestration code

_Generated with `mux`_

// Wait for first stream to complete
const collector1 = createEventCollector(env.sentEvents, workspaceId);
await collector1.waitForEvent("stream-end", 30000);
Copy link
Member Author

Choose a reason for hiding this comment

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

This was flaking on Thomas' PRs too, so I've bumped the timeout

Copy link
Contributor

@ibetitsmike ibetitsmike left a comment

Choose a reason for hiding this comment

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

lgtm but I'd add some testing

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