aid-runtime-python 0.1.111__py3-none-any.whl → 0.1.112__py3-none-any.whl

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.
aid_runtime/__init__.py CHANGED
@@ -310,7 +310,7 @@ from aid_runtime.types import (
310
310
  )
311
311
 
312
312
  #: Kept in sync with ``pyproject.toml`` by a test, so the two cannot drift.
313
- __version__ = "0.1.111"
313
+ __version__ = "0.1.112"
314
314
 
315
315
  __all__ = [
316
316
  "ANTHROPIC_VERSION",
@@ -114,7 +114,7 @@ class DeepAgentsEngine:
114
114
  check_engine(agent)
115
115
  if not frame.input_checked:
116
116
  frame.input_checked = True
117
- paused = self._runtime._guardrail_text(
117
+ paused = await self._runtime._guardrail_text(
118
118
  _GUARDRAIL_INPUT, frame, memory_key, span=span, rewrite=True
119
119
  )
120
120
  if paused is not None:
@@ -430,7 +430,7 @@ class DeepAgentsEngine:
430
430
  self, frame: _Frame, result: Any, *, memory_key: str, span: Span
431
431
  ) -> _Outcome:
432
432
  """Apply the output guardrail, record memory, and report the answer."""
433
- released = self._runtime._guardrail_answer(
433
+ released = await self._runtime._guardrail_answer(
434
434
  frame, self._answer(result), memory_key, span=span
435
435
  )
436
436
  if isinstance(released, _Outcome):
@@ -536,7 +536,7 @@ class _Guardrails(AgentMiddleware):
536
536
  # branch only has to open the delegate's span.
537
537
  return await self._delegate_in_span(request, handler, call)
538
538
  runtime._authorize(self._frame.agent, call, self._principal)
539
- guarded, halted = runtime._guardrail_tool_calls(
539
+ guarded, halted = await runtime._guardrail_tool_calls(
540
540
  self._frame, [call], self._memory_key, span=self._record_span
541
541
  )
542
542
  if halted is not None:
@@ -588,6 +588,19 @@ class GuardrailEngine:
588
588
  ) -> GuardrailVerdict:
589
589
  return self._check("tool-call", guardrails, dict(arguments), tool_id=tool_id)
590
590
 
591
+ async def check_tool_call_async(
592
+ self,
593
+ guardrails: Sequence[GuardrailDefinition],
594
+ tool_id: str,
595
+ arguments: Mapping[str, Any],
596
+ ) -> GuardrailVerdict:
597
+ """The ``tool-call`` stage when a guardrail needs a round trip.
598
+
599
+ A managed engine can apply at this stage, and the loop always uses this
600
+ variant: awaiting when no guardrail needs it costs nothing.
601
+ """
602
+ return await self._check_async("tool-call", guardrails, dict(arguments), tool_id=tool_id)
603
+
591
604
  def _check(
592
605
  self,
593
606
  stage: GuardrailStage,
@@ -612,6 +625,14 @@ class GuardrailEngine:
612
625
  stage,
613
626
  f"guardrail uses engine {guardrail.engine!r}, which needs the async check",
614
627
  )
628
+ if guardrail.decision is not None:
629
+ # Same rule as a managed engine: a synchronous caller cannot await the
630
+ # decision, and skipping it would be an allow nobody declared.
631
+ raise GuardrailBlockedError(
632
+ guardrail.id,
633
+ stage,
634
+ f"guardrail asks decision {guardrail.decision!r}, which needs the async check",
635
+ )
615
636
  policies = _firing_policies(guardrail, stage, payload)
616
637
  if not policies:
617
638
  continue
aid_runtime/agents/ir.py CHANGED
@@ -248,6 +248,9 @@ class RuntimeSpec:
248
248
  tools: Mapping[str, ToolSpec]
249
249
  agents: Mapping[str, AgentDefinition]
250
250
  guardrails: Mapping[str, GuardrailDefinition] = field(default_factory=dict)
251
+ #: The decisions a guardrail's ``decision`` may name, keyed by id. Resolved here
252
+ #: so the loop's engine can answer one without reading the document again.
253
+ decisions: Mapping[str, DecisionDefinition] = field(default_factory=dict)
251
254
  #: The single concurrency cap, read from the IR's ``deployments[].concurrency``.
252
255
  #: ``None`` means unbounded: the runtime only shares a limiter when a deployment
253
256
  #: declares one, which keeps plain request/response usage free of surprises.
@@ -330,6 +333,11 @@ def from_ir(document: Mapping[str, Any]) -> RuntimeSpec:
330
333
  for guardrail_id, entry in _by_id(document.get("guardrails"), "guardrails").items()
331
334
  }
332
335
 
336
+ decisions = {
337
+ decision_id: _decision(decision_id, entry)
338
+ for decision_id, entry in _by_id(document.get("decisions"), "decisions").items()
339
+ }
340
+
333
341
  fallbacks: dict[str, tuple[str, ...]] = {}
334
342
  for model_id, entry in raw_models.items():
335
343
  fallbacks[model_id] = tuple(_str_list(entry.get("fallbacks"), f"model {model_id!r}"))
@@ -350,6 +358,7 @@ def from_ir(document: Mapping[str, Any]) -> RuntimeSpec:
350
358
  tools=tools,
351
359
  agents=agents,
352
360
  guardrails=guardrails,
361
+ decisions=decisions,
353
362
  concurrency=_concurrency(document.get("deployments")),
354
363
  )
355
364
 
@@ -414,6 +423,29 @@ def _agent(
414
423
  )
415
424
 
416
425
 
426
+ def _decision(decision_id: str, entry: Mapping[str, Any]) -> DecisionDefinition:
427
+ """Resolve one IR decision: the model, the question, and the labels in order.
428
+
429
+ A ``choice`` decision's criteria is a label mapping in the emitter, and a ``score``
430
+ decision's is a list; both reduce to the ordered labels a runtime acts on. Sorted
431
+ keys match the TypeScript runtime, so the two never disagree about a label order.
432
+ """
433
+ where = f"decision {decision_id!r}"
434
+ criteria = entry.get("criteria")
435
+ if criteria is None:
436
+ labels: tuple[str, ...] = ()
437
+ elif isinstance(criteria, Mapping):
438
+ labels = tuple(sorted(str(label) for label in criteria))
439
+ else:
440
+ labels = tuple(_str_list(criteria, f"{where} criteria"))
441
+ return DecisionDefinition(
442
+ id=decision_id,
443
+ model=_required_str(entry, "model", where),
444
+ instructions=_required_str(entry, "instructions", where),
445
+ criteria=labels,
446
+ )
447
+
448
+
417
449
  def _guardrail(
418
450
  guardrail_id: str,
419
451
  entry: Mapping[str, Any],
@@ -475,6 +507,22 @@ def _guardrail(
475
507
  if missing:
476
508
  raise ConfigurationError(f"{where} names undeclared tools: {missing}")
477
509
 
510
+ # A decision guardrail asks a classifier instead of running detectors; without
511
+ # the mapping its labels mean nothing, so the two are parsed together.
512
+ decision = _str(entry.get("decision")) or None
513
+ on_decision: tuple[tuple[str, str], ...] = ()
514
+ min_confidence = 0.0
515
+ if decision is not None:
516
+ raw_map = entry.get("onDecision")
517
+ if raw_map is not None and not isinstance(raw_map, Mapping):
518
+ raise ConfigurationError(f"{where} onDecision must be a mapping of strings")
519
+ on_decision = tuple(
520
+ (label, outcome)
521
+ for label, outcome in (raw_map or {}).items()
522
+ if isinstance(label, str) and isinstance(outcome, str)
523
+ )
524
+ min_confidence = _optional_float(entry.get("minConfidence")) or 0.0
525
+
478
526
  on_fail = entry.get("onFail")
479
527
  if not isinstance(on_fail, Mapping):
480
528
  raise ConfigurationError(f"{where} needs an onFail object")
@@ -488,6 +536,9 @@ def _guardrail(
488
536
  message=_required_str(on_fail, "message", f"{where} onFail"),
489
537
  fallback_prompt=_str(on_fail.get("fallbackPrompt")),
490
538
  log=on_fail.get("log") is True,
539
+ decision=decision,
540
+ on_decision=on_decision,
541
+ min_confidence=min_confidence,
491
542
  engine=engine,
492
543
  bedrock=bedrock,
493
544
  )
@@ -201,7 +201,7 @@ class AgentRuntime:
201
201
  memory_store if memory_store is not None else InProcessMemoryStore()
202
202
  )
203
203
  self._tracer = tracer
204
- self._guardrails = GuardrailEngine(spec.guardrails)
204
+ self._guardrails = GuardrailEngine(spec.guardrails, decisions=spec.decisions)
205
205
  self._tools_by_name: dict[str, ToolSpec] = {
206
206
  definition.name: definition for definition in spec.tools.values()
207
207
  }
@@ -451,7 +451,7 @@ class AgentRuntime:
451
451
 
452
452
  if not frame.input_checked:
453
453
  frame.input_checked = True
454
- paused = self._guardrail_text(
454
+ paused = await self._guardrail_text(
455
455
  _GUARDRAIL_INPUT, frame, memory_key, span=span, rewrite=True
456
456
  )
457
457
  if paused is not None:
@@ -475,7 +475,7 @@ class AgentRuntime:
475
475
  response = generation.response
476
476
 
477
477
  if not response.tool_calls:
478
- answer = self._guardrail_answer(frame, response.text, memory_key, span=span)
478
+ answer = await self._guardrail_answer(frame, response.text, memory_key, span=span)
479
479
  if isinstance(answer, _Outcome):
480
480
  return answer
481
481
  frame.messages.append(Message.assistant(answer))
@@ -501,7 +501,7 @@ class AgentRuntime:
501
501
 
502
502
  record.kind = StepKind.TOOLS
503
503
  record.tool_names = tuple(call.name for call in response.tool_calls)
504
- guarded, paused = self._guardrail_tool_calls(
504
+ guarded, paused = await self._guardrail_tool_calls(
505
505
  frame, response.tool_calls, memory_key, span=span
506
506
  )
507
507
  if paused is not None:
@@ -528,7 +528,7 @@ class AgentRuntime:
528
528
 
529
529
  frame.messages.extend(await self._observe(agent, guarded, principal))
530
530
 
531
- def _guardrail_text(
531
+ async def _guardrail_text(
532
532
  self,
533
533
  stage: str,
534
534
  frame: _Frame,
@@ -545,7 +545,9 @@ class AgentRuntime:
545
545
  """
546
546
  index = _last_user_index(frame.messages)
547
547
  text = "" if index is None else frame.messages[index].content
548
- verdict = self._guardrails.check_input(frame.agent.stage_guardrails(stage), text)
548
+ verdict = await self._guardrails.check_input_async(
549
+ frame.agent.stage_guardrails(stage), text
550
+ )
549
551
  self._record_guardrails(frame, verdict, span)
550
552
  deciding = verdict.deciding
551
553
  if verdict.action == ACTION_BLOCK and deciding is not None:
@@ -556,7 +558,7 @@ class AgentRuntime:
556
558
  frame.messages[index] = Message.user(verdict.text)
557
559
  return None
558
560
 
559
- def _guardrail_answer(
561
+ async def _guardrail_answer(
560
562
  self,
561
563
  frame: _Frame,
562
564
  text: str,
@@ -570,7 +572,7 @@ class AgentRuntime:
570
572
  the checkpoint carries the text, so approving it releases this exact answer
571
573
  without paying for a second generation.
572
574
  """
573
- verdict = self._guardrails.check_output(
575
+ verdict = await self._guardrails.check_output_async(
574
576
  frame.agent.stage_guardrails(_GUARDRAIL_OUTPUT), text
575
577
  )
576
578
  self._record_guardrails(frame, verdict, span)
@@ -581,7 +583,7 @@ class AgentRuntime:
581
583
  return self._guardrail_pause(frame, verdict, memory_key, (), span=span, text=text)
582
584
  return verdict.text if verdict.action == ACTION_REDACT else text
583
585
 
584
- def _guardrail_tool_calls(
586
+ async def _guardrail_tool_calls(
585
587
  self,
586
588
  frame: _Frame,
587
589
  calls: Sequence[ToolCall],
@@ -602,7 +604,7 @@ class AgentRuntime:
602
604
  for call in calls:
603
605
  definition = self._tools_by_name.get(call.name)
604
606
  tool_id = definition.id if definition is not None else call.name
605
- verdict = self._guardrails.check_tool_call(
607
+ verdict = await self._guardrails.check_tool_call_async(
606
608
  frame.agent.stage_guardrails(_GUARDRAIL_TOOL), tool_id, call.arguments
607
609
  )
608
610
  self._record_guardrails(frame, verdict, span)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aid-runtime-python
3
- Version: 0.1.111
3
+ Version: 0.1.112
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
@@ -1,4 +1,4 @@
1
- aid_runtime/__init__.py,sha256=YfQe4LfSJahG3Yy_bxoOY71IYSv_UqQlX8v3VsNkYC8,13238
1
+ aid_runtime/__init__.py,sha256=53yja7xQ_11W7CFF2jjj2tRtW8KUZALc8E6wgja7Kns,13238
2
2
  aid_runtime/cache.py,sha256=jhw_EBd9dfWEUzYeibOvIkVBqqiLUjqF5WKEPz9GkhE,6836
3
3
  aid_runtime/concurrency.py,sha256=z0tFQHw35ttFLKfazGBjCMPOLQOnUotE_OPpvR1piH4,2829
4
4
  aid_runtime/control.py,sha256=y6wPn0fqYJv0nVSsjzpzfiaq1Y7zBoMxCpdrEuqkfvg,17529
@@ -17,11 +17,11 @@ aid_runtime/tracing.py,sha256=q7v1GQ0FaYOq4uWfGMhloXwQb3fXjxE0ixi6Yij9OJQ,9624
17
17
  aid_runtime/types.py,sha256=cVYgmacDIhStQT5dX_X67-q57sPBBaWC5nybtGVqVsw,10389
18
18
  aid_runtime/agents/__init__.py,sha256=H_WKu9u0fvF56W7ccTByEv1RdayzwUfRzEKsXXDRr2A,3113
19
19
  aid_runtime/agents/chat_model.py,sha256=fZdCmSpoECuBVGZE1s8NmT2erT33RhDuLh3yJM6kAJE,14808
20
- aid_runtime/agents/deep_agents.py,sha256=ZM6ZSc92arCvtsYGsIGdF-NryqN_tvXtRqbdlHaGpeE,26948
20
+ aid_runtime/agents/deep_agents.py,sha256=Eg_BLm2cg7T_tXSxR7ag8SEvTMCFBLHoIvln3BoWORw,26966
21
21
  aid_runtime/agents/engine.py,sha256=3cXCYY806URDblYD500cfw7873IoVCy8BH1d4h2YAsc,5789
22
- aid_runtime/agents/guardrails.py,sha256=0w0JDfQMamNiPpVkxdXmh-iB-Bc1i3QnvRTIsUzgCoA,31039
23
- aid_runtime/agents/ir.py,sha256=_6U6dU0yBpmL69bs2K_mnliW0z0YTCT4XqjGtadG-Ns,30155
24
- aid_runtime/agents/loop.py,sha256=T-QnC9BQ9K3RyZfHdAuzWiwMXBpQjDLGdHrLgf2fnFM,52608
22
+ aid_runtime/agents/guardrails.py,sha256=iCHq06zixe5vnhtSUrErG7TmS4lmzlpF-YFqUnRYq0k,31997
23
+ aid_runtime/agents/ir.py,sha256=GPfCzjcGiET79d1PlDU8H1PbUzQRBDk__xcvetK7ir0,32430
24
+ aid_runtime/agents/loop.py,sha256=iQ_kk-Miu7oZUKeRtCpMGzAQUwV2IaVM9BdXhThZ77I,52728
25
25
  aid_runtime/agents/managed.py,sha256=SFyx6Rca2aoiMzA9q6B4Nfes6ZCjn_5_zN0SxzemIII,23247
26
26
  aid_runtime/agents/memory.py,sha256=6_D6k2bzAiR2euNvzr6HB28wsUUq-R-WTQc12gkB8ao,9156
27
27
  aid_runtime/agents/policy.py,sha256=ael0EW_hWfLu95rbqFMrR4WyqAPtgiSNvNw97xnthdw,6122
@@ -55,7 +55,7 @@ aid_runtime/retrieval/rerank.py,sha256=L4ICBi9YHDzhnKVtImKh5Wzre1HDzS8at-AKDkXBJ
55
55
  aid_runtime/retrieval/retriever.py,sha256=5I8BocFaJ292cqh9-Gz2NRB4OwBHa-pitAB6--5ARBE,19890
56
56
  aid_runtime/retrieval/stores.py,sha256=VEi68AaMHcQjdAbWOHflp3jCl1_1db71rIzZjTouLlo,5857
57
57
  aid_runtime/retrieval/types.py,sha256=bke4W2q8ecGFvF2tj8y1tfFq8EtGb6iiPVbT-l02a98,19860
58
- aid_runtime_python-0.1.111.dist-info/METADATA,sha256=RhT-h_IIy5LKzupFCf6VUUW9MWVzmP9OfqzOxr3DXlM,17006
59
- aid_runtime_python-0.1.111.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
60
- aid_runtime_python-0.1.111.dist-info/top_level.txt,sha256=QvODgNVRDDMmLcTnoIQ4jYnhqf-N4b9sHWEmaUUxp44,12
61
- aid_runtime_python-0.1.111.dist-info/RECORD,,
58
+ aid_runtime_python-0.1.112.dist-info/METADATA,sha256=gEc6Zks6ZYPu0Saf5QJCrdYKcqQAkw6QKL_UOKwCQh0,17006
59
+ aid_runtime_python-0.1.112.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
60
+ aid_runtime_python-0.1.112.dist-info/top_level.txt,sha256=QvODgNVRDDMmLcTnoIQ4jYnhqf-N4b9sHWEmaUUxp44,12
61
+ aid_runtime_python-0.1.112.dist-info/RECORD,,