diff --git a/coverage.txt b/coverage.txt index d97fe82a..4dedc14a 100644 --- a/coverage.txt +++ b/coverage.txt @@ -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.39 | 86.75 | 88.37 | ℹ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ℹ end of coverage report diff --git a/src/tui/app.js b/src/tui/app.js index e106b992..e0253348 100644 --- a/src/tui/app.js +++ b/src/tui/app.js @@ -769,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 8b6cfcea..9cdc7000 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. * @@ -172,6 +196,7 @@ const ConversationArea = forwardRef(function ConversationArea( reasoningCollapsed, toolCallCollapsed, hidden = false, + contextWindow = 0, }, ref, ) { @@ -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,