Skip to content

fix(rag): honor a YAML integer strategy limit - #4646

Merged
aheritier merged 4 commits into
docker:mainfrom
spicysharkco:fix/rag-strategy-yaml-limit
Nov 12, 2026
Merged

aheritier merged 4 commits into
docker:mainfrom
spicysharkco:fix/rag-strategy-yaml-limit

Conversation

@spicysharkco

@spicysharkco spicysharkco commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

A RAG strategy with an integer limit ignores it and returns the default of 5 results: limit: 10 in the bm25 example in docs/tools/rag/index.md, or 20 and 15 in examples/rag/hybrid.yaml. agent-schema.json documents limit as 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 UnmarshalYAML only accepts an int, so Limit stays 0 and the value is left in Params (types.go L2264-L2267). Same class as #4596 for the DMR context_size.

Read it with parseRAGStrategyLimit, which UnmarshalYAML and UnmarshalJSON both call. UnmarshalJSON passes it the raw token, so a limit up to the largest int is read exactly. limit also no longer shows up in Params.

Now that the value is read, a negative limit panics when the results are sliced to it. agent-schema.json declares limit as type: integer, minimum: 1, so the helper returns a load error naming limit for 0, a negative number, a decimal, a string or a number that does not fit an int. A decimal is rejected even when whole, such as 1.0, because a decoded YAML decimal may already be rounded: 1.0000000000000001 reads as 1. limit: 0 used to mean the default of 5; leave limit out for that. An empty limit: is still unset. The version 15 to latest upgrade goes through CloneThroughJSON, which panics on an error, so it uses a new TryCloneThroughJSON that returns it.

TestRAGStrategyConfig_Unmarshal_Limit fails 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_OlderVersion loads a version 15 config with limit: 0, which fails, and with the largest int, which is kept:

go test ./pkg/config -run 'TestRAGStrategyConfig_Unmarshal_Limit|TestLoad_RAGStrategyLimit_OlderVersion'

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>
@spicysharkco
spicysharkco requested a review from a team as a code owner October 19, 2026 16:46
@aheritier aheritier added area/config For configuration parsing, YAML, environment variables kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Oct 19, 2026

@aheritier aheritier left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) > -1 is 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:

  1. Make a strategy limit that isn't a whole number ≥ 1 a load-time error, using one helper shared by UnmarshalYAML and UnmarshalJSON (types.go:2332 still uses its own .(float64) check). Or check Limit once in validate.go, the way MaxOutputBytes / IndexingTimeout are checked already.
  2. 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 every g.Go blocks 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.

@aheritier
aheritier marked this pull request as draft October 19, 2026 19:05
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>
@spicysharkco

Copy link
Copy Markdown
Contributor Author

Thanks for the review. A limit of 0, a negative number, a fraction, a string or a number too large for an int is now a load error, from one helper that UnmarshalYAML and UnmarshalJSON both call. The YAML tests cover -1, 0, 2.5, x, "10" and an overflow, the JSON tests a negative number, a fraction, a string and an overflow, and a version 15 config with limit: 0 fails to load too. The other minimum: 1 settings are left for the follow-up you mentioned.

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>
@aheritier
aheritier marked this pull request as ready for review November 11, 2026 22:23
@aheritier
aheritier requested review from aheritier and a balanced review from Copilot November 11, 2026 22:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 limit from 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

Comment thread pkg/config/latest/types.go Outdated
return int(val), nil
}
case float64:
if val == math.Trunc(val) && val >= 1 && val < math.MaxInt {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 aheritier left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@aheritier
aheritier marked this pull request as draft November 11, 2026 23:08
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>
@spicysharkco
spicysharkco marked this pull request as ready for review November 12, 2026 00:47
@aheritier
aheritier self-requested a review November 12, 2026 05:40

@aheritier aheritier left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@aheritier
aheritier added this pull request to the merge queue Nov 12, 2026
Merged via the queue into docker:main with commit 00725ed Nov 12, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/config For configuration parsing, YAML, environment variables kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants