aid-runtime-python 0.1.97__tar.gz → 0.1.98__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 (110) hide show
  1. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/PKG-INFO +1 -1
  2. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/pyproject.toml +1 -1
  3. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/__init__.py +1 -1
  4. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/agents/__init__.py +14 -0
  5. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/agents/guardrails.py +115 -49
  6. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/agents/ir.py +107 -1
  7. aid_runtime_python-0.1.98/src/aid_runtime/agents/managed.py +324 -0
  8. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/providers/bedrock.py +98 -48
  9. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime_python.egg-info/PKG-INFO +1 -1
  10. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime_python.egg-info/SOURCES.txt +2 -0
  11. aid_runtime_python-0.1.98/tests/test_guardrails_managed.py +401 -0
  12. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/README.md +0 -0
  13. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/setup.cfg +0 -0
  14. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/agents/chat_model.py +0 -0
  15. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/agents/deep_agents.py +0 -0
  16. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/agents/engine.py +0 -0
  17. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/agents/loop.py +0 -0
  18. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/agents/memory.py +0 -0
  19. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/agents/policy.py +0 -0
  20. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/agents/state.py +0 -0
  21. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/cache.py +0 -0
  22. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/concurrency.py +0 -0
  23. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/control.py +0 -0
  24. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/cost.py +0 -0
  25. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/decisions.py +0 -0
  26. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/errors.py +0 -0
  27. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/evals/__init__.py +0 -0
  28. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/evals/dataset.py +0 -0
  29. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/evals/gate.py +0 -0
  30. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/evals/metrics.py +0 -0
  31. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/evals/predicate.py +0 -0
  32. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/evals/runner.py +0 -0
  33. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/gateway.py +0 -0
  34. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/local_trace.py +0 -0
  35. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/pipeline.py +0 -0
  36. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/prompts.py +0 -0
  37. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/providers/__init__.py +0 -0
  38. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/providers/anthropic.py +0 -0
  39. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/providers/base.py +0 -0
  40. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/providers/fake.py +0 -0
  41. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/providers/gemini.py +0 -0
  42. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/providers/gemini_wire.py +0 -0
  43. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/providers/http.py +0 -0
  44. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/providers/vertex.py +0 -0
  45. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/py.typed +0 -0
  46. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/retrieval/__init__.py +0 -0
  47. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/retrieval/chunking.py +0 -0
  48. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/retrieval/citations.py +0 -0
  49. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/retrieval/connectors.py +0 -0
  50. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/retrieval/embedding.py +0 -0
  51. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/retrieval/gate.py +0 -0
  52. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/retrieval/hybrid.py +0 -0
  53. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/retrieval/metrics.py +0 -0
  54. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/retrieval/pgvector.py +0 -0
  55. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/retrieval/rerank.py +0 -0
  56. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/retrieval/retriever.py +0 -0
  57. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/retrieval/stores.py +0 -0
  58. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/retrieval/types.py +0 -0
  59. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/steps.py +0 -0
  60. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/structured.py +0 -0
  61. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/tools.py +0 -0
  62. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/tracing.py +0 -0
  63. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime/types.py +0 -0
  64. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime_python.egg-info/dependency_links.txt +0 -0
  65. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime_python.egg-info/requires.txt +0 -0
  66. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/src/aid_runtime_python.egg-info/top_level.txt +0 -0
  67. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_agents_budget.py +0 -0
  68. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_agents_deep_agents.py +0 -0
  69. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_agents_hitl.py +0 -0
  70. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_agents_loop.py +0 -0
  71. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_agents_memory.py +0 -0
  72. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_anthropic_provider.py +0 -0
  73. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_bedrock_provider.py +0 -0
  74. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_cache.py +0 -0
  75. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_concurrency.py +0 -0
  76. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_control.py +0 -0
  77. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_cost.py +0 -0
  78. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_dataset.py +0 -0
  79. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_decisions.py +0 -0
  80. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_evals.py +0 -0
  81. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_evals_assert_gate.py +0 -0
  82. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_evals_decision.py +0 -0
  83. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_evals_gate.py +0 -0
  84. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_evals_predicate.py +0 -0
  85. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_gateway.py +0 -0
  86. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_gemini_provider.py +0 -0
  87. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_guardrails.py +0 -0
  88. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_http_provider.py +0 -0
  89. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_local_trace.py +0 -0
  90. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_package.py +0 -0
  91. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_pipeline.py +0 -0
  92. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_prompts.py +0 -0
  93. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_retrieval_chunking.py +0 -0
  94. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_retrieval_citations.py +0 -0
  95. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_retrieval_connectors.py +0 -0
  96. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_retrieval_embedding.py +0 -0
  97. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_retrieval_gate.py +0 -0
  98. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_retrieval_hybrid.py +0 -0
  99. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_retrieval_metrics.py +0 -0
  100. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_retrieval_rerank.py +0 -0
  101. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_retrieval_stores.py +0 -0
  102. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_retrieval_types.py +0 -0
  103. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_steps.py +0 -0
  104. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_streaming.py +0 -0
  105. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_structured.py +0 -0
  106. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_tool_permissions.py +0 -0
  107. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_tools.py +0 -0
  108. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_tracing.py +0 -0
  109. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_types.py +0 -0
  110. {aid_runtime_python-0.1.97 → aid_runtime_python-0.1.98}/tests/test_vertex_provider.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aid-runtime-python
3
- Version: 0.1.97
3
+ Version: 0.1.98
4
4
  Summary: The AID Framework Python runtime: model gateway with pinning and cost budgets, prompt registry, tool executor, OTel GenAI tracing, plain-async pipeline, and an eval harness that gates on statistical regression against a stored baseline.
5
5
  Author: The AID Framework authors
6
6
  License-Expression: Apache-2.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "aid-runtime-python"
7
- version = "0.1.97"
7
+ version = "0.1.98"
8
8
  description = "The AID Framework Python runtime: model gateway with pinning and cost budgets, prompt registry, tool executor, OTel GenAI tracing, plain-async pipeline, and an eval harness that gates on statistical regression against a stored baseline."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -301,7 +301,7 @@ from aid_runtime.types import (
301
301
  )
302
302
 
303
303
  #: Kept in sync with ``pyproject.toml`` by a test, so the two cannot drift.
304
- __version__ = "0.1.97"
304
+ __version__ = "0.1.98"
305
305
 
306
306
  __all__ = [
307
307
  "ANTHROPIC_VERSION",
@@ -29,6 +29,7 @@ from aid_runtime.agents.guardrails import (
29
29
  )
30
30
  from aid_runtime.agents.ir import (
31
31
  AgentDefinition,
32
+ BedrockGuardrailPolicy,
32
33
  DecisionDefinition,
33
34
  GuardrailDefinition,
34
35
  HumanApproval,
@@ -37,6 +38,13 @@ from aid_runtime.agents.ir import (
37
38
  ToolSpec,
38
39
  )
39
40
  from aid_runtime.agents.loop import AgentRuntime
41
+ from aid_runtime.agents.managed import (
42
+ BedrockGuardrailClient,
43
+ GuardrailClient,
44
+ GuardrailOutcome,
45
+ ManagedGuardrailSettings,
46
+ managed_guardrail_settings,
47
+ )
40
48
  from aid_runtime.agents.memory import (
41
49
  IMPLEMENTED_MEMORY_KINDS,
42
50
  MEMORY_SCOPES,
@@ -67,18 +75,23 @@ __all__ = [
67
75
  "AgentCheckpoint",
68
76
  "AgentDefinition",
69
77
  "AgentRuntime",
78
+ "BedrockGuardrailClient",
79
+ "BedrockGuardrailPolicy",
70
80
  "DecisionAsker",
71
81
  "DecisionDefinition",
72
82
  "Detection",
73
83
  "FileMemoryStore",
84
+ "GuardrailClient",
74
85
  "GuardrailDefinition",
75
86
  "GuardrailEngine",
76
87
  "GuardrailEscalation",
88
+ "GuardrailOutcome",
77
89
  "GuardrailRecord",
78
90
  "GuardrailVerdict",
79
91
  "HumanApproval",
80
92
  "HumanDecision",
81
93
  "InProcessMemoryStore",
94
+ "ManagedGuardrailSettings",
82
95
  "MemoryDefinition",
83
96
  "MemoryStore",
84
97
  "PendingApproval",
@@ -93,6 +106,7 @@ __all__ = [
93
106
  "check_memory_supported",
94
107
  "detect",
95
108
  "evaluate_policy",
109
+ "managed_guardrail_settings",
96
110
  "policy_source",
97
111
  "render_context",
98
112
  ]
@@ -50,6 +50,7 @@ from typing import Any, Literal, Protocol, cast
50
50
  from pydantic import BaseModel, ConfigDict
51
51
 
52
52
  from aid_runtime.agents.ir import DecisionDefinition, GuardrailDefinition
53
+ from aid_runtime.agents.managed import BedrockGuardrailClient, GuardrailClient
53
54
  from aid_runtime.decisions import DecisionClient, DecisionQuestion, DecisionResponse
54
55
  from aid_runtime.errors import ConfigurationError, GuardrailBlockedError
55
56
 
@@ -77,6 +78,9 @@ ACTION_REDACT: GuardrailAction = "redact"
77
78
  ACTION_FLAG: GuardrailAction = "flag"
78
79
  ACTION_ESCALATE: GuardrailAction = "escalate"
79
80
 
81
+ #: The one managed engine. ``builtin`` is the default and needs no name here.
82
+ ENGINE_BEDROCK = "bedrock"
83
+
80
84
  GUARDRAIL_STAGES: tuple[GuardrailStage, ...] = ("input", "retrieval", "tool-call", "output")
81
85
  GUARDRAIL_ACTIONS: tuple[GuardrailAction, ...] = (
82
86
  ACTION_BLOCK,
@@ -323,6 +327,7 @@ class GuardrailEngine:
323
327
  guardrails: Mapping[str, GuardrailDefinition] | None = None,
324
328
  decisions: Mapping[str, DecisionDefinition] | None = None,
325
329
  client_factory: Callable[[], DecisionAsker] | None = None,
330
+ guardrail_client_factory: Callable[[], GuardrailClient] | None = None,
326
331
  ) -> None:
327
332
  self._declared = dict(guardrails or {})
328
333
  self._decisions = dict(decisions or {})
@@ -330,10 +335,19 @@ class GuardrailEngine:
330
335
  #: boot when no request reaches a decision-backed guardrail.
331
336
  self._client_factory = client_factory or DecisionClient
332
337
  self._client: DecisionAsker | None = None
338
+ #: Same rule for a managed engine: a document with no ``engine: bedrock``
339
+ #: guardrail must never require Bedrock credentials to start.
340
+ self._guardrail_client_factory = guardrail_client_factory or BedrockGuardrailClient
341
+ self._guardrail_client: GuardrailClient | None = None
333
342
 
334
343
  def declared(self, guardrail_id: str) -> GuardrailDefinition | None:
335
344
  return self._declared.get(guardrail_id)
336
345
 
346
+ def _managed(self) -> GuardrailClient:
347
+ if self._guardrail_client is None:
348
+ self._guardrail_client = self._guardrail_client_factory()
349
+ return self._guardrail_client
350
+
337
351
  def check_input(
338
352
  self,
339
353
  guardrails: Sequence[GuardrailDefinition],
@@ -346,33 +360,74 @@ class GuardrailEngine:
346
360
  guardrails: Sequence[GuardrailDefinition],
347
361
  text: str,
348
362
  ) -> GuardrailVerdict:
349
- """The ``input`` stage with decisions: the same rules, one awaitable question.
363
+ """The ``input`` stage when a guardrail needs more than a detector.
364
+
365
+ The async twin of :meth:`check_input`: decisions and managed engines are the
366
+ two guardrails that need a round trip, and both are awaited here. A caller
367
+ that has neither can keep using the synchronous method.
368
+ """
369
+ return await self._check_async("input", guardrails, {"text": text}, tool_id=None)
370
+
371
+ async def check_output_async(
372
+ self,
373
+ guardrails: Sequence[GuardrailDefinition],
374
+ text: str,
375
+ ) -> GuardrailVerdict:
376
+ """The ``output`` stage: managed engines only, since a decision guards input.
350
377
 
351
- This is the only stage a decision may guard, so it is the only stage with an
352
- async twin; ``check_input`` stays synchronous and unchanged for callers that
353
- have no network check to make. Order is preserved across both kinds of
354
- guardrail -- declaration order still decides between comparable actions, and
355
- ``block`` still outranks it.
378
+ The response text is what ``ApplyGuardrail`` rewrites, so a managed
379
+ ``redact`` returns the provider's rewritten text rather than a local mask.
380
+ """
381
+ return await self._check_async("output", guardrails, {"text": text}, tool_id=None)
382
+
383
+ async def _check_async(
384
+ self,
385
+ stage: GuardrailStage,
386
+ guardrails: Sequence[GuardrailDefinition],
387
+ payload: Mapping[str, Any],
388
+ tool_id: str | None,
389
+ ) -> GuardrailVerdict:
390
+ """One stage, allowing the guardrails that need an await.
356
391
 
357
- A guardrail that declares both policies and a decision asks the decision only
358
- when its policies found nothing: asking a model about input a detector already
359
- refused would spend tokens to learn what the detector said.
392
+ Order and precedence are the synchronous path's: a policy hit is recorded
393
+ before any network check is considered, declaration order still decides
394
+ between comparable actions, and ``block`` still outranks it.
360
395
  """
361
- payload: Mapping[str, Any] = {"text": text}
362
- applicable = [guardrail for guardrail in guardrails if "input" in guardrail.stages]
396
+ text = _text_of(payload)
397
+ applicable = [
398
+ guardrail
399
+ for guardrail in guardrails
400
+ if stage in guardrail.stages and _covers(guardrail, stage, tool_id)
401
+ ]
363
402
  records: list[GuardrailRecord] = []
364
403
  fired: list[GuardrailDefinition] = []
404
+ rewritten: dict[str, str] = {}
365
405
  for guardrail in applicable:
366
- policies = _firing_policies(guardrail, "input", payload)
406
+ policies = _firing_policies(guardrail, stage, payload)
367
407
  record: GuardrailRecord | None = None
368
408
  if policies:
369
409
  record = GuardrailRecord(
370
410
  guardrail=guardrail.id,
371
- stage="input",
411
+ stage=stage,
372
412
  action=_action(guardrail),
373
413
  policies=policies,
374
414
  message=guardrail.message,
375
415
  )
416
+ elif guardrail.engine == ENGINE_BEDROCK:
417
+ outcome = await self._managed().apply(
418
+ source="INPUT" if stage == "input" else "OUTPUT",
419
+ text=text,
420
+ )
421
+ if outcome.intervened:
422
+ if outcome.text is not None:
423
+ rewritten[guardrail.id] = outcome.text
424
+ record = GuardrailRecord(
425
+ guardrail=guardrail.id,
426
+ stage=stage,
427
+ action=_action(guardrail),
428
+ policies=outcome.policies,
429
+ message=guardrail.message or ", ".join(outcome.policies),
430
+ )
376
431
  elif guardrail.decision is not None:
377
432
  record = await self._ask_decision(guardrail, text)
378
433
  if record is None:
@@ -380,25 +435,7 @@ class GuardrailEngine:
380
435
  fired.append(guardrail)
381
436
  records.append(record)
382
437
 
383
- deciding = _decide(records)
384
- if deciding is None:
385
- return GuardrailVerdict(stage="input", text=text, arguments=dict(payload))
386
-
387
- redacted = payload
388
- if deciding.action == ACTION_REDACT:
389
- redacted = _redact(
390
- "input",
391
- [guardrail for guardrail in fired if guardrail.action == ACTION_REDACT],
392
- payload,
393
- )
394
- return GuardrailVerdict(
395
- stage="input",
396
- records=tuple(records),
397
- action=deciding.action,
398
- deciding=deciding,
399
- text=_text_of(redacted),
400
- arguments=dict(redacted),
401
- )
438
+ return _assemble(stage, payload, fired, records, rewritten)
402
439
 
403
440
  async def _ask_decision(
404
441
  self,
@@ -550,6 +587,15 @@ class GuardrailEngine:
550
587
  records: list[GuardrailRecord] = []
551
588
  fired: list[GuardrailDefinition] = []
552
589
  for guardrail in applicable:
590
+ if guardrail.engine != "builtin":
591
+ # Fail closed rather than skip: a synchronous caller cannot await the
592
+ # provider, and returning "no violation" would be an allow nobody
593
+ # declared. The generated pipeline calls the async variant instead.
594
+ raise GuardrailBlockedError(
595
+ guardrail.id,
596
+ stage,
597
+ f"guardrail uses engine {guardrail.engine!r}, which needs the async check",
598
+ )
553
599
  policies = _firing_policies(guardrail, stage, payload)
554
600
  if not policies:
555
601
  continue
@@ -564,29 +610,49 @@ class GuardrailEngine:
564
610
  )
565
611
  )
566
612
 
567
- deciding = _decide(records)
568
- if deciding is None:
569
- return GuardrailVerdict(
570
- stage=stage,
571
- text=_text_of(payload),
572
- arguments=dict(payload),
573
- )
613
+ return _assemble(stage, payload, fired, records)
614
+
615
+
616
+ def _assemble(
617
+ stage: GuardrailStage,
618
+ payload: Mapping[str, Any],
619
+ fired: Sequence[GuardrailDefinition],
620
+ records: Sequence[GuardrailRecord],
621
+ rewritten: Mapping[str, str] | None = None,
622
+ ) -> GuardrailVerdict:
623
+ """Turn the records a stage produced into the verdict the pipeline acts on.
624
+
625
+ ``rewritten`` maps a managed guardrail to the provider's replacement text. Only
626
+ the deciding guardrail's text is substituted: a provider that anonymised one
627
+ policy's spans did not necessarily see the others.
628
+ """
629
+ deciding = _decide(records)
630
+ if deciding is None:
631
+ return GuardrailVerdict(
632
+ stage=stage,
633
+ text=_text_of(payload),
634
+ arguments=dict(payload),
635
+ )
574
636
 
575
- redacted = payload
576
- if deciding.action == ACTION_REDACT:
637
+ redacted = payload
638
+ if deciding.action == ACTION_REDACT:
639
+ replacement = (rewritten or {}).get(deciding.guardrail)
640
+ if replacement is not None:
641
+ redacted = {**payload, "text": replacement}
642
+ else:
577
643
  redacted = _redact(
578
644
  stage,
579
645
  [guardrail for guardrail in fired if guardrail.action == ACTION_REDACT],
580
646
  payload,
581
647
  )
582
- return GuardrailVerdict(
583
- stage=stage,
584
- records=tuple(records),
585
- action=deciding.action,
586
- deciding=deciding,
587
- text=_text_of(redacted),
588
- arguments=dict(redacted),
589
- )
648
+ return GuardrailVerdict(
649
+ stage=stage,
650
+ records=tuple(records),
651
+ action=deciding.action,
652
+ deciding=deciding,
653
+ text=_text_of(redacted),
654
+ arguments=dict(redacted),
655
+ )
590
656
 
591
657
 
592
658
  def _covers(guardrail: GuardrailDefinition, stage: str, tool_id: str | None) -> bool:
@@ -28,6 +28,7 @@ from aid_runtime.types import (
28
28
 
29
29
  __all__ = [
30
30
  "AgentDefinition",
31
+ "BedrockGuardrailPolicy",
31
32
  "DecisionDefinition",
32
33
  "GuardrailDefinition",
33
34
  "HumanApproval",
@@ -57,6 +58,29 @@ _ON_BUDGET_EXHAUSTED = frozenset({"fail", "degrade"})
57
58
  #: rejects it again rather than trusting that every document came through a gate.
58
59
  _GUARDRAIL_STAGES = frozenset({"input", "output", "retrieval", "tool-call"})
59
60
  _GUARDRAIL_ACTIONS = frozenset({"block", "redact", "flag", "escalate"})
61
+ _GUARDRAIL_ENGINES = frozenset({"builtin", "bedrock"})
62
+ _BEDROCK_CONTENT_FILTERS = frozenset(
63
+ {"sexual", "violence", "hate", "insults", "misconduct", "prompt-attack"}
64
+ )
65
+ _BEDROCK_CONTENT_STRENGTHS = frozenset({"low", "medium", "high"})
66
+ _BEDROCK_PII_ENTITIES = frozenset(
67
+ {
68
+ "email",
69
+ "phone",
70
+ "name",
71
+ "address",
72
+ "ssn",
73
+ "credit-card",
74
+ "bank-account",
75
+ "passport",
76
+ "drivers-license",
77
+ "ip-address",
78
+ "url",
79
+ "aws-access-key",
80
+ "aws-secret-key",
81
+ }
82
+ )
83
+
60
84
  _GUARDRAIL_POLICIES = frozenset(
61
85
  {
62
86
  "pii-detect",
@@ -100,6 +124,22 @@ class DecisionDefinition:
100
124
  criteria: tuple[str, ...]
101
125
 
102
126
 
127
+ @dataclass(frozen=True, slots=True)
128
+ class BedrockGuardrailPolicy:
129
+ """The policy a managed guardrail asks Bedrock to enforce.
130
+
131
+ The IR's vocabulary is kept here, not AWS's: lowercase content filters and
132
+ strengths, hyphenated entity names. ``agents.managed`` is the one place that
133
+ maps it to the wire enums, which is what keeps the conversion testable without
134
+ an AWS call and stops the generator from owning a second copy of the table.
135
+ """
136
+
137
+ content: tuple[tuple[str, str], ...] = ()
138
+ topics: tuple[tuple[str, str], ...] = ()
139
+ words: tuple[str, ...] = ()
140
+ pii: tuple[str, ...] = ()
141
+
142
+
103
143
  @dataclass(frozen=True, slots=True)
104
144
  class GuardrailDefinition:
105
145
  """One resolved guardrail: where it runs, what it detects, and what it does.
@@ -111,6 +151,9 @@ class GuardrailDefinition:
111
151
  ``on_decision`` maps each of its labels to an outcome. ``allow`` is the one
112
152
  outcome that produces no record; the rest are guardrail actions. The trailing
113
153
  defaults keep every keyword call site written before decisions existed valid.
154
+
155
+ ``engine`` is ``builtin`` (this process runs the detectors) or ``bedrock`` (the
156
+ managed service evaluates ``bedrock`` instead, and ``policies`` stays empty).
114
157
  """
115
158
 
116
159
  id: str
@@ -125,6 +168,8 @@ class GuardrailDefinition:
125
168
  decision: str | None = None
126
169
  on_decision: tuple[tuple[str, str], ...] = ()
127
170
  min_confidence: float = 0.0
171
+ engine: str = "builtin"
172
+ bedrock: BedrockGuardrailPolicy | None = None
128
173
 
129
174
 
130
175
  @dataclass(frozen=True, slots=True)
@@ -391,12 +436,27 @@ def _guardrail(
391
436
  raise ConfigurationError(
392
437
  f"{where} has action {action!r}; expected one of {sorted(_GUARDRAIL_ACTIONS)}"
393
438
  )
439
+ engine = _str(entry.get("engine")) or "builtin"
440
+ if engine not in _GUARDRAIL_ENGINES:
441
+ raise ConfigurationError(
442
+ f"{where} has engine {engine!r}; expected one of {sorted(_GUARDRAIL_ENGINES)}"
443
+ )
444
+ bedrock = _bedrock_policy(entry.get("bedrock"), where) if engine == "bedrock" else None
445
+ if engine == "bedrock" and bedrock is None:
446
+ raise ConfigurationError(
447
+ f"{where} uses engine 'bedrock' with no bedrock policy; name at least one of "
448
+ "content, topics, words, pii"
449
+ )
394
450
  redact = tuple(_str_list(entry.get("redact"), f"{where} redact"))
395
- if action == "redact" and not redact:
451
+ if action == "redact" and not redact and engine == "builtin":
396
452
  raise ConfigurationError(
397
453
  f"{where} uses action 'redact' with no redact fields; a redaction that names "
398
454
  "no field cannot redact anything"
399
455
  )
456
+ if action == "redact" and engine == "bedrock":
457
+ # The managed service rewrites the one field it is given, so a managed
458
+ # redaction is always over the prompt text.
459
+ redact = ("text",)
400
460
  tool_ids = tuple(_str_list(entry.get("tools"), f"{where} tools"))
401
461
  missing = sorted(set(tool_ids) - set(tools))
402
462
  if missing:
@@ -415,6 +475,52 @@ def _guardrail(
415
475
  message=_required_str(on_fail, "message", f"{where} onFail"),
416
476
  fallback_prompt=_str(on_fail.get("fallbackPrompt")),
417
477
  log=on_fail.get("log") is True,
478
+ engine=engine,
479
+ bedrock=bedrock,
480
+ )
481
+
482
+
483
+ def _bedrock_policy(raw: Any, where: str) -> BedrockGuardrailPolicy | None:
484
+ """The managed policy, with each name checked against the closed vocabulary.
485
+
486
+ The build-time gate already refuses an unknown name; repeating the check here
487
+ is what keeps a hand-written document from reaching the wire, where the
488
+ service would answer with a validation error naming its own enum instead of
489
+ the spec's word.
490
+ """
491
+ if not isinstance(raw, Mapping):
492
+ return None
493
+ content: list[tuple[str, str]] = []
494
+ content_raw = raw.get("content")
495
+ if isinstance(content_raw, Mapping):
496
+ for name, strength in content_raw.items():
497
+ if name not in _BEDROCK_CONTENT_FILTERS:
498
+ raise ConfigurationError(f"{where} names unknown content filter {name!r}")
499
+ if strength not in _BEDROCK_CONTENT_STRENGTHS:
500
+ raise ConfigurationError(
501
+ f"{where} gives {name!r} strength {strength!r}; expected one of "
502
+ f"{sorted(_BEDROCK_CONTENT_STRENGTHS)}"
503
+ )
504
+ content.append((name, strength))
505
+ topics: list[tuple[str, str]] = []
506
+ topics_raw = raw.get("topics")
507
+ if isinstance(topics_raw, Mapping):
508
+ topics = [(name, definition) for name, definition in topics_raw.items()]
509
+ words = tuple(_str_list(raw.get("words"), f"{where} bedrock.words"))
510
+ pii_raw = _str_list(raw.get("pii"), f"{where} bedrock.pii")
511
+ unknown_pii = sorted(set(pii_raw) - _BEDROCK_PII_ENTITIES)
512
+ if unknown_pii:
513
+ raise ConfigurationError(
514
+ f"{where} names unknown PII entities {unknown_pii}; expected a subset of "
515
+ f"{sorted(_BEDROCK_PII_ENTITIES)}"
516
+ )
517
+ if not content and not topics and not words and not pii_raw:
518
+ return None
519
+ return BedrockGuardrailPolicy(
520
+ content=tuple(content),
521
+ topics=tuple(topics),
522
+ words=words,
523
+ pii=tuple(pii_raw),
418
524
  )
419
525
 
420
526