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(backend): update LRU cache implementation to use non-expirable version
Dmitry Ng9caa6a87753a
diff --git a/backend/pkg/controller/aslog.go b/backend/pkg/controller/aslog.goindex 769ccf87..ab1a48ef 100644--- a/backend/pkg/controller/aslog.go+++ b/backend/pkg/controller/aslog.go@@ -10,14 +10,13 @@ import ("pentagi/pkg/graph/subscriptions""pentagi/pkg/providers"- lru "github.com/hashicorp/golang-lru/v2/expirable"+ lru "github.com/hashicorp/golang-lru/v2""github.com/vxcontrol/langchaingo/llms/reasoning")const (updateMsgTimeout = 30 * time.SecondstreamCacheSize = 1000- streamCacheTTL = 2 * time.Hour)type FlowAssistantLogWorker interface {@@ -57,7 +56,7 @@ type flowAssistantLogWorker struct {flowID int64assistantID int64results map[int64]chan *providers.StreamMessageChunk- streamCache *lru.LRU[int64, int64] // streamID -> msgID+ streamCache *lru.Cache[int64, int64] // streamID -> msgIDpub subscriptions.FlowPublisher}@@ -70,7 +69,7 @@ func NewFlowAssistantLogWorker(flowID: flowID,assistantID: assistantID,results: make(map[int64]chan *providers.StreamMessageChunk),- streamCache: lru.NewLRU[int64, int64](streamCacheSize, nil, streamCacheTTL),+ streamCache: func() *lru.Cache[int64, int64] { c, _ := lru.New[int64, int64](streamCacheSize); return c }(),pub: pub,}}diff --git a/backend/pkg/providers/helpers_test.go b/backend/pkg/providers/helpers_test.goindex 1b4099b8..b5d3bb25 100644--- a/backend/pkg/providers/helpers_test.go+++ b/backend/pkg/providers/helpers_test.go@@ -12,7 +12,6 @@ import ("pentagi/pkg/cast"- lru "github.com/hashicorp/golang-lru/v2/expirable""github.com/stretchr/testify/assert""github.com/vxcontrol/langchaingo/llms")@@ -203,7 +202,7 @@ func newFlowProvider() *flowProvider {return &flowProvider{mx: &sync.RWMutex{},dataDir: "testdata",- summarizerCache: lru.NewLRU[[32]byte, string](summarizerCacheMaxSize, nil, summarizerCacheTTL),+ summarizerCache: newSummarizerCache(),callCounter: &atomic.Int64{},maxGACallsLimit: maxGeneralAgentChainIterations,maxLACallsLimit: maxLimitedAgentChainIterations,diff --git a/backend/pkg/providers/provider.go b/backend/pkg/providers/provider.goindex e3c7425b..eb996c99 100644--- a/backend/pkg/providers/provider.go+++ b/backend/pkg/providers/provider.go@@ -22,7 +22,7 @@ import ("pentagi/pkg/templates""pentagi/pkg/tools"- lru "github.com/hashicorp/golang-lru/v2/expirable"+ lru "github.com/hashicorp/golang-lru/v2""github.com/sirupsen/logrus""github.com/vxcontrol/langchaingo/llms""github.com/vxcontrol/langchaingo/llms/reasoning"@@ -157,7 +157,7 @@ type flowProvider struct {summarizer csum.Summarizer- summarizerCache *lru.LRU[[32]byte, string]+ summarizerCache *lru.Cache[[32]byte, string]maxGACallsLimit intmaxLACallsLimit intdiff --git a/backend/pkg/providers/providers.go b/backend/pkg/providers/providers.goindex d83cf5e0..20e54fb5 100644--- a/backend/pkg/providers/providers.go+++ b/backend/pkg/providers/providers.go@@ -26,7 +26,7 @@ import ("pentagi/pkg/templates""pentagi/pkg/tools"- lru "github.com/hashicorp/golang-lru/v2/expirable"+ lru "github.com/hashicorp/golang-lru/v2""github.com/sirupsen/logrus")@@ -34,10 +34,16 @@ const deltaCallCounter = 10000const defaultTestParallelWorkersNumber = 16-const (- summarizerCacheMaxSize = 1000- summarizerCacheTTL = 4 * time.Hour-)+const summarizerCacheMaxSize = 100++// newSummarizerCache creates a fixed-size LRU cache for summarizer results.+// Using lru.Cache (non-expirable) instead of expirable.LRU avoids spawning a+// per-instance background goroutine that would otherwise accumulate for every+// short-lived flow/assistant provider.+func newSummarizerCache() *lru.Cache[[32]byte, string] {+ c, _ := lru.New[[32]byte, string](summarizerCacheMaxSize)+ return c+}const pentestDockerImage = "vxcontrol/kali-linux"@@ -326,7 +332,7 @@ func (pc *providerController) NewFlowProvider(prompter: prompter,executor: executor,summarizer: pc.summarizerAgent,- summarizerCache: lru.NewLRU[[32]byte, string](summarizerCacheMaxSize, nil, summarizerCacheTTL),+ summarizerCache: newSummarizerCache(),Provider: prv,maxGACallsLimit: pc.cfg.MaxGeneralAgentToolCalls,maxLACallsLimit: pc.cfg.MaxLimitedAgentToolCalls,@@ -378,7 +384,7 @@ func (pc *providerController) LoadFlowProvider(prompter: prompter,executor: executor,summarizer: pc.summarizerAgent,- summarizerCache: lru.NewLRU[[32]byte, string](summarizerCacheMaxSize, nil, summarizerCacheTTL),+ summarizerCache: newSummarizerCache(),Provider: prv,maxGACallsLimit: pc.cfg.MaxGeneralAgentToolCalls,maxLACallsLimit: pc.cfg.MaxLimitedAgentToolCalls,@@ -474,7 +480,7 @@ func (pc *providerController) NewAssistantProvider(executor: executor,streamCb: streamCb,summarizer: pc.summarizerAgent,- summarizerCache: lru.NewLRU[[32]byte, string](summarizerCacheMaxSize, nil, summarizerCacheTTL),+ summarizerCache: newSummarizerCache(),Provider: prv,maxGACallsLimit: pc.cfg.MaxGeneralAgentToolCalls,maxLACallsLimit: pc.cfg.MaxLimitedAgentToolCalls,@@ -529,7 +535,7 @@ func (pc *providerController) LoadAssistantProvider(executor: executor,streamCb: streamCb,summarizer: pc.summarizerAgent,- summarizerCache: lru.NewLRU[[32]byte, string](summarizerCacheMaxSize, nil, summarizerCacheTTL),+ summarizerCache: newSummarizerCache(),Provider: prv,maxGACallsLimit: pc.cfg.MaxGeneralAgentToolCalls,maxLACallsLimit: pc.cfg.MaxLimitedAgentToolCalls,diff --git a/backend/pkg/templates/templates.go b/backend/pkg/templates/templates.goindex 4935dde8..66a49339 100644--- a/backend/pkg/templates/templates.go+++ b/backend/pkg/templates/templates.go@@ -7,11 +7,13 @@ import ("encoding/json""errors""fmt"+ "maps""math/big""path""regexp""strconv""strings"+ "sync""text/template")@@ -679,29 +681,52 @@ func (dp *defaultPrompter) DumpTemplates() ([]byte, error) {return blob, nil}-// LoadDefaultPromptsMap returns a freshly populated PromptsMap of the embedded-// default templates. Callers that need to overlay overrides on top of the-// defaults can mutate the returned map directly, without going through the-// JSON dump path used by Prompter.DumpTemplates(). Each call returns a new-// map, so mutations do not affect subsequent callers.-func LoadDefaultPromptsMap() (PromptsMap, error) {- prompts, err := promptTemplates.ReadDir("prompts")- if err != nil {- return nil, fmt.Errorf("failed to read templates: %w", err)- }+// defaultPromptsCache holds the singleton default prompts map loaded once+// from the embedded FS. String values are immutable in Go, so the map itself+// is read-only after initialisation and safe to read from multiple goroutines.+var (+ defaultPromptsCache PromptsMap+ defaultPromptsCacheOnce sync.Once+ defaultPromptsCacheErr error+)- promptsMap := make(PromptsMap, len(prompts))- for _, prompt := range prompts {- promptBytes, err := promptTemplates.ReadFile(path.Join("prompts", prompt.Name()))+// LoadDefaultPromptsMap returns a PromptsMap populated from the embedded+// default templates. The underlying template strings are loaded only once;+// every caller receives a shallow copy of the map so it can safely overlay+// user-specific overrides without affecting the shared cache or other callers.+// String values in Go are immutable, so the copy shares string memory with+// the cache rather than duplicating template content.+func LoadDefaultPromptsMap() (PromptsMap, error) {+ defaultPromptsCacheOnce.Do(func() {+ prompts, err := promptTemplates.ReadDir("prompts")if err != nil {- return nil, fmt.Errorf("failed to read template: %w", err)+ defaultPromptsCacheErr = fmt.Errorf("failed to read templates: %w", err)+ return}- promptName := strings.TrimSuffix(prompt.Name(), ".tmpl")- promptsMap[PromptType(promptName)] = string(promptBytes)+ m := make(PromptsMap, len(prompts))+ for _, prompt := range prompts {+ promptBytes, err := promptTemplates.ReadFile(path.Join("prompts", prompt.Name()))+ if err != nil {+ defaultPromptsCacheErr = fmt.Errorf("failed to read template: %w", err)+ return+ }+ promptName := strings.TrimSuffix(prompt.Name(), ".tmpl")+ m[PromptType(promptName)] = string(promptBytes)+ }+ defaultPromptsCache = m+ })++ if defaultPromptsCacheErr != nil {+ return nil, defaultPromptsCacheErr}- return promptsMap, nil+ // Return a shallow copy: new map header + copied string references.+ // Callers can mutate keys freely; the cached originals are untouched.+ copyMap := make(PromptsMap, len(defaultPromptsCache))+ maps.Copy(copyMap, defaultPromptsCache)++ return copyMap, nil}func RenderPrompt(name, prompt string, params any) (string, error) {