feat(bandit): Thompson Sampling consumer closing the brand_rules feedback loop#21
Open
ninan-versioning wants to merge 1 commit into
Open
feat(bandit): Thompson Sampling consumer closing the brand_rules feedback loop#21ninan-versioning wants to merge 1 commit into
ninan-versioning wants to merge 1 commit into
Conversation
…back loop New package @shoploop/bandit. Pure TypeScript consumer of the shared BANDIT_QUEUE_PATH: joins PerformanceObservation (Google Ads, PR #18) with RenderProvenance (animated-shorts, PR #19) by (twin_handle, preset), maintains Beta-Bernoulli arms in LibSQL, writes posteriors into the bandit: namespace of brand_rules.json (file-locked, isolation-safe), and emits DRAFT-only BudgetRecommendations to BANDIT_RECOMMENDATIONS_PATH. Distribution paid adapters stay DRAFT — recommendations are visible, never auto-applied. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This was referenced Jun 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
New package
@shoploop/bandit(pure TypeScript, no Python runtime) that closes the architecture loop:Performance Ingest → Thompson Sampling → brand_rules.json. It is a consumer — it learns, writes posteriors, and emits DRAFT-only recommendations. It never calls a paid ad API.It tails the shared
BANDIT_QUEUE_PATH(written by Google Ads PR #18 and animated-shorts PR #19), maintains Beta-Bernoulli arms keyed by(twin_handle, preset)in LibSQL, merges arm posteriors into abandit:namespace insidebrand_rules.json, and writesBudgetRecommendations to a new typedBANDIT_RECOMMENDATIONS_PATH.The 7-step consumer loop (
consumer/loop.ts)Math (
bandit/beta_arm.ts)BetaArm { alpha, beta, reward_sum, pulls }, priorBeta(1,1).α += r; β += (1−r)for rewardr ∈ [0,1].sample(rng)draws Beta via two Marsaglia-Tsang Gamma draws — seedable, so convergence is deterministic under test.BANDIT_DECAY=0.995).Numerical test results
Beta(9,3),E[θ]=0.75sample()empirical var ofBeta(9,3)≈0.0144[0.2, 0.5, 0.8], 2000 updates, seeded RNGprobabilityBestBeta(2,8)vsBeta(8,2)>0.95once17/17 tests pass; typecheck clean.bandit:namespace (only thebanditkey is written; all others preserved){ "bandit": { "version": 1, "updated_at": "2026-06-25T10:06:47.244Z", "arms": { "varitea_jasmine_pearl": { "hero": { "alpha": 4, "beta": 1, "pulls": 3, "last_reward": 1 }, "lifestyle": { "alpha": 1.1, "beta": 3.9, "pulls": 3, "last_reward": 0.02 } } }, "last_recommendation": { "twin_handle": "varitea_jasmine_pearl", "winning_preset": "hero", "confidence": 0.983 } } }Coordination with the distribution PR (the join seam)
PerformanceObservationknows the channelad_id;RenderProvenanceknows the arm. The bridge is provenance'sexternal_id— the dispatched ad_id, stamped by the paid adapter. That adapter (request_budget_shift,list_recent_dispatches) is OUT OF SCOPE here and stays DRAFT. Untilexternal_idis emitted, the joiner falls backproduct_gid ▶ campaign_id(flagged viaJoinedSample.join_kind). Documented inpackages/bandit/DESIGN.mdwith aTODO(distribution-PR)at the seam.Cost
Pure CPU, co-locates on the existing
t4g.small: $0 incremental. LibSQL state <$1. Total ≈ $0/mo.Acceptance
shoploop-bandit onceagainst the fixture queues updates thebrand_rules.jsonbandit namespace and emits exactly one recommendation — verified bytest/consumer.test.tsand by running the built CLI.Test plan
pnpm typecheck— cleanpnpm test— 17/17 pass (beta math, convergence, joiner, rules isolation, e2e)shoploop-bandit once/inspectagainst fixturesexternal_idon the animated-shorts provenance emitter (follow-up, additive)🤖 Generated with Claude Code