From af51830beb7a8d8f53da5b37c13e5f080e31ed31 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 17 Nov 2026 22:00:28 -0500 Subject: [PATCH 1/2] fix: auto-compact from the TUI when context reaches 80% of the window Compaction was only reachable via /compact and the loop-detection third strike. Add shouldAutoCompact and autoCompactIfNeeded, which recount the context from the checkpointer and call handleCompact() once the status-bar token count reaches summarization.contextFraction (default 0.8) of the context window. Checked before each new message and after each turn. Pass the resolved contextWindow from App into ConversationArea. --- coverage.txt | 8 ++-- src/tui/app.js | 1 + src/tui/conversationArea.js | 50 ++++++++++++++++++++++++- tests/unit/tui/conversationArea.test.js | 40 ++++++++++++++++++++ 4 files changed, 93 insertions(+), 6 deletions(-) diff --git a/coverage.txt b/coverage.txt index d97fe82a..a6fad623 100644 --- a/coverage.txt +++ b/coverage.txt @@ -61,7 +61,7 @@ ℹ copilotRetryMiddleware.js | 88.52 | 79.49 | 83.33 | 75-88 ℹ index.js | 100.00 | 100.00 | 100.00 | ℹ modelInfo.js | 97.25 | 93.98 | 100.00 | 128-129 131-132 179-180 -ℹ openai.js | 92.72 | 75.71 | 78.57 | 65 90-92 138-139 145-146 233 236-238 257-259 267-271 279-280 292-295 +ℹ openai.js | 91.60 | 74.65 | 78.57 | 65 90-92 138-139 145-146 181-184 233 236-238 257-259 267-271 279-280 292-295 ℹ summarizationMiddleware.js | 97.01 | 94.00 | 83.33 | 107 126-128 149 ℹ tokenBudget.js | 97.14 | 85.71 | 82.35 | 50-51 83-84 ℹ tokenBudgetMiddleware.js | 98.10 | 78.67 | 87.50 | 94-95 97-98 @@ -146,7 +146,7 @@ ℹ confluence | | | | ℹ index.js | 98.59 | 82.50 | 83.33 | 89 191-192 ℹ cron | | | | -ℹ index.js | 93.59 | 84.53 | 82.50 | 47-48 87-92 199-200 255-256 278-280 294-295 367-368 517-519 548-549 559-562 569-570 585-589 600-602 614-616 627-628 +ℹ index.js | 93.59 | 85.08 | 82.50 | 47-48 87-92 199-200 255-256 278-280 294-295 367-368 517-519 548-549 559-562 569-570 585-589 600-602 614-616 627-628 ℹ data | | | | ℹ index.js | 98.90 | 96.49 | 100.00 | 158 240-241 ℹ date | | | | @@ -243,7 +243,7 @@ ℹ yaml | | | | ℹ index.js | 100.00 | 94.38 | 100.00 | ℹ tui | | | | -ℹ app.js | 45.62 | 56.52 | 21.05 | 160 171 181-187 197-206 214-219 227-230 251-283 290-299 308-335 351-369 379-387 394-402 409-454 460-464 468-591 602-606 609-619 622-635 641-644 656-661 663-667 669-673 675-682 684-690 692-700 715-725 732-773 800-801 +ℹ app.js | 45.57 | 56.52 | 21.05 | 160 171 181-187 197-206 214-219 227-230 251-283 290-299 308-335 351-369 379-387 394-402 409-454 460-464 468-591 602-606 609-619 622-635 641-644 656-661 663-667 669-673 675-682 684-690 692-700 715-725 732-774 801-802 ℹ banner.js | 90.36 | 100.00 | 83.33 | 27-34 ℹ commandHelp.js | 100.00 | 100.00 | 100.00 | ℹ commandParser.js | 97.56 | 89.87 | 100.00 | 90-95 @@ -286,6 +286,6 @@ ℹ loadAgents.js | 74.29 | 78.57 | 50.00 | 25-26 41 56-70 ℹ project.js | 100.00 | 100.00 | 100.00 | ℹ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -ℹ all files | 93.39 | 86.74 | 88.37 | +ℹ all files | 93.38 | 86.74 | 88.37 | ℹ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ℹ end of coverage report diff --git a/src/tui/app.js b/src/tui/app.js index e106b992..66c0f451 100644 --- a/src/tui/app.js +++ b/src/tui/app.js @@ -760,6 +760,7 @@ function App({ onViewChange: handleViewChange, messageCountRef, compactContext, + contextWindow, getContextMessages, model, systemPrompt, diff --git a/src/tui/conversationArea.js b/src/tui/conversationArea.js index 8b6cfcea..a786674b 100644 --- a/src/tui/conversationArea.js +++ b/src/tui/conversationArea.js @@ -95,6 +95,30 @@ export function shouldContinueAfterCompact({ compacted, autoContinueCount, autoC return compacted === true && autoContinueCount < autoContinueLimit; } +/** Default fraction of the context window at which the TUI compacts automatically. */ +export const DEFAULT_AUTO_COMPACT_FRACTION = 0.8; + +/** + * Decide whether the context has reached the auto-compaction threshold. + * + * Driven purely by the same token count and context window the status-bar meter + * uses, so compaction fires exactly when the meter reaches the threshold. + * + * @param {Object} params - Decision inputs + * @param {number} params.contextSize - Current context size in tokens + * @param {number} params.contextWindow - Context window in tokens (0/unset disables) + * @param {number} [params.fraction=0.8] - Fraction of the window that triggers compaction + * @returns {boolean} True when the context should be compacted now + */ +export function shouldAutoCompact({ + contextSize, + contextWindow, + fraction = DEFAULT_AUTO_COMPACT_FRACTION, +}) { + if (!(contextWindow > 0) || !(contextSize > 0)) return false; + return contextSize >= contextWindow * fraction; +} + /** * Decide what to do after a turn when the loop detector may have abandoned the stream. * @@ -163,6 +187,7 @@ const ConversationArea = forwardRef(function ConversationArea( onViewChange, messageCountRef, compactContext, + contextWindow = 0, getContextMessages, model, systemPrompt, @@ -456,6 +481,20 @@ const ConversationArea = forwardRef(function ConversationArea( } }; + /** + * Compact the context when it has reached the auto-compaction threshold. + * Recounts from the checkpointer first so the decision uses the live size. + * @returns {Promise} True when a compaction ran and succeeded + */ + const autoCompactIfNeeded = async () => { + if (!sessionState || !compactContext) return false; + const size = await updateContextSize(sessionState, config); + const fraction = config?.summarization?.contextFraction ?? DEFAULT_AUTO_COMPACT_FRACTION; + if (!shouldAutoCompact({ contextSize: size, contextWindow, fraction })) return false; + logger.info({ size, contextWindow, fraction }, "[compact] auto-compacting at threshold"); + return handleCompact(); + }; + const handleChat = async (text, options = {}) => { if (shouldAbort()) return; gcManager?.(); @@ -478,6 +517,10 @@ const ConversationArea = forwardRef(function ConversationArea( return; } + // Compact BEFORE the new user exchange is recorded: compaction reloads the + // session conversation from the checkpointer, which would drop it. + await autoCompactIfNeeded(); + // silentUser: dispatch the message without rendering it as a user message // in the TUI (e.g., synthesized skill prompts). The assistant's streaming // response still renders normally. @@ -636,7 +679,9 @@ const ConversationArea = forwardRef(function ConversationArea( role: "assistant", content: responseContent, }); - updateContextSize(sessionState, config); + // Turn finished: compact now if the context crossed the threshold + // during it (the next turn would otherwise start over the limit). + await autoCompactIfNeeded(); } if (onSaveSession) { onSaveSession(); @@ -744,7 +789,7 @@ const ConversationArea = forwardRef(function ConversationArea( clearTimeout(contextUpdateTimerRef.current); contextUpdateTimerRef.current = null; } - if (!sessionState) return; + if (!sessionState) return 0; // When an explicit message set is provided (e.g. the user's just-sent // message, which isn't in the checkpointer yet on the first turn), use // it directly. Otherwise source the conversation from the real @@ -780,6 +825,7 @@ const ConversationArea = forwardRef(function ConversationArea( } setContextSize(totalTokens); onContextChange?.(totalTokens); + return totalTokens; }, [model, getContextMessages, systemPrompt], ); diff --git a/tests/unit/tui/conversationArea.test.js b/tests/unit/tui/conversationArea.test.js index 9e00a383..f8e3da6c 100644 --- a/tests/unit/tui/conversationArea.test.js +++ b/tests/unit/tui/conversationArea.test.js @@ -90,6 +90,46 @@ describe("shouldContinueAfterCompact", () => { }); }); +describe("shouldAutoCompact", () => { + it("fires at exactly 80% of the context window", async () => { + const mod = await import("../../../src/tui/conversationArea.js"); + assert.strictEqual(mod.shouldAutoCompact({ contextSize: 80000, contextWindow: 100000 }), true); + }); + + it("fires above the threshold", async () => { + const mod = await import("../../../src/tui/conversationArea.js"); + assert.strictEqual(mod.shouldAutoCompact({ contextSize: 95000, contextWindow: 100000 }), true); + }); + + it("does not fire below the threshold", async () => { + const mod = await import("../../../src/tui/conversationArea.js"); + assert.strictEqual(mod.shouldAutoCompact({ contextSize: 79999, contextWindow: 100000 }), false); + }); + + it("honors a custom fraction", async () => { + const mod = await import("../../../src/tui/conversationArea.js"); + const params = { contextSize: 60000, contextWindow: 100000 }; + assert.strictEqual(mod.shouldAutoCompact({ ...params, fraction: 0.5 }), true); + assert.strictEqual(mod.shouldAutoCompact({ ...params, fraction: 0.7 }), false); + }); + + it("never fires without a context window or size", async () => { + const mod = await import("../../../src/tui/conversationArea.js"); + assert.strictEqual(mod.shouldAutoCompact({ contextSize: 5000, contextWindow: 0 }), false); + assert.strictEqual( + mod.shouldAutoCompact({ contextSize: 5000, contextWindow: undefined }), + false, + ); + assert.strictEqual(mod.shouldAutoCompact({ contextSize: 0, contextWindow: 100000 }), false); + assert.strictEqual(mod.shouldAutoCompact({ contextSize: undefined, contextWindow: 1 }), false); + }); + + it("defaults to 80%", async () => { + const mod = await import("../../../src/tui/conversationArea.js"); + assert.strictEqual(mod.DEFAULT_AUTO_COMPACT_FRACTION, 0.8); + }); +}); + describe("decideLoopAction", () => { const base = { loopDetected: true, From 42328ac3750b690455c2fabdcf6b3b92f641cd20 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 17 Nov 2026 22:03:17 -0500 Subject: [PATCH 2/2] refactor: append contextWindow prop to the end of ConversationArea props --- coverage.txt | 4 ++-- src/tui/app.js | 2 +- src/tui/conversationArea.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/coverage.txt b/coverage.txt index a6fad623..4dedc14a 100644 --- a/coverage.txt +++ b/coverage.txt @@ -61,7 +61,7 @@ ℹ copilotRetryMiddleware.js | 88.52 | 79.49 | 83.33 | 75-88 ℹ index.js | 100.00 | 100.00 | 100.00 | ℹ modelInfo.js | 97.25 | 93.98 | 100.00 | 128-129 131-132 179-180 -ℹ openai.js | 91.60 | 74.65 | 78.57 | 65 90-92 138-139 145-146 181-184 233 236-238 257-259 267-271 279-280 292-295 +ℹ openai.js | 92.72 | 75.71 | 78.57 | 65 90-92 138-139 145-146 233 236-238 257-259 267-271 279-280 292-295 ℹ summarizationMiddleware.js | 97.01 | 94.00 | 83.33 | 107 126-128 149 ℹ tokenBudget.js | 97.14 | 85.71 | 82.35 | 50-51 83-84 ℹ tokenBudgetMiddleware.js | 98.10 | 78.67 | 87.50 | 94-95 97-98 @@ -286,6 +286,6 @@ ℹ loadAgents.js | 74.29 | 78.57 | 50.00 | 25-26 41 56-70 ℹ project.js | 100.00 | 100.00 | 100.00 | ℹ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -ℹ all files | 93.38 | 86.74 | 88.37 | +ℹ all files | 93.39 | 86.75 | 88.37 | ℹ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ℹ end of coverage report diff --git a/src/tui/app.js b/src/tui/app.js index 66c0f451..e0253348 100644 --- a/src/tui/app.js +++ b/src/tui/app.js @@ -760,7 +760,6 @@ function App({ onViewChange: handleViewChange, messageCountRef, compactContext, - contextWindow, getContextMessages, model, systemPrompt, @@ -770,6 +769,7 @@ function App({ reasoningCollapsed, toolCallCollapsed, hidden: currentView !== PANELS.CONVERSATION, + contextWindow, }), currentView !== PANELS.CONVERSATION ? panelComponent : null, ), diff --git a/src/tui/conversationArea.js b/src/tui/conversationArea.js index a786674b..9cdc7000 100644 --- a/src/tui/conversationArea.js +++ b/src/tui/conversationArea.js @@ -187,7 +187,6 @@ const ConversationArea = forwardRef(function ConversationArea( onViewChange, messageCountRef, compactContext, - contextWindow = 0, getContextMessages, model, systemPrompt, @@ -197,6 +196,7 @@ const ConversationArea = forwardRef(function ConversationArea( reasoningCollapsed, toolCallCollapsed, hidden = false, + contextWindow = 0, }, ref, ) {