Warn the model when SQL results are truncated by a LIMIT/TOP clause#863
Draft
Bl3f wants to merge 1 commit into
Draft
Warn the model when SQL results are truncated by a LIMIT/TOP clause#863Bl3f wants to merge 1 commit into
Bl3f wants to merge 1 commit into
Conversation
Contributor
🚀 Preview Deployment
Preview will be automatically removed when this PR is closed. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
When the agent writes a SQL query with a
LIMITclause (e.g.LIMIT 20), the warehouse returns exactly that many rows. Theexecute_sqltool output reportsrow_count = 20, and nothing signals to the model that the result was capped by its own SQL. The agent then confidently states things like "there are exactly 20 games added in 2026" when the true count is 509.The system already handles its own 40-row display cap correctly (it shows the true total plus a pagination hint), but it had no way to distinguish a SQL-imposed
LIMITfrom a complete result set.Fix
Detect a row limit imposed by the outermost query and warn the model when the returned row count hits that limit.
detectQueryRowLimit()insql-filter.tsparses the outermostLIMIT n,LIMIT offset, n,LIMIT n OFFSET m,TOP n/TOP (n)(ignoringTOP n PERCENT), andFETCH FIRST n ROWS ONLY. It masks string literals and subquery parentheses so it only sees the outer query's limit.execute_sqlnow setsapplied_limiton its output when a top-level limit is detected.ExecuteSqlOutputrenders a clear warning to the model whenrow_count >= applied_limit: the count reflects the LIMIT, not the total, and the model should run aCOUNT(*)query without a LIMIT to get the true total. Therow_count >= applied_limitguard means aggregates over limited subqueries (e.g.SELECT count(*) FROM (... LIMIT 20)) do not produce false warnings.SQL Query Rulessystem-prompt rule reinforcing that a LIMIT/TOP clause caps returned rows, not existing rows.Model-facing output (reproducing the reported scenario)
For
... ORDER BY total_downloads DESC LIMIT 20returning 20 rows, the model now sees:limit_warning_demo.log
Testing
npm run -w @nao/backend test -- sql-filter execute-sql— 30 tests pass (12 new detection cases + 2 new output cases).npm run lint— passes across backend, frontend, shared.Pre-existing, unrelated failures in
system-prompt.test.ts(timezone),sqlite.test.ts(native bindings), andcompaction.test.tswere confirmed to fail on the clean base as well.To show artifacts inline, enable in settings.