feat(tools): apply configurable timeout in ExecTool

This commit is contained in:
QuietyAwe 2026-02-28 19:11:26 +08:00
parent 8e1b64d140
commit fb17b55dd2

View file

@ -69,15 +69,16 @@ var defaultDenyPatterns = []*regexp.Regexp{
regexp.MustCompile(`\bsource\s+.*\.sh\b`), regexp.MustCompile(`\bsource\s+.*\.sh\b`),
} }
func NewExecTool(workingDir string, restrict bool) *ExecTool { func NewExecTool(workingDir string, restrict bool) (*ExecTool, error) {
return NewExecToolWithConfig(workingDir, restrict, nil) return NewExecToolWithConfig(workingDir, restrict, nil)
} }
func NewExecToolWithConfig(workingDir string, restrict bool, config *config.Config) *ExecTool { func NewExecToolWithConfig(workingDir string, restrict bool, cfg *config.Config) (*ExecTool, error) {
denyPatterns := make([]*regexp.Regexp, 0) denyPatterns := make([]*regexp.Regexp, 0)
timeout := 60 * time.Second // default timeout
if config != nil { if cfg != nil {
execConfig := config.Tools.Exec execConfig := cfg.Tools.Exec
enableDenyPatterns := execConfig.EnableDenyPatterns enableDenyPatterns := execConfig.EnableDenyPatterns
if enableDenyPatterns { if enableDenyPatterns {
denyPatterns = append(denyPatterns, defaultDenyPatterns...) denyPatterns = append(denyPatterns, defaultDenyPatterns...)
@ -86,8 +87,7 @@ func NewExecToolWithConfig(workingDir string, restrict bool, config *config.Conf
for _, pattern := range execConfig.CustomDenyPatterns { for _, pattern := range execConfig.CustomDenyPatterns {
re, err := regexp.Compile(pattern) re, err := regexp.Compile(pattern)
if err != nil { if err != nil {
fmt.Printf("Invalid custom deny pattern %q: %v\n", pattern, err) return nil, fmt.Errorf("invalid custom deny pattern %q: %w", pattern, err)
continue
} }
denyPatterns = append(denyPatterns, re) denyPatterns = append(denyPatterns, re)
} }
@ -96,17 +96,22 @@ func NewExecToolWithConfig(workingDir string, restrict bool, config *config.Conf
// If deny patterns are disabled, we won't add any patterns, allowing all commands. // If deny patterns are disabled, we won't add any patterns, allowing all commands.
fmt.Println("Warning: deny patterns are disabled. All commands will be allowed.") fmt.Println("Warning: deny patterns are disabled. All commands will be allowed.")
} }
// Apply configured timeout if set (0 means use default)
if execConfig.TimeoutSeconds > 0 {
timeout = time.Duration(execConfig.TimeoutSeconds) * time.Second
}
} else { } else {
denyPatterns = append(denyPatterns, defaultDenyPatterns...) denyPatterns = append(denyPatterns, defaultDenyPatterns...)
} }
return &ExecTool{ return &ExecTool{
workingDir: workingDir, workingDir: workingDir,
timeout: 60 * time.Second, timeout: timeout,
denyPatterns: denyPatterns, denyPatterns: denyPatterns,
allowPatterns: nil, allowPatterns: nil,
restrictToWorkspace: restrict, restrictToWorkspace: restrict,
} }, nil
} }
func (t *ExecTool) Name() string { func (t *ExecTool) Name() string {