Files
cleveragents-core/robot/helpers_common.py
T
Brent E. Edwards d88dad94f6 fix(test): resolve race condition in M4 validation integration test
Three-pronged fix for intermittent pabot-parallel race condition in
M4 validation integration tests:

1. Composable Setup Database Isolation keyword in common.resource
   gives each suite a unique CLEVERAGENTS_DATABASE_URL so concurrent
   pabot workers never contend on the same SQLite file.

2. Per-suite CLEVERAGENTS_HOME directories prevent shared temp
   directory cleanup from racing between workers.

3. Centralised reset_global_state() in robot/helpers_common.py clears
   Settings singleton, DI container, provider registry, and engine
   cache between chained CLI invocations in helper processes.

Also:
- Setup Test Environment now accepts optional mock_ai and
  auto_apply_migrations arguments (default TRUE) for backward
  compatibility while allowing suites to opt out.
- Added Suite Teardown to cli_plan_context_commands.robot.
- Fixed _COMMANDS typing in two helpers to eliminate type: ignore.
- Updated docs/development/testing.md to reflect helpers_common
  delegation pattern.
- Added timeout=30s to all Run Process calls in
  m4_e2e_verification.robot.

Fixes: #563
2026-03-10 19:04:57 +00:00

63 lines
2.1 KiB
Python

"""Shared utilities for Robot Framework helper scripts.
This module centralises the ``reset_global_state()`` function used by
``helper_m4_e2e_verification.py``,
``helper_m4_correction_subplan_smoke.py``, and
``helper_m3_decision_validation_smoke.py``.
Any helper script invoked via ``Run Process`` from a ``.robot`` suite
can import this module to reset process-wide singletons between
chained CLI invocations.
"""
import contextlib
import sys
def reset_global_state() -> None:
"""Reset process-wide singletons between CLI invocations.
Clears the Settings singleton, DI container, provider registry,
and in-memory SQLAlchemy engine cache so that chained CLI
invocations within the same helper process do not carry stale
state.
Each import is guarded by ``contextlib.suppress(ImportError)``
because this module lives in ``robot/`` and may be loaded in
contexts where not all application modules are on the path
(e.g. a minimal helper that only exercises domain models).
This is an intentional exception to the top-of-file import rule
per CONTRIBUTING.md §Import Guidelines.
"""
# Settings singleton
with contextlib.suppress(ImportError):
from cleveragents.config.settings import Settings
Settings._instance = None
# DI container singleton
with contextlib.suppress(ImportError):
from cleveragents.application.container import reset_container
reset_container()
# Provider registry singleton
with contextlib.suppress(ImportError):
from cleveragents.providers.registry import reset_provider_registry
reset_provider_registry()
# In-memory SQLAlchemy engine cache
with contextlib.suppress(ImportError):
from cleveragents.infrastructure.database.engine_cache import MEMORY_ENGINES
for _url, engine in list(MEMORY_ENGINES.items()):
try:
engine.dispose()
except Exception as exc:
print(
f"[helpers_common] engine.dispose() suppressed: {exc}",
file=sys.stderr,
)
MEMORY_ENGINES.clear()