Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions extensions/auto-compact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ export default function autoCompact(pi: ExtensionAPI) {
phase: AutoCompactPhase,
customInstructions?: string,
): void => {
// Guard: if compaction is already in progress, don't start another.
// This prevents races from callers that don't check pendingCompaction.
if (pendingCompaction) return;
// Guard: if the last session entry is already a compaction, pi core will
// throw "Already compacted". Skip to avoid the error.
const entries = ctx.sessionManager.getEntries();
const lastEntry = entries[entries.length - 1];
if (lastEntry?.type === "compaction") return;
pendingCompaction = true;
ctx.compact({
customInstructions,
Expand Down Expand Up @@ -382,11 +390,16 @@ export default function autoCompact(pi: ExtensionAPI) {
if (newMessages) {
truncationAppliedThisTurn = true;
setImmediate(() => {
triggerAutoCompact(
ctx,
"emergency",
"Emergency context truncation was applied. Generate a comprehensive summary.",
);
// Re-check pendingCompaction: another event may have triggered compaction
// while this callback was deferred. Without this check, we race and get
// "Already compacted" from pi core.
if (!pendingCompaction) {
triggerAutoCompact(
ctx,
"emergency",
"Emergency context truncation was applied. Generate a comprehensive summary.",
);
}
});
return { messages: newMessages };
}
Expand Down