agentcat 2.0.1__tar.gz → 2.0.2__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 (150) hide show
  1. {agentcat-2.0.1 → agentcat-2.0.2}/PKG-INFO +20 -1
  2. {agentcat-2.0.1 → agentcat-2.0.2}/README.md +19 -0
  3. {agentcat-2.0.1 → agentcat-2.0.2}/pyproject.toml +1 -1
  4. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/__init__.py +3 -1
  5. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/event_queue.py +30 -4
  6. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/redaction.py +114 -2
  7. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/types.py +29 -0
  8. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_event_queue.py +165 -0
  9. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_redaction.py +307 -2
  10. {agentcat-2.0.1 → agentcat-2.0.2}/.gitignore +0 -0
  11. {agentcat-2.0.1 → agentcat-2.0.2}/CONTRIBUTING.md +0 -0
  12. {agentcat-2.0.1 → agentcat-2.0.2}/LICENSE +0 -0
  13. {agentcat-2.0.1 → agentcat-2.0.2}/docs/cats/bibi.png +0 -0
  14. {agentcat-2.0.1 → agentcat-2.0.2}/docs/cats/void.jpg +0 -0
  15. {agentcat-2.0.1 → agentcat-2.0.2}/docs/cats/zelda.jpg +0 -0
  16. {agentcat-2.0.1 → agentcat-2.0.2}/docs/static/architecture.png +0 -0
  17. {agentcat-2.0.1 → agentcat-2.0.2}/docs/static/og-image.png +0 -0
  18. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/__init__.py +0 -0
  19. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/adapters/__init__.py +0 -0
  20. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/adapters/_common.py +0 -0
  21. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/adapters/_inner_tap.py +0 -0
  22. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/adapters/community.py +0 -0
  23. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/adapters/lowlevel_v1.py +0 -0
  24. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/adapters/lowlevel_v2.py +0 -0
  25. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/callpath.py +0 -0
  26. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/client_identity.py +0 -0
  27. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/constants.py +0 -0
  28. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/detection.py +0 -0
  29. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/diagnostics.py +0 -0
  30. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/exceptions.py +0 -0
  31. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/exporters/__init__.py +0 -0
  32. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/exporters/datadog.py +0 -0
  33. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/exporters/otlp.py +0 -0
  34. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/exporters/sentry.py +0 -0
  35. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/exporters/trace_context.py +0 -0
  36. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/handles.py +0 -0
  37. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/hooks.py +0 -0
  38. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/identify.py +0 -0
  39. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/injection.py +0 -0
  40. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/internal.py +0 -0
  41. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/logging.py +0 -0
  42. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/request_extra.py +0 -0
  43. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/sanitization.py +0 -0
  44. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/telemetry.py +0 -0
  45. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/tools.py +0 -0
  46. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/truncation.py +0 -0
  47. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/modules/validation.py +0 -0
  48. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/thirdparty/__init__.py +0 -0
  49. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/thirdparty/ksuid/__init__.py +0 -0
  50. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/thirdparty/ksuid/baseconv.py +0 -0
  51. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/thirdparty/ksuid/ksuid.py +0 -0
  52. {agentcat-2.0.1 → agentcat-2.0.2}/src/agentcat/utils.py +0 -0
  53. {agentcat-2.0.1 → agentcat-2.0.2}/tests/__init__.py +0 -0
  54. {agentcat-2.0.1 → agentcat-2.0.2}/tests/community/__init__.py +0 -0
  55. {agentcat-2.0.1 → agentcat-2.0.2}/tests/community/test_community_v3_event_serialization.py +0 -0
  56. {agentcat-2.0.1 → agentcat-2.0.2}/tests/community/test_community_v3_handles.py +0 -0
  57. {agentcat-2.0.1 → agentcat-2.0.2}/tests/community/test_community_v3_inject_context.py +0 -0
  58. {agentcat-2.0.1 → agentcat-2.0.2}/tests/community/test_community_v3_nested_calls.py +0 -0
  59. {agentcat-2.0.1 → agentcat-2.0.2}/tests/community/test_community_v3_openapi.py +0 -0
  60. {agentcat-2.0.1 → agentcat-2.0.2}/tests/community/test_community_v3_runtime_state_tools.py +0 -0
  61. {agentcat-2.0.1 → agentcat-2.0.2}/tests/conftest.py +0 -0
  62. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/__init__.py +0 -0
  63. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/_helpers.py +0 -0
  64. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/community_v3/__init__.py +0 -0
  65. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/community_v3/conftest.py +0 -0
  66. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/community_v3/test_agent_handle_http.py +0 -0
  67. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/community_v3/test_event_capture_http.py +0 -0
  68. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/community_v3/test_identify_http.py +0 -0
  69. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/community_v3/test_request_extra_http.py +0 -0
  70. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/community_v3/test_stateless_http.py +0 -0
  71. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/community_v4/__init__.py +0 -0
  72. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/community_v4/conftest.py +0 -0
  73. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/community_v4/test_agent_handle_http.py +0 -0
  74. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/community_v4/test_identify_http.py +0 -0
  75. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/community_v4/test_session_http.py +0 -0
  76. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/community_v4/test_stateless_http.py +0 -0
  77. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/conftest.py +0 -0
  78. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official/__init__.py +0 -0
  79. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official/conftest.py +0 -0
  80. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official/test_agent_handle_http.py +0 -0
  81. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official/test_event_capture_http.py +0 -0
  82. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official/test_identify_http.py +0 -0
  83. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official/test_redaction_http.py +0 -0
  84. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official/test_request_extra_http.py +0 -0
  85. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official/test_session_http.py +0 -0
  86. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official/test_stateless_http.py +0 -0
  87. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official_modern/__init__.py +0 -0
  88. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official_modern/conftest.py +0 -0
  89. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official_modern/test_agent_handle_http.py +0 -0
  90. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official_modern/test_identify_http.py +0 -0
  91. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official_modern/test_mcpserver_http.py +0 -0
  92. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official_modern/test_session_http.py +0 -0
  93. {agentcat-2.0.1 → agentcat-2.0.2}/tests/e2e/official_modern/test_stateless_http.py +0 -0
  94. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_api_base_url.py +0 -0
  95. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_async_hooks.py +0 -0
  96. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_callpath.py +0 -0
  97. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_client_identity.py +0 -0
  98. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_community_v4_handles.py +0 -0
  99. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_community_v4_nested_calls.py +0 -0
  100. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_concurrency_handles.py +0 -0
  101. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_constants_copy.py +0 -0
  102. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_customer_owned_parameters.py +0 -0
  103. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_detection.py +0 -0
  104. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_diagnostics_attributes.py +0 -0
  105. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_diagnostics_auth.py +0 -0
  106. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_diagnostics_export.py +0 -0
  107. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_diagnostics_integration.py +0 -0
  108. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_diagnostics_no_payload.py +0 -0
  109. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_diagnostics_optout.py +0 -0
  110. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_diagnostics_record.py +0 -0
  111. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_diagnostics_sink.py +0 -0
  112. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_diagnostics_test_env.py +0 -0
  113. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_dynamic_tracking.py +0 -0
  114. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_event_capture_completeness.py +0 -0
  115. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_event_tags_properties.py +0 -0
  116. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_exceptions.py +0 -0
  117. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_exporter_sdk_version.py +0 -0
  118. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_handles.py +0 -0
  119. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_hook_offload.py +0 -0
  120. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_identify_shape.py +0 -0
  121. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_injection.py +0 -0
  122. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_inner_tap.py +0 -0
  123. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_logging.py +0 -0
  124. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_lowlevel_v1_handles.py +0 -0
  125. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_lowlevel_v2_handles.py +0 -0
  126. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_multiple_servers.py +0 -0
  127. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_options_v2.py +0 -0
  128. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_process_safety.py +0 -0
  129. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_publish_custom_event.py +0 -0
  130. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_rebuild_on_demand.py +0 -0
  131. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_removed_events.py +0 -0
  132. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_report_missing.py +0 -0
  133. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_request_extra.py +0 -0
  134. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_response_shape.py +0 -0
  135. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_sanitization.py +0 -0
  136. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_session_id_validation.py +0 -0
  137. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_tag_validation.py +0 -0
  138. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_tool_context.py +0 -0
  139. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_truncation.py +0 -0
  140. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_truncation_json_safe.py +0 -0
  141. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_utils/__init__.py +0 -0
  142. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_utils/client.py +0 -0
  143. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_utils/community_catalog_server.py +0 -0
  144. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_utils/community_client.py +0 -0
  145. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_utils/community_openapi_server.py +0 -0
  146. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_utils/community_todo_server.py +0 -0
  147. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_utils/delivery.py +0 -0
  148. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_utils/flavors.py +0 -0
  149. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_utils/modern_server.py +0 -0
  150. {agentcat-2.0.1 → agentcat-2.0.2}/tests/test_utils/todo_server.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: agentcat
3
- Version: 2.0.1
3
+ Version: 2.0.2
4
4
  Summary: Analytics tool for MCP (Model Context Protocol) servers, Claude Connectors, and ChatGPT Plugins - tracks tool usage patterns and provides insights
5
5
  Project-URL: Homepage, https://github.com/agentcathq/agentcat-python-sdk
6
6
  Project-URL: Bug Tracker, https://github.com/agentcathq/agentcat-python-sdk/issues
@@ -175,6 +175,25 @@ async def redact(text: str) -> str:
175
175
  agentcat.track(server, "proj_0000000", AgentCatOptions(redact_sensitive_information=redact))
176
176
  ```
177
177
 
178
+ For redaction decisions that need more context than a single string — such as which tool was called or what type of event is being published — use the event-level `redact_event` hook. It receives the full event and returns a modified event, or `None` to drop the event entirely. It may be sync or async, runs before `redact_sensitive_information` (so it sees raw values), and can be combined with it.
179
+
180
+ Mutate the event you're given and return it, as in the example below — don't construct or return a different `Event`. The return value replaces the event's fields wholesale, not as a diff against the original, so returning a partial or freshly-built event will silently drop every field you didn't set.
181
+
182
+ ```python
183
+ from agentcat import AgentCatOptions
184
+
185
+ def redact_event(event):
186
+ # Drop events from tools that handle secrets entirely
187
+ if event.resource_name == "get_credentials":
188
+ return None
189
+ # Strip response payloads from a specific tool
190
+ if event.resource_name == "export_report":
191
+ event.response = None
192
+ return event
193
+
194
+ agentcat.track(server, "proj_0000000", AgentCatOptions(redact_event=redact_event))
195
+ ```
196
+
178
197
  ### Vendor Support
179
198
 
180
199
  AgentCat seamlessly integrates with your existing observability stack, providing automatic logging and tracing without the tedious setup typically required. Export telemetry data to multiple platforms simultaneously:
@@ -143,6 +143,25 @@ async def redact(text: str) -> str:
143
143
  agentcat.track(server, "proj_0000000", AgentCatOptions(redact_sensitive_information=redact))
144
144
  ```
145
145
 
146
+ For redaction decisions that need more context than a single string — such as which tool was called or what type of event is being published — use the event-level `redact_event` hook. It receives the full event and returns a modified event, or `None` to drop the event entirely. It may be sync or async, runs before `redact_sensitive_information` (so it sees raw values), and can be combined with it.
147
+
148
+ Mutate the event you're given and return it, as in the example below — don't construct or return a different `Event`. The return value replaces the event's fields wholesale, not as a diff against the original, so returning a partial or freshly-built event will silently drop every field you didn't set.
149
+
150
+ ```python
151
+ from agentcat import AgentCatOptions
152
+
153
+ def redact_event(event):
154
+ # Drop events from tools that handle secrets entirely
155
+ if event.resource_name == "get_credentials":
156
+ return None
157
+ # Strip response payloads from a specific tool
158
+ if event.resource_name == "export_report":
159
+ event.response = None
160
+ return event
161
+
162
+ agentcat.track(server, "proj_0000000", AgentCatOptions(redact_event=redact_event))
163
+ ```
164
+
146
165
  ### Vendor Support
147
166
 
148
167
  AgentCat seamlessly integrates with your existing observability stack, providing automatic logging and tracing without the tedious setup typically required. Export telemetry data to multiple platforms simultaneously:
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "agentcat"
3
- version = "2.0.1"
3
+ version = "2.0.2"
4
4
  description = "Analytics tool for MCP (Model Context Protocol) servers, Claude Connectors, and ChatGPT Plugins - tracks tool usage patterns and provides insights"
5
5
  authors = [
6
6
  { name = "AgentCat, Inc.", email = "support@agentcat.com" },
@@ -25,6 +25,7 @@ from .types import (
25
25
  EventPropertiesFunction,
26
26
  EventTagsFunction,
27
27
  IdentifyFunction,
28
+ RedactEventFunction,
28
29
  RedactionFunction,
29
30
  ResolveSessionIdFunction,
30
31
  UnredactedEvent,
@@ -449,8 +450,9 @@ __all__ = [
449
450
  # Types for identify functionality
450
451
  "UserIdentity",
451
452
  "IdentifyFunction",
452
- # Type for redaction functionality
453
+ # Types for redaction functionality
453
454
  "RedactionFunction",
455
+ "RedactEventFunction",
454
456
  # Types for event metadata callbacks
455
457
  "EventTagsFunction",
456
458
  "EventPropertiesFunction",
@@ -26,7 +26,7 @@ from ..types import Event, UnredactedEvent
26
26
  from ..utils import generate_prefixed_ksuid, get_agentcat_version
27
27
  from .internal import get_server_tracking_data
28
28
  from .logging import write_to_log
29
- from .redaction import redact_event
29
+ from .redaction import apply_event_redaction, redact_event
30
30
  from .sanitization import sanitize_event
31
31
  from .truncation import truncate_event
32
32
 
@@ -47,6 +47,12 @@ class _Stop:
47
47
  _STOP = _Stop()
48
48
 
49
49
 
50
+ def _ensure_event_id(event: UnredactedEvent) -> None:
51
+ """Assign a ksuid if the event doesn't already have one."""
52
+ if not event.id:
53
+ event.id = generate_prefixed_ksuid(EVENT_ID_PREFIX)
54
+
55
+
50
56
  class EventQueue:
51
57
  """Manages event queue and sending to AgentCat API."""
52
58
 
@@ -155,11 +161,30 @@ class EventQueue:
155
161
 
156
162
  def _process_event(self, event: UnredactedEvent) -> None:
157
163
  """Process a single event."""
164
+ if event and event.event_redaction_fn:
165
+ # Event-level redaction hook runs first, on raw values, and may
166
+ # drop the event entirely.
167
+ try:
168
+ _ensure_event_id(event)
169
+ redacted_event = apply_event_redaction(event, event.event_redaction_fn)
170
+ if redacted_event is None:
171
+ write_to_log(f"Event {event.id} dropped by redact_event hook")
172
+ return
173
+ event = redacted_event
174
+ event.event_redaction_fn = None # Clear to avoid reprocessing
175
+ except (Exception, SystemExit) as error:
176
+ # SystemExit included: a customer hook calling sys.exit() must
177
+ # not kill the worker thread.
178
+ write_to_log(
179
+ f"WARNING: Dropping event {event.id or 'unknown'} due to "
180
+ f"event redaction failure: {error}"
181
+ )
182
+ return # Skip this event if event redaction fails
183
+
158
184
  if event and event.redaction_fn:
159
185
  # Redact sensitive information if a redaction function is provided
160
186
  try:
161
- if not event.id:
162
- event.id = generate_prefixed_ksuid(EVENT_ID_PREFIX)
187
+ _ensure_event_id(event)
163
188
  redacted_event = redact_event(event, event.redaction_fn)
164
189
  # The redacted event is already the full event object, not a dict
165
190
  event = redacted_event
@@ -186,7 +211,7 @@ class EventQueue:
186
211
  )
187
212
 
188
213
  if event:
189
- event.id = event.id or generate_prefixed_ksuid("evt")
214
+ _ensure_event_id(event)
190
215
 
191
216
  # Send to AgentCat API only if project_id exists
192
217
  if event.project_id:
@@ -394,6 +419,7 @@ def publish_event(server: Any, event: UnredactedEvent) -> None:
394
419
  full_event = UnredactedEvent(
395
420
  **stamped,
396
421
  redaction_fn=data.options.redact_sensitive_information,
422
+ event_redaction_fn=data.options.redact_event,
397
423
  )
398
424
 
399
425
  event_queue.add(full_event)
@@ -1,11 +1,12 @@
1
1
  """PII redaction for AgentCat logs."""
2
2
 
3
+ import copy
3
4
  from typing import Any, TYPE_CHECKING, Callable, Set
4
5
 
5
6
  from agentcat.modules.hooks import drive_hook_result
6
7
 
7
8
  if TYPE_CHECKING:
8
- from agentcat.types import Event, UnredactedEvent
9
+ from agentcat.types import Event, RedactEventFunction, UnredactedEvent
9
10
 
10
11
 
11
12
  # Set of field names that should be protected from redaction.
@@ -16,6 +17,10 @@ PROTECTED_FIELDS: Set[str] = {
16
17
  "id",
17
18
  "project_id",
18
19
  "server",
20
+ "server_name",
21
+ "server_version",
22
+ "client_name",
23
+ "client_version",
19
24
  "identify_actor_given_id",
20
25
  "identify_actor_name",
21
26
  "identify_data",
@@ -146,8 +151,115 @@ def redact_event(event: "UnredactedEvent", redact_fn: Callable[[str], str]) -> "
146
151
  if not callable(dump):
147
152
  plain: Event = redact_strings_in_object(event, redact, "", False)
148
153
  return plain
149
- dumped = dump(exclude={"redaction_fn"}, warnings=False)
154
+ dumped = dump(exclude={"redaction_fn", "event_redaction_fn"}, warnings=False)
150
155
  redacted: Event = event.model_copy(
151
156
  update=redact_strings_in_object(dumped, redact, "", False)
152
157
  )
153
158
  return redacted
159
+
160
+
161
+ def _sync_event_redactor(
162
+ redact_event_fn: "RedactEventFunction",
163
+ ) -> Callable[["Event"], "Event | None"]:
164
+ """A synchronous view of the customer's event-level redaction hook.
165
+
166
+ Same rationale as `_sync_redactor`: the publish worker is a thread with no
167
+ event loop of its own, so an async hook's result has to be driven to
168
+ completion here rather than assigned straight into the pipeline.
169
+ """
170
+
171
+ def run(event: "Event") -> "Event | None":
172
+ return drive_hook_result(redact_event_fn(event), "redact_event")
173
+
174
+ return run
175
+
176
+
177
+ # Fields restored from the original event after the event-level redaction
178
+ # hook runs, regardless of what the hook returns. These are system-managed —
179
+ # not consumer-settable. Broader than the equivalent RESTORED_FIELDS in the
180
+ # TypeScript SDK and the snapshot/restore around ApplyEventRedaction in the Go
181
+ # SDK, which cover only id/session_id/project_id/event_type/timestamp and so
182
+ # still let a hook forge or erase actor identity, client/server identity,
183
+ # tags, and properties.
184
+ RESTORED_FIELDS: Set[str] = {
185
+ "id",
186
+ "session_id",
187
+ "project_id",
188
+ "event_type",
189
+ "timestamp",
190
+ "actor_id",
191
+ "identify_actor_given_id",
192
+ "identify_actor_name",
193
+ "identify_data",
194
+ "tags",
195
+ "properties",
196
+ "client_name",
197
+ "client_version",
198
+ "server_name",
199
+ "server_version",
200
+ }
201
+
202
+
203
+ def apply_event_redaction(
204
+ event: "UnredactedEvent",
205
+ redact_event_fn: "RedactEventFunction",
206
+ ) -> "UnredactedEvent | None":
207
+ """
208
+ Applies the customer's event-level redaction hook to an event.
209
+
210
+ Runs before `redact_event` (the string-level hook), so the hook receives
211
+ raw, unredacted values. The hook may return a modified event, or None to
212
+ drop the event entirely. System-managed fields (RESTORED_FIELDS) are
213
+ force-restored from the original event afterward, so a hook cannot forge
214
+ or erase what AgentCat itself assigned.
215
+
216
+ `RedactEventFunction` (see types.py) declares the hook returns a pydantic
217
+ `Event | None` — mutate and return the `Event` you were handed, not a
218
+ different object. That contract is enforced by type hints only, not at
219
+ runtime: `redact_event_fn` is called and its result is trusted to be
220
+ `Event`-shaped, so a hook that ignores its type hints and returns
221
+ something else (e.g. a plain dict) raises `AttributeError` here rather
222
+ than degrading gracefully. Type-check hooks against `RedactEventFunction`
223
+ (mypy/pyright) to catch this before it ships.
224
+
225
+ The return value is also applied as a wholesale replacement of the
226
+ event's fields (`result.model_dump()`, not a diff against the original),
227
+ so a freshly-built or partial `Event` — even though it satisfies the type
228
+ hint — will silently null out every field the hook didn't set.
229
+
230
+ The hook is handed a plain `Event` built from the dump, excluding
231
+ `redaction_fn`/`event_redaction_fn` — they are machinery, not event data,
232
+ and the customer's hook must not be handed its own function objects.
233
+
234
+ Args:
235
+ event: The event to run the hook on
236
+ redact_event_fn: The customer's event-level redaction hook
237
+
238
+ Returns:
239
+ The (possibly modified) event to keep processing, or None if the hook
240
+ dropped it.
241
+ """
242
+ from agentcat.types import Event as EventModel
243
+
244
+ run = _sync_event_redactor(redact_event_fn)
245
+ dumped = event.model_dump(
246
+ exclude={"redaction_fn", "event_redaction_fn"}, warnings=False
247
+ )
248
+ hook_input = EventModel(**dumped)
249
+
250
+ result = run(hook_input)
251
+
252
+ if result is None:
253
+ return None
254
+
255
+ # Deep-copied, not aliased: tags/properties/identify_data are mutable, and
256
+ # a customer callback commonly hands back the same cached dict across
257
+ # events — aliasing here would let mutating one event's dict silently
258
+ # corrupt another event that shares the reference.
259
+ restored = {
260
+ field: copy.deepcopy(getattr(event, field)) for field in RESTORED_FIELDS
261
+ }
262
+ updated = result.model_dump(warnings=False)
263
+ updated.update(restored)
264
+ redacted: UnredactedEvent = event.model_copy(update=updated)
265
+ return redacted
@@ -38,6 +38,21 @@ IdentifyFunction = Callable[
38
38
  ]
39
39
  # Type alias for redaction function
40
40
  RedactionFunction = Callable[[str], str | Awaitable[str]]
41
+ # Type alias for the event-level redaction hook. Receives the full event
42
+ # (raw, unredacted values — it runs before RedactionFunction) and MUST return
43
+ # a pydantic Event or None to drop the event entirely — never a dict or other
44
+ # object, even though one might satisfy a looser hand-written signature. This
45
+ # is enforced by type checking only: type-check hooks against
46
+ # RedactEventFunction (mypy/pyright), since a hook that returns something
47
+ # else at runtime fails inside apply_event_redaction rather than degrading
48
+ # gracefully. Accepts sync or async callables (mirrors RedactionFunction).
49
+ # Mutate and return the event you're handed — the return value replaces the
50
+ # event's fields wholesale, not a diff, so a freshly-built or partial event
51
+ # silently loses every field it didn't set. See the redact_event option on
52
+ # AgentCatOptions for the full contract.
53
+ RedactEventFunction = Callable[
54
+ ["Event"], Optional["Event"] | Awaitable[Optional["Event"]]
55
+ ]
41
56
  # Type alias for event_tags callback — returns str:str map attached to every auto-captured event.
42
57
  # Accepts sync or async callables (mirrors RedactionFunction).
43
58
  EventTagsFunction = Callable[
@@ -149,6 +164,7 @@ class EventType(str, Enum):
149
164
 
150
165
  class UnredactedEvent(Event):
151
166
  redaction_fn: RedactionFunction | None = None
167
+ event_redaction_fn: RedactEventFunction | None = None
152
168
 
153
169
 
154
170
  # Telemetry Exporter Configuration Types
@@ -202,6 +218,19 @@ class AgentCatOptions:
202
218
  # publishes anonymously rather than failing the tool call.
203
219
  identify: IdentifyFunction | None = None
204
220
  redact_sensitive_information: RedactionFunction | None = None
221
+ # Event-level redaction hook, invoked with the full event (raw, unredacted
222
+ # values) before redact_sensitive_information runs. May return a modified
223
+ # event, or None to drop the event entirely. May be sync or async. The
224
+ # system-managed fields id, session_id, project_id, event_type,
225
+ # timestamp, actor_id, identify_actor_given_id, identify_actor_name,
226
+ # identify_data, tags, and properties cannot be changed by this hook —
227
+ # they are restored from the original event afterward. If the hook
228
+ # raises, the event is dropped rather than published unredacted.
229
+ # Mutate and return the event you're given rather than constructing a new
230
+ # one: the return value replaces the event's fields wholesale (it is not
231
+ # diffed against the original), so a freshly-built or partial event
232
+ # silently drops everything you didn't set.
233
+ redact_event: RedactEventFunction | None = None
205
234
  exporters: dict[str, ExporterConfig] | None = None
206
235
  # Debug logging to ~/agentcat.log. Tri-state: None (the default) defers to
207
236
  # the AGENTCAT_DEBUG_MODE env var read at import; explicit True/False wins.
@@ -1,6 +1,7 @@
1
1
  """Test event queue functionality."""
2
2
 
3
3
  import queue
4
+ import sys
4
5
  import threading
5
6
  import time
6
7
  from datetime import datetime, timezone
@@ -274,6 +275,149 @@ class TestEventQueue:
274
275
  assert "test-id" in log_message
275
276
  mock_send.assert_not_called()
276
277
 
278
+ @patch("agentcat.modules.event_queue.apply_event_redaction")
279
+ def test_process_event_with_event_redaction(self, mock_apply):
280
+ """Test processing event with the event-level redaction hook."""
281
+ eq = EventQueue()
282
+ mock_event_redaction_fn = MagicMock()
283
+
284
+ redacted_event = UnredactedEvent(
285
+ id="test-id",
286
+ event_type="mcp:tools/call",
287
+ project_id="project-123",
288
+ session_id="session-123",
289
+ timestamp=datetime.now(timezone.utc),
290
+ resource_name="dropped-and-replaced",
291
+ # A real (unmocked) apply_event_redaction preserves the original
292
+ # hook on its returned copy — model_copy(update=...) never
293
+ # touches event_redaction_fn, since it's excluded from the dump
294
+ # and isn't in RESTORED_FIELDS. Mirroring that here means the
295
+ # `is None` assertion below actually exercises _process_event's
296
+ # own clearing line rather than just echoing the fixture.
297
+ event_redaction_fn=mock_event_redaction_fn,
298
+ )
299
+ mock_apply.return_value = redacted_event
300
+
301
+ event = UnredactedEvent(
302
+ id="test-id",
303
+ event_type="mcp:tools/call",
304
+ project_id="project-123",
305
+ session_id="session-123",
306
+ timestamp=datetime.now(timezone.utc),
307
+ resource_name="original",
308
+ event_redaction_fn=mock_event_redaction_fn,
309
+ )
310
+
311
+ with patch.object(eq, "_send_event") as mock_send:
312
+ eq._process_event(event)
313
+
314
+ mock_apply.assert_called_once_with(event, mock_event_redaction_fn)
315
+ called_event = mock_send.call_args[0][0]
316
+ assert called_event.resource_name == "dropped-and-replaced"
317
+ assert called_event.event_redaction_fn is None
318
+ mock_send.assert_called_once()
319
+
320
+ def test_process_event_event_redaction_drops_event(self):
321
+ """A hook returning None drops the event before it's ever sent."""
322
+ eq = EventQueue()
323
+
324
+ def drop_it(event):
325
+ return None
326
+
327
+ event = UnredactedEvent(
328
+ id="test-id",
329
+ event_type="mcp:tools/call",
330
+ project_id="project-123",
331
+ session_id="session-123",
332
+ timestamp=datetime.now(timezone.utc),
333
+ event_redaction_fn=drop_it,
334
+ )
335
+
336
+ with patch.object(eq, "_send_event") as mock_send:
337
+ eq._process_event(event)
338
+ mock_send.assert_not_called()
339
+
340
+ @patch("agentcat.modules.event_queue.apply_event_redaction")
341
+ @patch("agentcat.modules.event_queue.write_to_log")
342
+ def test_process_event_event_redaction_failure(self, mock_log, mock_apply):
343
+ """Test processing event when the event-level hook raises."""
344
+ eq = EventQueue()
345
+ mock_event_redaction_fn = MagicMock()
346
+ mock_apply.side_effect = Exception("Event redaction error")
347
+
348
+ event = UnredactedEvent(
349
+ id="test-id",
350
+ event_type="mcp:tools/call",
351
+ project_id="project-123",
352
+ session_id="session-123",
353
+ timestamp=datetime.now(timezone.utc),
354
+ event_redaction_fn=mock_event_redaction_fn,
355
+ )
356
+
357
+ with patch.object(eq, "_send_event") as mock_send:
358
+ eq._process_event(event)
359
+
360
+ assert mock_log.called
361
+ log_message = str(mock_log.call_args_list[0])
362
+ assert "WARNING" in log_message
363
+ assert "event redaction failure" in log_message
364
+ assert "test-id" in log_message
365
+ mock_send.assert_not_called()
366
+
367
+ def test_process_event_event_redaction_hook_sys_exit_does_not_kill_worker(self):
368
+ """A redact_event hook calling sys.exit() must not propagate out of
369
+ _process_event — SystemExit doesn't subclass Exception, so it needs
370
+ its own except clause, and _worker's own `except Exception` wrapper
371
+ would not have caught it either."""
372
+ eq = EventQueue()
373
+
374
+ def exits(event):
375
+ sys.exit(1)
376
+
377
+ event = UnredactedEvent(
378
+ id="test-id",
379
+ event_type="mcp:tools/call",
380
+ project_id="project-123",
381
+ session_id="session-123",
382
+ timestamp=datetime.now(timezone.utc),
383
+ event_redaction_fn=exits,
384
+ )
385
+
386
+ with patch.object(eq, "_send_event") as mock_send:
387
+ eq._process_event(event) # must not raise SystemExit
388
+ mock_send.assert_not_called()
389
+
390
+ def test_process_event_event_hook_runs_before_string_hook(self):
391
+ """Ordering: the event hook must see raw values, before the string
392
+ hook ever runs — nothing mocked, both hooks wired for real."""
393
+ eq = EventQueue()
394
+ order = []
395
+
396
+ def event_hook(event):
397
+ order.append(("event_hook", event.parameters))
398
+ return event
399
+
400
+ def string_hook(s):
401
+ order.append(("string_hook", s))
402
+ return "[REDACTED]"
403
+
404
+ event = UnredactedEvent(
405
+ id="test-id",
406
+ event_type="mcp:tools/call",
407
+ project_id="project-123",
408
+ session_id="session-123",
409
+ timestamp=datetime.now(timezone.utc),
410
+ parameters={"secret": "raw-value"},
411
+ event_redaction_fn=event_hook,
412
+ redaction_fn=string_hook,
413
+ )
414
+
415
+ with patch.object(eq, "_send_event"):
416
+ eq._process_event(event)
417
+
418
+ assert order[0] == ("event_hook", {"secret": "raw-value"})
419
+ assert order[1] == ("string_hook", "raw-value")
420
+
277
421
  @patch("agentcat.modules.event_queue.generate_prefixed_ksuid")
278
422
  def test_process_event_without_id(self, mock_ksuid):
279
423
  """Test processing event without ID generates one."""
@@ -825,6 +969,27 @@ class TestPublishEvent:
825
969
  added_event = mock_eq.add.call_args[0][0]
826
970
  assert added_event.redaction_fn == mock_redaction_fn
827
971
 
972
+ @patch("agentcat.modules.event_queue.get_server_tracking_data")
973
+ @patch("agentcat.modules.event_queue.event_queue")
974
+ def test_publish_event_with_event_redaction_function(self, mock_eq, mock_tracking):
975
+ """Test publishing event includes the event-level redaction hook."""
976
+ mock_server = MagicMock()
977
+ mock_event_redaction_fn = MagicMock()
978
+ mock_tracking.return_value = _tracking_data(
979
+ options=AgentCatOptions(redact_event=mock_event_redaction_fn)
980
+ )
981
+
982
+ event = UnredactedEvent(
983
+ event_type="mcp:tools/call",
984
+ session_id="ses_task_handle",
985
+ timestamp=datetime.now(timezone.utc),
986
+ )
987
+
988
+ publish_event(mock_server, event)
989
+
990
+ added_event = mock_eq.add.call_args[0][0]
991
+ assert added_event.event_redaction_fn == mock_event_redaction_fn
992
+
828
993
 
829
994
  def test_module_import_installs_no_process_hooks():
830
995
  """The SDK must never register signal handlers — the customer's process