agentscore-gate 1.0.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.
Files changed (34) hide show
  1. agentscore_gate-1.0.0/.claude/CLAUDE.md +46 -0
  2. agentscore_gate-1.0.0/.github/ISSUE_TEMPLATE/bug_report.md +21 -0
  3. agentscore_gate-1.0.0/.github/ISSUE_TEMPLATE/feature_request.md +13 -0
  4. agentscore_gate-1.0.0/.github/PULL_REQUEST_TEMPLATE.md +7 -0
  5. agentscore_gate-1.0.0/.github/dependabot.yml +20 -0
  6. agentscore_gate-1.0.0/.github/workflows/ci.yml +36 -0
  7. agentscore_gate-1.0.0/.github/workflows/publish.yml +37 -0
  8. agentscore_gate-1.0.0/.github/workflows/security.yml +50 -0
  9. agentscore_gate-1.0.0/.gitignore +18 -0
  10. agentscore_gate-1.0.0/CODE_OF_CONDUCT.md +31 -0
  11. agentscore_gate-1.0.0/CONTRIBUTING.md +37 -0
  12. agentscore_gate-1.0.0/LICENSE +21 -0
  13. agentscore_gate-1.0.0/PKG-INFO +107 -0
  14. agentscore_gate-1.0.0/README.md +76 -0
  15. agentscore_gate-1.0.0/SECURITY.md +23 -0
  16. agentscore_gate-1.0.0/agentscore_gate/__init__.py +22 -0
  17. agentscore_gate-1.0.0/agentscore_gate/cache.py +51 -0
  18. agentscore_gate-1.0.0/agentscore_gate/client.py +122 -0
  19. agentscore_gate-1.0.0/agentscore_gate/django.py +98 -0
  20. agentscore_gate-1.0.0/agentscore_gate/flask.py +126 -0
  21. agentscore_gate-1.0.0/agentscore_gate/middleware.py +126 -0
  22. agentscore_gate-1.0.0/agentscore_gate/py.typed +0 -0
  23. agentscore_gate-1.0.0/agentscore_gate/types.py +27 -0
  24. agentscore_gate-1.0.0/lefthook.yml +14 -0
  25. agentscore_gate-1.0.0/pyproject.toml +53 -0
  26. agentscore_gate-1.0.0/ruff.toml +45 -0
  27. agentscore_gate-1.0.0/tests/__init__.py +0 -0
  28. agentscore_gate-1.0.0/tests/test_cache.py +135 -0
  29. agentscore_gate-1.0.0/tests/test_client.py +85 -0
  30. agentscore_gate-1.0.0/tests/test_django.py +134 -0
  31. agentscore_gate-1.0.0/tests/test_flask.py +121 -0
  32. agentscore_gate-1.0.0/tests/test_middleware.py +235 -0
  33. agentscore_gate-1.0.0/uv.lock +678 -0
  34. agentscore_gate-1.0.0/vulture_whitelist.py +20 -0
@@ -0,0 +1,46 @@
1
+ # agentscore-gate
2
+
3
+ Trust-gating middleware for Python web frameworks using AgentScore. Includes adapters for Flask, Django, and Starlette/FastAPI.
4
+
5
+ ## Architecture
6
+
7
+ Single-package Python library published to PyPI.
8
+
9
+ | File | Purpose |
10
+ |------|---------|
11
+ | `agentscore_gate/` | Source code |
12
+ | `agentscore_gate/flask.py` | Flask adapter |
13
+ | `agentscore_gate/django.py` | Django adapter |
14
+ | `agentscore_gate/middleware.py` | ASGI/Starlette middleware |
15
+ | `tests/` | pytest tests |
16
+
17
+ ## Tooling
18
+
19
+ - **uv** — package manager. Use `uv sync`, `uv run`.
20
+ - **ruff** — linting + formatting. `uv run ruff check .` and `uv run ruff format --check .`.
21
+ - **vulture** — dead code detection.
22
+ - **pytest** — tests. `uv run pytest tests/`.
23
+ - **Lefthook** — git hooks. Pre-commit: ruff. Pre-push: vulture.
24
+
25
+ ## Key Commands
26
+
27
+ ```bash
28
+ uv sync --all-extras
29
+ uv run ruff check .
30
+ uv run ruff format .
31
+ uv run pytest tests/
32
+ ```
33
+
34
+ ## Workflow
35
+
36
+ 1. Create a branch
37
+ 2. Make changes
38
+ 3. Lefthook runs ruff on commit, vulture on push
39
+ 4. Open a PR — CI runs automatically
40
+ 5. Merge (squash)
41
+
42
+ ## Rules
43
+
44
+ - **No silent refactors**
45
+ - **Never commit .env files or secrets**
46
+ - **Use PRs** — never push directly to main
@@ -0,0 +1,21 @@
1
+ ---
2
+ name: Bug Report
3
+ about: Report a bug
4
+ title: ''
5
+ labels: bug
6
+ assignees: ''
7
+ ---
8
+
9
+ **Describe the bug**
10
+ A clear description of what the bug is.
11
+
12
+ **To reproduce**
13
+ Steps to reproduce the behavior.
14
+
15
+ **Expected behavior**
16
+ What you expected to happen.
17
+
18
+ **Environment**
19
+ - Package version:
20
+ - Runtime (Node.js/Python version):
21
+ - OS:
@@ -0,0 +1,13 @@
1
+ ---
2
+ name: Feature Request
3
+ about: Suggest an improvement
4
+ title: ''
5
+ labels: enhancement
6
+ assignees: ''
7
+ ---
8
+
9
+ **Describe the feature**
10
+ A clear description of what you'd like.
11
+
12
+ **Use case**
13
+ Why is this needed? What problem does it solve?
@@ -0,0 +1,7 @@
1
+ ## Summary
2
+
3
+ <!-- What does this PR do? -->
4
+
5
+ ## Test plan
6
+
7
+ <!-- How was this tested? -->
@@ -0,0 +1,20 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "uv"
4
+ directory: "/"
5
+ labels: ["dependencies", "python"]
6
+ schedule:
7
+ interval: "weekly"
8
+ day: "monday"
9
+ time: "06:00"
10
+ timezone: "America/New_York"
11
+
12
+ - package-ecosystem: "github-actions"
13
+ directory: "/"
14
+ labels: ["dependencies", "ci"]
15
+ schedule:
16
+ interval: "weekly"
17
+ day: "monday"
18
+ time: "06:00"
19
+ timezone: "America/New_York"
20
+
@@ -0,0 +1,36 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ permissions:
9
+ contents: read
10
+
11
+ jobs:
12
+ lint-and-test:
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - uses: actions/checkout@v6
16
+
17
+ - name: Install uv
18
+ uses: astral-sh/setup-uv@v7
19
+
20
+ - name: Set up Python
21
+ run: uv python install 3.12
22
+
23
+ - name: Install dependencies
24
+ run: uv sync --all-extras
25
+
26
+ - name: Ruff check
27
+ run: uv run ruff check .
28
+
29
+ - name: Ruff format check
30
+ run: uv run ruff format --check .
31
+
32
+ - name: Vulture
33
+ run: uv run vulture agentscore_gate/ vulture_whitelist.py
34
+
35
+ - name: Tests
36
+ run: uv run pytest tests/ -v
@@ -0,0 +1,37 @@
1
+ name: Publish
2
+
3
+ on:
4
+ push:
5
+ tags: ["v*"]
6
+
7
+ permissions:
8
+ contents: write
9
+ id-token: write
10
+
11
+ jobs:
12
+ publish:
13
+ runs-on: blacksmith-2vcpu-ubuntu-2404
14
+ steps:
15
+ - uses: actions/checkout@v6
16
+
17
+ - uses: astral-sh/setup-uv@v7
18
+
19
+ - name: Set version from tag
20
+ run: |
21
+ VERSION="${GITHUB_REF_NAME#v}"
22
+ sed -i "s/^version = .*/version = \"$VERSION\"/" pyproject.toml
23
+ echo "VERSION=$VERSION" >> "$GITHUB_ENV"
24
+
25
+ - run: uv build
26
+
27
+ - name: Publish to PyPI
28
+ uses: pypa/gh-action-pypi-publish@release/v1
29
+
30
+ - name: Commit version bump
31
+ run: |
32
+ git config user.name "github-actions[bot]"
33
+ git config user.email "github-actions[bot]@users.noreply.github.com"
34
+ git add pyproject.toml
35
+ git commit -m "chore: bump version to $VERSION" || true
36
+ git push origin HEAD:main
37
+
@@ -0,0 +1,50 @@
1
+ name: Security
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+
9
+ concurrency:
10
+ group: ${{ github.workflow }}-${{ github.ref }}
11
+ cancel-in-progress: true
12
+
13
+ permissions:
14
+ contents: read
15
+
16
+ jobs:
17
+ osv-scan:
18
+ name: Dependency Scan
19
+ runs-on: blacksmith-2vcpu-ubuntu-2404
20
+ timeout-minutes: 5
21
+ steps:
22
+ - uses: actions/checkout@v6
23
+
24
+ - name: Install osv-scanner
25
+ run: |
26
+ curl -fsSL https://github.com/google/osv-scanner/releases/download/v2.3.2/osv-scanner_linux_amd64 -o osv-scanner
27
+ chmod +x osv-scanner
28
+
29
+ - name: Scan dependencies
30
+ run: ./osv-scanner --lockfile=uv.lock --format=table || true
31
+
32
+ pip-audit:
33
+ name: Python Audit
34
+ runs-on: blacksmith-2vcpu-ubuntu-2404
35
+ timeout-minutes: 5
36
+ steps:
37
+ - uses: actions/checkout@v6
38
+ - uses: astral-sh/setup-uv@v7
39
+ - uses: actions/setup-python@v6
40
+ with:
41
+ python-version: "3.13"
42
+
43
+ - name: Install pip-audit
44
+ run: pip install pip-audit
45
+
46
+ - name: Audit dependencies
47
+ run: |
48
+ uv export --format requirements-txt --no-hashes > requirements.txt
49
+ pip-audit -r requirements.txt --disable-pip --no-deps || true
50
+
@@ -0,0 +1,18 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *$py.class
4
+ *.egg-info/
5
+ *.egg
6
+ dist/
7
+ build/
8
+ .eggs/
9
+ *.so
10
+ .venv/
11
+ venv/
12
+ .env
13
+ .pytest_cache/
14
+ .ruff_cache/
15
+ .mypy_cache/
16
+ *.swp
17
+ *.swo
18
+ .DS_Store
@@ -0,0 +1,31 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, caste, color, religion, or sexual identity and orientation.
6
+
7
+ ## Our Standards
8
+
9
+ Examples of behavior that contributes to a positive environment:
10
+
11
+ - Using welcoming and inclusive language
12
+ - Being respectful of differing viewpoints and experiences
13
+ - Gracefully accepting constructive criticism
14
+ - Focusing on what is best for the community
15
+ - Showing empathy towards other community members
16
+
17
+ Examples of unacceptable behavior:
18
+
19
+ - The use of sexualized language or imagery, and sexual attention or advances of any kind
20
+ - Trolling, insulting or derogatory comments, and personal or political attacks
21
+ - Public or private harassment
22
+ - Publishing others' private information without explicit permission
23
+ - Other conduct which could reasonably be considered inappropriate in a professional setting
24
+
25
+ ## Enforcement
26
+
27
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the project team at engineering@agentscore.sh. All complaints will be reviewed and investigated promptly and fairly.
28
+
29
+ ## Attribution
30
+
31
+ This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org), version 2.1.
@@ -0,0 +1,37 @@
1
+ # Contributing to AgentScore
2
+
3
+ Thanks for your interest in contributing! Here's how to get started.
4
+
5
+ ## Development Setup
6
+
7
+ 1. Fork the repo and clone it locally
8
+ 2. Install dependencies (see README for tooling)
9
+ 3. Create a branch from `main`
10
+ 4. Make your changes
11
+ 5. Run linting and tests
12
+ 6. Open a pull request
13
+
14
+ ## Pull Requests
15
+
16
+ - All PRs require 1 approval before merging
17
+ - Squash merge to `main` is the standard
18
+ - Keep PRs focused — one feature or fix per PR
19
+ - Include tests for new functionality
20
+ - Make sure CI passes before requesting review
21
+
22
+ ## Reporting Issues
23
+
24
+ Open an issue on GitHub with:
25
+ - A clear description of the problem
26
+ - Steps to reproduce
27
+ - Expected vs actual behavior
28
+ - Environment details (OS, runtime version)
29
+
30
+ ## Code Style
31
+
32
+ - **TypeScript repos**: ESLint handles formatting and style. Run `bun run lint` before committing.
33
+ - **Python repos**: Ruff handles linting and formatting. Run `uv run ruff check .` and `uv run ruff format .` before committing.
34
+
35
+ ## License
36
+
37
+ By contributing, you agree that your contributions will be licensed under the MIT License.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AgentScore
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.
@@ -0,0 +1,107 @@
1
+ Metadata-Version: 2.4
2
+ Name: agentscore-gate
3
+ Version: 1.0.0
4
+ Summary: Trust-gating middleware for Python web frameworks using AgentScore
5
+ Project-URL: Homepage, https://agentscore.sh
6
+ Project-URL: Repository, https://github.com/agentscore/python-gate
7
+ Project-URL: Issues, https://github.com/agentscore/python-gate/issues
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Keywords: agent,agentic-payments,agentscore,ai-agent,blockchain,django,erc-8004,fastapi,flask,gate,middleware,reputation,starlette,trust,wallet,x402
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Framework :: AsyncIO
13
+ Classifier: Framework :: Django
14
+ Classifier: Framework :: FastAPI
15
+ Classifier: Framework :: Flask
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: License :: OSI Approved :: MIT License
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Topic :: Software Development :: Libraries
20
+ Requires-Python: >=3.10
21
+ Requires-Dist: httpx<1.0.0,>=0.25.0
22
+ Provides-Extra: django
23
+ Requires-Dist: django>=4.0; extra == 'django'
24
+ Provides-Extra: fastapi
25
+ Requires-Dist: starlette>=0.27.0; extra == 'fastapi'
26
+ Provides-Extra: flask
27
+ Requires-Dist: flask>=2.0.0; extra == 'flask'
28
+ Provides-Extra: starlette
29
+ Requires-Dist: starlette>=0.27.0; extra == 'starlette'
30
+ Description-Content-Type: text/markdown
31
+
32
+ # agentscore-gate
33
+
34
+ [![PyPI version](https://img.shields.io/pypi/v/agentscore-gate.svg)](https://pypi.org/project/agentscore-gate/)
35
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
36
+
37
+ ASGI middleware for trust-gating requests using [AgentScore](https://agentscore.sh). Verify AI agent wallet reputation before allowing requests through, built for the [x402](https://github.com/coinbase/x402) payment ecosystem and [ERC-8004](https://eips.ethereum.org/EIPS/eip-8004) agent registry. Works with FastAPI, Starlette, and any ASGI framework.
38
+
39
+ ## Install
40
+
41
+ ```bash
42
+ pip install agentscore-gate
43
+ ```
44
+
45
+ ## Quick Start
46
+
47
+ ### FastAPI
48
+
49
+ ```python
50
+ from fastapi import FastAPI
51
+ from agentscore_gate import AgentScoreGate
52
+
53
+ app = FastAPI()
54
+ app.add_middleware(AgentScoreGate, api_key="ask_...", min_score=50)
55
+
56
+ @app.get("/")
57
+ async def root():
58
+ return {"message": "Hello, trusted agent!"}
59
+ ```
60
+
61
+ ### Starlette
62
+
63
+ ```python
64
+ from starlette.applications import Starlette
65
+ from starlette.responses import PlainTextResponse
66
+ from starlette.routing import Route
67
+ from agentscore_gate import AgentScoreGate
68
+
69
+ async def homepage(request):
70
+ agentscore_data = request.state.agentscore
71
+ return PlainTextResponse("Hello, trusted agent!")
72
+
73
+ app = Starlette(routes=[Route("/", homepage)])
74
+ app.add_middleware(AgentScoreGate, api_key="ask_...", min_score=50)
75
+ ```
76
+
77
+ ## Options
78
+
79
+ | Parameter | Type | Default | Description |
80
+ |-----------|------|---------|-------------|
81
+ | `api_key` | `str` | *required* | API key from [agentscore.sh](https://agentscore.sh) |
82
+ | `min_score` | `int \| None` | `None` | Minimum score (0–100) |
83
+ | `min_grade` | `str \| None` | `None` | Minimum grade (A–F) |
84
+ | `require_verified_activity` | `bool \| None` | `None` | Require verified payment activity |
85
+ | `fail_open` | `bool` | `False` | Allow requests when API is unreachable |
86
+ | `cache_seconds` | `int` | `300` | Cache TTL for results |
87
+ | `base_url` | `str` | `https://api.agentscore.sh` | API base URL |
88
+ | `extract_address` | `callable` | Reads `x-wallet-address` header | Custom address extractor |
89
+ | `on_denied` | `async callable` | Returns 403 JSON | Custom denial handler |
90
+
91
+ ## How It Works
92
+
93
+ 1. Extracts wallet address from request header (`x-wallet-address`)
94
+ 2. Checks in-memory cache for a previous result
95
+ 3. Calls AgentScore `/v1/assess` with your policy
96
+ 4. Allows or blocks based on the decision
97
+ 5. Attaches data to `request.state.agentscore` on allowed requests
98
+
99
+ ## Documentation
100
+
101
+ - [API Reference](https://docs.agentscore.sh)
102
+ - [ERC-8004 Standard](https://eips.ethereum.org/EIPS/eip-8004)
103
+ - [x402 Protocol](https://github.com/coinbase/x402)
104
+
105
+ ## License
106
+
107
+ [MIT](LICENSE)
@@ -0,0 +1,76 @@
1
+ # agentscore-gate
2
+
3
+ [![PyPI version](https://img.shields.io/pypi/v/agentscore-gate.svg)](https://pypi.org/project/agentscore-gate/)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
5
+
6
+ ASGI middleware for trust-gating requests using [AgentScore](https://agentscore.sh). Verify AI agent wallet reputation before allowing requests through, built for the [x402](https://github.com/coinbase/x402) payment ecosystem and [ERC-8004](https://eips.ethereum.org/EIPS/eip-8004) agent registry. Works with FastAPI, Starlette, and any ASGI framework.
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+ pip install agentscore-gate
12
+ ```
13
+
14
+ ## Quick Start
15
+
16
+ ### FastAPI
17
+
18
+ ```python
19
+ from fastapi import FastAPI
20
+ from agentscore_gate import AgentScoreGate
21
+
22
+ app = FastAPI()
23
+ app.add_middleware(AgentScoreGate, api_key="ask_...", min_score=50)
24
+
25
+ @app.get("/")
26
+ async def root():
27
+ return {"message": "Hello, trusted agent!"}
28
+ ```
29
+
30
+ ### Starlette
31
+
32
+ ```python
33
+ from starlette.applications import Starlette
34
+ from starlette.responses import PlainTextResponse
35
+ from starlette.routing import Route
36
+ from agentscore_gate import AgentScoreGate
37
+
38
+ async def homepage(request):
39
+ agentscore_data = request.state.agentscore
40
+ return PlainTextResponse("Hello, trusted agent!")
41
+
42
+ app = Starlette(routes=[Route("/", homepage)])
43
+ app.add_middleware(AgentScoreGate, api_key="ask_...", min_score=50)
44
+ ```
45
+
46
+ ## Options
47
+
48
+ | Parameter | Type | Default | Description |
49
+ |-----------|------|---------|-------------|
50
+ | `api_key` | `str` | *required* | API key from [agentscore.sh](https://agentscore.sh) |
51
+ | `min_score` | `int \| None` | `None` | Minimum score (0–100) |
52
+ | `min_grade` | `str \| None` | `None` | Minimum grade (A–F) |
53
+ | `require_verified_activity` | `bool \| None` | `None` | Require verified payment activity |
54
+ | `fail_open` | `bool` | `False` | Allow requests when API is unreachable |
55
+ | `cache_seconds` | `int` | `300` | Cache TTL for results |
56
+ | `base_url` | `str` | `https://api.agentscore.sh` | API base URL |
57
+ | `extract_address` | `callable` | Reads `x-wallet-address` header | Custom address extractor |
58
+ | `on_denied` | `async callable` | Returns 403 JSON | Custom denial handler |
59
+
60
+ ## How It Works
61
+
62
+ 1. Extracts wallet address from request header (`x-wallet-address`)
63
+ 2. Checks in-memory cache for a previous result
64
+ 3. Calls AgentScore `/v1/assess` with your policy
65
+ 4. Allows or blocks based on the decision
66
+ 5. Attaches data to `request.state.agentscore` on allowed requests
67
+
68
+ ## Documentation
69
+
70
+ - [API Reference](https://docs.agentscore.sh)
71
+ - [ERC-8004 Standard](https://eips.ethereum.org/EIPS/eip-8004)
72
+ - [x402 Protocol](https://github.com/coinbase/x402)
73
+
74
+ ## License
75
+
76
+ [MIT](LICENSE)
@@ -0,0 +1,23 @@
1
+ # Security Policy
2
+
3
+ ## Reporting a Vulnerability
4
+
5
+ If you discover a security vulnerability, please report it responsibly.
6
+
7
+ **Do not open a public GitHub issue for security vulnerabilities.**
8
+
9
+ Instead, email us at **security@agentscore.sh** with:
10
+
11
+ - Description of the vulnerability
12
+ - Steps to reproduce
13
+ - Potential impact
14
+ - Suggested fix (if any)
15
+
16
+ We will acknowledge receipt within 48 hours and aim to release a fix within 7 days for critical issues.
17
+
18
+ ## Supported Versions
19
+
20
+ | Version | Supported |
21
+ |---------|-----------|
22
+ | 1.x | ✅ |
23
+ | < 1.0 | ❌ |
@@ -0,0 +1,22 @@
1
+ """Trust-gating middleware for Python web frameworks using AgentScore."""
2
+
3
+ from agentscore_gate.client import GateClient
4
+ from agentscore_gate.types import AssessResult, DenialReason, Grade
5
+
6
+ # ASGI middleware is the default import.
7
+ # Flask and Django adapters are imported from their submodules:
8
+ # from agentscore_gate.flask import agentscore_gate
9
+ # from agentscore_gate.django import AgentScoreMiddleware
10
+ try:
11
+ from agentscore_gate.middleware import AgentScoreGate
12
+ except ImportError:
13
+ # starlette not installed
14
+ AgentScoreGate = None # type: ignore[assignment,misc]
15
+
16
+ __all__ = [
17
+ "AgentScoreGate",
18
+ "AssessResult",
19
+ "DenialReason",
20
+ "GateClient",
21
+ "Grade",
22
+ ]
@@ -0,0 +1,51 @@
1
+ from __future__ import annotations
2
+
3
+ import threading
4
+ import time
5
+ from typing import Generic, TypeVar
6
+
7
+ T = TypeVar("T")
8
+
9
+
10
+ class TTLCache(Generic[T]):
11
+ """Simple in-memory cache with per-entry TTL expiry."""
12
+
13
+ def __init__(self, default_ttl_seconds: float, max_size: int = 10000) -> None:
14
+ self._store: dict[str, tuple[T, float]] = {}
15
+ self._default_ttl = default_ttl_seconds
16
+ self._max_size = max_size
17
+ self._lock = threading.Lock()
18
+
19
+ def get(self, key: str) -> T | None:
20
+ """Return the cached value, or ``None`` if missing or expired."""
21
+ with self._lock:
22
+ entry = self._store.get(key)
23
+ if entry is None:
24
+ return None
25
+ value, expires_at = entry
26
+ if time.monotonic() > expires_at:
27
+ del self._store[key]
28
+ return None
29
+ return value
30
+
31
+ def set(self, key: str, value: T, ttl: float | None = None) -> None:
32
+ """Store *value* under *key* with an optional custom TTL (seconds)."""
33
+ with self._lock:
34
+ if len(self._store) >= self._max_size and key not in self._store:
35
+ self._sweep_expired()
36
+ if len(self._store) >= self._max_size:
37
+ self._evict_oldest()
38
+ self._store[key] = (value, time.monotonic() + (ttl if ttl is not None else self._default_ttl))
39
+
40
+ def _sweep_expired(self) -> None:
41
+ """Remove all expired entries. Must be called with ``_lock`` held."""
42
+ now = time.monotonic()
43
+ expired = [k for k, (_, exp) in self._store.items() if now > exp]
44
+ for k in expired:
45
+ del self._store[k]
46
+
47
+ def _evict_oldest(self) -> None:
48
+ """Evict entries with the earliest expiry until under max_size. Must be called with ``_lock`` held."""
49
+ entries = sorted(self._store.items(), key=lambda item: item[1][1])
50
+ while len(self._store) >= self._max_size and entries:
51
+ del self._store[entries.pop(0)[0]]