Fully autonomous AI Agents system capable of performing complex penetration testing tasks
Go ai-agentsai-security-toolanthropicautonomous-agentsgolanggptgraphqlmulti-agent-systemoffensive-securityopen-sourceopenaipenetration-testingpenetration-testing-toolsreactsecurity-automationsecurity-testingsecurity-toolsself-hosted
refactor(pconfig): centralize adaptive-thinking helpers, dedup bedrock/anthropic (refs #288)
Sergey Kozyrenko7e61bbea6145
diff --git a/backend/pkg/providers/anthropic/adaptive_thinking.go b/backend/pkg/providers/anthropic/adaptive_thinking.goindex e41f0698..b3887f60 100644--- a/backend/pkg/providers/anthropic/adaptive_thinking.go+++ b/backend/pkg/providers/anthropic/adaptive_thinking.go@@ -2,7 +2,6 @@ package anthropicimport ("bytes"- "context""encoding/json""fmt""io"@@ -10,35 +9,18 @@ import ("strconv""pentagi/pkg/providers/pconfig"-- "github.com/vxcontrol/langchaingo/llms")-type adaptiveThinkingEffortContextKey struct{}--func withAdaptiveThinkingEffort(ctx context.Context, effort string) context.Context {- if effort == "" {- effort = string(llms.ReasoningHigh)- }-- return context.WithValue(ctx, adaptiveThinkingEffortContextKey{}, effort)-}--func adaptiveThinkingEffortFromContext(ctx context.Context) (string, bool) {- effort, ok := ctx.Value(adaptiveThinkingEffortContextKey{}).(string)- return effort, ok && effort != ""-}-// adaptiveThinkingTransport rewrites the outgoing Anthropic Messages request body// to use adaptive thinking when the call context carries an effort (set by-// prepareCallOptions). langchaingo only emits budget thinking, but Claude Opus-// 4.7/4.8 only support thinking.type=adaptive and reject sampling params.+// pconfig.PrepareAdaptiveCallOptions). langchaingo only emits budget thinking, but+// Claude Opus 4.7/4.8 only support thinking.type=adaptive and reject sampling params.type adaptiveThinkingTransport struct {base http.RoundTripper}func (t *adaptiveThinkingTransport) RoundTrip(req *http.Request) (*http.Response, error) {- effort, ok := adaptiveThinkingEffortFromContext(req.Context())+ effort, ok := pconfig.AdaptiveEffortFromContext(req.Context())if !ok || req.Body == nil {return t.base.RoundTrip(req)}@@ -87,108 +69,10 @@ func rewriteAdaptiveThinkingBody(body []byte, effort string) ([]byte, error) {delete(thinking, "budget_tokens")payload["output_config"] = map[string]any{"effort": effort}- // Adaptive Claude models reject sampling params (Opus 4.7+ return 400 for any- // temperature/top_p/top_k).+ // Adaptive Claude models reject sampling params (Opus 4.7+ return 400). langchaingo's+ // Messages payload only serializes temperature/top_p, so those are the only keys to strip.delete(payload, "temperature")delete(payload, "top_p")- delete(payload, "top_k")return json.Marshal(payload)}--func (p *anthropicProvider) prepareCallOptions(- ctx context.Context,- opt pconfig.ProviderOptionsType,- options []llms.CallOption,-) (context.Context, []llms.CallOption) {- if !p.usesAdaptiveThinking(opt) {- return ctx, options- }-- reasoning, _ := p.reasoningConfigForType(opt)- ctx = withAdaptiveThinkingEffort(ctx, string(reasoning.Effort))- options = append(options, llms.WithReasoning(llms.ReasoningHigh, 0))-- return ctx, options-}--// usesAdaptiveThinking reports whether this agent's call must use adaptive thinking:-// either the agent config selected it, or the agent's model only supports adaptive-// (e.g. Opus 4.7/4.8, where budget thinking returns a 400).-func (p *anthropicProvider) usesAdaptiveThinking(opt pconfig.ProviderOptionsType) bool {- if p.modelReasoningMode(opt) == pconfig.ModelReasoningAdaptiveOnly {- return true- }-- reasoning, ok := p.reasoningConfigForType(opt)- return ok && reasoning.EffectiveMode() == pconfig.ReasoningModeAdaptive-}--// modelReasoningMode returns the reasoning capability declared in models.yml for-// the model assigned to this agent, or ModelReasoningNone if unknown.-func (p *anthropicProvider) modelReasoningMode(opt pconfig.ProviderOptionsType) pconfig.ModelReasoningMode {- agentConfig := p.agentConfigForType(opt)- if agentConfig == nil || agentConfig.Model == "" {- return pconfig.ModelReasoningNone- }-- for _, m := range p.models {- if m.Name == agentConfig.Model && m.Reasoning != nil {- return m.Reasoning.Mode- }- }-- return pconfig.ModelReasoningNone-}--func (p *anthropicProvider) reasoningConfigForType(opt pconfig.ProviderOptionsType) (pconfig.ReasoningConfig, bool) {- agentConfig := p.agentConfigForType(opt)- if agentConfig == nil || agentConfig.Reasoning.IsZero() {- return pconfig.ReasoningConfig{}, false- }-- return agentConfig.Reasoning, true-}--func (p *anthropicProvider) agentConfigForType(opt pconfig.ProviderOptionsType) *pconfig.AgentConfig {- if p == nil || p.providerConfig == nil {- return nil- }-- switch opt {- case pconfig.OptionsTypeSimple:- return p.providerConfig.Simple- case pconfig.OptionsTypeSimpleJSON:- if p.providerConfig.SimpleJSON != nil {- return p.providerConfig.SimpleJSON- }- return p.providerConfig.Simple- case pconfig.OptionsTypePrimaryAgent:- return p.providerConfig.PrimaryAgent- case pconfig.OptionsTypeAssistant:- if p.providerConfig.Assistant != nil {- return p.providerConfig.Assistant- }- return p.providerConfig.PrimaryAgent- case pconfig.OptionsTypeGenerator:- return p.providerConfig.Generator- case pconfig.OptionsTypeRefiner:- return p.providerConfig.Refiner- case pconfig.OptionsTypeAdviser:- return p.providerConfig.Adviser- case pconfig.OptionsTypeReflector:- return p.providerConfig.Reflector- case pconfig.OptionsTypeSearcher:- return p.providerConfig.Searcher- case pconfig.OptionsTypeEnricher:- return p.providerConfig.Enricher- case pconfig.OptionsTypeCoder:- return p.providerConfig.Coder- case pconfig.OptionsTypeInstaller:- return p.providerConfig.Installer- case pconfig.OptionsTypePentester:- return p.providerConfig.Pentester- default:- return nil- }-}diff --git a/backend/pkg/providers/anthropic/anthropic.go b/backend/pkg/providers/anthropic/anthropic.goindex 00d72beb..87ae9fc1 100644--- a/backend/pkg/providers/anthropic/anthropic.go+++ b/backend/pkg/providers/anthropic/anthropic.go@@ -155,7 +155,9 @@ func (p *anthropicProvider) Call(opt pconfig.ProviderOptionsType,prompt string,) (string, error) {- ctx, options := p.prepareCallOptions(ctx, opt, p.providerConfig.GetOptionsForType(opt))+ ctx, options := p.providerConfig.PrepareAdaptiveCallOptions(+ ctx, p.models, opt, p.providerConfig.GetOptionsForType(opt),+ )return provider.WrapGenerateFromSinglePrompt(ctx, p, opt, p.llm, prompt,options...,@@ -168,7 +170,7 @@ func (p *anthropicProvider) CallEx(chain []llms.MessageContent,streamCb streaming.Callback,) (*llms.ContentResponse, error) {- ctx, options := p.prepareCallOptions(ctx, opt, append([]llms.CallOption{+ ctx, options := p.providerConfig.PrepareAdaptiveCallOptions(ctx, p.models, opt, append([]llms.CallOption{llms.WithStreamingFunc(streamCb),}, p.providerConfig.GetOptionsForType(opt)...))return provider.WrapGenerateContent(@@ -184,7 +186,7 @@ func (p *anthropicProvider) CallWithTools(tools []llms.Tool,streamCb streaming.Callback,) (*llms.ContentResponse, error) {- ctx, options := p.prepareCallOptions(ctx, opt, append([]llms.CallOption{+ ctx, options := p.providerConfig.PrepareAdaptiveCallOptions(ctx, p.models, opt, append([]llms.CallOption{llms.WithTools(tools),llms.WithStreamingFunc(streamCb),}, p.providerConfig.GetOptionsForType(opt)...))diff --git a/backend/pkg/providers/bedrock/adaptive_thinking.go b/backend/pkg/providers/bedrock/adaptive_thinking.goindex 390c4878..cb28b21c 100644--- a/backend/pkg/providers/bedrock/adaptive_thinking.go+++ b/backend/pkg/providers/bedrock/adaptive_thinking.go@@ -12,24 +12,8 @@ import ("github.com/aws/smithy-go/middleware"smithyhttp "github.com/aws/smithy-go/transport/http"- "github.com/vxcontrol/langchaingo/llms")-type adaptiveThinkingEffortContextKey struct{}--func withAdaptiveThinkingEffort(ctx context.Context, effort string) context.Context {- if effort == "" {- effort = string(llms.ReasoningHigh)- }-- return context.WithValue(ctx, adaptiveThinkingEffortContextKey{}, effort)-}--func adaptiveThinkingEffortFromContext(ctx context.Context) (string, bool) {- effort, ok := ctx.Value(adaptiveThinkingEffortContextKey{}).(string)- return effort, ok && effort != ""-}-func addAdaptiveThinkingMiddleware(stack *middleware.Stack) error {return stack.Build.Add(middleware.BuildMiddlewareFunc("PentAGIBedrockAdaptiveThinking",@@ -38,7 +22,7 @@ func addAdaptiveThinkingMiddleware(stack *middleware.Stack) error {middleware.Metadata,error,) {- effort, ok := adaptiveThinkingEffortFromContext(ctx)+ effort, ok := pconfig.AdaptiveEffortFromContext(ctx)if !ok {return next.HandleBuild(ctx, in)}@@ -112,100 +96,3 @@ func rewriteAdaptiveThinkingBody(body []byte, effort string) ([]byte, error) {return updatedBody, nil}--func (p *bedrockProvider) prepareCallOptions(- ctx context.Context,- opt pconfig.ProviderOptionsType,- options []llms.CallOption,-) (context.Context, []llms.CallOption) {- if !p.usesAdaptiveThinking(opt) {- return ctx, options- }-- reasoning, _ := p.reasoningConfigForType(opt)- ctx = withAdaptiveThinkingEffort(ctx, string(reasoning.Effort))- options = append(options, llms.WithReasoning(llms.ReasoningHigh, 0))-- return ctx, options-}--// usesAdaptiveThinking reports whether this agent's call must use adaptive thinking:-// either the agent config selected it, or the agent's model only supports adaptive-// (e.g. Opus 4.7/4.8, where budget thinking returns a 400).-func (p *bedrockProvider) usesAdaptiveThinking(opt pconfig.ProviderOptionsType) bool {- if p.modelReasoningMode(opt) == pconfig.ModelReasoningAdaptiveOnly {- return true- }-- reasoning, ok := p.reasoningConfigForType(opt)- return ok && reasoning.EffectiveMode() == pconfig.ReasoningModeAdaptive-}--// modelReasoningMode returns the reasoning capability declared in models.yml for-// the model assigned to this agent, or ModelReasoningNone if unknown.-func (p *bedrockProvider) modelReasoningMode(opt pconfig.ProviderOptionsType) pconfig.ModelReasoningMode {- agentConfig := p.agentConfigForType(opt)- if agentConfig == nil || agentConfig.Model == "" {- return pconfig.ModelReasoningNone- }-- for _, m := range p.models {- if m.Name == agentConfig.Model && m.Reasoning != nil {- return m.Reasoning.Mode- }- }-- return pconfig.ModelReasoningNone-}--func (p *bedrockProvider) reasoningConfigForType(opt pconfig.ProviderOptionsType) (pconfig.ReasoningConfig, bool) {- agentConfig := p.agentConfigForType(opt)- if agentConfig == nil || agentConfig.Reasoning.IsZero() {- return pconfig.ReasoningConfig{}, false- }-- return agentConfig.Reasoning, true-}--func (p *bedrockProvider) agentConfigForType(opt pconfig.ProviderOptionsType) *pconfig.AgentConfig {- if p == nil || p.providerConfig == nil {- return nil- }-- switch opt {- case pconfig.OptionsTypeSimple:- return p.providerConfig.Simple- case pconfig.OptionsTypeSimpleJSON:- if p.providerConfig.SimpleJSON != nil {- return p.providerConfig.SimpleJSON- }- return p.providerConfig.Simple- case pconfig.OptionsTypePrimaryAgent:- return p.providerConfig.PrimaryAgent- case pconfig.OptionsTypeAssistant:- if p.providerConfig.Assistant != nil {- return p.providerConfig.Assistant- }- return p.providerConfig.PrimaryAgent- case pconfig.OptionsTypeGenerator:- return p.providerConfig.Generator- case pconfig.OptionsTypeRefiner:- return p.providerConfig.Refiner- case pconfig.OptionsTypeAdviser:- return p.providerConfig.Adviser- case pconfig.OptionsTypeReflector:- return p.providerConfig.Reflector- case pconfig.OptionsTypeSearcher:- return p.providerConfig.Searcher- case pconfig.OptionsTypeEnricher:- return p.providerConfig.Enricher- case pconfig.OptionsTypeCoder:- return p.providerConfig.Coder- case pconfig.OptionsTypeInstaller:- return p.providerConfig.Installer- case pconfig.OptionsTypePentester:- return p.providerConfig.Pentester- default:- return nil- }-}diff --git a/backend/pkg/providers/bedrock/adaptive_thinking_test.go b/backend/pkg/providers/bedrock/adaptive_thinking_test.goindex 2908d833..9e1245ee 100644--- a/backend/pkg/providers/bedrock/adaptive_thinking_test.go+++ b/backend/pkg/providers/bedrock/adaptive_thinking_test.go@@ -77,8 +77,8 @@ func TestRewriteAdaptiveThinkingBodyStripsSamplingAndSetsDisplay(t *testing.T) {}// TestBackstopForcesAdaptiveForAdaptiveOnlyModel verifies that an adaptive-only-// model (from models.yml) forces adaptive thinking via prepareCallOptions even-// when the agent config carries no reasoning block.+// model (from models.yml) forces adaptive thinking via PrepareAdaptiveCallOptions+// even when the agent config carries no reasoning block.func TestBackstopForcesAdaptiveForAdaptiveOnlyModel(t *testing.T) {models, err := DefaultModels(&config.Config{})require.NoError(t, err)@@ -86,14 +86,11 @@ func TestBackstopForcesAdaptiveForAdaptiveOnlyModel(t *testing.T) {pc, err := BuildProviderConfig([]byte("simple:\n model: us.anthropic.claude-opus-4-8\n temperature: 1.0\n n: 1\n max_tokens: 4000\n"))require.NoError(t, err)- p := &bedrockProvider{providerConfig: pc, models: models}-- assert.Equal(t, pconfig.ModelReasoningAdaptiveOnly, p.modelReasoningMode(pconfig.OptionsTypeSimple))- assert.True(t, p.usesAdaptiveThinking(pconfig.OptionsTypeSimple),+ assert.True(t, pc.UsesAdaptiveThinking(models, pconfig.OptionsTypeSimple),"adaptive-only model must force adaptive even without an agent reasoning block")- ctx, options := p.prepareCallOptions(context.Background(), pconfig.OptionsTypeSimple, nil)- effort, ok := adaptiveThinkingEffortFromContext(ctx)+ ctx, options := pc.PrepareAdaptiveCallOptions(context.Background(), models, pconfig.OptionsTypeSimple, nil)+ effort, ok := pconfig.AdaptiveEffortFromContext(ctx)assert.True(t, ok, "effort must be installed in context for the middleware")assert.Equal(t, string(llms.ReasoningHigh), effort)assert.NotEmpty(t, options, "WithReasoning must be appended so langchaingo emits a thinking block")@@ -107,13 +104,10 @@ func TestBackstopLeavesNonReasoningModelUntouched(t *testing.T) {pc, err := BuildProviderConfig([]byte("simple:\n model: openai.gpt-oss-120b-1:0\n temperature: 0.5\n n: 1\n max_tokens: 4000\n"))require.NoError(t, err)- p := &bedrockProvider{providerConfig: pc, models: models}-- assert.NotEqual(t, pconfig.ModelReasoningAdaptiveOnly, p.modelReasoningMode(pconfig.OptionsTypeSimple))- assert.False(t, p.usesAdaptiveThinking(pconfig.OptionsTypeSimple))+ assert.False(t, pc.UsesAdaptiveThinking(models, pconfig.OptionsTypeSimple))- ctx, options := p.prepareCallOptions(context.Background(), pconfig.OptionsTypeSimple, nil)- _, ok := adaptiveThinkingEffortFromContext(ctx)+ ctx, options := pc.PrepareAdaptiveCallOptions(context.Background(), models, pconfig.OptionsTypeSimple, nil)+ _, ok := pconfig.AdaptiveEffortFromContext(ctx)assert.False(t, ok, "no adaptive effort should be set for a non-adaptive model")assert.Empty(t, options)}diff --git a/backend/pkg/providers/bedrock/bedrock.go b/backend/pkg/providers/bedrock/bedrock.goindex 3bc8b3b7..37427121 100644--- a/backend/pkg/providers/bedrock/bedrock.go+++ b/backend/pkg/providers/bedrock/bedrock.go@@ -249,7 +249,9 @@ func (p *bedrockProvider) Call(opt pconfig.ProviderOptionsType,prompt string,) (string, error) {- ctx, options := p.prepareCallOptions(ctx, opt, p.providerConfig.GetOptionsForType(opt))+ ctx, options := p.providerConfig.PrepareAdaptiveCallOptions(+ ctx, p.models, opt, p.providerConfig.GetOptionsForType(opt),+ )return provider.WrapGenerateFromSinglePrompt(ctx, p, opt, p.llm, prompt, options...,@@ -282,7 +284,7 @@ func (p *bedrockProvider) CallEx(options := []llms.CallOption{llms.WithStreamingFunc(streamCb)}options = append(options, configOptions...)options = append(options, llms.WithTools(tools))- ctx, options = p.prepareCallOptions(ctx, opt, options)+ ctx, options = p.providerConfig.PrepareAdaptiveCallOptions(ctx, p.models, opt, options)return provider.WrapGenerateContent(ctx, p, opt, p.llm.GenerateContent, chain, options...)}@@ -307,7 +309,7 @@ func (p *bedrockProvider) CallWithTools(// Put cleaned tools after config to override any dirty tools restored from config.options := append(configOptions, llms.WithStreamingFunc(streamCb), llms.WithTools(tools))- ctx, options = p.prepareCallOptions(ctx, opt, options)+ ctx, options = p.providerConfig.PrepareAdaptiveCallOptions(ctx, p.models, opt, options)return provider.WrapGenerateContent(ctx, p, opt, p.llm.GenerateContent, chain, options...)}diff --git a/backend/pkg/providers/pconfig/config.go b/backend/pkg/providers/pconfig/config.goindex e3f5b478..0f8e0e08 100644--- a/backend/pkg/providers/pconfig/config.go+++ b/backend/pkg/providers/pconfig/config.go@@ -1,6 +1,7 @@package pconfigimport (+ "context""encoding/json""fmt""os"@@ -915,58 +916,137 @@ func (pc *ProviderConfig) GetOptionsForType(optType ProviderOptionsType) []llms.return pc.defaultOptions}-func (pc *ProviderConfig) GetPriceInfoForType(optType ProviderOptionsType) *PriceInfo {+// AgentConfigForType returns the agent config for opt, applying the+// SimpleJSON→Simple and Assistant→PrimaryAgent fallbacks. Returns nil for an+// unset slot or unknown type.+func (pc *ProviderConfig) AgentConfigForType(optType ProviderOptionsType) *AgentConfig {if pc == nil {return nil}- var agentConfig *AgentConfigswitch optType {case OptionsTypeSimple:- agentConfig = pc.Simple+ return pc.Simplecase OptionsTypeSimpleJSON:if pc.SimpleJSON != nil {- agentConfig = pc.SimpleJSON- } else {- agentConfig = pc.Simple+ return pc.SimpleJSON}+ return pc.Simplecase OptionsTypePrimaryAgent:- agentConfig = pc.PrimaryAgent+ return pc.PrimaryAgentcase OptionsTypeAssistant:if pc.Assistant != nil {- agentConfig = pc.Assistant- } else {- agentConfig = pc.PrimaryAgent+ return pc.Assistant}+ return pc.PrimaryAgentcase OptionsTypeGenerator:- agentConfig = pc.Generator+ return pc.Generatorcase OptionsTypeRefiner:- agentConfig = pc.Refiner+ return pc.Refinercase OptionsTypeAdviser:- agentConfig = pc.Adviser+ return pc.Advisercase OptionsTypeReflector:- agentConfig = pc.Reflector+ return pc.Reflectorcase OptionsTypeSearcher:- agentConfig = pc.Searcher+ return pc.Searchercase OptionsTypeEnricher:- agentConfig = pc.Enricher+ return pc.Enrichercase OptionsTypeCoder:- agentConfig = pc.Coder+ return pc.Codercase OptionsTypeInstaller:- agentConfig = pc.Installer+ return pc.Installercase OptionsTypePentester:- agentConfig = pc.Pentester+ return pc.Pentesterdefault:return nil}+}- if agentConfig != nil && agentConfig.Price != nil {+func (pc *ProviderConfig) GetPriceInfoForType(optType ProviderOptionsType) *PriceInfo {+ if agentConfig := pc.AgentConfigForType(optType); agentConfig != nil {return agentConfig.Price}return nil}+// Adaptive thinking is requested as a budget thinking block (via WithReasoning),+// which the bedrock/anthropic transport interceptors rewrite to+// thinking.type=adaptive + output_config.effort. The chosen effort travels to the+// interceptor on the request context.+type adaptiveEffortContextKey struct{}++// WithAdaptiveEffort threads the adaptive-thinking effort onto ctx. Empty effort+// defaults to "high" (the API rejects an empty effort).+func WithAdaptiveEffort(ctx context.Context, effort string) context.Context {+ if effort == "" {+ effort = string(llms.ReasoningHigh)+ }++ return context.WithValue(ctx, adaptiveEffortContextKey{}, effort)+}++func AdaptiveEffortFromContext(ctx context.Context) (string, bool) {+ effort, ok := ctx.Value(adaptiveEffortContextKey{}).(string)+ return effort, ok && effort != ""+}++func (pc *ProviderConfig) modelReasoningMode(models ModelsConfig, opt ProviderOptionsType) ModelReasoningMode {+ agentConfig := pc.AgentConfigForType(opt)+ if agentConfig == nil || agentConfig.Model == "" {+ return ModelReasoningNone+ }++ for _, m := range models {+ if m.Name == agentConfig.Model && m.Reasoning != nil {+ return m.Reasoning.Mode+ }+ }++ return ModelReasoningNone+}++func (pc *ProviderConfig) reasoningConfigForType(opt ProviderOptionsType) (ReasoningConfig, bool) {+ agentConfig := pc.AgentConfigForType(opt)+ if agentConfig == nil || agentConfig.Reasoning.IsZero() {+ return ReasoningConfig{}, false+ }++ return agentConfig.Reasoning, true+}++// UsesAdaptiveThinking reports whether this agent's call must use adaptive+// thinking: the agent selected adaptive mode, or the model only supports adaptive+// (e.g. Opus 4.7/4.8, where budget thinking returns a 400).+func (pc *ProviderConfig) UsesAdaptiveThinking(models ModelsConfig, opt ProviderOptionsType) bool {+ if pc.modelReasoningMode(models, opt) == ModelReasoningAdaptiveOnly {+ return true+ }++ reasoning, ok := pc.reasoningConfigForType(opt)+ return ok && reasoning.EffectiveMode() == ReasoningModeAdaptive+}++// PrepareAdaptiveCallOptions threads the effort onto ctx and appends the budget+// thinking CallOption that the interceptor rewrites to adaptive form. No-op for+// non-adaptive calls.+func (pc *ProviderConfig) PrepareAdaptiveCallOptions(+ ctx context.Context,+ models ModelsConfig,+ opt ProviderOptionsType,+ options []llms.CallOption,+) (context.Context, []llms.CallOption) {+ if !pc.UsesAdaptiveThinking(models, opt) {+ return ctx, options+ }++ reasoning, _ := pc.reasoningConfigForType(opt)+ ctx = WithAdaptiveEffort(ctx, string(reasoning.Effort))+ options = append(options, llms.WithReasoning(llms.ReasoningHigh, 0))++ return ctx, options+}+func (pc *ProviderConfig) BuildOptionsMap() map[ProviderOptionsType][]llms.CallOption {if pc == nil {return nil