hold-sdk 0.2.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.
- hold_sdk-0.2.0/PKG-INFO +32 -0
- hold_sdk-0.2.0/README.md +17 -0
- hold_sdk-0.2.0/hold_sdk/__init__.py +32 -0
- hold_sdk-0.2.0/hold_sdk/client.py +34 -0
- hold_sdk-0.2.0/hold_sdk.egg-info/PKG-INFO +32 -0
- hold_sdk-0.2.0/hold_sdk.egg-info/SOURCES.txt +11 -0
- hold_sdk-0.2.0/hold_sdk.egg-info/dependency_links.txt +1 -0
- hold_sdk-0.2.0/hold_sdk.egg-info/requires.txt +6 -0
- hold_sdk-0.2.0/hold_sdk.egg-info/top_level.txt +1 -0
- hold_sdk-0.2.0/pyproject.toml +34 -0
- hold_sdk-0.2.0/setup.cfg +4 -0
- hold_sdk-0.2.0/tests/test_client.py +74 -0
- hold_sdk-0.2.0/tests/test_request_approval.py +33 -0
hold_sdk-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hold-sdk
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Fail-closed approval client for Hold My Agent
|
|
5
|
+
License: MIT
|
|
6
|
+
Project-URL: Homepage, https://holdmyagent.com
|
|
7
|
+
Project-URL: Repository, https://github.com/holdmyagent/arbiter
|
|
8
|
+
Requires-Python: >=3.11
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: httpx>=0.27
|
|
11
|
+
Provides-Extra: dev
|
|
12
|
+
Requires-Dist: pytest>=8.3; extra == "dev"
|
|
13
|
+
Requires-Dist: pytest-asyncio>=0.24; extra == "dev"
|
|
14
|
+
Requires-Dist: ruff>=0.6; extra == "dev"
|
|
15
|
+
|
|
16
|
+
# `hold-sdk`
|
|
17
|
+
|
|
18
|
+
Python client for Hold My Agent's Arbiter server — gate an agent action
|
|
19
|
+
behind a human approval with one call:
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from hold_sdk import request_approval
|
|
23
|
+
|
|
24
|
+
if request_approval("Deploy to prod?", severity="high") != "approved":
|
|
25
|
+
raise SystemExit("blocked: not approved")
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Fail-closed: any timeout, network error, or unconfigured client returns
|
|
29
|
+
`"denied"`, never raises.
|
|
30
|
+
|
|
31
|
+
- Homepage: https://holdmyagent.com
|
|
32
|
+
- Source / issues: https://github.com/holdmyagent/arbiter
|
hold_sdk-0.2.0/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# `hold-sdk`
|
|
2
|
+
|
|
3
|
+
Python client for Hold My Agent's Arbiter server — gate an agent action
|
|
4
|
+
behind a human approval with one call:
|
|
5
|
+
|
|
6
|
+
```python
|
|
7
|
+
from hold_sdk import request_approval
|
|
8
|
+
|
|
9
|
+
if request_approval("Deploy to prod?", severity="high") != "approved":
|
|
10
|
+
raise SystemExit("blocked: not approved")
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Fail-closed: any timeout, network error, or unconfigured client returns
|
|
14
|
+
`"denied"`, never raises.
|
|
15
|
+
|
|
16
|
+
- Homepage: https://holdmyagent.com
|
|
17
|
+
- Source / issues: https://github.com/holdmyagent/arbiter
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import time
|
|
3
|
+
import httpx
|
|
4
|
+
|
|
5
|
+
def request_approval(title, *, description="", severity="medium", target=None,
|
|
6
|
+
ttl_seconds=300, payload=None, action_type="generic",
|
|
7
|
+
server_url=None, token=None, poll_interval=2, timeout=None,
|
|
8
|
+
_transport=None) -> str:
|
|
9
|
+
server_url = server_url or os.environ.get("HMA_SERVER_URL")
|
|
10
|
+
token = token or os.environ.get("HMA_AGENT_TOKEN")
|
|
11
|
+
if not server_url or not token:
|
|
12
|
+
return "denied" # fail-closed: unconfigured is a no
|
|
13
|
+
hdr = {"Authorization": f"Bearer {token}"}
|
|
14
|
+
try:
|
|
15
|
+
with httpx.Client(base_url=server_url.rstrip("/"), timeout=10, transport=_transport) as c:
|
|
16
|
+
r = c.post("/v1/requests", headers=hdr, json={
|
|
17
|
+
"title": title, "description": description, "action_type": action_type,
|
|
18
|
+
"payload": payload or {}, "severity": severity,
|
|
19
|
+
"ttl_seconds": ttl_seconds, "target": target})
|
|
20
|
+
r.raise_for_status()
|
|
21
|
+
rid = r.json()["id"]
|
|
22
|
+
deadline = time.time() + (timeout if timeout is not None else ttl_seconds + 5)
|
|
23
|
+
while time.time() < deadline:
|
|
24
|
+
g = c.get(f"/v1/requests/{rid}", headers=hdr)
|
|
25
|
+
g.raise_for_status()
|
|
26
|
+
status = g.json()["status"]
|
|
27
|
+
if status in ("approved", "denied", "expired"):
|
|
28
|
+
return status
|
|
29
|
+
time.sleep(poll_interval)
|
|
30
|
+
except Exception:
|
|
31
|
+
return "denied" # fail-closed
|
|
32
|
+
return "denied" # local timeout
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import httpx
|
|
3
|
+
|
|
4
|
+
class ArbiterClient:
|
|
5
|
+
def __init__(self, base_url, agent_token, app_token=None, verify=True):
|
|
6
|
+
self.base_url = base_url.rstrip("/")
|
|
7
|
+
self.agent_token = agent_token
|
|
8
|
+
self._http = httpx.Client(base_url=self.base_url, verify=verify, timeout=10)
|
|
9
|
+
|
|
10
|
+
def request_approval(self, title, description="", action_type="generic",
|
|
11
|
+
payload=None, severity="medium", ttl=300, target=None,
|
|
12
|
+
poll_interval=2, timeout=None) -> str:
|
|
13
|
+
try:
|
|
14
|
+
r = self._http.post("/v1/requests",
|
|
15
|
+
headers={"Authorization": f"Bearer {self.agent_token}"},
|
|
16
|
+
json={"title": title, "description": description, "action_type": action_type,
|
|
17
|
+
"payload": payload or {}, "severity": severity, "ttl_seconds": ttl, "target": target})
|
|
18
|
+
r.raise_for_status()
|
|
19
|
+
rid = r.json()["id"]
|
|
20
|
+
except Exception:
|
|
21
|
+
return "denied" # fail-closed
|
|
22
|
+
deadline = time.time() + (timeout if timeout is not None else ttl + 5)
|
|
23
|
+
while time.time() < deadline:
|
|
24
|
+
try:
|
|
25
|
+
g = self._http.get(f"/v1/requests/{rid}",
|
|
26
|
+
headers={"Authorization": f"Bearer {self.agent_token}"})
|
|
27
|
+
g.raise_for_status()
|
|
28
|
+
status = g.json()["status"]
|
|
29
|
+
except Exception:
|
|
30
|
+
return "denied" # fail-closed
|
|
31
|
+
if status in ("approved", "denied", "expired"):
|
|
32
|
+
return status
|
|
33
|
+
time.sleep(poll_interval)
|
|
34
|
+
return "denied" # fail-closed on local timeout
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hold-sdk
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Fail-closed approval client for Hold My Agent
|
|
5
|
+
License: MIT
|
|
6
|
+
Project-URL: Homepage, https://holdmyagent.com
|
|
7
|
+
Project-URL: Repository, https://github.com/holdmyagent/arbiter
|
|
8
|
+
Requires-Python: >=3.11
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: httpx>=0.27
|
|
11
|
+
Provides-Extra: dev
|
|
12
|
+
Requires-Dist: pytest>=8.3; extra == "dev"
|
|
13
|
+
Requires-Dist: pytest-asyncio>=0.24; extra == "dev"
|
|
14
|
+
Requires-Dist: ruff>=0.6; extra == "dev"
|
|
15
|
+
|
|
16
|
+
# `hold-sdk`
|
|
17
|
+
|
|
18
|
+
Python client for Hold My Agent's Arbiter server — gate an agent action
|
|
19
|
+
behind a human approval with one call:
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from hold_sdk import request_approval
|
|
23
|
+
|
|
24
|
+
if request_approval("Deploy to prod?", severity="high") != "approved":
|
|
25
|
+
raise SystemExit("blocked: not approved")
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Fail-closed: any timeout, network error, or unconfigured client returns
|
|
29
|
+
`"denied"`, never raises.
|
|
30
|
+
|
|
31
|
+
- Homepage: https://holdmyagent.com
|
|
32
|
+
- Source / issues: https://github.com/holdmyagent/arbiter
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
hold_sdk/__init__.py
|
|
4
|
+
hold_sdk/client.py
|
|
5
|
+
hold_sdk.egg-info/PKG-INFO
|
|
6
|
+
hold_sdk.egg-info/SOURCES.txt
|
|
7
|
+
hold_sdk.egg-info/dependency_links.txt
|
|
8
|
+
hold_sdk.egg-info/requires.txt
|
|
9
|
+
hold_sdk.egg-info/top_level.txt
|
|
10
|
+
tests/test_client.py
|
|
11
|
+
tests/test_request_approval.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hold_sdk
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "hold-sdk"
|
|
3
|
+
version = "0.2.0"
|
|
4
|
+
description = "Fail-closed approval client for Hold My Agent"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = {text = "MIT"}
|
|
7
|
+
requires-python = ">=3.11"
|
|
8
|
+
dependencies = [
|
|
9
|
+
"httpx>=0.27",
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
# SDK tests exercise the client against the real server — install with: pip install -r requirements-dev.txt
|
|
13
|
+
[project.optional-dependencies]
|
|
14
|
+
dev = ["pytest>=8.3", "pytest-asyncio>=0.24", "ruff>=0.6"]
|
|
15
|
+
|
|
16
|
+
[project.urls]
|
|
17
|
+
Homepage = "https://holdmyagent.com"
|
|
18
|
+
Repository = "https://github.com/holdmyagent/arbiter"
|
|
19
|
+
|
|
20
|
+
[tool.pytest.ini_options]
|
|
21
|
+
addopts = "-q"
|
|
22
|
+
testpaths = ["tests"]
|
|
23
|
+
|
|
24
|
+
[tool.ruff]
|
|
25
|
+
line-length = 110
|
|
26
|
+
target-version = "py311"
|
|
27
|
+
|
|
28
|
+
[build-system]
|
|
29
|
+
requires = ["setuptools>=68"]
|
|
30
|
+
build-backend = "setuptools.build_meta"
|
|
31
|
+
|
|
32
|
+
[tool.setuptools.packages.find]
|
|
33
|
+
where = ["."]
|
|
34
|
+
include = ["hold_sdk*"]
|
hold_sdk-0.2.0/setup.cfg
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import tempfile
|
|
2
|
+
import threading
|
|
3
|
+
import time
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from fastapi import FastAPI
|
|
6
|
+
from fastapi.responses import PlainTextResponse, Response
|
|
7
|
+
from fastapi.testclient import TestClient
|
|
8
|
+
from arbiter.app import create_app
|
|
9
|
+
from arbiter.config import Config
|
|
10
|
+
from arbiter.db import Database
|
|
11
|
+
from hold_sdk.client import ArbiterClient
|
|
12
|
+
|
|
13
|
+
def _server():
|
|
14
|
+
absent = Path(tempfile.mkdtemp()) / "absent.toml" # never written -> Config.load uses defaults
|
|
15
|
+
cfg = Config.load(str(absent))
|
|
16
|
+
cfg.auth.agent_token = "A"
|
|
17
|
+
cfg.auth.app_token = "P"
|
|
18
|
+
cfg.auth.admin_password = "pw"
|
|
19
|
+
cfg.auth.session_secret = "secret"
|
|
20
|
+
db = Database(":memory:")
|
|
21
|
+
class S:
|
|
22
|
+
async def send(self,t,p): return "skipped"
|
|
23
|
+
return create_app(cfg, db, S()), db
|
|
24
|
+
|
|
25
|
+
def test_approved():
|
|
26
|
+
app, db = _server()
|
|
27
|
+
# TestClient is an httpx.Client subclass that properly bridges sync→async ASGI
|
|
28
|
+
tc = TestClient(app, base_url="http://test", raise_server_exceptions=False)
|
|
29
|
+
client = ArbiterClient("http://test","A",app_token="P")
|
|
30
|
+
client._http = tc
|
|
31
|
+
# create via SDK in a thread, approve out-of-band
|
|
32
|
+
result = {}
|
|
33
|
+
def run(): result["r"] = client.request_approval("t", severity="low", ttl=300, poll_interval=0.05)
|
|
34
|
+
th = threading.Thread(target=run)
|
|
35
|
+
th.start()
|
|
36
|
+
time.sleep(0.2)
|
|
37
|
+
rid = db.list_requests("pending")[0]["id"]
|
|
38
|
+
db.set_decision(rid, "approve", "iPhone")
|
|
39
|
+
th.join(timeout=5)
|
|
40
|
+
assert result["r"] == "approved"
|
|
41
|
+
|
|
42
|
+
def test_failclosed_bad_url():
|
|
43
|
+
client = ArbiterClient("http://127.0.0.1:1","A")
|
|
44
|
+
assert client.request_approval("t", ttl=1, poll_interval=0.1, timeout=0.5)=="denied"
|
|
45
|
+
|
|
46
|
+
def test_failclosed_server_500_on_create():
|
|
47
|
+
"""Server returns HTTP 500 on POST /v1/requests → fail-closed (denied)."""
|
|
48
|
+
fapp = FastAPI()
|
|
49
|
+
|
|
50
|
+
@fapp.post("/v1/requests")
|
|
51
|
+
def _create_error():
|
|
52
|
+
return Response(status_code=500)
|
|
53
|
+
|
|
54
|
+
tc = TestClient(fapp, base_url="http://test", raise_server_exceptions=False)
|
|
55
|
+
client = ArbiterClient("http://test", "A")
|
|
56
|
+
client._http = tc
|
|
57
|
+
assert client.request_approval("t", ttl=5, poll_interval=0.05, timeout=1) == "denied"
|
|
58
|
+
|
|
59
|
+
def test_failclosed_garbage_poll_body():
|
|
60
|
+
"""Poll response is not valid JSON → fail-closed (denied)."""
|
|
61
|
+
fapp = FastAPI()
|
|
62
|
+
|
|
63
|
+
@fapp.post("/v1/requests")
|
|
64
|
+
def _create_ok():
|
|
65
|
+
return {"id": "stub-x"}
|
|
66
|
+
|
|
67
|
+
@fapp.get("/v1/requests/{rid}")
|
|
68
|
+
def _get_garbage(rid: str):
|
|
69
|
+
return PlainTextResponse("not-json!!!")
|
|
70
|
+
|
|
71
|
+
tc = TestClient(fapp, base_url="http://test", raise_server_exceptions=False)
|
|
72
|
+
client = ArbiterClient("http://test", "A")
|
|
73
|
+
client._http = tc
|
|
74
|
+
assert client.request_approval("t", ttl=5, poll_interval=0.05, timeout=1) == "denied"
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import httpx
|
|
2
|
+
from hold_sdk import request_approval
|
|
3
|
+
|
|
4
|
+
def _transport(status_after=1, decision="approved", capture=None):
|
|
5
|
+
state = {"polls": 0}
|
|
6
|
+
def handler(request):
|
|
7
|
+
if capture is not None and request.method == "POST":
|
|
8
|
+
import json
|
|
9
|
+
capture.update(json.loads(request.read()))
|
|
10
|
+
if request.method == "POST":
|
|
11
|
+
return httpx.Response(200, json={"id": "r1", "status": "pending"})
|
|
12
|
+
state["polls"] += 1
|
|
13
|
+
st = decision if state["polls"] >= status_after else "pending"
|
|
14
|
+
return httpx.Response(200, json={"id": "r1", "status": st})
|
|
15
|
+
return httpx.MockTransport(handler)
|
|
16
|
+
|
|
17
|
+
def test_approved_and_fields_sent(monkeypatch):
|
|
18
|
+
monkeypatch.setenv("HMA_SERVER_URL", "http://test")
|
|
19
|
+
monkeypatch.setenv("HMA_AGENT_TOKEN", "t")
|
|
20
|
+
sent = {}
|
|
21
|
+
out = request_approval("Deploy?", severity="high", target="prod", poll_interval=0,
|
|
22
|
+
_transport=_transport(capture=sent))
|
|
23
|
+
assert out == "approved" and sent["target"] == "prod" and sent["severity"] == "high"
|
|
24
|
+
|
|
25
|
+
def test_missing_config_is_denied(monkeypatch):
|
|
26
|
+
monkeypatch.delenv("HMA_SERVER_URL", raising=False)
|
|
27
|
+
monkeypatch.delenv("HMA_AGENT_TOKEN", raising=False)
|
|
28
|
+
assert request_approval("x") == "denied"
|
|
29
|
+
|
|
30
|
+
def test_server_down_is_denied(monkeypatch):
|
|
31
|
+
monkeypatch.setenv("HMA_SERVER_URL", "http://127.0.0.1:1")
|
|
32
|
+
monkeypatch.setenv("HMA_AGENT_TOKEN", "t")
|
|
33
|
+
assert request_approval("x", timeout=1, poll_interval=0) == "denied"
|