-
Notifications
You must be signed in to change notification settings - Fork 389
fix(rag): stop flooding the gateway on non-retryable model errors #3091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package rag | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net/http" | ||
| "sync/atomic" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/docker/docker-agent/pkg/modelerrors" | ||
| "github.com/docker/docker-agent/pkg/rag/database" | ||
| "github.com/docker/docker-agent/pkg/rag/strategy" | ||
| ) | ||
|
|
||
| // failingReranker counts calls and always fails with a fixed error. | ||
| type failingReranker struct { | ||
| calls atomic.Int64 | ||
| err error | ||
| } | ||
|
|
||
| func (r *failingReranker) Rerank(context.Context, string, []database.SearchResult) ([]database.SearchResult, error) { | ||
| r.calls.Add(1) | ||
| return nil, r.err | ||
| } | ||
|
|
||
| // staticStrategy returns fixed results for every query. | ||
| type staticStrategy struct { | ||
| results []database.SearchResult | ||
| } | ||
|
|
||
| func (s *staticStrategy) Initialize(context.Context, []string, strategy.ChunkingConfig) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (s *staticStrategy) Query(context.Context, string, int, float64) ([]database.SearchResult, error) { | ||
| return s.results, nil | ||
| } | ||
|
|
||
| func (s *staticStrategy) CheckAndReindexChangedFiles(context.Context, []string, strategy.ChunkingConfig) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (s *staticStrategy) StartFileWatcher(context.Context, []string, strategy.ChunkingConfig) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (s *staticStrategy) Close() error { return nil } | ||
|
|
||
| func newRerankTestManager(t *testing.T, rerankErr error) (*Manager, *failingReranker) { | ||
| t.Helper() | ||
|
|
||
| results := []database.SearchResult{ | ||
| {Document: database.Document{ID: "1", Content: "doc one"}, Similarity: 0.9}, | ||
| {Document: database.Document{ID: "2", Content: "doc two"}, Similarity: 0.8}, | ||
| } | ||
|
|
||
| reranker := &failingReranker{err: rerankErr} | ||
| cfg := Config{ | ||
| StrategyConfigs: []strategy.Config{{ | ||
| Name: "static", | ||
| Strategy: &staticStrategy{results: results}, | ||
| Limit: 5, | ||
| }}, | ||
| Results: ResultsConfig{ | ||
| RerankingConfig: &RerankingConfig{Reranker: reranker}, | ||
| }, | ||
| } | ||
|
|
||
| m, err := New(t.Context(), "test", cfg, nil) | ||
| require.NoError(t, err) | ||
| return m, reranker | ||
| } | ||
|
|
||
| func TestQueryDisablesRerankerAfterNonRetryableError(t *testing.T) { | ||
| rerankErr := &modelerrors.StatusError{ | ||
| StatusCode: http.StatusNotFound, | ||
| Err: errors.New("not_found_error: model: claude-sonnet-4-7"), | ||
| } | ||
| m, reranker := newRerankTestManager(t, rerankErr) | ||
|
|
||
| for range 3 { | ||
| results, err := m.Query(t.Context(), "some query") | ||
| require.NoError(t, err, "rerank failures must not fail the query") | ||
| assert.Len(t, results, 2, "original results are returned as fallback") | ||
| } | ||
|
|
||
| assert.Equal(t, int64(1), reranker.calls.Load(), | ||
| "reranker must be disabled after the first non-retryable error instead of being called on every query") | ||
| } | ||
|
|
||
| func TestQueryKeepsRerankerOnTransientError(t *testing.T) { | ||
| rerankErr := &modelerrors.StatusError{ | ||
| StatusCode: http.StatusInternalServerError, | ||
| Err: errors.New("server error"), | ||
| } | ||
| m, reranker := newRerankTestManager(t, rerankErr) | ||
|
|
||
| for range 3 { | ||
| results, err := m.Query(t.Context(), "some query") | ||
| require.NoError(t, err) | ||
| assert.Len(t, results, 2) | ||
| } | ||
|
|
||
| assert.Equal(t, int64(3), reranker.calls.Load(), | ||
| "transient errors should not disable the reranker") | ||
| } | ||
|
|
||
| func TestQueryKeepsRerankerOnContextCancellation(t *testing.T) { | ||
| m, reranker := newRerankTestManager(t, context.Canceled) | ||
|
|
||
| results, err := m.Query(t.Context(), "some query") | ||
| require.NoError(t, err) | ||
| assert.Len(t, results, 2) | ||
| assert.Equal(t, int64(1), reranker.calls.Load()) | ||
| assert.False(t, m.rerankDisabled.Load(), | ||
| "context cancellation must not permanently disable the reranker") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package strategy | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/docker/docker-agent/pkg/modelerrors" | ||
| ) | ||
|
|
||
| // errIndexingAborted marks a permanent model/provider failure (e.g. invalid | ||
| // model name, authentication failure, rate limit) encountered during indexing. | ||
| // When such an error occurs, the whole indexing run must stop immediately: | ||
| // every remaining file/chunk would trigger the same failing request, flooding | ||
| // the provider (see https://github.com/docker/docker-agent/issues/3082). | ||
| var errIndexingAborted = errors.New("indexing aborted due to non-retryable model error") | ||
|
|
||
| // classifyModelCallError inspects an error returned by an embedding or LLM | ||
| // call made during indexing. Permanent failures are wrapped with | ||
| // errIndexingAborted so callers can abort the run; transient failures (5xx, | ||
| // timeouts) and context cancellation are returned unchanged so callers can | ||
| // skip the current file and continue. | ||
| func classifyModelCallError(err error) error { | ||
| if err == nil { | ||
| return nil | ||
| } | ||
| if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { | ||
| return err | ||
| } | ||
| // Rate-limited (429) errors are also non-retryable here: continuing to | ||
| // index would keep hammering a provider that asked us to back off. | ||
| retryable, _, _ := modelerrors.ClassifyModelError(err) | ||
| if !retryable { | ||
| return fmt.Errorf("%w: %w", errIndexingAborted, err) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| // isIndexingAborted reports whether err carries the errIndexingAborted marker. | ||
| func isIndexingAborted(err error) bool { | ||
| return errors.Is(err, errIndexingAborted) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[LOW] watchLoop keeps running after abort — re-index will be retried on every subsequent filesystem event
The
breakon line 946 exits the innerfor i, file := range filesToReindexloop inside theprocessChangesclosure, stopping the current batch. However,watchLoop's outerfor { select { ... } }loop continues running. On the next filesystem write/create event,processChangesis called again, attempts to re-index with the same broken model, hits the same non-retryable error, emits another error event, and breaks again.This partially defeats the flood-prevention goal for the watcher path: the number of doomed requests is reduced from all files per event to one request per event, but remains unbounded over time. The
InitializeandCheckAndReindexChangedFilespaths are correctly fixed (abort propagates to the caller), but the watcher path has no persistent "disabled" state equivalent torerankDisabled.Store(true).Suggested fix: introduce a persistent
indexingDisabled atomic.BoolonVectorStore(similar toManager.rerankDisabled) and check it at the top ofprocessChanges(or at the start ofwatchLoop) after storingtrueon abort. Alternatively, cancel the watcher's context on abort.