Skip to content

fix: prevent race condition causing 'Already compacted' error - #5

Open
Bjynt wants to merge 1 commit into
capyup:mainfrom
Bjynt:main
Open

fix: prevent race condition causing 'Already compacted' error#5
Bjynt wants to merge 1 commit into
capyup:mainfrom
Bjynt:main

Conversation

@Bjynt

@Bjynt Bjynt commented Aug 20, 2026

Copy link
Copy Markdown

Summary

Fixes the race condition in the auto-compact extension that causes 'Compaction failed: Already compacted' errors.

Root Cause

The event handler checked then deferred via . Before the callback ran, another event (turn_start, turn_end, session_start) could trigger compaction. When the callback executed, it called without re-checking , causing a second call while one was in progress (or just completed). Pi core then threw 'Already compacted' because the last session entry was already a compaction entry.

Changes

  1. ** function**: Added defensive guards at the start:

    • Early return if is true (prevents concurrent calls)
    • Early return if the last session entry is already a compaction (prevents 'Already compacted' from pi core)
  2. ** event handler's callback**: Added re-check of before calling , preventing the race where another event triggers compaction during the deferral window.

These are minimal, focused changes that address the exact race condition.

- Add guard in triggerAutoCompact to check pendingCompaction and last entry type
- Re-check pendingCompaction in context event handler's setImmediate callback
- Prevents duplicate compaction calls from racing and throwing 'Already compacted'
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