Repository navigation
Add querypilot trace and hooks: audit, guard and diagnose agent SQL tool calls - #30
Merged
Merged
Conversation
…ool calls trace import reads Claude Code transcripts into the audit log, pairing each query tool call with its result (SQL, outcome, failure class, latency; never result rows). hooks install logs calls live, and with --guard a PreToolUse hook validates SQL against schemas cached from the agent's own schema calls, denies unknown columns with did-you-mean feedback, catches epoch-ms arithmetic on DATE columns, and enforces a row cap. trace doctor reports failure classes, retry chains, latency, a guard replay, and tool/prompt/data fixes, and exports recovered chains for eval replay. Palantir Foundry's SQL tool is mapped by default; any tool maps by name and SQL field. Includes the first redacted report over the Foundry history. 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.
What
Adds
querypilot traceandquerypilot hooks, which observe and improve the SQL that agents run through MCP and connector tools QueryPilot can't proxy. The first target is Palantir Foundry'smcp__palantir-mcp__run_sql_query_on_foundry_datasetin Claude Code. Any other tool can be mapped by name and SQL input field:--tool NAME=FIELD[:DIALECT[:ROW_LIMIT_FIELD]].Capture.
trace importpairs eachtool_usein a Claude Code transcript with itstool_resultby id. It writes the SQL, outcome, failure class, latency, session and order to the JSONL audit log, and never writes result rows.hooks installadds PostToolUse and PostToolUseFailure hooks that log calls live. Hooks never fail a tool call.Guard (opt-in,
--guard). A PreToolUse hook validates the SQL withSQLValidator(Spark dialect) against a schema cache. The cache is built fromget_foundry_dataset_schemaresults and single-datasetSELECT *results. The hook denies unknown columns with "did you mean" text, blocks epoch-ms arithmetic on DATE columns, and enforces--max-rows.--require-schemaalso blocks queries on datasets whose schema hasn't been fetched yet.Doctor.
trace doctorreports:hints:anddescriptions:blocks.--export-replay/--suiteturn recovered retry chains into aneval replayregression suite.QueryAuditRecordgains optionaltool_name,tool_call_idanddetailsfields.Results on the real Foundry history
The report is in
docs/trace-doctor-foundry-2026-10-21.md. It is redacted: dataset RIDs and paths become labels, literals become'?', and no result rows appear.docs/trace.mdcompares these numbers with the earlier quick scan.Testing
tests/test_trace.py(41 tests) covers the classifier, the transcript parser, the schema cache and the guard. It also covers the doctor and the replay export, and checks the hooks by replaying a synthetic sample transcript through them, including a subprocess round trip.ruff check src tests(0.17.1) is clean.Known gaps
alias.field) and backticked column names that contain dots as table qualifiers. The guard skips those queries instead of judging them.🤖 Generated with Claude Code