Skip to content

fix: sql explain / AI consent and output fixes - #169

Merged
alexshapalov merged 6 commits into
mainfrom
fix/sql-explain-ai
Nov 23, 2026
Merged

alexshapalov merged 6 commits into
mainfrom
fix/sql-explain-ai

Conversation

@alexshapalov

Copy link
Copy Markdown
Contributor

Fixes #156
Fixes #143
Fixes #140
Fixes #139
Fixes #141

One commit per issue, each with a regression test and a CHANGELOG [Unreleased] → Fixed line. The --json Context schema (internal/model, schema/*.json) is unchanged.

What changed

How verified

🤖 Generated with Claude Code

alexshapalov and others added 6 commits November 23, 2026 14:02
…#141)

HasPlaceholders only tracked single-quote state, so a "$1" inside a
dollar-quoted string constant ($$ … $$ or $tag$ … $tag$) was reported as a
parameter placeholder and `sql explain` printed the generic-plan
"parameter values unknown" caveat for a query that has no parameters.

The scanner now recognises a dollar-quote opening delimiter and skips to
its matching close (or to the end of the query when unterminated).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The --json branch of runSQLExplain printed the raw plan and returned
before the `if llm != nil` block, so --json --ai resolved the model,
asked for consent to send the SQL, then silently dropped the AI
explanation. Appending prose to --json output would break parsers, so
the combination is now rejected up front as a usage error (exit 64),
before a model is resolved, consent is requested, or a connection made.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…gs (#140)

printAISection was written for `pgbot explain`/`ask` and hard-coded
"The findings above are complete/authoritative…". `sql explain --ai`
reused it, so a command that computes no findings told the reader the
findings were complete — including on model error, when only a plan had
been printed.

printAISection now takes what the section explains (findings or a query
plan) and words the fallback note and footer accordingly; the findings
commands keep their existing text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
confirmDisclosureOf returned true for `yes || !isInteractive()`, so in
CI, pipes, containers and agent shells the disclosure line was printed
and the payload sent without any confirmation or --yes. For
`sql explain --ai` that payload is the user's SQL text, which can carry
literal values; `explain` and `ask` share the same gate.

Only --yes now short-circuits the prompt for a remote endpoint. With no
terminal to answer it, the command refuses before connecting and tells
the user to re-run with --yes. Local endpoints keep auto-consent since
nothing leaves the machine. isInteractive becomes a variable so the
regression test can pin it; the piped `sql explain --ai` help example
now shows --yes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
)

runExplain resolved the model first and returned ai.Resolve's error
before connecting, so with no key `pgbot explain` printed nothing and
exited 3 (the connection/execution-failure code) — contrary to the
README's promise that the deterministic report still stands when the
key is unset. Only a failed model call after rendering degraded
gracefully.

A missing model is now not a command error: resolveExplainModel returns
the resolve error as the AI section's "unavailable" reason, the
inspection runs and renders as usual, and the process exits on the
findings' code. Consent is still asked before connecting when a model
is configured, and a refused consent still aborts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alexshapalov
alexshapalov merged commit 10b7acc into main Nov 23, 2026
21 checks passed
@alexshapalov
alexshapalov deleted the fix/sql-explain-ai branch November 23, 2026 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment