Files
temp/features/steps/a2a_jsonrpc_wire_format_steps.py
freemo 02250473ad fix(ci): restore all CI quality gates to passing on master
Fix all failing CI quality gates (lint, unit_tests, format) without
suppressing any quality enforcement.

Root causes and fixes:

1. Format: features/steps/plan_namespaced_name_tdd_steps.py had trailing
   whitespace; fixed by running ruff format.

2. Unit tests - A2A JSON-RPC 2.0 migration (commit 9c6d6915) renamed
   A2aRequest fields (operation→method, request_id→id, a2a_version→jsonrpc)
   and A2aResponse fields (status+data→result, request_id→id) but did not
   update all step files and feature files:
   - a2a_jsonrpc_wire_format_steps.py: added use_step_matcher('re') and
     reset to 'parse' at end to prevent parallel test interference
   - a2a_facade_wiring_steps.py: updated operation= to method=, .status/.data
     to .result
   - a2a_facade_steps.py: updated request_id→id, a2a_version→jsonrpc,
     A2aResponse(request_id=..., status=...) to new API
   - m6_facade_steps.py: updated all old API usage
   - devcontainer_cleanup_steps.py: updated A2aRequest(operation=...)
   - plan_prompt_command_steps.py: updated A2aRequest(operation=...)
   - wf03_plan_prompt_confidence_steps.py: updated A2aRequest(operation=...)
   - consolidated_misc.feature: updated old A2aRequest/A2aResponse scenarios

3. Unit tests - Session CLI output changed (commit 0d5d9cf0 and others):
   - 'Session Created' → 'Session created' (lowercase)
   - 'Session Details' → 'Session Summary'
   - 'Sessions (N total)' → 'Sessions'
   - session list JSON: top-level 'total' → nested 'summary.total'
   - Fixed in: session_cli.feature, session_cli_coverage_boost.feature,
     session_cli_uncovered_branches.feature, session_list_error.feature,
     tdd_session_create_persist_steps.py

4. Unit tests - Plan list output changed (commit 1a07a891):
   - 'V3 Lifecycle Plans' → 'Plans'
   - 'Lifecycle Plans' → 'Plans'
   - Name column removed (restored in source)
   - Invariants column removed (restored in source)
   - Project truncation removed (restored in source)
   - Fixed in: plan_cli_cancel_revert_coverage.feature,
     plan_lifecycle_cli_coverage.feature, plan_cli_coverage_boost_steps.py,
     plan.py (source code restored)

5. Unit tests - Plan apply command now requires ULID (commit 300a5d6d):
   - plan_cli_coverage_r3.feature: updated 'PLAN-001' to valid ULID
   - plan_cli_coverage_r3_steps.py: added --yes flag, added new step for
     no-eligible-plans path

6. Unit tests - Various source code bugs:
   - ThoughtBlock: converted from @dataclass to Pydantic BaseModel
     (architecture test requires all dataclasses to use Pydantic)
   - session.py: added DatabaseError handling to export, import, tell commands
   - database.py: fixed rollback_to() to reuse checkpoint connection for writes
   - database.py: added _get_checkpoint_conn() helper
   - check-tls-cert.py: fixed SSLCertVerificationError.reason AttributeError

7. Unit tests - Test step bugs:
   - error_recovery_coverage_boost_steps.py: fixed invalid ULID _PLAN_ID
   - session_service_coverage_steps.py: fixed 'sha256:' prefix bug in checksum
   - database_models_new_coverage_steps.py: added 'name' field to session mock
   - async_audit_recording_steps.py: fixed Settings(audit_async=False) via env var
   - coverage_threshold_config_steps.py: added --coverage-min pattern support
   - m5_acms_smoke_steps.py: updated usage hint text
   - actor_cli_yaml_steps.py: updated 'Removed actor' → 'Actor removed'
   - aimodelscredentials_steps.py: set context.imported_class in import step
   - domain_base_model.feature: added missing 'When I examine model_config' step
   - tui_first_run_steps.py: fixed module reload to restore cleveragents.tui.*
     modules after test (prevented patch interference in subsequent tests)
   - tui_first_run_steps.py: added set_search('') step for empty string
   - resource_handler_base_coverage_r3_steps.py: use _MinimalHandler instead
     of DatabaseResourceHandler for NotImplementedError tests
   - resource_handler_crud.feature: updated to test new DatabaseHandler behavior
   - resource_handler_sandbox.feature: updated to test new DatabaseHandler behavior
   - tdd_json_decode_crash_persistence.feature: fixed @tdd_bug → @tdd_issue tags

8. Parallel test interference:
   - All step files using use_step_matcher('re') now reset to 'parse' at end
     to prevent global matcher state leaking to subsequent step files
2026-04-04 20:38:16 +00:00

355 lines
12 KiB
Python

"""Step definitions for a2a_jsonrpc_wire_format.feature.
Verifies that A2aRequest and A2aResponse models use JSON-RPC 2.0
compliant field names on the wire, and that the A2aLocalFacade
produces compliant responses.
"""
from __future__ import annotations
import json
from behave import given, then, use_step_matcher, when
from behave.runner import Context
from pydantic import ValidationError
use_step_matcher("re")
try:
from cleveragents.a2a.facade import A2aLocalFacade
from cleveragents.a2a.models import (
A2aErrorDetail,
A2aRequest,
A2aResponse,
)
except ImportError:
A2aLocalFacade = None # type: ignore[assignment,misc]
A2aRequest = None # type: ignore[assignment,misc]
A2aResponse = None # type: ignore[assignment,misc]
A2aErrorDetail = None # type: ignore[assignment,misc]
# ---------------------------------------------------------------------------
# A2aRequest — construction and serialisation
# ---------------------------------------------------------------------------
@given(
r'a valid A2aRequest with method "(?P<method>[^"]+)" and params (?P<params_json>.+)'
)
def step_create_request(context: Context, method: str, params_json: str) -> None:
params = json.loads(params_json)
context.request = A2aRequest(method=method, params=params)
@given(r'an A2aRequest with method "(?P<method>[^"]+)" and id "(?P<req_id>[^"]+)"')
def step_create_request_with_id(context: Context, method: str, req_id: str) -> None:
context.request = A2aRequest(method=method, id=req_id)
@when("I serialise the request to a dict")
def step_serialise_request(context: Context) -> None:
context.serialised = context.request.model_dump()
@then(
r'the serialised dict should contain key "(?P<key>[^"]+)" with value "(?P<value>[^"]+)"'
)
def step_dict_has_key_value(context: Context, key: str, value: str) -> None:
assert key in context.serialised, (
f"Expected key '{key}' in serialised dict, got keys: {list(context.serialised.keys())}"
)
assert str(context.serialised[key]) == value, (
f"Expected serialised['{key}'] = '{value}', got '{context.serialised[key]}'"
)
@then(r'the serialised dict should contain key "(?P<key>[^"]+)"')
def step_dict_has_key(context: Context, key: str) -> None:
assert key in context.serialised, (
f"Expected key '{key}' in serialised dict, got keys: {list(context.serialised.keys())}"
)
@then(r'the serialised dict should not contain key "(?P<key>[^"]+)"')
def step_dict_no_key(context: Context, key: str) -> None:
assert key not in context.serialised, (
f"Expected key '{key}' NOT in serialised dict, but it was present with value: "
f"{context.serialised.get(key)!r}"
)
@then("the request id should be non-empty")
def step_request_id_non_empty(context: Context) -> None:
assert context.request.id, (
f"Expected non-empty request id, got: {context.request.id!r}"
)
@then(r'the request id should equal "(?P<value>[^"]+)"')
def step_request_id_equals(context: Context, value: str) -> None:
assert context.request.id == value, (
f"Expected request id '{value}', got '{context.request.id}'"
)
@when("I try to create an A2aRequest with empty method")
def step_create_request_empty_method(context: Context) -> None:
context.caught_error = None
try:
A2aRequest(method="")
except (ValidationError, ValueError) as exc:
context.caught_error = exc
@when(r'I try to create an A2aRequest with jsonrpc "(?P<version>[^"]+)"')
def step_create_request_bad_jsonrpc(context: Context, version: str) -> None:
context.caught_error = None
try:
A2aRequest(method="test/method", jsonrpc=version)
except (ValidationError, ValueError) as exc:
context.caught_error = exc
@then("a wire format ValidationError should be raised")
def step_validation_error_raised(context: Context) -> None:
assert context.caught_error is not None, "Expected a ValidationError to be raised"
assert isinstance(context.caught_error, (ValidationError, ValueError)), (
f"Expected ValidationError or ValueError, got {type(context.caught_error)}"
)
# ---------------------------------------------------------------------------
# A2aResponse — construction and serialisation (success)
# ---------------------------------------------------------------------------
@given(
r'a successful A2aResponse with id "(?P<resp_id>[^"]+)" and result (?P<result_json>.+)'
)
def step_create_success_response(
context: Context, resp_id: str, result_json: str
) -> None:
result = json.loads(result_json)
context.response = A2aResponse(id=resp_id, result=result)
@when("I serialise the response to a dict")
def step_serialise_response(context: Context) -> None:
context.serialised = context.response.model_dump(exclude_none=True)
# ---------------------------------------------------------------------------
# A2aResponse — construction and serialisation (error)
# ---------------------------------------------------------------------------
@given(
r'an error A2aResponse with id "(?P<resp_id>[^"]+)" and error code "(?P<code>[^"]+)"'
)
def step_create_error_response(context: Context, resp_id: str, code: str) -> None:
context.response = A2aResponse(
id=resp_id,
error=A2aErrorDetail(code=code, message="Resource not found"),
)
# ---------------------------------------------------------------------------
# A2aResponse — validation
# ---------------------------------------------------------------------------
@when("I try to create an A2aResponse with neither result nor error")
def step_create_response_no_result_no_error(context: Context) -> None:
context.caught_error = None
try:
A2aResponse(id="test-id")
except (ValidationError, ValueError) as exc:
context.caught_error = exc
@when("I try to create an A2aResponse with both result and error")
def step_create_response_both_result_and_error(context: Context) -> None:
context.caught_error = None
try:
A2aResponse(
id="test-id",
result={"status": "ok"},
error=A2aErrorDetail(code="ERR", message="oops"),
)
except (ValidationError, ValueError) as exc:
context.caught_error = exc
# ---------------------------------------------------------------------------
# Deserialisation — inbound JSON-RPC 2.0 payloads
# ---------------------------------------------------------------------------
@given(
r'a JSON-RPC 2.0 request dict with method "(?P<method>[^"]+)" and id "(?P<req_id>[^"]+)"'
)
def step_jsonrpc_request_dict(context: Context, method: str, req_id: str) -> None:
context.raw_dict = {
"jsonrpc": "2.0",
"id": req_id,
"method": method,
"params": {},
}
@when("I deserialise the dict into an A2aRequest")
def step_deserialise_request(context: Context) -> None:
context.request = A2aRequest.model_validate(context.raw_dict)
@then(r'the request method should equal "(?P<value>[^"]+)"')
def step_request_method_equals(context: Context, value: str) -> None:
assert context.request.method == value, (
f"Expected method '{value}', got '{context.request.method}'"
)
@then(r'the request jsonrpc should equal "(?P<value>[^"]+)"')
def step_request_jsonrpc_equals(context: Context, value: str) -> None:
assert context.request.jsonrpc == value, (
f"Expected jsonrpc '{value}', got '{context.request.jsonrpc}'"
)
@given(
r'a JSON-RPC 2.0 success response dict with id "(?P<resp_id>[^"]+)" and result (?P<result_json>.+)'
)
def step_jsonrpc_success_response_dict(
context: Context, resp_id: str, result_json: str
) -> None:
result = json.loads(result_json)
context.raw_dict = {
"jsonrpc": "2.0",
"id": resp_id,
"result": result,
}
@when("I deserialise the dict into an A2aResponse")
def step_deserialise_response(context: Context) -> None:
context.response = A2aResponse.model_validate(context.raw_dict)
@then(r'the response result should contain key "(?P<key>[^"]+)"')
def step_response_result_has_key(context: Context, key: str) -> None:
assert context.response.result is not None, "Expected result to be set"
assert key in context.response.result, (
f"Expected key '{key}' in result, got: {list(context.response.result.keys())}"
)
@then("the response error should be None")
def step_response_error_none(context: Context) -> None:
assert context.response.error is None, (
f"Expected error to be None, got: {context.response.error}"
)
@given(
r'a JSON-RPC 2.0 error response dict with id "(?P<resp_id>[^"]+)" and error code "(?P<code>[^"]+)"'
)
def step_jsonrpc_error_response_dict(context: Context, resp_id: str, code: str) -> None:
context.raw_dict = {
"jsonrpc": "2.0",
"id": resp_id,
"error": {"code": code, "message": "Resource not found"},
}
@then("the response error should not be None")
def step_response_error_not_none(context: Context) -> None:
assert context.response.error is not None, "Expected error to be set"
@then("the response result should be None")
def step_response_result_none(context: Context) -> None:
assert context.response.result is None, (
f"Expected result to be None, got: {context.response.result}"
)
# ---------------------------------------------------------------------------
# Facade dispatch — JSON-RPC 2.0 compliant responses
# ---------------------------------------------------------------------------
@given("a wire-format facade with no services")
def step_wire_facade_no_services(context: Context) -> None:
context.wire_facade = A2aLocalFacade()
@when(
r'I dispatch wire-format method "(?P<method>[^"]+)" with params (?P<params_json>.+)'
)
def step_wire_dispatch(context: Context, method: str, params_json: str) -> None:
params = json.loads(params_json)
request = A2aRequest(method=method, params=params)
try:
context.wire_response = context.wire_facade.dispatch(request)
except Exception:
# For unknown methods, facade raises A2aOperationNotFoundError
# which is caught and returned as an error response
from cleveragents.a2a.models import A2aErrorDetail, A2aResponse
context.wire_response = A2aResponse(
id=request.id,
error=A2aErrorDetail(
code="NOT_FOUND",
message=f"Unknown A2A method: {method}",
),
)
@when(
r'I dispatch wire-format method "(?P<method>[^"]+)" with id "(?P<req_id>[^"]+)" and params (?P<params_json>.+)'
)
def step_wire_dispatch_with_id(
context: Context, method: str, req_id: str, params_json: str
) -> None:
params = json.loads(params_json)
request = A2aRequest(method=method, id=req_id, params=params)
context.wire_response = context.wire_facade.dispatch(request)
@then(r'the wire-format response jsonrpc should equal "(?P<value>[^"]+)"')
def step_wire_response_jsonrpc(context: Context, value: str) -> None:
assert context.wire_response.jsonrpc == value, (
f"Expected jsonrpc '{value}', got '{context.wire_response.jsonrpc}'"
)
@then("the wire-format response result should not be None")
def step_wire_response_result_not_none(context: Context) -> None:
assert context.wire_response.result is not None, (
f"Expected result to be set, got None. Error: {context.wire_response.error}"
)
@then("the wire-format response error should be None")
def step_wire_response_error_none(context: Context) -> None:
assert context.wire_response.error is None, (
f"Expected error to be None, got: {context.wire_response.error}"
)
@then("the wire-format response error should not be None")
def step_wire_response_error_not_none(context: Context) -> None:
assert context.wire_response.error is not None, "Expected error to be set, got None"
@then(r'the wire-format response id should equal "(?P<value>[^"]+)"')
def step_wire_response_id(context: Context, value: str) -> None:
assert context.wire_response.id == value, (
f"Expected response id '{value}', got '{context.wire_response.id}'"
)
# Reset step matcher to parse (default) so subsequent step files are not affected
use_step_matcher("parse")