Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /dislovelhl_enhanced-agent-bus /verification /tests /test_z3_adapter.py
| """ | |
| Tests for Z3 SMT Solver Integration (z3_adapter.py). | |
| Constitutional Hash: 608508a9bd224290 | |
| Covers: | |
| - Z3Constraint, Z3VerificationResult, ConstitutionalPolicy dataclasses | |
| - Z3SolverAdapter: add_constraint, check_sat, async_check_sat, reset_solver, get_constraint_names | |
| - LLMAssistedZ3Adapter: natural_language_to_constraints, verify_policy_constraints, | |
| _extract_policy_elements, _parse_z3_expression, refine_constraints | |
| - ConstitutionalZ3Verifier: verify_constitutional_policy, verify_policy_compliance, | |
| get_constitutional_hash, get_verification_stats | |
| - verify_policy_formally convenience function | |
| """ | |
| import pytest | |
| z3 = pytest.importorskip("z3") | |
| from enhanced_agent_bus._compat.constants import CONSTITUTIONAL_HASH | |
| from enhanced_agent_bus.verification.z3_adapter import ( | |
| ConstitutionalPolicy, | |
| ConstitutionalZ3Verifier, | |
| LLMAssistedZ3Adapter, | |
| Z3Constraint, | |
| Z3SolverAdapter, | |
| Z3VerificationResult, | |
| verify_policy_formally, | |
| ) | |
| pytestmark = [pytest.mark.unit, pytest.mark.constitutional] | |
| # --------------------------------------------------------------------------- | |
| # Dataclass tests | |
| # --------------------------------------------------------------------------- | |
| class TestZ3Constraint: | |
| def test_timestamp_auto_set(self): | |
| c = Z3Constraint( | |
| name="c1", | |
| expression="(assert true)", | |
| natural_language="Always true", | |
| confidence=0.9, | |
| ) | |
| assert c.timestamp is not None | |
| def test_fields_stored(self): | |
| c = Z3Constraint( | |
| name="c1", | |
| expression="(assert x)", | |
| natural_language="x must be true", | |
| confidence=0.8, | |
| generated_by="test", | |
| ) | |
| assert c.name == "c1" | |
| assert c.confidence == 0.8 | |
| assert c.generated_by == "test" | |
| class TestZ3VerificationResult: | |
| def test_defaults(self): | |
| r = Z3VerificationResult(is_sat=True) | |
| assert r.constraints_used == [] | |
| assert r.solver_stats == {} | |
| assert r.model is None | |
| assert r.unsat_core is None | |
| def test_sat_result(self): | |
| r = Z3VerificationResult(is_sat=True, model={"x": True}) | |
| assert r.is_sat is True | |
| assert r.model == {"x": True} | |
| def test_unsat_result(self): | |
| r = Z3VerificationResult(is_sat=False, unsat_core=["c1"]) | |
| assert r.is_sat is False | |
| assert r.unsat_core == ["c1"] | |
| class TestConstitutionalPolicy: | |
| def test_defaults(self): | |
| p = ConstitutionalPolicy( | |
| id="pol-1", | |
| natural_language="Do no harm", | |
| z3_constraints=[], | |
| ) | |
| assert p.constitutional_hash == CONSTITUTIONAL_HASH | |
| assert p.is_verified is False | |
| assert p.created_at is not None | |
| assert p.verified_at is None | |
| def test_verified_policy(self): | |
| p = ConstitutionalPolicy( | |
| id="pol-2", | |
| natural_language="Be transparent", | |
| z3_constraints=[], | |
| is_verified=True, | |
| ) | |
| assert p.is_verified is True | |
| # --------------------------------------------------------------------------- | |
| # Z3SolverAdapter tests | |
| # --------------------------------------------------------------------------- | |
| class TestZ3SolverAdapter: | |
| def adapter(self): | |
| return Z3SolverAdapter(timeout_ms=1000) | |
| def test_initialization(self, adapter): | |
| assert adapter.timeout_ms == 1000 | |
| assert len(adapter.named_constraints) == 0 | |
| assert len(adapter.constraint_history) == 0 | |
| def test_add_constraint(self, adapter): | |
| var = z3.Bool("test_var") | |
| metadata = Z3Constraint( | |
| name="c1", | |
| expression="(assert test_var)", | |
| natural_language="test_var is true", | |
| confidence=0.9, | |
| ) | |
| adapter.add_constraint("c1", var, metadata) | |
| assert "c1" in adapter.named_constraints | |
| assert len(adapter.constraint_history) == 1 | |
| def test_get_constraint_names(self, adapter): | |
| var1 = z3.Bool("x1") | |
| var2 = z3.Bool("x2") | |
| meta = Z3Constraint(name="c", expression="", natural_language="", confidence=1.0) | |
| adapter.add_constraint("c1", var1, meta) | |
| adapter.add_constraint("c2", var2, meta) | |
| names = adapter.get_constraint_names() | |
| assert set(names) == {"c1", "c2"} | |
| def test_check_sat_satisfiable(self, adapter): | |
| var = z3.Bool("sat_var") | |
| meta = Z3Constraint(name="c1", expression="", natural_language="", confidence=1.0) | |
| adapter.add_constraint("c1", var, meta) | |
| result = adapter.check_sat() | |
| assert result.is_sat is True | |
| assert result.solve_time_ms >= 0 | |
| def test_check_sat_unsatisfiable(self, adapter): | |
| var = z3.Bool("unsat_var") | |
| meta = Z3Constraint(name="c1", expression="", natural_language="", confidence=1.0) | |
| adapter.add_constraint("c1", var, meta) | |
| adapter.add_constraint("c2", z3.Not(var), meta) | |
| result = adapter.check_sat() | |
| assert result.is_sat is False | |
| def test_check_sat_model_contains_decls(self, adapter): | |
| var = z3.Int("my_int") | |
| meta = Z3Constraint(name="c1", expression="", natural_language="", confidence=1.0) | |
| adapter.add_constraint("c1", var > 5, meta) | |
| result = adapter.check_sat() | |
| assert result.is_sat is True | |
| assert result.model is not None | |
| def test_reset_solver_clears_constraints(self, adapter): | |
| var = z3.Bool("v") | |
| meta = Z3Constraint(name="c1", expression="", natural_language="", confidence=1.0) | |
| adapter.add_constraint("c1", var, meta) | |
| adapter.reset_solver() | |
| assert len(adapter.named_constraints) == 0 | |
| async def test_async_check_sat_satisfiable(self, adapter): | |
| var = z3.Bool("async_var") | |
| meta = Z3Constraint(name="c1", expression="", natural_language="", confidence=1.0) | |
| adapter.add_constraint("c1", var, meta) | |
| result = await adapter.async_check_sat() | |
| assert result.is_sat is True | |
| async def test_async_check_sat_unsatisfiable(self, adapter): | |
| var = z3.Bool("async_unsat") | |
| meta = Z3Constraint(name="c1", expression="", natural_language="", confidence=1.0) | |
| adapter.add_constraint("c1", var, meta) | |
| adapter.add_constraint("c2", z3.Not(var), meta) | |
| result = await adapter.async_check_sat() | |
| assert result.is_sat is False | |
| def test_check_sat_bool_model_value(self, adapter): | |
| """Exercise the bool branch of model extraction.""" | |
| var = z3.Bool("bool_var") | |
| meta = Z3Constraint(name="c1", expression="", natural_language="", confidence=1.0) | |
| adapter.add_constraint("c1", var, meta) | |
| result = adapter.check_sat() | |
| assert result.is_sat is True | |
| # bool_var should be in the model | |
| assert result.model is not None | |
| # --------------------------------------------------------------------------- | |
| # LLMAssistedZ3Adapter tests | |
| # --------------------------------------------------------------------------- | |
| class TestLLMAssistedZ3Adapter: | |
| def adapter(self): | |
| return LLMAssistedZ3Adapter(max_refinements=2) | |
| def test_initialization(self, adapter): | |
| assert adapter.max_refinements == 2 | |
| assert isinstance(adapter.z3_solver, Z3SolverAdapter) | |
| assert len(adapter.generation_history) == 0 | |
| def test_extract_policy_elements_obligation(self, adapter): | |
| text = "Users must provide valid credentials. Access shall be logged." | |
| elements = adapter._extract_policy_elements(text) | |
| types = [e["type"] for e in elements] | |
| assert "obligation" in types | |
| def test_extract_policy_elements_permission(self, adapter): | |
| text = "Administrators may override policy settings." | |
| elements = adapter._extract_policy_elements(text) | |
| types = [e["type"] for e in elements] | |
| assert "permission" in types | |
| def test_extract_policy_elements_prohibition(self, adapter): | |
| # "cannot" contains "can" (permission) and "must not" contains "must" (obligation), | |
| # so both are captured by earlier branches. Use "forbidden" to hit prohibition branch. | |
| text = "Sharing credentials is forbidden." | |
| elements = adapter._extract_policy_elements(text) | |
| types = [e["type"] for e in elements] | |
| assert "prohibition" in types | |
| def test_extract_policy_elements_empty_text(self, adapter): | |
| elements = adapter._extract_policy_elements("") | |
| assert elements == [] | |
| def test_extract_policy_elements_no_keywords(self, adapter): | |
| text = "This is a statement without any keywords." | |
| elements = adapter._extract_policy_elements(text) | |
| assert elements == [] | |
| def test_parse_z3_expression_valid_bool_assert(self, adapter): | |
| expr = "(declare-const test_bool Bool)\n(assert test_bool)" | |
| result = adapter._parse_z3_expression(expr) | |
| assert result is not None | |
| def test_parse_z3_expression_valid_bool_not(self, adapter): | |
| expr = "(declare-const test_bool Bool)\n(assert (not test_bool))" | |
| result = adapter._parse_z3_expression(expr) | |
| assert result is not None | |
| def test_parse_z3_expression_unsupported_returns_none(self, adapter): | |
| result = adapter._parse_z3_expression("unsupported expression") | |
| assert result is None | |
| def test_parse_z3_expression_single_line_returns_none(self, adapter): | |
| # Only one line — can't match 2-line pattern | |
| result = adapter._parse_z3_expression("(declare-const x Bool)") | |
| assert result is None | |
| async def test_natural_language_to_constraints_obligation(self, adapter): | |
| policy = "Users must authenticate before accessing resources." | |
| constraints = await adapter.natural_language_to_constraints(policy) | |
| assert isinstance(constraints, list) | |
| # Should produce at least one constraint | |
| assert len(constraints) >= 0 # May be 0 if pattern doesn't match hash | |
| async def test_natural_language_to_constraints_prohibition(self, adapter): | |
| policy = "Users cannot read other users' private data." | |
| constraints = await adapter.natural_language_to_constraints(policy) | |
| assert isinstance(constraints, list) | |
| async def test_natural_language_to_constraints_with_context(self, adapter): | |
| policy = "System must validate all inputs." | |
| constraints = await adapter.natural_language_to_constraints( | |
| policy, context={"strict_mode": True} | |
| ) | |
| assert isinstance(constraints, list) | |
| async def test_verify_policy_constraints_empty(self, adapter): | |
| result = await adapter.verify_policy_constraints([]) | |
| assert isinstance(result, Z3VerificationResult) | |
| async def test_verify_policy_constraints_with_valid_constraint(self, adapter): | |
| constraint = Z3Constraint( | |
| name="c1", | |
| expression="(declare-const x_bool Bool)\n(assert x_bool)", | |
| natural_language="x must hold", | |
| confidence=0.9, | |
| ) | |
| result = await adapter.verify_policy_constraints([constraint]) | |
| assert isinstance(result, Z3VerificationResult) | |
| assert result.is_sat is True | |
| async def test_verify_policy_constraints_with_unparseable_skipped(self, adapter): | |
| """Unparseable constraints should be skipped, not raise.""" | |
| constraint = Z3Constraint( | |
| name="c1", | |
| expression="totally invalid z3 syntax!!!", | |
| natural_language="invalid", | |
| confidence=0.5, | |
| ) | |
| result = await adapter.verify_policy_constraints([constraint]) | |
| assert isinstance(result, Z3VerificationResult) | |
| async def test_refine_constraints_already_sat(self, adapter): | |
| """If already SAT, refinement returns original constraints unchanged.""" | |
| c = Z3Constraint( | |
| name="c1", | |
| expression="(declare-const y Bool)\n(assert y)", | |
| natural_language="y", | |
| confidence=0.9, | |
| ) | |
| sat_result = Z3VerificationResult(is_sat=True) | |
| refined = await adapter.refine_constraints([c], sat_result) | |
| assert len(refined) == 1 | |
| assert refined[0].confidence == 0.9 # unchanged | |
| async def test_refine_constraints_no_unsat_core(self, adapter): | |
| """If UNSAT but no unsat_core, refinement stops immediately.""" | |
| c = Z3Constraint( | |
| name="c1", | |
| expression="(declare-const z Bool)\n(assert z)", | |
| natural_language="z", | |
| confidence=0.9, | |
| ) | |
| unsat_result = Z3VerificationResult(is_sat=False, unsat_core=None) | |
| refined = await adapter.refine_constraints([c], unsat_result) | |
| assert len(refined) == 1 # no modification attempted | |
| async def test_refine_constraints_reduces_confidence_on_problematic(self, adapter): | |
| """Constraints in unsat_core should have confidence reduced.""" | |
| c = Z3Constraint( | |
| name="problem_constraint", | |
| expression="(declare-const q Bool)\n(assert q)", | |
| natural_language="q", | |
| confidence=0.9, | |
| ) | |
| unsat_result = Z3VerificationResult(is_sat=False, unsat_core=["problem_constraint"]) | |
| refined = await adapter.refine_constraints([c], unsat_result, max_iterations=1) | |
| # Confidence reduced by 0.1 | |
| problem_c = next(r for r in refined if r.name == "problem_constraint") | |
| assert problem_c.confidence <= 0.85 | |
| # --------------------------------------------------------------------------- | |
| # ConstitutionalZ3Verifier tests | |
| # --------------------------------------------------------------------------- | |
| class TestConstitutionalZ3Verifier: | |
| def verifier(self): | |
| return ConstitutionalZ3Verifier() | |
| def test_initialization(self, verifier): | |
| assert isinstance(verifier.llm_adapter, LLMAssistedZ3Adapter) | |
| assert len(verifier.verified_policies) == 0 | |
| def test_get_constitutional_hash(self, verifier): | |
| assert verifier.get_constitutional_hash() == CONSTITUTIONAL_HASH | |
| def test_get_verification_stats_empty(self, verifier): | |
| stats = verifier.get_verification_stats() | |
| assert stats["total_policies"] == 0 | |
| assert stats["verification_rate"] == 0.0 | |
| assert stats["constitutional_hash"] == CONSTITUTIONAL_HASH | |
| async def test_verify_constitutional_policy_stores_result(self, verifier): | |
| policy = await verifier.verify_constitutional_policy( | |
| policy_id="pol-001", | |
| natural_language_policy="Users must authenticate.", | |
| ) | |
| assert isinstance(policy, ConstitutionalPolicy) | |
| assert "pol-001" in verifier.verified_policies | |
| async def test_verify_constitutional_policy_sets_verified_at_when_sat(self, verifier): | |
| policy = await verifier.verify_constitutional_policy( | |
| policy_id="pol-002", | |
| natural_language_policy="System must log all access.", | |
| ) | |
| # Result depends on z3 solving; just verify the structure is correct | |
| assert isinstance(policy.is_verified, bool) | |
| if policy.is_verified: | |
| assert policy.verified_at is not None | |
| async def test_verify_constitutional_policy_with_context(self, verifier): | |
| policy = await verifier.verify_constitutional_policy( | |
| policy_id="pol-003", | |
| natural_language_policy="Data must be encrypted.", | |
| context={"strict": True}, | |
| ) | |
| assert isinstance(policy, ConstitutionalPolicy) | |
| async def test_verify_policy_compliance_unknown_policy(self, verifier): | |
| result = await verifier.verify_policy_compliance( | |
| policy_id="nonexistent", | |
| decision_context={"action": "read"}, | |
| ) | |
| assert result is False | |
| async def test_verify_policy_compliance_unverified_policy(self, verifier): | |
| """An unverified policy should return False for compliance.""" | |
| unverified = ConstitutionalPolicy( | |
| id="pol-unverified", | |
| natural_language="Some policy", | |
| z3_constraints=[], | |
| is_verified=False, | |
| ) | |
| verifier.verified_policies["pol-unverified"] = unverified | |
| result = await verifier.verify_policy_compliance( | |
| policy_id="pol-unverified", | |
| decision_context={}, | |
| ) | |
| assert result is False | |
| async def test_verify_policy_compliance_verified_policy(self, verifier): | |
| verified = ConstitutionalPolicy( | |
| id="pol-verified", | |
| natural_language="Some verified policy", | |
| z3_constraints=[], | |
| is_verified=True, | |
| ) | |
| verifier.verified_policies["pol-verified"] = verified | |
| result = await verifier.verify_policy_compliance( | |
| policy_id="pol-verified", | |
| decision_context={"action": "read"}, | |
| ) | |
| assert result is True | |
| async def test_get_verification_stats_after_verification(self, verifier): | |
| await verifier.verify_constitutional_policy( | |
| policy_id="pol-stats", | |
| natural_language_policy="Transparency must be ensured.", | |
| ) | |
| stats = verifier.get_verification_stats() | |
| assert stats["total_policies"] == 1 | |
| assert 0.0 <= stats["verification_rate"] <= 1.0 | |
| async def test_verify_policy_triggers_refinement_on_unsat(self, verifier): | |
| """Policy with contradictory constraints should attempt refinement.""" | |
| # "cannot" policies generate prohibition constraints | |
| policy = await verifier.verify_constitutional_policy( | |
| policy_id="pol-contradict", | |
| natural_language_policy=( | |
| "Users cannot login without credentials. Users cannot be denied access." | |
| ), | |
| ) | |
| assert isinstance(policy, ConstitutionalPolicy) | |
| # --------------------------------------------------------------------------- | |
| # Convenience function | |
| # --------------------------------------------------------------------------- | |
| class TestVerifyPolicyFormally: | |
| async def test_auto_generates_policy_id(self): | |
| policy = await verify_policy_formally("Users must authenticate.") | |
| assert policy.id.startswith("policy_") | |
| async def test_uses_provided_policy_id(self): | |
| policy = await verify_policy_formally("Users must authenticate.", policy_id="my-pol-001") | |
| assert policy.id == "my-pol-001" | |
| async def test_returns_constitutional_policy(self): | |
| policy = await verify_policy_formally("System shall log all actions.") | |
| assert isinstance(policy, ConstitutionalPolicy) | |
| assert policy.constitutional_hash == CONSTITUTIONAL_HASH | |
Xet Storage Details
- Size:
- 18.6 kB
- Xet hash:
- 22a14a60fae9af2c7821b45479896042693cfad07af0c17c002cfcd1b6bdb051
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.