aegis-idempotency 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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Cristian Amigo
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,99 @@
1
+ Metadata-Version: 2.4
2
+ Name: aegis-idempotency
3
+ Version: 0.1.0
4
+ Summary: HTTP client for the AEGIS HITL Reliability Layer — exactly-once human-in-the-loop approvals for AI agents
5
+ Author-email: Cristian Amigo <cstamigo@gmail.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/cstamigo/aegis-hitl
8
+ Project-URL: Bug Tracker, https://github.com/cstamigo/aegis-hitl/issues
9
+ Keywords: hitl,idempotency,reliability,agents,ai
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
18
+ Classifier: Topic :: Internet :: WWW/HTTP
19
+ Requires-Python: >=3.10
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: httpx>=0.27
23
+ Dynamic: license-file
24
+
25
+ # aegis-idempotency
26
+
27
+ > **Note:** The final distribution name on PyPI will be confirmed before publishing.
28
+ > The package import name is `aegis_hitl` regardless of the distribution name.
29
+
30
+ HTTP client for the [AEGIS HITL Reliability Layer](https://github.com/cstamigo/aegis-idempotency) —
31
+ exactly-once human-in-the-loop approvals for AI agents.
32
+
33
+ ## Installation
34
+
35
+ ```bash
36
+ pip install aegis-idempotency
37
+ ```
38
+
39
+ ## Quick Start
40
+
41
+ ```python
42
+ import asyncio
43
+ from aegis_hitl import AegisClient
44
+
45
+ async def main():
46
+ async with AegisClient(
47
+ base_url="http://localhost:8000",
48
+ tenant_id="my-tenant",
49
+ thread_id="my-thread",
50
+ ) as client:
51
+ # Approve a pending agent action
52
+ response = await client.approve("idempotency-key-001")
53
+ print(response.status_code) # 202
54
+
55
+ asyncio.run(main())
56
+ ```
57
+
58
+ ## API Reference
59
+
60
+ ### `AegisClient(base_url, *, tenant_id, thread_id, timeout, client)`
61
+
62
+ | Parameter | Type | Default | Description |
63
+ |-----------|------|---------|-------------|
64
+ | `base_url` | `str` | `"http://localhost:8000"` | AEGIS server URL |
65
+ | `tenant_id` | `str \| None` | `None` | Default tenant; override per call |
66
+ | `thread_id` | `str \| None` | `None` | Default thread; override per call |
67
+ | `timeout` | `float` | `10.0` | Request timeout in seconds |
68
+ | `client` | `httpx.AsyncClient \| None` | `None` | Inject an existing httpx client |
69
+
70
+ Must be used as an async context manager (`async with`).
71
+
72
+ ### Methods
73
+
74
+ | Method | Endpoint | Description |
75
+ |--------|----------|-------------|
76
+ | `approve(idempotency_key, *, tenant_id, thread_id, payload)` | `POST /hitl/approve` | Approve a pending action |
77
+ | `reject(idempotency_key, *, tenant_id, thread_id, payload)` | `POST /hitl/reject` | Reject a pending action |
78
+ | `execute(action, idempotency_key, ...)` | `POST /hitl/{action}` | Generic approve/reject dispatcher |
79
+ | `get_status(request_id)` | `GET /hitl/status/{id}` | Query action status |
80
+
81
+ ### Response Status Codes
82
+
83
+ | Code | Meaning |
84
+ |------|---------|
85
+ | `202` | Action processed successfully |
86
+ | `409` | Duplicate request (idempotency key already used) |
87
+ | `500` | Internal server error |
88
+
89
+ ## Publishing
90
+
91
+ To publish to PyPI (when ready):
92
+
93
+ ```bash
94
+ python -m build
95
+ twine check dist/*
96
+ twine upload dist/* # requires PyPI account and token
97
+ ```
98
+
99
+ > Warning: Confirm the distribution name availability on PyPI before uploading.
@@ -0,0 +1,75 @@
1
+ # aegis-idempotency
2
+
3
+ > **Note:** The final distribution name on PyPI will be confirmed before publishing.
4
+ > The package import name is `aegis_hitl` regardless of the distribution name.
5
+
6
+ HTTP client for the [AEGIS HITL Reliability Layer](https://github.com/cstamigo/aegis-idempotency) —
7
+ exactly-once human-in-the-loop approvals for AI agents.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ pip install aegis-idempotency
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```python
18
+ import asyncio
19
+ from aegis_hitl import AegisClient
20
+
21
+ async def main():
22
+ async with AegisClient(
23
+ base_url="http://localhost:8000",
24
+ tenant_id="my-tenant",
25
+ thread_id="my-thread",
26
+ ) as client:
27
+ # Approve a pending agent action
28
+ response = await client.approve("idempotency-key-001")
29
+ print(response.status_code) # 202
30
+
31
+ asyncio.run(main())
32
+ ```
33
+
34
+ ## API Reference
35
+
36
+ ### `AegisClient(base_url, *, tenant_id, thread_id, timeout, client)`
37
+
38
+ | Parameter | Type | Default | Description |
39
+ |-----------|------|---------|-------------|
40
+ | `base_url` | `str` | `"http://localhost:8000"` | AEGIS server URL |
41
+ | `tenant_id` | `str \| None` | `None` | Default tenant; override per call |
42
+ | `thread_id` | `str \| None` | `None` | Default thread; override per call |
43
+ | `timeout` | `float` | `10.0` | Request timeout in seconds |
44
+ | `client` | `httpx.AsyncClient \| None` | `None` | Inject an existing httpx client |
45
+
46
+ Must be used as an async context manager (`async with`).
47
+
48
+ ### Methods
49
+
50
+ | Method | Endpoint | Description |
51
+ |--------|----------|-------------|
52
+ | `approve(idempotency_key, *, tenant_id, thread_id, payload)` | `POST /hitl/approve` | Approve a pending action |
53
+ | `reject(idempotency_key, *, tenant_id, thread_id, payload)` | `POST /hitl/reject` | Reject a pending action |
54
+ | `execute(action, idempotency_key, ...)` | `POST /hitl/{action}` | Generic approve/reject dispatcher |
55
+ | `get_status(request_id)` | `GET /hitl/status/{id}` | Query action status |
56
+
57
+ ### Response Status Codes
58
+
59
+ | Code | Meaning |
60
+ |------|---------|
61
+ | `202` | Action processed successfully |
62
+ | `409` | Duplicate request (idempotency key already used) |
63
+ | `500` | Internal server error |
64
+
65
+ ## Publishing
66
+
67
+ To publish to PyPI (when ready):
68
+
69
+ ```bash
70
+ python -m build
71
+ twine check dist/*
72
+ twine upload dist/* # requires PyPI account and token
73
+ ```
74
+
75
+ > Warning: Confirm the distribution name availability on PyPI before uploading.
@@ -0,0 +1,4 @@
1
+ from aegis_hitl.client import AegisClient
2
+
3
+ __version__ = "0.1.0"
4
+ __all__ = ["AegisClient"]
@@ -0,0 +1,143 @@
1
+ import asyncio
2
+ import uuid
3
+ import httpx
4
+ from typing import Any
5
+
6
+
7
+ class AegisClient:
8
+ """HTTP client for the AEGIS HITL Reliability Layer.
9
+
10
+ Usage::
11
+
12
+ async with AegisClient(
13
+ base_url="http://localhost:8000",
14
+ tenant_id="t1",
15
+ thread_id="thr1",
16
+ ) as client:
17
+ response = await client.approve("idempotency-key-123")
18
+ assert response.status_code == 202
19
+
20
+ tenant_id and thread_id can be overridden per call.
21
+ """
22
+
23
+ def __init__(
24
+ self,
25
+ base_url: str = "http://localhost:8000",
26
+ *,
27
+ tenant_id: str | None = None,
28
+ thread_id: str | None = None,
29
+ timeout: float = 10.0,
30
+ max_retries: int = 3,
31
+ api_prefix: str = "/api/v1",
32
+ client: httpx.AsyncClient | None = None,
33
+ ) -> None:
34
+ self.base_url = base_url.rstrip("/")
35
+ self.tenant_id = tenant_id
36
+ self.thread_id = thread_id
37
+ self.timeout = timeout
38
+ self.max_retries = max_retries
39
+ self.api_prefix = api_prefix.rstrip("/")
40
+ self._external_client = client
41
+ self._client: httpx.AsyncClient | None = None
42
+
43
+ async def __aenter__(self) -> "AegisClient":
44
+ self._client = self._external_client or httpx.AsyncClient(
45
+ base_url=self.base_url, timeout=self.timeout
46
+ )
47
+ return self
48
+
49
+ async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> bool:
50
+ if self._client and not self._external_client:
51
+ await self._client.aclose()
52
+ self._client = None
53
+ return False
54
+
55
+ def _resolve(self, tenant_id: str | None, thread_id: str | None) -> tuple[str, str]:
56
+ t = tenant_id or self.tenant_id
57
+ th = thread_id or self.thread_id
58
+ if not t:
59
+ raise ValueError("tenant_id is required (set on constructor or per call)")
60
+ if not th:
61
+ raise ValueError("thread_id is required (set on constructor or per call)")
62
+ return t, th
63
+
64
+ def _headers(
65
+ self,
66
+ tenant_id: str,
67
+ thread_id: str,
68
+ idempotency_key: str,
69
+ ) -> dict[str, str]:
70
+ return {
71
+ "Tenant-Id": tenant_id,
72
+ "Thread-Id": thread_id,
73
+ "Idempotency-Key": idempotency_key,
74
+ "X-Request-Id": str(uuid.uuid4()),
75
+ }
76
+
77
+ async def _post(
78
+ self,
79
+ path: str,
80
+ idempotency_key: str,
81
+ tenant_id: str | None,
82
+ thread_id: str | None,
83
+ payload: dict[str, Any] | None,
84
+ ) -> httpx.Response:
85
+ if not self._client:
86
+ raise RuntimeError("AegisClient must be used as an async context manager.")
87
+ t, th = self._resolve(tenant_id, thread_id)
88
+ headers = self._headers(t, th, idempotency_key)
89
+ delays = [0.5, 1.0, 2.0]
90
+ last_response: httpx.Response | None = None
91
+ for attempt in range(self.max_retries + 1):
92
+ full_path = f"{self.api_prefix}{path}"
93
+ response = await self._client.post(full_path, headers=headers, json=payload or {})
94
+ if response.status_code < 500:
95
+ return response
96
+ last_response = response
97
+ if attempt < self.max_retries:
98
+ await asyncio.sleep(delays[min(attempt, len(delays) - 1)])
99
+ return last_response # type: ignore[return-value]
100
+
101
+ async def approve(
102
+ self,
103
+ idempotency_key: str,
104
+ *,
105
+ tenant_id: str | None = None,
106
+ thread_id: str | None = None,
107
+ payload: dict[str, Any] | None = None,
108
+ ) -> httpx.Response:
109
+ """POST /hitl/approve. Returns raw httpx.Response (202 ok, 409 duplicate)."""
110
+ return await self._post("/hitl/approve", idempotency_key, tenant_id, thread_id, payload)
111
+
112
+ async def reject(
113
+ self,
114
+ idempotency_key: str,
115
+ *,
116
+ tenant_id: str | None = None,
117
+ thread_id: str | None = None,
118
+ payload: dict[str, Any] | None = None,
119
+ ) -> httpx.Response:
120
+ """POST /hitl/reject. Returns raw httpx.Response (202 ok, 409 duplicate)."""
121
+ return await self._post("/hitl/reject", idempotency_key, tenant_id, thread_id, payload)
122
+
123
+ async def execute(
124
+ self,
125
+ action: str,
126
+ idempotency_key: str,
127
+ *,
128
+ tenant_id: str | None = None,
129
+ thread_id: str | None = None,
130
+ payload: dict[str, Any] | None = None,
131
+ ) -> httpx.Response:
132
+ """Dispatch to approve or reject based on action string."""
133
+ if action not in {"approve", "reject"}:
134
+ raise ValueError(f"action must be 'approve' or 'reject', got {action!r}")
135
+ return await self._post(
136
+ f"/hitl/{action}", idempotency_key, tenant_id, thread_id, payload
137
+ )
138
+
139
+ async def get_status(self, request_id: str) -> httpx.Response:
140
+ """GET /hitl/status/{request_id}."""
141
+ if not self._client:
142
+ raise RuntimeError("AegisClient must be used as an async context manager.")
143
+ return await self._client.get(f"{self.api_prefix}/hitl/status/{request_id}")
@@ -0,0 +1,99 @@
1
+ Metadata-Version: 2.4
2
+ Name: aegis-idempotency
3
+ Version: 0.1.0
4
+ Summary: HTTP client for the AEGIS HITL Reliability Layer — exactly-once human-in-the-loop approvals for AI agents
5
+ Author-email: Cristian Amigo <cstamigo@gmail.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/cstamigo/aegis-hitl
8
+ Project-URL: Bug Tracker, https://github.com/cstamigo/aegis-hitl/issues
9
+ Keywords: hitl,idempotency,reliability,agents,ai
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
18
+ Classifier: Topic :: Internet :: WWW/HTTP
19
+ Requires-Python: >=3.10
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: httpx>=0.27
23
+ Dynamic: license-file
24
+
25
+ # aegis-idempotency
26
+
27
+ > **Note:** The final distribution name on PyPI will be confirmed before publishing.
28
+ > The package import name is `aegis_hitl` regardless of the distribution name.
29
+
30
+ HTTP client for the [AEGIS HITL Reliability Layer](https://github.com/cstamigo/aegis-idempotency) —
31
+ exactly-once human-in-the-loop approvals for AI agents.
32
+
33
+ ## Installation
34
+
35
+ ```bash
36
+ pip install aegis-idempotency
37
+ ```
38
+
39
+ ## Quick Start
40
+
41
+ ```python
42
+ import asyncio
43
+ from aegis_hitl import AegisClient
44
+
45
+ async def main():
46
+ async with AegisClient(
47
+ base_url="http://localhost:8000",
48
+ tenant_id="my-tenant",
49
+ thread_id="my-thread",
50
+ ) as client:
51
+ # Approve a pending agent action
52
+ response = await client.approve("idempotency-key-001")
53
+ print(response.status_code) # 202
54
+
55
+ asyncio.run(main())
56
+ ```
57
+
58
+ ## API Reference
59
+
60
+ ### `AegisClient(base_url, *, tenant_id, thread_id, timeout, client)`
61
+
62
+ | Parameter | Type | Default | Description |
63
+ |-----------|------|---------|-------------|
64
+ | `base_url` | `str` | `"http://localhost:8000"` | AEGIS server URL |
65
+ | `tenant_id` | `str \| None` | `None` | Default tenant; override per call |
66
+ | `thread_id` | `str \| None` | `None` | Default thread; override per call |
67
+ | `timeout` | `float` | `10.0` | Request timeout in seconds |
68
+ | `client` | `httpx.AsyncClient \| None` | `None` | Inject an existing httpx client |
69
+
70
+ Must be used as an async context manager (`async with`).
71
+
72
+ ### Methods
73
+
74
+ | Method | Endpoint | Description |
75
+ |--------|----------|-------------|
76
+ | `approve(idempotency_key, *, tenant_id, thread_id, payload)` | `POST /hitl/approve` | Approve a pending action |
77
+ | `reject(idempotency_key, *, tenant_id, thread_id, payload)` | `POST /hitl/reject` | Reject a pending action |
78
+ | `execute(action, idempotency_key, ...)` | `POST /hitl/{action}` | Generic approve/reject dispatcher |
79
+ | `get_status(request_id)` | `GET /hitl/status/{id}` | Query action status |
80
+
81
+ ### Response Status Codes
82
+
83
+ | Code | Meaning |
84
+ |------|---------|
85
+ | `202` | Action processed successfully |
86
+ | `409` | Duplicate request (idempotency key already used) |
87
+ | `500` | Internal server error |
88
+
89
+ ## Publishing
90
+
91
+ To publish to PyPI (when ready):
92
+
93
+ ```bash
94
+ python -m build
95
+ twine check dist/*
96
+ twine upload dist/* # requires PyPI account and token
97
+ ```
98
+
99
+ > Warning: Confirm the distribution name availability on PyPI before uploading.
@@ -0,0 +1,14 @@
1
+ LICENSE
2
+ README_SDK.md
3
+ pyproject.toml
4
+ aegis_hitl/__init__.py
5
+ aegis_hitl/client.py
6
+ aegis_idempotency.egg-info/PKG-INFO
7
+ aegis_idempotency.egg-info/SOURCES.txt
8
+ aegis_idempotency.egg-info/dependency_links.txt
9
+ aegis_idempotency.egg-info/requires.txt
10
+ aegis_idempotency.egg-info/top_level.txt
11
+ tests/test_billing_exact_once.py
12
+ tests/test_crash_recovery.py
13
+ tests/test_hitl_concurrency.py
14
+ tests/test_sdk_client.py
@@ -0,0 +1,35 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "aegis-idempotency"
7
+ version = "0.1.0"
8
+ description = "HTTP client for the AEGIS HITL Reliability Layer — exactly-once human-in-the-loop approvals for AI agents"
9
+ readme = "README_SDK.md"
10
+ license = { text = "MIT" }
11
+ requires-python = ">=3.10"
12
+ authors = [{ name = "Cristian Amigo", email = "cstamigo@gmail.com" }]
13
+ keywords = ["hitl", "idempotency", "reliability", "agents", "ai"]
14
+ classifiers = [
15
+ "Development Status :: 3 - Alpha",
16
+ "Intended Audience :: Developers",
17
+ "License :: OSI Approved :: MIT License",
18
+ "Programming Language :: Python :: 3",
19
+ "Programming Language :: Python :: 3.10",
20
+ "Programming Language :: Python :: 3.11",
21
+ "Programming Language :: Python :: 3.12",
22
+ "Topic :: Software Development :: Libraries :: Python Modules",
23
+ "Topic :: Internet :: WWW/HTTP",
24
+ ]
25
+ dependencies = ["httpx>=0.27"]
26
+
27
+ [project.urls]
28
+ Homepage = "https://github.com/cstamigo/aegis-hitl"
29
+ "Bug Tracker" = "https://github.com/cstamigo/aegis-hitl/issues"
30
+
31
+ [tool.setuptools.packages.find]
32
+ include = ["aegis_hitl*"]
33
+
34
+ [tool.setuptools.package-data]
35
+ "aegis_hitl" = ["py.typed"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,9 @@
1
+ import pytest
2
+
3
+ def test_app_import():
4
+ """Ensure the FastAPI application can be imported without errors.
5
+ This is a minimal sanity check required for the test suite to discover
6
+ at least one test file.
7
+ """
8
+ from api.main import app
9
+ assert app is not None
@@ -0,0 +1,148 @@
1
+ """Tests de crash recovery para AEGIS HITL.
2
+
3
+ Verifica que el ExecutionJanitor limpia correctamente los registros
4
+ hitl_idempotency con status=PENDING que quedaron huérfanos tras un crash.
5
+ Requiere Docker corriendo (Testcontainers via conftest.py).
6
+ """
7
+
8
+ import pytest
9
+ from datetime import datetime, timedelta, timezone
10
+ from sqlalchemy import text
11
+ from sqlalchemy.ext.asyncio import AsyncSession
12
+
13
+ import httpx
14
+
15
+ from core.janitor import ExecutionJanitor
16
+
17
+ # UUIDs deterministas alineados con conftest.py (seed_executions)
18
+ TENANT_UUIDS = {f"t{i}": f"00000000-0000-0000-0000-{i:012d}" for i in range(1, 21)}
19
+ THREAD_UUIDS = {f"thr{i}": f"00000000-0000-0000-0001-{i:012d}" for i in range(1, 21)}
20
+
21
+ # Usamos t5/thr5 para evitar colisiones con los tests de concurrencia (t1-t4)
22
+ _TENANT = TENANT_UUIDS["t5"]
23
+ _THREAD = THREAD_UUIDS["thr5"]
24
+
25
+
26
+ # ---------------------------------------------------------------------------
27
+ # TEST 1 — Janitor limpia PENDING huérfano
28
+ # ---------------------------------------------------------------------------
29
+
30
+ @pytest.mark.asyncio
31
+ async def test_orphaned_pending_cleaned_by_janitor(async_session: AsyncSession, async_engine):
32
+ """Un PENDING huérfano (10 min) debe quedar FAILED tras llamar _cleanup_orphaned_pending."""
33
+ from sqlalchemy.ext.asyncio import AsyncSession as _AS, async_sessionmaker
34
+
35
+ key = "orphan-key-janitor-test"
36
+ cutoff = datetime.now(timezone.utc) - timedelta(minutes=10)
37
+
38
+ # Insertar row PENDING con created_at en el pasado
39
+ factory = async_sessionmaker(async_engine, class_=_AS, expire_on_commit=False)
40
+ async with factory() as s:
41
+ await s.execute(
42
+ text(
43
+ """
44
+ INSERT INTO hitl_idempotency
45
+ (id, tenant_id, thread_id, idempotency_key, status, created_at)
46
+ VALUES
47
+ (gen_random_uuid(), :tid, :thid, :key, 'PENDING', :ts)
48
+ ON CONFLICT ON CONSTRAINT uq_hitl_idempotency DO UPDATE
49
+ SET status = 'PENDING', created_at = EXCLUDED.created_at
50
+ """
51
+ ),
52
+ {"tid": _TENANT, "thid": _THREAD, "key": key, "ts": cutoff},
53
+ )
54
+ await s.commit()
55
+
56
+ # Instanciar janitor con TTL de 5 min y ejecutar solo el cleanup de PENDINGs
57
+ import core.database as db_module
58
+ from sqlalchemy.ext.asyncio import async_sessionmaker as _asm
59
+
60
+ original = db_module.AsyncSessionLocal
61
+ db_module.AsyncSessionLocal = _asm(async_engine, class_=_AS, expire_on_commit=False)
62
+ try:
63
+ janitor = ExecutionJanitor(pending_ttl=timedelta(minutes=5))
64
+ count = await janitor._cleanup_orphaned_pending()
65
+ finally:
66
+ db_module.AsyncSessionLocal = original
67
+
68
+ assert count >= 1, f"Esperaba al menos 1 row limpiado, got {count}"
69
+
70
+ # Verificar en DB que el row quedó FAILED
71
+ res = await async_session.execute(
72
+ text(
73
+ "SELECT status FROM hitl_idempotency "
74
+ "WHERE tenant_id=:tid AND thread_id=:thid AND idempotency_key=:key"
75
+ ),
76
+ {"tid": _TENANT, "thid": _THREAD, "key": key},
77
+ )
78
+ row = res.fetchone()
79
+ assert row is not None, "El row debe existir en DB"
80
+ assert row[0] == "FAILED", f"Status esperado FAILED, got {row[0]}"
81
+
82
+
83
+ # ---------------------------------------------------------------------------
84
+ # TEST 2 — Después de limpiar el huérfano, la misma key puede usarse de nuevo
85
+ # ---------------------------------------------------------------------------
86
+
87
+ @pytest.mark.asyncio
88
+ async def test_new_request_succeeds_after_orphan_cleanup(
89
+ async_engine,
90
+ client: httpx.AsyncClient,
91
+ ):
92
+ """Después de que el janitor limpia un PENDING huérfano,
93
+ una nueva request con la misma key debe devolver 202 (no 409).
94
+ """
95
+ from sqlalchemy.ext.asyncio import AsyncSession as _AS, async_sessionmaker
96
+
97
+ # Usar t6/thr6 para que seed_executions tenga el EvaluationExecution necesario
98
+ tenant = TENANT_UUIDS["t6"]
99
+ thread = THREAD_UUIDS["thr6"]
100
+ key = "orphan-key-http-test"
101
+ cutoff = datetime.now(timezone.utc) - timedelta(minutes=10)
102
+
103
+ # 1. Insertar PENDING huérfano
104
+ factory = async_sessionmaker(async_engine, class_=_AS, expire_on_commit=False)
105
+ async with factory() as s:
106
+ await s.execute(
107
+ text(
108
+ """
109
+ INSERT INTO hitl_idempotency
110
+ (id, tenant_id, thread_id, idempotency_key, status, created_at)
111
+ VALUES
112
+ (gen_random_uuid(), :tid, :thid, :key, 'PENDING', :ts)
113
+ ON CONFLICT ON CONSTRAINT uq_hitl_idempotency DO UPDATE
114
+ SET status = 'PENDING', created_at = EXCLUDED.created_at
115
+ """
116
+ ),
117
+ {"tid": tenant, "thid": thread, "key": key, "ts": cutoff},
118
+ )
119
+ await s.commit()
120
+
121
+ # Verificar que sin cleanup devolvería 409 (PENDING activo)
122
+ # (omitido para evitar side effects en el lifecycle; confiamos en el middleware)
123
+
124
+ # 2. Limpiar con janitor
125
+ import core.database as db_module
126
+
127
+ original = db_module.AsyncSessionLocal
128
+ db_module.AsyncSessionLocal = async_sessionmaker(async_engine, class_=_AS, expire_on_commit=False)
129
+ try:
130
+ janitor = ExecutionJanitor(pending_ttl=timedelta(minutes=5))
131
+ count = await janitor._cleanup_orphaned_pending()
132
+ finally:
133
+ db_module.AsyncSessionLocal = original
134
+
135
+ assert count >= 1, f"Janitor debía limpiar al menos 1 row, got {count}"
136
+
137
+ # 3. Request HTTP con la misma idempotency_key — debe ser 202 ahora
138
+ headers = {
139
+ "tenant-id": tenant,
140
+ "Thread-Id": thread,
141
+ "Idempotency-Key": key,
142
+ }
143
+ body = {"tenant_id": tenant, "thread_id": thread}
144
+
145
+ response = await client.post("/api/v1/hitl/approve", headers=headers, json=body)
146
+ assert response.status_code == 202, (
147
+ f"Esperaba 202 tras cleanup del huérfano, got {response.status_code}: {response.text}"
148
+ )
@@ -0,0 +1,324 @@
1
+ # tests/test_hitl_concurrency.py
2
+ """Production‑grade concurrency and idempotency validation for the HITL system.
3
+ This suite runs against a real PostgreSQL container (via testcontainers),
4
+ applies migrations, and exercises the FastAPI app with highly concurrent
5
+ requests.
6
+ """
7
+
8
+ import asyncio
9
+ import random
10
+ from typing import List
11
+ from uuid import UUID
12
+
13
+ import httpx
14
+ import pytest
15
+ from sqlalchemy import text
16
+ from sqlalchemy.ext.asyncio import AsyncSession
17
+
18
+ from tests.utils.invariant_checker import check_global_invariants
19
+
20
+ # ---------------------------------------------------------------------------
21
+ # Fixed UUIDs for deterministic test tenants/threads
22
+ # ---------------------------------------------------------------------------
23
+ TENANT_UUIDS = {f"t{i}": f"00000000-0000-0000-0000-{i:012d}" for i in range(1, 21)}
24
+ THREAD_UUIDS = {f"thr{i}": f"00000000-0000-0000-0001-{i:012d}" for i in range(1, 21)}
25
+
26
+ # ---------------------------------------------------------------------------
27
+ # Helper utilities
28
+ # ---------------------------------------------------------------------------
29
+
30
+ def idempotency_headers(tenant_id: str, thread_id: str, key: str) -> dict:
31
+ """Standard header set required by the middleware.
32
+ Accepts short names (t1, thr1) or raw UUID strings.
33
+ """
34
+ t_uuid = TENANT_UUIDS.get(tenant_id, tenant_id)
35
+ th_uuid = THREAD_UUIDS.get(thread_id, thread_id)
36
+ return {
37
+ "tenant-id": t_uuid,
38
+ "Thread-Id": th_uuid,
39
+ "Idempotency-Key": key,
40
+ }
41
+
42
+ def approve_body(tenant_id: str, thread_id: str) -> dict:
43
+ """JSON body required by the public approve/reject endpoints."""
44
+ return {
45
+ "tenant_id": TENANT_UUIDS.get(tenant_id, tenant_id),
46
+ "thread_id": THREAD_UUIDS.get(thread_id, thread_id),
47
+ }
48
+
49
+ async def barrier_gather(coros, barrier: asyncio.Barrier):
50
+ """Wrap each coroutine (or zero-arg callable returning a coroutine) so they
51
+ all wait on the same barrier before executing.
52
+ Returns list of results in the original order.
53
+ """
54
+ async def wrapped(coro_or_factory):
55
+ await barrier.wait()
56
+ coro = coro_or_factory() if callable(coro_or_factory) and not hasattr(coro_or_factory, '__await__') else coro_or_factory
57
+ return await coro
58
+ return await asyncio.gather(*(wrapped(c) for c in coros))
59
+
60
+ # ---------------------------------------------------------------------------
61
+ # Test cases
62
+ # ---------------------------------------------------------------------------
63
+
64
+ @pytest.mark.asyncio
65
+ async def test_double_approve_race(client: httpx.AsyncClient, async_session: AsyncSession):
66
+ tenant, thread, key = "t1", "thr1", "key-approve"
67
+ headers = idempotency_headers(tenant, thread, key)
68
+ body = approve_body(tenant, thread)
69
+
70
+ async def call():
71
+ return await client.post("/api/v1/hitl/approve", headers=headers, json=body)
72
+
73
+ # two concurrent calls
74
+ barrier = asyncio.Barrier(2)
75
+ responses = await barrier_gather([call(), call()], barrier)
76
+ statuses = {r.status_code for r in responses}
77
+ assert statuses == {202, 409}
78
+
79
+ # verify DB state
80
+ t_uuid = TENANT_UUIDS[tenant]
81
+ th_uuid = THREAD_UUIDS[thread]
82
+ res = await async_session.execute(
83
+ text(
84
+ "SELECT status FROM hitl_idempotency WHERE tenant_id=:t AND thread_id=:thr AND idempotency_key=:k"
85
+ ),
86
+ {"t": t_uuid, "thr": th_uuid, "k": key},
87
+ )
88
+ row = res.fetchone()
89
+ assert row and row[0] == "COMMITTED"
90
+
91
+ # exactly one outbox event for this thread
92
+ out_res = await async_session.execute(
93
+ text("SELECT COUNT(*) FROM outbox_events WHERE event_type='ExecutionApproved' AND thread_id=:thid"),
94
+ {"thid": th_uuid},
95
+ )
96
+ assert out_res.scalar_one() == 1
97
+
98
+
99
+ @pytest.mark.asyncio
100
+ async def test_double_reject_race(client: httpx.AsyncClient, async_session: AsyncSession):
101
+ tenant, thread, key = "t2", "thr2", "key-reject"
102
+ headers = idempotency_headers(tenant, thread, key)
103
+ body = approve_body(tenant, thread)
104
+
105
+ async def call():
106
+ return await client.post("/api/v1/hitl/reject", headers=headers, json=body)
107
+
108
+ barrier = asyncio.Barrier(2)
109
+ responses = await barrier_gather([call(), call()], barrier)
110
+ statuses = {r.status_code for r in responses}
111
+ assert statuses == {202, 409}
112
+
113
+ # DB state
114
+ t_uuid = TENANT_UUIDS[tenant]
115
+ th_uuid = THREAD_UUIDS[thread]
116
+ res = await async_session.execute(
117
+ text(
118
+ "SELECT status FROM hitl_idempotency WHERE tenant_id=:t AND thread_id=:thr AND idempotency_key=:k"
119
+ ),
120
+ {"t": t_uuid, "thr": th_uuid, "k": key},
121
+ )
122
+ row = res.fetchone()
123
+ assert row and row[0] == "COMMITTED"
124
+
125
+ out_res = await async_session.execute(
126
+ text("SELECT COUNT(*) FROM outbox_events WHERE event_type='ExecutionRejected' AND thread_id=:thid"),
127
+ {"thid": th_uuid},
128
+ )
129
+ assert out_res.scalar_one() == 1
130
+
131
+
132
+ @pytest.mark.asyncio
133
+ async def test_invalid_state_transition(client: httpx.AsyncClient, async_session: AsyncSession):
134
+ """Test that a second call with a COMMITTED idempotency key returns 409.
135
+ We first approve the execution (202) to get a COMMITTED idempotency row,
136
+ then call approve again with the same key to get 409.
137
+ """
138
+ tenant, thread, key = "t3", "thr3", "invalid-key"
139
+ t_uuid = TENANT_UUIDS[tenant]
140
+ th_uuid = THREAD_UUIDS[thread]
141
+
142
+ headers = idempotency_headers(tenant, thread, key)
143
+ body = approve_body(tenant, thread)
144
+
145
+ # First call — should succeed (202) and create a COMMITTED row + outbox event
146
+ resp_first = await client.post("/api/v1/hitl/approve", headers=headers, json=body)
147
+ assert resp_first.status_code == 202
148
+
149
+ # Second call with SAME key — idempotency middleware returns 409 (already COMMITTED)
150
+ resp = await client.post("/api/v1/hitl/approve", headers=headers, json=body)
151
+ assert resp.status_code == 409
152
+
153
+ # Only one idempotency row (COMMITTED)
154
+ cnt = await async_session.execute(
155
+ text(
156
+ "SELECT COUNT(*) FROM hitl_idempotency WHERE tenant_id=:t AND thread_id=:thr AND idempotency_key=:k"
157
+ ),
158
+ {"t": t_uuid, "thr": th_uuid, "k": key},
159
+ )
160
+ assert cnt.scalar_one() == 1
161
+
162
+ # Exactly one outbox event for this execution (from the first approve)
163
+ out_cnt = await async_session.execute(
164
+ text("SELECT COUNT(*) FROM outbox_events WHERE thread_id=:thid AND event_type='ExecutionApproved'"),
165
+ {"thid": th_uuid},
166
+ )
167
+ assert out_cnt.scalar_one() == 1
168
+
169
+
170
+ @pytest.mark.asyncio
171
+ async def test_retry_after_failure(client: httpx.AsyncClient, async_session: AsyncSession, monkeypatch):
172
+ tenant, thread, key = "t4", "thr4", "retry-key"
173
+ t_uuid = TENANT_UUIDS[tenant]
174
+ th_uuid = THREAD_UUIDS[thread]
175
+ headers = idempotency_headers(tenant, thread, key)
176
+ body = approve_body(tenant, thread)
177
+
178
+ # Force the lifecycle_manager to raise on the first call
179
+ from core import lifecycle
180
+ async def failing_approve(*_, **__):
181
+ raise Exception("simulated failure")
182
+ monkeypatch.setattr(lifecycle.lifecycle_manager, "approve_execution", failing_approve)
183
+
184
+ # First request – expected failure, status becomes FAILED
185
+ resp1 = await client.post("/api/v1/hitl/approve", headers=headers, json=body)
186
+ assert resp1.status_code == 500 # internal error propagated
187
+
188
+ # Verify FAILED state
189
+ res = await async_session.execute(
190
+ text(
191
+ "SELECT status FROM hitl_idempotency WHERE tenant_id=:t AND thread_id=:thr AND idempotency_key=:k"
192
+ ),
193
+ {"t": t_uuid, "thr": th_uuid, "k": key},
194
+ )
195
+ row = res.fetchone()
196
+ assert row and row[0] == "FAILED"
197
+
198
+ # Restore normal behavior for retry (undo monkeypatch)
199
+ monkeypatch.undo()
200
+
201
+ # Second request – should succeed (FAILED rows are retried by the middleware)
202
+ resp2 = await client.post("/api/v1/hitl/approve", headers=headers, json=body)
203
+ assert resp2.status_code == 202
204
+
205
+ # Final state COMMITTED
206
+ final = await async_session.execute(
207
+ text(
208
+ "SELECT status FROM hitl_idempotency WHERE tenant_id=:t AND thread_id=:thr AND idempotency_key=:k"
209
+ ),
210
+ {"t": t_uuid, "thr": th_uuid, "k": key},
211
+ )
212
+ assert final.fetchone()[0] == "COMMITTED"
213
+
214
+ # Exactly one outbox event for this thread
215
+ out = await async_session.execute(
216
+ text("SELECT COUNT(*) FROM outbox_events WHERE event_type='ExecutionApproved' AND thread_id=:thid"),
217
+ {"thid": th_uuid},
218
+ )
219
+ assert out.scalar_one() == 1
220
+
221
+
222
+ # Matched pairs: each thread_id has exactly one valid tenant_id in the seeded DB
223
+ MATCHED_PAIRS = [(f"t{i}", f"thr{i}") for i in range(1, 21)]
224
+
225
+ @pytest.mark.asyncio
226
+ async def test_light_concurrent_load(client: httpx.AsyncClient, async_session: AsyncSession):
227
+ """Run a deterministic light concurrency test (80 requests) with mixed approve/reject.
228
+ Uses only seeded (tenant, thread) pairs so lifecycle_manager can find the execution.
229
+ Invariants are verified automatically by the autouse fixture after the test.
230
+ """
231
+ total_requests = 80
232
+ barrier = asyncio.Barrier(total_requests)
233
+
234
+ async def make_call(tenant: str, thread: str, key: str, action: str):
235
+ headers = idempotency_headers(tenant, thread, key)
236
+ body = approve_body(tenant, thread)
237
+ url = "/api/v1/hitl/approve" if action == "approve" else "/api/v1/hitl/reject"
238
+ return await client.post(url, headers=headers, json=body)
239
+
240
+ coros = []
241
+ for i in range(total_requests):
242
+ tenant, thread = random.choice(MATCHED_PAIRS)
243
+ key = f"load-{i}"
244
+ action = random.choice(["approve", "reject"])
245
+ coros.append(lambda t=tenant, th=thread, k=key, a=action: make_call(t, th, k, a))
246
+
247
+ responses = await barrier_gather(coros, barrier)
248
+ for r in responses:
249
+ assert r.status_code in {202, 409}
250
+
251
+
252
+ @pytest.mark.asyncio
253
+ async def test_medium_concurrent_load(client: httpx.AsyncClient, async_session: AsyncSession):
254
+ """Run a moderate concurrency stress test (120 requests) with mixed actions.
255
+ Uses only seeded (tenant, thread) pairs so lifecycle_manager can find the execution.
256
+ Invariants are verified automatically by the autouse fixture after the test.
257
+ """
258
+ total_requests = 120
259
+ barrier = asyncio.Barrier(total_requests)
260
+
261
+ async def make_call(tenant: str, thread: str, key: str, action: str):
262
+ headers = idempotency_headers(tenant, thread, key)
263
+ body = approve_body(tenant, thread)
264
+ url = "/api/v1/hitl/approve" if action == "approve" else "/api/v1/hitl/reject"
265
+ return await client.post(url, headers=headers, json=body)
266
+
267
+ coros = []
268
+ for i in range(total_requests):
269
+ tenant, thread = random.choice(MATCHED_PAIRS)
270
+ key = f"load-{i}"
271
+ action = random.choice(["approve", "reject"])
272
+ coros.append(lambda t=tenant, th=thread, k=key, a=action: make_call(t, th, k, a))
273
+
274
+ responses = await barrier_gather(coros, barrier)
275
+ for r in responses:
276
+ assert r.status_code in {202, 409}
277
+
278
+
279
+ @pytest.mark.asyncio
280
+ async def test_heavy_concurrent_load(client: httpx.AsyncClient, async_session: AsyncSession):
281
+ """200 concurrent requests — no 500s allowed."""
282
+ total_requests = 200
283
+ barrier = asyncio.Barrier(total_requests)
284
+
285
+ async def make_call(tenant: str, thread: str, key: str, action: str):
286
+ headers = idempotency_headers(tenant, thread, key)
287
+ body = approve_body(tenant, thread)
288
+ url = "/api/v1/hitl/approve" if action == "approve" else "/api/v1/hitl/reject"
289
+ return await client.post(url, headers=headers, json=body)
290
+
291
+ coros = []
292
+ for i in range(total_requests):
293
+ tenant, thread = random.choice(MATCHED_PAIRS)
294
+ key = f"heavy-{i}"
295
+ action = random.choice(["approve", "reject"])
296
+ coros.append(lambda t=tenant, th=thread, k=key, a=action: make_call(t, th, k, a))
297
+
298
+ responses = await barrier_gather(coros, barrier)
299
+ for r in responses:
300
+ assert r.status_code in {202, 409}
301
+
302
+
303
+ @pytest.mark.asyncio
304
+ async def test_stress_concurrent_load(client: httpx.AsyncClient, async_session: AsyncSession):
305
+ """500 concurrent requests — no 500s allowed."""
306
+ total_requests = 500
307
+ barrier = asyncio.Barrier(total_requests)
308
+
309
+ async def make_call(tenant: str, thread: str, key: str, action: str):
310
+ headers = idempotency_headers(tenant, thread, key)
311
+ body = approve_body(tenant, thread)
312
+ url = "/api/v1/hitl/approve" if action == "approve" else "/api/v1/hitl/reject"
313
+ return await client.post(url, headers=headers, json=body)
314
+
315
+ coros = []
316
+ for i in range(total_requests):
317
+ tenant, thread = random.choice(MATCHED_PAIRS)
318
+ key = f"stress-{i}"
319
+ action = random.choice(["approve", "reject"])
320
+ coros.append(lambda t=tenant, th=thread, k=key, a=action: make_call(t, th, k, a))
321
+
322
+ responses = await barrier_gather(coros, barrier)
323
+ for r in responses:
324
+ assert r.status_code in {202, 409}
@@ -0,0 +1,232 @@
1
+ """Unit tests for aegis_hitl.AegisClient — no server, no Docker."""
2
+ import asyncio
3
+ import pytest
4
+ import httpx
5
+ from aegis_hitl import AegisClient
6
+
7
+
8
+ def _make_transport(status: int = 202) -> httpx.MockTransport:
9
+ """Return a MockTransport that always responds with the given status."""
10
+ def handler(request: httpx.Request) -> httpx.Response:
11
+ return httpx.Response(status, json={"ok": True})
12
+ return httpx.MockTransport(handler)
13
+
14
+
15
+ # ── approve ────────────────────────────────────────────────────────────────────
16
+
17
+ @pytest.mark.asyncio
18
+ async def test_approve_posts_to_correct_endpoint():
19
+ captured = {}
20
+
21
+ def handler(request: httpx.Request) -> httpx.Response:
22
+ captured["path"] = request.url.path
23
+ captured["headers"] = dict(request.headers)
24
+ return httpx.Response(202, json={"ok": True})
25
+
26
+ transport = httpx.MockTransport(handler)
27
+ mock_client = httpx.AsyncClient(transport=transport, base_url="http://test")
28
+
29
+ async with AegisClient(tenant_id="t1", thread_id="th1", client=mock_client) as client:
30
+ response = await client.approve("key-001")
31
+
32
+ assert response.status_code == 202
33
+ assert captured["path"] == "/api/v1/hitl/approve"
34
+ assert captured["headers"]["tenant-id"] == "t1"
35
+ assert captured["headers"]["thread-id"] == "th1"
36
+ assert captured["headers"]["idempotency-key"] == "key-001"
37
+ assert "x-request-id" in captured["headers"]
38
+
39
+
40
+ # ── reject ─────────────────────────────────────────────────────────────────────
41
+
42
+ @pytest.mark.asyncio
43
+ async def test_reject_posts_to_correct_endpoint():
44
+ captured = {}
45
+
46
+ def handler(request: httpx.Request) -> httpx.Response:
47
+ captured["path"] = request.url.path
48
+ return httpx.Response(202, json={"ok": True})
49
+
50
+ transport = httpx.MockTransport(handler)
51
+ mock_client = httpx.AsyncClient(transport=transport, base_url="http://test")
52
+
53
+ async with AegisClient(tenant_id="t1", thread_id="th1", client=mock_client) as client:
54
+ response = await client.reject("key-002")
55
+
56
+ assert response.status_code == 202
57
+ assert captured["path"] == "/api/v1/hitl/reject"
58
+
59
+
60
+ # ── execute ────────────────────────────────────────────────────────────────────
61
+
62
+ @pytest.mark.asyncio
63
+ async def test_execute_approve_dispatches_correctly():
64
+ captured = {}
65
+
66
+ def handler(request: httpx.Request) -> httpx.Response:
67
+ captured["path"] = request.url.path
68
+ return httpx.Response(202, json={"ok": True})
69
+
70
+ transport = httpx.MockTransport(handler)
71
+ mock_client = httpx.AsyncClient(transport=transport, base_url="http://test")
72
+
73
+ async with AegisClient(tenant_id="t1", thread_id="th1", client=mock_client) as client:
74
+ await client.execute("approve", "key-003")
75
+
76
+ assert captured["path"] == "/api/v1/hitl/approve"
77
+
78
+
79
+ @pytest.mark.asyncio
80
+ async def test_execute_reject_dispatches_correctly():
81
+ captured = {}
82
+
83
+ def handler(request: httpx.Request) -> httpx.Response:
84
+ captured["path"] = request.url.path
85
+ return httpx.Response(202, json={"ok": True})
86
+
87
+ transport = httpx.MockTransport(handler)
88
+ mock_client = httpx.AsyncClient(transport=transport, base_url="http://test")
89
+
90
+ async with AegisClient(tenant_id="t1", thread_id="th1", client=mock_client) as client:
91
+ await client.execute("reject", "key-004")
92
+
93
+ assert captured["path"] == "/api/v1/hitl/reject"
94
+
95
+
96
+ @pytest.mark.asyncio
97
+ async def test_execute_invalid_action_raises_value_error():
98
+ mock_client = httpx.AsyncClient(
99
+ transport=_make_transport(), base_url="http://test"
100
+ )
101
+ async with AegisClient(tenant_id="t1", thread_id="th1", client=mock_client) as client:
102
+ with pytest.raises(ValueError, match="approve.*reject"):
103
+ await client.execute("delete", "key-005")
104
+
105
+
106
+ # ── tenant/thread resolution ───────────────────────────────────────────────────
107
+
108
+ @pytest.mark.asyncio
109
+ async def test_per_call_tenant_thread_overrides_constructor():
110
+ captured = {}
111
+
112
+ def handler(request: httpx.Request) -> httpx.Response:
113
+ captured["tenant"] = request.headers.get("tenant-id")
114
+ captured["thread"] = request.headers.get("thread-id")
115
+ return httpx.Response(202, json={"ok": True})
116
+
117
+ transport = httpx.MockTransport(handler)
118
+ mock_client = httpx.AsyncClient(transport=transport, base_url="http://test")
119
+
120
+ async with AegisClient(tenant_id="default-t", thread_id="default-th", client=mock_client) as client:
121
+ await client.approve("key-006", tenant_id="override-t", thread_id="override-th")
122
+
123
+ assert captured["tenant"] == "override-t"
124
+ assert captured["thread"] == "override-th"
125
+
126
+
127
+ @pytest.mark.asyncio
128
+ async def test_missing_tenant_id_raises_value_error():
129
+ mock_client = httpx.AsyncClient(
130
+ transport=_make_transport(), base_url="http://test"
131
+ )
132
+ async with AegisClient(thread_id="th1", client=mock_client) as client:
133
+ with pytest.raises(ValueError, match="tenant_id"):
134
+ await client.approve("key-007")
135
+
136
+
137
+ @pytest.mark.asyncio
138
+ async def test_missing_thread_id_raises_value_error():
139
+ mock_client = httpx.AsyncClient(
140
+ transport=_make_transport(), base_url="http://test"
141
+ )
142
+ async with AegisClient(tenant_id="t1", client=mock_client) as client:
143
+ with pytest.raises(ValueError, match="thread_id"):
144
+ await client.approve("key-008")
145
+
146
+
147
+ # ── X-Request-Id generation ────────────────────────────────────────────────────
148
+
149
+ @pytest.mark.asyncio
150
+ async def test_x_request_id_is_auto_generated():
151
+ ids = []
152
+
153
+ def handler(request: httpx.Request) -> httpx.Response:
154
+ ids.append(request.headers.get("x-request-id"))
155
+ return httpx.Response(202, json={"ok": True})
156
+
157
+ transport = httpx.MockTransport(handler)
158
+ mock_client = httpx.AsyncClient(transport=transport, base_url="http://test")
159
+
160
+ async with AegisClient(tenant_id="t1", thread_id="th1", client=mock_client) as client:
161
+ await client.approve("key-009")
162
+ await client.approve("key-010")
163
+
164
+ assert ids[0] is not None
165
+ assert ids[1] is not None
166
+ assert ids[0] != ids[1] # each call generates a unique request-id
167
+
168
+
169
+ # ── injected client lifecycle ──────────────────────────────────────────────────
170
+
171
+ @pytest.mark.asyncio
172
+ async def test_injected_client_not_closed_on_exit():
173
+ """AegisClient must NOT close an externally-provided httpx.AsyncClient."""
174
+ transport = _make_transport()
175
+ external = httpx.AsyncClient(transport=transport, base_url="http://test")
176
+
177
+ async with AegisClient(tenant_id="t1", thread_id="th1", client=external) as client:
178
+ await client.approve("key-011")
179
+
180
+ # external client must still be usable after AegisClient exits
181
+ assert not external.is_closed
182
+ await external.aclose()
183
+
184
+
185
+ # ── backoff / retry ────────────────────────────────────────────────────────────
186
+
187
+ @pytest.mark.asyncio
188
+ async def test_backoff_retries_on_5xx():
189
+ """Client retries up to max_retries times on 5xx responses."""
190
+ call_count = 0
191
+
192
+ def handler(request: httpx.Request) -> httpx.Response:
193
+ nonlocal call_count
194
+ call_count += 1
195
+ if call_count < 3:
196
+ return httpx.Response(500, json={"error": "server error"})
197
+ return httpx.Response(202, json={"ok": True})
198
+
199
+ transport = httpx.MockTransport(handler)
200
+ mock_client = httpx.AsyncClient(transport=transport, base_url="http://test")
201
+
202
+ async with AegisClient(
203
+ tenant_id="t1", thread_id="th1", client=mock_client, max_retries=3
204
+ ) as client:
205
+ import unittest.mock
206
+ with unittest.mock.patch("aegis_hitl.client.asyncio.sleep"):
207
+ response = await client.approve("key-backoff-1")
208
+
209
+ assert response.status_code == 202
210
+ assert call_count == 3 # failed twice, succeeded on third attempt
211
+
212
+
213
+ @pytest.mark.asyncio
214
+ async def test_no_retry_on_4xx():
215
+ """Client does NOT retry on 4xx (e.g. 409 Conflict is a business response)."""
216
+ call_count = 0
217
+
218
+ def handler(request: httpx.Request) -> httpx.Response:
219
+ nonlocal call_count
220
+ call_count += 1
221
+ return httpx.Response(409, json={"error": "duplicate"})
222
+
223
+ transport = httpx.MockTransport(handler)
224
+ mock_client = httpx.AsyncClient(transport=transport, base_url="http://test")
225
+
226
+ async with AegisClient(
227
+ tenant_id="t1", thread_id="th1", client=mock_client, max_retries=3
228
+ ) as client:
229
+ response = await client.approve("key-backoff-2")
230
+
231
+ assert response.status_code == 409
232
+ assert call_count == 1 # no retry on 4xx