forked from cleveragents/cleveragents-core
test(cli): add regression-guard tests for Container.resolve() crash
Add Behave and Robot Framework regression tests for bug #647, where plan tree, plan explain, and plan correct CLI commands crashed with AttributeError when resolving DecisionService from the DI container. Tests use a real DI container with seeded decisions (not MagicMock) to catch the exact class of bug that existing M3 tests missed. Assertions verify successful execution and command-specific output content. Includes Settings.reset() classmethod for robust singleton cleanup in test teardown. Review feedback addressed (hurui200320 Round 5): - Fixed Behave step engine leak by capturing UoW in cleanup closure - Removed dead @then decorator; renamed to private _assert_command_succeeded - Strengthened plan correct assertions with revert/dry-run content checks - Updated misleading get_container() comment to reflect singleton warming - Added test-only warning to Settings.reset() docstring - Added type annotations to 4 settings step functions - Fixed CONTRIBUTORS.md alphabetical ordering and removed duplicate entry - Replaced glob.glob with pathlib suffix iteration in Robot helper - Fixed feature description line break for readability - Removed redundant TYPE_CHECKING import for Decision ISSUES CLOSED: #648
This commit is contained in:
@@ -195,6 +195,10 @@
|
||||
matching primitives. Includes Behave feature coverage for TUI persona/input
|
||||
behavior, Robot TUI smoke validation, and an ASV fuzzy-reference benchmark.
|
||||
(#695)
|
||||
- Added TDD regression tests for bug #647 (`Container.resolve()` crash in
|
||||
`plan tree`, `plan explain`, and `plan correct`) using real DI wiring in
|
||||
Behave and Robot. Added regression-guard assertions, cache/singleton
|
||||
cleanup hardening, and targeted issue-648 review follow-ups. (#648)
|
||||
- Added four CLI-based integration test cases to M5 E2E verification suite
|
||||
for v3.4.0 milestone acceptance criteria validation. Tests exercise
|
||||
`project create`, `resource add git-checkout`, `project link-resource`, and
|
||||
|
||||
+2
-2
@@ -1,9 +1,9 @@
|
||||
# Contributors
|
||||
|
||||
* Jeffrey Phillips Freeman <jeffrey.freeman@syncleus.com>
|
||||
* Aditya Chhabra <aditya.chhabra@cleverthis.com>
|
||||
* Brent E. Edwards <brent.edwards@cleverthis.com>
|
||||
* Rui Hu <rui.hu@cleverthis.com>
|
||||
* Hamza Khyari <hamza.khyari@cleverthis.com>
|
||||
* Jeffrey Phillips Freeman <jeffrey.freeman@syncleus.com>
|
||||
* Luis Mendes <luis.p.mendes@gmail.com>
|
||||
* Rui Hu <rui.hu@cleverthis.com>
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
@tdd_bug @tdd_bug_647
|
||||
Feature: Container.resolve() crash in plan tree/explain/correct commands
|
||||
Regression guard for bug #647, where these CLI commands previously
|
||||
crashed when resolving DecisionService from the DI container.
|
||||
|
||||
These tests use a REAL DI container (not MagicMock). Existing M3 tests missed
|
||||
this class of bug because they mock get_container() with MagicMock, which
|
||||
auto-creates any attribute.
|
||||
|
||||
NOTE: Bug #647 appears fixed; these scenarios now run as normal
|
||||
regression checks for correct command behavior.
|
||||
`plan correct` is intentionally exercised with `--dry-run` in this
|
||||
real-container path so no live LLM execution is triggered.
|
||||
|
||||
Background:
|
||||
Given cr647- a real DI container with seeded decisions
|
||||
|
||||
Scenario: plan tree command regression guard for container.resolve() crash
|
||||
When cr647- I invoke the plan tree CLI command with a real container
|
||||
Then cr647- plan tree output should include seeded decision id
|
||||
|
||||
Scenario: plan explain command regression guard for container.resolve() crash
|
||||
When cr647- I invoke the plan explain CLI command with a real container
|
||||
Then cr647- plan explain output should include seeded decision details
|
||||
|
||||
Scenario: plan correct command regression guard for container.resolve() crash
|
||||
When cr647- I invoke the plan correct CLI command with a real container
|
||||
Then cr647- plan correct output should reference seeded decision
|
||||
@@ -167,3 +167,13 @@ Feature: Settings runtime helpers
|
||||
Scenario: Provider normalization returns canonical names for unknown providers
|
||||
When I normalize the provider name "Custom-LLM "
|
||||
Then the normalized provider name should be "customllm"
|
||||
|
||||
Scenario: Settings reset clears singleton cache
|
||||
Given no environment variables are set
|
||||
And the environment variable "CLEVERAGENTS_ENV" is set to "production"
|
||||
When I load singleton settings via get_settings
|
||||
And I reset singleton settings
|
||||
And the environment variable "CLEVERAGENTS_ENV" is set to "staging"
|
||||
And I load singleton settings via get_settings
|
||||
Then singleton settings instances should be different
|
||||
And singleton environment should be "staging"
|
||||
|
||||
@@ -0,0 +1,291 @@
|
||||
"""Step definitions for container_resolve_crash.feature.
|
||||
|
||||
Regression tests for bug #647 covering ``plan tree``, ``plan explain``,
|
||||
and ``plan correct`` command paths through a real DI container.
|
||||
|
||||
All step text uses the ``cr647-`` prefix to avoid collisions with
|
||||
other step files.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from contextlib import suppress
|
||||
|
||||
from behave import given, then, when
|
||||
from behave.runner import Context
|
||||
from typer.testing import CliRunner
|
||||
from ulid import ULID
|
||||
|
||||
from cleveragents.infrastructure.database.engine_cache import MEMORY_ENGINES
|
||||
|
||||
cli_runner = CliRunner()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# GIVEN — Real container with seeded decisions
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@given("cr647- a real DI container with seeded decisions")
|
||||
def step_cr647_setup_container(context: Context) -> None:
|
||||
"""Set up a real DI container with seeded decision tree.
|
||||
|
||||
Uses the real Container class (not MagicMock) with seeded decisions
|
||||
so command output can validate DI-resolved service behavior.
|
||||
"""
|
||||
from cleveragents.application.container import get_container, reset_container
|
||||
from cleveragents.application.services.decision_service import DecisionService
|
||||
from cleveragents.application.services.plan_lifecycle_service import (
|
||||
PlanLifecycleService,
|
||||
)
|
||||
from cleveragents.config.settings import Settings
|
||||
from cleveragents.domain.models.core.decision import (
|
||||
ContextSnapshot,
|
||||
Decision,
|
||||
DecisionType,
|
||||
ResourceRef,
|
||||
)
|
||||
from cleveragents.infrastructure.database.unit_of_work import UnitOfWork
|
||||
|
||||
# Reuse the per-scenario DB URL set by features/environment.py to avoid
|
||||
# orphaning temp-file bookkeeping and to keep cleanup semantics aligned.
|
||||
database_url = os.environ.get("CLEVERAGENTS_DATABASE_URL", "sqlite:///:memory:")
|
||||
|
||||
# Reset container to ensure clean state
|
||||
reset_container()
|
||||
|
||||
# Create UnitOfWork and initialize database
|
||||
uow = UnitOfWork(database_url)
|
||||
uow.init_database()
|
||||
|
||||
# Use real settings for integration-style behavior (no mocks).
|
||||
settings = Settings(database_url=database_url, async_enabled=False)
|
||||
|
||||
# Create DecisionService and seed decisions
|
||||
decision_svc = DecisionService(settings=settings, unit_of_work=uow)
|
||||
lifecycle_svc = PlanLifecycleService(settings=settings, unit_of_work=uow)
|
||||
|
||||
# Seed a small tree (root + child) so tree/explain/correct assertions
|
||||
# can validate command output beyond crash absence.
|
||||
action_name = f"local/cr647-{ULID()!s}"
|
||||
lifecycle_svc.create_action(
|
||||
name=action_name,
|
||||
description="Container.resolve regression action",
|
||||
definition_of_done="Regression guard setup complete",
|
||||
strategy_actor="openai/gpt-4",
|
||||
execution_actor="openai/gpt-4",
|
||||
)
|
||||
plan = lifecycle_svc.use_action(action_name)
|
||||
plan_id = plan.identity.plan_id
|
||||
root: Decision = decision_svc.record_decision(
|
||||
plan_id=plan_id,
|
||||
decision_type=DecisionType.PROMPT_DEFINITION,
|
||||
question="What should we build?",
|
||||
chosen_option="A REST API",
|
||||
alternatives_considered=["GraphQL API", "gRPC service"],
|
||||
confidence_score=0.95,
|
||||
rationale="REST fits requirements",
|
||||
context_snapshot=ContextSnapshot(
|
||||
hot_context_hash="sha256:root",
|
||||
hot_context_ref="store://root",
|
||||
relevant_resources=[
|
||||
ResourceRef(resource_id=str(ULID()), path="src/main.py"),
|
||||
],
|
||||
actor_state_ref="checkpoint://root",
|
||||
),
|
||||
)
|
||||
child: Decision = decision_svc.record_decision(
|
||||
plan_id=plan_id,
|
||||
decision_type=DecisionType.STRATEGY_CHOICE,
|
||||
question="Which framework should we use?",
|
||||
chosen_option="FastAPI",
|
||||
parent_decision_id=root.decision_id,
|
||||
alternatives_considered=["Flask", "Django"],
|
||||
confidence_score=0.90,
|
||||
rationale="FastAPI fits async service requirements",
|
||||
context_snapshot=ContextSnapshot(
|
||||
hot_context_hash="sha256:child",
|
||||
hot_context_ref="store://child",
|
||||
relevant_resources=[
|
||||
ResourceRef(resource_id=str(ULID()), path="src/api.py"),
|
||||
],
|
||||
actor_state_ref="checkpoint://child",
|
||||
),
|
||||
)
|
||||
|
||||
# Store IDs for use in test steps.
|
||||
# explain/correct only need one valid decision_id to trigger the resolve() path.
|
||||
context.cr647_plan_id = plan_id
|
||||
context.cr647_child_id = child.decision_id
|
||||
|
||||
# Pre-initialize the container singleton so CLI commands get DI wiring
|
||||
# from the seeded database.
|
||||
_ = get_container()
|
||||
|
||||
# Store cleanup handler — capture ``uow`` so its engine is explicitly
|
||||
# disposed even when the URL is not in ``MEMORY_ENGINES`` (file-based).
|
||||
def cleanup() -> None:
|
||||
"""Reset in-memory DB/cache and process-global singletons."""
|
||||
with suppress(Exception):
|
||||
uow.engine.dispose()
|
||||
with suppress(Exception):
|
||||
engine = MEMORY_ENGINES.pop(database_url, None)
|
||||
if engine is not None:
|
||||
engine.dispose()
|
||||
with suppress(Exception):
|
||||
reset_container()
|
||||
with suppress(Exception):
|
||||
os.environ.pop("CLEVERAGENTS_DATABASE_URL", None)
|
||||
# Keep reset last so any env-dependent lazy settings re-init cannot leak.
|
||||
with suppress(Exception):
|
||||
Settings.reset()
|
||||
|
||||
context.add_cleanup(cleanup)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# WHEN — Invoke CLI commands with real container
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@when("cr647- I invoke the plan tree CLI command with a real container")
|
||||
def step_cr647_invoke_plan_tree(context: Context) -> None:
|
||||
"""Invoke plan tree command and capture regression output."""
|
||||
from cleveragents.cli.main import app as cli_app
|
||||
|
||||
context.cr647_result = cli_runner.invoke(
|
||||
cli_app,
|
||||
["plan", "tree", context.cr647_plan_id, "--format", "json"],
|
||||
# Capture exceptions so the step can assert exact failure class and output.
|
||||
catch_exceptions=True,
|
||||
)
|
||||
|
||||
|
||||
@when("cr647- I invoke the plan explain CLI command with a real container")
|
||||
def step_cr647_invoke_plan_explain(context: Context) -> None:
|
||||
"""Invoke plan explain command and capture regression output."""
|
||||
from cleveragents.cli.main import app as cli_app
|
||||
|
||||
context.cr647_result = cli_runner.invoke(
|
||||
cli_app,
|
||||
[
|
||||
"plan",
|
||||
"explain",
|
||||
context.cr647_child_id,
|
||||
"--show-context",
|
||||
"--show-reasoning",
|
||||
"--format",
|
||||
"json",
|
||||
],
|
||||
# Capture exceptions so the step can assert exact failure class and output.
|
||||
catch_exceptions=True,
|
||||
)
|
||||
|
||||
|
||||
@when("cr647- I invoke the plan correct CLI command with a real container")
|
||||
def step_cr647_invoke_plan_correct(context: Context) -> None:
|
||||
"""Invoke plan correct command and capture regression output."""
|
||||
from cleveragents.cli.main import app as cli_app
|
||||
|
||||
context.cr647_result = cli_runner.invoke(
|
||||
cli_app,
|
||||
[
|
||||
"plan",
|
||||
"correct",
|
||||
context.cr647_child_id,
|
||||
"--mode",
|
||||
"revert",
|
||||
"--guidance",
|
||||
"Use Django instead",
|
||||
# NOTE: ``--plan`` is currently required by implementation when there
|
||||
# is no active plan in CLI context, though spec examples show omitted.
|
||||
"--plan",
|
||||
context.cr647_plan_id,
|
||||
"--dry-run",
|
||||
"--format",
|
||||
"json",
|
||||
],
|
||||
# Capture exceptions so the step can assert exact failure class and output.
|
||||
catch_exceptions=True,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# THEN — Verify successful command execution
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _assert_command_succeeded(context: Context) -> None:
|
||||
"""Assert common post-fix regression behavior for command execution."""
|
||||
result = context.cr647_result
|
||||
|
||||
# Defense-in-depth: if failure happens, ensure it matches historical bug class.
|
||||
if result.exit_code != 0 and result.exception is not None:
|
||||
assert isinstance(result.exception, AttributeError), (
|
||||
"Unexpected non-AttributeError exception. Got "
|
||||
f"{type(result.exception).__name__}: {result.exception}"
|
||||
)
|
||||
|
||||
assert result.exit_code == 0, (
|
||||
f"Expected exit code 0, got {result.exit_code}. "
|
||||
f"Output: {result.output}\nException: {result.exception}"
|
||||
)
|
||||
|
||||
assert result.exception is None, (
|
||||
f"Expected no exception, got {type(result.exception).__name__}: "
|
||||
f"{result.exception}\nOutput: {result.output}"
|
||||
)
|
||||
|
||||
# Guard against silent error traces while still returning 0.
|
||||
output_lower = (result.output or "").lower()
|
||||
assert "attributeerror" not in output_lower, (
|
||||
f"Did not expect AttributeError text in output:\n{result.output}"
|
||||
)
|
||||
assert "has no attribute 'resolve'" not in output_lower, (
|
||||
f"Did not expect resolve() attribute crash text in output:\n{result.output}"
|
||||
)
|
||||
|
||||
|
||||
@then("cr647- plan tree output should include seeded decision id")
|
||||
def step_cr647_tree_output_contains_seeded_id(context: Context) -> None:
|
||||
"""Verify tree command output contains seeded decision id."""
|
||||
_assert_command_succeeded(context)
|
||||
result = context.cr647_result
|
||||
assert context.cr647_child_id in (result.output or ""), (
|
||||
"Expected plan tree output to include seeded decision id. "
|
||||
f"Output: {result.output}"
|
||||
)
|
||||
|
||||
|
||||
@then("cr647- plan explain output should include seeded decision details")
|
||||
def step_cr647_explain_output_contains_seeded_details(context: Context) -> None:
|
||||
"""Verify explain command output includes seeded id and content."""
|
||||
_assert_command_succeeded(context)
|
||||
result = context.cr647_result
|
||||
assert context.cr647_child_id in (result.output or ""), (
|
||||
f"Expected plan explain output to include decision id. Output: {result.output}"
|
||||
)
|
||||
assert "FastAPI" in (result.output or ""), (
|
||||
"Expected plan explain output to include child decision content "
|
||||
"(FastAPI). "
|
||||
f"Output: {result.output}"
|
||||
)
|
||||
|
||||
|
||||
@then("cr647- plan correct output should reference seeded decision")
|
||||
def step_cr647_correct_output_references_seeded_decision(context: Context) -> None:
|
||||
"""Verify correct command output references seeded decision id and mode."""
|
||||
_assert_command_succeeded(context)
|
||||
result = context.cr647_result
|
||||
output = result.output or ""
|
||||
assert context.cr647_child_id in output, (
|
||||
"Expected plan correct output to reference seeded decision id. "
|
||||
f"Output: {output}"
|
||||
)
|
||||
# Validate command-specific content: dry-run mode and guidance text.
|
||||
output_lower = output.lower()
|
||||
assert "revert" in output_lower or "dry" in output_lower, (
|
||||
"Expected plan correct output to reference revert mode or dry-run. "
|
||||
f"Output: {output}"
|
||||
)
|
||||
@@ -1,14 +1,18 @@
|
||||
"""Step definitions for settings feature tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from behave import given, then, when
|
||||
from behave.runner import Context
|
||||
|
||||
from cleveragents.config.settings import Settings
|
||||
|
||||
|
||||
@given('the environment variable "{key}" is set to "{value}"')
|
||||
@when('the environment variable "{key}" is set to "{value}"')
|
||||
def step_set_env_var(context, key, value):
|
||||
"""Set an environment variable."""
|
||||
os.environ[key] = value
|
||||
@@ -605,3 +609,44 @@ def step_verify_environment_variable(context, env_var, expected):
|
||||
context.env_vars_to_clean = []
|
||||
if env_var not in context.env_vars_to_clean:
|
||||
context.env_vars_to_clean.append(env_var)
|
||||
|
||||
|
||||
@given("I load singleton settings via get_settings")
|
||||
@when("I load singleton settings via get_settings")
|
||||
def step_load_singleton_settings_via_get_settings(context: Context) -> None:
|
||||
"""Load settings through the singleton accessor and store the instance."""
|
||||
settings = Settings.get_settings()
|
||||
if not hasattr(context, "singleton_instances"):
|
||||
context.singleton_instances = []
|
||||
context.singleton_instances.append(settings)
|
||||
context.settings = settings
|
||||
|
||||
|
||||
@when("I reset singleton settings")
|
||||
def step_reset_singleton_settings(context: Context) -> None:
|
||||
"""Reset the singleton cache for Settings.
|
||||
|
||||
Uses the ``reset()`` classmethod when available, falling back to
|
||||
direct ``_instance = None`` assignment to guard against Pydantic
|
||||
metaclass attribute-resolution edge cases during test runs.
|
||||
"""
|
||||
reset_fn = Settings.__dict__.get("reset")
|
||||
if reset_fn is not None:
|
||||
# Unwrap the classmethod descriptor and call.
|
||||
reset_fn.__func__(Settings)
|
||||
else:
|
||||
Settings._instance = None # type: ignore[assignment]
|
||||
|
||||
|
||||
@then("singleton settings instances should be different")
|
||||
def step_singleton_instances_should_be_different(context: Context) -> None:
|
||||
"""Assert reset causes a fresh singleton instance to be constructed."""
|
||||
instances = getattr(context, "singleton_instances", [])
|
||||
assert len(instances) >= 2, "Expected at least two singleton instances."
|
||||
assert instances[0] is not instances[-1]
|
||||
|
||||
|
||||
@then('singleton environment should be "{expected}"')
|
||||
def step_singleton_environment_should_be(context: Context, expected: str) -> None:
|
||||
"""Assert the latest singleton reflects the current environment value."""
|
||||
assert context.settings.environment == expected
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
*** Settings ***
|
||||
Documentation Regression guard tests for bug #647 - Container.resolve() crash
|
||||
... Three CLI commands (plan tree, plan explain, plan correct)
|
||||
... are regression-guarded after the historical
|
||||
... Container.resolve() crash was fixed.
|
||||
...
|
||||
... These tests use a REAL DI container (not MagicMock).
|
||||
... Existing M3 tests missed this class of issue because
|
||||
... they mock get_container() with MagicMock which auto-creates
|
||||
... any attribute.
|
||||
...
|
||||
... NOTE: Bug #647 appears fixed; these tests now run as normal
|
||||
... regression checks for correct command behavior.
|
||||
...
|
||||
... Tags: tdd_bug, tdd_bug_647
|
||||
Resource ${CURDIR}/common.resource
|
||||
Suite Setup Run Keywords Setup Test Environment AND Setup Database Isolation
|
||||
Suite Teardown Cleanup Test Environment
|
||||
|
||||
*** Variables ***
|
||||
${HELPER} ${CURDIR}/helper_container_resolve_crash.py
|
||||
|
||||
*** Test Cases ***
|
||||
Plan Tree Regression Guard For Container.resolve()
|
||||
[Documentation] Regression guard: plan tree should run without resolve() crash
|
||||
[Tags] tdd_bug tdd_bug_647
|
||||
${result}= Run Process ${PYTHON} ${HELPER} plan-tree-crash cwd=${WORKSPACE} timeout=30s
|
||||
Log ${result.stdout}
|
||||
Log ${result.stderr}
|
||||
Run Keyword If ${result.rc} == 2 Fail Unexpected non-AttributeError failure: ${result.stderr}
|
||||
# Helper exits 0 only when command behaves correctly.
|
||||
Should Be Equal As Integers ${result.rc} 0
|
||||
Should Contain ${result.stdout} plan-tree-crash-ok
|
||||
|
||||
Plan Explain Regression Guard For Container.resolve()
|
||||
[Documentation] Regression guard: plan explain should run without resolve() crash
|
||||
[Tags] tdd_bug tdd_bug_647
|
||||
${result}= Run Process ${PYTHON} ${HELPER} plan-explain-crash cwd=${WORKSPACE} timeout=30s
|
||||
Log ${result.stdout}
|
||||
Log ${result.stderr}
|
||||
Run Keyword If ${result.rc} == 2 Fail Unexpected non-AttributeError failure: ${result.stderr}
|
||||
# Helper exits 0 only when command behaves correctly.
|
||||
Should Be Equal As Integers ${result.rc} 0
|
||||
Should Contain ${result.stdout} plan-explain-crash-ok
|
||||
|
||||
Plan Correct Regression Guard For Container.resolve()
|
||||
[Documentation] Regression guard: plan correct should run without resolve() crash
|
||||
[Tags] tdd_bug tdd_bug_647
|
||||
${result}= Run Process ${PYTHON} ${HELPER} plan-correct-crash cwd=${WORKSPACE} timeout=30s
|
||||
Log ${result.stdout}
|
||||
Log ${result.stderr}
|
||||
Run Keyword If ${result.rc} == 2 Fail Unexpected non-AttributeError failure: ${result.stderr}
|
||||
# Helper exits 0 only when command behaves correctly.
|
||||
Should Be Equal As Integers ${result.rc} 0
|
||||
Should Contain ${result.stdout} plan-correct-crash-ok
|
||||
@@ -0,0 +1,310 @@
|
||||
"""Robot Framework helper for bug #647 regression tests.
|
||||
|
||||
Runs `plan tree`, `plan explain`, and `plan correct` through real CLI
|
||||
subprocesses with real DI/container wiring (no mocks).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from collections.abc import Callable
|
||||
from contextlib import suppress
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import NoReturn
|
||||
|
||||
_SRC = str(Path(__file__).resolve().parents[1] / "src")
|
||||
if _SRC not in sys.path:
|
||||
sys.path.insert(0, _SRC)
|
||||
|
||||
_ROBOT = str(Path(__file__).resolve().parent)
|
||||
if _ROBOT not in sys.path:
|
||||
sys.path.insert(0, _ROBOT)
|
||||
|
||||
from helper_e2e_common import run_cli # noqa: E402
|
||||
from ulid import ULID # noqa: E402
|
||||
|
||||
from cleveragents.application.container import reset_container # noqa: E402
|
||||
from cleveragents.application.services import decision_service as ds_mod # noqa: E402
|
||||
from cleveragents.application.services.plan_lifecycle_service import ( # noqa: E402
|
||||
PlanLifecycleService,
|
||||
)
|
||||
from cleveragents.config.settings import Settings # noqa: E402
|
||||
from cleveragents.domain.models.core.decision import ( # noqa: E402
|
||||
ContextSnapshot,
|
||||
DecisionType,
|
||||
ResourceRef,
|
||||
)
|
||||
from cleveragents.infrastructure.database.unit_of_work import UnitOfWork # noqa: E402
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DecisionContext:
|
||||
"""Data required to run and verify one CLI command."""
|
||||
|
||||
plan_id: str
|
||||
decision_id: str
|
||||
database_url: str
|
||||
db_path: str
|
||||
uow: UnitOfWork
|
||||
|
||||
|
||||
def _fail(message: str) -> NoReturn:
|
||||
"""Exit with expected regression-failure status."""
|
||||
print(f"FAIL: {message}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def _fail_unexpected(message: str) -> NoReturn:
|
||||
"""Exit with unexpected-infrastructure-failure status."""
|
||||
print(f"UNEXPECTED: {message}", file=sys.stderr)
|
||||
sys.exit(2)
|
||||
|
||||
|
||||
def _setup_decisions() -> DecisionContext:
|
||||
"""Create a real sqlite database and seed a small decision tree."""
|
||||
fd, db_path = tempfile.mkstemp(prefix="cr647_", suffix=".db")
|
||||
os.close(fd)
|
||||
database_url = f"sqlite:///{db_path}"
|
||||
os.environ["CLEVERAGENTS_DATABASE_URL"] = database_url
|
||||
|
||||
reset_container()
|
||||
uow = UnitOfWork(database_url)
|
||||
uow.init_database()
|
||||
|
||||
settings = Settings(database_url=database_url, async_enabled=False)
|
||||
decision_svc = ds_mod.DecisionService(
|
||||
settings=settings,
|
||||
unit_of_work=uow,
|
||||
)
|
||||
|
||||
lifecycle_svc = PlanLifecycleService(settings=settings, unit_of_work=uow)
|
||||
action_name = f"local/cr647-{ULID()!s}"
|
||||
lifecycle_svc.create_action(
|
||||
name=action_name,
|
||||
description="Container.resolve regression action",
|
||||
definition_of_done="Regression guard setup complete",
|
||||
strategy_actor="openai/gpt-4",
|
||||
execution_actor="openai/gpt-4",
|
||||
)
|
||||
plan = lifecycle_svc.use_action(action_name)
|
||||
plan_id = plan.identity.plan_id
|
||||
root = decision_svc.record_decision(
|
||||
plan_id=plan_id,
|
||||
decision_type=DecisionType.PROMPT_DEFINITION,
|
||||
question="What should we build?",
|
||||
chosen_option="A REST API",
|
||||
alternatives_considered=["GraphQL API", "gRPC service"],
|
||||
confidence_score=0.95,
|
||||
rationale="REST fits requirements",
|
||||
context_snapshot=ContextSnapshot(
|
||||
hot_context_hash="sha256:root",
|
||||
hot_context_ref="store://root",
|
||||
relevant_resources=[
|
||||
ResourceRef(resource_id=str(ULID()), path="src/main.py"),
|
||||
],
|
||||
actor_state_ref="checkpoint://root",
|
||||
),
|
||||
)
|
||||
child = decision_svc.record_decision(
|
||||
plan_id=plan_id,
|
||||
decision_type=DecisionType.STRATEGY_CHOICE,
|
||||
question="Which framework should we use?",
|
||||
chosen_option="FastAPI",
|
||||
parent_decision_id=root.decision_id,
|
||||
alternatives_considered=["Flask", "Django"],
|
||||
confidence_score=0.90,
|
||||
rationale="FastAPI fits async service requirements",
|
||||
context_snapshot=ContextSnapshot(
|
||||
hot_context_hash="sha256:child",
|
||||
hot_context_ref="store://child",
|
||||
relevant_resources=[
|
||||
ResourceRef(resource_id=str(ULID()), path="src/api.py"),
|
||||
],
|
||||
actor_state_ref="checkpoint://child",
|
||||
),
|
||||
)
|
||||
return DecisionContext(
|
||||
plan_id=plan_id,
|
||||
decision_id=child.decision_id,
|
||||
database_url=database_url,
|
||||
db_path=db_path,
|
||||
uow=uow,
|
||||
)
|
||||
|
||||
|
||||
def _cleanup(ctx: DecisionContext) -> None:
|
||||
"""Best-effort cleanup that does not mask later teardown steps."""
|
||||
with suppress(Exception):
|
||||
ctx.uow.engine.dispose()
|
||||
with suppress(Exception):
|
||||
reset_container()
|
||||
with suppress(Exception):
|
||||
os.environ.pop("CLEVERAGENTS_DATABASE_URL", None)
|
||||
for suffix in ("", "-journal", "-wal", "-shm"):
|
||||
with suppress(OSError):
|
||||
Path(ctx.db_path + suffix).unlink(missing_ok=True)
|
||||
# Keep reset last to avoid stale singleton recreation during cleanup.
|
||||
with suppress(Exception):
|
||||
Settings.reset()
|
||||
|
||||
|
||||
def _assert_tree_output(ctx: DecisionContext, output: str) -> None:
|
||||
"""Require tree output to include the seeded decision id."""
|
||||
if ctx.decision_id not in output:
|
||||
_fail(
|
||||
"Expected plan tree output to include seeded decision data.\n"
|
||||
f"Output:\n{output}"
|
||||
)
|
||||
|
||||
|
||||
def _assert_explain_output(ctx: DecisionContext, output: str) -> None:
|
||||
"""Require explain output to include seeded id and content."""
|
||||
if ctx.decision_id not in output:
|
||||
_fail(
|
||||
"Expected plan explain output to include seeded decision id.\n"
|
||||
f"Output:\n{output}"
|
||||
)
|
||||
if "FastAPI" not in output:
|
||||
_fail(
|
||||
"Expected plan explain output to include child chosen decision content "
|
||||
"(FastAPI).\n"
|
||||
f"Output:\n{output}"
|
||||
)
|
||||
|
||||
|
||||
def _assert_correct_output(ctx: DecisionContext, output: str) -> None:
|
||||
"""Require correct output to reference the seeded decision id and mode."""
|
||||
if ctx.decision_id not in output:
|
||||
_fail(
|
||||
"Expected plan correct output to include seeded decision id.\n"
|
||||
f"Output:\n{output}"
|
||||
)
|
||||
# Validate command-specific content: dry-run mode or revert indication.
|
||||
lowered = output.lower()
|
||||
if "revert" not in lowered and "dry" not in lowered:
|
||||
_fail(
|
||||
"Expected plan correct output to reference revert mode or dry-run.\n"
|
||||
f"Output:\n{output}"
|
||||
)
|
||||
|
||||
|
||||
def _run_and_verify(
|
||||
*,
|
||||
label: str,
|
||||
cli_args_factory: Callable[[DecisionContext], list[str]],
|
||||
positive_assertion: Callable[[DecisionContext, str], None],
|
||||
) -> None:
|
||||
"""Run one command end-to-end and enforce regression invariants."""
|
||||
ctx = _setup_decisions()
|
||||
try:
|
||||
args = cli_args_factory(ctx)
|
||||
result = run_cli(
|
||||
*args,
|
||||
workspace=str(Path.cwd()),
|
||||
env_extra={"CLEVERAGENTS_DATABASE_URL": ctx.database_url},
|
||||
timeout=25,
|
||||
)
|
||||
output = (result.stdout or "") + (result.stderr or "")
|
||||
lowered = output.lower()
|
||||
|
||||
if result.returncode != 0:
|
||||
if "attributeerror" in lowered and "has no attribute 'resolve'" in lowered:
|
||||
_fail(
|
||||
f"{label} unexpectedly reproduced resolve() crash.\n"
|
||||
f"rc={result.returncode}\n{output}"
|
||||
)
|
||||
_fail_unexpected(
|
||||
f"{label} failed with unexpected non-zero exit.\n"
|
||||
f"rc={result.returncode}\n{output}"
|
||||
)
|
||||
|
||||
if "attributeerror" in lowered or "has no attribute 'resolve'" in lowered:
|
||||
_fail(
|
||||
f"{label} printed resolve() crash text despite exit 0.\n"
|
||||
f"Output:\n{output}"
|
||||
)
|
||||
|
||||
positive_assertion(ctx, output)
|
||||
print(f"{label}-ok")
|
||||
finally:
|
||||
_cleanup(ctx)
|
||||
|
||||
|
||||
def plan_tree_crash() -> None:
|
||||
"""Regression guard for ``plan tree`` DI-container path."""
|
||||
_run_and_verify(
|
||||
label="plan-tree-crash",
|
||||
cli_args_factory=lambda ctx: ["plan", "tree", ctx.plan_id, "--format", "json"],
|
||||
positive_assertion=_assert_tree_output,
|
||||
)
|
||||
|
||||
|
||||
def plan_explain_crash() -> None:
|
||||
"""Regression guard for ``plan explain`` DI-container path."""
|
||||
_run_and_verify(
|
||||
label="plan-explain-crash",
|
||||
cli_args_factory=lambda ctx: [
|
||||
"plan",
|
||||
"explain",
|
||||
ctx.decision_id,
|
||||
"--show-context",
|
||||
"--show-reasoning",
|
||||
"--format",
|
||||
"json",
|
||||
],
|
||||
positive_assertion=_assert_explain_output,
|
||||
)
|
||||
|
||||
|
||||
def plan_correct_crash() -> None:
|
||||
"""Regression guard for ``plan correct`` DI-container path."""
|
||||
_run_and_verify(
|
||||
label="plan-correct-crash",
|
||||
cli_args_factory=lambda ctx: [
|
||||
"plan",
|
||||
"correct",
|
||||
ctx.decision_id,
|
||||
"--mode",
|
||||
"revert",
|
||||
"--guidance",
|
||||
"Use Django instead",
|
||||
# NOTE: ``--plan`` is required by implementation when there is no
|
||||
# active-plan CLI context, though spec examples show it omitted.
|
||||
# See spec-vs-implementation drift tracked separately.
|
||||
"--plan",
|
||||
ctx.plan_id,
|
||||
"--dry-run",
|
||||
"--format",
|
||||
"json",
|
||||
],
|
||||
positive_assertion=_assert_correct_output,
|
||||
)
|
||||
|
||||
|
||||
_COMMANDS: dict[str, Callable[[], None]] = {
|
||||
"plan-tree-crash": plan_tree_crash,
|
||||
"plan-explain-crash": plan_explain_crash,
|
||||
"plan-correct-crash": plan_correct_crash,
|
||||
}
|
||||
|
||||
|
||||
def main() -> int:
|
||||
"""Dispatch helper command from Robot ``Run Process``."""
|
||||
if len(sys.argv) < 2:
|
||||
print(f"Usage: helper_container_resolve_crash.py <{'|'.join(_COMMANDS)}>")
|
||||
return 1
|
||||
|
||||
handler = _COMMANDS.get(sys.argv[1])
|
||||
if handler is None:
|
||||
print(f"Unknown command: {sys.argv[1]}")
|
||||
return 1
|
||||
|
||||
handler()
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -630,6 +630,19 @@ class Settings(BaseSettings):
|
||||
cls._instance = cls()
|
||||
return cls._instance
|
||||
|
||||
@classmethod
|
||||
def reset(cls: type[Settings]) -> None:
|
||||
"""Reset the cached singleton instance.
|
||||
|
||||
Ensures the next ``get_settings()`` call constructs a fresh
|
||||
instance instead of reusing stale process-global state.
|
||||
|
||||
.. warning:: **Test use only.** Do not call in production code
|
||||
paths — resetting the singleton mid-flight can cause
|
||||
inconsistent configuration state.
|
||||
"""
|
||||
cls._instance = None
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Derived properties & helpers
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user