cortexhub 0.1.2__tar.gz → 0.1.4__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.
- {cortexhub-0.1.2 → cortexhub-0.1.4}/.gitignore +3 -0
- cortexhub-0.1.4/LICENSE +21 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/PKG-INFO +14 -10
- {cortexhub-0.1.2 → cortexhub-0.1.4}/README.md +10 -9
- {cortexhub-0.1.2 → cortexhub-0.1.4}/pyproject.toml +5 -1
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/adapters/claude_agents.py +234 -7
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/adapters/crewai.py +275 -1
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/adapters/langgraph.py +119 -0
- cortexhub-0.1.4/src/cortexhub/adapters/openai_agents.py +516 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/client.py +84 -1
- cortexhub-0.1.2/src/cortexhub/adapters/openai_agents.py +0 -192
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/__init__.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/adapters/__init__.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/adapters/base.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/audit/__init__.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/audit/events.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/auto_protect.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/backend/__init__.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/backend/client.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/config.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/context/__init__.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/context/enricher.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/errors.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/frameworks.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/guardrails/__init__.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/guardrails/injection.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/guardrails/pii.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/guardrails/secrets.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/interceptors/__init__.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/interceptors/llm.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/interceptors/mcp.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/pipeline.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/policy/__init__.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/policy/effects.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/policy/evaluator.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/policy/loader.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/policy/models.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/policy/sync.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/telemetry/__init__.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/telemetry/otel.py +0 -0
- {cortexhub-0.1.2 → cortexhub-0.1.4}/src/cortexhub/version.py +0 -0
cortexhub-0.1.4/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CortexHub
|
|
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.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cortexhub
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: CortexHub Python SDK - Policy-as-Code for AI Agents
|
|
5
5
|
Project-URL: Homepage, https://cortexhub.ai
|
|
6
6
|
Project-URL: Documentation, https://docs.cortexhub.ai
|
|
@@ -8,6 +8,7 @@ Project-URL: Repository, https://github.com/cortexhub/sdks
|
|
|
8
8
|
Project-URL: Issues, https://github.com/cortexhub/sdks/issues
|
|
9
9
|
Author-email: CortexHub <hello@cortexhub.ai>
|
|
10
10
|
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
11
12
|
Keywords: agents,ai,authorization,cedar,governance,policy
|
|
12
13
|
Classifier: Development Status :: 4 - Beta
|
|
13
14
|
Classifier: Intended Audience :: Developers
|
|
@@ -38,12 +39,14 @@ Requires-Dist: crewai>=0.50.0; extra == 'all'
|
|
|
38
39
|
Requires-Dist: langchain-core>=0.2.0; extra == 'all'
|
|
39
40
|
Requires-Dist: langchain-openai>=0.1.0; extra == 'all'
|
|
40
41
|
Requires-Dist: langgraph>=0.2.0; extra == 'all'
|
|
42
|
+
Requires-Dist: litellm>=1.81.5; extra == 'all'
|
|
41
43
|
Requires-Dist: openai-agents>=0.0.3; extra == 'all'
|
|
42
44
|
Provides-Extra: claude-agents
|
|
43
45
|
Requires-Dist: anthropic>=0.40.0; extra == 'claude-agents'
|
|
44
46
|
Requires-Dist: claude-agent-sdk>=0.0.1; extra == 'claude-agents'
|
|
45
47
|
Provides-Extra: crewai
|
|
46
48
|
Requires-Dist: crewai>=0.50.0; extra == 'crewai'
|
|
49
|
+
Requires-Dist: litellm>=1.81.5; extra == 'crewai'
|
|
47
50
|
Provides-Extra: dev
|
|
48
51
|
Requires-Dist: mypy>=1.10.0; extra == 'dev'
|
|
49
52
|
Requires-Dist: pytest-asyncio>=0.24.0; extra == 'dev'
|
|
@@ -104,6 +107,16 @@ from langgraph.prebuilt import create_react_agent
|
|
|
104
107
|
| OpenAI Agents | `Framework.OPENAI_AGENTS` | `pip install cortexhub[openai-agents]` |
|
|
105
108
|
| Claude Agents | `Framework.CLAUDE_AGENTS` | `pip install cortexhub[claude-agents]` |
|
|
106
109
|
|
|
110
|
+
## Tracing Coverage
|
|
111
|
+
|
|
112
|
+
All frameworks emit `run.started` and `run.completed`/`run.failed` for each run.
|
|
113
|
+
Tool spans (`tool.invoke`) and model spans (`llm.call`) vary by SDK:
|
|
114
|
+
|
|
115
|
+
- **LangGraph**: tool calls via `BaseTool.invoke`, LLM calls via `BaseChatModel.invoke/ainvoke`
|
|
116
|
+
- **CrewAI**: tool calls via `CrewStructuredTool.invoke`/`BaseTool.run`, LLM calls via LiteLLM and `BaseLLM.call/acall`
|
|
117
|
+
- **OpenAI Agents**: tool calls via `function_tool`, LLM calls via `OpenAIResponsesModel` and `OpenAIChatCompletionsModel`
|
|
118
|
+
- **Claude Agents**: tool calls via `@tool` and built-in tool hooks; LLM calls run inside the Claude Code CLI and are not intercepted by the Python SDK
|
|
119
|
+
|
|
107
120
|
## Configuration
|
|
108
121
|
|
|
109
122
|
```bash
|
|
@@ -178,15 +191,6 @@ The SDK applies your configuration automatically:
|
|
|
178
191
|
# Only configured types are redacted
|
|
179
192
|
```
|
|
180
193
|
|
|
181
|
-
## Examples
|
|
182
|
-
|
|
183
|
-
Examples live in the separate `examples` repository
|
|
184
|
-
|
|
185
|
-
```bash
|
|
186
|
-
git clone git@github.com:cortexhub/examples.git
|
|
187
|
-
cd examples/langgraph # or crewai, openai-agents, claude-agents
|
|
188
|
-
```
|
|
189
|
-
|
|
190
194
|
## Important: Initialization Order
|
|
191
195
|
|
|
192
196
|
**Always initialize CortexHub FIRST**, before importing your framework:
|
|
@@ -44,6 +44,16 @@ from langgraph.prebuilt import create_react_agent
|
|
|
44
44
|
| OpenAI Agents | `Framework.OPENAI_AGENTS` | `pip install cortexhub[openai-agents]` |
|
|
45
45
|
| Claude Agents | `Framework.CLAUDE_AGENTS` | `pip install cortexhub[claude-agents]` |
|
|
46
46
|
|
|
47
|
+
## Tracing Coverage
|
|
48
|
+
|
|
49
|
+
All frameworks emit `run.started` and `run.completed`/`run.failed` for each run.
|
|
50
|
+
Tool spans (`tool.invoke`) and model spans (`llm.call`) vary by SDK:
|
|
51
|
+
|
|
52
|
+
- **LangGraph**: tool calls via `BaseTool.invoke`, LLM calls via `BaseChatModel.invoke/ainvoke`
|
|
53
|
+
- **CrewAI**: tool calls via `CrewStructuredTool.invoke`/`BaseTool.run`, LLM calls via LiteLLM and `BaseLLM.call/acall`
|
|
54
|
+
- **OpenAI Agents**: tool calls via `function_tool`, LLM calls via `OpenAIResponsesModel` and `OpenAIChatCompletionsModel`
|
|
55
|
+
- **Claude Agents**: tool calls via `@tool` and built-in tool hooks; LLM calls run inside the Claude Code CLI and are not intercepted by the Python SDK
|
|
56
|
+
|
|
47
57
|
## Configuration
|
|
48
58
|
|
|
49
59
|
```bash
|
|
@@ -118,15 +128,6 @@ The SDK applies your configuration automatically:
|
|
|
118
128
|
# Only configured types are redacted
|
|
119
129
|
```
|
|
120
130
|
|
|
121
|
-
## Examples
|
|
122
|
-
|
|
123
|
-
Examples live in the separate `examples` repository
|
|
124
|
-
|
|
125
|
-
```bash
|
|
126
|
-
git clone git@github.com:cortexhub/examples.git
|
|
127
|
-
cd examples/langgraph # or crewai, openai-agents, claude-agents
|
|
128
|
-
```
|
|
129
|
-
|
|
130
131
|
## Important: Initialization Order
|
|
131
132
|
|
|
132
133
|
**Always initialize CortexHub FIRST**, before importing your framework:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "cortexhub"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.4"
|
|
4
4
|
description = "CortexHub Python SDK - Policy-as-Code for AI Agents"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.10,<3.14"
|
|
@@ -55,6 +55,7 @@ langgraph = [
|
|
|
55
55
|
|
|
56
56
|
crewai = [
|
|
57
57
|
"crewai>=0.50.0",
|
|
58
|
+
"litellm>=1.81.5",
|
|
58
59
|
]
|
|
59
60
|
|
|
60
61
|
openai-agents = [
|
|
@@ -95,11 +96,13 @@ build-backend = "hatchling.build"
|
|
|
95
96
|
|
|
96
97
|
[tool.hatch.build.targets.wheel]
|
|
97
98
|
packages = ["src/cortexhub"]
|
|
99
|
+
include = ["LICENSE"]
|
|
98
100
|
|
|
99
101
|
[tool.hatch.build.targets.sdist]
|
|
100
102
|
include = [
|
|
101
103
|
"/src",
|
|
102
104
|
"/README.md",
|
|
105
|
+
"/LICENSE",
|
|
103
106
|
]
|
|
104
107
|
|
|
105
108
|
[tool.pytest.ini_options]
|
|
@@ -137,6 +140,7 @@ dev = [
|
|
|
137
140
|
"langchain-openai>=0.1.0",
|
|
138
141
|
"langgraph>=0.2.0",
|
|
139
142
|
"crewai>=0.50.0",
|
|
143
|
+
"litellm>=1.81.5",
|
|
140
144
|
"openai-agents>=0.0.3",
|
|
141
145
|
"anthropic>=0.40.0",
|
|
142
146
|
"claude-agent-sdk>=0.0.1",
|
|
@@ -19,11 +19,13 @@ Architectural rules:
|
|
|
19
19
|
- No governance logic in adapter
|
|
20
20
|
"""
|
|
21
21
|
|
|
22
|
+
import json
|
|
22
23
|
import os
|
|
23
24
|
from functools import wraps
|
|
24
25
|
from typing import TYPE_CHECKING, Any, Callable, Awaitable
|
|
25
26
|
|
|
26
27
|
import structlog
|
|
28
|
+
from opentelemetry.trace import SpanKind, Status, StatusCode
|
|
27
29
|
|
|
28
30
|
from cortexhub.adapters.base import ToolAdapter
|
|
29
31
|
from cortexhub.pipeline import govern_execution
|
|
@@ -36,6 +38,10 @@ logger = structlog.get_logger(__name__)
|
|
|
36
38
|
# Attribute names for storing originals
|
|
37
39
|
_ORIGINAL_TOOL_ATTR = "__cortexhub_original_tool__"
|
|
38
40
|
_PATCHED_ATTR = "__cortexhub_patched__"
|
|
41
|
+
_ORIGINAL_QUERY_ATTR = "__cortexhub_original_query__"
|
|
42
|
+
_ORIGINAL_RECEIVE_RESPONSE_ATTR = "__cortexhub_original_receive_response__"
|
|
43
|
+
_ORIGINAL_CLIENT_QUERY_ATTR = "__cortexhub_original_client_query__"
|
|
44
|
+
_PATCHED_RUN_ATTR = "__cortexhub_run_patched__"
|
|
39
45
|
|
|
40
46
|
|
|
41
47
|
class ClaudeAgentsAdapter(ToolAdapter):
|
|
@@ -64,6 +70,10 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
64
70
|
def framework_name(self) -> str:
|
|
65
71
|
return "claude_agents"
|
|
66
72
|
|
|
73
|
+
def __init__(self, cortex_hub: Any):
|
|
74
|
+
super().__init__(cortex_hub)
|
|
75
|
+
self._hook_spans: dict[str, Any] = {}
|
|
76
|
+
|
|
67
77
|
def _get_framework_modules(self) -> list[str]:
|
|
68
78
|
return ["claude_agent_sdk"]
|
|
69
79
|
|
|
@@ -145,6 +155,8 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
145
155
|
setattr(claude_agent_sdk, _PATCHED_ATTR, True)
|
|
146
156
|
|
|
147
157
|
logger.info("Claude Agent SDK @tool decorator patched successfully")
|
|
158
|
+
|
|
159
|
+
self._patch_run_completion(cortex_hub)
|
|
148
160
|
|
|
149
161
|
except ImportError:
|
|
150
162
|
logger.debug("Claude Agent SDK not installed, skipping adapter")
|
|
@@ -161,6 +173,23 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
161
173
|
claude_agent_sdk.tool = original
|
|
162
174
|
setattr(claude_agent_sdk, _PATCHED_ATTR, False)
|
|
163
175
|
logger.info("Claude Agent SDK adapter unpatched")
|
|
176
|
+
|
|
177
|
+
try:
|
|
178
|
+
from claude_agent_sdk import ClaudeSDKClient
|
|
179
|
+
|
|
180
|
+
if hasattr(claude_agent_sdk, _ORIGINAL_QUERY_ATTR):
|
|
181
|
+
claude_agent_sdk.query = getattr(claude_agent_sdk, _ORIGINAL_QUERY_ATTR)
|
|
182
|
+
if hasattr(ClaudeSDKClient, _ORIGINAL_CLIENT_QUERY_ATTR):
|
|
183
|
+
ClaudeSDKClient.query = getattr(
|
|
184
|
+
ClaudeSDKClient, _ORIGINAL_CLIENT_QUERY_ATTR
|
|
185
|
+
)
|
|
186
|
+
if hasattr(ClaudeSDKClient, _ORIGINAL_RECEIVE_RESPONSE_ATTR):
|
|
187
|
+
ClaudeSDKClient.receive_response = getattr(
|
|
188
|
+
ClaudeSDKClient, _ORIGINAL_RECEIVE_RESPONSE_ATTR
|
|
189
|
+
)
|
|
190
|
+
setattr(claude_agent_sdk, _PATCHED_RUN_ATTR, False)
|
|
191
|
+
except ImportError:
|
|
192
|
+
pass
|
|
164
193
|
except ImportError:
|
|
165
194
|
pass
|
|
166
195
|
|
|
@@ -171,6 +200,88 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
171
200
|
def _discover_tools(self) -> list[dict[str, Any]]:
|
|
172
201
|
"""Discover tools from Claude Agent SDK (best-effort)."""
|
|
173
202
|
return []
|
|
203
|
+
|
|
204
|
+
def _patch_run_completion(self, cortex_hub) -> None:
|
|
205
|
+
"""Patch Claude Agent SDK runs to emit run completion."""
|
|
206
|
+
try:
|
|
207
|
+
import claude_agent_sdk
|
|
208
|
+
from claude_agent_sdk import ClaudeSDKClient, ResultMessage
|
|
209
|
+
|
|
210
|
+
if getattr(claude_agent_sdk, _PATCHED_RUN_ATTR, False):
|
|
211
|
+
return
|
|
212
|
+
|
|
213
|
+
if not hasattr(claude_agent_sdk, _ORIGINAL_QUERY_ATTR):
|
|
214
|
+
setattr(claude_agent_sdk, _ORIGINAL_QUERY_ATTR, claude_agent_sdk.query)
|
|
215
|
+
original_query = getattr(claude_agent_sdk, _ORIGINAL_QUERY_ATTR)
|
|
216
|
+
|
|
217
|
+
async def patched_query(*args, **kwargs):
|
|
218
|
+
status = None
|
|
219
|
+
failed = False
|
|
220
|
+
cortex_hub.start_run(framework="claude_agents")
|
|
221
|
+
try:
|
|
222
|
+
async for message in original_query(*args, **kwargs):
|
|
223
|
+
if isinstance(message, ResultMessage):
|
|
224
|
+
status = "failed" if message.is_error else "completed"
|
|
225
|
+
yield message
|
|
226
|
+
except Exception:
|
|
227
|
+
failed = True
|
|
228
|
+
raise
|
|
229
|
+
finally:
|
|
230
|
+
if status is None and failed:
|
|
231
|
+
status = "failed"
|
|
232
|
+
if status:
|
|
233
|
+
cortex_hub.finish_run(framework="claude_agents", status=status)
|
|
234
|
+
|
|
235
|
+
claude_agent_sdk.query = patched_query
|
|
236
|
+
|
|
237
|
+
if not hasattr(ClaudeSDKClient, _ORIGINAL_RECEIVE_RESPONSE_ATTR):
|
|
238
|
+
setattr(
|
|
239
|
+
ClaudeSDKClient,
|
|
240
|
+
_ORIGINAL_RECEIVE_RESPONSE_ATTR,
|
|
241
|
+
ClaudeSDKClient.receive_response,
|
|
242
|
+
)
|
|
243
|
+
original_receive_response = getattr(ClaudeSDKClient, _ORIGINAL_RECEIVE_RESPONSE_ATTR)
|
|
244
|
+
if not hasattr(ClaudeSDKClient, _ORIGINAL_CLIENT_QUERY_ATTR):
|
|
245
|
+
setattr(
|
|
246
|
+
ClaudeSDKClient,
|
|
247
|
+
_ORIGINAL_CLIENT_QUERY_ATTR,
|
|
248
|
+
ClaudeSDKClient.query,
|
|
249
|
+
)
|
|
250
|
+
original_client_query = getattr(ClaudeSDKClient, _ORIGINAL_CLIENT_QUERY_ATTR)
|
|
251
|
+
|
|
252
|
+
async def patched_client_query(self, *args, **kwargs):
|
|
253
|
+
cortex_hub.start_run(framework="claude_agents")
|
|
254
|
+
try:
|
|
255
|
+
return await original_client_query(self, *args, **kwargs)
|
|
256
|
+
except Exception:
|
|
257
|
+
cortex_hub.finish_run(framework="claude_agents", status="failed")
|
|
258
|
+
raise
|
|
259
|
+
|
|
260
|
+
async def patched_receive_response(self, *args, **kwargs):
|
|
261
|
+
status = None
|
|
262
|
+
failed = False
|
|
263
|
+
try:
|
|
264
|
+
async for message in original_receive_response(self, *args, **kwargs):
|
|
265
|
+
if isinstance(message, ResultMessage):
|
|
266
|
+
status = "failed" if message.is_error else "completed"
|
|
267
|
+
yield message
|
|
268
|
+
except Exception:
|
|
269
|
+
failed = True
|
|
270
|
+
raise
|
|
271
|
+
finally:
|
|
272
|
+
if status is None and failed:
|
|
273
|
+
status = "failed"
|
|
274
|
+
if status:
|
|
275
|
+
cortex_hub.finish_run(framework="claude_agents", status=status)
|
|
276
|
+
|
|
277
|
+
ClaudeSDKClient.query = patched_client_query
|
|
278
|
+
ClaudeSDKClient.receive_response = patched_receive_response
|
|
279
|
+
setattr(claude_agent_sdk, _PATCHED_RUN_ATTR, True)
|
|
280
|
+
logger.info("Claude Agent SDK run completion patched successfully")
|
|
281
|
+
except ImportError:
|
|
282
|
+
logger.debug("Claude Agent SDK run completion patch skipped")
|
|
283
|
+
except Exception as e:
|
|
284
|
+
logger.debug("Claude Agent SDK run completion patch failed", reason=str(e))
|
|
174
285
|
|
|
175
286
|
def create_governance_hooks(self) -> dict[str, list]:
|
|
176
287
|
"""Create CortexHub governance hooks for Claude Agent SDK.
|
|
@@ -191,6 +302,61 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
191
302
|
)
|
|
192
303
|
"""
|
|
193
304
|
cortex_hub = self.cortex_hub
|
|
305
|
+
span_store = self._hook_spans
|
|
306
|
+
|
|
307
|
+
def _start_tool_span(
|
|
308
|
+
*,
|
|
309
|
+
tool_name: str,
|
|
310
|
+
tool_description: str,
|
|
311
|
+
policy_args: dict[str, Any],
|
|
312
|
+
raw_args: dict[str, Any],
|
|
313
|
+
tool_use_id: str | None,
|
|
314
|
+
):
|
|
315
|
+
span = cortex_hub._tracer.start_span(
|
|
316
|
+
name="tool.invoke",
|
|
317
|
+
kind=SpanKind.INTERNAL,
|
|
318
|
+
)
|
|
319
|
+
span.set_attribute("cortexhub.session.id", cortex_hub.session_id)
|
|
320
|
+
span.set_attribute("cortexhub.agent.id", cortex_hub.agent_id)
|
|
321
|
+
span.set_attribute("cortexhub.tool.name", tool_name)
|
|
322
|
+
span.set_attribute("cortexhub.tool.framework", "claude_agents")
|
|
323
|
+
span.set_attribute("cortexhub.tool.description", tool_description)
|
|
324
|
+
|
|
325
|
+
if tool_use_id:
|
|
326
|
+
span.set_attribute("cortexhub.tool.use_id", tool_use_id)
|
|
327
|
+
|
|
328
|
+
if policy_args:
|
|
329
|
+
arg_names = list(policy_args.keys())
|
|
330
|
+
if arg_names:
|
|
331
|
+
span.set_attribute("cortexhub.tool.arg_names", arg_names)
|
|
332
|
+
arg_schema = cortex_hub._infer_arg_schema(policy_args)
|
|
333
|
+
if arg_schema:
|
|
334
|
+
span.set_attribute(
|
|
335
|
+
"cortexhub.tool.arg_schema",
|
|
336
|
+
json.dumps(arg_schema),
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
if not cortex_hub.privacy and raw_args:
|
|
340
|
+
span.set_attribute("cortexhub.raw.args", json.dumps(raw_args, default=str))
|
|
341
|
+
|
|
342
|
+
return span
|
|
343
|
+
|
|
344
|
+
def _finish_tool_span(
|
|
345
|
+
span,
|
|
346
|
+
*,
|
|
347
|
+
success: bool,
|
|
348
|
+
error_message: str | None = None,
|
|
349
|
+
result: Any | None = None,
|
|
350
|
+
) -> None:
|
|
351
|
+
span.set_attribute("cortexhub.result.success", success)
|
|
352
|
+
if error_message:
|
|
353
|
+
span.set_attribute("cortexhub.error.message", error_message)
|
|
354
|
+
span.set_status(Status(StatusCode.ERROR, error_message))
|
|
355
|
+
else:
|
|
356
|
+
span.set_status(Status(StatusCode.OK))
|
|
357
|
+
if result is not None and not cortex_hub.privacy:
|
|
358
|
+
span.set_attribute("cortexhub.raw.result", json.dumps(result, default=str))
|
|
359
|
+
span.end()
|
|
194
360
|
|
|
195
361
|
async def pre_tool_governance(
|
|
196
362
|
input_data: dict[str, Any],
|
|
@@ -204,12 +370,18 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
204
370
|
"""
|
|
205
371
|
tool_name = input_data.get("tool_name", "unknown")
|
|
206
372
|
tool_input = input_data.get("tool_input", {})
|
|
373
|
+
if not isinstance(tool_input, dict):
|
|
374
|
+
tool_input = {"_raw": tool_input}
|
|
375
|
+
policy_args = cortex_hub._sanitize_policy_args(tool_input)
|
|
207
376
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
377
|
+
tool_description = f"Claude Agent SDK built-in tool: {tool_name}"
|
|
378
|
+
span = _start_tool_span(
|
|
379
|
+
tool_name=tool_name,
|
|
380
|
+
tool_description=tool_description,
|
|
381
|
+
policy_args=policy_args,
|
|
382
|
+
raw_args=tool_input,
|
|
383
|
+
tool_use_id=tool_use_id,
|
|
384
|
+
)
|
|
213
385
|
|
|
214
386
|
# Build authorization request and evaluate
|
|
215
387
|
from cortexhub.policy.models import (
|
|
@@ -222,7 +394,7 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
222
394
|
principal=Principal(type="Agent", id=cortex_hub.agent_id),
|
|
223
395
|
action=Action(type="tool.invoke", name=tool_name),
|
|
224
396
|
resource=PolicyResource(type="Tool", id=tool_name),
|
|
225
|
-
args=
|
|
397
|
+
args=policy_args,
|
|
226
398
|
framework="claude_agents",
|
|
227
399
|
)
|
|
228
400
|
|
|
@@ -230,8 +402,23 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
230
402
|
if cortex_hub.enforce and cortex_hub.evaluator:
|
|
231
403
|
from cortexhub.policy.effects import Effect
|
|
232
404
|
decision = cortex_hub.evaluator.evaluate(request)
|
|
405
|
+
|
|
406
|
+
span.add_event(
|
|
407
|
+
"policy.decision",
|
|
408
|
+
attributes={
|
|
409
|
+
"decision.effect": decision.effect.value,
|
|
410
|
+
"decision.policy_id": decision.policy_id or "",
|
|
411
|
+
"decision.reasoning": decision.reasoning,
|
|
412
|
+
"decision.policy_name": decision.policy_name or "",
|
|
413
|
+
},
|
|
414
|
+
)
|
|
233
415
|
|
|
234
416
|
if decision.effect == Effect.DENY:
|
|
417
|
+
_finish_tool_span(
|
|
418
|
+
span,
|
|
419
|
+
success=False,
|
|
420
|
+
error_message=decision.reasoning,
|
|
421
|
+
)
|
|
235
422
|
return {
|
|
236
423
|
"hookSpecificOutput": {
|
|
237
424
|
"hookEventName": "PreToolUse",
|
|
@@ -242,7 +429,7 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
242
429
|
|
|
243
430
|
if decision.effect == Effect.ESCALATE:
|
|
244
431
|
try:
|
|
245
|
-
context_hash = cortex_hub._compute_context_hash(tool_name,
|
|
432
|
+
context_hash = cortex_hub._compute_context_hash(tool_name, policy_args)
|
|
246
433
|
approval_response = cortex_hub.backend.create_approval(
|
|
247
434
|
run_id=cortex_hub.session_id,
|
|
248
435
|
trace_id=cortex_hub._get_current_trace_id(),
|
|
@@ -260,6 +447,11 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
260
447
|
approval_id = approval_response.get("approval_id", "unknown")
|
|
261
448
|
except Exception as e:
|
|
262
449
|
logger.error("Failed to create approval", error=str(e))
|
|
450
|
+
_finish_tool_span(
|
|
451
|
+
span,
|
|
452
|
+
success=False,
|
|
453
|
+
error_message=str(e),
|
|
454
|
+
)
|
|
263
455
|
return {
|
|
264
456
|
"hookSpecificOutput": {
|
|
265
457
|
"hookEventName": "PreToolUse",
|
|
@@ -270,6 +462,20 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
270
462
|
}
|
|
271
463
|
}
|
|
272
464
|
|
|
465
|
+
span.add_event(
|
|
466
|
+
"approval.created",
|
|
467
|
+
attributes={
|
|
468
|
+
"approval_id": approval_id,
|
|
469
|
+
"tool_name": tool_name,
|
|
470
|
+
"policy_id": decision.policy_id or "",
|
|
471
|
+
"expires_at": approval_response.get("expires_at", ""),
|
|
472
|
+
},
|
|
473
|
+
)
|
|
474
|
+
_finish_tool_span(
|
|
475
|
+
span,
|
|
476
|
+
success=False,
|
|
477
|
+
error_message="Approval required",
|
|
478
|
+
)
|
|
273
479
|
return {
|
|
274
480
|
"hookSpecificOutput": {
|
|
275
481
|
"hookEventName": "PreToolUse",
|
|
@@ -281,6 +487,10 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
281
487
|
}
|
|
282
488
|
|
|
283
489
|
# Allow execution
|
|
490
|
+
if tool_use_id:
|
|
491
|
+
span_store[tool_use_id] = span
|
|
492
|
+
else:
|
|
493
|
+
span.end()
|
|
284
494
|
return {}
|
|
285
495
|
|
|
286
496
|
async def post_tool_governance(
|
|
@@ -294,6 +504,21 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
294
504
|
"""
|
|
295
505
|
tool_name = input_data.get("tool_name", "unknown")
|
|
296
506
|
tool_response = input_data.get("tool_response", {})
|
|
507
|
+
tool_input = input_data.get("tool_input", {})
|
|
508
|
+
if not isinstance(tool_input, dict):
|
|
509
|
+
tool_input = {"_raw": tool_input}
|
|
510
|
+
policy_args = cortex_hub._sanitize_policy_args(tool_input)
|
|
511
|
+
|
|
512
|
+
span = span_store.pop(tool_use_id, None) if tool_use_id else None
|
|
513
|
+
if span is None:
|
|
514
|
+
tool_description = f"Claude Agent SDK built-in tool: {tool_name}"
|
|
515
|
+
span = _start_tool_span(
|
|
516
|
+
tool_name=tool_name,
|
|
517
|
+
tool_description=tool_description,
|
|
518
|
+
policy_args=policy_args,
|
|
519
|
+
raw_args=tool_input,
|
|
520
|
+
tool_use_id=tool_use_id,
|
|
521
|
+
)
|
|
297
522
|
|
|
298
523
|
# Log the tool execution
|
|
299
524
|
logger.debug(
|
|
@@ -301,6 +526,8 @@ class ClaudeAgentsAdapter(ToolAdapter):
|
|
|
301
526
|
tool=tool_name,
|
|
302
527
|
framework="claude_agents",
|
|
303
528
|
)
|
|
529
|
+
|
|
530
|
+
_finish_tool_span(span, success=True, result=tool_response)
|
|
304
531
|
|
|
305
532
|
return {}
|
|
306
533
|
|