Skip to content

Validate declared SQL result cardinality without truncating to fit - #355

Draft
seyoung4503 wants to merge 6 commits into
fix/integrated-output-contractfrom
feat/sql-result-cardinality
Draft

seyoung4503 wants to merge 6 commits into
fix/integrated-output-contractfrom
feat/sql-result-cardinality

Conversation

@seyoung4503

@seyoung4503 seyoung4503 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

A final aggregate request could return12monthly detail rows while the existing output-column check still passed. Extend RunSQL's existing output validation with optional expected_rows: an exact nonnegative integer cardinality contract for the executed result. A mismatch returns an auditable tool error while preserving the actual rows and executed SQL; it never adds LIMIT or truncates results to fit the expectation.

Invalid types (including bool/null/floats), negative counts, and expectations larger than the fetch budget are rejected before DB execution. Validation uses fetched rows plus the existing lookahead before fetch/display slicing, so50displayed rows are not mistaken for the complete result. SQL-only contracts remain unsupported in bound-metric mode. The original successful row header and executed-SQL footer are retained for provenance; contract failures cannot be accepted as successful final SQL by the evaluation harness.

Reuse: existing RunSQL execution/safety, expected_columns, audit, rendering and provenance paths; no new production function or alternate SQL executor. Product net+36lines across RunSQL/bound_metric; no env/config/deployment changes. BASE_INSTRUCTIONS is unchanged; the tool schema documents the new parameter. The contract is self-declared and validates only executed-result cardinality, not semantic intent or uncapped source size.

Validation:364root tests passed,1skipped.16new tests cover actual MCP stdio success/failure,0rows, one aggregate versus multiple rows, exact-budget results, lookahead truncation, display truncation, malformed declarations, insufficient fetch budget, audit preservation and rejection by final-SQL provenance checks. Existing Oracle metric test now also rejects expected_rows. Ruff and diff pass; no CODE MAP generator exists.

Selected failed-case probe (Astra/high, frozen79b0874):bird146 remains official0/1. The model used expected_rows=1 and returned one final aggregate row instead of12monthly rows; its relationship endpoint selection remains wrong. No cardinality error occurred during this model attempt, so do not claim an observed model repair loop. A separate read-only actual-MCP replay with the declared1row target rejects the old12-rowSQL and accepts the new1-rowSQL. The replay is not model inference or a benchmark score. Original user prompt bytes match; no prior answer/reference/analyst SQL was supplied to the model.

Evidence:bench/accuracy/mcp-review-results/output-row-contract/ contains17verified hashes, full traces/SQL/CSV, paired comparison, official score and the offline MCP replay/script. Keep draft: no accuracy increase or full95% result is established. Broad BIRD evaluation remains frozen on0c79539 and is not restarted.

Principle Verdict Evidence
Reuse O Existing execution, contracts, audit, rendering and lookahead;0new production functions; net+36lines
Cause/scope O Declared output cardinality mismatch only; no semantic guessing or SQL rewriting
Logging O Invalid declaration/budget and contract mismatch warning; successful count info with scope
Errors O Invalid inputs never execute; mismatch returns explicit auditable is_error; DB exceptions unchanged
Environment N/A No env reads/settings added
Issue comments O No issue-number comments
Verification O 364passed/1skipped, Ruff/diff, actual MCP replay,17archive hashes; no CODE MAP generator
Authority O Draft PR only; no merge, paid API use or live-job restart
Consumers O MCP definition receipt→RunSQL→existing safety/execution→contract check→rendered success/error
Sync/async O Same async execution path; no second engine or blocking path added
Constants/deployment O New request field only; no operational constants or deployment settings

Follow-up failed-case probes on frozen18170e6 (no additional code/prompt edits): bird273 remains0/1; newly observed projection failures bird149/bird163 score1/2, with only bird163 repaired. All original prompt hashes match their baselines. bird273 explicitly declared the wrong1-row Peter/John shape, which passed the contract: this is direct evidence that self-declared contracts do not establish intended semantics. bird149 still adds customer IDs to the three correct differences; bird163 now returns only both tied team names. No claim of causal attribution to expected_rows or full accuracy. Full traces, official grades, paired HTML/JSON and39verified artifact hashes are archived in grouped-row-contract and new-projection-failures. Product code unchanged; prior364passed/1skipped validation remains applicable. Artifact hashes and staged whitespace checks passed; no merge.

Interpretation caveat: bird149 explicitly says to identify the customers before asking for differences. Its native official reference contains only differences, so the extra IDs explain the grade, but this is not established as a deterministic SQL-engine defect. Do not force-remove identifying columns in production to fit this reference. Latest external overlapping live BIRD checkpoint006 is176/182 with the same6failures as005, not a500-case score; parent inference continues unchanged.

New failed-case generalization probe: bird278 from live-run checkpoint008 improves0→1 on the integrated candidate0d7be046, with no further code/prompt edit. Original code selects the correct two tied drivers and time23.633 but projects a float from milliseconds/1000; native BIRD reference projects stored duration text. Fresh candidate SQL retains milliseconds for minimum/tie selection and projects duration unchanged, passing the unchanged official evaluator. Original input prompt bytes are identical; no prior SQL, reference, grade or offline diagnosis supplied. Multiple previous changes are integrated, so this is not isolated attribution to expected_rows. Evidence: mcp-review-results/duration-output contains full trace/SQL/CSV, official score, paired comparison, native offline type audit and verified hashes. The parent checkpoint remains230/239, not replaced by this selected repair and not a500-case accuracy claim.

Artifact-only verification: existing runner/scorer/comparator reused, no product function/env/async/deployment changes; prompt/event/archive hashes and staged diff check pass. Root tests are unchanged and not repeated for evidence-only files; no merge or live-run restart.

Recovered historical failures follow-up: bird003/bird007 were the two failed records recovered from an interrupted Sol run and not yet tested with integrated Astra. Fresh Astra/high on82f2038, without further code/prompt edits or prior/reference feedback, scores1/2. bird007 now multiplies the numerator by100 before division and matches the native reference70.15113350125945; old divide-then-multiply produced70.15113350125944. No tolerance or grader change. bird003 now scopes Orange County to both numerator and denominator, but COUNT(School) excludes2null-name Elementary records already returned by its diagnostics, so remains wrong. The tool did not hide those records. Evidence: mcp-review-results/recovered-failures-astra includes all traces, scores, paired comparisons, native audit and verified hashes. Historical exact prompts were not archived, so no byte-identical baseline-prompt claim. Model and product revision both changed; do not attribute the selected repair to one modification or claim full accuracy.

Artifact-only checks: product net0lines/new functions0, existing runner/scorer/comparison reused, logs and failures preserved, source DB/question and new prompt/event/archive hashes verified, staged whitespace check passed. No runtime/env/exception/async/deployment changes; no new root tests or merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant