Skip to content

examples: drive both examples with the bandit module - #11

Merged
sshaplygin merged 1 commit into
mainfrom
v04/a2-drop-stitchfix
Aug 27, 2026
Merged

examples: drive both examples with the bandit module#11
sshaplygin merged 1 commit into
mainfrom
v04/a2-drop-stitchfix

Conversation

@sshaplygin

Copy link
Copy Markdown
Owner

Both examples carried their own ~120-line Thompson Sampling adapter over
stitchfix/mab, written before this repository shipped a bandit of its own.
The first thing a reader met was a page of glue rather than the cache, and it
pulled gonum and golang.org/x/exp into example modules that need neither.
Both now call bandit.NewThompson.

The basic example's adapter was broken

Its GetRewards ranged the per-arm map and used the PolicyType as a slice
index:

for i, arm := range crs.stats {        // i is a PolicyType, not an index
    distributions[i] = mab.Beta(...)   // LFU == 2, len(distributions) == 2

so the first SelectPolicy after an epoch elapsed panicked with an index out
of range. It compiled, and was never run.

Both examples take -smoke

Build the cache, drive 100 keys through it, report, exit 0, without serving.
CI runs it for both examples, and for migration across all three strategies —
which demonstrates the strategies actually differ rather than only that the
binary starts:

strategy=cold     LRU -> LFU    0/100 keys readable after the switch
strategy=warm     LRU -> LFU  100/100 keys readable after the switch
strategy=gradual  LRU -> LFU  100/100 keys readable after the switch

Neither example's policy set, epoch settings nor cache construction changed;
only the bandit did.

Docs

docs/design.md loses the pointer to the removed adapter and gains a short
illustrative skeleton for wrapping an outside bandit — checked line by line
against interfaces.go — with the two traps that catch people: consuming
randomness while ranging a map breaks a seeded run, and an arm that saw no
requests is not an arm that scored zero. docs/getting-started.md drops
stitchfix/mab from its references.

Verification

make all                                  -> Release checks passed
git ls-files | xargs grep -l stitchfix    -> no matches
grep -c 'gonum\|x/exp' examples/*/go.sum  -> 0
go run . -smoke                           -> exit 0, both examples

Both examples carried their own ~120-line Thompson Sampling adapter over
stitchfix/mab, written before this repository shipped a bandit of its own.
That made the first thing a reader meets a page of glue rather than the
cache, and it pulled gonum and golang.org/x/exp into an example module
that needs neither. Both now call bandit.NewThompson.

The basic example's adapter was also broken. Its GetRewards ranged the
per-arm map and used the PolicyType as a slice index:

    for i, arm := range crs.stats {        // i is a PolicyType, not an index
        distributions[i] = mab.Beta(...)   // LFU == 2, len(distributions) == 2

so the first SelectPolicy after an epoch elapsed would panic with an index
out of range. It compiled and was never run, which is the whole reason for
the -smoke flag added here.

Both examples take -smoke: build the cache, drive 100 keys through it,
report, exit 0, without serving. CI runs it for both, and for migration
across all three strategies -- which shows the strategies actually differ:

    strategy=cold     LRU -> LFU    0/100 keys readable after the switch
    strategy=warm     LRU -> LFU  100/100 keys readable after the switch
    strategy=gradual  LRU -> LFU  100/100 keys readable after the switch

Neither example's policy set, epoch settings nor cache construction
changed; only the bandit did.

docs/design.md loses the pointer to the removed adapter and gains a short
illustrative skeleton for wrapping an outside bandit, with the two traps
that catch people: consuming randomness while ranging a map breaks a
seeded run, and an arm with no requests is not an arm that scored zero.
@sshaplygin
sshaplygin merged commit 121b08f into main Aug 27, 2026
28 of 29 checks passed
@sshaplygin
sshaplygin deleted the v04/a2-drop-stitchfix branch August 27, 2026 20:08
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.

1 participant