fix: summarize context window preemptively before dispatch
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- javascript, nodejs
Hướng nghiên cứu
Start with the trigger derivation in src/agent/deepAgents.js around lines 458-472, then read createSummarizationMiddlewareFromConfig in src/provider/summarizationMiddleware.js and estimateCost in src/provider/tokenBudgetMiddleware.js (lines 69-80) to see how input and output budgets are counted. Done means a test in tests/unit/provider/summarizationMiddleware.test.js shows a request whose input plus maxTokens exceeds the window being compacted before dispatch. Expect to work through the deepagents library's trigger logic, which needs more context than the issue gives.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
The summarization middleware does not compact the context window preemptively. In long-running sessions it fails to compress before the model's context window is exceeded, causing repeated 400 context-window errors.
Environment
- OS: Linux 7.0.14-19-pve
- Node.js: v25.8.1
- madz version: 1.113.1
- LLM provider: OpenAI gpt-4o
Reproduction
Steps to reproduce the behavior:
- Start a long-running madz session with a large system prompt and many registered tools.
- Continue the conversation until the input approaches the model's context window.
- Observe the model return 400 context-window errors instead of the middleware compacting preemptively.
Expected Behavior
The summarization middleware should estimate the current context window usage (input + expected output) before dispatching, and compact the conversation when the total would exceed the model's context window — the same way the token-budget middleware estimates cost before sending.
Actual Behavior
The middleware only fires when the input token count (estimated via countTokensApproximately, chars/4) exceeds a fixed trigger (80% of context length). It does not account for expected output tokens, so a long response can push input + output over the window, causing 400 context-window errors that are handled reactively (compact + re-send) rather than preemptively.
Additional Context
The trigger is derived at init as Math.floor(contextLength * 0.8). For gpt-4o (128000), that is 102400. The trigger uses countTokensApproximately (chars/4), which differs from the real tokenizer used by the token-budget middleware (model.getNumTokensFromMessages). The token-budget middleware already estimates cost as input + output budget and reserves against a rolling window — the summarization middleware should apply the same preemptive estimate against the context window.
Audit Findings (for Issue #1399)
- src/agent/deepAgents.js:458 — The summarization trigger is derived at init as
Math.floor(contextLength * 0.8). For gpt-4o (128000) that is 102400. This is a fixed fraction of the context window with no output-token allowance. - src/agent/deepAgents.js:463 — The derived trigger is written to
config.summarization.triggeras{ type: "tokens", value: triggerTokens }, then passed tocreateSummarizationMiddlewareFromConfig. - src/agent/deepAgents.js:472 —
createSummarizationMiddlewareFromConfigbuilds the deepagentsSummarizationMiddleware, which fires only when the input token count exceeds the trigger. It does not account for expected output tokens. - src/provider/tokenBudgetMiddleware.js:69-80 — The token-budget middleware already estimates cost preemptively:
inputTokens(viamodel.getNumTokensFromMessages) +toolTokens+outputBudget(fromoptions.maxTokens). This is the pattern the summarization middleware should mirror. - src/provider/summarizationMiddleware.js:44-60 —
createSummarizationMiddlewareFromConfigpasses onlytriggerandkeeptocreateSummarizationMiddleware. The deepagents library computes the trigger fromcountTokensApproximately(chars/4), which diverges from the real tokenizer used by the token-budget middleware. - node_modules/deepagents/dist/langsmith--RbtjNdQ.js:3602 —
shouldSummarize()comparesadjustedTokensagainst the trigger.adjustedTokenscomes fromcountTotalTokens→countTokensApproximately, which is a character-based approximation, not the model tokenizer.
Fix Steps
- Add a preemptive context-window check — In
src/provider/summarizationMiddleware.js, before dispatching, estimate the total cost (input + expected output) using the model tokenizer, mirroringestimateCost()insrc/provider/tokenBudgetMiddleware.js:69. - Account for expected output — Add the configured
maxTokens(output budget) to the estimate so a long response is accounted for, not just the input. - Compact when total exceeds the window — When
input + output > contextWindow, trigger summarization (or the existing compaction path) before the request is sent, rather than waiting for a 400. - Use the real tokenizer — Prefer
model.getNumTokensFromMessagesovercountTokensApproximatelyfor the trigger estimate, so the threshold matches the model's actual tokenization. - Add a test — Add
tests/unit/provider/summarizationMiddleware.test.jscoverage for the preemptive path: a request whose input + output would exceed the window compacts before dispatch. - Verify — Run
npm run test,npm run lint, andnpm run coverageto confirm no regressions.
- Ngôn ngữ chính
- JavaScript
- Star
- 2
- Fork
- 0
- Merge trung bình
- 1 giờ 6 phút
- Pull request đã merge (30 ngày)
- 234
Chuẩn bị môi trường
- Có Dockerfile hoặc tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của avoidwork/madz
-
feature
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 22/100
avoidwork/madz#1358 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
feature
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 15/100
avoidwork/madz#1355 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
feature
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của avoidwork/madz
Issue tương tự
-
feature request
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
docs hacktoberfest help wanted
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
pacocartones/free-llm-api-hub#347 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
cytoscape/cytoscape.js#3536 ·
Maintainer thường phản hồi trong vòng 15 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
cuttle-cards/cuttle#1440 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
URL-encoded parser builds nested objects nothing readsCó thể đã có người làm @Senthil455 đã nhận hôm nay. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
Janani-bn/CivicFix#168 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày