{"record":{"id":"fa7fc16bf9217927","repo":"Tencent/WeKnora","slug":"cpu-limit-cannot-be-negative","errorCode":null,"errorMessage":"CPU limit cannot be negative","messagePattern":"CPU limit cannot be negative","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sandbox/sandbox.go","lineNumber":386,"sourceCode":"\t}\n\n\tswitch config.Type {\n\tcase SandboxTypeDocker, SandboxTypeCube, SandboxTypeE2B, SandboxTypeDisabled:\n\t\t// Valid types\n\tdefault:\n\t\treturn errors.New(\"invalid sandbox type\")\n\t}\n\n\tif config.DefaultTimeout < 0 {\n\t\treturn errors.New(\"timeout cannot be negative\")\n\t}\n\n\tif config.MaxMemory < 0 {\n\t\treturn errors.New(\"memory limit cannot be negative\")\n\t}\n\n\tif config.MaxCPU < 0 {\n\t\treturn errors.New(\"CPU limit cannot be negative\")\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":368,"sourceCodeEnd":391,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/sandbox/sandbox.go#L368-L391","documentation":"Range guard in ValidateConfig: fires when the effective sandbox configuration specifies a negative MaxCPU value. CPU limits are resource ceilings and must be zero-or-positive; a negative value indicates a miscomputed or malformed config and is rejected before any manager or session is created.","triggerScenarios":"Passing a SandboxConfig with MaxCPU < 0 to NewManager or NewSessionBoundManager, e.g. a negative CPU quota from a config file or an arithmetic error when computing core budgets.","commonSituations":"Computing reserved vs. total cores and subtracting in the wrong order; a stray minus sign in a limits config.","solutions":["Set MaxCPU to a positive core count or 0 for no limit.","Fix the calculation producing the negative value (check subtraction order and underflow).","Validate MaxCPU >= 0 before constructing the Manager."],"exampleFix":"// before\ncfg.MaxCPU = totalCores - reservedCores // can go negative\n// after\ncfg.MaxCPU = max(0, totalCores-reservedCores)","handlingStrategy":"validation","validationCode":"if cfg.MaxCPU < 0 {\n    return fmt.Errorf(\"invalid sandbox config: MaxCPU=%d must be >= 0\", cfg.MaxCPU)\n}","typeGuard":null,"tryCatchPattern":"if err := sandbox.ValidateConfig(cfg); err != nil {\n    if strings.Contains(err.Error(), \"CPU\") {\n        return fmt.Errorf(\"fix MaxCPU (got %d)\", cfg.MaxCPU)\n    }\n    return err\n}","preventionTips":["Clamp derived CPU budgets with max(0, x).","Keep CPU fields as int64 and validate parsed config values.","Add table tests covering negative-limit rejection."],"tags":["go","sandbox","configuration","validation","cpu"],"backgroundTag":"invalid-config-value","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}