diff --git a/internal/multiagent/eino_single_runner.go b/internal/multiagent/eino_single_runner.go index 50dc9acc..2bade093 100644 --- a/internal/multiagent/eino_single_runner.go +++ b/internal/multiagent/eino_single_runner.go @@ -160,13 +160,7 @@ func RunEinoSingleChatModelAgent( handlers = append(handlers, capMw) } - maxIter := ma.MaxIteration - if maxIter <= 0 { - maxIter = appCfg.Agent.MaxIterations - } - if maxIter <= 0 { - maxIter = 40 - } + maxIter := agentMaxIterations(appCfg) mainToolsCfg := adk.ToolsConfig{ ToolsNodeConfig: compose.ToolsNodeConfig{ diff --git a/internal/multiagent/max_iterations.go b/internal/multiagent/max_iterations.go new file mode 100644 index 00000000..2645d9f8 --- /dev/null +++ b/internal/multiagent/max_iterations.go @@ -0,0 +1,22 @@ +package multiagent + +import "cyberstrike-ai/internal/config" + +const defaultAgentMaxIterations = 3000 + +// agentMaxIterations 全局上限:仅使用 config.agent.max_iterations;≤0 时与 config 默认一致为 3000。 +func agentMaxIterations(appCfg *config.Config) int { + if appCfg != nil && appCfg.Agent.MaxIterations > 0 { + return appCfg.Agent.MaxIterations + } + return defaultAgentMaxIterations +} + +// resolveMaxIterations 统一迭代上限:Markdown/子代理 front matter 中 max_iterations>0 可单独覆盖,否则使用 agent.max_iterations。 +// multi_agent.max_iteration 与 sub_agent_max_iterations 已废弃,不再参与计算。 +func resolveMaxIterations(appCfg *config.Config, markdownOverride int) int { + if markdownOverride > 0 { + return markdownOverride + } + return agentMaxIterations(appCfg) +} diff --git a/internal/multiagent/max_iterations_test.go b/internal/multiagent/max_iterations_test.go new file mode 100644 index 00000000..9bab7328 --- /dev/null +++ b/internal/multiagent/max_iterations_test.go @@ -0,0 +1,31 @@ +package multiagent + +import ( + "testing" + + "cyberstrike-ai/internal/config" +) + +func TestAgentMaxIterations(t *testing.T) { + if got := agentMaxIterations(nil); got != defaultAgentMaxIterations { + t.Fatalf("nil cfg: got %d want %d", got, defaultAgentMaxIterations) + } + cfg := &config.Config{Agent: config.AgentConfig{MaxIterations: 12000}} + if got := agentMaxIterations(cfg); got != 12000 { + t.Fatalf("got %d want 12000", got) + } + cfg.Agent.MaxIterations = 0 + if got := agentMaxIterations(cfg); got != defaultAgentMaxIterations { + t.Fatalf("zero: got %d want %d", got, defaultAgentMaxIterations) + } +} + +func TestResolveMaxIterations(t *testing.T) { + cfg := &config.Config{Agent: config.AgentConfig{MaxIterations: 12000}} + if got := resolveMaxIterations(cfg, 0); got != 12000 { + t.Fatalf("global: got %d want 12000", got) + } + if got := resolveMaxIterations(cfg, 50); got != 50 { + t.Fatalf("override: got %d want 50", got) + } +} diff --git a/internal/multiagent/runner.go b/internal/multiagent/runner.go index 0d126001..d2f12db6 100644 --- a/internal/multiagent/runner.go +++ b/internal/multiagent/runner.go @@ -170,18 +170,7 @@ func RunDeepAgent( } reasoning.ApplyToEinoChatModelConfig(baseModelCfg, &appCfg.OpenAI, reasoningClient) - deepMaxIter := ma.MaxIteration - if deepMaxIter <= 0 { - deepMaxIter = appCfg.Agent.MaxIterations - } - if deepMaxIter <= 0 { - deepMaxIter = 40 - } - - subDefaultIter := ma.SubAgentMaxIterations - if subDefaultIter <= 0 { - subDefaultIter = 20 - } + deepMaxIter := agentMaxIterations(appCfg) var subAgents []adk.Agent if orchMode != "plan_execute" { @@ -230,10 +219,7 @@ func RunDeepAgent( return nil, fmt.Errorf("子代理 %q eino 中间件: %w", id, err) } - subMax := sub.MaxIterations - if subMax <= 0 { - subMax = subDefaultIter - } + subMax := resolveMaxIterations(appCfg, sub.MaxIterations) subSumMw, err := newEinoSummarizationMiddleware(ctx, subModel, appCfg, &ma.EinoMiddleware, conversationID, logger) if err != nil {