Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(agent): treat model_context_window_exceeded as a length stop in compaction (#4681)
ydflow7298da67a00d
diff --git a/internal/agent/compaction/compactor.go b/internal/agent/compaction/compactor.goindex bbfe48a2d..a96116472 100644--- a/internal/agent/compaction/compactor.go+++ b/internal/agent/compaction/compactor.go@@ -328,7 +328,10 @@ func validateSummary(content, finishReason string) error {switch strings.ToLower(strings.TrimSpace(finishReason)) {case types.FinishReasonIncomplete:return errors.New(types.StreamEndedEarlyError)- case "length", "max_tokens", "max_output_tokens":+ // model_context_window_exceeded is z.ai's length stop; #4589 normalized it+ // for the engine and the Anthropic adapter, and a truncated summary is+ // exactly as unusable here.+ case "length", "max_tokens", "max_output_tokens", "model_context_window_exceeded":return errors.New("generation hit the token cap and the summary is incomplete")}return nildiff --git a/internal/agent/compaction/length_finish_reason_test.go b/internal/agent/compaction/length_finish_reason_test.gonew file mode 100644index 000000000..1b3ee976f--- /dev/null+++ b/internal/agent/compaction/length_finish_reason_test.go@@ -0,0 +1,56 @@+package compaction++import (+ "testing"++ "github.com/Tencent/WeKnora/internal/types"+ "github.com/stretchr/testify/assert"+)++// z.ai ends a generation with `model_context_window_exceeded` when the input+// exhausts the window. #4589 normalized that finish reason to a length stop in+// the agent engine and the Anthropic adapter; the compaction path must agree,+// or a summary truncated mid-section is accepted as a checkpoint and every+// later round inherits the truncation as its only memory.+func TestValidateSummaryRejectsContextWindowExceededFinish(t *testing.T) {+ assert.Error(t, validateSummary("## Goal\nhalf a summary that stops mid",+ "model_context_window_exceeded"))+ // Case-insensitive: finish reasons arrive verbatim from providers.+ assert.Error(t, validateSummary("## Goal\nhalf a summary that stops mid",+ "Model_Context_Window_Exceeded"))+ // A natural stop with the same content stays valid.+ assert.NoError(t, validateSummary("## Goal\ncomplete summary", "stop"))+}++func TestIsLengthStopRecognizesContextWindowExceeded(t *testing.T) {+ assert.True(t, isLengthStop("model_context_window_exceeded"))+ assert.True(t, isLengthStop(" Model_Context_Window_Exceeded\n"))+ // The pre-existing length reasons keep working; ordinary stops do not+ // become length stops.+ assert.True(t, isLengthStop("length"))+ assert.False(t, isLengthStop("stop"))+}++// The general recoverable shape: the provider stopped on the context window+// having emitted less than the budget we asked for, so our own cap cannot be+// what stopped it. This is what lets the engine compact and retry once.+func TestResponseHitContextLimitContextWindowExceededFinish(t *testing.T) {+ const window, budget = 100000, 8000++ assert.True(t, ResponseHitContextLimit(&types.ChatResponse{+ FinishReason: "model_context_window_exceeded",+ Usage: types.TokenUsage{PromptTokens: 90000, CompletionTokens: 2000},+ }, window, budget))++ // Truncated to no output with a prompt that fills the window (shape 2).+ assert.True(t, ResponseHitContextLimit(&types.ChatResponse{+ FinishReason: "model_context_window_exceeded",+ Usage: types.TokenUsage{PromptTokens: window, CompletionTokens: 0},+ }, window, budget))++ // An ordinary stop with room left in the budget is not a context hit.+ assert.False(t, ResponseHitContextLimit(&types.ChatResponse{+ FinishReason: "stop",+ Usage: types.TokenUsage{PromptTokens: 90000, CompletionTokens: 2000},+ }, window, budget))+}diff --git a/internal/agent/compaction/overflow.go b/internal/agent/compaction/overflow.goindex bd666a046..ce41de1bb 100644--- a/internal/agent/compaction/overflow.go+++ b/internal/agent/compaction/overflow.go@@ -108,7 +108,9 @@ func ResponseHitContextLimit(func isLengthStop(reason string) bool {switch strings.ToLower(strings.TrimSpace(reason)) {- case "length", "max_tokens", "max_output_tokens":+ // model_context_window_exceeded is z.ai's length stop, matching+ // isLengthFinishReason in the agent engine (#4589).+ case "length", "max_tokens", "max_output_tokens", "model_context_window_exceeded":return truedefault:return false