From ece951aa6c8073001761d149c916657cde8e4c55 Mon Sep 17 00:00:00 2001 From: sourcegraph-release-guild-bot <107104610+sourcegraph-release-guild-bot@users.noreply.github.com> Date: Mon, 22 Apr 2024 11:31:43 -0400 Subject: [PATCH] [Backport 5.3.9104] Context: fix bug with result limits (#62054) Context: fix bug with result limits (#61806) While writing eval scripts for Cody context, I noticed that when searching multiple repos, we sometimes we return greater than the intended number of results. This PR makes a small fix so we always respect the result limits. (cherry picked from commit 6a4ac36778d382413be4b2dc2c47f99d808a0e1e) Co-authored-by: Julie Tibshirani --- internal/codycontext/context.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/codycontext/context.go b/internal/codycontext/context.go index b1bf18652f70..bb6111a0bc00 100644 --- a/internal/codycontext/context.go +++ b/internal/codycontext/context.go @@ -302,6 +302,12 @@ func (c *CodyContextClient) getKeywordContext(ctx context.Context, args GetConte mu.Lock() defer mu.Unlock() + // Another caller may have already hit the limit, but we haven't yet responded + // to the cancellation. Return immediately in this case. + if len(collected) >= limit { + return + } + for _, res := range e.Results { if fm, ok := res.(*result.FileMatch); ok { collected = append(collected, filter(fileMatchToContextMatches(fm))...)