confgate 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- confgate-0.1.0/LICENSE +21 -0
- confgate-0.1.0/PKG-INFO +54 -0
- confgate-0.1.0/README.md +36 -0
- confgate-0.1.0/pyproject.toml +25 -0
- confgate-0.1.0/setup.cfg +4 -0
- confgate-0.1.0/src/confgate/__init__.py +8 -0
- confgate-0.1.0/src/confgate/_core.py +55 -0
- confgate-0.1.0/src/confgate/_exceptions.py +9 -0
- confgate-0.1.0/src/confgate/_gate.py +46 -0
- confgate-0.1.0/src/confgate.egg-info/PKG-INFO +54 -0
- confgate-0.1.0/src/confgate.egg-info/SOURCES.txt +14 -0
- confgate-0.1.0/src/confgate.egg-info/dependency_links.txt +1 -0
- confgate-0.1.0/src/confgate.egg-info/requires.txt +4 -0
- confgate-0.1.0/src/confgate.egg-info/top_level.txt +1 -0
- confgate-0.1.0/tests/test_decision.py +122 -0
- confgate-0.1.0/tests/test_gate.py +160 -0
confgate-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sri Harsha
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
confgate-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: confgate
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Confidence-gated decisions for LLM agent outputs
|
|
5
|
+
Author: Sri Harsha
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: agents,llm,confidence,ai,agentic
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: pytest; extra == "dev"
|
|
16
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# confgate
|
|
20
|
+
|
|
21
|
+
Confidence-gated decisions for LLM agent outputs.
|
|
22
|
+
|
|
23
|
+
> Prevent false-positive fatigue by abstaining from low-confidence findings.
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install confgate
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Quick start
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from confgate import Decision, gate
|
|
35
|
+
|
|
36
|
+
@gate(threshold=0.75)
|
|
37
|
+
def security_agent(diff: str) -> Decision:
|
|
38
|
+
# your LLM call here
|
|
39
|
+
return Decision(
|
|
40
|
+
category="security",
|
|
41
|
+
confidence=0.9,
|
|
42
|
+
reasoning="Potential SQL injection in query builder.",
|
|
43
|
+
severity="high",
|
|
44
|
+
line_ref="src/db.py:42",
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
result = security_agent(diff)
|
|
48
|
+
if not result.abstained:
|
|
49
|
+
print(result)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
MIT
|
confgate-0.1.0/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# confgate
|
|
2
|
+
|
|
3
|
+
Confidence-gated decisions for LLM agent outputs.
|
|
4
|
+
|
|
5
|
+
> Prevent false-positive fatigue by abstaining from low-confidence findings.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install confgate
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from confgate import Decision, gate
|
|
17
|
+
|
|
18
|
+
@gate(threshold=0.75)
|
|
19
|
+
def security_agent(diff: str) -> Decision:
|
|
20
|
+
# your LLM call here
|
|
21
|
+
return Decision(
|
|
22
|
+
category="security",
|
|
23
|
+
confidence=0.9,
|
|
24
|
+
reasoning="Potential SQL injection in query builder.",
|
|
25
|
+
severity="high",
|
|
26
|
+
line_ref="src/db.py:42",
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
result = security_agent(diff)
|
|
30
|
+
if not result.abstained:
|
|
31
|
+
print(result)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## License
|
|
35
|
+
|
|
36
|
+
MIT
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "confgate"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Confidence-gated decisions for LLM agent outputs"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [{ name = " Sri Harsha" }]
|
|
13
|
+
keywords = ["agents", "llm", "confidence", "ai", "agentic"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Topic :: Software Development :: Libraries",
|
|
18
|
+
]
|
|
19
|
+
dependencies = []
|
|
20
|
+
|
|
21
|
+
[project.optional-dependencies]
|
|
22
|
+
dev = ["pytest", "pytest-cov"]
|
|
23
|
+
|
|
24
|
+
[tool.setuptools.packages.find]
|
|
25
|
+
where = ["src"]
|
confgate-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""confgate — confidence-gated decisions for LLM agent outputs."""
|
|
2
|
+
|
|
3
|
+
from confgate._core import Decision
|
|
4
|
+
from confgate._exceptions import GateError, InvalidDecisionError
|
|
5
|
+
from confgate._gate import gate
|
|
6
|
+
|
|
7
|
+
__version__ = "0.1.0"
|
|
8
|
+
__all__ = ["Decision", "gate", "GateError", "InvalidDecisionError"]
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Decision dataclass — the core data structure for confgate."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
|
|
5
|
+
_VALID_SEVERITIES = {"low", "medium", "high", "critical"}
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass
|
|
9
|
+
class Decision:
|
|
10
|
+
"""A structured finding produced by an LLM agent.
|
|
11
|
+
|
|
12
|
+
Attributes:
|
|
13
|
+
category: Type of finding, e.g. 'security' or 'style'.
|
|
14
|
+
confidence: Agent's self-reported confidence, 0.0–1.0.
|
|
15
|
+
reasoning: One-sentence explanation shown to end users.
|
|
16
|
+
severity: Impact level — 'low', 'medium', 'high', or 'critical'.
|
|
17
|
+
line_ref: Optional code location, e.g. 'src/auth.py:42'.
|
|
18
|
+
abstained: Set True by @gate when confidence < threshold.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
category: str
|
|
22
|
+
confidence: float
|
|
23
|
+
reasoning: str
|
|
24
|
+
severity: str = "medium"
|
|
25
|
+
line_ref: str | None = None
|
|
26
|
+
abstained: bool = False
|
|
27
|
+
|
|
28
|
+
def __post_init__(self) -> None:
|
|
29
|
+
if not (0.0 <= self.confidence <= 1.0):
|
|
30
|
+
raise ValueError(
|
|
31
|
+
f"confidence must be between 0.0 and 1.0, got {self.confidence}"
|
|
32
|
+
)
|
|
33
|
+
if self.severity not in _VALID_SEVERITIES:
|
|
34
|
+
raise ValueError(
|
|
35
|
+
f"severity must be one of {sorted(_VALID_SEVERITIES)}, got {self.severity!r}"
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
def to_dict(self) -> dict:
|
|
39
|
+
"""Return a plain dict suitable for JSON serialisation."""
|
|
40
|
+
return {
|
|
41
|
+
"category": self.category,
|
|
42
|
+
"confidence": self.confidence,
|
|
43
|
+
"reasoning": self.reasoning,
|
|
44
|
+
"severity": self.severity,
|
|
45
|
+
"line_ref": self.line_ref,
|
|
46
|
+
"abstained": self.abstained,
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
def __str__(self) -> str:
|
|
50
|
+
abstained_tag = " [ABSTAINED]" if self.abstained else ""
|
|
51
|
+
loc = f" @ {self.line_ref}" if self.line_ref else ""
|
|
52
|
+
return (
|
|
53
|
+
f"[{self.severity.upper()}]{abstained_tag} {self.category}{loc}"
|
|
54
|
+
f" (confidence={self.confidence:.2f}): {self.reasoning}"
|
|
55
|
+
)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""@gate decorator — confidence-gates a function that returns a Decision."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
from functools import wraps
|
|
5
|
+
from typing import TypeVar
|
|
6
|
+
|
|
7
|
+
from confgate._core import Decision
|
|
8
|
+
from confgate._exceptions import InvalidDecisionError
|
|
9
|
+
|
|
10
|
+
F = TypeVar("F", bound=Callable[..., Decision])
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def gate(threshold: float = 0.8) -> Callable[[F], F]:
|
|
14
|
+
"""Decorator factory that abstains low-confidence decisions.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
threshold: Minimum confidence required to pass. Decisions with
|
|
18
|
+
confidence strictly below this value have abstained=True.
|
|
19
|
+
Must be between 0.0 and 1.0 inclusive.
|
|
20
|
+
|
|
21
|
+
Raises:
|
|
22
|
+
ValueError: At decoration time if threshold is outside [0.0, 1.0].
|
|
23
|
+
InvalidDecisionError: At call time if the wrapped function does not
|
|
24
|
+
return a Decision instance.
|
|
25
|
+
"""
|
|
26
|
+
if not (0.0 <= threshold <= 1.0):
|
|
27
|
+
raise ValueError(
|
|
28
|
+
f"gate threshold must be between 0.0 and 1.0, got {threshold}"
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
def decorator(fn: F) -> F:
|
|
32
|
+
@wraps(fn)
|
|
33
|
+
def wrapper(*args, **kwargs):
|
|
34
|
+
result = fn(*args, **kwargs)
|
|
35
|
+
if not isinstance(result, Decision):
|
|
36
|
+
raise InvalidDecisionError(
|
|
37
|
+
f"{fn.__name__} must return a Decision instance, "
|
|
38
|
+
f"got {type(result).__name__}"
|
|
39
|
+
)
|
|
40
|
+
if result.confidence < threshold:
|
|
41
|
+
result.abstained = True
|
|
42
|
+
return result
|
|
43
|
+
|
|
44
|
+
return wrapper # type: ignore[return-value]
|
|
45
|
+
|
|
46
|
+
return decorator
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: confgate
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Confidence-gated decisions for LLM agent outputs
|
|
5
|
+
Author: Sri Harsha
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: agents,llm,confidence,ai,agentic
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: pytest; extra == "dev"
|
|
16
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# confgate
|
|
20
|
+
|
|
21
|
+
Confidence-gated decisions for LLM agent outputs.
|
|
22
|
+
|
|
23
|
+
> Prevent false-positive fatigue by abstaining from low-confidence findings.
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install confgate
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Quick start
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from confgate import Decision, gate
|
|
35
|
+
|
|
36
|
+
@gate(threshold=0.75)
|
|
37
|
+
def security_agent(diff: str) -> Decision:
|
|
38
|
+
# your LLM call here
|
|
39
|
+
return Decision(
|
|
40
|
+
category="security",
|
|
41
|
+
confidence=0.9,
|
|
42
|
+
reasoning="Potential SQL injection in query builder.",
|
|
43
|
+
severity="high",
|
|
44
|
+
line_ref="src/db.py:42",
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
result = security_agent(diff)
|
|
48
|
+
if not result.abstained:
|
|
49
|
+
print(result)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
MIT
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/confgate/__init__.py
|
|
5
|
+
src/confgate/_core.py
|
|
6
|
+
src/confgate/_exceptions.py
|
|
7
|
+
src/confgate/_gate.py
|
|
8
|
+
src/confgate.egg-info/PKG-INFO
|
|
9
|
+
src/confgate.egg-info/SOURCES.txt
|
|
10
|
+
src/confgate.egg-info/dependency_links.txt
|
|
11
|
+
src/confgate.egg-info/requires.txt
|
|
12
|
+
src/confgate.egg-info/top_level.txt
|
|
13
|
+
tests/test_decision.py
|
|
14
|
+
tests/test_gate.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
confgate
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""Tests for the Decision dataclass."""
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from confgate import Decision
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def make_decision(**overrides) -> Decision:
|
|
9
|
+
defaults = dict(
|
|
10
|
+
category="security",
|
|
11
|
+
confidence=0.9,
|
|
12
|
+
reasoning="SQL injection risk in query builder.",
|
|
13
|
+
)
|
|
14
|
+
return Decision(**{**defaults, **overrides})
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class TestDecisionDefaults:
|
|
18
|
+
def test_valid_creation_with_defaults(self):
|
|
19
|
+
d = make_decision()
|
|
20
|
+
assert d.category == "security"
|
|
21
|
+
assert d.confidence == 0.9
|
|
22
|
+
assert d.reasoning == "SQL injection risk in query builder."
|
|
23
|
+
assert d.severity == "medium"
|
|
24
|
+
assert d.line_ref is None
|
|
25
|
+
assert d.abstained is False
|
|
26
|
+
|
|
27
|
+
def test_all_fields_set_explicitly(self):
|
|
28
|
+
d = Decision(
|
|
29
|
+
category="style",
|
|
30
|
+
confidence=0.6,
|
|
31
|
+
reasoning="Missing type hint.",
|
|
32
|
+
severity="low",
|
|
33
|
+
line_ref="src/auth.py:42",
|
|
34
|
+
abstained=False,
|
|
35
|
+
)
|
|
36
|
+
assert d.line_ref == "src/auth.py:42"
|
|
37
|
+
assert d.severity == "low"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class TestDecisionValidation:
|
|
41
|
+
def test_confidence_above_one_raises(self):
|
|
42
|
+
with pytest.raises(ValueError, match="confidence"):
|
|
43
|
+
make_decision(confidence=1.1)
|
|
44
|
+
|
|
45
|
+
def test_confidence_below_zero_raises(self):
|
|
46
|
+
with pytest.raises(ValueError, match="confidence"):
|
|
47
|
+
make_decision(confidence=-0.01)
|
|
48
|
+
|
|
49
|
+
def test_confidence_exactly_zero_is_valid(self):
|
|
50
|
+
d = make_decision(confidence=0.0)
|
|
51
|
+
assert d.confidence == 0.0
|
|
52
|
+
|
|
53
|
+
def test_confidence_exactly_one_is_valid(self):
|
|
54
|
+
d = make_decision(confidence=1.0)
|
|
55
|
+
assert d.confidence == 1.0
|
|
56
|
+
|
|
57
|
+
def test_invalid_severity_raises(self):
|
|
58
|
+
with pytest.raises(ValueError, match="severity"):
|
|
59
|
+
make_decision(severity="fatal")
|
|
60
|
+
|
|
61
|
+
@pytest.mark.parametrize("sev", ["low", "medium", "high", "critical"])
|
|
62
|
+
def test_all_valid_severities(self, sev):
|
|
63
|
+
d = make_decision(severity=sev)
|
|
64
|
+
assert d.severity == sev
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class TestDecisionToDict:
|
|
68
|
+
def test_to_dict_keys(self):
|
|
69
|
+
d = make_decision()
|
|
70
|
+
result = d.to_dict()
|
|
71
|
+
assert set(result.keys()) == {
|
|
72
|
+
"category", "confidence", "reasoning", "severity", "line_ref", "abstained"
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
def test_to_dict_values(self):
|
|
76
|
+
d = Decision(
|
|
77
|
+
category="security",
|
|
78
|
+
confidence=0.85,
|
|
79
|
+
reasoning="Hardcoded secret.",
|
|
80
|
+
severity="high",
|
|
81
|
+
line_ref="config.py:10",
|
|
82
|
+
abstained=False,
|
|
83
|
+
)
|
|
84
|
+
assert d.to_dict() == {
|
|
85
|
+
"category": "security",
|
|
86
|
+
"confidence": 0.85,
|
|
87
|
+
"reasoning": "Hardcoded secret.",
|
|
88
|
+
"severity": "high",
|
|
89
|
+
"line_ref": "config.py:10",
|
|
90
|
+
"abstained": False,
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
def test_to_dict_line_ref_none(self):
|
|
94
|
+
d = make_decision()
|
|
95
|
+
assert d.to_dict()["line_ref"] is None
|
|
96
|
+
|
|
97
|
+
def test_to_dict_reflects_abstained_state(self):
|
|
98
|
+
d = make_decision()
|
|
99
|
+
d.abstained = True
|
|
100
|
+
assert d.to_dict()["abstained"] is True
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class TestDecisionStr:
|
|
104
|
+
def test_str_not_abstained(self):
|
|
105
|
+
d = make_decision(severity="high", confidence=0.9)
|
|
106
|
+
s = str(d)
|
|
107
|
+
assert "[HIGH]" in s
|
|
108
|
+
assert "0.90" in s
|
|
109
|
+
assert "ABSTAINED" not in s
|
|
110
|
+
|
|
111
|
+
def test_str_abstained(self):
|
|
112
|
+
d = make_decision()
|
|
113
|
+
d.abstained = True
|
|
114
|
+
assert "ABSTAINED" in str(d)
|
|
115
|
+
|
|
116
|
+
def test_str_includes_line_ref(self):
|
|
117
|
+
d = make_decision(line_ref="src/auth.py:42")
|
|
118
|
+
assert "src/auth.py:42" in str(d)
|
|
119
|
+
|
|
120
|
+
def test_str_no_line_ref_omits_at(self):
|
|
121
|
+
d = make_decision(line_ref=None)
|
|
122
|
+
assert " @ " not in str(d)
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"""Tests for the @gate decorator."""
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from confgate import Decision, InvalidDecisionError, gate
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _decision(confidence: float, **kwargs) -> Decision:
|
|
9
|
+
return Decision(
|
|
10
|
+
category="security",
|
|
11
|
+
confidence=confidence,
|
|
12
|
+
reasoning="Test finding.",
|
|
13
|
+
**kwargs,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class TestGateThreshold:
|
|
18
|
+
def test_above_threshold_not_abstained(self):
|
|
19
|
+
@gate(threshold=0.75)
|
|
20
|
+
def agent() -> Decision:
|
|
21
|
+
return _decision(0.9)
|
|
22
|
+
|
|
23
|
+
result = agent()
|
|
24
|
+
assert result.abstained is False
|
|
25
|
+
|
|
26
|
+
def test_below_threshold_abstained(self):
|
|
27
|
+
@gate(threshold=0.75)
|
|
28
|
+
def agent() -> Decision:
|
|
29
|
+
return _decision(0.5)
|
|
30
|
+
|
|
31
|
+
result = agent()
|
|
32
|
+
assert result.abstained is True
|
|
33
|
+
|
|
34
|
+
def test_exactly_at_threshold_not_abstained(self):
|
|
35
|
+
"""Equal to threshold must pass — strictly less-than triggers abstain."""
|
|
36
|
+
@gate(threshold=0.75)
|
|
37
|
+
def agent() -> Decision:
|
|
38
|
+
return _decision(0.75)
|
|
39
|
+
|
|
40
|
+
result = agent()
|
|
41
|
+
assert result.abstained is False
|
|
42
|
+
|
|
43
|
+
def test_just_below_threshold_abstained(self):
|
|
44
|
+
@gate(threshold=0.75)
|
|
45
|
+
def agent() -> Decision:
|
|
46
|
+
return _decision(0.7499)
|
|
47
|
+
|
|
48
|
+
result = agent()
|
|
49
|
+
assert result.abstained is True
|
|
50
|
+
|
|
51
|
+
def test_default_threshold_is_0_8(self):
|
|
52
|
+
@gate()
|
|
53
|
+
def agent() -> Decision:
|
|
54
|
+
return _decision(0.79)
|
|
55
|
+
|
|
56
|
+
result = agent()
|
|
57
|
+
assert result.abstained is True
|
|
58
|
+
|
|
59
|
+
def test_default_threshold_passes_0_8(self):
|
|
60
|
+
@gate()
|
|
61
|
+
def agent() -> Decision:
|
|
62
|
+
return _decision(0.8)
|
|
63
|
+
|
|
64
|
+
result = agent()
|
|
65
|
+
assert result.abstained is False
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class TestGateMetadataPreservation:
|
|
69
|
+
def test_preserves_name(self):
|
|
70
|
+
@gate(threshold=0.5)
|
|
71
|
+
def my_agent() -> Decision:
|
|
72
|
+
"""Agent docstring."""
|
|
73
|
+
return _decision(0.9)
|
|
74
|
+
|
|
75
|
+
assert my_agent.__name__ == "my_agent"
|
|
76
|
+
|
|
77
|
+
def test_preserves_doc(self):
|
|
78
|
+
@gate(threshold=0.5)
|
|
79
|
+
def my_agent() -> Decision:
|
|
80
|
+
"""Agent docstring."""
|
|
81
|
+
return _decision(0.9)
|
|
82
|
+
|
|
83
|
+
assert my_agent.__doc__ == "Agent docstring."
|
|
84
|
+
|
|
85
|
+
def test_preserves_annotations(self):
|
|
86
|
+
@gate(threshold=0.5)
|
|
87
|
+
def my_agent(diff: str) -> Decision:
|
|
88
|
+
return _decision(0.9)
|
|
89
|
+
|
|
90
|
+
assert my_agent.__annotations__["diff"] is str
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class TestGateReturnTypeEnforcement:
|
|
94
|
+
def test_non_decision_return_raises(self):
|
|
95
|
+
@gate(threshold=0.5)
|
|
96
|
+
def bad_agent():
|
|
97
|
+
return {"confidence": 0.9}
|
|
98
|
+
|
|
99
|
+
with pytest.raises(InvalidDecisionError):
|
|
100
|
+
bad_agent()
|
|
101
|
+
|
|
102
|
+
def test_none_return_raises(self):
|
|
103
|
+
@gate(threshold=0.5)
|
|
104
|
+
def bad_agent():
|
|
105
|
+
return None
|
|
106
|
+
|
|
107
|
+
with pytest.raises(InvalidDecisionError):
|
|
108
|
+
bad_agent()
|
|
109
|
+
|
|
110
|
+
def test_string_return_raises(self):
|
|
111
|
+
@gate(threshold=0.5)
|
|
112
|
+
def bad_agent():
|
|
113
|
+
return "finding"
|
|
114
|
+
|
|
115
|
+
with pytest.raises(InvalidDecisionError):
|
|
116
|
+
bad_agent()
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class TestGateThresholdValidation:
|
|
120
|
+
def test_threshold_above_one_raises_at_decoration(self):
|
|
121
|
+
with pytest.raises(ValueError, match="threshold"):
|
|
122
|
+
@gate(threshold=1.1)
|
|
123
|
+
def agent():
|
|
124
|
+
pass
|
|
125
|
+
|
|
126
|
+
def test_threshold_below_zero_raises_at_decoration(self):
|
|
127
|
+
with pytest.raises(ValueError, match="threshold"):
|
|
128
|
+
@gate(threshold=-0.1)
|
|
129
|
+
def agent():
|
|
130
|
+
pass
|
|
131
|
+
|
|
132
|
+
def test_threshold_exactly_zero_is_valid(self):
|
|
133
|
+
@gate(threshold=0.0)
|
|
134
|
+
def agent() -> Decision:
|
|
135
|
+
return _decision(0.0)
|
|
136
|
+
|
|
137
|
+
result = agent()
|
|
138
|
+
assert result.abstained is False
|
|
139
|
+
|
|
140
|
+
def test_threshold_exactly_one_is_valid(self):
|
|
141
|
+
@gate(threshold=1.0)
|
|
142
|
+
def agent() -> Decision:
|
|
143
|
+
return _decision(0.99)
|
|
144
|
+
|
|
145
|
+
result = agent()
|
|
146
|
+
assert result.abstained is True
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
class TestGatePassesArguments:
|
|
150
|
+
def test_wrapped_function_receives_args(self):
|
|
151
|
+
received = {}
|
|
152
|
+
|
|
153
|
+
@gate(threshold=0.5)
|
|
154
|
+
def agent(diff: str, extra: int = 0) -> Decision:
|
|
155
|
+
received["diff"] = diff
|
|
156
|
+
received["extra"] = extra
|
|
157
|
+
return _decision(0.9)
|
|
158
|
+
|
|
159
|
+
agent("some diff", extra=7)
|
|
160
|
+
assert received == {"diff": "some diff", "extra": 7}
|