Skip to content

test(fuzz): seed random per fuzz test for reproducibility - #531

Merged
Sinity merged 1 commit into
masterfrom
feature/fix/fuzz-tests-seeded-random
Apr 27, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/fix/fuzz-tests-seeded-random

Conversation

@Sinity

@Sinity Sinity commented Apr 27, 2026

Copy link
Copy Markdown
Owner

Summary

Addresses one item from #478 ("unseeded fuzz, mock overuse"). Adds an autouse conftest fixture that seeds the `random` module per fuzz test, so iteration-N failures inside 500-1000-iteration loops become reproducible by test name alone.

Ref #478.

Problem

Each fuzz test in `tests/fuzz/` (`fuzz_fts5_escape.py`, `fuzz_path_sanitizer.py`, `fuzz_timestamp.py`, `fuzz_json_parsers.py`) follows the shape:

```python
def test_X_random(self) -> None:
import random
for _ in range(1000):
length = random.randint(1, 200)
data = bytes(random.randint(0, 255) for _ in range(length))
...
```

`pytest-randomly` seeds the global random module at session start, but inside a long-running test the per-iteration random byte stream varies with anything else that touched `random` earlier in the session — and an iteration-N failure has no recipe to reproduce by test name. The audit flagged this as the "unseeded fuzz" gap.

Solution

`tests/fuzz/conftest.py` with an autouse fixture:

```python
@pytest.fixture(autouse=True)
def _seed_fuzz_random(request: pytest.FixtureRequest) -> None:
test_seed = _BASE_SEED ^ (hash(request.node.nodeid) & 0xFFFFFFFF)
random.seed(test_seed)
```

Each fuzz test gets a stable per-nodeid seed, so:

  • Re-running the same test produces the same byte stream.
  • Different fuzz tests get distinct streams (no cross-test repetition).
  • The session-wide `pytest-randomly` seed is irrelevant — re-seeding inside the test function overrides it.

The fixture is scoped to `tests/fuzz/` only; non-fuzz tests are unaffected.

Verification

```
$ nix develop -c pytest -q tests/fuzz/fuzz_fts5_escape.py tests/fuzz/fuzz_path_sanitizer.py tests/fuzz/fuzz_timestamp.py tests/fuzz/fuzz_json_parsers.py
305 passed in 8.59s

$
305 passed in 8.95s

$ nix develop -c devtools verify --quick
verify: all checks passed
```

Re-runs give identical outcomes. Failures (none expected today) would now be reproducible by re-running the same test alone.

Risks and Follow-ups

The fuzz suite isn't auto-collected by default `pytest tests/` (the files are named `fuzz_.py`, not `test_.py`). This change is correct for any explicit invocation; widening collection is out of scope here.

Other items from #478 (operations/archive coverage gap, site/ coverage gap, runtime test mock overuse, mypy gaps) are individually larger and remain open for follow-up PRs.

Closes one item in #478 ("unseeded fuzz, mock overuse").

The fuzz tests in tests/fuzz/ each call random.randint inside loops of
500-1000 iterations. Without a per-test seed, an iteration-N failure
inside the loop has no recipe to reproduce — pytest-randomly's
session-wide seed makes the run ID reproducible, but not each test in
isolation.

Add tests/fuzz/conftest.py with an autouse fixture that seeds random
per test. The seed is a stable XOR of a project base seed and the test's
nodeid hash, so every fuzz test gets a unique deterministic stream
regardless of test order or pytest-randomly's session seed. Re-running
the same test produces the same byte stream — failures become
reproducible by name alone.

Verified by repeating the fuzz suite twice with identical outcomes
(305 passed, identical timing).

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Apr 27, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@Sinity has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 21 minutes and 44 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: fdb3a171-87bc-48f1-8822-253f98f44f3e

📥 Commits

Reviewing files that changed from the base of the PR and between 782c7be and 5c163f8.

📒 Files selected for processing (1)
  • tests/fuzz/conftest.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/fix/fuzz-tests-seeded-random

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5c163f87e2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/fuzz/conftest.py Resolved
@Sinity
Sinity merged commit bc15804 into master Apr 27, 2026
13 checks passed
@Sinity
Sinity deleted the feature/fix/fuzz-tests-seeded-random branch April 27, 2026 05:16
@Sinity

Sinity commented Apr 28, 2026

Copy link
Copy Markdown
Owner Author

🤖 Post-hoc audit note: this PR received bot review feedback verified against current HEAD as part of #544. 1 finding(s) confirmed still present → tracked in #547.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sinity added a commit that referenced this pull request May 7, 2026
## Summary

Addresses one item from #478 ("unseeded fuzz, mock overuse"). Adds an
autouse conftest fixture that seeds the \`random\` module per fuzz test,
so iteration-N failures inside 500-1000-iteration loops become
reproducible by test name alone.

Ref #478.

## Problem

Each fuzz test in \`tests/fuzz/\` (\`fuzz_fts5_escape.py\`,
\`fuzz_path_sanitizer.py\`, \`fuzz_timestamp.py\`,
\`fuzz_json_parsers.py\`) follows the shape:

\`\`\`python
def test_X_random(self) -> None:
    import random
    for _ in range(1000):
        length = random.randint(1, 200)
        data = bytes(random.randint(0, 255) for _ in range(length))
        ...
\`\`\`

\`pytest-randomly\` seeds the global random module at session start, but
inside a long-running test the per-iteration random byte stream varies
with anything else that touched \`random\` earlier in the session — and
an iteration-N failure has no recipe to reproduce by test name. The
audit flagged this as the "unseeded fuzz" gap.

## Solution

\`tests/fuzz/conftest.py\` with an autouse fixture:

\`\`\`python
@pytest.fixture(autouse=True)
def _seed_fuzz_random(request: pytest.FixtureRequest) -> None:
    test_seed = _BASE_SEED ^ (hash(request.node.nodeid) & 0xFFFFFFFF)
    random.seed(test_seed)
\`\`\`

Each fuzz test gets a stable per-nodeid seed, so:
- Re-running the same test produces the same byte stream.
- Different fuzz tests get distinct streams (no cross-test repetition).
- The session-wide \`pytest-randomly\` seed is irrelevant — re-seeding
inside the test function overrides it.

The fixture is scoped to \`tests/fuzz/\` only; non-fuzz tests are
unaffected.

## Verification

\`\`\`
$ nix develop -c pytest -q tests/fuzz/fuzz_fts5_escape.py
tests/fuzz/fuzz_path_sanitizer.py tests/fuzz/fuzz_timestamp.py
tests/fuzz/fuzz_json_parsers.py
305 passed in 8.59s

$ <repeat>
305 passed in 8.95s

$ nix develop -c devtools verify --quick
verify: all checks passed
\`\`\`

Re-runs give identical outcomes. Failures (none expected today) would
now be reproducible by re-running the same test alone.

## Risks and Follow-ups

The fuzz suite isn't auto-collected by default \`pytest tests/\` (the
files are named \`fuzz_*.py\`, not \`test_*.py\`). This change is
correct for any explicit invocation; widening collection is out of scope
here.

Other items from #478 (operations/archive coverage gap, site/ coverage
gap, runtime test mock overuse, mypy gaps) are individually larger and
remain open for follow-up PRs.

Co-authored-by: Claude <[email protected]>
@Sinity

Sinity commented Oct 1, 2026

Copy link
Copy Markdown
Owner Author

Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.

  • 76.F106 / P1: fixed-already at the checked revision: Per-test fuzz seeds now use SHA-256 of the node ID instead of Python's randomized string hash, producing the same random seed in separate interpreters. See tests/fuzz/conftest.py:30.

Only the rows listed above are dispositioned. Held findings remain open.

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.

2 participants