Skip to content

test(unit): make bedrock collector and secret scan timing tests deterministic - #42405

Merged
kerry-berri merged 3 commits into
mainfrom
litellm_deterministic_collector_and_secret_scan_tests
Sep 22, 2026
Merged

kerry-berri merged 3 commits into
mainfrom
litellm_deterministic_collector_and_secret_scan_tests

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • The CircleCI unit job has been red on main since 2026-09-20 without a code change
  • Two bedrock passthrough tests measure process-wide tracemalloc deltas, so GC timing decides pass or fail
  • The secret detection adversarial test asserted one absolute wall-clock bound that a loaded runner exceeds

How it solves it:

  • Bedrock: compare reachable state of a 4000 and an 8000 token collector, growth must equal the text and stay under a quarter of the extra frame bytes
  • Secret detection: count Python and C calls with cProfile for a quarter-size and a full input, the full one must stay under 5x, no clock

User Flow

Test-only change, no user-facing behavior

Relevant issues

Affected release

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Test-only change, so the proof is the flake history and the mutation checks rather than a live proxy run

Before (2f76aa1)

CircleCI pipeline 90036 on main, unit job: test_converse_stream_collector_keeps_usage_without_retaining_the_stream failed with assert 179721 < (615582 // 4), the same test plus its invoke sibling and test_scan_message_stays_linear_on_adversarial_credential_lines have failed in 5 of the last 8 main runs with measured retention anywhere from 152 KB to 1.5 MB for identical code

After (84fc8ca)

Bedrock tests, baseline: growth, extra text bytes, allowed bound

converse 32000 32000 154000
invoke   32000 32000 231000

Bedrock tests, mutant that disables text coalescing in _CoalescedChunks.add (if text is not None and self._open_text_parts: -> if False:), both tests fail

converse 13702176 32000 154000
invoke   13446176 32000 231000

Secret detection no longer reads the clock. It runs redact_text under cProfile.Profile.runcall on a quarter-size and a full-size input, sums the Python and C call counts, and asserts the large count stays within 1.25 times the size ratio of the small one. The profiler that was active before the helper ran is restored afterwards. The count is a pure function of the input, so it is identical on a loaded runner. Baseline counts (small, large): value-run 342088 13138, quote-run 10883 10887, keyword-run 235200 895230, value-repeat 15635 13496, assignment-flood 3832288 15324119, all green

Secret detection, mutant that adds a quadratic _SHELL_ASSIGNMENT.match pass over the kept lines in _parseable_lines: assignment-flood small 4395769, large 24327372, bound 21978845, red

Type

✅ Test

Caveats (if any)

Low

  • The reachable-size walk counts object headers, so a future collector that stores far more Python objects per token than text would need the bound revisited
  • Call counts cover Python and C calls, not work inside a single regex match or a single inlined comprehension, so catastrophic backtracking in one of the owned patterns would surface through the 90 s pytest timeout CI already applies rather than through this assertion
  • The Bedrock reachable-object walk keeps a local worklist and a seen set; a persistent-structure rewrite would copy the set per visited object and turn a 10 ms traversal into seconds

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/a50faa6aa4104b818a8af665aecafb7e
Open in Devin Desktop: https://app.devin.ai/desktop/session/a50faa6aa4104b818a8af665aecafb7e?variant=devin


Note

Low Risk
Test-only changes; no runtime or production paths are modified.

Overview
Stabilizes flaky unit checks that were failing on CI without product code changes.

Bedrock passthrough stream collectors: test_converse_stream_collector_keeps_usage_without_retaining_the_stream and the invoke-stream sibling no longer use process-wide tracemalloc deltas. They compare reachable object size for collectors fed 4k vs 8k token streams and require retained growth to track added text while staying below a quarter of the extra raw stream bytes. Shared helpers build converse/invoke streams and factor out the comparison.

Enterprise secret detection: test_scan_message_stays_linear_on_adversarial_credential_lines drops a fixed wall-clock bound on redact_text. It profiles quarter-size and full-size adversarial inputs with cProfile, sums call counts (restoring any prior profiler), and asserts the large input stays within 1.25× the scaled small count across five parametrized patterns.

The Bedrock file also includes minor assert/formatting cleanups with no behavior change.

Reviewed by Cursor Bugbot for commit 84fc8ca. Bugbot is set up for automated code reviews on this repo. Configure here.

…ministic

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
devin-ai-integration Bot requested a review from a team September 22, 2026 04:02
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@CLAassistant

CLAassistant commented Sep 22, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@kerry-berri

Copy link
Copy Markdown
Contributor

@greptileai

@kerry-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@greptile-apps

greptile-apps Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; no outstanding findings or new actionable defects remain.

Summary

This test-only PR replaces timing-sensitive regression checks with deterministic measurements.

  • Secret-detection scaling is measured through Python and C call counts while preserving any previously installed profiler.
  • Bedrock collector retention is measured by comparing reachable object growth between 4,000-token and 8,000-token streams.
  • No production behavior is modified.

Reviews (4) · Last reviewed commit: "test(unit): profile the secret scan with..."

@codecov

codecov Bot commented Sep 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…et scan scaling test

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@kerry-berri

Copy link
Copy Markdown
Contributor

@greptileai

@kerry-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

…profiler and tighten the scaling bound

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@kerry-berri

Copy link
Copy Markdown
Contributor

@greptileai

@kerry-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 84fc8ca. Configure here.

@kerry-berri

Copy link
Copy Markdown
Contributor

@greptileai

@chilkotiKartik

Copy link
Copy Markdown

Review for PR #42405: Make bedrock collector and secret scan timing tests deterministic

Test Determinism & Flakiness Remediation

  1. Monotonic Clocks & Virtual Time Mocks:
    Replacing non-deterministic time.sleep() assertions with explicit time mocking (unittest.mock.patch("time.time") / time.monotonic) eliminates CI flakiness in AWS Bedrock secret scanning and telemetry flush timing loops.

  2. Thread Race Condition Safeguards:
    For asynchronous collector flushes, using threading.Event.wait(timeout=...) or condition variables ensures background worker batches flush deterministically before assertions run.

# Deterministic assertion pattern for collector telemetry flushes
def test_bedrock_telemetry_flush(mock_bedrock_client):
    flush_event = threading.Event()
    collector = BedrockTelemetryCollector(on_flush_callback=lambda: flush_event.set())
    
    collector.record_token_usage(model="anthropic.claude-3-sonnet", tokens=120)
    assert flush_event.wait(timeout=2.0), "Collector failed to flush metrics within timeout window"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants