mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-23 22:07:29 +00:00
* feat(lens): record run steps, trigger and exact run windows on investigations * feat(lens): scan only traces since the last run and keep a capped step log * feat(lens): log each analysis model call with its model, tokens and cost * feat(lens): accept agent and time window on run now and add turn-all-on * test(lens): cover new-traces-only windows, manual runs and the step cap * test(lens): cover run now overrides and turning paused investigations on * chore(ui): regenerate api types for lens run steps and run now options * feat(lens): group open findings into one row per problem with agent filters * test(lens): cover the findings table grouping, filters and schedule labels * feat(lens): add a findings table across all investigations * feat(lens): show a live step feed with the model behind each call * feat(lens): offer turning all paused investigations on * feat(lens): let run now pick an agent and time window * test(lens): cover run now request building * feat(lens): show the step feed and run now dialog on an investigation * feat(lens): open run now choices instead of running immediately * feat(lens): open findings first and peek a finding without leaving the table * feat(lens): fold investigation actions into the findings toolbar * feat(lens): show each investigation's schedule and open findings * feat(lens): name the agent on a finding * feat(lens): keep new investigations watching every 15 minutes by default * feat(lens): show the watch schedule outside advanced options * feat(lens): send run now options and turn-all-on from the dashboard * test(lens): give demo runs steps and a trigger * feat(lens): let the findings table fill the screen * test(lens): add steps and trigger to progress fixtures * test(lens): add steps and trigger to status fixtures * test(lens): cover the default watch schedule in setup * test(lens): cover run now choices from an investigation * test(lens): open saved investigations from the manage view * feat(lens): use one tab bar for traces, findings and investigations * feat(lens): place page actions on the lens tab row * feat(lens): drop the nested tabs and edit investigations in place * feat(lens): show investigations as a table with run now and edit * feat(lens): name each findings row for screen readers * test(lens): open saved investigation links on findings * test(lens): reach findings and investigations from the top tabs * fix(lens): mark run now jobs manual and keep them from moving the scheduled scan * fix(lens): keep run now since-last-run windows even with an agent override * test(lens): cover that manual runs never skip scheduled traces * test(lens): cover run now windows with agent and lookback overrides * fix(lens): group findings without Map.groupBy and expose sampled runs * fix(lens): open older findings and review every merged copy from one row * fix(lens): hide edit and run now from read-only viewers * chore(lens): drop restating comments from the findings table * chore(lens): drop restating comments from the step feed * chore(lens): drop restating comments from the paused banner * chore(lens): drop restating comments from header actions * chore(lens): drop restating comments from run now * test(lens): cover merged findings and read-only investigation rows * fix(lens): record a model step even when the response has no usage * test(lens): cover model steps with and without reported usage * fix(lens): keep a merged finding open when one of its updates fails * refactor(lens): accept update results from the investigations view * refactor(lens): accept update results in investigation actions * test(lens): cover retrying a merged finding after a failed update
347 lines
15 KiB
Python
347 lines
15 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
from functools import reduce
|
|
from typing import Final
|
|
|
|
import pytest
|
|
|
|
from litellm.proxy.lens.models import (
|
|
MAX_STEPS,
|
|
AgentTestCase,
|
|
Check,
|
|
Evidence,
|
|
FindingDraft,
|
|
IssueBrief,
|
|
Lens,
|
|
LensSettings,
|
|
Scope,
|
|
Step,
|
|
Worker,
|
|
)
|
|
from litellm.proxy.lens.state import (
|
|
add_step,
|
|
can_access,
|
|
claim_job,
|
|
current_job,
|
|
merge_finding,
|
|
next_scan_start,
|
|
queue_job,
|
|
renew_budget,
|
|
)
|
|
|
|
NOW: Final = datetime(2026, 1, 15, tzinfo=timezone.utc)
|
|
|
|
|
|
def lens() -> Lens:
|
|
return Lens(
|
|
id="lens",
|
|
scope=Scope(team_id="alpha"),
|
|
settings=LensSettings(
|
|
name="Research", model="analysis", checks=(Check(id="retries", instruction="Find unrecovered retries"),)
|
|
),
|
|
created_at=NOW,
|
|
next_run_at=NOW,
|
|
budget_month="2026-01",
|
|
)
|
|
|
|
|
|
def worker(team: str = "alpha", identity: str = "worker") -> Worker:
|
|
return Worker(id=identity, name=identity, scope=Scope(team_id=team), last_seen=NOW)
|
|
|
|
|
|
def finding(execution: str) -> FindingDraft:
|
|
return FindingDraft(
|
|
title="Repeated failed searches",
|
|
description="The agent repeats the same failed search",
|
|
check_id="retries",
|
|
evidence=(Evidence(execution_id=execution, span_id="span", quote="timeout"),),
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("viewer", "target", "allowed"),
|
|
(
|
|
(Scope(team_id="alpha"), Scope(team_id="beta"), False),
|
|
(Scope(team_id="alpha"), Scope(all_teams=True), False),
|
|
(Scope(all_teams=True), Scope(team_id="alpha"), True),
|
|
(Scope(api_key_hash="one"), Scope(api_key_hash="two"), False),
|
|
(Scope(team_id="alpha", api_key_hash="one"), Scope(team_id="alpha"), True),
|
|
),
|
|
)
|
|
def test_scope_never_crosses_another_team_or_key(viewer: Scope, target: Scope, allowed: bool) -> None:
|
|
assert can_access(viewer, target) is allowed
|
|
|
|
|
|
def test_queue_is_idempotent_and_settings_are_frozen() -> None:
|
|
original: Final = lens()
|
|
queued: Final = queue_job(original, NOW, "job")
|
|
edited: Final = queued.model_copy(
|
|
update={"settings": original.settings.model_copy(update={"model": "replacement"})}
|
|
)
|
|
|
|
assert queue_job(edited, NOW, "duplicate") is edited
|
|
assert edited.jobs[0].settings.model == "analysis"
|
|
assert (edited.jobs[0].start, edited.jobs[0].end) == (
|
|
NOW - timedelta(hours=24),
|
|
NOW - timedelta(minutes=2),
|
|
)
|
|
|
|
|
|
def test_one_off_overrides_do_not_change_saved_monitoring_settings() -> None:
|
|
original: Final = lens()
|
|
override: Final = original.settings.model_copy(
|
|
update={"sample_percent": 10, "sample_size": None, "concurrency": 3, "lookback_hours": 72}
|
|
)
|
|
queued: Final = queue_job(original, NOW, "one-off", settings=override)
|
|
assert queued.settings == original.settings
|
|
assert queued.jobs[0].settings == override
|
|
assert queued.jobs[0].start == NOW - timedelta(hours=72)
|
|
later: Final = queue_job(original, NOW + timedelta(days=1), "scheduled")
|
|
assert later.jobs[0].settings == original.settings
|
|
assert later.jobs[0].start == NOW
|
|
|
|
|
|
def test_behavior_description_is_sufficient_without_separate_checks() -> None:
|
|
settings: Final = LensSettings(name="Behavior", model="analysis", context="Answer using cited sources")
|
|
assert tuple(c.id for c in settings.analysis_checks) == ("expected_behavior",)
|
|
assert settings.sample_size is None
|
|
assert settings.sample_percent == 100
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"field,value",
|
|
(
|
|
("sample_percent", 0),
|
|
("sample_percent", 101),
|
|
("sample_size", 0),
|
|
("concurrency", 0),
|
|
("lookback_hours", 0),
|
|
),
|
|
)
|
|
def test_invalid_selection_and_parallelism_are_rejected(field: str, value: int) -> None:
|
|
from pydantic import ValidationError
|
|
|
|
with pytest.raises(ValidationError):
|
|
LensSettings.model_validate({**lens().settings.model_dump(), field: value})
|
|
|
|
|
|
def test_lease_prevents_double_claim_and_expires_with_bounded_retries() -> None:
|
|
queued: Final = queue_job(lens(), NOW, "job")
|
|
first: Final = claim_job(queued, worker(), NOW)
|
|
assert claim_job(first, worker(identity="second"), NOW) is first
|
|
assert claim_job(first, worker(team="beta"), NOW + timedelta(minutes=6)) is first
|
|
second: Final = claim_job(first, worker(identity="second"), NOW + timedelta(minutes=6))
|
|
assert second.jobs[0].worker_id == "second"
|
|
third: Final = claim_job(second, worker(), NOW + timedelta(minutes=12))
|
|
exhausted: Final = claim_job(third, worker(), NOW + timedelta(minutes=18))
|
|
assert current_job(exhausted) is None
|
|
assert exhausted.jobs[0].status == "failed"
|
|
assert exhausted.next_run_at > NOW + timedelta(minutes=18)
|
|
|
|
|
|
def test_replaying_evidence_does_not_reopen_but_new_occurrence_does() -> None:
|
|
from litellm.proxy.lens.state import snapshot_finding
|
|
|
|
original: Final = lens()
|
|
resolved: Final = merge_finding(original, finding("run1"), 1, NOW).model_copy(update={"status": "resolved"})
|
|
reviewed: Final = original.model_copy(update={"findings": (resolved,)})
|
|
assert merge_finding(reviewed, finding("run1"), 1, NOW).status == "resolved"
|
|
comparison: Final = finding("run1").model_copy(
|
|
update={
|
|
"evidence": (
|
|
*finding("run1").evidence,
|
|
Evidence(execution_id="recovered", span_id="step", quote="Recovered", role="counterexample"),
|
|
)
|
|
}
|
|
)
|
|
compared: Final = merge_finding(reviewed, comparison, 1, NOW + timedelta(days=1))
|
|
assert compared.status == "resolved"
|
|
assert compared.occurrences == ("run1",)
|
|
assert compared.last_seen == resolved.last_seen
|
|
assert compared.evidence[-1].role == "counterexample"
|
|
assert snapshot_finding(reviewed, comparison, 1, NOW).occurrences == ("run1",)
|
|
recurring: Final = merge_finding(reviewed, finding("run2"), 1, NOW + timedelta(days=1))
|
|
assert recurring.status == "open"
|
|
assert recurring.occurrences == ("run1", "run2")
|
|
dismissed: Final = reviewed.model_copy(update={"findings": (resolved.model_copy(update={"status": "dismissed"}),)})
|
|
assert merge_finding(dismissed, finding("run2"), 1, NOW).status == "dismissed"
|
|
|
|
|
|
def test_monthly_budget_renews_without_erasing_job_costs() -> None:
|
|
spent: Final = queue_job(lens(), NOW, "job").model_copy(update={"spent": 12})
|
|
renewed: Final = renew_budget(spent, datetime(2026, 2, 1, tzinfo=timezone.utc))
|
|
assert renewed.spent == 0
|
|
assert renewed.jobs == spent.jobs
|
|
assert renew_budget(spent, NOW) is spent
|
|
|
|
|
|
@pytest.mark.parametrize("hours", (24, 168, 720, 4800, 8760))
|
|
def test_first_scan_covers_the_configured_lookback_window(hours: int) -> None:
|
|
original: Final = lens()
|
|
configured: Final = original.model_copy(
|
|
update={"settings": LensSettings.model_validate({**original.settings.model_dump(), "lookback_hours": hours})}
|
|
)
|
|
first: Final = queue_job(configured, NOW, "first")
|
|
assert first.jobs[0].start == NOW - timedelta(hours=hours)
|
|
assert first.jobs[0].trigger == "schedule"
|
|
|
|
|
|
def test_later_scheduled_scans_only_cover_traces_since_the_last_scan() -> None:
|
|
resumed: Final = lens().model_copy(update={"last_scan_at": NOW - timedelta(hours=1)})
|
|
job: Final = queue_job(resumed, NOW, "next").jobs[0]
|
|
assert job.start == NOW - timedelta(hours=1)
|
|
assert job.end == NOW - timedelta(minutes=2)
|
|
|
|
|
|
def test_a_scan_after_a_long_outage_never_reaches_past_the_lookback_window() -> None:
|
|
stale: Final = lens().model_copy(update={"last_scan_at": NOW - timedelta(days=400)})
|
|
assert queue_job(stale, NOW, "next").jobs[0].start == NOW - timedelta(hours=stale.settings.lookback_hours)
|
|
|
|
|
|
def test_run_now_with_an_exact_window_scans_that_window_and_is_marked_manual() -> None:
|
|
window: Final = (NOW - timedelta(hours=5), NOW - timedelta(hours=3))
|
|
job: Final = queue_job(lens(), NOW, "manual", window=window, trigger="manual").jobs[0]
|
|
assert (job.start, job.end) == window
|
|
assert job.trigger == "manual"
|
|
|
|
|
|
def test_steps_keep_only_the_most_recent_entries() -> None:
|
|
job: Final = queue_job(lens(), NOW, "job").jobs[0]
|
|
steps: Final = tuple(Step(at=NOW, kind="stage", label=f"step {i}") for i in range(MAX_STEPS + 5))
|
|
grown: Final = reduce(add_step, steps, job)
|
|
assert len(grown.steps) == MAX_STEPS
|
|
assert grown.steps[0].label == "step 5"
|
|
assert grown.steps[-1].label == f"step {MAX_STEPS + 4}"
|
|
|
|
|
|
def test_finding_keeps_uncertainty_separate_from_the_main_summary() -> None:
|
|
draft: Final = finding("run1").model_copy(update={"limitation": "The final response was not recorded."})
|
|
saved: Final = merge_finding(lens(), draft, 1, NOW)
|
|
assert saved.limitation == draft.limitation
|
|
assert saved.description == draft.description
|
|
|
|
|
|
def issue_brief(problem: str) -> IssueBrief:
|
|
return IssueBrief(
|
|
problem=problem,
|
|
user_goal="Open a pull request",
|
|
what_happened="The agent replied that it lacked repository access",
|
|
test_cases=(AgentTestCase(input="Open a PR fixing the typo", expected="A PR URL is returned"),),
|
|
)
|
|
|
|
|
|
def test_issue_brief_survives_merges_and_refreshes_only_when_a_new_one_is_found() -> None:
|
|
draft: Final = finding("run1").model_copy(update={"brief": issue_brief("No repo tool")})
|
|
first: Final = merge_finding(lens(), draft, 1, NOW)
|
|
assert first.brief == issue_brief("No repo tool")
|
|
reviewed: Final = lens().model_copy(update={"findings": (first,)})
|
|
assert merge_finding(reviewed, finding("run2"), 2, NOW).brief == first.brief
|
|
refreshed: Final = finding("run2").model_copy(update={"brief": issue_brief("Token expired")})
|
|
assert merge_finding(reviewed, refreshed, 2, NOW).brief == refreshed.brief
|
|
|
|
|
|
def test_issue_brief_requires_a_test_case() -> None:
|
|
from pydantic import ValidationError
|
|
|
|
with pytest.raises(ValidationError):
|
|
IssueBrief.model_validate({**issue_brief("No repo tool").model_dump(), "test_cases": ()})
|
|
|
|
|
|
@pytest.mark.parametrize("interval", (1, 2, 37, 90, 10080))
|
|
def test_custom_schedule_does_not_overlap_an_active_scan(interval: int) -> None:
|
|
original: Final = lens()
|
|
settings: Final = LensSettings.model_validate({**original.settings.model_dump(), "interval_minutes": interval})
|
|
configured: Final = original.model_copy(update={"settings": settings})
|
|
running: Final = claim_job(queue_job(configured, NOW, "first"), worker(), NOW)
|
|
assert queue_job(running, NOW + timedelta(minutes=interval), "second") is running
|
|
|
|
|
|
@pytest.mark.parametrize("interval", (0, -1, 1.5))
|
|
def test_invalid_schedule_is_rejected(interval: float) -> None:
|
|
from pydantic import ValidationError
|
|
|
|
with pytest.raises(ValidationError):
|
|
LensSettings.model_validate({**lens().settings.model_dump(), "interval_minutes": interval})
|
|
|
|
|
|
def test_batch_snapshot_keeps_feedback_identity_and_only_current_evidence() -> None:
|
|
from litellm.proxy.lens.state import snapshot_finding
|
|
|
|
original: Final = lens()
|
|
dismissed: Final = merge_finding(original, finding("old-run"), 1, NOW).model_copy(
|
|
update={"status": "dismissed", "reason": "Expected recovery"}
|
|
)
|
|
saved: Final = original.model_copy(update={"findings": (dismissed,)})
|
|
draft: Final = finding("new-run").model_copy(
|
|
update={"title": "Updated wording", "existing_finding_id": dismissed.id}
|
|
)
|
|
snapshot: Final = snapshot_finding(saved, draft, 2, NOW + timedelta(days=1))
|
|
assert snapshot.id == dismissed.id
|
|
assert snapshot.status == "dismissed"
|
|
assert snapshot.reason == "Expected recovery"
|
|
assert snapshot.occurrences == ("new-run",)
|
|
assert snapshot.title == "Updated wording"
|
|
assert snapshot.evidence == draft.evidence
|
|
assert snapshot.revision == 2
|
|
|
|
|
|
@pytest.mark.parametrize("explicit_reference", (False, True))
|
|
def test_issue_and_pattern_with_same_title_keep_independent_feedback(explicit_reference: bool) -> None:
|
|
from litellm.proxy.lens.state import snapshot_finding
|
|
|
|
original: Final = lens()
|
|
issue: Final = merge_finding(original, finding("old"), 1, NOW).model_copy(
|
|
update={"status": "dismissed", "reason": "Expected retry"}
|
|
)
|
|
reviewed: Final = original.model_copy(update={"findings": (issue,)})
|
|
draft: Final = finding("new").model_copy(
|
|
update={"kind": "pattern", "existing_finding_id": issue.id if explicit_reference else None}
|
|
)
|
|
pattern: Final = merge_finding(reviewed, draft, 1, NOW)
|
|
assert pattern.id != issue.id
|
|
assert pattern.kind == "pattern"
|
|
assert pattern.status == "open" and pattern.reason == ""
|
|
assert pattern.occurrences == ("new",)
|
|
assert snapshot_finding(reviewed, draft, 1, NOW).id == pattern.id
|
|
both: Final = reviewed.model_copy(update={"findings": (issue, pattern)})
|
|
assert merge_finding(both, finding("again"), 1, NOW).id == issue.id
|
|
assert merge_finding(both, finding("again"), 1, NOW).status == "dismissed"
|
|
|
|
|
|
def test_legacy_finding_identity_preserves_feedback_only_for_same_kind_and_check() -> None:
|
|
import hashlib
|
|
|
|
original: Final = lens()
|
|
draft: Final = finding("old")
|
|
legacy_id: Final = hashlib.sha256(f"{original.id}:{draft.check_id}:{draft.title.lower()}".encode()).hexdigest()[:24]
|
|
legacy: Final = merge_finding(original, draft, 1, NOW).model_copy(
|
|
update={"id": legacy_id, "status": "dismissed", "reason": "Accepted"}
|
|
)
|
|
reviewed: Final = original.model_copy(update={"findings": (legacy,)})
|
|
repeated: Final = merge_finding(reviewed, finding("new"), 2, NOW)
|
|
assert repeated.id == legacy_id
|
|
assert repeated.status == "dismissed" and repeated.reason == "Accepted"
|
|
other: Final = finding("new").model_copy(update={"check_id": "different", "existing_finding_id": legacy_id})
|
|
separate: Final = merge_finding(reviewed, other, 2, NOW)
|
|
assert separate.id != legacy_id
|
|
assert separate.status == "open" and separate.reason == ""
|
|
|
|
|
|
def test_only_successful_scheduled_scans_move_the_next_scan_forward() -> None:
|
|
previous: Final = lens().model_copy(update={"last_scan_at": NOW - timedelta(hours=3)})
|
|
scheduled: Final = queue_job(previous, NOW, "scheduled").jobs[0]
|
|
manual: Final = queue_job(
|
|
previous, NOW, "manual", window=(NOW - timedelta(hours=2), NOW - timedelta(hours=1)), trigger="manual"
|
|
).jobs[0]
|
|
assert next_scan_start(previous, scheduled, failed=False) == scheduled.end
|
|
assert next_scan_start(previous, scheduled, failed=True) == previous.last_scan_at
|
|
assert next_scan_start(previous, manual, failed=False) == previous.last_scan_at
|
|
|
|
|
|
@pytest.mark.parametrize("field", ("lookback_hours", "interval_minutes"))
|
|
def test_calendar_overflow_is_rejected_without_the_old_history_and_interval_caps(field: str) -> None:
|
|
from pydantic import ValidationError
|
|
|
|
accepted: Final = LensSettings.model_validate({**lens().settings.model_dump(), field: 100000})
|
|
assert getattr(accepted, field) == 100000
|
|
with pytest.raises(ValidationError, match="supported calendar range"):
|
|
LensSettings.model_validate({**lens().settings.model_dump(), field: 10**30})
|