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
fix(cast,providers): replace invalid tool call JSON arguments with empty object
Dmitry Ng161afb8b5900
diff --git a/backend/pkg/cast/chain_ast.go b/backend/pkg/cast/chain_ast.goindex cddbccc9..5a84763a 100644--- a/backend/pkg/cast/chain_ast.go+++ b/backend/pkg/cast/chain_ast.go@@ -1127,7 +1127,12 @@ func SanitizeJSONControlChars(s string) string {// SanitizeToolCallArguments scans every tool call in the chain and ensures its Arguments// field is valid JSON. Literal control characters that some LLM providers occasionally// emit inside string values are escaped so that downstream consumers (e.g. vLLM's-// _postprocess_messages) can parse the arguments without errors.+// _postprocess_messages) can parse the arguments without errors. If the arguments are+// still not valid JSON after escaping (e.g. a truncated object like "{" produced by a+// partial LLM response), they are replaced with a fallback empty object so that the+// chain can be replayed without triggering an API-level 400 Bad Request. The missing+// arguments will then cause the tool call to fail, which in turn triggers the tool-call+// fixer to regenerate correct arguments.func (ast *ChainAST) SanitizeToolCallArguments() {for _, section := range ast.Sections {for _, bodyPair := range section.Body {@@ -1146,6 +1151,9 @@ func (ast *ChainAST) SanitizeToolCallArguments() {}sanitized := SanitizeJSONControlChars(toolCall.FunctionCall.Arguments)+ if !json.Valid([]byte(sanitized)) {+ sanitized = fallbackRequestArgs+ }if sanitized != toolCall.FunctionCall.Arguments {toolCall.FunctionCall.Arguments = sanitizedbodyPair.AIMessage.Parts[pdx] = toolCalldiff --git a/backend/pkg/cast/chain_ast_test.go b/backend/pkg/cast/chain_ast_test.goindex edfe4fb0..31b9bc34 100644--- a/backend/pkg/cast/chain_ast_test.go+++ b/backend/pkg/cast/chain_ast_test.go@@ -3176,6 +3176,29 @@ func TestSanitizeToolCallArguments(t *testing.T) {args: `{}`,wantArgs: `{}`,},+ {+ // Simulates a truncated LLM response where only the opening brace was emitted.+ // This exact pattern was observed with Qwen3 models via LiteLLM and caused+ // repeated 400 Bad Request errors on subsequent chain calls.+ name: "single opening brace (truncated output) - replaced with empty object",+ args: `{`,+ wantArgs: `{}`,+ },+ {+ name: "partially constructed JSON object - replaced with empty object",+ args: `{"input": "ls -la"`,+ wantArgs: `{}`,+ },+ {+ name: "empty string - replaced with empty object",+ args: ``,+ wantArgs: `{}`,+ },+ {+ name: "JSON array instead of object - unchanged (valid JSON)",+ args: `[]`,+ wantArgs: `[]`,+ },}for _, tt := range tests {diff --git a/backend/pkg/providers/performer.go b/backend/pkg/providers/performer.goindex 32f8e462..4e718e52 100644--- a/backend/pkg/providers/performer.go+++ b/backend/pkg/providers/performer.go@@ -444,7 +444,16 @@ func (fp *flowProvider) callWithRetries(if toolCall.FunctionCall == nil {continue}- toolCall.FunctionCall.Arguments = cast.SanitizeJSONControlChars(toolCall.FunctionCall.Arguments)+ sanitizedArgs := cast.SanitizeJSONControlChars(toolCall.FunctionCall.Arguments)+ if !json.Valid([]byte(sanitizedArgs)) {+ logger.WithFields(logrus.Fields{+ "tool_call_id": toolCall.ID,+ "tool_name": toolCall.FunctionCall.Name,+ "raw_args": toolCall.FunctionCall.Arguments[:min(200, len(toolCall.FunctionCall.Arguments))],+ }).Warn("tool call has invalid JSON arguments, replacing with empty object to allow tool-call fixer to regenerate them")+ sanitizedArgs = "{}"+ }+ toolCall.FunctionCall.Arguments = sanitizedArgsresult.funcCalls = append(result.funcCalls, toolCall)}}