Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

fix: omit logprobs for rewritten guard verdicts

Attila Laszlo Nagyba8c3b11e987
diff --git a/packages/sie_server/src/sie_server/adapters/sglang/generation.py b/packages/sie_server/src/sie_server/adapters/sglang/generation.py
index 16bbe4a63..8e64415ed 100644
--- a/packages/sie_server/src/sie_server/adapters/sglang/generation.py
+++ b/packages/sie_server/src/sie_server/adapters/sglang/generation.py
@@ -1085,13 +1085,6 @@ class SGLangGenerationAdapter(GenerationAdapter):
# verdict from the multi-candidate path. Inert for non-guard models.
if self._guard and ((n is not None and n > 1) or (best_of is not None and best_of > 1)):
raise ValueError("guard models support single-candidate generation only (n=1, best_of<=1)")
- # Whether the CLIENT asked for logprobs, captured before the guard
- # forcing below. Guard models force logprobs on internally to compute
- # the verdict threshold; those forced logprobs are an implementation
- # detail and MUST NOT leak to a client that did not request them
- # (GenerationChunk.logprobs contract). The streaming guard intercept
- # uses this to decide whether to strip the forced logprobs.
- client_requested_logprobs = logprobs
# Thresholding needs the verdict-token distribution — force logprobs on
# even if the caller didn't ask. Only affects the n=1 path below.
if self._guard:
@@ -1604,14 +1597,7 @@ class SGLangGenerationAdapter(GenerationAdapter):
verdict = _thresholded_verdict(guard_lp_buffer, self._guard)
if verdict is not None:
guard_resolved = True
- if client_requested_logprobs:
- v_idx = _verdict_position(tuple(guard_lp_buffer))
- remaining = tuple(e for i, e in enumerate(guard_lp_buffer) if i != v_idx) or None
- else:
- remaining = None
- chunk = dataclasses.replace(
- chunk, text_delta=verdict, is_first=True, logprobs=remaining
- )
+ chunk = dataclasses.replace(chunk, text_delta=verdict, is_first=True, logprobs=None)
elif chunk.done:
chunk = dataclasses.replace(
chunk,
@@ -1720,19 +1706,6 @@ def _p_unsafe_from_entry(entry: Any) -> float | None:
return ey / (ey + en) if (ey + en) > 0 else None
-def _verdict_position(chunk_logprobs: Any, scan_positions: int = _GUARD_VERDICT_SCAN_POSITIONS) -> int | None:
- """Index of the first position (within ``scan_positions``) carrying a verdict
- distribution, or ``None``. The consumed/rewritten verdict entry the streaming
- intercept drops from client-requested logprobs.
- """
- if not chunk_logprobs:
- return None
- for idx, entry in enumerate(chunk_logprobs[:scan_positions]):
- if _p_unsafe_from_entry(entry) is not None:
- return idx
- return None
-
-
def _p_unsafe_from_verdict_logprobs(
chunk_logprobs: Any, scan_positions: int = _GUARD_VERDICT_SCAN_POSITIONS
) -> float | None:
diff --git a/packages/sie_server/tests/adapters/test_sglang_generation.py b/packages/sie_server/tests/adapters/test_sglang_generation.py
index 050c1a4ce..2d2c7aaa5 100644
--- a/packages/sie_server/tests/adapters/test_sglang_generation.py
+++ b/packages/sie_server/tests/adapters/test_sglang_generation.py
@@ -2233,10 +2233,8 @@ def test_guard_no_client_logprobs_strips_forced_logprobs_on_error(mock_async_cli
@patch("sie_server.adapters.sglang.generation.httpx.AsyncClient")
-def test_guard_client_logprobs_preserved_minus_verdict_entry(mock_async_client: MagicMock) -> None:
- """M4: client DID request logprobs → logprobs preserved, minus the consumed
- verdict entry (the position that produced the verdict).
- """
+def test_guard_client_logprobs_omitted_for_rewritten_verdict(mock_async_client: MagicMock) -> None:
+ """Rewritten verdicts cannot expose logprobs for discarded tokens."""
# Position 0: leading whitespace (no verdict). Position 1: the "Yes" verdict.
# Both arrive in one event so the buffer holds two entries at resolution; the
# consumed verdict entry (position 1) is dropped, the whitespace entry kept.
@@ -2253,10 +2251,7 @@ def test_guard_client_logprobs_preserved_minus_verdict_entry(mock_async_client:
chunks = _drive_guard(_guard_adapter(), sse_lines, logprobs=True, top_logprobs=20)
verdict_chunk = next(c for c in chunks if c.text_delta)
assert verdict_chunk.text_delta == "Yes"
- # The consumed verdict entry (position 1) is dropped; the leading whitespace
- # entry remains so callers still get the surrounding token metadata.
- assert verdict_chunk.logprobs is not None
- assert [e["token"] for e in verdict_chunk.logprobs] == [" "]
+ assert verdict_chunk.logprobs is None
@patch("sie_server.adapters.sglang.generation.httpx.AsyncClient")
@@ -2538,8 +2533,9 @@ def test_guard_eos_with_verdict_alternatives_is_not_a_verdict(mock_async_client:
assert chunks[-1].text_delta == ""
[email protected]("combined", [False, True])
@patch("sie_server.adapters.sglang.generation.httpx.AsyncClient")
-def test_guard_tail_cannot_change_thresholded_verdict(mock_async_client: MagicMock) -> None:
+def test_guard_tail_cannot_change_thresholded_verdict(mock_async_client: MagicMock, combined: bool) -> None:
lines = [
_guard_event("Yes", [_sglang_token("Yes", -0.1)], [_guard_top(yes=-0.1, no=-3)]),
_guard_event(
@@ -2549,8 +2545,11 @@ def test_guard_tail_cannot_change_thresholded_verdict(mock_async_client: MagicMo
terminal=True,
),
]
+ if combined:
+ lines = lines[-1:]
mock_async_client.return_value = _make_client_with_stream(_FakeStreamingResponse(lines))
- chunks = _drive_guard(_guard_adapter(), lines)
+ chunks = _drive_guard(_guard_adapter(), lines, logprobs=True, top_logprobs=20)
+ assert all(chunk.logprobs is None for chunk in chunks)
assert "".join(c.text_delta for c in chunks) == "Yes"
assert chunks[-1].done
assert chunks[-1].completion_tokens == 2