Most software I've built answers questions. This one makes choices: approve or refer, hide a comment or answer it, post a Reel at 19:00 or a carousel at lunch. When software makes choices, the part that matters most isn't the choice. It's whether you can see why it chose, and whether it gets better or just gets more confident.
So the rule for the decision engine was simple: every decision has to show its work.
What it is
The decision engine is a TypeScript service (Fastify, PostgreSQL, React UI) with four building blocks:
- Decisions: versioned rule tables. A rule is a condition tree (
all,any,not, field comparisons) and an output. Rules run by priority, and a decision returns the first match, all matches, or the matches merged. - Flows: small programs made of steps: run a decision, score with a model, ask an AI to pick a label, set a value, branch, return. Every run is stored with a trace of each step.
- Models: binary classifiers. A built-in logistic regression whose weights explain every prediction, or gradient-boosted trees trained by a small Python service. The engine predicts with the trees itself, so serving never needs Python.
- A learn loop: you record what actually happened on a run. That outcome flows down to the predictions and rule hits the run made, and a model retrains itself after N new labels.
It also has the dull parts a real system needs: companies kept apart with row-level security, roles, an event log with signed webhooks, an error table, and a health page.
Every change is a version, and none goes live blind
The part I'm proudest of is the least flashy. Every save of a rule or flow is a new, immutable version. Each company picks how changes go live:
- auto: straight away.
- checks: the flow's saved example inputs are dry-run against the new version first. It goes live only if they all still complete, and you get a list of which outputs changed.
- manual: it stays a draft until someone with publish rights presses Publish.
Retrained models get the same treatment. With the safety gate on, a new version goes live only if it isn't worse than the current one on trusted examples: an audit set you mark as trusted, which the model never trains on. If it is worse, it's kept as rejected and the old version keeps serving. Rolling back is just publishing an older version again.
That's what turns "the model retrained overnight" from a scary sentence into a boring one.
The AI step is a step, not the engine
A flow can ask Claude (or Jev, TypeSafe's decision model) to classify something: "is this comment a question, a complaint or spam?". The AI must answer with one of the labels you gave it, plus a confidence. Claude also gives a one-sentence reason that lands in the run's trace.
The record is passed as data, and the model is told to ignore any instructions inside it. If there's no key, or the call fails, the step falls back to a label you chose, marked as a fallback in the trace. Dry runs and backtests never call the AI at all. The AI is one input the engine can branch on, never the thing that decides on its own.
The hardest test I gave it: growing an Instagram account
To find out whether the learning was real, I built something on top of it that has to learn fast or fail: growth engines for a brand-new Instagram account. Five background engines plan posts, generate the media, publish, read the insights and triage comments. Comments go through an ordinary flow: a spam model, then a rule table, then a reply.
The planner is where the interesting maths lives. Each account gets its own Bayesian linear regression over what a post is made of: format, topic, hook, time of day, posting hour, weekday or weekend. Each measured post teaches it about every attribute at once, so it's useful after 5 to 10 posts, not hundreds.
To choose, it uses Thompson sampling. For every free slot it draws one plausible version of the world from what it believes, and picks the best post in that world. Things it's unsure about win some draws and get tried. Things it knows are weak rarely do.
And every post carries its reason and how sure the engine is: Exploring, Fairly sure or Proven for you. The draw is stored with the post, so any choice can be replayed later.
How I tested it
You can't A/B test a new account's first eight weeks on real people a few hundred times. So the test is a simulation: 500 simulated accounts starting from zero followers, 12 weeks, posting 3, 5 or 7 times a week. Each account is run by the engine and by three baselines: random choices, a fixed best-practice plan (Reels in the evening, topics and hooks in turn), and an oracle that knows the simulated audience's real tastes.
The score is regret: how many follows you lost compared with the oracle. Lower is better. This test runs in CI on every push, and it fails the build if the engine isn't ahead of both baselines by week 8.
Median regret at week 8 (7 Nov run), engine vs best-practice vs random:
- 3 posts a week: 6,977 vs 9,471 vs 11,211
- 5 posts a week: 9,853 vs 23,196 vs 32,241
- 7 posts a week: 7,203 vs 17,367 vs 42,020
- All accounts: 7,733 vs 14,295 vs 25,061
A second planner I added later (information-directed sampling, which weighs what a post would teach against what it would cost) did better still: 5,017 overall.
Those are good numbers. That's exactly why I didn't trust them.
Then I audited it, and it was grading its own homework
On 15 November I did a read-only review with one question: where does this give wrong answers, learn the wrong thing, or claim more than it can back up? Not "does the code match the spec". The code did match the spec. That was the problem.
The headline finding was uncomfortable: the validation was circular. The simulated audience only cared about three things: topic, Reels, and two peak hours. I'd then tuned the engine's starting beliefs until it won on that simulator. The engine also learns about 15 creative attributes (hook, length, call to action, tone and more), and none of them had any effect in the simulator. So the test couldn't tell whether learning them helped or hurt.
Some of the other findings:
- The "why" lines could be wrong. A post's reason line called a value proven when it beat the account's usual. On a growing account the usual trails behind, so almost everything looked proven there and average on the "What works" page. Fixed the same day: every claim is now judged against the average post.
- Untried options could never win on curiosity. A value with fewer than two posts was encoded as "no effect, no uncertainty", so the sampler had no reason to try it. Hooks locked in early. Fixed, with a test that an untried hook wins some draws.
- Too many comparisons. With dozens of attributes, something always looks like a winner by luck. A value now has to clear a stricter bar (z = 2.5, not 1.96) before the engine names it as one.
- Selective labels. In a decision flow, outcomes usually come back only for the cases the flow approved, so a model can learn from a biased sample. That one is still open, and I've written it down as open.
What I took from it
Explanations are claims, and claims need tests. I spent weeks making sure the maths was right and almost no time checking whether the sentence on the screen matched it. Now the reason line, the weekly report and the "What works" page all use the same comparison, and the strict threshold applies wherever the engine names a winner.
Audit the test as hard as the model. A model that beats your simulator tells you about your simulator. Simulated growth compounds faster than real growth, so I read the regret table as a comparison between planners, never as a forecast. No tool can promise a follower count, and this one doesn't.
Write "still open" in the docs. The spec has a Known gaps section, and the audit lists what's fixed and what isn't. It keeps me honest, and it's the first thing I'd want to read about anyone else's system.
The engine is about 48,000 lines of TypeScript with 67 test files. None of that is what makes it trustworthy. What makes it trustworthy is that every decision can be replayed, every model change has to pass a gate, and the places where it still guesses are labelled as guesses.
- Server: TypeScript on Node.js 22 (runs .ts directly), Fastify 5.
- Data: PostgreSQL 16 with row-level security per company; events via an event table + LISTEN/NOTIFY, Server-Sent Events and HMAC-signed webhooks.
- Models: built-in logistic regression on a worker thread, or LightGBM trees from a Python trainer service, served by the engine as plain JSON trees.
- Learning planner: per-account Bayesian linear regression with recency weighting (×0.97 per week), Thompson sampling, a designed 5-post starter test, and an information-directed planner switched on per account only when replay on its own posts supports it.
- Safety: immutable versions, publishing modes (auto / checks / manual), a trusted-example gate for retrained models, AI steps with labelled fallbacks.
- Evaluation: backtests on labelled data, a 500-account regret simulation in CI, a 20,000-account stress test over HTTP.