Repository navigation
fix: stop rejecting valid NL-search SQL that contains the word error - #855
Merged
Merged
Conversation
Motivation: Natural language search returns a false "query is invalid" error for queries such as "Which pods are in error state", while unrelated queries succeed. The handler then renders this as an HTTP 500. Approach: ConvertTextToSQL (pkg/core/manager/ai/search.go) validated the LLM's raw response with IsInvalidQuery, which flagged the response as invalid whenever it contained the case-insensitive substring "error" anywhere in the text. A query about pod error state legitimately produces a generated SQL statement whose WHERE clause itself contains the word "error" (matching a Kubernetes pod status value), so this substring check rejected valid SQL as invalid. ConvertTextToSQL now uses "was a SELECT statement extracted from the response" as the sole validity signal, since the prompt (prompt.go, Text2sqlType) already instructs the model to always emit SQL starting with "select * from" and ending with ";", or the literal word "Error" when it cannot produce a query. The now-unused IsInvalidQuery helper and its test were removed since nothing else in the repo referenced it. Validation: - go build ./pkg/core/manager/ai/... passes. - go test ./pkg/core/manager/ai/... -run TestConvertTextToSQL -v passes. The new TestConvertTextToSQL/ValidQueryWithErrorAsValue case reproduces the reported bug: verified by stashing this change and re-running the same test, which fails with "query is invalid" on the old code and passes after this fix. - gofmt -l on all changed files reports no issues. - Could not run this repo's `make test` / `make lint` targets to completion: the local machine's disk is almost entirely full (roughly 300-900Mi free on a 228Gi volume), and pkg/core/manager/ai transitively depends on k8s.io/kubernetes/pkg/apiserver through pkg/kubernetes/registry, which this disk cannot fully compile with the repo's gcflags. This is a pre-existing local environment constraint unrelated to this change, not a failure of the change itself; the narrower go build/go test commands above did complete successfully against the real compiled package. Report: KusionStack#735 Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com> Assisted-by: claude-sonnet-5 (via Claude Code)
pujitha24
requested review from
adohe,
elliotxx,
ffforest and
ruquanzhao
as code owners
September 14, 2026 06:03
|
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
Contributor
Author
|
I have read the CLA Document and I hereby sign the CLA |
Contributor
Author
|
recheck |
1 similar comment
Contributor
Author
|
recheck |
Contributor
Author
|
This is rebased and the checks are green; if anything would make it easier to review, I am happy to adjust it. |
elliotxx
approved these changes
Oct 10, 2026
elliotxx
left a comment
Collaborator
There was a problem hiding this comment.
Nice catch, thanks for fixing this! 🙌 LGTM. CLA and license checks are green; the Check and Constraints workflows were waiting for approval, so I've kicked them off. Will merge once they pass.
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
What type of PR is this?
/kind bug
What this PR does / why we need it:
Natural language search returns a false "query is invalid" error (rendered as an HTTP 500) for queries like "Which pods are in error state", while other queries succeed.
The root cause is in
ConvertTextToSQL(pkg/core/manager/ai/search.go): it validated the LLM's raw response withIsInvalidQuery, which flagged the response as invalid whenever it contained the case-insensitive substring"error"anywhere in the text. A query about pod error state legitimately produces a generated SQL statement whoseWHEREclause itself contains the word "error" (matching a Kubernetes pod status value), so this substring check rejected perfectly valid SQL as invalid.ConvertTextToSQLnow uses "was aSELECTstatement extracted from the response" as the sole validity signal, since the prompt (prompt.go,Text2sqlType) already instructs the model to always emit SQL starting withselect * fromand ending with;, or the literal word"Error"when it cannot produce a query. The now-unusedIsInvalidQueryhelper and its test were removed since nothing else in the repo referenced it.Validation:
go build ./pkg/core/manager/ai/...passes.go test ./pkg/core/manager/ai/... -run TestConvertTextToSQL -vpasses. The newTestConvertTextToSQL/ValidQueryWithErrorAsValuecase reproduces the reported bug: confirmed by stashing this change and re-running the same test, which fails withquery is invalidon the old code and passes after this fix.gofmt -lon all changed files reports no issues.make test/make linttargets to completion: the local machine's disk is almost entirely full (roughly 300-900Mi free on a 228Gi volume), andpkg/core/manager/aitransitively depends onk8s.io/kubernetes/pkg/apiserverthroughpkg/kubernetes/registry, which this disk cannot fully compile with the repo'sgcflags. This is a pre-existing local environment constraint unrelated to this change, not a failure of the change itself — the narrowergo build/go testcommands above did complete successfully against the real compiled package. Happy to address anything the full CI run surfaces.Which issue(s) this PR fixes:
Fixes #735