From c6335ae601273de57a44a4f53ba4872af6737152 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 17 Nov 2026 22:20:40 -0500 Subject: [PATCH 1/3] fix: remove non-working summarization middleware PR #1881 moved auto-compaction into the TUI but left the library-replacing SummarizationMiddleware registered. Remove it, its onCompacted plumbing, and the trigger/keep/enabled config. Keep keepMessages and contextFraction, and make /compact and auto-compact honor keepMessages. --- config.yaml | 5 +- coverage.txt | 7 +- docs/CONFIGURATION.md | 25 +- docs/DOCKER.md | 8 +- index.js | 9 +- src/agent/deepAgents.js | 85 ++----- src/config/schemas/summarization.js | 38 +-- src/provider/summarizationMiddleware.js | 167 ------------- .../summarizationMiddleware.test.js | 230 ------------------ tests/unit/config/summarization.test.js | 112 ++------- .../provider/summarizationMiddleware.test.js | 171 ------------- 11 files changed, 61 insertions(+), 796 deletions(-) delete mode 100644 src/provider/summarizationMiddleware.js delete mode 100644 tests/integration/summarizationMiddleware.test.js delete mode 100644 tests/unit/provider/summarizationMiddleware.test.js diff --git a/config.yaml b/config.yaml index 8ea67273..b36c718f 100644 --- a/config.yaml +++ b/config.yaml @@ -221,11 +221,8 @@ subAgentsTemperature: textEditor: 0.4 translator: 0.3 summarization: - enabled: true keepMessages: 5 - trigger: - type: tokens - value: 100000 + contextFraction: 0.8 telemetry: enabled: false exporter: diff --git a/coverage.txt b/coverage.txt index 4dedc14a..c61dcbc9 100644 --- a/coverage.txt +++ b/coverage.txt @@ -8,7 +8,7 @@ ℹ compaction.js | 98.84 | 78.43 | 100.00 | 89-90 ℹ contextBackend.js | 100.00 | 100.00 | 100.00 | ℹ coreBackend.js | 100.00 | 100.00 | 100.00 | -ℹ deepAgents.js | 93.72 | 69.39 | 66.67 | 156-157 181 197-199 309-316 345-346 459-461 488-500 511 579-580 590-592 +ℹ deepAgents.js | 93.91 | 70.83 | 77.78 | 155-156 180 196-198 308-315 344-345 458-460 474-480 531-535 543-545 ℹ evictionCeiling.js | 100.00 | 100.00 | 100.00 | ℹ skillsMiddleware.js | 100.00 | 87.50 | 100.00 | ℹ config | | | | @@ -62,7 +62,6 @@ ℹ 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 -ℹ 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 ℹ sandbox | | | | @@ -187,7 +186,7 @@ ℹ xlsxParser.js | 82.21 | 83.33 | 88.89 | 47-48 93-112 130-137 150-156 ℹ zipExtractor.js | 82.32 | 77.78 | 66.67 | 62-63 104-107 124-142 149-152 ℹ git | | | | -ℹ index.js | 87.41 | 64.37 | 83.69 | 324-326 379-380 393-396 481-483 515-516 626-643 689-691 722-723 755-757 821-824 840-843 866-872 906 908-912 914-917 929-933 976-980 984 989-991 1006-1015 1024 1038-1044 1046-1049 1057-1063 1086-1091 1093-1099 1101-1110 1112-1115 1122-1126 1136-1143 1191-1192 1194-1199 1291-1292 1314-1315 1323-1324 1331-1332 1334-1340 1362 1366-1368 1373-1376 1428-1435 1439-1447 1450-1453 1455-1458 +ℹ index.js | 87.41 | 64.31 | 83.69 | 324-326 379-380 393-396 481-483 515-516 626-643 689-691 722-723 755-757 821-824 840-843 866-872 906 908-912 914-917 929-933 976-980 984 989-991 1006-1015 1024 1038-1044 1046-1049 1057-1063 1086-1091 1093-1099 1101-1110 1112-1115 1122-1126 1136-1143 1191-1192 1194-1199 1291-1292 1314-1315 1323-1324 1331-1332 1334-1340 1362 1366-1368 1373-1376 1428-1435 1439-1447 1450-1453 1455-1458 ℹ graphql | | | | ℹ index.js | 97.85 | 98.13 | 93.75 | 57-68 ℹ image | | | | @@ -286,6 +285,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.75 | 88.37 | +ℹ all files | 93.37 | 86.72 | 88.51 | ℹ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ℹ end of coverage report diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 11a9aa3e..b8753675 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -99,11 +99,8 @@ Graceful shutdown flushes all buffered log entries to disk before process exit. | `search` | `tavily.apiKey` | _(empty)_ | Tavily search API key | | `skillAgentMap` | `[].pattern` | _(none)_ | Regex pattern to match skill names | | | `[].agent` | _(none)_ | Agent name to assign when pattern matches | -| `summarization` | `enabled` | `false` | Enable proactive context compaction | -| | `trigger.type` | `tokens` | Trigger type (`tokens`, `messages`, `fraction`) | -| | `trigger.value` | `100000` | Trigger threshold value | -| | `keepMessages` | `5` | Most recent messages kept verbatim after compaction (older history becomes one summary) | -| | `keep` _(deprecated)_ | _(none)_ | Legacy form; a `messages` value is used when `keepMessages` is unset | +| `summarization` | `keepMessages` | `5` | Most recent messages kept verbatim after compaction (older history becomes one summary) | +| | `contextFraction` | `0.8` | Fraction of the context window at which the TUI auto-compacts | | `vector` | `model` | `local` | Embedding model (`local` or `openai`) | | | `projects..rootDir` | `.` | Project root directory to scan | | | `projects..dbPath` | _(none)_ | Path to sqlite-vec database file | @@ -151,28 +148,22 @@ Each element renders only when its boolean is `true` **and** its data is present ### Summarization -The `summarization` section controls proactive context compaction. When `enabled` is `false` (the default), deepagents' library default applies — a 170k token trigger that keeps the last 6 messages, and compaction only happens **reactively** after a request overflows the context window. When `enabled` is `true`, madz registers its own `SummarizationMiddleware` that fires at the configured `trigger`, so compaction is **proactive**. +The `summarization` section controls context compaction. The TUI compacts automatically once the context (the same count the status bar shows) reaches `contextFraction` of the model's context window, checked before each new message and after each turn. `/compact` runs the same routine manually, and the reactive context-window handler remains the safety net if a request still overflows. ```yaml summarization: - enabled: true - trigger: - type: tokens - value: 100000 keepMessages: 5 + contextFraction: 0.8 ``` -When the trigger fires, madz summarizes the history and **rewrites the checkpointer** to `[summary, ...last keepMessages messages]`. The system prompt is injected on every call rather than stored in state, so it is unaffected. The old history is not kept in state; the summarized messages are saved best-effort to `/conversation_history/` and the summary links to that file. +When compaction runs, madz summarizes the history and **rewrites the checkpointer** to `[summary, ...last keepMessages messages]`. The system prompt is injected on every call rather than stored in state, so it is unaffected. -- **`trigger`** — the threshold at which compaction fires. `tokens` counts the system prompt plus all stored messages; `messages` counts stored messages. `fraction` is not supported by the proactive middleware. - **`keepMessages`** — how many recent messages stay verbatim (default `5`). The tail never starts on a tool result: if the boundary lands on one, the whole tool call and result are summarized and the tail shrinks (possibly to zero). -- **`contextFraction`** (default `0.8`) — when the model's context length can be resolved at startup, the trigger is overridden with 80% of that context length (the configured `trigger.value` is then only a fallback). +- **`contextFraction`** (default `0.8`) — fraction of the context window at which auto-compaction fires. The window is the model's real context length when it can be resolved at startup, otherwise the configured `contextWindow`. -The summary is stored as a user message (some providers reject mid-conversation system messages). If summarization fails, state is left unchanged, a warning is logged, and compaction is retried after a few more messages; the reactive context-window handler remains the safety net. `/compact` uses the same routine. +The summary is stored as a user message (some providers reject mid-conversation system messages). If summarization fails, the legacy trim-only fallback is used. -Token counts come from the model's real tokenizer — the same count the status bar shows — so a status bar at 80% means compaction fires on the next model call. If the tokenizer is unavailable or errors, a characters-per-token estimate is used. - -**Note:** the custom middleware reaches the **orchestrator only**. Subagents are not forked, so they keep the library default `SummarizationMiddleware`. +**Note:** the removed keys `enabled`, `trigger` and `keep` are now rejected by the config schema; delete them from `config.yaml`. ### API Tool diff --git a/docs/DOCKER.md b/docs/DOCKER.md index 1b0254d9..0234d2cb 100644 --- a/docs/DOCKER.md +++ b/docs/DOCKER.md @@ -256,13 +256,7 @@ The active provider is selected by the `enabled` flag, not by config position or ### Summarization -| Variable | Default | Description | -| ----------------------------- | ---------- | ----------------------------------------------- | -| `SUMMARIZATION_ENABLED` | `false` | Enable proactive context compaction | -| `SUMMARIZATION_TRIGGER_TYPE` | `tokens` | Trigger type (`tokens`, `messages`, `fraction`) | -| `SUMMARIZATION_TRIGGER_VALUE` | `100000` | Trigger threshold value | -| `SUMMARIZATION_KEEP_TYPE` | `messages` | Keep type (`tokens`, `messages`, `fraction`) | -| `SUMMARIZATION_KEEP_VALUE` | `10` | Messages/tokens to keep after compaction | +Context compaction is configured in `config.yaml` (`summarization.keepMessages`, `summarization.contextFraction`); see [CONFIGURATION.md](CONFIGURATION.md#summarization). ### Vector Search diff --git a/index.js b/index.js index 5f501ce6..7b295922 100644 --- a/index.js +++ b/index.js @@ -134,8 +134,7 @@ export function bootstrap() { async function doBootstrap() { config = loadConfig(); - const { createDeepAgentsOrchestrator, toConversationExchange } = - await import("./src/agent/deepAgents.js"); + const { createDeepAgentsOrchestrator } = await import("./src/agent/deepAgents.js"); const { ensureToolsDir, ensureScreenshotsDir, ensureContextDir, expireEphemeralMemories } = await import("./src/memory/index.js"); const { ensureDownloadsDir } = await import("./src/downloads/index.js"); @@ -266,12 +265,6 @@ async function doBootstrap() { // Manual context compaction (the `/compact` slash command) compactContext = (options) => agent.compactContext(currentSessionConfig(), sessionState, options); - // Automatic compaction rewrites the checkpointer mid-run; keep the session's - // conversation in step so saved sessions and the TUI agree with the model. - agent.onCompacted?.((result) => { - sessionState.loadConversation(result.messages.map(toConversationExchange)); - }); - // Real LangChain message array from the checkpointer for the context counter. // Returns null on failure so the TUI falls back to sessionState. getContextMessages = async () => { diff --git a/src/agent/deepAgents.js b/src/agent/deepAgents.js index 57769489..74bee03d 100644 --- a/src/agent/deepAgents.js +++ b/src/agent/deepAgents.js @@ -16,7 +16,6 @@ import { getModelContextLength } from "../provider/modelInfo.js"; import { getActiveProviderConfig, getActiveProviderName } from "../provider/index.js"; import { createTokenBudgetMiddleware } from "../provider/tokenBudgetMiddleware.js"; import { createCopilotRetryMiddleware } from "../provider/copilotRetryMiddleware.js"; -import { createSummarizationMiddlewareFromConfig } from "../provider/summarizationMiddleware.js"; import { createEvictionAwareFilesystemMiddleware } from "./evictionCeiling.js"; import { createSkillsMiddleware } from "./skillsMiddleware.js"; import { @@ -325,7 +324,7 @@ export async function compactAgentContext(agent, config, sessionState, options = /** * Create a Deep Agents orchestrator with coding and utility sub-agents. - * Uses deepagents middleware for filesystem, memory, skills, and summarization. + * Uses deepagents middleware for filesystem, memory, and skills. * @param {import("@langchain/langgraph").BaseCheckpointSaver | null} [checkpointer=null] - Optional checkpointer * @returns {Promise<{agent: Object, model: Object}>} The orchestrator agent and its chat model */ @@ -462,36 +461,17 @@ export async function createDeepAgentsOrchestrator(checkpointer = null) { }, }); - // Configurable summarization middleware. Registered BEFORE the token-budget - // middleware so the budget observes the post-summarization message set. - // Returns null when the `summarization` config section is absent or - // `enabled` is false, so unset config is a true no-op (deepagents' library - // default of 170k trigger / keep 6 applies). When enabled, the returned - // middleware is named `SummarizationMiddleware`, which displaces the library - // default via same-name merge semantics in `createDeepAgent`. - // Derive the summarization trigger from the provider model context length. - // The configured token value is a guess that drifts from reality as models - // change. When the model's real context window is smaller than the - // configured trigger, the conversation overflows before summarization fires; - // when larger, we summarize too early. Resolve the context length at init - // and override the trigger with 80% of it. The 80% is hardcoded; no config - // schema change. If the context length cannot be resolved (unreachable, - // model not found, field absent), fall back to the configured token value so - // startup never blocks on a network call. + // Resolve the model's real context length at init. If it cannot be resolved + // (unreachable, model not found, field absent) startup continues and the + // configured context window is used. Auto-compaction itself runs from the + // TUI (see `autoCompactIfNeeded`). const contextLength = await getModelContextLength(providerConfig, { memoryDir }); - // Mutate the shared config singleton so the derived trigger and the - // server-derived context window are visible everywhere they are read - // (e.g. the /settings view and the TUI status-bar context meter). - // `loadConfig()` is cached, so this is the same object the SettingsPanel - // reads. The derived trigger is also used by the middleware created below. + // Mutate the shared config singleton so the server-derived context window is + // visible everywhere it is read (e.g. the /settings view and the TUI + // status-bar context meter). `loadConfig()` is cached, so this is the same + // object the SettingsPanel reads. if (contextLength !== undefined) { - const triggerTokens = Math.floor(contextLength * 0.8); - logger.info( - { contextLength, triggerTokens }, - "[summarization] derived trigger from provider model context length", - ); - config.summarization.trigger = { type: "tokens", value: triggerTokens }; - // Also update the active provider's context window so the TUI status bar + // Update the active provider's context window so the TUI status bar // spinner reflects the real model context length rather than the // configured default. `providerName` is the config key (e.g. "openai"). if (config.providers?.[providerName]) { @@ -499,32 +479,6 @@ export async function createDeepAgentsOrchestrator(checkpointer = null) { } } - // Listeners notified after an automatic compaction rewrites the checkpointer, - // so the TUI can resync its conversation view and context meter. - const compactionListeners = new Set(); - const summarizationMiddleware = createSummarizationMiddlewareFromConfig({ - backend, - model, - systemPrompt, - config: config.summarization, - onCompacted: (result) => { - for (const listener of compactionListeners) listener(result); - }, - // Same tokenizer the status bar uses, so "80%" means the same thing in - // both places. - tokenCounter: - typeof model.getNumTokensFromMessages === "function" - ? async (messages) => (await model.getNumTokensFromMessages(messages)).totalCount - : undefined, - }); - - // Image-dispatch middleware. Registered AFTER summarization and BEFORE - // token-budget: AgentNode composes the wrapModelCall chain backwards, so the - // last entry is innermost. Registering after summarization means this - // middleware observes the final post-summarization message set; registering - // before token-budget means the budget sees the injected image when - // estimating context cost. - // Copilot 403 retry middleware. Registered LAST so it is innermost and // retries the raw model call. Only wired for GitHub Copilot connections, // which signal rate limiting with 403 (the OpenAI SDK only retries @@ -539,10 +493,9 @@ export async function createDeepAgentsOrchestrator(checkpointer = null) { // stringified content exceeds `toolTokenLimitBeforeEvict * 4` characters // (default 20,000 tokens -> 80,000 chars), which destroys `readImage`'s // base64 JSON before the model can see it. - // Displaces the library default via same-name merge in `mergeMiddlewareStack` - // — the mechanism our SummarizationMiddleware above also relies on. The name - // is asserted in tests: if it drifts, a second copy stacks and the bug - // survives while looking fixed. + // Displaces the library default via same-name merge in `mergeMiddlewareStack`. + // The name is asserted in tests: if it drifts, a second copy stacks and the + // bug survives while looking fixed. const filesystemMiddleware = createEvictionAwareFilesystemMiddleware({ backend, permissions: [], @@ -564,7 +517,6 @@ export async function createDeepAgentsOrchestrator(checkpointer = null) { ...(skillPaths.length > 0 ? [createSkillsMiddleware({ registry: skillRegistry, sources: skillPaths })] : []), - ...(summarizationMiddleware ? [summarizationMiddleware] : []), ...(copilotRetryMiddleware ? [copilotRetryMiddleware] : []), ...(tokenBudgetMiddleware ? [tokenBudgetMiddleware] : []), ], @@ -575,12 +527,13 @@ export async function createDeepAgentsOrchestrator(checkpointer = null) { // window on demand. The callback is bound to the agent instance and accepts // a LangGraph runnable config (with `thread_id`), an optional session state // manager to sync, plus optional options. - agent.onCompacted = (listener) => { - compactionListeners.add(listener); - return () => compactionListeners.delete(listener); - }; agent.compactContext = async (config, sessionState, options) => - compactAgentContext(agent, config, sessionState, { ...options, backend, model }); + compactAgentContext(agent, config, sessionState, { + keepRecent: config.summarization?.keepMessages, + ...options, + backend, + model, + }); // Wire the middleware's 400 context-window handler to the agent's compaction // path. The middleware is created before the agent, so this closure is set diff --git a/src/config/schemas/summarization.js b/src/config/schemas/summarization.js index 1100c467..56118479 100644 --- a/src/config/schemas/summarization.js +++ b/src/config/schemas/summarization.js @@ -1,46 +1,18 @@ import { z } from "zod"; /** - * A summarization threshold: a trigger/keep policy expressed as a count of - * messages, a token count, or a fraction of the model's max input tokens. - * The `value` must be a positive number (fraction) or positive integer - * (messages/tokens). - */ -const SummarizationThresholdSchema = z.discriminatedUnion("type", [ - z.object({ - type: z.literal("tokens"), - value: z.number().int().positive(), - }), - z.object({ - type: z.literal("messages"), - value: z.number().int().positive(), - }), - z.object({ - type: z.literal("fraction"), - value: z.number().positive().max(1), - }), -]); - -/** - * Configuration for the deepagents `SummarizationMiddleware`. + * Configuration for context compaction. * - * When `enabled` is false (or the section is absent), madz does not register a - * custom summarization middleware and deepagents' library default applies - * (170k token trigger / keep last 6 messages). When enabled, the configured - * `trigger` and `keep` thresholds are passed to `createSummarizationMiddleware` - * so compaction is proactive rather than reactive. + * Compaction runs from the TUI: it fires automatically once the context + * reaches `contextFraction` of the model's context window, and on `/compact`. + * The reactive context-window handler remains the safety net. */ export const SummarizationSchema = z .object({ - enabled: z.boolean().default(false), - trigger: SummarizationThresholdSchema.optional(), - // Deprecated: use `keepMessages`. A `messages` keep value is still honored. - keep: SummarizationThresholdSchema.optional(), // Number of most recent messages kept verbatim after compaction; everything // older is replaced by one summary message. keepMessages: z.number().int().min(0).optional(), - // Fraction of the model's context window at which to compact. Used to - // derive a `tokens` trigger when the context length can be resolved. + // Fraction of the model's context window at which to auto-compact. contextFraction: z.number().positive().max(1).default(0.8), }) .strict() diff --git a/src/provider/summarizationMiddleware.js b/src/provider/summarizationMiddleware.js deleted file mode 100644 index ba7af4a9..00000000 --- a/src/provider/summarizationMiddleware.js +++ /dev/null @@ -1,167 +0,0 @@ -import { createMiddleware, countTokensApproximately } from "langchain"; -import { RemoveMessage, SystemMessage } from "@langchain/core/messages"; -import { compactMessages, DEFAULT_KEEP_MESSAGES } from "../agent/compaction.js"; -import { logger } from "../shared/logger.js"; - -/** After a failed compaction, wait for this many new messages before retrying. */ -const RETRY_AFTER_MESSAGES = 4; - -/** - * Resolve how many recent messages to keep verbatim. - * Precedence: `keepMessages`, then a legacy `keep: { type: "messages" }`, then 5. - * @param {Object} config - The resolved `summarization` config section - * @returns {number} Number of messages to keep - */ -export function resolveKeepMessages(config = {}) { - if (Number.isInteger(config.keepMessages) && config.keepMessages >= 0) { - return config.keepMessages; - } - if (config.keep?.type === "messages" && Number.isInteger(config.keep.value)) { - return config.keep.value; - } - return DEFAULT_KEEP_MESSAGES; -} - -/** - * Build the compaction middleware from madz config. - * - * When the conversation reaches the configured trigger, the history is - * summarized and the **checkpointer state is rewritten** to - * `[summary, ...last N messages]` (the system prompt is injected on every call - * and is not part of state). This replaces the library middleware, which only - * hides old messages behind a `_summarizationEvent` and leaves the stored - * history growing. The middleware is named `SummarizationMiddleware`, so - * `mergeMiddlewareStack` in `createDeepAgent` displaces the library default. - * - * Returns `null` when disabled or when no trigger is available, so the library - * default applies. - * - * @param {Object} options - Middleware options - * @param {Object} options.backend - deepagents backend used for history offload - * @param {Object} options.model - Chat model used to write the summary - * @param {Object} [options.config] - The resolved `summarization` config section - * @param {string} [options.systemPrompt] - System prompt, counted toward usage - * @param {Function} [options.tokenCounter] - async (messages) => token count - * @param {Function} [options.onCompacted] - Called with the compaction result - * @returns {Object|null} The middleware, or `null` when disabled - */ -export function createSummarizationMiddlewareFromConfig(options = {}) { - const { backend, model, config = {}, systemPrompt, tokenCounter, onCompacted } = options; - if (config.enabled !== true) return null; - - const trigger = config.trigger; - if (!trigger) { - logger.warn("[summarization] enabled but missing trigger; falling back to library defaults"); - return null; - } - if ( - (trigger.type !== "messages" && trigger.type !== "tokens") || - !Number.isFinite(trigger.value) || - trigger.value <= 0 - ) { - logger.warn( - { trigger }, - "[summarization] invalid trigger (expected type messages|tokens with a positive value); falling back to library defaults", - ); - return null; - } - const keep = resolveKeepMessages(config); - logger.info({ trigger, keep }, "[summarization] registering compaction middleware"); - - /** Message count at the last failed compaction, per thread. */ - const failedAtLength = new Map(); - - /** - * Count tokens for the messages the model would receive. - * @param {Array} messages - State messages - * @returns {Promise} Token count - */ - const count = async (messages) => { - const counted = systemPrompt ? [new SystemMessage(systemPrompt), ...messages] : messages; - if (typeof tokenCounter === "function") { - try { - return await tokenCounter(counted); - } catch (err) { - logger.warn( - { error: err?.message }, - "[summarization] token counter failed; using estimate", - ); - } - } - return countTokensApproximately(counted); - }; - - /** - * Decide whether the trigger has fired. - * @param {Array} messages - State messages - * @returns {Promise<{over: boolean, tokens: (number|undefined)}>} Decision - */ - const check = async (messages) => { - if (trigger.type === "messages") { - return { over: messages.length >= trigger.value, tokens: undefined }; - } - if (trigger.type === "tokens") { - const tokens = await count(messages); - return { over: tokens >= trigger.value, tokens }; - } - return { over: false, tokens: undefined }; - }; - - return createMiddleware({ - name: "SummarizationMiddleware", - beforeModel: { - canJumpTo: [], - hook: async (state, runtime) => { - const messages = state?.messages ?? []; - if (messages.length === 0) return undefined; - const threadKey = runtime?.configurable?.thread_id ?? ""; - const failedAt = failedAtLength.get(threadKey); - if (failedAt !== undefined && messages.length < failedAt + RETRY_AFTER_MESSAGES) { - return undefined; - } - let decision; - try { - decision = await check(messages); - } catch (err) { - logger.warn({ error: err?.message }, "[summarization] trigger check failed"); - return undefined; - } - logger.debug( - { tokens: decision.tokens, trigger, messages: messages.length, over: decision.over }, - "[summarization] trigger check", - ); - if (!decision.over) return undefined; - - try { - const threadId = runtime?.configurable?.thread_id; - const result = await compactMessages({ messages, model, backend, keep, threadId }); - if (!result) { - logger.info({ messages: messages.length }, "[summarization] nothing to compact"); - return undefined; - } - failedAtLength.delete(threadKey); - logger.info( - { summarized: result.summarized, kept: result.kept, offloadPath: result.offloadPath }, - "[summarization] compacted history", - ); - try { - await Promise.resolve(onCompacted?.(result)).catch((err) => { - logger.warn({ error: err?.message }, "[summarization] onCompacted callback failed"); - }); - } catch (err) { - logger.warn({ error: err?.message }, "[summarization] onCompacted callback failed"); - } - return { messages: [new RemoveMessage({ id: "__remove_all__" }), ...result.messages] }; - } catch (err) { - // Leave state untouched; the 400 context-window handler is the safety net. - failedAtLength.set(threadKey, messages.length); - logger.warn( - { error: err?.message }, - "[summarization] compaction failed; state unchanged", - ); - return undefined; - } - }, - }, - }); -} diff --git a/tests/integration/summarizationMiddleware.test.js b/tests/integration/summarizationMiddleware.test.js deleted file mode 100644 index 7729a0ce..00000000 --- a/tests/integration/summarizationMiddleware.test.js +++ /dev/null @@ -1,230 +0,0 @@ -import { describe, it, beforeEach, afterEach } from "node:test"; -import assert from "node:assert"; -import { createAgent } from "langchain"; -import { MemorySaver } from "@langchain/langgraph"; -import { ChatOpenAI } from "@langchain/openai"; -import { StateBackend } from "deepagents"; -import { tool } from "@langchain/core/tools"; -import { z } from "zod"; -import { createSummarizationMiddlewareFromConfig } from "../../src/provider/summarizationMiddleware.js"; - -/** - * Runtime probe for the blocking acceptance criterion: the custom - * `SummarizationMiddleware` must be PROVEN to fire at the configured value, - * not the 170k library default, and the library default must be genuinely - * absent from the effective stack. - * - * We drive a REAL `createAgent` with a REAL `ChatOpenAI` and a mocked fetch. - * The summarization middleware calls `chatModel.invoke(...)` to generate the - * summary, so a second fetch (beyond the agent dispatch) is the observable - * signal that compaction fired. With a `messages` trigger of 2 and 3 input - * messages, the 170k token default would NEVER fire — only the custom - * middleware at the configured value does. - */ -describe("integration - configurable summarization middleware fires at runtime", () => { - let originalFetch; - let fetchCalls; - - beforeEach(() => { - originalFetch = globalThis.fetch; - fetchCalls = 0; - }); - - afterEach(() => { - globalThis.fetch = originalFetch; - }); - - const completion = (content = "hi") => - new Response( - JSON.stringify({ - id: "x", - object: "chat.completion", - created: 0, - model: "gpt-4o", - choices: [{ index: 0, message: { role: "assistant", content }, finish_reason: "stop" }], - usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 }, - }), - { status: 200, headers: { "content-type": "application/json" } }, - ); - - const stubFetch = () => { - globalThis.fetch = async () => { - fetchCalls += 1; - return completion(); - }; - }; - - const makeModel = () => - new ChatOpenAI({ - model: "gpt-4o", - apiKey: "sk-test", - configuration: { baseURL: "http://127.0.0.1:9/v1" }, - maxRetries: 0, - }); - - const makeTool = () => - tool(() => "ok", { - name: "noop", - description: "d", - schema: z.object({ a: z.string().optional() }), - }); - - it("fires compaction at the configured messages trigger, not the 170k default", async () => { - stubFetch(); - const mw = createSummarizationMiddlewareFromConfig({ - backend: new StateBackend(), - model: makeModel(), - config: { - enabled: true, - trigger: { type: "messages", value: 2 }, - keepMessages: 1, - }, - }); - assert.ok(mw, "custom middleware should be built"); - assert.strictEqual(mw.name, "SummarizationMiddleware"); - - const agent = createAgent({ model: makeModel(), tools: [makeTool()], middleware: [mw] }); - await agent.invoke({ - messages: [ - { role: "user", content: "message one" }, - { role: "assistant", content: "reply one" }, - { role: "user", content: "message two" }, - ], - }); - - // fetch #1 = agent dispatch; fetch #2 = summary generation. The 170k - // token default would never summarize a 3-message conversation, so a - // second fetch proves the custom middleware (not the library default) - // fired at the configured value. - assert.ok(fetchCalls >= 2, `expected compaction to fire (>=2 fetches), got ${fetchCalls}`); - }); - - it("does not compact when the trigger is not crossed (library default absent)", async () => { - stubFetch(); - const mw = createSummarizationMiddlewareFromConfig({ - backend: new StateBackend(), - model: makeModel(), - config: { - enabled: true, - trigger: { type: "messages", value: 10 }, - keepMessages: 1, - }, - }); - assert.ok(mw, "custom middleware should be built"); - - const agent = createAgent({ model: makeModel(), tools: [makeTool()], middleware: [mw] }); - await agent.invoke({ - messages: [ - { role: "user", content: "message one" }, - { role: "assistant", content: "reply one" }, - ], - }); - - // Only the agent dispatch fetch should occur — no summary generation. - assert.strictEqual(fetchCalls, 1, "no compaction should fire below the trigger"); - }); - - it("rewrites the checkpointer to [summary, ...kept] instead of hiding old messages", async () => { - stubFetch(); - const mw = createSummarizationMiddlewareFromConfig({ - backend: new StateBackend(), - model: makeModel(), - config: { enabled: true, trigger: { type: "messages", value: 2 }, keepMessages: 1 }, - }); - const agent = createAgent({ - model: makeModel(), - tools: [makeTool()], - middleware: [mw], - checkpointer: new MemorySaver(), - }); - const cfg = { configurable: { thread_id: "rewrite" } }; - await agent.invoke( - { - messages: [ - { role: "user", content: "message one" }, - { role: "assistant", content: "reply one" }, - { role: "user", content: "message two" }, - ], - }, - cfg, - ); - const { values } = await agent.getState(cfg); - const texts = values.messages.map((m) => String(m.content)); - assert.ok(texts[0].startsWith("Here is a summary"), "first stored message is the summary"); - assert.ok(!texts.includes("message one"), "old messages are gone from the checkpointer"); - assert.ok(texts.includes("message two"), "the last message is kept"); - }); - - const tokenConfig = { - enabled: true, - trigger: { type: "tokens", value: 100000 }, - keepMessages: 1, - }; - const threeMessages = [ - { role: "user", content: "message one" }, - { role: "assistant", content: "reply one" }, - { role: "user", content: "message two" }, - ]; - - it("tokenCounter over threshold fires even when the library estimate is far below it", async () => { - stubFetch(); - const mw = createSummarizationMiddlewareFromConfig({ - backend: new StateBackend(), - model: makeModel(), - config: tokenConfig, - tokenCounter: async () => 100000, - }); - const agent = createAgent({ model: makeModel(), tools: [makeTool()], middleware: [mw] }); - await agent.invoke({ messages: threeMessages }); - assert.ok(fetchCalls >= 2, `expected compaction (>=2 fetches), got ${fetchCalls}`); - }); - - it("tokenCounter under threshold does not fire", async () => { - stubFetch(); - const mw = createSummarizationMiddlewareFromConfig({ - backend: new StateBackend(), - model: makeModel(), - config: tokenConfig, - tokenCounter: async () => 99999, - }); - const agent = createAgent({ model: makeModel(), tools: [makeTool()], middleware: [mw] }); - await agent.invoke({ messages: threeMessages }); - assert.strictEqual(fetchCalls, 1); - }); - - it("a throwing tokenCounter falls back to the built-in estimate", async () => { - stubFetch(); - const mw = createSummarizationMiddlewareFromConfig({ - backend: new StateBackend(), - model: makeModel(), - config: tokenConfig, - tokenCounter: async () => { - throw new Error("boom"); - }, - }); - const agent = createAgent({ model: makeModel(), tools: [makeTool()], middleware: [mw] }); - await agent.invoke({ messages: threeMessages }); - assert.strictEqual(fetchCalls, 1); - }); - - it("unset config reproduces today's behavior (no custom middleware, no compaction)", async () => { - stubFetch(); - // config absent -> factory returns null -> no custom middleware. - const mw = createSummarizationMiddlewareFromConfig({ - backend: new StateBackend(), - model: makeModel(), - }); - assert.strictEqual(mw, null, "unset config must be a true no-op"); - - const agent = createAgent({ model: makeModel(), tools: [makeTool()], middleware: [] }); - await agent.invoke({ - messages: [ - { role: "user", content: "message one" }, - { role: "assistant", content: "reply one" }, - { role: "user", content: "message two" }, - ], - }); - // No custom middleware -> no summary generation fetch. - assert.strictEqual(fetchCalls, 1, "no compaction without a custom middleware"); - }); -}); diff --git a/tests/unit/config/summarization.test.js b/tests/unit/config/summarization.test.js index a45da919..1dcc8328 100644 --- a/tests/unit/config/summarization.test.js +++ b/tests/unit/config/summarization.test.js @@ -4,106 +4,40 @@ import { SummarizationSchema } from "../../../src/config/schemas/summarization.j describe("SummarizationSchema", () => { describe("defaults", () => { - test("absent section parses to documented defaults (enabled false)", () => { + test("absent section parses to documented defaults", () => { const result = SummarizationSchema.parse({}); - assert.strictEqual(result.enabled, false); - assert.strictEqual(result.trigger, undefined); - assert.strictEqual(result.keep, undefined); - }); - - test("empty object parses to enabled false", () => { - const result = SummarizationSchema.parse({ enabled: false }); - assert.strictEqual(result.enabled, false); - }); - }); - - describe("trigger types", () => { - test("accepts a tokens trigger with a positive integer value", () => { - const result = SummarizationSchema.parse({ - enabled: true, - trigger: { type: "tokens", value: 28000 }, - keep: { type: "messages", value: 10 }, - }); - assert.strictEqual(result.trigger.type, "tokens"); - assert.strictEqual(result.trigger.value, 28000); - }); - - test("accepts a messages trigger with a positive integer value", () => { - const result = SummarizationSchema.parse({ - enabled: true, - trigger: { type: "messages", value: 20 }, - keep: { type: "messages", value: 6 }, - }); - assert.strictEqual(result.trigger.type, "messages"); - assert.strictEqual(result.trigger.value, 20); - }); - - test("accepts a fraction trigger with a value between 0 and 1", () => { - const result = SummarizationSchema.parse({ - enabled: true, - trigger: { type: "fraction", value: 0.85 }, - keep: { type: "fraction", value: 0.1 }, - }); - assert.strictEqual(result.trigger.type, "fraction"); - assert.strictEqual(result.trigger.value, 0.85); + assert.strictEqual(result.keepMessages, undefined); + assert.strictEqual(result.contextFraction, 0.8); }); }); describe("rejection", () => { - test("rejects a zero token trigger value", () => { - const result = SummarizationSchema.safeParse({ - enabled: true, - trigger: { type: "tokens", value: 0 }, - }); - assert.strictEqual(result.success, false); - }); - - test("rejects a negative messages trigger value", () => { - const result = SummarizationSchema.safeParse({ - enabled: true, - trigger: { type: "messages", value: -1 }, - }); - assert.strictEqual(result.success, false); - }); - - test("rejects a fraction trigger above 1", () => { - const result = SummarizationSchema.safeParse({ - enabled: true, - trigger: { type: "fraction", value: 1.5 }, - }); - assert.strictEqual(result.success, false); - }); - - test("rejects a non-integer token trigger value", () => { - const result = SummarizationSchema.safeParse({ - enabled: true, - trigger: { type: "tokens", value: 1.5 }, - }); - assert.strictEqual(result.success, false); + test("rejects unknown keys in the section", () => { + assert.strictEqual(SummarizationSchema.safeParse({ foo: 1 }).success, false); }); - test("rejects a wrong-typed trigger value", () => { - const result = SummarizationSchema.safeParse({ - enabled: true, - trigger: { type: "tokens", value: "abc" }, - }); - assert.strictEqual(result.success, false); + test("rejects the removed middleware keys", () => { + assert.strictEqual(SummarizationSchema.safeParse({ enabled: true }).success, false); + assert.strictEqual( + SummarizationSchema.safeParse({ trigger: { type: "tokens", value: 1000 } }).success, + false, + ); + assert.strictEqual( + SummarizationSchema.safeParse({ keep: { type: "messages", value: 5 } }).success, + false, + ); }); + }); - test("rejects an unknown trigger type", () => { - const result = SummarizationSchema.safeParse({ - enabled: true, - trigger: { type: "bytes", value: 100 }, - }); - assert.strictEqual(result.success, false); + describe("contextFraction", () => { + test("accepts values in (0, 1]", () => { + assert.strictEqual(SummarizationSchema.parse({ contextFraction: 0.5 }).contextFraction, 0.5); + assert.strictEqual(SummarizationSchema.parse({ contextFraction: 1 }).contextFraction, 1); }); - test("rejects unknown keys in the section", () => { - const result = SummarizationSchema.safeParse({ - enabled: true, - foo: 1, - }); - assert.strictEqual(result.success, false); + test("rejects zero and values above 1", () => { + assert.throws(() => SummarizationSchema.parse({ contextFraction: 0 })); + assert.throws(() => SummarizationSchema.parse({ contextFraction: 1.5 })); }); }); diff --git a/tests/unit/provider/summarizationMiddleware.test.js b/tests/unit/provider/summarizationMiddleware.test.js deleted file mode 100644 index 6d15f0de..00000000 --- a/tests/unit/provider/summarizationMiddleware.test.js +++ /dev/null @@ -1,171 +0,0 @@ -import { describe, it } from "node:test"; -import assert from "node:assert"; -import { HumanMessage } from "@langchain/core/messages"; -import { - createSummarizationMiddlewareFromConfig, - resolveKeepMessages, -} from "../../../src/provider/summarizationMiddleware.js"; - -const msgs = (n) => Array.from({ length: n }, (_, i) => new HumanMessage(`m${i}`)); -const model = { invoke: async () => ({ text: "SUM" }) }; -const enabled = (extra = {}) => ({ - enabled: true, - trigger: { type: "tokens", value: 1000 }, - keepMessages: 5, - ...extra, -}); -const run = (mw, state) => mw.beforeModel.hook(state, { configurable: { thread_id: "t" } }); - -describe("resolveKeepMessages", () => { - it("prefers keepMessages", () => { - assert.strictEqual( - resolveKeepMessages({ keepMessages: 3, keep: { type: "messages", value: 9 } }), - 3, - ); - }); - it("honors a legacy messages keep", () => { - assert.strictEqual(resolveKeepMessages({ keep: { type: "messages", value: 9 } }), 9); - }); - it("defaults to 5", () => { - assert.strictEqual(resolveKeepMessages({}), 5); - assert.strictEqual(resolveKeepMessages({ keep: { type: "tokens", value: 9 } }), 5); - }); -}); - -describe("createSummarizationMiddlewareFromConfig", () => { - it("returns null when config is absent or disabled", () => { - assert.strictEqual(createSummarizationMiddlewareFromConfig({}), null); - assert.strictEqual( - createSummarizationMiddlewareFromConfig({ config: { enabled: false } }), - null, - ); - }); - - it("returns null when enabled without a trigger", () => { - assert.strictEqual( - createSummarizationMiddlewareFromConfig({ config: { enabled: true } }), - null, - ); - }); - - it("is named SummarizationMiddleware and uses beforeModel", () => { - const mw = createSummarizationMiddlewareFromConfig({ model, config: enabled() }); - assert.strictEqual(mw.name, "SummarizationMiddleware"); - assert.strictEqual(typeof mw.beforeModel.hook, "function"); - assert.strictEqual(mw.wrapModelCall, undefined); - }); - - it("does nothing below the token trigger", async () => { - const mw = createSummarizationMiddlewareFromConfig({ - model, - config: enabled(), - tokenCounter: async () => 999, - }); - assert.strictEqual(await run(mw, { messages: msgs(20) }), undefined); - }); - - it("rewrites state to [RemoveAll, summary, ...5] at the trigger", async () => { - const seen = []; - const mw = createSummarizationMiddlewareFromConfig({ - model, - config: enabled(), - tokenCounter: async () => 1000, - onCompacted: (r) => seen.push(r), - }); - const out = await run(mw, { messages: msgs(20) }); - assert.strictEqual(out.messages.length, 7); - assert.strictEqual(out.messages[0].id, "__remove_all__"); - assert.match(out.messages[1].content, /SUM/); - assert.strictEqual(out.messages[2].content, "m15"); - assert.strictEqual(out.messages.at(-1).content, "m19"); - assert.strictEqual(seen.length, 1); - assert.strictEqual(seen[0].kept, 5); - }); - - it("counts the system prompt toward the trigger", async () => { - let counted; - const mw = createSummarizationMiddlewareFromConfig({ - model, - systemPrompt: "SYS", - config: enabled(), - tokenCounter: async (m) => { - counted = m; - return 0; - }, - }); - await run(mw, { messages: msgs(2) }); - assert.strictEqual(counted.length, 3); - assert.strictEqual(counted[0].content, "SYS"); - }); - - it("falls back to an estimate when the token counter throws", async () => { - const mw = createSummarizationMiddlewareFromConfig({ - model, - config: enabled({ trigger: { type: "tokens", value: 10 } }), - tokenCounter: async () => { - throw new Error("boom"); - }, - }); - const out = await run(mw, { messages: [new HumanMessage("x".repeat(4000)), ...msgs(8)] }); - assert.ok(out.messages.length > 0); - }); - - it("supports a messages trigger", async () => { - const mw = createSummarizationMiddlewareFromConfig({ - model, - config: enabled({ trigger: { type: "messages", value: 10 } }), - }); - assert.strictEqual(await run(mw, { messages: msgs(9) }), undefined); - assert.ok(await run(mw, { messages: msgs(10) })); - }); - - it("returns null (with a warning) for an unsupported trigger type", async () => { - const mw = createSummarizationMiddlewareFromConfig({ - model, - config: enabled({ trigger: { type: "fraction", value: 0.5 } }), - }); - assert.strictEqual(mw, null); - }); - - it("leaves state unchanged when summarization fails, and backs off", async () => { - let calls = 0; - const bad = { - invoke: async () => { - calls++; - throw new Error("down"); - }, - }; - const mw = createSummarizationMiddlewareFromConfig({ - model: bad, - config: enabled({ trigger: { type: "messages", value: 5 } }), - }); - assert.strictEqual(await run(mw, { messages: msgs(10) }), undefined); - assert.strictEqual(calls, 1); - // Too few new messages: do not retry yet. - assert.strictEqual(await run(mw, { messages: msgs(11) }), undefined); - assert.strictEqual(calls, 1); - // Enough new messages: retry. - await run(mw, { messages: msgs(14) }); - assert.strictEqual(calls, 2); - }); - - it("does nothing for empty state and survives a throwing onCompacted", async () => { - const mw = createSummarizationMiddlewareFromConfig({ - model, - config: enabled({ trigger: { type: "messages", value: 5 } }), - onCompacted: () => { - throw new Error("cb"); - }, - }); - assert.strictEqual(await run(mw, { messages: [] }), undefined); - assert.ok(await run(mw, { messages: msgs(10) })); - }); - - it("does nothing when there is nothing to compact", async () => { - const mw = createSummarizationMiddlewareFromConfig({ - model, - config: enabled({ trigger: { type: "messages", value: 3 } }), - }); - assert.strictEqual(await run(mw, { messages: msgs(4) }), undefined); - }); -}); From 031969de5a13c44d355592111b1641e08c297639 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 17 Nov 2026 22:23:30 -0500 Subject: [PATCH 2/3] docs: restore DOCKER.md summarization env var table --- coverage.txt | 4 ++-- docs/DOCKER.md | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/coverage.txt b/coverage.txt index c61dcbc9..c6e75c1a 100644 --- a/coverage.txt +++ b/coverage.txt @@ -186,7 +186,7 @@ ℹ xlsxParser.js | 82.21 | 83.33 | 88.89 | 47-48 93-112 130-137 150-156 ℹ zipExtractor.js | 82.32 | 77.78 | 66.67 | 62-63 104-107 124-142 149-152 ℹ git | | | | -ℹ index.js | 87.41 | 64.31 | 83.69 | 324-326 379-380 393-396 481-483 515-516 626-643 689-691 722-723 755-757 821-824 840-843 866-872 906 908-912 914-917 929-933 976-980 984 989-991 1006-1015 1024 1038-1044 1046-1049 1057-1063 1086-1091 1093-1099 1101-1110 1112-1115 1122-1126 1136-1143 1191-1192 1194-1199 1291-1292 1314-1315 1323-1324 1331-1332 1334-1340 1362 1366-1368 1373-1376 1428-1435 1439-1447 1450-1453 1455-1458 +ℹ index.js | 87.41 | 64.13 | 83.69 | 324-326 379-380 393-396 481-483 515-516 626-643 689-691 722-723 755-757 821-824 840-843 866-872 906 908-912 914-917 929-933 976-980 984 989-991 1006-1015 1024 1038-1044 1046-1049 1057-1063 1086-1091 1093-1099 1101-1110 1112-1115 1122-1126 1136-1143 1191-1192 1194-1199 1291-1292 1314-1315 1323-1324 1331-1332 1334-1340 1362 1366-1368 1373-1376 1428-1435 1439-1447 1450-1453 1455-1458 ℹ graphql | | | | ℹ index.js | 97.85 | 98.13 | 93.75 | 57-68 ℹ image | | | | @@ -285,6 +285,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.37 | 86.72 | 88.51 | +ℹ all files | 93.37 | 86.71 | 88.51 | ℹ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ℹ end of coverage report diff --git a/docs/DOCKER.md b/docs/DOCKER.md index 0234d2cb..4611fafc 100644 --- a/docs/DOCKER.md +++ b/docs/DOCKER.md @@ -256,7 +256,10 @@ The active provider is selected by the `enabled` flag, not by config position or ### Summarization -Context compaction is configured in `config.yaml` (`summarization.keepMessages`, `summarization.contextFraction`); see [CONFIGURATION.md](CONFIGURATION.md#summarization). +| Variable | Default | Description | +| -------------------------------- | ------- | ------------------------------------------------------------- | +| `SUMMARIZATION_CONTEXT_FRACTION` | `0.8` | Fraction of the context window at which the TUI auto-compacts | +| `SUMMARIZATION_KEEP_MESSAGES` | `5` | Recent messages kept verbatim after compaction | ### Vector Search From af8180e88784aeb49b8418345c8692a592b6ef8d Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 17 Nov 2026 22:28:52 -0500 Subject: [PATCH 3/3] fix: sync status bar color threshold with summarization.contextFraction The status bar spinner turned red at a hardcoded 80% while auto-compaction used config.summarization.contextFraction, so the two could drift apart. Pass contextFraction through App and InputArea to StatusBar so red starts exactly where auto-compaction fires. Take the fallback default from SummarizationSchema instead of duplicating 0.8 in the TUI. --- coverage.txt | 8 +++---- docs/CONFIGURATION.md | 2 +- src/tui/app.js | 1 + src/tui/conversationArea.js | 8 +++++-- src/tui/inputArea.js | 2 ++ src/tui/statusBar.js | 29 ++++++++++++++++++------- tests/unit/tui/conversationArea.test.js | 8 +++++-- tests/unit/tui/statusBar.test.js | 19 +++++++++++----- 8 files changed, 54 insertions(+), 23 deletions(-) diff --git a/coverage.txt b/coverage.txt index c6e75c1a..28c54fc2 100644 --- a/coverage.txt +++ b/coverage.txt @@ -242,7 +242,7 @@ ℹ yaml | | | | ℹ index.js | 100.00 | 94.38 | 100.00 | ℹ tui | | | | -ℹ 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 +ℹ app.js | 45.63 | 55.32 | 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 802-803 ℹ 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 @@ -250,7 +250,7 @@ ℹ conversationPanel.js | 95.45 | 95.83 | 100.00 | 26-31 ℹ filePicker.js | 69.72 | 89.66 | 73.33 | 143-168 194-195 200-244 267-279 ℹ headerClick.js | 99.02 | 96.55 | 100.00 | 99 -ℹ inputArea.js | 81.30 | 75.51 | 46.43 | 159-160 174 188-191 199-211 219-235 239-240 246-247 250-255 322-330 334-338 342-346 +ℹ inputArea.js | 81.41 | 75.51 | 46.43 | 160-161 175 189-192 200-212 220-236 240-241 247-248 251-256 324-332 336-340 344-348 ℹ inputPanel.js | 100.00 | 100.00 | 100.00 | ℹ linkHit.js | 100.00 | 91.67 | 100.00 | ℹ markdownText.js | 97.50 | 86.87 | 96.15 | 76 206-212 236-238 @@ -270,7 +270,7 @@ ℹ sessionsPanel.js | 83.19 | 41.03 | 90.91 | 44-47 80-81 97-98 102-104 111-115 140-142 151-152 167-173 176-182 193-196 ℹ settingsPanel.js | 72.02 | 80.00 | 73.33 | 55-69 95-121 158-168 174-178 184-186 ℹ skillsPanel.js | 86.67 | 55.56 | 66.67 | 45-47 53-59 -ℹ statusBar.js | 99.05 | 98.11 | 100.00 | 29-30 +ℹ statusBar.js | 99.11 | 98.15 | 100.00 | 34-35 ℹ subagentsPanel.js | 96.40 | 74.42 | 86.36 | 62-64 101-105 ℹ subagentStore.js | 100.00 | 100.00 | 100.00 | ℹ useMouseScroll.js | 100.00 | 100.00 | 100.00 | @@ -285,6 +285,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.37 | 86.71 | 88.51 | +ℹ all files | 93.38 | 86.70 | 88.51 | ℹ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ℹ end of coverage report diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index b8753675..b6350754 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -159,7 +159,7 @@ summarization: When compaction runs, madz summarizes the history and **rewrites the checkpointer** to `[summary, ...last keepMessages messages]`. The system prompt is injected on every call rather than stored in state, so it is unaffected. - **`keepMessages`** — how many recent messages stay verbatim (default `5`). The tail never starts on a tool result: if the boundary lands on one, the whole tool call and result are summarized and the tail shrinks (possibly to zero). -- **`contextFraction`** (default `0.8`) — fraction of the context window at which auto-compaction fires. The window is the model's real context length when it can be resolved at startup, otherwise the configured `contextWindow`. +- **`contextFraction`** (default `0.8`) — fraction of the context window at which auto-compaction fires. The status bar's context meter and streaming spinner turn red at the same point. The window is the model's real context length when it can be resolved at startup, otherwise the configured `contextWindow`. The summary is stored as a user message (some providers reject mid-conversation system messages). If summarization fails, the legacy trim-only fallback is used. diff --git a/src/tui/app.js b/src/tui/app.js index e0253348..697d2de3 100644 --- a/src/tui/app.js +++ b/src/tui/app.js @@ -792,6 +792,7 @@ function App({ appInfo, tokenBudget, contextWindow, + compactFraction: config?.summarization?.contextFraction, statusBar: config?.tui?.statusBar, cwd: activeProject || config?.cwd || process.cwd(), activeProject, diff --git a/src/tui/conversationArea.js b/src/tui/conversationArea.js index 9cdc7000..6164cff2 100644 --- a/src/tui/conversationArea.js +++ b/src/tui/conversationArea.js @@ -16,6 +16,7 @@ import { ConversationPanel } from "./conversationPanel.js"; import { CommandParser } from "./commandParser.js"; import { createSession } from "../session/factory.js"; import { setConfigValue } from "../config/loader.js"; +import { SummarizationSchema } from "../config/schemas/summarization.js"; import { isAvailable, getGcCalls } from "../memory/gc.js"; import { logger } from "../shared/logger.js"; import { formatClarifyPrompt } from "../shared/clarifyPrompt.js"; @@ -95,8 +96,11 @@ 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; +/** + * Default fraction of the context window at which the TUI compacts + * automatically. Taken from the config schema so there is one source for it. + */ +export const DEFAULT_AUTO_COMPACT_FRACTION = SummarizationSchema.parse({}).contextFraction; /** * Decide whether the context has reached the auto-compaction threshold. diff --git a/src/tui/inputArea.js b/src/tui/inputArea.js index e08382fb..e1d2d67c 100644 --- a/src/tui/inputArea.js +++ b/src/tui/inputArea.js @@ -65,6 +65,7 @@ const InputArea = forwardRef(function InputArea( appInfo, tokenBudget = 0, contextWindow = 0, + compactFraction, statusBar = {}, cwd, activeProject, @@ -281,6 +282,7 @@ const InputArea = forwardRef(function InputArea( tokenCount, tokenBudget, contextWindow, + compactFraction, statusBar, project: activeProject, }) diff --git a/src/tui/statusBar.js b/src/tui/statusBar.js index 47066fd7..82926fec 100644 --- a/src/tui/statusBar.js +++ b/src/tui/statusBar.js @@ -2,6 +2,11 @@ import React from "react"; import { Box, Text } from "ink"; import Spinner from "ink-spinner"; import { basename } from "node:path"; +import { SummarizationSchema } from "../config/schemas/summarization.js"; + +// Single source of the default: the config schema, so the status bar can never +// drift from the value auto-compaction uses. +const DEFAULT_COMPACT_FRACTION = SummarizationSchema.parse({}).contextFraction; // Resolve the user's locale once at module load. `Intl.NumberFormat` is the // correct formatter for numeric output; `Intl.DateTimeFormat` was used before @@ -49,18 +54,25 @@ export function formatSize(num) { /** * Determine the spinner color from context-window utilization. - * 0-60% → cyan, 61-80% → orange, 81-100% → red. Falls back to cyan when no - * context window is configured (contextWindow is 0/unset). + * 0-60% → cyan, 60% up to `compactFraction` → orange, `compactFraction` and + * above → red. `compactFraction` is `summarization.contextFraction` from + * config, so red marks exactly where auto-compaction fires. Falls back to cyan + * when no context window is configured (contextWindow is 0/unset). * @param {number} contextSize - Current context size in tokens * @param {number} contextWindow - Configured context window in tokens (0 = unset) + * @param {number} [compactFraction] - Fraction of the window at which auto-compaction fires * @returns {string} The color name for the streaming spinner */ -export function getContextUtilizationColor(contextSize, contextWindow) { +export function getContextUtilizationColor( + contextSize, + contextWindow, + compactFraction = DEFAULT_COMPACT_FRACTION, +) { if (!contextWindow || contextWindow <= 0) return "cyan"; - const utilization = (contextSize / contextWindow) * 100; - if (utilization <= 60) return "cyan"; - if (utilization <= 80) return "orange"; - return "red"; + const ratio = contextSize / contextWindow; + if (ratio >= compactFraction) return "red"; + if (ratio * 100 <= 60) return "cyan"; + return "orange"; } /** @@ -112,11 +124,12 @@ export const StatusBar = React.memo(function StatusBar({ tokenCount = 0, tokenBudget = 0, contextWindow = 0, + compactFraction, statusBar = {}, project = "", }) { const contextColor = isCompacting ? "red" : "gray"; - const spinnerColor = getContextUtilizationColor(contextSize, contextWindow); + const spinnerColor = getContextUtilizationColor(contextSize, contextWindow, compactFraction); const showModel = statusBar.model !== false && model; const showSkills = statusBar.skills !== false; const showMessages = statusBar.messages !== false; diff --git a/tests/unit/tui/conversationArea.test.js b/tests/unit/tui/conversationArea.test.js index f8e3da6c..119dc37b 100644 --- a/tests/unit/tui/conversationArea.test.js +++ b/tests/unit/tui/conversationArea.test.js @@ -5,6 +5,7 @@ import { describe, it } from "node:test"; import assert from "node:assert"; +import { SummarizationSchema } from "../../../src/config/schemas/summarization.js"; describe("ConversationArea module", () => { it("should have a default export", async () => { @@ -124,9 +125,12 @@ describe("shouldAutoCompact", () => { assert.strictEqual(mod.shouldAutoCompact({ contextSize: undefined, contextWindow: 1 }), false); }); - it("defaults to 80%", async () => { + it("defaults to the config schema's contextFraction", async () => { const mod = await import("../../../src/tui/conversationArea.js"); - assert.strictEqual(mod.DEFAULT_AUTO_COMPACT_FRACTION, 0.8); + assert.strictEqual( + mod.DEFAULT_AUTO_COMPACT_FRACTION, + SummarizationSchema.parse({}).contextFraction, + ); }); }); diff --git a/tests/unit/tui/statusBar.test.js b/tests/unit/tui/statusBar.test.js index 8db8e459..9685cf00 100644 --- a/tests/unit/tui/statusBar.test.js +++ b/tests/unit/tui/statusBar.test.js @@ -69,21 +69,28 @@ describe("getContextUtilizationColor", () => { assert.strictEqual(getContextUtilizationColor(76800, 128000), "cyan"); }); - it("returns orange for 61-80% utilization", () => { + it("returns orange from just above 60% until the compaction fraction", () => { // Boundary: just above 60% flips to orange assert.strictEqual(getContextUtilizationColor(76801, 128000), "orange"); assert.strictEqual(getContextUtilizationColor(100000, 128000), "orange"); - // Boundary: exactly 80% is still orange - assert.strictEqual(getContextUtilizationColor(102400, 128000), "orange"); + // Boundary: just below the default compaction fraction (0.8) is still orange + assert.strictEqual(getContextUtilizationColor(102399, 128000), "orange"); }); - it("returns red for 81-100% utilization", () => { - // Boundary: just above 80% flips to red - assert.strictEqual(getContextUtilizationColor(102401, 128000), "red"); + it("returns red from the compaction fraction upward", () => { + // Boundary: exactly the default fraction (0.8) is red, where auto-compact fires + assert.strictEqual(getContextUtilizationColor(102400, 128000), "red"); assert.strictEqual(getContextUtilizationColor(128000, 128000), "red"); // Over 100% is still red assert.strictEqual(getContextUtilizationColor(200000, 128000), "red"); }); + + it("follows the configured compaction fraction instead of a fixed 80%", () => { + // 1 is the schema maximum for contextFraction, so red starts only at 100%. + // At 80% the default would be red; with 1 it must be orange. + assert.strictEqual(getContextUtilizationColor(102400, 128000, 1), "orange"); + assert.strictEqual(getContextUtilizationColor(128000, 128000, 1), "red"); + }); }); describe("getContextUtilization", () => {