<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Posts on redgeoff</title><link>https://redgeoff.com/posts/</link><description>Recent content in Posts on redgeoff</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Mon, 09 Nov 2026 10:58:00 -0800</lastBuildDate><atom:link href="https://redgeoff.com/posts/index.xml" rel="self" type="application/rss+xml"/><item><title>My tests all passed. The broker disagreed.</title><link>https://redgeoff.com/posts/tests-passed-broker-disagreed/</link><pubDate>Mon, 09 Nov 2026 10:58:00 -0800</pubDate><guid>https://redgeoff.com/posts/tests-passed-broker-disagreed/</guid><description>&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/tests-passed-broker-disagreed/tests-hero.jpg"&#10;&#9;&#9;&#9;alt="Two cards under the headline My tests passed. The broker disagreed. The left card, My tests, lists lint, type checks, unit tests, integration tests, a 100-run soak and snapshots, all marked pass. The right card, Me vs. the broker, shows positions and cash as matching and corporate actions as differing, stamped MISMATCH in red"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: author, drawn with code&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;This is a story about the bugs my tests couldn&amp;rsquo;t find, and the check that found them&#10;anyway. TopSet is the trading system I built and run on my own accounts. It picks&#10;stocks with machine-learning models, rebalances on a schedule and places real orders&#10;through two brokers, Alpaca and Tradier. AI agents wrote most of the code, and&#10;&lt;a href="https://redgeoff.com/posts/ai-agents-test-gate/"&gt;my first post about it&lt;/a&gt; described the test gate that&#10;decides what merges: thousands of tests, a weekly 100-run soak and a few results I&#10;refuse to let change quietly.&lt;/p&gt;&#10;&lt;p&gt;That gate still matters, but over time I learned that each kind of test answers a&#10;different question, and none of them answers the one I care about most: do my records&#10;match the broker&amp;rsquo;s? A mock broker can only repeat what I believe about the real one.&#10;The bugs that got furthest were the ones where my belief was wrong, and the thing that&#10;caught them was a boring comparison that runs every day after trading.&lt;/p&gt;&#10;&lt;p&gt;So let&amp;rsquo;s take a look at what each layer caught, and then at what only the broker could&#10;tell me.&lt;/p&gt;&#10;&lt;h2 id="what-question-does-each-test-answer"&gt;What Question Does Each Test Answer?&lt;/h2&gt;&#10;&lt;p&gt;Here is the short version, with the longer stories below.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/tests-passed-broker-disagreed/test-layers.png"&#10;&#9;&#9;&#9;alt="Seven rows, each a kind of test and the question it answers. Lint and type checks: is the code well formed? Unit tests: does each piece do what I believed it should? Integration tests: do the pieces work together when the timing is awkward? The soak: does it work every time, or only usually? Snapshot tests: did the model&amp;#39;s output change? Leakage checks: can the model see the future? These six are bracketed as checked against what I believe. The last row, reconciliation, asks whether my books match the broker&amp;#39;s, and is bracketed in green as checked against the broker, daily."&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;The first six check the code against what I believe. Only reconciliation checks against the outside world.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;The first six check my code, either on every pull request or in a weekly run. The last&#10;one runs in production, and it is the only one that talks to the real world.&lt;/p&gt;&#10;&lt;h2 id="what-actually-failed-in-ci"&gt;What Actually Failed in CI?&lt;/h2&gt;&#10;&lt;p&gt;CI, short for continuous integration, is the set of checks that runs on every pull&#10;request (a proposed change) before it can merge. Before an agent pushes a change, it&#10;runs the same lint and type checks and most of the tests on its own machine, and fixes&#10;whatever fails there. Those local failures aren&amp;rsquo;t recorded anywhere, but I&amp;rsquo;d guess&#10;there were many more of them than in CI. So CI is a second line, and the interesting&#10;question is what still got past the first one. Over four months, TopSet had about 440&#10;pull requests, and about 1 in 9 of them still hit at least one failed build in CI. Counting every&#10;build, reruns included, 53 failed. Type checking stopped 26 of them, the tests 14 and&#10;lint 12, and one never got as far as starting.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/tests-passed-broker-disagreed/ci-failures.png"&#10;&#9;&#9;&#9;alt="Horizontal bar chart of the first check that failed in each of 53 failed pull-request builds over four months, reruns included: type checking 26, tests 14, lint 12, and 1 build that never started."&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Which check stopped each failed build over four months of pull requests.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Those numbers need some care. The steps run in order, so a lint failure hides any test&#10;failure behind it. A rerun replaces a failed result, so I counted every attempt; only one&#10;failure was hidden that way, and it was a real bug I&amp;rsquo;ll come back to below. Builds that&#10;were canceled because a newer push replaced them aren&amp;rsquo;t counted as failures. And the&#10;local run skips the slow and integration tests, so a test failure in CI is often one&#10;that couldn&amp;rsquo;t have shown up locally. A lint or type failure in CI usually means the&#10;local check was skipped or ran against something different.&lt;/p&gt;&#10;&lt;p&gt;I was curious whether the type checker was catching real bugs or just complaining, so I&#10;sampled a dozen type-check failures. One was a real bug: a function returned tuples where&#10;its callers expected strings, which would have failed as soon as that code ran. The rest&#10;were mostly annotations that didn&amp;rsquo;t match what pandas and numpy actually return. In other&#10;words, type checks mostly catch sloppiness and occasionally catch a real bug, and since&#10;agents produce a lot of code quickly, catching the sloppiness is worth it.&lt;/p&gt;&#10;&lt;h2 id="what-did-the-integration-tests-find"&gt;What Did the Integration Tests Find?&lt;/h2&gt;&#10;&lt;p&gt;The integration tests drive a full rebalancing against a mock broker that fills orders&#10;at random times, in random pieces, at random prices. That randomness is the point,&#10;because the interesting bugs live in the timing.&lt;/p&gt;&#10;&lt;p&gt;The best example was a race in how the system worked out how much cash it had to&#10;spend. It read the cash balance and the cost of orders that had already filled, and&#10;subtracted one from the other. The problem was that a fill could be recorded between&#10;those two reads, because the code that recorded fills and the code that planned&#10;purchases held different locks. When that happened, the same cash was counted as&#10;available twice, and the bot tried to spend more than it had. The cash position would&#10;have gone negative, but a database rule (cash can never be below zero) stopped it.&lt;/p&gt;&#10;&lt;p&gt;The first CI run on that pull request failed. The rerun passed. If I had treated it as&#10;a flaky test and moved on, the bug would still be there. A test that fails only&#10;sometimes is usually reporting a bug that happens only sometimes.&lt;/p&gt;&#10;&lt;p&gt;Another came from the scheduled weekly run rather than from a pull request. Some&#10;integration runs had slowed to about half an hour. The cause was that the code placing&#10;trades retried every error, including ones that could never succeed, like a record that&#10;didn&amp;rsquo;t exist. Each hopeless retry tied up a worker for about 25 minutes. The fix, made&#10;with Cursor, retries only the errors that can actually clear up.&lt;/p&gt;&#10;&lt;p&gt;Running the integration tests over and over found the rarer cases: one lock race that&#10;showed up once in 223 runs, and an 11-minute stall in 2 of 144 runs that turned out to be a timeout in how the test shut down its workers. I won&amp;rsquo;t&#10;retell the rest here, as &lt;a href="https://redgeoff.com/posts/ai-agents-test-gate/"&gt;the first post&lt;/a&gt; covers the soak.&lt;/p&gt;&#10;&lt;h2 id="what-about-snapshot-tests"&gt;What About Snapshot Tests?&lt;/h2&gt;&#10;&lt;p&gt;A snapshot test trains a model on a fixed set of data and compares its picks with a&#10;reference file that is checked into the repo. If anything about the training changes,&#10;the picks change, and the test fails.&lt;/p&gt;&#10;&lt;p&gt;After I changed the machine-learning code, the snapshots often didn&amp;rsquo;t match. Most of&#10;the time the right move was to fix the code, because the change had done something I&#10;didn&amp;rsquo;t intend. Sometimes the right move was to update the snapshot, but only when the&#10;difference was one I expected and understood. Most of this happened on my own machine&#10;before a pull request existed, so there isn&amp;rsquo;t much of a record of it.&lt;/p&gt;&#10;&lt;p&gt;There is one good example in the record, though. A company that had been renamed was&#10;showing up in the historical stock universe under both its old and new tickers, so on&#10;some dates the model held it twice and gave it double the weight. When we fixed the&#10;universe, the snapshot test failed, and the difference in the reference files showed&#10;exactly why: the old files had held one company under two names. That was a case where&#10;updating the snapshot was the right call, because the old snapshot had been wrong all&#10;along.&lt;/p&gt;&#10;&lt;p&gt;The leakage checks get one line here because&#10;&lt;a href="https://redgeoff.com/posts/trading-model-leakage/"&gt;another post&lt;/a&gt; tells that story: train on part of the&#10;data, continue on the rest, and the picks must match training on all of it from&#10;scratch, bit for bit. If they don&amp;rsquo;t, something is leaking from the future.&lt;/p&gt;&#10;&lt;h2 id="so-what-couldnt-the-tests-know"&gt;So What Couldn&amp;rsquo;t the Tests Know?&lt;/h2&gt;&#10;&lt;p&gt;Every test above checks the code against what I believe. The unit tests encode what I&#10;think a function should do, and the mock broker encodes what I think the real brokers&#10;do. When my belief is wrong, the code and the test are wrong together, and they agree.&lt;/p&gt;&#10;&lt;p&gt;What I learned about the real brokers is that they do things you would never think to&#10;put in a mock. One reuses an error code for two unrelated errors. Another accepts an&#10;order and rejects it later. The code for that second broker had been written against&#10;its sandbox, a test environment, and production behaved differently. My tests matched&#10;what I believed the brokers did.&lt;/p&gt;&#10;&lt;p&gt;So the check I rely on most compares my records with the broker&amp;rsquo;s. After trading each&#10;day, the system adds up each bot&amp;rsquo;s positions in my database and compares them with the&#10;positions the broker reports for that account. When it processes corporate actions,&#10;like mergers, splits and dividends, it also compares what each bot recorded with the&#10;account activity the broker reported. Any difference it can&amp;rsquo;t explain is an error, and&#10;errors send an alert to my phone.&lt;/p&gt;&#10;&lt;p&gt;Here are some of the times those comparisons found something, followed by one time an&#10;audit of my own missed something.&lt;/p&gt;&#10;&lt;h3 id="the-assumption-nobody-had-tested"&gt;The Assumption Nobody Had Tested&lt;/h3&gt;&#10;&lt;p&gt;When a company is bought for cash, shareholders give up their shares and receive cash.&#10;My code booked the cash but left the shares in place, because it assumed the broker&#10;always sent a separate stock event that removed them. That&amp;rsquo;s true when a deal pays&#10;partly in stock, but in a pure cash deal there is no stock event. No bot had ever held a&#10;company in a pure cash deal before, so the assumption had never been tested, and the&#10;first one to touch a bot broke it.&lt;/p&gt;&#10;&lt;p&gt;The tests passed, because they were built on the same assumption. The reconciliation&#10;failed the same day, because the broker&amp;rsquo;s account activity showed the shares leaving&#10;and my bot&amp;rsquo;s records didn&amp;rsquo;t. The fix removes the shares when there&amp;rsquo;s no stock event to do it,&#10;and it was checked against all 108 cash mergers in the production data rather than just&#10;the one that broke.&lt;/p&gt;&#10;&lt;h3 id="one-broker-runs-a-day-behind"&gt;One Broker Runs a Day Behind&lt;/h3&gt;&#10;&lt;p&gt;That same merger exposed a second problem. Alpaca reported the account activity on the&#10;same day it changed the position. Tradier changed the position about a day before it&#10;reported the activity (up to two weeks for some corporate actions). Until the activity&#10;arrived, my system hadn&amp;rsquo;t applied the merger, so the reconciliation saw a mismatch.&lt;/p&gt;&#10;&lt;p&gt;The quick fix was to tolerate mismatches on a symbol with a corporate action still&#10;pending. That turned out to be too generous, because it would also hide an unrelated&#10;mismatch on the same symbol, so the tolerance now requires the broker&amp;rsquo;s quantity to be&#10;exactly what the pending action predicts.&lt;/p&gt;&#10;&lt;p&gt;The follow-up found something worse than a false alarm. While the activity was pending,&#10;a Tradier bot could plan trades from the old quantity. So symbols with a pending&#10;corporate action are now frozen during a rebalancing: not priced, not traded and not&#10;counted.&lt;/p&gt;&#10;&lt;h3 id="a-false-alarm-that-found-a-real-problem"&gt;A False Alarm That Found a Real Problem&lt;/h3&gt;&#10;&lt;p&gt;One day after trading, the reconciliation reported 10 mismatches in a live account. On a system&#10;that trades real money, that reads as your records being wrong in 10 places.&lt;/p&gt;&#10;&lt;p&gt;Alarms like this are always scary on the surface. Even with the best safeguards in&#10;place, there&amp;rsquo;s always a chance that something has gone wrong. This time I wasn&amp;rsquo;t too&#10;worried, because the totals in my account were still roughly what I expected, so I had&#10;a good feeling it was an accounting problem rather than missing money. In other words,&#10;there was a second, cruder reconciliation running in my head.&lt;/p&gt;&#10;&lt;p&gt;That feeling was right. The broker had renamed a company that day, but my list of&#10;stocks came from a source that hadn&amp;rsquo;t caught up yet, so a bot tried to buy the old&#10;ticker. The broker rejected the order, the order got stuck, and the rebalancing never&#10;finished. The reconciliation then compared the last finished day in my database with a&#10;broker account that already showed the 10 orders that had filled that day.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/tests-passed-broker-disagreed/false-alarm.png"&#10;&#9;&#9;&#9;alt="Four steps from left to right: a company gets a new ticker, but my stock list still has the old one; a bot buys the old ticker, the broker rejects it and the order is stuck; the rebalancing never finishes, though its other orders have already filled; the audit compares my last finished day with the broker&amp;#39;s today. Below: 10 mismatches reported, exactly the 10 orders that had filled that day. The positions were fine."&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;How a renamed company became 10 false mismatches and one real hung rebalancing.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;So the alarm&amp;rsquo;s diagnosis was wrong, but the alarm was still worth having, because a&#10;rebalancing was hung and the alarm made sure I looked. The fix makes the stock list&#10;follow the broker&amp;rsquo;s renames and makes sure a rejected ticker can&amp;rsquo;t hang a rebalancing.&lt;/p&gt;&#10;&lt;h3 id="the-audit-that-looked-for-the-wrong-thing"&gt;The Audit That Looked for the Wrong Thing&lt;/h3&gt;&#10;&lt;p&gt;Checks like these have blind spots too, and the best example is an audit I had built&#10;for exactly the kind of problem it then missed.&lt;/p&gt;&#10;&lt;p&gt;TopSet has a report that compares what the model planned with what the bots actually&#10;got. Once, a missing price made one of its key numbers come out blank. An agent added an&#10;audit that warned whenever that number was missing, along with seven unit tests for it.&lt;/p&gt;&#10;&lt;p&gt;Later, a company that had recently been spun off had a gap of 10 trading days in its&#10;prices, and two bots I was comparing both held it. This time the missing price arrived as&#10;NaN (&amp;ldquo;not a number&amp;rdquo;) instead of an empty value. In Python, NaN counts as true, so the&#10;guard that should have skipped the missing price let it through, and that one value&#10;wiped out the whole day&amp;rsquo;s average for every other stock. The result was written as the&#10;text &amp;ldquo;nan&amp;rdquo;. The audit was checking for an empty value, so it let &amp;ldquo;nan&amp;rdquo; straight through.&#10;The report&amp;rsquo;s main numbers were usable on only 0 or 1 of about 13 days, with no warning,&#10;and the audit and its tests all passed. It was found by hand, by going through the&#10;per-stock data looking for missing prices.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/tests-passed-broker-disagreed/nan-average.png"&#10;&#9;&#9;&#9;alt="An illustration with made-up returns for ten equal holdings, one of which is NaN. Three boxes follow. The guard, if not price: skip, doesn&amp;#39;t skip it because NaN counts as true. The day&amp;#39;s average becomes the text nan, when it should have been &amp;#43;0.48 percent, the average of the other nine. The audit, if result is None: warn, passes it because the text nan isn&amp;#39;t None. The audit and its seven unit tests all passed, and nothing warned."&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;How one missing price wiped out a whole day, shown with made-up numbers.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Looking back, I don&amp;rsquo;t think this kind of failure can be caught up front. You need the&#10;unexpected data to show up, then you trace through the calculations, and something like&#10;an unexplained outlier tips you off. That&amp;rsquo;s why iterating on the tests matters so much:&#10;every edge case that breaks a pipeline becomes the next test, because an audit only&#10;checks what you pointed it at.&lt;/p&gt;&#10;&lt;h2 id="what-does-it-cost"&gt;What Does It Cost?&lt;/h2&gt;&#10;&lt;p&gt;Reconciliation isn&amp;rsquo;t free. It raises false alarms, like the renamed ticker and the&#10;broker that runs a day behind, and every false alarm is a choice between investigating&#10;and loosening a tolerance. Loosen it too far and it stops catching anything, which is&#10;why the pending-action tolerance had to be tightened again. Each new kind of corporate&#10;action also needs its own rules for what the broker should show, and those rules are&#10;code with their own bugs.&lt;/p&gt;&#10;&lt;p&gt;That being said, I&amp;rsquo;d make the same trade again. A false alarm costs me some time. A&#10;silent mismatch between my books and the broker&amp;rsquo;s could cost real money, and it would&#10;keep growing until something else noticed.&lt;/p&gt;&#10;&lt;h2 id="why-can-it-run-unattended"&gt;Why Can It Run Unattended?&lt;/h2&gt;&#10;&lt;p&gt;The tests decide what merges. The reconciliation decides whether I can leave the system&#10;alone, and that&amp;rsquo;s a different question. A one-person system can&amp;rsquo;t depend on someone&#10;watching it every day. It has to notice when it&amp;rsquo;s wrong and say so.&lt;/p&gt;&#10;&lt;p&gt;Here is what I&amp;rsquo;d tell anyone building something like this:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Use each kind of test for the question it answers, and don&amp;rsquo;t expect any of them to&#10;tell you what the outside world does.&lt;/li&gt;&#10;&lt;li&gt;Treat a test that fails sometimes as a bug report, and don&amp;rsquo;t just rerun it until it&#10;passes.&lt;/li&gt;&#10;&lt;li&gt;Compare your records with the outside system&amp;rsquo;s every day, and make any difference&#10;you can&amp;rsquo;t explain an error that reaches you.&lt;/li&gt;&#10;&lt;li&gt;When a check misses something, ask what else it isn&amp;rsquo;t looking for.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;TopSet runs unattended today because of that last layer. When something drifts, the&#10;broker disagrees with me, and I hear about it the same day.&lt;/p&gt;&#10;&lt;p&gt;Thanks for reading.&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;&lt;em&gt;This describes software I run on my own accounts. It is not investment advice and I&#10;do not offer investment services.&lt;/em&gt;&lt;/p&gt;&#10;</description></item><item><title>I stopped re-explaining myself to AI</title><link>https://redgeoff.com/posts/stopped-re-explaining-myself-to-ai/</link><pubDate>Mon, 02 Nov 2026 16:20:00 -0800</pubDate><guid>https://redgeoff.com/posts/stopped-re-explaining-myself-to-ai/</guid><description>&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/stopped-re-explaining-myself-to-ai/memex-hero.jpg"&#10;&#9;&#9;&#9;alt="Five labels on the left, Claude Code, Grok Bot, School bot, Memex bot and Another AI, with arrows into one card labeled memex/ that lists INDEX.md, decisions.md, knowledge/, context/, bots/ and dreams/, under the headline One memory, every AI"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: author, drawn with code&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;For a while, every new AI session started with me re-explaining things: who I am, what&#10;I&amp;rsquo;m working on and which documents matter, often by uploading the same PDFs again. Now&#10;any AI I use starts with all of that.&lt;/p&gt;&#10;&lt;p&gt;This is a story about how I moved my notes out of Google Drive and into a folder of plain&#10;text files that every AI I use can read and update, and about the handful of rules that&#10;keep that folder from turning into a mess. I call it my memex.&lt;/p&gt;&#10;&lt;p&gt;If you read &lt;a href="https://redgeoff.com/posts/school-email-bot/"&gt;my last post&lt;/a&gt;, you&amp;rsquo;ve already&#10;met it. The bot that reads our kids&amp;rsquo; school email keeps its rules there. This post is&#10;about the memex itself: what&amp;rsquo;s in it, how it stays accurate when several AIs are writing&#10;to it, and how you could start one.&lt;/p&gt;&#10;&lt;h3 id="what-was-wrong-with-google-drive"&gt;What Was Wrong With Google Drive?&lt;/h3&gt;&#10;&lt;p&gt;In short, AIs kept tripping over my documents. When I pointed Claude at my Google Drive&#10;and asked it to edit a Google Doc, it would create a new copy instead of changing the one&#10;I had. Claude has its own documents that it can edit, but other AIs can&amp;rsquo;t easily read&#10;them. And most AIs couldn&amp;rsquo;t search my Drive well enough to find things, so I often ended&#10;up exporting documents to PDF and uploading them by hand.&lt;/p&gt;&#10;&lt;p&gt;I wanted one place that any AI could read, search and change, without me copying things&#10;around. A note-taking app wasn&amp;rsquo;t the answer either, since you&amp;rsquo;re limited to the structure&#10;and features the app gives you.&lt;/p&gt;&#10;&lt;h3 id="what-is-the-memex"&gt;What Is the Memex?&lt;/h3&gt;&#10;&lt;p&gt;The name comes from Vannevar Bush&amp;rsquo;s 1945 essay &amp;ldquo;As We May Think&amp;rdquo;, where a memex is a&#10;desk that stores a person&amp;rsquo;s books, records and communications so they can be consulted&#10;quickly. Mine is a git repo of markdown files. Markdown is plain text with a little&#10;formatting, e.g. a &lt;code&gt;#&lt;/code&gt; in front of a heading. A git repo is a folder that records every&#10;change, so you can see what changed, when and why, and undo any of it. Mine lives on my&#10;laptop and in a private repo on GitHub.&lt;/p&gt;&#10;&lt;p&gt;The files are organized by what they&amp;rsquo;re about:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;code&gt;knowledge/&lt;/code&gt; holds the notes: my career history, projects, reference material, the&#10;household (school schedules, the cars, recipes) and a daily log.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;context/&lt;/code&gt; holds a few files that tell an AI who I am and how I like to work, e.g. &amp;ldquo;ask&#10;me instead of guessing&amp;rdquo; and &amp;ldquo;never commit directly to main&amp;rdquo;.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;bots/&lt;/code&gt; holds a profile for each bot, which is what my last post was about.&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;scripts/&lt;/code&gt; and &lt;code&gt;skills/&lt;/code&gt; hold the small programs that check and maintain everything,&#10;with their tests.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;I created the repo on March 1, 2026 from a template and then mostly forgot about it. The&#10;real work started on October 13, 2026. As of October 31, it has 131 notes and about&#10;157,000 words.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/stopped-re-explaining-myself-to-ai/memex-growth.png"&#10;&#9;&#9;&#9;alt="Line chart of words in the memex by day, October 12 to 30, 2026: 7 notes and about 3,000 words in the template, 27 notes after the Google Drive import on October 13, 79 notes on October 14, then a slower climb to 131 notes and about 157,000 words, with dreams marked on October 13, 15 and 25"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Words across all notes at the end of each day. Most of the jump came in the first two days.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h3 id="how-did-my-drive-get-into-it"&gt;How Did My Drive Get Into It?&lt;/h3&gt;&#10;&lt;p&gt;On October 13, I downloaded my Google Drive to my laptop and had Claude read it. It&#10;extracted the text from about 780 files: PDFs, Word documents, slide decks, spreadsheets&#10;and plain text. Google Docs were the one snag. They sync to a laptop as pointer files&#10;with no text in them, so I downloaded those folders again as Word and Excel files.&#10;Claude then read the converted files too and summarized everything it had read into&#10;notes, mostly about my career and past projects, rather than copying the documents in.&lt;/p&gt;&#10;&lt;p&gt;Some things I didn&amp;rsquo;t want in the memex at all, so before anything was summarized we&#10;agreed on what to leave out: tax and financial records, anything that looked like it&#10;held a password, medical and identity documents, and the folders about our kids. The&#10;memex keeps a ledger of the import that lists what was read, what was skipped and why.&#10;That way a later session doesn&amp;rsquo;t redo the work, or worse, import something I had&#10;already decided to exclude.&lt;/p&gt;&#10;&lt;p&gt;The import surfaced something I hadn&amp;rsquo;t expected. When you read 25 years of someone&amp;rsquo;s&#10;documents side by side, they disagree with each other. For example, one of my old&#10;self-reviews said our hotfixes had dropped from 8 a week to 1.5, while my other records&#10;said 7. Rather than pick one, Claude listed every conflict like that and asked me. I&#10;answered them in batches that day, and each answer became an entry in a decisions&#10;log.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/stopped-re-explaining-myself-to-ai/decision-to-rule.png"&#10;&#9;&#9;&#9;alt="Three boxes: documents disagree (a self-review said hotfixes fell from 8 a week to 1.5, other records said from 7), I decide (decision D13, 7 to 1.5 a week, not 8), and the checker holds it (a note that says 8 to 1.5 fails the check and cites D13)"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;One conflict from the import, from my documents to a rule the checker enforces.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h3 id="how-does-it-stay-accurate"&gt;How Does It Stay Accurate?&lt;/h3&gt;&#10;&lt;p&gt;A pile of notes that several AIs write to will drift. One session restates a fact a&#10;little differently, another copies an old number from a document, and a month later&#10;nobody knows which version is right. So the memex has a few rules, and a program checks&#10;most of them on every change.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/stopped-re-explaining-myself-to-ai/note-header.png"&#10;&#9;&#9;&#9;alt="An example note header with date, updated, tags, status, summary and sensitivity fields, each labeled with what it is for"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;An example header. The index is built from these lines.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;ol&gt;&#10;&lt;li&gt;Every note starts with a short header: when it was created and last updated, some&#10;tags, whether it&amp;rsquo;s active or a draft, a one-line summary and how private it is. Private&#10;notes never leave the memex. Usable notes can inform things I write, like this post.&#10;Public notes are already public.&lt;/li&gt;&#10;&lt;li&gt;A script builds an index from those headers, one line per note. An AI reads the index&#10;first and then opens only the notes it needs. That matters because an AI can only&#10;hold so much text at once (its &amp;ldquo;context window&amp;rdquo;), and 157,000 words is far more than&#10;you&amp;rsquo;d want it to load for every question.&lt;/li&gt;&#10;&lt;li&gt;The decisions log records each of my rulings with its date, 42 of them so far. Old&#10;decisions are never edited. A new one says which one it replaces.&lt;/li&gt;&#10;&lt;li&gt;Some decisions are backed by a rule in the checker, so a corrected fact can&amp;rsquo;t creep&#10;back in. If a note says hotfixes went from 8 to 1.5 a week, the check fails and points&#10;at the decision.&lt;/li&gt;&#10;&lt;li&gt;The checker, or linter, also makes sure every note has its header, every link points&#10;to a real note, file names follow one pattern, nothing looks like a password or an&#10;access key, and the index is current. It runs on every change, along with 196&#10;automated tests for the scripts.&lt;/li&gt;&#10;&lt;li&gt;Every change arrives as a pull request, which is a proposed change I can read before&#10;it&amp;rsquo;s accepted. Nothing is committed straight to the main copy. For the memex, I let&#10;the AI merge its own pull request once the checks pass.&lt;/li&gt;&#10;&lt;li&gt;Nothing is deleted. Notes that are out of date move to an archive folder.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;None of this is clever. The rules are simple enough that any AI can follow them, and the&#10;checker catches it when one doesn&amp;rsquo;t.&lt;/p&gt;&#10;&lt;h3 id="what-is-the-dream"&gt;What Is the Dream?&lt;/h3&gt;&#10;&lt;p&gt;The checker can tell you a link is broken, but it can&amp;rsquo;t tell you that two notes say the&#10;same thing in different words, or that a plan from three weeks ago is stale. For that,&#10;there is a cleanup pass called a &amp;ldquo;dream&amp;rdquo;, loosely after the way sleep is thought to&#10;consolidate memories.&lt;/p&gt;&#10;&lt;p&gt;I didn&amp;rsquo;t come up with the idea. Anthropic added&#10;&lt;a href="https://claude.com/blog/new-in-claude-managed-agents"&gt;dreaming&lt;/a&gt; to Claude Managed Agents&#10;in May 2026: a scheduled process that reviews an agent&amp;rsquo;s past sessions and memory, pulls&#10;out patterns and tidies the memory, either on its own or with you reviewing the changes&#10;first. Their Code with Claude talk,&#10;&lt;a href="https://claude.com/code-with-claude/session/sf-memory-and-dreaming-for-self-learning-agents"&gt;&amp;ldquo;Memory and dreaming for self-learning agents&amp;rdquo;&lt;/a&gt;,&#10;covers how they got there. &lt;a href="https://docs.openclaw.ai/concepts/dreaming"&gt;OpenClaw&lt;/a&gt; has its&#10;own version that runs in light, REM and deep phases and writes a diary of what it changed,&#10;and &lt;a href="https://hermes-agent.nousresearch.com/docs/plugins/hermes-dreaming"&gt;Hermes Agent&lt;/a&gt; has&#10;a community plugin modeled on OpenClaw&amp;rsquo;s. Mine borrows from all of them. The difference is&#10;that my memory is a git repo, so a dream&amp;rsquo;s changes arrive as a pull request I can read.&lt;/p&gt;&#10;&lt;p&gt;A dream has two halves. The mechanical half runs the checker and rebuilds the index. The&#10;reflective half is an AI working through a written checklist: promote anything durable&#10;from the daily log into the right note, find facts stated in more than one place and keep&#10;one owner for each, flag notes that have gone stale, and write down the questions only I&#10;can answer. It lands as a pull request with a short report.&lt;/p&gt;&#10;&lt;p&gt;What the AI is allowed to do on its own depends on the change:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Mechanical fixes that don&amp;rsquo;t change meaning, like a broken link or a stale index, it&#10;just does.&lt;/li&gt;&#10;&lt;li&gt;Moves, merges and new notes, it makes on the branch and calls out in the report, so I&#10;can accept or revert them.&lt;/li&gt;&#10;&lt;li&gt;Anything that changes what a fact means, which version of something is right, or a&#10;privacy rule, it asks me about first and doesn&amp;rsquo;t touch until I answer.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/stopped-re-explaining-myself-to-ai/dream-tiers.png"&#10;&#9;&#9;&#9;alt="Three columns: just does it (rebuild the index, fix a broken link, tidy a header), proposes in the PR (merge two notes, move a note, promote a daily-log fact) and asks me first (which version is right, a recorded decision, a privacy rule)"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;What a dream may do on its own, what it proposes and what it asks about.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;There have been four dreams so far: October 13, October 15, October 25 and November 1.&#10;The last two were run by a bot on its own weekly schedule. On October 25, lint was&#10;already clean, so the main results were notes for the two people mentioned most often&#10;who didn&amp;rsquo;t have one yet (notes on people are private and stay in the memex), and one&#10;place where the repo&amp;rsquo;s instructions contradicted a newer decision. The November 1 dream&#10;was lighter: one lint warning fixed, the bot&amp;rsquo;s own log brought up to date and no&#10;questions for me.&lt;/p&gt;&#10;&lt;h3 id="who-writes-to-it"&gt;Who Writes to It?&lt;/h3&gt;&#10;&lt;p&gt;More than one AI, which was the whole idea. From October 13 to 31, the memex had 190&#10;commits and 122 merged pull requests. Most came from Claude Code sessions on my laptop.&#10;Others came from Grok Bot, the agent I use to run bots on a cloud computer, and from the&#10;bots themselves, e.g. the school bot updating its profile and a memex bot that does the&#10;weekly upkeep.&lt;/p&gt;&#10;&lt;p&gt;Two things fall out of that. First, I&amp;rsquo;m not tied to any one AI. If I switch tools&#10;tomorrow, the notes, the rules and the history come with me, because they&amp;rsquo;re just files.&#10;Second, every session starts with context. I don&amp;rsquo;t paste my background into a prompt or&#10;explain how something works, because the AI reads the index and finds it.&lt;/p&gt;&#10;&lt;p&gt;In practice, I start most things with that same memex bot. When I want to add to&#10;something or work on it, like a new project idea, a blog post I should write or a book&#10;I should read, I ask the memex bot to add the details. It&#10;already has the context of many things in my life, both current and old. And when I&amp;rsquo;m&#10;working in another AI and I think my memex would help, I give that AI read and write&#10;access to the memex&amp;rsquo;s GitHub repo, and it has the same context right away. A bot gets&#10;its own access token that works only on that one repo, can only read and write files and&#10;open pull requests, and expires within 90 days.&lt;/p&gt;&#10;&lt;p&gt;Pull requests turned out to be useful for more than catching an AI&amp;rsquo;s mistakes. When I&#10;want feedback on a document, I open it as a pull request and leave comments on specific&#10;lines in GitHub, the way you&amp;rsquo;d review code. The AI answers each comment and pushes a new&#10;version. This post was reviewed that way.&lt;/p&gt;&#10;&lt;h3 id="is-it-safe"&gt;Is It Safe?&lt;/h3&gt;&#10;&lt;p&gt;I think it&amp;rsquo;s relatively safe, as long as you don&amp;rsquo;t have a lot of automations that send&#10;out or post content unattended. The only bot of mine that emails anyone sends to one family&#10;list, from its own account; the others open pull requests or message me in a private&#10;Slack channel. That said, there are risks, and you certainly don&amp;rsquo;t want to store&#10;anything sensitive in your memex, e.g. credit card numbers. The checker flags anything&#10;that looks like a password or an access key, but it can&amp;rsquo;t catch everything, so the&#10;better habit is to leave those things out in the first place.&lt;/p&gt;&#10;&lt;h3 id="what-would-i-do-differently-from-day-one"&gt;What Would I Do Differently From Day One?&lt;/h3&gt;&#10;&lt;p&gt;Not much, and I wouldn&amp;rsquo;t try to design it all up front. Everyone&amp;rsquo;s preferences differ,&#10;and mine weren&amp;rsquo;t clear until I&amp;rsquo;d used it for a while. A memex changes shape as you use&#10;it, and the goal is that over time it holds all of your context and preferences, written&#10;down.&lt;/p&gt;&#10;&lt;p&gt;That said, three things would have saved me some cleanup:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Start the decisions log on day one, not after the import.&lt;/li&gt;&#10;&lt;li&gt;Decide what stays out, e.g. financial and medical records, before an AI reads&#10;anything.&lt;/li&gt;&#10;&lt;li&gt;Add the index before the notes pass a few dozen.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h3 id="how-could-you-start-one"&gt;How Could You Start One?&lt;/h3&gt;&#10;&lt;p&gt;You don&amp;rsquo;t need my scripts to get most of the benefit. A folder of markdown files in a git&#10;repo, a short header on every note and an AI that can read files will get you a long way.&#10;I&amp;rsquo;d add the rest in roughly the order it paid off for me:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;A context file that says who you are and how you like to work.&lt;/li&gt;&#10;&lt;li&gt;The header on every note, and an index built from the headers.&lt;/li&gt;&#10;&lt;li&gt;A decisions log, as soon as you catch yourself correcting the same fact twice.&lt;/li&gt;&#10;&lt;li&gt;A checker that runs on every change.&lt;/li&gt;&#10;&lt;li&gt;Changes by pull request, so you see what an AI did before it sticks.&lt;/li&gt;&#10;&lt;li&gt;A cleanup pass once the notes outgrow what you can review by hand.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;If you&amp;rsquo;d like a head start, I&amp;rsquo;ve put the memex&amp;rsquo;s structure and tooling on GitHub as a&#10;template, with none of my notes in it:&#10;&lt;a href="https://github.com/redgeoff/memex-template"&gt;memex-template&lt;/a&gt;. It has the folders, the&#10;note header, the index builder, the checker, a decisions log with an example rule, and&#10;the dream protocol with its &lt;code&gt;/dream&lt;/code&gt; command. The bot template from my last post lives&#10;there too, since the bots deliver into the memex.&lt;/p&gt;&#10;&lt;p&gt;Thanks for reading.&lt;/p&gt;&#10;</description></item><item><title>I built an AI bot to read our kids' school email. Here's what I'd tell you before you build one.</title><link>https://redgeoff.com/posts/school-email-bot/</link><pubDate>Mon, 26 Oct 2026 15:33:00 -0700</pubDate><guid>https://redgeoff.com/posts/school-email-bot/</guid><description>&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/school-email-bot/school-bot-hero.jpg"&#10;&#9;&#9;&#9;alt="A pile of tilted cards labeled Newsletter, Reminder, Flyer, Form due and other school messages on a dark background, with an arrow to a single School digest page with Must-do, This week and Coming up sections"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: author, drawn with code&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;This is a story about how my wife and I went from several school emails and texts a day&#10;to one short digest every afternoon, and about what it took to trust an AI bot to write&#10;it. Getting the bot to summarize was the easy part. Most of the work went into deciding&#10;what it is not allowed to do, and into catching the times it got something wrong without&#10;saying so.&lt;/p&gt;&#10;&lt;p&gt;If you read &lt;a href="https://redgeoff.com/posts/ai-agents-test-gate/"&gt;my first post&lt;/a&gt;, this is the&#10;same idea in a very different setting. There, automated tests were the gate that let AI&#10;agents write code for a system that trades real money. Here, the gate is a short list of&#10;things the bot can never do, plus one place where every rule it follows is written down.&lt;/p&gt;&#10;&lt;h3 id="why-build-a-bot-for-school-email"&gt;Why Build a Bot for School Email?&lt;/h3&gt;&#10;&lt;p&gt;We have two kids at two different schools, a 5th grader and a 2nd grader. Between the&#10;district, both schools, the teachers, the parent groups, the after-school club and the&#10;flyers, the messages never really stop. We were getting multiple emails and texts a day,&#10;and I figured a bot would be a good way to consolidate the noise.&lt;/p&gt;&#10;&lt;p&gt;I counted afterwards. In the six weeks from September 9 to October 21, 2026, roughly 200&#10;emails came in that were actually about school, or a bit over 30 a week. Over the same&#10;weeks, the kids&amp;rsquo; sports team app sent another 120. Some of the school mail is duplicated,&#10;too, e.g. the after-school club often sends the same notice once for each kid.&lt;/p&gt;&#10;&lt;p&gt;On top of that, the schools use &lt;a href="https://talkingpts.org/"&gt;TalkingPoints&lt;/a&gt;, an app for&#10;school announcements and teacher messages, and those never reach email at all. In one&#10;sample week it had about 13 announcements and 3 active conversations with teachers. In&#10;other words, keeping up meant reading all of that email and then remembering to open a&#10;separate app every day.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/school-email-bot/six-weeks-of-messages.png"&#10;&#9;&#9;&#9;alt="Horizontal bar chart: about 200 school emails in, 120 sports team app emails left out, and 44 digests sent, September 9 to October 21, 2026"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Six weeks of school messages. TalkingPoints isn&amp;rsquo;t in the count, because it never reaches email.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h3 id="what-does-the-bot-actually-do"&gt;What Does the Bot Actually Do?&lt;/h3&gt;&#10;&lt;p&gt;Every afternoon, the bot reads the new school mail in Gmail and the new messages in&#10;TalkingPoints. It follows the links to the full newsletters instead of trusting the&#10;preview in the email, and then it writes one digest. Everything is grouped by school, with&#10;the must-dos first. Every event has a time, or says &amp;ldquo;time not listed&amp;rdquo; rather than a guess.&#10;A notice that was sent once per kid shows up once, and the sports team mail is left out.&#10;On Saturday evenings, a second email looks ahead at the next few weeks.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/school-email-bot/example-digest.png"&#10;&#9;&#9;&#9;alt="An illustrative school digest with must-dos first, an Add to calendar tag, Already in calendar tags, a section per grade and a coming-up section"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;An illustrative digest with made-up items, to show the format. The real ones are private.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Before it writes, the bot also checks my Google Calendar. If a must-do is already on it,&#10;the digest says &amp;ldquo;Already in calendar&amp;rdquo;. If it isn&amp;rsquo;t, the item gets an &amp;ldquo;Add to calendar&amp;rdquo;&#10;link, so adding it takes one click. And if nothing new came in, the bot sends nothing at&#10;all, so an email from it always means there&amp;rsquo;s something to read.&lt;/p&gt;&#10;&lt;p&gt;The digest goes from the bot&amp;rsquo;s own Gmail account to a mailing list that Mimi and I share.&#10;From September 9 to October 20 it sent 44 digests, 40 daily and 4 weekly. That&amp;rsquo;s about 7&#10;emails a week in place of 30-plus emails and an app. I&amp;rsquo;d estimate it saves me 30 to 45&#10;minutes of reading a week.&lt;/p&gt;&#10;&lt;p&gt;We both read the digest every day, and it has cut down the time I spend keeping up with&#10;all the notifications. Another benefit is that my day has far fewer interruptions. Instead&#10;of switching over every time an email or a message comes in, I let them pile up and read&#10;them all at once in the digest. It has also made it less likely that something falls&#10;through the cracks, and the calendar links have made it easy to keep my calendar up to&#10;date. Mimi and I share our calendars with each other, so whatever I add, she can see too.&lt;/p&gt;&#10;&lt;p&gt;Here are a few of its catches from the first six weeks:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;A school event that was posted only in TalkingPoints, with the details on a linked&#10;newsletter page. Reading email alone would have missed it.&lt;/li&gt;&#10;&lt;li&gt;Two notices gave different dates for the same family night, and the later date was the&#10;same evening as an event at the other school. The digest flagged it so we could check.&lt;/li&gt;&#10;&lt;li&gt;A calendar entry labeled with the wrong school, and another that ran half an hour&#10;longer than the school said.&lt;/li&gt;&#10;&lt;li&gt;A newsletter that printed last year&amp;rsquo;s date for this year&amp;rsquo;s parade.&lt;/li&gt;&#10;&lt;li&gt;A reading test on the same day as two appointments. A later teacher message showed it&#10;had already been handled, so the digest told us there was nothing to reschedule.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h3 id="what-is-the-bot-not-allowed-to-do"&gt;What Is the Bot Not Allowed to Do?&lt;/h3&gt;&#10;&lt;p&gt;The bot can read a lot: my Gmail, my calendar and TalkingPoints. If it could also write to&#10;all of those, one bad run could send an email in my name to a teacher, move an event on my calendar or reply in a class conversation. So most of its rules are about what it&#10;can&amp;rsquo;t do.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;It never sends from my account. It has its own Gmail account, and it only sends to our&#10;shared list. It can only read my Gmail.&lt;/li&gt;&#10;&lt;li&gt;It never creates, changes or deletes a calendar event. It reads the calendar, and one of&#10;us clicks &amp;ldquo;Add to calendar&amp;rdquo;.&lt;/li&gt;&#10;&lt;li&gt;It never replies to the school, by email or in TalkingPoints.&lt;/li&gt;&#10;&lt;li&gt;It never invents a detail. If a time or a place isn&amp;rsquo;t in the source, the digest says so.&lt;/li&gt;&#10;&lt;li&gt;It works out every day of the week itself from the date. It never copies one from a&#10;message or trusts its own guess.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/school-email-bot/what-the-bot-can-do.png"&#10;&#9;&#9;&#9;alt="Diagram: the bot reads Gmail and Google Calendar read-only and TalkingPoints in its own browser, sends only from its own Gmail to one shared list, and never sends as me, changes the calendar, replies to the school or invents a detail"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;What the bot can read, where it can send, and what it never does.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;TalkingPoints needed the most care. It has no API, i.e. no official way for a program to&#10;read it, so the bot reads it the way a person would, in a web browser. I run the bot on&#10;&lt;a href="https://cursor.com/docs/grok-bot/get-started"&gt;Grok Bot&lt;/a&gt;, which runs its bots on a cloud&#10;computer with a web browser, and the bot stays signed in to TalkingPoints there between&#10;runs.&lt;/p&gt;&#10;&lt;p&gt;Signing in to TalkingPoints takes a code sent by text message to a phone number that the&#10;school manages. So, when the session expires, the bot stops and asks me to sign in. I&#10;click &amp;ldquo;Open computer&amp;rdquo; in Grok Bot, take over the bot&amp;rsquo;s screen, type in the code from my&#10;phone and hand control back. I looked into giving the bot a virtual phone number so it&#10;could read its own codes, and decided against it. The number is managed by the school, and&#10;a number the bot could read would be a key to the account. So for now, I type in the code&#10;myself, and I&amp;rsquo;ll revisit that if it starts happening often.&lt;/p&gt;&#10;&lt;h3 id="what-went-wrong"&gt;What Went Wrong?&lt;/h3&gt;&#10;&lt;p&gt;Quite a bit, and the failures that matter most are the quiet ones. A crash is easy to&#10;spot, since no digest shows up. A digest that looks complete but isn&amp;rsquo;t is much worse,&#10;because you trust it.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;On September 11, the bot took the day of the week from the text of a message instead of from when the message was sent. By the end of September it had&#10;mixed up weekdays on its own too, so now it calculates every weekday from the date.&lt;/li&gt;&#10;&lt;li&gt;On October 10, the weekly email skipped TalkingPoints without saying so. Now the bot&#10;has to check TalkingPoints on every run, and if it can&amp;rsquo;t, a warning goes at the very top&#10;of the digest.&lt;/li&gt;&#10;&lt;li&gt;On October 11, a digest arrived full of raw formatting code, e.g. &lt;code&gt;&amp;lt;h3&amp;gt;&lt;/code&gt; where a&#10;heading should have been. Now a small program checks every email before it&amp;rsquo;s sent, and&#10;the bot isn&amp;rsquo;t allowed to send until the check passes.&lt;/li&gt;&#10;&lt;li&gt;On October 15, the bot reported that TalkingPoints had signed it out. It hadn&amp;rsquo;t. The&#10;bot had judged from the app&amp;rsquo;s front page alone. A corrected digest went out the same&#10;day, and now the bot confirms a sign-out by opening a page that only a signed-in user&#10;can reach.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/school-email-bot/failures-to-rules.png"&#10;&#9;&#9;&#9;alt="Four rows, each pairing a quiet failure with the rule it became: September 11 weekday, October 10 skipped TalkingPoints, October 11 raw formatting code, October 15 false sign-out"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;The four quiet failures, and the rule each one became.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;If you recall from my post on &lt;a href="https://redgeoff.com/posts/backtest-noise/"&gt;random seeds&lt;/a&gt;,&#10;the dangerous results there didn&amp;rsquo;t crash either. They looked convincing. A digest that&#10;quietly skips a source is the same kind of bug, and the fix was the same kind of fix: a&#10;check that runs every time, whether or not anyone remembers it.&lt;/p&gt;&#10;&lt;p&gt;There were plumbing problems too. In early October, the bot&amp;rsquo;s Gmail connection lost the&#10;ability to send, so it got a backup way to send email, and a week later the cloud computer&#10;blocked that backup, by which point the Gmail connection worked again. All told, 5 of the&#10;44 digests were corrections or second versions.&lt;/p&gt;&#10;&lt;p&gt;Every one of those lessons became a written rule. But written where?&lt;/p&gt;&#10;&lt;h3 id="why-do-the-bots-live-in-a-git-repo"&gt;Why Do the Bots Live in a Git Repo?&lt;/h3&gt;&#10;&lt;p&gt;For the first five weeks, the bot&amp;rsquo;s setup, its instructions and its rules lived only on&#10;the cloud computer. If that computer had gone away, I would have had to rebuild the bot&#10;from memory. On October 14 the bot exported all of it into my memex, and that is where its&#10;rules live now.&lt;/p&gt;&#10;&lt;p&gt;My memex is a git repo of markdown files. If you haven&amp;rsquo;t used git, a repo is a folder that&#10;records every change, so you can see what changed, when, and undo any of it. Markdown is&#10;plain text with a little formatting. The name comes from Vannevar Bush&amp;rsquo;s 1945 essay &amp;ldquo;As We&#10;May Think&amp;rdquo;, where a memex is a device in which a person &amp;ldquo;stores all his books, records,&#10;and communications&amp;rdquo;, so that it &amp;ldquo;may be consulted with exceeding speed and flexibility.&amp;rdquo;&#10;Mine is less grand than that.&lt;/p&gt;&#10;&lt;p&gt;I started it because AIs kept tripping over my documents. I had pointed Claude at my&#10;Google Drive, and when it edited a Google Doc, it would create a new version of the&#10;document instead of changing the one I had, which got messy and tedious fast. Claude has&#10;its own documents that it can edit, but other AIs can&amp;rsquo;t easily read them. Most AIs also&#10;couldn&amp;rsquo;t search my Drive well enough to find things, so I often ended up exporting&#10;documents to PDF and uploading them by hand. A note-taking app wasn&amp;rsquo;t the answer either,&#10;since you&amp;rsquo;re limited to the structure and features the app gives you. A folder of markdown&#10;files, on the other hand, seems to be something an AI can work with natively. So I&#10;downloaded my Drive, had the AI summarize the non-sensitive parts into the memex, and now&#10;any AI I use can read and update the same notes. I also have the full history of every&#10;change.&lt;/p&gt;&#10;&lt;p&gt;Every note starts with a short header that says what it is, when it changed and how&#10;private it is. A checker reads every note, a script builds an index of all of them, and a&#10;decisions log records each ruling with its date. 185 automated tests run on every change.&#10;There is even a cleanup pass I call &amp;ldquo;dream&amp;rdquo;, which checks, re-indexes and tidies up the&#10;notes.&lt;/p&gt;&#10;&lt;p&gt;Each bot has a profile in the repo called &lt;code&gt;BOT.md&lt;/code&gt;. It records where the bot runs, its&#10;instructions word for word, what it has access to, the decisions it must follow and every&#10;lesson above. Changes to it arrive as a pull request, which is a proposed change I can read&#10;before I accept it, and the bot is expected to update its profile whenever its setup&#10;changes. So, if the cloud computer disappeared tomorrow, I could rebuild the bot from&#10;what&amp;rsquo;s in the repo.&lt;/p&gt;&#10;&lt;p&gt;Having the profile has one more advantage. Because the bot already knows how it works and&#10;what was in the latest newsletters, I don&amp;rsquo;t have to explain anything when I ask it a&#10;question. I just ask, e.g. whether anything changed for a field trip, and it has the&#10;context.&lt;/p&gt;&#10;&lt;h3 id="what-would-i-do-differently-from-day-one"&gt;What Would I Do Differently From Day One?&lt;/h3&gt;&#10;&lt;p&gt;The school bot came first, and it grew its rules one failure at a time. The bots I&amp;rsquo;ve&#10;built since start from a template, and they go a step further: everything they produce,&#10;not just their profile, arrives in the memex as a pull request. I cleaned up that template&#10;and published it as &lt;a href="https://github.com/redgeoff/memex-bots"&gt;memex-bots&lt;/a&gt;. This part is&#10;more technical, so feel free to skim. These are its rules.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;The AI finds and judges, and ordinary code decides. The AI searches with its own tools&#10;and writes down what it found. A small Python program checks that, scores it and writes&#10;the digest. Because the scores come from code, the same input always gets the same&#10;result.&lt;/li&gt;&#10;&lt;li&gt;Every item needs a source. An item without a link and a short quote is dropped, and the&#10;digest says how many were dropped.&lt;/li&gt;&#10;&lt;li&gt;Every run is one pull request, never a direct change. GitHub enforces that, and it also&#10;requires CI to pass first. CI, short for continuous integration, is the set of automated&#10;checks that run on every proposed change.&lt;/li&gt;&#10;&lt;li&gt;A file named &lt;code&gt;PAUSED&lt;/code&gt; stops a bot, and a cap on unreviewed pull requests stops a bot&#10;that&amp;rsquo;s running away.&lt;/li&gt;&#10;&lt;li&gt;A failed run still opens a pull request, with a &amp;ldquo;Run problems&amp;rdquo; section that says what&#10;went wrong. That&amp;rsquo;s the October 10 lesson, built into the template.&lt;/li&gt;&#10;&lt;li&gt;Every bot starts as a written plan: what it&amp;rsquo;s for, how much of my time it may take each&#10;week, what&amp;rsquo;s off-limits, when to shut it down and the open questions, all before any&#10;code.&lt;/li&gt;&#10;&lt;li&gt;Every bot gets its own GitHub access token, a key that only works on the one repo and&#10;expires on a set date.&lt;/li&gt;&#10;&lt;li&gt;Every bot keeps its own &lt;code&gt;BOT.md&lt;/code&gt; up to date.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;You can try it without an AI. Download the repo and run the example, and the program runs&#10;on sample data: it writes a digest, adds a line to a log, and lists the one item with no&#10;link under &amp;ldquo;Run problems&amp;rdquo; instead of quietly using it.&lt;/p&gt;&#10;&lt;h3 id="what-does-it-take-to-run-them"&gt;What Does It Take to Run Them?&lt;/h3&gt;&#10;&lt;p&gt;I run my bots on Grok Bot for now, because it comes with my Cursor subscription and has a&#10;web browser built in. The template doesn&amp;rsquo;t depend on Grok Bot, though. Any AI agent that&#10;can run on a schedule, read and write files, run a program and open a pull request can run&#10;these bots, and the repo has a short guide to controlling a bot from your phone through a&#10;private Slack channel.&lt;/p&gt;&#10;&lt;p&gt;Grok Bot isn&amp;rsquo;t the only option anymore. OpenAI&amp;rsquo;s&#10;&lt;a href="https://help.openai.com/en/articles/20001530-getting-started-with-your-dot"&gt;dots&lt;/a&gt; and&#10;Meta&amp;rsquo;s &lt;a href="https://en.wikipedia.org/wiki/Muse_(AI_agent)"&gt;Muse&lt;/a&gt; are newer services that also&#10;give each AI agent a cloud computer with a browser, and each one has its own advantages&#10;when it comes to data. For example, Grok Bot appears to be able to search posts on X, and&#10;Muse can search and monitor Facebook. I&amp;rsquo;m currently using Muse to watch a Buy Nothing group&#10;for anyone giving away a golf bag.&lt;/p&gt;&#10;&lt;p&gt;Before Grok Bot, I built &amp;ldquo;automations&amp;rdquo; with OpenClaw, and then with Hermes because it&#10;handled failures better, using OpenRouter to choose the AI model. You can do some pretty&#10;powerful things that way, but you&amp;rsquo;re ultimately limited by the model. I&amp;rsquo;ve found that&#10;useful bots still need one of the most capable models, and the best bang for your buck&#10;right now seems to be the monthly subscriptions from the leading AI labs. Open-weight&#10;models like DeepSeek, which anyone can download and run, are promising, but in my&#10;experience they weren&amp;rsquo;t really cheaper than something like Grok Bot. My guess is that the&#10;AI companies are covering some of the cost today, so that could change.&lt;/p&gt;&#10;&lt;p&gt;Whichever service you use, I&amp;rsquo;d encourage you to build a bot for whatever keeps piling up in&#10;your inbox. Run it by hand for a few days and check every digest against the original&#10;messages before you put it on a schedule, and every time it gets something wrong, add a&#10;rule. I&amp;rsquo;m planning a follow-up post with step-by-step directions for building a school&#10;digest bot like this one. And if a bot turns into something you rely on, that&amp;rsquo;s the time&#10;to write its rules and profile down in a repo, so its memory belongs to you and it can move&#10;to the next service with you.&lt;/p&gt;&#10;&lt;p&gt;Thanks for reading.&lt;/p&gt;&#10;</description></item><item><title>I spent 21 months building a trading system, then had AI models tell me not to start the company</title><link>https://redgeoff.com/posts/dont-start-the-company/</link><pubDate>Tue, 20 Oct 2026 08:00:00 -0700</pubDate><guid>https://redgeoff.com/posts/dont-start-the-company/</guid><description>&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/dont-start-the-company/verdict-hero.jpg"&#10;&#9;&#9;&#9;alt="Two tilted memo pages of gray placeholder lines on a dark background, each stamped in red with the same verdict: PERSONAL ONLY"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: author, drawn with code&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;This is a story about how I spent 21 months building a trading system, wrote down what&#10;it would take for it to become a company, and then had two AI models tell me that it&#10;hadn&amp;rsquo;t.&lt;/p&gt;&#10;&lt;p&gt;If you&amp;rsquo;ve read my last three posts, you know TopSet. It&amp;rsquo;s the system I built, mostly with&#10;AI coding agents, to pick a portfolio of stocks with machine-learning models and trade it&#10;with real money. &lt;a href="https://redgeoff.com/posts/ai-agents-test-gate/"&gt;The first post&lt;/a&gt; was&#10;about the automated tests that made agent-written code safe to run,&#10;&lt;a href="https://redgeoff.com/posts/trading-model-leakage/"&gt;the second&lt;/a&gt; was about the ways&#10;information from the future leaked into my models, and&#10;&lt;a href="https://redgeoff.com/posts/backtest-noise/"&gt;the third&lt;/a&gt; was about noise, and ended with&#10;a promise to explain what I decided to do once I knew that my model&amp;rsquo;s results couldn&amp;rsquo;t&#10;be told apart from luck. This is that post, and you don&amp;rsquo;t need to have read the others&#10;to follow it. There are no charts of returns in it, and that&amp;rsquo;s on purpose.&lt;/p&gt;&#10;&lt;h2 id="why-did-i-give-myself-a-deadline"&gt;Why Did I Give Myself a Deadline?&lt;/h2&gt;&#10;&lt;p&gt;I started TopSet because I saw promise in using machine learning to find patterns in&#10;the stock market, and my early results looked promising enough that I thought I had&#10;something. I also knew that a project like this can absorb years without ever answering&#10;the question it was started to answer. So, I gave myself a kill criterion, i.e., a&#10;condition, decided ahead of time, under which I would stop: about two years full time&#10;to find out whether there was a business here. If there wasn&amp;rsquo;t, I&amp;rsquo;d go back to leading&#10;engineering teams.&lt;/p&gt;&#10;&lt;p&gt;The first live trading bot started on December 2, 2024, and I went full time in January&#10;2025. By September 2026, the system had run continuously on live capital for almost two&#10;years, through about 1,600 pull requests (each one a reviewed change to the code), and&#10;I was close enough to the deadline that I needed an answer I could trust.&lt;/p&gt;&#10;&lt;p&gt;To be honest, I already suspected what the answer would be. Around the beginning of the&#10;summer of 2026, I&amp;rsquo;d started to think that, measured over the long run, my results&#10;weren&amp;rsquo;t statistically strong enough to justify starting a company. But a suspicion is&#10;easy to argue yourself out of, especially when you want the other answer, so I wanted a&#10;process that would either confirm it or talk me out of it.&lt;/p&gt;&#10;&lt;h2 id="how-do-you-ask-an-ai-whether-to-start-a-company"&gt;How Do You Ask an AI Whether to Start a Company?&lt;/h2&gt;&#10;&lt;p&gt;Most of the work happened before I asked anything. The obvious way would have been to&#10;open a chat and type &amp;ldquo;here&amp;rsquo;s my trading system, should I start a company?&amp;rdquo; I&amp;rsquo;d have&#10;gotten an encouraging answer, because I&amp;rsquo;d have described the system the way founders&#10;describe their systems. Instead, on September 17, 2026, I wrote two documents first,&#10;working with a Cursor agent.&lt;/p&gt;&#10;&lt;p&gt;The first was an index of everything in the repository: what was built, what was live,&#10;and, just as importantly, what had already been tried and abandoned. Every closed&#10;experiment was listed with its verdict and a link to where it was decided. The point&#10;was that the evaluation would start from the record, not from my pitch.&lt;/p&gt;&#10;&lt;p&gt;The second was the evaluation itself, written as a set of gates. Each gate was a question&#10;with a written condition for failing it, so the evaluator couldn&amp;rsquo;t decide what counted&#10;as failure after seeing the answer. There were five:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Differentiation. What does a customer get here that a cheap index fund, i.e., a fund&#10;that simply holds every stock in an index like the S&amp;amp;P 500, doesn&amp;rsquo;t?&lt;/li&gt;&#10;&lt;li&gt;Evidence. Does the case for a company rest on anything unproven, or on anything I&amp;rsquo;d&#10;already tested and abandoned?&lt;/li&gt;&#10;&lt;li&gt;Regulatory fit. Can the product be marketed honestly within the rules for investment&#10;advisers? In the US, anyone paid to manage or advise on other people&amp;rsquo;s investments&#10;generally has to register with regulators, and there are strict rules about what&#10;they can say about performance.&lt;/li&gt;&#10;&lt;li&gt;Economics. Can it work for one person without raising money?&lt;/li&gt;&#10;&lt;li&gt;The personal path. Found a company, run a 90-day trial to see whether anyone would&#10;pay, keep it for my own money only, or stop and look for a job?&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The differentiation gate, for example, failed if the honest pitch collapsed to&#10;&amp;ldquo;levered / concentrated market beta with model complexity attached.&amp;rdquo; Beta is how much a&#10;portfolio moves with the market as a whole. In other words, if the product was really&#10;just the stock market with extra risk and a complicated model on top, it failed. That&#10;matters because extra risk is cheap to buy: anyone can get more of the market&amp;rsquo;s ups&#10;and downs by adding a little leverage to a low-cost index fund, so a product built on&#10;that would be charging for something customers can already get cheaply.&lt;/p&gt;&#10;&lt;p&gt;I also wrote down what the evaluator was not allowed to do. The brief at the top says&#10;&amp;ldquo;Do not treat years invested … as evidence of a company.&amp;rdquo; And a list titled&#10;&amp;ldquo;Anti-patterns (do not do these)&amp;rdquo; includes &amp;ldquo;Soften a STOP / JOB SEARCH verdict to&#10;protect sunk cost.&amp;rdquo; Sunk cost is the time and money already spent, which can&amp;rsquo;t be&#10;recovered whatever you decide next. The person most likely to want that verdict&#10;softened was me.&lt;/p&gt;&#10;&lt;p&gt;There&amp;rsquo;s a small irony in that brief. Under go-to-market, i.e., how customers would find&#10;the product, I&amp;rsquo;d written that I would attract them by blogging about building&#10;AI-powered investment products. Here I am, blogging about it.&lt;/p&gt;&#10;&lt;h2 id="what-did-the-memos-say"&gt;What Did the Memos Say?&lt;/h2&gt;&#10;&lt;p&gt;About half an hour after those documents were committed, Claude Fable 5.1, running in&#10;Claude Code, worked through the evaluation against the index, the issues and pull&#10;requests it pointed to, and its own web research on competitors and regulation. The&#10;next morning, Cursor&amp;rsquo;s agent, Composer, did the same evaluation independently.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/dont-start-the-company/how-the-memo-was-made.png"&#10;&#9;&#9;&#9;alt="Flow chart, top to bottom. September 17: an index of the whole record, abandoned experiments included, and five gates, each with its failure condition written first. September 17 to 18: Claude Fable 5.1 in Claude Code and Composer in Cursor each write a memo. Both say personal only. September 20: Claude Opus reviews both memos and finds the same two mistakes in each. September 20 to October 10: 25 arguments tried, each written down. Verdict unchanged."&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;How the evaluation ran, from the record to the verdict. Dates are 2026.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Both came back with the same verdict: &amp;ldquo;personal only.&amp;rdquo; Keep running the system on my&#10;own money, and don&amp;rsquo;t turn it into a company. The first memo spelled it out: don&amp;rsquo;t found&#10;one now, and don&amp;rsquo;t raise money. It summed up the honest pitch as &amp;ldquo;concentrated,&#10;high-turnover S&amp;amp;P exposure with ML attached, viable only inside an IRA,&amp;rdquo; which is very&#10;close to the failure condition I&amp;rsquo;d written for the first gate. It also recommended&#10;that I start the job search right away, rather than waiting for my two-year mark.&lt;/p&gt;&#10;&lt;p&gt;That phrase &amp;ldquo;viable only inside an IRA&amp;rdquo; needs some context. My system rebalances&#10;roughly once a month, which means it sells stocks it has held for less than a year. In&#10;an ordinary brokerage account, those profits are short-term capital gains, which in the&#10;US are taxed at the same rate as income, every year, and that tax drag erases a lot of&#10;the advantage of trading actively. In a retirement account like an IRA, trades aren&amp;rsquo;t&#10;taxed as they happen, so that drag disappears. So, any product built on my system would&#10;have had to be sold into retirement accounts.&lt;/p&gt;&#10;&lt;p&gt;I was heartbroken. I was 21 months in, and I felt close to something big. The answer&#10;was that launching a financial product on what I had wasn&amp;rsquo;t justified.&lt;/p&gt;&#10;&lt;h2 id="can-two-ai-models-be-wrong-in-the-same-way"&gt;Can Two AI Models Be Wrong in the Same Way?&lt;/h2&gt;&#10;&lt;p&gt;Yes, and that was the first thing I learned once I started pushing back.&lt;/p&gt;&#10;&lt;p&gt;On September 20, I had Claude Opus review both memos. It found the same two mistakes in&#10;each. The first was that both memos described the strategy as riskier than the version&#10;I actually run, calling it levered or high-beta when it moves with the market about&#10;one for one.&lt;/p&gt;&#10;&lt;p&gt;The second mistake was about those retirement accounts. Both memos said they were small&#10;by nature, because the government caps how much you can contribute to an IRA each year.&#10;That&amp;rsquo;s true for new contributions, but it misses rollover IRAs. When people leave a job,&#10;they often move their whole 401(k), i.e., their workplace retirement savings, into an&#10;IRA, and a rollover isn&amp;rsquo;t subject to the yearly cap. So, the accounts the product would&#10;have been sold into could be much larger than the memos assumed. The correction added&#10;to both memos says so plainly.&lt;/p&gt;&#10;&lt;p&gt;Neither mistake changed the verdict. But they changed how I read the agreement. Two&#10;models that read the same index and the same prompt are not two independent opinions.&#10;They share the blind spots of the record they were given, and, as the IRA mistake&#10;showed, they can also share a wrong assumption about the world that wasn&amp;rsquo;t in the record&#10;at all. Agreement between them is worth something, just much less than it feels like&#10;when you first read it.&lt;/p&gt;&#10;&lt;p&gt;So, I did with the memos what I&amp;rsquo;d started doing with my research: I reviewed them again&#10;with a different model, and I kept going until the review stopped finding anything&#10;major.&lt;/p&gt;&#10;&lt;h2 id="how-many-ways-can-you-argue-with-a-verdict"&gt;How Many Ways Can You Argue With a Verdict?&lt;/h2&gt;&#10;&lt;p&gt;I didn&amp;rsquo;t accept it. I spent days trying to poke holes in it, and every time I thought&#10;of a reason the memos might be wrong, I had it investigated. I didn&amp;rsquo;t sit down with a&#10;list of counterarguments. Each one was a follow-up thought I had while I was digesting&#10;the idea that there wasn&amp;rsquo;t going to be a company.&lt;/p&gt;&#10;&lt;p&gt;Each one also got written down, in a section of the memo called &amp;ldquo;Arguments tried and&#10;retired,&amp;rdquo; with a rule at the top: &amp;ldquo;A new argument should say which of these it is not.&amp;rdquo;&#10;In other words, before I could spend another afternoon on an idea, I had to check that&#10;I hadn&amp;rsquo;t already lost that argument once.&lt;/p&gt;&#10;&lt;p&gt;By October 10, there were 25 entries. Here are a few of them, so you get a feel for it:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Sell to financial advisers instead of to individual investors. It would fix the cost&#10;of finding customers one at a time, but professional buyers screen on exactly the&#10;long, audited track record that I didn&amp;rsquo;t have, so it raises the bar instead of&#10;lowering it.&lt;/li&gt;&#10;&lt;li&gt;Charge only a performance fee, i.e., a share of the profits instead of a flat fee. US&#10;rules generally allow performance fees only for clients who are already wealthy,&#10;which excludes the very customers every other idea targeted.&lt;/li&gt;&#10;&lt;li&gt;Wrap the strategy in an actively managed ETF, an exchange-traded fund that trades on&#10;the stock market like a single stock, to solve the tax problem. ETFs can avoid&#10;passing capital gains on to their investors through in-kind redemptions: when a&#10;large investor cashes out, the fund hands over shares of the stocks it holds rather&#10;than selling them for cash, so the gains are never realized inside the fund. The&#10;structure works, and it&amp;rsquo;s how established funds avoid distributing gains, but it&#10;costs hundreds of thousands of dollars a year to run, so it only makes sense for a&#10;fund that&amp;rsquo;s already large.&lt;/li&gt;&#10;&lt;li&gt;Registering as an adviser turned out to be much cheaper than the memos assumed, if&#10;I did the paperwork myself. That was true, and it was kept as a correction, but&#10;cheaper registration doesn&amp;rsquo;t create any evidence that the strategy is better than&#10;an index fund.&lt;/li&gt;&#10;&lt;li&gt;Instead of waiting years for the evidence, change the portfolio so that the evidence&#10;arrives sooner. Holding more stocks would make the portfolio swing less relative to&#10;the market, and with less noise, a real edge would show up in fewer years. The&#10;arithmetic worked, but the data didn&amp;rsquo;t cooperate: the model&amp;rsquo;s picks only stood out at&#10;the very top of its list, so adding more stocks diluted the edge as fast as it cut&#10;the noise.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/dont-start-the-company/arguments-tried.png"&#10;&#9;&#9;&#9;alt="A five-by-five grid of 25 cards, one per argument, in the order raised. Sixteen gray cards were retired, six blue cards were kept in part, such as rollover IRAs not being capped and registering as an adviser myself, and three amber cards are still open, including whether it is software rather than advice and having the customer approve each trade."&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;All 25 arguments from the memo, in the order I raised them. The blue ones fixed something the memos had gotten wrong; none of them changed the verdict.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;A handful were kept in part, because they fixed something the memos had gotten wrong.&#10;A few are still open, including two that only a securities lawyer can answer. None of&#10;them moved the verdict.&lt;/p&gt;&#10;&lt;h2 id="what-happened-on-the-day-the-verdict-almost-flipped"&gt;What Happened on the Day the Verdict Almost Flipped?&lt;/h2&gt;&#10;&lt;p&gt;On September 22, I ran an evidence review that went my way, four times in a row. Each of&#10;the four was a correction, and each one made the project look better than the memos had&#10;said. One of them confirmed that my execution costs, i.e., what I lose between deciding&#10;to trade and actually getting filled at the broker, were essentially zero, which is a&#10;measurement I&amp;rsquo;d spent months building the tooling to make.&lt;/p&gt;&#10;&lt;p&gt;The pull request I opened that day was drafted to change the verdict from &amp;ldquo;personal&#10;only&amp;rdquo; to &amp;ldquo;90-day discovery,&amp;rdquo; i.e., go and find out whether real customers would pay.&#10;Then I checked the biggest of the four results on a second, longer set of price&#10;history. The first set covered about 8 years and the second about 24. On the 24 years, the result didn&amp;rsquo;t hold. It was the second favorable&#10;result in three days to fail on the longer history. The pull request that merged says,&#10;near the top, &amp;ldquo;This PR was originally drafted as a revision to &lt;code&gt;90-DAY DISCOVERY&lt;/code&gt; on the&#10;eight-year evidence. Checking the 24-year pool reversed it.&amp;rdquo;&lt;/p&gt;&#10;&lt;p&gt;If you haven&amp;rsquo;t spent time with market data, this pattern is worth knowing about,&#10;because it&amp;rsquo;s the rule rather than the exception. Over a few years, it&amp;rsquo;s surprisingly&#10;easy to find a way of picking stocks that beats the market, partly because there are so&#10;many ways to try and some of them will win by chance. Over longer periods, most of those&#10;advantages shrink or disappear. One well-known study, by David McLean and Jeffrey Pontiff&#10;in the &lt;em&gt;Journal of Finance&lt;/em&gt; in 2016, looked at 97 stock-market patterns that academic&#10;papers had found to predict returns. After the papers were published, the patterns&#10;earned, on average, 58% less than they had in the original research.&lt;/p&gt;&#10;&lt;p&gt;It was incredibly disappointing. As an entrepreneur, it&amp;rsquo;s very exciting when you feel&#10;like you&amp;rsquo;ve discovered something significant, and when it turns out not to be&#10;significant, it&amp;rsquo;s hard to come down from that high. Higher highs and lower lows are the&#10;life of an entrepreneur.&lt;/p&gt;&#10;&lt;h2 id="so-what-actually-decides-it"&gt;So, What Actually Decides It?&lt;/h2&gt;&#10;&lt;p&gt;Looking back at all 25 arguments, the pattern is simple. Packaging, pricing, partners&#10;and the sales channel can all change the business questions. Only time, with real money&#10;and results that can&amp;rsquo;t be revised after the fact, changes the evidence question, and&#10;nothing I could build or buy makes that time pass faster.&lt;/p&gt;&#10;&lt;p&gt;The reason it takes so long comes down to signal and noise. If a strategy&amp;rsquo;s average&#10;advantage is small compared with how much its results swing from year to year, then a&#10;few good years look the same as a few lucky ones. At the size of advantage on offer&#10;here, and with how much a concentrated portfolio, one that holds relatively few stocks,&#10;swings around the market, telling skill from luck with live money would take decades,&#10;not quarters. That&amp;rsquo;s the same wall the momentum index in my last post hit, even after I&#10;reconstructed 24 years of its history.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/dont-start-the-company/years-to-tell-skill-from-luck.png"&#10;&#9;&#9;&#9;alt="A curve falling from upper left to lower right on a log scale: the years of results needed before an edge is twice its own noise, against how big the edge is compared with how much it swings around the index. A ratio of 1 needs 4 years, 0.5 needs 16 and 0.25 needs 64. A dashed red line at 2 years marks about how long I have traded live."&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;The textbook arithmetic, not my system&amp;rsquo;s numbers: the smaller the edge compared with its swings, the more years it takes before luck can be ruled out.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;My system has now run continuously on live capital for almost two years, and on the&#10;evidence so far, its results are not statistically distinguishable from cheaper&#10;alternatives. In other words, the difference is too small, compared with the noise, to&#10;rule out luck. Even the most celebrated stock pickers go through long stretches of&#10;trailing the index. A non-professional launching a financial product on that evidence&#10;just didn&amp;rsquo;t hold up.&lt;/p&gt;&#10;&lt;p&gt;Somewhere in the middle of the arguing, after the first memo was drafted, I read Burton&#10;Malkiel&amp;rsquo;s &lt;em&gt;A Random Walk Down Wall Street&lt;/em&gt;. I&amp;rsquo;d read a lot of finance books, but never&#10;this one. Malkiel&amp;rsquo;s argument, first published in 1973 and updated through many editions&#10;since, is that markets absorb new information so quickly that consistently beating&#10;them is extremely hard, so that an investor who buys and holds a low-cost, diversified&#10;index fund is likely to do better than most portfolios carefully picked by professionals. I&#10;generally knew that broad index funds are near impossible to beat, but hearing it laid&#10;out with that much research behind it was very helpful. I felt like I&amp;rsquo;d spent nearly&#10;two years proving the thesis of the book!&lt;/p&gt;&#10;&lt;h2 id="was-it-21-months-wasted"&gt;Was It 21 Months Wasted?&lt;/h2&gt;&#10;&lt;p&gt;Some people will read this as 21 months spent proving a negative. I don&amp;rsquo;t regret it. I&#10;went deep on the principles behind quantitative trading: finding and storing data you&#10;can trust, building machine-learning pipelines that are both fast and reliable, and&#10;building execution that&amp;rsquo;s fast and reliable with real money on the line. I learned a&#10;ton. That being said, I do wish the outcome had been different.&lt;/p&gt;&#10;&lt;p&gt;It&amp;rsquo;s always better if you can validate an idea up front, so that you don&amp;rsquo;t spend more&#10;time than you need to chasing something that won&amp;rsquo;t work. The kill criterion and the&#10;gates were my attempt at that. But I also don&amp;rsquo;t think I could have been confident&#10;either way without doing the work. Sometimes failing along the way, and the hindsight&#10;that comes with it, is the only way to know.&lt;/p&gt;&#10;&lt;h2 id="what-happens-now"&gt;What Happens Now?&lt;/h2&gt;&#10;&lt;p&gt;I stopped building toward a company. The system keeps running on my own accounts, and I&#10;still do research on the side in between other things. The evidence question stays&#10;open, and if it&amp;rsquo;s ever answered, it will be answered by time rather than by another&#10;memo.&lt;/p&gt;&#10;&lt;p&gt;Next, I&amp;rsquo;m going back to leading engineering teams, and I&amp;rsquo;m taking with me the habits&#10;that made this decision possible: writing the gates before looking at the result,&#10;holding my own ideas to the same evidence bar I&amp;rsquo;d hold anyone else&amp;rsquo;s, and treating a&#10;pre-committed stop as something to carry out rather than renegotiate.&lt;/p&gt;&#10;&lt;p&gt;Thanks for reading.&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;&lt;em&gt;This describes software I run on my own accounts. It is not investment advice and I&#10;do not offer investment services.&lt;/em&gt;&lt;/p&gt;&#10;</description></item><item><title>Changing only the random seed made my trading model look twice as good</title><link>https://redgeoff.com/posts/backtest-noise/</link><pubDate>Mon, 12 Oct 2026 14:27:00 -0700</pubDate><guid>https://redgeoff.com/posts/backtest-noise/</guid><description>&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/backtest-noise/seeds-hero.jpg"&#10;&#9;&#9;&#9;alt="Eleven thin gray lines wandering right from a single bright starting point on a dark background, spreading apart as they go, with one glowing red line among them ending highest"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: author, drawn with code&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;This is a story about how I found out that most of the decisions in one of my research&#10;logs were coin flips, and about what I changed so that it wouldn&amp;rsquo;t happen again.&lt;/p&gt;&#10;&lt;p&gt;In September 2026, I ran the same research model eleven times. The code, the data and&#10;the settings were identical each time, and the only thing I changed was the random&#10;seed. The best of those eleven runs scored about twice as well as the worst.&lt;/p&gt;&#10;&lt;p&gt;That would just be a curiosity if I hadn&amp;rsquo;t spent months making decisions by comparing a&#10;single run against a single other run. When we went back through the research log for&#10;that model, 14 of the 21 decisions it recorded, mostly about whether to keep a feature&#10;or throw it out, rested on a difference smaller than what the seed alone could produce.&lt;/p&gt;&#10;&lt;p&gt;If you&amp;rsquo;re new to this, here&amp;rsquo;s a bit of background. I build models that pick stocks,&#10;and I can&amp;rsquo;t test one by waiting ten years to see how it does. So, I backtest it: I&#10;replay history, let the model make its picks as if it were living through each date,&#10;and score those picks against what actually happened next. I also run hyperparameter&#10;searches, which backtest hundreds or thousands of versions of a model with different&#10;settings and compare them. Over the last couple of years, that has added up to a lot of&#10;models.&lt;/p&gt;&#10;&lt;p&gt;In &lt;a href="https://redgeoff.com/posts/trading-model-leakage/"&gt;my last post&lt;/a&gt;, I wrote about&#10;leakage, where information from the future sneaks into the replay and makes a backtest&#10;look better than anything you could have done for real. This post is about two other&#10;ways of fooling yourself: noise, and the choices you make as the researcher. They&amp;rsquo;re&#10;harder to catch, because neither of them is a bug in the code.&lt;/p&gt;&#10;&lt;h2 id="where-did-this-start"&gt;Where Did This Start?&lt;/h2&gt;&#10;&lt;p&gt;It started with a rerun. Earlier in 2026, I had measured what my live trades actually&#10;cost to execute, and the answer was close to nothing. That made me wonder whether a few&#10;research lines that I had already closed deserved a second look, since some of them had&#10;been judged with a cost assumption that was now clearly too pessimistic. So, an agent&#10;reran some of the old experiments.&lt;/p&gt;&#10;&lt;p&gt;When I reviewed the agent&amp;rsquo;s work, the conclusions held up, but several of the&#10;supporting numbers were wrong. Fixing those meant actually running a check that had&#10;been skipped, and that check reran a configuration from the old log with the identical&#10;command. The log had recorded it as a solid result. The rerun came back negative.&lt;/p&gt;&#10;&lt;p&gt;The first question was whether the engine was random at all, and it wasn&amp;rsquo;t. Two runs&#10;with the same seed on the same machine produced results that were identical down to the&#10;byte. So, whatever had changed between the original run and the rerun, it wasn&amp;rsquo;t&#10;chance, and the data changes since then became their own investigation.&lt;/p&gt;&#10;&lt;p&gt;However, it raised a question that I had never asked. If the engine gives the same&#10;answer every time for a given seed, how much does the answer move when the seed&#10;changes?&lt;/p&gt;&#10;&lt;h2 id="how-much-does-a-result-move-when-nothing-changes"&gt;How Much Does a Result Move When Nothing Changes?&lt;/h2&gt;&#10;&lt;p&gt;First, a little background on why a seed matters at all. The models in that research&#10;line were gradient-boosted trees, and like a lot of tree models, each tree is trained on&#10;a random subset of the features and a random subset of the rows. The seed decides which&#10;subsets. If you change it, you get a different set of trees from the same data, which&#10;is supposed to be a detail.&lt;/p&gt;&#10;&lt;p&gt;On September 8, 2026, Claude and I held one configuration fixed and ran it with eleven&#10;different seeds over the full four-year research window. The best seed scored about&#10;twice as well as the worst. From that spread, you can work out the smallest difference&#10;between two single runs that you can trust, i.e. a difference that you&amp;rsquo;d only expect to&#10;see by chance about one time in twenty.&lt;/p&gt;&#10;&lt;p&gt;Then we went back through the log. It recorded 21 decisions that had been made by&#10;comparing one run against one other run, and 14 of them rested on a smaller difference&#10;than that. Some were features I had thrown out as harmful, and four were features I had&#10;kept because they seemed to help. In other words, the evidence on file couldn&amp;rsquo;t tell&#10;any of those 14 variants apart from the run they were compared against, in either&#10;direction.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/backtest-noise/decisions-vs-noise.png"&#10;&#9;&#9;&#9;alt="Horizontal bar chart of 21 keep-or-drop decisions from a research log, sorted by the size of the difference each rested on, as a multiple of what changing the random seed alone can produce. A red line marks 1x. Fourteen bars, including four decisions to keep a feature, end left of the line, inside the noise; seven extend past it."&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Each bar is one of the 21 keep-or-drop decisions in the research log, measured by the difference it rested on, as a multiple of what the random seed alone can produce. Fourteen fall short of the line.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;One of those four soon confirmed it. A later rerun, done properly this time with the&#10;same seed on both sides, found that a feature the log had recorded as helping was&#10;actually hurting. That&amp;rsquo;s exactly what you&amp;rsquo;d expect from a difference inside the noise,&#10;since it can come out either way.&lt;/p&gt;&#10;&lt;p&gt;Fortunately, none of those features were in production, so nothing needed reverting.&#10;But the same habit of comparing one run against one other run was also in my more&#10;active research lines. When we measured the noise for the daily research engine the&#10;same way, one recorded decision stood out. In a long/short research line, which buys some&#10;stocks and bets against others by shorting them, the time of day to trade had been chosen on a difference between 17 and 29 times smaller than that&#10;engine&amp;rsquo;s noise. In other words, the backtest couldn&amp;rsquo;t separate the candidate times at&#10;all. The same measurement also showed that the worst loss from peak to trough was even&#10;more sensitive to the seed than the score was, and more than doubled between the&#10;gentlest seed and the harshest.&lt;/p&gt;&#10;&lt;p&gt;So, when does my live system trade? Shortly after the open rather than at the opening&#10;bell, and I&amp;rsquo;d defend that without any backtest. The first stretch after the market opens is the most volatile part of the&#10;day, which makes it hard to hit a price target or to model the price you&amp;rsquo;ll actually&#10;get. That reasoning is about how the market works, and no seed can flip it.&lt;/p&gt;&#10;&lt;p&gt;The rules that came out of all this are short:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;No feature is declared harmful, or kept, on a difference smaller than the measured&#10;noise.&lt;/li&gt;&#10;&lt;li&gt;Comparisons either use the same seed on both sides, or run several seeds per side&#10;and compare the averages.&lt;/li&gt;&#10;&lt;li&gt;The seed, the number of threads and where the data came from are stamped into every&#10;recorded result, so that any number in the log can be reproduced.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;There was one more surprise in the details. The library we were using doesn&amp;rsquo;t pick a&#10;random seed when you don&amp;rsquo;t give it one. It uses fixed defaults, so every &amp;ldquo;unseeded&amp;rdquo; run&#10;in the log had been perfectly reproducible all along, and had been sitting on one&#10;arbitrary draw that nobody had recorded or varied. Moreover, setting the seed to 0&#10;doesn&amp;rsquo;t reproduce those runs, because it overrides the defaults with different values.&#10;The agent had assumed that it would, and there&amp;rsquo;s now a test that pins the fact that it&#10;doesn&amp;rsquo;t.&lt;/p&gt;&#10;&lt;p&gt;This was tough to learn, as it had taken me a lot of time to engineer myself into those&#10;conclusions. Once the coin flips came to light, I kept pushing to see if there was a&#10;way to improve the statistical significance of my results. That question, how long it takes before you&#10;can tell a real edge from luck, runs through the rest of this post.&lt;/p&gt;&#10;&lt;h2 id="can-i-rebuild-an-index-fund-from-scratch"&gt;Can I Rebuild an Index Fund From Scratch?&lt;/h2&gt;&#10;&lt;p&gt;A couple of weeks later, I wanted a fair comparison for my own model: something cheap&#10;and simple that I could hold it up against, over long stretches of history that&#10;included real bear markets. The obvious candidate was a public momentum index fund,&#10;which holds the stocks that have risen the most over the past year. It&amp;rsquo;s one of the&#10;cheapest and best-known ways to buy a simple stock-picking rule, which makes it a fair&#10;bar for any model that picks stocks. The problem was&#10;that the fund only goes back to 2015, and its index&amp;rsquo;s history before that is the index&#10;provider&amp;rsquo;s own backtest.&lt;/p&gt;&#10;&lt;p&gt;So, the plan was to rebuild the index from its published rules, using my own data, and&#10;run it back through history myself.&lt;/p&gt;&#10;&lt;p&gt;The catch is that a rebuilt index involves a lot of choices, e.g. how far back to&#10;measure momentum, how to size each position and when to rebalance (reshuffle the&#10;portfolio), and every one of them is a knob. If I build the benchmark, look at how my model compares, and then adjust&#10;the benchmark, I&amp;rsquo;ve tuned my own yardstick.&lt;/p&gt;&#10;&lt;p&gt;To avoid that, the spec was written first and merged before any construction code&#10;existed. It included a gate that the rebuild had to pass over the years where the real&#10;fund exists: its annual return had to land within three percentage points of the&#10;fund&amp;rsquo;s, its risk-adjusted return (how much it earned for how bumpy the ride was) had to&#10;be within a fixed margin, and its returns had to move with the fund&amp;rsquo;s, period by&#10;period, with a correlation of at least 0.8, where 1 means moving in lockstep. All&#10;three had to pass or the rebuild was disqualified. The spec even explains why it was&#10;written first: every parameter is a researcher degree of freedom, and my research log&#10;already had four results on record that had been retracted after a closer look.&lt;/p&gt;&#10;&lt;p&gt;Writing the gate first wasn&amp;rsquo;t a new habit for me. When working with LLMs, I&amp;rsquo;ve found&#10;that you have to have very clear acceptance criteria, especially if you want an agent&#10;to run unsupervised for long periods of time. I set the initial rules and then refined&#10;them with the models.&lt;/p&gt;&#10;&lt;p&gt;There was one more guard. The benchmark was deliberately not registered as one of my&#10;models, as registered models can be fed into the hyperparameter search. Otherwise,&#10;someone, or more likely an agent, could have swept the benchmark&amp;rsquo;s own parameters and&#10;kept the best-scoring version. Changing it requires editing the code, which shows up in&#10;the history.&lt;/p&gt;&#10;&lt;p&gt;On September 23, 2026, the first build failed the gate by a wide margin. The leading&#10;suspect was that it weighted every stock equally, while the real index weights by&#10;company size. So, the second build pulled historical share counts from EDGAR, the SEC&amp;rsquo;s&#10;filing database, and weighted by size. That closed about a tenth of the gap, and it&#10;failed again.&lt;/p&gt;&#10;&lt;p&gt;That second failure was actually useful. A pass would have justified buying a&#10;commercial dataset to reach further back in time, but closing only a tenth of the gap&#10;meant that better share counts weren&amp;rsquo;t the problem. So, I found out that the data&#10;wasn&amp;rsquo;t worth buying without having to buy it.&lt;/p&gt;&#10;&lt;h2 id="was-that-moving-the-goalposts"&gt;Was That Moving the Goalposts?&lt;/h2&gt;&#10;&lt;p&gt;This is the part I&amp;rsquo;ve thought the hardest about how to tell honestly, because the spec&#10;said that if the gate failed, the work stopped. It failed twice, and I didn&amp;rsquo;t stop.&lt;/p&gt;&#10;&lt;p&gt;What changed was the diagnosis. The benchmark had been built to rebalance on my model&amp;rsquo;s&#10;dates, roughly once a month, so that a comparison between the two would only differ in&#10;how they picked stocks. Meanwhile, the gate asked it to track a fund that rebalances&#10;twice a year. Those two requirements contradict each other, so the gate was close to&#10;impossible to pass by construction, and both failures had to be read in that light.&lt;/p&gt;&#10;&lt;p&gt;So, the work split into two builds with different jobs. The first is a faithful&#10;replica on the fund&amp;rsquo;s own twice-a-year schedule, and its only purpose is to pass the&#10;gate and prove that the index can be recreated from my data at all. The second is the&#10;monthly build that my model gets compared against, which is then justified by the&#10;replica passing. The thresholds never moved, and each new attempt was written up as its&#10;own issue, with the reason it might close the gap, before it was run.&lt;/p&gt;&#10;&lt;p&gt;On the fund&amp;rsquo;s own schedule alone, the replica still failed. Then it was weighted by&#10;the shares available to public investors (the free float, which is what the index&#10;uses), rather than every share a company has issued, and it passed. That happened in&#10;the third pull request of the day, about an hour and a quarter after the first failure&#10;was merged.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/backtest-noise/gap-to-the-real-fund.png"&#10;&#9;&#9;&#9;alt="Bar chart of the gap in annual return between the rebuilt index and the real fund over six steps: 7.41 and 6.66 percentage points on my model&amp;#39;s monthly dates, then 5.42 on the fund&amp;#39;s own schedule, all above a dashed limit of 3 set before the first build; then 1.93 with free-float weights, 1.50 with the reference-date lag and daily volatility found from the fund&amp;#39;s holdings, and 0.86 with the provider&amp;#39;s published weight caps and buffer rule, all below it."&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;The gap between the rebuilt index and the real fund&amp;rsquo;s annual return, step by step, against the pre-registered limit of three percentage points. The first two were scored on my model&amp;rsquo;s monthly schedule, the rest on the fund&amp;rsquo;s.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;That being said, I don&amp;rsquo;t think &amp;ldquo;the thresholds never moved&amp;rdquo; settles it. A gate that&#10;you&amp;rsquo;re allowed to keep attempting is a search of its own, and with enough attempts,&#10;something will eventually pass. What convinced me that this one hadn&amp;rsquo;t just been&#10;searched into passing was the next step.&lt;/p&gt;&#10;&lt;h2 id="what-if-you-fit-to-holdings-instead-of-returns"&gt;What If You Fit to Holdings Instead of Returns?&lt;/h2&gt;&#10;&lt;p&gt;A replica that matches the fund&amp;rsquo;s return could still be holding the wrong stocks, as&#10;lots of different portfolios produce similar returns over a few years, especially when&#10;they&amp;rsquo;re all tilted the same way. However, the fund has to file its actual holdings with&#10;the SEC, so there&amp;rsquo;s a much harder target available: the list of roughly 100 companies&#10;it held at each rebalance, and how much of each it held.&lt;/p&gt;&#10;&lt;p&gt;So, with the gate already passed, Claude compared the replica against those filings.&#10;The first result said that where the replica held the same company as the fund, it&#10;sized it almost exactly right, but about a third of the companies were different.&lt;/p&gt;&#10;&lt;p&gt;Two bugs in the comparison itself came out along the way. It had been pitting a freshly&#10;rebalanced replica against holdings from months after the fund&amp;rsquo;s last rebalance, and&#10;the code that matched company names to tickers had been quietly dropping as much as a&#10;third of the fund&amp;rsquo;s holdings. For example, a check for &amp;quot; CO&amp;quot; ate the &amp;ldquo;Cos.&amp;rdquo; in &amp;ldquo;Marsh &amp;amp;&#10;McLennan Cos., Inc.,&amp;rdquo; which then matched nothing. After both fixes, the replica still&#10;disagreed with the fund on about a third of the companies, so the difference was real&#10;and it was in how the replica ranked stocks.&lt;/p&gt;&#10;&lt;p&gt;Part of that ranking was still a guess, and the agent couldn&amp;rsquo;t read the index&#10;provider&amp;rsquo;s methodology document, because the provider&amp;rsquo;s site blocked automated&#10;downloads. So, it swept the uncertain parameters and scored each setting by how many of&#10;the fund&amp;rsquo;s actual holdings the replica matched, never by return.&lt;/p&gt;&#10;&lt;p&gt;One sweep found that the index measures momentum about 42 trading days before each&#10;rebalance, rather than on the rebalance date itself. That moved the share of matching&#10;companies from about two thirds to about 80%, and it did better on the filings that&#10;were held out of the sweep, which it never saw while choosing, than on the ones it was&#10;fitted to.&lt;/p&gt;&#10;&lt;p&gt;Another sweep, over how many weeks of volatility (how much a stock&amp;rsquo;s price swings) to&#10;measure, came back flat across a&#10;tenfold range. That turned out to be the clue, as the form was wrong rather than the&#10;length. A line in the fund&amp;rsquo;s own documentation described volatility as measured on&#10;daily returns, not weekly, and on the holdings, one year of daily returns beat the old&#10;assumption on the held-out filings. A shorter window won on the filings it was fitted to&#10;and lost on the held-out ones, which is what overfitting looks like: a setting that fits&#10;the quirks of the data it was tuned on rather than the real pattern. So, the agent took&#10;the round one-year value instead.&lt;/p&gt;&#10;&lt;p&gt;Neither change was chosen by the gate, and the commit messages say so when the gate&#10;didn&amp;rsquo;t cooperate. The lag narrowed the return gap, while two of the gate&amp;rsquo;s other&#10;measures got slightly worse. Switching to daily volatility didn&amp;rsquo;t move the return gap&#10;at all, and it was adopted on the strength of the documentation and the held-out&#10;holdings.&lt;/p&gt;&#10;&lt;p&gt;Then I downloaded the methodology document by hand, and its appendix confirmed both.&#10;Volatility is the &amp;ldquo;standard deviation of daily price returns,&amp;rdquo; and momentum is measured&#10;between month-ends ending two months before the rebalance, which from a late-March&#10;rebalance is about 42 trading days. That&amp;rsquo;s exactly the lag the sweep had found! The&#10;document also corrected a couple of things we had guessed wrong, including the cap on&#10;how much any one company can weigh and the order of the rules for keeping existing&#10;holdings, and those brought the gap down to under one percentage point.&lt;/p&gt;&#10;&lt;p&gt;I think this is the most useful thing I took from the whole exercise. Fitting the same&#10;parameters to returns would have picked a volatility window that, measured against the&#10;holdings, didn&amp;rsquo;t matter at all, and I would have declared the spec identified. On the&#10;other hand, matching about 100 specific companies across 13 real rebalances isn&amp;rsquo;t&#10;something that a single free parameter does by luck. So, if you&amp;rsquo;re validating a model&#10;or a reconstruction, look for the target that is hardest to hit by accident, and fit to&#10;that.&lt;/p&gt;&#10;&lt;h2 id="the-gate-was-green-but-were-the-numbers-right"&gt;The Gate Was Green, but Were the Numbers Right?&lt;/h2&gt;&#10;&lt;p&gt;The replica still only started in 2011, because the free source of company sizes it&#10;used begins around then. To reach back further, Claude and I used the same trick as in&#10;my last post. An S&amp;amp;P 500 index fund holds every company in the index in proportion to&#10;its size, and it has filed those holdings with the SEC going back to 1995. So, the&#10;value of each holding in those filings tells you the company&amp;rsquo;s size on that date.&lt;/p&gt;&#10;&lt;p&gt;The longer replica passed the gate again on the years where the real fund exists, with&#10;about 81% of its companies in common with the fund and an average weight error of 0.13&#10;percentage points against the fund&amp;rsquo;s own filings.&lt;/p&gt;&#10;&lt;p&gt;Then I reviewed it, and the review still found three wrong numbers:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;The daily return series silently dropped one real day of returns in every rebalance&#10;period. After the fix, it agreed with the period-by-period returns, and a regression&#10;test now pins that.&lt;/li&gt;&#10;&lt;li&gt;For a company that joined the index between two filings, a fallback took its size&#10;from the next filing. That&amp;rsquo;s leakage, since it uses a filing nobody could have seen&#10;yet, which is my last post in a single sentence. The effect was small, but the&#10;default is now strictly point-in-time, i.e. it only uses what was known on each&#10;date.&lt;/li&gt;&#10;&lt;li&gt;Prices jumped at the point where one data vendor&amp;rsquo;s history hands over to another&amp;rsquo;s,&#10;so the long runs were redone on a single source, and the seam is filed as its own&#10;bug, which is still open.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The way I do these reviews isn&amp;rsquo;t sophisticated. On delicate or complex work, I ask a&#10;model to go through the result and find what&amp;rsquo;s wrong, often switching to a different&#10;model from the one that did the work, and I repeat this until the review stops finding&#10;anything major. All three of these turned up after every check had already passed.&lt;/p&gt;&#10;&lt;p&gt;Two more findings from that review were about noise rather than bugs. The first was that&#10;leaving out the companies that later failed, the survivorship bias from my last post,&#10;flattered the result by a measurable amount. It flattered it the most during the&#10;dot-com bust, because the missing companies were the ones that collapsed. The second&#10;was that shifting every rebalance date by a single week, earlier or later, moved the&#10;result about as much as fixing the survivorship bias had. Nobody chooses the calendar&#10;date their backtest happens to rebalance on, yet it moved the answer as much as a real&#10;data fix.&lt;/p&gt;&#10;&lt;h2 id="so-could-it-tell-itself-from-luck"&gt;So, Could It Tell Itself From Luck?&lt;/h2&gt;&#10;&lt;p&gt;No. After all of that, with a gate written in advance, passed on returns, checked&#10;against holdings and reviewed until the review went quiet, the reconstruction gave me 24&#10;years of a momentum index, most of them from before the fund existed. Over those 24&#10;years, its edge over the plain S&amp;amp;P 500 isn&amp;rsquo;t statistically established, meaning that&#10;it isn&amp;rsquo;t large enough, compared with the noise, to rule out luck.&lt;/p&gt;&#10;&lt;p&gt;The reconstruction did what it was built to do. When the edge you&amp;rsquo;re measuring is small&#10;and markets are noisy, telling skill from luck takes decades, not quarters, and even 24&#10;years can fall short. My own model runs into the same wall: on the evidence so far, it&#10;isn&amp;rsquo;t statistically distinguishable from cheaper alternatives. What I decided to do&#10;about that is the subject of my next post.&lt;/p&gt;&#10;&lt;h2 id="what-id-do-from-day-one"&gt;What I&amp;rsquo;d Do From Day One&lt;/h2&gt;&#10;&lt;p&gt;If I were starting a research project like this again, this is the list I&amp;rsquo;d hand to&#10;myself and to every agent working with me:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Measure the noise before the first comparison. Run one configuration across several&#10;seeds and work out the smallest difference you can trust.&lt;/li&gt;&#10;&lt;li&gt;Write the gate before the build. Freeze the spec and the pass/fail criteria first,&#10;with the reason for each, and make the thing you&amp;rsquo;re testing hard to tune by&#10;accident.&lt;/li&gt;&#10;&lt;li&gt;Never widen a threshold. If the gate itself turns out to be wrong, say so in writing,&#10;split the job and keep the threshold.&lt;/li&gt;&#10;&lt;li&gt;Fit to the hardest target you can find, e.g. holdings rather than returns.&lt;/li&gt;&#10;&lt;li&gt;Review the work again after the gate passes, with a different model if you can,&#10;until the review stops finding things.&lt;/li&gt;&#10;&lt;li&gt;Shift your dates. If moving the calendar by a week moves the answer, the answer is&#10;smaller than it looks.&lt;/li&gt;&#10;&lt;li&gt;Keep the graveyard. Every closed experiment stays in the repo with the gate it was&#10;judged against and its verdict, so that the next person, or the next agent, can see&#10;what was already tried and why it failed.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Thanks for reading.&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;&lt;em&gt;This describes software I run on my own accounts. It is not investment advice and I&#10;do not offer investment services.&lt;/em&gt;&lt;/p&gt;&#10;</description></item><item><title>My first trading models knew which companies would survive</title><link>https://redgeoff.com/posts/trading-model-leakage/</link><pubDate>Mon, 05 Oct 2026 15:38:00 -0700</pubDate><guid>https://redgeoff.com/posts/trading-model-leakage/</guid><description>&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/trading-model-leakage/survivors-hero.jpg"&#10;&#9;&#9;&#9;alt="Rows of grey gravestones receding into fog under a dusk sky, with a single bright red line chart climbing through them from lower left to upper right"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: author, drawn with code&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;My first live trading bot started trading real money on December 2, 2024. The models&#10;behind it had been trained on years of stock-market history, and every company in&#10;that history had something in common: it was in the S&amp;amp;P 500 on the day I trained the&#10;model.&lt;/p&gt;&#10;&lt;p&gt;That sounds harmless until you think about what it leaves out. A company that was in&#10;the index in 2016 and went bankrupt in 2019 wasn&amp;rsquo;t in the training data at all, and a&#10;company that joined the index in 2025 was in it all the way back. In other words, the&#10;models learned from a version of the market where nothing ever failed and every stock&#10;was already a future winner.&lt;/p&gt;&#10;&lt;p&gt;That was a leak, and it went live. It wasn&amp;rsquo;t the only one either.&lt;/p&gt;&#10;&lt;p&gt;If you haven&amp;rsquo;t built one of these systems, here&amp;rsquo;s what I mean by a leak. You can&amp;rsquo;t&#10;test a trading model by waiting ten years to see how it does, so instead you backtest&#10;it: replay history, let the model pick stocks as if it were living through each date,&#10;and score those picks against what actually happened next. A leak is any information&#10;that sneaks into that replay which the model couldn&amp;rsquo;t have had on the date in&#10;question. It doesn&amp;rsquo;t have to be anything as obvious as tomorrow&amp;rsquo;s price. Knowing which&#10;companies will still be around in five years is enough, and the result is a backtest&#10;that looks better than anything you could have done for real.&lt;/p&gt;&#10;&lt;p&gt;The standard defence is to train walk-forward. For each rebalancing date (the days the&#10;portfolio is reshuffled, roughly monthly for me), the model trains only on data from&#10;before that date, picks its stocks, holds them until the next rebalancing date, and&#10;then the whole thing steps forward and repeats. That way, no single model is ever&#10;scored on data it was trained on. Walk-forward is necessary, but as you&amp;rsquo;ll see, every&#10;leak in this post got through it, because each one smuggled the future in somewhere&#10;other than the training dates: the list of stocks, the shape of the data, or the data&#10;itself, through prices adjusted the wrong way or figures dated before anyone could have&#10;known them.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/trading-model-leakage/walk-forward-leaks.png"&#10;&#9;&#9;&#9;alt="Diagram of walk-forward training: four steps, each training on the past, picking on the rebalancing date and holding until the next one, moving forward in time. Below it, the three ways the leaks in this post got past walk-forward: the list of stocks (survivorship), the shape of the data (columns and rows decided by the whole run), and the data itself (prices adjusted the wrong way, or figures dated before anyone could know them). At the bottom, the check that catches the second: training step by step must give bit-for-bit the same picks as training all at once."&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;So, this post is about the leaks I shipped, the ones I caught in research before they&#10;shipped, and the times an AI was sure it had found a leak and was wrong. Every one of&#10;them is a reason that backtests are hard to trust, including your own. Along the way&#10;I&amp;rsquo;ve ended up with one rule for all three: you can&amp;rsquo;t reason your way to the answer. You&#10;need a test that makes a leak show up as a number.&lt;/p&gt;&#10;&lt;h2 id="survivors"&gt;Survivors&lt;/h2&gt;&#10;&lt;p&gt;What gave it away was a backtest that was too good. I tune the model by running&#10;hyperparameter searches, where I backtest hundreds of versions of the model with&#10;different settings and compare them. In August 2025, one of those runs came back with a&#10;2020 that was simply impossible, and most of that year&amp;rsquo;s profit came from a single&#10;holding. When I went to look it up, neither Yahoo Finance nor&#10;Google Finance had any prices for its ticker before 2021, and my note from the&#10;investigation reads: &amp;ldquo;Perhaps the symbol was renamed?&amp;rdquo;&lt;/p&gt;&#10;&lt;p&gt;It had been. It was Chesapeake Energy, which had been dropped from the S&amp;amp;P 500 in&#10;2018, did a 1-for-200 reverse split in April 2020, went bankrupt later that year, and&#10;eventually merged, took on a new name and rejoined the index in 2025. My backtest was&#10;holding it in 2020, two years after it had left the index, because the training code&#10;only knew about the index as it stood today. (Keep that reverse split in&#10;mind, as it shows up again later in this post.)&lt;/p&gt;&#10;&lt;p&gt;Fixing survivorship bias turned out to be mostly a data problem. First, I needed the&#10;S&amp;amp;P 500&amp;rsquo;s membership for every day of the training period, not just the current list.&#10;I built this from the public record of index changes and had a couple of AI models&#10;double-check the list. Then I needed price history for the companies that had left the&#10;index, many of which no longer trade. Finally, the training loop had to resolve the&#10;set of stocks it was allowed to pick from (its universe) date by date, so that a model&#10;trained on 2016 data only ever saw companies that were actually in the index in 2016.&lt;/p&gt;&#10;&lt;p&gt;When the fix first ran, the backtest fell apart. The note I left myself in the pull&#10;request was, in all caps, &amp;ldquo;THE ANNUALIZED RETURN IS NEAR 0!!!&amp;rdquo; That&amp;rsquo;s what removing a&#10;leak is supposed to do, and it&amp;rsquo;s a very unpleasant thing to watch.&lt;/p&gt;&#10;&lt;p&gt;The fix reached the production training path in late September 2025, about ten months&#10;after the first live trade.&lt;/p&gt;&#10;&lt;p&gt;There was one experiment from that period that I&amp;rsquo;m still glad I ran. I wondered whether&#10;a small, controlled leak might be acceptable: train the first stretch of the model on a&#10;later snapshot of the index, so that it learned from today&amp;rsquo;s large companies, and then&#10;walk forward point-in-time from there. It gave no reliable improvement. My conclusion&#10;at the time was that a model trained on a fixed list of companies never sees a stock&#10;enter or leave the index, which leaves it less prepared for the real index, where that&#10;happens all the time.&lt;/p&gt;&#10;&lt;p&gt;Unfortunately, that wasn&amp;rsquo;t the end of it. A year later, in October 2026, a copy of the&#10;equal-weight S&amp;amp;P 500 that an agent and I had rebuilt from my research data beat the&#10;real equal-weight index fund by a margin that a copy has no business beating it by.&#10;The two moved almost in lockstep, so the copy was tracking the right thing, but it was&#10;consistently doing better. That&amp;rsquo;s the same &amp;ldquo;too good&amp;rdquo; signature, and it turned out to&#10;be survivorship again, coming from two places.&lt;/p&gt;&#10;&lt;p&gt;The first was the membership list itself. The public record of index changes I&amp;rsquo;d built&#10;it from is sparse before about 2007, so the early lists were short and slightly wrong.&#10;To check, we rebuilt membership from the holdings of an S&amp;amp;P 500 index fund, which has&#10;to file its complete list of holdings with the SEC. Against those filings, my list was&#10;74 companies short in 2001 and still 5 short in 2009, and it even contained a few companies&#10;that hadn&amp;rsquo;t gone public yet. The AI models I&amp;rsquo;d asked to double-check the list in 2025&#10;hadn&amp;rsquo;t caught any of this.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/trading-model-leakage/sp500-membership-vs-spy.png"&#10;&#9;&#9;&#9;alt="Line chart from 1995 to 2026 of how many S&amp;amp;P 500 companies my list held on each date SPY filed its holdings with the SEC. SPY&amp;#39;s count sits near 500 throughout. My list starts at 379 in 1995, 121 short, is 74 short in 2001 and 5 short in 2009, and matches from 2010."&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;How many S&amp;amp;P 500 members my list knew about, against the holdings SPY filed with the SEC. The gap is the least my 2025 fix still left out, since the list also held a few companies that hadn&amp;rsquo;t gone public yet.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;The second was prices. Many of the companies that failed&#10;outright, like Lehman Brothers, Bear Stearns, Enron and WorldCom, have no price data in&#10;either of my data sources, so even with a correct list they can never be picked or&#10;held in a backtest.&lt;/p&gt;&#10;&lt;p&gt;This one mostly affects my long-history research backtests rather than the live&#10;system, since the data the live configuration was chosen from covers 99.6% of index&#10;members. But it means that for my backtests before about 2016, the honest reading is&#10;&amp;ldquo;probably an upper bound&amp;rdquo;, and fixing it properly needs a data source that still sells&#10;prices for companies that no longer exist.&lt;/p&gt;&#10;&lt;h2 id="so-what-did-it-cost"&gt;So, What Did It Cost?&lt;/h2&gt;&#10;&lt;p&gt;I don&amp;rsquo;t know, and I think the reason is worth explaining.&lt;/p&gt;&#10;&lt;p&gt;First, a correction to the intuition. A live model can&amp;rsquo;t see the future, because on the&#10;day it makes a prediction there&amp;rsquo;s no future data to see. Today&amp;rsquo;s S&amp;amp;P 500 list is the&#10;correct list for today. So, the live models were never peeking at tomorrow&amp;rsquo;s prices.&#10;The damage was in two other places:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;The backtests I used to choose the model and its settings were inflated by the leak,&#10;so the configuration that went live was selected on a flattering number.&lt;/li&gt;&#10;&lt;li&gt;The live model had been trained on a history with no failures in it, so whatever it&#10;had learned, it had learned from survivors.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Putting a price on that requires comparing what the model expected with what the&#10;account actually did, period by period, and in 2025 I had no mechanism for tracking&#10;that gap. Moreover, the model worked from end-of-day closing prices while the orders&#10;filled the next morning, so any gap I computed mixed model error with hours of ordinary&#10;market movement.&lt;/p&gt;&#10;&lt;p&gt;That was a consequence of how the early system worked. It ran at a fairly leisurely&#10;pace after the market closed: download the day&amp;rsquo;s prices, retrain the model, decide&#10;what to buy and sell, and queue the orders for the next morning. Since then, a lot of&#10;optimization and engineering has gone into doing all of that during the trading day.&#10;The system pulls a price snapshot at a fixed time in the morning, trains the model on&#10;it and starts trading, with a median of about 12 minutes from the snapshot to the first&#10;fill. For bots that run this way, the model&amp;rsquo;s prices and the fill prices are now minutes&#10;apart instead of a night apart.&lt;/p&gt;&#10;&lt;p&gt;There are also now reports that split a bot&amp;rsquo;s live result into what the model&#10;predicted, what the portfolio&amp;rsquo;s composition delivered and what execution cost. When I finally ran those reports across the live history in September&#10;2026, every period I analyzed had run without a fixed price time, so the execution&#10;numbers came with a caveat that they couldn&amp;rsquo;t really be read as execution at all. The&#10;measurement that would have priced the leak simply didn&amp;rsquo;t exist yet.&lt;/p&gt;&#10;&lt;h2 id="the-leak-the-llms-couldnt-find"&gt;The Leak the LLMs Couldn&amp;rsquo;t Find&lt;/h2&gt;&#10;&lt;p&gt;Survivorship was actually the second leak to reach production. The first was subtler,&#10;and it&amp;rsquo;s where most of the testing in the rest of this post came from.&lt;/p&gt;&#10;&lt;p&gt;In March 2025, while analyzing the returns of a newer model, I started to worry that&#10;there was leakage in the older one. So, I compared two ways of training the same model.&#10;One trained incrementally, one rebalancing period at a time, exactly as it would run&#10;live and exactly as walk-forward requires. The other trained over the whole period at once. Given the same data and the&#10;same settings, they should have produced the same results, but the annualized returns&#10;were different.&lt;/p&gt;&#10;&lt;p&gt;I asked the LLMs I was working with to explain the difference and they couldn&amp;rsquo;t. The&#10;pull request I opened to debug it says so directly: &amp;ldquo;The LLMs appear to be unable to&#10;determine why the annualized returns for leakage_check.py and train_and_predict.py&#10;differ. Let&amp;rsquo;s roll up our sleeves and figure this out.&amp;rdquo;&lt;/p&gt;&#10;&lt;p&gt;Rolling up my sleeves meant writing a logger. I logged every call to &lt;code&gt;fit()&lt;/code&gt; and&#10;&lt;code&gt;predict()&lt;/code&gt;, along with the shape and columns of every input, to a JSON file without&#10;timestamps, ran both paths and diffed the files. Then I shortened the run until I had&#10;the smallest period that still showed a difference and stepped through it in the&#10;debugger.&lt;/p&gt;&#10;&lt;p&gt;The difference was in the shape of the training matrix. Which columns it had, and which&#10;rows survived a filter for missing values, depended on the data loaded for the whole&#10;run rather than just the training window. When training all at once, the model for an&#10;early period was shaped by companies whose data only began years later. When training&#10;incrementally, it didn&amp;rsquo;t know those companies existed yet. Neither path was reading a&#10;future price directly, but the future was deciding what the training data looked like,&#10;and that turned out to be enough to change the results.&lt;/p&gt;&#10;&lt;p&gt;The comparison that found the leak became a test: train incrementally on part of the&#10;data, continue on the rest, and the picks must be bit-for-bit identical to training on&#10;all of it from scratch. It started as a script that I ran by hand, then became the code&#10;path that production uses to train, and it now runs every week in the automated test&#10;suite. If&#10;I could only keep one test from this whole project, it would be this one.&lt;/p&gt;&#10;&lt;h2 id="the-fix-that-switched-off-most-of-the-model"&gt;The Fix That Switched Off Most of the Model&lt;/h2&gt;&#10;&lt;p&gt;Unfortunately, the March 2025 refactor fixed the leak and broke something else, and&#10;nothing noticed for thirteen months.&lt;/p&gt;&#10;&lt;p&gt;Near the end of the data-preparation step, the refactored code selected the training&#10;columns by matching column names against a list of ticker symbols. The raw return&#10;column for each stock is named after its ticker, so it matched. However, the engineered&#10;features (moving averages and volatility measures built from those returns) have the&#10;ticker as a prefix rather than as the whole name, so they didn&amp;rsquo;t match and were&#10;dropped. A later step, which lines up every iteration&amp;rsquo;s columns with the model&amp;rsquo;s&#10;expected list of features, then quietly put them back, filled with zeros.&lt;/p&gt;&#10;&lt;p&gt;The model ran, the tests passed and the features were all present in the matrix. Every&#10;one of them was zero, and that version went into production along with the leak fix.&lt;/p&gt;&#10;&lt;p&gt;It finally came out in April 2026, while an LLM and I were wiring a new family of&#10;features into the training pipeline. Earlier that same day we had found a different&#10;silent drop, where three new settings were decoded from the model&amp;rsquo;s configuration and&#10;then never passed to the model. When we looked at how much the trained models relied on each&#10;input (their feature importance), every engineered feature had an importance of&#10;exactly zero.&lt;/p&gt;&#10;&lt;p&gt;The fix went in behind a flag so that it could be compared with the old behaviour, and&#10;a week later it became the default. If you read my last post, you may recall a change&#10;that did nothing, where two runs came back bit-for-bit identical. This is the same&#10;smell from a different angle, and I now treat an exact zero with the same suspicion as&#10;an exact match.&lt;/p&gt;&#10;&lt;h2 id="corporate-actions-keep-coming-back"&gt;Corporate Actions Keep Coming Back&lt;/h2&gt;&#10;&lt;p&gt;Most of the leaks I found in 2026 came from the same place: splits, reverse splits and&#10;dividends, which are known as corporate actions.&lt;/p&gt;&#10;&lt;p&gt;A quick primer, since everything below depends on it. A raw price is what a stock&#10;actually traded at on a given day. When a company does a 20-for-1 split, every share&#10;becomes 20 shares worth a twentieth as much, so the raw price drops 95% overnight even&#10;though no one lost a cent. A reverse split is the opposite: 200 shares become one, and&#10;the raw price jumps 200-fold. An adjusted price rewrites the history before each event&#10;so that the series is continuous, e.g. by dividing every price before a 20-for-1 split&#10;by 20, and it does something similar for dividends. Neither series is wrong. They&#10;answer different questions, and the bugs below all came from asking one series a&#10;question meant for the other.&lt;/p&gt;&#10;&lt;p&gt;I told the stock-dividend story in&#10;&lt;a href="https://redgeoff.com/posts/ai-agents-test-gate/"&gt;my last post&lt;/a&gt;, where a test asserted&#10;a data convention that never occurs in practice. Here are three more.&lt;/p&gt;&#10;&lt;h3 id="the-model-learned-to-short-stock-splits"&gt;The Model Learned to Short Stock Splits&lt;/h3&gt;&#10;&lt;p&gt;In a long/short research line, which buys some stocks and bets against others by&#10;shorting them, so that it profits when they fall, one data source&amp;rsquo;s features were&#10;computed from raw prices rather than adjusted ones. So, when Amazon split 20:1 in&#10;June 2022, the raw series showed a one-day move of about −95%. Alphabet&amp;rsquo;s 20:1 split about&#10;six weeks later looked the same, and Tesla&amp;rsquo;s 3:1 split read as −67%.&lt;/p&gt;&#10;&lt;p&gt;A −95% day dominates any ranking of stocks against each other, and the model&amp;rsquo;s&#10;momentum-style features, which look at how much a stock has risen or fallen recently,&#10;carried it for one to three months afterwards. The model learned that stocks with that&#10;shape made good shorts, but they weren&amp;rsquo;t failing companies. They had just split.&lt;/p&gt;&#10;&lt;p&gt;A Copilot agent found it while comparing the same strategy across two data vendors. One&#10;vendor&amp;rsquo;s prices came pre-adjusted, the other&amp;rsquo;s feature panel had been built from raw&#10;prices, and the two short sides disagreed by far more than data noise should explain.&#10;A simple count shows the problem: in a 2019 sample, 9.7% of the symbols in the raw panel&#10;had a single-day move bigger than 50%, against 6.1% in the other vendor&amp;rsquo;s adjusted data.&lt;/p&gt;&#10;&lt;p&gt;What I find interesting is why raw prices had been chosen in the first place. Adjusted&#10;prices rewrite the past, e.g. a 2022 split changes the adjusted price for 2018. That&#10;looks exactly like lookahead, so raw prices seemed like the safe choice. However, these&#10;features were ratios: a price today divided by a price from some days ago. For any&#10;window that doesn&amp;rsquo;t straddle a split, the split factor cancels out and raw and adjusted&#10;prices give identical values. For a window that does straddle a split, the adjusted&#10;ratio is the true economic return and the raw one is an artifact. In other words, the&#10;thing that looked like lookahead wasn&amp;rsquo;t, and the thing chosen to avoid it was the bug.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/trading-model-leakage/amzn-split-raw-vs-adjusted.png"&#10;&#9;&#9;&#9;alt="Two-panel chart of Amazon from March to September 2022. Top: the raw price falls from about $2,450 to about $125 on the June 6 split, while the adjusted price runs straight through. Bottom: the one-month return computed from raw prices drops to about minus 95 percent for 21 trading days after the split; computed from adjusted prices it stays within its normal range, and the two lines are identical outside those 21 days."&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Amazon&amp;rsquo;s 2022 split as raw and adjusted prices. The raw one-month return reads as a 95% crash for a month: the shape the model learned to treat as a good short.&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h3 id="ratios-want-adjusted-levels-want-raw"&gt;Ratios Want Adjusted, Levels Want Raw&lt;/h3&gt;&#10;&lt;p&gt;The mirror image turned up about a month later. The same research line had a&#10;minimum-price filter so that it wouldn&amp;rsquo;t trade anything under $10, and it was being&#10;applied to adjusted prices.&lt;/p&gt;&#10;&lt;p&gt;After a large reverse split, adjustment inflates a stock&amp;rsquo;s earlier history. A shell&#10;company that really traded at a fraction of a cent, and then did a reverse split of a&#10;few hundred to one, ends up with an adjusted history of tens to thousands of dollars.&#10;So, those names passed the $10 floor, were trained on and were shorted, and the backtest&#10;booked wins on positions that nobody could have actually held. Claude found this one&#10;while investigating short-side results that still looked wrong after the earlier fixes.&#10;Gating on the raw price instead took the number of candidate instances priced under&#10;$10 from 11,194 to zero.&lt;/p&gt;&#10;&lt;p&gt;The rule I took from the pair is that a ratio wants adjusted prices, because it&amp;rsquo;s asking&#10;how much a holding changed, while a price level wants raw prices, because it&amp;rsquo;s asking&#10;what the stock actually traded at. Neither series is a safe default.&lt;/p&gt;&#10;&lt;p&gt;After four layers of price corruption had been removed from that research line, its log&#10;summed it up plainly: the earlier positive results had been &amp;ldquo;largely the corruption we&#10;removed.&amp;rdquo;&lt;/p&gt;&#10;&lt;h3 id="the-screen-in-front-of-everything-was-leaking"&gt;The Screen in Front of Everything Was Leaking&lt;/h3&gt;&#10;&lt;p&gt;This last one is the one I find most instructive, because it was in the tool I use to&#10;decide what is worth testing in the first place.&lt;/p&gt;&#10;&lt;p&gt;I screen new signal ideas (candidate inputs for the model) with a quick check that&#10;runs in seconds, before committing to a hyperparameter search that runs for days. It&#10;asks a simple question: on each past date, did ranking stocks by this signal put them&#10;in roughly the right order of what they did next? In September 2026, an agent&#10;using that screen to check a textbook momentum signal for another project found that&#10;the screen itself had two problems.&lt;/p&gt;&#10;&lt;p&gt;The first was that it loaded prices through a function whose &lt;code&gt;adjusted&lt;/code&gt; flag defaults to&#10;&lt;code&gt;False&lt;/code&gt;, and which returns its price column under the key &lt;code&gt;&amp;quot;Adj Close&amp;quot;&lt;/code&gt; either way. The&#10;variable was named for adjusted prices, the column was named for adjusted prices and&#10;every statistic downstream said adjusted, but the data was raw.&lt;/p&gt;&#10;&lt;p&gt;The second was that it built its universe once, for the whole run, from every company&#10;that had ever been in the index. On the dates checked, 42 to 47% of the names it was&#10;ranking on a given date weren&amp;rsquo;t index members on that date. Some joined later, which is&#10;lookahead, since companies are added to the index because they grew. Others had left&#10;years before.&lt;/p&gt;&#10;&lt;p&gt;The tell had been sitting in the documentation for months, as a statistic with a note&#10;beside it saying &amp;ldquo;investigate&amp;rdquo;. It traced back to Chesapeake&amp;rsquo;s 1-for-200 reverse split&#10;in April 2020, the same reverse split from the survivorship investigation a year&#10;earlier. Here, raw prices turned it into a gain of more than 10,000%, and it was counted&#10;twice because the universe carried both the old and the new ticker. The problem also&#10;generalizes. Reverse splits tend to happen to companies in trouble, and companies in&#10;trouble are exactly what sits at the bottom of a ranking by recent performance, so any&#10;signal that sorts on losers is contaminated in the same direction.&lt;/p&gt;&#10;&lt;p&gt;Fortunately, the decisions I had made with that screen were based on rank correlation,&#10;which only asks whether the stocks came out in the right order, not by how much, so a&#10;few enormous outliers barely move it. When the agent recomputed a signal on&#10;corrected data, its rank correlation hardly changed, so those decisions probably still&#10;stand. Two other columns in the screen&amp;rsquo;s output didn&amp;rsquo;t survive. A leak doesn&amp;rsquo;t always&#10;invalidate everything it touches, and most of the work is finding out which numbers it&#10;actually reached.&lt;/p&gt;&#10;&lt;h2 id="time-has-two-dates"&gt;Time Has Two Dates&lt;/h2&gt;&#10;&lt;p&gt;Point-in-time doesn&amp;rsquo;t just mean the date on the row.&lt;/p&gt;&#10;&lt;p&gt;Some data describes a period but only becomes public later. One data vendor only keys&#10;its quarterly company financials by the end of the quarter they describe. A&#10;company&amp;rsquo;s December quarter isn&amp;rsquo;t public until its annual report is filed, around the&#10;start of March for the largest companies, and the vendor then needs time to pick it up.&#10;A reporting lag of 45 trading days after the period end made those numbers &amp;ldquo;known&amp;rdquo; a&#10;week or two before the vendor would actually have had them. That setting had already stood&#10;out in my hyperparameter search results, and the vendor&amp;rsquo;s own documentation settled it, as the&#10;period end is the only date it provides. So, I removed the option.&lt;/p&gt;&#10;&lt;p&gt;EDGAR, the SEC&amp;rsquo;s filing database, does record the filing date, so when I later needed historical share&#10;counts, I keyed them on the date they were filed, typically about five weeks after the&#10;quarter they describe. Keying on the period end would have used numbers that nobody&#10;could have known yet.&lt;/p&gt;&#10;&lt;p&gt;One vendor field had no code bug at all. An earnings-surprise figure (how far a company&amp;rsquo;s reported&#10;earnings beat or missed what analysts expected) was being computed against the&#10;analysts&amp;rsquo; average forecast as it stood after later revisions, rather than as it&#10;stood on the day the earnings came out. The fix was to rebuild it from point-in-time&#10;estimate snapshots, which meant dropping every year before the snapshots begin and&#10;about a fifth of the symbols, which had no snapshots at all. Sometimes an honest fix&#10;makes your dataset smaller.&lt;/p&gt;&#10;&lt;h2 id="the-false-alarms"&gt;The False Alarms&lt;/h2&gt;&#10;&lt;p&gt;Leakage can also go wrong in the other direction.&lt;/p&gt;&#10;&lt;p&gt;In August 2025, in the middle of the survivorship work, two issues drafted with an AI&amp;rsquo;s&#10;help claimed there was lookahead in my volume filter and in how the code decided which&#10;symbols were available. Both read convincingly. When I traced the dates, though, the&#10;rebalancing decision happens at the end of the window the code was reading, so all of&#10;that data was already known. My reply on the first one starts, &amp;ldquo;Actually, I believe we&#10;DO NOT have a lookahead bias.&amp;rdquo;&lt;/p&gt;&#10;&lt;p&gt;A year later, an agent flagged code that was, in its words, &amp;ldquo;target-leakage-shaped&amp;rdquo;:&#10;the training target also appears among the input features. It does, and at a glance it&#10;looks alarming. However, tracing every date boundary showed no lookahead, as the&#10;training window ends where the test window begins and returns are measured forward from&#10;the pick date. The agent&amp;rsquo;s follow-up claim, that the model must therefore be a simple&#10;momentum ranker, didn&amp;rsquo;t survive a measurement either. That&amp;rsquo;s two confident answers,&#10;each corrected by measuring rather than arguing.&lt;/p&gt;&#10;&lt;p&gt;The repo&amp;rsquo;s instruction file for agents now says that any leakage claim must name the&#10;specific date boundary that it violates and cite the code path, and those boundaries&#10;are pinned by tests named &lt;code&gt;test_no_lookahead_*&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h2 id="who-found-what"&gt;Who Found What?&lt;/h2&gt;&#10;&lt;p&gt;When I went back through the issues and pull requests to write this post, the pattern&#10;was clearer than I&amp;rsquo;d remembered. I found the early leaks, the ones that reached&#10;production, from numbers that were too good or didn&amp;rsquo;t match: an impossible year and two&#10;training paths that disagreed. The 2026 leaks were mostly found by agents, and almost&#10;never while they were looking for leaks. They turned up while we were wiring new&#10;features, building a new test harness, comparing two data vendors or checking a rebuilt&#10;index against the real one, with me reviewing what came back. The agents also raised most of the false alarms.&lt;/p&gt;&#10;&lt;p&gt;The tests mostly arrived after the leak that justified them. I don&amp;rsquo;t think that&amp;rsquo;s a&#10;failure. A test written before you&amp;rsquo;ve been bitten is a guess about where the bugs are,&#10;whereas a test written afterwards is a record of where they were.&lt;/p&gt;&#10;&lt;h2 id="making-leaks-show-up-as-numbers"&gt;Making Leaks Show Up as Numbers&lt;/h2&gt;&#10;&lt;p&gt;Here&amp;rsquo;s what actually caught things, roughly in the order that I&amp;rsquo;d add them to a new&#10;project:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;Invariance.&lt;/strong&gt; A result for a past window must not depend on the future. Training&#10;incrementally must match training all at once, and moving a run&amp;rsquo;s end date must not&#10;change an earlier window. The March 2025 leak broke exactly this, and so did a&#10;similar one I found later in a different research line, where extending a run&amp;rsquo;s end&#10;date changed results for years that were already over.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Counts of the impossible.&lt;/strong&gt; Single-day moves beyond 50%, prices under a cent passing&#10;a $10 filter, feature importance of exactly zero, or a year of returns that no&#10;strategy has ever produced. Several of the leaks in this post would have shown up in a&#10;count like that well before they showed up in a result.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Point-in-time by construction.&lt;/strong&gt; Resolve index membership per date, from the best&#10;source you can find, and key vendor fields on when they became knowable, not on the&#10;period they describe.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Compare against something real.&lt;/strong&gt; A copy of an index fund that beats the fund, or&#10;two vendors that disagree, is a leak or a data bug until proven otherwise.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;A pure-noise null.&lt;/strong&gt; Run the whole pipeline on random-walk prices, where there&amp;rsquo;s no&#10;signal by construction. If it finds one, it leaks. In one research line, after four&#10;leak fixes, the null finally sat on zero, and that was the result I trusted most.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Replication on independent data.&lt;/strong&gt; When an agent hands you an encouraging&#10;statistic, rerun it on data it hasn&amp;rsquo;t seen before you believe it.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;&lt;em&gt;This describes software I run on my own accounts. It is not investment advice and I&#10;do not offer investment services.&lt;/em&gt;&lt;/p&gt;&#10;</description></item><item><title>I let AI agents write most of a live-trading codebase. Here's the gate that made it safe.</title><link>https://redgeoff.com/posts/ai-agents-test-gate/</link><pubDate>Mon, 28 Sep 2026 16:50:00 -0700</pubDate><guid>https://redgeoff.com/posts/ai-agents-test-gate/</guid><description>&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/ai-agents-test-gate/railroad-crossing.jpg"&#10;&#9;&#9;&#9;alt="A lowered railroad crossing barrier with glowing red lights across a wet road at dusk, a train blurring past behind it"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: AI-generated&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;For the last 21 months I have been the only engineer on TopSet, a system that trades&#10;real money. It picks a portfolio with machine-learning models, rebalances on a&#10;schedule, and executes through live broker APIs on infrastructure I own end to end.&#10;It is my own capital, not anyone else&amp;rsquo;s.&lt;/p&gt;&#10;&lt;p&gt;Agents wrote most of the code. Claude Code, Cursor, Copilot. About 1,600 pull&#10;requests in those 21 months, which is more than I could have typed in three times&#10;the span.&lt;/p&gt;&#10;&lt;p&gt;That leaves you with a choice everyone adopting these tools runs into. You can read&#10;every line the agent produces, in which case you have bought yourself a slower&#10;version of writing it yourself. Or you can skim, merge, and move fast, in which case&#10;you accumulate code that looks right and isn&amp;rsquo;t. On a system that places orders with&#10;real money, the second option ends with trades you didn&amp;rsquo;t intend.&lt;/p&gt;&#10;&lt;p&gt;I took a third option: make something other than my attention the thing that decides&#10;what ships. Tests became the gate. Nothing merges or deploys without a green build,&#10;and my own reading moved up a level, to design, to test coverage, and to the handful&#10;of results I refuse to let change quietly.&lt;/p&gt;&#10;&lt;p&gt;That worked. It also failed in ways I did not expect, and the failures are the&#10;interesting part, so I&amp;rsquo;ll spend most of this post there.&lt;/p&gt;&#10;&lt;h2 id="what-i-actually-read"&gt;What I actually read&lt;/h2&gt;&#10;&lt;p&gt;&amp;ldquo;Tests are the gate&amp;rdquo; is not the same as &amp;ldquo;I stopped looking.&amp;rdquo; What changed is where I&#10;spend attention.&lt;/p&gt;&#10;&lt;p&gt;Most pull requests get a light review. Research code I skim. Changes to the parts&#10;that decide what to buy or how to trade it, the model training and the execution&#10;paths, I read properly, looking for whether the approach is reasonable rather than&#10;whether every line is correct. The tests cover the lines. I cover the idea.&lt;/p&gt;&#10;&lt;p&gt;Then there is one rule I do not bend. If a change moves a snapshot, or shifts the&#10;result of an end-to-end test, that is a stop. Snapshots and integration results are&#10;supposed to be stable, so a diff in one of them means either the change did something&#10;I didn&amp;rsquo;t intend, or it did something I did intend and I now owe an explanation. An&#10;agent will happily regenerate a snapshot to make a build go green. That is the single&#10;most dangerous thing these tools do, and it is why the snapshot files live in the&#10;repo where a diff is visible in review.&lt;/p&gt;&#10;&lt;p&gt;For anything involving a calculation I have not verified before, the tests are not&#10;enough either. I have the agent write a report script, and there are now 161 of them,&#10;that dumps the pipeline to CSV. I open the CSV in a spreadsheet and check the&#10;arithmetic by hand: return per trade, slippage, annualized return. Then I spot-check&#10;inputs, picking a few prices and going back to the raw data directory to confirm&#10;those were the prices the system actually used.&lt;/p&gt;&#10;&lt;p&gt;It is slow and it is not clever, and it has caught things nothing else would. A test&#10;asserts that a function returns what the author believed it should return. A&#10;spreadsheet asks whether the number is true.&lt;/p&gt;&#10;&lt;h2 id="what-the-gate-is"&gt;What the gate is&lt;/h2&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/ai-agents-test-gate/test-gate.png"&#10;&#9;&#9;&#9;alt="Diagram of the test gate: every agent pull request runs lint, types, about 7,500 tests, an empty-database check, a migration round trip and terraform validate before it can merge and deploy. A weekly suite runs end-to-end rebalancings, a 100-run flake hunt, training snapshots and leakage checks against main. Production errors page me on Discord, and every fix comes back through the same gate. Underneath: what I still check myself."&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Every pull request runs:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;lint (ruff) and type checking (mypy)&lt;/li&gt;&#10;&lt;li&gt;about 7,500 tests across 331 test modules&lt;/li&gt;&#10;&lt;li&gt;an assertion that the test database is empty afterward&lt;/li&gt;&#10;&lt;li&gt;a migration rollback-and-reapply, all the way to base and back&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;terraform validate&lt;/code&gt; against both AWS accounts&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;If any of that fails, nothing merges. The build is the merge gate, and it is not&#10;advisory.&lt;/p&gt;&#10;&lt;p&gt;Once a week, on a schedule, a heavier set runs:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;two end-to-end rebalancing runs against a mock broker&lt;/li&gt;&#10;&lt;li&gt;the same integration test 100 times in a row, looking for flakes&lt;/li&gt;&#10;&lt;li&gt;nine deterministic model-training snapshot runs, pinned inside Docker&lt;/li&gt;&#10;&lt;li&gt;two leakage checks on the training pipeline&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Running the same integration test 100 times only makes sense because the test is not&#10;the same each time. The mock broker fills orders probabilistically: a 20% chance per&#10;10-second cycle, 70% of those complete and the rest partial at a random size, at&#10;random prices. Every iteration produces a different interleaving of fills, partial&#10;fills and timing. A hundred runs is a search for race conditions, not a hundred&#10;repeats of one path.&lt;/p&gt;&#10;&lt;p&gt;What it finds are real concurrency bugs wearing flaky-test costumes. Worker threads&#10;leaking between tests. A queued task waking up after teardown had already deleted the&#10;row it wanted. A data loader that wasn&amp;rsquo;t thread-safe and corrupted a shared index&#10;under load. Each one showed up first as &amp;ldquo;that test fails sometimes.&amp;rdquo;&lt;/p&gt;&#10;&lt;p&gt;The weekly set exists because a flaky gate is worse than no gate. If a test fails once&#10;every thirty runs, and twenty agent PRs land in a week, that one test goes red most&#10;weeks. Have a few like it and something red is always on screen, and you start&#10;ignoring red. Hunting flakes is not hygiene here. It is what&#10;keeps the gate meaningful.&lt;/p&gt;&#10;&lt;h2 id="the-tests-that-matter-are-about-money-not-functions"&gt;The tests that matter are about money, not functions&lt;/h2&gt;&#10;&lt;p&gt;Unit tests are cheap and agents write them happily. They are not what I trust.&lt;/p&gt;&#10;&lt;p&gt;What I trust are end-to-end tests that drive a full rebalancing against a mock&#10;broker, with the awkward parts turned on. They&amp;rsquo;re named after the ways money goes&#10;wrong:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;orders that have to be resubmitted&lt;/li&gt;&#10;&lt;li&gt;a rebalancing that resumes from the middle, with partial fills already on the books&lt;/li&gt;&#10;&lt;li&gt;a resume that has both buys and sells outstanding&lt;/li&gt;&#10;&lt;li&gt;smart orders cancelled mid-flight, on the sell side and then the buy side&lt;/li&gt;&#10;&lt;li&gt;deposits and repeating withdrawals arriving while a rebalancing is in progress&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;These are exactly the control flows a language model gets plausibly wrong.&#10;Interrupted work, partial state, money arriving in the middle of an operation: the&#10;code for each of these reads fine in isolation. It&amp;rsquo;s the interaction that bites.&lt;/p&gt;&#10;&lt;p&gt;Here is one that made it past review and got caught by the constraint at the bottom&#10;of the stack. When the last pending buy of a rebalancing had its amount adjusted for&#10;pending withdrawals, the adjusted value could land on exactly zero. The existing&#10;guard raised an error on a negative amount, but zero isn&amp;rsquo;t negative. The&#10;affordability check, &lt;code&gt;available_cash &amp;gt;= amount&lt;/code&gt;, was &lt;code&gt;0 &amp;gt;= 0&lt;/code&gt;, so it passed. The&#10;order was promoted, and the database rejected it: &lt;code&gt;amount &amp;gt; 0&lt;/code&gt; is a check constraint&#10;on that table.&lt;/p&gt;&#10;&lt;p&gt;The interesting part is what that did to the retry. The integrity error fired inside&#10;the same transaction that carried the completing order&amp;rsquo;s status update, so the&#10;rollback discarded that too. The scheduler came back around, found identical inputs,&#10;and did it again. Not a self-healing retry: a stuck rebalancing.&lt;/p&gt;&#10;&lt;p&gt;The fix took three attempts, and the second one is the useful story. Gating the&#10;promotion on &lt;code&gt;amount &amp;gt; 0&lt;/code&gt; looks correct and does nothing, because the code had&#10;already assigned the adjusted value to a live ORM object. That marked it dirty, and&#10;the next autoflush in the same transaction persisted the zero regardless of whether&#10;the order was ever promoted. The condition guarded the decision, not the mutation.&lt;/p&gt;&#10;&lt;p&gt;I only know that because the failure was reproduced live against &lt;code&gt;main&lt;/code&gt; before any&#10;fix was written, and the first fix was re-run against that same repro rather than&#10;checked by reading the code. When an agent is writing the patch, &amp;ldquo;I read it and it&#10;looks right&amp;rdquo; is the single least reliable signal available to you.&lt;/p&gt;&#10;&lt;h2 id="three-ways-a-green-build-lied-to-me"&gt;Three ways a green build lied to me&lt;/h2&gt;&#10;&lt;p&gt;This is the part I&amp;rsquo;d want to read, so here are the three worst cases, all real, all&#10;from this codebase.&lt;/p&gt;&#10;&lt;h3 id="1-the-test-asserted-a-convention-that-does-not-exist"&gt;1. The test asserted a convention that does not exist&lt;/h3&gt;&#10;&lt;p&gt;Corporate actions have to be applied to historical prices, and stock dividends are&#10;the fiddly ones. My broker&amp;rsquo;s API returns a &lt;code&gt;rate&lt;/code&gt; for each. The code treated that&#10;rate as a bare fraction: a 5% stock dividend arrives as &lt;code&gt;0.05&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;It isn&amp;rsquo;t. It&amp;rsquo;s the ratio of new shares to old. Every one of the 101 stock-dividend&#10;records across 43 symbols in my data has a rate of at least 1.0016. Not one is a&#10;bare fraction. The convention the code assumed does not occur in the data at all.&lt;/p&gt;&#10;&lt;p&gt;The unit test passed, because the test asserted the same wrong convention with a&#10;fixture value of &lt;code&gt;rate: 0.05&lt;/code&gt;, a number that cannot appear in production. The test&#10;and the code were wrong in exactly the same way, which is what you get when both are&#10;written in the same sitting from the same misunderstanding.&lt;/p&gt;&#10;&lt;p&gt;The effect was that each stock dividend roughly halved the adjusted price, and it&#10;compounded across events. One symbol with nine of them came out deflated by about&#10;490×, turning a real price history into a fake ramp of more than a thousand-fold&#10;over the period. And because every price stayed between a few cents and a few&#10;hundred dollars, a check for implausible price levels would not have flagged it&#10;either.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/ai-agents-test-gate/scco-stock-dividends.png"&#10;&#9;&#9;&#9;alt="Log-scale chart of Southern Copper (SCCO) adjusted close from 2016 to 2026. The fixed series runs from about $15 to $200. The buggy series starts about 490 times lower, near 3 cents, and steps up at each of nine stock-dividend ex-dates in 2024 to 2026 until it meets the fixed series."&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Southern Copper&amp;rsquo;s real daily prices, adjusted twice by the same code. The only difference is whether the stock-dividend factor is 1 / rate or 1 / (1 + rate).&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;The fix was a corrected fixture, plus a regression test that runs a nine-event&#10;sequence and asserts the cumulative adjustment factor stays sane. The lesson I took:&#10;a fixture is a claim about the world, and nobody reviews fixtures.&lt;/p&gt;&#10;&lt;h3 id="2-the-test-passed-on-nan"&gt;2. The test passed on NaN&lt;/h3&gt;&#10;&lt;p&gt;In a research tool, an agent added a second way to select a model configuration and&#10;reported the result in the PR: 100% agreement with the existing selector, at every&#10;sample size tried.&lt;/p&gt;&#10;&lt;p&gt;That number was not real. The tool built its pool from a source that was missing the&#10;columns the default scoring version needs, so every score came out NaN. With all&#10;scores NaN, the selector fell through to a fixed tie-break order, which of course&#10;agrees with itself 100% of the time at every sample size.&lt;/p&gt;&#10;&lt;p&gt;The test that was supposed to cover this had been passing on those same NaN scores&#10;for as long as it had existed. It never checked anything. Green, meaningless, and&#10;quoted in a PR description as a finding.&lt;/p&gt;&#10;&lt;p&gt;The follow-up PR made the condition a hard error rather than a silent NaN, pinned&#10;the test to a scoring version where the columns exist, and added guards. It also&#10;flagged that an older result produced the same way might carry the same artifact.&#10;That is the right instinct: when you find a lie, go and check what else it told you.&lt;/p&gt;&#10;&lt;h3 id="3-the-change-did-nothing-at-all"&gt;3. The change did nothing at all&lt;/h3&gt;&#10;&lt;p&gt;An agent wired a new training target through what looked like the right code path.&#10;The first screen ran clean and produced numbers bit-for-bit identical to the&#10;baseline. Every metric, to four decimal places.&lt;/p&gt;&#10;&lt;p&gt;Identical results are not a pass. They&amp;rsquo;re a smell. Two different configurations&#10;producing byte-equal output usually means one of them didn&amp;rsquo;t happen.&lt;/p&gt;&#10;&lt;p&gt;It hadn&amp;rsquo;t. The runner always sets a parameter that routes execution to a completely&#10;separate function, which builds its own training target and never looked at the new&#10;field. Unpickling both runs and diffing the actual selections showed 238 of 238&#10;identical picks for the test year. After the real fix, 0 of 238 were identical, and&#10;a regression test now asserts the two modes produce different output.&lt;/p&gt;&#10;&lt;p&gt;Nothing failed. The compute was spent. The result would have gone into the research&#10;log as &amp;ldquo;no effect,&amp;rdquo; which is a conclusion, and it would have been wrong.&lt;/p&gt;&#10;&lt;h3 id="the-shape-of-all-three"&gt;The shape of all three&lt;/h3&gt;&#10;&lt;p&gt;In every case the build was green, and the number was wrong. Tests prove that the&#10;code does what the tests say. They say nothing about whether what the tests say is&#10;true, whether the code under test ran at all, or whether the data going in is what&#10;you think.&lt;/p&gt;&#10;&lt;p&gt;That is the agent-era failure mode, and it is worse with agents than without them,&#10;for a simple reason: an agent writes the code and the test together, from one&#10;reading of the problem. If the reading is wrong, both artifacts are wrong, and they&#10;agree. A human pair would at least have had two readings.&lt;/p&gt;&#10;&lt;p&gt;What I do about it now, concretely:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Distrust results that are identical, unchanged, or perfect. Check the output, not&#10;the exit code.&lt;/li&gt;&#10;&lt;li&gt;Treat fixtures as claims. If a fixture value can&amp;rsquo;t occur in production, that&amp;rsquo;s a&#10;bug in the test.&lt;/li&gt;&#10;&lt;li&gt;Make impossible states loud. NaN, missing data, and empty inputs should raise, not&#10;flow through and produce a number.&lt;/li&gt;&#10;&lt;li&gt;When something is found to be wrong, go back and check what else that thing told&#10;you.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="the-reviewer-that-isnt-a-test"&gt;The reviewer that isn&amp;rsquo;t a test&lt;/h2&gt;&#10;&lt;p&gt;Some mistakes are not code-shaped and no test will ever catch them.&lt;/p&gt;&#10;&lt;p&gt;So I added a second kind of review: a deliberately skeptical reviewer persona, run&#10;against a finished result rather than a diff, with instructions to argue that the&#10;result is not real. I iterated on the persona with Claude until it stopped being&#10;agreeable.&lt;/p&gt;&#10;&lt;p&gt;It found things I am glad it found. That a configuration had been tuned by&#10;repeatedly checking results on data I had promised myself I would only look at once,&#10;so my holdout wasn&amp;rsquo;t a holdout any more. That a cost model was running with zero&#10;financing cost while the strategy it priced used nearly four times leverage.&lt;/p&gt;&#10;&lt;p&gt;Neither is a bug. Both are the kind of error that invalidates a conclusion, and no&#10;test suite in the world was going to tell me.&lt;/p&gt;&#10;&lt;h2 id="the-gate-after-the-gate"&gt;The gate after the gate&lt;/h2&gt;&#10;&lt;p&gt;CI stops caring the moment something merges. After that the system runs unattended,&#10;on a schedule, against a live broker, and the question changes from &amp;ldquo;is this code&#10;correct&amp;rdquo; to &amp;ldquo;is this thing behaving right now.&amp;rdquo;&lt;/p&gt;&#10;&lt;p&gt;Every ERROR the system logs in production becomes a CloudWatch alarm that posts to a&#10;Discord channel on my phone. That is the whole alerting stack, and for a one-person&#10;operation it is enough: I am not running a rotation, I just need to know.&lt;/p&gt;&#10;&lt;p&gt;The loop from there is the same one I use for everything else. Point an agent at the&#10;production logs, hand it a local copy of the database from around the failure, ask&#10;for a diagnosis and a fix with a test. The logs and the data are usually enough for&#10;it to find the problem, and the test it writes is the thing that stops the problem&#10;coming back. Several of the bugs in this post arrived that way rather than through&#10;CI.&lt;/p&gt;&#10;&lt;p&gt;Worth saying plainly: the tests did not catch those. The tests caught the next&#10;occurrence.&lt;/p&gt;&#10;&lt;h2 id="what-stays-human"&gt;What stays human&lt;/h2&gt;&#10;&lt;p&gt;Architecture. Test design. What &amp;ldquo;done&amp;rdquo; means. Merge judgment when the tests pass and&#10;something still feels off.&lt;/p&gt;&#10;&lt;p&gt;And one more thing that turned out to matter: the repo&amp;rsquo;s own instruction file. Every&#10;time an agent reached a wrong conclusion in a way another agent would repeat, I&#10;wrote the correction down there. One rule says that any claim of data leakage must&#10;name the specific date boundary it violates, because two different agents had&#10;asserted leakage from reasoning alone, and both were wrong. That file is now a list&#10;of the mistakes this codebase invites. It&amp;rsquo;s the closest thing I have to&#10;institutional memory for a team of one.&lt;/p&gt;&#10;&lt;h2 id="what-it-cost"&gt;What it cost&lt;/h2&gt;&#10;&lt;p&gt;The per-PR suite takes about ten minutes. That is the tax on every change, and it is&#10;the number I would defend hardest: ten minutes is short enough that I never route&#10;around it, and long enough to run tests that touch a database and a fake broker.&lt;/p&gt;&#10;&lt;p&gt;The machine bill is every one of the 3,000 GitHub Actions minutes my plan includes,&#10;plus roughly $20 a month for extra minutes on top. The agents themselves are Claude&#10;Pro and Cursor Pro, $20 each. I also paid $40 a month for Copilot until GitHub moved&#10;it to per-token billing, which would have raised my bill more than tenfold, so that&#10;work moved to Claude Code and Cursor. So the tools that wrote most of this codebase&#10;cost tens of dollars a month, like the compute spent checking them, and neither is&#10;what the project actually cost.&lt;/p&gt;&#10;&lt;p&gt;What it actually cost is fixtures and flakes. Writing a mock broker that fails in&#10;realistic ways, maintaining a self-contained dataset so model training can be tested&#10;at all, and then the long tail of chasing tests that fail once in thirty runs. None&#10;of that is glamorous and all of it is load-bearing. Every hour I skipped there came&#10;back later as an hour of not being able to trust a result.&lt;/p&gt;&#10;&lt;p&gt;And sometimes the gate itself is the thing that&amp;rsquo;s broken. The 100-run soak lives in a&#10;shell script rather than inline in CI, because the inline version ran under a POSIX&#10;shell where &lt;code&gt;{1..100}&lt;/code&gt; isn&amp;rsquo;t brace expansion. It was a literal string. The loop ran&#10;exactly once, and reported success, for longer than I&amp;rsquo;d like to admit.&lt;/p&gt;&#10;&lt;p&gt;The tests are not where I over-built. Every gate in this post exists because&#10;something broke, or nearly did, and the existing checks let it through. I would add&#10;all of them again.&lt;/p&gt;&#10;&lt;p&gt;Where I over-built is everything the tests had to cover.&lt;/p&gt;&#10;&lt;p&gt;I started TopSet expecting to turn it into a product, so I built it for customers who&#10;never existed. A bot lifecycle with eleven kinds of change request, each one a queued,&#10;locked, asynchronous workflow object, so that deposits and withdrawals and stops could&#10;be requested safely by someone who wasn&amp;rsquo;t me. Two broker integrations instead of one.&#10;A staging environment that mirrors production, behind a VPN. Cross-account backups&#10;with compliance-grade retention locks on a database that holds my own trades.&lt;/p&gt;&#10;&lt;p&gt;For one person running their own accounts, several of those could have been a script I&#10;ran by hand while watching it.&lt;/p&gt;&#10;&lt;p&gt;That is the part that compounds, and it is why I am putting it in a post about&#10;testing. Every one of those surfaces is something the gate has to cover. The change&#10;requests need end-to-end tests for money arriving mid-rebalance. Two brokers means two&#10;of everything, including the mocks. The staging environment is a second place for&#10;migrations to fail. I did not pay for the product shape once, in the building. I have&#10;paid for it every week since, in the size of the harness that has to keep it honest.&lt;/p&gt;&#10;&lt;p&gt;If I were starting again for a book of one, I would build the trading and the research&#10;properly and leave the product machinery until somebody asked for it.&lt;/p&gt;&#10;&lt;h2 id="if-youre-rolling-this-out-to-a-team"&gt;If you&amp;rsquo;re rolling this out to a team&lt;/h2&gt;&#10;&lt;p&gt;I&amp;rsquo;ve done this alone, with tests I wrote and one person&amp;rsquo;s judgment. That is not the&#10;same problem as a team of fifteen, and I&amp;rsquo;d be careful about anyone who tells you&#10;otherwise. But some of it transfers, and the ordering I&amp;rsquo;d insist on comes from a&#10;team I did run: at a healthcare company, requiring automated tests on every PR is&#10;what took active medium and high bugs down 72% and weekly hotfixes from 7 to 1.5.&#10;That was before agents. It&amp;rsquo;s the same first move.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;strong&gt;Fix the gate before you raise throughput.&lt;/strong&gt; Coverage on the paths that lose&#10;money or data, and no tolerated flakes. Agents multiply whatever your CI already&#10;is.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;The author owns the change, whoever typed it.&lt;/strong&gt; &amp;ldquo;The agent wrote it&amp;rdquo; is not a&#10;review comment, and it is not a defense in a postmortem.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Review the result, not the diff.&lt;/strong&gt; For anything that produces a number, ask&#10;what would look different if the change had done nothing.&lt;/li&gt;&#10;&lt;li&gt;&lt;strong&gt;Measure escaped defects and hotfix rate.&lt;/strong&gt; Not lines, not PR counts. Agents&#10;make those metrics meaningless overnight.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The part I would not change: the gate is what let one person move at this speed on a&#10;system where mistakes cost real money. The part I underestimated: a gate is a claim&#10;about correctness, and claims need checking too.&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;&lt;em&gt;This describes software I run on my own accounts. It is not investment advice and I&#10;do not offer investment services.&lt;/em&gt;&lt;/p&gt;&#10;</description></item><item><title>Turnkey Data Analysis Using DuckDB and Metabase for PostgreSQL &amp; CloudWatch</title><link>https://redgeoff.com/posts/duckdb-metabase-postgresql-cloudwatch/</link><pubDate>Fri, 01 Mar 2024 05:05:39 -0800</pubDate><guid>https://redgeoff.com/posts/duckdb-metabase-postgresql-cloudwatch/</guid><description>&lt;p&gt;How we used DuckDB and Metabase to streamline our data analysis workflow, enabling us to build an email list to contact our frequent users.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/duckdb-metabase-postgresql-cloudwatch/hero.png"&#10;&#9;&#9;&#9;alt="A cartoon duck with glasses sitting at a computer whose screen reads QuackMeUp"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Data Analysis with DuckDB and Metabase. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Back in July 2023, my friend and I were excited about building something with a Large Language Model (LLM). At that time, we noticed ChatGPT had no native internet access. ChatGPT did roll out a beta feature providing Bing search access, but it was retracted shortly after its release. We observed several ChatGPT plugins offering internet access, but none were free. Anticipating OpenAI’s reintroduction of Bing search, we decided to quickly develop our own web search plugin for ChatGPT to attract some users.&lt;/p&gt;&#10;&lt;p&gt;After spending a few days coding and setting up AWS infrastructure, we successfully launched our MindfulDataAI Search Plugin in the ChatGPT Plus store. Impressively, OpenAI approved our plugin within hours. Within days, we observed thousands of web searches being conducted through our plugin. This success spurred us to explore other ideas, such as developing business tools for ChatGPT. We knew our plugin’s relevance might be short-lived, but we were hopeful that we could identify a business-specific problem before then. Indeed, this was underscored in September 2023 when ChatGPT reintroduced Bing search.&lt;/p&gt;&#10;&lt;p&gt;Surprisingly, even after the reintroduction of ChatGPT’s native Bing search, a significant number of users continued using our plugin. Intrigued by this preference, we sought to understand the underlying reasons.&lt;/p&gt;&#10;&lt;p&gt;Our challenge was to analyze user data efficiently. User activities were logged, and email addresses were stored in a PostgreSQL database. Initially, we used CloudWatch Insights to identify the most active users, followed by PostgreSQL queries to look up their email addresses. This method was effective but disjointed. I was also looking for an opportunity to experiment more with DuckDB, so I decided to create an open-source repository, called &lt;a href="https://github.com/redgeoff/QuackMeUp"&gt;QuackMeUp&lt;/a&gt;, along with a guide to help others perform this type of data analysis more efficiently.&lt;/p&gt;&#10;&lt;p&gt;In the following sections, we’ll discuss how we used DuckDB and Metabase to streamline our data analysis workflow, enabling us to build an email list so that we could contact our users to understand why they still preferred our plugin.&lt;/p&gt;&#10;&lt;h2 id="duckdb--metabase"&gt;DuckDB &amp;amp; Metabase&lt;/h2&gt;&#10;&lt;p&gt;&lt;a href="https://duckdb.org/"&gt;&lt;strong&gt;DuckDB&lt;/strong&gt;&lt;/a&gt; is an in-memory, columnar database optimized for quick analytical queries. Its main advantages are speed, ease of setup, and SQL compatibility. It’s particularly useful for fast data analysis without heavy resource requirements.&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="https://www.metabase.com/"&gt;&lt;strong&gt;Metabase&lt;/strong&gt;&lt;/a&gt; is a user-friendly, open-source BI tool. It allows easy data querying and visualization, even for non-technical users. Metabase supports various data sources and is known for its intuitive interface and collaboration features.&lt;/p&gt;&#10;&lt;h3 id="why-use-them"&gt;Why Use Them?&lt;/h3&gt;&#10;&lt;p&gt;For MindfulDataAI, DuckDB offers the speed we need for analyzing logs and PostgreSQL data, while Metabase makes data visualization and sharing straightforward. This combination aims to streamline our data analysis, merging different data sources efficiently and reducing complexity.&lt;/p&gt;&#10;&lt;p&gt;Next, we’ll explore how we integrated these tools into our workflow and the insights we gained.&lt;/p&gt;&#10;&lt;h2 id="high-level-overview-of-the-solution"&gt;High-Level Overview of the Solution&lt;/h2&gt;&#10;&lt;p&gt;Let’s start with an overview of the key components of our solution:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Modify the app’s code so that it logs in JSON format.&lt;/li&gt;&#10;&lt;li&gt;Deploy a scheduled AWS Lambda function that will automate the export of CloudWatch logs to an S3 bucket.&lt;/li&gt;&#10;&lt;li&gt;Download the logs from the S3 bucket using the &lt;code&gt;aws s3 sync&lt;/code&gt; command.&lt;/li&gt;&#10;&lt;li&gt;Run a Python script to extract JSON segments from the logs and save them as &lt;code&gt;.json&lt;/code&gt; files.&lt;/li&gt;&#10;&lt;li&gt;Configure DuckDB to connect to your PostgreSQL instance and to access these &lt;code&gt;.json&lt;/code&gt; files.&lt;/li&gt;&#10;&lt;li&gt;Use Metabase for business intelligence. This setup will enable you to join datasets and create useful analytics dashboards.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;This is how the pipeline works:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/duckdb-metabase-postgresql-cloudwatch/pipeline.png"&#10;&#9;&#9;&#9;alt="Flow diagram: app code writes JSON logs, an AWS Lambda function exports them to S3, aws s3 sync downloads them, format_logs.py extracts and saves the JSON, DuckDB points to Postgres and the JSON logs, and Metabase is used for analysis"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;End-to-End Data Pipeline for Business Analytics. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h2 id="the-detailed-solution"&gt;The Detailed Solution&lt;/h2&gt;&#10;&lt;h3 id="download-the-github-repo"&gt;Download the GitHub repo&lt;/h3&gt;&#10;&lt;p&gt;To make it faster to get started, I’ve packaged all the code in a GitHub repo that you can download with:&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;git clone https://github.com/redgeoff/QuackMeUp&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;Then, &lt;code&gt;cd QuackMeUp&lt;/code&gt; so that you are ready for the next steps.&lt;/p&gt;&#10;&lt;h3 id="install-the-dependencies"&gt;Install the dependencies&lt;/h3&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://docs.docker.com/get-docker/"&gt;Install Docker&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html"&gt;Install the awscli&lt;/a&gt; and run &lt;code&gt;aws configure&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;Install Python, e.g. &lt;code&gt;brew install python@3.11&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;python3.11 -m pip install poetry&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;poetry install&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;Install the Git hooks for code formatting with black: &lt;code&gt;poetry run pre-commit install&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://duckdb.org/docs/installation/"&gt;Install DuckDB&lt;/a&gt;, e.g. &lt;code&gt;brew install duckdb&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h3 id="configure-the-env-file"&gt;Configure the .env file&lt;/h3&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;cp .env.example .env&lt;/code&gt; and then edit &lt;code&gt;.env&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;Choose a value for &lt;code&gt;TERRAFORM_BUCKET_NAME&lt;/code&gt; that will be unique across all AWS users, e.g. &lt;code&gt;myorg-quackmeup-terraform-bucket&lt;/code&gt;, where &lt;code&gt;myorg&lt;/code&gt; is a unique name for your organization. This bucket will be used to remotely store your Terraform state.&lt;/li&gt;&#10;&lt;li&gt;Choose a value for &lt;code&gt;LOGS_BUCKET_NAME&lt;/code&gt; that will be unique across all AWS users, e.g. &lt;code&gt;myorg-quackmeup-logs&lt;/code&gt;, where &lt;code&gt;myorg&lt;/code&gt; is a unique name for your organization. This bucket will be used to store logs exported from CloudWatch.&lt;/li&gt;&#10;&lt;li&gt;Be sure to substitute the value of &lt;code&gt;PG_CONNECTION_STRING&lt;/code&gt; with the connection string for your Postgres instance. Ideally, you’d use the connection string for a read replica.&lt;/li&gt;&#10;&lt;li&gt;You can leave the rest of the values in &lt;code&gt;.env&lt;/code&gt; intact unless you prefer them to be different.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h3 id="deploy-the-infrastructure"&gt;Deploy the infrastructure&lt;/h3&gt;&#10;&lt;p&gt;&lt;a href="https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli#install-terraform"&gt;Install Terraform&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;Load the AWS Console and manually create an S3 bucket with the same name that you specified for &lt;code&gt;TERRAFORM_BUCKET_NAME&lt;/code&gt;.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/duckdb-metabase-postgresql-cloudwatch/terraform-bucket.png"&#10;&#9;&#9;&#9;alt="The AWS Console Create bucket form for the Terraform state bucket"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Create S3 bucket for Terraform state. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Initialize the terraform environment: &lt;code&gt;./terraform/init.sh&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;Make sure the terraform plan doesn’t error out: &lt;code&gt;./terraform/plan.sh&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;The following &lt;code&gt;apply.sh&lt;/code&gt; script will then:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Create an S3 bucket for &lt;code&gt;LOGS_BUCKET_NAME&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;Create an ECR repository for the Lambda code&lt;/li&gt;&#10;&lt;li&gt;Build and push a Docker image to this ECR repository&lt;/li&gt;&#10;&lt;li&gt;Schedule a Lambda (using the Docker image) that will export the latest CloudWatch logs to the S3 bucket every 4 hours&lt;/li&gt;&#10;&lt;li&gt;Create IAM policies and roles as needed&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Deploy the infrastructure with &lt;code&gt;./terraform/apply.sh&lt;/code&gt;&lt;/p&gt;&#10;&lt;h3 id="tag-target-logs-for-export"&gt;Tag target logs for export&lt;/h3&gt;&#10;&lt;p&gt;Open the AWS Console and navigate to the &lt;em&gt;CloudWatch Log Groups&lt;/em&gt; page. For each log that you wish to be exported to S3, simply add a tag with Key=&lt;code&gt;ExportToS3&lt;/code&gt; and Value=&lt;code&gt;true&lt;/code&gt;. The log exporter Lambda runs every 4 hours so you’ll have to wait at least 4 hours before you start seeing any data in the S3 bucket.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/duckdb-metabase-postgresql-cloudwatch/tag-log-groups.png"&#10;&#9;&#9;&#9;alt="The CloudWatch Log Groups page with an ExportToS3 tag set to true"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Tag CloudWatch logs for export. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h3 id="install-quackmeup"&gt;Install QuackMeUp&lt;/h3&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Create the DuckDB instance: &lt;code&gt;./scripts/duckdb/create_db.sh&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;Attach the Postgres instance to DuckDB: &lt;code&gt;./scripts/duckdb/attach_postgres.sh&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;Build the Docker image that will run Metabase with DuckDB support: &lt;code&gt;./scripts/metabase/build_docker.sh&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;Run the Docker containers: &lt;code&gt;docker-compose up -d&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;Seed the Metabase config so that you can just log in: &lt;code&gt;./scripts/metabase/import_db.sh&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h3 id="execute-the-pipeline"&gt;Execute the pipeline&lt;/h3&gt;&#10;&lt;p&gt;After the logs have been updated, run the following pipeline to ingest the updated data into DuckDB:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Use &lt;code&gt;s3 sync&lt;/code&gt; to download the logs from S3: &lt;code&gt;./scripts/s3/download_logs.sh&lt;/code&gt;. Note: if you have a lot of data, this can take a while when it is first run.&lt;/li&gt;&#10;&lt;li&gt;Run a Python script to extract the JSON payloads in the logs so that they can be analyzed with DuckDB: &lt;code&gt;make format_logs&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;Create a DuckDB table pointing to the extracted logs: &lt;code&gt;./scripts/duckdb/import_logs.sh&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;Restart Metabase so that it loads the updated DuckDB: &lt;code&gt;docker restart quackmeup-metabase-1&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h3 id="accessing-metabase"&gt;Accessing Metabase&lt;/h3&gt;&#10;&lt;p&gt;You can then access Metabase by visiting &lt;a href="http://localhost:3000/"&gt;http://localhost:3000&lt;/a&gt; and logging in with:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Email: &lt;code&gt;quackmeup@example.com&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;Password: &lt;code&gt;E!v_#nc$48pqfZJ&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h3 id="troubleshooting"&gt;Troubleshooting&lt;/h3&gt;&#10;&lt;p&gt;DuckDB is incredibly fast because it stores a substantial amount of data in memory. If you encounter errors related to insufficient memory, it’s advisable to open the Docker console and increase the memory allocation. In our experience, for processing 3GB of zipped logs, we needed to increase the memory allocation to 10GB. Alternatively, you can modify the &lt;code&gt;format_logs.py&lt;/code&gt; script so that it filters out any JSON data that you don’t need.&lt;/p&gt;&#10;&lt;h2 id="example-analysis"&gt;Example Analysis&lt;/h2&gt;&#10;&lt;p&gt;Now that we’ve got QuackMeUp running, we can start doing some data Analysis. The following section is specific to our data, but it will help you to visualize the power of the QuackMeUp Analytics pipeline.&lt;/p&gt;&#10;&lt;p&gt;Recall that in the case of MindfulDataAI, we want to get the email addresses of all the frequent users. In our particular case, the search requests are logged in payloads like the following:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-json" data-lang="json"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;#34;json&amp;#34;&lt;/span&gt;: {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;#34;asctime&amp;#34;&lt;/span&gt;: &lt;span style="color:#e6db74"&gt;&amp;#34;2024-01-08 23:57:51,020&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;#34;name&amp;#34;&lt;/span&gt;: &lt;span style="color:#e6db74"&gt;&amp;#34;my_logger&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;#34;levelname&amp;#34;&lt;/span&gt;: &lt;span style="color:#e6db74"&gt;&amp;#34;INFO&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;#34;message&amp;#34;&lt;/span&gt;: &lt;span style="color:#e6db74"&gt;&amp;#34;Response&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;#34;code&amp;#34;&lt;/span&gt;: &lt;span style="color:#e6db74"&gt;&amp;#34;RESPONSE&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;#34;request&amp;#34;&lt;/span&gt;: {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;#34;method&amp;#34;&lt;/span&gt;: &lt;span style="color:#e6db74"&gt;&amp;#34;GET&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;#34;path&amp;#34;&lt;/span&gt;: &lt;span style="color:#e6db74"&gt;&amp;#34;/search&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;#34;query&amp;#34;&lt;/span&gt;: {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;#34;query&amp;#34;&lt;/span&gt;: [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;number of tourists in Orlando 2022&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;#34;body&amp;#34;&lt;/span&gt;: &lt;span style="color:#960050;background-color:#1e0010"&gt;…&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To start, we can look at things like the number of searches each month:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;SELECT&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;SUBSTRING&lt;/span&gt;(json&lt;span style="color:#f92672"&gt;-&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;asctime&amp;#39;&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;7&lt;/span&gt;) &lt;span style="color:#66d9ef"&gt;AS&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;month&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;COUNT&lt;/span&gt;(&lt;span style="color:#f92672"&gt;*&lt;/span&gt;) &lt;span style="color:#66d9ef"&gt;as&lt;/span&gt; num_searches&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;FROM&lt;/span&gt; logs&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;WHERE&lt;/span&gt; JSON_EXTRACT(json, &lt;span style="color:#e6db74"&gt;&amp;#39;$.code&amp;#39;&lt;/span&gt;)&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;&amp;#34;RESPONSE&amp;#34;&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;AND&lt;/span&gt; JSON_EXTRACT(json, &lt;span style="color:#e6db74"&gt;&amp;#39;$.request.path&amp;#39;&lt;/span&gt;)&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;&amp;#34;/search&amp;#34;&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;GROUP&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;BY&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;month&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;ORDER&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;BY&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;month&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;ASC&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can run this query in Metabase by creating a new &lt;em&gt;Question&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/duckdb-metabase-postgresql-cloudwatch/metabase-query.png"&#10;&#9;&#9;&#9;alt="A Metabase question with the SQL query for searches per month"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Metabase query for searches per month. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;And then you can render a nice-looking visualization:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/duckdb-metabase-postgresql-cloudwatch/monthly-searches.png"&#10;&#9;&#9;&#9;alt="A Metabase chart of monthly searches rising in August 2023 and then roughly halving each month"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Monthly searches. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Note: You may notice from our data that it contrasts with the typical “up and to the right” trend. We launched the plugin in late July, which led to a significant increase in searches in August. However, in September, OpenAI reintroduced native Bing search support in ChatGPT. Since then, the number of searches using our plugin has nearly halved each month! 😱. Nonetheless, we still retain a user base, and we are interested in contacting them to understand why they prefer our plugin over the native Bing search.&lt;/p&gt;&#10;&lt;p&gt;We utilize Supabase and its OAuth integrations, which means the users’ email addresses are stored in a PostgreSQL table named &lt;code&gt;users&lt;/code&gt;. By joining the &lt;code&gt;searches&lt;/code&gt; and &lt;code&gt;users&lt;/code&gt; using the &lt;code&gt;user_id&lt;/code&gt;, we can compile a list of users with the highest number of searches:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;SELECT&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; s.num_searches,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; s.user_id,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; s.first_search,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; s.last_search,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; u.raw_user_meta_data&lt;span style="color:#f92672"&gt;-&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;name&amp;#39;&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;as&lt;/span&gt; name,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; u.email&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;FROM&lt;/span&gt; (&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;SELECT&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;COUNT&lt;/span&gt;(&lt;span style="color:#f92672"&gt;*&lt;/span&gt;) &lt;span style="color:#66d9ef"&gt;as&lt;/span&gt; num_searches,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; json&lt;span style="color:#f92672"&gt;-&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;request&amp;#39;&lt;/span&gt;&lt;span style="color:#f92672"&gt;-&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;headers&amp;#39;&lt;/span&gt;&lt;span style="color:#f92672"&gt;-&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;MINDFULDATAAI_USER_ID&amp;#39;&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;as&lt;/span&gt; user_id,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;MIN&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;SUBSTRING&lt;/span&gt;(json&lt;span style="color:#f92672"&gt;-&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;asctime&amp;#39;&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;11&lt;/span&gt;)) &lt;span style="color:#66d9ef"&gt;as&lt;/span&gt; first_search,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;MAX&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;SUBSTRING&lt;/span&gt;(json&lt;span style="color:#f92672"&gt;-&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;asctime&amp;#39;&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;11&lt;/span&gt;)) &lt;span style="color:#66d9ef"&gt;as&lt;/span&gt; last_search&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;FROM&lt;/span&gt; logs&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;WHERE&lt;/span&gt; JSON_EXTRACT(json, &lt;span style="color:#e6db74"&gt;&amp;#39;$.code&amp;#39;&lt;/span&gt;)&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;&amp;#34;RESPONSE&amp;#34;&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;AND&lt;/span&gt; JSON_EXTRACT(json, &lt;span style="color:#e6db74"&gt;&amp;#39;$.request.path&amp;#39;&lt;/span&gt;)&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;&amp;#34;/search&amp;#34;&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;AND&lt;/span&gt; user_id &lt;span style="color:#66d9ef"&gt;IS&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;NOT&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;NULL&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;GROUP&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;BY&lt;/span&gt; user_id&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;) s &lt;span style="color:#66d9ef"&gt;JOIN&lt;/span&gt; users u &lt;span style="color:#66d9ef"&gt;ON&lt;/span&gt; u.id&lt;span style="color:#f92672"&gt;=&lt;/span&gt;s.user_id&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;ORDER&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;BY&lt;/span&gt; s.num_searches &lt;span style="color:#66d9ef"&gt;DESC&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;LIMIT&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;10&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And when run, Metabase will give you a table of data that can easily be exported to a CSV file:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/duckdb-metabase-postgresql-cloudwatch/top-users-table.png"&#10;&#9;&#9;&#9;alt="A Metabase table of the ten most active users with search counts, first and last search dates, and redacted names and emails"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;We just joined the JSON log data (searches) with the Postgres data (users) in a single query using DuckDB. Isn’t that cool?! 😎&lt;/p&gt;&#10;&lt;p&gt;Although this solution was somewhat more complex than necessary, it has provided us with a robust pipeline that enables further analysis through the crafting of new SQL queries.&lt;/p&gt;&#10;&lt;h2 id="alternative-solutions"&gt;Alternative Solutions&lt;/h2&gt;&#10;&lt;p&gt;While the combination of DuckDB and Metabase presents an efficient and effective approach for data analysis in our scenario, it’s important to consider alternative solutions that might better align with different requirements or preferences. For example, you may want to consider a hosted version of Metabase that can be shared with your coworkers. Alternatively, you might prefer not to have to re-execute the entire pipeline whenever there are changes. Here are some noteworthy alternatives:&lt;/p&gt;&#10;&lt;h3 id="1-using-presto-or-athena-for-querying"&gt;1. Using Presto or Athena for Querying:&lt;/h3&gt;&#10;&lt;p&gt;&lt;a href="https://prestodb.io/"&gt;Presto&lt;/a&gt;, an open-source distributed SQL query engine, and &lt;a href="https://aws.amazon.com/athena/"&gt;Athena&lt;/a&gt;, the AWS managed version of Presto, are powerful tools for querying data across various sources like S3 and PostgreSQL. By employing either of these, you can execute SQL queries across your data lake and database without the need to load data into an analytics tool. This approach can significantly reduce the memory footprint of your Metabase instance, as it doesn’t require storing large datasets for querying purposes.&lt;/p&gt;&#10;&lt;h3 id="2-analytics-postgres-database-for-reporting"&gt;2. Analytics Postgres Database for Reporting:&lt;/h3&gt;&#10;&lt;p&gt;Another approach is to set up a pipeline that selectively copies key log events and PostgreSQL data into a dedicated analytics database. This strategy can streamline the data analysis process, particularly for creating an analytics dashboard aimed at end-users. By isolating the analytics data, you can optimize the database schema for reporting and ensure that your production databases are not burdened by heavy analysis queries.&lt;/p&gt;&#10;&lt;h3 id="3-exploring-other-bi-tools"&gt;3. Exploring Other BI Tools:&lt;/h3&gt;&#10;&lt;p&gt;While &lt;a href="https://www.metabase.com/"&gt;Metabase&lt;/a&gt; is known for its user-friendliness and simplicity, other Business Intelligence tools like &lt;a href="https://superset.apache.org/"&gt;Superset&lt;/a&gt;, &lt;a href="https://cloud.google.com/looker"&gt;Looker&lt;/a&gt;, or &lt;a href="https://www.tableau.com/"&gt;Tableau&lt;/a&gt; might offer additional features or better integration with existing systems. Each of these tools has its unique strengths — for example, Superset is highly customizable, Looker excels in creating complex data models, and Tableau is renowned for its data visualization capabilities. Depending on the specific needs and existing tech stack of your organization, one of these alternatives might be a more suitable choice.&lt;/p&gt;&#10;&lt;h3 id="4-utilizing-snowflake-bigquery-or-redshift"&gt;4. Utilizing Snowflake, BigQuery, or Redshift:&lt;/h3&gt;&#10;&lt;p&gt;For more robust data warehousing solutions, &lt;a href="https://www.snowflake.com/en/"&gt;Snowflake&lt;/a&gt;, &lt;a href="https://aws.amazon.com/redshift/"&gt;Redshift&lt;/a&gt;, or &lt;a href="https://cloud.google.com/bigquery"&gt;BigQuery&lt;/a&gt; can be considered. These platforms are designed to handle large volumes of data and complex queries efficiently. They can directly query data stored in S3 and integrate with PostgreSQL, similar to DuckDB. However, they offer additional features like automatic scaling, extensive support for different data formats, and advanced security features. These platforms are particularly beneficial for organizations dealing with massive datasets and requiring a comprehensive data warehousing solution.&lt;/p&gt;&#10;&lt;p&gt;In conclusion, while our implementation with DuckDB and Metabase offers a streamlined and effective solution for our needs, these alternative solutions provide different benefits that could be more aligned with other specific requirements or preferences. The choice of tools and architecture depends greatly on the scale of data, specific use cases, and the existing technical infrastructure of the organization.&lt;/p&gt;&#10;&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;&#10;&lt;p&gt;In wrapping up our journey with DuckDB and Metabase, the key takeaway is the value of simplicity and efficiency in data analysis. Our experience with the MindfulDataAI Search Plugin highlighted how the right tools, like DuckDB and Metabase, can simplify the process of merging different data sources and extracting meaningful insights quickly.&lt;/p&gt;&#10;&lt;p&gt;These tools enabled us to analyze log data and PostgreSQL records effectively, leading to a better understanding of user preferences and improving our services. However, it’s essential to recognize that different scenarios may call for different tools. Alternatives like Presto, Athena, or other BI tools might be more suitable in varying contexts.&lt;/p&gt;&#10;&lt;p&gt;Ultimately, the goal is to choose a solution that is not only effective but also aligns with your project’s specific needs and technical environment. This approach ensures that data analysis remains a practical and valuable asset in decision-making processes.&lt;/p&gt;&#10;&lt;h2 id="want-to-use-chatgpt-in-slack"&gt;Want to use ChatGPT in Slack?&lt;/h2&gt;&#10;&lt;p&gt;At &lt;a href="https://mindfuldataai.com/?utm_source=redgeoff_com&amp;amp;utm_campaign=mpt-7b"&gt;MindfulDataAI.com&lt;/a&gt;, we offer &lt;em&gt;Arty&lt;/em&gt;, an app that integrates ChatGPT with Slack. Arty can be used in group settings or one-on-one, seamlessly incorporating ChatGPT and web search capabilities directly into Slack.&lt;/p&gt;&#10;</description></item><item><title>Running MosaicML's MPT-7B, a ChatGPT Competitor, on AWS SageMaker</title><link>https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/</link><pubDate>Tue, 18 Jul 2023 08:48:01 -0700</pubDate><guid>https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/</guid><description>&lt;p&gt;In this blog post, I’m going to take you step-by-step through the process of running MosaicML’s ChatGPT competitor, MPT-7B, on your own AWS SageMaker instance.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/mpt-7b-sagemaker.png"&#10;&#9;&#9;&#9;alt="MPT-7B and AWS SageMaker"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;MPT-7B and AWS SageMaker. Image credit: &lt;a href="https://www.mosaicml.com/blog/mpt-30b"&gt;MosaicML&lt;/a&gt; and Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Are you excited about the capabilities of ChatGPT, but have concerns about exposing your sensitive data to OpenAI? Luckily, there are alternatives that you can run on your own infrastructure. One such alternative is MosaicML&amp;rsquo;s MPT-7b, a competitor to ChatGPT, which we&amp;rsquo;ll explore in this blog post.&lt;/p&gt;&#10;&lt;h3 id="introduction-to-mosaicml-and-mpt-7b"&gt;Introduction to MosaicML and MPT-7B&lt;/h3&gt;&#10;&lt;p&gt;MosaicML, recently &lt;a href="https://www.reuters.com/markets/deals/databricks-strikes-13-bln-deal-generative-ai-startup-mosaicml-2023-06-26"&gt;acquired by Databricks for $1.3 billion&lt;/a&gt;, has been making waves in the ML community with their MPT-7B model, a supposed competitor to ChatGPT. Despite its promise, running this model can be daunting due to sparse documentation and its heavy resource requirements. However, one can run MPT-7B on AWS SageMaker in a Jupyter notebook, an environment that is beginner-friendly and highly flexible for rapid iteration. This setup allows you to test the model’s feasibility and hardware requirements before deciding to move into production.&lt;/p&gt;&#10;&lt;h3 id="running-mpt-7b-on-aws-sagemaker"&gt;Running MPT-7B on AWS SageMaker&lt;/h3&gt;&#10;&lt;p&gt;Running MPT-7B in a Jupyter notebook on AWS SageMaker provides several benefits. Not only can you pay just for what you use and turn it off when you&amp;rsquo;re done, but the ability to easily rerun portions of your code without having to reload the model saves time during iterative development. But do beware! If you forget to stop your notebook instances, the charges can quickly add up.&lt;/p&gt;&#10;&lt;p&gt;While this method is relatively convenient, there are some considerations you must take into account. Firstly, loading the model can take up to 20 minutes even on a high-performance GPU, making this process somewhat time-consuming. Also, the cost is a factor to consider, as the running cost is at least $4 per hour. You&amp;rsquo;ll need to run MPT-7B on at least a &lt;em&gt;p3.2xlarge&lt;/em&gt; instance; anything smaller does not seem feasible. If you opt for EC2 instead of SageMaker, you&amp;rsquo;ll have to ask AWS for permission to use a &lt;em&gt;p3.2xlarge&lt;/em&gt; instance.&lt;/p&gt;&#10;&lt;p&gt;In the next sections I’ll take you step-by-step through how to run the MPT-7B model in your very own SageMaker jupyter notebook:&lt;/p&gt;&#10;&lt;h4 id="step-1---open-the-sagemaker-console"&gt;Step 1 - Open the SageMaker Console&lt;/h4&gt;&#10;&lt;p&gt;Fire up the AWS Console and search for SageMaker:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/sage-maker.png"&#10;&#9;&#9;&#9;alt="Search for SageMaker"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Search for SageMaker. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h4 id="step-2---create-a-notebook-instance"&gt;Step 2 - Create a notebook instance&lt;/h4&gt;&#10;&lt;p&gt;From the left-side menu, select &lt;em&gt;Notebook-&amp;gt;Notebook instances&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/notebook-instances.png"&#10;&#9;&#9;&#9;alt="Notebook instances"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Notebook instances. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Click the &lt;em&gt;Create notebook instance&lt;/em&gt; button:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/create-notebook-instance.png"&#10;&#9;&#9;&#9;alt="Create notebook instance"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Create notebook instance. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Specify an instance name. Choose the instance type &lt;em&gt;m1.p3.2xlarge&lt;/em&gt;. Unfortunately, it seems that an instance as powerful as &lt;em&gt;m1.p3.2xlarge&lt;/em&gt; is required, or else your instance may run out of memory or take an excessive amount of time to respond to even the simplest questions. However, please note that this instance will cost approximately $4/hr, so it&amp;rsquo;s important to monitor your usage carefully.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/create-instance-2.png"&#10;&#9;&#9;&#9;alt="Specify notebook instance details"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Specify notebook instance details. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Create a new IAM role:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/create-role.png"&#10;&#9;&#9;&#9;alt="Create a new role"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Create a new role. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;If your test environment doesn’t have any particularly sensitive data in it then you can grant access to &lt;em&gt;Any S3 bucket&lt;/em&gt;. Otherwise, you’ll need to be more explicit.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/create-role-s3.png"&#10;&#9;&#9;&#9;alt="Specify S3 access"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Specify S3 access. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Click the &lt;em&gt;Create notebook instance&lt;/em&gt; button:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/create-instance-3.png"&#10;&#9;&#9;&#9;alt="Create notebook instance"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Create notebook instance. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;The notebook will then be in a &lt;em&gt;Pending&lt;/em&gt; status. This will likely last for about 10 minutes:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/notebook-pending.png"&#10;&#9;&#9;&#9;alt="Pending notebook instance"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Pending notebook instance. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;In the meantime, we’ll download a notebook so that we can upload it after the AWS SageMaker instance has finished provisioning.&lt;/p&gt;&#10;&lt;h4 id="step-3---download-the-notebook"&gt;Step 3 - Download the notebook&lt;/h4&gt;&#10;&lt;p&gt;Head on over to the notebook at &lt;a href="https://colab.research.google.com/drive/1kJr2LHHLKYkbnNutVYEkt2vrYsbO38aw"&gt;MPT-7B on AWS SageMaker.ipynb&lt;/a&gt; and download it:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/colab-notebook.png"&#10;&#9;&#9;&#9;alt="The notebook on Google Colab"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;The notebook on Google Colab. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/download-colab-notebook.png"&#10;&#9;&#9;&#9;alt="Download the notebook"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Download the notebook. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;In this notebook you’ll notice two main code blocks. The first block loads the MPT-7B tokenizer and model:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; torch &lt;span style="color:#f92672"&gt;import&lt;/span&gt; cuda, bfloat16&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; transformers &lt;span style="color:#f92672"&gt;import&lt;/span&gt; AutoTokenizer, AutoModelForCausalLM, AutoConfig&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;device &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;f&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;cuda:&lt;/span&gt;&lt;span style="color:#e6db74"&gt;{&lt;/span&gt;cuda&lt;span style="color:#f92672"&gt;.&lt;/span&gt;current_device()&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; cuda&lt;span style="color:#f92672"&gt;.&lt;/span&gt;is_available() &lt;span style="color:#66d9ef"&gt;else&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;cpu&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;tokenizer &lt;span style="color:#f92672"&gt;=&lt;/span&gt; AutoTokenizer&lt;span style="color:#f92672"&gt;.&lt;/span&gt;from_pretrained(&lt;span style="color:#e6db74"&gt;&amp;#34;mosaicml/mpt-7b-chat&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; trust_remote_code&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;True&lt;/span&gt;)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;config&lt;span style="color:#f92672"&gt;=&lt;/span&gt;{&lt;span style="color:#e6db74"&gt;&amp;#34;init_device&amp;#34;&lt;/span&gt;: &lt;span style="color:#e6db74"&gt;&amp;#34;meta&amp;#34;&lt;/span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;model &lt;span style="color:#f92672"&gt;=&lt;/span&gt; AutoModelForCausalLM&lt;span style="color:#f92672"&gt;.&lt;/span&gt;from_pretrained(&lt;span style="color:#e6db74"&gt;&amp;#34;mosaicml/mpt-7b-chat&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; trust_remote_code&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;True&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; config&lt;span style="color:#f92672"&gt;=&lt;/span&gt;config,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; torch_dtype&lt;span style="color:#f92672"&gt;=&lt;/span&gt;bfloat16)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;print(&lt;span style="color:#e6db74"&gt;f&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;device=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;{&lt;/span&gt;device&lt;span style="color:#e6db74"&gt;}&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;print(&lt;span style="color:#e6db74"&gt;&amp;#39;model loaded&amp;#39;&lt;/span&gt;)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The tokenizer is used to encode the question sent to the model and decode the response from the model. Additionally, we obtain the device specification for our GPU so that we can configure the model to utilize it later:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;import&lt;/span&gt; time&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; IPython.display &lt;span style="color:#f92672"&gt;import&lt;/span&gt; Markdown&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;import&lt;/span&gt; torch&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; transformers &lt;span style="color:#f92672"&gt;import&lt;/span&gt; StoppingCriteria, StoppingCriteriaList&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# mtp-7b is trained to add &amp;#34;&amp;lt;|endoftext|&amp;gt;&amp;#34; at the end of generations&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;stop_token_ids &lt;span style="color:#f92672"&gt;=&lt;/span&gt; [tokenizer&lt;span style="color:#f92672"&gt;.&lt;/span&gt;eos_token_id]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Define custom stopping criteria object.&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;# Source: https://github.com/pinecone-io/examples/blob/master/generation/llm-field-guide/mpt-7b/mpt-7b-huggingface-langchain.ipynb&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;class&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;StopOnTokens&lt;/span&gt;(StoppingCriteria):&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;__call__&lt;/span&gt;(self, input_ids: torch&lt;span style="color:#f92672"&gt;.&lt;/span&gt;LongTensor,scores: torch&lt;span style="color:#f92672"&gt;.&lt;/span&gt;FloatTensor,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;**&lt;/span&gt;kwargs) &lt;span style="color:#f92672"&gt;-&amp;gt;&lt;/span&gt; bool:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; stop_id &lt;span style="color:#f92672"&gt;in&lt;/span&gt; stop_token_ids:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; input_ids[&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;][&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;] &lt;span style="color:#f92672"&gt;==&lt;/span&gt; stop_id:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;True&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;False&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;stopping_criteria &lt;span style="color:#f92672"&gt;=&lt;/span&gt; StoppingCriteriaList([StopOnTokens()])&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;ask_question&lt;/span&gt;(question, max_length&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;100&lt;/span&gt;):&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; start_time &lt;span style="color:#f92672"&gt;=&lt;/span&gt; time&lt;span style="color:#f92672"&gt;.&lt;/span&gt;time()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# Encode the question&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; input_ids &lt;span style="color:#f92672"&gt;=&lt;/span&gt; tokenizer&lt;span style="color:#f92672"&gt;.&lt;/span&gt;encode(question, return_tensors&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;pt&amp;#39;&lt;/span&gt;)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# Use the GPU&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; input_ids &lt;span style="color:#f92672"&gt;=&lt;/span&gt; input_ids&lt;span style="color:#f92672"&gt;.&lt;/span&gt;to(device)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# Generate a response&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; output &lt;span style="color:#f92672"&gt;=&lt;/span&gt; model&lt;span style="color:#f92672"&gt;.&lt;/span&gt;generate(&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; input_ids,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; max_new_tokens&lt;span style="color:#f92672"&gt;=&lt;/span&gt;max_length,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; temperature&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;0.9&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; stopping_criteria&lt;span style="color:#f92672"&gt;=&lt;/span&gt;stopping_criteria&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; )&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;# Decode the response&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; response &lt;span style="color:#f92672"&gt;=&lt;/span&gt; tokenizer&lt;span style="color:#f92672"&gt;.&lt;/span&gt;decode(output[:, input_ids&lt;span style="color:#f92672"&gt;.&lt;/span&gt;shape[&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;]:][&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;],&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; skip_special_tokens&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;True&lt;/span&gt;)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; end_time &lt;span style="color:#f92672"&gt;=&lt;/span&gt; time&lt;span style="color:#f92672"&gt;.&lt;/span&gt;time()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; duration &lt;span style="color:#f92672"&gt;=&lt;/span&gt; end_time &lt;span style="color:#f92672"&gt;-&lt;/span&gt; start_time&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; display(Markdown(response))&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; print(&lt;span style="color:#e6db74"&gt;&amp;#34;Function duration:&amp;#34;&lt;/span&gt;, duration, &lt;span style="color:#e6db74"&gt;&amp;#34;seconds&amp;#34;&lt;/span&gt;)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note the use of &lt;code&gt;stopping_critera&lt;/code&gt;, which is needed or else the model will just start babbling on, even after it has answered our question.&lt;/p&gt;&#10;&lt;p&gt;See &lt;a href="https://huggingface.co/transformers/v4.12.5/main_classes/model.html#transformers.generation_utils.GenerationMixin.generate"&gt;model generate parameters&lt;/a&gt; if you want to explore the different options.&lt;/p&gt;&#10;&lt;p&gt;Now, let’s upload this notebook to SageMaker.&lt;/p&gt;&#10;&lt;h4 id="step-4---upload-the-notebook"&gt;Step 4 - Upload the notebook&lt;/h4&gt;&#10;&lt;p&gt;Hopefully by this time your SageMaker notebook instance has finished being provisioned. When it has, click the &lt;em&gt;Open Jupyter&lt;/em&gt; link:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/created-notebook-instances.png"&#10;&#9;&#9;&#9;alt="Open Jupyter"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Open Jupyter. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Then, click the &lt;em&gt;Upload&lt;/em&gt; button in the top-right corner of your screen and select the notebook that you just downloaded:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/upload-notebook.png"&#10;&#9;&#9;&#9;alt="Upload the notebook"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Upload the notebook. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Set the kernel to &lt;em&gt;conda_python3&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/set-kernel.png"&#10;&#9;&#9;&#9;alt="Set the kernel"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Set the kernel. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h4 id="step-5---run-the-notebook"&gt;Step 5 - Run the notebook&lt;/h4&gt;&#10;&lt;p&gt;Select &lt;em&gt;Cell -&amp;gt; Run All&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/run-all.png"&#10;&#9;&#9;&#9;alt="Run all cells"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Run all cells. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;An hourglass logo will then appear in the browser tab:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/wait-for-notebook.png"&#10;&#9;&#9;&#9;alt="Wait for notebook"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Wait for notebook. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;You’ll then need to wait about 10 minutes for the model to be downloaded:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/notebook-downloads-model.png"&#10;&#9;&#9;&#9;alt="Download model"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Download model. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;After it runs, you’ll see the answer to the question &lt;em&gt;Explain to me the difference between nuclear fission and fusion&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/fission-vs-fusion.png"&#10;&#9;&#9;&#9;alt="Explain to me the difference between nuclear fission and fusion"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Explain to me the difference between nuclear fission and fusion. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Since the model and tokenizer have already been loaded above, you can simply modify the &lt;em&gt;ask_question&lt;/em&gt; code block and click the &lt;em&gt;Run&lt;/em&gt; button to ask any other questions. This will save you from spending 10 minutes each time you want to test a new question.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/capital-of-france.png"&#10;&#9;&#9;&#9;alt="What is the capital of France?"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;What is the capital of France?. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h4 id="step-6---stop-the-notebook"&gt;Step 6 - Stop the notebook&lt;/h4&gt;&#10;&lt;p&gt;As soon as you have finished testing the model, you’ll want to head back to your list of notebook instances and stop it. If you don’t, $4/hr will add up very quickly 💸&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/stop-notebook.png"&#10;&#9;&#9;&#9;alt="Stop the notebook"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Stop the notebook. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h3 id="performance-comparison"&gt;Performance Comparison&lt;/h3&gt;&#10;&lt;p&gt;In terms of performance, my preliminary tests suggest that MPT-7B&amp;rsquo;s results might not be as good as ChatGPT’s. It does a decent job answering questions like &lt;em&gt;What is the capital of France?&lt;/em&gt;, &lt;em&gt;Explain to me the difference between nuclear fission and fusion&lt;/em&gt;, and &lt;em&gt;Write python code that converts a csv into pdf&lt;/em&gt;. But for questions like_What is the capital of Belize?_ it fails pretty horribly:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/mpt-7b-on-aws-sagemaker/capital-of-belize.png"&#10;&#9;&#9;&#9;alt="What is the capital of Belize?"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;What is the capital of Belize? Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;I am currently collecting more data and will conduct a comprehensive comparative analysis in a follow-up blog post. In that post, I will compare the question and answer performance of MPT-7B, MPT-30B, Falcon-40b, and ChatGPT using actual conversation history.&lt;/p&gt;&#10;&lt;h3 id="from-testing-to-production"&gt;From Testing to Production&lt;/h3&gt;&#10;&lt;p&gt;Once you&amp;rsquo;re ready to transition from testing to production, SageMaker offers an additional benefit - the ability to create an endpoint for your model. With SageMaker, you can auto-scale based on demand to the endpoint, optimizing your resources.&lt;/p&gt;&#10;&lt;h3 id="additional-tips"&gt;Additional Tips&lt;/h3&gt;&#10;&lt;p&gt;Be mindful that it&amp;rsquo;s easy for your process to get forked while running in a Jupyter notebook and run out of memory. If this happens, simply shut down the kernel and run all the commands again.&lt;/p&gt;&#10;&lt;p&gt;If you&amp;rsquo;re curious about running this model on a platform other than AWS, Google Colab Pro is another viable option at $9/month. However, based on our testing, we found that we exhausted the provided credits within just a few hours. 😳&lt;/p&gt;&#10;&lt;p&gt;Another challenge you may face is the inability to utilize the &lt;a href="https://huggingface.co/mosaicml/mpt-7b#how-to-use"&gt;Triton optimization&lt;/a&gt; on SageMaker due to a CUDA version incompatibility. Unfortunately, AWS&amp;rsquo;s current P3 instances do not include a recent CUDA version. Therefore, if you wish to utilize the Triton optimization, you will need to create an EC2 container with command line access. However, it&amp;rsquo;s important to note that you will also require special permission from AWS Support to run an instance with 8 VCPUs. In a future post, I will provide a detailed guide on how to integrate Triton and utilize a more cost-effective GPU cloud provider, such as &lt;a href="lambdalabs.com"&gt;Lambda Labs&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h3 id="final-thoughts"&gt;Final Thoughts&lt;/h3&gt;&#10;&lt;p&gt;While MosaicML&amp;rsquo;s MPT-7B offers a viable alternative to OpenAI&amp;rsquo;s ChatGPT, it presents its own set of challenges. Running the model can be time-consuming, expensive, and the available documentation is lacking. However, the ability to keep the model in-house and protect your data from being exposed to OpenAI can be compelling for certain use cases.&lt;/p&gt;&#10;&lt;p&gt;SageMaker offers great convenience for quickly testing the model and provides the flexibility to transition to production when you&amp;rsquo;re ready. Whether you&amp;rsquo;re just starting with MPT-7B or have been using it for a while, we hope this guide has provided valuable insights.&lt;/p&gt;&#10;&lt;p&gt;Stay tuned for our next blog post, where we&amp;rsquo;ll delve deeper into the performance comparisons between MPT-7B, MPT-30B, Falcon-40b, and ChatGPT.&lt;/p&gt;&#10;&lt;p&gt;See the following links if you are interested in learning more about &lt;a href="https://www.mosaicml.com/blog/mpt-7b"&gt;MPT-7B&lt;/a&gt; or its larger variant, &lt;a href="https://www.mosaicml.com/blog/mpt-30b"&gt;MPT-30B&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;And remember, whether you&amp;rsquo;re working with ChatGPT or MPT-7B, the key is to ensure your use case is served without compromising data privacy and cost-effectiveness. Happy tinkering!&lt;/p&gt;&#10;&lt;h3 id="want-a-turn-key-solution-for-using-mpt-or-chatgpt-with-your-data"&gt;Want a Turn-Key Solution for Using MPT or ChatGPT with Your Data?&lt;/h3&gt;&#10;&lt;p&gt;At &lt;a href="https://mindfuldataai.com?utm_source=redgeoff_com&amp;amp;utm_campaign=mpt-7b"&gt;MindfulDataAI.com&lt;/a&gt;, we offer ChatGPT for businesses. If you&amp;rsquo;re interested in leveraging ChatGPT, MPT, or other models with your company&amp;rsquo;s data, please get in touch with us.&lt;/p&gt;&#10;</description></item><item><title>My Journey with ChatGPT for Business: Unleashing the Power of AI for Software Engineers</title><link>https://redgeoff.com/posts/chatgpt-for-business/</link><pubDate>Sat, 08 Jul 2023 07:16:09 -0700</pubDate><guid>https://redgeoff.com/posts/chatgpt-for-business/</guid><description>&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/chatgpt-for-business/gpt4.jpg"&#10;&#9;&#9;&#9;alt="ChatGPT for business"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: &lt;a href="https://unsplash.com/@dkoi"&gt;D koi&lt;/a&gt; on &lt;a href="https://unsplash.com"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h3 id="introduction"&gt;Introduction&lt;/h3&gt;&#10;&lt;p&gt;As a software engineer, I&amp;rsquo;m constantly searching for tools and technologies that can revolutionize the way I work. Today, I want to share a prototype app: ChatGPT for Business. I believe if we follow through on building this, it has the potential to transform our industry. Join me as I take you through my experiences, challenges, and the immense possibilities that lie ahead.&lt;/p&gt;&#10;&lt;h3 id="from-crypto-trading-to-ai-assisted-productivity"&gt;From Crypto Trading to AI-Assisted Productivity&lt;/h3&gt;&#10;&lt;p&gt;The story begins with me leaving my Director of Engineering job at a fast-growing startup that was about to be acquired. I stumbled into the world of crypto trading after attending a web3 event in early 2022, where I heard about arbitrage opportunities. Eager to explore further, I began experimenting with free bots at Pionex and discovered the passive income potential of trading bots. That&amp;rsquo;s when everything changed.&lt;/p&gt;&#10;&lt;p&gt;Driven by curiosity, I reverse-engineered a promising bot, conducted extensive backtesting using historical price data from Binance, and optimized its parameters using genetic mutation techniques. Deploying it on AWS, I witnessed consistent daily returns, achieving a remarkable 18% annual return on a sizable investment. But as the market landscape shifted, I realized that luck played a significant role in my success, and a long-term buy-and-hold strategy would yield better results. When the SEC&amp;rsquo;s lawsuit against Binance and Coinbase loomed, I secured my crypto in wallets and closed my trading operations.&lt;/p&gt;&#10;&lt;h3 id="discovering-the-power-of-chatgpt"&gt;Discovering the Power of ChatGPT&lt;/h3&gt;&#10;&lt;p&gt;During this transformative period, I found myself increasingly relying on ChatGPT, a powerful generative AI model. What began as a tool for trivial questions quickly evolved into something much more. ChatGPT gradually replaced around 80% of my Google searches, becoming indispensable for coding technical analysis algorithms, troubleshooting Docker configurations, proofreading emails, and even generating pitch deck outlines. I realized that ChatGPT had become my go-to resource, surpassing Google searches in many aspects. However, it was missing the capability to search my private data and access any information released after 2021.&lt;/p&gt;&#10;&lt;h3 id="the-spark-of-innovation"&gt;The Spark of Innovation&lt;/h3&gt;&#10;&lt;p&gt;One day, over coffee, a friend shared an interesting challenge: scraping information from multiple mom-and-pop websites to create a comprehensive activities-for-kids marketplace. It was at that moment that I had a revelation—what if ChatGPT could help? Instead of writing custom web scrapers for each site, could ChatGPT extract the desired information? Intrigued, I stumbled upon an informative post titled &lt;a href="https://beebom.com/how-train-ai-chatbot-custom-knowledge-base-chatgpt-api"&gt;&amp;ldquo;How to Train an AI Chatbot With Custom Knowledge Base Using ChatGPT API&amp;rdquo;&lt;/a&gt;. Inspired by its possibilities, I pointed ChatGPT to a Boys and Girls Club website, asking questions about activities in Seattle, their schedules, and costs. To my delight, the results were remarkably accurate, and the cost of using OpenAI’s API was surprisingly affordable.&lt;/p&gt;&#10;&lt;h3 id="unlocking-the-power-of-chatgpt-for-business"&gt;Unlocking the Power of ChatGPT for Business&lt;/h3&gt;&#10;&lt;p&gt;Fueled by this success, I couldn&amp;rsquo;t help but wonder how I could further enhance my productivity using ChatGPT. To my surprise, I discovered that very few business products allowed users to leverage ChatGPT with their own data. Existing solutions fell short in terms of integrations, customization, and scalability. &lt;a href="https://myaskai.com"&gt;MyAskAI&lt;/a&gt; catered to individuals with one-off files and only allowed two questions per message thread. &lt;a href="https://aisera.com"&gt;AiSera&lt;/a&gt; seemed more focused on building chatbots with custom data. &lt;a href="https://customgpt.ai"&gt;CustomGPT&lt;/a&gt; lacked the necessary integrations with data sources like Google Drive, Salesforce, and Confluence. &lt;a href="https://gerev.ai"&gt;Gerev.ai&lt;/a&gt;, although open-source and appealing, failed to index a 5-page PDF on a brand-new MacBook Pro. &lt;a href="https://klu.so"&gt;Klu&lt;/a&gt; had an impressive turnkey integration with sources like Google Drive, but its &amp;ldquo;Beta AI&amp;rdquo; spun endlessly when asked a basic question about a single 5-page PDF file.&lt;/p&gt;&#10;&lt;p&gt;This realization sparked a vision in me—I wanted to build a product specifically designed to empower all businesses, not just the ones with the funds to train new AI models, with ChatGPT&amp;rsquo;s capabilities. Excited by the possibilities, I reached out to my friend Arty, a talented engineer, and we began brainstorming ideas together.&lt;/p&gt;&#10;&lt;h3 id="seeking-validation-and-refining-the-vision"&gt;Seeking Validation and Refining the Vision&lt;/h3&gt;&#10;&lt;p&gt;To refine our concept and gain support, we sought guidance from a local venture capitalist (VC). We were told that our idea might be too broad and that we should instead focus on a specific industry. However, we remained unconvinced that there isn&amp;rsquo;t a place for a general-purpose ChatGPT tool for businesses. Therefore, we decided to rely on data to guide us in the right direction. Undeterred, we developed a playbook, crafted surveys, optimized keywords for SEO and advertising, and created compelling website mock-ups. But we knew that true validation would come from our fellow software engineers—the very people who could benefit from ChatGPT for Business—&lt;strong&gt;YOU!&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;Imagine, if you will…&lt;/p&gt;&#10;&lt;p&gt;A ChatGPT-powered teammate, seamlessly integrated with your data sources and the web, providing real-time insights, securely linked to source documents, and offering turnkey integrations with your company&amp;rsquo;s tools and systems. We envision automated document synchronization, real-time web searches, single sign-on (SSO) integration, access controls, conversation sharing, onboarding and training, and robust security and compliance measures.&lt;/p&gt;&#10;&lt;p&gt;Imagine being able get the status of a Jira ticket in real-time:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/chatgpt-for-business/mindful-screen-jira.png"&#10;&#9;&#9;&#9;alt="Jira status"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Or, what about keeping tabs on the competition?&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/chatgpt-for-business/mindful-screen-competitor.png"&#10;&#9;&#9;&#9;alt="Competitor research"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Or, even synthesizing reports from raw data. Sharing and explaining insights (like a goalie, answering questions before you need to!). The more documents you share with the agent, the more time it will save for you as you could even let this AI agent handle basic questions on your behalf!&lt;/p&gt;&#10;&lt;div style="width:100%;height:0;padding-bottom:54%;position:relative;"&gt;&#10; &lt;iframe src="https://giphy.com/embed/KXJeRE9I6MvZ2ZPwY8" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen&gt;&#10; &lt;/iframe&gt;&#10;&lt;/div&gt;&#10;&lt;figcaption&gt;&lt;small&gt;&lt;i&gt;Image credit: &lt;a href="https://giphy.com/gifs/nhl-hockey-ice-17-18-season-KXJeRE9I6MvZ2ZPwY8"&gt;Ice Hockey Dancing&lt;/a&gt; by &lt;a href="https://giphy.com/channel/nhl"&gt;NHL&lt;/a&gt;&lt;/i&gt;&lt;/small&gt;&lt;/figcaption&gt;&#10;&#10;&lt;p&gt;The possibilities are endless.&lt;/p&gt;&#10;&lt;h3 id="join-the-journey"&gt;Join the Journey&lt;/h3&gt;&#10;&lt;p&gt;And now, dear reader, we invite you to embark on this incredible journey with us. We think we’re on to something, but we want your thoughts! Your insights and feedback are essential as we shape the future of ChatGPT for Business. Together, we can unleash the potential of ChatGPT, transforming the way we work, and empowering ourselves and our teams with a tool that will revolutionize communication. Please take a moment to share your thoughts by participating in our quick &lt;a href="https://mindfuldataai.com/?utm_campaign=2023_blog_origin_story&amp;amp;utm_source=redgeoff_com"&gt;survey&lt;/a&gt;. Additionally, you can also &lt;a href="https://mindfuldataai.com/survey.html?utm_campaign=2023_blog_origin_story&amp;amp;utm_source=redgeoff_com"&gt;register for the beta release&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h3 id="conclusion"&gt;Conclusion&lt;/h3&gt;&#10;&lt;p&gt;Our journey with ChatGPT for Business has been one of passion, innovation, and relentless pursuit. We have witnessed firsthand the immense potential of AI in our field, and we’re determined to harness that power to create a tool that will revolutionize software engineering. Thank you for reading, and we eagerly await your input as we shape the future of ChatGPT for Business together.&lt;/p&gt;&#10;</description></item><item><title>Run a Free VPN Server on AWS (2022 Edition)</title><link>https://redgeoff.com/posts/free-vpn-aws-2022/</link><pubDate>Tue, 15 Nov 2022 10:48:50 -0800</pubDate><guid>https://redgeoff.com/posts/free-vpn-aws-2022/</guid><description>&lt;p&gt;This tutorial will take you through the steps of setting up an EC2 instance running OpenVPN server. It will then cover how to grant and revoke access to your VPN server.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/vpn-splash.jpg"&#10;&#9;&#9;&#9;alt="Free VPN on AWS"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: &lt;a href="https://unsplash.com/@privecstasy"&gt;Privecstasy&lt;/a&gt; on &lt;a href="https://unsplash.com"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;AWS has an awesome firewall built into its core services, which can easily be used to make sure that only certain ports are open to the outside world. One extra step that we can take is to run a VPN server that serves as the gateway to our protected AWS resource, e.g. EC2, RDS, etc&amp;hellip; We can then shutdown direct access to our AWS resources just by revoking access via our VPN server. This is very useful if you need to revoke access for a former employee.&lt;/p&gt;&#10;&lt;p&gt;AWS now offers a &lt;a href="https://aws.amazon.com/vpn/"&gt;managed VPN Service&lt;/a&gt;, but this service costs at least $72 a month and is even more expensive when your VPN serves a lot of traffic. A lot of smaller organizations don&amp;rsquo;t need all the features of the managed service and instead can run their own VPN server for just the cost of an EC2 instance. You can even use the free tier so that you don&amp;rsquo;t have to pay anything for the EC2 instance for the 1st year.&lt;/p&gt;&#10;&lt;h3 id="step-1--create-the-vpn-security-group"&gt;Step 1 — Create the VPN Security Group&lt;/h3&gt;&#10;&lt;p&gt;Overview: security groups allow your servers to communicate with each other in a private cloud while exposing specific ports to the world. We are going to create a security group to allow VPN access to our VPN server. We will assume that all your other AWS resources are members of the default security group and that the default security group does not allow access from the outside world.&lt;/p&gt;&#10;&lt;p&gt;Log in at &lt;a href="https://aws.amazon.com"&gt;https://aws.amazon.com&lt;/a&gt;, type &lt;em&gt;EC2&lt;/em&gt; in the search box, and click on the target to go to the EC2 Dashboard.&lt;/p&gt;&#10;&lt;p&gt;From the EC2 dashboard, click &lt;em&gt;Security Groups&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/security-groups.png"&#10;&#9;&#9;&#9;alt="Security Groups"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Click &lt;em&gt;Create security group&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/create-security-group.png"&#10;&#9;&#9;&#9;alt="Create Security Group"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Enter a name and description of &lt;em&gt;vpn&lt;/em&gt; and specify the following inbound rules on ports &lt;em&gt;22&lt;/em&gt;, &lt;em&gt;443&lt;/em&gt;, &lt;em&gt;943&lt;/em&gt;, and &lt;em&gt;1194&lt;/em&gt;. Note: the protocol for port &lt;em&gt;1194&lt;/em&gt; is &lt;strong&gt;UDP&lt;/strong&gt;.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/inbound-rules.png"&#10;&#9;&#9;&#9;alt="Ports"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Note: if the IP addresses that your team uses are static then you can add yet another layer of security by specifying an IP address range in the &lt;em&gt;Source&lt;/em&gt; of your rules. However, you’ll want to leave the &lt;em&gt;Source&lt;/em&gt; open to anywhere if you want your team to be able to connect from any IP as they may be working from a hotel, home, cafe, etc…&lt;/p&gt;&#10;&lt;h3 id="step-2--create-the-ec2-instance"&gt;Step 2 — Create the EC2 Instance&lt;/h3&gt;&#10;&lt;p&gt;Return to the EC2 Dashboard and then click &lt;em&gt;Launch instances&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/launch-instances.png"&#10;&#9;&#9;&#9;alt="Launch instances"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Select Ubuntu (you can of course select almost any other OS that runs OpenVPN, but this tutorial is tailored to Ubuntu)&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/ubuntu-22.png"&#10;&#9;&#9;&#9;alt="Ubuntu 22"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Select &lt;em&gt;t2.micro&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/t2-nano.png"&#10;&#9;&#9;&#9;alt="t2 nano"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Note: you can use a nano instance instead of a micro instance, but nano instances are not eligible for the free tier.&lt;/p&gt;&#10;&lt;p&gt;In the &lt;em&gt;Network settings&lt;/em&gt; section, select your default VPC and disable the auto-assign public IP option. Then, select both your default security group and the security group that you created above for the VPN.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/network-settings.png"&#10;&#9;&#9;&#9;alt="Edit security groups"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Click &lt;em&gt;Launch instance&lt;/em&gt;&lt;/p&gt;&#10;&lt;h3 id="step-3--disable-sourcedestination-check"&gt;Step 3 — Disable Source/Destination Check&lt;/h3&gt;&#10;&lt;p&gt;From the list of instances, select the VPN instance and then &lt;em&gt;Actions -&amp;gt; Networking -&amp;gt; Change source/destination check&lt;/em&gt; from the drop down menu.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/instance-source-destination.png"&#10;&#9;&#9;&#9;alt="Instance source destination check"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Select &lt;em&gt;Stop&lt;/em&gt; and click &lt;em&gt;Save&lt;/em&gt;. This is needed as otherwise, your VPN server will not be able to connect to your other AWS resources.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/edit-source-destination.png"&#10;&#9;&#9;&#9;alt="Edit source destination check"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h3 id="step-4--create-an-elastic-ip-address"&gt;Step 4 — Create an Elastic IP Address&lt;/h3&gt;&#10;&lt;p&gt;Overview: when an EC2 instance is stopped and restarted, the Public IP address changes. We want the IP address of our VPN server to remain static so we’ll use an Elastic IP Address.&lt;/p&gt;&#10;&lt;p&gt;From the E2C Dashboard, select &lt;em&gt;Elastic IPs&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/elastic-ips-menu.png"&#10;&#9;&#9;&#9;alt="Elastic IPs"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Click &lt;em&gt;Allocate Elastic IP address&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/allocate-elastic-ip.png"&#10;&#9;&#9;&#9;alt="Allocate Elastic IP address"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Make a note of your new Elastic IP address as this will be the Public IP Address of your VPN server. We will refer to this address later as &lt;em&gt;PUBLIC-IP-OF-VPN-SERVER&lt;/em&gt;.&lt;/p&gt;&#10;&lt;p&gt;Select the IP address you just created and click &lt;em&gt;Associate Elastic IP address&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/associate-elastic-ip.png"&#10;&#9;&#9;&#9;alt="Associate Elastic IP address"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Then, select the Elastic IP and click &lt;em&gt;Associate address&lt;/em&gt; from the drop down menu.&lt;/p&gt;&#10;&lt;p&gt;Select the EC2 instance you created above and click &lt;em&gt;Associate&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/ec2-elastic-ip.png"&#10;&#9;&#9;&#9;alt="EC2 Elastic IP"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h3 id="step-5-install-and-configure-the-openvpn-server"&gt;Step 5— Install and Configure the OpenVPN Server&lt;/h3&gt;&#10;&lt;p&gt;SSH into your VPN server:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ ssh ubuntu@PUBLIC-IP-OF-VPN-SERVER&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Download our helper scripts and set up a default config:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ git clone https://github.com/redgeoff/openvpn-server-vagrant&#10;$ cd openvpn-server-vagrant&#10;$ cp config-default.sh config.sh&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Edit config.sh and enter in your configuration. Note: &lt;em&gt;PUBLIC_IP&lt;/em&gt; should be equal to the Elastic IP Address that you created above.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ nano config.sh&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Switch to root:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ sudo su -&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You&amp;rsquo;ll now update Ubuntu. Note: you will be prompted several times and when you do, just press the &lt;em&gt;Enter&lt;/em&gt; key.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ /home/ubuntu/openvpn-server-vagrant/ubuntu.sh&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now, install OpenVPN. Note: you will be prompted several times and when you do, just press the &lt;em&gt;Enter&lt;/em&gt; key.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ /home/ubuntu/openvpn-server-vagrant/openvpn.sh&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;At this point, the OpenVPN server is running.&lt;/p&gt;&#10;&lt;h3 id="step-6--add-the-route"&gt;Step 6 — Add the Route&lt;/h3&gt;&#10;&lt;p&gt;Routes must be added to the server so that your team’s clients know which traffic to route to the VPN server.&lt;/p&gt;&#10;&lt;p&gt;You can determine the proper subnet by returning to your list of EC2 instances, clicking on a target instance and identifying the Private IP.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/vpn-2022/identify-private-ip.png"&#10;&#9;&#9;&#9;alt="Identify Private IP"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Your network will be the first 2 parts of the Private IP appended with zeros, e.g. &lt;em&gt;172.31.0.0&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;On the VPN server edit &lt;em&gt;/etc/openvpn/server/server.conf&lt;/em&gt; and add something like the following:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;push &amp;#34;route 172.31.0.0 255.255.0.0&amp;#34;&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then, restart the VPN server with:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ systemctl restart openvpn-server@server.service&#10;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="step-7--grant-access-to-your-vpn"&gt;Step 7 — Grant Access to Your VPN&lt;/h3&gt;&#10;&lt;p&gt;Note: We assume that you are still SSH’d into the VPN and logged in as root.&lt;/p&gt;&#10;&lt;p&gt;Run the following command and be sure to replace &lt;em&gt;client&lt;/em&gt; below with a unique name for your user/client.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ /home/ubuntu/openvpn-server-vagrant/add-client.sh client&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You’ll then find a configuration file at&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;~/client-configs/files/client-name.ovpn&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You will want to provide this file to the individual on your team who will be connecting to your VPN. SCP is handy for downloading this .ovpn file from your VPN server.&lt;/p&gt;&#10;&lt;p&gt;Your team can use one of various VPN clients such as &lt;a href="https://tunnelblick.net/"&gt;Tunnelblick&lt;/a&gt; (OS X) and &lt;a href="https://openvpn.net/community-downloads/"&gt;OpenVPN&lt;/a&gt; (Linux, iOS, Android and Windows). After installing one of these clients, they should be able to set up the VPN config just by double clicking on the .ovpn file.&lt;/p&gt;&#10;&lt;p&gt;Note: once connected to the VPN, your users will want to use the Private IPs of your AWS resources. You may want to use &lt;em&gt;nslookup&lt;/em&gt; to lookup the IP address from the AWS custom domain names provided by AWS. You’ll probably want to use Route 53 to create subdomain records that route to the Private IPs.&lt;/p&gt;&#10;&lt;h3 id="step-8--revoke-access-to-your-vpn"&gt;Step 8 — Revoke Access to Your VPN&lt;/h3&gt;&#10;&lt;p&gt;Note: We assume that you are still SSH’d into the VPN and logged in as root.&lt;/p&gt;&#10;&lt;p&gt;Run the following command and be sure to replace &lt;em&gt;client&lt;/em&gt; below with a unique name for your user/client.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ /home/ubuntu/openvpn-server-vagrant/revoke-full.sh client&#10;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="troubleshooting"&gt;Troubleshooting&lt;/h3&gt;&#10;&lt;p&gt;If your VPN client reports a &lt;em&gt;TLS handshake failed&lt;/em&gt; error then this is most likely because your VPN security group (Step 1) is incorrect. Make sure that you have the correct ports and protocols specified — a common problem is not specifying UDP for port 1194.&lt;/p&gt;&#10;</description></item><item><title>Grocery Hacking: Automate Your Grocery Shopping Using the Robot Framework</title><link>https://redgeoff.com/posts/grocery-hacking-robot-framework/</link><pubDate>Wed, 09 Mar 2022 07:32:52 -0800</pubDate><guid>https://redgeoff.com/posts/grocery-hacking-robot-framework/</guid><description>&lt;p&gt;I used Robotic Process Automation (RPA) to automate my grocery shopping so that I can spend less time shopping.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/grocery-hacking-robot-framework/hero.png"&#10;&#9;&#9;&#9;alt="A smiling dad holding a toddler beside a child in a shopping cart in a supermarket produce aisle, with the Robot Framework logo overlaid"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;My family and I are lucky enough to live in a Seattle neighborhood with 7 supermarkets that are each within walking distance. So, it wasn’t until the pandemic hit, and we were wishing to minimize our contact with others, that we started experimenting with grocery delivery. Like many others, we started working from home and we could now be available to receive deliveries at any time, it just seemed like a no-brainer to give grocery delivery a try.&lt;/p&gt;&#10;&lt;p&gt;At the time, the first grocery store in our area to offer delivery, which wasn’t the megacorp that is Amazon, was Fred Meyer, through a partnership with Instacart. Our initial experience was pretty good and we figured that the delivery cost of an extra 15% of the total bill was worth the extra safety of a contact-free delivery. We used Fred Meyer’s delivery service weekly for about 9 months and during this time we learned that it had its shortcomings. We found that Fred Meyer’s online inventory was often out of sync with its actual inventory, which meant that we could easily receive substitutions, or items would just be missing from the delivery as they were out of stock. It became pretty frustrating to have to physically go to the store after a delivery just to pick up the 20% of the items that were not delivered.&lt;/p&gt;&#10;&lt;p&gt;Fortunately, in mid 2021, our regular pre-COVID supermarket, Safeway, started offering a delivery service. With a delivery fee of just $3.95, the products we were used to, and substantial savings, this service seemed promising. And, indeed it was — so much so that we’ve been using it for the last 9 months and plan to continue to use the service.&lt;/p&gt;&#10;&lt;p&gt;COVID is still very much a thing, but most of my family is vaccinated (we’re still waiting for a vaccine for my 2-year old) and with masks we find it an acceptable risk to shop indoors in well-ventilated spaces. So what keeps us coming back to use a grocery delivery service? Convenience and cost. As much as going to the supermarket with my kids can be fun, as we get to buy a whole bunch of yummy food and we’re all a bunch of goofballs, it’s also tough for my kids to stay focused for 45 minutes during a big shop. This means they’ll be running all over the place and are going to slow down the shopping to a crawling speed (see what I did there?). Instead, my wife and I can do some meal planning on Sunday night, order the groceries, and then have them delivered for just $3.95!&lt;/p&gt;&#10;&lt;p&gt;What&amp;rsquo;s more? Safeway delivery drivers are employees of the store so Safeway doesn’t ask for a tip. With competing services, like those from Instacart, you can easily fork over an additional 10–20% of your bill for a tip and delivery fee. That adds up when you talk about a large shop. Moreover, our Safeway shopping actually leads to significant savings at the gas pump, which is a nice added bonus!&lt;/p&gt;&#10;&lt;p&gt;OK, enough background already… let’s talk about how I used to do my shopping before I automated it.&lt;/p&gt;&#10;&lt;h2 id="how-i-used-to-do-my-shopping"&gt;How I used to do my shopping&lt;/h2&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/grocery-hacking-robot-framework/old-workflow.png"&#10;&#9;&#9;&#9;alt="Diagram of the old weekly workflow: add items to the Alexa list, sync to Todoist, then add each item to the Safeway cart by hand"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;How I used to do my grocery shopping. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Here’s how it would go:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;During the week, my wife and I (and sometimes our kids) would ask Alexa to add an item to our shopping list. This is easy to do if you just add the item to the list as soon as you know you’re going to need more of it.&lt;/li&gt;&#10;&lt;li&gt;There is then an integration, provided by Todoist, that syncs the items from Alexa to Todoist. We use Todoist instead of the Alexa app to manage our list, as during the early days of Alexa, we found that it was much easier for my wife and I to share lists via Todoist. (This may no longer be the case, but Todoist works well for us and is free, so we keep using it).&lt;/li&gt;&#10;&lt;li&gt;On Sunday night, my wife and I sit down and do some meal planning, survey the refrigerator and cupboard, and then add items to the list via Alexa.&lt;/li&gt;&#10;&lt;li&gt;I fire up my computer and load Todoist and Safeway.com in different browser tabs.&lt;/li&gt;&#10;&lt;li&gt;I then go one by one through the items in my Todoist list. First, I look at the previously purchased items on Safeway, AKA &lt;em&gt;Buy It Again&lt;/em&gt; items and if there is a match, I add the item to my cart. Safeway does a pretty good job at recalling the last 300 or so items that I’ve purchased and there is a darn good chance that my item will be in this list.&lt;/li&gt;&#10;&lt;li&gt;If I’ve never purchased the item before or my previous purchase is out of stock, I use the Safeway search feature to search for the item and then add it to the cart.&lt;/li&gt;&#10;&lt;li&gt;I mark the item as completed on my Todolist&lt;/li&gt;&#10;&lt;li&gt;I check out my Safeway cart and schedule a delivery&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;What’s important to note is that the repetitive tasks of steps 4–7 can take up to &lt;strong&gt;30 minutes&lt;/strong&gt;. You may ask &lt;em&gt;how this is possible&lt;/em&gt;? Well, each item takes about 1 minute to process manually and I can easily have about 30–40 items on my list. This is partly because browsing the Safeway website is a bit slow. On many occasions, I’ve thought that it is a bummer that I have to spend 30 minutes each week ordering my groceries. After all, I’m usually ordering the same stuff and it doesn’t require much intelligence to do this shopping. There must be a better way, right?&lt;/p&gt;&#10;&lt;p&gt;You may be thinking:&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;OK, but doesn’t Alexa already provide a way to shop for products? Surely, this is why Amazon created Alexa?&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;Actually, it does! Fairly recently, the Alexa app started allowing you to &lt;a href="https://www.amazon.com/fmc/m/20190164?almBrandId=QW1hem9uIEZyZXNo"&gt;Shop your list&lt;/a&gt;. You simply add items to your Alexa list and then use the Alexa app to select from a list of Fresh or Whole Foods products:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/grocery-hacking-robot-framework/alexa-shop-your-list.png"&#10;&#9;&#9;&#9;alt="The Alexa app&amp;#39;s Shop Your List screen showing Amazon Fresh and Whole Foods products for each list item"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Alexa’s Shop Your List feature. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;This interface is pretty slick, but there’s one problem. My wife says:&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;&lt;em&gt;I’ve already thought about boycotting Amazon as they aren’t the most socially responsible company. We already buy too much on Amazon and we need to support other grocery stores.&lt;/em&gt;&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;As much as a I love the convenience, speed, and prices on Amazon, she’s right! So, for now, we’ll stick to shopping at Safeway. Sure, Safeway (a subsidiary of Albertsons) is one of the 5 largest grocery chains in the US, but at least it isn’t Amazon.&lt;/p&gt;&#10;&lt;p&gt;I was still left wondering if Safeway or Amazon is cheaper. So, I did a quick cost comparison between Safeway and Amazon Fresh by building a shopping cart of 32 similar items on each of their websites. In doing this, I found that their pricing was roughly the same for my basket of products, e.g. around $150:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/grocery-hacking-robot-framework/safeway-vs-amazon-fresh.png"&#10;&#9;&#9;&#9;alt="Two shopping carts of 32 similar items, one from Safeway and one from Amazon Fresh, with totals of roughly $150 each"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Safeway vs Amazon Fresh pricing. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Knowing that we wanted to continue using Safeway for grocery delivery and Alexa for our shopping list, I began to wonder how I could automate my shopping? Enter: Robotic Process Automation (RPA)&lt;/p&gt;&#10;&lt;h2 id="robot-process-automation-rpa"&gt;Robot Process Automation (RPA)&lt;/h2&gt;&#10;&lt;p&gt;If you haven’t heard of it, RPA is simply a way of automating repetitive tasks, that are normally done by a human, with a software robot. These are often tasks done in Graphical User Interfaces (GUIs) like web browsers, MS Excel, email clients, etc…&lt;/p&gt;&#10;&lt;p&gt;I had been casually reading about RPA for the past few years and even pitched it to my non-technical leadership peers at work, when they were looking for a way to automate operational processes without distracting our already-spread-thin engineers. It seemed like the perfect opportunity for me to jump in and build an RPA bot.&lt;/p&gt;&#10;&lt;p&gt;After doing some quick research, I quickly learned that there are a number of expensive proprietary RPA vendors like UIPath and Automation Anywhere. I don’t doubt that these systems are worth what you pay for them as they can ultimately reduce your labor costs. But, there are also a few open-source solutions like &lt;a href="https://github.com/robotframework/robotframework"&gt;Robot Framework&lt;/a&gt; and &lt;a href="https://github.com/kelaberetiv/TagUI"&gt;TagUI&lt;/a&gt;, and as a frequent contributor to open source, I’m a big fan of open source.&lt;/p&gt;&#10;&lt;p&gt;Currently, Robot Framework has the most GitHub stars and is supported by an extensive community. So, I fired up a quick &lt;a href="https://dev.to/thebadcoder/robot-framework-quick-start-ui-automation-4966"&gt;Getting Started with Robot Framework Tutorial&lt;/a&gt; and found it pretty straightforward to build a bot that could navigate a website. I found it very cool that Robot Framework is a layer on top of Python and therefore, it is easy to insert custom Python code into the bot. And, what I found particularly cool is that there are &lt;a href="https://github.com/fkromer/awesome-robotframework#libraries"&gt;community provided libraries&lt;/a&gt; for all types of things.&lt;/p&gt;&#10;&lt;h2 id="goals-of-the-bot"&gt;Goals of the bot&lt;/h2&gt;&#10;&lt;p&gt;Before we get into how I built my bot, let’s discuss my goals. As a software engineer, I knew I could craft some code that fired off API requests to the Todoist and Safeway backends. However, this would sidestep my desire to play with the Robot Framework. Instead, I opted to use the &lt;a href="https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html"&gt;SeleniumLibrary&lt;/a&gt; with Robot Framework, which under the hood would allow me to build a bot that uses Selenium to control a browser in a way that is very similar to how I would otherwise manually use a browser to do my grocery shopping.&lt;/p&gt;&#10;&lt;p&gt;Later, I’ll mention some of the pitfalls I found with RPA and why in the future, I would choose to make an API-based bot instead of a GUI-based bot. In fact, Todoist appears to have a supported &lt;a href="https://developer.todoist.com/"&gt;developer API&lt;/a&gt;. Safeway/Albertsons does not appear to have a developer API, but its website does make backend calls and therefore you could reverse engineer the backend API.&lt;/p&gt;&#10;&lt;p&gt;My other goal was to simplify the deployment architecture by not introducing another datastore. In other words, I wanted my bot to just use the data in Todoist and Safeway and not have to store anything in a separate datastore. Ultimately, I’m able to do my grocery shopping without writing down anything extra so why can’t the bot function without an additional database?&lt;/p&gt;&#10;&lt;h2 id="building-the-bot"&gt;Building the bot&lt;/h2&gt;&#10;&lt;p&gt;In the next few sections, I’m going to be walking through excerpts of my bot code. Please feel free to download the full source code at &lt;a href="https://github.com/redgeoff/grocery-shopping-bot"&gt;github.com/redgeoff/grocery-shopping-bot&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;If you are new to the Robot Framework, please read through &lt;a href="https://dev.to/thebadcoder/robot-framework-quick-start-ui-automation-4966"&gt;Robot Framework Quick Start: UI Automation Tutorial&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h3 id="reusable-keywords"&gt;Reusable keywords&lt;/h3&gt;&#10;&lt;p&gt;The Robot Framework refers to your typical programming language function as a &lt;em&gt;keyword&lt;/em&gt;. We’re going to define some keywords so that we can abstract some of the rough edges that you’ll encounter when working with a website in a browser. In particular, websites are beasts that load many elements asynchronously, which means that elements may not yet be visible or enabled. Bots are inherently much faster than humans so if you aren’t careful, it is easy for your bot to error out from the UI not yet being ready or because other race conditions occur. Of course, you could insert &lt;code&gt;Sleep&lt;/code&gt; statements in your code, which would simulate the timing that a human would encounter, but this is an anti-pattern because:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Adding &lt;code&gt;Sleep&lt;/code&gt; statements will make your bot much slower&lt;/li&gt;&#10;&lt;li&gt;Timing can vary based on the network, the backend servers, and even your compute power.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Instead, we’ll create some helper keywords that will wait for elements to be visible and enabled, and even introduce a retry loop for when the page takes an exorbitant amount of time to load or an unexpected error occurs.&lt;/p&gt;&#10;&lt;p&gt;Our first keyword will be &lt;code&gt;Wait Until Element Ready&lt;/code&gt;:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code class="language-robotframework" data-lang="robotframework"&gt;Wait Until Element Ready Inner&#10; [Arguments] ${locator} ${timeout}=None&#10;&#10; Wait Until Page Contains Element ${locator} ${timeout}&#10; Wait Until Element Is Visible ${locator}&#10; Wait Until Element Is Enabled ${locator}&#10; Scroll Element Into View ${locator}&#10; &#10; Wait Until Element Is Visible ${locator}&#10; Wait Until Element Is Enabled ${locator}&#10; Scroll Element Into View ${locator}&#10;&#10;Wait Until Element Ready&#10; [Arguments] ${locator} ${timeout}=None&#10; Wait Until Keyword Succeeds 3 1s Wait Until Element Ready Inner ${locator} ${timeout}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next, we’ll define &lt;code&gt;Click Element When Ready&lt;/code&gt;, which uses &lt;code&gt;Wait Until Element Ready&lt;/code&gt; and then clicks the element.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code class="language-robotframework" data-lang="robotframework"&gt;Click Element When Ready Inner&#10; [Arguments] ${locator}&#10; Wait Until Element Ready ${locator}&#10; ${element}= Get WebElement ${locator}&#10; Click Element ${element}&#10; [Return] ${element}&#10;&#10;Click Element When Ready&#10; [Arguments] ${locator}&#10; Wait Until Keyword Succeeds 3 1s Click Element When Ready Inner ${locator}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To make it so that we can resume the same browser session, which will help us to avoid Todoist sending us an email each time we log in, we’ll borrow a trick from &lt;a href="https://www.formulatedautomation.com/post/staying-logged-in-with-robot-framework-rpaframework-browser"&gt;this post&lt;/a&gt;.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code class="language-robotframework" data-lang="robotframework"&gt;Open Browser Profiled&#10; [Arguments] ${url}&#10; ${options}= Evaluate sys.modules[&amp;#39;selenium.webdriver&amp;#39;].ChromeOptions() sys, selenium.webdriver&#10; Call Method ${options} add_argument --user-data-dir\=${USER_DATA_PATH}&#10; Create WebDriver Chrome chrome_options=${options}&#10; Go To ${url}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Finally, using a trick from &lt;a href="https://stackoverflow.com/questions/53761393/no-keyword-with-name-get-child-webelement-found-in-robot/53763716#53763716"&gt;this post&lt;/a&gt;, we’ll define a way to get child elements. This will help us to do things like get a list of groceries and then pluck out the nested attributes.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code class="language-robotframework" data-lang="robotframework"&gt;Get Child WebElements&#10; [Arguments] ${locator} ${child_xpath}&#10;&#10; ${element} Get WebElement ${locator}&#10; ${children} Call Method&#10; ... ${element}&#10; ... find_elements&#10; ... by=xpath value=.${child_xpath}&#10;&#10; [Return] ${children}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The actual bot, has a number of &lt;a href="https://github.com/redgeoff/grocery-shopping-bot/blob/main/robot/common.robot"&gt;other reusable keywords&lt;/a&gt;, but they are omitted from this post for brevity.&lt;/p&gt;&#10;&lt;p&gt;We’ve now abstracted some of the less-fun stuff, let’s jump into making the bot do it’s thang!&lt;/p&gt;&#10;&lt;h3 id="get-the-list-from-todoist"&gt;Get the list from Todoist&lt;/h3&gt;&#10;&lt;p&gt;The first thing we want the bot to do is log in to Todoist, select our desired list, and then save the list to memory. The Todoist page that lists our items, looks like this:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/grocery-hacking-robot-framework/todoist-list.png"&#10;&#9;&#9;&#9;alt="A Todoist shopping list of grocery items"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Todoist shopping list. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;To avoid making extra log-ins and triggering emails from Todoist, we’ll make it so that the bot only logs in when we aren’t already logged in. This is particularly helpful during testing when you need to run the script MANY times to find all the race conditions that result from navigating the website in the browser. Naturally, you could log in (and log out) each time the script runs if you’d prefer.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code class="language-robotframework" data-lang="robotframework"&gt;*** Variables ***&#10;${TODOIST_TODAY} https://todoist.com/app/today&#10;${TODOIST_TODAY_TITLE} Today: Todoist&#10;&#10;*** Keywords ***&#10;&#10;Log In To Todoist&#10; [Arguments] ${email} ${password}&#10; Wait Until Element Ready email&#10; Wait Until Element Ready password&#10; Input Text email ${email}&#10; Input Password password ${password}&#10; Click Button Log in&#10; Wait Until Page Contains Today timeout=30s&#10; Update Settings If Needed&#10;&#10;Log In Or Open Todoist Inner&#10; [Arguments] ${email} ${password}&#10; Go To ${TODOIST_TODAY}&#10; Wait Until Page Contains Element xpath=//*[.=&amp;#39;Log in&amp;#39; or .=&amp;#39;Today&amp;#39;] # Wait for page to settle&#10; ${logged_in}= Run Keyword And Return Status Title Should Be ${TODOIST_TODAY_TITLE}&#10; IF not ${logged_in}&#10; Log In To Todoist ${email} ${password}&#10; END&#10;&#10;Log In Or Open Todoist&#10; [Arguments] ${email} ${password}&#10; Wait Until Keyword Succeeds 5x 2 sec Log In Or Open Todoist Inner ${email} ${password}&#10;&#10;Select Todoist List&#10; [Arguments] ${list_name}&#10; Update Settings If Needed&#10; Click Element When Ready xpath:(.//span[contains(., &amp;#39;${list_name}&amp;#39;)])[1]&#10;&#10;Get Todoist List Items&#10; @{locators}= Get WebElements xpath=//li[contains(@class,&amp;#39;task_list_item&amp;#39;)]&#10; ${items}= Create List&#10; FOR ${locator} IN @{locators}&#10; ${list_item} Get Child WebElements ${locator} //div[contains(@class,&amp;#39;task_list_item__content&amp;#39;)]&#10; ${name}= Get Text ${list_item}&#10; ${id}= Get Element Attribute ${locator} data-item-id&#10; ${item}= Create Dictionary id=${id} name=${name}&#10; Append To List ${items} ${item}&#10; END&#10; [Return] ${items}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you take a closer look at the &lt;code&gt;Get Todoist List Items&lt;/code&gt; keyword, you’ll see that we are using an XPath query to get all the &lt;code&gt;li&lt;/code&gt; elements with a &lt;code&gt;task_list_item&lt;/code&gt; class. And then we pluck the &lt;code&gt;list_item&lt;/code&gt; child element so that we can get the item &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;id&lt;/code&gt;. (Yes, this is how Todoist identifies the items in their HTML and if you think this looks brittle… well, yup)&lt;/p&gt;&#10;&lt;p&gt;You can find the complete code at &lt;a href="https://github.com/redgeoff/grocery-shopping-bot/blob/main/robot/todoist.robot"&gt;todoist.robot&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h3 id="buy-it-again"&gt;Buy it again&lt;/h3&gt;&#10;&lt;p&gt;After we get the items from Todoist, the next step is to add corresponding items to our Safeway cart. Now, we could just go to the Safeway search page and search for the item, but the search page orders by &lt;em&gt;Best Match&lt;/em&gt; and doesn’t allow you to order by the most recently purchased items._&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/grocery-hacking-robot-framework/safeway-search.png"&#10;&#9;&#9;&#9;alt="Safeway&amp;#39;s search results page, ordered by Best Match"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Safeway’s search page. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;The problem is that sometimes we buy items just because others are out of stock, even though we have a preference for the items that are out of stock. Instead, I want to first give preference to the items that we frequently purchase. This way, we’ll always attempt to first grab our preferred item, even if it is not in stock until the next time we shop. Sure as a human, I can simply pick what I want from the list, but the bot isn’t that smart. For the bot, we can do this by going to the &lt;em&gt;Buy It Again&lt;/em&gt; page and sorting by &lt;em&gt;Frequently Purchased&lt;/em&gt;:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/grocery-hacking-robot-framework/frequently-purchased.png"&#10;&#9;&#9;&#9;alt="Safeway&amp;#39;s Buy It Again page sorted by Frequently Purchased"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Sort by Frequently Purchased. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Our bot will then iterate through the list of items that we got from Todoist and look for the first match in Safeway’s &lt;em&gt;Buy It Again&lt;/em&gt; list. It will then add that item to the Safeway cart.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code class="language-robotframework" data-lang="robotframework"&gt;*** Variables ***&#10;${SAFEWAY_HOMEPAGE} https://www.safeway.com&#10;${SAFEWAY_BUY_IT_AGAIN_URL} https://www.safeway.com/shop/purchases/buy-it-again.html&#10;&#10;*** Keywords ***&#10;&#10;Capture List&#10; @{locators}= Get WebElements xpath=//product-item-v2&#10; ${items}= Create List&#10; FOR ${locator} IN @{locators}&#10; ${title} Get Child WebElements ${locator} //*[contains(@class,&amp;#39;product-title&amp;#39;)]&#10; ${name}= Get Text ${title}&#10; ${id}= Get Element Attribute ${title} data-bpn&#10; ${url}= Get Element Attribute ${title} href&#10; ${add_button} Get Child WebElements ${locator} //*[@data-qa=&amp;#39;addbutton&amp;#39;]&#10; ${available}= Run Keyword And Return Status Page Should Contain Element ${add_button}&#10; ${remove_button} Get Child WebElements ${locator} //*[@data-qa=&amp;#39;prdctdcrmntr&amp;#39;]&#10; ${in_cart}= Run Keyword And Return Status Page Should Contain Element ${remove_button}&#10; ${item}= Create Dictionary id=${id} name=${name} url=${url} available=${available} in_cart=${in_cart}&#10; Append To List ${items} ${item}&#10; END&#10; [Return] ${items}&#10;&#10;Sort Buy It Again Items&#10; Click Element When Ready //*[@data-qa=&amp;#39;srt-ptns-dflt&amp;#39;]&#10; Click Link Frequently Purchased&#10;&#10;Get Buy It Again Items&#10; [Arguments] ${max_pages}&#10; Go To ${SAFEWAY_BUY_IT_AGAIN_URL}&#10; Dismiss Popups If Needed&#10; Sort Buy It Again Items&#10; Load More Items ${max_pages}&#10; ${items}= Capture List&#10; [Return] ${items}&#10;&#10;Load More Items&#10; [Arguments] ${max_pages}&#10; ${result} ${condition}= Run Keyword And Ignore Error Wait Until Page Contains Load more error=false&#10; FOR ${i} IN RANGE 1 ${max_pages}&#10; Exit For Loop If &amp;#39;${result}&amp;#39;==&amp;#39;FAIL&amp;#39;&#10; Click Button Load more&#10; ${result} ${condition}= Run Keyword And Ignore Error Wait Until Page Contains Load more error=false&#10; END&#10;&#10;Add To Cart&#10; [Arguments] ${item_id}&#10;&#10; Wait Until Element Ready //*[@id=&amp;#39;addButton_${item_id}&amp;#39;]&#10;&#10; # Safeway requires a mouse over before the item can be added to the cart&#10; Set Focus To Element //*[@id=&amp;#39;addButton_${item_id}&amp;#39;]&#10; Mouse Over //*[@id=&amp;#39;addButton_${item_id}&amp;#39;]&#10;&#10; Click Element When Ready //*[@id=&amp;#39;addButton_${item_id}&amp;#39;]&#10;&#10; # Wait for loading spinner to disappear&#10; Wait Until Page Does Not Contain Element //*[@class=&amp;#39;quantity-loading&amp;#39;]&#10;&#10; # Wait enough time for cart addition to settle&#10; Wait Until Page Does Not Contain Element //*[@id=&amp;#39;addButton_${item_id}&amp;#39;]&#10; Wait Until Element Ready //*[@id=&amp;#39;dec_qtyInfo_${item_id}&amp;#39;] timeout=10s&#10;&#10;Buy It Again&#10; [Arguments] ${items_to_buy} ${max_pages}&#10; ${buy_it_again_items}= Get Buy It Again Items ${max_pages}&#10; ${items_in_cart}= Create List&#10; ${items_not_found}= Create List&#10; FOR ${item_to_buy} IN @{items_to_buy}&#10; Set Local Variable ${added_to_cart} False&#10; FOR ${buy_it_again_item} IN @{buy_it_again_items}&#10; ${name_matches}= Run Keyword And Return Status Should Contain ${buy_it_again_item.name} ${item_to_buy.name} ignore_case=True&#10; IF &amp;#39;${name_matches}&amp;#39;==&amp;#39;True&amp;#39; and (&amp;#39;${buy_it_again_item.available}&amp;#39;==&amp;#39;True&amp;#39; or &amp;#39;${buy_it_again_item.in_cart}&amp;#39;==&amp;#39;True&amp;#39;)&#10; # Look up the availability again as we may have just added the item to the cart so&#10; # we cannot use ${buy_it_again_item.available}&#10; ${available}= Run Keyword And Return Status Page Should Contain Element //*[@id=&amp;#39;addButton_${buy_it_again_item.id}&amp;#39;]&#10;&#10; # If the item is already in the cart then we just ignore it as this allows us to&#10; # retry the script without it adding any additional items to the cart&#10; IF &amp;#39;${available}&amp;#39;==&amp;#39;True&amp;#39;&#10; Log To Console Adding to cart: ${buy_it_again_item.name} (Todoist item name: ${item_to_buy.name})&#10; Wait Until Keyword Succeeds 10x 2 sec Add To Cart ${buy_it_again_item.id}&#10; ELSE&#10; Log To Console Already in cart: ${buy_it_again_item.name} (Todoist item name: ${item_to_buy.name})&#10; END&#10; Set To Dictionary ${item_to_buy} name_in_store=${buy_it_again_item.name}&#10; Append To List ${items_in_cart} ${item_to_buy}&#10; Set Local Variable ${added_to_cart} True&#10; Exit For Loop&#10; END&#10; END&#10; IF &amp;#39;${added_to_cart}&amp;#39;==&amp;#39;False&amp;#39;&#10; Append To List ${items_not_found} ${item_to_buy}&#10; END&#10; END&#10;&#10; [Return] ${items_in_cart} ${items_not_found} ${buy_it_again_items}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Did you notice the fun stuff happening in &lt;code&gt;Add To Cart&lt;/code&gt;? Well, the Safeway website requires that you mouse over an item and then click it before it can be added to the cart. As for the &lt;code&gt;Wait Until Page Does Not Contain Element&lt;/code&gt;, the website flashes a loading icon when an item is added to the cart and we want to wait until this icon is no longer displayed as this confirms that the item is in our cart. (Pro Tip: to inspect a disappearing element in Chrome, open the &lt;em&gt;Sources&lt;/em&gt; tab, and hit &lt;em&gt;F8&lt;/em&gt; to stop the browser execution and keep the icon displayed)&lt;/p&gt;&#10;&lt;p&gt;You can view the complete Safeway robot code at &lt;a href="https://github.com/redgeoff/grocery-shopping-bot/blob/main/robot/safeway.robot"&gt;safeway.robot&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h3 id="search-for-items-not-purchased-before"&gt;Search for items not purchased before&lt;/h3&gt;&#10;&lt;p&gt;If you are like me, and most of your grocery shopping includes the same 300 or so items, the bot will have completed almost your entire list just by shopping from the &lt;em&gt;Buy It Again&lt;/em&gt; (recently purchased) page. The remaining handful of items on your list will likely be those that you have never purchased before, e.g. you are trying a new recipe or your typical brand is out of stock.&lt;/p&gt;&#10;&lt;p&gt;There are also other reasons why you’ll have remaining items in your list:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Safeway truncates long product names, e.g. &lt;em&gt;Signature Care Wipes Sensitive…&lt;/em&gt;&lt;/li&gt;&#10;&lt;li&gt;The formatting of the items in your list may not match, e.g. &lt;em&gt;nutrigrain vs nutri-grain&lt;/em&gt;&lt;/li&gt;&#10;&lt;li&gt;The items in your list have names that don’t match the name of the Safeway item. As you might expect, Safeway associates hidden keywords with each product. For example, you may have &lt;em&gt;pepper jack cheese&lt;/em&gt; on your list, but the Safeway product may be called &lt;em&gt;Cheese Slices Pepper Jack&lt;/em&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;To handle these cases, our bot, visits the Safeway search page and simply picks the first item that is in stock to add to the cart:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code class="language-robotframework" data-lang="robotframework"&gt;Find Item And Buy It Again&#10; [Arguments] ${item_name} ${buy_it_again_items}&#10; Go To https://www.safeway.com/shop/search-results.html?q=${item_name}&#10; Load More Items 2&#10;&#10; ${added_to_cart} ${name_in_store}= Add Found Items With Buy It Again Badge&#10;&#10; [Return] ${added_to_cart} ${name_in_store}&#10;&#10;Find And Buy It Again&#10; [Arguments] ${items_to_buy} ${buy_it_again_items}&#10; ${items_in_cart}= Create List&#10; ${items_not_found}= Create List&#10; FOR ${item_to_buy} IN @{items_to_buy}&#10; ${added_to_cart} ${name_in_store} Find Item And Buy It Again ${item_to_buy.name} ${buy_it_again_items}&#10; IF &amp;#39;${added_to_cart}&amp;#39;==&amp;#39;True&amp;#39;&#10; Log To Console Found and adding to cart: ${item_to_buy.name} (Name in store: ${name_in_store})&#10; Set To Dictionary ${item_to_buy} name_in_store=${name_in_store}&#10; Append To List ${items_in_cart} ${item_to_buy}&#10; ELSE&#10; Log To Console Not found: ${item_to_buy.name}&#10; Append To List ${items_not_found} ${item_to_buy}&#10; END&#10; END&#10; [Return] ${items_in_cart} ${items_not_found}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="mark-todolist-items-completed"&gt;Mark Todolist items completed&lt;/h3&gt;&#10;&lt;p&gt;By now, our bot created a cart with every item it could find. The next step is to mark these items as completed on our Todoist list.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code class="language-robotframework" data-lang="robotframework"&gt;Mark Todoist Item Completed&#10; [Arguments] ${id}&#10; Click Element When Ready //button[@aria-describedBy=&amp;#39;task-${id}-content&amp;#39;]&#10;&#10;Mark Todoist Items Completed&#10; [Arguments] ${items}&#10; Set Local Variable ${i} 1&#10; FOR ${item} IN @{items}&#10; Log To Console Marking Todoist item #${i} as completed: ${item.name} (Name in Store: ${item.name_in_store})&#10; Wait Until Keyword Succeeds 10x 2 sec Mark Todoist Item Completed ${item.id}&#10; IF ${i} % 10 == 0&#10; Sleep 10&#10; ELSE&#10; Sleep 2&#10; END&#10; ${i} Evaluate ${i}+1&#10; END&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Hold on, what’s up with those &lt;code&gt;Sleep&lt;/code&gt; statements? Shouldn’t we avoid them? Yes, but Todoist appears to have some sort of rate limiting in place. In other words, if our bot it too fast, the website will error out as humans don’t complete items that quickly. In my testing, I found that when I took 2 seconds in between items and waited 10 seconds after each batch of 10 items I avoided this rate limiting. (You may be saying once again, &lt;em&gt;but this looks brittle&lt;/em&gt;… yup! 😵‍💫)&lt;/p&gt;&#10;&lt;h3 id="send-an-email-with-the-list-of-items-added-to-cart"&gt;Send an email with the list of items added to cart&lt;/h3&gt;&#10;&lt;p&gt;The last thing that I wanted my bot to do was to send an email that lists all the items that were added to the cart. The email should list the name of the item as it appeared on my Todoist list and as it appears in my Safeway shopping cart. I’m then able to inspect this email quickly to see if anything looks out of place. I’ve typically found that at least a couple items look funky.&lt;/p&gt;&#10;&lt;p&gt;One rare reason is that Safeway’s search can be a little buggy. For example, if you search for &lt;em&gt;bananas&lt;/em&gt;, the first result is a cabbage:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/grocery-hacking-robot-framework/banana-cabbage.png"&#10;&#9;&#9;&#9;alt="Safeway search results for banana where the first result is a cabbage"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Searching for “banana”, finds a cabbage. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Another thing that can occur is that a simple Todoist item like &lt;em&gt;onion&lt;/em&gt; can actually match both &lt;em&gt;Green Onion&lt;/em&gt; and &lt;em&gt;Yellow Onion&lt;/em&gt; in the &lt;em&gt;Buy It Again&lt;/em&gt; list. One way that I’ve thought about improving this in the future is to have a &lt;em&gt;Replacements&lt;/em&gt; list that I can use to train the bot so that whenever I say &lt;em&gt;onion&lt;/em&gt;, I mean &lt;em&gt;Yellow Onion.&lt;/em&gt; For the MVP of my bot however, a simple email like the following is enough to empower me to make a few manual tweaks to my cart:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/grocery-hacking-robot-framework/bot-email.png"&#10;&#9;&#9;&#9;alt="An email from the bot listing each Todoist item next to the Safeway item added to the cart"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Example grocery shopping bot email. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;From what I can tell, sending an email with Robot Framework isn’t as out-of-the-box as you would hope. Robocorp, a pretty cool hosting company for Robot Framework bots, maintains an RPA Library for &lt;a href="https://rpaframework.org/libraries/email_imapsmtp/"&gt;Email&lt;/a&gt;, but I found that it didn’t play well with the &lt;code&gt;ppodgorsek/robot-framework&lt;/code&gt; Docker image that I was using to run my robot in a container. In addition, I was excited to use a custom Python script in my bot and knew that sending an email with Python is trivial. So, I simply created a custom &lt;em&gt;Email.py&lt;/em&gt; file:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;import&lt;/span&gt; smtplib&lt;span style="color:#f92672"&gt;,&lt;/span&gt; ssl&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#f92672"&gt;from&lt;/span&gt; email.message &lt;span style="color:#f92672"&gt;import&lt;/span&gt; EmailMessage&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;def&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;send_html_email&lt;/span&gt;(smtp_username, smtp_password, smtp_server, smtp_port, email_recipient, subject, body):&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; msg &lt;span style="color:#f92672"&gt;=&lt;/span&gt; EmailMessage()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; msg[&lt;span style="color:#e6db74"&gt;&amp;#39;Subject&amp;#39;&lt;/span&gt;] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; subject&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; msg[&lt;span style="color:#e6db74"&gt;&amp;#39;From&amp;#39;&lt;/span&gt;] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; smtp_username&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; msg[&lt;span style="color:#e6db74"&gt;&amp;#39;To&amp;#39;&lt;/span&gt;] &lt;span style="color:#f92672"&gt;=&lt;/span&gt; email_recipient&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; msg&lt;span style="color:#f92672"&gt;.&lt;/span&gt;add_alternative(body, subtype&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#39;html&amp;#39;&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; context &lt;span style="color:#f92672"&gt;=&lt;/span&gt; ssl&lt;span style="color:#f92672"&gt;.&lt;/span&gt;create_default_context()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;with&lt;/span&gt; smtplib&lt;span style="color:#f92672"&gt;.&lt;/span&gt;SMTP_SSL(smtp_server, smtp_port, context&lt;span style="color:#f92672"&gt;=&lt;/span&gt;context) &lt;span style="color:#66d9ef"&gt;as&lt;/span&gt; server:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; server&lt;span style="color:#f92672"&gt;.&lt;/span&gt;login(smtp_username, smtp_password)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; server&lt;span style="color:#f92672"&gt;.&lt;/span&gt;send_message(msg);&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and exposed it via a &lt;code&gt;Send Email&lt;/code&gt; keyword:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code class="language-robotframework" data-lang="robotframework"&gt;*** Settings ***&#10;Documentation Send Email&#10;Library DateTime&#10;Library Email.py&#10;&#10;*** Keywords ***&#10;&#10;Send Email&#10; [Arguments] ${smtp_username} ${smtp_password} ${smtp_server} ${smtp_port} ${email_recipient} ${items_in_cart}&#10; ${date}= Get Current Date UTC exclude_millis=yes&#10; ${converted_date}= Convert Date ${date} result_format=%a %B %d %H:%M:%S UTC %Y&#10;&#10; Set Local Variable ${i} 1&#10; Set Local Variable ${body} &amp;lt;table border=&amp;#34;1&amp;#34;&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;th&amp;gt;#&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Todoist Item&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Name in Safeway Cart&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&#10; FOR ${item} IN @{items_in_cart}&#10; ${body}= Catenate ${body} &amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;${i}&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;${item.name}&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;${item.name_in_store}&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&#10; ${i} Evaluate ${i}+1&#10; END&#10; ${body}= Catenate ${body} &amp;lt;/table&amp;gt;&#10;&#10; Send Html Email ${smtp_username} ${smtp_password} ${smtp_server} ${smtp_port} ${email_recipient} Grocery shopping bot ${converted_date} ${body}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;Email.py&lt;/code&gt; script connects to an SMTP server and sends an HTML email. I simply set up a new Gmail account and enabled SMTP access by enabling the &lt;a href="https://kb.synology.com/en-global/SRM/tutorial/How_to_use_Gmail_SMTP_server_to_send_emails_for_SRM"&gt;allow less secure apps option&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;Note: it is very likely that emails sent this way will end up in your Spam folder so you may need to create a filter to keep them out of your Spam folder. Sending non-spamy emails from a proper mail host is a rather involved process and is out of the scope of this post.&lt;/p&gt;&#10;&lt;h2 id="running-the-bot"&gt;Running the bot&lt;/h2&gt;&#10;&lt;p&gt;Here’s a quick video that demos how to run the bot: &lt;a href="https://www.loom.com/share/fd321cbcaeba4337a6dd593190dff71c"&gt;watch the demo on Loom&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;To run the bot locally, follow the steps &lt;a href="https://github.com/redgeoff/grocery-shopping-bot#run-locally-without-docker"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;After you run the bot, you’ll see that an &lt;code&gt;output&lt;/code&gt; directory was created and it contains some new artifacts:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/grocery-hacking-robot-framework/output-directory.png"&#10;&#9;&#9;&#9;alt="A directory listing of the output folder with the Robot Framework log and report files"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Output directory contains Robot Framework logs. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;You can open the &lt;code&gt;log.html&lt;/code&gt; to view a detailed log from the bot, e.g.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/grocery-hacking-robot-framework/robot-log.png"&#10;&#9;&#9;&#9;alt="The Robot Framework log.html showing the bot&amp;#39;s steps and screenshots"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Robot Framework logs. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;This log, and the embedded screenshots, can be quite helpful to troubleshoot any issues.&lt;/p&gt;&#10;&lt;h2 id="useful-tooling"&gt;Useful tooling&lt;/h2&gt;&#10;&lt;p&gt;If you are a Visual Studio (VS) Code user, I found that the &lt;a href="https://marketplace.visualstudio.com/items?itemName=TomiTurtiainen.rf-intellisense"&gt;Robot Framework Intellisense&lt;/a&gt; extension can be very helpful.&lt;/p&gt;&#10;&lt;p&gt;If you are not already acquainted with the &lt;em&gt;Inspect&lt;/em&gt; feature in the browser console, I suggest that you give it a try as it allows you to discover the structure of the elements on a webpage:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/grocery-hacking-robot-framework/inspect-firefox.png"&#10;&#9;&#9;&#9;alt="Firefox&amp;#39;s Inspect feature highlighting an element in a web page"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Inspect HTML elements in Firefox. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;As you may have noticed in the code above, because Todoist’s and Safeway’s websites aren’t really the most accessible, the bot uses XPath to navigate the websites. For example, we select all the Todoist items with:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;Get WebElements xpath=//li[contains(@class,&amp;#39;task_list_item&amp;#39;)]&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A lesser known feature of Chrome and Firefox is that you can actually test out your XPath query in the console with the &lt;code&gt;$x()&lt;/code&gt; function:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/grocery-hacking-robot-framework/xpath-firefox.png"&#10;&#9;&#9;&#9;alt="Testing an XPath query with the $x() function in the Firefox console"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Test XPath queries in Firefox. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;h2 id="running-the-bot-in-headless-mode-using-docker"&gt;Running the bot in headless mode using Docker&lt;/h2&gt;&#10;&lt;p&gt;What if you want to run the bot without a UI (headless) and in the background? Easy, just follow the directions at &lt;a href="https://github.com/redgeoff/grocery-shopping-bot/blob/main/README.md#run-locally-with-docker"&gt;Run Locally with Docker&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;What if you want to deploy the bot to the cloud? I’ll cover this in detail in a later post, but for now, take a look at the steps at &lt;a href="https://github.com/redgeoff/grocery-shopping-bot#delpoy-to-aws"&gt;Deploy to AWS&lt;/a&gt; for how I run the bot for just pennies a month.&lt;/p&gt;&#10;&lt;h2 id="possible-enhancements"&gt;Possible Enhancements&lt;/h2&gt;&#10;&lt;p&gt;This bot is just a Minimal Viable Product (MVP). Here are some things that I think could be added to make it even more useful:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;As mentioned above, a list of &lt;em&gt;Replacements&lt;/em&gt; could be used to substitute items like &lt;em&gt;onion&lt;/em&gt; with &lt;em&gt;Yellow Onion&lt;/em&gt; so that it doesn’t just match &lt;em&gt;Green Onion.&lt;/em&gt;&lt;/li&gt;&#10;&lt;li&gt;Assuming that a human will perform minor tweaks to the list after the bot has run, it should be possible for the bot to learn from these tweaks. In other words, if the bot determines that a &lt;em&gt;Yellow Onion&lt;/em&gt; was purchased, when the original item on the list was &lt;em&gt;onion,&lt;/em&gt; it could add &lt;code&gt;onion =&amp;gt; yellow onion&lt;/code&gt; to the &lt;em&gt;Replacements&lt;/em&gt; list on your behalf.&lt;/li&gt;&#10;&lt;li&gt;Keyword expansion could be used so that a single word can represent a basket of items. For example, your family may eat a lot of fruit and want the &lt;em&gt;fruit&lt;/em&gt; keyword to include strawberries, raspberries, and blueberries. This feature could also be good for ordering all the groceries for a dish. Perhaps, this is implemented as part of the &lt;em&gt;Replacements&lt;/em&gt; list, i.e. &lt;code&gt;lasagne =&amp;gt; ricotta, beef, eggs, ...&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;Support for item counts. For example, &lt;em&gt;7 bananas&lt;/em&gt; would add 7 bananas to the shopping cart. For now, the bot adds just one banana to the cart and a human has to increase the count manually after the bot has finished executing.&lt;/li&gt;&#10;&lt;li&gt;On Safeway’s product pages there is sometimes an option to clip a coupon for a specific product, i.e. you can save some money on the purchase. The bot should do this for you.&lt;/li&gt;&#10;&lt;li&gt;As shown above, Alexa’s &lt;em&gt;shop your list&lt;/em&gt; feature provides a UI that allows you to select from a list of the top search results that match the item on your shopping list. There could be value in displaying such an interface after the bot has run so that you can make any needed tweaks quickly.&lt;/li&gt;&#10;&lt;li&gt;The bot could analyze your purchasing habits and make recommendations to buy certain items in bulk when you consume items fast enough and when purchasing in bulk would result in saving money.&lt;/li&gt;&#10;&lt;li&gt;Use machine learning to suggest products that have never been shopped for (new products) based on your existing preferences for brand, price, flavor, and size.&lt;/li&gt;&#10;&lt;li&gt;Support for other grocery stores. The existing bot already supports any smart speaker that integrates with Todoist, but it only supports the grocery store, Safeway.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="is-rpa-the-best-way-to-implement-this-bot"&gt;Is RPA the best way to implement this bot?&lt;/h2&gt;&#10;&lt;p&gt;Probably not. As much fun as it was playing with the Robot Framework, the use of the framework, through the browser, led to a fairly brittle solution. If Todoist or Safeway decide to change their websites, the bot will most likely break.&lt;/p&gt;&#10;&lt;p&gt;I’d argue that the best way to implement such a bot is to use an officially supported &lt;a href="https://developer.todoist.com/guides/#developing-with-todoist"&gt;developer API&lt;/a&gt;, such as what is provided by Todoist (or Alexa). The main reason is that this API is a contract with which Todoist will attempt to adhere. Or, in other words, it would make it a lot less likely that Todoist would introduce a change that would break the bot.&lt;/p&gt;&#10;&lt;p&gt;Safeway on the other hand, doesn’t appear to have a supported developer API. Instead, you could use the browser’s &lt;em&gt;Network&lt;/em&gt; tab to reverse engineer their backend API. Using this API would significantly improve the bot in that it would:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Make it less dependent on the presentation layer (HTML) of the website. In other words, the look of the website could change, but as long as the backend API doesn’t change, the bot wouldn’t break. Even though Safeway doesn’t expose the developer API officially, they likely have teams of engineers working on the backend and frontend and they would most likely avoid breaking the API as it could cause instability in their website and reduce their team’s velocity.&lt;/li&gt;&#10;&lt;li&gt;Make it much faster as navigating the GUI is SLOW.&lt;/li&gt;&#10;&lt;li&gt;Reduce the amount of resources that the bot consumes as making a RESTful API call takes a lot less resources than running a browser&lt;/li&gt;&#10;&lt;li&gt;Reduce the possibility for race conditions that are encountered in the browser when a bot navigates the GUI faster than a human&lt;/li&gt;&#10;&lt;li&gt;Avoid the complexity of working with popups, which may be added at any time to the GUI and may have little to do with the bot’s primary objective&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="how-has-the-bot-been-working-for-me"&gt;How has the bot been working for me?&lt;/h2&gt;&#10;&lt;p&gt;I’ve been using the bot now for several weeks. I’ve found that it accurately builds a cart for about 90% of the items on my list. I then go in and make some minor tweaks to my shopping cart via the Safeway mobile app, but overall, I’ve cut down the weekly time I spend ordering groceries from about 30 minutes to 5 minutes. So, yeah, it works pretty well for me!&lt;/p&gt;&#10;&lt;p&gt;I host the bot on AWS Batch for pennies a month and have even created a custom Alexa skill that kicks off the bot. This means that after my Alexa list is done, I can just say &lt;em&gt;open grocery shopping bot&lt;/em&gt; and the bot does its magic. In a future post, I’ll dive into how this deployment works, but for now, you can play with it &lt;a href="https://github.com/redgeoff/grocery-shopping-bot/blob/main/README.md#delpoy-to-aws"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;Enjoyed this article?&lt;/strong&gt; If this bot were turned into an easy-to-use service and supported your preferred grocery store and smart speaker, would you pay for it? Please consider taking 3 minutes to &lt;a href="https://docs.google.com/forms/d/e/1FAIpQLSeVdhAr3ucfz6YhLDKYeWUMTcJAB5woLiQqgjW80cYti9TdEw/viewform?usp=sf_link"&gt;answer these 3 quick questions&lt;/a&gt;&lt;/p&gt;&#10;&lt;p&gt;Thanks for reading this post.&lt;/p&gt;&#10;&lt;h2 id="resources"&gt;Resources&lt;/h2&gt;&#10;&lt;p&gt;Here is a list of helpful resources mentioned in this article:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://docs.google.com/forms/d/e/1FAIpQLSeVdhAr3ucfz6YhLDKYeWUMTcJAB5woLiQqgjW80cYti9TdEw/viewform?usp=sf_link"&gt;Grocery Shopping Bot Survey&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/redgeoff/grocery-shopping-bot"&gt;github.com/redgeoff/grocery-shopping-bot&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://dev.to/thebadcoder/robot-framework-quick-start-ui-automation-4966"&gt;Robot Framework Quick Start: UI Automation Tutorial&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/robotframework/robotframework"&gt;github.com/robotframework/robotframework&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;</description></item><item><title>We Migrated From AWS Lambda to ECS, but Hope to Eventually Migrate Back</title><link>https://redgeoff.com/posts/lambda-to-ecs/</link><pubDate>Tue, 17 Aug 2021 07:16:09 -0700</pubDate><guid>https://redgeoff.com/posts/lambda-to-ecs/</guid><description>&lt;p&gt;The pitfalls of Lambda that made us switch away&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/lambda-to-ecs/lambda-vs-ecs.png"&#10;&#9;&#9;&#9;alt="Lambda vs ECS"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Image credit: &lt;a href="https://unsplash.com/@attentieattentie"&gt;Attentie Attentie&lt;/a&gt; on &lt;a href="https://unsplash.com"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;&lt;em&gt;Author’s note: The views expressed in this post are my own and not necessarily those of my employer.&lt;/em&gt;&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;This is a story about how we migrated from Lambda to ECS because of a 2% error rate and limited concurrency controls. And, about how AWS has more recently fixed their error-rate issues and how enhancements to concurrency controls could win us back to Lambda.&#10;When I first started at &lt;a href="https://knockcrm.com"&gt;Knock&lt;/a&gt; in 2018, we were running our entire stack on AWS EC2 instances that we were manually managing.&lt;/p&gt;&#10;&lt;p&gt;As a result, it was painful to scale and maintain our infrastructure, and we were wasting money as we were unable to distribute our resource consumption. We were considering a managed container service like AWS Elastic Container Service (ECS) and AWS’s most recent serverless option, Lambda.&lt;/p&gt;&#10;&lt;p&gt;AWS claimed that Lambda was a reliable, feature-rich environment that abstracts away any notion of having to manage a server, and we were excited about the prospect of just being able to focus on the code.&lt;/p&gt;&#10;&lt;p&gt;Unfortunately, after a year of using Lambda in production, we observed Lambda was a relatively unreliable technology and when it was used to deliver customer-facing features, a 2% error rate in the AWS layer resulted in a very poor user experience. So, we decided to migrate some of our customer-facing Lambdas to ECS.&lt;/p&gt;&#10;&lt;p&gt;Before we dive into the details, I’d like to clarify that this post is not a hate letter to AWS about Lambda.&lt;/p&gt;&#10;&lt;p&gt;As you will read later, in 2020, AWS made improvements to Lambda networking, which has almost completely eroded the error rates we were seeing.&lt;/p&gt;&#10;&lt;p&gt;This leaves just the concern of concurrency, which is something that can be mitigated in certain use cases, but not all.&lt;/p&gt;&#10;&lt;p&gt;Because there is one shared concurrency pool for all your Lambdas, a burst of activity in any of your Lambdas can easily lead to significant throttling of mission-critical functionality, which results in an unacceptable user experience.&lt;/p&gt;&#10;&lt;p&gt;Overall, I’m hopeful that AWS will eventually enhance the concurrency model for Lambda and remain committed to the promise of a world where engineers spend less time managing servers.&lt;/p&gt;&#10;&lt;h3 id="what-were-we-running-on-lambda"&gt;What Were We Running On Lambda?&lt;/h3&gt;&#10;&lt;p&gt;Let’s start by describing the types of services we had running on Lambda, as after all, we can sometimes tolerate latency and failures in certain use cases.&lt;/p&gt;&#10;&lt;p&gt;The first class of services comprises a backend of RESTful APIs fronted by API Gateway and backed by a Lambda function for each route.&lt;/p&gt;&#10;&lt;p&gt;We had both Node.js (Typescript) and Python code running in this layer. Although the programming language choices here have little impact on what we discuss in this post, they do impact cost and overall Lambda concurrency and latency.&lt;/p&gt;&#10;&lt;p&gt;The main callout here is that most of these APIs were being called by frontends synchronously and thus, significant latency and failure is unacceptable as it manifests as downtime to the user.&lt;/p&gt;&#10;&lt;p&gt;The second class of services mostly consists of background tasks that read messages from SQS queues and then perform IO operations on RDS instances and S3 buckets.&lt;/p&gt;&#10;&lt;p&gt;In our case, nothing in this pipeline is too time-sensitive, so if these background tasks fail occasionally, a retry mechanism is enough to keep our system healthy and everyone happy.&lt;/p&gt;&#10;&lt;h3 id="up-to-2-error-rates-in-lambda-from-20192020"&gt;Up to 2% Error Rates in Lambda From 2019–2020&lt;/h3&gt;&#10;&lt;p&gt;It wasn’t until we launched RESTful APIs in production that we began to notice that transient errors in the AWS layer were common for AWS Lambda. In particular, we started noticing errors in CloudWatch where a Lambda would timeout before it even ran any of our application-layer code. We knew the application code was not running as the very first thing our application code does is log the incoming request and these log statements from our application layer were missing. In other words, there was nothing that we could tune to prevent this from happening as the issue was occurring in the AWS layers. For example:&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;START RequestId: 56a12bf8–43eb-4b6c-8a20-c73fa2152c6b Version: $LATEST&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;&lt;strong&gt;[Missing entries from application logs]&lt;/strong&gt;&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;END RequestId: 56a12bf8–43eb-4b6c-8a20-c73fa2152c6b&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;REPORT RequestId: 56a12bf8–43eb-4b6c-8a20-c73fa2152c6b Duration: 30019.42 ms Billed Duration: 30000 ms Memory Size: 1024 MB Max Memory Used: 77 MB&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;2019–08–22T20:10:31.270Z 56a12bf8–43eb-4b6c-8a20-c73fa2152c6b &lt;strong&gt;Task timed out after 30.02 seconds&lt;/strong&gt;&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;On August 28th, 2019, We reached out to AWS via a support ticket and they confirmed that a 1–2% failure rate was somewhat normal:&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;Just to summarize the conversation, we talked about a 1%-2% failure rate not being outside of the realm of possibility in spikes, and the recommended implementation is backoff and retry on the client side.&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;AWS recommended a retry mechanism, but in practice, the only way to detect these problems was by setting a more aggressive timeout and then retrying.&lt;/p&gt;&#10;&lt;p&gt;Some of our legacy code in the critical code path can take several seconds to run during heavy times of load and Lambda cold starts used to be fairly significant.&lt;/p&gt;&#10;&lt;p&gt;So, let’s say that we implemented a five-second timeout and then retried at this point. This would result in the user having to wait at least five seconds, which is unacceptable when it happens 2% of the time!&lt;/p&gt;&#10;&lt;p&gt;Moreover, our backend is distributed, and therefore operations are not always atomic, meaning that there would be an increased possibility of duplicate requests when aborting the Lambda invocation and retrying the request.&lt;/p&gt;&#10;&lt;p&gt;This was a major deal-breaker for us, so we decided to start migrating away from Lambda at the end of 2019.&lt;/p&gt;&#10;&lt;h3 id="shared-concurrency-leads-to-throttled-execution-and-frustrated-users"&gt;Shared Concurrency Leads To Throttled Execution and Frustrated Users&lt;/h3&gt;&#10;&lt;p&gt;One could argue that the concurrency control capabilities of Lambda are severely lacking as there is a single per-region cap on Lambda currency per AWS account.&lt;/p&gt;&#10;&lt;p&gt;This means that any bursting Lambda activity can cause customer-facing Lambdas to be throttled. Here, I’m defining customer-facing Lambdas as those fielding synchronous requests from our users via a UI.&lt;/p&gt;&#10;&lt;p&gt;In other words, Lambda-powered background tasks can starve these customer-facing Lambdas and can essentially result in your app becoming unresponsive. This manifests as an outage to your users and is completely unacceptable when it occurs frequently.&lt;/p&gt;&#10;&lt;p&gt;As illustrated in the next set of graphs, you can see that spikes in customer-facing errors roughly correlate to the spikes in our account’s Lambda concurrency:&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/lambda-to-ecs/concurrent-executions.png"&#10;&#9;&#9;&#9;alt="Errors from hitting max concurrency"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Errors from hitting max concurrency. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;This cannot be easily prevented as there is a limit of 1,000 concurrent Lambdas on our account, and the only way to bound Lambda executions is by reserving part of this pool of 1,000. In other words, each time you reserve concurrency for a Lambda, you forever take away potential concurrency for your other Lambdas, resulting in an awkward juggling act where you have some Lambdas that are statically bounded and others that are not.&lt;/p&gt;&#10;&lt;p&gt;If you are familiar with AWS Elastic Container Service (ECS) or Elastic Kubernetes Service (EKS), you’ll know that it is fairly trivial to control the number of containers (worker pool) by scaling up or down the number of tasks running for a service. In other words, you have full control over how you allocate your resources and aren’t locked into a primitive per-function concurrency model — like you are with Lambda. In a later section in this post, we’ll investigate how I think AWS could improve its concurrency controls to facilitate better resource sharing.&lt;/p&gt;&#10;&lt;p&gt;Given Lambda’s basic concurrency controls, is there anything we can do to prevent throttling our user-facing Lambdas? Yes, you can:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;temporarily constrain the concurrency of more Lambdas with reserved concurrency&lt;/li&gt;&#10;&lt;li&gt;file a request with AWS support to bump up your concurrency limit from 1,000&lt;/li&gt;&#10;&lt;li&gt;increase the batch size on your SQS reads to a maximum of 10. This means that it takes fewer Lambdas to service the same SQS traffic. Let’s just hope you structured your code to work this way before your Lambdas were throttled&lt;/li&gt;&#10;&lt;li&gt;set the reserved or provisioned concurrency for user-facing Lambdas. This is fine if you only have a few Lambdas, but if you have one for each RESTful API route, you could have hundreds of Lambdas and you’ll quickly get into a situation where you actually lock up all of your concurrency. For example, imagine you have just 10 Lambdas/routes and set the reserved concurrency at 20 for each of these Lambdas, resulting in a total reserved concurrency of 20 x 10 = 200 (recall you have a max of 1,000 for your entire AWS account). That number can quickly add up and what’s more is that each of these Lambdas is now bound to a max concurrency of 20, meaning that your user-facing Lambdas aren’t going to scale as your traffic increases, which is a big reason why you even use Lambda in the first place.&lt;/li&gt;&#10;&lt;li&gt;stand up a SQS queue in front of API endpoints for things that are resource-intensive and/or can be processed asynchronously, e.g., email sending and report generation. This allows the customer-facing Lambdas to complete their execution faster as the resource-intensive work is deferred. Although this is typically a good pattern as it reduces latency to your user, it’s not always enough to keep your Lambda concurrency low enough to prevent throttling and it may just increase the concurrency among your background tasks if you are using Lambda to run your background tasks.&lt;/li&gt;&#10;&lt;li&gt;Move your user-facing Lambdas to a different region so that you have access to a different concurrency pool of 1,000. This can greatly increase the complexity of your deployments and it may even mean that other services like Cognito, DynamoDB, SQS, etc., would need to be moved to another region.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;A lot of these mitigations are good things to consider when using Lambda, but they don’t solve the root problem of very limited concurrency controls leading to throttling. If you use these mitigations, you’re basically just incurring tech debt that you’ll be on the hook for later.&lt;/p&gt;&#10;&lt;p&gt;Poor error rates and unavoidable throttling? These were both deal-breakers, so we had no choice but to migrate away from Lambda.&lt;/p&gt;&#10;&lt;h3 id="refactoring-our-code-so-that-it-can-run-on-both-lambda-and-ecs"&gt;Refactoring Our Code So That It Can Run On Both Lambda And ECS&lt;/h3&gt;&#10;&lt;p&gt;Now that we had decided to migrate away from Lambda, we wondered if there was a way to make a minimal set of changes to our code so that it could also run in a Docker environment.&lt;/p&gt;&#10;&lt;p&gt;We chose AWS’s Elastic Container Service (ECS) to run our containers as it is relatively easy to manage and something that our DevOps team had the bandwidth to support. Fortunately, we noticed that the Lambda handlers we already implemented utilize a nice abstraction, which could be used to translate from a Docker-supported framework to a Lambda one and vice-versa. All we needed to do was create a wrapper that would:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;translate the ExpressJS request payload to the event payload needed by the Lambda handler&lt;/li&gt;&#10;&lt;li&gt;translate the Lambda response to an ExpressJS response&lt;/li&gt;&#10;&lt;li&gt;use a wrapper that would allow us to reuse all our existing Lambda handler code and run it on top of the ExpressJS framework! We chose to go with ExpressJS to build the RESTful API as we were using Node.js and ExpressJS is a battle-tested framework. The following implementation is in JS but the pattern will work with any web framework and any programming language. (Our actual implementation is in TypeScript.)&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Let’s take a look at some code to see how we did this. Consider a simplified version of a Lambda handler that implements a &lt;code&gt;POST /lead/:leadId&lt;/code&gt; route, which accepts a JSON body of &lt;code&gt;{ name, email }&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;leadService&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;./lead-service&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Define a handler for `PUT /lead/:leadId` that accepts the request body&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// payload `{ name, email }`&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;handler&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;async&lt;/span&gt; (&lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;) =&amp;gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Parse the request parameters&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; { &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;email&lt;/span&gt; } &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;JSON&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;parse&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;body&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; { &lt;span style="color:#a6e22e"&gt;leadId&lt;/span&gt; } &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;pathParameters&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Assume this is the service-layer code that actually performs the&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// updating of the lead&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;await&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;leadService&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;update&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;leadId&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;email&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Return a 200 success code and the full lead&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;statusCode&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;200&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;body&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;JSON&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;stringify&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;id&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;leadId&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;email&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;export&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;default&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;handler&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We created a &lt;code&gt;lambdaToExpress&lt;/code&gt; function that translates from ExpressJS to Lambda and back again, using the following code:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Convert from a Lambda to an ExpressJS handler&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;lambdaToExpress&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;async&lt;/span&gt; (&lt;span style="color:#a6e22e"&gt;handler&lt;/span&gt;) =&amp;gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;async&lt;/span&gt; (&lt;span style="color:#a6e22e"&gt;req&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;res&lt;/span&gt;) =&amp;gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Repackage the express parameters as an event object for the Lambda&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;event&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;body&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;JSON&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;stringify&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;req&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;body&lt;/span&gt;),&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;headers&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;req&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;headers&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;queryStringParameters&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;req&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;query&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;pathParameters&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;req&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;params&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;handlerRes&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;await&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;handler&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Instruct express to use the response from the Lambda &#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;res&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;status&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;handlerRes&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;statusCode&lt;/span&gt;).&lt;span style="color:#a6e22e"&gt;json&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;JSON&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;parse&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;handlerRes&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;body&lt;/span&gt;))&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; };&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;export&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;default&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;lambdaToExpress&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then, we registered our ExpressJS routes using our new wrapper. Here is its code:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;express&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;express&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;cors&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;cors&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;handler&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;./lambda&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;lambdaToExpress&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;./lambda-to-express&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;PORT&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;3000&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;app&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;express&lt;/span&gt;();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;app&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;use&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;cors&lt;/span&gt;());&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;app&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;use&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;express&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;json&lt;/span&gt;()); &lt;span style="color:#75715e"&gt;// Parse JSON bodies&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;app&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;put&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;/lead/:leadId&amp;#39;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;lambdaToExpress&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;handler&lt;/span&gt;));&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;app&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;listen&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;PORT&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With just a few lines of code, we enhanced our single codebase to support both Lambda and ExpressJS. One thing to note is that both the &lt;code&gt;lambdaToExpress&lt;/code&gt; and &lt;code&gt;handler&lt;/code&gt; functions are performing a &lt;code&gt;JSON.stringify()&lt;/code&gt; and &lt;code&gt;JSON.parse()&lt;/code&gt;, which adds a tiny bit of extra overhead. That being said, these operations are very quick and unnoticeable in the grand scheme of things.&lt;/p&gt;&#10;&lt;p&gt;The code was all set, but what about anything provided by AWS’s API Gateway?&lt;/p&gt;&#10;&lt;p&gt;As part of our API is external-facing, we needed to support rate-limiting. To achieve this, we incorporated &lt;a href="https://github.com/animir/node-rate-limiter-flexible/wiki/Overall-example#authorized-and-not-authorized-users"&gt;node-rate-limiter-flexible&lt;/a&gt; (backed by ElasticCache) to implement rate-limiting. And, we simply added this rate-limiting check to our &lt;code&gt;lambdaToExpress&lt;/code&gt; wrapper to implement this in a single place.&lt;/p&gt;&#10;&lt;p&gt;We were using the &lt;a href="https://www.serverless.com/"&gt;Serverless Framework&lt;/a&gt; to deploy our Lambdas, and you could probably use this framework to deploy code to ECS, but we find &lt;a href="https://www.terraform.io/"&gt;Terraform&lt;/a&gt; to be a slightly more versatile tool for configuring non-serverless infrastructure and were already using Terraform to deploy other microservices.&lt;/p&gt;&#10;&lt;p&gt;So, we created &lt;a href="https://www.terraform.io/docs/language/modules/develop/index.html"&gt;Terraform Modules&lt;/a&gt; alongside our &lt;code&gt;serverless.yml&lt;/code&gt; configuration and deployed them with &lt;a href="https://www.jenkins.io/doc/book/pipeline/jenkinsfile/"&gt;Jenkinsfiles&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;And now, we were running on ECS!&lt;/p&gt;&#10;&lt;p&gt;We have been happily running a portion of our RESTful API backend on AWS Lambda for over a year and have found that we have reduced our compute costs by 90%! In a future post, I’ll dive into a cost comparison between Lambda and ECS.&lt;/p&gt;&#10;&lt;h3 id="lambda-reduced-its-error-rate-from-2-to-00000625"&gt;Lambda Reduced Its Error Rate From 2% To 0.0000625%&lt;/h3&gt;&#10;&lt;p&gt;If you recall from earlier, in August 2019, we found that AWS Lambda could suffer from an error rate of up to 2%!&lt;/p&gt;&#10;&lt;p&gt;In September 2019, AWS released a blob post touting &lt;a href="https://aws.amazon.com/blogs/compute/announcing-improved-vpc-networking-for-aws-lambda-functions/"&gt;improvements to networking for Lambda&lt;/a&gt; that would reduce latency incurred with cold starts. We were very excited about this news as it meant that AWS had acknowledged the problem and was actively working on a fix.&lt;/p&gt;&#10;&lt;p&gt;We had migrated some of our backends from Lambda to ECS but had a lot of remaining functionality on Lambda.&lt;/p&gt;&#10;&lt;p&gt;In 2021, we performed another analysis and found that the latest version of Lambda was resulting in an error rate of only 0.0000625%. This is a significant improvement from 2%!&lt;/p&gt;&#10;&lt;p&gt;Here is the analysis we did for our auth-service, a RESTful API responsible for authentication and authorization functionality in our system. In a seven-day period, the auth-service had about 1.6 million invocations and only one resulted in a timeout.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/lambda-to-ecs/auth-service-invocations.png"&#10;&#9;&#9;&#9;alt="auth-service invocations"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;auth-service invocations. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;figure&gt;&lt;img src="https://redgeoff.com/posts/lambda-to-ecs/auth-service-errors.png"&#10;&#9;&#9;&#9;alt="auth-service errors"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;auth-service invocations. Image credit: Author&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;With error rates as low as this, our error rate concern with user-facing Lambdas has been eliminated! But what about the lingering concern with concurrency controls?&lt;/p&gt;&#10;&lt;h3 id="how-could-aws-improve-its-concurrency-controls"&gt;How Could AWS Improve Its Concurrency Controls?&lt;/h3&gt;&#10;&lt;p&gt;If you recall, we ended up performing many of the concurrency mitigations mentioned above and moved a number of our Lambdas to ECS.&lt;/p&gt;&#10;&lt;p&gt;Afterward, we were left with a relatively comfortable set of about 200 Lambdas, and as a result, bursting concurrency is now less of a concern.&lt;/p&gt;&#10;&lt;p&gt;That being said, we now avoid using Lambda for any new user-facing services. And, we generally avoid using it for substantial background jobs as we are worried that excessive use will result in throttling our few remaining user-facing Lambdas.&lt;/p&gt;&#10;&lt;p&gt;Despite this current strategy, I’m hoping the day will come when AWS implements a more feature-rich implementation of Lambda concurrency controls that better mimics that of an ECS environment. Here are some enhancements that would make a huge difference:&lt;/p&gt;&#10;&lt;h4 id="max-concurrency"&gt;Max Concurrency&lt;/h4&gt;&#10;&lt;p&gt;Let’s define the Max Concurrency of a Lambda as a setting for the max concurrent invocations of a specific Lambda. This value is not the same as Reserved Concurrency in that the value of Max Concurrency wouldn’t subtract from the overall concurrency available for the AWS account.&lt;/p&gt;&#10;&lt;p&gt;Here’s an example: assume Lambda A has a Max Concurrency of 100 and your account has a global max concurrency of 1,000 (the AWS default).&lt;/p&gt;&#10;&lt;p&gt;Lambda A would not be throttled until there are over 100 instances of Lambda A running concurrently. Other Lambdas, e.g., Lambda B/C, can still consume up to 1,000 instances, if Lambda A has 0 running instances.&lt;/p&gt;&#10;&lt;p&gt;This in essence means that no matter what, Lambda A cannot consume more than 100 instances of the total 1,000, but doesn’t require us to set aside 100 instances from our total 1,000 for Lambda A.&lt;/p&gt;&#10;&lt;p&gt;This setting is somewhat akin to having an ECS service that can burst to an upper bound of max tasks but doesn’t require us to reserve a portion of our cluster, even if that service is not running all the instances.&lt;/p&gt;&#10;&lt;h4 id="group-max-concurrency"&gt;Group Max Concurrency&lt;/h4&gt;&#10;&lt;p&gt;We consider a service to be a group of related Lambdas. For example, consider 10 Lambdas that comprise 10 RESTful endpoints of a service. Or, 10 Lambdas that define the workers of an ETL pipeline. Often, we’d like to define an upper bound on concurrency for a group of Lambdas, e.g., we don’t want our ETL pipeline to consume more than 100 out of our total of 1,000 current Lambda invocations.&lt;/p&gt;&#10;&lt;p&gt;A Group Max Concurrency could effectively be accomplished by first allowing you to tag Lambdas to be part of a group. Second, you could then set the Group Max Concurrency for that group. Or AWS could even reuse the concept of a “stack” and have a Stack Max Concurrency.&lt;/p&gt;&#10;&lt;p&gt;This Group Max Concurrency is similar to how you may have an ECS service where each container is performing multiple (a group of) operations, e.g., a web server fielding requests for multiple RESTful API endpoints.&lt;/p&gt;&#10;&lt;h4 id="ability-to-increase-account-wide-max-concurrency-without-contacting-aws-support"&gt;Ability to increase account-wide Max Concurrency without contacting AWS support&lt;/h4&gt;&#10;&lt;p&gt;Some will point out that you can always increase your total Lambda concurrency just by contacting AWS support.&lt;/p&gt;&#10;&lt;p&gt;In fact, we actually did this to bump our Full account concurrency from 1,000 to 2,000. AWS support was pretty amenable to this, but it took a couple of days for them to make the change and we needed to provide justification.&lt;/p&gt;&#10;&lt;p&gt;All the while, our user-facing Lambdas were throttled, and our users were upset. I’ve even heard of companies being able to get AWS to increase their max concurrency to hundreds of thousands of Lambdas.&lt;/p&gt;&#10;&lt;p&gt;That’s great, but why is special approval even needed? Isn’t the fact that a company is willing to pay for the increased usage enough?&lt;/p&gt;&#10;&lt;p&gt;To compare with ECS, this max-concurrency-per-account setting is similar to how you can easily customize the number of nodes in an ECS cluster, which effectively defines a max number of ECS tasks that can be run concurrently.&lt;/p&gt;&#10;&lt;p&gt;And of course, ECS has autoscaling capabilities that allow you to scale your cluster based on your own needs. None of this scaling requires any special approval from AWS.&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;If AWS were able to implement these controls, it would bring AWS Lambda’s concurrency controls to parity with those of ECS and EKS, and I’d once again advocate for using Lambda in many more areas of our system.&lt;/p&gt;&#10;&lt;h3 id="when-does-lambda-shine"&gt;When Does Lambda Shine?&lt;/h3&gt;&#10;&lt;p&gt;Even with its limited concurrency controls, Lambda shines in the following areas:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;For background tasks, e.g., ones that read off SQS. Sure, you can stand up something like &lt;a href="https://docs.celeryproject.org/en/stable/"&gt;celery&lt;/a&gt; or &lt;a href="https://github.com/OptimalBits/bull"&gt;bull&lt;/a&gt; to get something similar, but wiring up a Lambda to an SQS queue or one of the many other AWS triggers, is such a beautifully simple thing. Note: Lambda has a max timeout of 15 minutes, so it is only suitable for running relatively quick tasks.&lt;/li&gt;&#10;&lt;li&gt;When you have other Lambdas that don’t burst to consume all of the available concurrency, such as when you have a relatively small amount of Lambda activity.&lt;/li&gt;&#10;&lt;li&gt;When you wish to optimize autoscaling so that you only pay for what you use. Lambda automatically scales up and down at the single invocation layer so that you can save a ton of money during low traffic periods&lt;/li&gt;&#10;&lt;li&gt;As a place to run your code without the need to manage infrastructure&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;h3 id="conclusion"&gt;Conclusion&lt;/h3&gt;&#10;&lt;p&gt;The promise of serverless — where developers don’t need to manage infrastructure — is very compelling, and I don’t want to let this dream die.&lt;/p&gt;&#10;&lt;p&gt;AWS has fixed its error-rate problem with Lambdas, but Lambda is still lacking better concurrency controls.&lt;/p&gt;&#10;&lt;p&gt;Until these concurrency controls are enhanced, you’ll probably want to use ECS or EKS for your user-facing services, if your Lambda usage is significant and will lead to throttling.&lt;/p&gt;&#10;&lt;p&gt;I suspect that AWS will continue to evolve the Lambda technology to the point where it becomes a strong alternative to ECS and when it does, I’ll once again tout the virtues of using Lambda everywhere.&lt;/p&gt;&#10;&lt;p&gt;Thanks for reading.&lt;/p&gt;&#10;</description></item><item><title>What are MSON Template Queries?</title><link>https://redgeoff.com/posts/template-queries/</link><pubDate>Mon, 08 Feb 2021 00:00:00 +0000</pubDate><guid>https://redgeoff.com/posts/template-queries/</guid><description>&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/template-queries/template-queries.png" alt="Template Queries" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&#10;&lt;p&gt;Template Queries are dynamic templates constructed with MongoDB-style operators. They allow you to customize MSON components with less code and are very extensible.&lt;/p&gt;&#10;&lt;h3 id="ok-but-what-the-heck-is-mson"&gt;OK, but what the heck is MSON?&lt;/h3&gt;&#10;&lt;p&gt;&lt;a href="https://github.com/redgeoff/mson"&gt;MSON&lt;/a&gt; is a low-code way to create apps from JSON. The ultimate goal of &lt;a href="https://github.com/redgeoff/mson"&gt;MSON&lt;/a&gt; is to allow anyone to develop software visually. You can also use pieces of &lt;a href="https://github.com/redgeoff/mson"&gt;MSON&lt;/a&gt; to turbo charge your existing apps.&lt;/p&gt;&#10;&lt;p&gt;A simple form component used to collect a name and email address could look like:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyForm&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Form&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; { &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;name&amp;#39;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;TextField&amp;#39;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Name&amp;#39;&lt;/span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; { &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;email&amp;#39;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;EmailField&amp;#39;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Email&amp;#39;&lt;/span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can read more about MSON&amp;rsquo;s language principles &lt;a href="https://redgeoff.com/posts/everyone-can-make-software"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h3 id="what-are-mongodb-operators"&gt;What are MongoDB operators?&lt;/h3&gt;&#10;&lt;p&gt;MongoDB uses an extensive query language that is written in JSON. Because this language is so powerful, a number of projects, that work independently from MongoDB, have adopted support for the query language:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;In-memory libraries for JS, like &lt;a href="https://github.com/kofrasa/mingo"&gt;mingo&lt;/a&gt; and &lt;a href="https://github.com/crcn/sift.js/"&gt;sift&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;Operators in &lt;a href="https://sequelize.org/master/manual/model-querying-basics.html"&gt;sequelize ORM&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;Other databases, like &lt;a href="https://stackoverflow.com/questions/45976416/couchdb-mango-queries-couchdb-2-0-1"&gt;CouchDB&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;This query language also contains a construct for &lt;a href="https://docs.mongodb.com/manual/reference/operator/aggregation/"&gt;pipeline aggregations&lt;/a&gt;, which can be used to execute operations like &lt;code&gt;$add&lt;/code&gt;, &lt;code&gt;$multiply&lt;/code&gt;, &lt;code&gt;$filter&lt;/code&gt;, &lt;code&gt;$map&lt;/code&gt;, etc&amp;hellip; For example:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;{&#10; $add: [ 1, 2 ]&#10;}&#10;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="a-basic-mson-template-query"&gt;A basic MSON Template Query&lt;/h3&gt;&#10;&lt;p&gt;Now that you have that background, let&amp;rsquo;s take a look at an example where we extract the year from a user-provided date. Upon selecting a date with the date picker, you&amp;rsquo;ll see that the &lt;code&gt;Year&lt;/code&gt; field is populated:&lt;/p&gt;&#10;&#10;&lt;iframe src="https://codesandbox.io/embed/mson-template-query-6zb3b?fontsize=14&amp;hidenavigation=1&amp;theme=dark"&#10; style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;"&#10; &#10; allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"&#10; sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"&#10; &gt;&lt;/iframe&gt;&#10;&lt;p&gt;Let&amp;rsquo;s step through this example. First, we construct a form with &lt;code&gt;date&lt;/code&gt; and &lt;code&gt;year&lt;/code&gt; fields:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Form&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;date&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;DateField&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Date&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;year&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;IntegerField&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Year&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Second, we listen for changes to the &lt;code&gt;date&lt;/code&gt; value, extract the year and then set the value of the &lt;code&gt;year&lt;/code&gt; field:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Form&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; ...,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;listeners&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;fields.date.value&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;actions&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Set&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;fields.year.value&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$year&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$toDate&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;{{fields.date.value}}&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;$toDate&lt;/code&gt; operator is used to convert the date value, which is in milliseconds, to a date object. The &lt;code&gt;$year&lt;/code&gt; operator is used to extract the year portion from the date. The &lt;code&gt;Set&lt;/code&gt; action is native to MSON and is used to set the value of the &lt;code&gt;year&lt;/code&gt; field.&lt;/p&gt;&#10;&lt;p&gt;Pretty cool? Let&amp;rsquo;s go a bit deeper&amp;hellip;&lt;/p&gt;&#10;&lt;h3 id="how-can-we-make-this-reusable"&gt;How can we make this reusable?&lt;/h3&gt;&#10;&lt;p&gt;You can create custom actions, which can then be reused. Let&amp;rsquo;s assume that we want to create an &lt;code&gt;app.GetYear&lt;/code&gt; action that retrieves the year from a DateField value:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;compiler&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;registerComponent&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;app.GetYear&amp;#34;&lt;/span&gt;, {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Extend Set, a core MSON component, that sets a&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// property on another component&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Set&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Define the inputs to the custom action&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;schema&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Form&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;date&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;DateField&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;required&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Omitting the &amp;#34;name&amp;#34; here allows the value&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// to be passed to the next action via&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// &amp;#34;{{arguments}}&amp;#34;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;//&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// name: &amp;#34;&amp;#34;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Use the template query to extract the year&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// from the date&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$year&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$toDate&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;{{date}}&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We can then use this custom action in a chain of actions:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;form&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;compiler&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;newComponent&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Form&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; ...,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;listeners&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;fields.date.value&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;actions&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Extract the year and pass it to the next action&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;app.GetYear&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;date&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;{{fields.date.value}}&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Use the extracted year to set the year field&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Set&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;fields.year.value&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// {{arguments}} is the output of app.GetYear&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;{{arguments}}&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here is the completed example:&lt;/p&gt;&#10;&#10;&lt;iframe src="https://codesandbox.io/embed/mson-custom-action-cziwj?fontsize=14&amp;hidenavigation=1&amp;theme=dark"&#10; style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;"&#10; &#10; allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"&#10; sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"&#10; &gt;&lt;/iframe&gt;&#10;&lt;h3 id="another-example-a-conditional-survey-question"&gt;Another example: a conditional survey question&lt;/h3&gt;&#10;&lt;p&gt;Let&amp;rsquo;s assume that we have a survey question and we want the user to be able to enter a free response when they select &lt;code&gt;Other&lt;/code&gt;:&lt;/p&gt;&#10;&#10;&lt;iframe src="https://codesandbox.io/embed/mson-dynamic-survey-question-713s8?fontsize=14&amp;hidenavigation=1&amp;theme=dark"&#10; style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;"&#10; &#10; allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"&#10; sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"&#10; &gt;&lt;/iframe&gt;&#10;&lt;p&gt;We define the form and hide the &lt;code&gt;covidOther&lt;/code&gt; text field by default:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Form&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;covid&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;SelectField&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;My government&amp;#39;s response to COVID-19 was...&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fullWidth&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;options&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; { &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Good&amp;#34;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;good&amp;#34;&lt;/span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; { &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Bad&amp;#34;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;bad&amp;#34;&lt;/span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; { &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Other&amp;#34;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;other&amp;#34;&lt;/span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;covidOther&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;TextField&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Please explain&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;multiline&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;hidden&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If the user selects &lt;code&gt;Other&lt;/code&gt;, we toggle the &lt;code&gt;hidden&lt;/code&gt; boolean property of the &lt;code&gt;covidOther&lt;/code&gt; field. The &lt;code&gt;$ne&lt;/code&gt; operator is shorthand for &lt;em&gt;not equal&lt;/em&gt;.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;listeners&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;fields.covid.value&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;actions&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Set&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;fields.covidOther.hidden&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$ne&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&lt;span style="color:#e6db74"&gt;&amp;#34;{{fields.covid.value}}&amp;#34;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#34;other&amp;#34;&lt;/span&gt;]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="why-doesnt-mson-support-custom-js-in-templates"&gt;Why doesn&amp;rsquo;t MSON support custom JS in templates?&lt;/h3&gt;&#10;&lt;p&gt;By now, you can probably see the power of Template Queries, but you may be wondering why MSON doesn&amp;rsquo;t support custom JavaScript. To support custom JS in template parameters, it would require breaking two of MSON&amp;rsquo;s core design principles:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Compilation by instantiation&lt;/strong&gt; - Components are &lt;em&gt;compiled&lt;/em&gt; into JS objects by simply instantiating a JS object and setting the props dynamically. This method of &lt;em&gt;compilation&lt;/em&gt; allows us to avoid a transpilation step and makes it much easier to dynamically modify components.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;strong&gt;Serialization &amp;amp; deserialization without &lt;code&gt;eval()&lt;/code&gt;&lt;/strong&gt; - Components can be serialized using &lt;code&gt;JSON.stringfy()&lt;/code&gt; and stored practically anywhere. Moreover, components can be deserialized by dynamically compiling the result of &lt;code&gt;JSON.parse()&lt;/code&gt;. As a result, raw JS (in a string), including JS template literals, are not supported as deserializing such JS would require the use of &lt;code&gt;eval()&lt;/code&gt; or &lt;code&gt;new Function()&lt;/code&gt;, which would expose a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#never_use_eval!"&gt;XSS vulnerability&lt;/a&gt; and add significant performance issues.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;You can read more about this reasoning &lt;a href="https://github.com/redgeoff/mson/blob/master/DESIGN.md#why-doesnt-mson-support-custom-js-in-template-parameters"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;&lt;h3 id="wrap-it-up"&gt;Wrap It Up!&lt;/h3&gt;&#10;&lt;p&gt;Template Queries add a ton of capability to &lt;a href="https://github.com/redgeoff/mson"&gt;MSON&lt;/a&gt; by providing another powerful way of customizing your &lt;a href="https://github.com/redgeoff/mson"&gt;MSON&lt;/a&gt; components. And, they can be particularly useful in minimizing the code it takes to conditionally chain a series of actions.&lt;/p&gt;&#10;&lt;p&gt;If you liked this post, please give it a like or two. Happy building!&lt;/p&gt;&#10;</description></item><item><title>How to autogenerate forms in React and Material-UI with MSON</title><link>https://redgeoff.com/posts/mson-react-material-ui-form/</link><pubDate>Mon, 22 Oct 2018 00:00:00 +0000</pubDate><guid>https://redgeoff.com/posts/mson-react-material-ui-form/</guid><description>&#10; &lt;figure class="image"&gt;&#10; &lt;a href="https://redgeoff.github.io/mson-react"&gt;&#10; &lt;img src="https://redgeoff.com/posts/mson-react-material-ui-form/demo.png" alt="MSON Demo" /&gt;&#10; &lt;/a&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&lt;p&gt;Implementing great forms can be a real time-waster. With just a few lines of JSON, you can use &lt;a href="https://github.com/redgeoff/mson"&gt;MSON&lt;/a&gt; to generate forms that perform real-time validation and have a consistent layout. And, &lt;a href="https://github.com/redgeoff/mson"&gt;MSON&lt;/a&gt; comes with a bunch of cool stuff like date pickers, masked fields and field collections.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;: this post is geared towards those wishing to use Material-UI with React. Future versions of MSON will support other rendering layers.&lt;/p&gt;&#10;&lt;h3 id="what-the-heck-is-mson"&gt;What the heck is MSON?&lt;/h3&gt;&#10;&lt;p&gt;&lt;a href="https://github.com/redgeoff/mson"&gt;MSON&lt;/a&gt; is a declarative language that has the capabilities of an object-oriented language. The MSON compiler allows you to generate apps from JSON. The ultimate goal of MSON is to allow anyone to develop software visually. You can also use pieces of MSON to turbo charge your form development.&lt;/p&gt;&#10;&lt;h3 id="a-basic-form-generated-with-mson"&gt;A Basic Form Generated with MSON&lt;/h3&gt;&#10;&lt;p&gt;Simply declare your form in JSON. Then let the MSON compiler and UI-rendering layer autogenerate your UI:&lt;/p&gt;&#10;&#10;&lt;iframe src="https://codesandbox.io/embed/00ql22mx8w?fontsize=14&amp;hidenavigation=1&amp;theme=dark"&#10; style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;"&#10; &#10; allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"&#10; sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"&#10; &gt;&lt;/iframe&gt;&#10;&lt;p&gt;Did you try submitting the form without filling in any values? Did you notice how real-time validation is automatically baked in?&lt;/p&gt;&#10;&lt;p&gt;Now, let’s take a closer look at what is happening. The first block of code contains a JSON definition that describes the fields in the form:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;definition&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Form&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;heading&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Text&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;text&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;# Form using [MSON](https://github.com/redgeoff/mson)&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;fullName&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;PersonFullNameField&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;required&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;birthday&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;DateField&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Birthday&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;required&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;phone&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;PhoneField&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Phone&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;submit&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;ButtonField&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Submit&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;type&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;submit&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;icon&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Send&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This code adds the following fields to your form:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;The &lt;em&gt;Text&lt;/em&gt; component displays some &lt;a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet"&gt;markdown&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;The &lt;em&gt;PersonNameField&lt;/em&gt; is used to capture a person’s first and last names&lt;/li&gt;&#10;&lt;li&gt;The &lt;em&gt;DateField&lt;/em&gt; allows a user to choose a date using a slick date picker&lt;/li&gt;&#10;&lt;li&gt;The &lt;em&gt;PhoneField&lt;/em&gt; uses an input mask and country codes to guide the user when entering a phone number&lt;/li&gt;&#10;&lt;li&gt;The &lt;em&gt;SubmitField&lt;/em&gt; contains a &lt;em&gt;Send&lt;/em&gt; icon and allows the user to submit the form via a click or by pressing enter&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Now, let’s take a look at the code that renders the component and handles the submit event:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;ReactDOM&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;render&lt;/span&gt;(&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a6e22e"&gt;Component&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;definition&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;{&lt;span style="color:#a6e22e"&gt;definition&lt;/span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;onSubmit&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;{({ &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt; }) =&amp;gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;alert&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;JSON&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;stringify&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;getValues&lt;/span&gt;()));&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }}&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#f92672"&gt;/&amp;gt;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; document.&lt;span style="color:#a6e22e"&gt;getElementById&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;root&amp;#34;&lt;/span&gt;)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That’s it!? Yup! The &lt;a href="https://github.com/redgeoff/mson-react"&gt;mson-react&lt;/a&gt; layer &lt;strong&gt;automatically&lt;/strong&gt; knows how to render the form component. It uses &lt;a href="https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern"&gt;pub/sub&lt;/a&gt; and &lt;a href="https://reactjs.org/docs/react-api.html#reactpurecomponent"&gt;Pure Components&lt;/a&gt; to keep the rendering up-to-date.&lt;/p&gt;&#10;&lt;p&gt;When there is no validation error and the user clicks the submit button, an event with the name equal to the button’s name is emitted. In our case, this event is called &lt;em&gt;submit&lt;/em&gt;. Therefore, we can define a handler using the &lt;em&gt;onSubmit&lt;/em&gt; property. To keep things simple, we just alert the user of the entered values. Typically you want to do something like contact an API, redirect, etc…&lt;/p&gt;&#10;&lt;h3 id="basic-form-2"&gt;Basic Form 2&lt;/h3&gt;&#10;&lt;p&gt;Now, let’s go a bit deeper into the CRUD with a different example:&lt;/p&gt;&#10;&#10;&lt;iframe src="https://codesandbox.io/embed/n10q2y7ojj?fontsize=14&amp;hidenavigation=1&amp;theme=dark"&#10; style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;"&#10; &#10; allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"&#10; sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"&#10; &gt;&lt;/iframe&gt;&#10;&lt;p&gt;The first thing you may notice are the &lt;a href="https://github.com/redgeoff/mson#validators"&gt;validators&lt;/a&gt; in the definition:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;validators&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;where&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;fields.email.value&amp;#34;&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;nope@example.com&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;field&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;email&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;must not be {{fields.email.value}}&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Each field has a default set of &lt;a href="https://github.com/redgeoff/mson#validators"&gt;validators&lt;/a&gt;, e.g. the &lt;em&gt;EmailField&lt;/em&gt; ensures that email addresses are in a valid format. You can also extend these validators for a particular field or even for an entire form. For example, you can prevent the user from entering &lt;em&gt;nope@example.com&lt;/em&gt;.&lt;/p&gt;&#10;&lt;p&gt;Next, let’s take a look at the code that loads the initial values when the component is mounted:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;onMount&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;{({ &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt; }) =&amp;gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Load any initial data, e.g. from an API&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;setValues&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;id&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;abc123&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;firstName&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Bob&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;lastName&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;Marley&amp;#34;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;email&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;bob@example.com&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This code could just as easily be replaced by code that retrieves the values from some API asynchronously.&lt;/p&gt;&#10;&lt;p&gt;Finally, we use a more sophisticated event listener to handle the submit action. In a real-world app, you’d probably want to send a request to an API to save the data. You would receive a response from this API. If you receive an error, e.g. the email address is already in use, you can present this error to the user:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;onSubmit&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;{({ &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt; }) =&amp;gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// TODO: Contact some API with the data&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;submitting&amp;#34;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;getValues&lt;/span&gt;());&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Simulate response from API saying that email address is already in use and report this&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// error to the user&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (&lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#34;fields.email.value&amp;#34;&lt;/span&gt;) &lt;span style="color:#f92672"&gt;===&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;taken@example.com&amp;#34;&lt;/span&gt;) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;set&lt;/span&gt;({ &lt;span style="color:#e6db74"&gt;&amp;#34;fields.email.err&amp;#34;&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#34;already in use&amp;#34;&lt;/span&gt; });&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; } &lt;span style="color:#66d9ef"&gt;else&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Everything was successful so redirect, show confirmation, etc...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="live-demo"&gt;&lt;a href="https://redgeoff.com/mson-react/"&gt;Live Demo&lt;/a&gt;&lt;/h3&gt;&#10;&lt;p&gt;This post only touches on a small piece of what you can do using &lt;a href="https://github.com/redgeoff/mson"&gt;MSON&lt;/a&gt;. Because &lt;a href="https://github.com/redgeoff/mson"&gt;MSON&lt;/a&gt; is a full-featured language, you can declare all types of cool components. If you are interested in seeing more live examples, check out the &lt;a href="https://redgeoff.com/mson-react/"&gt;live demo&lt;/a&gt;.&lt;/p&gt;&#10;&#10; &lt;figure class="image"&gt;&#10; &lt;a href="https://redgeoff.github.io/mson-react"&gt;&#10; &lt;img src="https://redgeoff.com/posts/mson-react-material-ui-form/demo.png" alt="MSON Demo" /&gt;&#10; &lt;/a&gt;&#10; &#10; &lt;/figure&gt;&#10;&#10;&lt;h3 id="wrap-it-up"&gt;Wrap It Up!&lt;/h3&gt;&#10;&lt;p&gt;If you are using React and Material-UI, you can speed up your app development by autogenerating your forms from JSON. This can be particularly useful if you need to bootstrap an app quickly and don’t want to have to worry about building a UI from scratch.&#10;If you liked this post, please give it a clap or two. Happy &lt;a href="https://github.com/redgeoff/mson"&gt;autogenerating!&lt;/a&gt;&lt;/p&gt;&#10;</description></item><item><title>Creating a New Programming Language That Will Allow Anyone to Make Software</title><link>https://redgeoff.com/posts/everyone-can-make-software/</link><pubDate>Tue, 31 Jul 2018 00:00:00 +0000</pubDate><guid>https://redgeoff.com/posts/everyone-can-make-software/</guid><description>&lt;p&gt;I’ve spent the last 7 months working on a new programming language called MSON. Here is a nerdy post about why I did it, how it works and where I want to take it.&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/everyone-can-make-software/mson-logo.png" alt="Everyone Can Make Software" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="there-are-already-a-bazillion-programming-languages-why-do-we-need-another"&gt;There are already a bazillion programming languages, why do we need another?&lt;/h3&gt;&#10;&lt;p&gt;Software continues to eat the world and software development tools are evolving at lightning speeds, but it stills takes an in-depth knowledge of a programming language to develop software. Sure, there are millions of open-source projects that can be mashed together and robust cloud platforms that make the process easier, but it takes considerable skill to get all of these technologies working together. When I envision the future, I imagine the average person being able to make software, just like how the average person can create a spreadsheet today.&lt;/p&gt;&#10;&lt;p&gt;Creating a spreadsheet has become commonplace and this is largely a result of the intuitive user interface (UI) in MS Excel. Developing software; however, still remains something that is only done by a relative few and using complex programming languages. One could argue that it is somewhat trivial to create a UI that generates code, but it is almost infeasible to reverse the process and digest coded logic, as most programming languages have an almost infinite number of ways of representing logic. In other words, we don’t usually know how a piece of software will execute until we actually execute it. Creating a truly intuitive way of developing software requires a new paradigm, a new language that is easily read, modified and written by a UI.&lt;/p&gt;&#10;&lt;p&gt;It was back in 2003, when PHP was &lt;em&gt;the hotness&lt;/em&gt;, that I started constructing a framework called FORMfields. At the time, FORMfields was pretty cool as you could create forms with PHP objects and then FORMfields would automatically generate the corresponding HTML, CSS and JS. Soon after creating FORMfields, I co-founded a company called GoExpo that produces event management software that is powered by FORMfields. GoExpo grew to process millions of dollars in booth sales annually for some of the largest trade shows in the world and this was a testament to the effectiveness of FORMfields.&lt;/p&gt;&#10;&lt;p&gt;Over the years, FORMfields continued to be essential in allowing our team to create a consistent UI that could be modified quickly (remember, these were the days before Twitter Bootstrap and more modern UI frameworks). However, as browsers were becoming more capable, more logic was moving into the front end; and FORMfields, which is written in PHP, only runs in the back end.&lt;/p&gt;&#10;&lt;p&gt;Fast forward to 2018, where JavaScript is the workhorse of the web and the difference between the front end and back end can feel like night and day. GoExpo has been acquired and I now have time to revisit the notion of FORMfields with a more modern stack. I want the next generation of FORMfields to run in both the browser and the back end, be open source and become the foundation of something that can be used by the masses.&lt;/p&gt;&#10;&lt;p&gt;This line of thinking has given birth to &lt;a href="https://github.com/redgeoff/mson"&gt;MSON&lt;/a&gt;, an extensible declarative language that can be easily manipulated by a UI. MSON is comprised of just a few building blocks, yet it is as powerful as its non-declarative counterparts.&lt;/p&gt;&#10;&lt;p&gt;The purpose of this initial post is to introduce the core principles of the &lt;a href="https://github.com/redgeoff/mson"&gt;MSON&lt;/a&gt; programming language and elaborate on its unique design. I plan on writing a follow-up post later that will explain MSON from a layman’s perspective. This post; however, is rather technical as it is about the developer-layer stuff in MSON and not about the designed-for-all UI layer I’ll be writing on top. I will be elaborating on the following topics:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Declarative Syntax&lt;/li&gt;&#10;&lt;li&gt;Components, Validators, Events &amp;amp; Listeners&lt;/li&gt;&#10;&lt;li&gt;Access Control&lt;/li&gt;&#10;&lt;li&gt;Inheritance, Template Parameters &amp;amp; Composition&lt;/li&gt;&#10;&lt;li&gt;Aggregate Components&lt;/li&gt;&#10;&lt;li&gt;Schemas and Self Documentation&lt;/li&gt;&#10;&lt;li&gt;User-Defined JavaScript Components&lt;/li&gt;&#10;&lt;li&gt;Using MSON in Any JavaScript Code&lt;/li&gt;&#10;&lt;li&gt;Reusing MSON Code Throughout the Full Stack&lt;/li&gt;&#10;&lt;li&gt;In/Out Properties&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;I will then finish with a statement about how I plan to monetize MSON and make it a self-sustaining project. I’d love to hear what you think.&lt;/p&gt;&#10;&lt;hr&gt;&#10;&lt;h3 id="language-principles"&gt;Language Principles&lt;/h3&gt;&#10;&lt;h4 id="declarative-syntax"&gt;Declarative Syntax&lt;/h4&gt;&#10;&lt;p&gt;MSON is short for Model Script Object Notation, which is intentionally similar to JSON (JavaScript Object Notation). In fact, MSON is a subset of JSON, so if you know JSON then you know the syntax of MSON!&lt;/p&gt;&#10;&lt;p&gt;Declarative languages are much easier for software to read and write as they define what the software must do without stating exactly how to do it. And JSON is a great foundation on which to build. It contains just a few main constructs, is ubiquitous and supported by a vast ecosystem.&lt;/p&gt;&#10;&lt;h4 id="components"&gt;Components&lt;/h4&gt;&#10;&lt;p&gt;The smallest building block in MSON is called a component. Components maintain state and can also control presentation and are very similar to the components now commonplace in most web frameworks. Components can inherit, contain or wrap other components. The rendering layer supports plugins for different environments and the default plugin supports React and Material-UI. Use of the rendering layer is optional, so components can be used on both the front end and back end.&lt;/p&gt;&#10;&lt;p&gt;A simple form component used to collect a name and email address could look like:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyForm&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Form&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;name&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;TextField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Name&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;required&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;email&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;EmailField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Email&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;submit&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;ButtonField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Submit&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;icon&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;CheckCircle&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And when it renders it looks like:&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/everyone-can-make-software/form.png" alt="Rendered Form" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;And, real-time validation is automatically baked in:&lt;/p&gt;&#10;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/everyone-can-make-software/validation.png" alt="Real-time Form Validation" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&#10;&lt;p&gt;The majority of the remaining examples in this post will focus on form components, as they are simple to visualize, but MSON can support any type of component, e.g. menus, snackbars, redirects, etc… In addition, you can use JavaScript to create user-defined components that can pretty much do anything else you can imagine.&lt;/p&gt;&#10;&lt;h3 id="validators"&gt;Validators&lt;/h3&gt;&#10;&lt;p&gt;Each field has a default set of validators, e.g. the EmailField ensures that email addresses are in a valid format. You can also extend these validators for a particular field or even for an entire form.&lt;/p&gt;&#10;&lt;p&gt;For example, you can prevent the user from entering &lt;em&gt;nope@example.com&lt;/em&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyForm&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Form&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; ...,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;validators&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;where&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;fields.email.value&amp;#39;&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;nope@example.com&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;field&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;email&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;must not be {{fields.email.value}}&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Template parameters like &lt;code&gt;{{fields.email.value}}&lt;/code&gt; can be used to inject the values of fields. And, you can use any &lt;a href="https://docs.mongodb.com/manual/reference/operator/query/"&gt;MongoDB-style query&lt;/a&gt; in the &lt;code&gt;where&lt;/code&gt;. For example, if you had &lt;code&gt;password&lt;/code&gt; and &lt;code&gt;retypePassword&lt;/code&gt; fields, you could ensure that they are equivalent with:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;where&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;retypePassword.fields.value&amp;#39;&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$ne&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;{{fields.password.value}}&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;error&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id="events--listeners"&gt;Events &amp;amp; Listeners&lt;/h4&gt;&#10;&lt;p&gt;Changes to properties in a component generate events and you can create listeners that respond to these events with actions. There are basic actions that set, emit, email, contact APIs, etc… and custom actions can also be built using JavaScript.&lt;/p&gt;&#10;&lt;p&gt;The following example sets the value of the &lt;code&gt;email&lt;/code&gt; field based on the value supplied in the &lt;code&gt;name&lt;/code&gt; field when the user clicks the &lt;code&gt;submit&lt;/code&gt; button:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyForm&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Form&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; ...,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;validators&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; ...,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;listeners&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;submit&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;actions&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Set&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;fields.email.value&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;{{fields.name.value}}@example.com&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We can also make this action conditional, e.g. only set the &lt;code&gt;email&lt;/code&gt; if it is blank:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;listeners&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;submit&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;actions&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Set&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;fields.email&amp;#39;&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$or&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;null&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;fields.email.value&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;{{fields.name.value}}@example.com&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And sometimes we want to nest actions so that a condition is met before all actions are executed:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;listeners&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;submit&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;actions&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Action&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;fields.email&amp;#39;&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;$or&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;null&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;actions&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Set&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;fields.email.value&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;{{fields.name.value}}@example.com&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Set&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;fields.name.value&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;{{fields.name.value}} Builder&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id="access-control"&gt;Access Control&lt;/h4&gt;&#10;&lt;p&gt;Unlike most programming languages, access control is a first-class citizen in MSON, so its easy to use without a lot of work. Access can be restricted at the form or field layers for the create, read, update and archive operations. (MSON is designed to encourage data archiving instead of deletion so that data can be restored when it is accidentally archived. You can, of course, permanently delete data when needed).&lt;/p&gt;&#10;&lt;p&gt;Each user can have any number of user-defined roles and access is then limited to users with specified roles. There is also a system role of &lt;code&gt;owner&lt;/code&gt; that is defined for the owner of the data. Field-layer access is checked first and if it is missing it will cascade to checking the form-layer access. When the access is undefined at the form layer (and not defined at the field-layer), all users have access.&lt;/p&gt;&#10;&lt;p&gt;Here is an example configuration:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyForm&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Form&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; ...,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;validators&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; ...,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;listeners&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; ...,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;access&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;form&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;create&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&lt;span style="color:#e6db74"&gt;&amp;#39;admin&amp;#39;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#39;manager&amp;#39;&lt;/span&gt;],&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;read&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&lt;span style="color:#e6db74"&gt;&amp;#39;admin&amp;#39;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#39;employee&amp;#39;&lt;/span&gt;],&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;update&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&lt;span style="color:#e6db74"&gt;&amp;#39;admin&amp;#39;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#39;owner&amp;#39;&lt;/span&gt;, &lt;span style="color:#e6db74"&gt;&amp;#39;manager&amp;#39;&lt;/span&gt;],&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;archive&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&lt;span style="color:#e6db74"&gt;&amp;#39;admin&amp;#39;&lt;/span&gt;]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;create&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&lt;span style="color:#e6db74"&gt;&amp;#39;admin&amp;#39;&lt;/span&gt;],&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;update&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&lt;span style="color:#e6db74"&gt;&amp;#39;owner&amp;#39;&lt;/span&gt;]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Among other things, only users with the &lt;code&gt;admin&lt;/code&gt; or &lt;code&gt;manager&lt;/code&gt; roles can create records. In addition, only owners of a record can modify the &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;&#10;&lt;h4 id="inheritance"&gt;Inheritance&lt;/h4&gt;&#10;&lt;p&gt;Inheritance is used to add additional functionality to a component. For example, we can extend &lt;code&gt;MyForm&lt;/code&gt; and add a phone number:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyFormExtended&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyForm&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;phone&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;PhoneField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Phone Number&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;before&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;submit&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We can define new validators, listeners, access, etc… at this new layer. For example, we can pre-populate some data, lay out all fields on the same line and disable the email field by creating a listener for the &lt;code&gt;create&lt;/code&gt; event:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyFormExtended&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyForm&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; ...,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;listeners&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;create&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;actions&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Set&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;value&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Bob Builder&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;email&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;bob@example.com&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;phone&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;(206)-123-4567&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Set&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;fields.name.block&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;false&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Set&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;fields.email.block&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;false&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Set&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;fields.email.disabled&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;which would render as:&lt;/p&gt;&#10;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/everyone-can-make-software/bob-builder.png" alt="Bob Builder Form" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&#10;&lt;h4 id="template-parameters"&gt;Template Parameters&lt;/h4&gt;&#10;&lt;p&gt;Template parameters are helpful when creating reusable components as they allow you to make pieces of your component dynamic. For example, let’s say that we want our first field and the label of our second field to be dynamic:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyTemplatedForm&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Form&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;{{firstField}}&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;secondField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;{{secondFieldLabel}}&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;EmailField&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;we can then extend &lt;code&gt;MyTemplatedForm&lt;/code&gt; and fill in the pieces:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyFilledTemplatedForm&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyTemplatedForm&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;firstField&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;firstName&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;TextField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;First Name&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;secondFieldLabel&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Email Address&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id="composition"&gt;Composition&lt;/h4&gt;&#10;&lt;p&gt;The &lt;code&gt;componentToWrap&lt;/code&gt; property allows you to wrap components, enabling your reusable components to transform any component. For example, we can use composition to create a reusable component that adds a phone number:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;AddPhone&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Form&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;componentToWrap&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;{{baseForm}}&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;phone&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;PhoneField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Phone Number&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;before&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;submit&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and then pass in a component to be wrapped:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyFormWithPhone&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;AddPhone&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;baseForm&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyForm&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can even extend wrapped components, paving the way for a rich ecosystem of aggregate components comprised of other components.&lt;/p&gt;&#10;&lt;h4 id="aggregate-components"&gt;Aggregate Components&lt;/h4&gt;&#10;&lt;p&gt;MSON ships with a number of aggregate components such as the &lt;code&gt;RecordEditor&lt;/code&gt; and &lt;code&gt;RecordList&lt;/code&gt;, which make it easy to turn your form components into editable UIs with just a few lines of code.&lt;/p&gt;&#10;&lt;p&gt;Let’s define a user component:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyAccount&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Form&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;firstName&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;TextField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;First Name&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }, &#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;lastName&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;TextField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Last Name&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;email&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;EmailField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Email&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;we can then use a &lt;code&gt;RecordEditor&lt;/code&gt; to allow the user to edit her/his account:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyAccountEditor&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;RecordEditor&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;baseForm&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyAccount&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Account&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;rendering as:&lt;/p&gt;&#10;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/everyone-can-make-software/builder-display.png" alt="RecordEditor Detail" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&#10;&lt;p&gt;and if you were to click the &lt;em&gt;Edit&lt;/em&gt; button you’d see:&lt;/p&gt;&#10;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/everyone-can-make-software/record-editor-edit.png" alt="RecordEditor Edit" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&#10;&lt;p&gt;You can also use the &lt;code&gt;RecordList&lt;/code&gt; to display an editable list of these accounts:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyAccountsList&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;RecordList&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Accounts&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;baseFormFactory&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Factory&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;product&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; { &#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyAccount&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;rendering as:&lt;/p&gt;&#10;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/everyone-can-make-software/record-list-edit.png" alt="RecordList Edit" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&#10;&lt;h4 id="schemas-and-self-documentation"&gt;Schemas and Self Documentation&lt;/h4&gt;&#10;&lt;p&gt;Schemas must be defined for all components, which means that MSON is strongly typed. For example, a schema that defines boolean and date properties may look like:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyComponent&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Component&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;schema&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Form&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;hidden&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;BooleanField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;help&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Whether or not the component is hidden&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;updatedAt&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;DateTimeField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;required&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;, &#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;help&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;When the component was updated&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Schemas can also contain documentation via &lt;code&gt;help&lt;/code&gt; properties, which means that components are self-documenting! In addition, schemas are inherited and can be overwritten to allow for more or even less constraints.&lt;/p&gt;&#10;&lt;h4 id="user-defined-javascript-components"&gt;User-Defined JavaScript Components&lt;/h4&gt;&#10;&lt;p&gt;The MSON compiler is written in JavaScript and can run in both the browser and in Node.js. As such, you can use any custom JS, including external JS libraries, to create your own components.&lt;/p&gt;&#10;&lt;p&gt;For example, here is a component that uses &lt;a href="https://momentjs.com/"&gt;Moment.js&lt;/a&gt; to set a &lt;code&gt;currentDay&lt;/code&gt; property to the current day:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;compiler&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;mson/lib/compiler&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Component&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;mson/lib/component&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Form&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;mson/lib/form&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; { &lt;span style="color:#a6e22e"&gt;TextField&lt;/span&gt; } &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;mson/lib/fields&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;moment&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;moment&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;class&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;MyComponent&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;extends&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Component&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// className is needed as JS minification strips the constructor name&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;className&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyComponent&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;create&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;props&lt;/span&gt;) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;super&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;create&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;props&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;this&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;set&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Define a currentDay property&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;schema&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Form&lt;/span&gt;(&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;TextField&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;currentDay&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ),&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Default currentDay&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;currentDay&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;moment&lt;/span&gt;().&lt;span style="color:#a6e22e"&gt;format&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;dddd&amp;#39;&lt;/span&gt;)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;compiler&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;registerComponent&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;MyComponent&amp;#39;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;MyComponent&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And then &lt;code&gt;MyComponent&lt;/code&gt; can be used in any MSON code.&lt;/p&gt;&#10;&lt;p&gt;You can also do things like define custom asynchronous actions, e.g. one that POSTs form data:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;compiler&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;mson/lib/compiler&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Action&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;mson/lib/actions/action&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Form&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;mson/lib/form&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; { &lt;span style="color:#a6e22e"&gt;TextField&lt;/span&gt; } &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;mson/lib/fields&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;class&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;MyAction&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;extends&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Action&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;className&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyAction&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;create&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;props&lt;/span&gt;) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;super&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;create&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;props&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;this&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;set&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;schema&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Form&lt;/span&gt;(&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;TextField&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;foo&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; )&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; });&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;async&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;act&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;props&lt;/span&gt;) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;form&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;FormData&lt;/span&gt;();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;form&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;append&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;foo&amp;#39;&lt;/span&gt;, &lt;span style="color:#66d9ef"&gt;this&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;foo&amp;#39;&lt;/span&gt;));&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;account&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;props&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;form&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;append&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;firstName&amp;#39;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;account&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;firstName&amp;#39;&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;form&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;append&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;lastName&amp;#39;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;account&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;lastName&amp;#39;&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;form&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;append&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;email&amp;#39;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;account&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;get&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;email&amp;#39;&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;fetch&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;https://api.example.com&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;method&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;POST&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;body&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;form&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;compiler&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;registerComponent&lt;/span&gt;(&lt;span style="color:#e6db74"&gt;&amp;#39;MyAction&amp;#39;&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;MyAction&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And then you can use this in your MSON code:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyAccountExtended&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyAccount&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;listeners&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;submit&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;actions&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyAction&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;foo&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;bar&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id="using-mson-in-any-javascript-code"&gt;Using MSON in Any JavaScript Code&lt;/h4&gt;&#10;&lt;p&gt;There is always parity between compiled and uncompiled components so that the same feature set is supported by both compiled and uncompiled code. For example, our same &lt;code&gt;MyAccount&lt;/code&gt; component can also be defined as:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Form&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;mson/lib/form&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; { &lt;span style="color:#a6e22e"&gt;TextField&lt;/span&gt;, &lt;span style="color:#a6e22e"&gt;Email&lt;/span&gt; } &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;mson/lib/fields&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;class&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;MyAccount&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;extends&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Form&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;className&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyAccount&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;create&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;props&lt;/span&gt;) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;super&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;create&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;props&lt;/span&gt;);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;this&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;set&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;TextField&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;firstName&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;First Name&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }),&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;TextField&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;lastName&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Last Name&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }),&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;EmailField&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;email&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Email&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; })&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In fact, converting MSON code to this type of code is basically what the compiler does. Although, the compiler doesn’t actually transpile MSON to JS, it merely instantiates JS code based on the MSON definitions.&lt;/p&gt;&#10;&lt;p&gt;Since all MSON code can be compiled to JS code, you can use MSON components in any JS code. For example, you can set some fields and validate the data:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;import&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;compiler&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;mson/lib/compiler&amp;#39;&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Compile the MyAccount component&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;MyAccount&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;compiler&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;compile&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyAccount&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Instantiate the JS class with a default value&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;const&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;myAccount&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;MyAccount&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Default values&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;firstName&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Bob&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Set the remaining data&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;myAccount&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;set&lt;/span&gt;({&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;lastName&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Builder&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;email&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;invalid-email@&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;});&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;// Make sure the values are valid&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;myAccount&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;validate&lt;/span&gt;();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; (&lt;span style="color:#a6e22e"&gt;myAccount&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;hasErr&lt;/span&gt;()) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;console&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;log&lt;/span&gt;(&lt;span style="color:#a6e22e"&gt;myAccount&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;getErrs&lt;/span&gt;());&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In other words, you can use MSON in your existing JS code to save time writing complex code. By declaring components in MSON, you’ll remove a lot of boilerplate code and reduce the possibility of bugs. You’ll also have code that has a standard structure and is framework agnostic. And this code doesn’t add any unneeded frameworks or back-end dependencies to your codebase.&lt;/p&gt;&#10;&lt;h4 id="reusing-mson-code-throughout-the-full-stack"&gt;Reusing MSON Code Throughout the Full Stack&lt;/h4&gt;&#10;&lt;p&gt;MSON components can be shared by both the front end and back end, allowing for key logic to be written once and then reused. For example, the same form validation rules can be enforced in the browser and by your back-end API.&lt;/p&gt;&#10;&lt;p&gt;Moreover, actions can be limited to the &lt;code&gt;backEnd&lt;/code&gt; or &lt;code&gt;frontEnd&lt;/code&gt;, so that the same component can adjust according to the host environment. For example, you may want a contact form to send an email to the user when it is used on the back end, but only display a &lt;a href="https://material-ui.com/components/snackbars/"&gt;snackbar&lt;/a&gt; on the front end:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Form&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;email&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;EmailField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Email&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;message&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;TextField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Message&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Submit&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;ButtonField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Submit&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ],&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;listeners&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;submit&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;actions&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Send an email on the back end&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Email&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;layer&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;backEnd&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;from&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;{{fields.email.value}}&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;to&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;noreply@example.com&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;subject&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;My message&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;body&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;{{fields.message.value}}&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Detach so that user doesn&amp;#39;t have to wait for email&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// to send&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;detach&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Display a message to the user on the front end&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Snackbar&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;layer&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;frontEnd&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;message&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Thanks for the message&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id="inout-properties"&gt;In/Out Properties&lt;/h4&gt;&#10;&lt;p&gt;Sometimes you want the presence of data, but don’t want it to be written or read from the back end. For example, your default user component may not allow for the password to be read or edited:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyUser&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Form&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;fields&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;name&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;TextField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Name&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;email&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;EmailField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Email&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;password&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;PasswordField&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;label&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Password&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;hidden&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;in&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;false&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;out&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;false&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;However, your &lt;code&gt;EditPasswordForm&lt;/code&gt; may need to allow such access:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:2;-o-tab-size:2;tab-size:2;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-js" data-lang="js"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;EditPasswordForm&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;MyUser&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;listeners&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;event&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;create&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;actions&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; [&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Hide all fields&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Set&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;hidden&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Show password field&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Set&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;fields.password.hidden&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;false&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;// Allow user to write password to the back end&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;component&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;Set&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;name&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#e6db74"&gt;&amp;#39;fields.password.out&amp;#39;&lt;/span&gt;,&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;true&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ]&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;&#10;&lt;h3 id="next-steps"&gt;Next Steps&lt;/h3&gt;&#10;&lt;p&gt;Before publishing this post, I had showed MSON to only a handful of people. This may seem odd for an open-source project, but it was intentional, as I wanted to flesh out the language before doing a wider release. I didn’t want people to start using something where the building blocks would be changing drastically. Therefore, I started with a basic prototype of the compiler and then started building a sample app written in MSON. This allowed me to use an iterative approach to see what works and what doesn’t work. And after several rewrites of the compiler, thank goodness for unit tests and a patient wife, I’ve been able to implement a full-featured app. I’m sure the language will continue to evolve, but I now feel confident that MSON is a solid foundation on which to build.&lt;/p&gt;&#10;&lt;p&gt;The MSON compiler is completely written in JS and runs in both the browser and on Node.js. The repo is hosted on github at &lt;a href="https://github.com/redgeoff/mson"&gt;https://github.com/redgeoff/mson&lt;/a&gt; — if you like the idea please give it a star.&lt;/p&gt;&#10;&lt;p&gt;I also plan on monetizing this idea by developing out mson.co, which will host a UI that anyone can use to make software. The UI will read, modify and write MSON behind the scenes and a back end will automatically be created based on your component definitions. And for developers, you’ll have the option of using as much of the mson.co stack as you wish, e.g. you could bootstrap your app using the MSON UI Builder and then replace/customize the front end with your favorite technologies (the mson.co back end is exposed via a GraphQL API). Or, you can use MSON on just the front end and create your own custom back-end API.&lt;/p&gt;&#10;&lt;p&gt;I hope you enjoyed reading this post and I would love to hear your thoughts.&lt;/p&gt;&#10;</description></item><item><title>Running a Free VPN Server on AWS</title><link>https://redgeoff.com/posts/running-a-free-vpn-server-on-aws/</link><pubDate>Mon, 24 Jul 2017 07:39:02 -0800</pubDate><guid>https://redgeoff.com/posts/running-a-free-vpn-server-on-aws/</guid><description>&lt;blockquote&gt;&#10;&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; This tutorial is from 2017 and uses Ubuntu 16. For current steps with Ubuntu 22 and the AWS free tier, see &lt;a href="https://redgeoff.com/posts/free-vpn-aws-2022/"&gt;Run a Free VPN Server on AWS (2022 Edition)&lt;/a&gt;.&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/network-with-vpn.png" alt="Network with VPN" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;This tutorial will take you through the steps of setting up an EC2 instance that will run the OpenVPN Server. It will then cover how to grant and revoke access through the VPN Server.&lt;/p&gt;&#10;&lt;p&gt;AWS has an awesome firewall built into its core services which can easily be used to make sure that only certain ports are open to the outside world. One extra step that we can take is to run a VPN Server that serves as the gateway to our protected EC2 instances. We can then shutdown direct SSH access to our EC2 instances and also have the freedom to block access to our entire network just by revoking access via our VPN Server. The later is very useful if you need to revoke access for a former employee.&lt;/p&gt;&#10;&lt;h3 id="step-1-create-the-vpn-security-group"&gt;Step 1— Create the VPN Security Group&lt;/h3&gt;&#10;&lt;p&gt;Overview: security groups allow your servers to communicate with each other in a private cloud while exposing specific ports to the world. We are going to create a security group to allow VPN access to our VPN Server. We will assume that all your other EC2 instances are members of the default security group and that the default security group does not allow access from the outside world.&lt;/p&gt;&#10;&lt;p&gt;Log in at &lt;a href="https://aws.amazon.com"&gt;https://aws.amazon.com&lt;/a&gt;, type EC2 in the search box and click on the target to go to the EC2 Dashboard.&lt;/p&gt;&#10;&lt;p&gt;From the EC2 dashboard, click &lt;em&gt;Security Groups&lt;/em&gt;:&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/security-group.png" alt="Security Groups" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;Click Create &lt;em&gt;Security Group&lt;/em&gt;:&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/create-security-group.png" alt="Create Security Group" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;Enter a name and description of vpn and specify inbound rules on ports 22, 443, 943 and 1194. Note: the protocol for port 1194 is UDP.&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/ports.png" alt="Ports" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;Note: if the IP addresses that your team uses are static then you can add yet another layer of security by specifying that IP address range in the Source of your rules. However, you’ll want to leave the Source blank if you want your team to be able to connect from different IPs as they may be working from a hotel, home, cafe, etc…&lt;/p&gt;&#10;&lt;h3 id="step-2--create-the-ec2-instance"&gt;Step 2 — Create the EC2 Instance&lt;/h3&gt;&#10;&lt;p&gt;Return to the EC2 Dashboard and then click &lt;em&gt;Launch Instance&lt;/em&gt;:&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/create-instance.png" alt="Create EC2 Instance" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;Select Ubuntu (you can of course select almost any other OS that runs OpenVPN, but this tutorial is tailored for Ubuntu)&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/ubuntu.png" alt="Ubuntu 16 AMI" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;Select t2.nano and click &lt;em&gt;Review and Launch&lt;/em&gt;&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/t2-nano.png" alt="t2.nano" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;On the next screen, click &lt;em&gt;Edit security groups&lt;/em&gt;&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/security-groups.png" alt="Edit security groups" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;Select the &lt;em&gt;vpn&lt;/em&gt; and &lt;em&gt;default security groups&lt;/em&gt; and click &lt;em&gt;Review and Launch&lt;/em&gt;&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/review-and-launch.png" alt="Review and Launch" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;Click &lt;em&gt;Launch&lt;/em&gt;, choose your key pair and then click &lt;em&gt;Launch Instances&lt;/em&gt;&lt;/p&gt;&#10;&lt;h3 id="step-3--disable-sourcedestination-check"&gt;Step 3 — Disable Source/Destination Check&lt;/h3&gt;&#10;&lt;p&gt;From the list of instances, select the VPN instance and then Networking-&amp;gt;Change Source/Dest. Check from the drop down menu. Then click Yes, Disable. This is needed as otherwise, your VPN server will not be able to connect to your other EC2 instances.&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/disable-check.png" alt="Disable Source/Destination Check" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="step-4-create-an-elastic-ip-address"&gt;Step 4— Create an Elastic IP Address&lt;/h3&gt;&#10;&lt;p&gt;Overview: when an EC2 instance is stopped and restarted, the Public IP address changes. We want the IP address of our VPN Server to remain static so we’ll use an Elastic IP Address.&lt;/p&gt;&#10;&lt;p&gt;From the E2c Dashboard, select &lt;em&gt;Elastic IPs&lt;/em&gt;&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/elastic-ips.png" alt="Elastic IPs" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;Click &lt;em&gt;Allocate new address&lt;/em&gt;&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/allocate-new-address.png" alt="Allocate New Address" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;Click &lt;em&gt;Allocate&lt;/em&gt; and then &lt;em&gt;Close&lt;/em&gt;.&lt;/p&gt;&#10;&lt;p&gt;Make a note of your Elastic IP address as this will be the Public IP Address of your VPN Server.&lt;/p&gt;&#10;&lt;p&gt;Then select the Elastic IP and click &lt;em&gt;Associate address&lt;/em&gt; from the drop down menu.&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/associate-address.png" alt="Associate Address" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;Select the EC2 instance you just created and click &lt;em&gt;Associate&lt;/em&gt;.&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/associate-private-ip.png" alt="Associate Private IP" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;h3 id="step-5-install-and-configure-the-openvpn-server"&gt;Step 5— Install and Configure the OpenVPN Server&lt;/h3&gt;&#10;&lt;p&gt;SSH into your VPN server:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ ssh ubuntu@PUBLIC-IP-OF-VPN-SERVER&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Download our helper scripts and set up a default config:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ git clone https://github.com/redgeoff/openvpn-server-vagrant&#10;$ cd openvpn-server-vagrant&#10;$ cp config-default.sh config.sh&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Edit config.sh and enter in your configuration. Note: PUBLIC_IP should be equal to the Elastic IP Address that you created above.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ nano config.sh&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Switch to root&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ sudo su -&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Update Ubuntu and install OpenVPN. Note: you will be prompted twice and when you do, select &lt;code&gt;Keep the local version currently installed&lt;/code&gt;&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ /home/ubuntu/openvpn-server-vagrant/ubuntu.sh&#10;$ /home/ubuntu/openvpn-server-vagrant/openvpn.sh&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;At this point, the OpenVPN Server is running.&lt;/p&gt;&#10;&lt;h3 id="step-6--add-the-route"&gt;Step 6 — Add the Route&lt;/h3&gt;&#10;&lt;p&gt;Routes must be added to the server so that your team’s clients know which traffic to route to the VPN Server.&lt;/p&gt;&#10;&lt;p&gt;You can determine the proper subnet by returning to your list of EC2 instances, clicking on a target instance and identifying the Private IP.&lt;/p&gt;&#10;&lt;div style="text-align: center;"&gt;&#10; &lt;figure class="image"&gt;&#10; &#10; &lt;img src="https://redgeoff.com/posts/vpn/identify-private-ip.png" alt="Identify Private IP" /&gt;&#10; &#10; &#10; &lt;/figure&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;Your network will be the first 2 parts of the Private IP appended with zeros, e.g. 172.31.0.0&lt;/p&gt;&#10;&lt;p&gt;On the VPN Server edit &lt;em&gt;/etc/openvpn/server.conf&lt;/em&gt; and add something like the following:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;push &amp;#34;route 172.31.0.0 255.255.0.0&amp;#34;&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then restart the VPN Server with:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ systemctl restart openvpn@server&#10;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="step-7-grant-access-to-your-vpn"&gt;Step 7— Grant Access to Your VPN&lt;/h3&gt;&#10;&lt;p&gt;Note: We assume that you are still SSH’d into the VPN and logged in as root.&lt;/p&gt;&#10;&lt;p&gt;Run the following command and be sure to replace &lt;em&gt;client&lt;/em&gt; below with a unique name for your user/client.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ /home/ubuntu/openvpn-server-vagrant/add-client.sh client&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You’ll then find a configuration file at&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;~/client-configs/files/client-name.ovpn&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You will want to provide this file to the individual on your team who will be connecting to your VPN. SCP is handy for downloading this .ovpn file from your VPN Server.&lt;/p&gt;&#10;&lt;p&gt;Your team can use one of various VPN clients such as &lt;a href="https://tunnelblick.net/"&gt;Tunnelblick&lt;/a&gt; (OS X) and &lt;a href="https://openvpn.net/community-downloads/"&gt;OpenVPN&lt;/a&gt; (Linux, iOS, Android and Windows). After installing one of these clients they should be able to set up the VPN config just by double clicking on the .ovpn file.&lt;/p&gt;&#10;&lt;p&gt;Note: once connected to the VPN, your users will want to use the Private IPs of your EC2 instances. You’ll probably want to use Route 53 to create subdomain records that route to the Private IPs.&lt;/p&gt;&#10;&lt;h3 id="step-8-revoke-access-to-your-vpn"&gt;Step 8— Revoke Access to Your VPN&lt;/h3&gt;&#10;&lt;p&gt;Note: We assume that you are still SSH’d into the VPN and logged in as root.&lt;/p&gt;&#10;&lt;p&gt;Run the following command and be sure to replace &lt;em&gt;client&lt;/em&gt; below with a unique name for your user/client.&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;$ /home/ubuntu/openvpn-server-vagrant/revoke-full.sh client&#10;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="troubleshooting"&gt;Troubleshooting&lt;/h3&gt;&#10;&lt;p&gt;If your VPN client reports a &lt;em&gt;TLS handshake failed&lt;/em&gt; error then this is most likely because your VPN security group (Step 1) is incorrect. Make sure that you have the correct ports and protocols specified — a common problem is not specifying UDP for port 1194.&lt;/p&gt;&#10;</description></item></channel></rss>