Skip to content

Fully autonomous AI Agents system capable of performing complex penetration testing tasks

feat(pconfig): validate agent config ranges on create/update (refs #288)

Sergey Kozyrenko4c0f4204cabd
diff --git a/backend/pkg/providers/pconfig/config.go b/backend/pkg/providers/pconfig/config.go
index b3eddfb9..e3f5b478 100644
--- a/backend/pkg/providers/pconfig/config.go
+++ b/backend/pkg/providers/pconfig/config.go
@@ -276,6 +276,71 @@ type ProviderConfig struct {
rawConfig []byte `json:"-" yaml:"-"`
}
+// Validate rejects universally-invalid agent values (negatives where a value is
+// physically meaningless, an inverted length window, an out-of-range probability
+// or temperature, a reasoning budget over the engine cap). It stays permissive —
+// 0 means "unset", and provider-specific tuning ranges are the LLM API's job —
+// so it never rejects a valid provider-specific config.
+func (pc *ProviderConfig) Validate() error {
+ agents := map[string]*AgentConfig{
+ "simple": pc.Simple, "simple_json": pc.SimpleJSON, "primary_agent": pc.PrimaryAgent,
+ "assistant": pc.Assistant, "generator": pc.Generator, "refiner": pc.Refiner,
+ "adviser": pc.Adviser, "reflector": pc.Reflector, "searcher": pc.Searcher,
+ "enricher": pc.Enricher, "coder": pc.Coder, "installer": pc.Installer,
+ "pentester": pc.Pentester,
+ }
+ for name, ac := range agents {
+ if ac == nil {
+ continue
+ }
+ if err := ac.Validate(); err != nil {
+ return fmt.Errorf("%s: %w", name, err)
+ }
+ }
+ return nil
+}
+
+func (ac *AgentConfig) Validate() error {
+ if ac.Temperature < 0 || ac.Temperature > 2 {
+ return fmt.Errorf("temperature %v out of range [0, 2]", ac.Temperature)
+ }
+ if ac.TopP < 0 || ac.TopP > 1 {
+ return fmt.Errorf("top_p %v out of range [0, 1]", ac.TopP)
+ }
+ if ac.TopK < 0 {
+ return fmt.Errorf("top_k %d must be >= 0", ac.TopK)
+ }
+ if ac.MaxTokens < 0 {
+ return fmt.Errorf("max_tokens %d must be >= 0", ac.MaxTokens)
+ }
+ if ac.MinLength < 0 {
+ return fmt.Errorf("min_length %d must be >= 0", ac.MinLength)
+ }
+ if ac.MaxLength < 0 {
+ return fmt.Errorf("max_length %d must be >= 0", ac.MaxLength)
+ }
+ if ac.MinLength > 0 && ac.MaxLength > 0 && ac.MinLength > ac.MaxLength {
+ return fmt.Errorf("min_length %d must not exceed max_length %d", ac.MinLength, ac.MaxLength)
+ }
+ if ac.RepetitionPenalty < 0 || ac.RepetitionPenalty > 2 {
+ return fmt.Errorf("repetition_penalty %v out of range [0, 2]", ac.RepetitionPenalty)
+ }
+ if ac.FrequencyPenalty < -2 || ac.FrequencyPenalty > 2 {
+ return fmt.Errorf("frequency_penalty %v out of range [-2, 2]", ac.FrequencyPenalty)
+ }
+ if ac.PresencePenalty < -2 || ac.PresencePenalty > 2 {
+ return fmt.Errorf("presence_penalty %v out of range [-2, 2]", ac.PresencePenalty)
+ }
+ if ac.Reasoning.MaxTokens < 0 || ac.Reasoning.MaxTokens > 32000 {
+ return fmt.Errorf("reasoning.max_tokens %d out of range [0, 32000]", ac.Reasoning.MaxTokens)
+ }
+ if ac.Price != nil &&
+ (ac.Price.Input < 0 || ac.Price.Output < 0 || ac.Price.CacheRead < 0 || ac.Price.CacheWrite < 0) {
+ return fmt.Errorf("price values must be >= 0")
+ }
+ return nil
+}
+
const EmptyProviderConfigRaw = `{
"simple": {},
"simple_json": {},
diff --git a/backend/pkg/providers/pconfig/config_test.go b/backend/pkg/providers/pconfig/config_test.go
index 4b119afe..d70881f0 100644
--- a/backend/pkg/providers/pconfig/config_test.go
+++ b/backend/pkg/providers/pconfig/config_test.go
@@ -1956,3 +1956,35 @@ func TestReasoningConfig_IsZero(t *testing.T) {
assert.False(t, ReasoningConfig{Effort: llms.ReasoningLow}.IsZero())
assert.False(t, ReasoningConfig{MaxTokens: 1}.IsZero())
}
+
+func TestAgentConfigValidate(t *testing.T) {
+ require.NoError(t, (&AgentConfig{
+ Temperature: 2, TopP: 1, TopK: 40, MaxTokens: 8192,
+ MinLength: 1, MaxLength: 100,
+ RepetitionPenalty: 1.1, FrequencyPenalty: -2, PresencePenalty: 2,
+ Reasoning: ReasoningConfig{MaxTokens: 32000},
+ Price: &PriceInfo{Input: 5, Output: 25},
+ }).Validate())
+ require.NoError(t, (&AgentConfig{}).Validate(), "zero value (all unset) is valid")
+
+ cases := map[string]*AgentConfig{
+ "temperature too high": {Temperature: 5},
+ "temperature negative": {Temperature: -1},
+ "top_p too high": {TopP: 1.5},
+ "top_k negative": {TopK: -1},
+ "max_tokens negative": {MaxTokens: -100},
+ "inverted length window": {MinLength: 5000, MaxLength: 10},
+ "frequency out of range": {FrequencyPenalty: 9},
+ "reasoning budget over cap": {Reasoning: ReasoningConfig{MaxTokens: 40000}},
+ "negative price": {Price: &PriceInfo{Input: -1}},
+ }
+ for name, ac := range cases {
+ t.Run(name, func(t *testing.T) {
+ assert.Error(t, ac.Validate())
+ })
+ }
+
+ err := (&ProviderConfig{Adviser: &AgentConfig{Temperature: 5}}).Validate()
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "adviser")
+}
diff --git a/backend/pkg/providers/providers.go b/backend/pkg/providers/providers.go
index 23af1003..e7691f19 100644
--- a/backend/pkg/providers/providers.go
+++ b/backend/pkg/providers/providers.go
@@ -888,6 +888,10 @@ func (pc *providerController) CreateProvider(
return result, fmt.Errorf("failed to patch provider config: %w", err)
}
+ if err = config.Validate(); err != nil {
+ return result, fmt.Errorf("invalid provider config: %w", err)
+ }
+
rawConfig, err := json.Marshal(config)
if err != nil {
return result, fmt.Errorf("failed to marshal provider config: %w", err)
@@ -934,6 +938,10 @@ func (pc *providerController) UpdateProvider(
return result, fmt.Errorf("failed to patch provider config: %w", err)
}
+ if err = config.Validate(); err != nil {
+ return result, fmt.Errorf("invalid provider config: %w", err)
+ }
+
rawConfig, err := json.Marshal(config)
if err != nil {
return result, fmt.Errorf("failed to marshal provider config: %w", err)