Skip to content

fix(fault): each rule draws from its own PRNG (#510) - #584

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/510-per-rule-prng
Aug 6, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/510-per-rule-prng

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

The gap

FaultController rolled probability from one PRNG shared by every rule, so a
rule's outcomes depended on how many rolls the rules before it had taken. A fixed
seed reproduced a run only while the whole sequence of requests was unchanged:
adding a request upstream, or a retry, shifted every later roll — including for
rules that request never touched. That is the opposite of what a deterministic
emulator is for, and the caveat documenting it was the largest asterisk on the
fault tier.

The fix

Each rule gets its own PRNG stream, so its outcome sequence depends only on how
many requests that rule matched. Streams are re-derived whenever a config is
armed, so re-arming resets them exactly as it resets each rule's fired count —
which matters for a fixture that re-arms between phases.

The index identifies a stream, not a hash of the rule's matchers — the decision
#510 asks to be made in the issue. Two rules with identical matchers are legitimate
and useful (fail with one error, then with another), and a matcher hash would make
them share a stream, which is the exact coupling this removes. A matcher hash would
also make a rule's outcomes depend on its matcher text, so editing a path suffix
would silently reshuffle results — a worse surprise than reordering rules, which is
at least visible in the config. The cost is that reordering rules does change their
outcomes, and FaultRule.Probability now says so.

On the FNV mixing, and a correction

Seed and index are mixed through FNV rather than added. My earlier comment on #510
justified this by claiming adjacent seeds give correlated streams in Go's default
source. That claim is wrong and is corrected in
#510 (comment). Measured
over 5000 draws per stream across 60 base seeds, worst-case |Pearson r| between
adjacent indices: seed+i 0.030, FNV 0.036. Neither scheme shows a correlation the
other lacks.

The mixing stays, for reasons that survive measurement: that scrambling is an
unspecified property of math/rand rather than a documented one, so mixing means
per-rule independence does not rest on it; and seed+i makes one controller's
streams collide with an adjacent controller's, since seed+1 at index 0 and seed
at index 1 are the same source. Both the code comment and the test now say what was
actually measured rather than asserting a correlation that does not hold.

Compatibility

A consumer asserting on specific outcomes of a seeded intermediate probability
will see different results and must re-record them. Nothing changes for
probability of 0 or 1, or for a rule bounded by times, which needs no roll at
all. In-repo this cost is zero: every probability that actually reaches
InjectFault in the test suite is 1.0 or 0.0. The one 0.5 (config_test.go)
only asserts the field round-trips through ToFaultConfig and never runs a request.

Tests

Five new tests in emulator/fault_prng_test.go, driving unbounded Probability: 0.5 rules so the assertions are on PRNG traces rather than Times bounds:

  • FaultController: the PRNG is shared across rules, so Probability determinism depends on request ordering #510's acceptance property: rule 2's outcome sequence is unchanged whether
    rule 1 matched 0 or 7 requests, and unchanged when the two are interleaved.
  • Appending a rule does not shift an existing rule's outcomes.
  • A fixed seed still reproduces a run exactly (with non-vacuity checks that the
    trace contains both outcomes), and a different seed does not.
  • Two adjacent rules with the same probability do not trace identically.
  • Re-arming a config resets the streams, and rule 0's stream survives a rule
    appended after it.

Mutation tested, 3 mutants: rngs[i] → rngs[0] (restoring #510) CAUGHT;
the rngs rebuild dropped from UpdateConfig CAUGHT; seed+int64(i) in place
of the FNV mix EQUIVALENT, with the correlation measurement above as its written
proof — the mutant is observably indistinguishable at this scale, which is precisely
what the corrected comment now claims.

Docs

The ordering caveat and its #510 pointers are gone from FaultRule.Probability,
docs/services.md and docs/testing-guide.md, replaced by what now holds: per-rule
streams, keyed by index, reset on arming.

Verification

gofmt -l ., go build ./..., go vet ./..., golangci-lint run ./... (0
issues), make test (race), make docs-reference docs-reference-check docs-versions, npm --prefix docs run build, go vet -C test/e2e ./..., go test -C test/e2e ./... — all clean.

Closes #510

FaultController rolled probability from one PRNG shared by every rule, so a
rule's outcomes depended on how many rolls the rules before it had taken. A fixed
seed therefore reproduced a run only while the whole sequence of requests was
unchanged: adding a request upstream, or a retry, shifted every later roll —
including for rules that request never touched. That is the opposite of what a
deterministic emulator is for.

A rule now has its own stream, so its outcome sequence depends only on how many
requests that rule itself matched. Streams are seeded from the controller's seed
and the rule's index and re-derived whenever a config is armed, so re-arming
resets them exactly as it resets each rule's fired count.

Keying by index rather than by the rule's matchers is deliberate: two rules with
identical matchers are legitimate and useful — fail with one error, then with
another — and a matcher hash would make them share a stream, which is the
coupling this removes. A matcher hash would also make outcomes depend on matcher
text, so editing a path suffix would silently reshuffle results. The cost is that
reordering rules changes their outcomes, which the field now documents.

Seed and index are mixed through FNV rather than added. Measured, both schemes
decorrelate adjacent rules equally well — Go's source scrambles nearby seeds on
its own — so the mixing buys no measurable independence. It is there because that
scrambling is unspecified rather than documented, and because seed+i makes one
controller's streams collide with an adjacent controller's.

Compatibility: a consumer asserting on specific outcomes of a seeded intermediate
probability must re-record them. Nothing changes for probability 0 or 1, or for a
rule bounded by times, which needs no roll at all.
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.

FaultController: the PRNG is shared across rules, so Probability determinism depends on request ordering

1 participant