From 8dcc2564182a9d5ddf368539e6496bc0bb5e1371 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 1 Jul 2026 12:04:55 +0000 Subject: [PATCH] perf(agent): read static inject files concurrently Replaces sequential file reading loop with a parallel execution using goroutines and a WaitGroup in `injectSystemPrompt`. Results are assembled using the original order, avoiding data races and ensuring deterministic string generation. Co-authored-by: Gerifield <195914+Gerifield@users.noreply.github.com> --- internal/ai/agent/logic.go | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/internal/ai/agent/logic.go b/internal/ai/agent/logic.go index 3b93fe4..8891f67 100644 --- a/internal/ai/agent/logic.go +++ b/internal/ai/agent/logic.go @@ -10,6 +10,7 @@ import ( "os" "os/exec" "strings" + "sync" "time" "hairy-botter/internal/ai/domain" @@ -272,14 +273,35 @@ func (l *Logic) HandleMessage(ctx context.Context, sessionID string, req domain. func injectSystemPrompt(logger *slog.Logger, systemPrompt string, contextConfig config.ContextConfig) string { var contextInjector strings.Builder - // Inject static data first - for _, file := range contextConfig.StaticInject { - content, err := os.ReadFile(file) - if err != nil { - logger.Warn("failed to load auto-inject file", slog.String("file", file), slog.String("error", err.Error())) - continue + + n := len(contextConfig.StaticInject) + if n > 0 { + type result struct { + content []byte + err error + } + results := make([]result, n) + var wg sync.WaitGroup + + // Inject static data first + for i, file := range contextConfig.StaticInject { + wg.Add(1) + go func(i int, file string) { + defer wg.Done() + content, err := os.ReadFile(file) + results[i] = result{content, err} + }(i, file) + } + wg.Wait() + + for i, file := range contextConfig.StaticInject { + res := results[i] + if res.err != nil { + logger.Warn("failed to load auto-inject file", slog.String("file", file), slog.String("error", res.err.Error())) + continue + } + contextInjector.WriteString(fmt.Sprintf("\n\n[File: %s]\n%s", file, string(res.content))) } - contextInjector.WriteString(fmt.Sprintf("\n\n[File: %s]\n%s", file, string(content))) } // Inject dynamic data