Skip to content

feat(vnext): authenticate CTE main query sites#190

Merged
Light2Dark merged 1 commit into
dev-refactorfrom
codex/vnext-cte-main-query
Jul 25, 2026
Merged

feat(vnext): authenticate CTE main query sites#190
Light2Dark merged 1 commit into
dev-refactorfrom
codex/vnext-cte-main-query

Conversation

@Light2Dark

@Light2Dark Light2Dark commented Jul 25, 2026

Copy link
Copy Markdown
Member

Summary

  • authenticate bounded CTE main-query entrypoints before the relation query-site recognizer consumes them
  • unify source, statement-index, slot, layout, and built-in runtime provenance so mixed or stale evidence fails closed
  • keep the low-level query-site recognizer independent of CTE and aggregate runtime modules through a one-way orchestration layer
  • preserve independent old/new statement contexts when incremental indexes reuse immutable slot identities
  • validate real ready paths in the CTE/query-site benchmarks

Correctness and trust boundaries

  • package-created sources, statement indexes, exact slots, CTE layouts, and coherent dialect runtimes are authenticated by identity
  • each statement index privately owns its exact analysis text, frozen lexical-profile scalar snapshot, and bounded slot membership
  • foreign sources, indexes, slots, profiles, layouts, mixed runtimes, mutable structural copies, and hostile proxies return unavailable
  • incomplete and invalid CTE headers remain fail-closed while valid main queries work across PostgreSQL, DuckDB, BigQuery, and Dremio

Verification

  • 1,671 tests passed and 1 expected failure remained expected
  • changed coverage: 96.99% statements, 95.95% branches, 100% functions, 96.96% lines
  • all five TypeScript configurations passed
  • oxlint passed with zero warnings or errors
  • test-integrity and diff checks passed
  • exact tarball package smoke, SSR import, strict CSP, worker placement, and bundle budgets passed
  • Chromium browser suite passed: 4 files / 7 tests
  • statement-index, CTE-layout, and query-site benchmarks passed
  • exact SHA c096a16 received three independent adversarial approvals after five review/fix loops

Part of #169.


Summary by cubic

Authenticate CTE main‑query entrypoints and only recognize relation query sites when the source, statement index, slot, and dialect all match. Ambiguous evidence fails closed, keeping the core recognizer independent; part of #169.

  • New Features

    • Authenticated CTE layouts: analyzeSqlCteLayout(source, index, slot, dialect) records provenance and binds main‑query entrypoints to the exact source/index/slot/dialect; resolveAuthenticatedSqlCteEntrypoints(...) exposes them safely.
    • Orchestration: recognizeSqlRelationQuerySiteWithCteLayout(...) validates runtime/source/slot via isSqlRelationDialectRuntime, isSqlSourceSnapshot, and isSqlStatementSlotSnapshot, then feeds authenticated entrypoints into the query‑site recognizer; runtimes are registered with registerSqlRelationDialectRuntime.
    • Entrypoint‑aware query‑site: recognizeSqlRelationQuerySiteWithEntrypoints(...) matches SELECTs at the exact depth/offset for nested CTEs.
    • Statement index provenance: buildSqlStatementIndex snapshots the lexical profile; isExactSqlStatementSlotSnapshot(For) ensures slots belong to the index and text/profile; updateSqlStatementIndex rebuilds when text or lexical profile changes.
  • Migration

    • Pass the statement index into analyzeSqlCteLayout (new signature: (source, index, slot, dialect)).
    • Use recognizeSqlRelationQuerySiteWithCteLayout when combining CTE layout with relation query‑site; keep recognizeSqlRelationQuerySite for non‑CTE cases.
    • Use built‑in registered runtimes from relation-dialect and authentic sources/slots produced by our builders.

Written for commit 9ca185f. Summary will update on new commits.

Review in cubic

@github-actions

github-actions Bot commented Jul 25, 2026

Copy link
Copy Markdown

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 96.96% (🎯 91%) 2043 / 2107
🔵 Statements 96.99% (🎯 91%) 2066 / 2130
🔵 Functions 100% (🎯 90%) 188 / 188
🔵 Branches 95.95% (🎯 85%) 1900 / 1980
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/vnext/cte-layout.ts 96.58% 95.25% 100% 96.55% 351, 506, 520-522, 736, 831-833, 876, 1279, 1325, 1544-1547, 1717, 1765-1767
src/vnext/query-site.ts 96.55% 95.25% 100% 96.54% 391, 544-545, 571, 658, 777, 799, 863, 949-952, 1004, 1007, 1012-1015, 1113, 1205, 1291-1294, 1332, 1340-1343
src/vnext/relation-dialect.ts 96.72% 96.47% 100% 96.63% 143, 294, 322, 379, 385, 424, 631, 732, 746, 808, 821, 827, 835, 880, 1037, 1047, 1085, 1348
src/vnext/relation-query-site.ts 100% 100% 100% 100%
src/vnext/relation-runtime-auth.ts 100% 100% 100% 100%
src/vnext/source.ts 99.09% 98.57% 100% 99.07% 191-194
src/vnext/statement-index.ts 98.12% 96.56% 100% 98.12% 264, 660, 850, 871, 984, 1039
Generated in workflow #732 for commit 9ca185f by the Vitest Coverage Report Action

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR strengthens vnext SQL “query-site” recognition by authenticating the provenance of CTE main-query entrypoints (and related inputs) before they can influence relation query-site detection, while keeping the low-level recognizer independent of CTE/layout modules.

Changes:

  • Adds snapshot/authentication mechanisms for SQL sources, statement slots/indexes, and relation dialect runtimes (WeakSet/WeakMap-based provenance).
  • Extends the query-site recognizer to optionally accept authenticated entrypoints, and introduces a one-way orchestration layer to combine CTE layout + query-site recognition safely.
  • Updates CTE layout analysis to record provenance and to resolve authenticated entrypoints; expands tests and benchmarks to validate fail-closed behavior and ready paths.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/vnext/statement-index.ts Tracks provenance and authenticates statement slot/index snapshots; hardens incremental update behavior when metadata/profile mismatches.
src/vnext/source.ts Introduces authentication for package-created SqlSourceSnapshot objects.
src/vnext/relation-runtime-auth.ts Adds runtime registration/authentication for coherent relation dialect runtime aggregates.
src/vnext/relation-query-site.ts New orchestration entrypoint: authenticates runtime/source/slot, resolves authenticated CTE entrypoints, then delegates to the query-site recognizer.
src/vnext/relation-dialect.ts Registers built-in relation dialect runtimes so they can be authenticated downstream.
src/vnext/query-site.ts Adds entrypoint-aware internal recognizer and exports a new entrypoint-enabled API surface.
src/vnext/cte-layout.ts Records provenance for layouts built from authenticated inputs and exposes entrypoint-resolution that fails closed on mixed/stale evidence.
src/vnext/tests/statement-index.test.ts Adds tests for slot snapshot authentication behavior.
src/vnext/tests/source.test.ts Adds tests for source snapshot authentication behavior.
src/vnext/tests/relation-dialect.test.ts Adds tests ensuring only package-owned runtime aggregates authenticate.
src/vnext/tests/query-site.test.ts Adds comprehensive tests for authenticated CTE main-query entrypoints and fail-closed behavior across dialects and hostile inputs.
src/vnext/tests/query-site.bench.ts Adds benchmarks validating “ready” CTE paths and measuring double-scan orchestration cost.
src/vnext/tests/incremental-statement-index.test.ts Adds regression tests for rebuild conditions and validates provenance across reused slot identities.
src/vnext/tests/cte-layout.test.ts Updates fixtures for the new analyzeSqlCteLayout(source, index, slot, dialect) signature and adds cross-dialect coverage.
src/vnext/tests/cte-layout.bench.ts Updates benchmarks to pass the statement index into CTE layout analysis.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 15 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/vnext/cte-layout.ts">

<violation number="1" location="src/vnext/cte-layout.ts:1591">
P2: Hostile dialect proxies can throw after successful dialect validation because provenance reads `dialect.lexicalProfile` directly. Use the already-safe `validatedDialect.lexicalProfile` for the slot provenance check so this path remains fail-closed.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread src/vnext/cte-layout.ts Outdated
index,
slot,
source.analysisText,
dialect.lexicalProfile,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Hostile dialect proxies can throw after successful dialect validation because provenance reads dialect.lexicalProfile directly. Use the already-safe validatedDialect.lexicalProfile for the slot provenance check so this path remains fail-closed.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/vnext/cte-layout.ts, line 1591:

<comment>Hostile dialect proxies can throw after successful dialect validation because provenance reads `dialect.lexicalProfile` directly. Use the already-safe `validatedDialect.lexicalProfile` for the slot provenance check so this path remains fail-closed.</comment>

<file context>
@@ -1561,6 +1582,57 @@ export function analyzeSqlCteLayout(
+      index,
+      slot,
+      source.analysisText,
+      dialect.lexicalProfile,
+    )
+  ) {
</file context>
Suggested change
dialect.lexicalProfile,
validatedDialect.lexicalProfile,

@Light2Dark
Light2Dark force-pushed the codex/vnext-cte-main-query branch from c096a16 to 9ca185f Compare July 25, 2026 03:47
@Light2Dark
Light2Dark merged commit 1dc15d8 into dev-refactor Jul 25, 2026
9 checks passed
@Light2Dark
Light2Dark deleted the codex/vnext-cte-main-query branch July 25, 2026 03:50
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.

2 participants