Skip to content

fix(plan): pass plan snapshots and deployability index to audit-only stage - #6258

Open
codewithsruthi wants to merge 1 commit into
SQLMesh:mainfrom
codewithsruthi:fix/6256-audit-only-parents
Open

codewithsruthi wants to merge 1 commit into
SQLMesh:mainfrom
codewithsruthi:fix/6256-audit-only-parents

Conversation

@codewithsruthi

@codewithsruthi codewithsruthi commented Nov 25, 2026 •

Copy link
Copy Markdown

Description

Fixes #6256

Problem & Root Cause

  1. PlanStagesBuilder.build() constructed AuditOnlyRunStage with only audit_only_snapshots, omitting all_snapshots and deployability_index, and could include non-representative dev METADATA snapshots whose __dev table does not exist yet (which BackfillStage creates and audits right after).
  2. BuiltInPlanEvaluator.visit_audit_only_run_stage() built a Scheduler from only the audited snapshots, causing parent models referenced in audits to fall back to environment views — failing on newly added parents or silently auditing stale views after a partially failed prod plan, failing on dev breaking/preview parents and embedded parents, and losing start_date inheritance.
  3. Scheduler._run_or_audit() refreshed snapshot intervals and mutated self.snapshots[s_id].remove_interval(...) in place on shared Snapshot objects held by subsequent stages (BackfillStage) and CachingStateSync, which could wipe intervals on audited incremental models when AuditOnlyRunStage and BackfillStage ran in the same plan. Additionally, Scheduler.run_merged_intervals() still computed snapshots_to_create (and could emit CreateNode) when audit_only=True.

Changes

  • sqlmesh/core/plan/stages.py:
    • Added all_snapshots: t.Dict[str, Snapshot] and deployability_index: DeployabilityIndex to AuditOnlyRunStage and populated them in PlanStagesBuilder.build().
    • Passed deployability_index to _get_audit_only_snapshots() and excluded non-representative dev snapshots without existing dev_intervals so AuditOnlyRunStage does not attempt to audit a not-yet-created __dev table before BackfillStage.
  • sqlmesh/core/plan/evaluator.py:
    • Updated BuiltInPlanEvaluator.visit_audit_only_run_stage() to traverse upward from stage.snapshots through snapshot.parents, shallow-copying only the audited snapshots and their upstream ancestors (including intervals, dev_intervals, and pending_restatement_intervals) so state refreshes cannot mutate shared snapshots or refresh unrelated project snapshots, and forwarded selected_snapshots, deployability_index, ignore_cron, and circuit_breaker to scheduler.audit(...).
  • sqlmesh/core/scheduler.py:
    • Scoped snapshots passed to _audit_snapshot() during audit_only=True to {snapshot.name: snapshot} plus snapshot.parents present in self.snapshots (matching Scheduler.evaluate() so unrelated breaking/new sibling models in the same plan are not resolved to uncreated physical tables).
    • Set snapshots_to_create = set() when audit_only=True in run_merged_intervals() so audit-only runs never query physical objects for creation or schedule CreateNode.
    • Filtered remove_intervals in Scheduler.audit() to snapshot.evaluatable snapshots matching selected_snapshots, preventing ValueError on unselected depends_on_past snapshots when execution_time < start, and guarded in-place remove_interval in _run_or_audit() with if not audit_only:.

Test Plan

  • Updated/added unit and integration tests in:
    • tests/core/test_plan_stages.py (AuditOnlyRunStage all_snapshots + deployability_index for dev and prod, and excluding non-representative dev snapshots without dev_intervals while including them once populated)
    • tests/core/test_plan_evaluator.py (visit_audit_only_run_stage ancestor-scoped snapshot copying, no shared mutation, argument forwarding, and PlanError on audit failure)
    • tests/core/test_scheduler.py (selected_snapshots, deployability_index, embedded parent, unselected depends_on_past safety, no in-memory interval mutation, no get_snapshots_to_create call, sibling exclusion, and missing-parent tolerance)
    • tests/core/integration/test_audits.py (faithful #6256 repros with failed after_promote prod plan + pure audit-only follow-up for new/changed parents, dev breaking parent with/without select_models, dev paused forward-only preview, embedded parent, external model, INCREMENTAL_BY_UNIQUE_KEY historical row preservation, uncreated sibling table safety, and non-representative dev FULL / INCREMENTAL_BY_TIME_RANGE models)
  • Executed checks:
    • make py-style: Passed (ruff, ruff-format, mypy, valid migrations)
    • make fast-test: Passed (3,134 passed, 0 failed across all fast/isolated/fork/registry/dialect suites)
    • pytest tests/core/test_plan_stages.py tests/core/test_plan_evaluator.py tests/core/test_scheduler.py tests/core/integration/test_audits.py: Passed (66 passed)
    • make doc-test: Passed (29 passed)

Checklist

  • I have run make py-style and fixed any issues
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test)
  • My commits are signed off (git commit -s) per the DCO

Summary by CodeRabbit

  • Bug Fixes
    • Audit-only plan runs now resolve the necessary parent models more reliably, including after failed plans and when models have changed or are being backfilled.
    • Audits now respect selected models and skip models that cannot be evaluated. They no longer create physical tables or alter snapshot intervals.
    • Development metadata models are audited at the appropriate stage based on whether development intervals are available, helping avoid unnecessary backfills and missed audits.

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Audit-only plan stages now carry plan snapshots and deployability data. The evaluator supplies audited snapshots and available ancestors to the scheduler. Scheduler audit execution filters selected snapshots and avoids table creation and interval mutation.

Changes: Audit-only execution

Layer / File(s) Summary
Stage inputs and audit eligibility
sqlmesh/core/plan/stages.py, tests/core/test_plan_stages.py, tests/core/integration/test_audits.py
AuditOnlyRunStage carries all plan snapshots and a deployability index. Metadata snapshots that are not deployability representatives are excluded when they have no dev intervals. Tests cover production and development stage selection.
Parent resolution and evaluator wiring
sqlmesh/core/plan/evaluator.py, tests/core/test_plan_evaluator.py, tests/core/integration/test_audits.py
The evaluator collects audited snapshots and available ancestors, copies snapshots and interval collections, and passes the snapshots, selection, deployability index, and other audit options to the scheduler. Tests cover parent resolution, dev plans, embedded parents, and evaluator failure handling.
Scheduler audit isolation
sqlmesh/core/scheduler.py, tests/core/test_scheduler.py, tests/core/integration/test_audits.py
The scheduler filters non-evaluatable and unselected snapshots, disables table creation for audit-only runs, limits evaluation to the snapshot and available direct parents, and preserves intervals. Tests cover interval handling, external models, incremental history, and unrelated sibling changes.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant PlanStagesBuilder
  participant BuiltInPlanEvaluator
  participant Scheduler
  participant SnapshotEvaluator
  PlanStagesBuilder->>BuiltInPlanEvaluator: Audit-only stage with snapshots and deployability index
  BuiltInPlanEvaluator->>Scheduler: Snapshot copies and selected audit names
  Scheduler->>SnapshotEvaluator: Evaluate selected snapshot with available parents
Loading

Possibly related PRs

  • SQLMesh/sqlmesh#6261: Addresses the same linked issue with overlapping changes to audit-only stage data and scheduler snapshot handling.

Suggested reviewers: izeigerman, vaggelisd

Merge Risk: Minimal · up to 2d3e3

Audit-only plan stages now resolve parents from the plan's snapshots instead of environment views, and audits no longer modify shared snapshot state. No merge-blocking risk was found. Reported test results come from the author and were not independently run.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: audit-only plan stages now receive the plan's snapshots and deployability index. It follows the conventional-commit style. The changeset also covers evaluator snapshot…
Description check Passed The description follows the template. It has the Description section with a problem, root cause, and detailed changes. It has the Test Plan section with the unit, integration, and style runs. It has t…
Linked Issues check Passed Issue #6256 requires audit-only runs to resolve parents to the plan's physical snapshots, not environment views, and to avoid shared-state mutation. The PR addresses each point. (1) stages.py: AuditOn…
Out of Scope Changes check Passed All changed areas connect to #6256. The evaluator change (ancestor-scoped copies, argument forwarding) and the stages.py change (all_snapshots, deployability_index, dev non-representative filtering) i…
Full details: Title check

Explanation

The title names the main change: audit-only plan stages now receive the plan's snapshots and deployability index. It follows the conventional-commit style. The changeset also covers evaluator snapshot copying and scheduler scoping, which the title does not mention, but it does not misrepresent the change.

Full details: Description check

Explanation

The description follows the template. It has the Description section with a problem, root cause, and detailed changes. It has the Test Plan section with the unit, integration, and style runs. It has the checklist, with items marked. The template's HTML comment placeholders were replaced with content. The checklist marks DCO sign-off as done, which the template requires.

Full details: Linked Issues check

Explanation

Issue #6256 requires audit-only runs to resolve parents to the plan's physical snapshots, not environment views, and to avoid shared-state mutation. The PR addresses each point. (1) stages.py: AuditOnlyRunStage now carries all_snapshots and deployability_index, populated in PlanStagesBuilder.build(), matching BackfillStage. (2) evaluator.py visit_audit_only_run_stage: traverses snapshot.parents from the audited snapshots using stage.all_snapshots, so direct and upstream parents are included. It passes shallow copies with copied intervals, dev_intervals, and pending_restatement_intervals to the scheduler, so in-place interval updates do not reach shared objects. It also forwards selected_snapshots={audited names}, deployability_index, ignore_cron, and circuit_breaker to scheduler.audit, as the issue suggests. (3) scheduler.py: audit-only runs skip snapshots_to_create and in-place remove_interval, and audit scope is limited to the audited snapshots and their parents. Tests in test_audits.py reproduce the new-parent and changed-parent cases from the issue, plus the dev and embedded-parent cases. The author reports these tests pass. Unresolved: I did not run the tests. The conclusion rests on the diff and the author's reported results. Whether stage.all_snapshots contains every environment parent in all cases was not separately verified, but the summary states it holds all plan snapshots.

Full details: Out of Scope Changes check

Explanation

All changed areas connect to #6256. The evaluator change (ancestor-scoped copies, argument forwarding) and the stages.py change (all_snapshots, deployability_index, dev non-representative filtering) implement the parent-resolution and dev-case requirements. The scheduler changes (selected snapshot filtering, no physical creation checks in audit-only mode, guarded in-place interval removal) implement the shared-mutation requirement and the suggested selectedsnapshots approach. The tests cover the same behaviors. The PR description states the additional dev, embedded-parent, and sibling-table handling addresses the issue's dev case and shared-state concerns. No unrelated feature or refactor was identified in the reviewed summary and evaluator diff.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: dbb07576-ac49-4c68-b28c-97bab3ac90cc
Commits

Reviewing files that changed from the base of the PR and between ce229ae and 2d3e3a5.

Files selected for processing: 7

  • sqlmesh/core/plan/evaluator.py
  • sqlmesh/core/plan/stages.py
  • sqlmesh/core/scheduler.py
  • tests/core/integration/test_audits.py
  • tests/core/test_plan_evaluator.py
  • tests/core/test_plan_stages.py
  • tests/core/test_scheduler.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

…stage

Signed-off-by: Sruthi <codewithsruthi@users.noreply.github.com>
@codewithsruthi
codewithsruthi force-pushed the fix/6256-audit-only-parents branch from 32fd88a to 2d3e3a5 Compare November 26, 2026 00:22
@codewithsruthi
codewithsruthi marked this pull request as ready for review November 26, 2026 00:29
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.

Audit-only plan stage reads parents through environment views: missing table or stale rows after a failed prod plan

1 participant