mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(oracle_router): feedback payloads cannot pre-seed the guardrail's status record
- The guardrail verifier reads its verdict from the status record the guardrail appends to the request metadata it is handed, and that metadata was the caller's feedback payload spread as-is. A payload carrying `standard_logging_guardrail_information: [0]` made the recorded list unreadable, so a rejected answer under `on_failure: log` scored 1.0. The status key is now dropped from the payload before the guardrail runs, and the payload can no longer rename the program. - Log lines in the router and its hook no longer carry anything derived from the request (program id, prompt classification, chosen model, exception text): `logger.exception` keeps the traceback, and CodeQL's log-injection query no longer has request-fed sinks to trace through the Router. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HeiPj7YeRpKatjj2J7XhJ3
This commit is contained in:
parent
583e027b2d
commit
de79b11922
4 changed files with 34 additions and 20 deletions
|
|
@ -113,5 +113,5 @@ class OracleRouterPostCallHook(CustomLogger):
|
|||
)
|
||||
if metadata.get(PROGRAM_DONE_KEY):
|
||||
self.oracle_router.complete(program_id, score=_score(metadata), owner=owner)
|
||||
except Exception as error: # noqa: BLE001 # a logging callback must never fail the request it observes
|
||||
verbose_router_logger.exception("OracleRouterPostCallHook: failed to record request: %s", error)
|
||||
except Exception: # noqa: BLE001 # a logging callback must never fail the request it observes
|
||||
verbose_router_logger.exception("OracleRouterPostCallHook: failed to record a request")
|
||||
|
|
|
|||
|
|
@ -265,12 +265,8 @@ class OracleRouter:
|
|||
chosen_model, request_type = await self._decide(program_id, prompt, _api_key_hash(request_kwargs))
|
||||
_stamp_internal(request_kwargs, CHOSEN_MODEL_METADATA_KEY, chosen_model)
|
||||
_stamp_internal(request_kwargs, PROGRAM_ID_METADATA_KEY, program_id)
|
||||
verbose_router_logger.debug( # caller-supplied program ids stay out of the log
|
||||
"OracleRouter[%s]: %s request, request_type=%s -> %s",
|
||||
self.router_name,
|
||||
"program" if program_id else "stateless",
|
||||
request_type.value,
|
||||
chosen_model,
|
||||
verbose_router_logger.debug( # nothing request-derived is logged; the decision is on the request's metadata
|
||||
"OracleRouter: routed a %s request", "program" if program_id else "stateless"
|
||||
)
|
||||
return PreRoutingHookResponse(
|
||||
model=chosen_model,
|
||||
|
|
@ -340,15 +336,9 @@ class OracleRouter:
|
|||
async def _verify_and_learn(self, binding: ProgramBinding, outcome: ProgramOutcome) -> float:
|
||||
try:
|
||||
score: Final = clamp_score(await self.verifier.verify(outcome))
|
||||
except Exception as error: # noqa: BLE001 # any verifier failure is counted and must not break the feedback loop
|
||||
except Exception: # noqa: BLE001 # any verifier failure is counted and must not break the feedback loop
|
||||
self.verifications_failed += 1
|
||||
verbose_router_logger.exception(
|
||||
"OracleRouter[%s]: verification of a %s program on %s failed: %s",
|
||||
self.router_name,
|
||||
binding.context.request_type.value,
|
||||
binding.model,
|
||||
error,
|
||||
)
|
||||
verbose_router_logger.exception("OracleRouter: a verification failed")
|
||||
return float("nan")
|
||||
async with self._update_lock:
|
||||
self.decision_maker.update(binding.context, binding.model, score)
|
||||
|
|
|
|||
|
|
@ -82,6 +82,19 @@ def find_guardrail(name: str) -> CustomGuardrail | None:
|
|||
return next((guardrail for guardrail in guardrails if guardrail.guardrail_name == name), None)
|
||||
|
||||
|
||||
def _verifier_metadata(outcome: ProgramOutcome) -> dict[str, object]: # mutable-ok: the guardrail writes into it
|
||||
"""The feedback payload as the guardrail's request metadata.
|
||||
|
||||
The verdict is read from the status record the guardrail writes here, so the caller's payload must not
|
||||
pre-seed that key (a non-record entry would make the whole list unreadable and hide a rejection), and
|
||||
it cannot rename the program.
|
||||
"""
|
||||
return {
|
||||
**{key: value for key, value in outcome.payload.items() if key != _STATUS_RECORD_KEY},
|
||||
"program_id": outcome.program_id,
|
||||
}
|
||||
|
||||
|
||||
def _status_records(bucket: object) -> Sequence[Mapping[str, object]]:
|
||||
"""The ``standard_logging_guardrail_information`` records of one metadata bucket, or none."""
|
||||
try:
|
||||
|
|
@ -163,7 +176,7 @@ class GuardrailVerifier:
|
|||
)
|
||||
request_data: Final[dict[str, object]] = { # mutable-ok: the guardrail records its status into it
|
||||
"model": outcome.model,
|
||||
"metadata": {"program_id": outcome.program_id, **outcome.payload},
|
||||
"metadata": _verifier_metadata(outcome),
|
||||
}
|
||||
verdict_error: Final = await _apply(guardrail, inputs, request_data)
|
||||
status: Final = recorded_status(request_data, self._name)
|
||||
|
|
|
|||
|
|
@ -53,9 +53,9 @@ class _RecordingGuardrail:
|
|||
self.status, self.error, self.name = status, error, name
|
||||
|
||||
async def apply_guardrail(self, inputs, request_data, input_type, logging_obj=None):
|
||||
request_data["metadata"]["standard_logging_guardrail_information"] = [
|
||||
{"guardrail_name": self.name, "guardrail_status": self.status}
|
||||
]
|
||||
self.seen = dict(request_data["metadata"]) # what the guardrail was handed, before it records anything
|
||||
records = request_data["metadata"].setdefault("standard_logging_guardrail_information", [])
|
||||
records.append({"guardrail_name": self.name, "guardrail_status": self.status}) # appended, like the real ones
|
||||
if self.error is not None:
|
||||
raise self.error
|
||||
return inputs
|
||||
|
|
@ -104,6 +104,17 @@ async def test_guardrail_verifier_reads_the_recorded_verdict_before_the_exceptio
|
|||
assert await GuardrailVerifier("flagged", lookup=judge.get).verify(_outcome()) == 0.0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_feedback_payload_cannot_pre_seed_the_status_record_or_rename_the_program():
|
||||
judge = _RecordingGuardrail("guardrail_intervened", name="judge") # on_failure: log -> no exception
|
||||
forged = {"standard_logging_guardrail_information": [0], "program_id": "someone-else", "container": "c1"}
|
||||
outcome = ProgramOutcome(
|
||||
program_id="p", model="smart", prompt="task", response_text="wrong", cost=0.0, messages=(), payload=forged
|
||||
)
|
||||
assert await GuardrailVerifier("judge", lookup={"judge": judge}.get).verify(outcome) == 0.0
|
||||
assert judge.seen == {"program_id": "p", "container": "c1"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_guardrail_that_could_not_evaluate_is_a_failed_verification_not_a_pass():
|
||||
judge = {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue