Harden production workflows and agent admission
This commit is contained in:
@@ -25,6 +25,43 @@ def _backoff(attempts: int) -> timedelta:
|
||||
return timedelta(seconds=_BACKOFF_SCHEDULE_SEC[idx])
|
||||
|
||||
|
||||
def _apply_output_policy(payload: dict) -> dict:
|
||||
"""PH-020: enforce a single notifier output policy.
|
||||
|
||||
Per-notifier code keeps its own format-specific shortening (e.g. Telegram
|
||||
300 chars), but a hard cap and include-flag apply consistently before any
|
||||
delivery. Truncation uses a clear marker so receivers can see it happened.
|
||||
"""
|
||||
settings = get_settings()
|
||||
out = dict(payload)
|
||||
text_value = out.get("output")
|
||||
if text_value is None:
|
||||
return out
|
||||
if not settings.notifier_include_output:
|
||||
out["output"] = None
|
||||
out["output_truncated"] = True
|
||||
return out
|
||||
max_bytes = max(settings.notifier_max_output_bytes, 0)
|
||||
encoded = text_value.encode("utf-8")
|
||||
if len(encoded) <= max_bytes:
|
||||
return out
|
||||
# Reserve room for the marker so the final string never exceeds max_bytes.
|
||||
# If max_bytes is smaller than even the marker, drop the body entirely —
|
||||
# this keeps the absolute cap a hard guarantee under any setting.
|
||||
dropped = len(encoded) - max_bytes
|
||||
marker = f"...[truncated {dropped} bytes]"
|
||||
marker_bytes = len(marker.encode("utf-8"))
|
||||
if marker_bytes >= max_bytes:
|
||||
out["output"] = ""
|
||||
out["output_truncated"] = True
|
||||
return out
|
||||
prefix_budget = max_bytes - marker_bytes
|
||||
truncated = encoded[:prefix_budget].decode("utf-8", errors="ignore")
|
||||
out["output"] = f"{truncated}{marker}"
|
||||
out["output_truncated"] = True
|
||||
return out
|
||||
|
||||
|
||||
async def _claim_batch(session, limit: int, lease_sec: int) -> list[dict]:
|
||||
res = await session.execute(
|
||||
text(
|
||||
@@ -107,8 +144,10 @@ async def _process_row(
|
||||
metrics.notifications_total.labels(notifier=row["notifier"], result="discarded").inc()
|
||||
return
|
||||
|
||||
# PH-020: apply central output policy before handing to any notifier.
|
||||
delivery_payload = _apply_output_policy(row["payload"])
|
||||
try:
|
||||
result = await notifier.deliver(row["event_type"], row["payload"])
|
||||
result = await notifier.deliver(row["event_type"], delivery_payload)
|
||||
except Exception as exc: # defensive: notifier should not raise
|
||||
result_ok = False
|
||||
result_perm = False
|
||||
|
||||
Reference in New Issue
Block a user