Repository navigation
fix(rag): honor a YAML integer strategy limit - #4646
Conversation
A bare `limit: 10` on a RAG strategy comes out of the YAML decoder as a uint64. RAGStrategyConfig.UnmarshalYAML only accepted an int, so the value was left in Params, Limit stayed 0 and the strategy returned its default of 5 results. Read the value with coerceToInt, as the chunking size and overlap already do. Signed-off-by: spicysharkco <321699413+spicysharkco@users.noreply.github.com>
aheritier
left a comment
There was a problem hiding this comment.
Thanks for catching this! YAML gives int64/uint64 for whole numbers, so the old .(int) check never matched and every YAML strategy limit was ignored. The fix is right in principle, but now that the value gets through, a bad value can crash the agent.
🔴 A negative limit panics at query time
coerceToInt accepts any number, and nothing checks the schema's minimum: 1 at runtime. cmp.Or(cfg.Limit, 5) only replaces 0, so -1 reaches BM25Strategy.Query and fails there:
runtime error: slice bounds out of range [:-1] // pkg/rag/strategy/bm25.go:355
Before this PR the same config was harmless: the limit was ignored and the default of 5 applied. Notes:
- It triggers even when nothing matches (
len(scores) > -1is always true), so any query panics as long as the store holds documents. - The vector stores slice the same way (
chunked_embeddings_database.go:200,semantic_embeddings_database.go:208,memory_database.go:265). - With several strategies, each query runs in a goroutine with no
recover(manager.go:347), so the panic takes down the whole process.
I reproduced it with this PR applied:
| Input | Path | Result |
|---|---|---|
limit: 10 |
YAML | 10 ✅ |
limit: -1 |
YAML | -1 → panic |
limit: 18446744073709551615 |
YAML | -1 (uint64 wraps) → panic |
limit: 2.5 |
YAML | 2, silently rounded down |
limit: "10" / limit: ten |
YAML | 0, silently dropped (removed from Params too) |
"limit": -3 |
JSON | -3 → panic |
"limit": "10" |
JSON | 0, but left in Params (differs from YAML) |
Suggested change
The schema already declares type: integer, minimum: 1, so I don't think negatives or fractions need support. Could you:
- Make a strategy
limitthat isn't a whole number ≥ 1 a load-time error, using one helper shared byUnmarshalYAMLandUnmarshalJSON(types.go:2332still uses its own.(float64)check). Or checkLimitonce invalidate.go, the wayMaxOutputBytes/IndexingTimeoutare checked already. - Add test cases for
-1,0,2.5,"x"and a value that overflows. The current test only covers a valid limit and an unset one, so it passes even with the panic above.
Not blocking: same gap elsewhere (OK as a follow-up)
Other RAG strategy settings declare minimum: 1 in the schema but aren't checked at runtime. I read the code but didn't run these:
embedding_batch_size: 0→ endless loop (start += 0,embed.go:125)max_embedding_concurrency: 0/max_indexing_concurrency: 0→errgroup.SetLimit(0), so everyg.Goblocks forever (embed.go:122,vector_store.go:332)
GetParam and coerceToInt both carry //nolint:gosec // bounds enforced upstream, but nothing upstream enforces them. I'm happy to open a separate issue for adding RAG range checks to validate.go.
Now that a YAML strategy limit is read, a bad value reaches the retrieval code, where a negative limit panics when the results are sliced to it. The schema already declares `type: integer, minimum: 1`. parseRAGStrategyLimit checks the value, and UnmarshalYAML and UnmarshalJSON both call it. It returns an error for 0, a negative number, a fraction, a string or a number that does not fit an int, so the two decoders treat a bad limit the same way. An empty limit stays unset. An older config version can pass a bad value into UnmarshalJSON when it is upgraded. Return that error from the upgrade instead of panicking in CloneThroughJSON. Signed-off-by: spicysharkco <321699413+spicysharkco@users.noreply.github.com>
|
Thanks for the review. A |
The version 15 upgrade moved from latest/parse.go to legacy/register.go, and older versions now load only with WithVersions(legacy.Register). Return the clone error from the new upgradeToLatest, and register the legacy versions in the test that loads a version 15 config. Signed-off-by: spicysharkco <321699413+spicysharkco@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
JSON limit validation loses numeric precision, rejecting the valid 64-bit maximum and accepting fractional values, breaking round-trips and version-15 migration for the maximum value.
1 open issue
What changed in this PR
Fixes RAG strategy limits being ignored when loaded from YAML, and adds shared validation for YAML and JSON configuration.
Changes:
- Parses integer strategy limits and removes
limitfrom strategy parameters. - Rejects invalid limits and returns migration errors instead of panicking.
- Adds regression tests for limit parsing and older-version migration.
| File | Description |
|---|---|
pkg/config/types/clone.go |
Adds an error-returning JSON clone helper. |
pkg/config/types_test.go |
Tests valid and invalid limits and migration error handling. |
pkg/config/legacy/register.go |
Propagates errors when upgrading to the latest schema. |
pkg/config/latest/types.go |
Shares limit parsing and validation between YAML and JSON. |
Review effort: Balanced
| return int(val), nil | ||
| } | ||
| case float64: | ||
| if val == math.Trunc(val) && val >= 1 && val < math.MaxInt { |
There was a problem hiding this comment.
Fixed in 39fb24c. UnmarshalJSON now reads limit from its raw token and parses it with strconv.ParseInt, so the largest int loads exactly, also after a JSON round trip and through the version 15 upgrade. The other params still decode as before.
1.0000000000000001 is now a load error. A decimal limit is rejected in both formats, 1.0 included: YAML has already rounded the value by the time UnmarshalYAML sees it, so only the integer form can be trusted. The error now says "must be an integer" instead of "a whole number". New rows cover the largest int in YAML, in JSON and after a JSON round trip, 1.0 in YAML and JSON, 1.0000000000000001 in JSON, !!int 0, and a version 15 config with the largest int.
aheritier
left a comment
There was a problem hiding this comment.
The shared limit validation addresses the earlier negative-limit panic, and the legacy upgrade propagates validation errors instead of panicking. Please address the open precision issue and add regression tests, then mark this PR ready for review.
Prior threads: 4543894038 — [blocking] High confidence; unresolved and not outdated. Exact JSON numeric validation remains broken: the valid 64-bit maximum is rejected, while 1.0000000000000001 is accepted as 1. Preserve the exact numeric token and test both cases, including round-trip and legacy migration.
UnmarshalJSON decoded the limit as a float64, which rounds. The largest int became 2^63 and was rejected, so it did not survive a JSON round trip or a version 15 upgrade, while 1.0000000000000001 became 1 and loaded. Read the limit from its raw JSON token and accept only an integer from 1 to the largest int. A decoded YAML decimal may be rounded the same way, so a decimal limit is now an error in both formats. The other strategy params decode as before. Signed-off-by: spicysharkco <321699413+spicysharkco@users.noreply.github.com>
aheritier
left a comment
There was a problem hiding this comment.
The shared parser preserves exact integer limits, rejects invalid values, and propagates legacy migration errors. Regression coverage exercises YAML/JSON parsing, maximum-int round trips, and version-15 migration.
Prior threads: 4543894038 — addressed; raw JSON token parsing and regression cases cover the maximum-int and rounded-fraction defects.

A RAG strategy with an integer
limitignores it and returns the default of 5 results:limit: 10in the bm25 example indocs/tools/rag/index.md, or 20 and 15 inexamples/rag/hybrid.yaml.agent-schema.jsondocumentslimitas the max results from a strategy, defaulting to 5 only when unset.This affects configs with no
version:or the current one. Older versions go through a JSON conversion that already reads the value, but as a float64, which rounds a large limit.The YAML decoder returns a positive integer as a uint64, and
UnmarshalYAMLonly accepts an int, soLimitstays 0 and the value is left inParams(types.go L2264-L2267). Same class as #4596 for the DMRcontext_size.Read it with
parseRAGStrategyLimit, whichUnmarshalYAMLandUnmarshalJSONboth call.UnmarshalJSONpasses it the raw token, so a limit up to the largest int is read exactly.limitalso no longer shows up inParams.Now that the value is read, a negative limit panics when the results are sliced to it.
agent-schema.jsondeclareslimitastype: integer, minimum: 1, so the helper returns a load error naminglimitfor0, a negative number, a decimal, a string or a number that does not fit an int. A decimal is rejected even when whole, such as1.0, because a decoded YAML decimal may already be rounded:1.0000000000000001reads as1.limit: 0used to mean the default of 5; leavelimitout for that. An emptylimit:is still unset. The version 15 to latest upgrade goes throughCloneThroughJSON, which panics on an error, so it uses a newTryCloneThroughJSONthat returns it.TestRAGStrategyConfig_Unmarshal_Limitfails on main (expected: 10,actual : 0) and passes with the change. Its new rows reject bad limits in YAML (-1,0,!!int 0,2.5,1.0,x,"10", an overflow) and in JSON (a negative number,2.5,1.0,1.0000000000000001, a string, an overflow), and load the largest int from YAML and JSON, also after a JSON round trip.TestLoad_RAGStrategyLimit_OlderVersionloads a version 15 config withlimit: 0, which fails, and with the largest int, which is kept: