From 353c59ca732f95c299879abb1dabfabeb01e9abc Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 20 Mar 2026 19:11:49 +0000 Subject: [PATCH 1/3] fix: fix rag embeddedDocs calculation and genkit retrieval hang - Update `l.embeddedDocs` calculation in `internal/rag/rag.go` to use `coll.Count()` instead of the number of newly loaded files so that it correctly accounts for already embedded documents. - Unconditionally pass `ai.WithDocs(ragContextDocs...)` in `internal/ai/agent/logic.go` when generating content, even if the document slice is empty. This prevents a blocking loop/hang in `genkit.Generate` when `SearchEnable` (server-side tools) is enabled but no context documents are explicitly provided to the generation options. Co-authored-by: Gerifield <195914+Gerifield@users.noreply.github.com> --- internal/ai/agent/logic.go | 4 +--- internal/rag/rag.go | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/internal/ai/agent/logic.go b/internal/ai/agent/logic.go index d7e3902..cbe2282 100644 --- a/internal/ai/agent/logic.go +++ b/internal/ai/agent/logic.go @@ -150,9 +150,7 @@ func (l *Logic) HandleMessage(ctx context.Context, sessionID string, req domain. ai.WithConfig(l.customConfig), // It has a nil check internally } - if len(ragContextDocs) > 0 { - genOpts = append(genOpts, ai.WithDocs(ragContextDocs...)) - } + genOpts = append(genOpts, ai.WithDocs(ragContextDocs...)) resp, err := genkit.Generate(ctx, l.g, genOpts...) // TODO: if we rewrite, make this smarter if err != nil { diff --git a/internal/rag/rag.go b/internal/rag/rag.go index f5d5fef..cee81cb 100644 --- a/internal/rag/rag.go +++ b/internal/rag/rag.go @@ -121,7 +121,7 @@ func (l *Logic) loadContent() error { return err }) - l.embeddedDocs = id - 1 // Set the number of embedded documents + l.embeddedDocs = coll.Count() // Set the number of embedded documents l.logger.Info("rag embedding done", slog.Int("num", l.embeddedDocs)) From 201b0823c38e92c38f91ad719694cea538d91e5f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 20 Mar 2026 20:08:37 +0000 Subject: [PATCH 2/3] fix: implement graceful shutdown properly via syscall.SIGTERM - Changed `os.Kill` to `syscall.SIGTERM` in `signal.Notify` and `signal.NotifyContext` calls across application entrypoints (`cmd/server-bot` and `cmd/client-telegram`). - `os.Kill` (SIGKILL) cannot be caught or intercepted by user-space programs, which resulted in the application terminating immediately upon receiving a stop signal without running its shutdown sequence (such as flushing the RAG database). Using `SIGTERM` resolves this and permits the shutdown routines to complete gracefully. Co-authored-by: Gerifield <195914+Gerifield@users.noreply.github.com> --- cmd/client-telegram/main.go | 3 ++- cmd/server-bot/main.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cmd/client-telegram/main.go b/cmd/client-telegram/main.go index fb9ab1e..2c5f096 100644 --- a/cmd/client-telegram/main.go +++ b/cmd/client-telegram/main.go @@ -9,6 +9,7 @@ import ( "os" "os/signal" "strings" + "syscall" "sync" "github.com/go-telegram/bot" @@ -39,7 +40,7 @@ func main() { } l := New(aiSrv, usernameLimits) - ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer cancel() opts := []bot.Option{ diff --git a/cmd/server-bot/main.go b/cmd/server-bot/main.go index cd4436f..b519903 100644 --- a/cmd/server-bot/main.go +++ b/cmd/server-bot/main.go @@ -9,6 +9,7 @@ import ( "os/signal" "strconv" "strings" + "syscall" "hairy-botter/internal/ai/agent" "hairy-botter/internal/ai/gemini" @@ -165,7 +166,7 @@ func main() { }) stopCh := make(chan os.Signal, 1) - signal.Notify(stopCh, os.Interrupt, os.Kill) + signal.Notify(stopCh, os.Interrupt, syscall.SIGTERM) finishedCh := make(chan struct{}) // Signal the end of the graceful shutdown go func() { <-stopCh From e2b7491d036fd64e96ea2524a1cc2afd263c6586 Mon Sep 17 00:00:00 2001 From: Gergely Radics Date: Fri, 20 Mar 2026 21:25:05 +0100 Subject: [PATCH 3/3] fix: add docs only when needed --- internal/ai/agent/logic.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/ai/agent/logic.go b/internal/ai/agent/logic.go index cbe2282..d7e3902 100644 --- a/internal/ai/agent/logic.go +++ b/internal/ai/agent/logic.go @@ -150,7 +150,9 @@ func (l *Logic) HandleMessage(ctx context.Context, sessionID string, req domain. ai.WithConfig(l.customConfig), // It has a nil check internally } - genOpts = append(genOpts, ai.WithDocs(ragContextDocs...)) + if len(ragContextDocs) > 0 { + genOpts = append(genOpts, ai.WithDocs(ragContextDocs...)) + } resp, err := genkit.Generate(ctx, l.g, genOpts...) // TODO: if we rewrite, make this smarter if err != nil {