From 0d97169d07c28a12dfb739203a98e311f69af887 Mon Sep 17 00:00:00 2001 From: Sycun <165354365+Sycun@users.noreply.github.com> Date: Wed, 21 Oct 2026 00:16:30 +0800 Subject: [PATCH] fix(ai): report the configured budget in context clamp warnings The warning was computed after the runtime config had already been clamped, so configured and effective values were identical and the clamp warning was lost. --- internal/multiagent/context_budget.go | 20 +++++--- internal/multiagent/context_budget_test.go | 59 ++++++++++++++++++++++ internal/multiagent/eino_single_runner.go | 3 +- internal/multiagent/runner.go | 3 +- 4 files changed, 77 insertions(+), 8 deletions(-) diff --git a/internal/multiagent/context_budget.go b/internal/multiagent/context_budget.go index 96088a7e..37037002 100644 --- a/internal/multiagent/context_budget.go +++ b/internal/multiagent/context_budget.go @@ -17,23 +17,31 @@ import ( var contextBudgetWarned sync.Map -func logContextBudgetWarnings(oa config.OpenAIConfig, logger *zap.Logger, phase string) { +// logContextBudgetWarnings reports the clamp applied by WithEffectiveContextBudget. +// configured must be the pre-clamp channel config so the warning set and the +// configured value stay meaningful. +func logContextBudgetWarnings(configured, effective config.OpenAIConfig, logger *zap.Logger, phase string) { if logger == nil { return } - warnings := oa.ContextBudgetWarnings() + warnings := configured.ContextBudgetWarnings() if len(warnings) == 0 { return } - key := fmt.Sprintf("%d|%d|%s", oa.EffectiveContextWindow(), oa.MaxTotalTokens, strings.Join(warnings, ",")) + key := fmt.Sprintf("%d|%d|%d|%s", + configured.EffectiveContextWindow(), + configured.MaxTotalTokens, + effective.MaxTotalTokens, + strings.Join(warnings, ","), + ) if _, loaded := contextBudgetWarned.LoadOrStore(key, struct{}{}); loaded { return } logger.Warn("model context budget adjusted", zap.String("phase", phase), - zap.Int("context_window", oa.EffectiveContextWindow()), - zap.Int("configured_max_total_tokens", oa.MaxTotalTokens), - zap.Int("effective_max_total_tokens", oa.EffectiveMaxTotalTokens()), + zap.Int("context_window", configured.EffectiveContextWindow()), + zap.Int("configured_max_total_tokens", configured.MaxTotalTokens), + zap.Int("effective_max_total_tokens", effective.MaxTotalTokens), zap.Strings("warnings", warnings), ) } diff --git a/internal/multiagent/context_budget_test.go b/internal/multiagent/context_budget_test.go index 7e657592..c285e398 100644 --- a/internal/multiagent/context_budget_test.go +++ b/internal/multiagent/context_budget_test.go @@ -11,6 +11,9 @@ import ( "github.com/cloudwego/eino/adk" "github.com/cloudwego/eino/adk/middlewares/summarization" "github.com/cloudwego/eino/schema" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "go.uber.org/zap/zaptest/observer" ) func TestIsEinoContextOverflowError(t *testing.T) { @@ -89,6 +92,62 @@ func TestBuildBudgetedSummarizationModelInputTruncatesOversizedLatestRound(t *te } } +func TestLogContextBudgetWarningsKeepsConfiguredValue(t *testing.T) { + t.Parallel() + core, logs := observer.New(zapcore.WarnLevel) + logger := zap.New(core) + configured := config.OpenAIConfig{ + ContextWindow: 131073, + ContextProfile: "local", + MaxTotalTokens: 120000, + } + effective := configured + effective.MaxTotalTokens = configured.EffectiveMaxTotalTokens() + logContextBudgetWarnings(configured, effective, logger, "test") + + entries := logs.All() + if len(entries) != 1 { + t.Fatalf("warn entries = %d, want 1", len(entries)) + } + fields := entries[0].ContextMap() + if got, ok := fields["configured_max_total_tokens"].(int64); !ok || got != 120000 { + t.Fatalf("configured_max_total_tokens = %#v, want 120000", fields["configured_max_total_tokens"]) + } + if got, ok := fields["effective_max_total_tokens"].(int64); !ok || got != 78643 { + t.Fatalf("effective_max_total_tokens = %#v, want 78643", fields["effective_max_total_tokens"]) + } + warnings := warningCodes(fields["warnings"]) + if !hasWarningCode(warnings, config.ContextWarningMaxTotalClamped) { + t.Fatalf("warnings = %#v, want clamp warning", fields["warnings"]) + } +} + +func warningCodes(value interface{}) []string { + switch v := value.(type) { + case []string: + return v + case []interface{}: + out := make([]string, 0, len(v)) + for _, item := range v { + if code, ok := item.(string); ok { + out = append(out, code) + } + } + return out + default: + return nil + } +} + +func hasWarningCode(items []string, want string) bool { + for _, item := range items { + if item == want { + return true + } + } + return false +} + func TestLocalTokenCounterSafetyMargin(t *testing.T) { t.Parallel() input := &summarization.TokenCounterInput{Messages: []adk.Message{ diff --git a/internal/multiagent/eino_single_runner.go b/internal/multiagent/eino_single_runner.go index d3f545ba..58d34c2b 100644 --- a/internal/multiagent/eino_single_runner.go +++ b/internal/multiagent/eino_single_runner.go @@ -45,13 +45,14 @@ func RunEinoSingleChatModelAgent( if ma == nil { return nil, fmt.Errorf("eino single: multi_agent 配置为空") } + configuredOpenAI := appCfg.OpenAI appCfg = appCfg.WithEffectiveContextBudget() { maCopy := *ma maCopy.EinoMiddleware = appCfg.MultiAgent.EinoMiddleware ma = &maCopy } - logContextBudgetWarnings(appCfg.OpenAI, logger, "eino_single") + logContextBudgetWarnings(configuredOpenAI, appCfg.OpenAI, logger, "eino_single") runtimeUserMessage := prepareLatestUserMessageForModel(userMessage, appCfg, &ma.EinoMiddleware, conversationID, logger) diff --git a/internal/multiagent/runner.go b/internal/multiagent/runner.go index 47924da2..d8406ee4 100644 --- a/internal/multiagent/runner.go +++ b/internal/multiagent/runner.go @@ -84,13 +84,14 @@ func RunDeepAgent( return nil, fmt.Errorf("multiagent: 配置或 Agent 为空") } + configuredOpenAI := appCfg.OpenAI appCfg = appCfg.WithEffectiveContextBudget() if ma != nil { maCopy := *ma maCopy.EinoMiddleware = appCfg.MultiAgent.EinoMiddleware ma = &maCopy } - logContextBudgetWarnings(appCfg.OpenAI, logger, "deep") + logContextBudgetWarnings(configuredOpenAI, appCfg.OpenAI, logger, "deep") runtimeUserMessage := prepareLatestUserMessageForModel(userMessage, appCfg, &ma.EinoMiddleware, conversationID, logger)