Repository navigation
fix(orchestrator): repair failed artifact sub-queries and stop inventing figures - #90
Merged
Merged
Conversation
…ing figures The analytical sub-queries behind /v1/artifact are written by an LLM that sees neither the semantic layer nor the server version. On ClickHouse 22.1 two of three failed in the first GONAC artifact: an alias named after its column (SUM(facturado) AS facturado) turned a later SUM(facturado) into a sum of a sum, and a ratio of two window functions in one SELECT is an unknown identifier. Nothing retried them. - Each failed sub-query gets one repair attempt with its SQL and the database error, run on the request thread so the token tracker counts it. Where the error text does not name its fix, a hint explains it: without the hint the window case failed again; with it, 6 of 6 repairs ran on the real server. The user's sql_override is never rewritten. - The ClickHouse dialect note carries both rules. - With no data at all, the document said to use "realistic benchmarks" and "indicative estimates", so it printed invented figures. It now says the data could not be retrieved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…epair it run_query_pipeline reports a failure in `answer` with rows=None instead of raising, and create_artifact only caught exceptions. When the chat's own SQL failed on the database, the main dataset came back empty with no error and the sub-queries were derived from the statement that had just failed. Seen on the GONAC PoC: the main SQL carried SUM(facturado) AS facturado next to SUM(facturado) / SUM(objetivo) and ClickHouse 22.1 rejected it. The failure is now read from the pipeline result. A failed main SQL gets the same single repair a sub-query gets; if it cannot be repaired, the main dataset carries the error and no sub-query is built on it. A query that ran and matched nothing is not a failure. The user's sql_override is still never rewritten. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
The analytical sub-queries behind
/v1/artifactare written by an LLM that sees only the main SQL and a one-line dialect note: no semantic layer, no server version. In the first GONAC artifact on ClickHouse 22.1, two of three failed and came back empty:SUM("facturado") AS facturado, SUM("facturado") / nullIf(SUM("objetivo"), 0): the alias shadows the column, so the secondSUM(facturado)is a sum of a sum (ILLEGAL_AGGREGATION).SUM(SUM(x)) OVER (...) / nullIf(SUM(SUM(y)) OVER (...), 0): a window result cannot be used inside another expression of the same SELECT, not even through its alias (UNKNOWN_IDENTIFIER).Nothing retried them. Separately, when no dataset has rows, the document prompt asked for "realistic benchmarks" and "indicative estimates", so the document printed invented figures that read like real ones.
Change
ContextVarthe pool threads do not inherit. The user'ssql_overrideis never rewritten._ERROR_HINTSexplains the errors whose text does not name the fix.run_query_pipelinereports a failure inanswerwithrows=Noneinstead of raising, so the artifact used to show an empty main dataset with no error and derive the sub-queries from the failed statement. Seen live on the PoC: the chat's own SQL carriedSUM(facturado) AS facturadonext toSUM(facturado) / SUM(objetivo). If the repair fails too, the main dataset carries the error and no sub-query is built on it. A query that matched nothing is not a failure.Verification
tests/unit/test_artifact_sub_sql_repair.py: 15 tests. The first 10 fail againstmain, and 3 of the 4 main-SQL tests fail against the first commit (the fourth guards the no-rows case). Together withtest_artifact_env.py: 22 passed. Ruff clean.Out of scope
The main chat SQL generator (
ask-sql-generation/.../prompts/clickhouse.py) lacks the same two rules and does not retry on execution errors either (BACKLOG group P). Changing it affects every question and needs a benchmark re-run.🤖 Generated with Claude Code