Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/pt_snap_cli/query/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,9 @@ def execute(
if timeout_s is not None and timeout_s > 0 and _is_sqlite_interrupt(e):
raise self._timeout_error(timeout_s) from e
raise self._execution_error(e) from e
except QueryTimeoutError:
except QueryExecutionError:
# Domain failures (including timeouts and budget callbacks) are
# already normalized; preserve their type, message, and cause.
raise
except Exception as e:
raise QueryExecutionError(f"Query execution failed: {e}") from e
Expand All @@ -399,7 +401,9 @@ def count(self, sql: str, timeout_s: float | None = None) -> int:
if timeout_s is not None and timeout_s > 0 and _is_sqlite_interrupt(e):
raise self._timeout_error(timeout_s) from e
raise self._execution_error(e) from e
except QueryTimeoutError:
except QueryExecutionError:
# Domain failures (including timeouts and budget callbacks) are
# already normalized; preserve their type, message, and cause.
raise
except Exception as e:
raise QueryExecutionError(f"Query execution failed: {e}") from e
Expand Down
99 changes: 99 additions & 0 deletions tests/core/test_query_error_wrapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""Budget messages retain one domain boundary across dataset execution paths."""

import sqlite3
import time
from contextlib import closing

import pytest

from pt_snap_cli.core.context_cache import ContextCache
from pt_snap_cli.core.dataset_lifecycle_connection import _AttachedSources
from pt_snap_cli.core.dataset_lifecycle_scratch import scratch_lifecycles
from pt_snap_cli.core.dataset_resolver import DatasetResolver
from pt_snap_cli.core.dataset_sources import DatasetSourceResolver, QueryBudget
from pt_snap_cli.core.errors import QueryExecutionError
from pt_snap_cli.core.query_service import QueryService
from tests.test_dataset_focus import make_dataset


def _budget(limit):
limits = {"max_work_rows": 1} if limit == "rows" else {"max_work_bytes": 1}
return QueryBudget(None, time.monotonic(), **limits)


@pytest.mark.parametrize("route", ["source", "attached", "scratch", "output"])
@pytest.mark.parametrize("limit", ["rows", "bytes"])
def test_same_budget_error_message_across_dataset_read_paths(tmp_path, route, limit):
root = make_dataset(tmp_path / "dataset", devices=(0,), slices=1)
path = root / "device_0/slice_00000.db"
with closing(sqlite3.connect(path)) as connection, connection:
connection.executemany(
"INSERT INTO block_0 VALUES (?,?,8,8,1,?,-1)", [(0, 100, 0), (1, 108, 1)]
)
dataset = DatasetResolver().inspect(root)
with pytest.raises(QueryExecutionError) as expected:
_budget(limit).consume([{"id": 0}, {"id": 1}])

with closing(ContextCache()) as cache:
sources = DatasetSourceResolver(dataset, 0, cache, _budget(limit))
if route == "source":
with pytest.raises(QueryExecutionError) as caught:
sources.read(path, "SELECT id FROM trace_entry_0 WHERE id>=0 ORDER BY id")
elif route == "attached":
with _AttachedSources(sources) as database:
with pytest.raises(QueryExecutionError) as caught:
database.read("SELECT id FROM s0.trace_entry_0 WHERE id>=0 ORDER BY id")
elif route == "scratch":
# Build the real private reducer first, then exhaust its read budget.
sources.budget = QueryBudget(None, time.monotonic())
with scratch_lifecycles(sources, sources.device.slices) as (database, _):
sources.budget = _budget(limit)
with pytest.raises(QueryExecutionError) as caught:
database.read("SELECT id FROM lifecycle_state ORDER BY id")
else:
with pytest.raises(QueryExecutionError) as caught:
sources.budget.consume([{"id": 0}, {"id": 1}])

assert type(caught.value) is QueryExecutionError
assert str(caught.value) == str(expected.value)
assert "no partial global result" in str(caught.value)
if route in {"attached", "scratch"}:
with pytest.raises(sqlite3.ProgrammingError, match="closed"):
database.connection.execute("SELECT 1")


def test_final_output_budget_failure_is_preserved_before_dataset_early_return(
tmp_path, monkeypatch
):
root = make_dataset(tmp_path / "dataset", devices=(0,), slices=1)
consume_source = DatasetSourceResolver._consume
consume_budget = QueryBudget.consume
reading_source = False
failures = []

def source_consume(sources, rows):
nonlocal reading_source
reading_source = True
try:
consume_source(sources, rows)
finally:
reading_source = False

def budget_consume(budget, rows):
# Let the real source reads finish, then fail final result accounting.
if not reading_source and rows:
budget.max_work_rows = budget.work_rows
try:
consume_budget(budget, rows)
except QueryExecutionError as exc:
failures.append(exc)
raise

monkeypatch.setattr(DatasetSourceResolver, "_consume", source_consume)
monkeypatch.setattr(QueryBudget, "consume", budget_consume)
with closing(QueryService()) as service:
with pytest.raises(QueryExecutionError) as caught:
service.execute_query("allocation", db_path=root)
assert len(failures) == 1
assert caught.value is failures[0]
assert "no partial global result" in str(caught.value)
64 changes: 63 additions & 1 deletion tests/query/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

from pt_snap_cli.context import Context
from pt_snap_cli.query.config import QueryParameter, QueryTemplate
from pt_snap_cli.query.executor import QueryExecutionError, QueryExecutor, TemplateRenderError
from pt_snap_cli.query.executor import (
QueryExecutionError,
QueryExecutor,
QueryTimeoutError,
TemplateRenderError,
)


class TestQueryExecutor:
Expand Down Expand Up @@ -457,3 +462,60 @@ def _make_mock_context(device_ids: list[int]):
mock = MagicMock()
mock.device_ids = device_ids
return mock


@pytest.mark.parametrize("method", ["execute", "count"])
@pytest.mark.parametrize(
"failure",
[
QueryExecutionError("Dataset query work budget exceeded; no partial global result."),
QueryExecutionError("Query execution failed: already normalized"),
QueryTimeoutError("Query timed out after 1 seconds"),
],
)
def test_existing_query_errors_cross_executor_unchanged(method, failure):
context = _make_mock_context(device_ids=[0])
context.connect.side_effect = failure
with pytest.raises(type(failure)) as caught:
getattr(QueryExecutor(context), method)("SELECT 1")
assert caught.value is failure
assert str(caught.value) == str(failure)


@pytest.mark.parametrize("method", ["execute", "count"])
@pytest.mark.parametrize(
"failure", [RuntimeError("backend failed"), sqlite3.OperationalError("broken SQL")]
)
def test_unknown_query_failures_are_wrapped_once(method, failure):
context = _make_mock_context(device_ids=[0])
context.connect.side_effect = failure
with pytest.raises(QueryExecutionError) as caught:
getattr(QueryExecutor(context), method)("SELECT 1")
assert str(caught.value) == f"Query execution failed: {failure}"
assert caught.value.__cause__ is failure


@pytest.mark.parametrize("error_type", [QueryExecutionError, QueryTimeoutError])
def test_consumer_domain_error_discards_prior_batches_and_keeps_connection_usable(error_type):
context = _make_mock_context(device_ids=[0])
failure = error_type("consumer budget exhausted")
batches = []

def consume(rows):
batches.append(len(rows))
if len(batches) == 2:
raise failure

with closing(sqlite3.connect(":memory:")) as connection:
context.connect.return_value.__enter__.return_value = connection
executor = QueryExecutor(context)
with pytest.raises(error_type) as caught:
executor.execute(
"WITH RECURSIVE rows(id) AS (SELECT 0 UNION ALL "
"SELECT id+1 FROM rows WHERE id<256) SELECT id FROM rows",
timeout_s=10,
consume=consume,
)
assert caught.value is failure
assert batches == [256, 1]
assert executor.execute("SELECT 42 AS answer") == [{"answer": 42}]
71 changes: 71 additions & 0 deletions tests/test_contract_cli_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
from pt_snap_cli.api import FocusState, SnapshotAnalyzer
from pt_snap_cli.cli import app
from pt_snap_cli.core import InvalidDeviceError, QueryExecutionError, TemplateRenderError
from pt_snap_cli.core.dataset_sources import QueryBudget
from pt_snap_cli.core.errors import QueryTimeoutError
from pt_snap_cli.core.json_codec import JSON_SCHEMA_VERSION
from pt_snap_cli.core.query_service import QueryService
from pt_snap_cli.core.report_service import ReportService
from pt_snap_cli.query.config import QueryParameter, QueryTemplate
from pt_snap_cli.query.registry import QueryRegistry, register_query
from tests.test_dataset_focus import make_dataset
Expand Down Expand Up @@ -52,6 +56,73 @@ def test_dataset_int64_overflow_cli_api_domain_error(tmp_path, template, params)
assert "Traceback" not in result.stderr


@pytest.mark.parametrize("surface", ["service", "api", "report", "cli-query", "cli-report"])
@pytest.mark.parametrize("failure_kind", ["budget", "timeout"])
def test_dataset_budget_error_keeps_message_kind_and_no_partial_result(
tmp_path, monkeypatch, surface, failure_kind
):
root = make_dataset(tmp_path / "budget-error", devices=(0,))
consume = QueryBudget.consume
failures = []

def bounded_consume(budget, rows):
try:
if failure_kind == "timeout":
raise QueryTimeoutError(f"Query timed out after {budget.timeout_s} seconds")
budget.max_work_rows = 1
consume(budget, rows)
except QueryExecutionError as exc:
failures.append(exc)
raise

monkeypatch.setattr(QueryBudget, "consume", bounded_consume)
expected_type = QueryTimeoutError if failure_kind == "timeout" else QueryExecutionError
if surface in {"service", "api", "report"}:
with (
closing(QueryService()) as service,
SnapshotAnalyzer(root) as analyzer,
closing(ReportService()) as report,
):
with pytest.raises(expected_type) as caught:
if surface == "service":
service.execute_query("event", db_path=root, timeout_s=30)
elif surface == "api":
analyzer.execute_query("event", timeout_s=30)
else:
report.peak_memory_report(root, timeout_s=30)
assert len(failures) == 1
assert type(caught.value) is expected_type
assert str(caught.value) == str(failures[0])
else:
args = (
["query", str(root), "--template-use", "event"]
if surface == "cli-query"
else ["report", "peak-memory", str(root)]
) + ["--timeout", "30"]
for json_mode in (False, True):
failures.clear()
result = runner.invoke(app, args + (["--json"] if json_mode else []))
assert result.exit_code == 1
assert len(failures) == 1
expected_message = str(failures[0])
if json_mode:
assert result.stdout == ""
payload = json.loads(result.stderr)
assert payload["error"]["code"] == (
"QUERY_TIMEOUT" if failure_kind == "timeout" else "QUERY_FAILED"
)
assert payload["error"]["message"] == expected_message
assert "rows" not in payload and "callstack_groups" not in payload
else:
prefix = (
"Error executing query: "
if surface == "cli-query"
else "Error generating report: "
)
assert result.stdout == f"{prefix}{expected_message}\n"
assert result.stderr == ""


@pytest.mark.parametrize("template", ["event", "active_memory_callstack_at_event"])
def test_stack_summary_cli_api_and_catalog_contract(contract_db: Path, template: str):
with closing(sqlite3.connect(contract_db)) as conn, conn:
Expand Down
Loading