diff --git a/backend/tests/scripts/test_verify_dashboard_testing_current.py b/backend/tests/scripts/test_verify_dashboard_testing_current.py index 797e0e0c5..fd3031f7c 100644 --- a/backend/tests/scripts/test_verify_dashboard_testing_current.py +++ b/backend/tests/scripts/test_verify_dashboard_testing_current.py @@ -85,4 +85,47 @@ def test_junit_failure_projection_keeps_type_and_stack_without_driver_message(): assert projection['locations'][-1]['path'] == __file__ assert projection['locations'][-1]['line'] > 0 assert 'PRIVATE_JUNIT_SENTINEL' not in observed and '[SQL:' not in observed + + +def test_delayed_node_observes_locations_without_locals_or_source_and_cancels_timer(): + import sys + from contextvars import ContextVar + from types import SimpleNamespace + timers, events = [], [] + private = 'PRIVATE_DELAYED_NODE_SENTINEL' + trace = ContextVar('test_delay_trace', default='foreign') + + class Timer: + def __init__(self, seconds, callback): + assert seconds == 60 + self.callback, self.cancelled, self.started = callback, False, False + timers.append(self) + + def start(self): + self.started = True + + def cancel(self): + self.cancelled = True + + foreign = SimpleNamespace(f_code=SimpleNamespace(co_filename='https://' + private, co_name=private), + f_lineno=7, f_back=None, f_locals={'password':private}) + token = trace.set('owned-node-trace') + observer = launcher.DelayedNode(ROOT, 'tests/test_current.py::test_owned', + lambda message, **event: events.append({'trace':trace.get(), **event}), timer_factory=Timer, + frames=lambda: {1:sys._getframe(), 2:foreign}) + trace.reset(token) + observer.start() + observer.stage = 'call' + timers[0].callback() + assert timers[0].started + assert len(events) == 1 and events[0]['error_code'] == 'FINAL_NODE_DELAYED' + assert events[0]['trace'] == 'owned-node-trace' + assert events[0]['payload']['stage'] == 'call' + assert {'file':'outside_source_roots','line':7,'function':'unavailable'} in events[0]['payload']['locations'] + wire = json.dumps(events) + assert private not in wire and 'password' not in wire and 'https://' not in wire + observer.finish() + assert timers[0].cancelled + timers[0].callback() + assert len(events) == 1 # #endregion Test.VerifyDashboardTestingCurrent diff --git a/scripts/verify_dashboard_testing_current.py b/scripts/verify_dashboard_testing_current.py index 1cd4d9582..3fe479bf1 100644 --- a/scripts/verify_dashboard_testing_current.py +++ b/scripts/verify_dashboard_testing_current.py @@ -13,6 +13,7 @@ import ast import base64 from collections import Counter from contextlib import contextmanager, redirect_stderr, redirect_stdout +from contextvars import copy_context import json import logging import os @@ -22,6 +23,7 @@ import secrets import stat import subprocess import sys +import threading API_FILES = ( @@ -168,6 +170,54 @@ class SafeLogs(logging.Filter): # #endregion Tooling.VerifyDashboardTestingCurrent.SafeLogs +# #region Tooling.VerifyDashboardTestingCurrent.DelayedNode [C:3] [TYPE Class] +# @POST A still-running node emits one bounded observation with stack locations only; completion cancels its timer. +# @INVARIANT No locals, source lines, exception text or private filenames are inspected or emitted; a delay neither passes nor retries a node. +class DelayedNode: + def __init__(self, root, node_id, emit, *, timer_factory=threading.Timer, frames=sys._current_frames): + self.root = root.resolve() + self.node_id = node_id + self.emit = emit + self.frames = frames + self.stage = 'setup' + self.finished = False + self.lock = threading.Lock() + context = copy_context() + self.timer = timer_factory(60, lambda: context.run(self.observe)) + self.timer.daemon = True + + def start(self): + self.timer.start() + + def finish(self): + with self.lock: + self.finished = True + self.timer.cancel() + + def observe(self): + with self.lock: + if self.finished: + return + locations = [] + for frame in list(self.frames().values())[:3]: + for _ in range(2): + if frame is None: + break + code = frame.f_code + path = Path(code.co_filename) + admitted = (path.is_absolute() and path.suffix == '.py' + and any(path.is_relative_to(base) for base in (self.root, Path(sys.base_prefix), Path(sys.prefix)))) + locations.append({'file':path.name if admitted and len(path.name) <= 96 else 'outside_source_roots', + 'line':frame.f_lineno, + 'function':code.co_name if admitted and len(code.co_name) <= 64 else 'unavailable'}) + frame = frame.f_back + self.emit('Frozen suite node remains in progress', + src='Tooling.VerifyDashboardTestingCurrent', error_code='FINAL_NODE_DELAYED', + claim='OBSERVED: node has not completed after 60 seconds', + payload={'node_id':self.node_id, 'stage':self.stage, 'locations':locations}) +# #endregion Tooling.VerifyDashboardTestingCurrent.DelayedNode + + # #region Tooling.VerifyDashboardTestingCurrent.Dispatch [C:3] [TYPE Function] # @POST Exactly one pytest dispatch; actual node IDs and stage outcomes are retained, raw exception/capture text is excluded from JUnit. def run_suite(root: Path, evidence: Path, commit: str, safe: SafeLogs): @@ -187,6 +237,8 @@ def run_suite(root: Path, evidence: Path, commit: str, safe: SafeLogs): owner.addHandler(handler) class SuiteEvidence: + active = None + def pytest_collection_modifyitems(self, items): forbidden = {'postgres_container', 'superset_container', 'integration_network', 'db_factory'} nodes.extend(item.nodeid for item in items) @@ -203,7 +255,13 @@ def run_suite(root: Path, evidence: Path, commit: str, safe: SafeLogs): seed_trace_id() logger.reason('Run frozen suite node', src='Tooling.VerifyDashboardTestingCurrent', payload={'node_id':item.nodeid}) - yield + self.active = DelayedNode(root, item.nodeid, logger.explore) + self.active.start() + try: + yield + finally: + self.active.finish() + self.active = None @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(self, call): @@ -221,6 +279,8 @@ def run_suite(root: Path, evidence: Path, commit: str, safe: SafeLogs): def pytest_runtest_logreport(self, report): counts[report.when + ':' + report.outcome] += 1 + if self.active is not None: + self.active.stage = {'setup':'call', 'call':'teardown', 'teardown':'complete'}[report.when] logger.reflect('Frozen suite node stage observed', src='Tooling.VerifyDashboardTestingCurrent', payload={'node_id':report.nodeid,'stage':report.when,'outcome':report.outcome})