39
TypesafeMissingApiKeyError,
40
TypesafeProvider,
41
TypesafeProviderError,
42
+ companion,
43
+ parse_decision,
44
)
43
-from composio_typesafe.classify import classify_property # noqa: E402
45
+from composio_typesafe.classify import classify_property, parse_property # noqa: E402
46
from composio_typesafe.compile import routing_question # noqa: E402
47
+from composio_typesafe.decide import normalize_state, stable # noqa: E402
48
from composio_typesafe.keys import ( # noqa: E402
49
build_options,
50
index_prefixed_key,
51
is_safe_label,
52
option_values,
53
)
54
+from composio_typesafe.types import Probability # noqa: E402
55
from hypothesis import given # noqa: E402
56
from hypothesis import strategies as st # noqa: E402
57
+from pydantic import TypeAdapter # noqa: E402
58
+from pydantic import ValidationError as PydanticValidationError # noqa: E402
59
60
from composio.client.types import Tool # noqa: E402
61
from composio.core.models.base import allow_tracking # noqa: E402
324
class TestClassifyAndKeys:
325
def test_python_number_traps(self) -> None:
326
# bool is an int subclass, JSON 1.0 is the integer 1, and 1 and "1" are mixed types.
321
- assert classify_property({"enum": [True, False]}) == {"kind": "boolean"}
327
+ assert classify_property({"enum": [True, False]}) == {
328
+ "kind": "boolean",
329
+ "nullable": False,
330
+ }
331
assert classify_property({"enum": [1, True]}) == {"kind": "open"}
332
assert classify_property({"enum": [1, "1"]}) == {"kind": "open"}
333
assert classify_property({"enum": [1.0, 2, 2.0]}) == {
340
assert classify_property({"enum": [2**53 - 1]})["kind"] == "enum"
341
342
@pytest.mark.parametrize(
334
- ("schema", "kind"),
343
+ ("schema", "expected"),
344
[
345
# A yes/no Choice would offer a value the schema forbids.
337
- ({"const": True}, "open"),
338
- ({"enum": [False, None]}, "open"),
339
- ({"oneOf": [{"enum": [False]}, {"type": "null"}]}, "open"),
340
- ({"type": ["boolean", "null"]}, "boolean"),
341
- ({"enum": [False, True, None]}, "boolean"),
342
- ({"anyOf": [{"const": True}, {"const": False}]}, "boolean"),
346
+ ({"const": True}, {"kind": "open"}),
347
+ ({"enum": [False, None]}, {"kind": "open"}),
348
+ ({"oneOf": [{"enum": [False]}, {"type": "null"}]}, {"kind": "open"}),
349
+ ({"type": ["boolean", "null"]}, {"kind": "boolean", "nullable": True}),
350
+ ({"type": "boolean"}, {"kind": "boolean", "nullable": False}),
351
+ ({"enum": [False, True, None]}, {"kind": "boolean", "nullable": True}),
352
+ (
353
+ {"anyOf": [{"const": True}, {"const": False}]},
354
+ {"kind": "boolean", "nullable": False},
355
+ ),
356
],
357
)
345
- def test_a_boolean_needs_both_values(self, schema: t.Any, kind: str) -> None:
346
- assert classify_property(schema) == {"kind": kind}
358
+ def test_a_boolean_needs_both_values(
359
+ self, schema: t.Any, expected: t.Dict[str, t.Any]
360
+ ) -> None:
361
+ assert classify_property(schema) == expected
362
+
363
+ def test_an_enum_array_keeps_integer_max_items_and_min_items(self) -> None:
364
+ # JSON whole numbers arrive as ints; a strict float used to make the property
365
+ # open-ended instead.
366
+ prop = parse_property(
367
+ {
368
+ "type": "array",
369
+ "items": {"type": "string", "enum": ["a", "b"]},
370
+ "maxItems": 2,
371
+ "minItems": 1,
372
+ }
373
+ )
374
+ assert prop is not None
375
+ assert classify_property(prop) == {
376
+ "kind": "enum_array",
377
+ "values": ["a", "b"],
378
+ "maxItems": 2,
379
+ "minItems": 1,
380
+ }
381
+ assert parse_property({"type": "array", "items": {}, "maxItems": "2"}) is None
382
383
def test_option_count_limit(self) -> None:
384
members = [f"m{index}" for index in range(254)]
567
)
568
assert capped["arguments"] == {"pick": ["b"]}
569
570
+ def test_a_selection_below_min_items_is_not_stated(self) -> None:
571
+ tool = make_tool(
572
+ "PICK_SOME",
573
+ {
574
+ "pick": {
575
+ "type": "array",
576
+ "items": {"enum": ["a", "b"]},
577
+ "minItems": 1,
578
+ },
579
+ "pick_many": {
580
+ "type": "array",
581
+ "items": {"enum": ["x", "y"]},
582
+ "minItems": 2,
583
+ },
584
+ },
585
+ ["pick"],
586
+ )
587
+ compiled = json.loads(
588
+ json.dumps(TypesafeProvider().wrap_tool(tool)["arguments"])
589
+ )
590
+ assert [a["name"] for a in compiled] == ["pick", "pick_many"]
591
+ assert compiled[0]["minItems"] == 1
592
+ assert compiled[1]["minItems"] == 2
593
+
594
+ # "mentioned" with zero members selected leaves the required argument missing.
595
+ none = decide_both(
596
+ respond(("PICK_SOME", 0.9), answers={"t0_a0_mentioned": 0.9}),
597
+ [tool],
598
+ "pick some",
599
+ )
600
+ assert matches(
601
+ none, {"kind": "partial", "missing": [["pick"]], "arguments": {}}
602
+ )
603
+
604
+ # Enough members selected binds the argument.
605
+ enough = decide_both(
606
+ respond(
607
+ ("PICK_SOME", 0.9),
608
+ answers={"t0_a0_mentioned": 0.9, "t0_a0_m0": 0.9},
609
+ ),
610
+ [tool],
611
+ "pick a",
612
+ )
613
+ assert matches(enough, {"kind": "call", "arguments": {"pick": ["a"]}})
614
+
615
+ # An optional argument that stays below minItems is not bound and not dropped.
616
+ optional = decide_both(
617
+ respond(
618
+ ("PICK_SOME", 0.9),
619
+ answers={
620
+ "t0_a0_mentioned": 0.9,
621
+ "t0_a0_m0": 0.9,
622
+ "t0_a1_mentioned": 0.9,
623
+ "t0_a1_m0": 0.9,
624
+ },
625
+ ),
626
+ [tool],
627
+ "pick some",
628
+ )
629
+ assert matches(
630
+ optional,
631
+ {"kind": "call", "arguments": {"pick": ["a"]}, "dropped": []},
632
+ )
633
+
634
+ def test_a_nullable_boolean_offers_and_binds_null(self) -> None:
635
+ nullable = make_tool("CLEAR_FLAG", {"flag": {"type": ["boolean", "null"]}})
636
+ compiled = json.loads(
637
+ json.dumps(TypesafeProvider().wrap_tool(nullable)["arguments"][0])
638
+ )
639
+ assert compiled["options"] == [
640
+ {"key": "yes", "value": True},
641
+ {"key": "no", "value": False},
642
+ {"key": "__null__", "value": None},
643
+ ]
644
+ assert (
645
+ compiled["question"]["criteria"]["__null__"]
646
+ == 'The request explicitly asks for no value (null) for "flag".'
647
+ )
648
+ bound = decide_both(
649
+ respond(("CLEAR_FLAG", 0.9), answers={"t0_a0": ("__null__", 0.9)}),
650
+ [nullable],
651
+ "clear the flag",
652
+ )
653
+ assert matches(bound, {"kind": "call", "arguments": {"flag": None}})
654
+ assert bound["arguments"]["flag"] is None
655
+
656
+ plain = make_tool("SET_FLAG", {"flag": {"type": "boolean"}})
657
+ plain_compiled = json.loads(
658
+ json.dumps(TypesafeProvider().wrap_tool(plain)["arguments"][0])
659
+ )
660
+ assert plain_compiled["options"] == [
661
+ {"key": "yes", "value": True},
662
+ {"key": "no", "value": False},
663
+ ]
664
+
665
def test_restores_typed_values(self) -> None:
666
tool = make_tool(
667
"TYPED",
750
{"request": "open a ticket", "context": cyclic},
751
)
752
753
+ def test_stable_keeps_json_null_and_rejects_the_rest(self) -> None:
754
+ # JSON null is a value; nothing else is dropped or rewritten.
755
+ assert stable(None) is None
756
+ assert stable({"b": 1, "a": None}) == {"a": None, "b": 1}
757
+ assert stable([1, None]) == [1, None]
758
+ for not_json in (
759
+ float("nan"),
760
+ float("inf"),
761
+ datetime.date(2026, 1, 2),
762
+ object(),
763
+ ):
764
+ with pytest.raises(TypesafeInvalidOptionsError):
765
+ stable(not_json)
766
+
767
+ contextless = normalize_state({"request": "open a ticket"})
768
+ assert contextless.has_context is False
769
+ assert contextless.full == {"request": "open a ticket"}
770
+ with_context = normalize_state({"request": "open", "context": {"a": None}})
771
+ assert with_context.has_context is True
772
+ assert with_context.full == {"context": {"a": None}, "request": "open"}
773
+
774
775
def many_tools(count: int) -> t.List[Tool]:
776
return [make_tool(f"TOOL_{index}", description="x") for index in range(count)]
974
)
975
976
977
+class TestProbabilityAcceptsJsonNumbers:
978
+ def test_probability_accepts_whole_numbers_and_rejects_the_rest(self) -> None:
979
+ adapter = TypeAdapter(Probability)
980
+ assert adapter.validate_python(0) == 0.0
981
+ assert adapter.validate_python(1) == 1.0
982
+ assert adapter.validate_python(0.5) == 0.5
983
+ assert isinstance(adapter.validate_python(1), float)
984
+ for bad in (True, "0.5", 1.5, float("nan"), float("inf"), -0.1, None):
985
+ with pytest.raises(PydanticValidationError):
986
+ adapter.validate_python(bad)
987
+
988
+ def test_a_stored_decision_with_whole_number_confidence_parses(self) -> None:
989
+ parsed = parse_decision({**CALL, "confidence": 1})
990
+ assert parsed["confidence"] == 1.0
991
+ for bad in (True, "0.9", 1.5, float("nan")):
992
+ with pytest.raises(TypesafeMalformedDecisionError):
993
+ parse_decision({**CALL, "confidence": bad})
994
+
995
+ def test_an_api_answer_with_whole_number_probabilities_parses(self) -> None:
996
+ # JSON 0 and 1 are valid probabilities, not malformed responses.
997
+ response = {
998
+ "model": "jev-1.13",
999
+ "answers": {
1000
+ "route": {
1001
+ "type": "choice",
1002
+ "choice": "TICKETS_CREATE",
1003
+ "confidence": 1,
1004
+ "probabilities": {"TICKETS_CREATE": 1, NONE: 0},
1005
+ },
1006
+ "gate_0": {"type": "noul", "noul": 1},
1007
+ "gate_1": {"type": "noul", "noul": 0},
1008
+ "gate_2": {"type": "noul", "noul": 1},
1009
+ "t0_a0": {
1010
+ "type": "choice",
1011
+ "choice": "high",
1012
+ "confidence": 1,
1013
+ "probabilities": {
1014
+ "low": 0,
1015
+ "medium": 0,
1016
+ "high": 1,
1017
+ NOT_STATED: 0,
1018
+ },
1019
+ },
1020
+ },
1021
+ }
1022
+ client = Mock()
1023
+ client.system_one = MagicMock(return_value=response)
1024
+ provider = TypesafeProvider(client=client)
1025
+ decision = provider.decide(provider.wrap_tools([TICKETS]), "open a ticket")
1026
+ # `title` is required and open-ended, so the decision stays partial.
1027
+ assert matches(decision, {"kind": "partial", "arguments": {"priority": "high"}})
1028
+ assert decision["confidence"] == (1 + 0 + 1) / 3
1029
+
1030
+
1031
VALID_ROUTE = {
1032
"type": "choice",
1033
"choice": "TICKETS_CREATE",
1508
("options", "arguments"),
1509
[
1510
({"redact_arguments": lambda _slug, _arguments: "secret"}, {}),
1511
+ (
1512
+ {
1513
+ "redact_arguments": lambda _slug, arguments: {
1514
+ k: v for k, v in arguments.items() if k != "password"
1515
+ }
1516
+ },
1517
+ {"password": "hunter2"},
1518
+ ),
1519
+ (
1520
+ {
1521
+ "redact_arguments": lambda _slug, arguments: {
1522
+ **arguments,
1523
+ "password": 42,
1524
+ }
1525
+ },
1526
+ {},
1527
+ ),
1528
+ (
1529
+ {
1530
+ "redact_arguments": lambda _slug, arguments: {
1531
+ **arguments,
1532
+ "to": arguments["to"][:-1],
1533
+ }
1534
+ },
1535
+ {"to": ["a@example.com", "b@example.com"]},
1536
+ ),
1537
+ (
1538
+ {
1539
+ "redact_arguments": lambda _slug, arguments: {
1540
+ **arguments,
1541
+ "meta": {"visible": arguments["meta"]["visible"]},
1542
+ }
1543
+ },
1544
+ {"meta": {"visible": "v", "secret": "s"}},
1545
+ ),
1546
({}, {"when": datetime.datetime(2026, 1, 2)}),
1547
({"get_context": lambda _context: {"size": float("inf")}}, {}),
1548
],
1309
- ids=["redactor returns no dict", "arguments not JSON", "context not JSON"],
1549
+ ids=[
1550
+ "redactor returns no dict",
1551
+ "redactor deletes a key",
1552
+ "redactor retypes a value",
1553
+ "redactor shortens an array",
1554
+ "redactor deletes a nested key",
1555
+ "arguments not JSON",
1556
+ "context not JSON",
1557
+ ],
1558
)
1559
def test_blocks_and_sends_nothing(self, options: t.Any, arguments: t.Any) -> None:
1560
on_bypass = Mock()
1564
with pytest.raises(TypesafeGateBlockedError) as caught:
1565
run_gate(gate, {**PARAMS, "arguments": arguments})
1566
assert caught.value.reason == "check_failed"
1567
+ assert caught.value.__cause__ is None and caught.value.__context__ is None
1568
client.system_one.assert_not_called()
1569
on_bypass.assert_not_called()
1570
1571
+ def test_a_masking_redactor_keeps_the_executed_call_original(self) -> None:
1572
+ before = json.loads(json.dumps(PARAMS["arguments"]))
1573
+
1574
+ def redact(_slug: str, arguments: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]:
1575
+ return {
1576
+ **arguments,
1577
+ "password": "•" * len(t.cast(str, arguments["password"])),
1578
+ }
1579
+
1580
+ gate, client = gate_with(0.9, redact_arguments=redact)
1581
+ assert run_gate(gate) is PARAMS
1582
+ assert PARAMS["arguments"] == before
1583
+ sent_state = client.sent()["state"]
1584
+ assert sent_state["proposed_call"]["arguments"] == {
1585
+ "to": "ada@example.com",
1586
+ "password": "•" * len("hunter2-SENTINEL-4"),
1587
+ }
1588
+
1589
+ @pytest.mark.parametrize("value", ["alow", "ALLOW", 1, None])
1590
+ def test_an_invalid_on_unavailable_raises_at_construction(
1591
+ self, value: t.Any
1592
+ ) -> None:
1593
+ # Both entry points validate: the provider method and the module function.
1594
+ with pytest.raises(TypesafeInvalidOptionsError):
1595
+ TypesafeProvider(client=MockClient(respond())).confidence_gate(
1596
+ tools=RAW_TOOLS,
1597
+ get_request=lambda _context: "email ada",
1598
+ on_unavailable=value,
1599
+ )
1600
+ with pytest.raises(TypesafeInvalidOptionsError):
1601
+ companion.confidence_gate(
1602
+ ask=t.cast(t.Any, lambda *_: None),
1603
+ tools=RAW_TOOLS,
1604
+ get_request=lambda _context: "email ada",
1605
+ on_unavailable=value,
1606
+ )
1607
+
1608
def test_blocks_on_unavailability_and_allow_lets_it_through(self) -> None:
1609
with pytest.raises(TypesafeGateUnavailableError) as caught:
1610
run_gate(failing_gate(UNAVAILABLE))